blob: 928554360b85da78ff2645bb8e6b01d186bf8a26 [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,
torbjornga81a42f2015-09-23 02:16:58 -0700321 BaseSession* session,
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*>(
torbjornga81a42f2015-09-23 02:16:58 -0700326 Bind(&ChannelManager::CreateVoiceChannel_w,
327 this,
328 media_controller,
329 session,
330 content_name,
331 rtcp,
332 options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333}
334
335VoiceChannel* ChannelManager::CreateVoiceChannel_w(
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200336 webrtc::MediaControllerInterface* media_controller,
torbjornga81a42f2015-09-23 02:16:58 -0700337 BaseSession* session,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200338 const std::string& content_name,
339 bool rtcp,
340 const AudioOptions& options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341 ASSERT(initialized_);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200342 ASSERT(worker_thread_ == rtc::Thread::Current());
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200343 ASSERT(nullptr != media_controller);
344 VoiceMediaChannel* media_channel =
345 media_engine_->CreateChannel(media_controller->call_w(), options);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200346 if (!media_channel)
347 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000348
torbjornga81a42f2015-09-23 02:16:58 -0700349 VoiceChannel* voice_channel = new VoiceChannel(
350 worker_thread_, media_engine_.get(), media_channel,
351 session, content_name, rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352 if (!voice_channel->Init()) {
353 delete voice_channel;
Jelena Marusicc28a8962015-05-29 15:05:44 +0200354 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355 }
356 voice_channels_.push_back(voice_channel);
357 return voice_channel;
358}
359
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200360void ChannelManager::DestroyVoiceChannel(VoiceChannel* voice_channel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361 if (voice_channel) {
362 worker_thread_->Invoke<void>(
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200363 Bind(&ChannelManager::DestroyVoiceChannel_w, this, voice_channel));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364 }
365}
366
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200367void ChannelManager::DestroyVoiceChannel_w(VoiceChannel* voice_channel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368 // Destroy voice channel.
369 ASSERT(initialized_);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200370 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371 VoiceChannels::iterator it = std::find(voice_channels_.begin(),
372 voice_channels_.end(), voice_channel);
373 ASSERT(it != voice_channels_.end());
374 if (it == voice_channels_.end())
375 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000376 voice_channels_.erase(it);
377 delete voice_channel;
378}
379
380VideoChannel* ChannelManager::CreateVideoChannel(
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200381 webrtc::MediaControllerInterface* media_controller,
torbjornga81a42f2015-09-23 02:16:58 -0700382 BaseSession* session,
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000383 const std::string& content_name,
384 bool rtcp,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200385 const VideoOptions& options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386 return worker_thread_->Invoke<VideoChannel*>(
torbjornga81a42f2015-09-23 02:16:58 -0700387 Bind(&ChannelManager::CreateVideoChannel_w,
388 this,
389 media_controller,
390 session,
391 content_name,
392 rtcp,
393 options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000394}
395
396VideoChannel* ChannelManager::CreateVideoChannel_w(
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200397 webrtc::MediaControllerInterface* media_controller,
torbjornga81a42f2015-09-23 02:16:58 -0700398 BaseSession* session,
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000399 const std::string& content_name,
400 bool rtcp,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200401 const VideoOptions& options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402 ASSERT(initialized_);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200403 ASSERT(worker_thread_ == rtc::Thread::Current());
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200404 ASSERT(nullptr != media_controller);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405 VideoMediaChannel* media_channel =
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200406 media_engine_->CreateVideoChannel(media_controller->call_w(), options);
torbjornga81a42f2015-09-23 02:16:58 -0700407 if (media_channel == NULL)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000408 return NULL;
409
410 VideoChannel* video_channel = new VideoChannel(
torbjornga81a42f2015-09-23 02:16:58 -0700411 worker_thread_, media_channel,
412 session, content_name, rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413 if (!video_channel->Init()) {
414 delete video_channel;
415 return NULL;
416 }
417 video_channels_.push_back(video_channel);
418 return video_channel;
419}
420
421void ChannelManager::DestroyVideoChannel(VideoChannel* video_channel) {
422 if (video_channel) {
423 worker_thread_->Invoke<void>(
424 Bind(&ChannelManager::DestroyVideoChannel_w, this, video_channel));
425 }
426}
427
428void ChannelManager::DestroyVideoChannel_w(VideoChannel* video_channel) {
429 // Destroy video channel.
430 ASSERT(initialized_);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200431 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000432 VideoChannels::iterator it = std::find(video_channels_.begin(),
433 video_channels_.end(), video_channel);
434 ASSERT(it != video_channels_.end());
435 if (it == video_channels_.end())
436 return;
437
438 video_channels_.erase(it);
439 delete video_channel;
440}
441
442DataChannel* ChannelManager::CreateDataChannel(
torbjornga81a42f2015-09-23 02:16:58 -0700443 BaseSession* session, const std::string& content_name,
444 bool rtcp, DataChannelType channel_type) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000445 return worker_thread_->Invoke<DataChannel*>(
torbjornga81a42f2015-09-23 02:16:58 -0700446 Bind(&ChannelManager::CreateDataChannel_w, this, session, content_name,
447 rtcp, channel_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448}
449
450DataChannel* ChannelManager::CreateDataChannel_w(
torbjornga81a42f2015-09-23 02:16:58 -0700451 BaseSession* session, const std::string& content_name,
452 bool rtcp, DataChannelType data_channel_type) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000453 // This is ok to alloc from a thread other than the worker thread.
454 ASSERT(initialized_);
455 DataMediaChannel* media_channel = data_media_engine_->CreateChannel(
456 data_channel_type);
457 if (!media_channel) {
458 LOG(LS_WARNING) << "Failed to create data channel of type "
459 << data_channel_type;
460 return NULL;
461 }
462
463 DataChannel* data_channel = new DataChannel(
torbjornga81a42f2015-09-23 02:16:58 -0700464 worker_thread_, media_channel,
465 session, content_name, rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466 if (!data_channel->Init()) {
467 LOG(LS_WARNING) << "Failed to init data channel.";
468 delete data_channel;
469 return NULL;
470 }
471 data_channels_.push_back(data_channel);
472 return data_channel;
473}
474
475void ChannelManager::DestroyDataChannel(DataChannel* data_channel) {
476 if (data_channel) {
477 worker_thread_->Invoke<void>(
478 Bind(&ChannelManager::DestroyDataChannel_w, this, data_channel));
479 }
480}
481
482void ChannelManager::DestroyDataChannel_w(DataChannel* data_channel) {
483 // Destroy data channel.
484 ASSERT(initialized_);
485 DataChannels::iterator it = std::find(data_channels_.begin(),
486 data_channels_.end(), data_channel);
487 ASSERT(it != data_channels_.end());
488 if (it == data_channels_.end())
489 return;
490
491 data_channels_.erase(it);
492 delete data_channel;
493}
494
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495bool ChannelManager::GetAudioOptions(std::string* in_name,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000496 std::string* out_name,
497 AudioOptions* options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498 if (in_name)
499 *in_name = audio_in_device_;
500 if (out_name)
501 *out_name = audio_out_device_;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000502 if (options)
503 *options = audio_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504 return true;
505}
506
507bool ChannelManager::SetAudioOptions(const std::string& in_name,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000508 const std::string& out_name,
509 const AudioOptions& options) {
510 return SetAudioOptions(in_name, out_name, options, audio_delay_offset_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511}
512
513bool ChannelManager::SetAudioOptions(const std::string& in_name,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000514 const std::string& out_name,
515 const AudioOptions& options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000516 int delay_offset) {
517 // Get device ids from DeviceManager.
518 Device in_dev, out_dev;
519 if (!device_manager_->GetAudioInputDevice(in_name, &in_dev)) {
520 LOG(LS_WARNING) << "Failed to GetAudioInputDevice: " << in_name;
521 return false;
522 }
523 if (!device_manager_->GetAudioOutputDevice(out_name, &out_dev)) {
524 LOG(LS_WARNING) << "Failed to GetAudioOutputDevice: " << out_name;
525 return false;
526 }
527
528 // If we're initialized, pass the settings to the media engine.
529 bool ret = true;
530 if (initialized_) {
531 ret = worker_thread_->Invoke<bool>(
532 Bind(&ChannelManager::SetAudioOptions_w, this,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000533 options, delay_offset, &in_dev, &out_dev));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534 }
535
536 // If all worked well, save the values for use in GetAudioOptions.
537 if (ret) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000538 audio_options_ = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539 audio_in_device_ = in_name;
540 audio_out_device_ = out_name;
541 audio_delay_offset_ = delay_offset;
542 }
543 return ret;
544}
545
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000546bool ChannelManager::SetAudioOptions_w(
547 const AudioOptions& options, int delay_offset,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548 const Device* in_dev, const Device* out_dev) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000549 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550 ASSERT(initialized_);
551
552 // Set audio options
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000553 bool ret = media_engine_->SetAudioOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554
555 if (ret) {
556 ret = media_engine_->SetAudioDelayOffset(delay_offset);
557 }
558
559 // Set the audio devices
560 if (ret) {
561 ret = media_engine_->SetSoundDevices(in_dev, out_dev);
562 }
563
564 return ret;
565}
566
567bool ChannelManager::GetOutputVolume(int* level) {
568 if (!initialized_) {
569 return false;
570 }
571 return worker_thread_->Invoke<bool>(
572 Bind(&MediaEngineInterface::GetOutputVolume, media_engine_.get(), level));
573}
574
575bool ChannelManager::SetOutputVolume(int level) {
576 bool ret = level >= 0 && level <= 255;
577 if (initialized_) {
578 ret &= worker_thread_->Invoke<bool>(
579 Bind(&MediaEngineInterface::SetOutputVolume,
580 media_engine_.get(), level));
581 }
582
583 if (ret) {
584 audio_output_volume_ = level;
585 }
586
587 return ret;
588}
589
590bool ChannelManager::IsSameCapturer(const std::string& capturer_name,
591 VideoCapturer* capturer) {
592 if (capturer == NULL) {
593 return false;
594 }
595 Device device;
596 if (!device_manager_->GetVideoCaptureDevice(capturer_name, &device)) {
597 return false;
598 }
599 return capturer->GetId() == device.id;
600}
601
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000602bool ChannelManager::GetVideoCaptureDevice(Device* device) {
603 std::string device_name;
604 if (!GetCaptureDevice(&device_name)) {
605 return false;
606 }
607 return device_manager_->GetVideoCaptureDevice(device_name, device);
608}
609
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610bool ChannelManager::GetCaptureDevice(std::string* cam_name) {
611 if (camera_device_.empty()) {
612 // Initialize camera_device_ with default.
613 Device device;
614 if (!device_manager_->GetVideoCaptureDevice(
615 DeviceManagerInterface::kDefaultDeviceName, &device)) {
616 LOG(LS_WARNING) << "Device manager can't find default camera: " <<
617 DeviceManagerInterface::kDefaultDeviceName;
618 return false;
619 }
620 camera_device_ = device.name;
621 }
622 *cam_name = camera_device_;
623 return true;
624}
625
626bool ChannelManager::SetCaptureDevice(const std::string& cam_name) {
627 Device device;
628 bool ret = true;
629 if (!device_manager_->GetVideoCaptureDevice(cam_name, &device)) {
630 if (!cam_name.empty()) {
631 LOG(LS_WARNING) << "Device manager can't find camera: " << cam_name;
632 }
633 ret = false;
634 }
635
636 // If we're running, tell the media engine about it.
637 if (initialized_ && ret) {
638 ret = worker_thread_->Invoke<bool>(
639 Bind(&ChannelManager::SetCaptureDevice_w, this, &device));
640 }
641
642 // If everything worked, retain the name of the selected camera.
643 if (ret) {
644 camera_device_ = device.name;
645 } else if (camera_device_.empty()) {
646 // When video option setting fails, we still want camera_device_ to be in a
647 // good state, so we initialize it with default if it's empty.
648 Device default_device;
649 if (!device_manager_->GetVideoCaptureDevice(
650 DeviceManagerInterface::kDefaultDeviceName, &default_device)) {
651 LOG(LS_WARNING) << "Device manager can't find default camera: " <<
652 DeviceManagerInterface::kDefaultDeviceName;
653 }
654 camera_device_ = default_device.name;
655 }
656
657 return ret;
658}
659
660VideoCapturer* ChannelManager::CreateVideoCapturer() {
661 Device device;
662 if (!device_manager_->GetVideoCaptureDevice(camera_device_, &device)) {
663 if (!camera_device_.empty()) {
664 LOG(LS_WARNING) << "Device manager can't find camera: " << camera_device_;
665 }
666 return NULL;
667 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000668 VideoCapturer* capturer = device_manager_->CreateVideoCapturer(device);
669 if (capturer && default_video_encoder_config_.max_codec.id != 0) {
670 // For now, use the aspect ratio of the default_video_encoder_config_,
671 // which may be different than the native aspect ratio of the start
672 // format the camera may use.
673 capturer->UpdateAspectRatio(
674 default_video_encoder_config_.max_codec.width,
675 default_video_encoder_config_.max_codec.height);
676 }
677 return capturer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000678}
679
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000680VideoCapturer* ChannelManager::CreateScreenCapturer(
681 const ScreencastId& screenid) {
682 return device_manager_->CreateScreenCapturer(screenid);
683}
684
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000685bool ChannelManager::SetCaptureDevice_w(const Device* cam_device) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000686 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000687 ASSERT(initialized_);
688
689 if (!cam_device) {
690 video_device_name_.clear();
691 return true;
692 }
693 video_device_name_ = cam_device->name;
694 return true;
695}
696
697bool ChannelManager::SetDefaultVideoEncoderConfig(const VideoEncoderConfig& c) {
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000698 bool ret = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000699 if (initialized_) {
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000700 ret = worker_thread_->Invoke<bool>(
701 Bind(&MediaEngineInterface::SetDefaultVideoEncoderConfig,
702 media_engine_.get(), c));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000703 }
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000704 if (ret) {
705 default_video_encoder_config_ = c;
706 }
707 return ret;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708}
709
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710void ChannelManager::SetVoiceLogging(int level, const char* filter) {
711 if (initialized_) {
712 worker_thread_->Invoke<void>(
713 Bind(&MediaEngineInterface::SetVoiceLogging,
714 media_engine_.get(), level, filter));
715 } else {
716 media_engine_->SetVoiceLogging(level, filter);
717 }
718}
719
720void ChannelManager::SetVideoLogging(int level, const char* filter) {
721 if (initialized_) {
722 worker_thread_->Invoke<void>(
723 Bind(&MediaEngineInterface::SetVideoLogging,
724 media_engine_.get(), level, filter));
725 } else {
726 media_engine_->SetVideoLogging(level, filter);
727 }
728}
729
hbos@webrtc.org1e642632015-02-25 09:49:41 +0000730std::vector<cricket::VideoFormat> ChannelManager::GetSupportedFormats(
731 VideoCapturer* capturer) const {
732 ASSERT(capturer != NULL);
733 std::vector<VideoFormat> formats;
734 worker_thread_->Invoke<void>(rtc::Bind(&ChannelManager::GetSupportedFormats_w,
735 this, capturer, &formats));
736 return formats;
737}
738
739void ChannelManager::GetSupportedFormats_w(
740 VideoCapturer* capturer,
741 std::vector<cricket::VideoFormat>* out_formats) const {
742 const std::vector<VideoFormat>* formats = capturer->GetSupportedFormats();
743 if (formats != NULL)
744 *out_formats = *formats;
745}
746
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747// The following are done in the new "CaptureManager" style that
748// all local video capturers, processors, and managers should move
749// to.
750// TODO(pthatcher): Add more of the CaptureManager interface.
751bool ChannelManager::StartVideoCapture(
752 VideoCapturer* capturer, const VideoFormat& video_format) {
753 return initialized_ && worker_thread_->Invoke<bool>(
754 Bind(&CaptureManager::StartVideoCapture,
755 capture_manager_.get(), capturer, video_format));
756}
757
758bool ChannelManager::MuteToBlackThenPause(
759 VideoCapturer* video_capturer, bool muted) {
760 if (!initialized_) {
761 return false;
762 }
763 worker_thread_->Invoke<void>(
764 Bind(&VideoCapturer::MuteToBlackThenPause, video_capturer, muted));
765 return true;
766}
767
768bool ChannelManager::StopVideoCapture(
769 VideoCapturer* capturer, const VideoFormat& video_format) {
770 return initialized_ && worker_thread_->Invoke<bool>(
771 Bind(&CaptureManager::StopVideoCapture,
772 capture_manager_.get(), capturer, video_format));
773}
774
775bool ChannelManager::RestartVideoCapture(
776 VideoCapturer* video_capturer,
777 const VideoFormat& previous_format,
778 const VideoFormat& desired_format,
779 CaptureManager::RestartOptions options) {
780 return initialized_ && worker_thread_->Invoke<bool>(
781 Bind(&CaptureManager::RestartVideoCapture, capture_manager_.get(),
782 video_capturer, previous_format, desired_format, options));
783}
784
785bool ChannelManager::AddVideoRenderer(
786 VideoCapturer* capturer, VideoRenderer* renderer) {
787 return initialized_ && worker_thread_->Invoke<bool>(
788 Bind(&CaptureManager::AddVideoRenderer,
789 capture_manager_.get(), capturer, renderer));
790}
791
792bool ChannelManager::RemoveVideoRenderer(
793 VideoCapturer* capturer, VideoRenderer* renderer) {
794 return initialized_ && worker_thread_->Invoke<bool>(
795 Bind(&CaptureManager::RemoveVideoRenderer,
796 capture_manager_.get(), capturer, renderer));
797}
798
799bool ChannelManager::IsScreencastRunning() const {
800 return initialized_ && worker_thread_->Invoke<bool>(
801 Bind(&ChannelManager::IsScreencastRunning_w, this));
802}
803
804bool ChannelManager::IsScreencastRunning_w() const {
805 VideoChannels::const_iterator it = video_channels_.begin();
806 for ( ; it != video_channels_.end(); ++it) {
807 if ((*it) && (*it)->IsScreencasting()) {
808 return true;
809 }
810 }
811 return false;
812}
813
814void ChannelManager::OnVideoCaptureStateChange(VideoCapturer* capturer,
815 CaptureState result) {
816 // TODO(whyuan): Check capturer and signal failure only for camera video, not
817 // screencast.
818 capturing_ = result == CS_RUNNING;
819 main_thread_->Post(this, MSG_VIDEOCAPTURESTATE,
820 new CaptureStateParams(capturer, result));
821}
822
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000823void ChannelManager::OnMessage(rtc::Message* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824 switch (message->message_id) {
825 case MSG_VIDEOCAPTURESTATE: {
826 CaptureStateParams* data =
827 static_cast<CaptureStateParams*>(message->pdata);
828 SignalVideoCaptureStateChange(data->capturer, data->state);
829 delete data;
830 break;
831 }
832 }
833}
834
835
836static void GetDeviceNames(const std::vector<Device>& devs,
837 std::vector<std::string>* names) {
838 names->clear();
839 for (size_t i = 0; i < devs.size(); ++i) {
840 names->push_back(devs[i].name);
841 }
842}
843
844bool ChannelManager::GetAudioInputDevices(std::vector<std::string>* names) {
845 names->clear();
846 std::vector<Device> devs;
847 bool ret = device_manager_->GetAudioInputDevices(&devs);
848 if (ret)
849 GetDeviceNames(devs, names);
850
851 return ret;
852}
853
854bool ChannelManager::GetAudioOutputDevices(std::vector<std::string>* names) {
855 names->clear();
856 std::vector<Device> devs;
857 bool ret = device_manager_->GetAudioOutputDevices(&devs);
858 if (ret)
859 GetDeviceNames(devs, names);
860
861 return ret;
862}
863
864bool ChannelManager::GetVideoCaptureDevices(std::vector<std::string>* names) {
865 names->clear();
866 std::vector<Device> devs;
867 bool ret = device_manager_->GetVideoCaptureDevices(&devs);
868 if (ret)
869 GetDeviceNames(devs, names);
870
871 return ret;
872}
873
874void ChannelManager::SetVideoCaptureDeviceMaxFormat(
875 const std::string& usb_id,
876 const VideoFormat& max_format) {
877 device_manager_->SetVideoCaptureDeviceMaxFormat(usb_id, max_format);
878}
879
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000880bool ChannelManager::StartAecDump(rtc::PlatformFile file) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000881 return worker_thread_->Invoke<bool>(
882 Bind(&MediaEngineInterface::StartAecDump, media_engine_.get(), file));
883}
884
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885} // namespace cricket