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
12 #include <list>
13
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "webrtc/base/scoped_ptr.h"
17 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
18 #include "webrtc/modules/rtp_rtcp/mocks/mock_rtp_rtcp.h"
19 #include "webrtc/video/payload_router.h"
20
21 using ::testing::_;
22 using ::testing::AnyNumber;
23 using ::testing::NiceMock;
24 using ::testing::Return;
25
26 namespace webrtc {
27
28 class PayloadRouterTest : public ::testing::Test {
29 protected:
SetUp()30 virtual void SetUp() {
31 payload_router_.reset(new PayloadRouter());
32 }
33 rtc::scoped_ptr<PayloadRouter> payload_router_;
34 };
35
TEST_F(PayloadRouterTest,SendOnOneModule)36 TEST_F(PayloadRouterTest, SendOnOneModule) {
37 MockRtpRtcp rtp;
38 std::list<RtpRtcp*> modules(1, &rtp);
39
40 payload_router_->SetSendingRtpModules(modules);
41
42 uint8_t payload = 'a';
43 FrameType frame_type = kVideoFrameKey;
44 int8_t payload_type = 96;
45
46 EXPECT_CALL(rtp, SendOutgoingData(frame_type, payload_type, 0, 0, _, 1, NULL,
47 NULL))
48 .Times(0);
49 EXPECT_FALSE(payload_router_->RoutePayload(frame_type, payload_type, 0, 0,
50 &payload, 1, NULL, NULL));
51
52 payload_router_->set_active(true);
53 EXPECT_CALL(rtp, SendOutgoingData(frame_type, payload_type, 0, 0, _, 1, NULL,
54 NULL))
55 .Times(1);
56 EXPECT_TRUE(payload_router_->RoutePayload(frame_type, payload_type, 0, 0,
57 &payload, 1, NULL, NULL));
58
59 payload_router_->set_active(false);
60 EXPECT_CALL(rtp, SendOutgoingData(frame_type, payload_type, 0, 0, _, 1, NULL,
61 NULL))
62 .Times(0);
63 EXPECT_FALSE(payload_router_->RoutePayload(frame_type, payload_type, 0, 0,
64 &payload, 1, NULL, NULL));
65
66 payload_router_->set_active(true);
67 EXPECT_CALL(rtp, SendOutgoingData(frame_type, payload_type, 0, 0, _, 1, NULL,
68 NULL))
69 .Times(1);
70 EXPECT_TRUE(payload_router_->RoutePayload(frame_type, payload_type, 0, 0,
71 &payload, 1, NULL, NULL));
72
73 modules.clear();
74 payload_router_->SetSendingRtpModules(modules);
75 EXPECT_CALL(rtp, SendOutgoingData(frame_type, payload_type, 0, 0, _, 1, NULL,
76 NULL))
77 .Times(0);
78 EXPECT_FALSE(payload_router_->RoutePayload(frame_type, payload_type, 0, 0,
79 &payload, 1, NULL, NULL));
80 }
81
TEST_F(PayloadRouterTest,SendSimulcast)82 TEST_F(PayloadRouterTest, SendSimulcast) {
83 MockRtpRtcp rtp_1;
84 MockRtpRtcp rtp_2;
85 std::list<RtpRtcp*> modules;
86 modules.push_back(&rtp_1);
87 modules.push_back(&rtp_2);
88
89 payload_router_->SetSendingRtpModules(modules);
90
91 uint8_t payload_1 = 'a';
92 FrameType frame_type_1 = kVideoFrameKey;
93 int8_t payload_type_1 = 96;
94 RTPVideoHeader rtp_hdr_1;
95 rtp_hdr_1.simulcastIdx = 0;
96
97 payload_router_->set_active(true);
98 EXPECT_CALL(rtp_1, SendOutgoingData(frame_type_1, payload_type_1, 0, 0, _, 1,
99 NULL, &rtp_hdr_1))
100 .Times(1);
101 EXPECT_CALL(rtp_2, SendOutgoingData(_, _, _, _, _, _, _, _))
102 .Times(0);
103 EXPECT_TRUE(payload_router_->RoutePayload(frame_type_1, payload_type_1, 0, 0,
104 &payload_1, 1, NULL, &rtp_hdr_1));
105
106 uint8_t payload_2 = 'b';
107 FrameType frame_type_2 = kVideoFrameDelta;
108 int8_t payload_type_2 = 97;
109 RTPVideoHeader rtp_hdr_2;
110 rtp_hdr_2.simulcastIdx = 1;
111 EXPECT_CALL(rtp_2, SendOutgoingData(frame_type_2, payload_type_2, 0, 0, _, 1,
112 NULL, &rtp_hdr_2))
113 .Times(1);
114 EXPECT_CALL(rtp_1, SendOutgoingData(_, _, _, _, _, _, _, _))
115 .Times(0);
116 EXPECT_TRUE(payload_router_->RoutePayload(frame_type_2, payload_type_2, 0, 0,
117 &payload_2, 1, NULL, &rtp_hdr_2));
118
119 // Inactive.
120 payload_router_->set_active(false);
121 EXPECT_CALL(rtp_1, SendOutgoingData(_, _, _, _, _, _, _, _))
122 .Times(0);
123 EXPECT_CALL(rtp_2, SendOutgoingData(_, _, _, _, _, _, _, _))
124 .Times(0);
125 EXPECT_FALSE(payload_router_->RoutePayload(frame_type_1, payload_type_1, 0, 0,
126 &payload_1, 1, NULL, &rtp_hdr_1));
127 EXPECT_FALSE(payload_router_->RoutePayload(frame_type_2, payload_type_2, 0, 0,
128 &payload_2, 1, NULL, &rtp_hdr_2));
129
130 // Invalid simulcast index.
131 payload_router_->set_active(true);
132 EXPECT_CALL(rtp_1, SendOutgoingData(_, _, _, _, _, _, _, _))
133 .Times(0);
134 EXPECT_CALL(rtp_2, SendOutgoingData(_, _, _, _, _, _, _, _))
135 .Times(0);
136 rtp_hdr_1.simulcastIdx = 2;
137 EXPECT_FALSE(payload_router_->RoutePayload(frame_type_1, payload_type_1, 0, 0,
138 &payload_1, 1, NULL, &rtp_hdr_1));
139 }
140
TEST_F(PayloadRouterTest,MaxPayloadLength)141 TEST_F(PayloadRouterTest, MaxPayloadLength) {
142 // Without any limitations from the modules, verify we get the max payload
143 // length for IP/UDP/SRTP with a MTU of 150 bytes.
144 const size_t kDefaultMaxLength = 1500 - 20 - 8 - 12 - 4;
145 EXPECT_EQ(kDefaultMaxLength, payload_router_->DefaultMaxPayloadLength());
146 EXPECT_EQ(kDefaultMaxLength, payload_router_->MaxPayloadLength());
147
148 MockRtpRtcp rtp_1;
149 MockRtpRtcp rtp_2;
150 std::list<RtpRtcp*> modules;
151 modules.push_back(&rtp_1);
152 modules.push_back(&rtp_2);
153 payload_router_->SetSendingRtpModules(modules);
154
155 // Modules return a higher length than the default value.
156 EXPECT_CALL(rtp_1, MaxDataPayloadLength())
157 .Times(1)
158 .WillOnce(Return(kDefaultMaxLength + 10));
159 EXPECT_CALL(rtp_2, MaxDataPayloadLength())
160 .Times(1)
161 .WillOnce(Return(kDefaultMaxLength + 10));
162 EXPECT_EQ(kDefaultMaxLength, payload_router_->MaxPayloadLength());
163
164 // The modules return a value lower than default.
165 const size_t kTestMinPayloadLength = 1001;
166 EXPECT_CALL(rtp_1, MaxDataPayloadLength())
167 .Times(1)
168 .WillOnce(Return(kTestMinPayloadLength + 10));
169 EXPECT_CALL(rtp_2, MaxDataPayloadLength())
170 .Times(1)
171 .WillOnce(Return(kTestMinPayloadLength));
172 EXPECT_EQ(kTestMinPayloadLength, payload_router_->MaxPayloadLength());
173 }
174
TEST_F(PayloadRouterTest,SetTargetSendBitrates)175 TEST_F(PayloadRouterTest, SetTargetSendBitrates) {
176 MockRtpRtcp rtp_1;
177 MockRtpRtcp rtp_2;
178 std::list<RtpRtcp*> modules;
179 modules.push_back(&rtp_1);
180 modules.push_back(&rtp_2);
181 payload_router_->SetSendingRtpModules(modules);
182
183 const uint32_t bitrate_1 = 10000;
184 const uint32_t bitrate_2 = 76543;
185 std::vector<uint32_t> bitrates(2, bitrate_1);
186 bitrates[1] = bitrate_2;
187 EXPECT_CALL(rtp_1, SetTargetSendBitrate(bitrate_1))
188 .Times(1);
189 EXPECT_CALL(rtp_2, SetTargetSendBitrate(bitrate_2))
190 .Times(1);
191 payload_router_->SetTargetSendBitrates(bitrates);
192
193 bitrates.resize(1);
194 EXPECT_CALL(rtp_1, SetTargetSendBitrate(bitrate_1))
195 .Times(0);
196 EXPECT_CALL(rtp_2, SetTargetSendBitrate(bitrate_2))
197 .Times(0);
198 payload_router_->SetTargetSendBitrates(bitrates);
199
200 bitrates.resize(3);
201 bitrates[1] = bitrate_2;
202 bitrates[2] = bitrate_1 + bitrate_2;
203 EXPECT_CALL(rtp_1, SetTargetSendBitrate(bitrate_1))
204 .Times(1);
205 EXPECT_CALL(rtp_2, SetTargetSendBitrate(bitrate_2))
206 .Times(1);
207 payload_router_->SetTargetSendBitrates(bitrates);
208 }
209 } // namespace webrtc
210