Home | History | Annotate | Download | only in base
      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 
     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 
     53 namespace cricket {
     54 
     55 class 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.
     61 class MediaEngineInterface {
     62  public:
     63   // 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
     88   // Gets global audio options.
     89   virtual AudioOptions GetAudioOptions() const = 0;
     90   // Sets global audio options. "options" are from AudioOptions, above.
     91   virtual bool SetAudioOptions(const AudioOptions& options) = 0;
     92   // Sets global video options. "options" are from VideoOptions, above.
     93   virtual bool SetVideoOptions(const VideoOptions& options) = 0;
     94   // 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;
    101   // 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;
    105 
    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;
    110 
    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;
    126 
    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)
    154 class MediaEngineFactory {
    155  public:
    156   static MediaEngineInterface* Create();
    157 };
    158 #endif
    159 
    160 // CompositeMediaEngine constructs a MediaEngine from separate
    161 // voice and video engine classes.
    162 template<class VOICE, class VIDEO>
    163 class 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 
    195   virtual AudioOptions GetAudioOptions() const {
    196     return voice_.GetOptions();
    197   }
    198   virtual bool SetAudioOptions(const AudioOptions& options) {
    199     return voice_.SetOptions(options);
    200   }
    201   virtual bool SetVideoOptions(const VideoOptions& options) {
    202     return video_.SetOptions(options);
    203   }
    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   }
    210   virtual VideoEncoderConfig GetDefaultVideoEncoderConfig() const {
    211     return video_.GetDefaultEncoderConfig();
    212   }
    213 
    214   virtual bool SetSoundDevices(const Device* in_device,
    215                                const Device* out_device) {
    216     return voice_.SetDevices(in_device, out_device);
    217   }
    218 
    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   }
    235 
    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) {
    250     voice_.SetLogging(min_sev, filter);
    251   }
    252   virtual void SetVideoLogging(int min_sev, const char* filter) {
    253     video_.SetLogging(min_sev, filter);
    254   }
    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.
    282 class 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; }
    295   AudioOptions GetOptions() const { return AudioOptions(); }
    296   bool SetOptions(const AudioOptions& options) { return true; }
    297   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.
    326 class 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   }
    336   bool SetOptions(const VideoOptions& options) { return true; }
    337   VideoEncoderConfig GetDefaultEncoderConfig() const {
    338     return VideoEncoderConfig();
    339   }
    340   bool SetDefaultEncoderConfig(const VideoEncoderConfig& config) {
    341     return true;
    342   }
    343   bool SetLocalRenderer(VideoRenderer* renderer) { return true; }
    344   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(); }
    350 
    351   sigslot::signal2<VideoCapturer*, CaptureState> SignalCaptureStateChange;
    352  private:
    353   std::vector<VideoCodec> codecs_;
    354   std::vector<RtpHeaderExtension> rtp_header_extensions_;
    355 };
    356 
    357 typedef CompositeMediaEngine<NullVoiceEngine, NullVideoEngine> NullMediaEngine;
    358 
    359 enum DataChannelType {
    360   DCT_NONE = 0,
    361   DCT_RTP = 1,
    362   DCT_SCTP = 2
    363 };
    364 
    365 class 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_
    375