henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | 65c7f67 | 2016-02-12 00:05:01 -0800 | [diff] [blame] | 2 | * Copyright 2004 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "pc/channel_manager.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 13 | #include <utility> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 14 | |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 15 | #include "absl/algorithm/container.h" |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 16 | #include "absl/memory/memory.h" |
Niels Möller | 3c7d599 | 2018-10-19 15:29:54 +0200 | [diff] [blame] | 17 | #include "absl/strings/match.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 18 | #include "media/base/media_constants.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "rtc_base/checks.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 20 | #include "rtc_base/location.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/logging.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 22 | #include "rtc_base/thread_checker.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "rtc_base/trace_event.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 24 | |
| 25 | namespace cricket { |
| 26 | |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 27 | ChannelManager::ChannelManager( |
| 28 | std::unique_ptr<MediaEngineInterface> media_engine, |
| 29 | std::unique_ptr<DataEngineInterface> data_engine, |
| 30 | rtc::Thread* worker_thread, |
| 31 | rtc::Thread* network_thread) |
| 32 | : media_engine_(std::move(media_engine)), |
| 33 | data_engine_(std::move(data_engine)), |
| 34 | main_thread_(rtc::Thread::Current()), |
| 35 | worker_thread_(worker_thread), |
| 36 | network_thread_(network_thread) { |
| 37 | RTC_DCHECK(data_engine_); |
| 38 | RTC_DCHECK(worker_thread_); |
| 39 | RTC_DCHECK(network_thread_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | ChannelManager::~ChannelManager() { |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 43 | if (initialized_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 44 | Terminate(); |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 45 | } |
perkj | c11b184 | 2016-03-07 17:34:13 -0800 | [diff] [blame] | 46 | // The media engine needs to be deleted on the worker thread for thread safe |
| 47 | // destruction, |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 48 | worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] { media_engine_.reset(); }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | bool ChannelManager::SetVideoRtxEnabled(bool enable) { |
| 52 | // To be safe, this call is only allowed before initialization. Apps like |
| 53 | // Flute only have a singleton ChannelManager and we don't want this flag to |
| 54 | // be toggled between calls or when there's concurrent calls. We expect apps |
| 55 | // to enable this at startup and retain that setting for the lifetime of the |
| 56 | // app. |
| 57 | if (!initialized_) { |
| 58 | enable_rtx_ = enable; |
| 59 | return true; |
| 60 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 61 | RTC_LOG(LS_WARNING) << "Cannot toggle rtx after initialization!"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 62 | return false; |
| 63 | } |
| 64 | } |
| 65 | |
ossu | dedfd28 | 2016-06-14 07:12:39 -0700 | [diff] [blame] | 66 | void ChannelManager::GetSupportedAudioSendCodecs( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 67 | std::vector<AudioCodec>* codecs) const { |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 68 | if (!media_engine_) { |
| 69 | return; |
| 70 | } |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 71 | *codecs = media_engine_->voice().send_codecs(); |
ossu | dedfd28 | 2016-06-14 07:12:39 -0700 | [diff] [blame] | 72 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 73 | |
ossu | dedfd28 | 2016-06-14 07:12:39 -0700 | [diff] [blame] | 74 | void ChannelManager::GetSupportedAudioReceiveCodecs( |
| 75 | std::vector<AudioCodec>* codecs) const { |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 76 | if (!media_engine_) { |
| 77 | return; |
| 78 | } |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 79 | *codecs = media_engine_->voice().recv_codecs(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Johannes Kron | 8e8b36a | 2020-02-07 14:23:45 +0000 | [diff] [blame] | 82 | void ChannelManager::GetSupportedVideoCodecs( |
magjed | 3cf8ece | 2016-11-10 03:36:53 -0800 | [diff] [blame] | 83 | std::vector<VideoCodec>* codecs) const { |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 84 | if (!media_engine_) { |
| 85 | return; |
| 86 | } |
magjed | 3cf8ece | 2016-11-10 03:36:53 -0800 | [diff] [blame] | 87 | codecs->clear(); |
| 88 | |
Johannes Kron | 8e8b36a | 2020-02-07 14:23:45 +0000 | [diff] [blame] | 89 | std::vector<VideoCodec> video_codecs = media_engine_->video().codecs(); |
brandtr | ffc6118 | 2016-11-28 06:02:22 -0800 | [diff] [blame] | 90 | for (const auto& video_codec : video_codecs) { |
| 91 | if (!enable_rtx_ && |
Niels Möller | 3c7d599 | 2018-10-19 15:29:54 +0200 | [diff] [blame] | 92 | absl::EqualsIgnoreCase(kRtxCodecName, video_codec.name)) { |
magjed | 3cf8ece | 2016-11-10 03:36:53 -0800 | [diff] [blame] | 93 | continue; |
| 94 | } |
brandtr | ffc6118 | 2016-11-28 06:02:22 -0800 | [diff] [blame] | 95 | codecs->push_back(video_codec); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 99 | void ChannelManager::GetSupportedDataCodecs( |
| 100 | std::vector<DataCodec>* codecs) const { |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 101 | *codecs = data_engine_->data_codecs(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | bool ChannelManager::Init() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 105 | RTC_DCHECK(!initialized_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 106 | if (initialized_) { |
| 107 | return false; |
| 108 | } |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 109 | RTC_DCHECK(network_thread_); |
| 110 | RTC_DCHECK(worker_thread_); |
| 111 | if (!network_thread_->IsCurrent()) { |
| 112 | // Do not allow invoking calls to other threads on the network thread. |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 113 | network_thread_->Invoke<void>( |
Karl Wiberg | 3256225 | 2019-02-21 13:38:30 +0100 | [diff] [blame] | 114 | RTC_FROM_HERE, [&] { network_thread_->DisallowBlockingCalls(); }); |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 115 | } |
| 116 | |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 117 | if (media_engine_) { |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 118 | initialized_ = worker_thread_->Invoke<bool>( |
| 119 | RTC_FROM_HERE, [&] { return media_engine_->Init(); }); |
| 120 | RTC_DCHECK(initialized_); |
| 121 | } else { |
| 122 | initialized_ = true; |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 123 | } |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 124 | return initialized_; |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Markus Handell | 0357b3e | 2020-03-16 13:40:51 +0100 | [diff] [blame] | 127 | RtpHeaderExtensions ChannelManager::GetDefaultEnabledAudioRtpHeaderExtensions() |
| 128 | const { |
| 129 | if (!media_engine_) |
| 130 | return {}; |
| 131 | return GetDefaultEnabledRtpHeaderExtensions(media_engine_->voice()); |
| 132 | } |
| 133 | |
| 134 | std::vector<webrtc::RtpHeaderExtensionCapability> |
| 135 | ChannelManager::GetSupportedAudioRtpHeaderExtensions() const { |
| 136 | if (!media_engine_) |
| 137 | return {}; |
| 138 | return media_engine_->voice().GetRtpHeaderExtensions(); |
| 139 | } |
| 140 | |
| 141 | RtpHeaderExtensions ChannelManager::GetDefaultEnabledVideoRtpHeaderExtensions() |
| 142 | const { |
| 143 | if (!media_engine_) |
| 144 | return {}; |
| 145 | return GetDefaultEnabledRtpHeaderExtensions(media_engine_->video()); |
| 146 | } |
| 147 | |
| 148 | std::vector<webrtc::RtpHeaderExtensionCapability> |
| 149 | ChannelManager::GetSupportedVideoRtpHeaderExtensions() const { |
| 150 | if (!media_engine_) |
| 151 | return {}; |
| 152 | return media_engine_->video().GetRtpHeaderExtensions(); |
| 153 | } |
| 154 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 155 | void ChannelManager::Terminate() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 156 | RTC_DCHECK(initialized_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 157 | if (!initialized_) { |
| 158 | return; |
| 159 | } |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 160 | // Need to destroy the channels on the worker thread. |
| 161 | worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] { |
| 162 | video_channels_.clear(); |
| 163 | voice_channels_.clear(); |
| 164 | data_channels_.clear(); |
| 165 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 166 | initialized_ = false; |
| 167 | } |
| 168 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 169 | VoiceChannel* ChannelManager::CreateVoiceChannel( |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 170 | webrtc::Call* call, |
| 171 | const cricket::MediaConfig& media_config, |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 172 | webrtc::RtpTransportInternal* rtp_transport, |
Anton Sukhanov | 4f08faa | 2019-05-21 11:12:57 -0700 | [diff] [blame] | 173 | const webrtc::MediaTransportConfig& media_transport_config, |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 174 | rtc::Thread* signaling_thread, |
| 175 | const std::string& content_name, |
| 176 | bool srtp_required, |
Benjamin Wright | a54daf1 | 2018-10-11 15:33:17 -0700 | [diff] [blame] | 177 | const webrtc::CryptoOptions& crypto_options, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 178 | rtc::UniqueRandomIdGenerator* ssrc_generator, |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 179 | const AudioOptions& options) { |
| 180 | if (!worker_thread_->IsCurrent()) { |
| 181 | return worker_thread_->Invoke<VoiceChannel*>(RTC_FROM_HERE, [&] { |
Anton Sukhanov | 4f08faa | 2019-05-21 11:12:57 -0700 | [diff] [blame] | 182 | return CreateVoiceChannel(call, media_config, rtp_transport, |
| 183 | media_transport_config, signaling_thread, |
| 184 | content_name, srtp_required, crypto_options, |
| 185 | ssrc_generator, options); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 186 | }); |
| 187 | } |
| 188 | |
| 189 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 190 | RTC_DCHECK(initialized_); |
| 191 | RTC_DCHECK(call); |
| 192 | if (!media_engine_) { |
| 193 | return nullptr; |
| 194 | } |
| 195 | |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 196 | VoiceMediaChannel* media_channel = media_engine_->voice().CreateMediaChannel( |
| 197 | call, media_config, options, crypto_options); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 198 | if (!media_channel) { |
| 199 | return nullptr; |
| 200 | } |
| 201 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 202 | auto voice_channel = std::make_unique<VoiceChannel>( |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 203 | worker_thread_, network_thread_, signaling_thread, |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 204 | absl::WrapUnique(media_channel), content_name, srtp_required, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 205 | crypto_options, ssrc_generator); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 206 | |
Anton Sukhanov | 4f08faa | 2019-05-21 11:12:57 -0700 | [diff] [blame] | 207 | voice_channel->Init_w(rtp_transport, media_transport_config); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 208 | |
| 209 | VoiceChannel* voice_channel_ptr = voice_channel.get(); |
| 210 | voice_channels_.push_back(std::move(voice_channel)); |
| 211 | return voice_channel_ptr; |
| 212 | } |
| 213 | |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 214 | void ChannelManager::DestroyVoiceChannel(VoiceChannel* voice_channel) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 215 | TRACE_EVENT0("webrtc", "ChannelManager::DestroyVoiceChannel"); |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 216 | if (!voice_channel) { |
| 217 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 218 | } |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 219 | if (!worker_thread_->IsCurrent()) { |
| 220 | worker_thread_->Invoke<void>(RTC_FROM_HERE, |
| 221 | [&] { DestroyVoiceChannel(voice_channel); }); |
| 222 | return; |
| 223 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 224 | |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 225 | RTC_DCHECK(initialized_); |
Steve Anton | 774115c | 2017-08-30 10:48:46 -0700 | [diff] [blame] | 226 | |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 227 | auto it = absl::c_find_if(voice_channels_, |
| 228 | [&](const std::unique_ptr<VoiceChannel>& p) { |
| 229 | return p.get() == voice_channel; |
| 230 | }); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 231 | RTC_DCHECK(it != voice_channels_.end()); |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 232 | if (it == voice_channels_.end()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 233 | return; |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 234 | } |
| 235 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 236 | voice_channels_.erase(it); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | VideoChannel* ChannelManager::CreateVideoChannel( |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 240 | webrtc::Call* call, |
| 241 | const cricket::MediaConfig& media_config, |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 242 | webrtc::RtpTransportInternal* rtp_transport, |
Anton Sukhanov | 4f08faa | 2019-05-21 11:12:57 -0700 | [diff] [blame] | 243 | const webrtc::MediaTransportConfig& media_transport_config, |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 244 | rtc::Thread* signaling_thread, |
| 245 | const std::string& content_name, |
| 246 | bool srtp_required, |
Benjamin Wright | a54daf1 | 2018-10-11 15:33:17 -0700 | [diff] [blame] | 247 | const webrtc::CryptoOptions& crypto_options, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 248 | rtc::UniqueRandomIdGenerator* ssrc_generator, |
Jonas Oreland | a3aa9bd | 2019-04-17 07:38:40 +0200 | [diff] [blame] | 249 | const VideoOptions& options, |
| 250 | webrtc::VideoBitrateAllocatorFactory* video_bitrate_allocator_factory) { |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 251 | if (!worker_thread_->IsCurrent()) { |
| 252 | return worker_thread_->Invoke<VideoChannel*>(RTC_FROM_HERE, [&] { |
Anton Sukhanov | 4f08faa | 2019-05-21 11:12:57 -0700 | [diff] [blame] | 253 | return CreateVideoChannel( |
| 254 | call, media_config, rtp_transport, media_transport_config, |
| 255 | signaling_thread, content_name, srtp_required, crypto_options, |
| 256 | ssrc_generator, options, video_bitrate_allocator_factory); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 257 | }); |
| 258 | } |
| 259 | |
| 260 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 261 | RTC_DCHECK(initialized_); |
| 262 | RTC_DCHECK(call); |
| 263 | if (!media_engine_) { |
| 264 | return nullptr; |
| 265 | } |
| 266 | |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 267 | VideoMediaChannel* media_channel = media_engine_->video().CreateMediaChannel( |
Jonas Oreland | a3aa9bd | 2019-04-17 07:38:40 +0200 | [diff] [blame] | 268 | call, media_config, options, crypto_options, |
| 269 | video_bitrate_allocator_factory); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 270 | if (!media_channel) { |
| 271 | return nullptr; |
| 272 | } |
| 273 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 274 | auto video_channel = std::make_unique<VideoChannel>( |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 275 | worker_thread_, network_thread_, signaling_thread, |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 276 | absl::WrapUnique(media_channel), content_name, srtp_required, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 277 | crypto_options, ssrc_generator); |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 278 | |
Anton Sukhanov | 4f08faa | 2019-05-21 11:12:57 -0700 | [diff] [blame] | 279 | video_channel->Init_w(rtp_transport, media_transport_config); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 280 | |
| 281 | VideoChannel* video_channel_ptr = video_channel.get(); |
| 282 | video_channels_.push_back(std::move(video_channel)); |
| 283 | return video_channel_ptr; |
| 284 | } |
| 285 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 286 | void ChannelManager::DestroyVideoChannel(VideoChannel* video_channel) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 287 | TRACE_EVENT0("webrtc", "ChannelManager::DestroyVideoChannel"); |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 288 | if (!video_channel) { |
| 289 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 290 | } |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 291 | if (!worker_thread_->IsCurrent()) { |
| 292 | worker_thread_->Invoke<void>(RTC_FROM_HERE, |
| 293 | [&] { DestroyVideoChannel(video_channel); }); |
| 294 | return; |
| 295 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 296 | |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 297 | RTC_DCHECK(initialized_); |
Steve Anton | 774115c | 2017-08-30 10:48:46 -0700 | [diff] [blame] | 298 | |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 299 | auto it = absl::c_find_if(video_channels_, |
| 300 | [&](const std::unique_ptr<VideoChannel>& p) { |
| 301 | return p.get() == video_channel; |
| 302 | }); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 303 | RTC_DCHECK(it != video_channels_.end()); |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 304 | if (it == video_channels_.end()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 305 | return; |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 306 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 307 | |
| 308 | video_channels_.erase(it); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 309 | } |
| 310 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 311 | RtpDataChannel* ChannelManager::CreateRtpDataChannel( |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 312 | const cricket::MediaConfig& media_config, |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 313 | webrtc::RtpTransportInternal* rtp_transport, |
| 314 | rtc::Thread* signaling_thread, |
| 315 | const std::string& content_name, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 316 | bool srtp_required, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 317 | const webrtc::CryptoOptions& crypto_options, |
| 318 | rtc::UniqueRandomIdGenerator* ssrc_generator) { |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 319 | if (!worker_thread_->IsCurrent()) { |
| 320 | return worker_thread_->Invoke<RtpDataChannel*>(RTC_FROM_HERE, [&] { |
| 321 | return CreateRtpDataChannel(media_config, rtp_transport, signaling_thread, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 322 | content_name, srtp_required, crypto_options, |
| 323 | ssrc_generator); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 324 | }); |
| 325 | } |
| 326 | |
| 327 | // This is ok to alloc from a thread other than the worker thread. |
| 328 | RTC_DCHECK(initialized_); |
| 329 | DataMediaChannel* media_channel = data_engine_->CreateChannel(media_config); |
| 330 | if (!media_channel) { |
| 331 | RTC_LOG(LS_WARNING) << "Failed to create RTP data channel."; |
| 332 | return nullptr; |
| 333 | } |
| 334 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 335 | auto data_channel = std::make_unique<RtpDataChannel>( |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 336 | worker_thread_, network_thread_, signaling_thread, |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 337 | absl::WrapUnique(media_channel), content_name, srtp_required, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 338 | crypto_options, ssrc_generator); |
Anton Sukhanov | 4f08faa | 2019-05-21 11:12:57 -0700 | [diff] [blame] | 339 | |
| 340 | // Media Transports are not supported with Rtp Data Channel. |
| 341 | data_channel->Init_w(rtp_transport, webrtc::MediaTransportConfig()); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 342 | |
| 343 | RtpDataChannel* data_channel_ptr = data_channel.get(); |
| 344 | data_channels_.push_back(std::move(data_channel)); |
| 345 | return data_channel_ptr; |
| 346 | } |
| 347 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 348 | void ChannelManager::DestroyRtpDataChannel(RtpDataChannel* data_channel) { |
| 349 | TRACE_EVENT0("webrtc", "ChannelManager::DestroyRtpDataChannel"); |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 350 | if (!data_channel) { |
| 351 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 352 | } |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 353 | if (!worker_thread_->IsCurrent()) { |
| 354 | worker_thread_->Invoke<void>( |
| 355 | RTC_FROM_HERE, [&] { return DestroyRtpDataChannel(data_channel); }); |
| 356 | return; |
| 357 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 358 | |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 359 | RTC_DCHECK(initialized_); |
Steve Anton | 774115c | 2017-08-30 10:48:46 -0700 | [diff] [blame] | 360 | |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 361 | auto it = absl::c_find_if(data_channels_, |
| 362 | [&](const std::unique_ptr<RtpDataChannel>& p) { |
| 363 | return p.get() == data_channel; |
| 364 | }); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 365 | RTC_DCHECK(it != data_channels_.end()); |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 366 | if (it == data_channels_.end()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 367 | return; |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 368 | } |
Zhi Huang | 95e7dbb | 2018-03-29 00:08:03 +0000 | [diff] [blame] | 369 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 370 | data_channels_.erase(it); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Niels Möller | e8e4dc4 | 2019-06-11 14:04:16 +0200 | [diff] [blame] | 373 | bool ChannelManager::StartAecDump(webrtc::FileWrapper file, |
ivoc | d66b44d | 2016-01-15 03:06:36 -0800 | [diff] [blame] | 374 | int64_t max_size_bytes) { |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 375 | return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
Niels Möller | e8e4dc4 | 2019-06-11 14:04:16 +0200 | [diff] [blame] | 376 | return media_engine_->voice().StartAecDump(std::move(file), max_size_bytes); |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 377 | }); |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 378 | } |
| 379 | |
ivoc | 797ef12 | 2015-10-22 03:25:41 -0700 | [diff] [blame] | 380 | void ChannelManager::StopAecDump() { |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 381 | worker_thread_->Invoke<void>(RTC_FROM_HERE, |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 382 | [&] { media_engine_->voice().StopAecDump(); }); |
ivoc | 797ef12 | 2015-10-22 03:25:41 -0700 | [diff] [blame] | 383 | } |
| 384 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 385 | } // namespace cricket |