blob: 8468e07293aa6521f5b814a471db50314d815d2e [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 virtual ~MediaEngineInterface() {}
68
69 // Initialization
70 // Starts the engine.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000071 virtual bool Init(rtc::Thread* worker_thread) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072 // Shuts down the engine.
73 virtual void Terminate() = 0;
Fredrik Solenberg709ed672015-09-15 12:26:33 +020074 // TODO(solenberg): Remove once VoE API refactoring is done.
75 virtual webrtc::VoiceEngine* GetVoE() = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076
77 // MediaChannel creation
78 // Creates a voice media channel. Returns NULL on failure.
Fredrik Solenberg709ed672015-09-15 12:26:33 +020079 virtual VoiceMediaChannel* CreateChannel(
80 webrtc::Call* call,
81 const AudioOptions& options) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082 // Creates a video media channel, paired with the specified voice channel.
83 // Returns NULL on failure.
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +000084 virtual VideoMediaChannel* CreateVideoChannel(
Fredrik Solenberg709ed672015-09-15 12:26:33 +020085 webrtc::Call* call,
86 const VideoOptions& options) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 // Configuration
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000089 // Gets global audio options.
90 virtual AudioOptions GetAudioOptions() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 // Sets global audio options. "options" are from AudioOptions, above.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000092 virtual bool SetAudioOptions(const AudioOptions& options) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 // Sets the default (maximum) codec/resolution and encoder option to capture
94 // and encode video.
95 virtual bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config)
96 = 0;
97
98 // Device selection
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 virtual bool SetSoundDevices(const Device* in_device,
100 const Device* out_device) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101
102 // Device configuration
103 // Gets the current speaker volume, as a value between 0 and 255.
104 virtual bool GetOutputVolume(int* level) = 0;
105 // Sets the current speaker volume, as a value between 0 and 255.
106 virtual bool SetOutputVolume(int level) = 0;
107
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108 // Gets the current microphone level, as a value between 0 and 10.
109 virtual int GetInputLevel() = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110
111 virtual const std::vector<AudioCodec>& audio_codecs() = 0;
112 virtual const std::vector<RtpHeaderExtension>&
113 audio_rtp_header_extensions() = 0;
114 virtual const std::vector<VideoCodec>& video_codecs() = 0;
115 virtual const std::vector<RtpHeaderExtension>&
116 video_rtp_header_extensions() = 0;
117
118 // Logging control
119 virtual void SetVoiceLogging(int min_sev, const char* filter) = 0;
120 virtual void SetVideoLogging(int min_sev, const char* filter) = 0;
121
wu@webrtc.orga9890802013-12-13 00:21:03 +0000122 // Starts AEC dump using existing file.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000123 virtual bool StartAecDump(rtc::PlatformFile file) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124};
125
126
127#if !defined(DISABLE_MEDIA_ENGINE_FACTORY)
128class MediaEngineFactory {
129 public:
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000130 typedef cricket::MediaEngineInterface* (*MediaEngineCreateFunction)();
131 // Creates a media engine, using either the compiled system default or the
132 // creation function specified in SetCreateFunction, if specified.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 static MediaEngineInterface* Create();
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000134 // Sets the function used when calling Create. If unset, the compiled system
135 // default will be used. Returns the old create function, or NULL if one
136 // wasn't set. Likewise, NULL can be used as the |function| parameter to
137 // reset to the default behavior.
138 static MediaEngineCreateFunction SetCreateFunction(
139 MediaEngineCreateFunction function);
140 private:
141 static MediaEngineCreateFunction create_function_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142};
143#endif
144
145// CompositeMediaEngine constructs a MediaEngine from separate
146// voice and video engine classes.
147template<class VOICE, class VIDEO>
148class CompositeMediaEngine : public MediaEngineInterface {
149 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150 virtual ~CompositeMediaEngine() {}
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000151 virtual bool Init(rtc::Thread* worker_thread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152 if (!voice_.Init(worker_thread))
153 return false;
Fredrik Solenberg9a416bd2015-05-22 09:04:09 +0200154 video_.Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155 return true;
156 }
157 virtual void Terminate() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158 voice_.Terminate();
159 }
160
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200161 virtual webrtc::VoiceEngine* GetVoE() {
162 return voice_.GetVoE();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 }
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200164 virtual VoiceMediaChannel* CreateChannel(webrtc::Call* call,
165 const AudioOptions& options) {
166 return voice_.CreateChannel(call, options);
167 }
168 virtual VideoMediaChannel* CreateVideoChannel(webrtc::Call* call,
169 const VideoOptions& options) {
170 return video_.CreateChannel(call, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000173 virtual AudioOptions GetAudioOptions() const {
174 return voice_.GetOptions();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000176 virtual bool SetAudioOptions(const AudioOptions& options) {
177 return voice_.SetOptions(options);
178 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179 virtual bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config) {
180 return video_.SetDefaultEncoderConfig(config);
181 }
182
183 virtual bool SetSoundDevices(const Device* in_device,
184 const Device* out_device) {
185 return voice_.SetDevices(in_device, out_device);
186 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187
188 virtual bool GetOutputVolume(int* level) {
189 return voice_.GetOutputVolume(level);
190 }
191 virtual bool SetOutputVolume(int level) {
192 return voice_.SetOutputVolume(level);
193 }
194
195 virtual int GetInputLevel() {
196 return voice_.GetInputLevel();
197 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 virtual const std::vector<AudioCodec>& audio_codecs() {
199 return voice_.codecs();
200 }
201 virtual const std::vector<RtpHeaderExtension>& audio_rtp_header_extensions() {
202 return voice_.rtp_header_extensions();
203 }
204 virtual const std::vector<VideoCodec>& video_codecs() {
205 return video_.codecs();
206 }
207 virtual const std::vector<RtpHeaderExtension>& video_rtp_header_extensions() {
208 return video_.rtp_header_extensions();
209 }
210
211 virtual void SetVoiceLogging(int min_sev, const char* filter) {
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000212 voice_.SetLogging(min_sev, filter);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213 }
214 virtual void SetVideoLogging(int min_sev, const char* filter) {
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000215 video_.SetLogging(min_sev, filter);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216 }
217
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000218 virtual bool StartAecDump(rtc::PlatformFile file) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000219 return voice_.StartAecDump(file);
220 }
221
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000222 protected:
223 VOICE voice_;
224 VIDEO video_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225};
226
227// NullVoiceEngine can be used with CompositeMediaEngine in the case where only
228// a video engine is desired.
229class NullVoiceEngine {
230 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000231 bool Init(rtc::Thread* worker_thread) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 void Terminate() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000233 // If you need this to return an actual channel, use FakeMediaEngine instead.
Jelena Marusicc28a8962015-05-29 15:05:44 +0200234 VoiceMediaChannel* CreateChannel(const AudioOptions& options) {
235 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236 }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000237 AudioOptions GetOptions() const { return AudioOptions(); }
238 bool SetOptions(const AudioOptions& options) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239 bool SetDevices(const Device* in_device, const Device* out_device) {
240 return true;
241 }
242 bool GetOutputVolume(int* level) {
243 *level = 0;
244 return true;
245 }
246 bool SetOutputVolume(int level) { return true; }
247 int GetInputLevel() { return 0; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248 const std::vector<AudioCodec>& codecs() { return codecs_; }
249 const std::vector<RtpHeaderExtension>& rtp_header_extensions() {
250 return rtp_header_extensions_;
251 }
252 void SetLogging(int min_sev, const char* filter) {}
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000253 bool StartAecDump(rtc::PlatformFile file) { return false; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254
255 private:
256 std::vector<AudioCodec> codecs_;
257 std::vector<RtpHeaderExtension> rtp_header_extensions_;
258};
259
260// NullVideoEngine can be used with CompositeMediaEngine in the case where only
261// a voice engine is desired.
262class NullVideoEngine {
263 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000264 bool Init(rtc::Thread* worker_thread) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265 void Terminate() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266 // If you need this to return an actual channel, use FakeMediaEngine instead.
267 VideoMediaChannel* CreateChannel(
Noah Richards99c2fe52015-04-10 14:32:39 -0700268 const VideoOptions& options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269 VoiceMediaChannel* voice_media_channel) {
270 return NULL;
271 }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000272 bool SetOptions(const VideoOptions& options) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273 bool SetDefaultEncoderConfig(const VideoEncoderConfig& config) {
274 return true;
275 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276 const std::vector<VideoCodec>& codecs() { return codecs_; }
277 const std::vector<RtpHeaderExtension>& rtp_header_extensions() {
278 return rtp_header_extensions_;
279 }
280 void SetLogging(int min_sev, const char* filter) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282 private:
283 std::vector<VideoCodec> codecs_;
284 std::vector<RtpHeaderExtension> rtp_header_extensions_;
285};
286
287typedef CompositeMediaEngine<NullVoiceEngine, NullVideoEngine> NullMediaEngine;
288
289enum DataChannelType {
290 DCT_NONE = 0,
291 DCT_RTP = 1,
292 DCT_SCTP = 2
293};
294
295class DataEngineInterface {
296 public:
297 virtual ~DataEngineInterface() {}
298 virtual DataMediaChannel* CreateChannel(DataChannelType type) = 0;
299 virtual const std::vector<DataCodec>& data_codecs() = 0;
300};
301
302} // namespace cricket
303
304#endif // TALK_MEDIA_BASE_MEDIAENGINE_H_