blob: 34d9108cff888b35717a9649fde9a149a2fa231c [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 {
deadbeef7af91dd2016-12-13 11:29:11 -080024const bool kDefaultRtcpEnabled = false;
25const bool kDefaultSrtpRequired = true;
26}
27
28namespace cricket {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029
30static const AudioCodec kAudioCodecs[] = {
deadbeef67cf2c12016-04-13 10:07:16 -070031 AudioCodec(97, "voice", 1, 2, 3), AudioCodec(111, "OPUS", 48000, 32000, 2),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032};
33
34static const VideoCodec kVideoCodecs[] = {
perkj26752742016-10-24 01:21:16 -070035 VideoCodec(99, "H264"), VideoCodec(100, "VP8"), VideoCodec(96, "rtx"),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036};
37
38class ChannelManagerTest : public testing::Test {
39 protected:
deadbeefcbecd352015-09-23 11:50:27 -070040 ChannelManagerTest()
stefanc1aeaf02015-10-15 07:26:07 -070041 : fme_(new cricket::FakeMediaEngine()),
42 fdme_(new cricket::FakeDataEngine()),
skvlad11a9cbf2016-10-07 11:53:05 -070043 cm_(new cricket::ChannelManager(fme_, fdme_, rtc::Thread::Current())),
44 fake_call_(webrtc::Call::Config(&event_log_)),
stefanc1aeaf02015-10-15 07:26:07 -070045 fake_mc_(cm_, &fake_call_),
46 transport_controller_(
47 new cricket::FakeTransportController(ICEROLE_CONTROLLING)) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048
49 virtual void SetUp() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050 fme_->SetAudioCodecs(MAKE_VECTOR(kAudioCodecs));
51 fme_->SetVideoCodecs(MAKE_VECTOR(kVideoCodecs));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052 }
53
54 virtual void TearDown() {
deadbeefcbecd352015-09-23 11:50:27 -070055 delete transport_controller_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056 delete cm_;
57 cm_ = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058 fdme_ = NULL;
59 fme_ = NULL;
60 }
61
skvlad11a9cbf2016-10-07 11:53:05 -070062 webrtc::RtcEventLogNullImpl event_log_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +020063 rtc::Thread network_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000064 rtc::Thread worker_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065 cricket::FakeMediaEngine* fme_;
66 cricket::FakeDataEngine* fdme_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067 cricket::ChannelManager* cm_;
stefanc1aeaf02015-10-15 07:26:07 -070068 cricket::FakeCall fake_call_;
69 cricket::FakeMediaController fake_mc_;
deadbeefcbecd352015-09-23 11:50:27 -070070 cricket::FakeTransportController* transport_controller_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071};
72
73// Test that we startup/shutdown properly.
74TEST_F(ChannelManagerTest, StartupShutdown) {
75 EXPECT_FALSE(cm_->initialized());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000076 EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077 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.
84TEST_F(ChannelManagerTest, StartupShutdownOnThread) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +020085 network_.Start();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 worker_.Start();
87 EXPECT_FALSE(cm_->initialized());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000088 EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread());
Danil Chapovalov33b01f22016-05-11 19:55:27 +020089 EXPECT_TRUE(cm_->set_network_thread(&network_));
90 EXPECT_EQ(&network_, cm_->network_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 EXPECT_TRUE(cm_->set_worker_thread(&worker_));
92 EXPECT_EQ(&worker_, cm_->worker_thread());
93 EXPECT_TRUE(cm_->Init());
94 EXPECT_TRUE(cm_->initialized());
Danil Chapovalov33b01f22016-05-11 19:55:27 +020095 // Setting the network or worker thread while initialized should fail.
96 EXPECT_FALSE(cm_->set_network_thread(rtc::Thread::Current()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000097 EXPECT_FALSE(cm_->set_worker_thread(rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 cm_->Terminate();
99 EXPECT_FALSE(cm_->initialized());
100}
101
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102// Test that we can create and destroy a voice and video channel.
103TEST_F(ChannelManagerTest, CreateDestroyChannels) {
104 EXPECT_TRUE(cm_->Init());
zhihuangf5b251b2017-01-12 19:37:48 -0800105 cricket::TransportChannel* rtp_transport =
106 transport_controller_->CreateTransportChannel(
107 cricket::CN_AUDIO, cricket::ICE_CANDIDATE_COMPONENT_RTP);
skvlad6c87a672016-05-17 17:49:52 -0700108 cricket::VoiceChannel* voice_channel = cm_->CreateVoiceChannel(
zhihuangf5b251b2017-01-12 19:37:48 -0800109 &fake_mc_, rtp_transport, nullptr /*rtcp_transport*/,
110 rtc::Thread::Current(), cricket::CN_AUDIO, nullptr, kDefaultRtcpEnabled,
111 kDefaultSrtpRequired, AudioOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200112 EXPECT_TRUE(voice_channel != nullptr);
skvlad6c87a672016-05-17 17:49:52 -0700113 cricket::VideoChannel* video_channel = cm_->CreateVideoChannel(
zhihuangf5b251b2017-01-12 19:37:48 -0800114 &fake_mc_, rtp_transport, nullptr /*rtcp_transport*/,
115 rtc::Thread::Current(), cricket::CN_VIDEO, nullptr, kDefaultRtcpEnabled,
116 kDefaultSrtpRequired, VideoOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200117 EXPECT_TRUE(video_channel != nullptr);
deadbeef953c2ce2017-01-09 14:53:41 -0800118 cricket::RtpDataChannel* rtp_data_channel = cm_->CreateRtpDataChannel(
zhihuangf5b251b2017-01-12 19:37:48 -0800119 &fake_mc_, rtp_transport, nullptr /*rtcp_transport*/,
120 rtc::Thread::Current(), cricket::CN_DATA, nullptr, kDefaultRtcpEnabled,
121 kDefaultSrtpRequired);
deadbeef953c2ce2017-01-09 14:53:41 -0800122 EXPECT_TRUE(rtp_data_channel != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123 cm_->DestroyVideoChannel(video_channel);
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200124 cm_->DestroyVoiceChannel(voice_channel);
deadbeef953c2ce2017-01-09 14:53:41 -0800125 cm_->DestroyRtpDataChannel(rtp_data_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126 cm_->Terminate();
127}
128
129// Test that we can create and destroy a voice and video channel with a worker.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000130TEST_F(ChannelManagerTest, CreateDestroyChannelsOnThread) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200131 network_.Start();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132 worker_.Start();
133 EXPECT_TRUE(cm_->set_worker_thread(&worker_));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200134 EXPECT_TRUE(cm_->set_network_thread(&network_));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135 EXPECT_TRUE(cm_->Init());
deadbeefcbecd352015-09-23 11:50:27 -0700136 delete transport_controller_;
137 transport_controller_ =
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200138 new cricket::FakeTransportController(&network_, ICEROLE_CONTROLLING);
zhihuangf5b251b2017-01-12 19:37:48 -0800139 cricket::TransportChannel* rtp_transport =
140 transport_controller_->CreateTransportChannel(
141 cricket::CN_AUDIO, cricket::ICE_CANDIDATE_COMPONENT_RTP);
skvlad6c87a672016-05-17 17:49:52 -0700142 cricket::VoiceChannel* voice_channel = cm_->CreateVoiceChannel(
zhihuangf5b251b2017-01-12 19:37:48 -0800143 &fake_mc_, rtp_transport, nullptr /*rtcp_transport*/,
144 rtc::Thread::Current(), cricket::CN_AUDIO, nullptr, kDefaultRtcpEnabled,
145 kDefaultSrtpRequired, AudioOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200146 EXPECT_TRUE(voice_channel != nullptr);
skvlad6c87a672016-05-17 17:49:52 -0700147 cricket::VideoChannel* video_channel = cm_->CreateVideoChannel(
zhihuangf5b251b2017-01-12 19:37:48 -0800148 &fake_mc_, rtp_transport, nullptr /*rtcp_transport*/,
149 rtc::Thread::Current(), cricket::CN_VIDEO, nullptr, kDefaultRtcpEnabled,
150 kDefaultSrtpRequired, VideoOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200151 EXPECT_TRUE(video_channel != nullptr);
deadbeef953c2ce2017-01-09 14:53:41 -0800152 cricket::RtpDataChannel* rtp_data_channel = cm_->CreateRtpDataChannel(
zhihuangf5b251b2017-01-12 19:37:48 -0800153 &fake_mc_, rtp_transport, nullptr /*rtcp_transport*/,
154 rtc::Thread::Current(), cricket::CN_DATA, nullptr, kDefaultRtcpEnabled,
155 kDefaultSrtpRequired);
deadbeef953c2ce2017-01-09 14:53:41 -0800156 EXPECT_TRUE(rtp_data_channel != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157 cm_->DestroyVideoChannel(video_channel);
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200158 cm_->DestroyVoiceChannel(voice_channel);
deadbeef953c2ce2017-01-09 14:53:41 -0800159 cm_->DestroyRtpDataChannel(rtp_data_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160 cm_->Terminate();
161}
162
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163TEST_F(ChannelManagerTest, SetVideoRtxEnabled) {
164 std::vector<VideoCodec> codecs;
perkj26752742016-10-24 01:21:16 -0700165 const VideoCodec rtx_codec(96, "rtx");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166
167 // By default RTX is disabled.
magjed3cf8ece2016-11-10 03:36:53 -0800168 cm_->GetSupportedVideoCodecs(&codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169 EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec));
170
171 // Enable and check.
172 EXPECT_TRUE(cm_->SetVideoRtxEnabled(true));
magjed3cf8ece2016-11-10 03:36:53 -0800173 cm_->GetSupportedVideoCodecs(&codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174 EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec));
175
176 // Disable and check.
177 EXPECT_TRUE(cm_->SetVideoRtxEnabled(false));
magjed3cf8ece2016-11-10 03:36:53 -0800178 cm_->GetSupportedVideoCodecs(&codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179 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));
magjed3cf8ece2016-11-10 03:36:53 -0800189 cm_->GetSupportedVideoCodecs(&codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec));
191}
192
193} // namespace cricket