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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "pc/channelmanager.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 13 | #include <algorithm> |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 14 | #include <utility> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "media/base/rtpdataengine.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/checks.h" |
| 18 | #include "rtc_base/logging.h" |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 19 | #include "rtc_base/ptr_util.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/stringutils.h" |
| 21 | #include "rtc_base/trace_event.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 22 | |
| 23 | namespace cricket { |
| 24 | |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 25 | ChannelManager::ChannelManager( |
| 26 | std::unique_ptr<MediaEngineInterface> media_engine, |
| 27 | std::unique_ptr<DataEngineInterface> data_engine, |
| 28 | rtc::Thread* worker_thread, |
| 29 | rtc::Thread* network_thread) |
| 30 | : media_engine_(std::move(media_engine)), |
| 31 | data_engine_(std::move(data_engine)), |
| 32 | main_thread_(rtc::Thread::Current()), |
| 33 | worker_thread_(worker_thread), |
| 34 | network_thread_(network_thread) { |
| 35 | RTC_DCHECK(data_engine_); |
| 36 | RTC_DCHECK(worker_thread_); |
| 37 | RTC_DCHECK(network_thread_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | ChannelManager::~ChannelManager() { |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 41 | if (initialized_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 42 | Terminate(); |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 43 | } |
perkj | c11b184 | 2016-03-07 17:34:13 -0800 | [diff] [blame] | 44 | // The media engine needs to be deleted on the worker thread for thread safe |
| 45 | // destruction, |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 46 | worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] { media_engine_.reset(); }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | bool ChannelManager::SetVideoRtxEnabled(bool enable) { |
| 50 | // To be safe, this call is only allowed before initialization. Apps like |
| 51 | // Flute only have a singleton ChannelManager and we don't want this flag to |
| 52 | // be toggled between calls or when there's concurrent calls. We expect apps |
| 53 | // to enable this at startup and retain that setting for the lifetime of the |
| 54 | // app. |
| 55 | if (!initialized_) { |
| 56 | enable_rtx_ = enable; |
| 57 | return true; |
| 58 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 59 | RTC_LOG(LS_WARNING) << "Cannot toggle rtx after initialization!"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 60 | return false; |
| 61 | } |
| 62 | } |
| 63 | |
ossu | dedfd28 | 2016-06-14 07:12:39 -0700 | [diff] [blame] | 64 | void ChannelManager::GetSupportedAudioSendCodecs( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 65 | std::vector<AudioCodec>* codecs) const { |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 66 | if (!media_engine_) { |
| 67 | return; |
| 68 | } |
ossu | dedfd28 | 2016-06-14 07:12:39 -0700 | [diff] [blame] | 69 | *codecs = media_engine_->audio_send_codecs(); |
| 70 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 71 | |
ossu | dedfd28 | 2016-06-14 07:12:39 -0700 | [diff] [blame] | 72 | void ChannelManager::GetSupportedAudioReceiveCodecs( |
| 73 | std::vector<AudioCodec>* codecs) const { |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 74 | if (!media_engine_) { |
| 75 | return; |
| 76 | } |
ossu | dedfd28 | 2016-06-14 07:12:39 -0700 | [diff] [blame] | 77 | *codecs = media_engine_->audio_recv_codecs(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void ChannelManager::GetSupportedAudioRtpHeaderExtensions( |
| 81 | RtpHeaderExtensions* ext) const { |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 82 | if (!media_engine_) { |
| 83 | return; |
| 84 | } |
Stefan Holmer | 9d69c3f | 2015-12-07 10:45:43 +0100 | [diff] [blame] | 85 | *ext = media_engine_->GetAudioCapabilities().header_extensions; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 86 | } |
| 87 | |
magjed | 3cf8ece | 2016-11-10 03:36:53 -0800 | [diff] [blame] | 88 | void ChannelManager::GetSupportedVideoCodecs( |
| 89 | std::vector<VideoCodec>* codecs) const { |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 90 | if (!media_engine_) { |
| 91 | return; |
| 92 | } |
magjed | 3cf8ece | 2016-11-10 03:36:53 -0800 | [diff] [blame] | 93 | codecs->clear(); |
| 94 | |
brandtr | ffc6118 | 2016-11-28 06:02:22 -0800 | [diff] [blame] | 95 | std::vector<VideoCodec> video_codecs = media_engine_->video_codecs(); |
| 96 | for (const auto& video_codec : video_codecs) { |
| 97 | if (!enable_rtx_ && |
| 98 | _stricmp(kRtxCodecName, video_codec.name.c_str()) == 0) { |
magjed | 3cf8ece | 2016-11-10 03:36:53 -0800 | [diff] [blame] | 99 | continue; |
| 100 | } |
brandtr | ffc6118 | 2016-11-28 06:02:22 -0800 | [diff] [blame] | 101 | codecs->push_back(video_codec); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | void ChannelManager::GetSupportedVideoRtpHeaderExtensions( |
| 106 | RtpHeaderExtensions* ext) const { |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 107 | if (!media_engine_) { |
| 108 | return; |
| 109 | } |
Stefan Holmer | 9d69c3f | 2015-12-07 10:45:43 +0100 | [diff] [blame] | 110 | *ext = media_engine_->GetVideoCapabilities().header_extensions; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void ChannelManager::GetSupportedDataCodecs( |
| 114 | std::vector<DataCodec>* codecs) const { |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 115 | *codecs = data_engine_->data_codecs(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | bool ChannelManager::Init() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 119 | RTC_DCHECK(!initialized_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 120 | if (initialized_) { |
| 121 | return false; |
| 122 | } |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 123 | RTC_DCHECK(network_thread_); |
| 124 | RTC_DCHECK(worker_thread_); |
| 125 | if (!network_thread_->IsCurrent()) { |
| 126 | // Do not allow invoking calls to other threads on the network thread. |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 127 | network_thread_->Invoke<void>( |
| 128 | RTC_FROM_HERE, [&] { network_thread_->SetAllowBlockingCalls(false); }); |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 129 | } |
| 130 | |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 131 | if (media_engine_) { |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 132 | initialized_ = worker_thread_->Invoke<bool>( |
| 133 | RTC_FROM_HERE, [&] { return media_engine_->Init(); }); |
| 134 | RTC_DCHECK(initialized_); |
| 135 | } else { |
| 136 | initialized_ = true; |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 137 | } |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 138 | return initialized_; |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 139 | } |
| 140 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 141 | void ChannelManager::Terminate() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 142 | RTC_DCHECK(initialized_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 143 | if (!initialized_) { |
| 144 | return; |
| 145 | } |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 146 | // Need to destroy the channels on the worker thread. |
| 147 | worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] { |
| 148 | video_channels_.clear(); |
| 149 | voice_channels_.clear(); |
| 150 | data_channels_.clear(); |
| 151 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 152 | initialized_ = false; |
| 153 | } |
| 154 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 155 | VoiceChannel* ChannelManager::CreateVoiceChannel( |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 156 | webrtc::Call* call, |
| 157 | const cricket::MediaConfig& media_config, |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 158 | DtlsTransportInternal* rtp_transport, |
| 159 | DtlsTransportInternal* rtcp_transport, |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 160 | rtc::Thread* signaling_thread, |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 161 | const std::string& content_name, |
deadbeef | 7af91dd | 2016-12-13 11:29:11 -0800 | [diff] [blame] | 162 | bool srtp_required, |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 163 | const AudioOptions& options) { |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 164 | return worker_thread_->Invoke<VoiceChannel*>(RTC_FROM_HERE, [&] { |
| 165 | return CreateVoiceChannel_w( |
| 166 | call, media_config, rtp_transport, rtcp_transport, rtp_transport, |
| 167 | rtcp_transport, signaling_thread, content_name, srtp_required, options); |
| 168 | }); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | VoiceChannel* ChannelManager::CreateVoiceChannel( |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 172 | webrtc::Call* call, |
| 173 | const cricket::MediaConfig& media_config, |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 174 | rtc::PacketTransportInternal* rtp_transport, |
| 175 | rtc::PacketTransportInternal* rtcp_transport, |
| 176 | rtc::Thread* signaling_thread, |
| 177 | const std::string& content_name, |
| 178 | bool srtp_required, |
| 179 | const AudioOptions& options) { |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 180 | return worker_thread_->Invoke<VoiceChannel*>(RTC_FROM_HERE, [&] { |
| 181 | return CreateVoiceChannel_w(call, media_config, nullptr, nullptr, |
| 182 | rtp_transport, rtcp_transport, signaling_thread, |
| 183 | content_name, srtp_required, options); |
| 184 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 187 | VoiceChannel* ChannelManager::CreateVoiceChannel( |
| 188 | webrtc::Call* call, |
| 189 | const cricket::MediaConfig& media_config, |
| 190 | webrtc::RtpTransportInternal* rtp_transport, |
| 191 | rtc::Thread* signaling_thread, |
| 192 | const std::string& content_name, |
| 193 | bool srtp_required, |
| 194 | const AudioOptions& options) { |
| 195 | if (!worker_thread_->IsCurrent()) { |
| 196 | return worker_thread_->Invoke<VoiceChannel*>(RTC_FROM_HERE, [&] { |
| 197 | return CreateVoiceChannel(call, media_config, rtp_transport, |
| 198 | signaling_thread, content_name, srtp_required, |
| 199 | options); |
| 200 | }); |
| 201 | } |
| 202 | |
| 203 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 204 | RTC_DCHECK(initialized_); |
| 205 | RTC_DCHECK(call); |
| 206 | if (!media_engine_) { |
| 207 | return nullptr; |
| 208 | } |
| 209 | |
| 210 | VoiceMediaChannel* media_channel = |
| 211 | media_engine_->CreateChannel(call, media_config, options); |
| 212 | if (!media_channel) { |
| 213 | return nullptr; |
| 214 | } |
| 215 | |
| 216 | auto voice_channel = rtc::MakeUnique<VoiceChannel>( |
| 217 | worker_thread_, network_thread_, signaling_thread, media_engine_.get(), |
| 218 | rtc::WrapUnique(media_channel), content_name, |
| 219 | rtp_transport->rtcp_packet_transport() == nullptr, srtp_required); |
| 220 | |
| 221 | voice_channel->Init_w(rtp_transport); |
| 222 | |
| 223 | VoiceChannel* voice_channel_ptr = voice_channel.get(); |
| 224 | voice_channels_.push_back(std::move(voice_channel)); |
| 225 | return voice_channel_ptr; |
| 226 | } |
| 227 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 228 | VoiceChannel* ChannelManager::CreateVoiceChannel_w( |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 229 | webrtc::Call* call, |
| 230 | const cricket::MediaConfig& media_config, |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 231 | DtlsTransportInternal* rtp_dtls_transport, |
| 232 | DtlsTransportInternal* rtcp_dtls_transport, |
| 233 | rtc::PacketTransportInternal* rtp_packet_transport, |
| 234 | rtc::PacketTransportInternal* rtcp_packet_transport, |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 235 | rtc::Thread* signaling_thread, |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 236 | const std::string& content_name, |
deadbeef | 7af91dd | 2016-12-13 11:29:11 -0800 | [diff] [blame] | 237 | bool srtp_required, |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 238 | const AudioOptions& options) { |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 239 | RTC_DCHECK_RUN_ON(worker_thread_); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 240 | RTC_DCHECK(initialized_); |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 241 | RTC_DCHECK(call); |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 242 | if (!media_engine_) { |
| 243 | return nullptr; |
| 244 | } |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 245 | |
nisse | 51542be | 2016-02-12 02:27:06 -0800 | [diff] [blame] | 246 | VoiceMediaChannel* media_channel = media_engine_->CreateChannel( |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 247 | call, media_config, options); |
Steve Anton | 774115c | 2017-08-30 10:48:46 -0700 | [diff] [blame] | 248 | if (!media_channel) { |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 249 | return nullptr; |
Steve Anton | 774115c | 2017-08-30 10:48:46 -0700 | [diff] [blame] | 250 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 251 | |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 252 | auto voice_channel = rtc::MakeUnique<VoiceChannel>( |
| 253 | worker_thread_, network_thread_, signaling_thread, media_engine_.get(), |
Steve Anton | 8699a32 | 2017-11-06 15:53:33 -0800 | [diff] [blame] | 254 | rtc::WrapUnique(media_channel), content_name, |
| 255 | rtcp_packet_transport == nullptr, srtp_required); |
| 256 | voice_channel->Init_w(rtp_dtls_transport, rtcp_dtls_transport, |
| 257 | rtp_packet_transport, rtcp_packet_transport); |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 258 | |
Steve Anton | 774115c | 2017-08-30 10:48:46 -0700 | [diff] [blame] | 259 | VoiceChannel* voice_channel_ptr = voice_channel.get(); |
| 260 | voice_channels_.push_back(std::move(voice_channel)); |
| 261 | return voice_channel_ptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 264 | void ChannelManager::DestroyVoiceChannel(VoiceChannel* voice_channel) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 265 | TRACE_EVENT0("webrtc", "ChannelManager::DestroyVoiceChannel"); |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 266 | if (!voice_channel) { |
| 267 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 268 | } |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 269 | if (!worker_thread_->IsCurrent()) { |
| 270 | worker_thread_->Invoke<void>(RTC_FROM_HERE, |
| 271 | [&] { DestroyVoiceChannel(voice_channel); }); |
| 272 | return; |
| 273 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 274 | |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 275 | RTC_DCHECK(initialized_); |
Steve Anton | 774115c | 2017-08-30 10:48:46 -0700 | [diff] [blame] | 276 | |
| 277 | auto it = std::find_if(voice_channels_.begin(), voice_channels_.end(), |
| 278 | [&](const std::unique_ptr<VoiceChannel>& p) { |
| 279 | return p.get() == voice_channel; |
| 280 | }); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 281 | RTC_DCHECK(it != voice_channels_.end()); |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 282 | if (it == voice_channels_.end()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 283 | return; |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 284 | } |
| 285 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 286 | voice_channels_.erase(it); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | VideoChannel* ChannelManager::CreateVideoChannel( |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 290 | webrtc::Call* call, |
| 291 | const cricket::MediaConfig& media_config, |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 292 | DtlsTransportInternal* rtp_transport, |
| 293 | DtlsTransportInternal* rtcp_transport, |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 294 | rtc::Thread* signaling_thread, |
buildbot@webrtc.org | 1ecbe45 | 2014-10-14 20:29:28 +0000 | [diff] [blame] | 295 | const std::string& content_name, |
deadbeef | 7af91dd | 2016-12-13 11:29:11 -0800 | [diff] [blame] | 296 | bool srtp_required, |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 297 | const VideoOptions& options) { |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 298 | return worker_thread_->Invoke<VideoChannel*>(RTC_FROM_HERE, [&] { |
| 299 | return CreateVideoChannel_w( |
| 300 | call, media_config, rtp_transport, rtcp_transport, rtp_transport, |
| 301 | rtcp_transport, signaling_thread, content_name, srtp_required, options); |
| 302 | }); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | VideoChannel* ChannelManager::CreateVideoChannel( |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 306 | webrtc::Call* call, |
| 307 | const cricket::MediaConfig& media_config, |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 308 | rtc::PacketTransportInternal* rtp_transport, |
| 309 | rtc::PacketTransportInternal* rtcp_transport, |
| 310 | rtc::Thread* signaling_thread, |
| 311 | const std::string& content_name, |
| 312 | bool srtp_required, |
| 313 | const VideoOptions& options) { |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 314 | return worker_thread_->Invoke<VideoChannel*>(RTC_FROM_HERE, [&] { |
| 315 | return CreateVideoChannel_w(call, media_config, nullptr, nullptr, |
| 316 | rtp_transport, rtcp_transport, signaling_thread, |
| 317 | content_name, srtp_required, options); |
| 318 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 321 | VideoChannel* ChannelManager::CreateVideoChannel( |
| 322 | webrtc::Call* call, |
| 323 | const cricket::MediaConfig& media_config, |
| 324 | webrtc::RtpTransportInternal* rtp_transport, |
| 325 | rtc::Thread* signaling_thread, |
| 326 | const std::string& content_name, |
| 327 | bool srtp_required, |
| 328 | const VideoOptions& options) { |
| 329 | if (!worker_thread_->IsCurrent()) { |
| 330 | return worker_thread_->Invoke<VideoChannel*>(RTC_FROM_HERE, [&] { |
| 331 | return CreateVideoChannel(call, media_config, rtp_transport, |
| 332 | signaling_thread, content_name, srtp_required, |
| 333 | options); |
| 334 | }); |
| 335 | } |
| 336 | |
| 337 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 338 | RTC_DCHECK(initialized_); |
| 339 | RTC_DCHECK(call); |
| 340 | if (!media_engine_) { |
| 341 | return nullptr; |
| 342 | } |
| 343 | |
| 344 | VideoMediaChannel* media_channel = |
| 345 | media_engine_->CreateVideoChannel(call, media_config, options); |
| 346 | if (!media_channel) { |
| 347 | return nullptr; |
| 348 | } |
| 349 | |
| 350 | auto video_channel = rtc::MakeUnique<VideoChannel>( |
| 351 | worker_thread_, network_thread_, signaling_thread, |
| 352 | rtc::WrapUnique(media_channel), content_name, |
| 353 | rtp_transport->rtcp_packet_transport() == nullptr, srtp_required); |
| 354 | video_channel->Init_w(rtp_transport); |
| 355 | |
| 356 | VideoChannel* video_channel_ptr = video_channel.get(); |
| 357 | video_channels_.push_back(std::move(video_channel)); |
| 358 | return video_channel_ptr; |
| 359 | } |
| 360 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 361 | VideoChannel* ChannelManager::CreateVideoChannel_w( |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 362 | webrtc::Call* call, |
| 363 | const cricket::MediaConfig& media_config, |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 364 | DtlsTransportInternal* rtp_dtls_transport, |
| 365 | DtlsTransportInternal* rtcp_dtls_transport, |
| 366 | rtc::PacketTransportInternal* rtp_packet_transport, |
| 367 | rtc::PacketTransportInternal* rtcp_packet_transport, |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 368 | rtc::Thread* signaling_thread, |
buildbot@webrtc.org | 1ecbe45 | 2014-10-14 20:29:28 +0000 | [diff] [blame] | 369 | const std::string& content_name, |
deadbeef | 7af91dd | 2016-12-13 11:29:11 -0800 | [diff] [blame] | 370 | bool srtp_required, |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 371 | const VideoOptions& options) { |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 372 | RTC_DCHECK_RUN_ON(worker_thread_); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 373 | RTC_DCHECK(initialized_); |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 374 | RTC_DCHECK(call); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 375 | if (!media_engine_) { |
| 376 | return nullptr; |
| 377 | } |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 378 | |
nisse | 51542be | 2016-02-12 02:27:06 -0800 | [diff] [blame] | 379 | VideoMediaChannel* media_channel = media_engine_->CreateVideoChannel( |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 380 | call, media_config, options); |
Steve Anton | 774115c | 2017-08-30 10:48:46 -0700 | [diff] [blame] | 381 | if (!media_channel) { |
| 382 | return nullptr; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 383 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 384 | |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 385 | auto video_channel = rtc::MakeUnique<VideoChannel>( |
Steve Anton | 8699a32 | 2017-11-06 15:53:33 -0800 | [diff] [blame] | 386 | worker_thread_, network_thread_, signaling_thread, |
| 387 | rtc::WrapUnique(media_channel), content_name, |
| 388 | rtcp_packet_transport == nullptr, srtp_required); |
| 389 | video_channel->Init_w(rtp_dtls_transport, rtcp_dtls_transport, |
| 390 | rtp_packet_transport, rtcp_packet_transport); |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 391 | |
Steve Anton | 774115c | 2017-08-30 10:48:46 -0700 | [diff] [blame] | 392 | VideoChannel* video_channel_ptr = video_channel.get(); |
| 393 | video_channels_.push_back(std::move(video_channel)); |
| 394 | return video_channel_ptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | void ChannelManager::DestroyVideoChannel(VideoChannel* video_channel) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 398 | TRACE_EVENT0("webrtc", "ChannelManager::DestroyVideoChannel"); |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 399 | if (!video_channel) { |
| 400 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 401 | } |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 402 | if (!worker_thread_->IsCurrent()) { |
| 403 | worker_thread_->Invoke<void>(RTC_FROM_HERE, |
| 404 | [&] { DestroyVideoChannel(video_channel); }); |
| 405 | return; |
| 406 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 407 | |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 408 | RTC_DCHECK(initialized_); |
Steve Anton | 774115c | 2017-08-30 10:48:46 -0700 | [diff] [blame] | 409 | |
| 410 | auto it = std::find_if(video_channels_.begin(), video_channels_.end(), |
| 411 | [&](const std::unique_ptr<VideoChannel>& p) { |
| 412 | return p.get() == video_channel; |
| 413 | }); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 414 | RTC_DCHECK(it != video_channels_.end()); |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 415 | if (it == video_channels_.end()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 416 | return; |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 417 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 418 | |
| 419 | video_channels_.erase(it); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 420 | } |
| 421 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 422 | RtpDataChannel* ChannelManager::CreateRtpDataChannel( |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 423 | const cricket::MediaConfig& media_config, |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 424 | DtlsTransportInternal* rtp_transport, |
| 425 | DtlsTransportInternal* rtcp_transport, |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 426 | rtc::Thread* signaling_thread, |
zhihuang | ebbe4f2 | 2016-12-06 10:45:42 -0800 | [diff] [blame] | 427 | const std::string& content_name, |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 428 | bool srtp_required) { |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 429 | if (!worker_thread_->IsCurrent()) { |
| 430 | return worker_thread_->Invoke<RtpDataChannel*>(RTC_FROM_HERE, [&] { |
| 431 | return CreateRtpDataChannel(media_config, rtp_transport, rtcp_transport, |
| 432 | signaling_thread, content_name, |
| 433 | srtp_required); |
| 434 | }); |
| 435 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 436 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 437 | // This is ok to alloc from a thread other than the worker thread. |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 438 | RTC_DCHECK(initialized_); |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 439 | DataMediaChannel* media_channel = data_engine_->CreateChannel(media_config); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 440 | if (!media_channel) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 441 | RTC_LOG(LS_WARNING) << "Failed to create RTP data channel."; |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 442 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 445 | auto data_channel = rtc::MakeUnique<RtpDataChannel>( |
Steve Anton | 8699a32 | 2017-11-06 15:53:33 -0800 | [diff] [blame] | 446 | worker_thread_, network_thread_, signaling_thread, |
| 447 | rtc::WrapUnique(media_channel), content_name, rtcp_transport == nullptr, |
| 448 | srtp_required); |
| 449 | data_channel->Init_w(rtp_transport, rtcp_transport, rtp_transport, |
| 450 | rtcp_transport); |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 451 | |
Steve Anton | 774115c | 2017-08-30 10:48:46 -0700 | [diff] [blame] | 452 | RtpDataChannel* data_channel_ptr = data_channel.get(); |
| 453 | data_channels_.push_back(std::move(data_channel)); |
| 454 | return data_channel_ptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 457 | RtpDataChannel* ChannelManager::CreateRtpDataChannel( |
| 458 | const cricket::MediaConfig& media_config, |
| 459 | webrtc::RtpTransportInternal* rtp_transport, |
| 460 | rtc::Thread* signaling_thread, |
| 461 | const std::string& content_name, |
| 462 | bool srtp_required) { |
| 463 | if (!worker_thread_->IsCurrent()) { |
| 464 | return worker_thread_->Invoke<RtpDataChannel*>(RTC_FROM_HERE, [&] { |
| 465 | return CreateRtpDataChannel(media_config, rtp_transport, signaling_thread, |
| 466 | content_name, srtp_required); |
| 467 | }); |
| 468 | } |
| 469 | |
| 470 | // This is ok to alloc from a thread other than the worker thread. |
| 471 | RTC_DCHECK(initialized_); |
| 472 | DataMediaChannel* media_channel = data_engine_->CreateChannel(media_config); |
| 473 | if (!media_channel) { |
| 474 | RTC_LOG(LS_WARNING) << "Failed to create RTP data channel."; |
| 475 | return nullptr; |
| 476 | } |
| 477 | |
| 478 | auto data_channel = rtc::MakeUnique<RtpDataChannel>( |
| 479 | worker_thread_, network_thread_, signaling_thread, |
| 480 | rtc::WrapUnique(media_channel), content_name, |
| 481 | rtp_transport->rtcp_packet_transport() == nullptr, srtp_required); |
| 482 | data_channel->Init_w(rtp_transport); |
| 483 | |
| 484 | RtpDataChannel* data_channel_ptr = data_channel.get(); |
| 485 | data_channels_.push_back(std::move(data_channel)); |
| 486 | return data_channel_ptr; |
| 487 | } |
| 488 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 489 | void ChannelManager::DestroyRtpDataChannel(RtpDataChannel* data_channel) { |
| 490 | TRACE_EVENT0("webrtc", "ChannelManager::DestroyRtpDataChannel"); |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 491 | if (!data_channel) { |
| 492 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 493 | } |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 494 | if (!worker_thread_->IsCurrent()) { |
| 495 | worker_thread_->Invoke<void>( |
| 496 | RTC_FROM_HERE, [&] { return DestroyRtpDataChannel(data_channel); }); |
| 497 | return; |
| 498 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 499 | |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 500 | RTC_DCHECK(initialized_); |
Steve Anton | 774115c | 2017-08-30 10:48:46 -0700 | [diff] [blame] | 501 | |
| 502 | auto it = std::find_if(data_channels_.begin(), data_channels_.end(), |
| 503 | [&](const std::unique_ptr<RtpDataChannel>& p) { |
| 504 | return p.get() == data_channel; |
| 505 | }); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 506 | RTC_DCHECK(it != data_channels_.end()); |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 507 | if (it == data_channels_.end()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 508 | return; |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 509 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 510 | |
| 511 | data_channels_.erase(it); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 512 | } |
| 513 | |
ivoc | d66b44d | 2016-01-15 03:06:36 -0800 | [diff] [blame] | 514 | bool ChannelManager::StartAecDump(rtc::PlatformFile file, |
| 515 | int64_t max_size_bytes) { |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 516 | return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
| 517 | return media_engine_->StartAecDump(file, max_size_bytes); |
| 518 | }); |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 519 | } |
| 520 | |
ivoc | 797ef12 | 2015-10-22 03:25:41 -0700 | [diff] [blame] | 521 | void ChannelManager::StopAecDump() { |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 522 | worker_thread_->Invoke<void>(RTC_FROM_HERE, |
| 523 | [&] { media_engine_->StopAecDump(); }); |
ivoc | 797ef12 | 2015-10-22 03:25:41 -0700 | [diff] [blame] | 524 | } |
| 525 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 526 | } // namespace cricket |