blob: 27f874b59d6566f8fb142fa48217510fae23d3f2 [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
43namespace cricket {
44
henrike@webrtc.org0481f152014-08-19 14:56:59 +000045const int kDefaultAudioDelayOffset = 0;
46
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047class Soundclip;
48class VideoProcessor;
49class VoiceChannel;
50class VoiceProcessor;
51
52// ChannelManager allows the MediaEngine to run on a separate thread, and takes
53// care of marshalling calls between threads. It also creates and keeps track of
54// voice and video channels; by doing so, it can temporarily pause all the
55// channels when a new audio or video device is chosen. The voice and video
56// channels are stored in separate vectors, to easily allow operations on just
57// voice or just video channels.
58// ChannelManager also allows the application to discover what devices it has
59// using device manager.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000060class ChannelManager : public rtc::MessageHandler,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061 public sigslot::has_slots<> {
62 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063 // For testing purposes. Allows the media engine and data media
64 // engine and dev manager to be mocks. The ChannelManager takes
65 // ownership of these objects.
66 ChannelManager(MediaEngineInterface* me,
67 DataEngineInterface* dme,
68 DeviceManagerInterface* dm,
69 CaptureManager* cm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000070 rtc::Thread* worker);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071 // Same as above, but gives an easier default DataEngine.
72 ChannelManager(MediaEngineInterface* me,
73 DeviceManagerInterface* dm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000074 rtc::Thread* worker);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 ~ChannelManager();
76
77 // Accessors for the worker thread, allowing it to be set after construction,
78 // but before Init. set_worker_thread will return false if called after Init.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000079 rtc::Thread* worker_thread() const { return worker_thread_; }
80 bool set_worker_thread(rtc::Thread* thread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081 if (initialized_) return false;
82 worker_thread_ = thread;
83 return true;
84 }
85
86 // Gets capabilities. Can be called prior to starting the media engine.
87 int GetCapabilities();
88
89 // Retrieves the list of supported audio & video codec types.
90 // Can be called before starting the media engine.
91 void GetSupportedAudioCodecs(std::vector<AudioCodec>* codecs) const;
92 void GetSupportedAudioRtpHeaderExtensions(RtpHeaderExtensions* ext) const;
93 void GetSupportedVideoCodecs(std::vector<VideoCodec>* codecs) const;
94 void GetSupportedVideoRtpHeaderExtensions(RtpHeaderExtensions* ext) const;
95 void GetSupportedDataCodecs(std::vector<DataCodec>* codecs) const;
96
97 // Indicates whether the media engine is started.
98 bool initialized() const { return initialized_; }
99 // Starts up the media engine.
100 bool Init();
101 // Shuts down the media engine.
102 void Terminate();
103
104 // The operations below all occur on the worker thread.
105
106 // Creates a voice channel, to be associated with the specified session.
107 VoiceChannel* CreateVoiceChannel(
108 BaseSession* session, const std::string& content_name, bool rtcp);
109 // Destroys a voice channel created with the Create API.
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200110 void DestroyVoiceChannel(VoiceChannel* voice_channel,
111 VideoChannel* video_channel);
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000112 // TODO(pbos): Remove as soon as all call sites specify VideoOptions.
113 VideoChannel* CreateVideoChannel(BaseSession* session,
114 const std::string& content_name,
115 bool rtcp,
116 VoiceChannel* voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117 // Creates a video channel, synced with the specified voice channel, and
118 // associated with the specified session.
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000119 VideoChannel* CreateVideoChannel(BaseSession* session,
120 const std::string& content_name,
121 bool rtcp,
122 const VideoOptions& options,
123 VoiceChannel* voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124 // Destroys a video channel created with the Create API.
125 void DestroyVideoChannel(VideoChannel* video_channel);
126 DataChannel* CreateDataChannel(
127 BaseSession* session, const std::string& content_name,
128 bool rtcp, DataChannelType data_channel_type);
129 // Destroys a data channel created with the Create API.
130 void DestroyDataChannel(DataChannel* data_channel);
131
132 // Creates a soundclip.
133 Soundclip* CreateSoundclip();
134 // Destroys a soundclip created with the Create API.
135 void DestroySoundclip(Soundclip* soundclip);
136
137 // Indicates whether any channels exist.
138 bool has_channels() const {
139 return (!voice_channels_.empty() || !video_channels_.empty() ||
140 !soundclips_.empty());
141 }
142
143 // Configures the audio and video devices. A null pointer can be passed to
144 // GetAudioOptions() for any parameter of no interest.
145 bool GetAudioOptions(std::string* wave_in_device,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000146 std::string* wave_out_device,
147 AudioOptions* options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 bool SetAudioOptions(const std::string& wave_in_device,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000149 const std::string& wave_out_device,
150 const AudioOptions& options);
buildbot@webrtc.org88d9fa62014-06-16 14:11:32 +0000151 // Sets Engine-specific audio options according to enabled experiments.
152 bool SetEngineAudioOptions(const AudioOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 bool GetOutputVolume(int* level);
154 bool SetOutputVolume(int level);
155 bool IsSameCapturer(const std::string& capturer_name,
156 VideoCapturer* capturer);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000157 // TODO(noahric): Nearly everything called "device" in this API is actually a
158 // device name, so this should really be GetCaptureDeviceName, and the
159 // next method should be GetCaptureDevice.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160 bool GetCaptureDevice(std::string* cam_device);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000161 // Gets the current capture Device.
162 bool GetVideoCaptureDevice(Device* device);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 // Create capturer based on what has been set in SetCaptureDevice().
164 VideoCapturer* CreateVideoCapturer();
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000165 // Create capturer from a screen.
166 VideoCapturer* CreateScreenCapturer(const ScreencastId& screenid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 bool SetCaptureDevice(const std::string& cam_device);
168 bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config);
169 // RTX will be enabled/disabled in engines that support it. The supporting
170 // engines will start offering an RTX codec. Must be called before Init().
171 bool SetVideoRtxEnabled(bool enable);
172
173 // Starts/stops the local microphone and enables polling of the input level.
174 bool SetLocalMonitor(bool enable);
175 bool monitoring() const { return monitoring_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 bool capturing() const { return capturing_; }
177
178 // Configures the logging output of the mediaengine(s).
179 void SetVoiceLogging(int level, const char* filter);
180 void SetVideoLogging(int level, const char* filter);
181
hbos@webrtc.org1e642632015-02-25 09:49:41 +0000182 // Gets capturer's supported formats in a thread safe manner
183 std::vector<cricket::VideoFormat> GetSupportedFormats(
184 VideoCapturer* capturer) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 // The channel manager handles the Tx side for Video processing,
186 // as well as Tx and Rx side for Voice processing.
187 // (The Rx Video processing will go throug the simplerenderingmanager,
188 // to be implemented).
189 bool RegisterVideoProcessor(VideoCapturer* capturer,
190 VideoProcessor* processor);
191 bool UnregisterVideoProcessor(VideoCapturer* capturer,
192 VideoProcessor* processor);
193 bool RegisterVoiceProcessor(uint32 ssrc,
194 VoiceProcessor* processor,
195 MediaProcessorDirection direction);
196 bool UnregisterVoiceProcessor(uint32 ssrc,
197 VoiceProcessor* processor,
198 MediaProcessorDirection direction);
199 // The following are done in the new "CaptureManager" style that
200 // all local video capturers, processors, and managers should move to.
201 // TODO(pthatcher): Make methods nicer by having start return a handle that
202 // can be used for stop and restart, rather than needing to pass around
203 // formats a a pseudo-handle.
204 bool StartVideoCapture(VideoCapturer* video_capturer,
205 const VideoFormat& video_format);
206 // When muting, produce black frames then pause the camera.
207 // When unmuting, start the camera. Camera starts unmuted.
208 bool MuteToBlackThenPause(VideoCapturer* video_capturer, bool muted);
209 bool StopVideoCapture(VideoCapturer* video_capturer,
210 const VideoFormat& video_format);
211 bool RestartVideoCapture(VideoCapturer* video_capturer,
212 const VideoFormat& previous_format,
213 const VideoFormat& desired_format,
214 CaptureManager::RestartOptions options);
215
216 bool AddVideoRenderer(VideoCapturer* capturer, VideoRenderer* renderer);
217 bool RemoveVideoRenderer(VideoCapturer* capturer, VideoRenderer* renderer);
218 bool IsScreencastRunning() const;
219
220 // The operations below occur on the main thread.
221
222 bool GetAudioInputDevices(std::vector<std::string>* names);
223 bool GetAudioOutputDevices(std::vector<std::string>* names);
224 bool GetVideoCaptureDevices(std::vector<std::string>* names);
225 void SetVideoCaptureDeviceMaxFormat(const std::string& usb_id,
226 const VideoFormat& max_format);
227
wu@webrtc.orga9890802013-12-13 00:21:03 +0000228 // Starts AEC dump using existing file.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000229 bool StartAecDump(rtc::PlatformFile file);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000230
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231 sigslot::repeater0<> SignalDevicesChange;
232 sigslot::signal2<VideoCapturer*, CaptureState> SignalVideoCaptureStateChange;
233
234 // Returns the current selected device. Note: Subtly different from
235 // GetCaptureDevice(). See member video_device_ for more details.
236 // This API is mainly a hook used by unittests.
237 const std::string& video_device_name() const { return video_device_name_; }
238
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000239
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240 protected:
241 // Adds non-transient parameters which can only be changed through the
242 // options store.
243 bool SetAudioOptions(const std::string& wave_in_device,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000244 const std::string& wave_out_device,
245 const AudioOptions& options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000246 int delay_offset);
247 int audio_delay_offset() const { return audio_delay_offset_; }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000248 // This is here so that ChannelManager subclasses can set the video
249 // capturer factories to use.
250 DeviceManagerInterface* device_manager() { return device_manager_.get(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251
252 private:
253 typedef std::vector<VoiceChannel*> VoiceChannels;
254 typedef std::vector<VideoChannel*> VideoChannels;
255 typedef std::vector<DataChannel*> DataChannels;
256 typedef std::vector<Soundclip*> Soundclips;
257
258 void Construct(MediaEngineInterface* me,
259 DataEngineInterface* dme,
260 DeviceManagerInterface* dm,
261 CaptureManager* cm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000262 rtc::Thread* worker_thread);
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000263 bool InitMediaEngine_w();
hbos@webrtc.org4aef5fe2015-02-25 10:09:05 +0000264 void DestructorDeletes_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265 void Terminate_w();
266 VoiceChannel* CreateVoiceChannel_w(
267 BaseSession* session, const std::string& content_name, bool rtcp);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200268 void DestroyVoiceChannel_w(VoiceChannel* voice_channel,
269 VideoChannel* video_channel);
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000270 VideoChannel* CreateVideoChannel_w(BaseSession* session,
271 const std::string& content_name,
272 bool rtcp,
273 const VideoOptions& options,
274 VoiceChannel* voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275 void DestroyVideoChannel_w(VideoChannel* video_channel);
276 DataChannel* CreateDataChannel_w(
277 BaseSession* session, const std::string& content_name,
278 bool rtcp, DataChannelType data_channel_type);
279 void DestroyDataChannel_w(DataChannel* data_channel);
280 Soundclip* CreateSoundclip_w();
281 void DestroySoundclip_w(Soundclip* soundclip);
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000282 bool SetAudioOptions_w(const AudioOptions& options, int delay_offset,
283 const Device* in_dev, const Device* out_dev);
buildbot@webrtc.org88d9fa62014-06-16 14:11:32 +0000284 bool SetEngineAudioOptions_w(const AudioOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285 bool SetCaptureDevice_w(const Device* cam_device);
286 void OnVideoCaptureStateChange(VideoCapturer* capturer,
287 CaptureState result);
hbos@webrtc.org1e642632015-02-25 09:49:41 +0000288 void GetSupportedFormats_w(
289 VideoCapturer* capturer,
290 std::vector<cricket::VideoFormat>* out_formats) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 bool RegisterVideoProcessor_w(VideoCapturer* capturer,
292 VideoProcessor* processor);
293 bool UnregisterVideoProcessor_w(VideoCapturer* capturer,
294 VideoProcessor* processor);
295 bool IsScreencastRunning_w() const;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000296 virtual void OnMessage(rtc::Message *message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000298 rtc::scoped_ptr<MediaEngineInterface> media_engine_;
299 rtc::scoped_ptr<DataEngineInterface> data_media_engine_;
300 rtc::scoped_ptr<DeviceManagerInterface> device_manager_;
301 rtc::scoped_ptr<CaptureManager> capture_manager_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000302 bool initialized_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000303 rtc::Thread* main_thread_;
304 rtc::Thread* worker_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305
306 VoiceChannels voice_channels_;
307 VideoChannels video_channels_;
308 DataChannels data_channels_;
309 Soundclips soundclips_;
310
311 std::string audio_in_device_;
312 std::string audio_out_device_;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000313 AudioOptions audio_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000314 int audio_delay_offset_;
315 int audio_output_volume_;
316 std::string camera_device_;
317 VideoEncoderConfig default_video_encoder_config_;
318 VideoRenderer* local_renderer_;
319 bool enable_rtx_;
320
321 bool capturing_;
322 bool monitoring_;
323
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000324 // String containing currently set device. Note that this string is subtly
325 // different from camera_device_. E.g. camera_device_ will list unplugged
326 // but selected devices while this sting will be empty or contain current
327 // selected device.
328 // TODO(hellner): refactor the code such that there is no need to keep two
329 // strings for video devices that have subtle differences in behavior.
330 std::string video_device_name_;
331};
332
333} // namespace cricket
334
335#endif // TALK_SESSION_MEDIA_CHANNELMANAGER_H_