blob: 1eb09198d9f7f43f88baa980f673dd866663cad6 [file] [log] [blame]
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00001/*
kjellander65c7f672016-02-12 00:05:01 -08002 * Copyright 2008 The WebRTC project authors. All Rights Reserved.
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 *
kjellander65c7f672016-02-12 00:05:01 -08004 * 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.
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00009 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +000010
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#include "webrtc/api/fakemediacontroller.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000012#include "webrtc/base/gunit.h"
13#include "webrtc/base/logging.h"
14#include "webrtc/base/thread.h"
kjellandera96e2d72016-02-04 23:52:28 -080015#include "webrtc/media/base/fakemediaengine.h"
16#include "webrtc/media/base/fakevideocapturer.h"
17#include "webrtc/media/base/testutils.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010018#include "webrtc/media/engine/fakewebrtccall.h"
deadbeefcbecd352015-09-23 11:50:27 -070019#include "webrtc/p2p/base/faketransportcontroller.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010020#include "webrtc/pc/channelmanager.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021
22namespace cricket {
23
24static const AudioCodec kAudioCodecs[] = {
25 AudioCodec(97, "voice", 1, 2, 3, 0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000026 AudioCodec(111, "OPUS", 48000, 32000, 2, 0),
27};
28
29static const VideoCodec kVideoCodecs[] = {
30 VideoCodec(99, "H264", 100, 200, 300, 0),
31 VideoCodec(100, "VP8", 100, 200, 300, 0),
32 VideoCodec(96, "rtx", 100, 200, 300, 0),
33};
34
35class ChannelManagerTest : public testing::Test {
36 protected:
deadbeefcbecd352015-09-23 11:50:27 -070037 ChannelManagerTest()
stefanc1aeaf02015-10-15 07:26:07 -070038 : fme_(new cricket::FakeMediaEngine()),
39 fdme_(new cricket::FakeDataEngine()),
stefanc1aeaf02015-10-15 07:26:07 -070040 cm_(new cricket::ChannelManager(fme_,
41 fdme_,
stefanc1aeaf02015-10-15 07:26:07 -070042 rtc::Thread::Current())),
43 fake_call_(webrtc::Call::Config()),
44 fake_mc_(cm_, &fake_call_),
45 transport_controller_(
46 new cricket::FakeTransportController(ICEROLE_CONTROLLING)) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047
48 virtual void SetUp() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049 fme_->SetAudioCodecs(MAKE_VECTOR(kAudioCodecs));
50 fme_->SetVideoCodecs(MAKE_VECTOR(kVideoCodecs));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051 }
52
53 virtual void TearDown() {
deadbeefcbecd352015-09-23 11:50:27 -070054 delete transport_controller_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055 delete cm_;
56 cm_ = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057 fdme_ = NULL;
58 fme_ = NULL;
59 }
60
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000061 rtc::Thread worker_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062 cricket::FakeMediaEngine* fme_;
63 cricket::FakeDataEngine* fdme_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064 cricket::ChannelManager* cm_;
stefanc1aeaf02015-10-15 07:26:07 -070065 cricket::FakeCall fake_call_;
66 cricket::FakeMediaController fake_mc_;
deadbeefcbecd352015-09-23 11:50:27 -070067 cricket::FakeTransportController* transport_controller_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068};
69
70// Test that we startup/shutdown properly.
71TEST_F(ChannelManagerTest, StartupShutdown) {
72 EXPECT_FALSE(cm_->initialized());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000073 EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074 EXPECT_TRUE(cm_->Init());
75 EXPECT_TRUE(cm_->initialized());
76 cm_->Terminate();
77 EXPECT_FALSE(cm_->initialized());
78}
79
80// Test that we startup/shutdown properly with a worker thread.
81TEST_F(ChannelManagerTest, StartupShutdownOnThread) {
82 worker_.Start();
83 EXPECT_FALSE(cm_->initialized());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000084 EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085 EXPECT_TRUE(cm_->set_worker_thread(&worker_));
86 EXPECT_EQ(&worker_, cm_->worker_thread());
87 EXPECT_TRUE(cm_->Init());
88 EXPECT_TRUE(cm_->initialized());
89 // Setting the worker thread while initialized should fail.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000090 EXPECT_FALSE(cm_->set_worker_thread(rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 cm_->Terminate();
92 EXPECT_FALSE(cm_->initialized());
93}
94
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095// Test that we can create and destroy a voice and video channel.
96TEST_F(ChannelManagerTest, CreateDestroyChannels) {
97 EXPECT_TRUE(cm_->Init());
deadbeefcbecd352015-09-23 11:50:27 -070098 cricket::VoiceChannel* voice_channel =
99 cm_->CreateVoiceChannel(&fake_mc_, transport_controller_,
100 cricket::CN_AUDIO, false, AudioOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200101 EXPECT_TRUE(voice_channel != nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700102 cricket::VideoChannel* video_channel =
103 cm_->CreateVideoChannel(&fake_mc_, transport_controller_,
104 cricket::CN_VIDEO, false, VideoOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200105 EXPECT_TRUE(video_channel != nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700106 cricket::DataChannel* data_channel = cm_->CreateDataChannel(
107 transport_controller_, cricket::CN_DATA, false, cricket::DCT_RTP);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200108 EXPECT_TRUE(data_channel != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109 cm_->DestroyVideoChannel(video_channel);
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200110 cm_->DestroyVoiceChannel(voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111 cm_->DestroyDataChannel(data_channel);
112 cm_->Terminate();
113}
114
115// Test that we can create and destroy a voice and video channel with a worker.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000116TEST_F(ChannelManagerTest, CreateDestroyChannelsOnThread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117 worker_.Start();
118 EXPECT_TRUE(cm_->set_worker_thread(&worker_));
119 EXPECT_TRUE(cm_->Init());
deadbeefcbecd352015-09-23 11:50:27 -0700120 delete transport_controller_;
121 transport_controller_ =
122 new cricket::FakeTransportController(&worker_, ICEROLE_CONTROLLING);
123 cricket::VoiceChannel* voice_channel =
124 cm_->CreateVoiceChannel(&fake_mc_, transport_controller_,
125 cricket::CN_AUDIO, false, AudioOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200126 EXPECT_TRUE(voice_channel != nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700127 cricket::VideoChannel* video_channel =
128 cm_->CreateVideoChannel(&fake_mc_, transport_controller_,
129 cricket::CN_VIDEO, false, VideoOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200130 EXPECT_TRUE(video_channel != nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700131 cricket::DataChannel* data_channel = cm_->CreateDataChannel(
132 transport_controller_, cricket::CN_DATA, false, cricket::DCT_RTP);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200133 EXPECT_TRUE(data_channel != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 cm_->DestroyVideoChannel(video_channel);
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200135 cm_->DestroyVoiceChannel(voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 cm_->DestroyDataChannel(data_channel);
137 cm_->Terminate();
138}
139
140// Test that we fail to create a voice/video channel if the session is unable
141// to create a cricket::TransportChannel
142TEST_F(ChannelManagerTest, NoTransportChannelTest) {
143 EXPECT_TRUE(cm_->Init());
deadbeefcbecd352015-09-23 11:50:27 -0700144 transport_controller_->set_fail_channel_creation(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145 // The test is useless unless the session does not fail creating
146 // cricket::TransportChannel.
deadbeefcbecd352015-09-23 11:50:27 -0700147 ASSERT_TRUE(transport_controller_->CreateTransportChannel_w(
Jelena Marusicc28a8962015-05-29 15:05:44 +0200148 "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149
deadbeefcbecd352015-09-23 11:50:27 -0700150 cricket::VoiceChannel* voice_channel =
151 cm_->CreateVoiceChannel(&fake_mc_, transport_controller_,
152 cricket::CN_AUDIO, false, AudioOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200153 EXPECT_TRUE(voice_channel == nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700154 cricket::VideoChannel* video_channel =
155 cm_->CreateVideoChannel(&fake_mc_, transport_controller_,
156 cricket::CN_VIDEO, false, VideoOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200157 EXPECT_TRUE(video_channel == nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700158 cricket::DataChannel* data_channel = cm_->CreateDataChannel(
159 transport_controller_, cricket::CN_DATA, false, cricket::DCT_RTP);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200160 EXPECT_TRUE(data_channel == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161 cm_->Terminate();
162}
163
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164TEST_F(ChannelManagerTest, GetSetOutputVolumeBeforeInit) {
165 int level;
166 // Before init, SetOutputVolume() remembers the volume but does not change the
167 // volume of the engine. GetOutputVolume() should fail.
168 EXPECT_EQ(-1, fme_->output_volume());
169 EXPECT_FALSE(cm_->GetOutputVolume(&level));
170 EXPECT_FALSE(cm_->SetOutputVolume(-1)); // Invalid volume.
171 EXPECT_TRUE(cm_->SetOutputVolume(99));
172 EXPECT_EQ(-1, fme_->output_volume());
173
174 // Init() will apply the remembered volume.
175 EXPECT_TRUE(cm_->Init());
176 EXPECT_TRUE(cm_->GetOutputVolume(&level));
177 EXPECT_EQ(99, level);
178 EXPECT_EQ(level, fme_->output_volume());
179
180 EXPECT_TRUE(cm_->SetOutputVolume(60));
181 EXPECT_TRUE(cm_->GetOutputVolume(&level));
182 EXPECT_EQ(60, level);
183 EXPECT_EQ(level, fme_->output_volume());
184}
185
186TEST_F(ChannelManagerTest, GetSetOutputVolume) {
187 int level;
188 EXPECT_TRUE(cm_->Init());
189 EXPECT_TRUE(cm_->GetOutputVolume(&level));
190 EXPECT_EQ(level, fme_->output_volume());
191
192 EXPECT_FALSE(cm_->SetOutputVolume(-1)); // Invalid volume.
193 EXPECT_TRUE(cm_->SetOutputVolume(60));
194 EXPECT_EQ(60, fme_->output_volume());
195 EXPECT_TRUE(cm_->GetOutputVolume(&level));
196 EXPECT_EQ(60, level);
197}
198
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199TEST_F(ChannelManagerTest, SetVideoRtxEnabled) {
200 std::vector<VideoCodec> codecs;
201 const VideoCodec rtx_codec(96, "rtx", 0, 0, 0, 0);
202
203 // By default RTX is disabled.
204 cm_->GetSupportedVideoCodecs(&codecs);
205 EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec));
206
207 // Enable and check.
208 EXPECT_TRUE(cm_->SetVideoRtxEnabled(true));
209 cm_->GetSupportedVideoCodecs(&codecs);
210 EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec));
211
212 // Disable and check.
213 EXPECT_TRUE(cm_->SetVideoRtxEnabled(false));
214 cm_->GetSupportedVideoCodecs(&codecs);
215 EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec));
216
217 // Cannot toggle rtx after initialization.
218 EXPECT_TRUE(cm_->Init());
219 EXPECT_FALSE(cm_->SetVideoRtxEnabled(true));
220 EXPECT_FALSE(cm_->SetVideoRtxEnabled(false));
221
222 // Can set again after terminate.
223 cm_->Terminate();
224 EXPECT_TRUE(cm_->SetVideoRtxEnabled(true));
225 cm_->GetSupportedVideoCodecs(&codecs);
226 EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec));
227}
228
229} // namespace cricket