blob: ba9338947483ca64844cd3c405aaa9a1be6e0218 [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 VideoProcessor;
48class VoiceChannel;
49class VoiceProcessor;
50
51// ChannelManager allows the MediaEngine to run on a separate thread, and takes
52// care of marshalling calls between threads. It also creates and keeps track of
53// voice and video channels; by doing so, it can temporarily pause all the
54// channels when a new audio or video device is chosen. The voice and video
55// channels are stored in separate vectors, to easily allow operations on just
56// voice or just video channels.
57// ChannelManager also allows the application to discover what devices it has
58// using device manager.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000059class ChannelManager : public rtc::MessageHandler,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060 public sigslot::has_slots<> {
61 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062 // For testing purposes. Allows the media engine and data media
63 // engine and dev manager to be mocks. The ChannelManager takes
64 // ownership of these objects.
65 ChannelManager(MediaEngineInterface* me,
66 DataEngineInterface* dme,
67 DeviceManagerInterface* dm,
68 CaptureManager* cm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000069 rtc::Thread* worker);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070 // Same as above, but gives an easier default DataEngine.
71 ChannelManager(MediaEngineInterface* me,
72 DeviceManagerInterface* dm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000073 rtc::Thread* worker);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074 ~ChannelManager();
75
76 // Accessors for the worker thread, allowing it to be set after construction,
77 // but before Init. set_worker_thread will return false if called after Init.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000078 rtc::Thread* worker_thread() const { return worker_thread_; }
79 bool set_worker_thread(rtc::Thread* thread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080 if (initialized_) return false;
81 worker_thread_ = thread;
82 return true;
83 }
84
85 // Gets capabilities. Can be called prior to starting the media engine.
86 int GetCapabilities();
87
88 // Retrieves the list of supported audio & video codec types.
89 // Can be called before starting the media engine.
90 void GetSupportedAudioCodecs(std::vector<AudioCodec>* codecs) const;
91 void GetSupportedAudioRtpHeaderExtensions(RtpHeaderExtensions* ext) const;
92 void GetSupportedVideoCodecs(std::vector<VideoCodec>* codecs) const;
93 void GetSupportedVideoRtpHeaderExtensions(RtpHeaderExtensions* ext) const;
94 void GetSupportedDataCodecs(std::vector<DataCodec>* codecs) const;
95
96 // Indicates whether the media engine is started.
97 bool initialized() const { return initialized_; }
98 // Starts up the media engine.
99 bool Init();
100 // Shuts down the media engine.
101 void Terminate();
102
103 // The operations below all occur on the worker thread.
104
105 // Creates a voice channel, to be associated with the specified session.
106 VoiceChannel* CreateVoiceChannel(
107 BaseSession* session, const std::string& content_name, bool rtcp);
108 // Destroys a voice channel created with the Create API.
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200109 void DestroyVoiceChannel(VoiceChannel* voice_channel,
110 VideoChannel* video_channel);
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000111 // TODO(pbos): Remove as soon as all call sites specify VideoOptions.
112 VideoChannel* CreateVideoChannel(BaseSession* session,
113 const std::string& content_name,
114 bool rtcp,
115 VoiceChannel* voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116 // Creates a video channel, synced with the specified voice channel, and
117 // associated with the specified session.
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000118 VideoChannel* CreateVideoChannel(BaseSession* session,
119 const std::string& content_name,
120 bool rtcp,
121 const VideoOptions& options,
122 VoiceChannel* voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123 // Destroys a video channel created with the Create API.
124 void DestroyVideoChannel(VideoChannel* video_channel);
125 DataChannel* CreateDataChannel(
126 BaseSession* session, const std::string& content_name,
127 bool rtcp, DataChannelType data_channel_type);
128 // Destroys a data channel created with the Create API.
129 void DestroyDataChannel(DataChannel* data_channel);
130
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 // Indicates whether any channels exist.
132 bool has_channels() const {
Fredrik Solenbergccb49e72015-05-19 11:37:56 +0200133 return (!voice_channels_.empty() || !video_channels_.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 }
135
136 // Configures the audio and video devices. A null pointer can be passed to
137 // GetAudioOptions() for any parameter of no interest.
138 bool GetAudioOptions(std::string* wave_in_device,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000139 std::string* wave_out_device,
140 AudioOptions* options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141 bool SetAudioOptions(const std::string& wave_in_device,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000142 const std::string& wave_out_device,
143 const AudioOptions& options);
buildbot@webrtc.org88d9fa62014-06-16 14:11:32 +0000144 // Sets Engine-specific audio options according to enabled experiments.
145 bool SetEngineAudioOptions(const AudioOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146 bool GetOutputVolume(int* level);
147 bool SetOutputVolume(int level);
148 bool IsSameCapturer(const std::string& capturer_name,
149 VideoCapturer* capturer);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000150 // TODO(noahric): Nearly everything called "device" in this API is actually a
151 // device name, so this should really be GetCaptureDeviceName, and the
152 // next method should be GetCaptureDevice.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 bool GetCaptureDevice(std::string* cam_device);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000154 // Gets the current capture Device.
155 bool GetVideoCaptureDevice(Device* device);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156 // Create capturer based on what has been set in SetCaptureDevice().
157 VideoCapturer* CreateVideoCapturer();
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000158 // Create capturer from a screen.
159 VideoCapturer* CreateScreenCapturer(const ScreencastId& screenid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160 bool SetCaptureDevice(const std::string& cam_device);
161 bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config);
162 // RTX will be enabled/disabled in engines that support it. The supporting
163 // engines will start offering an RTX codec. Must be called before Init().
164 bool SetVideoRtxEnabled(bool enable);
165
166 // Starts/stops the local microphone and enables polling of the input level.
167 bool SetLocalMonitor(bool enable);
168 bool monitoring() const { return monitoring_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169 bool capturing() const { return capturing_; }
170
171 // Configures the logging output of the mediaengine(s).
172 void SetVoiceLogging(int level, const char* filter);
173 void SetVideoLogging(int level, const char* filter);
174
hbos@webrtc.org1e642632015-02-25 09:49:41 +0000175 // Gets capturer's supported formats in a thread safe manner
176 std::vector<cricket::VideoFormat> GetSupportedFormats(
177 VideoCapturer* capturer) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178 // The channel manager handles the Tx side for Video processing,
179 // as well as Tx and Rx side for Voice processing.
180 // (The Rx Video processing will go throug the simplerenderingmanager,
181 // to be implemented).
182 bool RegisterVideoProcessor(VideoCapturer* capturer,
183 VideoProcessor* processor);
184 bool UnregisterVideoProcessor(VideoCapturer* capturer,
185 VideoProcessor* processor);
186 bool RegisterVoiceProcessor(uint32 ssrc,
187 VoiceProcessor* processor,
188 MediaProcessorDirection direction);
189 bool UnregisterVoiceProcessor(uint32 ssrc,
190 VoiceProcessor* processor,
191 MediaProcessorDirection direction);
192 // The following are done in the new "CaptureManager" style that
193 // all local video capturers, processors, and managers should move to.
194 // TODO(pthatcher): Make methods nicer by having start return a handle that
195 // can be used for stop and restart, rather than needing to pass around
196 // formats a a pseudo-handle.
197 bool StartVideoCapture(VideoCapturer* video_capturer,
198 const VideoFormat& video_format);
199 // When muting, produce black frames then pause the camera.
200 // When unmuting, start the camera. Camera starts unmuted.
201 bool MuteToBlackThenPause(VideoCapturer* video_capturer, bool muted);
202 bool StopVideoCapture(VideoCapturer* video_capturer,
203 const VideoFormat& video_format);
204 bool RestartVideoCapture(VideoCapturer* video_capturer,
205 const VideoFormat& previous_format,
206 const VideoFormat& desired_format,
207 CaptureManager::RestartOptions options);
208
209 bool AddVideoRenderer(VideoCapturer* capturer, VideoRenderer* renderer);
210 bool RemoveVideoRenderer(VideoCapturer* capturer, VideoRenderer* renderer);
211 bool IsScreencastRunning() const;
212
213 // The operations below occur on the main thread.
214
215 bool GetAudioInputDevices(std::vector<std::string>* names);
216 bool GetAudioOutputDevices(std::vector<std::string>* names);
217 bool GetVideoCaptureDevices(std::vector<std::string>* names);
218 void SetVideoCaptureDeviceMaxFormat(const std::string& usb_id,
219 const VideoFormat& max_format);
220
wu@webrtc.orga9890802013-12-13 00:21:03 +0000221 // Starts AEC dump using existing file.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000222 bool StartAecDump(rtc::PlatformFile file);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000223
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000224 sigslot::repeater0<> SignalDevicesChange;
225 sigslot::signal2<VideoCapturer*, CaptureState> SignalVideoCaptureStateChange;
226
227 // Returns the current selected device. Note: Subtly different from
228 // GetCaptureDevice(). See member video_device_ for more details.
229 // This API is mainly a hook used by unittests.
230 const std::string& video_device_name() const { return video_device_name_; }
231
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000232
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000233 protected:
234 // Adds non-transient parameters which can only be changed through the
235 // options store.
236 bool SetAudioOptions(const std::string& wave_in_device,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000237 const std::string& wave_out_device,
238 const AudioOptions& options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239 int delay_offset);
240 int audio_delay_offset() const { return audio_delay_offset_; }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000241 // This is here so that ChannelManager subclasses can set the video
242 // capturer factories to use.
243 DeviceManagerInterface* device_manager() { return device_manager_.get(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244
245 private:
246 typedef std::vector<VoiceChannel*> VoiceChannels;
247 typedef std::vector<VideoChannel*> VideoChannels;
248 typedef std::vector<DataChannel*> DataChannels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249
250 void Construct(MediaEngineInterface* me,
251 DataEngineInterface* dme,
252 DeviceManagerInterface* dm,
253 CaptureManager* cm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000254 rtc::Thread* worker_thread);
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000255 bool InitMediaEngine_w();
hbos@webrtc.org4aef5fe2015-02-25 10:09:05 +0000256 void DestructorDeletes_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257 void Terminate_w();
258 VoiceChannel* CreateVoiceChannel_w(
259 BaseSession* session, const std::string& content_name, bool rtcp);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200260 void DestroyVoiceChannel_w(VoiceChannel* voice_channel,
261 VideoChannel* video_channel);
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000262 VideoChannel* CreateVideoChannel_w(BaseSession* session,
263 const std::string& content_name,
264 bool rtcp,
265 const VideoOptions& options,
266 VoiceChannel* voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267 void DestroyVideoChannel_w(VideoChannel* video_channel);
268 DataChannel* CreateDataChannel_w(
269 BaseSession* session, const std::string& content_name,
270 bool rtcp, DataChannelType data_channel_type);
271 void DestroyDataChannel_w(DataChannel* data_channel);
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000272 bool SetAudioOptions_w(const AudioOptions& options, int delay_offset,
273 const Device* in_dev, const Device* out_dev);
buildbot@webrtc.org88d9fa62014-06-16 14:11:32 +0000274 bool SetEngineAudioOptions_w(const AudioOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275 bool SetCaptureDevice_w(const Device* cam_device);
276 void OnVideoCaptureStateChange(VideoCapturer* capturer,
277 CaptureState result);
hbos@webrtc.org1e642632015-02-25 09:49:41 +0000278 void GetSupportedFormats_w(
279 VideoCapturer* capturer,
280 std::vector<cricket::VideoFormat>* out_formats) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281 bool RegisterVideoProcessor_w(VideoCapturer* capturer,
282 VideoProcessor* processor);
283 bool UnregisterVideoProcessor_w(VideoCapturer* capturer,
284 VideoProcessor* processor);
285 bool IsScreencastRunning_w() const;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000286 virtual void OnMessage(rtc::Message *message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000288 rtc::scoped_ptr<MediaEngineInterface> media_engine_;
289 rtc::scoped_ptr<DataEngineInterface> data_media_engine_;
290 rtc::scoped_ptr<DeviceManagerInterface> device_manager_;
291 rtc::scoped_ptr<CaptureManager> capture_manager_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000292 bool initialized_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000293 rtc::Thread* main_thread_;
294 rtc::Thread* worker_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295
296 VoiceChannels voice_channels_;
297 VideoChannels video_channels_;
298 DataChannels data_channels_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299
300 std::string audio_in_device_;
301 std::string audio_out_device_;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000302 AudioOptions audio_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303 int audio_delay_offset_;
304 int audio_output_volume_;
305 std::string camera_device_;
306 VideoEncoderConfig default_video_encoder_config_;
307 VideoRenderer* local_renderer_;
308 bool enable_rtx_;
309
310 bool capturing_;
311 bool monitoring_;
312
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000313 // String containing currently set device. Note that this string is subtly
314 // different from camera_device_. E.g. camera_device_ will list unplugged
315 // but selected devices while this sting will be empty or contain current
316 // selected device.
317 // TODO(hellner): refactor the code such that there is no need to keep two
318 // strings for video devices that have subtle differences in behavior.
319 std::string video_device_name_;
320};
321
322} // namespace cricket
323
324#endif // TALK_SESSION_MEDIA_CHANNELMANAGER_H_