3 * Copyright 2004 Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #ifndef TALK_MEDIA_BASE_MEDIAENGINE_H_
29 #define TALK_MEDIA_BASE_MEDIAENGINE_H_
32 #include <CoreAudio/CoreAudio.h>
40 #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"
48 #include "webrtc/base/fileutils.h"
49 #include "webrtc/base/sigslotrepeater.h"
51 #if defined(GOOGLE_CHROME_BUILD) || defined(CHROMIUM_BUILD)
52 #define DISABLE_MEDIA_ENGINE_FACTORY
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.
63 class MediaEngineInterface {
65 // Default value to be used for SetAudioDelayOffset().
66 static const int kDefaultAudioDelayOffset;
68 virtual ~MediaEngineInterface() {}
72 virtual bool Init(rtc::Thread* worker_thread) = 0;
73 // 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;
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 const VideoOptions& options,
85 VoiceMediaChannel* voice_media_channel) = 0;
87 // Creates a soundclip object for playing sounds on. Returns NULL on failure.
88 virtual SoundclipMedia *CreateSoundclip() = 0;
91 // Gets global audio options.
92 virtual AudioOptions GetAudioOptions() const = 0;
93 // Sets global audio options. "options" are from AudioOptions, above.
94 virtual bool SetAudioOptions(const AudioOptions& options) = 0;
95 // Sets the value used by the echo canceller to offset delay values obtained
97 virtual bool SetAudioDelayOffset(int offset) = 0;
98 // Sets the default (maximum) codec/resolution and encoder option to capture
100 virtual bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config)
104 // TODO(tschmelcher): Add method for selecting the soundclip device.
105 virtual bool SetSoundDevices(const Device* in_device,
106 const Device* out_device) = 0;
108 // Device configuration
109 // Gets the current speaker volume, as a value between 0 and 255.
110 virtual bool GetOutputVolume(int* level) = 0;
111 // Sets the current speaker volume, as a value between 0 and 255.
112 virtual bool SetOutputVolume(int level) = 0;
115 // Gets the current microphone level, as a value between 0 and 10.
116 virtual int GetInputLevel() = 0;
117 // Starts or stops the local microphone. Useful if local mic info is needed
118 // prior to a call being connected; the mic will be started automatically
119 // when a VoiceMediaChannel starts sending.
120 virtual bool SetLocalMonitor(bool enable) = 0;
121 // Installs a callback for raw frames from the local camera.
123 virtual const std::vector<AudioCodec>& audio_codecs() = 0;
124 virtual const std::vector<RtpHeaderExtension>&
125 audio_rtp_header_extensions() = 0;
126 virtual const std::vector<VideoCodec>& video_codecs() = 0;
127 virtual const std::vector<RtpHeaderExtension>&
128 video_rtp_header_extensions() = 0;
131 virtual void SetVoiceLogging(int min_sev, const char* filter) = 0;
132 virtual void SetVideoLogging(int min_sev, const char* filter) = 0;
134 // Starts AEC dump using existing file.
135 virtual bool StartAecDump(rtc::PlatformFile file) = 0;
137 // Voice processors for effects.
138 virtual bool RegisterVoiceProcessor(uint32 ssrc,
139 VoiceProcessor* video_processor,
140 MediaProcessorDirection direction) = 0;
141 virtual bool UnregisterVoiceProcessor(uint32 ssrc,
142 VoiceProcessor* video_processor,
143 MediaProcessorDirection direction) = 0;
145 virtual VideoFormat GetStartCaptureFormat() const = 0;
147 virtual sigslot::repeater2<VideoCapturer*, CaptureState>&
148 SignalVideoCaptureStateChange() = 0;
152 #if !defined(DISABLE_MEDIA_ENGINE_FACTORY)
153 class MediaEngineFactory {
155 typedef cricket::MediaEngineInterface* (*MediaEngineCreateFunction)();
156 // Creates a media engine, using either the compiled system default or the
157 // creation function specified in SetCreateFunction, if specified.
158 static MediaEngineInterface* Create();
159 // Sets the function used when calling Create. If unset, the compiled system
160 // default will be used. Returns the old create function, or NULL if one
161 // wasn't set. Likewise, NULL can be used as the |function| parameter to
162 // reset to the default behavior.
163 static MediaEngineCreateFunction SetCreateFunction(
164 MediaEngineCreateFunction function);
166 static MediaEngineCreateFunction create_function_;
170 // CompositeMediaEngine constructs a MediaEngine from separate
171 // voice and video engine classes.
172 template<class VOICE, class VIDEO>
173 class CompositeMediaEngine : public MediaEngineInterface {
175 CompositeMediaEngine() {}
176 virtual ~CompositeMediaEngine() {}
177 virtual bool Init(rtc::Thread* worker_thread) {
178 if (!voice_.Init(worker_thread))
180 if (!video_.Init(worker_thread)) {
184 SignalVideoCaptureStateChange().repeat(video_.SignalCaptureStateChange);
187 virtual void Terminate() {
192 virtual int GetCapabilities() {
193 return (voice_.GetCapabilities() | video_.GetCapabilities());
195 virtual VoiceMediaChannel *CreateChannel() {
196 return voice_.CreateChannel();
198 virtual VideoMediaChannel* CreateVideoChannel(const VideoOptions& options,
199 VoiceMediaChannel* channel) {
200 return video_.CreateChannel(options, channel);
202 virtual SoundclipMedia *CreateSoundclip() {
203 return voice_.CreateSoundclip();
206 virtual AudioOptions GetAudioOptions() const {
207 return voice_.GetOptions();
209 virtual bool SetAudioOptions(const AudioOptions& options) {
210 return voice_.SetOptions(options);
212 virtual bool SetAudioDelayOffset(int offset) {
213 return voice_.SetDelayOffset(offset);
215 virtual bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config) {
216 return video_.SetDefaultEncoderConfig(config);
219 virtual bool SetSoundDevices(const Device* in_device,
220 const Device* out_device) {
221 return voice_.SetDevices(in_device, out_device);
224 virtual bool GetOutputVolume(int* level) {
225 return voice_.GetOutputVolume(level);
227 virtual bool SetOutputVolume(int level) {
228 return voice_.SetOutputVolume(level);
231 virtual int GetInputLevel() {
232 return voice_.GetInputLevel();
234 virtual bool SetLocalMonitor(bool enable) {
235 return voice_.SetLocalMonitor(enable);
237 virtual const std::vector<AudioCodec>& audio_codecs() {
238 return voice_.codecs();
240 virtual const std::vector<RtpHeaderExtension>& audio_rtp_header_extensions() {
241 return voice_.rtp_header_extensions();
243 virtual const std::vector<VideoCodec>& video_codecs() {
244 return video_.codecs();
246 virtual const std::vector<RtpHeaderExtension>& video_rtp_header_extensions() {
247 return video_.rtp_header_extensions();
250 virtual void SetVoiceLogging(int min_sev, const char* filter) {
251 voice_.SetLogging(min_sev, filter);
253 virtual void SetVideoLogging(int min_sev, const char* filter) {
254 video_.SetLogging(min_sev, filter);
257 virtual bool StartAecDump(rtc::PlatformFile file) {
258 return voice_.StartAecDump(file);
261 virtual bool RegisterVoiceProcessor(uint32 ssrc,
262 VoiceProcessor* processor,
263 MediaProcessorDirection direction) {
264 return voice_.RegisterProcessor(ssrc, processor, direction);
266 virtual bool UnregisterVoiceProcessor(uint32 ssrc,
267 VoiceProcessor* processor,
268 MediaProcessorDirection direction) {
269 return voice_.UnregisterProcessor(ssrc, processor, direction);
271 virtual VideoFormat GetStartCaptureFormat() const {
272 return video_.GetStartCaptureFormat();
274 virtual sigslot::repeater2<VideoCapturer*, CaptureState>&
275 SignalVideoCaptureStateChange() {
276 return signal_state_change_;
282 sigslot::repeater2<VideoCapturer*, CaptureState> signal_state_change_;
285 // NullVoiceEngine can be used with CompositeMediaEngine in the case where only
286 // a video engine is desired.
287 class NullVoiceEngine {
289 bool Init(rtc::Thread* worker_thread) { return true; }
291 int GetCapabilities() { return 0; }
292 // If you need this to return an actual channel, use FakeMediaEngine instead.
293 VoiceMediaChannel* CreateChannel() {
296 SoundclipMedia* CreateSoundclip() {
299 bool SetDelayOffset(int offset) { return true; }
300 AudioOptions GetOptions() const { return AudioOptions(); }
301 bool SetOptions(const AudioOptions& options) { return true; }
302 bool SetDevices(const Device* in_device, const Device* out_device) {
305 bool GetOutputVolume(int* level) {
309 bool SetOutputVolume(int level) { return true; }
310 int GetInputLevel() { return 0; }
311 bool SetLocalMonitor(bool enable) { return true; }
312 const std::vector<AudioCodec>& codecs() { return codecs_; }
313 const std::vector<RtpHeaderExtension>& rtp_header_extensions() {
314 return rtp_header_extensions_;
316 void SetLogging(int min_sev, const char* filter) {}
317 bool StartAecDump(rtc::PlatformFile file) { return false; }
318 bool RegisterProcessor(uint32 ssrc,
319 VoiceProcessor* voice_processor,
320 MediaProcessorDirection direction) { return true; }
321 bool UnregisterProcessor(uint32 ssrc,
322 VoiceProcessor* voice_processor,
323 MediaProcessorDirection direction) { return true; }
326 std::vector<AudioCodec> codecs_;
327 std::vector<RtpHeaderExtension> rtp_header_extensions_;
330 // NullVideoEngine can be used with CompositeMediaEngine in the case where only
331 // a voice engine is desired.
332 class NullVideoEngine {
334 bool Init(rtc::Thread* worker_thread) { return true; }
336 int GetCapabilities() { return 0; }
337 // If you need this to return an actual channel, use FakeMediaEngine instead.
338 VideoMediaChannel* CreateChannel(
339 VoiceMediaChannel* voice_media_channel) {
342 bool SetOptions(const VideoOptions& options) { return true; }
343 bool SetDefaultEncoderConfig(const VideoEncoderConfig& config) {
346 const std::vector<VideoCodec>& codecs() { return codecs_; }
347 const std::vector<RtpHeaderExtension>& rtp_header_extensions() {
348 return rtp_header_extensions_;
350 void SetLogging(int min_sev, const char* filter) {}
351 VideoFormat GetStartCaptureFormat() const { return VideoFormat(); }
353 sigslot::signal2<VideoCapturer*, CaptureState> SignalCaptureStateChange;
355 std::vector<VideoCodec> codecs_;
356 std::vector<RtpHeaderExtension> rtp_header_extensions_;
359 typedef CompositeMediaEngine<NullVoiceEngine, NullVideoEngine> NullMediaEngine;
361 enum DataChannelType {
367 class DataEngineInterface {
369 virtual ~DataEngineInterface() {}
370 virtual DataMediaChannel* CreateChannel(DataChannelType type) = 0;
371 virtual const std::vector<DataCodec>& data_codecs() = 0;
374 } // namespace cricket
376 #endif // TALK_MEDIA_BASE_MEDIAENGINE_H_