blob: f4706e9cac9bde42d63979dc586adff9228fd599 [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
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087 // Configuration
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000088 // Gets global audio options.
89 virtual AudioOptions GetAudioOptions() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 // Sets global audio options. "options" are from AudioOptions, above.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000091 virtual bool SetAudioOptions(const AudioOptions& options) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 // Sets the value used by the echo canceller to offset delay values obtained
93 // from the OS.
94 virtual bool SetAudioDelayOffset(int offset) = 0;
95 // Sets the default (maximum) codec/resolution and encoder option to capture
96 // and encode video.
97 virtual bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config)
98 = 0;
99
100 // Device selection
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 virtual bool SetSoundDevices(const Device* in_device,
102 const Device* out_device) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103
104 // Device configuration
105 // Gets the current speaker volume, as a value between 0 and 255.
106 virtual bool GetOutputVolume(int* level) = 0;
107 // Sets the current speaker volume, as a value between 0 and 255.
108 virtual bool SetOutputVolume(int level) = 0;
109
110 // Local monitoring
111 // Gets the current microphone level, as a value between 0 and 10.
112 virtual int GetInputLevel() = 0;
113 // Starts or stops the local microphone. Useful if local mic info is needed
114 // prior to a call being connected; the mic will be started automatically
115 // when a VoiceMediaChannel starts sending.
116 virtual bool SetLocalMonitor(bool enable) = 0;
117 // Installs a callback for raw frames from the local camera.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118
119 virtual const std::vector<AudioCodec>& audio_codecs() = 0;
120 virtual const std::vector<RtpHeaderExtension>&
121 audio_rtp_header_extensions() = 0;
122 virtual const std::vector<VideoCodec>& video_codecs() = 0;
123 virtual const std::vector<RtpHeaderExtension>&
124 video_rtp_header_extensions() = 0;
125
126 // Logging control
127 virtual void SetVoiceLogging(int min_sev, const char* filter) = 0;
128 virtual void SetVideoLogging(int min_sev, const char* filter) = 0;
129
wu@webrtc.orga9890802013-12-13 00:21:03 +0000130 // Starts AEC dump using existing file.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000131 virtual bool StartAecDump(rtc::PlatformFile file) = 0;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000132
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 // Voice processors for effects.
134 virtual bool RegisterVoiceProcessor(uint32 ssrc,
135 VoiceProcessor* video_processor,
136 MediaProcessorDirection direction) = 0;
137 virtual bool UnregisterVoiceProcessor(uint32 ssrc,
138 VoiceProcessor* video_processor,
139 MediaProcessorDirection direction) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140};
141
142
143#if !defined(DISABLE_MEDIA_ENGINE_FACTORY)
144class MediaEngineFactory {
145 public:
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000146 typedef cricket::MediaEngineInterface* (*MediaEngineCreateFunction)();
147 // Creates a media engine, using either the compiled system default or the
148 // creation function specified in SetCreateFunction, if specified.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149 static MediaEngineInterface* Create();
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000150 // Sets the function used when calling Create. If unset, the compiled system
151 // default will be used. Returns the old create function, or NULL if one
152 // wasn't set. Likewise, NULL can be used as the |function| parameter to
153 // reset to the default behavior.
154 static MediaEngineCreateFunction SetCreateFunction(
155 MediaEngineCreateFunction function);
156 private:
157 static MediaEngineCreateFunction create_function_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158};
159#endif
160
161// CompositeMediaEngine constructs a MediaEngine from separate
162// voice and video engine classes.
163template<class VOICE, class VIDEO>
164class CompositeMediaEngine : public MediaEngineInterface {
165 public:
pbos@webrtc.orgf1f0d9a2015-03-02 13:30:15 +0000166 CompositeMediaEngine() : video_(&voice_) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 virtual ~CompositeMediaEngine() {}
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000168 virtual bool Init(rtc::Thread* worker_thread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169 if (!voice_.Init(worker_thread))
170 return false;
171 if (!video_.Init(worker_thread)) {
172 voice_.Terminate();
173 return false;
174 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 return true;
176 }
177 virtual void Terminate() {
178 video_.Terminate();
179 voice_.Terminate();
180 }
181
182 virtual int GetCapabilities() {
183 return (voice_.GetCapabilities() | video_.GetCapabilities());
184 }
185 virtual VoiceMediaChannel *CreateChannel() {
186 return voice_.CreateChannel();
187 }
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000188 virtual VideoMediaChannel* CreateVideoChannel(const VideoOptions& options,
189 VoiceMediaChannel* channel) {
190 return video_.CreateChannel(options, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000193 virtual AudioOptions GetAudioOptions() const {
194 return voice_.GetOptions();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195 }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000196 virtual bool SetAudioOptions(const AudioOptions& options) {
197 return voice_.SetOptions(options);
198 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 virtual bool SetAudioDelayOffset(int offset) {
200 return voice_.SetDelayOffset(offset);
201 }
202 virtual bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config) {
203 return video_.SetDefaultEncoderConfig(config);
204 }
205
206 virtual bool SetSoundDevices(const Device* in_device,
207 const Device* out_device) {
208 return voice_.SetDevices(in_device, out_device);
209 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210
211 virtual bool GetOutputVolume(int* level) {
212 return voice_.GetOutputVolume(level);
213 }
214 virtual bool SetOutputVolume(int level) {
215 return voice_.SetOutputVolume(level);
216 }
217
218 virtual int GetInputLevel() {
219 return voice_.GetInputLevel();
220 }
221 virtual bool SetLocalMonitor(bool enable) {
222 return voice_.SetLocalMonitor(enable);
223 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000224 virtual const std::vector<AudioCodec>& audio_codecs() {
225 return voice_.codecs();
226 }
227 virtual const std::vector<RtpHeaderExtension>& audio_rtp_header_extensions() {
228 return voice_.rtp_header_extensions();
229 }
230 virtual const std::vector<VideoCodec>& video_codecs() {
231 return video_.codecs();
232 }
233 virtual const std::vector<RtpHeaderExtension>& video_rtp_header_extensions() {
234 return video_.rtp_header_extensions();
235 }
236
237 virtual void SetVoiceLogging(int min_sev, const char* filter) {
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000238 voice_.SetLogging(min_sev, filter);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239 }
240 virtual void SetVideoLogging(int min_sev, const char* filter) {
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000241 video_.SetLogging(min_sev, filter);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242 }
243
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000244 virtual bool StartAecDump(rtc::PlatformFile file) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000245 return voice_.StartAecDump(file);
246 }
247
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248 virtual bool RegisterVoiceProcessor(uint32 ssrc,
249 VoiceProcessor* processor,
250 MediaProcessorDirection direction) {
251 return voice_.RegisterProcessor(ssrc, processor, direction);
252 }
253 virtual bool UnregisterVoiceProcessor(uint32 ssrc,
254 VoiceProcessor* processor,
255 MediaProcessorDirection direction) {
256 return voice_.UnregisterProcessor(ssrc, processor, direction);
257 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258
259 protected:
260 VOICE voice_;
261 VIDEO video_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262};
263
264// NullVoiceEngine can be used with CompositeMediaEngine in the case where only
265// a video engine is desired.
266class NullVoiceEngine {
267 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000268 bool Init(rtc::Thread* worker_thread) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269 void Terminate() {}
270 int GetCapabilities() { return 0; }
271 // If you need this to return an actual channel, use FakeMediaEngine instead.
272 VoiceMediaChannel* CreateChannel() {
273 return NULL;
274 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275 bool SetDelayOffset(int offset) { return true; }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000276 AudioOptions GetOptions() const { return AudioOptions(); }
277 bool SetOptions(const AudioOptions& options) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278 bool SetDevices(const Device* in_device, const Device* out_device) {
279 return true;
280 }
281 bool GetOutputVolume(int* level) {
282 *level = 0;
283 return true;
284 }
285 bool SetOutputVolume(int level) { return true; }
286 int GetInputLevel() { return 0; }
287 bool SetLocalMonitor(bool enable) { return true; }
288 const std::vector<AudioCodec>& codecs() { return codecs_; }
289 const std::vector<RtpHeaderExtension>& rtp_header_extensions() {
290 return rtp_header_extensions_;
291 }
292 void SetLogging(int min_sev, const char* filter) {}
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000293 bool StartAecDump(rtc::PlatformFile file) { return false; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294 bool RegisterProcessor(uint32 ssrc,
295 VoiceProcessor* voice_processor,
296 MediaProcessorDirection direction) { return true; }
297 bool UnregisterProcessor(uint32 ssrc,
298 VoiceProcessor* voice_processor,
299 MediaProcessorDirection direction) { return true; }
300
301 private:
302 std::vector<AudioCodec> codecs_;
303 std::vector<RtpHeaderExtension> rtp_header_extensions_;
304};
305
306// NullVideoEngine can be used with CompositeMediaEngine in the case where only
307// a voice engine is desired.
308class NullVideoEngine {
309 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000310 bool Init(rtc::Thread* worker_thread) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311 void Terminate() {}
312 int GetCapabilities() { return 0; }
313 // If you need this to return an actual channel, use FakeMediaEngine instead.
314 VideoMediaChannel* CreateChannel(
Noah Richards99c2fe52015-04-10 14:32:39 -0700315 const VideoOptions& options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316 VoiceMediaChannel* voice_media_channel) {
317 return NULL;
318 }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000319 bool SetOptions(const VideoOptions& options) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320 bool SetDefaultEncoderConfig(const VideoEncoderConfig& config) {
321 return true;
322 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323 const std::vector<VideoCodec>& codecs() { return codecs_; }
324 const std::vector<RtpHeaderExtension>& rtp_header_extensions() {
325 return rtp_header_extensions_;
326 }
327 void SetLogging(int min_sev, const char* filter) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329 private:
330 std::vector<VideoCodec> codecs_;
331 std::vector<RtpHeaderExtension> rtp_header_extensions_;
332};
333
334typedef CompositeMediaEngine<NullVoiceEngine, NullVideoEngine> NullMediaEngine;
335
336enum DataChannelType {
337 DCT_NONE = 0,
338 DCT_RTP = 1,
339 DCT_SCTP = 2
340};
341
342class DataEngineInterface {
343 public:
344 virtual ~DataEngineInterface() {}
345 virtual DataMediaChannel* CreateChannel(DataChannelType type) = 0;
346 virtual const std::vector<DataCodec>& data_codecs() = 0;
347};
348
349} // namespace cricket
350
351#endif // TALK_MEDIA_BASE_MEDIAENGINE_H_