File: remb_unittest.cc

package info (click to toggle)
chromium 140.0.7339.127-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,201,772 kB
  • sloc: cpp: 35,093,800; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,798; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,119; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (148 lines) | stat: -rw-r--r-- 4,672 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 *  Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "modules/rtp_rtcp/source/rtcp_packet/remb.h"

#include <cstddef>
#include <cstdint>
#include <cstring>
#include <iterator>
#include <vector>

#include "rtc_base/buffer.h"
#include "test/gmock.h"
#include "test/gtest.h"
#include "test/rtcp_packet_parser.h"

using ::testing::ElementsAreArray;
using ::testing::IsEmpty;
using ::testing::make_tuple;
using webrtc::rtcp::Remb;

namespace webrtc {
namespace {
constexpr uint32_t kSenderSsrc = 0x12345678;
constexpr uint32_t kRemoteSsrcs[] = {0x23456789, 0x2345678a, 0x2345678b};
constexpr uint32_t kBitrateBps = 0x3fb93 * 2;  // 522022;
constexpr int64_t kBitrateBps64bit = int64_t{0x3fb93} << 30;
constexpr uint8_t kPacket[] = {0x8f, 206,  0x00, 0x07, 0x12, 0x34, 0x56, 0x78,
                               0x00, 0x00, 0x00, 0x00, 'R',  'E',  'M',  'B',
                               0x03, 0x07, 0xfb, 0x93, 0x23, 0x45, 0x67, 0x89,
                               0x23, 0x45, 0x67, 0x8a, 0x23, 0x45, 0x67, 0x8b};
constexpr size_t kPacketLength = sizeof(kPacket);
}  // namespace

TEST(RtcpPacketRembTest, Create) {
  Remb remb;
  remb.SetSenderSsrc(kSenderSsrc);
  remb.SetSsrcs(
      std::vector<uint32_t>(std::begin(kRemoteSsrcs), std::end(kRemoteSsrcs)));
  remb.SetBitrateBps(kBitrateBps);

  Buffer packet = remb.Build();

  EXPECT_THAT(make_tuple(packet.data(), packet.size()),
              ElementsAreArray(kPacket));
}

TEST(RtcpPacketRembTest, Parse) {
  Remb remb;
  EXPECT_TRUE(test::ParseSinglePacket(kPacket, &remb));
  const Remb& parsed = remb;

  EXPECT_EQ(kSenderSsrc, parsed.sender_ssrc());
  EXPECT_EQ(kBitrateBps, parsed.bitrate_bps());
  EXPECT_THAT(parsed.ssrcs(), ElementsAreArray(kRemoteSsrcs));
}

TEST(RtcpPacketRembTest, CreateAndParseWithoutSsrcs) {
  Remb remb;
  remb.SetSenderSsrc(kSenderSsrc);
  remb.SetBitrateBps(kBitrateBps);
  Buffer packet = remb.Build();

  Remb parsed;
  EXPECT_TRUE(test::ParseSinglePacket(packet, &parsed));
  EXPECT_EQ(kSenderSsrc, parsed.sender_ssrc());
  EXPECT_EQ(kBitrateBps, parsed.bitrate_bps());
  EXPECT_THAT(parsed.ssrcs(), IsEmpty());
}

TEST(RtcpPacketRembTest, CreateAndParse64bitBitrate) {
  Remb remb;
  remb.SetBitrateBps(kBitrateBps64bit);
  Buffer packet = remb.Build();

  Remb parsed;
  EXPECT_TRUE(test::ParseSinglePacket(packet, &parsed));
  EXPECT_EQ(kBitrateBps64bit, parsed.bitrate_bps());
}

TEST(RtcpPacketRembTest, ParseFailsOnTooSmallPacketToBeRemb) {
  // Make it too small.
  constexpr size_t kTooSmallSize = (1 + 3) * 4;
  uint8_t packet[kTooSmallSize];
  memcpy(packet, kPacket, kTooSmallSize);
  packet[3] = 3;

  Remb remb;
  EXPECT_FALSE(test::ParseSinglePacket(packet, &remb));
}

TEST(RtcpPacketRembTest, ParseFailsWhenUniqueIdentifierIsNotRemb) {
  uint8_t packet[kPacketLength];
  memcpy(packet, kPacket, kPacketLength);
  packet[12] = 'N';  // Swap 'R' -> 'N' in the 'REMB' unique identifier.

  Remb remb;
  EXPECT_FALSE(test::ParseSinglePacket(packet, &remb));
}

TEST(RtcpPacketRembTest, ParseFailsWhenBitrateDoNotFitIn64bits) {
  uint8_t packet[kPacketLength];
  memcpy(packet, kPacket, kPacketLength);
  packet[17] |= 0xfc;  // Set exponent component to maximum of 63.
  packet[19] |= 0x02;  // Ensure mantissa is at least 2.

  Remb remb;
  EXPECT_FALSE(test::ParseSinglePacket(packet, &remb));
}

TEST(RtcpPacketRembTest, ParseFailsWhenBitrateDoNotFitIn63bits) {
  uint8_t packet[kPacketLength];
  memcpy(packet, kPacket, kPacketLength);
  packet[17] = 56 << 2;  // Set exponent component to 56.
  packet[18] = 0;        // Set mantissa to 200 > 128
  packet[19] = 200;

  // Result value 200 * 2^56 can't be represented with int64_t and thus should
  // be rejected.
  Remb remb;
  EXPECT_FALSE(test::ParseSinglePacket(packet, &remb));
}

TEST(RtcpPacketRembTest, ParseFailsWhenSsrcCountMismatchLength) {
  uint8_t packet[kPacketLength];
  memcpy(packet, kPacket, kPacketLength);
  packet[16]++;  // Swap 3 -> 4 in the ssrcs count.

  Remb remb;
  EXPECT_FALSE(test::ParseSinglePacket(packet, &remb));
}

TEST(RtcpPacketRembTest, TooManySsrcs) {
  Remb remb;
  EXPECT_FALSE(remb.SetSsrcs(
      std::vector<uint32_t>(Remb::kMaxNumberOfSsrcs + 1, kRemoteSsrcs[0])));
  EXPECT_TRUE(remb.SetSsrcs(
      std::vector<uint32_t>(Remb::kMaxNumberOfSsrcs, kRemoteSsrcs[0])));
}

}  // namespace webrtc