Skip to content

Commit

Permalink
Add xr targetbitrate process and update js demo sdk (ZLMediaKit#4031)
Browse files Browse the repository at this point in the history
rtc 增加对于rtcp xr target bitrate 的解析,参照
https://webrtc.googlesource.com/src/+/refs/heads/main/modules/rtp_rtcp/source/rtcp_packet/target_bitrate.cc

zlmrtcclient.js 更新,修复推屏幕流失败的问题
  • Loading branch information
xiongguangjie authored Nov 21, 2024
1 parent 226b87a commit 8868320
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 20 deletions.
72 changes: 71 additions & 1 deletion src/Rtcp/Rtcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,13 @@ void RtcpHeader::net2Host(size_t len) {
RtcpXRDLRR *dlrr = (RtcpXRDLRR *)this;
dlrr->net2Host(len);
TraceL << dlrr->dumpString();
} else if (xr->bt == 42){
//当有浏览器将屏幕推流到服务器时会发生这个, 暂时没发现什么作用,先解析出来,不做处理
RtcpXRTargetBitrate* tb = (RtcpXRTargetBitrate *)this;
tb->net2Host(len);
//TraceL << tb->dumpString();
} else {
throw std::runtime_error(StrPrinter << "rtcp xr bt " << xr->bt << " not support");
throw std::runtime_error(StrPrinter << "rtcp xr bt " << (int)xr->bt << " not support");
}
break;
}
Expand Down Expand Up @@ -796,6 +801,71 @@ std::shared_ptr<RtcpXRDLRR> RtcpXRDLRR::create(size_t item_count) {
return std::shared_ptr<RtcpXRDLRR>(ptr, [](RtcpXRDLRR *ptr) { delete[](char *) ptr; });
}

////////////////////////////////////
string RtcpXRTargetBitrateItem::dumpString() const {
_StrPrinter printer;

printer << "Spatial Layer :" << spatial_layer << "\r\n";
printer << "Temporal Layer :" << temporal_layer << "\r\n";
printer << "Target Bitrate: " << target_bitrate << "\r\n";

return std::move(printer);
}

void RtcpXRTargetBitrateItem::net2Host() {

target_bitrate = ntohl(target_bitrate) >> 8;
}

std::vector<RtcpXRTargetBitrateItem *> RtcpXRTargetBitrate::getItemList() {
auto count = block_length;
RtcpXRTargetBitrateItem *ptr = &items;
vector<RtcpXRTargetBitrateItem *> ret;
for (int i = 0; i < (int)count; ++i) {
ret.emplace_back(ptr);
++ptr;
}
return ret;
}
string RtcpXRTargetBitrate::dumpString() const {
_StrPrinter printer;
printer << RtcpHeader::dumpHeader();
printer << "ssrc :" << ssrc << "\r\n";
printer << "bt :" << (int)bt << "\r\n";
printer << "block_length : " << block_length << "\r\n";
auto items_list = ((RtcpXRTargetBitrate *)this)->getItemList();
auto i = 0;
for (auto &item : items_list) {
printer << "---- item:" << i++ << " ----\r\n";
printer << item->dumpString();
}
return std::move(printer);
}

void RtcpXRTargetBitrate::net2Host(size_t size) {
static const size_t kMinSize = sizeof(RtcpHeader);
CHECK_MIN_SIZE(size, kMinSize);

ssrc = ntohl(ssrc);
block_length = ntohs(block_length);

auto count = block_length;
for (int i = 0; i < (int)count; ++i) {
RtcpXRTargetBitrateItem *ptr = &items;
ptr->net2Host();
ptr++;
}
}

std::shared_ptr<RtcpXRTargetBitrate> RtcpXRTargetBitrate::create(size_t item_count) {
auto real_size = sizeof(RtcpXRTargetBitrate) - sizeof(RtcpXRTargetBitrateItem) + item_count * sizeof(RtcpXRTargetBitrateItem);
auto bytes = alignSize(real_size);
auto ptr = (RtcpXRTargetBitrate *)new char[bytes];
setupHeader(ptr, RtcpType::RTCP_XR, 0, bytes);
setupPadding(ptr, bytes - real_size);
return std::shared_ptr<RtcpXRTargetBitrate>(ptr, [](RtcpXRTargetBitrate *ptr) { delete[](char *) ptr; });
}

#if 0
#include "Util/onceToken.h"

Expand Down
100 changes: 99 additions & 1 deletion src/Rtcp/Rtcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,6 @@ class RtcpXRDLRRReportItem {

/**
* 网络字节序转换为主机字节序
* @param size 字节长度,防止内存越界
*/
void net2Host();
};
Expand Down Expand Up @@ -814,6 +813,105 @@ class RtcpXRDLRR : public RtcpHeader {

};

// RFC 4585: Feedback format.
//
// Common packet format:
//
// 0 1 2 3
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | BT=42 | reserved | block length |
// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
//
// Target bitrate item (repeat as many times as necessary).
//
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | S | T | Target Bitrate |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// : ... :
//
// Spatial Layer (S): 4 bits
// Indicates which temporal layer this bitrate concerns.
//
// Temporal Layer (T): 4 bits
// Indicates which temporal layer this bitrate concerns.
//
// Target Bitrate: 24 bits
// The encoder target bitrate for this layer, in kbps.
//
// As an example of how S and T are intended to be used, VP8 simulcast will
// use a separate TargetBitrate message per stream, since they are transmitted
// on separate SSRCs, with temporal layers grouped by stream.
// If VP9 SVC is used, there will be only one SSRC, so each spatial and
// temporal layer combo used shall be specified in the TargetBitrate packet.
class RtcpXRTargetBitrateItem {
public:
friend class RtcpXRTargetBitrate;
#if __BYTE_ORDER == __BIG_ENDIAN
// Indicates which temporal layer this bitrate concerns.
uint32_t spatial_layer : 4;
// Indicates which temporal layer this bitrate concerns.
uint32_t temporal_layer : 4;
#else
// Indicates which temporal layer this bitrate concerns.
uint32_t temporal_layer : 4;
// Indicates which temporal layer this bitrate concerns.
uint32_t spatial_layer : 4;
#endif
//The encoder target bitrate for this layer, in kbps.
uint32_t target_bitrate : 24;

private:
/**
* 打印字段详情
* 使用net2Host转换成主机字节序后才可使用此函数
*/
std::string dumpString() const;

/**
* 网络字节序转换为主机字节序
*/
void net2Host();
};


class RtcpXRTargetBitrate : public RtcpHeader {
public:
friend class RtcpHeader;
uint32_t ssrc;
uint8_t bt;
uint8_t reserved;
uint16_t block_length;
RtcpXRTargetBitrateItem items;

/**
* 创建RtcpXRTargetBitrate包,只赋值了RtcpHeader部分(网络字节序)
* @param item_count RtcpXRTargetBitrateItem对象个数
* @return RtcpXRTargetBitrate包
*/
static std::shared_ptr<RtcpXRTargetBitrate> create(size_t item_count);

/**
* 获取RtcpXRTargetBitrateItem对象指针列表
* 使用net2Host转换成主机字节序后才可使用此函数
*/
std::vector<RtcpXRTargetBitrateItem *> getItemList();

private:
/**
* 打印字段详情
* 使用net2Host转换成主机字节序后才可使用此函数
*/
std::string dumpString() const;

/**
* 网络字节序转换为主机字节序
* @param size 字节长度,防止内存越界
*/
void net2Host(size_t size);

};

#pragma pack(pop)

} // namespace mediakit
Expand Down
19 changes: 11 additions & 8 deletions www/ZLMRTCClient.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion www/ZLMRTCClient.js.map

Large diffs are not rendered by default.

19 changes: 11 additions & 8 deletions www/webrtc/ZLMRTCClient.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion www/webrtc/ZLMRTCClient.js.map

Large diffs are not rendered by default.

0 comments on commit 8868320

Please sign in to comment.