Skip to content

Commit

Permalink
v3c: Divide code into functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tampsa committed Jan 12, 2024
1 parent bfe220b commit 822f595
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 90 deletions.
117 changes: 45 additions & 72 deletions examples/v3c_receiver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ void ad_receive_hook(void* arg, uvgrtp::frame::rtp_frame* frame);
void ovd_receive_hook(void* arg, uvgrtp::frame::rtp_frame* frame);
void gvd_receive_hook(void* arg, uvgrtp::frame::rtp_frame* frame);
void avd_receive_hook(void* arg, uvgrtp::frame::rtp_frame* frame);
void pvd_receive_hook(void* arg, uvgrtp::frame::rtp_frame* frame);
void cad_receive_hook(void* arg, uvgrtp::frame::rtp_frame* frame);

void copy_rtp_payload(uint64_t max_size, v3c_unit_info& unit, uvgrtp::frame::rtp_frame* frame);
void copy_rtp_payload(std::vector<v3c_unit_info> &units, uint64_t max_size, uvgrtp::frame::rtp_frame* frame);
uint64_t vps_count;

constexpr int VPS_NALS = 1;
Expand Down Expand Up @@ -69,8 +67,6 @@ int main(void)
uvgrtp::media_stream* ovd = sess->create_stream(8895, 8894, RTP_FORMAT_H265, flags);
uvgrtp::media_stream* gvd = sess->create_stream(8897, 8896, RTP_FORMAT_H265, flags);
uvgrtp::media_stream* avd = sess->create_stream(8899, 8898, RTP_FORMAT_H265, flags);
uvgrtp::media_stream* pvd = sess->create_stream(9001, 9000, RTP_FORMAT_H265, flags);
uvgrtp::media_stream* cad = sess->create_stream(9003, 9002, RTP_FORMAT_ATLAS, flags);
avd->configure_ctx(RCC_RING_BUFFER_SIZE, 40*1000*1000);

char* out_buf = new char[len];
Expand Down Expand Up @@ -103,8 +99,6 @@ int main(void)
ovd->install_receive_hook(&mmap.ovd_units, ovd_receive_hook);
gvd->install_receive_hook(&mmap.gvd_units, gvd_receive_hook);
avd->install_receive_hook(&mmap.avd_units, avd_receive_hook);
pvd->install_receive_hook(nullptr, pvd_receive_hook);
cad->install_receive_hook(nullptr, cad_receive_hook);


std::cout << "Waiting incoming packets for " << RECEIVE_TIME_S.count() << " s" << std::endl;
Expand All @@ -131,8 +125,6 @@ int main(void)
sess->destroy_stream(ovd);
sess->destroy_stream(gvd);
sess->destroy_stream(avd);
sess->destroy_stream(pvd);
sess->destroy_stream(cad);

ctx.destroy_session(sess);

Expand All @@ -149,18 +141,47 @@ int main(void)
return EXIT_SUCCESS;
}

void copy_rtp_payload(uint64_t max_size, v3c_unit_info &unit, uvgrtp::frame::rtp_frame *frame)
void copy_rtp_payload(std::vector<v3c_unit_info> &units, uint64_t max_size, uvgrtp::frame::rtp_frame *frame)
{
if (unit.nal_infos.size() <= max_size) {
//std::cout << "Copy info " << std::endl;
memcpy(&unit.buf[unit.ptr], frame->payload, frame->payload_len);

unit.nal_infos.push_back({ unit.ptr, frame->payload_len });
if ((units.end() - 1)->nal_infos.size() == max_size) {
std::cout << "AD size == 35, adding new v3c_unit " << std::endl;
v3c_unit_header hdr = {(units.end()-1)->header.vuh_unit_type};
switch ((units.end()-1)->header.vuh_unit_type) {
case V3C_AD: {
hdr.ad = { (uint8_t)units.size(), 0 };
v3c_unit_info info = { hdr, {}, new char[400 * 1000], 0, false };
units.push_back(info);
break;
}
case V3C_OVD: {
hdr.ovd = { (uint8_t)units.size(), 0 };
v3c_unit_info info = { hdr, {}, new char[400 * 1000], 0, false };
units.push_back(info);
break;
}
case V3C_GVD: {
hdr.gvd = { (uint8_t)units.size(), 0, 0, 0 };
v3c_unit_info info = { hdr, {}, new char[400 * 1000], 0, false };
units.push_back(info);
break;
}
case V3C_AVD: {
hdr.avd = { (uint8_t)units.size(), 0 };
v3c_unit_info info = { hdr, {}, new char[40 * 1000 * 1000], 0, false };
units.push_back(info);
break;
}
}
}
auto &current = units.end() - 1;

unit.ptr += frame->payload_len;
if (current->nal_infos.size() <= max_size) {
memcpy(&current->buf[current->ptr], frame->payload, frame->payload_len);
current->nal_infos.push_back({ current->ptr, frame->payload_len });
current->ptr += frame->payload_len;
}
if (unit.nal_infos.size() == max_size) {
unit.ready = true;
if (current->nal_infos.size() == max_size) {
current->ready = true;
}
}

Expand All @@ -181,17 +202,7 @@ void ad_receive_hook(void* arg, uvgrtp::frame::rtp_frame* frame)
{
std::vector<v3c_unit_info>* vec = (std::vector<v3c_unit_info>*)arg;


if ((vec->end()-1)->nal_infos.size() == AD_NALS) {
std::cout << "AD size == 35, adding new v3c_unit " << std::endl;
v3c_unit_header hdr = { V3C_AD };
hdr.ad = { (uint8_t)vec->size(), 0 };
v3c_unit_info info = { hdr, {}, new char[400 * 1000], 0, false};
vec->push_back(info);
}
auto current = vec->end()-1;
// GET THIS NUMBER 35 DYNAMICALLY
copy_rtp_payload(AD_NALS, *current, frame);
copy_rtp_payload(*vec, AD_NALS, frame);

//std::cout << "Done with AD frame, num: " << current->nal_infos.size() << std::endl;

Expand All @@ -202,61 +213,23 @@ void ovd_receive_hook(void* arg, uvgrtp::frame::rtp_frame* frame)
//std::cout << "Received OVD frame, size: " << frame->payload_len << " bytes" << std::endl;
std::vector<v3c_unit_info>* vec = (std::vector<v3c_unit_info>*)arg;

if ((vec->end() - 1)->nal_infos.size() == OVD_NALS) {
std::cout << "OVD size == 35, adding new v3c_unit " << std::endl;
v3c_unit_header hdr = { V3C_OVD};
hdr.ovd = {(uint8_t)vec->size(), 0};
v3c_unit_info info = { hdr, {}, new char[400 * 1000], 0, false };
vec->push_back(info);
}
auto current = vec->end() - 1;
// GET THIS NUMBER 35 DYNAMICALLY
copy_rtp_payload(OVD_NALS, *current, frame);
copy_rtp_payload(*vec, OVD_NALS, frame);
(void)uvgrtp::frame::dealloc_frame(frame);
}
void gvd_receive_hook(void* arg, uvgrtp::frame::rtp_frame* frame)
{
//std::cout << "Received GVD frame, size: " << frame->payload_len << " bytes" << std::endl;
std::vector<v3c_unit_info>* vec = (std::vector<v3c_unit_info>*)arg;

if ((vec->end() - 1)->nal_infos.size() == GVD_NALS) {
std::cout << "GVD size == 131, adding new v3c_unit " << std::endl;
v3c_unit_header hdr = { V3C_GVD };
hdr.gvd = { (uint8_t)vec->size(), 0, 0, 0 };
v3c_unit_info info = { hdr, {}, new char[400 * 1000], 0, false };
vec->push_back(info);
}
auto current = vec->end() - 1;
// GET THIS NUMBER 35 DYNAMICALLY
copy_rtp_payload(GVD_NALS, *current, frame);
copy_rtp_payload(*vec, GVD_NALS, frame);

(void)uvgrtp::frame::dealloc_frame(frame);
}
void avd_receive_hook(void* arg, uvgrtp::frame::rtp_frame* frame)
{
//std::cout << "Received AVD frame, size: " << frame->payload_len << " bytes" << std::endl;
std::vector<v3c_unit_info>* vec = (std::vector<v3c_unit_info>*)arg;

if ((vec->end() - 1)->nal_infos.size() == AVD_NALS) {
std::cout << "AVD size == 131, adding new v3c_unit " << std::endl;
v3c_unit_header hdr = { V3C_AVD };
hdr.avd = { (uint8_t)vec->size(), 0 };
v3c_unit_info info = { hdr, {}, new char[40 * 1000 * 1000], 0, false };
vec->push_back(info);
}
auto current = vec->end() - 1;
// GET THIS NUMBER 35 DYNAMICALLY
copy_rtp_payload(AVD_NALS, *current, frame);
(void)uvgrtp::frame::dealloc_frame(frame);
}
void pvd_receive_hook(void* arg, uvgrtp::frame::rtp_frame* frame)
{
//std::cout << "Received PVD frame, size: " << frame->payload_len << " bytes" << std::endl;

(void)uvgrtp::frame::dealloc_frame(frame);
}
void cad_receive_hook(void* arg, uvgrtp::frame::rtp_frame* frame)
{
//std::cout << "Received CAD frame, size: " << frame->payload_len << " bytes" << std::endl;


copy_rtp_payload(*vec, AVD_NALS, frame);
(void)uvgrtp::frame::dealloc_frame(frame);
}
18 changes: 0 additions & 18 deletions examples/v3c_sender.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ int main(void)
uvgrtp::media_stream* ovd = sess->create_stream(8894, 8895, RTP_FORMAT_H265, flags);
uvgrtp::media_stream* gvd = sess->create_stream(8896, 8897, RTP_FORMAT_H265, flags);
uvgrtp::media_stream* avd = sess->create_stream(8898, 8899, RTP_FORMAT_H265, flags);
uvgrtp::media_stream* pvd = sess->create_stream(9000, 9001, RTP_FORMAT_H265, flags);
uvgrtp::media_stream* cad = sess->create_stream(9002, 9003, RTP_FORMAT_ATLAS, flags);


uint64_t send_ptr = 0;
Expand All @@ -78,12 +76,6 @@ int main(void)
std::unique_ptr<std::thread> avd_thread =
std::unique_ptr<std::thread>(new std::thread(sender_func, avd, cbuf, mmap.avd_units, RTP_NO_H26X_SCL, V3C_AVD));

std::unique_ptr<std::thread> pvd_thread =
std::unique_ptr<std::thread>(new std::thread(sender_func, pvd, cbuf, mmap.pvd_units, RTP_NO_H26X_SCL, V3C_PVD));

std::unique_ptr<std::thread> cad_thread =
std::unique_ptr<std::thread>(new std::thread(sender_func, cad, cbuf, mmap.cad_units, RTP_NO_FLAGS, V3C_CAD));

if (vps_thread && vps_thread->joinable())
{
vps_thread->join();
Expand All @@ -104,22 +96,12 @@ int main(void)
{
avd_thread->join();
}
if (pvd_thread && pvd_thread->joinable())
{
pvd_thread->join();
}
if (cad_thread && cad_thread->joinable())
{
cad_thread->join();
}

sess->destroy_stream(vps);
sess->destroy_stream(ad);
sess->destroy_stream(ovd);
sess->destroy_stream(gvd);
sess->destroy_stream(avd);
sess->destroy_stream(pvd);
sess->destroy_stream(cad);


std::cout << "Sending finished" << std::endl;
Expand Down

0 comments on commit 822f595

Please sign in to comment.