jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 1 | /* |
kjellander | 65c7f67 | 2016-02-12 00:05:01 -0800 | [diff] [blame] | 2 | * Copyright 2008 The WebRTC project authors. All Rights Reserved. |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * |
kjellander | 65c7f67 | 2016-02-12 00:05:01 -0800 | [diff] [blame] | 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. |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 9 | */ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 10 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 11 | #include "webrtc/api/fakemediacontroller.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 12 | #include "webrtc/base/gunit.h" |
| 13 | #include "webrtc/base/logging.h" |
| 14 | #include "webrtc/base/thread.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 15 | #include "webrtc/media/base/fakemediaengine.h" |
| 16 | #include "webrtc/media/base/fakevideocapturer.h" |
| 17 | #include "webrtc/media/base/testutils.h" |
kjellander@webrtc.org | 5ad1297 | 2016-02-12 06:39:40 +0100 | [diff] [blame] | 18 | #include "webrtc/media/engine/fakewebrtccall.h" |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 19 | #include "webrtc/p2p/base/faketransportcontroller.h" |
kjellander@webrtc.org | 9b8df25 | 2016-02-12 06:47:59 +0100 | [diff] [blame] | 20 | #include "webrtc/pc/channelmanager.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 21 | |
| 22 | namespace cricket { |
| 23 | |
| 24 | static const AudioCodec kAudioCodecs[] = { |
| 25 | AudioCodec(97, "voice", 1, 2, 3, 0), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 26 | AudioCodec(111, "OPUS", 48000, 32000, 2, 0), |
| 27 | }; |
| 28 | |
| 29 | static 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 | |
| 35 | class ChannelManagerTest : public testing::Test { |
| 36 | protected: |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 37 | ChannelManagerTest() |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 38 | : fme_(new cricket::FakeMediaEngine()), |
| 39 | fdme_(new cricket::FakeDataEngine()), |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 40 | cm_(new cricket::ChannelManager(fme_, |
| 41 | fdme_, |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 42 | 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 47 | |
| 48 | virtual void SetUp() { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 49 | fme_->SetAudioCodecs(MAKE_VECTOR(kAudioCodecs)); |
| 50 | fme_->SetVideoCodecs(MAKE_VECTOR(kVideoCodecs)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | virtual void TearDown() { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 54 | delete transport_controller_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 55 | delete cm_; |
| 56 | cm_ = NULL; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 57 | fdme_ = NULL; |
| 58 | fme_ = NULL; |
| 59 | } |
| 60 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 61 | rtc::Thread worker_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 62 | cricket::FakeMediaEngine* fme_; |
| 63 | cricket::FakeDataEngine* fdme_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 64 | cricket::ChannelManager* cm_; |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 65 | cricket::FakeCall fake_call_; |
| 66 | cricket::FakeMediaController fake_mc_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 67 | cricket::FakeTransportController* transport_controller_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | // Test that we startup/shutdown properly. |
| 71 | TEST_F(ChannelManagerTest, StartupShutdown) { |
| 72 | EXPECT_FALSE(cm_->initialized()); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 73 | EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 74 | 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. |
| 81 | TEST_F(ChannelManagerTest, StartupShutdownOnThread) { |
| 82 | worker_.Start(); |
| 83 | EXPECT_FALSE(cm_->initialized()); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 84 | EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 85 | 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.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 90 | EXPECT_FALSE(cm_->set_worker_thread(rtc::Thread::Current())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 91 | cm_->Terminate(); |
| 92 | EXPECT_FALSE(cm_->initialized()); |
| 93 | } |
| 94 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 95 | // Test that we can create and destroy a voice and video channel. |
| 96 | TEST_F(ChannelManagerTest, CreateDestroyChannels) { |
| 97 | EXPECT_TRUE(cm_->Init()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 98 | cricket::VoiceChannel* voice_channel = |
| 99 | cm_->CreateVoiceChannel(&fake_mc_, transport_controller_, |
| 100 | cricket::CN_AUDIO, false, AudioOptions()); |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 101 | EXPECT_TRUE(voice_channel != nullptr); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 102 | cricket::VideoChannel* video_channel = |
| 103 | cm_->CreateVideoChannel(&fake_mc_, transport_controller_, |
| 104 | cricket::CN_VIDEO, false, VideoOptions()); |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 105 | EXPECT_TRUE(video_channel != nullptr); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 106 | cricket::DataChannel* data_channel = cm_->CreateDataChannel( |
| 107 | transport_controller_, cricket::CN_DATA, false, cricket::DCT_RTP); |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 108 | EXPECT_TRUE(data_channel != nullptr); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 109 | cm_->DestroyVideoChannel(video_channel); |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 110 | cm_->DestroyVoiceChannel(voice_channel); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 111 | 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.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 116 | TEST_F(ChannelManagerTest, CreateDestroyChannelsOnThread) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 117 | worker_.Start(); |
| 118 | EXPECT_TRUE(cm_->set_worker_thread(&worker_)); |
| 119 | EXPECT_TRUE(cm_->Init()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 120 | 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 Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 126 | EXPECT_TRUE(voice_channel != nullptr); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 127 | cricket::VideoChannel* video_channel = |
| 128 | cm_->CreateVideoChannel(&fake_mc_, transport_controller_, |
| 129 | cricket::CN_VIDEO, false, VideoOptions()); |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 130 | EXPECT_TRUE(video_channel != nullptr); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 131 | cricket::DataChannel* data_channel = cm_->CreateDataChannel( |
| 132 | transport_controller_, cricket::CN_DATA, false, cricket::DCT_RTP); |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 133 | EXPECT_TRUE(data_channel != nullptr); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 134 | cm_->DestroyVideoChannel(video_channel); |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 135 | cm_->DestroyVoiceChannel(voice_channel); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 136 | 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 |
| 142 | TEST_F(ChannelManagerTest, NoTransportChannelTest) { |
| 143 | EXPECT_TRUE(cm_->Init()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 144 | transport_controller_->set_fail_channel_creation(true); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 145 | // The test is useless unless the session does not fail creating |
| 146 | // cricket::TransportChannel. |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 147 | ASSERT_TRUE(transport_controller_->CreateTransportChannel_w( |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 148 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP) == nullptr); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 149 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 150 | cricket::VoiceChannel* voice_channel = |
| 151 | cm_->CreateVoiceChannel(&fake_mc_, transport_controller_, |
| 152 | cricket::CN_AUDIO, false, AudioOptions()); |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 153 | EXPECT_TRUE(voice_channel == nullptr); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 154 | cricket::VideoChannel* video_channel = |
| 155 | cm_->CreateVideoChannel(&fake_mc_, transport_controller_, |
| 156 | cricket::CN_VIDEO, false, VideoOptions()); |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 157 | EXPECT_TRUE(video_channel == nullptr); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 158 | cricket::DataChannel* data_channel = cm_->CreateDataChannel( |
| 159 | transport_controller_, cricket::CN_DATA, false, cricket::DCT_RTP); |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 160 | EXPECT_TRUE(data_channel == nullptr); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 161 | cm_->Terminate(); |
| 162 | } |
| 163 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 164 | TEST_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 | |
| 186 | TEST_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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 199 | TEST_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 |