blob: bccae99515abd845cdec6eabe635368acb78a094 [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
35#include <climits>
36#include <string>
37#include <vector>
38
wu@webrtc.orga8910d22014-01-23 22:12:45 +000039#include "talk/base/fileutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040#include "talk/base/sigslotrepeater.h"
41#include "talk/media/base/codec.h"
42#include "talk/media/base/mediachannel.h"
43#include "talk/media/base/mediacommon.h"
44#include "talk/media/base/videocapturer.h"
45#include "talk/media/base/videocommon.h"
46#include "talk/media/base/videoprocessor.h"
47#include "talk/media/base/voiceprocessor.h"
48#include "talk/media/devices/devicemanager.h"
49
50#if defined(GOOGLE_CHROME_BUILD) || defined(CHROMIUM_BUILD)
51#define DISABLE_MEDIA_ENGINE_FACTORY
52#endif
53
54namespace cricket {
55
56class VideoCapturer;
57
58// MediaEngineInterface is an abstraction of a media engine which can be
59// subclassed to support different media componentry backends.
60// It supports voice and video operations in the same class to facilitate
61// proper synchronization between both media types.
62class MediaEngineInterface {
63 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064 // Default value to be used for SetAudioDelayOffset().
65 static const int kDefaultAudioDelayOffset;
66
67 virtual ~MediaEngineInterface() {}
68
69 // Initialization
70 // Starts the engine.
71 virtual bool Init(talk_base::Thread* worker_thread) = 0;
72 // Shuts down the engine.
73 virtual void Terminate() = 0;
74 // Returns what the engine is capable of, as a set of Capabilities, above.
75 virtual int GetCapabilities() = 0;
76
77 // MediaChannel creation
78 // Creates a voice media channel. Returns NULL on failure.
79 virtual VoiceMediaChannel *CreateChannel() = 0;
80 // Creates a video media channel, paired with the specified voice channel.
81 // Returns NULL on failure.
82 virtual VideoMediaChannel *CreateVideoChannel(
83 VoiceMediaChannel* voice_media_channel) = 0;
84
85 // Creates a soundclip object for playing sounds on. Returns NULL on failure.
86 virtual SoundclipMedia *CreateSoundclip() = 0;
87
88 // 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 global video options. "options" are from VideoOptions, above.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000094 virtual bool SetVideoOptions(const VideoOptions& 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.
126 virtual bool SetLocalRenderer(VideoRenderer* renderer) = 0;
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.
wu@webrtc.orga8910d22014-01-23 22:12:45 +0000140 virtual bool StartAecDump(talk_base::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
150 virtual VideoFormat GetStartCaptureFormat() const = 0;
151
152 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() {}
182 virtual bool Init(talk_base::Thread* worker_thread) {
183 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 }
247 virtual bool SetLocalRenderer(VideoRenderer* renderer) {
248 return video_.SetLocalRenderer(renderer);
249 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250
251 virtual const std::vector<AudioCodec>& audio_codecs() {
252 return voice_.codecs();
253 }
254 virtual const std::vector<RtpHeaderExtension>& audio_rtp_header_extensions() {
255 return voice_.rtp_header_extensions();
256 }
257 virtual const std::vector<VideoCodec>& video_codecs() {
258 return video_.codecs();
259 }
260 virtual const std::vector<RtpHeaderExtension>& video_rtp_header_extensions() {
261 return video_.rtp_header_extensions();
262 }
263
264 virtual void SetVoiceLogging(int min_sev, const char* filter) {
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000265 voice_.SetLogging(min_sev, filter);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266 }
267 virtual void SetVideoLogging(int min_sev, const char* filter) {
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000268 video_.SetLogging(min_sev, filter);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269 }
270
wu@webrtc.orga8910d22014-01-23 22:12:45 +0000271 virtual bool StartAecDump(talk_base::PlatformFile file) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000272 return voice_.StartAecDump(file);
273 }
274
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275 virtual bool RegisterVoiceProcessor(uint32 ssrc,
276 VoiceProcessor* processor,
277 MediaProcessorDirection direction) {
278 return voice_.RegisterProcessor(ssrc, processor, direction);
279 }
280 virtual bool UnregisterVoiceProcessor(uint32 ssrc,
281 VoiceProcessor* processor,
282 MediaProcessorDirection direction) {
283 return voice_.UnregisterProcessor(ssrc, processor, direction);
284 }
285 virtual VideoFormat GetStartCaptureFormat() const {
286 return video_.GetStartCaptureFormat();
287 }
288 virtual sigslot::repeater2<VideoCapturer*, CaptureState>&
289 SignalVideoCaptureStateChange() {
290 return signal_state_change_;
291 }
292
293 protected:
294 VOICE voice_;
295 VIDEO video_;
296 sigslot::repeater2<VideoCapturer*, CaptureState> signal_state_change_;
297};
298
299// NullVoiceEngine can be used with CompositeMediaEngine in the case where only
300// a video engine is desired.
301class NullVoiceEngine {
302 public:
303 bool Init(talk_base::Thread* worker_thread) { return true; }
304 void Terminate() {}
305 int GetCapabilities() { return 0; }
306 // If you need this to return an actual channel, use FakeMediaEngine instead.
307 VoiceMediaChannel* CreateChannel() {
308 return NULL;
309 }
310 SoundclipMedia* CreateSoundclip() {
311 return NULL;
312 }
313 bool SetDelayOffset(int offset) { return true; }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000314 AudioOptions GetOptions() const { return AudioOptions(); }
315 bool SetOptions(const AudioOptions& options) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316 bool SetDevices(const Device* in_device, const Device* out_device) {
317 return true;
318 }
319 bool GetOutputVolume(int* level) {
320 *level = 0;
321 return true;
322 }
323 bool SetOutputVolume(int level) { return true; }
324 int GetInputLevel() { return 0; }
325 bool SetLocalMonitor(bool enable) { return true; }
326 const std::vector<AudioCodec>& codecs() { return codecs_; }
327 const std::vector<RtpHeaderExtension>& rtp_header_extensions() {
328 return rtp_header_extensions_;
329 }
330 void SetLogging(int min_sev, const char* filter) {}
wu@webrtc.orga8910d22014-01-23 22:12:45 +0000331 bool StartAecDump(talk_base::PlatformFile file) { return false; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332 bool RegisterProcessor(uint32 ssrc,
333 VoiceProcessor* voice_processor,
334 MediaProcessorDirection direction) { return true; }
335 bool UnregisterProcessor(uint32 ssrc,
336 VoiceProcessor* voice_processor,
337 MediaProcessorDirection direction) { return true; }
338
339 private:
340 std::vector<AudioCodec> codecs_;
341 std::vector<RtpHeaderExtension> rtp_header_extensions_;
342};
343
344// NullVideoEngine can be used with CompositeMediaEngine in the case where only
345// a voice engine is desired.
346class NullVideoEngine {
347 public:
348 bool Init(talk_base::Thread* worker_thread) { return true; }
349 void Terminate() {}
350 int GetCapabilities() { return 0; }
351 // If you need this to return an actual channel, use FakeMediaEngine instead.
352 VideoMediaChannel* CreateChannel(
353 VoiceMediaChannel* voice_media_channel) {
354 return NULL;
355 }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000356 bool SetOptions(const VideoOptions& options) { return true; }
wu@webrtc.org78187522013-10-07 23:32:02 +0000357 VideoEncoderConfig GetDefaultEncoderConfig() const {
358 return VideoEncoderConfig();
359 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000360 bool SetDefaultEncoderConfig(const VideoEncoderConfig& config) {
361 return true;
362 }
363 bool SetLocalRenderer(VideoRenderer* renderer) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364 const std::vector<VideoCodec>& codecs() { return codecs_; }
365 const std::vector<RtpHeaderExtension>& rtp_header_extensions() {
366 return rtp_header_extensions_;
367 }
368 void SetLogging(int min_sev, const char* filter) {}
369 VideoFormat GetStartCaptureFormat() const { return VideoFormat(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000370
371 sigslot::signal2<VideoCapturer*, CaptureState> SignalCaptureStateChange;
372 private:
373 std::vector<VideoCodec> codecs_;
374 std::vector<RtpHeaderExtension> rtp_header_extensions_;
375};
376
377typedef CompositeMediaEngine<NullVoiceEngine, NullVideoEngine> NullMediaEngine;
378
379enum DataChannelType {
380 DCT_NONE = 0,
381 DCT_RTP = 1,
382 DCT_SCTP = 2
383};
384
385class DataEngineInterface {
386 public:
387 virtual ~DataEngineInterface() {}
388 virtual DataMediaChannel* CreateChannel(DataChannelType type) = 0;
389 virtual const std::vector<DataCodec>& data_codecs() = 0;
390};
391
392} // namespace cricket
393
394#endif // TALK_MEDIA_BASE_MEDIAENGINE_H_