blob: 08addc46400553df42bc9946bf9f118a63d0507c [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
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035#include <string>
36#include <vector>
37
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038#include "talk/media/base/codec.h"
39#include "talk/media/base/mediachannel.h"
40#include "talk/media/base/mediacommon.h"
41#include "talk/media/base/videocapturer.h"
42#include "talk/media/base/videocommon.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043#include "talk/media/base/voiceprocessor.h"
44#include "talk/media/devices/devicemanager.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000045#include "webrtc/base/fileutils.h"
46#include "webrtc/base/sigslotrepeater.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047
48#if defined(GOOGLE_CHROME_BUILD) || defined(CHROMIUM_BUILD)
49#define DISABLE_MEDIA_ENGINE_FACTORY
50#endif
51
Fredrik Solenberg709ed672015-09-15 12:26:33 +020052namespace webrtc {
53class Call;
54class VoiceEngine;
55}
56
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057namespace cricket {
58
59class VideoCapturer;
60
61// MediaEngineInterface is an abstraction of a media engine which can be
62// subclassed to support different media componentry backends.
63// It supports voice and video operations in the same class to facilitate
64// proper synchronization between both media types.
65class MediaEngineInterface {
66 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067 // Default value to be used for SetAudioDelayOffset().
68 static const int kDefaultAudioDelayOffset;
69
70 virtual ~MediaEngineInterface() {}
71
72 // Initialization
73 // Starts the engine.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000074 virtual bool Init(rtc::Thread* worker_thread) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 // Shuts down the engine.
76 virtual void Terminate() = 0;
77 // Returns what the engine is capable of, as a set of Capabilities, above.
78 virtual int GetCapabilities() = 0;
Fredrik Solenberg709ed672015-09-15 12:26:33 +020079 // TODO(solenberg): Remove once VoE API refactoring is done.
80 virtual webrtc::VoiceEngine* GetVoE() = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081
82 // MediaChannel creation
83 // Creates a voice media channel. Returns NULL on failure.
Fredrik Solenberg709ed672015-09-15 12:26:33 +020084 virtual VoiceMediaChannel* CreateChannel(
85 webrtc::Call* call,
86 const AudioOptions& options) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087 // Creates a video media channel, paired with the specified voice channel.
88 // Returns NULL on failure.
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +000089 virtual VideoMediaChannel* CreateVideoChannel(
Fredrik Solenberg709ed672015-09-15 12:26:33 +020090 webrtc::Call* call,
91 const VideoOptions& options) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 // Configuration
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000094 // Gets global audio options.
95 virtual AudioOptions GetAudioOptions() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 // Sets global audio options. "options" are from AudioOptions, above.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000097 virtual bool SetAudioOptions(const AudioOptions& options) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 // Sets the value used by the echo canceller to offset delay values obtained
99 // from the OS.
100 virtual bool SetAudioDelayOffset(int offset) = 0;
101 // Sets the default (maximum) codec/resolution and encoder option to capture
102 // and encode video.
103 virtual bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config)
104 = 0;
105
106 // Device selection
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107 virtual bool SetSoundDevices(const Device* in_device,
108 const Device* out_device) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109
110 // Device configuration
111 // Gets the current speaker volume, as a value between 0 and 255.
112 virtual bool GetOutputVolume(int* level) = 0;
113 // Sets the current speaker volume, as a value between 0 and 255.
114 virtual bool SetOutputVolume(int level) = 0;
115
116 // Local monitoring
117 // Gets the current microphone level, as a value between 0 and 10.
118 virtual int GetInputLevel() = 0;
119 // Starts or stops the local microphone. Useful if local mic info is needed
120 // prior to a call being connected; the mic will be started automatically
121 // when a VoiceMediaChannel starts sending.
122 virtual bool SetLocalMonitor(bool enable) = 0;
123 // Installs a callback for raw frames from the local camera.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124
125 virtual const std::vector<AudioCodec>& audio_codecs() = 0;
126 virtual const std::vector<RtpHeaderExtension>&
127 audio_rtp_header_extensions() = 0;
128 virtual const std::vector<VideoCodec>& video_codecs() = 0;
129 virtual const std::vector<RtpHeaderExtension>&
130 video_rtp_header_extensions() = 0;
131
132 // Logging control
133 virtual void SetVoiceLogging(int min_sev, const char* filter) = 0;
134 virtual void SetVideoLogging(int min_sev, const char* filter) = 0;
135
wu@webrtc.orga9890802013-12-13 00:21:03 +0000136 // Starts AEC dump using existing file.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000137 virtual bool StartAecDump(rtc::PlatformFile file) = 0;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000138
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 // Voice processors for effects.
140 virtual bool RegisterVoiceProcessor(uint32 ssrc,
141 VoiceProcessor* video_processor,
142 MediaProcessorDirection direction) = 0;
143 virtual bool UnregisterVoiceProcessor(uint32 ssrc,
144 VoiceProcessor* video_processor,
145 MediaProcessorDirection direction) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146};
147
148
149#if !defined(DISABLE_MEDIA_ENGINE_FACTORY)
150class MediaEngineFactory {
151 public:
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000152 typedef cricket::MediaEngineInterface* (*MediaEngineCreateFunction)();
153 // Creates a media engine, using either the compiled system default or the
154 // creation function specified in SetCreateFunction, if specified.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155 static MediaEngineInterface* Create();
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000156 // Sets the function used when calling Create. If unset, the compiled system
157 // default will be used. Returns the old create function, or NULL if one
158 // wasn't set. Likewise, NULL can be used as the |function| parameter to
159 // reset to the default behavior.
160 static MediaEngineCreateFunction SetCreateFunction(
161 MediaEngineCreateFunction function);
162 private:
163 static MediaEngineCreateFunction create_function_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164};
165#endif
166
167// CompositeMediaEngine constructs a MediaEngine from separate
168// voice and video engine classes.
169template<class VOICE, class VIDEO>
170class CompositeMediaEngine : public MediaEngineInterface {
171 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172 virtual ~CompositeMediaEngine() {}
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000173 virtual bool Init(rtc::Thread* worker_thread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174 if (!voice_.Init(worker_thread))
175 return false;
Fredrik Solenberg9a416bd2015-05-22 09:04:09 +0200176 video_.Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 return true;
178 }
179 virtual void Terminate() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 voice_.Terminate();
181 }
182
183 virtual int GetCapabilities() {
184 return (voice_.GetCapabilities() | video_.GetCapabilities());
185 }
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200186 virtual webrtc::VoiceEngine* GetVoE() {
187 return voice_.GetVoE();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 }
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200189 virtual VoiceMediaChannel* CreateChannel(webrtc::Call* call,
190 const AudioOptions& options) {
191 return voice_.CreateChannel(call, options);
192 }
193 virtual VideoMediaChannel* CreateVideoChannel(webrtc::Call* call,
194 const VideoOptions& options) {
195 return video_.CreateChannel(call, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000198 virtual AudioOptions GetAudioOptions() const {
199 return voice_.GetOptions();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200 }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000201 virtual bool SetAudioOptions(const AudioOptions& options) {
202 return voice_.SetOptions(options);
203 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204 virtual bool SetAudioDelayOffset(int offset) {
205 return voice_.SetDelayOffset(offset);
206 }
207 virtual bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config) {
208 return video_.SetDefaultEncoderConfig(config);
209 }
210
211 virtual bool SetSoundDevices(const Device* in_device,
212 const Device* out_device) {
213 return voice_.SetDevices(in_device, out_device);
214 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215
216 virtual bool GetOutputVolume(int* level) {
217 return voice_.GetOutputVolume(level);
218 }
219 virtual bool SetOutputVolume(int level) {
220 return voice_.SetOutputVolume(level);
221 }
222
223 virtual int GetInputLevel() {
224 return voice_.GetInputLevel();
225 }
226 virtual bool SetLocalMonitor(bool enable) {
227 return voice_.SetLocalMonitor(enable);
228 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229 virtual const std::vector<AudioCodec>& audio_codecs() {
230 return voice_.codecs();
231 }
232 virtual const std::vector<RtpHeaderExtension>& audio_rtp_header_extensions() {
233 return voice_.rtp_header_extensions();
234 }
235 virtual const std::vector<VideoCodec>& video_codecs() {
236 return video_.codecs();
237 }
238 virtual const std::vector<RtpHeaderExtension>& video_rtp_header_extensions() {
239 return video_.rtp_header_extensions();
240 }
241
242 virtual void SetVoiceLogging(int min_sev, const char* filter) {
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000243 voice_.SetLogging(min_sev, filter);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244 }
245 virtual void SetVideoLogging(int min_sev, const char* filter) {
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000246 video_.SetLogging(min_sev, filter);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247 }
248
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000249 virtual bool StartAecDump(rtc::PlatformFile file) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000250 return voice_.StartAecDump(file);
251 }
252
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253 virtual bool RegisterVoiceProcessor(uint32 ssrc,
254 VoiceProcessor* processor,
255 MediaProcessorDirection direction) {
256 return voice_.RegisterProcessor(ssrc, processor, direction);
257 }
258 virtual bool UnregisterVoiceProcessor(uint32 ssrc,
259 VoiceProcessor* processor,
260 MediaProcessorDirection direction) {
261 return voice_.UnregisterProcessor(ssrc, processor, direction);
262 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263
264 protected:
265 VOICE voice_;
266 VIDEO video_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267};
268
269// NullVoiceEngine can be used with CompositeMediaEngine in the case where only
270// a video engine is desired.
271class NullVoiceEngine {
272 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000273 bool Init(rtc::Thread* worker_thread) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 void Terminate() {}
275 int GetCapabilities() { return 0; }
276 // If you need this to return an actual channel, use FakeMediaEngine instead.
Jelena Marusicc28a8962015-05-29 15:05:44 +0200277 VoiceMediaChannel* CreateChannel(const AudioOptions& options) {
278 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280 bool SetDelayOffset(int offset) { return true; }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000281 AudioOptions GetOptions() const { return AudioOptions(); }
282 bool SetOptions(const AudioOptions& options) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283 bool SetDevices(const Device* in_device, const Device* out_device) {
284 return true;
285 }
286 bool GetOutputVolume(int* level) {
287 *level = 0;
288 return true;
289 }
290 bool SetOutputVolume(int level) { return true; }
291 int GetInputLevel() { return 0; }
292 bool SetLocalMonitor(bool enable) { return true; }
293 const std::vector<AudioCodec>& codecs() { return codecs_; }
294 const std::vector<RtpHeaderExtension>& rtp_header_extensions() {
295 return rtp_header_extensions_;
296 }
297 void SetLogging(int min_sev, const char* filter) {}
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000298 bool StartAecDump(rtc::PlatformFile file) { return false; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299 bool RegisterProcessor(uint32 ssrc,
300 VoiceProcessor* voice_processor,
301 MediaProcessorDirection direction) { return true; }
302 bool UnregisterProcessor(uint32 ssrc,
303 VoiceProcessor* voice_processor,
304 MediaProcessorDirection direction) { return true; }
305
306 private:
307 std::vector<AudioCodec> codecs_;
308 std::vector<RtpHeaderExtension> rtp_header_extensions_;
309};
310
311// NullVideoEngine can be used with CompositeMediaEngine in the case where only
312// a voice engine is desired.
313class NullVideoEngine {
314 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000315 bool Init(rtc::Thread* worker_thread) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316 void Terminate() {}
317 int GetCapabilities() { return 0; }
318 // If you need this to return an actual channel, use FakeMediaEngine instead.
319 VideoMediaChannel* CreateChannel(
Noah Richards99c2fe52015-04-10 14:32:39 -0700320 const VideoOptions& options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321 VoiceMediaChannel* voice_media_channel) {
322 return NULL;
323 }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000324 bool SetOptions(const VideoOptions& options) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325 bool SetDefaultEncoderConfig(const VideoEncoderConfig& config) {
326 return true;
327 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328 const std::vector<VideoCodec>& codecs() { return codecs_; }
329 const std::vector<RtpHeaderExtension>& rtp_header_extensions() {
330 return rtp_header_extensions_;
331 }
332 void SetLogging(int min_sev, const char* filter) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000334 private:
335 std::vector<VideoCodec> codecs_;
336 std::vector<RtpHeaderExtension> rtp_header_extensions_;
337};
338
339typedef CompositeMediaEngine<NullVoiceEngine, NullVideoEngine> NullMediaEngine;
340
341enum DataChannelType {
342 DCT_NONE = 0,
343 DCT_RTP = 1,
344 DCT_SCTP = 2
345};
346
347class DataEngineInterface {
348 public:
349 virtual ~DataEngineInterface() {}
350 virtual DataMediaChannel* CreateChannel(DataChannelType type) = 0;
351 virtual const std::vector<DataCodec>& data_codecs() = 0;
352};
353
354} // namespace cricket
355
356#endif // TALK_MEDIA_BASE_MEDIAENGINE_H_