You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

376 lines
14 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  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. #ifndef TALK_MEDIA_BASE_MEDIAENGINE_H_
  28. #define TALK_MEDIA_BASE_MEDIAENGINE_H_
  29. #ifdef OSX
  30. #include <CoreAudio/CoreAudio.h>
  31. #endif
  32. #include <climits>
  33. #include <string>
  34. #include <vector>
  35. #include "talk/base/sigslotrepeater.h"
  36. #include "talk/media/base/codec.h"
  37. #include "talk/media/base/mediachannel.h"
  38. #include "talk/media/base/mediacommon.h"
  39. #include "talk/media/base/videocapturer.h"
  40. #include "talk/media/base/videocommon.h"
  41. #include "talk/media/base/videoprocessor.h"
  42. #include "talk/media/base/voiceprocessor.h"
  43. #include "talk/media/devices/devicemanager.h"
  44. namespace cricket {
  45. class VideoCapturer;
  46. // MediaEngineInterface is an abstraction of a media engine which can be
  47. // subclassed to support different media componentry backends.
  48. // It supports voice and video operations in the same class to facilitate
  49. // proper synchronization between both media types.
  50. class MediaEngineInterface {
  51. public:
  52. // Bitmask flags for options that may be supported by the media engine
  53. // implementation. This can be converted to and from an
  54. // AudioOptions struct for backwards compatibility with calls that
  55. // use flags until we transition to using structs everywhere.
  56. enum AudioFlags {
  57. // Audio processing that attempts to filter away the output signal from
  58. // later inbound pickup.
  59. ECHO_CANCELLATION = 1 << 0,
  60. // Audio processing to adjust the sensitivity of the local mic dynamically.
  61. AUTO_GAIN_CONTROL = 1 << 1,
  62. // Audio processing to filter out background noise.
  63. NOISE_SUPPRESSION = 1 << 2,
  64. // Audio processing to remove background noise of lower frequencies.
  65. HIGHPASS_FILTER = 1 << 3,
  66. // A switch to swap which captured signal is left and right in stereo mode.
  67. STEREO_FLIPPING = 1 << 4,
  68. // Controls delegation echo cancellation to use the OS' facility.
  69. SYSTEM_AEC_MODE = 1 << 5,
  70. ALL_AUDIO_OPTIONS = (1 << 6) - 1,
  71. DEFAULT_AUDIO_OPTIONS = ECHO_CANCELLATION | AUTO_GAIN_CONTROL |
  72. NOISE_SUPPRESSION | HIGHPASS_FILTER,
  73. };
  74. // Default value to be used for SetAudioDelayOffset().
  75. static const int kDefaultAudioDelayOffset;
  76. virtual ~MediaEngineInterface() {}
  77. // Initialization
  78. // Starts the engine.
  79. virtual bool Init() = 0;
  80. // Shuts down the engine.
  81. virtual void Terminate() = 0;
  82. // Returns what the engine is capable of, as a set of Capabilities, above.
  83. virtual int GetCapabilities() = 0;
  84. // MediaChannel creation
  85. // Creates a voice media channel. Returns NULL on failure.
  86. virtual VoiceMediaChannel *CreateChannel() = 0;
  87. // Creates a video media channel, paired with the specified voice channel.
  88. // Returns NULL on failure.
  89. virtual VideoMediaChannel *CreateVideoChannel(
  90. VoiceMediaChannel* voice_media_channel) = 0;
  91. // Creates a soundclip object for playing sounds on. Returns NULL on failure.
  92. virtual SoundclipMedia *CreateSoundclip() = 0;
  93. // Configuration
  94. // Sets global audio options. "options" are from AudioOptions, above.
  95. virtual bool SetAudioOptions(int options) = 0;
  96. // Sets global video options. "options" are from VideoOptions, above.
  97. virtual bool SetVideoOptions(int options) = 0;
  98. // Sets the value used by the echo canceller to offset delay values obtained
  99. // from the OS.
  100. virtual bool SetAudioDelayOffset(int offset) = 0;
  101. // Sets the default (maximum) codec/resolution and encoder option to capture
  102. // and encode video.
  103. virtual bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config)
  104. = 0;
  105. // Device selection
  106. // TODO(tschmelcher): Add method for selecting the soundclip device.
  107. virtual bool SetSoundDevices(const Device* in_device,
  108. const Device* out_device) = 0;
  109. // Sets the externally provided video capturer. The ssrc is the ssrc of the
  110. // (video) stream for which the video capturer should be set.
  111. virtual bool SetVideoCapturer(VideoCapturer* capturer) = 0;
  112. virtual VideoCapturer* GetVideoCapturer() const = 0;
  113. // Device configuration
  114. // Gets the current speaker volume, as a value between 0 and 255.
  115. virtual bool GetOutputVolume(int* level) = 0;
  116. // Sets the current speaker volume, as a value between 0 and 255.
  117. virtual bool SetOutputVolume(int level) = 0;
  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;
  127. // Starts/stops local camera.
  128. virtual bool SetVideoCapture(bool capture) = 0;
  129. virtual const std::vector<AudioCodec>& audio_codecs() = 0;
  130. virtual const std::vector<VideoCodec>& video_codecs() = 0;
  131. // Logging control
  132. virtual void SetVoiceLogging(int min_sev, const char* filter) = 0;
  133. virtual void SetVideoLogging(int min_sev, const char* filter) = 0;
  134. // media processors for effects
  135. virtual bool RegisterVideoProcessor(VideoProcessor* video_processor) = 0;
  136. virtual bool UnregisterVideoProcessor(VideoProcessor* video_processor) = 0;
  137. virtual bool RegisterVoiceProcessor(uint32 ssrc,
  138. VoiceProcessor* video_processor,
  139. MediaProcessorDirection direction) = 0;
  140. virtual bool UnregisterVoiceProcessor(uint32 ssrc,
  141. VoiceProcessor* video_processor,
  142. MediaProcessorDirection direction) = 0;
  143. virtual VideoFormat GetStartCaptureFormat() const = 0;
  144. sigslot::repeater2<VideoCapturer*, CaptureState>
  145. SignalVideoCaptureStateChange;
  146. };
  147. class MediaEngineFactory {
  148. public:
  149. static MediaEngineInterface* Create();
  150. };
  151. // CompositeMediaEngine constructs a MediaEngine from separate
  152. // voice and video engine classes.
  153. template<class VOICE, class VIDEO>
  154. class CompositeMediaEngine : public MediaEngineInterface {
  155. public:
  156. CompositeMediaEngine() {}
  157. virtual ~CompositeMediaEngine() {}
  158. virtual bool Init() {
  159. if (!voice_.Init())
  160. return false;
  161. if (!video_.Init()) {
  162. voice_.Terminate();
  163. return false;
  164. }
  165. SignalVideoCaptureStateChange.repeat(video_.SignalCaptureStateChange);
  166. return true;
  167. }
  168. virtual void Terminate() {
  169. video_.Terminate();
  170. voice_.Terminate();
  171. }
  172. virtual int GetCapabilities() {
  173. return (voice_.GetCapabilities() | video_.GetCapabilities());
  174. }
  175. virtual VoiceMediaChannel *CreateChannel() {
  176. return voice_.CreateChannel();
  177. }
  178. virtual VideoMediaChannel *CreateVideoChannel(VoiceMediaChannel* channel) {
  179. return video_.CreateChannel(channel);
  180. }
  181. virtual SoundclipMedia *CreateSoundclip() {
  182. return voice_.CreateSoundclip();
  183. }
  184. virtual bool SetAudioOptions(int o) {
  185. return voice_.SetOptions(o);
  186. }
  187. virtual bool SetVideoOptions(int o) {
  188. return video_.SetOptions(o);
  189. }
  190. virtual bool SetAudioDelayOffset(int offset) {
  191. return voice_.SetDelayOffset(offset);
  192. }
  193. virtual bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config) {
  194. return video_.SetDefaultEncoderConfig(config);
  195. }
  196. virtual bool SetSoundDevices(const Device* in_device,
  197. const Device* out_device) {
  198. return voice_.SetDevices(in_device, out_device);
  199. }
  200. virtual bool SetVideoCapturer(VideoCapturer* capturer) {
  201. return video_.SetVideoCapturer(capturer);
  202. }
  203. virtual VideoCapturer* GetVideoCapturer() const {
  204. return video_.GetVideoCapturer();
  205. }
  206. virtual bool GetOutputVolume(int* level) {
  207. return voice_.GetOutputVolume(level);
  208. }
  209. virtual bool SetOutputVolume(int level) {
  210. return voice_.SetOutputVolume(level);
  211. }
  212. virtual int GetInputLevel() {
  213. return voice_.GetInputLevel();
  214. }
  215. virtual bool SetLocalMonitor(bool enable) {
  216. return voice_.SetLocalMonitor(enable);
  217. }
  218. virtual bool SetLocalRenderer(VideoRenderer* renderer) {
  219. return video_.SetLocalRenderer(renderer);
  220. }
  221. virtual bool SetVideoCapture(bool capture) {
  222. return video_.SetCapture(capture);
  223. }
  224. virtual const std::vector<AudioCodec>& audio_codecs() {
  225. return voice_.codecs();
  226. }
  227. virtual const std::vector<VideoCodec>& video_codecs() {
  228. return video_.codecs();
  229. }
  230. virtual void SetVoiceLogging(int min_sev, const char* filter) {
  231. return voice_.SetLogging(min_sev, filter);
  232. }
  233. virtual void SetVideoLogging(int min_sev, const char* filter) {
  234. return video_.SetLogging(min_sev, filter);
  235. }
  236. virtual bool RegisterVideoProcessor(VideoProcessor* processor) {
  237. return video_.RegisterProcessor(processor);
  238. }
  239. virtual bool UnregisterVideoProcessor(VideoProcessor* processor) {
  240. return video_.UnregisterProcessor(processor);
  241. }
  242. virtual bool RegisterVoiceProcessor(uint32 ssrc,
  243. VoiceProcessor* processor,
  244. MediaProcessorDirection direction) {
  245. return voice_.RegisterProcessor(ssrc, processor, direction);
  246. }
  247. virtual bool UnregisterVoiceProcessor(uint32 ssrc,
  248. VoiceProcessor* processor,
  249. MediaProcessorDirection direction) {
  250. return voice_.UnregisterProcessor(ssrc, processor, direction);
  251. }
  252. virtual VideoFormat GetStartCaptureFormat() const {
  253. return video_.GetStartCaptureFormat();
  254. }
  255. protected:
  256. VOICE voice_;
  257. VIDEO video_;
  258. };
  259. // NullVoiceEngine can be used with CompositeMediaEngine in the case where only
  260. // a video engine is desired.
  261. class NullVoiceEngine {
  262. public:
  263. bool Init() { return true; }
  264. void Terminate() {}
  265. int GetCapabilities() { return 0; }
  266. // If you need this to return an actual channel, use FakeMediaEngine instead.
  267. VoiceMediaChannel* CreateChannel() {
  268. return NULL;
  269. }
  270. SoundclipMedia* CreateSoundclip() {
  271. return NULL;
  272. }
  273. bool SetDelayOffset(int offset) { return true; }
  274. bool SetOptions(int opts) { return true; }
  275. bool SetDevices(const Device* in_device, const Device* out_device) {
  276. return true;
  277. }
  278. bool GetOutputVolume(int* level) {
  279. *level = 0;
  280. return true;
  281. }
  282. bool SetOutputVolume(int level) { return true; }
  283. int GetInputLevel() { return 0; }
  284. bool SetLocalMonitor(bool enable) { return true; }
  285. const std::vector<AudioCodec>& codecs() { return codecs_; }
  286. void SetLogging(int min_sev, const char* filter) {}
  287. bool RegisterProcessor(uint32 ssrc,
  288. VoiceProcessor* voice_processor,
  289. MediaProcessorDirection direction) { return true; }
  290. bool UnregisterProcessor(uint32 ssrc,
  291. VoiceProcessor* voice_processor,
  292. MediaProcessorDirection direction) { return true; }
  293. private:
  294. std::vector<AudioCodec> codecs_;
  295. };
  296. // NullVideoEngine can be used with CompositeMediaEngine in the case where only
  297. // a voice engine is desired.
  298. class NullVideoEngine {
  299. public:
  300. bool Init() { return true; }
  301. void Terminate() {}
  302. int GetCapabilities() { return 0; }
  303. // If you need this to return an actual channel, use FakeMediaEngine instead.
  304. VideoMediaChannel* CreateChannel(
  305. VoiceMediaChannel* voice_media_channel) {
  306. return NULL;
  307. }
  308. bool SetOptions(int opts) { return true; }
  309. bool SetDefaultEncoderConfig(const VideoEncoderConfig& config) {
  310. return true;
  311. }
  312. bool SetLocalRenderer(VideoRenderer* renderer) { return true; }
  313. bool SetCapture(bool capture) { return true; }
  314. const std::vector<VideoCodec>& codecs() { return codecs_; }
  315. void SetLogging(int min_sev, const char* filter) {}
  316. bool RegisterProcessor(VideoProcessor* video_processor) { return true; }
  317. bool UnregisterProcessor(VideoProcessor* video_processor) { return true; }
  318. VideoFormat GetStartCaptureFormat() const { return VideoFormat(); }
  319. bool SetVideoCapturer(VideoCapturer* capturer) { return true; }
  320. VideoCapturer* GetVideoCapturer() const { return NULL; }
  321. sigslot::signal2<VideoCapturer*, CaptureState> SignalCaptureStateChange;
  322. private:
  323. std::vector<VideoCodec> codecs_;
  324. };
  325. typedef CompositeMediaEngine<NullVoiceEngine, NullVideoEngine> NullMediaEngine;
  326. class DataEngineInterface {
  327. public:
  328. virtual ~DataEngineInterface() {}
  329. virtual DataMediaChannel* CreateChannel() = 0;
  330. virtual const std::vector<DataCodec>& data_codecs() = 0;
  331. };
  332. } // namespace cricket
  333. #endif // TALK_MEDIA_BASE_MEDIAENGINE_H_