如何在 RTP header 中添加额外的 value/parameters

How to add additional value/parameters in RTP header

基本上,我在 VOIP 应用程序中工作并尝试使用 WebRTC 构建应用程序。我已经知道关于 RTP header 的完整实现和细节,其中包含

 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

但我想在 RTP header 中添加额外的参数,以便我可以将它发送给另一个 PEER。另外,请告诉我如何添加信息和更新 12 字节的 RTP header。

这是来自 webrtc 本机堆栈的文件。

如何在 WEBRTC 中使用 RTP header 插入额外的 values/Parameters?

如果您要实现带有附加参数的 RTP 数据包,则需要将它们放在“Extension header”中。扩展位于默认 RTP header 值之后。不要忘记设置“profile-specific 分机 header ID”(您的分机 ID)和“分机 header 长度”(分机长度不包括分机 header)。 添加扩展后,您需要确保接收方应用程序熟悉该扩展。否则,它将被忽略(在最好的情况下)。

关于 Google Chromium 实施,我建议深入研究实施。

从下面的评论中复制:

#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

这里我假设你已经有了RTP数据包的结构。 如果你不这样做,请参考 Manuel 的评论或在互联网上查找。

#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.