blob: 764451d0f468e4032680896ca60f981e9253487b [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:
63#if !defined(DISABLE_MEDIA_ENGINE_FACTORY)
64 // Creates the channel manager, and specifies the worker thread to use.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000065 explicit ChannelManager(rtc::Thread* worker);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066#endif
67
68 // For testing purposes. Allows the media engine and data media
69 // engine and dev manager to be mocks. The ChannelManager takes
70 // ownership of these objects.
71 ChannelManager(MediaEngineInterface* me,
72 DataEngineInterface* dme,
73 DeviceManagerInterface* dm,
74 CaptureManager* cm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000075 rtc::Thread* worker);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076 // Same as above, but gives an easier default DataEngine.
77 ChannelManager(MediaEngineInterface* me,
78 DeviceManagerInterface* dm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000079 rtc::Thread* worker);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080 ~ChannelManager();
81
82 // Accessors for the worker thread, allowing it to be set after construction,
83 // but before Init. set_worker_thread will return false if called after Init.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000084 rtc::Thread* worker_thread() const { return worker_thread_; }
85 bool set_worker_thread(rtc::Thread* thread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 if (initialized_) return false;
87 worker_thread_ = thread;
88 return true;
89 }
90
91 // Gets capabilities. Can be called prior to starting the media engine.
92 int GetCapabilities();
93
94 // Retrieves the list of supported audio & video codec types.
95 // Can be called before starting the media engine.
96 void GetSupportedAudioCodecs(std::vector<AudioCodec>* codecs) const;
97 void GetSupportedAudioRtpHeaderExtensions(RtpHeaderExtensions* ext) const;
98 void GetSupportedVideoCodecs(std::vector<VideoCodec>* codecs) const;
99 void GetSupportedVideoRtpHeaderExtensions(RtpHeaderExtensions* ext) const;
100 void GetSupportedDataCodecs(std::vector<DataCodec>* codecs) const;
101
102 // Indicates whether the media engine is started.
103 bool initialized() const { return initialized_; }
104 // Starts up the media engine.
105 bool Init();
106 // Shuts down the media engine.
107 void Terminate();
108
109 // The operations below all occur on the worker thread.
110
111 // Creates a voice channel, to be associated with the specified session.
112 VoiceChannel* CreateVoiceChannel(
113 BaseSession* session, const std::string& content_name, bool rtcp);
114 // Destroys a voice channel created with the Create API.
115 void DestroyVoiceChannel(VoiceChannel* voice_channel);
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000116 // TODO(pbos): Remove as soon as all call sites specify VideoOptions.
117 VideoChannel* CreateVideoChannel(BaseSession* session,
118 const std::string& content_name,
119 bool rtcp,
120 VoiceChannel* voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121 // Creates a video channel, synced with the specified voice channel, and
122 // associated with the specified session.
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000123 VideoChannel* CreateVideoChannel(BaseSession* session,
124 const std::string& content_name,
125 bool rtcp,
126 const VideoOptions& options,
127 VoiceChannel* voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 // Destroys a video channel created with the Create API.
129 void DestroyVideoChannel(VideoChannel* video_channel);
130 DataChannel* CreateDataChannel(
131 BaseSession* session, const std::string& content_name,
132 bool rtcp, DataChannelType data_channel_type);
133 // Destroys a data channel created with the Create API.
134 void DestroyDataChannel(DataChannel* data_channel);
135
136 // Creates a soundclip.
137 Soundclip* CreateSoundclip();
138 // Destroys a soundclip created with the Create API.
139 void DestroySoundclip(Soundclip* soundclip);
140
141 // Indicates whether any channels exist.
142 bool has_channels() const {
143 return (!voice_channels_.empty() || !video_channels_.empty() ||
144 !soundclips_.empty());
145 }
146
147 // Configures the audio and video devices. A null pointer can be passed to
148 // GetAudioOptions() for any parameter of no interest.
149 bool GetAudioOptions(std::string* wave_in_device,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000150 std::string* wave_out_device,
151 AudioOptions* options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152 bool SetAudioOptions(const std::string& wave_in_device,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000153 const std::string& wave_out_device,
154 const AudioOptions& options);
buildbot@webrtc.org88d9fa62014-06-16 14:11:32 +0000155 // Sets Engine-specific audio options according to enabled experiments.
156 bool SetEngineAudioOptions(const AudioOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157 bool GetOutputVolume(int* level);
158 bool SetOutputVolume(int level);
159 bool IsSameCapturer(const std::string& capturer_name,
160 VideoCapturer* capturer);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000161 // TODO(noahric): Nearly everything called "device" in this API is actually a
162 // device name, so this should really be GetCaptureDeviceName, and the
163 // next method should be GetCaptureDevice.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164 bool GetCaptureDevice(std::string* cam_device);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000165 // Gets the current capture Device.
166 bool GetVideoCaptureDevice(Device* device);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 // Create capturer based on what has been set in SetCaptureDevice().
168 VideoCapturer* CreateVideoCapturer();
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000169 // Create capturer from a screen.
170 VideoCapturer* CreateScreenCapturer(const ScreencastId& screenid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171 bool SetCaptureDevice(const std::string& cam_device);
172 bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config);
173 // RTX will be enabled/disabled in engines that support it. The supporting
174 // engines will start offering an RTX codec. Must be called before Init().
175 bool SetVideoRtxEnabled(bool enable);
176
177 // Starts/stops the local microphone and enables polling of the input level.
178 bool SetLocalMonitor(bool enable);
179 bool monitoring() const { return monitoring_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 bool capturing() const { return capturing_; }
181
182 // Configures the logging output of the mediaengine(s).
183 void SetVoiceLogging(int level, const char* filter);
184 void SetVideoLogging(int level, const char* filter);
185
186 // The channel manager handles the Tx side for Video processing,
187 // as well as Tx and Rx side for Voice processing.
188 // (The Rx Video processing will go throug the simplerenderingmanager,
189 // to be implemented).
190 bool RegisterVideoProcessor(VideoCapturer* capturer,
191 VideoProcessor* processor);
192 bool UnregisterVideoProcessor(VideoCapturer* capturer,
193 VideoProcessor* processor);
194 bool RegisterVoiceProcessor(uint32 ssrc,
195 VoiceProcessor* processor,
196 MediaProcessorDirection direction);
197 bool UnregisterVoiceProcessor(uint32 ssrc,
198 VoiceProcessor* processor,
199 MediaProcessorDirection direction);
200 // The following are done in the new "CaptureManager" style that
201 // all local video capturers, processors, and managers should move to.
202 // TODO(pthatcher): Make methods nicer by having start return a handle that
203 // can be used for stop and restart, rather than needing to pass around
204 // formats a a pseudo-handle.
205 bool StartVideoCapture(VideoCapturer* video_capturer,
206 const VideoFormat& video_format);
207 // When muting, produce black frames then pause the camera.
208 // When unmuting, start the camera. Camera starts unmuted.
209 bool MuteToBlackThenPause(VideoCapturer* video_capturer, bool muted);
210 bool StopVideoCapture(VideoCapturer* video_capturer,
211 const VideoFormat& video_format);
212 bool RestartVideoCapture(VideoCapturer* video_capturer,
213 const VideoFormat& previous_format,
214 const VideoFormat& desired_format,
215 CaptureManager::RestartOptions options);
216
217 bool AddVideoRenderer(VideoCapturer* capturer, VideoRenderer* renderer);
218 bool RemoveVideoRenderer(VideoCapturer* capturer, VideoRenderer* renderer);
219 bool IsScreencastRunning() const;
220
221 // The operations below occur on the main thread.
222
223 bool GetAudioInputDevices(std::vector<std::string>* names);
224 bool GetAudioOutputDevices(std::vector<std::string>* names);
225 bool GetVideoCaptureDevices(std::vector<std::string>* names);
226 void SetVideoCaptureDeviceMaxFormat(const std::string& usb_id,
227 const VideoFormat& max_format);
228
wu@webrtc.orga9890802013-12-13 00:21:03 +0000229 // Starts AEC dump using existing file.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000230 bool StartAecDump(rtc::PlatformFile file);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000231
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 sigslot::repeater0<> SignalDevicesChange;
233 sigslot::signal2<VideoCapturer*, CaptureState> SignalVideoCaptureStateChange;
234
235 // Returns the current selected device. Note: Subtly different from
236 // GetCaptureDevice(). See member video_device_ for more details.
237 // This API is mainly a hook used by unittests.
238 const std::string& video_device_name() const { return video_device_name_; }
239
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000240 // TODO(hellner): Remove this function once the engine capturer has been
241 // removed.
242 VideoFormat GetStartCaptureFormat();
243
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244 protected:
245 // Adds non-transient parameters which can only be changed through the
246 // options store.
247 bool SetAudioOptions(const std::string& wave_in_device,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000248 const std::string& wave_out_device,
249 const AudioOptions& options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250 int delay_offset);
251 int audio_delay_offset() const { return audio_delay_offset_; }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000252 // This is here so that ChannelManager subclasses can set the video
253 // capturer factories to use.
254 DeviceManagerInterface* device_manager() { return device_manager_.get(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255
256 private:
257 typedef std::vector<VoiceChannel*> VoiceChannels;
258 typedef std::vector<VideoChannel*> VideoChannels;
259 typedef std::vector<DataChannel*> DataChannels;
260 typedef std::vector<Soundclip*> Soundclips;
261
262 void Construct(MediaEngineInterface* me,
263 DataEngineInterface* dme,
264 DeviceManagerInterface* dm,
265 CaptureManager* cm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000266 rtc::Thread* worker_thread);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267 void Terminate_w();
268 VoiceChannel* CreateVoiceChannel_w(
269 BaseSession* session, const std::string& content_name, bool rtcp);
270 void DestroyVoiceChannel_w(VoiceChannel* voice_channel);
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000271 VideoChannel* CreateVideoChannel_w(BaseSession* session,
272 const std::string& content_name,
273 bool rtcp,
274 const VideoOptions& options,
275 VoiceChannel* voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276 void DestroyVideoChannel_w(VideoChannel* video_channel);
277 DataChannel* CreateDataChannel_w(
278 BaseSession* session, const std::string& content_name,
279 bool rtcp, DataChannelType data_channel_type);
280 void DestroyDataChannel_w(DataChannel* data_channel);
281 Soundclip* CreateSoundclip_w();
282 void DestroySoundclip_w(Soundclip* soundclip);
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000283 bool SetAudioOptions_w(const AudioOptions& options, int delay_offset,
284 const Device* in_dev, const Device* out_dev);
buildbot@webrtc.org88d9fa62014-06-16 14:11:32 +0000285 bool SetEngineAudioOptions_w(const AudioOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286 bool SetCaptureDevice_w(const Device* cam_device);
287 void OnVideoCaptureStateChange(VideoCapturer* capturer,
288 CaptureState result);
289 bool RegisterVideoProcessor_w(VideoCapturer* capturer,
290 VideoProcessor* processor);
291 bool UnregisterVideoProcessor_w(VideoCapturer* capturer,
292 VideoProcessor* processor);
293 bool IsScreencastRunning_w() const;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000294 virtual void OnMessage(rtc::Message *message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000296 rtc::scoped_ptr<MediaEngineInterface> media_engine_;
297 rtc::scoped_ptr<DataEngineInterface> data_media_engine_;
298 rtc::scoped_ptr<DeviceManagerInterface> device_manager_;
299 rtc::scoped_ptr<CaptureManager> capture_manager_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300 bool initialized_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000301 rtc::Thread* main_thread_;
302 rtc::Thread* worker_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303
304 VoiceChannels voice_channels_;
305 VideoChannels video_channels_;
306 DataChannels data_channels_;
307 Soundclips soundclips_;
308
309 std::string audio_in_device_;
310 std::string audio_out_device_;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000311 AudioOptions audio_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312 int audio_delay_offset_;
313 int audio_output_volume_;
314 std::string camera_device_;
315 VideoEncoderConfig default_video_encoder_config_;
316 VideoRenderer* local_renderer_;
317 bool enable_rtx_;
318
319 bool capturing_;
320 bool monitoring_;
321
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322 // String containing currently set device. Note that this string is subtly
323 // different from camera_device_. E.g. camera_device_ will list unplugged
324 // but selected devices while this sting will be empty or contain current
325 // selected device.
326 // TODO(hellner): refactor the code such that there is no need to keep two
327 // strings for video devices that have subtle differences in behavior.
328 std::string video_device_name_;
329};
330
331} // namespace cricket
332
333#endif // TALK_SESSION_MEDIA_CHANNELMANAGER_H_