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/fakecapturemanager.h" |
| 16 | #include "webrtc/media/base/fakemediaengine.h" |
| 17 | #include "webrtc/media/base/fakevideocapturer.h" |
| 18 | #include "webrtc/media/base/testutils.h" |
kjellander@webrtc.org | 5ad1297 | 2016-02-12 06:39:40 +0100 | [diff] [blame] | 19 | #include "webrtc/media/engine/fakewebrtccall.h" |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 20 | #include "webrtc/p2p/base/faketransportcontroller.h" |
kjellander@webrtc.org | 9b8df25 | 2016-02-12 06:47:59 +0100 | [diff] [blame] | 21 | #include "webrtc/pc/channelmanager.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 22 | |
| 23 | namespace cricket { |
| 24 | |
| 25 | static const AudioCodec kAudioCodecs[] = { |
| 26 | AudioCodec(97, "voice", 1, 2, 3, 0), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 27 | AudioCodec(111, "OPUS", 48000, 32000, 2, 0), |
| 28 | }; |
| 29 | |
| 30 | static const VideoCodec kVideoCodecs[] = { |
| 31 | VideoCodec(99, "H264", 100, 200, 300, 0), |
| 32 | VideoCodec(100, "VP8", 100, 200, 300, 0), |
| 33 | VideoCodec(96, "rtx", 100, 200, 300, 0), |
| 34 | }; |
| 35 | |
| 36 | class ChannelManagerTest : public testing::Test { |
| 37 | protected: |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 38 | ChannelManagerTest() |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 39 | : fme_(new cricket::FakeMediaEngine()), |
| 40 | fdme_(new cricket::FakeDataEngine()), |
| 41 | fcm_(new cricket::FakeCaptureManager()), |
| 42 | cm_(new cricket::ChannelManager(fme_, |
| 43 | fdme_, |
| 44 | fcm_, |
| 45 | rtc::Thread::Current())), |
| 46 | fake_call_(webrtc::Call::Config()), |
| 47 | fake_mc_(cm_, &fake_call_), |
| 48 | transport_controller_( |
| 49 | new cricket::FakeTransportController(ICEROLE_CONTROLLING)) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 50 | |
| 51 | virtual void SetUp() { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 52 | fme_->SetAudioCodecs(MAKE_VECTOR(kAudioCodecs)); |
| 53 | fme_->SetVideoCodecs(MAKE_VECTOR(kVideoCodecs)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | virtual void TearDown() { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 57 | delete transport_controller_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 58 | delete cm_; |
| 59 | cm_ = NULL; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 60 | fcm_ = NULL; |
| 61 | fdme_ = NULL; |
| 62 | fme_ = NULL; |
| 63 | } |
| 64 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 65 | rtc::Thread worker_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 66 | cricket::FakeMediaEngine* fme_; |
| 67 | cricket::FakeDataEngine* fdme_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 68 | cricket::FakeCaptureManager* fcm_; |
| 69 | cricket::ChannelManager* cm_; |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 70 | cricket::FakeCall fake_call_; |
| 71 | cricket::FakeMediaController fake_mc_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 72 | cricket::FakeTransportController* transport_controller_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | // Test that we startup/shutdown properly. |
| 76 | TEST_F(ChannelManagerTest, StartupShutdown) { |
| 77 | EXPECT_FALSE(cm_->initialized()); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 78 | EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 79 | EXPECT_TRUE(cm_->Init()); |
| 80 | EXPECT_TRUE(cm_->initialized()); |
| 81 | cm_->Terminate(); |
| 82 | EXPECT_FALSE(cm_->initialized()); |
| 83 | } |
| 84 | |
| 85 | // Test that we startup/shutdown properly with a worker thread. |
| 86 | TEST_F(ChannelManagerTest, StartupShutdownOnThread) { |
| 87 | worker_.Start(); |
| 88 | EXPECT_FALSE(cm_->initialized()); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 89 | EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 90 | EXPECT_TRUE(cm_->set_worker_thread(&worker_)); |
| 91 | EXPECT_EQ(&worker_, cm_->worker_thread()); |
| 92 | EXPECT_TRUE(cm_->Init()); |
| 93 | EXPECT_TRUE(cm_->initialized()); |
| 94 | // Setting the worker thread while initialized should fail. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 95 | EXPECT_FALSE(cm_->set_worker_thread(rtc::Thread::Current())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 96 | cm_->Terminate(); |
| 97 | EXPECT_FALSE(cm_->initialized()); |
| 98 | } |
| 99 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 100 | // Test that we can create and destroy a voice and video channel. |
| 101 | TEST_F(ChannelManagerTest, CreateDestroyChannels) { |
| 102 | EXPECT_TRUE(cm_->Init()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 103 | cricket::VoiceChannel* voice_channel = |
| 104 | cm_->CreateVoiceChannel(&fake_mc_, transport_controller_, |
| 105 | cricket::CN_AUDIO, false, AudioOptions()); |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 106 | EXPECT_TRUE(voice_channel != nullptr); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 107 | cricket::VideoChannel* video_channel = |
| 108 | cm_->CreateVideoChannel(&fake_mc_, transport_controller_, |
| 109 | cricket::CN_VIDEO, false, VideoOptions()); |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 110 | EXPECT_TRUE(video_channel != nullptr); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 111 | cricket::DataChannel* data_channel = cm_->CreateDataChannel( |
| 112 | transport_controller_, cricket::CN_DATA, false, cricket::DCT_RTP); |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 113 | EXPECT_TRUE(data_channel != nullptr); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 114 | cm_->DestroyVideoChannel(video_channel); |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 115 | cm_->DestroyVoiceChannel(voice_channel); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 116 | cm_->DestroyDataChannel(data_channel); |
| 117 | cm_->Terminate(); |
| 118 | } |
| 119 | |
| 120 | // 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] | 121 | TEST_F(ChannelManagerTest, CreateDestroyChannelsOnThread) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 122 | worker_.Start(); |
| 123 | EXPECT_TRUE(cm_->set_worker_thread(&worker_)); |
| 124 | EXPECT_TRUE(cm_->Init()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 125 | delete transport_controller_; |
| 126 | transport_controller_ = |
| 127 | new cricket::FakeTransportController(&worker_, ICEROLE_CONTROLLING); |
| 128 | cricket::VoiceChannel* voice_channel = |
| 129 | cm_->CreateVoiceChannel(&fake_mc_, transport_controller_, |
| 130 | cricket::CN_AUDIO, false, AudioOptions()); |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 131 | EXPECT_TRUE(voice_channel != nullptr); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 132 | cricket::VideoChannel* video_channel = |
| 133 | cm_->CreateVideoChannel(&fake_mc_, transport_controller_, |
| 134 | cricket::CN_VIDEO, false, VideoOptions()); |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 135 | EXPECT_TRUE(video_channel != nullptr); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 136 | cricket::DataChannel* data_channel = cm_->CreateDataChannel( |
| 137 | transport_controller_, cricket::CN_DATA, false, cricket::DCT_RTP); |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 138 | EXPECT_TRUE(data_channel != nullptr); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 139 | cm_->DestroyVideoChannel(video_channel); |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 140 | cm_->DestroyVoiceChannel(voice_channel); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 141 | cm_->DestroyDataChannel(data_channel); |
| 142 | cm_->Terminate(); |
| 143 | } |
| 144 | |
| 145 | // Test that we fail to create a voice/video channel if the session is unable |
| 146 | // to create a cricket::TransportChannel |
| 147 | TEST_F(ChannelManagerTest, NoTransportChannelTest) { |
| 148 | EXPECT_TRUE(cm_->Init()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 149 | transport_controller_->set_fail_channel_creation(true); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 150 | // The test is useless unless the session does not fail creating |
| 151 | // cricket::TransportChannel. |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 152 | ASSERT_TRUE(transport_controller_->CreateTransportChannel_w( |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 153 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP) == nullptr); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 154 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 155 | cricket::VoiceChannel* voice_channel = |
| 156 | cm_->CreateVoiceChannel(&fake_mc_, transport_controller_, |
| 157 | cricket::CN_AUDIO, false, AudioOptions()); |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 158 | EXPECT_TRUE(voice_channel == nullptr); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 159 | cricket::VideoChannel* video_channel = |
| 160 | cm_->CreateVideoChannel(&fake_mc_, transport_controller_, |
| 161 | cricket::CN_VIDEO, false, VideoOptions()); |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 162 | EXPECT_TRUE(video_channel == nullptr); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 163 | cricket::DataChannel* data_channel = cm_->CreateDataChannel( |
| 164 | transport_controller_, cricket::CN_DATA, false, cricket::DCT_RTP); |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 165 | EXPECT_TRUE(data_channel == nullptr); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 166 | cm_->Terminate(); |
| 167 | } |
| 168 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 169 | TEST_F(ChannelManagerTest, GetSetOutputVolumeBeforeInit) { |
| 170 | int level; |
| 171 | // Before init, SetOutputVolume() remembers the volume but does not change the |
| 172 | // volume of the engine. GetOutputVolume() should fail. |
| 173 | EXPECT_EQ(-1, fme_->output_volume()); |
| 174 | EXPECT_FALSE(cm_->GetOutputVolume(&level)); |
| 175 | EXPECT_FALSE(cm_->SetOutputVolume(-1)); // Invalid volume. |
| 176 | EXPECT_TRUE(cm_->SetOutputVolume(99)); |
| 177 | EXPECT_EQ(-1, fme_->output_volume()); |
| 178 | |
| 179 | // Init() will apply the remembered volume. |
| 180 | EXPECT_TRUE(cm_->Init()); |
| 181 | EXPECT_TRUE(cm_->GetOutputVolume(&level)); |
| 182 | EXPECT_EQ(99, level); |
| 183 | EXPECT_EQ(level, fme_->output_volume()); |
| 184 | |
| 185 | EXPECT_TRUE(cm_->SetOutputVolume(60)); |
| 186 | EXPECT_TRUE(cm_->GetOutputVolume(&level)); |
| 187 | EXPECT_EQ(60, level); |
| 188 | EXPECT_EQ(level, fme_->output_volume()); |
| 189 | } |
| 190 | |
| 191 | TEST_F(ChannelManagerTest, GetSetOutputVolume) { |
| 192 | int level; |
| 193 | EXPECT_TRUE(cm_->Init()); |
| 194 | EXPECT_TRUE(cm_->GetOutputVolume(&level)); |
| 195 | EXPECT_EQ(level, fme_->output_volume()); |
| 196 | |
| 197 | EXPECT_FALSE(cm_->SetOutputVolume(-1)); // Invalid volume. |
| 198 | EXPECT_TRUE(cm_->SetOutputVolume(60)); |
| 199 | EXPECT_EQ(60, fme_->output_volume()); |
| 200 | EXPECT_TRUE(cm_->GetOutputVolume(&level)); |
| 201 | EXPECT_EQ(60, level); |
| 202 | } |
| 203 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 204 | TEST_F(ChannelManagerTest, SetVideoRtxEnabled) { |
| 205 | std::vector<VideoCodec> codecs; |
| 206 | const VideoCodec rtx_codec(96, "rtx", 0, 0, 0, 0); |
| 207 | |
| 208 | // By default RTX is disabled. |
| 209 | cm_->GetSupportedVideoCodecs(&codecs); |
| 210 | EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec)); |
| 211 | |
| 212 | // Enable and check. |
| 213 | EXPECT_TRUE(cm_->SetVideoRtxEnabled(true)); |
| 214 | cm_->GetSupportedVideoCodecs(&codecs); |
| 215 | EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec)); |
| 216 | |
| 217 | // Disable and check. |
| 218 | EXPECT_TRUE(cm_->SetVideoRtxEnabled(false)); |
| 219 | cm_->GetSupportedVideoCodecs(&codecs); |
| 220 | EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec)); |
| 221 | |
| 222 | // Cannot toggle rtx after initialization. |
| 223 | EXPECT_TRUE(cm_->Init()); |
| 224 | EXPECT_FALSE(cm_->SetVideoRtxEnabled(true)); |
| 225 | EXPECT_FALSE(cm_->SetVideoRtxEnabled(false)); |
| 226 | |
| 227 | // Can set again after terminate. |
| 228 | cm_->Terminate(); |
| 229 | EXPECT_TRUE(cm_->SetVideoRtxEnabled(true)); |
| 230 | cm_->GetSupportedVideoCodecs(&codecs); |
| 231 | EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec)); |
| 232 | } |
| 233 | |
| 234 | } // namespace cricket |