blob: 81cb3eed61e591e3d8c0b3bc9ac457d8125f496d [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 VoiceChannel;
48class VoiceProcessor;
49
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,
66 DeviceManagerInterface* dm,
67 CaptureManager* cm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000068 rtc::Thread* worker);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 // Same as above, but gives an easier default DataEngine.
70 ChannelManager(MediaEngineInterface* me,
71 DeviceManagerInterface* dm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000072 rtc::Thread* worker);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073 ~ChannelManager();
74
75 // Accessors for the worker thread, allowing it to be set after construction,
76 // but before Init. set_worker_thread will return false if called after Init.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000077 rtc::Thread* worker_thread() const { return worker_thread_; }
78 bool set_worker_thread(rtc::Thread* thread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079 if (initialized_) return false;
80 worker_thread_ = thread;
81 return true;
82 }
83
84 // Gets capabilities. Can be called prior to starting the media engine.
85 int GetCapabilities();
86
87 // Retrieves the list of supported audio & video codec types.
88 // Can be called before starting the media engine.
89 void GetSupportedAudioCodecs(std::vector<AudioCodec>* codecs) const;
90 void GetSupportedAudioRtpHeaderExtensions(RtpHeaderExtensions* ext) const;
91 void GetSupportedVideoCodecs(std::vector<VideoCodec>* codecs) const;
92 void GetSupportedVideoRtpHeaderExtensions(RtpHeaderExtensions* ext) const;
93 void GetSupportedDataCodecs(std::vector<DataCodec>* codecs) const;
94
95 // Indicates whether the media engine is started.
96 bool initialized() const { return initialized_; }
97 // Starts up the media engine.
98 bool Init();
99 // Shuts down the media engine.
100 void Terminate();
101
102 // The operations below all occur on the worker thread.
103
104 // Creates a voice channel, to be associated with the specified session.
Jelena Marusicc28a8962015-05-29 15:05:44 +0200105 VoiceChannel* CreateVoiceChannel(BaseSession* session,
106 const std::string& content_name,
107 bool rtcp,
108 const AudioOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109 // 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
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132 // Indicates whether any channels exist.
133 bool has_channels() const {
Fredrik Solenbergccb49e72015-05-19 11:37:56 +0200134 return (!voice_channels_.empty() || !video_channels_.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135 }
136
137 // Configures the audio and video devices. A null pointer can be passed to
138 // GetAudioOptions() for any parameter of no interest.
139 bool GetAudioOptions(std::string* wave_in_device,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000140 std::string* wave_out_device,
141 AudioOptions* options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142 bool SetAudioOptions(const std::string& wave_in_device,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000143 const std::string& wave_out_device,
144 const AudioOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145 bool GetOutputVolume(int* level);
146 bool SetOutputVolume(int level);
147 bool IsSameCapturer(const std::string& capturer_name,
148 VideoCapturer* capturer);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000149 // TODO(noahric): Nearly everything called "device" in this API is actually a
150 // device name, so this should really be GetCaptureDeviceName, and the
151 // next method should be GetCaptureDevice.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152 bool GetCaptureDevice(std::string* cam_device);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000153 // Gets the current capture Device.
154 bool GetVideoCaptureDevice(Device* device);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155 // Create capturer based on what has been set in SetCaptureDevice().
156 VideoCapturer* CreateVideoCapturer();
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000157 // Create capturer from a screen.
158 VideoCapturer* CreateScreenCapturer(const ScreencastId& screenid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159 bool SetCaptureDevice(const std::string& cam_device);
160 bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config);
161 // RTX will be enabled/disabled in engines that support it. The supporting
162 // engines will start offering an RTX codec. Must be called before Init().
163 bool SetVideoRtxEnabled(bool enable);
164
165 // Starts/stops the local microphone and enables polling of the input level.
166 bool SetLocalMonitor(bool enable);
167 bool monitoring() const { return monitoring_; }
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();
Jelena Marusicc28a8962015-05-29 15:05:44 +0200250 VoiceChannel* CreateVoiceChannel_w(BaseSession* session,
251 const std::string& content_name,
252 bool rtcp,
253 const AudioOptions& options);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200254 void DestroyVoiceChannel_w(VoiceChannel* voice_channel,
255 VideoChannel* video_channel);
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000256 VideoChannel* CreateVideoChannel_w(BaseSession* session,
257 const std::string& content_name,
258 bool rtcp,
259 const VideoOptions& options,
260 VoiceChannel* voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261 void DestroyVideoChannel_w(VideoChannel* video_channel);
262 DataChannel* CreateDataChannel_w(
263 BaseSession* session, const std::string& content_name,
264 bool rtcp, DataChannelType data_channel_type);
265 void DestroyDataChannel_w(DataChannel* data_channel);
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000266 bool SetAudioOptions_w(const AudioOptions& options, int delay_offset,
267 const Device* in_dev, const Device* out_dev);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268 bool SetCaptureDevice_w(const Device* cam_device);
269 void OnVideoCaptureStateChange(VideoCapturer* capturer,
270 CaptureState result);
hbos@webrtc.org1e642632015-02-25 09:49:41 +0000271 void GetSupportedFormats_w(
272 VideoCapturer* capturer,
273 std::vector<cricket::VideoFormat>* out_formats) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 bool IsScreencastRunning_w() const;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000275 virtual void OnMessage(rtc::Message *message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000277 rtc::scoped_ptr<MediaEngineInterface> media_engine_;
278 rtc::scoped_ptr<DataEngineInterface> data_media_engine_;
279 rtc::scoped_ptr<DeviceManagerInterface> device_manager_;
280 rtc::scoped_ptr<CaptureManager> capture_manager_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281 bool initialized_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000282 rtc::Thread* main_thread_;
283 rtc::Thread* worker_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284
285 VoiceChannels voice_channels_;
286 VideoChannels video_channels_;
287 DataChannels data_channels_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288
289 std::string audio_in_device_;
290 std::string audio_out_device_;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000291 AudioOptions audio_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000292 int audio_delay_offset_;
293 int audio_output_volume_;
294 std::string camera_device_;
295 VideoEncoderConfig default_video_encoder_config_;
296 VideoRenderer* local_renderer_;
297 bool enable_rtx_;
298
299 bool capturing_;
300 bool monitoring_;
301
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000302 // String containing currently set device. Note that this string is subtly
303 // different from camera_device_. E.g. camera_device_ will list unplugged
304 // but selected devices while this sting will be empty or contain current
305 // selected device.
306 // TODO(hellner): refactor the code such that there is no need to keep two
307 // strings for video devices that have subtle differences in behavior.
308 std::string video_device_name_;
309};
310
311} // namespace cricket
312
313#endif // TALK_SESSION_MEDIA_CHANNELMANAGER_H_