OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/remb.h" |
| 12 |
| 13 #include "webrtc/base/logging.h" |
| 14 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" |
| 15 |
| 16 using webrtc::RTCPUtility::PT_PSFB; |
| 17 using webrtc::RTCPUtility::RTCPPacketPSFBAPP; |
| 18 using webrtc::RTCPUtility::RTCPPacketPSFBREMBItem; |
| 19 |
| 20 namespace webrtc { |
| 21 namespace rtcp { |
| 22 namespace { |
| 23 const uint32_t kUnusedMediaSourceSsrc0 = 0; |
| 24 |
| 25 void AssignUWord8(uint8_t* buffer, size_t* offset, uint8_t value) { |
| 26 buffer[(*offset)++] = value; |
| 27 } |
| 28 |
| 29 void AssignUWord32(uint8_t* buffer, size_t* offset, uint32_t value) { |
| 30 ByteWriter<uint32_t>::WriteBigEndian(buffer + *offset, value); |
| 31 *offset += 4; |
| 32 } |
| 33 |
| 34 void ComputeMantissaAnd6bitBase2Exponent(uint32_t input_base10, |
| 35 uint8_t bits_mantissa, |
| 36 uint32_t* mantissa, |
| 37 uint8_t* exp) { |
| 38 // input_base10 = mantissa * 2^exp |
| 39 assert(bits_mantissa <= 32); |
| 40 uint32_t mantissa_max = (1 << bits_mantissa) - 1; |
| 41 uint8_t exponent = 0; |
| 42 for (uint32_t i = 0; i < 64; ++i) { |
| 43 if (input_base10 <= (mantissa_max << i)) { |
| 44 exponent = i; |
| 45 break; |
| 46 } |
| 47 } |
| 48 *exp = exponent; |
| 49 *mantissa = (input_base10 >> exponent); |
| 50 } |
| 51 |
| 52 // Receiver Estimated Max Bitrate (REMB) (draft-alvestrand-rmcat-remb). |
| 53 // |
| 54 // 0 1 2 3 |
| 55 // 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 |
| 56 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 57 // |V=2|P| FMT=15 | PT=206 | length | |
| 58 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 59 // | SSRC of packet sender | |
| 60 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 61 // | SSRC of media source | |
| 62 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 63 // | Unique identifier 'R' 'E' 'M' 'B' | |
| 64 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 65 // | Num SSRC | BR Exp | BR Mantissa | |
| 66 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 67 // | SSRC feedback | |
| 68 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 69 // | ... | |
| 70 void CreateRemb(const RTCPPacketPSFBAPP& remb, |
| 71 const RTCPPacketPSFBREMBItem& remb_item, |
| 72 uint8_t* buffer, |
| 73 size_t* pos) { |
| 74 uint32_t mantissa = 0; |
| 75 uint8_t exp = 0; |
| 76 ComputeMantissaAnd6bitBase2Exponent(remb_item.BitRate, 18, &mantissa, &exp); |
| 77 |
| 78 AssignUWord32(buffer, pos, remb.SenderSSRC); |
| 79 AssignUWord32(buffer, pos, kUnusedMediaSourceSsrc0); |
| 80 AssignUWord8(buffer, pos, 'R'); |
| 81 AssignUWord8(buffer, pos, 'E'); |
| 82 AssignUWord8(buffer, pos, 'M'); |
| 83 AssignUWord8(buffer, pos, 'B'); |
| 84 AssignUWord8(buffer, pos, remb_item.NumberOfSSRCs); |
| 85 AssignUWord8(buffer, pos, (exp << 2) + ((mantissa >> 16) & 0x03)); |
| 86 AssignUWord8(buffer, pos, mantissa >> 8); |
| 87 AssignUWord8(buffer, pos, mantissa); |
| 88 for (uint8_t i = 0; i < remb_item.NumberOfSSRCs; ++i) { |
| 89 AssignUWord32(buffer, pos, remb_item.SSRCs[i]); |
| 90 } |
| 91 } |
| 92 } // namespace |
| 93 |
| 94 bool Remb::Create(uint8_t* packet, |
| 95 size_t* index, |
| 96 size_t max_length, |
| 97 RtcpPacket::PacketReadyCallback* callback) const { |
| 98 while (*index + BlockLength() > max_length) { |
| 99 if (!OnBufferFull(packet, index, callback)) |
| 100 return false; |
| 101 } |
| 102 const uint8_t kFmt = 15; |
| 103 CreateHeader(kFmt, PT_PSFB, HeaderLength(), packet, index); |
| 104 CreateRemb(remb_, remb_item_, packet, index); |
| 105 return true; |
| 106 } |
| 107 |
| 108 void Remb::AppliesTo(uint32_t ssrc) { |
| 109 if (remb_item_.NumberOfSSRCs >= kMaxNumberOfSsrcs) { |
| 110 LOG(LS_WARNING) << "Max number of REMB feedback SSRCs reached."; |
| 111 return; |
| 112 } |
| 113 remb_item_.SSRCs[remb_item_.NumberOfSSRCs++] = ssrc; |
| 114 } |
| 115 |
| 116 } // namespace rtcp |
| 117 } // namespace webrtc |
OLD | NEW |