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 | |
| 82 | void ChannelManager::GetSupportedAudioRtpHeaderExtensions( |
| 83 | RtpHeaderExtensions* ext) const { |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 84 | if (!media_engine_) { |
| 85 | return; |
| 86 | } |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 87 | *ext = media_engine_->voice().GetCapabilities().header_extensions; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 88 | } |
| 89 | |
magjed | 3cf8ece | 2016-11-10 03:36:53 -0800 | [diff] [blame] | 90 | void ChannelManager::GetSupportedVideoCodecs( |
| 91 | std::vector<VideoCodec>* codecs) const { |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 92 | if (!media_engine_) { |
| 93 | return; |
| 94 | } |
magjed | 3cf8ece | 2016-11-10 03:36:53 -0800 | [diff] [blame] | 95 | codecs->clear(); |
| 96 | |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 97 | std::vector<VideoCodec> video_codecs = media_engine_->video().codecs(); |
brandtr | ffc6118 | 2016-11-28 06:02:22 -0800 | [diff] [blame] | 98 | for (const auto& video_codec : video_codecs) { |
| 99 | if (!enable_rtx_ && |
Niels Möller | 3c7d599 | 2018-10-19 15:29:54 +0200 | [diff] [blame] | 100 | absl::EqualsIgnoreCase(kRtxCodecName, video_codec.name)) { |
magjed | 3cf8ece | 2016-11-10 03:36:53 -0800 | [diff] [blame] | 101 | continue; |
| 102 | } |
brandtr | ffc6118 | 2016-11-28 06:02:22 -0800 | [diff] [blame] | 103 | codecs->push_back(video_codec); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
| 107 | void ChannelManager::GetSupportedVideoRtpHeaderExtensions( |
| 108 | RtpHeaderExtensions* ext) const { |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 109 | if (!media_engine_) { |
| 110 | return; |
| 111 | } |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 112 | *ext = media_engine_->video().GetCapabilities().header_extensions; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void ChannelManager::GetSupportedDataCodecs( |
| 116 | std::vector<DataCodec>* codecs) const { |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 117 | *codecs = data_engine_->data_codecs(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | bool ChannelManager::Init() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 121 | RTC_DCHECK(!initialized_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 122 | if (initialized_) { |
| 123 | return false; |
| 124 | } |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 125 | RTC_DCHECK(network_thread_); |
| 126 | RTC_DCHECK(worker_thread_); |
| 127 | if (!network_thread_->IsCurrent()) { |
| 128 | // Do not allow invoking calls to other threads on the network thread. |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 129 | network_thread_->Invoke<void>( |
Karl Wiberg | 3256225 | 2019-02-21 13:38:30 +0100 | [diff] [blame^] | 130 | RTC_FROM_HERE, [&] { network_thread_->DisallowBlockingCalls(); }); |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 131 | } |
| 132 | |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 133 | if (media_engine_) { |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 134 | initialized_ = worker_thread_->Invoke<bool>( |
| 135 | RTC_FROM_HERE, [&] { return media_engine_->Init(); }); |
| 136 | RTC_DCHECK(initialized_); |
| 137 | } else { |
| 138 | initialized_ = true; |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 139 | } |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 140 | return initialized_; |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 141 | } |
| 142 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 143 | void ChannelManager::Terminate() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 144 | RTC_DCHECK(initialized_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 145 | if (!initialized_) { |
| 146 | return; |
| 147 | } |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 148 | // Need to destroy the channels on the worker thread. |
| 149 | worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] { |
| 150 | video_channels_.clear(); |
| 151 | voice_channels_.clear(); |
| 152 | data_channels_.clear(); |
| 153 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 154 | initialized_ = false; |
| 155 | } |
| 156 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 157 | VoiceChannel* ChannelManager::CreateVoiceChannel( |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 158 | webrtc::Call* call, |
| 159 | const cricket::MediaConfig& media_config, |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 160 | webrtc::RtpTransportInternal* rtp_transport, |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 161 | webrtc::MediaTransportInterface* media_transport, |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 162 | rtc::Thread* signaling_thread, |
| 163 | const std::string& content_name, |
| 164 | bool srtp_required, |
Benjamin Wright | a54daf1 | 2018-10-11 15:33:17 -0700 | [diff] [blame] | 165 | const webrtc::CryptoOptions& crypto_options, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 166 | rtc::UniqueRandomIdGenerator* ssrc_generator, |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 167 | const AudioOptions& options) { |
| 168 | if (!worker_thread_->IsCurrent()) { |
| 169 | return worker_thread_->Invoke<VoiceChannel*>(RTC_FROM_HERE, [&] { |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 170 | return CreateVoiceChannel( |
| 171 | call, media_config, rtp_transport, media_transport, signaling_thread, |
| 172 | content_name, srtp_required, crypto_options, ssrc_generator, options); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 173 | }); |
| 174 | } |
| 175 | |
| 176 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 177 | RTC_DCHECK(initialized_); |
| 178 | RTC_DCHECK(call); |
| 179 | if (!media_engine_) { |
| 180 | return nullptr; |
| 181 | } |
| 182 | |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 183 | VoiceMediaChannel* media_channel = media_engine_->voice().CreateMediaChannel( |
| 184 | call, media_config, options, crypto_options); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 185 | if (!media_channel) { |
| 186 | return nullptr; |
| 187 | } |
| 188 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 189 | auto voice_channel = absl::make_unique<VoiceChannel>( |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 190 | worker_thread_, network_thread_, signaling_thread, |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 191 | absl::WrapUnique(media_channel), content_name, srtp_required, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 192 | crypto_options, ssrc_generator); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 193 | |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 194 | voice_channel->Init_w(rtp_transport, media_transport); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 195 | |
| 196 | VoiceChannel* voice_channel_ptr = voice_channel.get(); |
| 197 | voice_channels_.push_back(std::move(voice_channel)); |
| 198 | return voice_channel_ptr; |
| 199 | } |
| 200 | |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 201 | void ChannelManager::DestroyVoiceChannel(VoiceChannel* voice_channel) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 202 | TRACE_EVENT0("webrtc", "ChannelManager::DestroyVoiceChannel"); |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 203 | if (!voice_channel) { |
| 204 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 205 | } |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 206 | if (!worker_thread_->IsCurrent()) { |
| 207 | worker_thread_->Invoke<void>(RTC_FROM_HERE, |
| 208 | [&] { DestroyVoiceChannel(voice_channel); }); |
| 209 | return; |
| 210 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 211 | |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 212 | RTC_DCHECK(initialized_); |
Steve Anton | 774115c | 2017-08-30 10:48:46 -0700 | [diff] [blame] | 213 | |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 214 | auto it = absl::c_find_if(voice_channels_, |
| 215 | [&](const std::unique_ptr<VoiceChannel>& p) { |
| 216 | return p.get() == voice_channel; |
| 217 | }); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 218 | RTC_DCHECK(it != voice_channels_.end()); |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 219 | if (it == voice_channels_.end()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 220 | return; |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 221 | } |
| 222 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 223 | voice_channels_.erase(it); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | VideoChannel* ChannelManager::CreateVideoChannel( |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 227 | webrtc::Call* call, |
| 228 | const cricket::MediaConfig& media_config, |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 229 | webrtc::RtpTransportInternal* rtp_transport, |
Niels Möller | 4687915 | 2019-01-07 15:54:47 +0100 | [diff] [blame] | 230 | webrtc::MediaTransportInterface* media_transport, |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 231 | rtc::Thread* signaling_thread, |
| 232 | const std::string& content_name, |
| 233 | bool srtp_required, |
Benjamin Wright | a54daf1 | 2018-10-11 15:33:17 -0700 | [diff] [blame] | 234 | const webrtc::CryptoOptions& crypto_options, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 235 | rtc::UniqueRandomIdGenerator* ssrc_generator, |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 236 | const VideoOptions& options) { |
| 237 | if (!worker_thread_->IsCurrent()) { |
| 238 | return worker_thread_->Invoke<VideoChannel*>(RTC_FROM_HERE, [&] { |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 239 | return CreateVideoChannel( |
| 240 | call, media_config, rtp_transport, media_transport, signaling_thread, |
| 241 | content_name, srtp_required, crypto_options, ssrc_generator, options); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 242 | }); |
| 243 | } |
| 244 | |
| 245 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 246 | RTC_DCHECK(initialized_); |
| 247 | RTC_DCHECK(call); |
| 248 | if (!media_engine_) { |
| 249 | return nullptr; |
| 250 | } |
| 251 | |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 252 | VideoMediaChannel* media_channel = media_engine_->video().CreateMediaChannel( |
Benjamin Wright | bfb444c | 2018-10-15 10:20:24 -0700 | [diff] [blame] | 253 | call, media_config, options, crypto_options); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 254 | if (!media_channel) { |
| 255 | return nullptr; |
| 256 | } |
| 257 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 258 | auto video_channel = absl::make_unique<VideoChannel>( |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 259 | worker_thread_, network_thread_, signaling_thread, |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 260 | absl::WrapUnique(media_channel), content_name, srtp_required, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 261 | crypto_options, ssrc_generator); |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 262 | |
Niels Möller | 4687915 | 2019-01-07 15:54:47 +0100 | [diff] [blame] | 263 | video_channel->Init_w(rtp_transport, media_transport); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 264 | |
| 265 | VideoChannel* video_channel_ptr = video_channel.get(); |
| 266 | video_channels_.push_back(std::move(video_channel)); |
| 267 | return video_channel_ptr; |
| 268 | } |
| 269 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 270 | void ChannelManager::DestroyVideoChannel(VideoChannel* video_channel) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 271 | TRACE_EVENT0("webrtc", "ChannelManager::DestroyVideoChannel"); |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 272 | if (!video_channel) { |
| 273 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 274 | } |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 275 | if (!worker_thread_->IsCurrent()) { |
| 276 | worker_thread_->Invoke<void>(RTC_FROM_HERE, |
| 277 | [&] { DestroyVideoChannel(video_channel); }); |
| 278 | return; |
| 279 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 280 | |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 281 | RTC_DCHECK(initialized_); |
Steve Anton | 774115c | 2017-08-30 10:48:46 -0700 | [diff] [blame] | 282 | |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 283 | auto it = absl::c_find_if(video_channels_, |
| 284 | [&](const std::unique_ptr<VideoChannel>& p) { |
| 285 | return p.get() == video_channel; |
| 286 | }); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 287 | RTC_DCHECK(it != video_channels_.end()); |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 288 | if (it == video_channels_.end()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 289 | return; |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 290 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 291 | |
| 292 | video_channels_.erase(it); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 293 | } |
| 294 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 295 | RtpDataChannel* ChannelManager::CreateRtpDataChannel( |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 296 | const cricket::MediaConfig& media_config, |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 297 | webrtc::RtpTransportInternal* rtp_transport, |
| 298 | rtc::Thread* signaling_thread, |
| 299 | const std::string& content_name, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 300 | bool srtp_required, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 301 | const webrtc::CryptoOptions& crypto_options, |
| 302 | rtc::UniqueRandomIdGenerator* ssrc_generator) { |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 303 | if (!worker_thread_->IsCurrent()) { |
| 304 | return worker_thread_->Invoke<RtpDataChannel*>(RTC_FROM_HERE, [&] { |
| 305 | return CreateRtpDataChannel(media_config, rtp_transport, signaling_thread, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 306 | content_name, srtp_required, crypto_options, |
| 307 | ssrc_generator); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 308 | }); |
| 309 | } |
| 310 | |
| 311 | // This is ok to alloc from a thread other than the worker thread. |
| 312 | RTC_DCHECK(initialized_); |
| 313 | DataMediaChannel* media_channel = data_engine_->CreateChannel(media_config); |
| 314 | if (!media_channel) { |
| 315 | RTC_LOG(LS_WARNING) << "Failed to create RTP data channel."; |
| 316 | return nullptr; |
| 317 | } |
| 318 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 319 | auto data_channel = absl::make_unique<RtpDataChannel>( |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 320 | worker_thread_, network_thread_, signaling_thread, |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 321 | absl::WrapUnique(media_channel), content_name, srtp_required, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 322 | crypto_options, ssrc_generator); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 323 | data_channel->Init_w(rtp_transport); |
| 324 | |
| 325 | RtpDataChannel* data_channel_ptr = data_channel.get(); |
| 326 | data_channels_.push_back(std::move(data_channel)); |
| 327 | return data_channel_ptr; |
| 328 | } |
| 329 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 330 | void ChannelManager::DestroyRtpDataChannel(RtpDataChannel* data_channel) { |
| 331 | TRACE_EVENT0("webrtc", "ChannelManager::DestroyRtpDataChannel"); |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 332 | if (!data_channel) { |
| 333 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 334 | } |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 335 | if (!worker_thread_->IsCurrent()) { |
| 336 | worker_thread_->Invoke<void>( |
| 337 | RTC_FROM_HERE, [&] { return DestroyRtpDataChannel(data_channel); }); |
| 338 | return; |
| 339 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 340 | |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 341 | RTC_DCHECK(initialized_); |
Steve Anton | 774115c | 2017-08-30 10:48:46 -0700 | [diff] [blame] | 342 | |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 343 | auto it = absl::c_find_if(data_channels_, |
| 344 | [&](const std::unique_ptr<RtpDataChannel>& p) { |
| 345 | return p.get() == data_channel; |
| 346 | }); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 347 | RTC_DCHECK(it != data_channels_.end()); |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 348 | if (it == data_channels_.end()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 349 | return; |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 350 | } |
Zhi Huang | 95e7dbb | 2018-03-29 00:08:03 +0000 | [diff] [blame] | 351 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 352 | data_channels_.erase(it); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 353 | } |
| 354 | |
ivoc | d66b44d | 2016-01-15 03:06:36 -0800 | [diff] [blame] | 355 | bool ChannelManager::StartAecDump(rtc::PlatformFile file, |
| 356 | int64_t max_size_bytes) { |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 357 | return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 358 | return media_engine_->voice().StartAecDump(file, max_size_bytes); |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 359 | }); |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 360 | } |
| 361 | |
ivoc | 797ef12 | 2015-10-22 03:25:41 -0700 | [diff] [blame] | 362 | void ChannelManager::StopAecDump() { |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 363 | worker_thread_->Invoke<void>(RTC_FROM_HERE, |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 364 | [&] { media_engine_->voice().StopAecDump(); }); |
ivoc | 797ef12 | 2015-10-22 03:25:41 -0700 | [diff] [blame] | 365 | } |
| 366 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 367 | } // namespace cricket |