blob: d39f2b45e6d0f945ce7509bbf9bfda19ed00ba34 [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
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035#include <string>
36#include <vector>
37
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038#include "talk/media/base/codec.h"
39#include "talk/media/base/mediachannel.h"
40#include "talk/media/base/mediacommon.h"
41#include "talk/media/base/videocapturer.h"
42#include "talk/media/base/videocommon.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043#include "talk/media/devices/devicemanager.h"
solenberg566ef242015-11-06 15:34:49 -080044#include "webrtc/audio_state.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000045#include "webrtc/base/fileutils.h"
46#include "webrtc/base/sigslotrepeater.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047
48#if defined(GOOGLE_CHROME_BUILD) || defined(CHROMIUM_BUILD)
49#define DISABLE_MEDIA_ENGINE_FACTORY
50#endif
51
Fredrik Solenberg709ed672015-09-15 12:26:33 +020052namespace webrtc {
53class Call;
Fredrik Solenberg709ed672015-09-15 12:26:33 +020054}
55
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056namespace cricket {
57
58class VideoCapturer;
59
60// MediaEngineInterface is an abstraction of a media engine which can be
61// subclassed to support different media componentry backends.
62// It supports voice and video operations in the same class to facilitate
63// proper synchronization between both media types.
64class MediaEngineInterface {
65 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066 virtual ~MediaEngineInterface() {}
67
68 // Initialization
69 // Starts the engine.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000070 virtual bool Init(rtc::Thread* worker_thread) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071 // Shuts down the engine.
72 virtual void Terminate() = 0;
Fredrik Solenberg709ed672015-09-15 12:26:33 +020073 // TODO(solenberg): Remove once VoE API refactoring is done.
solenberg566ef242015-11-06 15:34:49 -080074 virtual rtc::scoped_refptr<webrtc::AudioState> GetAudioState() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075
76 // MediaChannel creation
77 // Creates a voice media channel. Returns NULL on failure.
Fredrik Solenberg709ed672015-09-15 12:26:33 +020078 virtual VoiceMediaChannel* CreateChannel(
79 webrtc::Call* call,
80 const AudioOptions& options) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081 // Creates a video media channel, paired with the specified voice channel.
82 // Returns NULL on failure.
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +000083 virtual VideoMediaChannel* CreateVideoChannel(
Fredrik Solenberg709ed672015-09-15 12:26:33 +020084 webrtc::Call* call,
85 const VideoOptions& options) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087 // 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 the default (maximum) codec/resolution and encoder option to capture
93 // and encode video.
94 virtual bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config)
95 = 0;
96
97 // Device selection
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 virtual bool SetSoundDevices(const Device* in_device,
99 const Device* out_device) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100
101 // Device configuration
102 // Gets the current speaker volume, as a value between 0 and 255.
103 virtual bool GetOutputVolume(int* level) = 0;
104 // Sets the current speaker volume, as a value between 0 and 255.
105 virtual bool SetOutputVolume(int level) = 0;
106
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107 // Gets the current microphone level, as a value between 0 and 10.
108 virtual int GetInputLevel() = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109
110 virtual const std::vector<AudioCodec>& audio_codecs() = 0;
111 virtual const std::vector<RtpHeaderExtension>&
112 audio_rtp_header_extensions() = 0;
113 virtual const std::vector<VideoCodec>& video_codecs() = 0;
114 virtual const std::vector<RtpHeaderExtension>&
115 video_rtp_header_extensions() = 0;
116
wu@webrtc.orga9890802013-12-13 00:21:03 +0000117 // Starts AEC dump using existing file.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000118 virtual bool StartAecDump(rtc::PlatformFile file) = 0;
ivoc112a3d82015-10-16 02:22:18 -0700119
ivoc797ef122015-10-22 03:25:41 -0700120 // Stops recording AEC dump.
121 virtual void StopAecDump() = 0;
122
ivoc112a3d82015-10-16 02:22:18 -0700123 // Starts RtcEventLog using existing file.
124 virtual bool StartRtcEventLog(rtc::PlatformFile file) = 0;
125
126 // Stops recording an RtcEventLog.
127 virtual void StopRtcEventLog() = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128};
129
130
131#if !defined(DISABLE_MEDIA_ENGINE_FACTORY)
132class MediaEngineFactory {
133 public:
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000134 typedef cricket::MediaEngineInterface* (*MediaEngineCreateFunction)();
135 // Creates a media engine, using either the compiled system default or the
136 // creation function specified in SetCreateFunction, if specified.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137 static MediaEngineInterface* Create();
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000138 // Sets the function used when calling Create. If unset, the compiled system
139 // default will be used. Returns the old create function, or NULL if one
140 // wasn't set. Likewise, NULL can be used as the |function| parameter to
141 // reset to the default behavior.
142 static MediaEngineCreateFunction SetCreateFunction(
143 MediaEngineCreateFunction function);
144 private:
145 static MediaEngineCreateFunction create_function_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146};
147#endif
148
149// CompositeMediaEngine constructs a MediaEngine from separate
150// voice and video engine classes.
151template<class VOICE, class VIDEO>
152class CompositeMediaEngine : public MediaEngineInterface {
153 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154 virtual ~CompositeMediaEngine() {}
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000155 virtual bool Init(rtc::Thread* worker_thread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156 if (!voice_.Init(worker_thread))
157 return false;
Fredrik Solenberg9a416bd2015-05-22 09:04:09 +0200158 video_.Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159 return true;
160 }
161 virtual void Terminate() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162 voice_.Terminate();
163 }
164
solenberg566ef242015-11-06 15:34:49 -0800165 virtual rtc::scoped_refptr<webrtc::AudioState> GetAudioState() const {
166 return voice_.GetAudioState();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 }
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200168 virtual VoiceMediaChannel* CreateChannel(webrtc::Call* call,
169 const AudioOptions& options) {
170 return voice_.CreateChannel(call, options);
171 }
172 virtual VideoMediaChannel* CreateVideoChannel(webrtc::Call* call,
173 const VideoOptions& options) {
174 return video_.CreateChannel(call, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000177 virtual AudioOptions GetAudioOptions() const {
178 return voice_.GetOptions();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179 }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000180 virtual bool SetAudioOptions(const AudioOptions& options) {
181 return voice_.SetOptions(options);
182 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 virtual bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config) {
184 return video_.SetDefaultEncoderConfig(config);
185 }
186
187 virtual bool SetSoundDevices(const Device* in_device,
188 const Device* out_device) {
189 return voice_.SetDevices(in_device, out_device);
190 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191
192 virtual bool GetOutputVolume(int* level) {
193 return voice_.GetOutputVolume(level);
194 }
195 virtual bool SetOutputVolume(int level) {
196 return voice_.SetOutputVolume(level);
197 }
198
199 virtual int GetInputLevel() {
200 return voice_.GetInputLevel();
201 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202 virtual const std::vector<AudioCodec>& audio_codecs() {
203 return voice_.codecs();
204 }
205 virtual const std::vector<RtpHeaderExtension>& audio_rtp_header_extensions() {
206 return voice_.rtp_header_extensions();
207 }
208 virtual const std::vector<VideoCodec>& video_codecs() {
209 return video_.codecs();
210 }
211 virtual const std::vector<RtpHeaderExtension>& video_rtp_header_extensions() {
212 return video_.rtp_header_extensions();
213 }
214
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000215 virtual bool StartAecDump(rtc::PlatformFile file) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000216 return voice_.StartAecDump(file);
217 }
218
ivoc797ef122015-10-22 03:25:41 -0700219 virtual void StopAecDump() {
220 voice_.StopAecDump();
221 }
222
ivoc112a3d82015-10-16 02:22:18 -0700223 virtual bool StartRtcEventLog(rtc::PlatformFile file) {
224 return voice_.StartRtcEventLog(file);
225 }
226
227 virtual void StopRtcEventLog() { voice_.StopRtcEventLog(); }
228
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229 protected:
230 VOICE voice_;
231 VIDEO video_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232};
233
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234enum DataChannelType {
235 DCT_NONE = 0,
236 DCT_RTP = 1,
237 DCT_SCTP = 2
238};
239
240class DataEngineInterface {
241 public:
242 virtual ~DataEngineInterface() {}
243 virtual DataMediaChannel* CreateChannel(DataChannelType type) = 0;
244 virtual const std::vector<DataCodec>& data_codecs() = 0;
245};
246
247} // namespace cricket
248
249#endif // TALK_MEDIA_BASE_MEDIAENGINE_H_