blob: e13ae12ec73824fd301a105361af89c2e2923b23 [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"
36#include "talk/p2p/base/session.h"
37#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
45class Soundclip;
46class VideoProcessor;
47class 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:
61#if !defined(DISABLE_MEDIA_ENGINE_FACTORY)
62 // Creates the channel manager, and specifies the worker thread to use.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000063 explicit ChannelManager(rtc::Thread* worker);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064#endif
65
66 // For testing purposes. Allows the media engine and data media
67 // engine and dev manager to be mocks. The ChannelManager takes
68 // ownership of these objects.
69 ChannelManager(MediaEngineInterface* me,
70 DataEngineInterface* dme,
71 DeviceManagerInterface* dm,
72 CaptureManager* cm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000073 rtc::Thread* worker);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074 // Same as above, but gives an easier default DataEngine.
75 ChannelManager(MediaEngineInterface* me,
76 DeviceManagerInterface* dm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000077 rtc::Thread* worker);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078 ~ChannelManager();
79
80 // Accessors for the worker thread, allowing it to be set after construction,
81 // but before Init. set_worker_thread will return false if called after Init.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000082 rtc::Thread* worker_thread() const { return worker_thread_; }
83 bool set_worker_thread(rtc::Thread* thread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084 if (initialized_) return false;
85 worker_thread_ = thread;
86 return true;
87 }
88
89 // 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.
108
109 // Creates a voice channel, to be associated with the specified session.
110 VoiceChannel* CreateVoiceChannel(
111 BaseSession* session, const std::string& content_name, bool rtcp);
112 // Destroys a voice channel created with the Create API.
113 void DestroyVoiceChannel(VoiceChannel* voice_channel);
114 // Creates a video channel, synced with the specified voice channel, and
115 // associated with the specified session.
116 VideoChannel* CreateVideoChannel(
117 BaseSession* session, const std::string& content_name, bool rtcp,
118 VoiceChannel* voice_channel);
119 // Destroys a video channel created with the Create API.
120 void DestroyVideoChannel(VideoChannel* video_channel);
121 DataChannel* CreateDataChannel(
122 BaseSession* session, const std::string& content_name,
123 bool rtcp, DataChannelType data_channel_type);
124 // Destroys a data channel created with the Create API.
125 void DestroyDataChannel(DataChannel* data_channel);
126
127 // Creates a soundclip.
128 Soundclip* CreateSoundclip();
129 // Destroys a soundclip created with the Create API.
130 void DestroySoundclip(Soundclip* soundclip);
131
132 // Indicates whether any channels exist.
133 bool has_channels() const {
134 return (!voice_channels_.empty() || !video_channels_.empty() ||
135 !soundclips_.empty());
136 }
137
138 // Configures the audio and video devices. A null pointer can be passed to
139 // GetAudioOptions() for any parameter of no interest.
140 bool GetAudioOptions(std::string* wave_in_device,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000141 std::string* wave_out_device,
142 AudioOptions* options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143 bool SetAudioOptions(const std::string& wave_in_device,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000144 const std::string& wave_out_device,
145 const AudioOptions& options);
buildbot@webrtc.org88d9fa62014-06-16 14:11:32 +0000146 // Sets Engine-specific audio options according to enabled experiments.
147 bool SetEngineAudioOptions(const AudioOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 bool GetOutputVolume(int* level);
149 bool SetOutputVolume(int level);
150 bool IsSameCapturer(const std::string& capturer_name,
151 VideoCapturer* capturer);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000152 // TODO(noahric): Nearly everything called "device" in this API is actually a
153 // device name, so this should really be GetCaptureDeviceName, and the
154 // next method should be GetCaptureDevice.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155 bool GetCaptureDevice(std::string* cam_device);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000156 // Gets the current capture Device.
157 bool GetVideoCaptureDevice(Device* device);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158 // Create capturer based on what has been set in SetCaptureDevice().
159 VideoCapturer* CreateVideoCapturer();
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000160 // Create capturer from a screen.
161 VideoCapturer* CreateScreenCapturer(const ScreencastId& screenid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162 bool SetCaptureDevice(const std::string& cam_device);
163 bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config);
164 // RTX will be enabled/disabled in engines that support it. The supporting
165 // engines will start offering an RTX codec. Must be called before Init().
166 bool SetVideoRtxEnabled(bool enable);
167
168 // Starts/stops the local microphone and enables polling of the input level.
169 bool SetLocalMonitor(bool enable);
170 bool monitoring() const { return monitoring_; }
171 // Sets the local renderer where to renderer the local camera.
172 bool SetLocalRenderer(VideoRenderer* renderer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173 bool capturing() const { return capturing_; }
174
175 // Configures the logging output of the mediaengine(s).
176 void SetVoiceLogging(int level, const char* filter);
177 void SetVideoLogging(int level, const char* filter);
178
179 // The channel manager handles the Tx side for Video processing,
180 // as well as Tx and Rx side for Voice processing.
181 // (The Rx Video processing will go throug the simplerenderingmanager,
182 // to be implemented).
183 bool RegisterVideoProcessor(VideoCapturer* capturer,
184 VideoProcessor* processor);
185 bool UnregisterVideoProcessor(VideoCapturer* capturer,
186 VideoProcessor* processor);
187 bool RegisterVoiceProcessor(uint32 ssrc,
188 VoiceProcessor* processor,
189 MediaProcessorDirection direction);
190 bool UnregisterVoiceProcessor(uint32 ssrc,
191 VoiceProcessor* processor,
192 MediaProcessorDirection direction);
193 // The following are done in the new "CaptureManager" style that
194 // all local video capturers, processors, and managers should move to.
195 // TODO(pthatcher): Make methods nicer by having start return a handle that
196 // can be used for stop and restart, rather than needing to pass around
197 // formats a a pseudo-handle.
198 bool StartVideoCapture(VideoCapturer* video_capturer,
199 const VideoFormat& video_format);
200 // When muting, produce black frames then pause the camera.
201 // When unmuting, start the camera. Camera starts unmuted.
202 bool MuteToBlackThenPause(VideoCapturer* video_capturer, bool muted);
203 bool StopVideoCapture(VideoCapturer* video_capturer,
204 const VideoFormat& video_format);
205 bool RestartVideoCapture(VideoCapturer* video_capturer,
206 const VideoFormat& previous_format,
207 const VideoFormat& desired_format,
208 CaptureManager::RestartOptions options);
209
210 bool AddVideoRenderer(VideoCapturer* capturer, VideoRenderer* renderer);
211 bool RemoveVideoRenderer(VideoCapturer* capturer, VideoRenderer* renderer);
212 bool IsScreencastRunning() const;
213
214 // The operations below occur on the main thread.
215
216 bool GetAudioInputDevices(std::vector<std::string>* names);
217 bool GetAudioOutputDevices(std::vector<std::string>* names);
218 bool GetVideoCaptureDevices(std::vector<std::string>* names);
219 void SetVideoCaptureDeviceMaxFormat(const std::string& usb_id,
220 const VideoFormat& max_format);
221
wu@webrtc.orga9890802013-12-13 00:21:03 +0000222 // Starts AEC dump using existing file.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000223 bool StartAecDump(rtc::PlatformFile file);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000224
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225 sigslot::repeater0<> SignalDevicesChange;
226 sigslot::signal2<VideoCapturer*, CaptureState> SignalVideoCaptureStateChange;
227
228 // Returns the current selected device. Note: Subtly different from
229 // GetCaptureDevice(). See member video_device_ for more details.
230 // This API is mainly a hook used by unittests.
231 const std::string& video_device_name() const { return video_device_name_; }
232
233 // TODO(hellner): Remove this function once the engine capturer has been
234 // removed.
235 VideoFormat GetStartCaptureFormat();
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000236
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000237 protected:
238 // Adds non-transient parameters which can only be changed through the
239 // options store.
240 bool SetAudioOptions(const std::string& wave_in_device,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000241 const std::string& wave_out_device,
242 const AudioOptions& options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000243 int delay_offset);
244 int audio_delay_offset() const { return audio_delay_offset_; }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000245 // This is here so that ChannelManager subclasses can set the video
246 // capturer factories to use.
247 DeviceManagerInterface* device_manager() { return device_manager_.get(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248
249 private:
250 typedef std::vector<VoiceChannel*> VoiceChannels;
251 typedef std::vector<VideoChannel*> VideoChannels;
252 typedef std::vector<DataChannel*> DataChannels;
253 typedef std::vector<Soundclip*> Soundclips;
254
255 void Construct(MediaEngineInterface* me,
256 DataEngineInterface* dme,
257 DeviceManagerInterface* dm,
258 CaptureManager* cm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000259 rtc::Thread* worker_thread);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260 void Terminate_w();
261 VoiceChannel* CreateVoiceChannel_w(
262 BaseSession* session, const std::string& content_name, bool rtcp);
263 void DestroyVoiceChannel_w(VoiceChannel* voice_channel);
264 VideoChannel* CreateVideoChannel_w(
265 BaseSession* session, const std::string& content_name, bool rtcp,
266 VoiceChannel* voice_channel);
267 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);
272 Soundclip* CreateSoundclip_w();
273 void DestroySoundclip_w(Soundclip* soundclip);
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000274 bool SetAudioOptions_w(const AudioOptions& options, int delay_offset,
275 const Device* in_dev, const Device* out_dev);
buildbot@webrtc.org88d9fa62014-06-16 14:11:32 +0000276 bool SetEngineAudioOptions_w(const AudioOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277 bool SetCaptureDevice_w(const Device* cam_device);
278 void OnVideoCaptureStateChange(VideoCapturer* capturer,
279 CaptureState result);
280 bool RegisterVideoProcessor_w(VideoCapturer* capturer,
281 VideoProcessor* processor);
282 bool UnregisterVideoProcessor_w(VideoCapturer* capturer,
283 VideoProcessor* processor);
284 bool IsScreencastRunning_w() const;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000285 virtual void OnMessage(rtc::Message *message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000287 rtc::scoped_ptr<MediaEngineInterface> media_engine_;
288 rtc::scoped_ptr<DataEngineInterface> data_media_engine_;
289 rtc::scoped_ptr<DeviceManagerInterface> device_manager_;
290 rtc::scoped_ptr<CaptureManager> capture_manager_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 bool initialized_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000292 rtc::Thread* main_thread_;
293 rtc::Thread* worker_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294
295 VoiceChannels voice_channels_;
296 VideoChannels video_channels_;
297 DataChannels data_channels_;
298 Soundclips soundclips_;
299
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_