首页 > 解决方案 > 如何在 RTP 标头中添加附加值/参数

问题描述

基本上,我在 VOIP 应用程序中工作并尝试使用 WebRTC 构建应用程序。我已经知道有关 RTP 标头的完整实现和详细信息,其中包含诸如

 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 标头中添加其他参数,以便可以将其发送到另一个 PEER。另外,请告诉我如何添加信息和更新 12 字节的 RTP 标头。

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

如何在 WEBRTC 中插入带有 RTP 标头的附加值/参数?

标签: c++webrtcvoip

解决方案


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

关于 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 的评论或在 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.

推荐阅读