Warm tip: This article is reproduced from serverfault.com, please click

How to add additional value/parameters in RTP header

发布于 2020-12-08 12:19:49

Basically, I am working in the VOIP application and trying to build an app using the WebRTC. I already know complete implementation and details about the RTP header which contains things like

 1. version
 2. padding
 3. extension
 4. CSRC count
 5. marker
 6. payload type
 7. sequence number
 8. Time Stamp
 9. SSRC
 10. CSRC list

But I want to add additional parameters in the RTP header so that I can send it to another PEER. Also, please do update me how to add information and update the RTP header which is of 12 bytes.

Here are the files from webrtc native stack.

How can I insert additional values/Parameters with RTP header in WEBRTC?

Questioner
Abdul ahad
Viewed
0
fsquirrel 2020-12-10 00:06:26

If you're implementing an RTP packet with additional parameters, you need to put them in the "Extension header". The extension is located right after the default RTP header values. Don't forget to set "profile-specific extension header ID"(your extension id) and "extension header length"(length of extension excludes the extension header). After you add the extension, you need to make sure that the receiver application is familiar with the extension. Otherwise, it will be ignored(in the best case).

Regarding the Google Chromium implementation, I'd recommend diving into the implementation.

Copied from the comment below:

#pragma pack(1) // in order to avoid padding
struct RtpExtension {
    // Use strict types such as uint8_t/int8_t, uint32_t/int32_t, etc
    // to avoid possible compatibility issues between
    // different CPUs
    
    // Extension Header
    uint16_t profile_id;
    uint16_t length;

    // Actual extension values
    uint32_t enery;
};
#pragma pop

Here I assume that you already have a structure for the RTP packet. In case you don't, please refer to Manuel's comment or lookup on the Internet.

#pragma pack(1)
struct RtpHeader {
   // default fields...
   struct RtpExtension extension;
};


// Actual usage
struct RtpHeader h;
// Fill the header with the default values(sequence number, timestamp, whatever) 

// Fill the extension:
// if the value that you want to end is longer than 1 byte,
// don't forget to convert it to the network byte order(htol).
h.extension.energy = htol(some_energy_value);

// length of the extention
// h.extension.length = htons(<length of the extension>);
// In this specific case it can be calculated as:
h.extension.length = htons(sizeof(RtpExtension) - sizeof(uint16_t) - sizoef(uint16_t));

// Make sure that RTP header reflects that it has the extension:
h.x = 1; // x is a bitfield, in your implementation, it may be called differently and set in another way.