blob: 93586bb5b2eafca673d53aeb6cc888575b3de43d [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:
160 static MediaEngineInterface* Create();
161};
162#endif
163
164// CompositeMediaEngine constructs a MediaEngine from separate
165// voice and video engine classes.
166template<class VOICE, class VIDEO>
167class CompositeMediaEngine : public MediaEngineInterface {
168 public:
169 CompositeMediaEngine() {}
170 virtual ~CompositeMediaEngine() {}
171 virtual bool Init(talk_base::Thread* worker_thread) {
172 if (!voice_.Init(worker_thread))
173 return false;
174 if (!video_.Init(worker_thread)) {
175 voice_.Terminate();
176 return false;
177 }
178 SignalVideoCaptureStateChange().repeat(video_.SignalCaptureStateChange);
179 return true;
180 }
181 virtual void Terminate() {
182 video_.Terminate();
183 voice_.Terminate();
184 }
185
186 virtual int GetCapabilities() {
187 return (voice_.GetCapabilities() | video_.GetCapabilities());
188 }
189 virtual VoiceMediaChannel *CreateChannel() {
190 return voice_.CreateChannel();
191 }
192 virtual VideoMediaChannel *CreateVideoChannel(VoiceMediaChannel* channel) {
193 return video_.CreateChannel(channel);
194 }
195 virtual SoundclipMedia *CreateSoundclip() {
196 return voice_.CreateSoundclip();
197 }
198
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000199 virtual AudioOptions GetAudioOptions() const {
200 return voice_.GetOptions();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201 }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000202 virtual bool SetAudioOptions(const AudioOptions& options) {
203 return voice_.SetOptions(options);
204 }
205 virtual bool SetVideoOptions(const VideoOptions& options) {
206 return video_.SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207 }
208 virtual bool SetAudioDelayOffset(int offset) {
209 return voice_.SetDelayOffset(offset);
210 }
211 virtual bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config) {
212 return video_.SetDefaultEncoderConfig(config);
213 }
wu@webrtc.org78187522013-10-07 23:32:02 +0000214 virtual VideoEncoderConfig GetDefaultVideoEncoderConfig() const {
215 return video_.GetDefaultEncoderConfig();
216 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217
218 virtual bool SetSoundDevices(const Device* in_device,
219 const Device* out_device) {
220 return voice_.SetDevices(in_device, out_device);
221 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000222
223 virtual bool GetOutputVolume(int* level) {
224 return voice_.GetOutputVolume(level);
225 }
226 virtual bool SetOutputVolume(int level) {
227 return voice_.SetOutputVolume(level);
228 }
229
230 virtual int GetInputLevel() {
231 return voice_.GetInputLevel();
232 }
233 virtual bool SetLocalMonitor(bool enable) {
234 return voice_.SetLocalMonitor(enable);
235 }
236 virtual bool SetLocalRenderer(VideoRenderer* renderer) {
237 return video_.SetLocalRenderer(renderer);
238 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239
240 virtual const std::vector<AudioCodec>& audio_codecs() {
241 return voice_.codecs();
242 }
243 virtual const std::vector<RtpHeaderExtension>& audio_rtp_header_extensions() {
244 return voice_.rtp_header_extensions();
245 }
246 virtual const std::vector<VideoCodec>& video_codecs() {
247 return video_.codecs();
248 }
249 virtual const std::vector<RtpHeaderExtension>& video_rtp_header_extensions() {
250 return video_.rtp_header_extensions();
251 }
252
253 virtual void SetVoiceLogging(int min_sev, const char* filter) {
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000254 voice_.SetLogging(min_sev, filter);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255 }
256 virtual void SetVideoLogging(int min_sev, const char* filter) {
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000257 video_.SetLogging(min_sev, filter);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258 }
259
wu@webrtc.orga8910d22014-01-23 22:12:45 +0000260 virtual bool StartAecDump(talk_base::PlatformFile file) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000261 return voice_.StartAecDump(file);
262 }
263
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264 virtual bool RegisterVoiceProcessor(uint32 ssrc,
265 VoiceProcessor* processor,
266 MediaProcessorDirection direction) {
267 return voice_.RegisterProcessor(ssrc, processor, direction);
268 }
269 virtual bool UnregisterVoiceProcessor(uint32 ssrc,
270 VoiceProcessor* processor,
271 MediaProcessorDirection direction) {
272 return voice_.UnregisterProcessor(ssrc, processor, direction);
273 }
274 virtual VideoFormat GetStartCaptureFormat() const {
275 return video_.GetStartCaptureFormat();
276 }
277 virtual sigslot::repeater2<VideoCapturer*, CaptureState>&
278 SignalVideoCaptureStateChange() {
279 return signal_state_change_;
280 }
281
282 protected:
283 VOICE voice_;
284 VIDEO video_;
285 sigslot::repeater2<VideoCapturer*, CaptureState> signal_state_change_;
286};
287
288// NullVoiceEngine can be used with CompositeMediaEngine in the case where only
289// a video engine is desired.
290class NullVoiceEngine {
291 public:
292 bool Init(talk_base::Thread* worker_thread) { return true; }
293 void Terminate() {}
294 int GetCapabilities() { return 0; }
295 // If you need this to return an actual channel, use FakeMediaEngine instead.
296 VoiceMediaChannel* CreateChannel() {
297 return NULL;
298 }
299 SoundclipMedia* CreateSoundclip() {
300 return NULL;
301 }
302 bool SetDelayOffset(int offset) { return true; }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000303 AudioOptions GetOptions() const { return AudioOptions(); }
304 bool SetOptions(const AudioOptions& options) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305 bool SetDevices(const Device* in_device, const Device* out_device) {
306 return true;
307 }
308 bool GetOutputVolume(int* level) {
309 *level = 0;
310 return true;
311 }
312 bool SetOutputVolume(int level) { return true; }
313 int GetInputLevel() { return 0; }
314 bool SetLocalMonitor(bool enable) { return true; }
315 const std::vector<AudioCodec>& codecs() { return codecs_; }
316 const std::vector<RtpHeaderExtension>& rtp_header_extensions() {
317 return rtp_header_extensions_;
318 }
319 void SetLogging(int min_sev, const char* filter) {}
wu@webrtc.orga8910d22014-01-23 22:12:45 +0000320 bool StartAecDump(talk_base::PlatformFile file) { return false; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321 bool RegisterProcessor(uint32 ssrc,
322 VoiceProcessor* voice_processor,
323 MediaProcessorDirection direction) { return true; }
324 bool UnregisterProcessor(uint32 ssrc,
325 VoiceProcessor* voice_processor,
326 MediaProcessorDirection direction) { return true; }
327
328 private:
329 std::vector<AudioCodec> codecs_;
330 std::vector<RtpHeaderExtension> rtp_header_extensions_;
331};
332
333// NullVideoEngine can be used with CompositeMediaEngine in the case where only
334// a voice engine is desired.
335class NullVideoEngine {
336 public:
337 bool Init(talk_base::Thread* worker_thread) { return true; }
338 void Terminate() {}
339 int GetCapabilities() { return 0; }
340 // If you need this to return an actual channel, use FakeMediaEngine instead.
341 VideoMediaChannel* CreateChannel(
342 VoiceMediaChannel* voice_media_channel) {
343 return NULL;
344 }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000345 bool SetOptions(const VideoOptions& options) { return true; }
wu@webrtc.org78187522013-10-07 23:32:02 +0000346 VideoEncoderConfig GetDefaultEncoderConfig() const {
347 return VideoEncoderConfig();
348 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349 bool SetDefaultEncoderConfig(const VideoEncoderConfig& config) {
350 return true;
351 }
352 bool SetLocalRenderer(VideoRenderer* renderer) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353 const std::vector<VideoCodec>& codecs() { return codecs_; }
354 const std::vector<RtpHeaderExtension>& rtp_header_extensions() {
355 return rtp_header_extensions_;
356 }
357 void SetLogging(int min_sev, const char* filter) {}
358 VideoFormat GetStartCaptureFormat() const { return VideoFormat(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359
360 sigslot::signal2<VideoCapturer*, CaptureState> SignalCaptureStateChange;
361 private:
362 std::vector<VideoCodec> codecs_;
363 std::vector<RtpHeaderExtension> rtp_header_extensions_;
364};
365
366typedef CompositeMediaEngine<NullVoiceEngine, NullVideoEngine> NullMediaEngine;
367
368enum DataChannelType {
369 DCT_NONE = 0,
370 DCT_RTP = 1,
371 DCT_SCTP = 2
372};
373
374class DataEngineInterface {
375 public:
376 virtual ~DataEngineInterface() {}
377 virtual DataMediaChannel* CreateChannel(DataChannelType type) = 0;
378 virtual const std::vector<DataCodec>& data_codecs() = 0;
379};
380
381} // namespace cricket
382
383#endif // TALK_MEDIA_BASE_MEDIAENGINE_H_