Skip to content

Commit

Permalink
v3c: Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tampsa committed Jan 12, 2024
1 parent 3efbd09 commit d681961
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 119 deletions.
44 changes: 8 additions & 36 deletions examples/v3c_receiver.cc
Original file line number Diff line number Diff line change
@@ -1,34 +1,13 @@

#include <uvgrtp/lib.hh>

#include <thread>
#include <iostream>
#include <fstream>

/* There are two main ways of getting received RTP frames from uvgRTP.
* This example demonstrates the usage of hook function to receive RTP frames.
*
* The advantage of using a hook function is minimal CPU usage and delay between
* uvgRTP receiving the frame and application processing the frame. When using
* the hook method, the application must take care that it is not using the hook
* function for heavy processing since this may block RTP frame reception.
*
* Hook based frame reception is generally recommended for most serious applications,
* but there can be situations where polling method is better, especially if performance
* is not a huge concern or if there needs to be tight control when the frame is
* received by the application.
*
* This example only implements the receiving, but it can be used together with the
* sending example to test the functionality.
*/

// parameters for this test. You can change these to suit your network environment
constexpr uint16_t LOCAL_PORT = 8890;

constexpr char LOCAL_ADDRESS[] = "127.0.0.1";

// This example runs for 5 seconds
constexpr auto RECEIVE_TIME_S = std::chrono::seconds(5);
constexpr auto RECEIVE_TIME_S = std::chrono::seconds(10);

void vps_receive_hook(void* arg, uvgrtp::frame::rtp_frame* frame);
void ad_receive_hook(void* arg, uvgrtp::frame::rtp_frame* frame);
Expand All @@ -43,6 +22,8 @@ constexpr int AD_NALS = 35;
constexpr int OVD_NALS = 35;
constexpr int GVD_NALS = 131;
constexpr int AVD_NALS = 131;
constexpr int EXPECTED_GOPS = 1;

std::string PATH = "C:\\Users\\ngheta\\Documents\\v3c_test_seq_2.vpcc";

int main(void)
Expand Down Expand Up @@ -77,16 +58,14 @@ int main(void)
uint64_t bytes = 0;
uint64_t ptr = 0;
bool hdb = true;
char* out_buf = new char[len];
char* out_buf = nullptr;

while (ngops <= 1) {
while (ngops <= EXPECTED_GOPS) {
if (is_gop_ready(ngops, mmap)) {
std::cout << "Full GoP received, num: " << ngops << std::endl;
bytes += reconstruct_v3c_gop(hdb, out_buf, ptr, mmap, ngops - 1);
std::cout << "GoP size " << bytes << std::endl;

std::cout << "Full GoP received, num: " << ngops << std::endl;
ngops++;
hdb = false;
hdb = false; // Only add the V3C Sample Stream header byte to only the first GoP
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
Expand All @@ -108,16 +87,13 @@ int main(void)
break;
}
}

std::cout << "DONE " << std::endl;
std::cout << "Done " << std::endl;

return EXIT_SUCCESS;
}

void vps_receive_hook(void* arg, uvgrtp::frame::rtp_frame* frame)
{
std::cout << "Received VPS frame, size: " << frame->payload_len << " bytes" << std::endl;

std::vector<v3c_unit_info>* vec = (std::vector<v3c_unit_info>*)arg;

char* cbuf = new char[frame->payload_len];
Expand All @@ -130,28 +106,24 @@ void vps_receive_hook(void* arg, uvgrtp::frame::rtp_frame* frame)
void ad_receive_hook(void* arg, uvgrtp::frame::rtp_frame* frame)
{
std::vector<v3c_unit_info>* vec = (std::vector<v3c_unit_info>*)arg;

copy_rtp_payload(*vec, AD_NALS, frame);
(void)uvgrtp::frame::dealloc_frame(frame);
}
void ovd_receive_hook(void* arg, uvgrtp::frame::rtp_frame* frame)
{
std::vector<v3c_unit_info>* vec = (std::vector<v3c_unit_info>*)arg;

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::vector<v3c_unit_info>* vec = (std::vector<v3c_unit_info>*)arg;
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::vector<v3c_unit_info>* vec = (std::vector<v3c_unit_info>*)arg;

copy_rtp_payload(*vec, AVD_NALS, frame);
(void)uvgrtp::frame::dealloc_frame(frame);
}
33 changes: 17 additions & 16 deletions examples/v3c_sender.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,35 @@
#include <cstring>
#include <vector>
#include <string>
#include <atomic>

constexpr char REMOTE_ADDRESS[] = "127.0.0.1";
constexpr uint16_t REMOTE_PORT = 8890;

// the parameters of demostration
constexpr size_t PAYLOAD_LEN = 100;
constexpr int AMOUNT_OF_TEST_PACKETS = 100;
constexpr auto END_WAIT = std::chrono::seconds(5);

//std::string PATH = "C:\\Users\\ngheta\\Documents\\TMIV_A3_C_QP3.bit";
std::string PATH = "C:\\Users\\ngheta\\Documents\\v3c_test_seq_2.vpcc";

void sender_func(uvgrtp::media_stream* stream, const char* cbuf, const std::vector<v3c_unit_info> &units, rtp_flags_t flags, int fmt);

std::atomic<uint64_t> bytes_sent;
int main(void)
{
std::cout << "Parsing V3C file" << std::endl;

/* A V3C Sample stream is divided into 6 types of 'sub-bitstreams' + parameters.
- The nal_map holds nal_info structs
/* A V3C Sample stream is divided into 4 types of 'sub-bitstreams' + parameters.
- In v3c_file_map there are vectors of
- V3C unit infos
- v3c_unit_infos
- header
- nal_infos
- buf
- ptr
- ready
- The nal_infos holds nal_info structs
- nal_info struct holds the format(Atlas, H264, H265, H266), start position and size of the NAL unit
- With this info you can send the data via different uvgRTP media streams. Usually 2 streams, one in RTP_FORMAT_ATLAS for
Atlas NAL units and a second one for the video NAL units in the correct format
- With this info you can send the data via different uvgRTP media streams.
Note: Use RTP_NO_H26X_SCL when sending video frames, as there is no start codes in the video sub-streams */

bytes_sent = 0;
v3c_file_map mmap;

/* Fetch the file and its size */
Expand Down Expand Up @@ -92,8 +95,7 @@ int main(void)
sess->destroy_stream(streams.gvd);
sess->destroy_stream(streams.avd);


std::cout << "Sending finished" << std::endl;
std::cout << "Sending finished, " << bytes_sent << " bytes sent" << std::endl;

if (sess)
{
Expand All @@ -113,8 +115,7 @@ void sender_func(uvgrtp::media_stream* stream, const char* cbuf, const std::vect
if (ret != RTP_OK) {
std::cout << "Failed to send RTP frame!" << std::endl;
}
bytes_sent += i.size;
}
}


}
10 changes: 8 additions & 2 deletions include/uvgrtp/v3c_parser.hh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ enum CODEC {

constexpr int V3C_HDR_LEN = 4; // 32 bits for v3c unit header

// These are signaled to the receiver one way or the other, for example SDP
constexpr uint8_t ATLAS_NAL_SIZE_PRECISION = 2;
constexpr uint8_t VIDEO_NAL_SIZE_PRECISION = 4;
constexpr uint8_t V3C_SIZE_PRECISION = 3;
constexpr int INPUT_BUFFER_SIZE = 40 * 1000 * 1000; // Received NAL units are copied to the input buffer


struct vuh_ad {
uint8_t vuh_v3c_parameter_set_id = 0;
uint8_t vuh_atlas_id = 0;
Expand Down Expand Up @@ -104,7 +111,6 @@ struct v3c_streams {
uint32_t combineBytes(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4);
uint32_t combineBytes(uint8_t byte1, uint8_t byte2, uint8_t byte3);
uint32_t combineBytes(uint8_t byte1, uint8_t byte2);
void convert_size_little_endian(uint32_t in, uint8_t* out, size_t output_size);
void convert_size_big_endian(uint32_t in, uint8_t* out, size_t output_size);

// Get size of a file in bytes
Expand Down Expand Up @@ -132,7 +138,7 @@ void copy_rtp_payload(std::vector<v3c_unit_info>& units, uint64_t max_size, uvgr
void create_v3c_unit(v3c_unit_info& current_unit, char* buf, uint64_t& ptr, uint64_t v3c_precision, uint32_t nal_precision);

// Reconstruct a whole GoP from V3C Units
uint64_t reconstruct_v3c_gop(bool hdr_byte, char* buf, uint64_t& ptr, v3c_file_map& mmap, uint64_t index);
uint64_t reconstruct_v3c_gop(bool hdr_byte, char* &buf, uint64_t& ptr, v3c_file_map& mmap, uint64_t index);

// Check if there is a complete GoP in the memory map
bool is_gop_ready(uint64_t index, v3c_file_map& mmap);
Loading

0 comments on commit d681961

Please sign in to comment.