blob: 5f7651934dae862688dbe9b2275a7cdaee2b2fe2 [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;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051
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:
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063 // For testing purposes. Allows the media engine and data media
64 // engine and dev manager to be mocks. The ChannelManager takes
65 // ownership of these objects.
66 ChannelManager(MediaEngineInterface* me,
67 DataEngineInterface* dme,
68 DeviceManagerInterface* dm,
69 CaptureManager* cm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000070 rtc::Thread* worker);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071 // Same as above, but gives an easier default DataEngine.
72 ChannelManager(MediaEngineInterface* me,
73 DeviceManagerInterface* dm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000074 rtc::Thread* worker);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 ~ChannelManager();
76
77 // Accessors for the worker thread, allowing it to be set after construction,
78 // but before Init. set_worker_thread will return false if called after Init.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000079 rtc::Thread* worker_thread() const { return worker_thread_; }
80 bool set_worker_thread(rtc::Thread* thread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081 if (initialized_) return false;
82 worker_thread_ = thread;
83 return true;
84 }
85
Fredrik Solenberg709ed672015-09-15 12:26:33 +020086 MediaEngineInterface* media_engine() { return media_engine_.get(); }
87
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 // Gets capabilities. Can be called prior to starting the media engine.
89 int GetCapabilities();
90
91 // Retrieves the list of supported audio & video codec types.
92 // Can be called before starting the media engine.
93 void GetSupportedAudioCodecs(std::vector<AudioCodec>* codecs) const;
94 void GetSupportedAudioRtpHeaderExtensions(RtpHeaderExtensions* ext) const;
95 void GetSupportedVideoCodecs(std::vector<VideoCodec>* codecs) const;
96 void GetSupportedVideoRtpHeaderExtensions(RtpHeaderExtensions* ext) const;
97 void GetSupportedDataCodecs(std::vector<DataCodec>* codecs) const;
98
99 // Indicates whether the media engine is started.
100 bool initialized() const { return initialized_; }
101 // Starts up the media engine.
102 bool Init();
103 // Shuts down the media engine.
104 void Terminate();
105
106 // The operations below all occur on the worker thread.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107 // Creates a voice channel, to be associated with the specified session.
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200108 VoiceChannel* CreateVoiceChannel(
109 webrtc::MediaControllerInterface* media_controller,
deadbeefcbecd352015-09-23 11:50:27 -0700110 TransportController* transport_controller,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200111 const std::string& content_name,
112 bool rtcp,
113 const AudioOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114 // Destroys a voice channel created with the Create API.
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200115 void DestroyVoiceChannel(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.
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200118 VideoChannel* CreateVideoChannel(
119 webrtc::MediaControllerInterface* media_controller,
deadbeefcbecd352015-09-23 11:50:27 -0700120 TransportController* transport_controller,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200121 const std::string& content_name,
122 bool rtcp,
123 const VideoOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124 // Destroys a video channel created with the Create API.
125 void DestroyVideoChannel(VideoChannel* video_channel);
deadbeefcbecd352015-09-23 11:50:27 -0700126 DataChannel* CreateDataChannel(TransportController* transport_controller,
127 const std::string& content_name,
128 bool rtcp,
129 DataChannelType data_channel_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130 // Destroys a data channel created with the Create API.
131 void DestroyDataChannel(DataChannel* data_channel);
132
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 // Indicates whether any channels exist.
134 bool has_channels() const {
Fredrik Solenbergccb49e72015-05-19 11:37:56 +0200135 return (!voice_channels_.empty() || !video_channels_.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 }
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);
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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 bool capturing() const { return capturing_; }
168
169 // Configures the logging output of the mediaengine(s).
170 void SetVoiceLogging(int level, const char* filter);
171 void SetVideoLogging(int level, const char* filter);
172
hbos@webrtc.org1e642632015-02-25 09:49:41 +0000173 // Gets capturer's supported formats in a thread safe manner
174 std::vector<cricket::VideoFormat> GetSupportedFormats(
175 VideoCapturer* capturer) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 // The following are done in the new "CaptureManager" style that
177 // all local video capturers, processors, and managers should move to.
178 // TODO(pthatcher): Make methods nicer by having start return a handle that
179 // can be used for stop and restart, rather than needing to pass around
180 // formats a a pseudo-handle.
181 bool StartVideoCapture(VideoCapturer* video_capturer,
182 const VideoFormat& video_format);
183 // When muting, produce black frames then pause the camera.
184 // When unmuting, start the camera. Camera starts unmuted.
185 bool MuteToBlackThenPause(VideoCapturer* video_capturer, bool muted);
186 bool StopVideoCapture(VideoCapturer* video_capturer,
187 const VideoFormat& video_format);
188 bool RestartVideoCapture(VideoCapturer* video_capturer,
189 const VideoFormat& previous_format,
190 const VideoFormat& desired_format,
191 CaptureManager::RestartOptions options);
192
193 bool AddVideoRenderer(VideoCapturer* capturer, VideoRenderer* renderer);
194 bool RemoveVideoRenderer(VideoCapturer* capturer, VideoRenderer* renderer);
195 bool IsScreencastRunning() const;
196
197 // The operations below occur on the main thread.
198
199 bool GetAudioInputDevices(std::vector<std::string>* names);
200 bool GetAudioOutputDevices(std::vector<std::string>* names);
201 bool GetVideoCaptureDevices(std::vector<std::string>* names);
202 void SetVideoCaptureDeviceMaxFormat(const std::string& usb_id,
203 const VideoFormat& max_format);
204
wu@webrtc.orga9890802013-12-13 00:21:03 +0000205 // Starts AEC dump using existing file.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000206 bool StartAecDump(rtc::PlatformFile file);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000207
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208 sigslot::repeater0<> SignalDevicesChange;
209 sigslot::signal2<VideoCapturer*, CaptureState> SignalVideoCaptureStateChange;
210
211 // Returns the current selected device. Note: Subtly different from
212 // GetCaptureDevice(). See member video_device_ for more details.
213 // This API is mainly a hook used by unittests.
214 const std::string& video_device_name() const { return video_device_name_; }
215
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000216
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217 protected:
218 // Adds non-transient parameters which can only be changed through the
219 // options store.
220 bool SetAudioOptions(const std::string& wave_in_device,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000221 const std::string& wave_out_device,
222 const AudioOptions& options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000223 int delay_offset);
224 int audio_delay_offset() const { return audio_delay_offset_; }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000225 // This is here so that ChannelManager subclasses can set the video
226 // capturer factories to use.
227 DeviceManagerInterface* device_manager() { return device_manager_.get(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228
229 private:
230 typedef std::vector<VoiceChannel*> VoiceChannels;
231 typedef std::vector<VideoChannel*> VideoChannels;
232 typedef std::vector<DataChannel*> DataChannels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000233
234 void Construct(MediaEngineInterface* me,
235 DataEngineInterface* dme,
236 DeviceManagerInterface* dm,
237 CaptureManager* cm,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000238 rtc::Thread* worker_thread);
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000239 bool InitMediaEngine_w();
hbos@webrtc.org4aef5fe2015-02-25 10:09:05 +0000240 void DestructorDeletes_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000241 void Terminate_w();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200242 VoiceChannel* CreateVoiceChannel_w(
243 webrtc::MediaControllerInterface* media_controller,
deadbeefcbecd352015-09-23 11:50:27 -0700244 TransportController* transport_controller,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200245 const std::string& content_name,
246 bool rtcp,
247 const AudioOptions& options);
248 void DestroyVoiceChannel_w(VoiceChannel* voice_channel);
249 VideoChannel* CreateVideoChannel_w(
250 webrtc::MediaControllerInterface* media_controller,
deadbeefcbecd352015-09-23 11:50:27 -0700251 TransportController* transport_controller,
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200252 const std::string& content_name,
253 bool rtcp,
254 const VideoOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255 void DestroyVideoChannel_w(VideoChannel* video_channel);
deadbeefcbecd352015-09-23 11:50:27 -0700256 DataChannel* CreateDataChannel_w(TransportController* transport_controller,
257 const std::string& content_name,
258 bool rtcp,
259 DataChannelType data_channel_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260 void DestroyDataChannel_w(DataChannel* data_channel);
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000261 bool SetAudioOptions_w(const AudioOptions& options, int delay_offset,
262 const Device* in_dev, const Device* out_dev);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263 bool SetCaptureDevice_w(const Device* cam_device);
264 void OnVideoCaptureStateChange(VideoCapturer* capturer,
265 CaptureState result);
hbos@webrtc.org1e642632015-02-25 09:49:41 +0000266 void GetSupportedFormats_w(
267 VideoCapturer* capturer,
268 std::vector<cricket::VideoFormat>* out_formats) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269 bool IsScreencastRunning_w() const;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000270 virtual void OnMessage(rtc::Message *message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000272 rtc::scoped_ptr<MediaEngineInterface> media_engine_;
273 rtc::scoped_ptr<DataEngineInterface> data_media_engine_;
274 rtc::scoped_ptr<DeviceManagerInterface> device_manager_;
275 rtc::scoped_ptr<CaptureManager> capture_manager_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276 bool initialized_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000277 rtc::Thread* main_thread_;
278 rtc::Thread* worker_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279
280 VoiceChannels voice_channels_;
281 VideoChannels video_channels_;
282 DataChannels data_channels_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283
284 std::string audio_in_device_;
285 std::string audio_out_device_;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000286 AudioOptions audio_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287 int audio_delay_offset_;
288 int audio_output_volume_;
289 std::string camera_device_;
290 VideoEncoderConfig default_video_encoder_config_;
291 VideoRenderer* local_renderer_;
292 bool enable_rtx_;
293
294 bool capturing_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000296 // String containing currently set device. Note that this string is subtly
297 // different from camera_device_. E.g. camera_device_ will list unplugged
298 // but selected devices while this sting will be empty or contain current
299 // selected device.
300 // TODO(hellner): refactor the code such that there is no need to keep two
301 // strings for video devices that have subtle differences in behavior.
302 std::string video_device_name_;
303};
304
305} // namespace cricket
306
307#endif // TALK_SESSION_MEDIA_CHANNELMANAGER_H_