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 | |
kjellander@webrtc.org | 9b8df25 | 2016-02-12 06:47:59 +0100 | [diff] [blame] | 11 | #include "webrtc/pc/channelmanager.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
| 13 | #ifdef HAVE_CONFIG_H |
| 14 | #include <config.h> |
| 15 | #endif |
| 16 | |
| 17 | #include <algorithm> |
| 18 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 19 | #include "webrtc/api/mediacontroller.h" |
| 20 | #include "webrtc/base/bind.h" |
| 21 | #include "webrtc/base/common.h" |
| 22 | #include "webrtc/base/logging.h" |
| 23 | #include "webrtc/base/sigslotrepeater.h" |
| 24 | #include "webrtc/base/stringencode.h" |
| 25 | #include "webrtc/base/stringutils.h" |
| 26 | #include "webrtc/base/trace_event.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 27 | #include "webrtc/media/base/capturemanager.h" |
| 28 | #include "webrtc/media/base/device.h" |
| 29 | #include "webrtc/media/base/hybriddataengine.h" |
| 30 | #include "webrtc/media/base/rtpdataengine.h" |
| 31 | #include "webrtc/media/base/videocapturer.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 32 | #ifdef HAVE_SCTP |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 33 | #include "webrtc/media/sctp/sctpdataengine.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 34 | #endif |
kjellander@webrtc.org | 9b8df25 | 2016-02-12 06:47:59 +0100 | [diff] [blame] | 35 | #include "webrtc/pc/srtpfilter.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 36 | |
| 37 | namespace cricket { |
| 38 | |
| 39 | enum { |
| 40 | MSG_VIDEOCAPTURESTATE = 1, |
| 41 | }; |
| 42 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 43 | using rtc::Bind; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 44 | |
| 45 | static const int kNotSetOutputVolume = -1; |
| 46 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 47 | struct CaptureStateParams : public rtc::MessageData { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 48 | CaptureStateParams(cricket::VideoCapturer* c, cricket::CaptureState s) |
| 49 | : capturer(c), |
| 50 | state(s) {} |
| 51 | cricket::VideoCapturer* capturer; |
| 52 | cricket::CaptureState state; |
| 53 | }; |
| 54 | |
| 55 | static DataEngineInterface* ConstructDataEngine() { |
| 56 | #ifdef HAVE_SCTP |
| 57 | return new HybridDataEngine(new RtpDataEngine(), new SctpDataEngine()); |
| 58 | #else |
| 59 | return new RtpDataEngine(); |
| 60 | #endif |
| 61 | } |
| 62 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 63 | ChannelManager::ChannelManager(MediaEngineInterface* me, |
| 64 | DataEngineInterface* dme, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 65 | CaptureManager* cm, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 66 | rtc::Thread* worker_thread) { |
solenberg | facbbec | 2015-09-24 00:41:50 -0700 | [diff] [blame] | 67 | Construct(me, dme, cm, worker_thread); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | ChannelManager::ChannelManager(MediaEngineInterface* me, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 71 | rtc::Thread* worker_thread) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 72 | Construct(me, |
| 73 | ConstructDataEngine(), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 74 | new CaptureManager(), |
| 75 | worker_thread); |
| 76 | } |
| 77 | |
| 78 | void ChannelManager::Construct(MediaEngineInterface* me, |
| 79 | DataEngineInterface* dme, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 80 | CaptureManager* cm, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 81 | rtc::Thread* worker_thread) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 82 | media_engine_.reset(me); |
| 83 | data_media_engine_.reset(dme); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 84 | capture_manager_.reset(cm); |
| 85 | initialized_ = false; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 86 | main_thread_ = rtc::Thread::Current(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 87 | worker_thread_ = worker_thread; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 88 | audio_output_volume_ = kNotSetOutputVolume; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 89 | capturing_ = false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 90 | enable_rtx_ = false; |
| 91 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 92 | capture_manager_->SignalCapturerStateChange.connect( |
| 93 | this, &ChannelManager::OnVideoCaptureStateChange); |
| 94 | } |
| 95 | |
| 96 | ChannelManager::~ChannelManager() { |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 97 | if (initialized_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 98 | Terminate(); |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 99 | // If srtp is initialized (done by the Channel) then we must call |
| 100 | // srtp_shutdown to free all crypto kernel lists. But we need to make sure |
| 101 | // shutdown always called at the end, after channels are destroyed. |
| 102 | // ChannelManager d'tor is always called last, it's safe place to call |
| 103 | // shutdown. |
| 104 | ShutdownSrtp(); |
| 105 | } |
hbos@webrtc.org | 4aef5fe | 2015-02-25 10:09:05 +0000 | [diff] [blame] | 106 | // Some deletes need to be on the worker thread for thread safe destruction, |
| 107 | // this includes the media engine and capture manager. |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 108 | worker_thread_->Invoke<void>(Bind( |
hbos@webrtc.org | 4aef5fe | 2015-02-25 10:09:05 +0000 | [diff] [blame] | 109 | &ChannelManager::DestructorDeletes_w, this)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | bool ChannelManager::SetVideoRtxEnabled(bool enable) { |
| 113 | // To be safe, this call is only allowed before initialization. Apps like |
| 114 | // Flute only have a singleton ChannelManager and we don't want this flag to |
| 115 | // be toggled between calls or when there's concurrent calls. We expect apps |
| 116 | // to enable this at startup and retain that setting for the lifetime of the |
| 117 | // app. |
| 118 | if (!initialized_) { |
| 119 | enable_rtx_ = enable; |
| 120 | return true; |
| 121 | } else { |
| 122 | LOG(LS_WARNING) << "Cannot toggle rtx after initialization!"; |
| 123 | return false; |
| 124 | } |
| 125 | } |
| 126 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 127 | void ChannelManager::GetSupportedAudioCodecs( |
| 128 | std::vector<AudioCodec>* codecs) const { |
| 129 | codecs->clear(); |
| 130 | |
| 131 | for (std::vector<AudioCodec>::const_iterator it = |
| 132 | media_engine_->audio_codecs().begin(); |
| 133 | it != media_engine_->audio_codecs().end(); ++it) { |
| 134 | codecs->push_back(*it); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | void ChannelManager::GetSupportedAudioRtpHeaderExtensions( |
| 139 | RtpHeaderExtensions* ext) const { |
Stefan Holmer | 9d69c3f | 2015-12-07 10:45:43 +0100 | [diff] [blame] | 140 | *ext = media_engine_->GetAudioCapabilities().header_extensions; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void ChannelManager::GetSupportedVideoCodecs( |
| 144 | std::vector<VideoCodec>* codecs) const { |
| 145 | codecs->clear(); |
| 146 | |
| 147 | std::vector<VideoCodec>::const_iterator it; |
| 148 | for (it = media_engine_->video_codecs().begin(); |
| 149 | it != media_engine_->video_codecs().end(); ++it) { |
| 150 | if (!enable_rtx_ && _stricmp(kRtxCodecName, it->name.c_str()) == 0) { |
| 151 | continue; |
| 152 | } |
| 153 | codecs->push_back(*it); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void ChannelManager::GetSupportedVideoRtpHeaderExtensions( |
| 158 | RtpHeaderExtensions* ext) const { |
Stefan Holmer | 9d69c3f | 2015-12-07 10:45:43 +0100 | [diff] [blame] | 159 | *ext = media_engine_->GetVideoCapabilities().header_extensions; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void ChannelManager::GetSupportedDataCodecs( |
| 163 | std::vector<DataCodec>* codecs) const { |
| 164 | *codecs = data_media_engine_->data_codecs(); |
| 165 | } |
| 166 | |
| 167 | bool ChannelManager::Init() { |
| 168 | ASSERT(!initialized_); |
| 169 | if (initialized_) { |
| 170 | return false; |
| 171 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 172 | ASSERT(worker_thread_ != NULL); |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 173 | if (!worker_thread_) { |
| 174 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 175 | } |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 176 | if (worker_thread_ != rtc::Thread::Current()) { |
| 177 | // Do not allow invoking calls to other threads on the worker thread. |
| 178 | worker_thread_->Invoke<bool>(rtc::Bind( |
| 179 | &rtc::Thread::SetAllowBlockingCalls, worker_thread_, false)); |
| 180 | } |
| 181 | |
| 182 | initialized_ = worker_thread_->Invoke<bool>(Bind( |
| 183 | &ChannelManager::InitMediaEngine_w, this)); |
| 184 | ASSERT(initialized_); |
| 185 | if (!initialized_) { |
| 186 | return false; |
| 187 | } |
| 188 | |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 189 | // If audio_output_volume_ has been set via SetOutputVolume(), set the |
| 190 | // audio output volume of the engine. |
| 191 | if (kNotSetOutputVolume != audio_output_volume_ && |
| 192 | !SetOutputVolume(audio_output_volume_)) { |
| 193 | LOG(LS_WARNING) << "Failed to SetOutputVolume to " |
| 194 | << audio_output_volume_; |
| 195 | } |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 196 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 197 | return initialized_; |
| 198 | } |
| 199 | |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 200 | bool ChannelManager::InitMediaEngine_w() { |
| 201 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 202 | return (media_engine_->Init(worker_thread_)); |
| 203 | } |
| 204 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 205 | void ChannelManager::Terminate() { |
| 206 | ASSERT(initialized_); |
| 207 | if (!initialized_) { |
| 208 | return; |
| 209 | } |
| 210 | worker_thread_->Invoke<void>(Bind(&ChannelManager::Terminate_w, this)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 211 | initialized_ = false; |
| 212 | } |
| 213 | |
hbos@webrtc.org | 4aef5fe | 2015-02-25 10:09:05 +0000 | [diff] [blame] | 214 | void ChannelManager::DestructorDeletes_w() { |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 215 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 216 | media_engine_.reset(NULL); |
hbos@webrtc.org | 4aef5fe | 2015-02-25 10:09:05 +0000 | [diff] [blame] | 217 | capture_manager_.reset(NULL); |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 218 | } |
| 219 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 220 | void ChannelManager::Terminate_w() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 221 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 222 | // Need to destroy the voice/video channels |
| 223 | while (!video_channels_.empty()) { |
| 224 | DestroyVideoChannel_w(video_channels_.back()); |
| 225 | } |
| 226 | while (!voice_channels_.empty()) { |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 227 | DestroyVoiceChannel_w(voice_channels_.back()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 228 | } |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 229 | media_engine_->Terminate(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | VoiceChannel* ChannelManager::CreateVoiceChannel( |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 233 | webrtc::MediaControllerInterface* media_controller, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 234 | TransportController* transport_controller, |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 235 | const std::string& content_name, |
| 236 | bool rtcp, |
| 237 | const AudioOptions& options) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 238 | return worker_thread_->Invoke<VoiceChannel*>( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 239 | Bind(&ChannelManager::CreateVoiceChannel_w, this, media_controller, |
| 240 | transport_controller, content_name, rtcp, options)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | VoiceChannel* ChannelManager::CreateVoiceChannel_w( |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 244 | webrtc::MediaControllerInterface* media_controller, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 245 | TransportController* transport_controller, |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 246 | const std::string& content_name, |
| 247 | bool rtcp, |
| 248 | const AudioOptions& options) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 249 | ASSERT(initialized_); |
Fredrik Solenberg | 4b60c73 | 2015-05-07 14:07:48 +0200 | [diff] [blame] | 250 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 251 | ASSERT(nullptr != media_controller); |
nisse | 51542be | 2016-02-12 02:27:06 -0800 | [diff] [blame] | 252 | VoiceMediaChannel* media_channel = media_engine_->CreateChannel( |
| 253 | media_controller->call_w(), media_controller->config(), options); |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 254 | if (!media_channel) |
| 255 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 256 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 257 | VoiceChannel* voice_channel = |
| 258 | new VoiceChannel(worker_thread_, media_engine_.get(), media_channel, |
| 259 | transport_controller, content_name, rtcp); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 260 | if (!voice_channel->Init()) { |
| 261 | delete voice_channel; |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 262 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 263 | } |
| 264 | voice_channels_.push_back(voice_channel); |
| 265 | return voice_channel; |
| 266 | } |
| 267 | |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 268 | void ChannelManager::DestroyVoiceChannel(VoiceChannel* voice_channel) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 269 | TRACE_EVENT0("webrtc", "ChannelManager::DestroyVoiceChannel"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 270 | if (voice_channel) { |
| 271 | worker_thread_->Invoke<void>( |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 272 | Bind(&ChannelManager::DestroyVoiceChannel_w, this, voice_channel)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 276 | void ChannelManager::DestroyVoiceChannel_w(VoiceChannel* voice_channel) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 277 | TRACE_EVENT0("webrtc", "ChannelManager::DestroyVoiceChannel_w"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 278 | // Destroy voice channel. |
| 279 | ASSERT(initialized_); |
Fredrik Solenberg | 4b60c73 | 2015-05-07 14:07:48 +0200 | [diff] [blame] | 280 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 281 | VoiceChannels::iterator it = std::find(voice_channels_.begin(), |
| 282 | voice_channels_.end(), voice_channel); |
| 283 | ASSERT(it != voice_channels_.end()); |
| 284 | if (it == voice_channels_.end()) |
| 285 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 286 | voice_channels_.erase(it); |
| 287 | delete voice_channel; |
| 288 | } |
| 289 | |
| 290 | VideoChannel* ChannelManager::CreateVideoChannel( |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 291 | webrtc::MediaControllerInterface* media_controller, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 292 | TransportController* transport_controller, |
buildbot@webrtc.org | 1ecbe45 | 2014-10-14 20:29:28 +0000 | [diff] [blame] | 293 | const std::string& content_name, |
| 294 | bool rtcp, |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 295 | const VideoOptions& options) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 296 | return worker_thread_->Invoke<VideoChannel*>( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 297 | Bind(&ChannelManager::CreateVideoChannel_w, this, media_controller, |
| 298 | transport_controller, content_name, rtcp, options)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | VideoChannel* ChannelManager::CreateVideoChannel_w( |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 302 | webrtc::MediaControllerInterface* media_controller, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 303 | TransportController* transport_controller, |
buildbot@webrtc.org | 1ecbe45 | 2014-10-14 20:29:28 +0000 | [diff] [blame] | 304 | const std::string& content_name, |
| 305 | bool rtcp, |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 306 | const VideoOptions& options) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 307 | ASSERT(initialized_); |
Fredrik Solenberg | 4b60c73 | 2015-05-07 14:07:48 +0200 | [diff] [blame] | 308 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 309 | ASSERT(nullptr != media_controller); |
nisse | 51542be | 2016-02-12 02:27:06 -0800 | [diff] [blame] | 310 | VideoMediaChannel* media_channel = media_engine_->CreateVideoChannel( |
| 311 | media_controller->call_w(), media_controller->config(), options); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 312 | if (media_channel == NULL) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 313 | return NULL; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 314 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 315 | |
| 316 | VideoChannel* video_channel = new VideoChannel( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 317 | worker_thread_, media_channel, transport_controller, content_name, rtcp); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 318 | if (!video_channel->Init()) { |
| 319 | delete video_channel; |
| 320 | return NULL; |
| 321 | } |
| 322 | video_channels_.push_back(video_channel); |
| 323 | return video_channel; |
| 324 | } |
| 325 | |
| 326 | void ChannelManager::DestroyVideoChannel(VideoChannel* video_channel) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 327 | TRACE_EVENT0("webrtc", "ChannelManager::DestroyVideoChannel"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 328 | if (video_channel) { |
| 329 | worker_thread_->Invoke<void>( |
| 330 | Bind(&ChannelManager::DestroyVideoChannel_w, this, video_channel)); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | void ChannelManager::DestroyVideoChannel_w(VideoChannel* video_channel) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 335 | TRACE_EVENT0("webrtc", "ChannelManager::DestroyVideoChannel_w"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 336 | // Destroy video channel. |
| 337 | ASSERT(initialized_); |
Fredrik Solenberg | 4b60c73 | 2015-05-07 14:07:48 +0200 | [diff] [blame] | 338 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 339 | VideoChannels::iterator it = std::find(video_channels_.begin(), |
| 340 | video_channels_.end(), video_channel); |
| 341 | ASSERT(it != video_channels_.end()); |
| 342 | if (it == video_channels_.end()) |
| 343 | return; |
| 344 | |
| 345 | video_channels_.erase(it); |
| 346 | delete video_channel; |
| 347 | } |
| 348 | |
| 349 | DataChannel* ChannelManager::CreateDataChannel( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 350 | TransportController* transport_controller, |
| 351 | const std::string& content_name, |
| 352 | bool rtcp, |
| 353 | DataChannelType channel_type) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 354 | return worker_thread_->Invoke<DataChannel*>( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 355 | Bind(&ChannelManager::CreateDataChannel_w, this, transport_controller, |
| 356 | content_name, rtcp, channel_type)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | DataChannel* ChannelManager::CreateDataChannel_w( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 360 | TransportController* transport_controller, |
| 361 | const std::string& content_name, |
| 362 | bool rtcp, |
| 363 | DataChannelType data_channel_type) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 364 | // This is ok to alloc from a thread other than the worker thread. |
| 365 | ASSERT(initialized_); |
| 366 | DataMediaChannel* media_channel = data_media_engine_->CreateChannel( |
| 367 | data_channel_type); |
| 368 | if (!media_channel) { |
| 369 | LOG(LS_WARNING) << "Failed to create data channel of type " |
| 370 | << data_channel_type; |
| 371 | return NULL; |
| 372 | } |
| 373 | |
| 374 | DataChannel* data_channel = new DataChannel( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 375 | worker_thread_, media_channel, transport_controller, content_name, rtcp); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 376 | if (!data_channel->Init()) { |
| 377 | LOG(LS_WARNING) << "Failed to init data channel."; |
| 378 | delete data_channel; |
| 379 | return NULL; |
| 380 | } |
| 381 | data_channels_.push_back(data_channel); |
| 382 | return data_channel; |
| 383 | } |
| 384 | |
| 385 | void ChannelManager::DestroyDataChannel(DataChannel* data_channel) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 386 | TRACE_EVENT0("webrtc", "ChannelManager::DestroyDataChannel"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 387 | if (data_channel) { |
| 388 | worker_thread_->Invoke<void>( |
| 389 | Bind(&ChannelManager::DestroyDataChannel_w, this, data_channel)); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | void ChannelManager::DestroyDataChannel_w(DataChannel* data_channel) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 394 | TRACE_EVENT0("webrtc", "ChannelManager::DestroyDataChannel_w"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 395 | // Destroy data channel. |
| 396 | ASSERT(initialized_); |
| 397 | DataChannels::iterator it = std::find(data_channels_.begin(), |
| 398 | data_channels_.end(), data_channel); |
| 399 | ASSERT(it != data_channels_.end()); |
| 400 | if (it == data_channels_.end()) |
| 401 | return; |
| 402 | |
| 403 | data_channels_.erase(it); |
| 404 | delete data_channel; |
| 405 | } |
| 406 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 407 | bool ChannelManager::GetOutputVolume(int* level) { |
| 408 | if (!initialized_) { |
| 409 | return false; |
| 410 | } |
| 411 | return worker_thread_->Invoke<bool>( |
| 412 | Bind(&MediaEngineInterface::GetOutputVolume, media_engine_.get(), level)); |
| 413 | } |
| 414 | |
| 415 | bool ChannelManager::SetOutputVolume(int level) { |
| 416 | bool ret = level >= 0 && level <= 255; |
| 417 | if (initialized_) { |
| 418 | ret &= worker_thread_->Invoke<bool>( |
| 419 | Bind(&MediaEngineInterface::SetOutputVolume, |
| 420 | media_engine_.get(), level)); |
| 421 | } |
| 422 | |
| 423 | if (ret) { |
| 424 | audio_output_volume_ = level; |
| 425 | } |
| 426 | |
| 427 | return ret; |
| 428 | } |
| 429 | |
hbos@webrtc.org | 1e64263 | 2015-02-25 09:49:41 +0000 | [diff] [blame] | 430 | std::vector<cricket::VideoFormat> ChannelManager::GetSupportedFormats( |
| 431 | VideoCapturer* capturer) const { |
| 432 | ASSERT(capturer != NULL); |
| 433 | std::vector<VideoFormat> formats; |
| 434 | worker_thread_->Invoke<void>(rtc::Bind(&ChannelManager::GetSupportedFormats_w, |
| 435 | this, capturer, &formats)); |
| 436 | return formats; |
| 437 | } |
| 438 | |
| 439 | void ChannelManager::GetSupportedFormats_w( |
| 440 | VideoCapturer* capturer, |
| 441 | std::vector<cricket::VideoFormat>* out_formats) const { |
| 442 | const std::vector<VideoFormat>* formats = capturer->GetSupportedFormats(); |
| 443 | if (formats != NULL) |
| 444 | *out_formats = *formats; |
| 445 | } |
| 446 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 447 | // The following are done in the new "CaptureManager" style that |
| 448 | // all local video capturers, processors, and managers should move |
| 449 | // to. |
| 450 | // TODO(pthatcher): Add more of the CaptureManager interface. |
| 451 | bool ChannelManager::StartVideoCapture( |
| 452 | VideoCapturer* capturer, const VideoFormat& video_format) { |
| 453 | return initialized_ && worker_thread_->Invoke<bool>( |
| 454 | Bind(&CaptureManager::StartVideoCapture, |
| 455 | capture_manager_.get(), capturer, video_format)); |
| 456 | } |
| 457 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 458 | bool ChannelManager::StopVideoCapture( |
| 459 | VideoCapturer* capturer, const VideoFormat& video_format) { |
| 460 | return initialized_ && worker_thread_->Invoke<bool>( |
| 461 | Bind(&CaptureManager::StopVideoCapture, |
| 462 | capture_manager_.get(), capturer, video_format)); |
| 463 | } |
| 464 | |
nisse | e73afba | 2016-01-28 04:47:08 -0800 | [diff] [blame] | 465 | void ChannelManager::AddVideoSink( |
| 466 | VideoCapturer* capturer, rtc::VideoSinkInterface<VideoFrame>* sink) { |
| 467 | if (initialized_) |
| 468 | worker_thread_->Invoke<void>( |
| 469 | Bind(&CaptureManager::AddVideoSink, |
| 470 | capture_manager_.get(), capturer, sink)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 471 | } |
| 472 | |
nisse | e73afba | 2016-01-28 04:47:08 -0800 | [diff] [blame] | 473 | void ChannelManager::RemoveVideoSink( |
| 474 | VideoCapturer* capturer, rtc::VideoSinkInterface<VideoFrame>* sink) { |
| 475 | if (initialized_) |
| 476 | worker_thread_->Invoke<void>( |
| 477 | Bind(&CaptureManager::RemoveVideoSink, |
| 478 | capture_manager_.get(), capturer, sink)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | bool ChannelManager::IsScreencastRunning() const { |
| 482 | return initialized_ && worker_thread_->Invoke<bool>( |
| 483 | Bind(&ChannelManager::IsScreencastRunning_w, this)); |
| 484 | } |
| 485 | |
| 486 | bool ChannelManager::IsScreencastRunning_w() const { |
| 487 | VideoChannels::const_iterator it = video_channels_.begin(); |
| 488 | for ( ; it != video_channels_.end(); ++it) { |
| 489 | if ((*it) && (*it)->IsScreencasting()) { |
| 490 | return true; |
| 491 | } |
| 492 | } |
| 493 | return false; |
| 494 | } |
| 495 | |
| 496 | void ChannelManager::OnVideoCaptureStateChange(VideoCapturer* capturer, |
| 497 | CaptureState result) { |
| 498 | // TODO(whyuan): Check capturer and signal failure only for camera video, not |
| 499 | // screencast. |
| 500 | capturing_ = result == CS_RUNNING; |
| 501 | main_thread_->Post(this, MSG_VIDEOCAPTURESTATE, |
| 502 | new CaptureStateParams(capturer, result)); |
| 503 | } |
| 504 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 505 | void ChannelManager::OnMessage(rtc::Message* message) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 506 | switch (message->message_id) { |
| 507 | case MSG_VIDEOCAPTURESTATE: { |
| 508 | CaptureStateParams* data = |
| 509 | static_cast<CaptureStateParams*>(message->pdata); |
| 510 | SignalVideoCaptureStateChange(data->capturer, data->state); |
| 511 | delete data; |
| 512 | break; |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | |
ivoc | d66b44d | 2016-01-15 03:06:36 -0800 | [diff] [blame] | 517 | bool ChannelManager::StartAecDump(rtc::PlatformFile file, |
| 518 | int64_t max_size_bytes) { |
| 519 | return worker_thread_->Invoke<bool>(Bind(&MediaEngineInterface::StartAecDump, |
| 520 | media_engine_.get(), file, |
| 521 | max_size_bytes)); |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 522 | } |
| 523 | |
ivoc | 797ef12 | 2015-10-22 03:25:41 -0700 | [diff] [blame] | 524 | void ChannelManager::StopAecDump() { |
| 525 | worker_thread_->Invoke<void>( |
| 526 | Bind(&MediaEngineInterface::StopAecDump, media_engine_.get())); |
| 527 | } |
| 528 | |
ivoc | 112a3d8 | 2015-10-16 02:22:18 -0700 | [diff] [blame] | 529 | bool ChannelManager::StartRtcEventLog(rtc::PlatformFile file) { |
| 530 | return worker_thread_->Invoke<bool>( |
| 531 | Bind(&MediaEngineInterface::StartRtcEventLog, media_engine_.get(), file)); |
| 532 | } |
| 533 | |
| 534 | void ChannelManager::StopRtcEventLog() { |
| 535 | worker_thread_->Invoke<void>( |
| 536 | Bind(&MediaEngineInterface::StopRtcEventLog, media_engine_.get())); |
| 537 | } |
| 538 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 539 | } // namespace cricket |