tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

dlrr.cc (3717B)


      1 /*
      2 *  Copyright (c) 2015 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/dlrr.h"
     12 
     13 #include <cstddef>
     14 #include <cstdint>
     15 
     16 #include "modules/rtp_rtcp/source/byte_io.h"
     17 #include "rtc_base/checks.h"
     18 #include "rtc_base/logging.h"
     19 #include "rtc_base/numerics/safe_conversions.h"
     20 
     21 namespace webrtc {
     22 namespace rtcp {
     23 // DLRR Report Block (RFC 3611).
     24 //
     25 //   0                   1                   2                   3
     26 //   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
     27 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
     28 //  |     BT=5      |   reserved    |         block length          |
     29 //  +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
     30 //  |                 SSRC_1 (SSRC of first receiver)               | sub-
     31 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ block
     32 //  |                         last RR (LRR)                         |   1
     33 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
     34 //  |                   delay since last RR (DLRR)                  |
     35 //  +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
     36 //  |                 SSRC_2 (SSRC of second receiver)              | sub-
     37 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ block
     38 //  :                               ...                             :   2
     39 
     40 Dlrr::Dlrr() = default;
     41 
     42 Dlrr::Dlrr(const Dlrr& other) = default;
     43 
     44 Dlrr::~Dlrr() = default;
     45 
     46 bool Dlrr::Parse(const uint8_t* buffer, uint16_t block_length_32bits) {
     47  RTC_DCHECK(buffer[0] == kBlockType);
     48  // kReserved = buffer[1];
     49  RTC_DCHECK_EQ(block_length_32bits,
     50                ByteReader<uint16_t>::ReadBigEndian(&buffer[2]));
     51  if (block_length_32bits % 3 != 0) {
     52    RTC_LOG(LS_WARNING) << "Invalid size for dlrr block.";
     53    return false;
     54  }
     55 
     56  size_t blocks_count = block_length_32bits / 3;
     57  const uint8_t* read_at = buffer + kBlockHeaderLength;
     58  sub_blocks_.resize(blocks_count);
     59  for (ReceiveTimeInfo& sub_block : sub_blocks_) {
     60    sub_block.ssrc = ByteReader<uint32_t>::ReadBigEndian(&read_at[0]);
     61    sub_block.last_rr = ByteReader<uint32_t>::ReadBigEndian(&read_at[4]);
     62    sub_block.delay_since_last_rr =
     63        ByteReader<uint32_t>::ReadBigEndian(&read_at[8]);
     64    read_at += kSubBlockLength;
     65  }
     66  return true;
     67 }
     68 
     69 size_t Dlrr::BlockLength() const {
     70  if (sub_blocks_.empty())
     71    return 0;
     72  return kBlockHeaderLength + kSubBlockLength * sub_blocks_.size();
     73 }
     74 
     75 void Dlrr::Create(uint8_t* buffer) const {
     76  if (sub_blocks_.empty())  // No subblocks, no need to write header either.
     77    return;
     78  // Create block header.
     79  const uint8_t kReserved = 0;
     80  buffer[0] = kBlockType;
     81  buffer[1] = kReserved;
     82  ByteWriter<uint16_t>::WriteBigEndian(
     83      &buffer[2], dchecked_cast<uint16_t>(3 * sub_blocks_.size()));
     84  // Create sub blocks.
     85  uint8_t* write_at = buffer + kBlockHeaderLength;
     86  for (const ReceiveTimeInfo& sub_block : sub_blocks_) {
     87    ByteWriter<uint32_t>::WriteBigEndian(&write_at[0], sub_block.ssrc);
     88    ByteWriter<uint32_t>::WriteBigEndian(&write_at[4], sub_block.last_rr);
     89    ByteWriter<uint32_t>::WriteBigEndian(&write_at[8],
     90                                         sub_block.delay_since_last_rr);
     91    write_at += kSubBlockLength;
     92  }
     93  RTC_DCHECK_EQ(buffer + BlockLength(), write_at);
     94 }
     95 
     96 }  // namespace rtcp
     97 }  // namespace webrtc