1 /*
2 * Copyright 2008 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/channel_manager.h"
12
13 #include <memory>
14
15 #include "api/rtc_error.h"
16 #include "api/video/builtin_video_bitrate_allocator_factory.h"
17 #include "media/base/fake_media_engine.h"
18 #include "media/base/test_utils.h"
19 #include "media/engine/fake_webrtc_call.h"
20 #include "p2p/base/dtls_transport_internal.h"
21 #include "p2p/base/fake_dtls_transport.h"
22 #include "p2p/base/p2p_constants.h"
23 #include "p2p/base/packet_transport_internal.h"
24 #include "pc/dtls_srtp_transport.h"
25 #include "rtc_base/checks.h"
26 #include "rtc_base/thread.h"
27 #include "test/gtest.h"
28
29 namespace {
30 const bool kDefaultSrtpRequired = true;
31 }
32
33 namespace cricket {
34
35 static const AudioCodec kAudioCodecs[] = {
36 AudioCodec(97, "voice", 1, 2, 3),
37 AudioCodec(111, "OPUS", 48000, 32000, 2),
38 };
39
40 static const VideoCodec kVideoCodecs[] = {
41 VideoCodec(99, "H264"),
42 VideoCodec(100, "VP8"),
43 VideoCodec(96, "rtx"),
44 };
45
46 class ChannelManagerTest : public ::testing::Test {
47 protected:
ChannelManagerTest()48 ChannelManagerTest()
49 : network_(rtc::Thread::CreateWithSocketServer()),
50 worker_(rtc::Thread::Create()),
51 video_bitrate_allocator_factory_(
52 webrtc::CreateBuiltinVideoBitrateAllocatorFactory()),
53 fme_(new cricket::FakeMediaEngine()),
54 fdme_(new cricket::FakeDataEngine()),
55 cm_(new cricket::ChannelManager(
56 std::unique_ptr<MediaEngineInterface>(fme_),
57 std::unique_ptr<DataEngineInterface>(fdme_),
58 rtc::Thread::Current(),
59 rtc::Thread::Current())),
60 fake_call_() {
61 fme_->SetAudioCodecs(MAKE_VECTOR(kAudioCodecs));
62 fme_->SetVideoCodecs(MAKE_VECTOR(kVideoCodecs));
63 }
64
CreateDtlsSrtpTransport()65 std::unique_ptr<webrtc::RtpTransportInternal> CreateDtlsSrtpTransport() {
66 rtp_dtls_transport_ = std::make_unique<FakeDtlsTransport>(
67 "fake_dtls_transport", cricket::ICE_CANDIDATE_COMPONENT_RTP);
68 auto dtls_srtp_transport = std::make_unique<webrtc::DtlsSrtpTransport>(
69 /*rtcp_mux_required=*/true);
70 dtls_srtp_transport->SetDtlsTransports(rtp_dtls_transport_.get(),
71 /*rtcp_dtls_transport=*/nullptr);
72 return dtls_srtp_transport;
73 }
74
TestCreateDestroyChannels(webrtc::RtpTransportInternal * rtp_transport)75 void TestCreateDestroyChannels(webrtc::RtpTransportInternal* rtp_transport) {
76 cricket::VoiceChannel* voice_channel = cm_->CreateVoiceChannel(
77 &fake_call_, cricket::MediaConfig(), rtp_transport,
78 rtc::Thread::Current(), cricket::CN_AUDIO, kDefaultSrtpRequired,
79 webrtc::CryptoOptions(), &ssrc_generator_, AudioOptions());
80 EXPECT_TRUE(voice_channel != nullptr);
81 cricket::VideoChannel* video_channel = cm_->CreateVideoChannel(
82 &fake_call_, cricket::MediaConfig(), rtp_transport,
83 rtc::Thread::Current(), cricket::CN_VIDEO, kDefaultSrtpRequired,
84 webrtc::CryptoOptions(), &ssrc_generator_, VideoOptions(),
85 video_bitrate_allocator_factory_.get());
86 EXPECT_TRUE(video_channel != nullptr);
87 cricket::RtpDataChannel* rtp_data_channel = cm_->CreateRtpDataChannel(
88 cricket::MediaConfig(), rtp_transport, rtc::Thread::Current(),
89 cricket::CN_DATA, kDefaultSrtpRequired, webrtc::CryptoOptions(),
90 &ssrc_generator_);
91 EXPECT_TRUE(rtp_data_channel != nullptr);
92 cm_->DestroyVideoChannel(video_channel);
93 cm_->DestroyVoiceChannel(voice_channel);
94 cm_->DestroyRtpDataChannel(rtp_data_channel);
95 cm_->Terminate();
96 }
97
98 std::unique_ptr<DtlsTransportInternal> rtp_dtls_transport_;
99 std::unique_ptr<rtc::Thread> network_;
100 std::unique_ptr<rtc::Thread> worker_;
101 std::unique_ptr<webrtc::VideoBitrateAllocatorFactory>
102 video_bitrate_allocator_factory_;
103 // |fme_| and |fdme_| are actually owned by |cm_|.
104 cricket::FakeMediaEngine* fme_;
105 cricket::FakeDataEngine* fdme_;
106 std::unique_ptr<cricket::ChannelManager> cm_;
107 cricket::FakeCall fake_call_;
108 rtc::UniqueRandomIdGenerator ssrc_generator_;
109 };
110
111 // Test that we startup/shutdown properly.
TEST_F(ChannelManagerTest,StartupShutdown)112 TEST_F(ChannelManagerTest, StartupShutdown) {
113 EXPECT_FALSE(cm_->initialized());
114 EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread());
115 EXPECT_TRUE(cm_->Init());
116 EXPECT_TRUE(cm_->initialized());
117 cm_->Terminate();
118 EXPECT_FALSE(cm_->initialized());
119 }
120
121 // Test that we startup/shutdown properly with a worker thread.
TEST_F(ChannelManagerTest,StartupShutdownOnThread)122 TEST_F(ChannelManagerTest, StartupShutdownOnThread) {
123 network_->Start();
124 worker_->Start();
125 EXPECT_FALSE(cm_->initialized());
126 EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread());
127 EXPECT_TRUE(cm_->set_network_thread(network_.get()));
128 EXPECT_EQ(network_.get(), cm_->network_thread());
129 EXPECT_TRUE(cm_->set_worker_thread(worker_.get()));
130 EXPECT_EQ(worker_.get(), cm_->worker_thread());
131 EXPECT_TRUE(cm_->Init());
132 EXPECT_TRUE(cm_->initialized());
133 // Setting the network or worker thread while initialized should fail.
134 EXPECT_FALSE(cm_->set_network_thread(rtc::Thread::Current()));
135 EXPECT_FALSE(cm_->set_worker_thread(rtc::Thread::Current()));
136 cm_->Terminate();
137 EXPECT_FALSE(cm_->initialized());
138 }
139
TEST_F(ChannelManagerTest,SetVideoRtxEnabled)140 TEST_F(ChannelManagerTest, SetVideoRtxEnabled) {
141 std::vector<VideoCodec> send_codecs;
142 std::vector<VideoCodec> recv_codecs;
143 const VideoCodec rtx_codec(96, "rtx");
144
145 // By default RTX is disabled.
146 cm_->GetSupportedVideoSendCodecs(&send_codecs);
147 EXPECT_FALSE(ContainsMatchingCodec(send_codecs, rtx_codec));
148 cm_->GetSupportedVideoSendCodecs(&recv_codecs);
149 EXPECT_FALSE(ContainsMatchingCodec(recv_codecs, rtx_codec));
150
151 // Enable and check.
152 EXPECT_TRUE(cm_->SetVideoRtxEnabled(true));
153 cm_->GetSupportedVideoSendCodecs(&send_codecs);
154 EXPECT_TRUE(ContainsMatchingCodec(send_codecs, rtx_codec));
155 cm_->GetSupportedVideoSendCodecs(&recv_codecs);
156 EXPECT_TRUE(ContainsMatchingCodec(recv_codecs, rtx_codec));
157
158 // Disable and check.
159 EXPECT_TRUE(cm_->SetVideoRtxEnabled(false));
160 cm_->GetSupportedVideoSendCodecs(&send_codecs);
161 EXPECT_FALSE(ContainsMatchingCodec(send_codecs, rtx_codec));
162 cm_->GetSupportedVideoSendCodecs(&recv_codecs);
163 EXPECT_FALSE(ContainsMatchingCodec(recv_codecs, rtx_codec));
164
165 // Cannot toggle rtx after initialization.
166 EXPECT_TRUE(cm_->Init());
167 EXPECT_FALSE(cm_->SetVideoRtxEnabled(true));
168 EXPECT_FALSE(cm_->SetVideoRtxEnabled(false));
169
170 // Can set again after terminate.
171 cm_->Terminate();
172 EXPECT_TRUE(cm_->SetVideoRtxEnabled(true));
173 cm_->GetSupportedVideoSendCodecs(&send_codecs);
174 EXPECT_TRUE(ContainsMatchingCodec(send_codecs, rtx_codec));
175 cm_->GetSupportedVideoSendCodecs(&recv_codecs);
176 EXPECT_TRUE(ContainsMatchingCodec(recv_codecs, rtx_codec));
177 }
178
TEST_F(ChannelManagerTest,CreateDestroyChannels)179 TEST_F(ChannelManagerTest, CreateDestroyChannels) {
180 EXPECT_TRUE(cm_->Init());
181 auto rtp_transport = CreateDtlsSrtpTransport();
182 TestCreateDestroyChannels(rtp_transport.get());
183 }
184
TEST_F(ChannelManagerTest,CreateDestroyChannelsOnThread)185 TEST_F(ChannelManagerTest, CreateDestroyChannelsOnThread) {
186 network_->Start();
187 worker_->Start();
188 EXPECT_TRUE(cm_->set_worker_thread(worker_.get()));
189 EXPECT_TRUE(cm_->set_network_thread(network_.get()));
190 EXPECT_TRUE(cm_->Init());
191 auto rtp_transport = CreateDtlsSrtpTransport();
192 TestCreateDestroyChannels(rtp_transport.get());
193 }
194
195 } // namespace cricket
196