blob: e0a0fb2a704a41875d08415d70a59835e84054d3 [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#ifndef TALK_SESSION_MEDIA_CHANNELMANAGER_H_
29#define TALK_SESSION_MEDIA_CHANNELMANAGER_H_
30
31#include <string>
32#include <vector>
33
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +000034#include "talk/media/base/capturemanager.h"
35#include "talk/media/base/mediaengine.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000036#include "webrtc/p2p/base/session.h"
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +000037#include "talk/session/media/voicechannel.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000038#include "webrtc/base/criticalsection.h"
39#include "webrtc/base/fileutils.h"
40#include "webrtc/base/sigslotrepeater.h"
41#include "webrtc/base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042
Fredrik Solenberg709ed672015-09-15 12:26:33 +020043namespace webrtc {
44class MediaControllerInterface;
45}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046namespace cricket {
47
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048class VoiceChannel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049
50// ChannelManager allows the MediaEngine to run on a separate thread, and takes
51// care of marshalling calls between threads. It also creates and keeps track of
52// voice and video channels; by doing so, it can temporarily pause all the
53// channels when a new audio or video device is chosen. The voice and video
54// channels are stored in separate vectors, to easily allow operations on just
55// voice or just video channels.
56// ChannelManager also allows the application to discover what devices it has
57// using device manager.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000058class ChannelManager : public rtc::MessageHandler,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059 public sigslot::has_slots<> {
60 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061 // For testing purposes. Allows the media engine and data media
62 // engine and dev manager to be mocks. The ChannelManager takes
63 // ownership of these objects.
64 ChannelManager(MediaEngineInterface* me,
65 DataEngineInterface* dme,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066 CaptureManager* cm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000067 rtc::Thread* worker);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068 // Same as above, but gives an easier default DataEngine.
69 ChannelManager(MediaEngineInterface* me,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000070 rtc::Thread* worker);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071 ~ChannelManager();
72
73 // Accessors for the worker thread, allowing it to be set after construction,
74 // but before Init. set_worker_thread will return false if called after Init.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000075 rtc::Thread* worker_thread() const { return worker_thread_; }
76 bool set_worker_thread(rtc::Thread* thread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077 if (initialized_) return false;
78 worker_thread_ = thread;
79 return true;
80 }
81
Fredrik Solenberg709ed672015-09-15 12:26:33 +020082 MediaEngineInterface* media_engine() { return media_engine_.get(); }
83
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084 // Retrieves the list of supported audio & video codec types.
85 // Can be called before starting the media engine.
86 void GetSupportedAudioCodecs(std::vector<AudioCodec>* codecs) const;
87 void GetSupportedAudioRtpHeaderExtensions(RtpHeaderExtensions* ext) const;
88 void GetSupportedVideoCodecs(std::vector<VideoCodec>* codecs) const;
89 void GetSupportedVideoRtpHeaderExtensions(RtpHeaderExtensions* ext) const;
90 void GetSupportedDataCodecs(std::vector<DataCodec>* codecs) const;
91
92 // Indicates whether the media engine is started.
93 bool initialized() const { return initialized_; }
94 // Starts up the media engine.
95 bool Init();
96 // Shuts down the media engine.
97 void Terminate();
98
99 // The operations below all occur on the worker thread.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100 // Creates a voice channel, to be associated with the specified session.
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200101 VoiceChannel* CreateVoiceChannel(
102 webrtc::MediaControllerInterface* media_controller,
deadbeefcbecd352015-09-23 11:50:27 -0700103 TransportController* transport_controller,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200104 const std::string& content_name,
105 bool rtcp,
106 const AudioOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107 // Destroys a voice channel created with the Create API.
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200108 void DestroyVoiceChannel(VoiceChannel* voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109 // Creates a video channel, synced with the specified voice channel, and
110 // associated with the specified session.
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200111 VideoChannel* CreateVideoChannel(
112 webrtc::MediaControllerInterface* media_controller,
deadbeefcbecd352015-09-23 11:50:27 -0700113 TransportController* transport_controller,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200114 const std::string& content_name,
115 bool rtcp,
116 const VideoOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117 // Destroys a video channel created with the Create API.
118 void DestroyVideoChannel(VideoChannel* video_channel);
deadbeefcbecd352015-09-23 11:50:27 -0700119 DataChannel* CreateDataChannel(TransportController* transport_controller,
120 const std::string& content_name,
121 bool rtcp,
122 DataChannelType data_channel_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123 // Destroys a data channel created with the Create API.
124 void DestroyDataChannel(DataChannel* data_channel);
125
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126 // Indicates whether any channels exist.
127 bool has_channels() const {
Fredrik Solenbergccb49e72015-05-19 11:37:56 +0200128 return (!voice_channels_.empty() || !video_channels_.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 }
130
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 bool GetOutputVolume(int* level);
132 bool SetOutputVolume(int level);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config);
134 // RTX will be enabled/disabled in engines that support it. The supporting
135 // engines will start offering an RTX codec. Must be called before Init().
136 bool SetVideoRtxEnabled(bool enable);
137
138 // Starts/stops the local microphone and enables polling of the input level.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 bool capturing() const { return capturing_; }
140
141 // Configures the logging output of the mediaengine(s).
142 void SetVoiceLogging(int level, const char* filter);
143 void SetVideoLogging(int level, const char* filter);
144
hbos@webrtc.org1e642632015-02-25 09:49:41 +0000145 // Gets capturer's supported formats in a thread safe manner
146 std::vector<cricket::VideoFormat> GetSupportedFormats(
147 VideoCapturer* capturer) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 // The following are done in the new "CaptureManager" style that
149 // all local video capturers, processors, and managers should move to.
150 // TODO(pthatcher): Make methods nicer by having start return a handle that
151 // can be used for stop and restart, rather than needing to pass around
152 // formats a a pseudo-handle.
153 bool StartVideoCapture(VideoCapturer* video_capturer,
154 const VideoFormat& video_format);
155 // When muting, produce black frames then pause the camera.
156 // When unmuting, start the camera. Camera starts unmuted.
157 bool MuteToBlackThenPause(VideoCapturer* video_capturer, bool muted);
158 bool StopVideoCapture(VideoCapturer* video_capturer,
159 const VideoFormat& video_format);
160 bool RestartVideoCapture(VideoCapturer* video_capturer,
161 const VideoFormat& previous_format,
162 const VideoFormat& desired_format,
163 CaptureManager::RestartOptions options);
164
165 bool AddVideoRenderer(VideoCapturer* capturer, VideoRenderer* renderer);
166 bool RemoveVideoRenderer(VideoCapturer* capturer, VideoRenderer* renderer);
167 bool IsScreencastRunning() const;
168
169 // The operations below occur on the main thread.
170
wu@webrtc.orga9890802013-12-13 00:21:03 +0000171 // Starts AEC dump using existing file.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000172 bool StartAecDump(rtc::PlatformFile file);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000173
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174 sigslot::signal2<VideoCapturer*, CaptureState> SignalVideoCaptureStateChange;
175
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 protected:
177 // Adds non-transient parameters which can only be changed through the
178 // options store.
solenberg4a3ccad2015-09-24 03:53:08 -0700179 bool SetAudioOptions(const AudioOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180
181 private:
182 typedef std::vector<VoiceChannel*> VoiceChannels;
183 typedef std::vector<VideoChannel*> VideoChannels;
184 typedef std::vector<DataChannel*> DataChannels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185
186 void Construct(MediaEngineInterface* me,
187 DataEngineInterface* dme,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 CaptureManager* cm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000189 rtc::Thread* worker_thread);
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000190 bool InitMediaEngine_w();
hbos@webrtc.org4aef5fe2015-02-25 10:09:05 +0000191 void DestructorDeletes_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192 void Terminate_w();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200193 VoiceChannel* CreateVoiceChannel_w(
194 webrtc::MediaControllerInterface* media_controller,
deadbeefcbecd352015-09-23 11:50:27 -0700195 TransportController* transport_controller,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200196 const std::string& content_name,
197 bool rtcp,
198 const AudioOptions& options);
199 void DestroyVoiceChannel_w(VoiceChannel* voice_channel);
200 VideoChannel* CreateVideoChannel_w(
201 webrtc::MediaControllerInterface* media_controller,
deadbeefcbecd352015-09-23 11:50:27 -0700202 TransportController* transport_controller,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200203 const std::string& content_name,
204 bool rtcp,
205 const VideoOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 void DestroyVideoChannel_w(VideoChannel* video_channel);
deadbeefcbecd352015-09-23 11:50:27 -0700207 DataChannel* CreateDataChannel_w(TransportController* transport_controller,
208 const std::string& content_name,
209 bool rtcp,
210 DataChannelType data_channel_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211 void DestroyDataChannel_w(DataChannel* data_channel);
solenberg4a3ccad2015-09-24 03:53:08 -0700212 bool SetAudioOptions_w(const AudioOptions& options,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000213 const Device* in_dev, const Device* out_dev);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 void OnVideoCaptureStateChange(VideoCapturer* capturer,
215 CaptureState result);
hbos@webrtc.org1e642632015-02-25 09:49:41 +0000216 void GetSupportedFormats_w(
217 VideoCapturer* capturer,
218 std::vector<cricket::VideoFormat>* out_formats) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219 bool IsScreencastRunning_w() const;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000220 virtual void OnMessage(rtc::Message *message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000222 rtc::scoped_ptr<MediaEngineInterface> media_engine_;
223 rtc::scoped_ptr<DataEngineInterface> data_media_engine_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000224 rtc::scoped_ptr<CaptureManager> capture_manager_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225 bool initialized_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000226 rtc::Thread* main_thread_;
227 rtc::Thread* worker_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228
229 VoiceChannels voice_channels_;
230 VideoChannels video_channels_;
231 DataChannels data_channels_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000233 AudioOptions audio_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234 int audio_output_volume_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000235 VideoEncoderConfig default_video_encoder_config_;
236 VideoRenderer* local_renderer_;
237 bool enable_rtx_;
238
239 bool capturing_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240};
241
242} // namespace cricket
243
244#endif // TALK_SESSION_MEDIA_CHANNELMANAGER_H_