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