blob: 8cb066b25df3eb61db5c6b3cf03745863017e7d1 [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[] = {
deadbeef67cf2c12016-04-13 10:07:16 -070025 AudioCodec(97, "voice", 1, 2, 3), AudioCodec(111, "OPUS", 48000, 32000, 2),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000026};
27
28static const VideoCodec kVideoCodecs[] = {
deadbeef67cf2c12016-04-13 10:07:16 -070029 VideoCodec(99, "H264", 100, 200, 300),
30 VideoCodec(100, "VP8", 100, 200, 300), VideoCodec(96, "rtx", 100, 200, 300),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031};
32
33class ChannelManagerTest : public testing::Test {
34 protected:
deadbeefcbecd352015-09-23 11:50:27 -070035 ChannelManagerTest()
stefanc1aeaf02015-10-15 07:26:07 -070036 : fme_(new cricket::FakeMediaEngine()),
37 fdme_(new cricket::FakeDataEngine()),
stefanc1aeaf02015-10-15 07:26:07 -070038 cm_(new cricket::ChannelManager(fme_,
39 fdme_,
stefanc1aeaf02015-10-15 07:26:07 -070040 rtc::Thread::Current())),
41 fake_call_(webrtc::Call::Config()),
42 fake_mc_(cm_, &fake_call_),
43 transport_controller_(
44 new cricket::FakeTransportController(ICEROLE_CONTROLLING)) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045
46 virtual void SetUp() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047 fme_->SetAudioCodecs(MAKE_VECTOR(kAudioCodecs));
48 fme_->SetVideoCodecs(MAKE_VECTOR(kVideoCodecs));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049 }
50
51 virtual void TearDown() {
deadbeefcbecd352015-09-23 11:50:27 -070052 delete transport_controller_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053 delete cm_;
54 cm_ = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055 fdme_ = NULL;
56 fme_ = NULL;
57 }
58
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000059 rtc::Thread worker_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060 cricket::FakeMediaEngine* fme_;
61 cricket::FakeDataEngine* fdme_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062 cricket::ChannelManager* cm_;
stefanc1aeaf02015-10-15 07:26:07 -070063 cricket::FakeCall fake_call_;
64 cricket::FakeMediaController fake_mc_;
deadbeefcbecd352015-09-23 11:50:27 -070065 cricket::FakeTransportController* transport_controller_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066};
67
68// Test that we startup/shutdown properly.
69TEST_F(ChannelManagerTest, StartupShutdown) {
70 EXPECT_FALSE(cm_->initialized());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000071 EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072 EXPECT_TRUE(cm_->Init());
73 EXPECT_TRUE(cm_->initialized());
74 cm_->Terminate();
75 EXPECT_FALSE(cm_->initialized());
76}
77
78// Test that we startup/shutdown properly with a worker thread.
79TEST_F(ChannelManagerTest, StartupShutdownOnThread) {
80 worker_.Start();
81 EXPECT_FALSE(cm_->initialized());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000082 EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083 EXPECT_TRUE(cm_->set_worker_thread(&worker_));
84 EXPECT_EQ(&worker_, cm_->worker_thread());
85 EXPECT_TRUE(cm_->Init());
86 EXPECT_TRUE(cm_->initialized());
87 // Setting the worker thread while initialized should fail.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000088 EXPECT_FALSE(cm_->set_worker_thread(rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 cm_->Terminate();
90 EXPECT_FALSE(cm_->initialized());
91}
92
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093// Test that we can create and destroy a voice and video channel.
94TEST_F(ChannelManagerTest, CreateDestroyChannels) {
95 EXPECT_TRUE(cm_->Init());
deadbeefcbecd352015-09-23 11:50:27 -070096 cricket::VoiceChannel* voice_channel =
97 cm_->CreateVoiceChannel(&fake_mc_, transport_controller_,
98 cricket::CN_AUDIO, false, AudioOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +020099 EXPECT_TRUE(voice_channel != nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700100 cricket::VideoChannel* video_channel =
101 cm_->CreateVideoChannel(&fake_mc_, transport_controller_,
102 cricket::CN_VIDEO, false, VideoOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200103 EXPECT_TRUE(video_channel != nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700104 cricket::DataChannel* data_channel = cm_->CreateDataChannel(
105 transport_controller_, cricket::CN_DATA, false, cricket::DCT_RTP);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200106 EXPECT_TRUE(data_channel != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107 cm_->DestroyVideoChannel(video_channel);
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200108 cm_->DestroyVoiceChannel(voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109 cm_->DestroyDataChannel(data_channel);
110 cm_->Terminate();
111}
112
113// Test that we can create and destroy a voice and video channel with a worker.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000114TEST_F(ChannelManagerTest, CreateDestroyChannelsOnThread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 worker_.Start();
116 EXPECT_TRUE(cm_->set_worker_thread(&worker_));
117 EXPECT_TRUE(cm_->Init());
deadbeefcbecd352015-09-23 11:50:27 -0700118 delete transport_controller_;
119 transport_controller_ =
120 new cricket::FakeTransportController(&worker_, ICEROLE_CONTROLLING);
121 cricket::VoiceChannel* voice_channel =
122 cm_->CreateVoiceChannel(&fake_mc_, transport_controller_,
123 cricket::CN_AUDIO, false, AudioOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200124 EXPECT_TRUE(voice_channel != nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700125 cricket::VideoChannel* video_channel =
126 cm_->CreateVideoChannel(&fake_mc_, transport_controller_,
127 cricket::CN_VIDEO, false, VideoOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200128 EXPECT_TRUE(video_channel != nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700129 cricket::DataChannel* data_channel = cm_->CreateDataChannel(
130 transport_controller_, cricket::CN_DATA, false, cricket::DCT_RTP);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200131 EXPECT_TRUE(data_channel != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132 cm_->DestroyVideoChannel(video_channel);
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200133 cm_->DestroyVoiceChannel(voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 cm_->DestroyDataChannel(data_channel);
135 cm_->Terminate();
136}
137
138// Test that we fail to create a voice/video channel if the session is unable
139// to create a cricket::TransportChannel
140TEST_F(ChannelManagerTest, NoTransportChannelTest) {
141 EXPECT_TRUE(cm_->Init());
deadbeefcbecd352015-09-23 11:50:27 -0700142 transport_controller_->set_fail_channel_creation(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143 // The test is useless unless the session does not fail creating
144 // cricket::TransportChannel.
deadbeefcbecd352015-09-23 11:50:27 -0700145 ASSERT_TRUE(transport_controller_->CreateTransportChannel_w(
Jelena Marusicc28a8962015-05-29 15:05:44 +0200146 "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147
deadbeefcbecd352015-09-23 11:50:27 -0700148 cricket::VoiceChannel* voice_channel =
149 cm_->CreateVoiceChannel(&fake_mc_, transport_controller_,
150 cricket::CN_AUDIO, false, AudioOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200151 EXPECT_TRUE(voice_channel == nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700152 cricket::VideoChannel* video_channel =
153 cm_->CreateVideoChannel(&fake_mc_, transport_controller_,
154 cricket::CN_VIDEO, false, VideoOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200155 EXPECT_TRUE(video_channel == nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700156 cricket::DataChannel* data_channel = cm_->CreateDataChannel(
157 transport_controller_, cricket::CN_DATA, false, cricket::DCT_RTP);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200158 EXPECT_TRUE(data_channel == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159 cm_->Terminate();
160}
161
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162TEST_F(ChannelManagerTest, GetSetOutputVolumeBeforeInit) {
163 int level;
164 // Before init, SetOutputVolume() remembers the volume but does not change the
165 // volume of the engine. GetOutputVolume() should fail.
166 EXPECT_EQ(-1, fme_->output_volume());
167 EXPECT_FALSE(cm_->GetOutputVolume(&level));
168 EXPECT_FALSE(cm_->SetOutputVolume(-1)); // Invalid volume.
169 EXPECT_TRUE(cm_->SetOutputVolume(99));
170 EXPECT_EQ(-1, fme_->output_volume());
171
172 // Init() will apply the remembered volume.
173 EXPECT_TRUE(cm_->Init());
174 EXPECT_TRUE(cm_->GetOutputVolume(&level));
175 EXPECT_EQ(99, level);
176 EXPECT_EQ(level, fme_->output_volume());
177
178 EXPECT_TRUE(cm_->SetOutputVolume(60));
179 EXPECT_TRUE(cm_->GetOutputVolume(&level));
180 EXPECT_EQ(60, level);
181 EXPECT_EQ(level, fme_->output_volume());
182}
183
184TEST_F(ChannelManagerTest, GetSetOutputVolume) {
185 int level;
186 EXPECT_TRUE(cm_->Init());
187 EXPECT_TRUE(cm_->GetOutputVolume(&level));
188 EXPECT_EQ(level, fme_->output_volume());
189
190 EXPECT_FALSE(cm_->SetOutputVolume(-1)); // Invalid volume.
191 EXPECT_TRUE(cm_->SetOutputVolume(60));
192 EXPECT_EQ(60, fme_->output_volume());
193 EXPECT_TRUE(cm_->GetOutputVolume(&level));
194 EXPECT_EQ(60, level);
195}
196
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197TEST_F(ChannelManagerTest, SetVideoRtxEnabled) {
198 std::vector<VideoCodec> codecs;
deadbeef67cf2c12016-04-13 10:07:16 -0700199 const VideoCodec rtx_codec(96, "rtx", 0, 0, 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200
201 // By default RTX is disabled.
202 cm_->GetSupportedVideoCodecs(&codecs);
203 EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec));
204
205 // Enable and check.
206 EXPECT_TRUE(cm_->SetVideoRtxEnabled(true));
207 cm_->GetSupportedVideoCodecs(&codecs);
208 EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec));
209
210 // Disable and check.
211 EXPECT_TRUE(cm_->SetVideoRtxEnabled(false));
212 cm_->GetSupportedVideoCodecs(&codecs);
213 EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec));
214
215 // Cannot toggle rtx after initialization.
216 EXPECT_TRUE(cm_->Init());
217 EXPECT_FALSE(cm_->SetVideoRtxEnabled(true));
218 EXPECT_FALSE(cm_->SetVideoRtxEnabled(false));
219
220 // Can set again after terminate.
221 cm_->Terminate();
222 EXPECT_TRUE(cm_->SetVideoRtxEnabled(true));
223 cm_->GetSupportedVideoCodecs(&codecs);
224 EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec));
225}
226
227} // namespace cricket