blob: f9165728ddebaf96d93ae7a633572cc8e224bff4 [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
39#include "talk/base/sigslotrepeater.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
49#if defined(GOOGLE_CHROME_BUILD) || defined(CHROMIUM_BUILD)
50#define DISABLE_MEDIA_ENGINE_FACTORY
51#endif
52
53namespace cricket {
54
55class VideoCapturer;
56
57// MediaEngineInterface is an abstraction of a media engine which can be
58// subclassed to support different media componentry backends.
59// It supports voice and video operations in the same class to facilitate
60// proper synchronization between both media types.
61class MediaEngineInterface {
62 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063 // Default value to be used for SetAudioDelayOffset().
64 static const int kDefaultAudioDelayOffset;
65
66 virtual ~MediaEngineInterface() {}
67
68 // Initialization
69 // Starts the engine.
70 virtual bool Init(talk_base::Thread* worker_thread) = 0;
71 // Shuts down the engine.
72 virtual void Terminate() = 0;
73 // Returns what the engine is capable of, as a set of Capabilities, above.
74 virtual int GetCapabilities() = 0;
75
76 // MediaChannel creation
77 // Creates a voice media channel. Returns NULL on failure.
78 virtual VoiceMediaChannel *CreateChannel() = 0;
79 // Creates a video media channel, paired with the specified voice channel.
80 // Returns NULL on failure.
81 virtual VideoMediaChannel *CreateVideoChannel(
82 VoiceMediaChannel* voice_media_channel) = 0;
83
84 // Creates a soundclip object for playing sounds on. Returns NULL on failure.
85 virtual SoundclipMedia *CreateSoundclip() = 0;
86
87 // Configuration
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000088 // Gets global audio options.
89 virtual AudioOptions GetAudioOptions() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 // Sets global audio options. "options" are from AudioOptions, above.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000091 virtual bool SetAudioOptions(const AudioOptions& options) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 // Sets global video options. "options" are from VideoOptions, above.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000093 virtual bool SetVideoOptions(const VideoOptions& options) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 // Sets the value used by the echo canceller to offset delay values obtained
95 // from the OS.
96 virtual bool SetAudioDelayOffset(int offset) = 0;
97 // Sets the default (maximum) codec/resolution and encoder option to capture
98 // and encode video.
99 virtual bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config)
100 = 0;
wu@webrtc.org78187522013-10-07 23:32:02 +0000101 // Gets the default (maximum) codec/resolution and encoder option used to
102 // capture and encode video, as set by SetDefaultVideoEncoderConfig or the
103 // default from the video engine if not previously set.
104 virtual VideoEncoderConfig GetDefaultVideoEncoderConfig() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105
106 // Device selection
107 // TODO(tschmelcher): Add method for selecting the soundclip device.
108 virtual bool SetSoundDevices(const Device* in_device,
109 const Device* out_device) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110
111 // Device configuration
112 // Gets the current speaker volume, as a value between 0 and 255.
113 virtual bool GetOutputVolume(int* level) = 0;
114 // Sets the current speaker volume, as a value between 0 and 255.
115 virtual bool SetOutputVolume(int level) = 0;
116
117 // Local monitoring
118 // Gets the current microphone level, as a value between 0 and 10.
119 virtual int GetInputLevel() = 0;
120 // Starts or stops the local microphone. Useful if local mic info is needed
121 // prior to a call being connected; the mic will be started automatically
122 // when a VoiceMediaChannel starts sending.
123 virtual bool SetLocalMonitor(bool enable) = 0;
124 // Installs a callback for raw frames from the local camera.
125 virtual bool SetLocalRenderer(VideoRenderer* renderer) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126
127 virtual const std::vector<AudioCodec>& audio_codecs() = 0;
128 virtual const std::vector<RtpHeaderExtension>&
129 audio_rtp_header_extensions() = 0;
130 virtual const std::vector<VideoCodec>& video_codecs() = 0;
131 virtual const std::vector<RtpHeaderExtension>&
132 video_rtp_header_extensions() = 0;
133
134 // Logging control
135 virtual void SetVoiceLogging(int min_sev, const char* filter) = 0;
136 virtual void SetVideoLogging(int min_sev, const char* filter) = 0;
137
138 // Voice processors for effects.
139 virtual bool RegisterVoiceProcessor(uint32 ssrc,
140 VoiceProcessor* video_processor,
141 MediaProcessorDirection direction) = 0;
142 virtual bool UnregisterVoiceProcessor(uint32 ssrc,
143 VoiceProcessor* video_processor,
144 MediaProcessorDirection direction) = 0;
145
146 virtual VideoFormat GetStartCaptureFormat() const = 0;
147
148 virtual sigslot::repeater2<VideoCapturer*, CaptureState>&
149 SignalVideoCaptureStateChange() = 0;
150};
151
152
153#if !defined(DISABLE_MEDIA_ENGINE_FACTORY)
154class MediaEngineFactory {
155 public:
156 static MediaEngineInterface* Create();
157};
158#endif
159
160// CompositeMediaEngine constructs a MediaEngine from separate
161// voice and video engine classes.
162template<class VOICE, class VIDEO>
163class CompositeMediaEngine : public MediaEngineInterface {
164 public:
165 CompositeMediaEngine() {}
166 virtual ~CompositeMediaEngine() {}
167 virtual bool Init(talk_base::Thread* worker_thread) {
168 if (!voice_.Init(worker_thread))
169 return false;
170 if (!video_.Init(worker_thread)) {
171 voice_.Terminate();
172 return false;
173 }
174 SignalVideoCaptureStateChange().repeat(video_.SignalCaptureStateChange);
175 return true;
176 }
177 virtual void Terminate() {
178 video_.Terminate();
179 voice_.Terminate();
180 }
181
182 virtual int GetCapabilities() {
183 return (voice_.GetCapabilities() | video_.GetCapabilities());
184 }
185 virtual VoiceMediaChannel *CreateChannel() {
186 return voice_.CreateChannel();
187 }
188 virtual VideoMediaChannel *CreateVideoChannel(VoiceMediaChannel* channel) {
189 return video_.CreateChannel(channel);
190 }
191 virtual SoundclipMedia *CreateSoundclip() {
192 return voice_.CreateSoundclip();
193 }
194
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000195 virtual AudioOptions GetAudioOptions() const {
196 return voice_.GetOptions();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197 }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000198 virtual bool SetAudioOptions(const AudioOptions& options) {
199 return voice_.SetOptions(options);
200 }
201 virtual bool SetVideoOptions(const VideoOptions& options) {
202 return video_.SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203 }
204 virtual bool SetAudioDelayOffset(int offset) {
205 return voice_.SetDelayOffset(offset);
206 }
207 virtual bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config) {
208 return video_.SetDefaultEncoderConfig(config);
209 }
wu@webrtc.org78187522013-10-07 23:32:02 +0000210 virtual VideoEncoderConfig GetDefaultVideoEncoderConfig() const {
211 return video_.GetDefaultEncoderConfig();
212 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213
214 virtual bool SetSoundDevices(const Device* in_device,
215 const Device* out_device) {
216 return voice_.SetDevices(in_device, out_device);
217 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218
219 virtual bool GetOutputVolume(int* level) {
220 return voice_.GetOutputVolume(level);
221 }
222 virtual bool SetOutputVolume(int level) {
223 return voice_.SetOutputVolume(level);
224 }
225
226 virtual int GetInputLevel() {
227 return voice_.GetInputLevel();
228 }
229 virtual bool SetLocalMonitor(bool enable) {
230 return voice_.SetLocalMonitor(enable);
231 }
232 virtual bool SetLocalRenderer(VideoRenderer* renderer) {
233 return video_.SetLocalRenderer(renderer);
234 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000235
236 virtual const std::vector<AudioCodec>& audio_codecs() {
237 return voice_.codecs();
238 }
239 virtual const std::vector<RtpHeaderExtension>& audio_rtp_header_extensions() {
240 return voice_.rtp_header_extensions();
241 }
242 virtual const std::vector<VideoCodec>& video_codecs() {
243 return video_.codecs();
244 }
245 virtual const std::vector<RtpHeaderExtension>& video_rtp_header_extensions() {
246 return video_.rtp_header_extensions();
247 }
248
249 virtual void SetVoiceLogging(int min_sev, const char* filter) {
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000250 voice_.SetLogging(min_sev, filter);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251 }
252 virtual void SetVideoLogging(int min_sev, const char* filter) {
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000253 video_.SetLogging(min_sev, filter);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254 }
255
256 virtual bool RegisterVoiceProcessor(uint32 ssrc,
257 VoiceProcessor* processor,
258 MediaProcessorDirection direction) {
259 return voice_.RegisterProcessor(ssrc, processor, direction);
260 }
261 virtual bool UnregisterVoiceProcessor(uint32 ssrc,
262 VoiceProcessor* processor,
263 MediaProcessorDirection direction) {
264 return voice_.UnregisterProcessor(ssrc, processor, direction);
265 }
266 virtual VideoFormat GetStartCaptureFormat() const {
267 return video_.GetStartCaptureFormat();
268 }
269 virtual sigslot::repeater2<VideoCapturer*, CaptureState>&
270 SignalVideoCaptureStateChange() {
271 return signal_state_change_;
272 }
273
274 protected:
275 VOICE voice_;
276 VIDEO video_;
277 sigslot::repeater2<VideoCapturer*, CaptureState> signal_state_change_;
278};
279
280// NullVoiceEngine can be used with CompositeMediaEngine in the case where only
281// a video engine is desired.
282class NullVoiceEngine {
283 public:
284 bool Init(talk_base::Thread* worker_thread) { return true; }
285 void Terminate() {}
286 int GetCapabilities() { return 0; }
287 // If you need this to return an actual channel, use FakeMediaEngine instead.
288 VoiceMediaChannel* CreateChannel() {
289 return NULL;
290 }
291 SoundclipMedia* CreateSoundclip() {
292 return NULL;
293 }
294 bool SetDelayOffset(int offset) { return true; }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000295 AudioOptions GetOptions() const { return AudioOptions(); }
296 bool SetOptions(const AudioOptions& options) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297 bool SetDevices(const Device* in_device, const Device* out_device) {
298 return true;
299 }
300 bool GetOutputVolume(int* level) {
301 *level = 0;
302 return true;
303 }
304 bool SetOutputVolume(int level) { return true; }
305 int GetInputLevel() { return 0; }
306 bool SetLocalMonitor(bool enable) { return true; }
307 const std::vector<AudioCodec>& codecs() { return codecs_; }
308 const std::vector<RtpHeaderExtension>& rtp_header_extensions() {
309 return rtp_header_extensions_;
310 }
311 void SetLogging(int min_sev, const char* filter) {}
312 bool RegisterProcessor(uint32 ssrc,
313 VoiceProcessor* voice_processor,
314 MediaProcessorDirection direction) { return true; }
315 bool UnregisterProcessor(uint32 ssrc,
316 VoiceProcessor* voice_processor,
317 MediaProcessorDirection direction) { return true; }
318
319 private:
320 std::vector<AudioCodec> codecs_;
321 std::vector<RtpHeaderExtension> rtp_header_extensions_;
322};
323
324// NullVideoEngine can be used with CompositeMediaEngine in the case where only
325// a voice engine is desired.
326class NullVideoEngine {
327 public:
328 bool Init(talk_base::Thread* worker_thread) { return true; }
329 void Terminate() {}
330 int GetCapabilities() { return 0; }
331 // If you need this to return an actual channel, use FakeMediaEngine instead.
332 VideoMediaChannel* CreateChannel(
333 VoiceMediaChannel* voice_media_channel) {
334 return NULL;
335 }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000336 bool SetOptions(const VideoOptions& options) { return true; }
wu@webrtc.org78187522013-10-07 23:32:02 +0000337 VideoEncoderConfig GetDefaultEncoderConfig() const {
338 return VideoEncoderConfig();
339 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340 bool SetDefaultEncoderConfig(const VideoEncoderConfig& config) {
341 return true;
342 }
343 bool SetLocalRenderer(VideoRenderer* renderer) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344 const std::vector<VideoCodec>& codecs() { return codecs_; }
345 const std::vector<RtpHeaderExtension>& rtp_header_extensions() {
346 return rtp_header_extensions_;
347 }
348 void SetLogging(int min_sev, const char* filter) {}
349 VideoFormat GetStartCaptureFormat() const { return VideoFormat(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000350
351 sigslot::signal2<VideoCapturer*, CaptureState> SignalCaptureStateChange;
352 private:
353 std::vector<VideoCodec> codecs_;
354 std::vector<RtpHeaderExtension> rtp_header_extensions_;
355};
356
357typedef CompositeMediaEngine<NullVoiceEngine, NullVideoEngine> NullMediaEngine;
358
359enum DataChannelType {
360 DCT_NONE = 0,
361 DCT_RTP = 1,
362 DCT_SCTP = 2
363};
364
365class DataEngineInterface {
366 public:
367 virtual ~DataEngineInterface() {}
368 virtual DataMediaChannel* CreateChannel(DataChannelType type) = 0;
369 virtual const std::vector<DataCodec>& data_codecs() = 0;
370};
371
372} // namespace cricket
373
374#endif // TALK_MEDIA_BASE_MEDIAENGINE_H_