Skip to content

Commit bb5e11a

Browse files
Merge pull request #1500 from Polusummator/fix-remb-ssrc
Fix REMB SSRC
2 parents 01543d5 + 51f74ce commit bb5e11a

5 files changed

Lines changed: 38 additions & 12 deletions

File tree

include/rtc/rtp.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,17 +269,18 @@ struct RTC_CPP_EXPORT RtcpRemb {
269269

270270
char _id[4]; // Unique identifier ('R' 'E' 'M' 'B')
271271
uint32_t _bitrate; // Num SSRC, Br Exp, Br Mantissa (bit mask)
272-
SSRC _ssrc[1];
272+
SSRC _ssrcs[1];
273273

274274
[[nodiscard]] static size_t SizeWithSSRCs(int count);
275275

276276
[[nodiscard]] unsigned int getSize() const;
277277

278278
void preparePacket(SSRC senderSSRC, unsigned int numSSRC, unsigned int in_bitrate);
279279
void setBitrate(unsigned int numSSRC, unsigned int in_bitrate);
280-
void setSsrc(int iterator, SSRC newSssrc);
281-
unsigned int getNumSSRC();
282-
unsigned int getBitrate();
280+
SSRC getSSRC(int num) const;
281+
void setSSRC(int num, SSRC newSsrc);
282+
unsigned int getNumSSRC() const;
283+
unsigned int getBitrate() const;
283284
};
284285

285286
struct RTC_CPP_EXPORT RtcpPli {

src/impl/peerconnection.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,11 +577,27 @@ void PeerConnection::dispatchMedia([[maybe_unused]] message_ptr message) {
577577
break;
578578
}
579579
offset += header->lengthInBytes();
580-
if (header->payloadType() == 205 || header->payloadType() == 206) {
580+
if (header->payloadType() == 205) {
581581
auto rtcpfb = reinterpret_cast<RtcpFbHeader *>(header);
582582
ssrcs.insert(rtcpfb->packetSenderSSRC());
583583
ssrcs.insert(rtcpfb->mediaSourceSSRC());
584-
584+
} else if (header->payloadType() == 206) {
585+
auto rtcpfb = reinterpret_cast<RtcpFbHeader *>(header);
586+
ssrcs.insert(rtcpfb->packetSenderSSRC());
587+
if (header->reportCount() == 15 && header->lengthInBytes() >= sizeof(RtcpRemb)) {
588+
auto remb = reinterpret_cast<RtcpRemb *>(header);
589+
unsigned numSsrc = remb->getNumSSRC();
590+
if (RtcpRemb::SizeWithSSRCs(numSsrc) > message->size() + header->lengthInBytes() - offset) {
591+
continue;
592+
}
593+
if (remb->_id[0] == 'R' && remb->_id[1] == 'E' && remb->_id[2] == 'M' && remb->_id[3] == 'B') {
594+
for (unsigned i = 0; i < numSsrc; i++) {
595+
ssrcs.insert(remb->getSSRC(i));
596+
}
597+
continue;
598+
}
599+
}
600+
ssrcs.insert(rtcpfb->mediaSourceSSRC());
585601
} else if (header->payloadType() == 200) {
586602
auto rtcpsr = reinterpret_cast<RtcpSr *>(header);
587603
ssrcs.insert(rtcpsr->senderSSRC());

src/rembhandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void RembHandler::incoming(message_vector &messages, [[maybe_unused]] const mess
2828
auto header = reinterpret_cast<RtcpHeader *>(message->data() + offset);
2929
uint8_t payload_type = header->payloadType();
3030

31-
if (payload_type == 206 && header->reportCount() == 15 && header->lengthInBytes() == sizeof(RtcpRemb)) {
31+
if (payload_type == 206 && header->reportCount() == 15 && header->lengthInBytes() >= sizeof(RtcpRemb)) {
3232
if (offset + sizeof(RtcpRemb) > message->size())
3333
break;
3434

src/rtcpreceivingsession.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ void RtcpReceivingSession::pushREMB(const message_callback &send, unsigned int b
236236
auto message = make_message(RtcpRemb::SizeWithSSRCs(1), Message::Control);
237237
auto remb = reinterpret_cast<RtcpRemb *>(message->data());
238238
remb->preparePacket(mSsrc, 1, bitrate);
239-
remb->setSsrc(0, mSsrc);
239+
remb->setSSRC(0, mSsrc);
240240
send(message);
241241
}
242242

src/rtp.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -597,16 +597,25 @@ void RtcpRemb::setBitrate(unsigned int numSSRC, unsigned int in_bitrate) {
597597
}
598598

599599
// "length" in packet is one less than the number of 32 bit words in the packet.
600-
header.header.setLength(uint16_t((offsetof(RtcpRemb, _ssrc) / sizeof(uint32_t)) - 1 + numSSRC));
600+
header.header.setLength(uint16_t((offsetof(RtcpRemb, _ssrcs) / sizeof(uint32_t)) - 1 + numSSRC));
601601

602602
_bitrate = htonl((numSSRC << (32u - 8u)) | (exp << (32u - 8u - 6u)) | in_bitrate);
603603
}
604604

605-
void RtcpRemb::setSsrc(int iterator, SSRC newSssrc) { _ssrc[iterator] = htonl(newSssrc); }
605+
SSRC RtcpRemb::getSSRC(int num) const {
606+
if (num < 0 || static_cast<unsigned>(num) >= getNumSSRC())
607+
throw std::out_of_range("SSRC num out of range");
608+
return ntohl(_ssrcs[num]);
609+
}
610+
void RtcpRemb::setSSRC(int num, SSRC newSsrc) {
611+
if (num < 0 || static_cast<unsigned>(num) >= getNumSSRC())
612+
throw std::out_of_range("SSRC num out of range");
613+
_ssrcs[num] = htonl(newSsrc);
614+
}
606615

607-
unsigned int RtcpRemb::getNumSSRC() { return ntohl(_bitrate) >> 24u; }
616+
unsigned int RtcpRemb::getNumSSRC() const { return ntohl(_bitrate) >> 24u; }
608617

609-
unsigned int RtcpRemb::getBitrate() {
618+
unsigned int RtcpRemb::getBitrate() const {
610619
uint32_t br = ntohl(_bitrate);
611620
uint8_t exp = (br << 8u) >> 26u;
612621
return (br & 0x3FFFF) * static_cast<unsigned int>(pow(2, exp));

0 commit comments

Comments
 (0)