blob: 174d064b0543b0da6fd8cd10ddf4a02910d79723 [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"
skvlad11a9cbf2016-10-07 11:53:05 -070015#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
kjellandera96e2d72016-02-04 23:52:28 -080016#include "webrtc/media/base/fakemediaengine.h"
17#include "webrtc/media/base/fakevideocapturer.h"
18#include "webrtc/media/base/testutils.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010019#include "webrtc/media/engine/fakewebrtccall.h"
deadbeefcbecd352015-09-23 11:50:27 -070020#include "webrtc/p2p/base/faketransportcontroller.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010021#include "webrtc/pc/channelmanager.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022
23namespace cricket {
24
25static const AudioCodec kAudioCodecs[] = {
deadbeef67cf2c12016-04-13 10:07:16 -070026 AudioCodec(97, "voice", 1, 2, 3), AudioCodec(111, "OPUS", 48000, 32000, 2),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000027};
28
29static const VideoCodec kVideoCodecs[] = {
perkj26752742016-10-24 01:21:16 -070030 VideoCodec(99, "H264"), VideoCodec(100, "VP8"), VideoCodec(96, "rtx"),
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()),
skvlad11a9cbf2016-10-07 11:53:05 -070038 cm_(new cricket::ChannelManager(fme_, fdme_, rtc::Thread::Current())),
39 fake_call_(webrtc::Call::Config(&event_log_)),
stefanc1aeaf02015-10-15 07:26:07 -070040 fake_mc_(cm_, &fake_call_),
41 transport_controller_(
42 new cricket::FakeTransportController(ICEROLE_CONTROLLING)) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043
44 virtual void SetUp() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045 fme_->SetAudioCodecs(MAKE_VECTOR(kAudioCodecs));
46 fme_->SetVideoCodecs(MAKE_VECTOR(kVideoCodecs));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047 }
48
49 virtual void TearDown() {
deadbeefcbecd352015-09-23 11:50:27 -070050 delete transport_controller_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051 delete cm_;
52 cm_ = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053 fdme_ = NULL;
54 fme_ = NULL;
55 }
56
skvlad11a9cbf2016-10-07 11:53:05 -070057 webrtc::RtcEventLogNullImpl event_log_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +020058 rtc::Thread network_;
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) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +020080 network_.Start();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081 worker_.Start();
82 EXPECT_FALSE(cm_->initialized());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000083 EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread());
Danil Chapovalov33b01f22016-05-11 19:55:27 +020084 EXPECT_TRUE(cm_->set_network_thread(&network_));
85 EXPECT_EQ(&network_, cm_->network_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 EXPECT_TRUE(cm_->set_worker_thread(&worker_));
87 EXPECT_EQ(&worker_, cm_->worker_thread());
88 EXPECT_TRUE(cm_->Init());
89 EXPECT_TRUE(cm_->initialized());
Danil Chapovalov33b01f22016-05-11 19:55:27 +020090 // Setting the network or worker thread while initialized should fail.
91 EXPECT_FALSE(cm_->set_network_thread(rtc::Thread::Current()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000092 EXPECT_FALSE(cm_->set_worker_thread(rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 cm_->Terminate();
94 EXPECT_FALSE(cm_->initialized());
95}
96
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097// Test that we can create and destroy a voice and video channel.
98TEST_F(ChannelManagerTest, CreateDestroyChannels) {
99 EXPECT_TRUE(cm_->Init());
skvlad6c87a672016-05-17 17:49:52 -0700100 cricket::VoiceChannel* voice_channel = cm_->CreateVoiceChannel(
101 &fake_mc_, transport_controller_, cricket::CN_AUDIO, nullptr, false,
102 AudioOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200103 EXPECT_TRUE(voice_channel != nullptr);
skvlad6c87a672016-05-17 17:49:52 -0700104 cricket::VideoChannel* video_channel = cm_->CreateVideoChannel(
105 &fake_mc_, transport_controller_, cricket::CN_VIDEO, nullptr, false,
106 VideoOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200107 EXPECT_TRUE(video_channel != nullptr);
skvlad6c87a672016-05-17 17:49:52 -0700108 cricket::DataChannel* data_channel =
109 cm_->CreateDataChannel(transport_controller_, cricket::CN_DATA, nullptr,
110 false, cricket::DCT_RTP);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200111 EXPECT_TRUE(data_channel != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112 cm_->DestroyVideoChannel(video_channel);
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200113 cm_->DestroyVoiceChannel(voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114 cm_->DestroyDataChannel(data_channel);
115 cm_->Terminate();
116}
117
118// Test that we can create and destroy a voice and video channel with a worker.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000119TEST_F(ChannelManagerTest, CreateDestroyChannelsOnThread) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200120 network_.Start();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121 worker_.Start();
122 EXPECT_TRUE(cm_->set_worker_thread(&worker_));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200123 EXPECT_TRUE(cm_->set_network_thread(&network_));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124 EXPECT_TRUE(cm_->Init());
deadbeefcbecd352015-09-23 11:50:27 -0700125 delete transport_controller_;
126 transport_controller_ =
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200127 new cricket::FakeTransportController(&network_, ICEROLE_CONTROLLING);
skvlad6c87a672016-05-17 17:49:52 -0700128 cricket::VoiceChannel* voice_channel = cm_->CreateVoiceChannel(
129 &fake_mc_, transport_controller_, cricket::CN_AUDIO, nullptr, false,
130 AudioOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200131 EXPECT_TRUE(voice_channel != nullptr);
skvlad6c87a672016-05-17 17:49:52 -0700132 cricket::VideoChannel* video_channel = cm_->CreateVideoChannel(
133 &fake_mc_, transport_controller_, cricket::CN_VIDEO, nullptr, false,
134 VideoOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200135 EXPECT_TRUE(video_channel != nullptr);
skvlad6c87a672016-05-17 17:49:52 -0700136 cricket::DataChannel* data_channel =
137 cm_->CreateDataChannel(transport_controller_, cricket::CN_DATA, nullptr,
138 false, cricket::DCT_RTP);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200139 EXPECT_TRUE(data_channel != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140 cm_->DestroyVideoChannel(video_channel);
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200141 cm_->DestroyVoiceChannel(voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142 cm_->DestroyDataChannel(data_channel);
143 cm_->Terminate();
144}
145
146// Test that we fail to create a voice/video channel if the session is unable
147// to create a cricket::TransportChannel
148TEST_F(ChannelManagerTest, NoTransportChannelTest) {
149 EXPECT_TRUE(cm_->Init());
deadbeefcbecd352015-09-23 11:50:27 -0700150 transport_controller_->set_fail_channel_creation(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151 // The test is useless unless the session does not fail creating
152 // cricket::TransportChannel.
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200153 ASSERT_TRUE(transport_controller_->CreateTransportChannel_n(
Jelena Marusicc28a8962015-05-29 15:05:44 +0200154 "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155
skvlad6c87a672016-05-17 17:49:52 -0700156 cricket::VoiceChannel* voice_channel = cm_->CreateVoiceChannel(
157 &fake_mc_, transport_controller_, cricket::CN_AUDIO, nullptr, false,
158 AudioOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200159 EXPECT_TRUE(voice_channel == nullptr);
skvlad6c87a672016-05-17 17:49:52 -0700160 cricket::VideoChannel* video_channel = cm_->CreateVideoChannel(
161 &fake_mc_, transport_controller_, cricket::CN_VIDEO, nullptr, false,
162 VideoOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200163 EXPECT_TRUE(video_channel == nullptr);
skvlad6c87a672016-05-17 17:49:52 -0700164 cricket::DataChannel* data_channel =
165 cm_->CreateDataChannel(transport_controller_, cricket::CN_DATA, nullptr,
166 false, cricket::DCT_RTP);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200167 EXPECT_TRUE(data_channel == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168 cm_->Terminate();
169}
170
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171TEST_F(ChannelManagerTest, SetVideoRtxEnabled) {
172 std::vector<VideoCodec> codecs;
perkj26752742016-10-24 01:21:16 -0700173 const VideoCodec rtx_codec(96, "rtx");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174
175 // By default RTX is disabled.
magjed3cf8ece2016-11-10 03:36:53 -0800176 cm_->GetSupportedVideoCodecs(&codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec));
178
179 // Enable and check.
180 EXPECT_TRUE(cm_->SetVideoRtxEnabled(true));
magjed3cf8ece2016-11-10 03:36:53 -0800181 cm_->GetSupportedVideoCodecs(&codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec));
183
184 // Disable and check.
185 EXPECT_TRUE(cm_->SetVideoRtxEnabled(false));
magjed3cf8ece2016-11-10 03:36:53 -0800186 cm_->GetSupportedVideoCodecs(&codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec));
188
189 // Cannot toggle rtx after initialization.
190 EXPECT_TRUE(cm_->Init());
191 EXPECT_FALSE(cm_->SetVideoRtxEnabled(true));
192 EXPECT_FALSE(cm_->SetVideoRtxEnabled(false));
193
194 // Can set again after terminate.
195 cm_->Terminate();
196 EXPECT_TRUE(cm_->SetVideoRtxEnabled(true));
magjed3cf8ece2016-11-10 03:36:53 -0800197 cm_->GetSupportedVideoCodecs(&codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec));
199}
200
201} // namespace cricket