blob: 201a031cc3e47f7546ecb607f54634a4f95e262d [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
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 Solenberg709ed672015-09-15 12:26:33 +020036#include "talk/app/webrtc/mediacontroller.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037#include "talk/media/base/capturemanager.h"
38#include "talk/media/base/hybriddataengine.h"
39#include "talk/media/base/rtpdataengine.h"
40#include "talk/media/base/videocapturer.h"
henrike@webrtc.org723d6832013-07-12 16:04:50 +000041#include "talk/media/devices/devicemanager.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042#ifdef HAVE_SCTP
43#include "talk/media/sctp/sctpdataengine.h"
44#endif
wu@webrtc.org9dba5252013-08-05 20:36:57 +000045#include "talk/session/media/srtpfilter.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000046#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"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052
53namespace cricket {
54
55enum {
56 MSG_VIDEOCAPTURESTATE = 1,
57};
58
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000059using rtc::Bind;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060
61static const int kNotSetOutputVolume = -1;
62
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000063struct CaptureStateParams : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064 CaptureStateParams(cricket::VideoCapturer* c, cricket::CaptureState s)
65 : capturer(c),
66 state(s) {}
67 cricket::VideoCapturer* capturer;
68 cricket::CaptureState state;
69};
70
71static DataEngineInterface* ConstructDataEngine() {
72#ifdef HAVE_SCTP
73 return new HybridDataEngine(new RtpDataEngine(), new SctpDataEngine());
74#else
75 return new RtpDataEngine();
76#endif
77}
78
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079ChannelManager::ChannelManager(MediaEngineInterface* me,
80 DataEngineInterface* dme,
81 DeviceManagerInterface* dm,
82 CaptureManager* cm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000083 rtc::Thread* worker_thread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084 Construct(me, dme, dm, cm, worker_thread);
85}
86
87ChannelManager::ChannelManager(MediaEngineInterface* me,
88 DeviceManagerInterface* dm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000089 rtc::Thread* worker_thread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 Construct(me,
91 ConstructDataEngine(),
92 dm,
93 new CaptureManager(),
94 worker_thread);
95}
96
97void ChannelManager::Construct(MediaEngineInterface* me,
98 DataEngineInterface* dme,
99 DeviceManagerInterface* dm,
100 CaptureManager* cm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000101 rtc::Thread* worker_thread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 media_engine_.reset(me);
103 data_media_engine_.reset(dme);
104 device_manager_.reset(dm);
105 capture_manager_.reset(cm);
106 initialized_ = false;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000107 main_thread_ = rtc::Thread::Current();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108 worker_thread_ = worker_thread;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000109 // Get the default audio options from the media engine.
110 audio_options_ = media_engine_->GetAudioOptions();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111 audio_in_device_ = DeviceManagerInterface::kDefaultDeviceName;
112 audio_out_device_ = DeviceManagerInterface::kDefaultDeviceName;
henrike@webrtc.org0481f152014-08-19 14:56:59 +0000113 audio_delay_offset_ = kDefaultAudioDelayOffset;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114 audio_output_volume_ = kNotSetOutputVolume;
115 local_renderer_ = NULL;
116 capturing_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117 enable_rtx_ = false;
118
119 // Init the device manager immediately, and set up our default video device.
120 SignalDevicesChange.repeat(device_manager_->SignalDevicesChange);
121 device_manager_->Init();
122
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123 capture_manager_->SignalCapturerStateChange.connect(
124 this, &ChannelManager::OnVideoCaptureStateChange);
125}
126
127ChannelManager::~ChannelManager() {
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000128 if (initialized_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 Terminate();
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000130 // If srtp is initialized (done by the Channel) then we must call
131 // srtp_shutdown to free all crypto kernel lists. But we need to make sure
132 // shutdown always called at the end, after channels are destroyed.
133 // ChannelManager d'tor is always called last, it's safe place to call
134 // shutdown.
135 ShutdownSrtp();
136 }
hbos@webrtc.org4aef5fe2015-02-25 10:09:05 +0000137 // Some deletes need to be on the worker thread for thread safe destruction,
138 // this includes the media engine and capture manager.
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000139 worker_thread_->Invoke<void>(Bind(
hbos@webrtc.org4aef5fe2015-02-25 10:09:05 +0000140 &ChannelManager::DestructorDeletes_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141}
142
143bool ChannelManager::SetVideoRtxEnabled(bool enable) {
144 // To be safe, this call is only allowed before initialization. Apps like
145 // Flute only have a singleton ChannelManager and we don't want this flag to
146 // be toggled between calls or when there's concurrent calls. We expect apps
147 // to enable this at startup and retain that setting for the lifetime of the
148 // app.
149 if (!initialized_) {
150 enable_rtx_ = enable;
151 return true;
152 } else {
153 LOG(LS_WARNING) << "Cannot toggle rtx after initialization!";
154 return false;
155 }
156}
157
158int ChannelManager::GetCapabilities() {
159 return media_engine_->GetCapabilities() & device_manager_->GetCapabilities();
160}
161
162void ChannelManager::GetSupportedAudioCodecs(
163 std::vector<AudioCodec>* codecs) const {
164 codecs->clear();
165
166 for (std::vector<AudioCodec>::const_iterator it =
167 media_engine_->audio_codecs().begin();
168 it != media_engine_->audio_codecs().end(); ++it) {
169 codecs->push_back(*it);
170 }
171}
172
173void ChannelManager::GetSupportedAudioRtpHeaderExtensions(
174 RtpHeaderExtensions* ext) const {
175 *ext = media_engine_->audio_rtp_header_extensions();
176}
177
178void ChannelManager::GetSupportedVideoCodecs(
179 std::vector<VideoCodec>* codecs) const {
180 codecs->clear();
181
182 std::vector<VideoCodec>::const_iterator it;
183 for (it = media_engine_->video_codecs().begin();
184 it != media_engine_->video_codecs().end(); ++it) {
185 if (!enable_rtx_ && _stricmp(kRtxCodecName, it->name.c_str()) == 0) {
186 continue;
187 }
188 codecs->push_back(*it);
189 }
190}
191
192void ChannelManager::GetSupportedVideoRtpHeaderExtensions(
193 RtpHeaderExtensions* ext) const {
194 *ext = media_engine_->video_rtp_header_extensions();
195}
196
197void ChannelManager::GetSupportedDataCodecs(
198 std::vector<DataCodec>* codecs) const {
199 *codecs = data_media_engine_->data_codecs();
200}
201
202bool ChannelManager::Init() {
203 ASSERT(!initialized_);
204 if (initialized_) {
205 return false;
206 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207 ASSERT(worker_thread_ != NULL);
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000208 if (!worker_thread_) {
209 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210 }
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000211 if (worker_thread_ != rtc::Thread::Current()) {
212 // Do not allow invoking calls to other threads on the worker thread.
213 worker_thread_->Invoke<bool>(rtc::Bind(
214 &rtc::Thread::SetAllowBlockingCalls, worker_thread_, false));
215 }
216
217 initialized_ = worker_thread_->Invoke<bool>(Bind(
218 &ChannelManager::InitMediaEngine_w, this));
219 ASSERT(initialized_);
220 if (!initialized_) {
221 return false;
222 }
223
224 // Now that we're initialized, apply any stored preferences. A preferred
225 // device might have been unplugged. In this case, we fallback to the
226 // default device but keep the user preferences. The preferences are
227 // changed only when the Javascript FE changes them.
228 const std::string preferred_audio_in_device = audio_in_device_;
229 const std::string preferred_audio_out_device = audio_out_device_;
230 const std::string preferred_camera_device = camera_device_;
231 Device device;
232 if (!device_manager_->GetAudioInputDevice(audio_in_device_, &device)) {
233 LOG(LS_WARNING) << "The preferred microphone '" << audio_in_device_
234 << "' is unavailable. Fall back to the default.";
235 audio_in_device_ = DeviceManagerInterface::kDefaultDeviceName;
236 }
237 if (!device_manager_->GetAudioOutputDevice(audio_out_device_, &device)) {
238 LOG(LS_WARNING) << "The preferred speaker '" << audio_out_device_
239 << "' is unavailable. Fall back to the default.";
240 audio_out_device_ = DeviceManagerInterface::kDefaultDeviceName;
241 }
242 if (!device_manager_->GetVideoCaptureDevice(camera_device_, &device)) {
243 if (!camera_device_.empty()) {
244 LOG(LS_WARNING) << "The preferred camera '" << camera_device_
245 << "' is unavailable. Fall back to the default.";
246 }
247 camera_device_ = DeviceManagerInterface::kDefaultDeviceName;
248 }
249
250 if (!SetAudioOptions(audio_in_device_, audio_out_device_,
251 audio_options_, audio_delay_offset_)) {
252 LOG(LS_WARNING) << "Failed to SetAudioOptions with"
253 << " microphone: " << audio_in_device_
254 << " speaker: " << audio_out_device_
255 << " options: " << audio_options_.ToString()
256 << " delay: " << audio_delay_offset_;
257 }
258
259 // If audio_output_volume_ has been set via SetOutputVolume(), set the
260 // audio output volume of the engine.
261 if (kNotSetOutputVolume != audio_output_volume_ &&
262 !SetOutputVolume(audio_output_volume_)) {
263 LOG(LS_WARNING) << "Failed to SetOutputVolume to "
264 << audio_output_volume_;
265 }
266 if (!SetCaptureDevice(camera_device_) && !camera_device_.empty()) {
267 LOG(LS_WARNING) << "Failed to SetCaptureDevice with camera: "
268 << camera_device_;
269 }
270
271 // Restore the user preferences.
272 audio_in_device_ = preferred_audio_in_device;
273 audio_out_device_ = preferred_audio_out_device;
274 camera_device_ = preferred_camera_device;
275
276 // Now apply the default video codec that has been set earlier.
277 if (default_video_encoder_config_.max_codec.id != 0) {
278 SetDefaultVideoEncoderConfig(default_video_encoder_config_);
279 }
280
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281 return initialized_;
282}
283
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000284bool ChannelManager::InitMediaEngine_w() {
285 ASSERT(worker_thread_ == rtc::Thread::Current());
286 return (media_engine_->Init(worker_thread_));
287}
288
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000289void ChannelManager::Terminate() {
290 ASSERT(initialized_);
291 if (!initialized_) {
292 return;
293 }
294 worker_thread_->Invoke<void>(Bind(&ChannelManager::Terminate_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295 initialized_ = false;
296}
297
hbos@webrtc.org4aef5fe2015-02-25 10:09:05 +0000298void ChannelManager::DestructorDeletes_w() {
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000299 ASSERT(worker_thread_ == rtc::Thread::Current());
300 media_engine_.reset(NULL);
hbos@webrtc.org4aef5fe2015-02-25 10:09:05 +0000301 capture_manager_.reset(NULL);
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000302}
303
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000304void ChannelManager::Terminate_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000305 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000306 // Need to destroy the voice/video channels
307 while (!video_channels_.empty()) {
308 DestroyVideoChannel_w(video_channels_.back());
309 }
310 while (!voice_channels_.empty()) {
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200311 DestroyVoiceChannel_w(voice_channels_.back());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000313 if (!SetCaptureDevice_w(NULL)) {
314 LOG(LS_WARNING) << "failed to delete video capturer";
315 }
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000316 media_engine_->Terminate();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000317}
318
319VoiceChannel* ChannelManager::CreateVoiceChannel(
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200320 webrtc::MediaControllerInterface* media_controller,
deadbeefcbecd352015-09-23 11:50:27 -0700321 TransportController* transport_controller,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200322 const std::string& content_name,
323 bool rtcp,
324 const AudioOptions& options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325 return worker_thread_->Invoke<VoiceChannel*>(
deadbeefcbecd352015-09-23 11:50:27 -0700326 Bind(&ChannelManager::CreateVoiceChannel_w, this, media_controller,
327 transport_controller, content_name, rtcp, options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328}
329
330VoiceChannel* ChannelManager::CreateVoiceChannel_w(
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200331 webrtc::MediaControllerInterface* media_controller,
deadbeefcbecd352015-09-23 11:50:27 -0700332 TransportController* transport_controller,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200333 const std::string& content_name,
334 bool rtcp,
335 const AudioOptions& options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336 ASSERT(initialized_);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200337 ASSERT(worker_thread_ == rtc::Thread::Current());
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200338 ASSERT(nullptr != media_controller);
339 VoiceMediaChannel* media_channel =
340 media_engine_->CreateChannel(media_controller->call_w(), options);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200341 if (!media_channel)
342 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343
deadbeefcbecd352015-09-23 11:50:27 -0700344 VoiceChannel* voice_channel =
345 new VoiceChannel(worker_thread_, media_engine_.get(), media_channel,
346 transport_controller, content_name, rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347 if (!voice_channel->Init()) {
348 delete voice_channel;
Jelena Marusicc28a8962015-05-29 15:05:44 +0200349 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000350 }
351 voice_channels_.push_back(voice_channel);
352 return voice_channel;
353}
354
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200355void ChannelManager::DestroyVoiceChannel(VoiceChannel* voice_channel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356 if (voice_channel) {
357 worker_thread_->Invoke<void>(
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200358 Bind(&ChannelManager::DestroyVoiceChannel_w, this, voice_channel));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359 }
360}
361
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200362void ChannelManager::DestroyVoiceChannel_w(VoiceChannel* voice_channel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000363 // Destroy voice channel.
364 ASSERT(initialized_);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200365 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000366 VoiceChannels::iterator it = std::find(voice_channels_.begin(),
367 voice_channels_.end(), voice_channel);
368 ASSERT(it != voice_channels_.end());
369 if (it == voice_channels_.end())
370 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371 voice_channels_.erase(it);
372 delete voice_channel;
373}
374
375VideoChannel* ChannelManager::CreateVideoChannel(
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200376 webrtc::MediaControllerInterface* media_controller,
deadbeefcbecd352015-09-23 11:50:27 -0700377 TransportController* transport_controller,
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000378 const std::string& content_name,
379 bool rtcp,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200380 const VideoOptions& options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000381 return worker_thread_->Invoke<VideoChannel*>(
deadbeefcbecd352015-09-23 11:50:27 -0700382 Bind(&ChannelManager::CreateVideoChannel_w, this, media_controller,
383 transport_controller, content_name, rtcp, options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000384}
385
386VideoChannel* ChannelManager::CreateVideoChannel_w(
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200387 webrtc::MediaControllerInterface* media_controller,
deadbeefcbecd352015-09-23 11:50:27 -0700388 TransportController* transport_controller,
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000389 const std::string& content_name,
390 bool rtcp,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200391 const VideoOptions& options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000392 ASSERT(initialized_);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200393 ASSERT(worker_thread_ == rtc::Thread::Current());
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200394 ASSERT(nullptr != media_controller);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395 VideoMediaChannel* media_channel =
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200396 media_engine_->CreateVideoChannel(media_controller->call_w(), options);
deadbeefcbecd352015-09-23 11:50:27 -0700397 if (media_channel == NULL) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000398 return NULL;
deadbeefcbecd352015-09-23 11:50:27 -0700399 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000400
401 VideoChannel* video_channel = new VideoChannel(
deadbeefcbecd352015-09-23 11:50:27 -0700402 worker_thread_, media_channel, transport_controller, content_name, rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000403 if (!video_channel->Init()) {
404 delete video_channel;
405 return NULL;
406 }
407 video_channels_.push_back(video_channel);
408 return video_channel;
409}
410
411void ChannelManager::DestroyVideoChannel(VideoChannel* video_channel) {
412 if (video_channel) {
413 worker_thread_->Invoke<void>(
414 Bind(&ChannelManager::DestroyVideoChannel_w, this, video_channel));
415 }
416}
417
418void ChannelManager::DestroyVideoChannel_w(VideoChannel* video_channel) {
419 // Destroy video channel.
420 ASSERT(initialized_);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200421 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000422 VideoChannels::iterator it = std::find(video_channels_.begin(),
423 video_channels_.end(), video_channel);
424 ASSERT(it != video_channels_.end());
425 if (it == video_channels_.end())
426 return;
427
428 video_channels_.erase(it);
429 delete video_channel;
430}
431
432DataChannel* ChannelManager::CreateDataChannel(
deadbeefcbecd352015-09-23 11:50:27 -0700433 TransportController* transport_controller,
434 const std::string& content_name,
435 bool rtcp,
436 DataChannelType channel_type) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000437 return worker_thread_->Invoke<DataChannel*>(
deadbeefcbecd352015-09-23 11:50:27 -0700438 Bind(&ChannelManager::CreateDataChannel_w, this, transport_controller,
439 content_name, rtcp, channel_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000440}
441
442DataChannel* ChannelManager::CreateDataChannel_w(
deadbeefcbecd352015-09-23 11:50:27 -0700443 TransportController* transport_controller,
444 const std::string& content_name,
445 bool rtcp,
446 DataChannelType data_channel_type) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000447 // This is ok to alloc from a thread other than the worker thread.
448 ASSERT(initialized_);
449 DataMediaChannel* media_channel = data_media_engine_->CreateChannel(
450 data_channel_type);
451 if (!media_channel) {
452 LOG(LS_WARNING) << "Failed to create data channel of type "
453 << data_channel_type;
454 return NULL;
455 }
456
457 DataChannel* data_channel = new DataChannel(
deadbeefcbecd352015-09-23 11:50:27 -0700458 worker_thread_, media_channel, transport_controller, content_name, rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459 if (!data_channel->Init()) {
460 LOG(LS_WARNING) << "Failed to init data channel.";
461 delete data_channel;
462 return NULL;
463 }
464 data_channels_.push_back(data_channel);
465 return data_channel;
466}
467
468void ChannelManager::DestroyDataChannel(DataChannel* data_channel) {
469 if (data_channel) {
470 worker_thread_->Invoke<void>(
471 Bind(&ChannelManager::DestroyDataChannel_w, this, data_channel));
472 }
473}
474
475void ChannelManager::DestroyDataChannel_w(DataChannel* data_channel) {
476 // Destroy data channel.
477 ASSERT(initialized_);
478 DataChannels::iterator it = std::find(data_channels_.begin(),
479 data_channels_.end(), data_channel);
480 ASSERT(it != data_channels_.end());
481 if (it == data_channels_.end())
482 return;
483
484 data_channels_.erase(it);
485 delete data_channel;
486}
487
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000488bool ChannelManager::GetAudioOptions(std::string* in_name,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000489 std::string* out_name,
490 AudioOptions* options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491 if (in_name)
492 *in_name = audio_in_device_;
493 if (out_name)
494 *out_name = audio_out_device_;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000495 if (options)
496 *options = audio_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 return true;
498}
499
500bool ChannelManager::SetAudioOptions(const std::string& in_name,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000501 const std::string& out_name,
502 const AudioOptions& options) {
503 return SetAudioOptions(in_name, out_name, options, audio_delay_offset_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504}
505
506bool ChannelManager::SetAudioOptions(const std::string& in_name,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000507 const std::string& out_name,
508 const AudioOptions& options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000509 int delay_offset) {
510 // Get device ids from DeviceManager.
511 Device in_dev, out_dev;
512 if (!device_manager_->GetAudioInputDevice(in_name, &in_dev)) {
513 LOG(LS_WARNING) << "Failed to GetAudioInputDevice: " << in_name;
514 return false;
515 }
516 if (!device_manager_->GetAudioOutputDevice(out_name, &out_dev)) {
517 LOG(LS_WARNING) << "Failed to GetAudioOutputDevice: " << out_name;
518 return false;
519 }
520
521 // If we're initialized, pass the settings to the media engine.
522 bool ret = true;
523 if (initialized_) {
524 ret = worker_thread_->Invoke<bool>(
525 Bind(&ChannelManager::SetAudioOptions_w, this,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000526 options, delay_offset, &in_dev, &out_dev));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527 }
528
529 // If all worked well, save the values for use in GetAudioOptions.
530 if (ret) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000531 audio_options_ = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532 audio_in_device_ = in_name;
533 audio_out_device_ = out_name;
534 audio_delay_offset_ = delay_offset;
535 }
536 return ret;
537}
538
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000539bool ChannelManager::SetAudioOptions_w(
540 const AudioOptions& options, int delay_offset,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541 const Device* in_dev, const Device* out_dev) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000542 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543 ASSERT(initialized_);
544
545 // Set audio options
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000546 bool ret = media_engine_->SetAudioOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547
548 if (ret) {
549 ret = media_engine_->SetAudioDelayOffset(delay_offset);
550 }
551
552 // Set the audio devices
553 if (ret) {
554 ret = media_engine_->SetSoundDevices(in_dev, out_dev);
555 }
556
557 return ret;
558}
559
560bool ChannelManager::GetOutputVolume(int* level) {
561 if (!initialized_) {
562 return false;
563 }
564 return worker_thread_->Invoke<bool>(
565 Bind(&MediaEngineInterface::GetOutputVolume, media_engine_.get(), level));
566}
567
568bool ChannelManager::SetOutputVolume(int level) {
569 bool ret = level >= 0 && level <= 255;
570 if (initialized_) {
571 ret &= worker_thread_->Invoke<bool>(
572 Bind(&MediaEngineInterface::SetOutputVolume,
573 media_engine_.get(), level));
574 }
575
576 if (ret) {
577 audio_output_volume_ = level;
578 }
579
580 return ret;
581}
582
583bool ChannelManager::IsSameCapturer(const std::string& capturer_name,
584 VideoCapturer* capturer) {
585 if (capturer == NULL) {
586 return false;
587 }
588 Device device;
589 if (!device_manager_->GetVideoCaptureDevice(capturer_name, &device)) {
590 return false;
591 }
592 return capturer->GetId() == device.id;
593}
594
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000595bool ChannelManager::GetVideoCaptureDevice(Device* device) {
596 std::string device_name;
597 if (!GetCaptureDevice(&device_name)) {
598 return false;
599 }
600 return device_manager_->GetVideoCaptureDevice(device_name, device);
601}
602
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603bool ChannelManager::GetCaptureDevice(std::string* cam_name) {
604 if (camera_device_.empty()) {
605 // Initialize camera_device_ with default.
606 Device device;
607 if (!device_manager_->GetVideoCaptureDevice(
608 DeviceManagerInterface::kDefaultDeviceName, &device)) {
609 LOG(LS_WARNING) << "Device manager can't find default camera: " <<
610 DeviceManagerInterface::kDefaultDeviceName;
611 return false;
612 }
613 camera_device_ = device.name;
614 }
615 *cam_name = camera_device_;
616 return true;
617}
618
619bool ChannelManager::SetCaptureDevice(const std::string& cam_name) {
620 Device device;
621 bool ret = true;
622 if (!device_manager_->GetVideoCaptureDevice(cam_name, &device)) {
623 if (!cam_name.empty()) {
624 LOG(LS_WARNING) << "Device manager can't find camera: " << cam_name;
625 }
626 ret = false;
627 }
628
629 // If we're running, tell the media engine about it.
630 if (initialized_ && ret) {
631 ret = worker_thread_->Invoke<bool>(
632 Bind(&ChannelManager::SetCaptureDevice_w, this, &device));
633 }
634
635 // If everything worked, retain the name of the selected camera.
636 if (ret) {
637 camera_device_ = device.name;
638 } else if (camera_device_.empty()) {
639 // When video option setting fails, we still want camera_device_ to be in a
640 // good state, so we initialize it with default if it's empty.
641 Device default_device;
642 if (!device_manager_->GetVideoCaptureDevice(
643 DeviceManagerInterface::kDefaultDeviceName, &default_device)) {
644 LOG(LS_WARNING) << "Device manager can't find default camera: " <<
645 DeviceManagerInterface::kDefaultDeviceName;
646 }
647 camera_device_ = default_device.name;
648 }
649
650 return ret;
651}
652
653VideoCapturer* ChannelManager::CreateVideoCapturer() {
654 Device device;
655 if (!device_manager_->GetVideoCaptureDevice(camera_device_, &device)) {
656 if (!camera_device_.empty()) {
657 LOG(LS_WARNING) << "Device manager can't find camera: " << camera_device_;
658 }
659 return NULL;
660 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000661 VideoCapturer* capturer = device_manager_->CreateVideoCapturer(device);
662 if (capturer && default_video_encoder_config_.max_codec.id != 0) {
663 // For now, use the aspect ratio of the default_video_encoder_config_,
664 // which may be different than the native aspect ratio of the start
665 // format the camera may use.
666 capturer->UpdateAspectRatio(
667 default_video_encoder_config_.max_codec.width,
668 default_video_encoder_config_.max_codec.height);
669 }
670 return capturer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000671}
672
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000673VideoCapturer* ChannelManager::CreateScreenCapturer(
674 const ScreencastId& screenid) {
675 return device_manager_->CreateScreenCapturer(screenid);
676}
677
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000678bool ChannelManager::SetCaptureDevice_w(const Device* cam_device) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000679 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000680 ASSERT(initialized_);
681
682 if (!cam_device) {
683 video_device_name_.clear();
684 return true;
685 }
686 video_device_name_ = cam_device->name;
687 return true;
688}
689
690bool ChannelManager::SetDefaultVideoEncoderConfig(const VideoEncoderConfig& c) {
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000691 bool ret = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692 if (initialized_) {
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000693 ret = worker_thread_->Invoke<bool>(
694 Bind(&MediaEngineInterface::SetDefaultVideoEncoderConfig,
695 media_engine_.get(), c));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 }
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000697 if (ret) {
698 default_video_encoder_config_ = c;
699 }
700 return ret;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000701}
702
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000703void ChannelManager::SetVoiceLogging(int level, const char* filter) {
704 if (initialized_) {
705 worker_thread_->Invoke<void>(
706 Bind(&MediaEngineInterface::SetVoiceLogging,
707 media_engine_.get(), level, filter));
708 } else {
709 media_engine_->SetVoiceLogging(level, filter);
710 }
711}
712
713void ChannelManager::SetVideoLogging(int level, const char* filter) {
714 if (initialized_) {
715 worker_thread_->Invoke<void>(
716 Bind(&MediaEngineInterface::SetVideoLogging,
717 media_engine_.get(), level, filter));
718 } else {
719 media_engine_->SetVideoLogging(level, filter);
720 }
721}
722
hbos@webrtc.org1e642632015-02-25 09:49:41 +0000723std::vector<cricket::VideoFormat> ChannelManager::GetSupportedFormats(
724 VideoCapturer* capturer) const {
725 ASSERT(capturer != NULL);
726 std::vector<VideoFormat> formats;
727 worker_thread_->Invoke<void>(rtc::Bind(&ChannelManager::GetSupportedFormats_w,
728 this, capturer, &formats));
729 return formats;
730}
731
732void ChannelManager::GetSupportedFormats_w(
733 VideoCapturer* capturer,
734 std::vector<cricket::VideoFormat>* out_formats) const {
735 const std::vector<VideoFormat>* formats = capturer->GetSupportedFormats();
736 if (formats != NULL)
737 *out_formats = *formats;
738}
739
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000740// The following are done in the new "CaptureManager" style that
741// all local video capturers, processors, and managers should move
742// to.
743// TODO(pthatcher): Add more of the CaptureManager interface.
744bool ChannelManager::StartVideoCapture(
745 VideoCapturer* capturer, const VideoFormat& video_format) {
746 return initialized_ && worker_thread_->Invoke<bool>(
747 Bind(&CaptureManager::StartVideoCapture,
748 capture_manager_.get(), capturer, video_format));
749}
750
751bool ChannelManager::MuteToBlackThenPause(
752 VideoCapturer* video_capturer, bool muted) {
753 if (!initialized_) {
754 return false;
755 }
756 worker_thread_->Invoke<void>(
757 Bind(&VideoCapturer::MuteToBlackThenPause, video_capturer, muted));
758 return true;
759}
760
761bool ChannelManager::StopVideoCapture(
762 VideoCapturer* capturer, const VideoFormat& video_format) {
763 return initialized_ && worker_thread_->Invoke<bool>(
764 Bind(&CaptureManager::StopVideoCapture,
765 capture_manager_.get(), capturer, video_format));
766}
767
768bool ChannelManager::RestartVideoCapture(
769 VideoCapturer* video_capturer,
770 const VideoFormat& previous_format,
771 const VideoFormat& desired_format,
772 CaptureManager::RestartOptions options) {
773 return initialized_ && worker_thread_->Invoke<bool>(
774 Bind(&CaptureManager::RestartVideoCapture, capture_manager_.get(),
775 video_capturer, previous_format, desired_format, options));
776}
777
778bool ChannelManager::AddVideoRenderer(
779 VideoCapturer* capturer, VideoRenderer* renderer) {
780 return initialized_ && worker_thread_->Invoke<bool>(
781 Bind(&CaptureManager::AddVideoRenderer,
782 capture_manager_.get(), capturer, renderer));
783}
784
785bool ChannelManager::RemoveVideoRenderer(
786 VideoCapturer* capturer, VideoRenderer* renderer) {
787 return initialized_ && worker_thread_->Invoke<bool>(
788 Bind(&CaptureManager::RemoveVideoRenderer,
789 capture_manager_.get(), capturer, renderer));
790}
791
792bool ChannelManager::IsScreencastRunning() const {
793 return initialized_ && worker_thread_->Invoke<bool>(
794 Bind(&ChannelManager::IsScreencastRunning_w, this));
795}
796
797bool ChannelManager::IsScreencastRunning_w() const {
798 VideoChannels::const_iterator it = video_channels_.begin();
799 for ( ; it != video_channels_.end(); ++it) {
800 if ((*it) && (*it)->IsScreencasting()) {
801 return true;
802 }
803 }
804 return false;
805}
806
807void ChannelManager::OnVideoCaptureStateChange(VideoCapturer* capturer,
808 CaptureState result) {
809 // TODO(whyuan): Check capturer and signal failure only for camera video, not
810 // screencast.
811 capturing_ = result == CS_RUNNING;
812 main_thread_->Post(this, MSG_VIDEOCAPTURESTATE,
813 new CaptureStateParams(capturer, result));
814}
815
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000816void ChannelManager::OnMessage(rtc::Message* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 switch (message->message_id) {
818 case MSG_VIDEOCAPTURESTATE: {
819 CaptureStateParams* data =
820 static_cast<CaptureStateParams*>(message->pdata);
821 SignalVideoCaptureStateChange(data->capturer, data->state);
822 delete data;
823 break;
824 }
825 }
826}
827
828
829static void GetDeviceNames(const std::vector<Device>& devs,
830 std::vector<std::string>* names) {
831 names->clear();
832 for (size_t i = 0; i < devs.size(); ++i) {
833 names->push_back(devs[i].name);
834 }
835}
836
837bool ChannelManager::GetAudioInputDevices(std::vector<std::string>* names) {
838 names->clear();
839 std::vector<Device> devs;
840 bool ret = device_manager_->GetAudioInputDevices(&devs);
841 if (ret)
842 GetDeviceNames(devs, names);
843
844 return ret;
845}
846
847bool ChannelManager::GetAudioOutputDevices(std::vector<std::string>* names) {
848 names->clear();
849 std::vector<Device> devs;
850 bool ret = device_manager_->GetAudioOutputDevices(&devs);
851 if (ret)
852 GetDeviceNames(devs, names);
853
854 return ret;
855}
856
857bool ChannelManager::GetVideoCaptureDevices(std::vector<std::string>* names) {
858 names->clear();
859 std::vector<Device> devs;
860 bool ret = device_manager_->GetVideoCaptureDevices(&devs);
861 if (ret)
862 GetDeviceNames(devs, names);
863
864 return ret;
865}
866
867void ChannelManager::SetVideoCaptureDeviceMaxFormat(
868 const std::string& usb_id,
869 const VideoFormat& max_format) {
870 device_manager_->SetVideoCaptureDeviceMaxFormat(usb_id, max_format);
871}
872
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000873bool ChannelManager::StartAecDump(rtc::PlatformFile file) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000874 return worker_thread_->Invoke<bool>(
875 Bind(&MediaEngineInterface::StartAecDump, media_engine_.get(), file));
876}
877
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878} // namespace cricket