| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/tmmb_item.h" | 11 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/tmmb_item.h" |
| 12 | 12 |
| 13 #include "webrtc/base/checks.h" | 13 #include "webrtc/base/checks.h" |
| 14 #include "webrtc/base/logging.h" |
| 14 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" | 15 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" |
| 15 | 16 |
| 16 namespace webrtc { | 17 namespace webrtc { |
| 17 namespace rtcp { | 18 namespace rtcp { |
| 18 TmmbItem::TmmbItem(uint32_t ssrc, uint32_t bitrate_bps, uint16_t overhead) | 19 TmmbItem::TmmbItem(uint32_t ssrc, uint64_t bitrate_bps, uint16_t overhead) |
| 19 : ssrc_(ssrc), bitrate_bps_(bitrate_bps), packet_overhead_(overhead) { | 20 : ssrc_(ssrc), bitrate_bps_(bitrate_bps), packet_overhead_(overhead) { |
| 20 RTC_DCHECK_LE(overhead, 0x1ffu); | 21 RTC_DCHECK_LE(overhead, 0x1ffu); |
| 21 } | 22 } |
| 22 | 23 |
| 23 // 0 1 2 3 | 24 // 0 1 2 3 |
| 24 // 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 | 25 // 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 |
| 25 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 26 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 26 // 0 | SSRC | | 27 // 0 | SSRC | |
| 27 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 28 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 28 // 4 | MxTBR Exp | MxTBR Mantissa |Measured Overhead| | 29 // 4 | MxTBR Exp | MxTBR Mantissa |Measured Overhead| |
| 29 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 30 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 30 void TmmbItem::Parse(const uint8_t* buffer) { | 31 bool TmmbItem::Parse(const uint8_t* buffer) { |
| 31 ssrc_ = ByteReader<uint32_t>::ReadBigEndian(&buffer[0]); | 32 ssrc_ = ByteReader<uint32_t>::ReadBigEndian(&buffer[0]); |
| 32 // Read 4 bytes into 1 block. | 33 // Read 4 bytes into 1 block. |
| 33 uint32_t compact = ByteReader<uint32_t>::ReadBigEndian(&buffer[4]); | 34 uint32_t compact = ByteReader<uint32_t>::ReadBigEndian(&buffer[4]); |
| 34 // Split 1 block into 3 components. | 35 // Split 1 block into 3 components. |
| 35 uint8_t exponent = compact >> 26; // 6 bits. | 36 uint8_t exponent = compact >> 26; // 6 bits. |
| 36 uint32_t mantissa = (compact >> 9) & 0x1ffff; // 17 bits. | 37 uint64_t mantissa = (compact >> 9) & 0x1ffff; // 17 bits. |
| 37 uint16_t overhead = compact & 0x1ff; // 9 bits. | 38 uint16_t overhead = compact & 0x1ff; // 9 bits. |
| 38 // Combine 3 components into 2 values. | 39 // Combine 3 components into 2 values. |
| 39 bitrate_bps_ = (mantissa << exponent); | 40 bitrate_bps_ = (mantissa << exponent); |
| 41 |
| 42 bool shift_overflow = (bitrate_bps_ >> exponent) != mantissa; |
| 43 if (shift_overflow) { |
| 44 LOG(LS_ERROR) << "Invalid tmmb bitrate value : " << mantissa |
| 45 << "*2^" << static_cast<int>(exponent); |
| 46 return false; |
| 47 } |
| 40 packet_overhead_ = overhead; | 48 packet_overhead_ = overhead; |
| 49 return true; |
| 41 } | 50 } |
| 42 | 51 |
| 43 void TmmbItem::Create(uint8_t* buffer) const { | 52 void TmmbItem::Create(uint8_t* buffer) const { |
| 44 const uint32_t kMaxMantissa = 0x1ffff; // 17 bits. | 53 constexpr uint64_t kMaxMantissa = 0x1ffff; // 17 bits. |
| 45 uint32_t mantissa = bitrate_bps_; | 54 uint64_t mantissa = bitrate_bps_; |
| 46 uint32_t exponent = 0; | 55 uint32_t exponent = 0; |
| 47 while (mantissa > kMaxMantissa) { | 56 while (mantissa > kMaxMantissa) { |
| 48 mantissa >>= 1; | 57 mantissa >>= 1; |
| 49 ++exponent; | 58 ++exponent; |
| 50 } | 59 } |
| 51 | 60 |
| 52 ByteWriter<uint32_t>::WriteBigEndian(&buffer[0], ssrc_); | 61 ByteWriter<uint32_t>::WriteBigEndian(&buffer[0], ssrc_); |
| 53 uint32_t compact = (exponent << 26) | (mantissa << 9) | packet_overhead_; | 62 uint32_t compact = (exponent << 26) | (mantissa << 9) | packet_overhead_; |
| 54 ByteWriter<uint32_t>::WriteBigEndian(&buffer[4], compact); | 63 ByteWriter<uint32_t>::WriteBigEndian(&buffer[4], compact); |
| 55 } | 64 } |
| 56 | 65 |
| 57 void TmmbItem::set_packet_overhead(uint16_t overhead) { | 66 void TmmbItem::set_packet_overhead(uint16_t overhead) { |
| 58 RTC_DCHECK_LE(overhead, 0x1ffu); | 67 RTC_DCHECK_LE(overhead, 0x1ffu); |
| 59 packet_overhead_ = overhead; | 68 packet_overhead_ = overhead; |
| 60 } | 69 } |
| 61 } // namespace rtcp | 70 } // namespace rtcp |
| 62 } // namespace webrtc | 71 } // namespace webrtc |
| OLD | NEW |