blob: 25f7eb3368969e0583f4373b80301fc9693de314 [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.
83 virtual VideoMediaChannel *CreateVideoChannel(
84 VoiceMediaChannel* voice_media_channel) = 0;
85
86 // Creates a soundclip object for playing sounds on. Returns NULL on failure.
87 virtual SoundclipMedia *CreateSoundclip() = 0;
88
89 // Configuration
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000090 // Gets global audio options.
91 virtual AudioOptions GetAudioOptions() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 // Sets global audio options. "options" are from AudioOptions, above.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000093 virtual bool SetAudioOptions(const AudioOptions& options) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 // Sets global video options. "options" are from VideoOptions, above.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000095 virtual bool SetVideoOptions(const VideoOptions& options) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 // Sets the value used by the echo canceller to offset delay values obtained
97 // from the OS.
98 virtual bool SetAudioDelayOffset(int offset) = 0;
99 // Sets the default (maximum) codec/resolution and encoder option to capture
100 // and encode video.
101 virtual bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config)
102 = 0;
wu@webrtc.org78187522013-10-07 23:32:02 +0000103 // Gets the default (maximum) codec/resolution and encoder option used to
104 // capture and encode video, as set by SetDefaultVideoEncoderConfig or the
105 // default from the video engine if not previously set.
106 virtual VideoEncoderConfig GetDefaultVideoEncoderConfig() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107
108 // Device selection
109 // TODO(tschmelcher): Add method for selecting the soundclip device.
110 virtual bool SetSoundDevices(const Device* in_device,
111 const Device* out_device) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112
113 // Device configuration
114 // Gets the current speaker volume, as a value between 0 and 255.
115 virtual bool GetOutputVolume(int* level) = 0;
116 // Sets the current speaker volume, as a value between 0 and 255.
117 virtual bool SetOutputVolume(int level) = 0;
118
119 // Local monitoring
120 // Gets the current microphone level, as a value between 0 and 10.
121 virtual int GetInputLevel() = 0;
122 // Starts or stops the local microphone. Useful if local mic info is needed
123 // prior to a call being connected; the mic will be started automatically
124 // when a VoiceMediaChannel starts sending.
125 virtual bool SetLocalMonitor(bool enable) = 0;
126 // Installs a callback for raw frames from the local camera.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127
128 virtual const std::vector<AudioCodec>& audio_codecs() = 0;
129 virtual const std::vector<RtpHeaderExtension>&
130 audio_rtp_header_extensions() = 0;
131 virtual const std::vector<VideoCodec>& video_codecs() = 0;
132 virtual const std::vector<RtpHeaderExtension>&
133 video_rtp_header_extensions() = 0;
134
135 // Logging control
136 virtual void SetVoiceLogging(int min_sev, const char* filter) = 0;
137 virtual void SetVideoLogging(int min_sev, const char* filter) = 0;
138
wu@webrtc.orga9890802013-12-13 00:21:03 +0000139 // Starts AEC dump using existing file.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000140 virtual bool StartAecDump(rtc::PlatformFile file) = 0;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000141
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142 // Voice processors for effects.
143 virtual bool RegisterVoiceProcessor(uint32 ssrc,
144 VoiceProcessor* video_processor,
145 MediaProcessorDirection direction) = 0;
146 virtual bool UnregisterVoiceProcessor(uint32 ssrc,
147 VoiceProcessor* video_processor,
148 MediaProcessorDirection direction) = 0;
149
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000150 virtual VideoFormat GetStartCaptureFormat() const = 0;
151
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152 virtual sigslot::repeater2<VideoCapturer*, CaptureState>&
153 SignalVideoCaptureStateChange() = 0;
154};
155
156
157#if !defined(DISABLE_MEDIA_ENGINE_FACTORY)
158class MediaEngineFactory {
159 public:
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000160 typedef cricket::MediaEngineInterface* (*MediaEngineCreateFunction)();
161 // Creates a media engine, using either the compiled system default or the
162 // creation function specified in SetCreateFunction, if specified.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 static MediaEngineInterface* Create();
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000164 // Sets the function used when calling Create. If unset, the compiled system
165 // default will be used. Returns the old create function, or NULL if one
166 // wasn't set. Likewise, NULL can be used as the |function| parameter to
167 // reset to the default behavior.
168 static MediaEngineCreateFunction SetCreateFunction(
169 MediaEngineCreateFunction function);
170 private:
171 static MediaEngineCreateFunction create_function_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172};
173#endif
174
175// CompositeMediaEngine constructs a MediaEngine from separate
176// voice and video engine classes.
177template<class VOICE, class VIDEO>
178class CompositeMediaEngine : public MediaEngineInterface {
179 public:
180 CompositeMediaEngine() {}
181 virtual ~CompositeMediaEngine() {}
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000182 virtual bool Init(rtc::Thread* worker_thread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 if (!voice_.Init(worker_thread))
184 return false;
185 if (!video_.Init(worker_thread)) {
186 voice_.Terminate();
187 return false;
188 }
189 SignalVideoCaptureStateChange().repeat(video_.SignalCaptureStateChange);
190 return true;
191 }
192 virtual void Terminate() {
193 video_.Terminate();
194 voice_.Terminate();
195 }
196
197 virtual int GetCapabilities() {
198 return (voice_.GetCapabilities() | video_.GetCapabilities());
199 }
200 virtual VoiceMediaChannel *CreateChannel() {
201 return voice_.CreateChannel();
202 }
203 virtual VideoMediaChannel *CreateVideoChannel(VoiceMediaChannel* channel) {
204 return video_.CreateChannel(channel);
205 }
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 }
216 virtual bool SetVideoOptions(const VideoOptions& options) {
217 return video_.SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218 }
219 virtual bool SetAudioDelayOffset(int offset) {
220 return voice_.SetDelayOffset(offset);
221 }
222 virtual bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config) {
223 return video_.SetDefaultEncoderConfig(config);
224 }
wu@webrtc.org78187522013-10-07 23:32:02 +0000225 virtual VideoEncoderConfig GetDefaultVideoEncoderConfig() const {
226 return video_.GetDefaultEncoderConfig();
227 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228
229 virtual bool SetSoundDevices(const Device* in_device,
230 const Device* out_device) {
231 return voice_.SetDevices(in_device, out_device);
232 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000233
234 virtual bool GetOutputVolume(int* level) {
235 return voice_.GetOutputVolume(level);
236 }
237 virtual bool SetOutputVolume(int level) {
238 return voice_.SetOutputVolume(level);
239 }
240
241 virtual int GetInputLevel() {
242 return voice_.GetInputLevel();
243 }
244 virtual bool SetLocalMonitor(bool enable) {
245 return voice_.SetLocalMonitor(enable);
246 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247 virtual const std::vector<AudioCodec>& audio_codecs() {
248 return voice_.codecs();
249 }
250 virtual const std::vector<RtpHeaderExtension>& audio_rtp_header_extensions() {
251 return voice_.rtp_header_extensions();
252 }
253 virtual const std::vector<VideoCodec>& video_codecs() {
254 return video_.codecs();
255 }
256 virtual const std::vector<RtpHeaderExtension>& video_rtp_header_extensions() {
257 return video_.rtp_header_extensions();
258 }
259
260 virtual void SetVoiceLogging(int min_sev, const char* filter) {
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000261 voice_.SetLogging(min_sev, filter);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262 }
263 virtual void SetVideoLogging(int min_sev, const char* filter) {
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000264 video_.SetLogging(min_sev, filter);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265 }
266
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000267 virtual bool StartAecDump(rtc::PlatformFile file) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000268 return voice_.StartAecDump(file);
269 }
270
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 virtual bool RegisterVoiceProcessor(uint32 ssrc,
272 VoiceProcessor* processor,
273 MediaProcessorDirection direction) {
274 return voice_.RegisterProcessor(ssrc, processor, direction);
275 }
276 virtual bool UnregisterVoiceProcessor(uint32 ssrc,
277 VoiceProcessor* processor,
278 MediaProcessorDirection direction) {
279 return voice_.UnregisterProcessor(ssrc, processor, direction);
280 }
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000281 virtual VideoFormat GetStartCaptureFormat() const {
282 return video_.GetStartCaptureFormat();
283 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 virtual sigslot::repeater2<VideoCapturer*, CaptureState>&
285 SignalVideoCaptureStateChange() {
286 return signal_state_change_;
287 }
288
289 protected:
290 VOICE voice_;
291 VIDEO video_;
292 sigslot::repeater2<VideoCapturer*, CaptureState> signal_state_change_;
293};
294
295// NullVoiceEngine can be used with CompositeMediaEngine in the case where only
296// a video engine is desired.
297class NullVoiceEngine {
298 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000299 bool Init(rtc::Thread* worker_thread) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300 void Terminate() {}
301 int GetCapabilities() { return 0; }
302 // If you need this to return an actual channel, use FakeMediaEngine instead.
303 VoiceMediaChannel* CreateChannel() {
304 return NULL;
305 }
306 SoundclipMedia* CreateSoundclip() {
307 return NULL;
308 }
309 bool SetDelayOffset(int offset) { return true; }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000310 AudioOptions GetOptions() const { return AudioOptions(); }
311 bool SetOptions(const AudioOptions& options) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312 bool SetDevices(const Device* in_device, const Device* out_device) {
313 return true;
314 }
315 bool GetOutputVolume(int* level) {
316 *level = 0;
317 return true;
318 }
319 bool SetOutputVolume(int level) { return true; }
320 int GetInputLevel() { return 0; }
321 bool SetLocalMonitor(bool enable) { return true; }
322 const std::vector<AudioCodec>& codecs() { return codecs_; }
323 const std::vector<RtpHeaderExtension>& rtp_header_extensions() {
324 return rtp_header_extensions_;
325 }
326 void SetLogging(int min_sev, const char* filter) {}
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000327 bool StartAecDump(rtc::PlatformFile file) { return false; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328 bool RegisterProcessor(uint32 ssrc,
329 VoiceProcessor* voice_processor,
330 MediaProcessorDirection direction) { return true; }
331 bool UnregisterProcessor(uint32 ssrc,
332 VoiceProcessor* voice_processor,
333 MediaProcessorDirection direction) { return true; }
334
335 private:
336 std::vector<AudioCodec> codecs_;
337 std::vector<RtpHeaderExtension> rtp_header_extensions_;
338};
339
340// NullVideoEngine can be used with CompositeMediaEngine in the case where only
341// a voice engine is desired.
342class NullVideoEngine {
343 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000344 bool Init(rtc::Thread* worker_thread) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345 void Terminate() {}
346 int GetCapabilities() { return 0; }
347 // If you need this to return an actual channel, use FakeMediaEngine instead.
348 VideoMediaChannel* CreateChannel(
349 VoiceMediaChannel* voice_media_channel) {
350 return NULL;
351 }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000352 bool SetOptions(const VideoOptions& options) { return true; }
wu@webrtc.org78187522013-10-07 23:32:02 +0000353 VideoEncoderConfig GetDefaultEncoderConfig() const {
354 return VideoEncoderConfig();
355 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356 bool SetDefaultEncoderConfig(const VideoEncoderConfig& config) {
357 return true;
358 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359 const std::vector<VideoCodec>& codecs() { return codecs_; }
360 const std::vector<RtpHeaderExtension>& rtp_header_extensions() {
361 return rtp_header_extensions_;
362 }
363 void SetLogging(int min_sev, const char* filter) {}
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000364 VideoFormat GetStartCaptureFormat() const { return VideoFormat(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000365
366 sigslot::signal2<VideoCapturer*, CaptureState> SignalCaptureStateChange;
367 private:
368 std::vector<VideoCodec> codecs_;
369 std::vector<RtpHeaderExtension> rtp_header_extensions_;
370};
371
372typedef CompositeMediaEngine<NullVoiceEngine, NullVideoEngine> NullMediaEngine;
373
374enum DataChannelType {
375 DCT_NONE = 0,
376 DCT_RTP = 1,
377 DCT_SCTP = 2
378};
379
380class DataEngineInterface {
381 public:
382 virtual ~DataEngineInterface() {}
383 virtual DataMediaChannel* CreateChannel(DataChannelType type) = 0;
384 virtual const std::vector<DataCodec>& data_codecs() = 0;
385};
386
387} // namespace cricket
388
389#endif // TALK_MEDIA_BASE_MEDIAENGINE_H_