blob: 03c68ea0d2bc0ab1c538f51b86ed6eeb04ebc6df [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
deadbeef112b2e92017-02-10 20:13:37 -080011#include <memory>
12
Yves Gerey3e707812018-11-28 16:47:49 +010013#include "absl/memory/memory.h"
Steve Anton10542f22019-01-11 09:11:00 -080014#include "api/rtc_error.h"
Anton Sukhanov98a462c2018-10-17 13:15:42 -070015#include "api/test/fake_media_transport.h"
Steve Anton10542f22019-01-11 09:11:00 -080016#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 Gerey3e707812018-11-28 16:47:49 +010025#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "rtc_base/thread.h"
Yves Gerey3e707812018-11-28 16:47:49 +010027#include "test/gtest.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028
deadbeef1a2183d2017-02-10 23:44:49 -080029namespace {
deadbeef7af91dd2016-12-13 11:29:11 -080030const bool kDefaultSrtpRequired = true;
31}
32
33namespace cricket {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034
35static const AudioCodec kAudioCodecs[] = {
deadbeef67cf2c12016-04-13 10:07:16 -070036 AudioCodec(97, "voice", 1, 2, 3), AudioCodec(111, "OPUS", 48000, 32000, 2),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037};
38
39static const VideoCodec kVideoCodecs[] = {
perkj26752742016-10-24 01:21:16 -070040 VideoCodec(99, "H264"), VideoCodec(100, "VP8"), VideoCodec(96, "rtx"),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041};
42
43class ChannelManagerTest : public testing::Test {
44 protected:
deadbeefcbecd352015-09-23 11:50:27 -070045 ChannelManagerTest()
tommie7251592017-07-14 14:44:46 -070046 : network_(rtc::Thread::CreateWithSocketServer()),
47 worker_(rtc::Thread::Create()),
48 fme_(new cricket::FakeMediaEngine()),
stefanc1aeaf02015-10-15 07:26:07 -070049 fdme_(new cricket::FakeDataEngine()),
deadbeef112b2e92017-02-10 20:13:37 -080050 cm_(new cricket::ChannelManager(
51 std::unique_ptr<MediaEngineInterface>(fme_),
52 std::unique_ptr<DataEngineInterface>(fdme_),
Steve Antonc9e15602017-11-06 15:40:09 -080053 rtc::Thread::Current(),
deadbeef112b2e92017-02-10 20:13:37 -080054 rtc::Thread::Current())),
Zhi Huange830e682018-03-30 10:48:35 -070055 fake_call_() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056 fme_->SetAudioCodecs(MAKE_VECTOR(kAudioCodecs));
57 fme_->SetVideoCodecs(MAKE_VECTOR(kVideoCodecs));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058 }
59
Zhi Huange830e682018-03-30 10:48:35 -070060 std::unique_ptr<webrtc::RtpTransportInternal> CreateDtlsSrtpTransport() {
Karl Wiberg918f50c2018-07-05 11:40:33 +020061 rtp_dtls_transport_ = absl::make_unique<FakeDtlsTransport>(
Zhi Huange830e682018-03-30 10:48:35 -070062 "fake_dtls_transport", cricket::ICE_CANDIDATE_COMPONENT_RTP);
Karl Wiberg918f50c2018-07-05 11:40:33 +020063 auto dtls_srtp_transport = absl::make_unique<webrtc::DtlsSrtpTransport>(
64 /*rtcp_mux_required=*/true);
Zhi Huange830e682018-03-30 10:48:35 -070065 dtls_srtp_transport->SetDtlsTransports(rtp_dtls_transport_.get(),
66 /*rtcp_dtls_transport=*/nullptr);
67 return dtls_srtp_transport;
68 }
69
Anton Sukhanov98a462c2018-10-17 13:15:42 -070070 std::unique_ptr<webrtc::MediaTransportInterface> CreateMediaTransport(
71 rtc::PacketTransportInternal* packet_transport) {
Piotr (Peter) Slatala1a16da12019-02-22 10:39:55 -080072 webrtc::MediaTransportSettings settings;
73 settings.is_caller = true;
Anton Sukhanov98a462c2018-10-17 13:15:42 -070074 auto media_transport_result =
Piotr (Peter) Slatala1a16da12019-02-22 10:39:55 -080075 fake_media_transport_factory_.CreateMediaTransport(
76 packet_transport, network_.get(),
77 /*is_caller=*/settings);
Anton Sukhanov98a462c2018-10-17 13:15:42 -070078 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 Huange830e682018-03-30 10:48:35 -070085 cricket::VoiceChannel* voice_channel = cm_->CreateVoiceChannel(
Anton Sukhanov98a462c2018-10-17 13:15:42 -070086 &fake_call_, cricket::MediaConfig(), rtp_transport, media_transport,
Zhi Huange830e682018-03-30 10:48:35 -070087 rtc::Thread::Current(), cricket::CN_AUDIO, kDefaultSrtpRequired,
Amit Hilbuchbcd39d42019-01-25 17:13:56 -080088 webrtc::CryptoOptions(), &ssrc_generator_, AudioOptions());
Zhi Huange830e682018-03-30 10:48:35 -070089 EXPECT_TRUE(voice_channel != nullptr);
90 cricket::VideoChannel* video_channel = cm_->CreateVideoChannel(
Niels Möller46879152019-01-07 15:54:47 +010091 &fake_call_, cricket::MediaConfig(), rtp_transport, media_transport,
Zhi Huange830e682018-03-30 10:48:35 -070092 rtc::Thread::Current(), cricket::CN_VIDEO, kDefaultSrtpRequired,
Amit Hilbuchbcd39d42019-01-25 17:13:56 -080093 webrtc::CryptoOptions(), &ssrc_generator_, VideoOptions());
Zhi Huange830e682018-03-30 10:48:35 -070094 EXPECT_TRUE(video_channel != nullptr);
95 cricket::RtpDataChannel* rtp_data_channel = cm_->CreateRtpDataChannel(
96 cricket::MediaConfig(), rtp_transport, rtc::Thread::Current(),
Amit Hilbuchbcd39d42019-01-25 17:13:56 -080097 cricket::CN_DATA, kDefaultSrtpRequired, webrtc::CryptoOptions(),
98 &ssrc_generator_);
Zhi Huange830e682018-03-30 10:48:35 -070099 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_;
tommie7251592017-07-14 14:44:46 -0700107 std::unique_ptr<rtc::Thread> network_;
108 std::unique_ptr<rtc::Thread> worker_;
deadbeef112b2e92017-02-10 20:13:37 -0800109 // |fme_| and |fdme_| are actually owned by |cm_|.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110 cricket::FakeMediaEngine* fme_;
111 cricket::FakeDataEngine* fdme_;
deadbeef112b2e92017-02-10 20:13:37 -0800112 std::unique_ptr<cricket::ChannelManager> cm_;
stefanc1aeaf02015-10-15 07:26:07 -0700113 cricket::FakeCall fake_call_;
Anton Sukhanov98a462c2018-10-17 13:15:42 -0700114 webrtc::FakeMediaTransportFactory fake_media_transport_factory_;
Amit Hilbuchbcd39d42019-01-25 17:13:56 -0800115 rtc::UniqueRandomIdGenerator ssrc_generator_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116};
117
118// Test that we startup/shutdown properly.
119TEST_F(ChannelManagerTest, StartupShutdown) {
120 EXPECT_FALSE(cm_->initialized());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000121 EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122 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.
129TEST_F(ChannelManagerTest, StartupShutdownOnThread) {
tommie7251592017-07-14 14:44:46 -0700130 network_->Start();
131 worker_->Start();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132 EXPECT_FALSE(cm_->initialized());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000133 EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread());
tommie7251592017-07-14 14:44:46 -0700134 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.org28e20752013-07-10 00:45:36 +0000138 EXPECT_TRUE(cm_->Init());
139 EXPECT_TRUE(cm_->initialized());
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200140 // Setting the network or worker thread while initialized should fail.
141 EXPECT_FALSE(cm_->set_network_thread(rtc::Thread::Current()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000142 EXPECT_FALSE(cm_->set_worker_thread(rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143 cm_->Terminate();
144 EXPECT_FALSE(cm_->initialized());
145}
146
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147TEST_F(ChannelManagerTest, SetVideoRtxEnabled) {
148 std::vector<VideoCodec> codecs;
perkj26752742016-10-24 01:21:16 -0700149 const VideoCodec rtx_codec(96, "rtx");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150
151 // By default RTX is disabled.
magjed3cf8ece2016-11-10 03:36:53 -0800152 cm_->GetSupportedVideoCodecs(&codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec));
154
155 // Enable and check.
156 EXPECT_TRUE(cm_->SetVideoRtxEnabled(true));
magjed3cf8ece2016-11-10 03:36:53 -0800157 cm_->GetSupportedVideoCodecs(&codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158 EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec));
159
160 // Disable and check.
161 EXPECT_TRUE(cm_->SetVideoRtxEnabled(false));
magjed3cf8ece2016-11-10 03:36:53 -0800162 cm_->GetSupportedVideoCodecs(&codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 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));
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
Zhi Huange830e682018-03-30 10:48:35 -0700177TEST_F(ChannelManagerTest, CreateDestroyChannels) {
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800178 EXPECT_TRUE(cm_->Init());
Zhi Huange830e682018-03-30 10:48:35 -0700179 auto rtp_transport = CreateDtlsSrtpTransport();
Anton Sukhanov98a462c2018-10-17 13:15:42 -0700180 TestCreateDestroyChannels(rtp_transport.get(), /*media_transport=*/nullptr);
181}
182
183TEST_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 Huang2dfc42d2017-12-04 13:38:48 -0800189}
190
Zhi Huange830e682018-03-30 10:48:35 -0700191TEST_F(ChannelManagerTest, CreateDestroyChannelsOnThread) {
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800192 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 Huange830e682018-03-30 10:48:35 -0700197 auto rtp_transport = CreateDtlsSrtpTransport();
Anton Sukhanov98a462c2018-10-17 13:15:42 -0700198 TestCreateDestroyChannels(rtp_transport.get(), /*media_transport=*/nullptr);
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800199}
200
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201} // namespace cricket