blob: 68b3c9efa73fa3bb1719c7e16a788f0a5c65efd0 [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.org0481f152014-08-19 14:56:59 +000048const int kDefaultAudioDelayOffset = 0;
49
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050class VoiceChannel;
51class VoiceProcessor;
52
53// ChannelManager allows the MediaEngine to run on a separate thread, and takes
54// care of marshalling calls between threads. It also creates and keeps track of
55// voice and video channels; by doing so, it can temporarily pause all the
56// channels when a new audio or video device is chosen. The voice and video
57// channels are stored in separate vectors, to easily allow operations on just
58// voice or just video channels.
59// ChannelManager also allows the application to discover what devices it has
60// using device manager.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000061class ChannelManager : public rtc::MessageHandler,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062 public sigslot::has_slots<> {
63 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064 // For testing purposes. Allows the media engine and data media
65 // engine and dev manager to be mocks. The ChannelManager takes
66 // ownership of these objects.
67 ChannelManager(MediaEngineInterface* me,
68 DataEngineInterface* dme,
69 DeviceManagerInterface* dm,
70 CaptureManager* cm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000071 rtc::Thread* worker);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072 // Same as above, but gives an easier default DataEngine.
73 ChannelManager(MediaEngineInterface* me,
74 DeviceManagerInterface* dm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000075 rtc::Thread* worker);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076 ~ChannelManager();
77
78 // Accessors for the worker thread, allowing it to be set after construction,
79 // but before Init. set_worker_thread will return false if called after Init.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000080 rtc::Thread* worker_thread() const { return worker_thread_; }
81 bool set_worker_thread(rtc::Thread* thread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082 if (initialized_) return false;
83 worker_thread_ = thread;
84 return true;
85 }
86
Fredrik Solenberg709ed672015-09-15 12:26:33 +020087 MediaEngineInterface* media_engine() { return media_engine_.get(); }
88
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 // Gets capabilities. Can be called prior to starting the media engine.
90 int GetCapabilities();
91
92 // Retrieves the list of supported audio & video codec types.
93 // Can be called before starting the media engine.
94 void GetSupportedAudioCodecs(std::vector<AudioCodec>* codecs) const;
95 void GetSupportedAudioRtpHeaderExtensions(RtpHeaderExtensions* ext) const;
96 void GetSupportedVideoCodecs(std::vector<VideoCodec>* codecs) const;
97 void GetSupportedVideoRtpHeaderExtensions(RtpHeaderExtensions* ext) const;
98 void GetSupportedDataCodecs(std::vector<DataCodec>* codecs) const;
99
100 // Indicates whether the media engine is started.
101 bool initialized() const { return initialized_; }
102 // Starts up the media engine.
103 bool Init();
104 // Shuts down the media engine.
105 void Terminate();
106
107 // The operations below all occur on the worker thread.
torbjornga81a42f2015-09-23 02:16:58 -0700108
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109 // Creates a voice channel, to be associated with the specified session.
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200110 VoiceChannel* CreateVoiceChannel(
111 webrtc::MediaControllerInterface* media_controller,
torbjornga81a42f2015-09-23 02:16:58 -0700112 BaseSession* session,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200113 const std::string& content_name,
114 bool rtcp,
115 const AudioOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116 // Destroys a voice channel created with the Create API.
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200117 void DestroyVoiceChannel(VoiceChannel* voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118 // Creates a video channel, synced with the specified voice channel, and
119 // associated with the specified session.
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200120 VideoChannel* CreateVideoChannel(
121 webrtc::MediaControllerInterface* media_controller,
torbjornga81a42f2015-09-23 02:16:58 -0700122 BaseSession* session,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200123 const std::string& content_name,
124 bool rtcp,
125 const VideoOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126 // Destroys a video channel created with the Create API.
127 void DestroyVideoChannel(VideoChannel* video_channel);
torbjornga81a42f2015-09-23 02:16:58 -0700128 DataChannel* CreateDataChannel(
129 BaseSession* session, const std::string& content_name,
130 bool rtcp, DataChannelType data_channel_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 // Destroys a data channel created with the Create API.
132 void DestroyDataChannel(DataChannel* data_channel);
133
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 // Indicates whether any channels exist.
135 bool has_channels() const {
Fredrik Solenbergccb49e72015-05-19 11:37:56 +0200136 return (!voice_channels_.empty() || !video_channels_.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137 }
138
139 // Configures the audio and video devices. A null pointer can be passed to
140 // GetAudioOptions() for any parameter of no interest.
141 bool GetAudioOptions(std::string* wave_in_device,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000142 std::string* wave_out_device,
143 AudioOptions* options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144 bool SetAudioOptions(const std::string& wave_in_device,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000145 const std::string& wave_out_device,
146 const AudioOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147 bool GetOutputVolume(int* level);
148 bool SetOutputVolume(int level);
149 bool IsSameCapturer(const std::string& capturer_name,
150 VideoCapturer* capturer);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000151 // TODO(noahric): Nearly everything called "device" in this API is actually a
152 // device name, so this should really be GetCaptureDeviceName, and the
153 // next method should be GetCaptureDevice.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154 bool GetCaptureDevice(std::string* cam_device);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000155 // Gets the current capture Device.
156 bool GetVideoCaptureDevice(Device* device);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157 // Create capturer based on what has been set in SetCaptureDevice().
158 VideoCapturer* CreateVideoCapturer();
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000159 // Create capturer from a screen.
160 VideoCapturer* CreateScreenCapturer(const ScreencastId& screenid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161 bool SetCaptureDevice(const std::string& cam_device);
162 bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config);
163 // RTX will be enabled/disabled in engines that support it. The supporting
164 // engines will start offering an RTX codec. Must be called before Init().
165 bool SetVideoRtxEnabled(bool enable);
166
167 // Starts/stops the local microphone and enables polling of the input level.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168 bool capturing() const { return capturing_; }
169
170 // Configures the logging output of the mediaengine(s).
171 void SetVoiceLogging(int level, const char* filter);
172 void SetVideoLogging(int level, const char* filter);
173
hbos@webrtc.org1e642632015-02-25 09:49:41 +0000174 // Gets capturer's supported formats in a thread safe manner
175 std::vector<cricket::VideoFormat> GetSupportedFormats(
176 VideoCapturer* capturer) const;
Magnus Jedvertc2320962015-08-21 11:40:30 +0200177 // The channel manager handles the Tx and Rx side for Voice processing.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178 bool RegisterVoiceProcessor(uint32 ssrc,
179 VoiceProcessor* processor,
180 MediaProcessorDirection direction);
181 bool UnregisterVoiceProcessor(uint32 ssrc,
182 VoiceProcessor* processor,
183 MediaProcessorDirection direction);
184 // The following are done in the new "CaptureManager" style that
185 // all local video capturers, processors, and managers should move to.
186 // TODO(pthatcher): Make methods nicer by having start return a handle that
187 // can be used for stop and restart, rather than needing to pass around
188 // formats a a pseudo-handle.
189 bool StartVideoCapture(VideoCapturer* video_capturer,
190 const VideoFormat& video_format);
191 // When muting, produce black frames then pause the camera.
192 // When unmuting, start the camera. Camera starts unmuted.
193 bool MuteToBlackThenPause(VideoCapturer* video_capturer, bool muted);
194 bool StopVideoCapture(VideoCapturer* video_capturer,
195 const VideoFormat& video_format);
196 bool RestartVideoCapture(VideoCapturer* video_capturer,
197 const VideoFormat& previous_format,
198 const VideoFormat& desired_format,
199 CaptureManager::RestartOptions options);
200
201 bool AddVideoRenderer(VideoCapturer* capturer, VideoRenderer* renderer);
202 bool RemoveVideoRenderer(VideoCapturer* capturer, VideoRenderer* renderer);
203 bool IsScreencastRunning() const;
204
205 // The operations below occur on the main thread.
206
207 bool GetAudioInputDevices(std::vector<std::string>* names);
208 bool GetAudioOutputDevices(std::vector<std::string>* names);
209 bool GetVideoCaptureDevices(std::vector<std::string>* names);
210 void SetVideoCaptureDeviceMaxFormat(const std::string& usb_id,
211 const VideoFormat& max_format);
212
wu@webrtc.orga9890802013-12-13 00:21:03 +0000213 // Starts AEC dump using existing file.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000214 bool StartAecDump(rtc::PlatformFile file);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000215
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216 sigslot::repeater0<> SignalDevicesChange;
217 sigslot::signal2<VideoCapturer*, CaptureState> SignalVideoCaptureStateChange;
218
219 // Returns the current selected device. Note: Subtly different from
220 // GetCaptureDevice(). See member video_device_ for more details.
221 // This API is mainly a hook used by unittests.
222 const std::string& video_device_name() const { return video_device_name_; }
223
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000224
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225 protected:
226 // Adds non-transient parameters which can only be changed through the
227 // options store.
228 bool SetAudioOptions(const std::string& wave_in_device,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000229 const std::string& wave_out_device,
230 const AudioOptions& options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231 int delay_offset);
232 int audio_delay_offset() const { return audio_delay_offset_; }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000233 // This is here so that ChannelManager subclasses can set the video
234 // capturer factories to use.
235 DeviceManagerInterface* device_manager() { return device_manager_.get(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236
237 private:
238 typedef std::vector<VoiceChannel*> VoiceChannels;
239 typedef std::vector<VideoChannel*> VideoChannels;
240 typedef std::vector<DataChannel*> DataChannels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000241
242 void Construct(MediaEngineInterface* me,
243 DataEngineInterface* dme,
244 DeviceManagerInterface* dm,
245 CaptureManager* cm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000246 rtc::Thread* worker_thread);
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000247 bool InitMediaEngine_w();
hbos@webrtc.org4aef5fe2015-02-25 10:09:05 +0000248 void DestructorDeletes_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249 void Terminate_w();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200250 VoiceChannel* CreateVoiceChannel_w(
251 webrtc::MediaControllerInterface* media_controller,
torbjornga81a42f2015-09-23 02:16:58 -0700252 BaseSession* session,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200253 const std::string& content_name,
254 bool rtcp,
255 const AudioOptions& options);
256 void DestroyVoiceChannel_w(VoiceChannel* voice_channel);
257 VideoChannel* CreateVideoChannel_w(
258 webrtc::MediaControllerInterface* media_controller,
torbjornga81a42f2015-09-23 02:16:58 -0700259 BaseSession* session,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200260 const std::string& content_name,
261 bool rtcp,
262 const VideoOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263 void DestroyVideoChannel_w(VideoChannel* video_channel);
torbjornga81a42f2015-09-23 02:16:58 -0700264 DataChannel* CreateDataChannel_w(
265 BaseSession* session, const std::string& content_name,
266 bool rtcp, DataChannelType data_channel_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267 void DestroyDataChannel_w(DataChannel* data_channel);
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000268 bool SetAudioOptions_w(const AudioOptions& options, int delay_offset,
269 const Device* in_dev, const Device* out_dev);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270 bool SetCaptureDevice_w(const Device* cam_device);
271 void OnVideoCaptureStateChange(VideoCapturer* capturer,
272 CaptureState result);
hbos@webrtc.org1e642632015-02-25 09:49:41 +0000273 void GetSupportedFormats_w(
274 VideoCapturer* capturer,
275 std::vector<cricket::VideoFormat>* out_formats) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276 bool IsScreencastRunning_w() const;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000277 virtual void OnMessage(rtc::Message *message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000279 rtc::scoped_ptr<MediaEngineInterface> media_engine_;
280 rtc::scoped_ptr<DataEngineInterface> data_media_engine_;
281 rtc::scoped_ptr<DeviceManagerInterface> device_manager_;
282 rtc::scoped_ptr<CaptureManager> capture_manager_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283 bool initialized_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000284 rtc::Thread* main_thread_;
285 rtc::Thread* worker_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286
287 VoiceChannels voice_channels_;
288 VideoChannels video_channels_;
289 DataChannels data_channels_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000290
291 std::string audio_in_device_;
292 std::string audio_out_device_;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000293 AudioOptions audio_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294 int audio_delay_offset_;
295 int audio_output_volume_;
296 std::string camera_device_;
297 VideoEncoderConfig default_video_encoder_config_;
298 VideoRenderer* local_renderer_;
299 bool enable_rtx_;
300
301 bool capturing_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000302
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303 // String containing currently set device. Note that this string is subtly
304 // different from camera_device_. E.g. camera_device_ will list unplugged
305 // but selected devices while this sting will be empty or contain current
306 // selected device.
307 // TODO(hellner): refactor the code such that there is no need to keep two
308 // strings for video devices that have subtle differences in behavior.
309 std::string video_device_name_;
310};
311
312} // namespace cricket
313
314#endif // TALK_SESSION_MEDIA_CHANNELMANAGER_H_