tmmb_item.cc (2725B)
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 "modules/rtp_rtcp/source/rtcp_packet/tmmb_item.h" 12 13 #include <cstdint> 14 15 #include "modules/rtp_rtcp/source/byte_io.h" 16 #include "rtc_base/checks.h" 17 #include "rtc_base/logging.h" 18 19 namespace webrtc { 20 namespace rtcp { 21 TmmbItem::TmmbItem(uint32_t ssrc, uint64_t bitrate_bps, uint16_t overhead) 22 : ssrc_(ssrc), bitrate_bps_(bitrate_bps), packet_overhead_(overhead) { 23 RTC_DCHECK_LE(overhead, 0x1ffu); 24 } 25 26 // 0 1 2 3 27 // 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 28 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 29 // 0 | SSRC | 30 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 31 // 4 | MxTBR Exp | MxTBR Mantissa |Measured Overhead| 32 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 33 bool TmmbItem::Parse(const uint8_t* buffer) { 34 ssrc_ = ByteReader<uint32_t>::ReadBigEndian(&buffer[0]); 35 // Read 4 bytes into 1 block. 36 uint32_t compact = ByteReader<uint32_t>::ReadBigEndian(&buffer[4]); 37 // Split 1 block into 3 components. 38 uint8_t exponent = compact >> 26; // 6 bits. 39 uint64_t mantissa = (compact >> 9) & 0x1ffff; // 17 bits. 40 uint16_t overhead = compact & 0x1ff; // 9 bits. 41 // Combine 3 components into 2 values. 42 bitrate_bps_ = (mantissa << exponent); 43 44 bool shift_overflow = (bitrate_bps_ >> exponent) != mantissa; 45 if (shift_overflow) { 46 RTC_LOG(LS_ERROR) << "Invalid tmmb bitrate value : " << mantissa << "*2^" 47 << static_cast<int>(exponent); 48 return false; 49 } 50 packet_overhead_ = overhead; 51 return true; 52 } 53 54 void TmmbItem::Create(uint8_t* buffer) const { 55 constexpr uint64_t kMaxMantissa = 0x1ffff; // 17 bits. 56 uint64_t mantissa = bitrate_bps_; 57 uint32_t exponent = 0; 58 while (mantissa > kMaxMantissa) { 59 mantissa >>= 1; 60 ++exponent; 61 } 62 63 ByteWriter<uint32_t>::WriteBigEndian(&buffer[0], ssrc_); 64 uint32_t compact = (exponent << 26) | (mantissa << 9) | packet_overhead_; 65 ByteWriter<uint32_t>::WriteBigEndian(&buffer[4], compact); 66 } 67 68 void TmmbItem::set_packet_overhead(uint16_t overhead) { 69 RTC_DCHECK_LE(overhead, 0x1ffu); 70 packet_overhead_ = overhead; 71 } 72 } // namespace rtcp 73 } // namespace webrtc