blob: e7222f2d4e2899962ccc67f53c22941782b395a4 [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_MEDIA_BASE_MEDIAENGINE_H_
29#define TALK_MEDIA_BASE_MEDIAENGINE_H_
30
31#ifdef OSX
32#include <CoreAudio/CoreAudio.h>
33#endif
34
pbos@webrtc.org371243d2014-03-07 15:22:04 +000035#include <limits.h>
36
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037#include <string>
38#include <vector>
39
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040#include "talk/media/base/codec.h"
41#include "talk/media/base/mediachannel.h"
42#include "talk/media/base/mediacommon.h"
43#include "talk/media/base/videocapturer.h"
44#include "talk/media/base/videocommon.h"
45#include "talk/media/base/videoprocessor.h"
46#include "talk/media/base/voiceprocessor.h"
47#include "talk/media/devices/devicemanager.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000048#include "webrtc/base/fileutils.h"
49#include "webrtc/base/sigslotrepeater.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050
51#if defined(GOOGLE_CHROME_BUILD) || defined(CHROMIUM_BUILD)
52#define DISABLE_MEDIA_ENGINE_FACTORY
53#endif
54
55namespace cricket {
56
57class VideoCapturer;
58
59// MediaEngineInterface is an abstraction of a media engine which can be
60// subclassed to support different media componentry backends.
61// It supports voice and video operations in the same class to facilitate
62// proper synchronization between both media types.
63class MediaEngineInterface {
64 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065 // Default value to be used for SetAudioDelayOffset().
66 static const int kDefaultAudioDelayOffset;
67
68 virtual ~MediaEngineInterface() {}
69
70 // Initialization
71 // Starts the engine.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000072 virtual bool Init(rtc::Thread* worker_thread) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073 // Shuts down the engine.
74 virtual void Terminate() = 0;
75 // Returns what the engine is capable of, as a set of Capabilities, above.
76 virtual int GetCapabilities() = 0;
77
78 // MediaChannel creation
79 // Creates a voice media channel. Returns NULL on failure.
80 virtual VoiceMediaChannel *CreateChannel() = 0;
81 // Creates a video media channel, paired with the specified voice channel.
82 // Returns NULL on failure.
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +000083 virtual VideoMediaChannel* CreateVideoChannel(
84 const VideoOptions& options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085 VoiceMediaChannel* voice_media_channel) = 0;
86
87 // Creates a soundclip object for playing sounds on. Returns NULL on failure.
88 virtual SoundclipMedia *CreateSoundclip() = 0;
89
90 // Configuration
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000091 // Gets global audio options.
92 virtual AudioOptions GetAudioOptions() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 // Sets global audio options. "options" are from AudioOptions, above.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000094 virtual bool SetAudioOptions(const AudioOptions& options) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095 // Sets the value used by the echo canceller to offset delay values obtained
96 // from the OS.
97 virtual bool SetAudioDelayOffset(int offset) = 0;
98 // Sets the default (maximum) codec/resolution and encoder option to capture
99 // and encode video.
100 virtual bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config)
101 = 0;
wu@webrtc.org78187522013-10-07 23:32:02 +0000102 // Gets the default (maximum) codec/resolution and encoder option used to
103 // capture and encode video, as set by SetDefaultVideoEncoderConfig or the
104 // default from the video engine if not previously set.
105 virtual VideoEncoderConfig GetDefaultVideoEncoderConfig() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106
107 // Device selection
108 // TODO(tschmelcher): Add method for selecting the soundclip device.
109 virtual bool SetSoundDevices(const Device* in_device,
110 const Device* out_device) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111
112 // Device configuration
113 // Gets the current speaker volume, as a value between 0 and 255.
114 virtual bool GetOutputVolume(int* level) = 0;
115 // Sets the current speaker volume, as a value between 0 and 255.
116 virtual bool SetOutputVolume(int level) = 0;
117
118 // Local monitoring
119 // Gets the current microphone level, as a value between 0 and 10.
120 virtual int GetInputLevel() = 0;
121 // Starts or stops the local microphone. Useful if local mic info is needed
122 // prior to a call being connected; the mic will be started automatically
123 // when a VoiceMediaChannel starts sending.
124 virtual bool SetLocalMonitor(bool enable) = 0;
125 // Installs a callback for raw frames from the local camera.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126
127 virtual const std::vector<AudioCodec>& audio_codecs() = 0;
128 virtual const std::vector<RtpHeaderExtension>&
129 audio_rtp_header_extensions() = 0;
130 virtual const std::vector<VideoCodec>& video_codecs() = 0;
131 virtual const std::vector<RtpHeaderExtension>&
132 video_rtp_header_extensions() = 0;
133
134 // Logging control
135 virtual void SetVoiceLogging(int min_sev, const char* filter) = 0;
136 virtual void SetVideoLogging(int min_sev, const char* filter) = 0;
137
wu@webrtc.orga9890802013-12-13 00:21:03 +0000138 // Starts AEC dump using existing file.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000139 virtual bool StartAecDump(rtc::PlatformFile file) = 0;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000140
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141 // Voice processors for effects.
142 virtual bool RegisterVoiceProcessor(uint32 ssrc,
143 VoiceProcessor* video_processor,
144 MediaProcessorDirection direction) = 0;
145 virtual bool UnregisterVoiceProcessor(uint32 ssrc,
146 VoiceProcessor* video_processor,
147 MediaProcessorDirection direction) = 0;
148
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000149 virtual VideoFormat GetStartCaptureFormat() const = 0;
150
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151 virtual sigslot::repeater2<VideoCapturer*, CaptureState>&
152 SignalVideoCaptureStateChange() = 0;
153};
154
155
156#if !defined(DISABLE_MEDIA_ENGINE_FACTORY)
157class MediaEngineFactory {
158 public:
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000159 typedef cricket::MediaEngineInterface* (*MediaEngineCreateFunction)();
160 // Creates a media engine, using either the compiled system default or the
161 // creation function specified in SetCreateFunction, if specified.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162 static MediaEngineInterface* Create();
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000163 // Sets the function used when calling Create. If unset, the compiled system
164 // default will be used. Returns the old create function, or NULL if one
165 // wasn't set. Likewise, NULL can be used as the |function| parameter to
166 // reset to the default behavior.
167 static MediaEngineCreateFunction SetCreateFunction(
168 MediaEngineCreateFunction function);
169 private:
170 static MediaEngineCreateFunction create_function_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171};
172#endif
173
174// CompositeMediaEngine constructs a MediaEngine from separate
175// voice and video engine classes.
176template<class VOICE, class VIDEO>
177class CompositeMediaEngine : public MediaEngineInterface {
178 public:
179 CompositeMediaEngine() {}
180 virtual ~CompositeMediaEngine() {}
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000181 virtual bool Init(rtc::Thread* worker_thread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 if (!voice_.Init(worker_thread))
183 return false;
184 if (!video_.Init(worker_thread)) {
185 voice_.Terminate();
186 return false;
187 }
188 SignalVideoCaptureStateChange().repeat(video_.SignalCaptureStateChange);
189 return true;
190 }
191 virtual void Terminate() {
192 video_.Terminate();
193 voice_.Terminate();
194 }
195
196 virtual int GetCapabilities() {
197 return (voice_.GetCapabilities() | video_.GetCapabilities());
198 }
199 virtual VoiceMediaChannel *CreateChannel() {
200 return voice_.CreateChannel();
201 }
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000202 virtual VideoMediaChannel* CreateVideoChannel(const VideoOptions& options,
203 VoiceMediaChannel* channel) {
204 return video_.CreateChannel(options, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205 }
206 virtual SoundclipMedia *CreateSoundclip() {
207 return voice_.CreateSoundclip();
208 }
209
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000210 virtual AudioOptions GetAudioOptions() const {
211 return voice_.GetOptions();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000212 }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000213 virtual bool SetAudioOptions(const AudioOptions& options) {
214 return voice_.SetOptions(options);
215 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216 virtual bool SetAudioDelayOffset(int offset) {
217 return voice_.SetDelayOffset(offset);
218 }
219 virtual bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config) {
220 return video_.SetDefaultEncoderConfig(config);
221 }
wu@webrtc.org78187522013-10-07 23:32:02 +0000222 virtual VideoEncoderConfig GetDefaultVideoEncoderConfig() const {
223 return video_.GetDefaultEncoderConfig();
224 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225
226 virtual bool SetSoundDevices(const Device* in_device,
227 const Device* out_device) {
228 return voice_.SetDevices(in_device, out_device);
229 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230
231 virtual bool GetOutputVolume(int* level) {
232 return voice_.GetOutputVolume(level);
233 }
234 virtual bool SetOutputVolume(int level) {
235 return voice_.SetOutputVolume(level);
236 }
237
238 virtual int GetInputLevel() {
239 return voice_.GetInputLevel();
240 }
241 virtual bool SetLocalMonitor(bool enable) {
242 return voice_.SetLocalMonitor(enable);
243 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244 virtual const std::vector<AudioCodec>& audio_codecs() {
245 return voice_.codecs();
246 }
247 virtual const std::vector<RtpHeaderExtension>& audio_rtp_header_extensions() {
248 return voice_.rtp_header_extensions();
249 }
250 virtual const std::vector<VideoCodec>& video_codecs() {
251 return video_.codecs();
252 }
253 virtual const std::vector<RtpHeaderExtension>& video_rtp_header_extensions() {
254 return video_.rtp_header_extensions();
255 }
256
257 virtual void SetVoiceLogging(int min_sev, const char* filter) {
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000258 voice_.SetLogging(min_sev, filter);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259 }
260 virtual void SetVideoLogging(int min_sev, const char* filter) {
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000261 video_.SetLogging(min_sev, filter);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262 }
263
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000264 virtual bool StartAecDump(rtc::PlatformFile file) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000265 return voice_.StartAecDump(file);
266 }
267
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268 virtual bool RegisterVoiceProcessor(uint32 ssrc,
269 VoiceProcessor* processor,
270 MediaProcessorDirection direction) {
271 return voice_.RegisterProcessor(ssrc, processor, direction);
272 }
273 virtual bool UnregisterVoiceProcessor(uint32 ssrc,
274 VoiceProcessor* processor,
275 MediaProcessorDirection direction) {
276 return voice_.UnregisterProcessor(ssrc, processor, direction);
277 }
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000278 virtual VideoFormat GetStartCaptureFormat() const {
279 return video_.GetStartCaptureFormat();
280 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281 virtual sigslot::repeater2<VideoCapturer*, CaptureState>&
282 SignalVideoCaptureStateChange() {
283 return signal_state_change_;
284 }
285
286 protected:
287 VOICE voice_;
288 VIDEO video_;
289 sigslot::repeater2<VideoCapturer*, CaptureState> signal_state_change_;
290};
291
292// NullVoiceEngine can be used with CompositeMediaEngine in the case where only
293// a video engine is desired.
294class NullVoiceEngine {
295 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000296 bool Init(rtc::Thread* worker_thread) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297 void Terminate() {}
298 int GetCapabilities() { return 0; }
299 // If you need this to return an actual channel, use FakeMediaEngine instead.
300 VoiceMediaChannel* CreateChannel() {
301 return NULL;
302 }
303 SoundclipMedia* CreateSoundclip() {
304 return NULL;
305 }
306 bool SetDelayOffset(int offset) { return true; }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000307 AudioOptions GetOptions() const { return AudioOptions(); }
308 bool SetOptions(const AudioOptions& options) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309 bool SetDevices(const Device* in_device, const Device* out_device) {
310 return true;
311 }
312 bool GetOutputVolume(int* level) {
313 *level = 0;
314 return true;
315 }
316 bool SetOutputVolume(int level) { return true; }
317 int GetInputLevel() { return 0; }
318 bool SetLocalMonitor(bool enable) { return true; }
319 const std::vector<AudioCodec>& codecs() { return codecs_; }
320 const std::vector<RtpHeaderExtension>& rtp_header_extensions() {
321 return rtp_header_extensions_;
322 }
323 void SetLogging(int min_sev, const char* filter) {}
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000324 bool StartAecDump(rtc::PlatformFile file) { return false; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325 bool RegisterProcessor(uint32 ssrc,
326 VoiceProcessor* voice_processor,
327 MediaProcessorDirection direction) { return true; }
328 bool UnregisterProcessor(uint32 ssrc,
329 VoiceProcessor* voice_processor,
330 MediaProcessorDirection direction) { return true; }
331
332 private:
333 std::vector<AudioCodec> codecs_;
334 std::vector<RtpHeaderExtension> rtp_header_extensions_;
335};
336
337// NullVideoEngine can be used with CompositeMediaEngine in the case where only
338// a voice engine is desired.
339class NullVideoEngine {
340 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000341 bool Init(rtc::Thread* worker_thread) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342 void Terminate() {}
343 int GetCapabilities() { return 0; }
344 // If you need this to return an actual channel, use FakeMediaEngine instead.
345 VideoMediaChannel* CreateChannel(
346 VoiceMediaChannel* voice_media_channel) {
347 return NULL;
348 }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000349 bool SetOptions(const VideoOptions& options) { return true; }
wu@webrtc.org78187522013-10-07 23:32:02 +0000350 VideoEncoderConfig GetDefaultEncoderConfig() const {
351 return VideoEncoderConfig();
352 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353 bool SetDefaultEncoderConfig(const VideoEncoderConfig& config) {
354 return true;
355 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356 const std::vector<VideoCodec>& codecs() { return codecs_; }
357 const std::vector<RtpHeaderExtension>& rtp_header_extensions() {
358 return rtp_header_extensions_;
359 }
360 void SetLogging(int min_sev, const char* filter) {}
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000361 VideoFormat GetStartCaptureFormat() const { return VideoFormat(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362
363 sigslot::signal2<VideoCapturer*, CaptureState> SignalCaptureStateChange;
364 private:
365 std::vector<VideoCodec> codecs_;
366 std::vector<RtpHeaderExtension> rtp_header_extensions_;
367};
368
369typedef CompositeMediaEngine<NullVoiceEngine, NullVideoEngine> NullMediaEngine;
370
371enum DataChannelType {
372 DCT_NONE = 0,
373 DCT_RTP = 1,
374 DCT_SCTP = 2
375};
376
377class DataEngineInterface {
378 public:
379 virtual ~DataEngineInterface() {}
380 virtual DataMediaChannel* CreateChannel(DataChannelType type) = 0;
381 virtual const std::vector<DataCodec>& data_codecs() = 0;
382};
383
384} // namespace cricket
385
386#endif // TALK_MEDIA_BASE_MEDIAENGINE_H_