blob: 5e3546b04ba561e15b097aadabaf5f4a990120c5 [file] [log] [blame]
danilchap54999d42015-12-16 01:56:20 -08001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/rtp_rtcp/source/rtcp_packet/dlrr.h"
danilchap54999d42015-12-16 01:56:20 -080012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "modules/rtp_rtcp/source/byte_io.h"
14#include "rtc_base/checks.h"
15#include "rtc_base/logging.h"
danilchap54999d42015-12-16 01:56:20 -080016
17namespace webrtc {
18namespace rtcp {
19// DLRR Report Block (RFC 3611).
20//
21// 0 1 2 3
22// 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
23// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
24// | BT=5 | reserved | block length |
25// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
26// | SSRC_1 (SSRC of first receiver) | sub-
27// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ block
28// | last RR (LRR) | 1
29// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
30// | delay since last RR (DLRR) |
31// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
32// | SSRC_2 (SSRC of second receiver) | sub-
33// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ block
34// : ... : 2
sprangb84ad632016-11-01 02:50:12 -070035
eladalon8fa21c42017-06-16 07:07:47 -070036Dlrr::Dlrr() = default;
37
38Dlrr::Dlrr(const Dlrr& other) = default;
39
40Dlrr::~Dlrr() = default;
41
danilchap54999d42015-12-16 01:56:20 -080042bool Dlrr::Parse(const uint8_t* buffer, uint16_t block_length_32bits) {
43 RTC_DCHECK(buffer[0] == kBlockType);
44 // kReserved = buffer[1];
45 RTC_DCHECK_EQ(block_length_32bits,
46 ByteReader<uint16_t>::ReadBigEndian(&buffer[2]));
47 if (block_length_32bits % 3 != 0) {
48 LOG(LS_WARNING) << "Invalid size for dlrr block.";
49 return false;
50 }
51
52 size_t blocks_count = block_length_32bits / 3;
53 const uint8_t* read_at = buffer + kBlockHeaderLength;
54 sub_blocks_.resize(blocks_count);
Danil Chapovalovd0c7bba2016-01-26 14:12:46 +010055 for (ReceiveTimeInfo& sub_block : sub_blocks_) {
danilchap54999d42015-12-16 01:56:20 -080056 sub_block.ssrc = ByteReader<uint32_t>::ReadBigEndian(&read_at[0]);
57 sub_block.last_rr = ByteReader<uint32_t>::ReadBigEndian(&read_at[4]);
58 sub_block.delay_since_last_rr =
59 ByteReader<uint32_t>::ReadBigEndian(&read_at[8]);
60 read_at += kSubBlockLength;
61 }
62 return true;
63}
64
65size_t Dlrr::BlockLength() const {
66 if (sub_blocks_.empty())
67 return 0;
68 return kBlockHeaderLength + kSubBlockLength * sub_blocks_.size();
69}
70
71void Dlrr::Create(uint8_t* buffer) const {
72 if (sub_blocks_.empty()) // No subblocks, no need to write header either.
73 return;
74 // Create block header.
75 const uint8_t kReserved = 0;
76 buffer[0] = kBlockType;
77 buffer[1] = kReserved;
78 ByteWriter<uint16_t>::WriteBigEndian(&buffer[2], 3 * sub_blocks_.size());
79 // Create sub blocks.
80 uint8_t* write_at = buffer + kBlockHeaderLength;
Danil Chapovalovd0c7bba2016-01-26 14:12:46 +010081 for (const ReceiveTimeInfo& sub_block : sub_blocks_) {
danilchap54999d42015-12-16 01:56:20 -080082 ByteWriter<uint32_t>::WriteBigEndian(&write_at[0], sub_block.ssrc);
83 ByteWriter<uint32_t>::WriteBigEndian(&write_at[4], sub_block.last_rr);
84 ByteWriter<uint32_t>::WriteBigEndian(&write_at[8],
85 sub_block.delay_since_last_rr);
86 write_at += kSubBlockLength;
87 }
88 RTC_DCHECK_EQ(buffer + BlockLength(), write_at);
89}
90
danilchap54999d42015-12-16 01:56:20 -080091} // namespace rtcp
92} // namespace webrtc