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 "webrtc/api/fakemediacontroller.h"
12 #include "webrtc/base/gunit.h"
13 #include "webrtc/base/logging.h"
14 #include "webrtc/base/thread.h"
15 #include "webrtc/logging/rtc_event_log/rtc_event_log.h"
16 #include "webrtc/media/base/fakemediaengine.h"
17 #include "webrtc/media/base/fakevideocapturer.h"
18 #include "webrtc/media/base/testutils.h"
19 #include "webrtc/media/engine/fakewebrtccall.h"
20 #include "webrtc/p2p/base/faketransportcontroller.h"
21 #include "webrtc/pc/channelmanager.h"
22 
23 namespace cricket {
24 const bool kDefaultRtcpMuxRequired = true;
25 const bool kDefaultSrtpRequired = true;
26 }
27 
28 namespace cricket {
29 
30 static const AudioCodec kAudioCodecs[] = {
31     AudioCodec(97, "voice", 1, 2, 3), AudioCodec(111, "OPUS", 48000, 32000, 2),
32 };
33 
34 static const VideoCodec kVideoCodecs[] = {
35     VideoCodec(99, "H264"), VideoCodec(100, "VP8"), VideoCodec(96, "rtx"),
36 };
37 
38 class ChannelManagerTest : public testing::Test {
39  protected:
ChannelManagerTest()40   ChannelManagerTest()
41       : fme_(new cricket::FakeMediaEngine()),
42         fdme_(new cricket::FakeDataEngine()),
43         cm_(new cricket::ChannelManager(fme_, fdme_, rtc::Thread::Current())),
44         fake_call_(webrtc::Call::Config(&event_log_)),
45         fake_mc_(cm_, &fake_call_),
46         transport_controller_(
47             new cricket::FakeTransportController(ICEROLE_CONTROLLING)) {}
48 
SetUp()49   virtual void SetUp() {
50     fme_->SetAudioCodecs(MAKE_VECTOR(kAudioCodecs));
51     fme_->SetVideoCodecs(MAKE_VECTOR(kVideoCodecs));
52   }
53 
TearDown()54   virtual void TearDown() {
55     delete transport_controller_;
56     delete cm_;
57     cm_ = NULL;
58     fdme_ = NULL;
59     fme_ = NULL;
60   }
61 
62   webrtc::RtcEventLogNullImpl event_log_;
63   rtc::Thread network_;
64   rtc::Thread worker_;
65   cricket::FakeMediaEngine* fme_;
66   cricket::FakeDataEngine* fdme_;
67   cricket::ChannelManager* cm_;
68   cricket::FakeCall fake_call_;
69   cricket::FakeMediaController fake_mc_;
70   cricket::FakeTransportController* transport_controller_;
71 };
72 
73 // Test that we startup/shutdown properly.
TEST_F(ChannelManagerTest,StartupShutdown)74 TEST_F(ChannelManagerTest, StartupShutdown) {
75   EXPECT_FALSE(cm_->initialized());
76   EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread());
77   EXPECT_TRUE(cm_->Init());
78   EXPECT_TRUE(cm_->initialized());
79   cm_->Terminate();
80   EXPECT_FALSE(cm_->initialized());
81 }
82 
83 // Test that we startup/shutdown properly with a worker thread.
TEST_F(ChannelManagerTest,StartupShutdownOnThread)84 TEST_F(ChannelManagerTest, StartupShutdownOnThread) {
85   network_.Start();
86   worker_.Start();
87   EXPECT_FALSE(cm_->initialized());
88   EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread());
89   EXPECT_TRUE(cm_->set_network_thread(&network_));
90   EXPECT_EQ(&network_, cm_->network_thread());
91   EXPECT_TRUE(cm_->set_worker_thread(&worker_));
92   EXPECT_EQ(&worker_, cm_->worker_thread());
93   EXPECT_TRUE(cm_->Init());
94   EXPECT_TRUE(cm_->initialized());
95   // Setting the network or worker thread while initialized should fail.
96   EXPECT_FALSE(cm_->set_network_thread(rtc::Thread::Current()));
97   EXPECT_FALSE(cm_->set_worker_thread(rtc::Thread::Current()));
98   cm_->Terminate();
99   EXPECT_FALSE(cm_->initialized());
100 }
101 
102 // Test that we can create and destroy a voice and video channel.
TEST_F(ChannelManagerTest,CreateDestroyChannels)103 TEST_F(ChannelManagerTest, CreateDestroyChannels) {
104   EXPECT_TRUE(cm_->Init());
105   cricket::TransportChannel* rtp_transport =
106       transport_controller_->CreateTransportChannel(
107           cricket::CN_AUDIO, cricket::ICE_CANDIDATE_COMPONENT_RTP);
108   cricket::VoiceChannel* voice_channel = cm_->CreateVoiceChannel(
109       &fake_mc_, rtp_transport, nullptr /*rtcp_transport*/,
110       rtc::Thread::Current(), cricket::CN_AUDIO, nullptr,
111       kDefaultRtcpMuxRequired, kDefaultSrtpRequired, AudioOptions());
112   EXPECT_TRUE(voice_channel != nullptr);
113   cricket::VideoChannel* video_channel = cm_->CreateVideoChannel(
114       &fake_mc_, rtp_transport, nullptr /*rtcp_transport*/,
115       rtc::Thread::Current(), cricket::CN_VIDEO, nullptr,
116       kDefaultRtcpMuxRequired, kDefaultSrtpRequired, VideoOptions());
117   EXPECT_TRUE(video_channel != nullptr);
118   cricket::RtpDataChannel* rtp_data_channel = cm_->CreateRtpDataChannel(
119       &fake_mc_, rtp_transport, nullptr /*rtcp_transport*/,
120       rtc::Thread::Current(), cricket::CN_DATA, nullptr,
121       kDefaultRtcpMuxRequired, kDefaultSrtpRequired);
122   EXPECT_TRUE(rtp_data_channel != nullptr);
123   cm_->DestroyVideoChannel(video_channel);
124   cm_->DestroyVoiceChannel(voice_channel);
125   cm_->DestroyRtpDataChannel(rtp_data_channel);
126   cm_->Terminate();
127 }
128 
129 // Test that we can create and destroy a voice and video channel with a worker.
TEST_F(ChannelManagerTest,CreateDestroyChannelsOnThread)130 TEST_F(ChannelManagerTest, CreateDestroyChannelsOnThread) {
131   network_.Start();
132   worker_.Start();
133   EXPECT_TRUE(cm_->set_worker_thread(&worker_));
134   EXPECT_TRUE(cm_->set_network_thread(&network_));
135   EXPECT_TRUE(cm_->Init());
136   delete transport_controller_;
137   transport_controller_ =
138       new cricket::FakeTransportController(&network_, ICEROLE_CONTROLLING);
139   cricket::TransportChannel* rtp_transport =
140       transport_controller_->CreateTransportChannel(
141           cricket::CN_AUDIO, cricket::ICE_CANDIDATE_COMPONENT_RTP);
142   cricket::VoiceChannel* voice_channel = cm_->CreateVoiceChannel(
143       &fake_mc_, rtp_transport, nullptr /*rtcp_transport*/,
144       rtc::Thread::Current(), cricket::CN_AUDIO, nullptr,
145       kDefaultRtcpMuxRequired, kDefaultSrtpRequired, AudioOptions());
146   EXPECT_TRUE(voice_channel != nullptr);
147   cricket::VideoChannel* video_channel = cm_->CreateVideoChannel(
148       &fake_mc_, rtp_transport, nullptr /*rtcp_transport*/,
149       rtc::Thread::Current(), cricket::CN_VIDEO, nullptr,
150       kDefaultRtcpMuxRequired, kDefaultSrtpRequired, VideoOptions());
151   EXPECT_TRUE(video_channel != nullptr);
152   cricket::RtpDataChannel* rtp_data_channel = cm_->CreateRtpDataChannel(
153       &fake_mc_, rtp_transport, nullptr /*rtcp_transport*/,
154       rtc::Thread::Current(), cricket::CN_DATA, nullptr,
155       kDefaultRtcpMuxRequired, kDefaultSrtpRequired);
156   EXPECT_TRUE(rtp_data_channel != nullptr);
157   cm_->DestroyVideoChannel(video_channel);
158   cm_->DestroyVoiceChannel(voice_channel);
159   cm_->DestroyRtpDataChannel(rtp_data_channel);
160   cm_->Terminate();
161 }
162 
TEST_F(ChannelManagerTest,SetVideoRtxEnabled)163 TEST_F(ChannelManagerTest, SetVideoRtxEnabled) {
164   std::vector<VideoCodec> codecs;
165   const VideoCodec rtx_codec(96, "rtx");
166 
167   // By default RTX is disabled.
168   cm_->GetSupportedVideoCodecs(&codecs);
169   EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec));
170 
171   // Enable and check.
172   EXPECT_TRUE(cm_->SetVideoRtxEnabled(true));
173   cm_->GetSupportedVideoCodecs(&codecs);
174   EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec));
175 
176   // Disable and check.
177   EXPECT_TRUE(cm_->SetVideoRtxEnabled(false));
178   cm_->GetSupportedVideoCodecs(&codecs);
179   EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec));
180 
181   // Cannot toggle rtx after initialization.
182   EXPECT_TRUE(cm_->Init());
183   EXPECT_FALSE(cm_->SetVideoRtxEnabled(true));
184   EXPECT_FALSE(cm_->SetVideoRtxEnabled(false));
185 
186   // Can set again after terminate.
187   cm_->Terminate();
188   EXPECT_TRUE(cm_->SetVideoRtxEnabled(true));
189   cm_->GetSupportedVideoCodecs(&codecs);
190   EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec));
191 }
192 
193 }  // namespace cricket
194