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/target_bitrate.h"
12
13 #include "modules/rtp_rtcp/source/byte_io.h"
14 #include "modules/rtp_rtcp/source/rtcp_packet/extended_reports.h"
15 #include "rtc_base/buffer.h"
16 #include "test/gtest.h"
17 #include "test/rtcp_packet_parser.h"
18
19 namespace webrtc {
20 namespace {
21 using BitrateItem = rtcp::TargetBitrate::BitrateItem;
22 using rtcp::TargetBitrate;
23 using test::ParseSinglePacket;
24
25 constexpr uint32_t kSsrc = 0x12345678;
26
27 // clang-format off
28 const uint8_t kPacket[] = { TargetBitrate::kBlockType, // Block ID.
29 0x00, // Reserved.
30 0x00, 0x04, // Length = 4 words.
31 0x00, 0x01, 0x02, 0x03, // S0T0 0x010203 kbps.
32 0x01, 0x02, 0x03, 0x04, // S0T1 0x020304 kbps.
33 0x10, 0x03, 0x04, 0x05, // S1T0 0x030405 kbps.
34 0x11, 0x04, 0x05, 0x06 }; // S1T1 0x040506 kbps.
35 constexpr size_t kPacketLengthBlocks = ((sizeof(kPacket) + 3) / 4) - 1;
36 // clang-format on
37
ExpectBirateItemEquals(const BitrateItem & expected,const BitrateItem & actual)38 void ExpectBirateItemEquals(const BitrateItem& expected,
39 const BitrateItem& actual) {
40 EXPECT_EQ(expected.spatial_layer, actual.spatial_layer);
41 EXPECT_EQ(expected.temporal_layer, actual.temporal_layer);
42 EXPECT_EQ(expected.target_bitrate_kbps, actual.target_bitrate_kbps);
43 }
44
CheckBitrateItems(const std::vector<BitrateItem> & bitrates)45 void CheckBitrateItems(const std::vector<BitrateItem>& bitrates) {
46 EXPECT_EQ(4U, bitrates.size());
47 ExpectBirateItemEquals(BitrateItem(0, 0, 0x010203), bitrates[0]);
48 ExpectBirateItemEquals(BitrateItem(0, 1, 0x020304), bitrates[1]);
49 ExpectBirateItemEquals(BitrateItem(1, 0, 0x030405), bitrates[2]);
50 ExpectBirateItemEquals(BitrateItem(1, 1, 0x040506), bitrates[3]);
51 }
52
53 } // namespace
54
TEST(TargetBitrateTest,Parse)55 TEST(TargetBitrateTest, Parse) {
56 TargetBitrate target_bitrate;
57 target_bitrate.Parse(kPacket, kPacketLengthBlocks);
58 CheckBitrateItems(target_bitrate.GetTargetBitrates());
59 }
60
TEST(TargetBitrateTest,FullPacket)61 TEST(TargetBitrateTest, FullPacket) {
62 const size_t kXRHeaderSize = 8; // RTCP header (4) + SSRC (4).
63 const size_t kTotalSize = kXRHeaderSize + sizeof(kPacket);
64 uint8_t kRtcpPacket[kTotalSize] = {2 << 6, 207, 0x00, (kTotalSize / 4) - 1,
65 0x12, 0x34, 0x56, 0x78}; // SSRC.
66 memcpy(&kRtcpPacket[kXRHeaderSize], kPacket, sizeof(kPacket));
67 rtcp::ExtendedReports xr;
68 EXPECT_TRUE(ParseSinglePacket(kRtcpPacket, &xr));
69 EXPECT_EQ(kSsrc, xr.sender_ssrc());
70 const absl::optional<TargetBitrate>& target_bitrate = xr.target_bitrate();
71 ASSERT_TRUE(static_cast<bool>(target_bitrate));
72 CheckBitrateItems(target_bitrate->GetTargetBitrates());
73 }
74
TEST(TargetBitrateTest,Create)75 TEST(TargetBitrateTest, Create) {
76 TargetBitrate target_bitrate;
77 target_bitrate.AddTargetBitrate(0, 0, 0x010203);
78 target_bitrate.AddTargetBitrate(0, 1, 0x020304);
79 target_bitrate.AddTargetBitrate(1, 0, 0x030405);
80 target_bitrate.AddTargetBitrate(1, 1, 0x040506);
81
82 uint8_t buffer[sizeof(kPacket)] = {};
83 ASSERT_EQ(sizeof(kPacket), target_bitrate.BlockLength());
84 target_bitrate.Create(buffer);
85
86 EXPECT_EQ(0, memcmp(kPacket, buffer, sizeof(kPacket)));
87 }
88
TEST(TargetBitrateTest,ParseNullBitratePacket)89 TEST(TargetBitrateTest, ParseNullBitratePacket) {
90 const uint8_t kNullPacket[] = {TargetBitrate::kBlockType, 0x00, 0x00, 0x00};
91 TargetBitrate target_bitrate;
92 target_bitrate.Parse(kNullPacket, 0);
93 EXPECT_TRUE(target_bitrate.GetTargetBitrates().empty());
94 }
95
96 } // namespace webrtc
97