blob: 52116ddb39c7b2298c251546468764dd2fe066d9 [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
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036#include "talk/media/base/capturemanager.h"
37#include "talk/media/base/hybriddataengine.h"
38#include "talk/media/base/rtpdataengine.h"
39#include "talk/media/base/videocapturer.h"
henrike@webrtc.org723d6832013-07-12 16:04:50 +000040#include "talk/media/devices/devicemanager.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041#ifdef HAVE_SCTP
42#include "talk/media/sctp/sctpdataengine.h"
43#endif
wu@webrtc.org9dba5252013-08-05 20:36:57 +000044#include "talk/session/media/srtpfilter.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000045#include "webrtc/base/bind.h"
46#include "webrtc/base/common.h"
47#include "webrtc/base/logging.h"
48#include "webrtc/base/sigslotrepeater.h"
49#include "webrtc/base/stringencode.h"
50#include "webrtc/base/stringutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051
52namespace cricket {
53
54enum {
55 MSG_VIDEOCAPTURESTATE = 1,
56};
57
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000058using rtc::Bind;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059
60static const int kNotSetOutputVolume = -1;
61
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000062struct CaptureStateParams : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063 CaptureStateParams(cricket::VideoCapturer* c, cricket::CaptureState s)
64 : capturer(c),
65 state(s) {}
66 cricket::VideoCapturer* capturer;
67 cricket::CaptureState state;
68};
69
70static DataEngineInterface* ConstructDataEngine() {
71#ifdef HAVE_SCTP
72 return new HybridDataEngine(new RtpDataEngine(), new SctpDataEngine());
73#else
74 return new RtpDataEngine();
75#endif
76}
77
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078ChannelManager::ChannelManager(MediaEngineInterface* me,
79 DataEngineInterface* dme,
80 DeviceManagerInterface* dm,
81 CaptureManager* cm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000082 rtc::Thread* worker_thread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083 Construct(me, dme, dm, cm, worker_thread);
84}
85
86ChannelManager::ChannelManager(MediaEngineInterface* me,
87 DeviceManagerInterface* dm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000088 rtc::Thread* worker_thread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 Construct(me,
90 ConstructDataEngine(),
91 dm,
92 new CaptureManager(),
93 worker_thread);
94}
95
96void ChannelManager::Construct(MediaEngineInterface* me,
97 DataEngineInterface* dme,
98 DeviceManagerInterface* dm,
99 CaptureManager* cm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000100 rtc::Thread* worker_thread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 media_engine_.reset(me);
102 data_media_engine_.reset(dme);
103 device_manager_.reset(dm);
104 capture_manager_.reset(cm);
105 initialized_ = false;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000106 main_thread_ = rtc::Thread::Current();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107 worker_thread_ = worker_thread;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000108 // Get the default audio options from the media engine.
109 audio_options_ = media_engine_->GetAudioOptions();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110 audio_in_device_ = DeviceManagerInterface::kDefaultDeviceName;
111 audio_out_device_ = DeviceManagerInterface::kDefaultDeviceName;
henrike@webrtc.org0481f152014-08-19 14:56:59 +0000112 audio_delay_offset_ = kDefaultAudioDelayOffset;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 audio_output_volume_ = kNotSetOutputVolume;
114 local_renderer_ = NULL;
115 capturing_ = false;
116 monitoring_ = false;
117 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 Solenberg4b60c732015-05-07 14:07:48 +0200311 DestroyVoiceChannel_w(voice_channels_.back(), nullptr);
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(
Jelena Marusicc28a8962015-05-29 15:05:44 +0200320 BaseSession* session,
321 const std::string& content_name,
322 bool rtcp,
323 const AudioOptions& options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000324 return worker_thread_->Invoke<VoiceChannel*>(
Jelena Marusicc28a8962015-05-29 15:05:44 +0200325 Bind(&ChannelManager::CreateVoiceChannel_w, this, session, content_name,
326 rtcp, options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327}
328
329VoiceChannel* ChannelManager::CreateVoiceChannel_w(
Jelena Marusicc28a8962015-05-29 15:05:44 +0200330 BaseSession* session,
331 const std::string& content_name,
332 bool rtcp,
333 const AudioOptions& options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000334 ASSERT(initialized_);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200335 ASSERT(worker_thread_ == rtc::Thread::Current());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200336 VoiceMediaChannel* media_channel = media_engine_->CreateChannel(options);
337 if (!media_channel)
338 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000339
340 VoiceChannel* voice_channel = new VoiceChannel(
341 worker_thread_, media_engine_.get(), media_channel,
342 session, content_name, rtcp);
343 if (!voice_channel->Init()) {
344 delete voice_channel;
Jelena Marusicc28a8962015-05-29 15:05:44 +0200345 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346 }
347 voice_channels_.push_back(voice_channel);
348 return voice_channel;
349}
350
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200351void ChannelManager::DestroyVoiceChannel(VoiceChannel* voice_channel,
352 VideoChannel* video_channel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353 if (voice_channel) {
354 worker_thread_->Invoke<void>(
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200355 Bind(&ChannelManager::DestroyVoiceChannel_w, this, voice_channel,
356 video_channel));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357 }
358}
359
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200360void ChannelManager::DestroyVoiceChannel_w(VoiceChannel* voice_channel,
361 VideoChannel* video_channel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362 // Destroy voice channel.
363 ASSERT(initialized_);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200364 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000365 VoiceChannels::iterator it = std::find(voice_channels_.begin(),
366 voice_channels_.end(), voice_channel);
367 ASSERT(it != voice_channels_.end());
368 if (it == voice_channels_.end())
369 return;
370
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200371 if (video_channel) {
372 video_channel->media_channel()->DetachVoiceChannel();
373 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000374 voice_channels_.erase(it);
375 delete voice_channel;
376}
377
378VideoChannel* ChannelManager::CreateVideoChannel(
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000379 BaseSession* session,
380 const std::string& content_name,
381 bool rtcp,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000382 VoiceChannel* voice_channel) {
383 return worker_thread_->Invoke<VideoChannel*>(
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000384 Bind(&ChannelManager::CreateVideoChannel_w,
385 this,
386 session,
387 content_name,
388 rtcp,
389 VideoOptions(),
390 voice_channel));
391}
392
393VideoChannel* ChannelManager::CreateVideoChannel(
394 BaseSession* session,
395 const std::string& content_name,
396 bool rtcp,
397 const VideoOptions& options,
398 VoiceChannel* voice_channel) {
399 return worker_thread_->Invoke<VideoChannel*>(
400 Bind(&ChannelManager::CreateVideoChannel_w,
401 this,
402 session,
403 content_name,
404 rtcp,
405 options,
406 voice_channel));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407}
408
409VideoChannel* ChannelManager::CreateVideoChannel_w(
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000410 BaseSession* session,
411 const std::string& content_name,
412 bool rtcp,
413 const VideoOptions& options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000414 VoiceChannel* voice_channel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000415 ASSERT(initialized_);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200416 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000417 VideoMediaChannel* media_channel =
418 // voice_channel can be NULL in case of NullVoiceEngine.
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000419 media_engine_->CreateVideoChannel(
420 options, voice_channel ? voice_channel->media_channel() : NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000421 if (media_channel == NULL)
422 return NULL;
423
424 VideoChannel* video_channel = new VideoChannel(
425 worker_thread_, media_engine_.get(), media_channel,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +0200426 session, content_name, rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000427 if (!video_channel->Init()) {
428 delete video_channel;
429 return NULL;
430 }
431 video_channels_.push_back(video_channel);
432 return video_channel;
433}
434
435void ChannelManager::DestroyVideoChannel(VideoChannel* video_channel) {
436 if (video_channel) {
437 worker_thread_->Invoke<void>(
438 Bind(&ChannelManager::DestroyVideoChannel_w, this, video_channel));
439 }
440}
441
442void ChannelManager::DestroyVideoChannel_w(VideoChannel* video_channel) {
443 // Destroy video channel.
444 ASSERT(initialized_);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200445 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000446 VideoChannels::iterator it = std::find(video_channels_.begin(),
447 video_channels_.end(), video_channel);
448 ASSERT(it != video_channels_.end());
449 if (it == video_channels_.end())
450 return;
451
452 video_channels_.erase(it);
453 delete video_channel;
454}
455
456DataChannel* ChannelManager::CreateDataChannel(
457 BaseSession* session, const std::string& content_name,
458 bool rtcp, DataChannelType channel_type) {
459 return worker_thread_->Invoke<DataChannel*>(
460 Bind(&ChannelManager::CreateDataChannel_w, this, session, content_name,
461 rtcp, channel_type));
462}
463
464DataChannel* ChannelManager::CreateDataChannel_w(
465 BaseSession* session, const std::string& content_name,
466 bool rtcp, DataChannelType data_channel_type) {
467 // This is ok to alloc from a thread other than the worker thread.
468 ASSERT(initialized_);
469 DataMediaChannel* media_channel = data_media_engine_->CreateChannel(
470 data_channel_type);
471 if (!media_channel) {
472 LOG(LS_WARNING) << "Failed to create data channel of type "
473 << data_channel_type;
474 return NULL;
475 }
476
477 DataChannel* data_channel = new DataChannel(
478 worker_thread_, media_channel,
479 session, content_name, rtcp);
480 if (!data_channel->Init()) {
481 LOG(LS_WARNING) << "Failed to init data channel.";
482 delete data_channel;
483 return NULL;
484 }
485 data_channels_.push_back(data_channel);
486 return data_channel;
487}
488
489void ChannelManager::DestroyDataChannel(DataChannel* data_channel) {
490 if (data_channel) {
491 worker_thread_->Invoke<void>(
492 Bind(&ChannelManager::DestroyDataChannel_w, this, data_channel));
493 }
494}
495
496void ChannelManager::DestroyDataChannel_w(DataChannel* data_channel) {
497 // Destroy data channel.
498 ASSERT(initialized_);
499 DataChannels::iterator it = std::find(data_channels_.begin(),
500 data_channels_.end(), data_channel);
501 ASSERT(it != data_channels_.end());
502 if (it == data_channels_.end())
503 return;
504
505 data_channels_.erase(it);
506 delete data_channel;
507}
508
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000509bool ChannelManager::GetAudioOptions(std::string* in_name,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000510 std::string* out_name,
511 AudioOptions* options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512 if (in_name)
513 *in_name = audio_in_device_;
514 if (out_name)
515 *out_name = audio_out_device_;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000516 if (options)
517 *options = audio_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000518 return true;
519}
520
521bool ChannelManager::SetAudioOptions(const std::string& in_name,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000522 const std::string& out_name,
523 const AudioOptions& options) {
524 return SetAudioOptions(in_name, out_name, options, audio_delay_offset_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525}
526
527bool ChannelManager::SetAudioOptions(const std::string& in_name,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000528 const std::string& out_name,
529 const AudioOptions& options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530 int delay_offset) {
531 // Get device ids from DeviceManager.
532 Device in_dev, out_dev;
533 if (!device_manager_->GetAudioInputDevice(in_name, &in_dev)) {
534 LOG(LS_WARNING) << "Failed to GetAudioInputDevice: " << in_name;
535 return false;
536 }
537 if (!device_manager_->GetAudioOutputDevice(out_name, &out_dev)) {
538 LOG(LS_WARNING) << "Failed to GetAudioOutputDevice: " << out_name;
539 return false;
540 }
541
542 // If we're initialized, pass the settings to the media engine.
543 bool ret = true;
544 if (initialized_) {
545 ret = worker_thread_->Invoke<bool>(
546 Bind(&ChannelManager::SetAudioOptions_w, this,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000547 options, delay_offset, &in_dev, &out_dev));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548 }
549
550 // If all worked well, save the values for use in GetAudioOptions.
551 if (ret) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000552 audio_options_ = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 audio_in_device_ = in_name;
554 audio_out_device_ = out_name;
555 audio_delay_offset_ = delay_offset;
556 }
557 return ret;
558}
559
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000560bool ChannelManager::SetAudioOptions_w(
561 const AudioOptions& options, int delay_offset,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000562 const Device* in_dev, const Device* out_dev) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000563 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000564 ASSERT(initialized_);
565
566 // Set audio options
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000567 bool ret = media_engine_->SetAudioOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000568
569 if (ret) {
570 ret = media_engine_->SetAudioDelayOffset(delay_offset);
571 }
572
573 // Set the audio devices
574 if (ret) {
575 ret = media_engine_->SetSoundDevices(in_dev, out_dev);
576 }
577
578 return ret;
579}
580
581bool ChannelManager::GetOutputVolume(int* level) {
582 if (!initialized_) {
583 return false;
584 }
585 return worker_thread_->Invoke<bool>(
586 Bind(&MediaEngineInterface::GetOutputVolume, media_engine_.get(), level));
587}
588
589bool ChannelManager::SetOutputVolume(int level) {
590 bool ret = level >= 0 && level <= 255;
591 if (initialized_) {
592 ret &= worker_thread_->Invoke<bool>(
593 Bind(&MediaEngineInterface::SetOutputVolume,
594 media_engine_.get(), level));
595 }
596
597 if (ret) {
598 audio_output_volume_ = level;
599 }
600
601 return ret;
602}
603
604bool ChannelManager::IsSameCapturer(const std::string& capturer_name,
605 VideoCapturer* capturer) {
606 if (capturer == NULL) {
607 return false;
608 }
609 Device device;
610 if (!device_manager_->GetVideoCaptureDevice(capturer_name, &device)) {
611 return false;
612 }
613 return capturer->GetId() == device.id;
614}
615
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000616bool ChannelManager::GetVideoCaptureDevice(Device* device) {
617 std::string device_name;
618 if (!GetCaptureDevice(&device_name)) {
619 return false;
620 }
621 return device_manager_->GetVideoCaptureDevice(device_name, device);
622}
623
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624bool ChannelManager::GetCaptureDevice(std::string* cam_name) {
625 if (camera_device_.empty()) {
626 // Initialize camera_device_ with default.
627 Device device;
628 if (!device_manager_->GetVideoCaptureDevice(
629 DeviceManagerInterface::kDefaultDeviceName, &device)) {
630 LOG(LS_WARNING) << "Device manager can't find default camera: " <<
631 DeviceManagerInterface::kDefaultDeviceName;
632 return false;
633 }
634 camera_device_ = device.name;
635 }
636 *cam_name = camera_device_;
637 return true;
638}
639
640bool ChannelManager::SetCaptureDevice(const std::string& cam_name) {
641 Device device;
642 bool ret = true;
643 if (!device_manager_->GetVideoCaptureDevice(cam_name, &device)) {
644 if (!cam_name.empty()) {
645 LOG(LS_WARNING) << "Device manager can't find camera: " << cam_name;
646 }
647 ret = false;
648 }
649
650 // If we're running, tell the media engine about it.
651 if (initialized_ && ret) {
652 ret = worker_thread_->Invoke<bool>(
653 Bind(&ChannelManager::SetCaptureDevice_w, this, &device));
654 }
655
656 // If everything worked, retain the name of the selected camera.
657 if (ret) {
658 camera_device_ = device.name;
659 } else if (camera_device_.empty()) {
660 // When video option setting fails, we still want camera_device_ to be in a
661 // good state, so we initialize it with default if it's empty.
662 Device default_device;
663 if (!device_manager_->GetVideoCaptureDevice(
664 DeviceManagerInterface::kDefaultDeviceName, &default_device)) {
665 LOG(LS_WARNING) << "Device manager can't find default camera: " <<
666 DeviceManagerInterface::kDefaultDeviceName;
667 }
668 camera_device_ = default_device.name;
669 }
670
671 return ret;
672}
673
674VideoCapturer* ChannelManager::CreateVideoCapturer() {
675 Device device;
676 if (!device_manager_->GetVideoCaptureDevice(camera_device_, &device)) {
677 if (!camera_device_.empty()) {
678 LOG(LS_WARNING) << "Device manager can't find camera: " << camera_device_;
679 }
680 return NULL;
681 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000682 VideoCapturer* capturer = device_manager_->CreateVideoCapturer(device);
683 if (capturer && default_video_encoder_config_.max_codec.id != 0) {
684 // For now, use the aspect ratio of the default_video_encoder_config_,
685 // which may be different than the native aspect ratio of the start
686 // format the camera may use.
687 capturer->UpdateAspectRatio(
688 default_video_encoder_config_.max_codec.width,
689 default_video_encoder_config_.max_codec.height);
690 }
691 return capturer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692}
693
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000694VideoCapturer* ChannelManager::CreateScreenCapturer(
695 const ScreencastId& screenid) {
696 return device_manager_->CreateScreenCapturer(screenid);
697}
698
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000699bool ChannelManager::SetCaptureDevice_w(const Device* cam_device) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000700 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000701 ASSERT(initialized_);
702
703 if (!cam_device) {
704 video_device_name_.clear();
705 return true;
706 }
707 video_device_name_ = cam_device->name;
708 return true;
709}
710
711bool ChannelManager::SetDefaultVideoEncoderConfig(const VideoEncoderConfig& c) {
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000712 bool ret = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713 if (initialized_) {
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000714 ret = worker_thread_->Invoke<bool>(
715 Bind(&MediaEngineInterface::SetDefaultVideoEncoderConfig,
716 media_engine_.get(), c));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 }
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000718 if (ret) {
719 default_video_encoder_config_ = c;
720 }
721 return ret;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722}
723
724bool ChannelManager::SetLocalMonitor(bool enable) {
725 bool ret = initialized_ && worker_thread_->Invoke<bool>(
726 Bind(&MediaEngineInterface::SetLocalMonitor,
727 media_engine_.get(), enable));
728 if (ret) {
729 monitoring_ = enable;
730 }
731 return ret;
732}
733
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000734void ChannelManager::SetVoiceLogging(int level, const char* filter) {
735 if (initialized_) {
736 worker_thread_->Invoke<void>(
737 Bind(&MediaEngineInterface::SetVoiceLogging,
738 media_engine_.get(), level, filter));
739 } else {
740 media_engine_->SetVoiceLogging(level, filter);
741 }
742}
743
744void ChannelManager::SetVideoLogging(int level, const char* filter) {
745 if (initialized_) {
746 worker_thread_->Invoke<void>(
747 Bind(&MediaEngineInterface::SetVideoLogging,
748 media_engine_.get(), level, filter));
749 } else {
750 media_engine_->SetVideoLogging(level, filter);
751 }
752}
753
hbos@webrtc.org1e642632015-02-25 09:49:41 +0000754std::vector<cricket::VideoFormat> ChannelManager::GetSupportedFormats(
755 VideoCapturer* capturer) const {
756 ASSERT(capturer != NULL);
757 std::vector<VideoFormat> formats;
758 worker_thread_->Invoke<void>(rtc::Bind(&ChannelManager::GetSupportedFormats_w,
759 this, capturer, &formats));
760 return formats;
761}
762
763void ChannelManager::GetSupportedFormats_w(
764 VideoCapturer* capturer,
765 std::vector<cricket::VideoFormat>* out_formats) const {
766 const std::vector<VideoFormat>* formats = capturer->GetSupportedFormats();
767 if (formats != NULL)
768 *out_formats = *formats;
769}
770
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771// TODO(janahan): For now pass this request through the mediaengine to the
772// voice and video engines to do the real work. Once the capturer refactoring
773// is done, we will access the capturer using the ssrc (similar to how the
774// renderer is accessed today) and register with it directly.
775bool ChannelManager::RegisterVideoProcessor(VideoCapturer* capturer,
776 VideoProcessor* processor) {
777 return initialized_ && worker_thread_->Invoke<bool>(
778 Bind(&ChannelManager::RegisterVideoProcessor_w, this,
779 capturer, processor));
780}
781
782bool ChannelManager::RegisterVideoProcessor_w(VideoCapturer* capturer,
783 VideoProcessor* processor) {
784 return capture_manager_->AddVideoProcessor(capturer, processor);
785}
786
787bool ChannelManager::UnregisterVideoProcessor(VideoCapturer* capturer,
788 VideoProcessor* processor) {
789 return initialized_ && worker_thread_->Invoke<bool>(
790 Bind(&ChannelManager::UnregisterVideoProcessor_w, this,
791 capturer, processor));
792}
793
794bool ChannelManager::UnregisterVideoProcessor_w(VideoCapturer* capturer,
795 VideoProcessor* processor) {
796 return capture_manager_->RemoveVideoProcessor(capturer, processor);
797}
798
799bool ChannelManager::RegisterVoiceProcessor(
800 uint32 ssrc,
801 VoiceProcessor* processor,
802 MediaProcessorDirection direction) {
803 return initialized_ && worker_thread_->Invoke<bool>(
804 Bind(&MediaEngineInterface::RegisterVoiceProcessor, media_engine_.get(),
805 ssrc, processor, direction));
806}
807
808bool ChannelManager::UnregisterVoiceProcessor(
809 uint32 ssrc,
810 VoiceProcessor* processor,
811 MediaProcessorDirection direction) {
812 return initialized_ && worker_thread_->Invoke<bool>(
813 Bind(&MediaEngineInterface::UnregisterVoiceProcessor,
814 media_engine_.get(), ssrc, processor, direction));
815}
816
817// The following are done in the new "CaptureManager" style that
818// all local video capturers, processors, and managers should move
819// to.
820// TODO(pthatcher): Add more of the CaptureManager interface.
821bool ChannelManager::StartVideoCapture(
822 VideoCapturer* capturer, const VideoFormat& video_format) {
823 return initialized_ && worker_thread_->Invoke<bool>(
824 Bind(&CaptureManager::StartVideoCapture,
825 capture_manager_.get(), capturer, video_format));
826}
827
828bool ChannelManager::MuteToBlackThenPause(
829 VideoCapturer* video_capturer, bool muted) {
830 if (!initialized_) {
831 return false;
832 }
833 worker_thread_->Invoke<void>(
834 Bind(&VideoCapturer::MuteToBlackThenPause, video_capturer, muted));
835 return true;
836}
837
838bool ChannelManager::StopVideoCapture(
839 VideoCapturer* capturer, const VideoFormat& video_format) {
840 return initialized_ && worker_thread_->Invoke<bool>(
841 Bind(&CaptureManager::StopVideoCapture,
842 capture_manager_.get(), capturer, video_format));
843}
844
845bool ChannelManager::RestartVideoCapture(
846 VideoCapturer* video_capturer,
847 const VideoFormat& previous_format,
848 const VideoFormat& desired_format,
849 CaptureManager::RestartOptions options) {
850 return initialized_ && worker_thread_->Invoke<bool>(
851 Bind(&CaptureManager::RestartVideoCapture, capture_manager_.get(),
852 video_capturer, previous_format, desired_format, options));
853}
854
855bool ChannelManager::AddVideoRenderer(
856 VideoCapturer* capturer, VideoRenderer* renderer) {
857 return initialized_ && worker_thread_->Invoke<bool>(
858 Bind(&CaptureManager::AddVideoRenderer,
859 capture_manager_.get(), capturer, renderer));
860}
861
862bool ChannelManager::RemoveVideoRenderer(
863 VideoCapturer* capturer, VideoRenderer* renderer) {
864 return initialized_ && worker_thread_->Invoke<bool>(
865 Bind(&CaptureManager::RemoveVideoRenderer,
866 capture_manager_.get(), capturer, renderer));
867}
868
869bool ChannelManager::IsScreencastRunning() const {
870 return initialized_ && worker_thread_->Invoke<bool>(
871 Bind(&ChannelManager::IsScreencastRunning_w, this));
872}
873
874bool ChannelManager::IsScreencastRunning_w() const {
875 VideoChannels::const_iterator it = video_channels_.begin();
876 for ( ; it != video_channels_.end(); ++it) {
877 if ((*it) && (*it)->IsScreencasting()) {
878 return true;
879 }
880 }
881 return false;
882}
883
884void ChannelManager::OnVideoCaptureStateChange(VideoCapturer* capturer,
885 CaptureState result) {
886 // TODO(whyuan): Check capturer and signal failure only for camera video, not
887 // screencast.
888 capturing_ = result == CS_RUNNING;
889 main_thread_->Post(this, MSG_VIDEOCAPTURESTATE,
890 new CaptureStateParams(capturer, result));
891}
892
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000893void ChannelManager::OnMessage(rtc::Message* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000894 switch (message->message_id) {
895 case MSG_VIDEOCAPTURESTATE: {
896 CaptureStateParams* data =
897 static_cast<CaptureStateParams*>(message->pdata);
898 SignalVideoCaptureStateChange(data->capturer, data->state);
899 delete data;
900 break;
901 }
902 }
903}
904
905
906static void GetDeviceNames(const std::vector<Device>& devs,
907 std::vector<std::string>* names) {
908 names->clear();
909 for (size_t i = 0; i < devs.size(); ++i) {
910 names->push_back(devs[i].name);
911 }
912}
913
914bool ChannelManager::GetAudioInputDevices(std::vector<std::string>* names) {
915 names->clear();
916 std::vector<Device> devs;
917 bool ret = device_manager_->GetAudioInputDevices(&devs);
918 if (ret)
919 GetDeviceNames(devs, names);
920
921 return ret;
922}
923
924bool ChannelManager::GetAudioOutputDevices(std::vector<std::string>* names) {
925 names->clear();
926 std::vector<Device> devs;
927 bool ret = device_manager_->GetAudioOutputDevices(&devs);
928 if (ret)
929 GetDeviceNames(devs, names);
930
931 return ret;
932}
933
934bool ChannelManager::GetVideoCaptureDevices(std::vector<std::string>* names) {
935 names->clear();
936 std::vector<Device> devs;
937 bool ret = device_manager_->GetVideoCaptureDevices(&devs);
938 if (ret)
939 GetDeviceNames(devs, names);
940
941 return ret;
942}
943
944void ChannelManager::SetVideoCaptureDeviceMaxFormat(
945 const std::string& usb_id,
946 const VideoFormat& max_format) {
947 device_manager_->SetVideoCaptureDeviceMaxFormat(usb_id, max_format);
948}
949
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000950bool ChannelManager::StartAecDump(rtc::PlatformFile file) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000951 return worker_thread_->Invoke<bool>(
952 Bind(&MediaEngineInterface::StartAecDump, media_engine_.get(), file));
953}
954
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955} // namespace cricket