1 /*
2 * Copyright 2017 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 "pc/rtp_transport.h"
12
13 #include <cstdint>
14 #include <set>
15 #include <string>
16 #include <utility>
17
18 #include "api/rtp_headers.h"
19 #include "api/rtp_parameters.h"
20 #include "p2p/base/fake_packet_transport.h"
21 #include "pc/test/rtp_transport_test_util.h"
22 #include "rtc_base/buffer.h"
23 #include "rtc_base/third_party/sigslot/sigslot.h"
24 #include "test/gtest.h"
25
26 namespace webrtc {
27
28 constexpr bool kMuxDisabled = false;
29 constexpr bool kMuxEnabled = true;
30 constexpr uint16_t kLocalNetId = 1;
31 constexpr uint16_t kRemoteNetId = 2;
32 constexpr int kLastPacketId = 100;
33 constexpr int kTransportOverheadPerPacket = 28; // Ipv4(20) + UDP(8).
34
35 class SignalObserver : public sigslot::has_slots<> {
36 public:
SignalObserver(RtpTransport * transport)37 explicit SignalObserver(RtpTransport* transport) {
38 transport_ = transport;
39 transport->SignalReadyToSend.connect(this, &SignalObserver::OnReadyToSend);
40 transport->SignalNetworkRouteChanged.connect(
41 this, &SignalObserver::OnNetworkRouteChanged);
42 if (transport->rtp_packet_transport()) {
43 transport->rtp_packet_transport()->SignalSentPacket.connect(
44 this, &SignalObserver::OnSentPacket);
45 }
46
47 if (transport->rtcp_packet_transport()) {
48 transport->rtcp_packet_transport()->SignalSentPacket.connect(
49 this, &SignalObserver::OnSentPacket);
50 }
51 }
52
ready() const53 bool ready() const { return ready_; }
OnReadyToSend(bool ready)54 void OnReadyToSend(bool ready) { ready_ = ready; }
55
network_route()56 absl::optional<rtc::NetworkRoute> network_route() { return network_route_; }
OnNetworkRouteChanged(absl::optional<rtc::NetworkRoute> network_route)57 void OnNetworkRouteChanged(absl::optional<rtc::NetworkRoute> network_route) {
58 network_route_ = network_route;
59 }
60
OnSentPacket(rtc::PacketTransportInternal * packet_transport,const rtc::SentPacket & sent_packet)61 void OnSentPacket(rtc::PacketTransportInternal* packet_transport,
62 const rtc::SentPacket& sent_packet) {
63 if (packet_transport == transport_->rtp_packet_transport()) {
64 rtp_transport_sent_count_++;
65 } else {
66 ASSERT_EQ(transport_->rtcp_packet_transport(), packet_transport);
67 rtcp_transport_sent_count_++;
68 }
69 }
70
rtp_transport_sent_count()71 int rtp_transport_sent_count() { return rtp_transport_sent_count_; }
72
rtcp_transport_sent_count()73 int rtcp_transport_sent_count() { return rtcp_transport_sent_count_; }
74
75 private:
76 int rtp_transport_sent_count_ = 0;
77 int rtcp_transport_sent_count_ = 0;
78 RtpTransport* transport_ = nullptr;
79 bool ready_ = false;
80 absl::optional<rtc::NetworkRoute> network_route_;
81 };
82
TEST(RtpTransportTest,SettingRtcpAndRtpSignalsReady)83 TEST(RtpTransportTest, SettingRtcpAndRtpSignalsReady) {
84 RtpTransport transport(kMuxDisabled);
85 SignalObserver observer(&transport);
86 rtc::FakePacketTransport fake_rtcp("fake_rtcp");
87 fake_rtcp.SetWritable(true);
88 rtc::FakePacketTransport fake_rtp("fake_rtp");
89 fake_rtp.SetWritable(true);
90
91 transport.SetRtcpPacketTransport(&fake_rtcp); // rtcp ready
92 EXPECT_FALSE(observer.ready());
93 transport.SetRtpPacketTransport(&fake_rtp); // rtp ready
94 EXPECT_TRUE(observer.ready());
95 }
96
TEST(RtpTransportTest,SettingRtpAndRtcpSignalsReady)97 TEST(RtpTransportTest, SettingRtpAndRtcpSignalsReady) {
98 RtpTransport transport(kMuxDisabled);
99 SignalObserver observer(&transport);
100 rtc::FakePacketTransport fake_rtcp("fake_rtcp");
101 fake_rtcp.SetWritable(true);
102 rtc::FakePacketTransport fake_rtp("fake_rtp");
103 fake_rtp.SetWritable(true);
104
105 transport.SetRtpPacketTransport(&fake_rtp); // rtp ready
106 EXPECT_FALSE(observer.ready());
107 transport.SetRtcpPacketTransport(&fake_rtcp); // rtcp ready
108 EXPECT_TRUE(observer.ready());
109 }
110
TEST(RtpTransportTest,SettingRtpWithRtcpMuxEnabledSignalsReady)111 TEST(RtpTransportTest, SettingRtpWithRtcpMuxEnabledSignalsReady) {
112 RtpTransport transport(kMuxEnabled);
113 SignalObserver observer(&transport);
114 rtc::FakePacketTransport fake_rtp("fake_rtp");
115 fake_rtp.SetWritable(true);
116
117 transport.SetRtpPacketTransport(&fake_rtp); // rtp ready
118 EXPECT_TRUE(observer.ready());
119 }
120
TEST(RtpTransportTest,DisablingRtcpMuxSignalsNotReady)121 TEST(RtpTransportTest, DisablingRtcpMuxSignalsNotReady) {
122 RtpTransport transport(kMuxEnabled);
123 SignalObserver observer(&transport);
124 rtc::FakePacketTransport fake_rtp("fake_rtp");
125 fake_rtp.SetWritable(true);
126
127 transport.SetRtpPacketTransport(&fake_rtp); // rtp ready
128 EXPECT_TRUE(observer.ready());
129
130 transport.SetRtcpMuxEnabled(false);
131 EXPECT_FALSE(observer.ready());
132 }
133
TEST(RtpTransportTest,EnablingRtcpMuxSignalsReady)134 TEST(RtpTransportTest, EnablingRtcpMuxSignalsReady) {
135 RtpTransport transport(kMuxDisabled);
136 SignalObserver observer(&transport);
137 rtc::FakePacketTransport fake_rtp("fake_rtp");
138 fake_rtp.SetWritable(true);
139
140 transport.SetRtpPacketTransport(&fake_rtp); // rtp ready
141 EXPECT_FALSE(observer.ready());
142
143 transport.SetRtcpMuxEnabled(true);
144 EXPECT_TRUE(observer.ready());
145 }
146
147 // Tests the SignalNetworkRoute is fired when setting a packet transport.
TEST(RtpTransportTest,SetRtpTransportWithNetworkRouteChanged)148 TEST(RtpTransportTest, SetRtpTransportWithNetworkRouteChanged) {
149 RtpTransport transport(kMuxDisabled);
150 SignalObserver observer(&transport);
151 rtc::FakePacketTransport fake_rtp("fake_rtp");
152
153 EXPECT_FALSE(observer.network_route());
154
155 rtc::NetworkRoute network_route;
156 // Set a non-null RTP transport with a new network route.
157 network_route.connected = true;
158 network_route.local = rtc::RouteEndpoint::CreateWithNetworkId(kLocalNetId);
159 network_route.remote = rtc::RouteEndpoint::CreateWithNetworkId(kRemoteNetId);
160 network_route.last_sent_packet_id = kLastPacketId;
161 network_route.packet_overhead = kTransportOverheadPerPacket;
162 fake_rtp.SetNetworkRoute(absl::optional<rtc::NetworkRoute>(network_route));
163 transport.SetRtpPacketTransport(&fake_rtp);
164 ASSERT_TRUE(observer.network_route());
165 EXPECT_TRUE(observer.network_route()->connected);
166 EXPECT_EQ(kLocalNetId, observer.network_route()->local.network_id());
167 EXPECT_EQ(kRemoteNetId, observer.network_route()->remote.network_id());
168 EXPECT_EQ(kTransportOverheadPerPacket,
169 observer.network_route()->packet_overhead);
170 EXPECT_EQ(kLastPacketId, observer.network_route()->last_sent_packet_id);
171
172 // Set a null RTP transport.
173 transport.SetRtpPacketTransport(nullptr);
174 EXPECT_FALSE(observer.network_route());
175 }
176
TEST(RtpTransportTest,SetRtcpTransportWithNetworkRouteChanged)177 TEST(RtpTransportTest, SetRtcpTransportWithNetworkRouteChanged) {
178 RtpTransport transport(kMuxDisabled);
179 SignalObserver observer(&transport);
180 rtc::FakePacketTransport fake_rtcp("fake_rtcp");
181
182 EXPECT_FALSE(observer.network_route());
183
184 rtc::NetworkRoute network_route;
185 // Set a non-null RTCP transport with a new network route.
186 network_route.connected = true;
187 network_route.local = rtc::RouteEndpoint::CreateWithNetworkId(kLocalNetId);
188 network_route.remote = rtc::RouteEndpoint::CreateWithNetworkId(kRemoteNetId);
189 network_route.last_sent_packet_id = kLastPacketId;
190 network_route.packet_overhead = kTransportOverheadPerPacket;
191 fake_rtcp.SetNetworkRoute(absl::optional<rtc::NetworkRoute>(network_route));
192 transport.SetRtcpPacketTransport(&fake_rtcp);
193 ASSERT_TRUE(observer.network_route());
194 EXPECT_TRUE(observer.network_route()->connected);
195 EXPECT_EQ(kLocalNetId, observer.network_route()->local.network_id());
196 EXPECT_EQ(kRemoteNetId, observer.network_route()->remote.network_id());
197 EXPECT_EQ(kTransportOverheadPerPacket,
198 observer.network_route()->packet_overhead);
199 EXPECT_EQ(kLastPacketId, observer.network_route()->last_sent_packet_id);
200
201 // Set a null RTCP transport.
202 transport.SetRtcpPacketTransport(nullptr);
203 EXPECT_FALSE(observer.network_route());
204 }
205
206 // Test that RTCP packets are sent over correct transport based on the RTCP-mux
207 // status.
TEST(RtpTransportTest,RtcpPacketSentOverCorrectTransport)208 TEST(RtpTransportTest, RtcpPacketSentOverCorrectTransport) {
209 // If the RTCP-mux is not enabled, RTCP packets are expected to be sent over
210 // the RtcpPacketTransport.
211 RtpTransport transport(kMuxDisabled);
212 rtc::FakePacketTransport fake_rtcp("fake_rtcp");
213 rtc::FakePacketTransport fake_rtp("fake_rtp");
214 transport.SetRtcpPacketTransport(&fake_rtcp); // rtcp ready
215 transport.SetRtpPacketTransport(&fake_rtp); // rtp ready
216 SignalObserver observer(&transport);
217
218 fake_rtp.SetDestination(&fake_rtp, true);
219 fake_rtcp.SetDestination(&fake_rtcp, true);
220
221 rtc::CopyOnWriteBuffer packet;
222 EXPECT_TRUE(transport.SendRtcpPacket(&packet, rtc::PacketOptions(), 0));
223 EXPECT_EQ(1, observer.rtcp_transport_sent_count());
224
225 // The RTCP packets are expected to be sent over RtpPacketTransport if
226 // RTCP-mux is enabled.
227 transport.SetRtcpMuxEnabled(true);
228 EXPECT_TRUE(transport.SendRtcpPacket(&packet, rtc::PacketOptions(), 0));
229 EXPECT_EQ(1, observer.rtp_transport_sent_count());
230 }
231
TEST(RtpTransportTest,ChangingReadyToSendStateOnlySignalsWhenChanged)232 TEST(RtpTransportTest, ChangingReadyToSendStateOnlySignalsWhenChanged) {
233 RtpTransport transport(kMuxEnabled);
234 TransportObserver observer(&transport);
235 rtc::FakePacketTransport fake_rtp("fake_rtp");
236 fake_rtp.SetWritable(true);
237
238 // State changes, so we should signal.
239 transport.SetRtpPacketTransport(&fake_rtp);
240 EXPECT_EQ(observer.ready_to_send_signal_count(), 1);
241
242 // State does not change, so we should not signal.
243 transport.SetRtpPacketTransport(&fake_rtp);
244 EXPECT_EQ(observer.ready_to_send_signal_count(), 1);
245
246 // State does not change, so we should not signal.
247 transport.SetRtcpMuxEnabled(true);
248 EXPECT_EQ(observer.ready_to_send_signal_count(), 1);
249
250 // State changes, so we should signal.
251 transport.SetRtcpMuxEnabled(false);
252 EXPECT_EQ(observer.ready_to_send_signal_count(), 2);
253 }
254
255 // Test that SignalPacketReceived fires with rtcp=true when a RTCP packet is
256 // received.
TEST(RtpTransportTest,SignalDemuxedRtcp)257 TEST(RtpTransportTest, SignalDemuxedRtcp) {
258 RtpTransport transport(kMuxDisabled);
259 rtc::FakePacketTransport fake_rtp("fake_rtp");
260 fake_rtp.SetDestination(&fake_rtp, true);
261 transport.SetRtpPacketTransport(&fake_rtp);
262 TransportObserver observer(&transport);
263
264 // An rtcp packet.
265 const unsigned char data[] = {0x80, 73, 0, 0};
266 const int len = 4;
267 const rtc::PacketOptions options;
268 const int flags = 0;
269 fake_rtp.SendPacket(reinterpret_cast<const char*>(data), len, options, flags);
270 EXPECT_EQ(0, observer.rtp_count());
271 EXPECT_EQ(1, observer.rtcp_count());
272 }
273
274 static const unsigned char kRtpData[] = {0x80, 0x11, 0, 0, 0, 0,
275 0, 0, 0, 0, 0, 0};
276 static const int kRtpLen = 12;
277
278 // Test that SignalPacketReceived fires with rtcp=false when a RTP packet with a
279 // handled payload type is received.
TEST(RtpTransportTest,SignalHandledRtpPayloadType)280 TEST(RtpTransportTest, SignalHandledRtpPayloadType) {
281 RtpTransport transport(kMuxDisabled);
282 rtc::FakePacketTransport fake_rtp("fake_rtp");
283 fake_rtp.SetDestination(&fake_rtp, true);
284 transport.SetRtpPacketTransport(&fake_rtp);
285 TransportObserver observer(&transport);
286 RtpDemuxerCriteria demuxer_criteria;
287 // Add a handled payload type.
288 demuxer_criteria.payload_types = {0x11};
289 transport.RegisterRtpDemuxerSink(demuxer_criteria, &observer);
290
291 // An rtp packet.
292 const rtc::PacketOptions options;
293 const int flags = 0;
294 rtc::Buffer rtp_data(kRtpData, kRtpLen);
295 fake_rtp.SendPacket(rtp_data.data<char>(), kRtpLen, options, flags);
296 EXPECT_EQ(1, observer.rtp_count());
297 EXPECT_EQ(0, observer.rtcp_count());
298 // Remove the sink before destroying the transport.
299 transport.UnregisterRtpDemuxerSink(&observer);
300 }
301
302 // Test that SignalPacketReceived does not fire when a RTP packet with an
303 // unhandled payload type is received.
TEST(RtpTransportTest,DontSignalUnhandledRtpPayloadType)304 TEST(RtpTransportTest, DontSignalUnhandledRtpPayloadType) {
305 RtpTransport transport(kMuxDisabled);
306 rtc::FakePacketTransport fake_rtp("fake_rtp");
307 fake_rtp.SetDestination(&fake_rtp, true);
308 transport.SetRtpPacketTransport(&fake_rtp);
309 TransportObserver observer(&transport);
310 RtpDemuxerCriteria demuxer_criteria;
311 // Add an unhandled payload type.
312 demuxer_criteria.payload_types = {0x12};
313 transport.RegisterRtpDemuxerSink(demuxer_criteria, &observer);
314
315 const rtc::PacketOptions options;
316 const int flags = 0;
317 rtc::Buffer rtp_data(kRtpData, kRtpLen);
318 fake_rtp.SendPacket(rtp_data.data<char>(), kRtpLen, options, flags);
319 EXPECT_EQ(0, observer.rtp_count());
320 EXPECT_EQ(0, observer.rtcp_count());
321 // Remove the sink before destroying the transport.
322 transport.UnregisterRtpDemuxerSink(&observer);
323 }
324
325 } // namespace webrtc
326