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 | |
deadbeef | 112b2e9 | 2017-02-10 20:13:37 -0800 | [diff] [blame] | 11 | #include <memory> |
| 12 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include "absl/memory/memory.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 14 | #include "api/rtc_error.h" |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 15 | #include "api/test/fake_media_transport.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 16 | #include "media/base/fake_media_engine.h" |
| 17 | #include "media/base/test_utils.h" |
| 18 | #include "media/engine/fake_webrtc_call.h" |
| 19 | #include "p2p/base/dtls_transport_internal.h" |
| 20 | #include "p2p/base/fake_dtls_transport.h" |
| 21 | #include "p2p/base/p2p_constants.h" |
| 22 | #include "p2p/base/packet_transport_internal.h" |
| 23 | #include "pc/channel_manager.h" |
| 24 | #include "pc/dtls_srtp_transport.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 25 | #include "rtc_base/checks.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "rtc_base/thread.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 27 | #include "test/gtest.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 28 | |
deadbeef | 1a2183d | 2017-02-10 23:44:49 -0800 | [diff] [blame] | 29 | namespace { |
deadbeef | 7af91dd | 2016-12-13 11:29:11 -0800 | [diff] [blame] | 30 | const bool kDefaultSrtpRequired = true; |
| 31 | } |
| 32 | |
| 33 | namespace cricket { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 34 | |
| 35 | static const AudioCodec kAudioCodecs[] = { |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 36 | AudioCodec(97, "voice", 1, 2, 3), AudioCodec(111, "OPUS", 48000, 32000, 2), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | static const VideoCodec kVideoCodecs[] = { |
perkj | 2675274 | 2016-10-24 01:21:16 -0700 | [diff] [blame] | 40 | VideoCodec(99, "H264"), VideoCodec(100, "VP8"), VideoCodec(96, "rtx"), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | class ChannelManagerTest : public testing::Test { |
| 44 | protected: |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 45 | ChannelManagerTest() |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 46 | : network_(rtc::Thread::CreateWithSocketServer()), |
| 47 | worker_(rtc::Thread::Create()), |
| 48 | fme_(new cricket::FakeMediaEngine()), |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 49 | fdme_(new cricket::FakeDataEngine()), |
deadbeef | 112b2e9 | 2017-02-10 20:13:37 -0800 | [diff] [blame] | 50 | cm_(new cricket::ChannelManager( |
| 51 | std::unique_ptr<MediaEngineInterface>(fme_), |
| 52 | std::unique_ptr<DataEngineInterface>(fdme_), |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 53 | rtc::Thread::Current(), |
deadbeef | 112b2e9 | 2017-02-10 20:13:37 -0800 | [diff] [blame] | 54 | rtc::Thread::Current())), |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 55 | fake_call_() { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 56 | fme_->SetAudioCodecs(MAKE_VECTOR(kAudioCodecs)); |
| 57 | fme_->SetVideoCodecs(MAKE_VECTOR(kVideoCodecs)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 60 | std::unique_ptr<webrtc::RtpTransportInternal> CreateDtlsSrtpTransport() { |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 61 | rtp_dtls_transport_ = absl::make_unique<FakeDtlsTransport>( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 62 | "fake_dtls_transport", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 63 | auto dtls_srtp_transport = absl::make_unique<webrtc::DtlsSrtpTransport>( |
| 64 | /*rtcp_mux_required=*/true); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 65 | dtls_srtp_transport->SetDtlsTransports(rtp_dtls_transport_.get(), |
| 66 | /*rtcp_dtls_transport=*/nullptr); |
| 67 | return dtls_srtp_transport; |
| 68 | } |
| 69 | |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 70 | std::unique_ptr<webrtc::MediaTransportInterface> CreateMediaTransport( |
| 71 | rtc::PacketTransportInternal* packet_transport) { |
Piotr (Peter) Slatala | 1a16da1 | 2019-02-22 10:39:55 -0800 | [diff] [blame] | 72 | webrtc::MediaTransportSettings settings; |
| 73 | settings.is_caller = true; |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 74 | auto media_transport_result = |
Piotr (Peter) Slatala | 1a16da1 | 2019-02-22 10:39:55 -0800 | [diff] [blame] | 75 | fake_media_transport_factory_.CreateMediaTransport( |
| 76 | packet_transport, network_.get(), |
| 77 | /*is_caller=*/settings); |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 78 | RTC_CHECK(media_transport_result.ok()); |
| 79 | return media_transport_result.MoveValue(); |
| 80 | } |
| 81 | |
| 82 | void TestCreateDestroyChannels( |
| 83 | webrtc::RtpTransportInternal* rtp_transport, |
| 84 | webrtc::MediaTransportInterface* media_transport) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 85 | cricket::VoiceChannel* voice_channel = cm_->CreateVoiceChannel( |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 86 | &fake_call_, cricket::MediaConfig(), rtp_transport, media_transport, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 87 | rtc::Thread::Current(), cricket::CN_AUDIO, kDefaultSrtpRequired, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 88 | webrtc::CryptoOptions(), &ssrc_generator_, AudioOptions()); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 89 | EXPECT_TRUE(voice_channel != nullptr); |
| 90 | cricket::VideoChannel* video_channel = cm_->CreateVideoChannel( |
Niels Möller | 4687915 | 2019-01-07 15:54:47 +0100 | [diff] [blame] | 91 | &fake_call_, cricket::MediaConfig(), rtp_transport, media_transport, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 92 | rtc::Thread::Current(), cricket::CN_VIDEO, kDefaultSrtpRequired, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 93 | webrtc::CryptoOptions(), &ssrc_generator_, VideoOptions()); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 94 | EXPECT_TRUE(video_channel != nullptr); |
| 95 | cricket::RtpDataChannel* rtp_data_channel = cm_->CreateRtpDataChannel( |
| 96 | cricket::MediaConfig(), rtp_transport, rtc::Thread::Current(), |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 97 | cricket::CN_DATA, kDefaultSrtpRequired, webrtc::CryptoOptions(), |
| 98 | &ssrc_generator_); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 99 | EXPECT_TRUE(rtp_data_channel != nullptr); |
| 100 | cm_->DestroyVideoChannel(video_channel); |
| 101 | cm_->DestroyVoiceChannel(voice_channel); |
| 102 | cm_->DestroyRtpDataChannel(rtp_data_channel); |
| 103 | cm_->Terminate(); |
| 104 | } |
| 105 | |
| 106 | std::unique_ptr<DtlsTransportInternal> rtp_dtls_transport_; |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 107 | std::unique_ptr<rtc::Thread> network_; |
| 108 | std::unique_ptr<rtc::Thread> worker_; |
deadbeef | 112b2e9 | 2017-02-10 20:13:37 -0800 | [diff] [blame] | 109 | // |fme_| and |fdme_| are actually owned by |cm_|. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 110 | cricket::FakeMediaEngine* fme_; |
| 111 | cricket::FakeDataEngine* fdme_; |
deadbeef | 112b2e9 | 2017-02-10 20:13:37 -0800 | [diff] [blame] | 112 | std::unique_ptr<cricket::ChannelManager> cm_; |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 113 | cricket::FakeCall fake_call_; |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 114 | webrtc::FakeMediaTransportFactory fake_media_transport_factory_; |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 115 | rtc::UniqueRandomIdGenerator ssrc_generator_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | // Test that we startup/shutdown properly. |
| 119 | TEST_F(ChannelManagerTest, StartupShutdown) { |
| 120 | EXPECT_FALSE(cm_->initialized()); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 121 | EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 122 | EXPECT_TRUE(cm_->Init()); |
| 123 | EXPECT_TRUE(cm_->initialized()); |
| 124 | cm_->Terminate(); |
| 125 | EXPECT_FALSE(cm_->initialized()); |
| 126 | } |
| 127 | |
| 128 | // Test that we startup/shutdown properly with a worker thread. |
| 129 | TEST_F(ChannelManagerTest, StartupShutdownOnThread) { |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 130 | network_->Start(); |
| 131 | worker_->Start(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 132 | EXPECT_FALSE(cm_->initialized()); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 133 | EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread()); |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 134 | EXPECT_TRUE(cm_->set_network_thread(network_.get())); |
| 135 | EXPECT_EQ(network_.get(), cm_->network_thread()); |
| 136 | EXPECT_TRUE(cm_->set_worker_thread(worker_.get())); |
| 137 | EXPECT_EQ(worker_.get(), cm_->worker_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 138 | EXPECT_TRUE(cm_->Init()); |
| 139 | EXPECT_TRUE(cm_->initialized()); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 140 | // Setting the network or worker thread while initialized should fail. |
| 141 | EXPECT_FALSE(cm_->set_network_thread(rtc::Thread::Current())); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 142 | EXPECT_FALSE(cm_->set_worker_thread(rtc::Thread::Current())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 143 | cm_->Terminate(); |
| 144 | EXPECT_FALSE(cm_->initialized()); |
| 145 | } |
| 146 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 147 | TEST_F(ChannelManagerTest, SetVideoRtxEnabled) { |
| 148 | std::vector<VideoCodec> codecs; |
perkj | 2675274 | 2016-10-24 01:21:16 -0700 | [diff] [blame] | 149 | const VideoCodec rtx_codec(96, "rtx"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 150 | |
| 151 | // By default RTX is disabled. |
magjed | 3cf8ece | 2016-11-10 03:36:53 -0800 | [diff] [blame] | 152 | cm_->GetSupportedVideoCodecs(&codecs); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 153 | EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec)); |
| 154 | |
| 155 | // Enable and check. |
| 156 | EXPECT_TRUE(cm_->SetVideoRtxEnabled(true)); |
magjed | 3cf8ece | 2016-11-10 03:36:53 -0800 | [diff] [blame] | 157 | cm_->GetSupportedVideoCodecs(&codecs); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 158 | EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec)); |
| 159 | |
| 160 | // Disable and check. |
| 161 | EXPECT_TRUE(cm_->SetVideoRtxEnabled(false)); |
magjed | 3cf8ece | 2016-11-10 03:36:53 -0800 | [diff] [blame] | 162 | cm_->GetSupportedVideoCodecs(&codecs); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 163 | EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec)); |
| 164 | |
| 165 | // Cannot toggle rtx after initialization. |
| 166 | EXPECT_TRUE(cm_->Init()); |
| 167 | EXPECT_FALSE(cm_->SetVideoRtxEnabled(true)); |
| 168 | EXPECT_FALSE(cm_->SetVideoRtxEnabled(false)); |
| 169 | |
| 170 | // Can set again after terminate. |
| 171 | cm_->Terminate(); |
| 172 | EXPECT_TRUE(cm_->SetVideoRtxEnabled(true)); |
magjed | 3cf8ece | 2016-11-10 03:36:53 -0800 | [diff] [blame] | 173 | cm_->GetSupportedVideoCodecs(&codecs); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 174 | EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec)); |
| 175 | } |
| 176 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 177 | TEST_F(ChannelManagerTest, CreateDestroyChannels) { |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 178 | EXPECT_TRUE(cm_->Init()); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 179 | auto rtp_transport = CreateDtlsSrtpTransport(); |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 180 | TestCreateDestroyChannels(rtp_transport.get(), /*media_transport=*/nullptr); |
| 181 | } |
| 182 | |
| 183 | TEST_F(ChannelManagerTest, CreateDestroyChannelsWithMediaTransport) { |
| 184 | EXPECT_TRUE(cm_->Init()); |
| 185 | auto rtp_transport = CreateDtlsSrtpTransport(); |
| 186 | auto media_transport = |
| 187 | CreateMediaTransport(rtp_transport->rtcp_packet_transport()); |
| 188 | TestCreateDestroyChannels(rtp_transport.get(), media_transport.get()); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 191 | TEST_F(ChannelManagerTest, CreateDestroyChannelsOnThread) { |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 192 | network_->Start(); |
| 193 | worker_->Start(); |
| 194 | EXPECT_TRUE(cm_->set_worker_thread(worker_.get())); |
| 195 | EXPECT_TRUE(cm_->set_network_thread(network_.get())); |
| 196 | EXPECT_TRUE(cm_->Init()); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 197 | auto rtp_transport = CreateDtlsSrtpTransport(); |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 198 | TestCreateDestroyChannels(rtp_transport.get(), /*media_transport=*/nullptr); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 199 | } |
| 200 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 201 | } // namespace cricket |