3 * Copyright 2004 Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #include "talk/session/media/channelmanager.h"
36 #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"
40 #include "talk/media/devices/devicemanager.h"
42 #include "talk/media/sctp/sctpdataengine.h"
44 #include "talk/session/media/soundclip.h"
45 #include "talk/session/media/srtpfilter.h"
46 #include "webrtc/base/bind.h"
47 #include "webrtc/base/common.h"
48 #include "webrtc/base/logging.h"
49 #include "webrtc/base/sigslotrepeater.h"
50 #include "webrtc/base/stringencode.h"
51 #include "webrtc/base/stringutils.h"
56 MSG_VIDEOCAPTURESTATE = 1,
61 static const int kNotSetOutputVolume = -1;
63 struct CaptureStateParams : public rtc::MessageData {
64 CaptureStateParams(cricket::VideoCapturer* c, cricket::CaptureState s)
67 cricket::VideoCapturer* capturer;
68 cricket::CaptureState state;
71 static DataEngineInterface* ConstructDataEngine() {
73 return new HybridDataEngine(new RtpDataEngine(), new SctpDataEngine());
75 return new RtpDataEngine();
79 #if !defined(DISABLE_MEDIA_ENGINE_FACTORY)
80 ChannelManager::ChannelManager(rtc::Thread* worker_thread) {
81 Construct(MediaEngineFactory::Create(),
82 ConstructDataEngine(),
83 cricket::DeviceManagerFactory::Create(),
89 ChannelManager::ChannelManager(MediaEngineInterface* me,
90 DataEngineInterface* dme,
91 DeviceManagerInterface* dm,
93 rtc::Thread* worker_thread) {
94 Construct(me, dme, dm, cm, worker_thread);
97 ChannelManager::ChannelManager(MediaEngineInterface* me,
98 DeviceManagerInterface* dm,
99 rtc::Thread* worker_thread) {
101 ConstructDataEngine(),
103 new CaptureManager(),
107 void ChannelManager::Construct(MediaEngineInterface* me,
108 DataEngineInterface* dme,
109 DeviceManagerInterface* dm,
111 rtc::Thread* worker_thread) {
112 media_engine_.reset(me);
113 data_media_engine_.reset(dme);
114 device_manager_.reset(dm);
115 capture_manager_.reset(cm);
116 initialized_ = false;
117 main_thread_ = rtc::Thread::Current();
118 worker_thread_ = worker_thread;
119 // Get the default audio options from the media engine.
120 audio_options_ = media_engine_->GetAudioOptions();
121 audio_in_device_ = DeviceManagerInterface::kDefaultDeviceName;
122 audio_out_device_ = DeviceManagerInterface::kDefaultDeviceName;
123 audio_delay_offset_ = kDefaultAudioDelayOffset;
124 audio_output_volume_ = kNotSetOutputVolume;
125 local_renderer_ = NULL;
130 // Init the device manager immediately, and set up our default video device.
131 SignalDevicesChange.repeat(device_manager_->SignalDevicesChange);
132 device_manager_->Init();
134 // Camera is started asynchronously, request callbacks when startup
135 // completes to be able to forward them to the rendering manager.
136 media_engine_->SignalVideoCaptureStateChange().connect(
137 this, &ChannelManager::OnVideoCaptureStateChange);
138 capture_manager_->SignalCapturerStateChange.connect(
139 this, &ChannelManager::OnVideoCaptureStateChange);
141 if (worker_thread_ != rtc::Thread::Current()) {
142 // Do not allow invoking calls to other threads on the worker thread.
143 worker_thread_->Invoke<bool>(
144 rtc::Bind(&rtc::Thread::SetAllowBlockingCalls, worker_thread_, false));
148 ChannelManager::~ChannelManager() {
151 // If srtp is initialized (done by the Channel) then we must call
152 // srtp_shutdown to free all crypto kernel lists. But we need to make sure
153 // shutdown always called at the end, after channels are destroyed.
154 // ChannelManager d'tor is always called last, it's safe place to call
160 bool ChannelManager::SetVideoRtxEnabled(bool enable) {
161 // To be safe, this call is only allowed before initialization. Apps like
162 // Flute only have a singleton ChannelManager and we don't want this flag to
163 // be toggled between calls or when there's concurrent calls. We expect apps
164 // to enable this at startup and retain that setting for the lifetime of the
167 enable_rtx_ = enable;
170 LOG(LS_WARNING) << "Cannot toggle rtx after initialization!";
175 int ChannelManager::GetCapabilities() {
176 return media_engine_->GetCapabilities() & device_manager_->GetCapabilities();
179 void ChannelManager::GetSupportedAudioCodecs(
180 std::vector<AudioCodec>* codecs) const {
183 for (std::vector<AudioCodec>::const_iterator it =
184 media_engine_->audio_codecs().begin();
185 it != media_engine_->audio_codecs().end(); ++it) {
186 codecs->push_back(*it);
190 void ChannelManager::GetSupportedAudioRtpHeaderExtensions(
191 RtpHeaderExtensions* ext) const {
192 *ext = media_engine_->audio_rtp_header_extensions();
195 void ChannelManager::GetSupportedVideoCodecs(
196 std::vector<VideoCodec>* codecs) const {
199 std::vector<VideoCodec>::const_iterator it;
200 for (it = media_engine_->video_codecs().begin();
201 it != media_engine_->video_codecs().end(); ++it) {
202 if (!enable_rtx_ && _stricmp(kRtxCodecName, it->name.c_str()) == 0) {
205 codecs->push_back(*it);
209 void ChannelManager::GetSupportedVideoRtpHeaderExtensions(
210 RtpHeaderExtensions* ext) const {
211 *ext = media_engine_->video_rtp_header_extensions();
214 void ChannelManager::GetSupportedDataCodecs(
215 std::vector<DataCodec>* codecs) const {
216 *codecs = data_media_engine_->data_codecs();
219 bool ChannelManager::Init() {
220 ASSERT(!initialized_);
225 ASSERT(worker_thread_ != NULL);
226 if (worker_thread_) {
227 if (media_engine_->Init(worker_thread_)) {
230 // Now that we're initialized, apply any stored preferences. A preferred
231 // device might have been unplugged. In this case, we fallback to the
232 // default device but keep the user preferences. The preferences are
233 // changed only when the Javascript FE changes them.
234 const std::string preferred_audio_in_device = audio_in_device_;
235 const std::string preferred_audio_out_device = audio_out_device_;
236 const std::string preferred_camera_device = camera_device_;
238 if (!device_manager_->GetAudioInputDevice(audio_in_device_, &device)) {
239 LOG(LS_WARNING) << "The preferred microphone '" << audio_in_device_
240 << "' is unavailable. Fall back to the default.";
241 audio_in_device_ = DeviceManagerInterface::kDefaultDeviceName;
243 if (!device_manager_->GetAudioOutputDevice(audio_out_device_, &device)) {
244 LOG(LS_WARNING) << "The preferred speaker '" << audio_out_device_
245 << "' is unavailable. Fall back to the default.";
246 audio_out_device_ = DeviceManagerInterface::kDefaultDeviceName;
248 if (!device_manager_->GetVideoCaptureDevice(camera_device_, &device)) {
249 if (!camera_device_.empty()) {
250 LOG(LS_WARNING) << "The preferred camera '" << camera_device_
251 << "' is unavailable. Fall back to the default.";
253 camera_device_ = DeviceManagerInterface::kDefaultDeviceName;
256 if (!SetAudioOptions(audio_in_device_, audio_out_device_,
257 audio_options_, audio_delay_offset_)) {
258 LOG(LS_WARNING) << "Failed to SetAudioOptions with"
259 << " microphone: " << audio_in_device_
260 << " speaker: " << audio_out_device_
261 << " options: " << audio_options_.ToString()
262 << " delay: " << audio_delay_offset_;
265 // If audio_output_volume_ has been set via SetOutputVolume(), set the
266 // audio output volume of the engine.
267 if (kNotSetOutputVolume != audio_output_volume_ &&
268 !SetOutputVolume(audio_output_volume_)) {
269 LOG(LS_WARNING) << "Failed to SetOutputVolume to "
270 << audio_output_volume_;
272 if (!SetCaptureDevice(camera_device_) && !camera_device_.empty()) {
273 LOG(LS_WARNING) << "Failed to SetCaptureDevice with camera: "
277 // Restore the user preferences.
278 audio_in_device_ = preferred_audio_in_device;
279 audio_out_device_ = preferred_audio_out_device;
280 camera_device_ = preferred_camera_device;
282 // Now apply the default video codec that has been set earlier.
283 if (default_video_encoder_config_.max_codec.id != 0) {
284 SetDefaultVideoEncoderConfig(default_video_encoder_config_);
291 void ChannelManager::Terminate() {
292 ASSERT(initialized_);
296 worker_thread_->Invoke<void>(Bind(&ChannelManager::Terminate_w, this));
297 media_engine_->Terminate();
298 initialized_ = false;
301 void ChannelManager::Terminate_w() {
302 ASSERT(worker_thread_ == rtc::Thread::Current());
303 // Need to destroy the voice/video channels
304 while (!video_channels_.empty()) {
305 DestroyVideoChannel_w(video_channels_.back());
307 while (!voice_channels_.empty()) {
308 DestroyVoiceChannel_w(voice_channels_.back());
310 while (!soundclips_.empty()) {
311 DestroySoundclip_w(soundclips_.back());
313 if (!SetCaptureDevice_w(NULL)) {
314 LOG(LS_WARNING) << "failed to delete video capturer";
318 VoiceChannel* ChannelManager::CreateVoiceChannel(
319 BaseSession* session, const std::string& content_name, bool rtcp) {
320 return worker_thread_->Invoke<VoiceChannel*>(
321 Bind(&ChannelManager::CreateVoiceChannel_w, this,
322 session, content_name, rtcp));
325 VoiceChannel* ChannelManager::CreateVoiceChannel_w(
326 BaseSession* session, const std::string& content_name, bool rtcp) {
327 // This is ok to alloc from a thread other than the worker thread
328 ASSERT(initialized_);
329 VoiceMediaChannel* media_channel = media_engine_->CreateChannel();
330 if (media_channel == NULL)
333 VoiceChannel* voice_channel = new VoiceChannel(
334 worker_thread_, media_engine_.get(), media_channel,
335 session, content_name, rtcp);
336 if (!voice_channel->Init()) {
337 delete voice_channel;
340 voice_channels_.push_back(voice_channel);
341 return voice_channel;
344 void ChannelManager::DestroyVoiceChannel(VoiceChannel* voice_channel) {
346 worker_thread_->Invoke<void>(
347 Bind(&ChannelManager::DestroyVoiceChannel_w, this, voice_channel));
351 void ChannelManager::DestroyVoiceChannel_w(VoiceChannel* voice_channel) {
352 // Destroy voice channel.
353 ASSERT(initialized_);
354 VoiceChannels::iterator it = std::find(voice_channels_.begin(),
355 voice_channels_.end(), voice_channel);
356 ASSERT(it != voice_channels_.end());
357 if (it == voice_channels_.end())
360 voice_channels_.erase(it);
361 delete voice_channel;
364 VideoChannel* ChannelManager::CreateVideoChannel(
365 BaseSession* session, const std::string& content_name, bool rtcp,
366 VoiceChannel* voice_channel) {
367 return worker_thread_->Invoke<VideoChannel*>(
368 Bind(&ChannelManager::CreateVideoChannel_w, this, session,
369 content_name, rtcp, voice_channel));
372 VideoChannel* ChannelManager::CreateVideoChannel_w(
373 BaseSession* session, const std::string& content_name, bool rtcp,
374 VoiceChannel* voice_channel) {
375 // This is ok to alloc from a thread other than the worker thread
376 ASSERT(initialized_);
377 VideoMediaChannel* media_channel =
378 // voice_channel can be NULL in case of NullVoiceEngine.
379 media_engine_->CreateVideoChannel(voice_channel ?
380 voice_channel->media_channel() : NULL);
381 if (media_channel == NULL)
384 VideoChannel* video_channel = new VideoChannel(
385 worker_thread_, media_engine_.get(), media_channel,
386 session, content_name, rtcp, voice_channel);
387 if (!video_channel->Init()) {
388 delete video_channel;
391 video_channels_.push_back(video_channel);
392 return video_channel;
395 void ChannelManager::DestroyVideoChannel(VideoChannel* video_channel) {
397 worker_thread_->Invoke<void>(
398 Bind(&ChannelManager::DestroyVideoChannel_w, this, video_channel));
402 void ChannelManager::DestroyVideoChannel_w(VideoChannel* video_channel) {
403 // Destroy video channel.
404 ASSERT(initialized_);
405 VideoChannels::iterator it = std::find(video_channels_.begin(),
406 video_channels_.end(), video_channel);
407 ASSERT(it != video_channels_.end());
408 if (it == video_channels_.end())
411 video_channels_.erase(it);
412 delete video_channel;
415 DataChannel* ChannelManager::CreateDataChannel(
416 BaseSession* session, const std::string& content_name,
417 bool rtcp, DataChannelType channel_type) {
418 return worker_thread_->Invoke<DataChannel*>(
419 Bind(&ChannelManager::CreateDataChannel_w, this, session, content_name,
420 rtcp, channel_type));
423 DataChannel* ChannelManager::CreateDataChannel_w(
424 BaseSession* session, const std::string& content_name,
425 bool rtcp, DataChannelType data_channel_type) {
426 // This is ok to alloc from a thread other than the worker thread.
427 ASSERT(initialized_);
428 DataMediaChannel* media_channel = data_media_engine_->CreateChannel(
430 if (!media_channel) {
431 LOG(LS_WARNING) << "Failed to create data channel of type "
432 << data_channel_type;
436 DataChannel* data_channel = new DataChannel(
437 worker_thread_, media_channel,
438 session, content_name, rtcp);
439 if (!data_channel->Init()) {
440 LOG(LS_WARNING) << "Failed to init data channel.";
444 data_channels_.push_back(data_channel);
448 void ChannelManager::DestroyDataChannel(DataChannel* data_channel) {
450 worker_thread_->Invoke<void>(
451 Bind(&ChannelManager::DestroyDataChannel_w, this, data_channel));
455 void ChannelManager::DestroyDataChannel_w(DataChannel* data_channel) {
456 // Destroy data channel.
457 ASSERT(initialized_);
458 DataChannels::iterator it = std::find(data_channels_.begin(),
459 data_channels_.end(), data_channel);
460 ASSERT(it != data_channels_.end());
461 if (it == data_channels_.end())
464 data_channels_.erase(it);
468 Soundclip* ChannelManager::CreateSoundclip() {
469 return worker_thread_->Invoke<Soundclip*>(
470 Bind(&ChannelManager::CreateSoundclip_w, this));
473 Soundclip* ChannelManager::CreateSoundclip_w() {
474 ASSERT(initialized_);
475 ASSERT(worker_thread_ == rtc::Thread::Current());
477 SoundclipMedia* soundclip_media = media_engine_->CreateSoundclip();
478 if (!soundclip_media) {
482 Soundclip* soundclip = new Soundclip(worker_thread_, soundclip_media);
483 soundclips_.push_back(soundclip);
487 void ChannelManager::DestroySoundclip(Soundclip* soundclip) {
489 worker_thread_->Invoke<void>(
490 Bind(&ChannelManager::DestroySoundclip_w, this, soundclip));
494 void ChannelManager::DestroySoundclip_w(Soundclip* soundclip) {
495 // Destroy soundclip.
496 ASSERT(initialized_);
497 Soundclips::iterator it = std::find(soundclips_.begin(),
498 soundclips_.end(), soundclip);
499 ASSERT(it != soundclips_.end());
500 if (it == soundclips_.end())
503 soundclips_.erase(it);
507 bool ChannelManager::GetAudioOptions(std::string* in_name,
508 std::string* out_name,
509 AudioOptions* options) {
511 *in_name = audio_in_device_;
513 *out_name = audio_out_device_;
515 *options = audio_options_;
519 bool ChannelManager::SetAudioOptions(const std::string& in_name,
520 const std::string& out_name,
521 const AudioOptions& options) {
522 return SetAudioOptions(in_name, out_name, options, audio_delay_offset_);
525 bool ChannelManager::SetAudioOptions(const std::string& in_name,
526 const std::string& out_name,
527 const AudioOptions& options,
529 // Get device ids from DeviceManager.
530 Device in_dev, out_dev;
531 if (!device_manager_->GetAudioInputDevice(in_name, &in_dev)) {
532 LOG(LS_WARNING) << "Failed to GetAudioInputDevice: " << in_name;
535 if (!device_manager_->GetAudioOutputDevice(out_name, &out_dev)) {
536 LOG(LS_WARNING) << "Failed to GetAudioOutputDevice: " << out_name;
540 // If we're initialized, pass the settings to the media engine.
543 ret = worker_thread_->Invoke<bool>(
544 Bind(&ChannelManager::SetAudioOptions_w, this,
545 options, delay_offset, &in_dev, &out_dev));
548 // If all worked well, save the values for use in GetAudioOptions.
550 audio_options_ = options;
551 audio_in_device_ = in_name;
552 audio_out_device_ = out_name;
553 audio_delay_offset_ = delay_offset;
558 bool ChannelManager::SetAudioOptions_w(
559 const AudioOptions& options, int delay_offset,
560 const Device* in_dev, const Device* out_dev) {
561 ASSERT(worker_thread_ == rtc::Thread::Current());
562 ASSERT(initialized_);
565 bool ret = media_engine_->SetAudioOptions(options);
568 ret = media_engine_->SetAudioDelayOffset(delay_offset);
571 // Set the audio devices
573 ret = media_engine_->SetSoundDevices(in_dev, out_dev);
579 // Sets Engine-specific audio options according to enabled experiments.
580 bool ChannelManager::SetEngineAudioOptions(const AudioOptions& options) {
581 // If we're initialized, pass the settings to the media engine.
584 ret = worker_thread_->Invoke<bool>(
585 Bind(&ChannelManager::SetEngineAudioOptions_w, this, options));
588 // If all worked well, save the audio options.
590 audio_options_ = options;
595 bool ChannelManager::SetEngineAudioOptions_w(const AudioOptions& options) {
596 ASSERT(worker_thread_ == rtc::Thread::Current());
597 ASSERT(initialized_);
599 return media_engine_->SetAudioOptions(options);
602 bool ChannelManager::GetOutputVolume(int* level) {
606 return worker_thread_->Invoke<bool>(
607 Bind(&MediaEngineInterface::GetOutputVolume, media_engine_.get(), level));
610 bool ChannelManager::SetOutputVolume(int level) {
611 bool ret = level >= 0 && level <= 255;
613 ret &= worker_thread_->Invoke<bool>(
614 Bind(&MediaEngineInterface::SetOutputVolume,
615 media_engine_.get(), level));
619 audio_output_volume_ = level;
625 bool ChannelManager::IsSameCapturer(const std::string& capturer_name,
626 VideoCapturer* capturer) {
627 if (capturer == NULL) {
631 if (!device_manager_->GetVideoCaptureDevice(capturer_name, &device)) {
634 return capturer->GetId() == device.id;
637 bool ChannelManager::GetVideoCaptureDevice(Device* device) {
638 std::string device_name;
639 if (!GetCaptureDevice(&device_name)) {
642 return device_manager_->GetVideoCaptureDevice(device_name, device);
645 bool ChannelManager::GetCaptureDevice(std::string* cam_name) {
646 if (camera_device_.empty()) {
647 // Initialize camera_device_ with default.
649 if (!device_manager_->GetVideoCaptureDevice(
650 DeviceManagerInterface::kDefaultDeviceName, &device)) {
651 LOG(LS_WARNING) << "Device manager can't find default camera: " <<
652 DeviceManagerInterface::kDefaultDeviceName;
655 camera_device_ = device.name;
657 *cam_name = camera_device_;
661 bool ChannelManager::SetCaptureDevice(const std::string& cam_name) {
664 if (!device_manager_->GetVideoCaptureDevice(cam_name, &device)) {
665 if (!cam_name.empty()) {
666 LOG(LS_WARNING) << "Device manager can't find camera: " << cam_name;
671 // If we're running, tell the media engine about it.
672 if (initialized_ && ret) {
673 ret = worker_thread_->Invoke<bool>(
674 Bind(&ChannelManager::SetCaptureDevice_w, this, &device));
677 // If everything worked, retain the name of the selected camera.
679 camera_device_ = device.name;
680 } else if (camera_device_.empty()) {
681 // When video option setting fails, we still want camera_device_ to be in a
682 // good state, so we initialize it with default if it's empty.
683 Device default_device;
684 if (!device_manager_->GetVideoCaptureDevice(
685 DeviceManagerInterface::kDefaultDeviceName, &default_device)) {
686 LOG(LS_WARNING) << "Device manager can't find default camera: " <<
687 DeviceManagerInterface::kDefaultDeviceName;
689 camera_device_ = default_device.name;
695 VideoCapturer* ChannelManager::CreateVideoCapturer() {
697 if (!device_manager_->GetVideoCaptureDevice(camera_device_, &device)) {
698 if (!camera_device_.empty()) {
699 LOG(LS_WARNING) << "Device manager can't find camera: " << camera_device_;
703 VideoCapturer* capturer = device_manager_->CreateVideoCapturer(device);
704 if (capturer && default_video_encoder_config_.max_codec.id != 0) {
705 // For now, use the aspect ratio of the default_video_encoder_config_,
706 // which may be different than the native aspect ratio of the start
707 // format the camera may use.
708 capturer->UpdateAspectRatio(
709 default_video_encoder_config_.max_codec.width,
710 default_video_encoder_config_.max_codec.height);
715 VideoCapturer* ChannelManager::CreateScreenCapturer(
716 const ScreencastId& screenid) {
717 return device_manager_->CreateScreenCapturer(screenid);
720 bool ChannelManager::SetCaptureDevice_w(const Device* cam_device) {
721 ASSERT(worker_thread_ == rtc::Thread::Current());
722 ASSERT(initialized_);
725 video_device_name_.clear();
728 video_device_name_ = cam_device->name;
732 bool ChannelManager::SetDefaultVideoEncoderConfig(const VideoEncoderConfig& c) {
735 ret = worker_thread_->Invoke<bool>(
736 Bind(&MediaEngineInterface::SetDefaultVideoEncoderConfig,
737 media_engine_.get(), c));
740 default_video_encoder_config_ = c;
745 bool ChannelManager::SetLocalMonitor(bool enable) {
746 bool ret = initialized_ && worker_thread_->Invoke<bool>(
747 Bind(&MediaEngineInterface::SetLocalMonitor,
748 media_engine_.get(), enable));
750 monitoring_ = enable;
755 void ChannelManager::SetVoiceLogging(int level, const char* filter) {
757 worker_thread_->Invoke<void>(
758 Bind(&MediaEngineInterface::SetVoiceLogging,
759 media_engine_.get(), level, filter));
761 media_engine_->SetVoiceLogging(level, filter);
765 void ChannelManager::SetVideoLogging(int level, const char* filter) {
767 worker_thread_->Invoke<void>(
768 Bind(&MediaEngineInterface::SetVideoLogging,
769 media_engine_.get(), level, filter));
771 media_engine_->SetVideoLogging(level, filter);
775 // TODO(janahan): For now pass this request through the mediaengine to the
776 // voice and video engines to do the real work. Once the capturer refactoring
777 // is done, we will access the capturer using the ssrc (similar to how the
778 // renderer is accessed today) and register with it directly.
779 bool ChannelManager::RegisterVideoProcessor(VideoCapturer* capturer,
780 VideoProcessor* processor) {
781 return initialized_ && worker_thread_->Invoke<bool>(
782 Bind(&ChannelManager::RegisterVideoProcessor_w, this,
783 capturer, processor));
786 bool ChannelManager::RegisterVideoProcessor_w(VideoCapturer* capturer,
787 VideoProcessor* processor) {
788 return capture_manager_->AddVideoProcessor(capturer, processor);
791 bool ChannelManager::UnregisterVideoProcessor(VideoCapturer* capturer,
792 VideoProcessor* processor) {
793 return initialized_ && worker_thread_->Invoke<bool>(
794 Bind(&ChannelManager::UnregisterVideoProcessor_w, this,
795 capturer, processor));
798 bool ChannelManager::UnregisterVideoProcessor_w(VideoCapturer* capturer,
799 VideoProcessor* processor) {
800 return capture_manager_->RemoveVideoProcessor(capturer, processor);
803 bool ChannelManager::RegisterVoiceProcessor(
805 VoiceProcessor* processor,
806 MediaProcessorDirection direction) {
807 return initialized_ && worker_thread_->Invoke<bool>(
808 Bind(&MediaEngineInterface::RegisterVoiceProcessor, media_engine_.get(),
809 ssrc, processor, direction));
812 bool ChannelManager::UnregisterVoiceProcessor(
814 VoiceProcessor* processor,
815 MediaProcessorDirection direction) {
816 return initialized_ && worker_thread_->Invoke<bool>(
817 Bind(&MediaEngineInterface::UnregisterVoiceProcessor,
818 media_engine_.get(), ssrc, processor, direction));
821 // The following are done in the new "CaptureManager" style that
822 // all local video capturers, processors, and managers should move
824 // TODO(pthatcher): Add more of the CaptureManager interface.
825 bool ChannelManager::StartVideoCapture(
826 VideoCapturer* capturer, const VideoFormat& video_format) {
827 return initialized_ && worker_thread_->Invoke<bool>(
828 Bind(&CaptureManager::StartVideoCapture,
829 capture_manager_.get(), capturer, video_format));
832 bool ChannelManager::MuteToBlackThenPause(
833 VideoCapturer* video_capturer, bool muted) {
837 worker_thread_->Invoke<void>(
838 Bind(&VideoCapturer::MuteToBlackThenPause, video_capturer, muted));
842 bool ChannelManager::StopVideoCapture(
843 VideoCapturer* capturer, const VideoFormat& video_format) {
844 return initialized_ && worker_thread_->Invoke<bool>(
845 Bind(&CaptureManager::StopVideoCapture,
846 capture_manager_.get(), capturer, video_format));
849 bool ChannelManager::RestartVideoCapture(
850 VideoCapturer* video_capturer,
851 const VideoFormat& previous_format,
852 const VideoFormat& desired_format,
853 CaptureManager::RestartOptions options) {
854 return initialized_ && worker_thread_->Invoke<bool>(
855 Bind(&CaptureManager::RestartVideoCapture, capture_manager_.get(),
856 video_capturer, previous_format, desired_format, options));
859 bool ChannelManager::AddVideoRenderer(
860 VideoCapturer* capturer, VideoRenderer* renderer) {
861 return initialized_ && worker_thread_->Invoke<bool>(
862 Bind(&CaptureManager::AddVideoRenderer,
863 capture_manager_.get(), capturer, renderer));
866 bool ChannelManager::RemoveVideoRenderer(
867 VideoCapturer* capturer, VideoRenderer* renderer) {
868 return initialized_ && worker_thread_->Invoke<bool>(
869 Bind(&CaptureManager::RemoveVideoRenderer,
870 capture_manager_.get(), capturer, renderer));
873 bool ChannelManager::IsScreencastRunning() const {
874 return initialized_ && worker_thread_->Invoke<bool>(
875 Bind(&ChannelManager::IsScreencastRunning_w, this));
878 bool ChannelManager::IsScreencastRunning_w() const {
879 VideoChannels::const_iterator it = video_channels_.begin();
880 for ( ; it != video_channels_.end(); ++it) {
881 if ((*it) && (*it)->IsScreencasting()) {
888 void ChannelManager::OnVideoCaptureStateChange(VideoCapturer* capturer,
889 CaptureState result) {
890 // TODO(whyuan): Check capturer and signal failure only for camera video, not
892 capturing_ = result == CS_RUNNING;
893 main_thread_->Post(this, MSG_VIDEOCAPTURESTATE,
894 new CaptureStateParams(capturer, result));
897 void ChannelManager::OnMessage(rtc::Message* message) {
898 switch (message->message_id) {
899 case MSG_VIDEOCAPTURESTATE: {
900 CaptureStateParams* data =
901 static_cast<CaptureStateParams*>(message->pdata);
902 SignalVideoCaptureStateChange(data->capturer, data->state);
910 static void GetDeviceNames(const std::vector<Device>& devs,
911 std::vector<std::string>* names) {
913 for (size_t i = 0; i < devs.size(); ++i) {
914 names->push_back(devs[i].name);
918 bool ChannelManager::GetAudioInputDevices(std::vector<std::string>* names) {
920 std::vector<Device> devs;
921 bool ret = device_manager_->GetAudioInputDevices(&devs);
923 GetDeviceNames(devs, names);
928 bool ChannelManager::GetAudioOutputDevices(std::vector<std::string>* names) {
930 std::vector<Device> devs;
931 bool ret = device_manager_->GetAudioOutputDevices(&devs);
933 GetDeviceNames(devs, names);
938 bool ChannelManager::GetVideoCaptureDevices(std::vector<std::string>* names) {
940 std::vector<Device> devs;
941 bool ret = device_manager_->GetVideoCaptureDevices(&devs);
943 GetDeviceNames(devs, names);
948 void ChannelManager::SetVideoCaptureDeviceMaxFormat(
949 const std::string& usb_id,
950 const VideoFormat& max_format) {
951 device_manager_->SetVideoCaptureDeviceMaxFormat(usb_id, max_format);
954 VideoFormat ChannelManager::GetStartCaptureFormat() {
955 return worker_thread_->Invoke<VideoFormat>(
956 Bind(&MediaEngineInterface::GetStartCaptureFormat, media_engine_.get()));
959 bool ChannelManager::StartAecDump(rtc::PlatformFile file) {
960 return worker_thread_->Invoke<bool>(
961 Bind(&MediaEngineInterface::StartAecDump, media_engine_.get(), file));
964 } // namespace cricket