blob: e811bb7342484466da0f19e803af9fe40722d3ac [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
34#include "talk/base/criticalsection.h"
wu@webrtc.orga8910d22014-01-23 22:12:45 +000035#include "talk/base/fileutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036#include "talk/base/sigslotrepeater.h"
37#include "talk/base/thread.h"
38#include "talk/media/base/capturemanager.h"
39#include "talk/media/base/mediaengine.h"
40#include "talk/p2p/base/session.h"
41#include "talk/session/media/voicechannel.h"
42
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.
58class ChannelManager : public talk_base::MessageHandler,
59 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.
63 explicit ChannelManager(talk_base::Thread* worker);
64#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,
73 talk_base::Thread* worker);
74 // Same as above, but gives an easier default DataEngine.
75 ChannelManager(MediaEngineInterface* me,
76 DeviceManagerInterface* dm,
77 talk_base::Thread* worker);
78 ~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.
82 talk_base::Thread* worker_thread() const { return worker_thread_; }
83 bool set_worker_thread(talk_base::Thread* thread) {
84 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);
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();
158 bool SetCaptureDevice(const std::string& cam_device);
159 bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config);
160 // RTX will be enabled/disabled in engines that support it. The supporting
161 // engines will start offering an RTX codec. Must be called before Init().
162 bool SetVideoRtxEnabled(bool enable);
163
164 // Starts/stops the local microphone and enables polling of the input level.
165 bool SetLocalMonitor(bool enable);
166 bool monitoring() const { return monitoring_; }
167 // Sets the local renderer where to renderer the local camera.
168 bool SetLocalRenderer(VideoRenderer* renderer);
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
175 // The channel manager handles the Tx side for Video processing,
176 // as well as Tx and Rx side for Voice processing.
177 // (The Rx Video processing will go throug the simplerenderingmanager,
178 // to be implemented).
179 bool RegisterVideoProcessor(VideoCapturer* capturer,
180 VideoProcessor* processor);
181 bool UnregisterVideoProcessor(VideoCapturer* capturer,
182 VideoProcessor* processor);
183 bool RegisterVoiceProcessor(uint32 ssrc,
184 VoiceProcessor* processor,
185 MediaProcessorDirection direction);
186 bool UnregisterVoiceProcessor(uint32 ssrc,
187 VoiceProcessor* processor,
188 MediaProcessorDirection direction);
189 // The following are done in the new "CaptureManager" style that
190 // all local video capturers, processors, and managers should move to.
191 // TODO(pthatcher): Make methods nicer by having start return a handle that
192 // can be used for stop and restart, rather than needing to pass around
193 // formats a a pseudo-handle.
194 bool StartVideoCapture(VideoCapturer* video_capturer,
195 const VideoFormat& video_format);
196 // When muting, produce black frames then pause the camera.
197 // When unmuting, start the camera. Camera starts unmuted.
198 bool MuteToBlackThenPause(VideoCapturer* video_capturer, bool muted);
199 bool StopVideoCapture(VideoCapturer* video_capturer,
200 const VideoFormat& video_format);
201 bool RestartVideoCapture(VideoCapturer* video_capturer,
202 const VideoFormat& previous_format,
203 const VideoFormat& desired_format,
204 CaptureManager::RestartOptions options);
205
206 bool AddVideoRenderer(VideoCapturer* capturer, VideoRenderer* renderer);
207 bool RemoveVideoRenderer(VideoCapturer* capturer, VideoRenderer* renderer);
208 bool IsScreencastRunning() const;
209
210 // The operations below occur on the main thread.
211
212 bool GetAudioInputDevices(std::vector<std::string>* names);
213 bool GetAudioOutputDevices(std::vector<std::string>* names);
214 bool GetVideoCaptureDevices(std::vector<std::string>* names);
215 void SetVideoCaptureDeviceMaxFormat(const std::string& usb_id,
216 const VideoFormat& max_format);
217
wu@webrtc.orga9890802013-12-13 00:21:03 +0000218 // Starts AEC dump using existing file.
wu@webrtc.orga8910d22014-01-23 22:12:45 +0000219 bool StartAecDump(talk_base::PlatformFile file);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000220
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221 sigslot::repeater0<> SignalDevicesChange;
222 sigslot::signal2<VideoCapturer*, CaptureState> SignalVideoCaptureStateChange;
223
224 // Returns the current selected device. Note: Subtly different from
225 // GetCaptureDevice(). See member video_device_ for more details.
226 // This API is mainly a hook used by unittests.
227 const std::string& video_device_name() const { return video_device_name_; }
228
229 // TODO(hellner): Remove this function once the engine capturer has been
230 // removed.
231 VideoFormat GetStartCaptureFormat();
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +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_; }
241
242 private:
243 typedef std::vector<VoiceChannel*> VoiceChannels;
244 typedef std::vector<VideoChannel*> VideoChannels;
245 typedef std::vector<DataChannel*> DataChannels;
246 typedef std::vector<Soundclip*> Soundclips;
247
248 void Construct(MediaEngineInterface* me,
249 DataEngineInterface* dme,
250 DeviceManagerInterface* dm,
251 CaptureManager* cm,
252 talk_base::Thread* worker_thread);
253 void Terminate_w();
254 VoiceChannel* CreateVoiceChannel_w(
255 BaseSession* session, const std::string& content_name, bool rtcp);
256 void DestroyVoiceChannel_w(VoiceChannel* voice_channel);
257 VideoChannel* CreateVideoChannel_w(
258 BaseSession* session, const std::string& content_name, bool rtcp,
259 VoiceChannel* voice_channel);
260 void DestroyVideoChannel_w(VideoChannel* video_channel);
261 DataChannel* CreateDataChannel_w(
262 BaseSession* session, const std::string& content_name,
263 bool rtcp, DataChannelType data_channel_type);
264 void DestroyDataChannel_w(DataChannel* data_channel);
265 Soundclip* CreateSoundclip_w();
266 void DestroySoundclip_w(Soundclip* soundclip);
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000267 bool SetAudioOptions_w(const AudioOptions& options, int delay_offset,
268 const Device* in_dev, const Device* out_dev);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269 bool SetCaptureDevice_w(const Device* cam_device);
270 void OnVideoCaptureStateChange(VideoCapturer* capturer,
271 CaptureState result);
272 bool RegisterVideoProcessor_w(VideoCapturer* capturer,
273 VideoProcessor* processor);
274 bool UnregisterVideoProcessor_w(VideoCapturer* capturer,
275 VideoProcessor* processor);
276 bool IsScreencastRunning_w() const;
277 virtual void OnMessage(talk_base::Message *message);
278
279 talk_base::scoped_ptr<MediaEngineInterface> media_engine_;
280 talk_base::scoped_ptr<DataEngineInterface> data_media_engine_;
281 talk_base::scoped_ptr<DeviceManagerInterface> device_manager_;
282 talk_base::scoped_ptr<CaptureManager> capture_manager_;
283 bool initialized_;
284 talk_base::Thread* main_thread_;
285 talk_base::Thread* worker_thread_;
286
287 VoiceChannels voice_channels_;
288 VideoChannels video_channels_;
289 DataChannels data_channels_;
290 Soundclips soundclips_;
291
292 std::string audio_in_device_;
293 std::string audio_out_device_;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000294 AudioOptions audio_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295 int audio_delay_offset_;
296 int audio_output_volume_;
297 std::string camera_device_;
298 VideoEncoderConfig default_video_encoder_config_;
299 VideoRenderer* local_renderer_;
300 bool enable_rtx_;
301
302 bool capturing_;
303 bool monitoring_;
304
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305 // String containing currently set device. Note that this string is subtly
306 // different from camera_device_. E.g. camera_device_ will list unplugged
307 // but selected devices while this sting will be empty or contain current
308 // selected device.
309 // TODO(hellner): refactor the code such that there is no need to keep two
310 // strings for video devices that have subtle differences in behavior.
311 std::string video_device_name_;
312};
313
314} // namespace cricket
315
316#endif // TALK_SESSION_MEDIA_CHANNELMANAGER_H_