Home | History | Annotate | Download | only in base
      1 // libjingle
      2 // Copyright 2004 Google Inc.
      3 //
      4 // Redistribution and use in source and binary forms, with or without
      5 // modification, are permitted provided that the following conditions are met:
      6 //
      7 //  1. Redistributions of source code must retain the above copyright notice,
      8 //     this list of conditions and the following disclaimer.
      9 //  2. Redistributions in binary form must reproduce the above copyright notice,
     10 //     this list of conditions and the following disclaimer in the documentation
     11 //     and/or other materials provided with the distribution.
     12 //  3. The name of the author may not be used to endorse or promote products
     13 //     derived from this software without specific prior written permission.
     14 //
     15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
     18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25 
     26 #ifndef TALK_MEDIA_BASE_FILEMEDIAENGINE_H_
     27 #define TALK_MEDIA_BASE_FILEMEDIAENGINE_H_
     28 
     29 #include <string>
     30 #include <vector>
     31 
     32 #include "talk/base/scoped_ptr.h"
     33 #include "talk/base/stream.h"
     34 #include "talk/media/base/codec.h"
     35 #include "talk/media/base/mediachannel.h"
     36 #include "talk/media/base/mediaengine.h"
     37 
     38 namespace talk_base {
     39 class StreamInterface;
     40 }
     41 
     42 namespace cricket {
     43 
     44 // A media engine contains a capturer, an encoder, and a sender in the sender
     45 // side and a receiver, a decoder, and a renderer in the receiver side.
     46 // FileMediaEngine simulates the capturer and the encoder via an input RTP dump
     47 // stream and simulates the decoder and the renderer via an output RTP dump
     48 // stream. Depending on the parameters of the constructor, FileMediaEngine can
     49 // act as file voice engine, file video engine, or both. Currently, we use
     50 // only the RTP dump packets. TODO(whyuan): Enable RTCP packets.
     51 class FileMediaEngine : public MediaEngineInterface {
     52  public:
     53   FileMediaEngine() {}
     54   virtual ~FileMediaEngine() {}
     55 
     56   // Set the file name of the input or output RTP dump for voice or video.
     57   // Should be called before the channel is created.
     58   void set_voice_input_filename(const std::string& filename) {
     59     voice_input_filename_ = filename;
     60   }
     61   void set_voice_output_filename(const std::string& filename) {
     62     voice_output_filename_ = filename;
     63   }
     64   void set_video_input_filename(const std::string& filename) {
     65     video_input_filename_ = filename;
     66   }
     67   void set_video_output_filename(const std::string& filename) {
     68     video_output_filename_ = filename;
     69   }
     70 
     71   // Should be called before codecs() and video_codecs() are called. We need to
     72   // set the voice and video codecs; otherwise, Jingle initiation will fail.
     73   void set_voice_codecs(const std::vector<AudioCodec>& codecs) {
     74     voice_codecs_ = codecs;
     75   }
     76   void set_video_codecs(const std::vector<VideoCodec>& codecs) {
     77     video_codecs_ = codecs;
     78   }
     79 
     80   // Implement pure virtual methods of MediaEngine.
     81   virtual bool Init(talk_base::Thread* worker_thread) {
     82     return true;
     83   }
     84   virtual void Terminate() {}
     85   virtual int GetCapabilities();
     86   virtual VoiceMediaChannel* CreateChannel();
     87   virtual VideoMediaChannel* CreateVideoChannel(VoiceMediaChannel* voice_ch);
     88   virtual SoundclipMedia* CreateSoundclip() { return NULL; }
     89   virtual bool SetAudioOptions(int options) { return true; }
     90   virtual bool SetVideoOptions(int options) { return true; }
     91   virtual bool SetAudioDelayOffset(int offset) { return true; }
     92   virtual bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config) {
     93     return true;
     94   }
     95   virtual bool SetSoundDevices(const Device* in_dev, const Device* out_dev) {
     96     return true;
     97   }
     98   virtual bool SetVideoCaptureDevice(const Device* cam_device) { return true; }
     99   virtual bool SetVideoCapturer(VideoCapturer* /*capturer*/) {
    100     return true;
    101   }
    102   virtual VideoCapturer* GetVideoCapturer() const {
    103     return NULL;
    104   }
    105   virtual bool GetOutputVolume(int* level) {
    106     *level = 0;
    107     return true;
    108   }
    109   virtual bool SetOutputVolume(int level) { return true; }
    110   virtual int GetInputLevel() { return 0; }
    111   virtual bool SetLocalMonitor(bool enable) { return true; }
    112   virtual bool SetLocalRenderer(VideoRenderer* renderer) { return true; }
    113   // TODO(whyuan): control channel send?
    114   virtual bool SetVideoCapture(bool capture) { return true; }
    115   virtual const std::vector<AudioCodec>& audio_codecs() {
    116     return voice_codecs_;
    117   }
    118   virtual const std::vector<VideoCodec>& video_codecs() {
    119     return video_codecs_;
    120   }
    121   virtual const std::vector<RtpHeaderExtension>& audio_rtp_header_extensions() {
    122     return audio_rtp_header_extensions_;
    123   }
    124   virtual const std::vector<RtpHeaderExtension>& video_rtp_header_extensions() {
    125     return video_rtp_header_extensions_;
    126   }
    127 
    128   virtual bool FindAudioCodec(const AudioCodec& codec) { return true; }
    129   virtual bool FindVideoCodec(const VideoCodec& codec) { return true; }
    130   virtual void SetVoiceLogging(int min_sev, const char* filter) {}
    131   virtual void SetVideoLogging(int min_sev, const char* filter) {}
    132 
    133   virtual bool RegisterVideoProcessor(VideoProcessor* processor) {
    134     return true;
    135   }
    136   virtual bool UnregisterVideoProcessor(VideoProcessor* processor) {
    137     return true;
    138   }
    139   virtual bool RegisterVoiceProcessor(uint32 ssrc,
    140                                       VoiceProcessor* processor,
    141                                       MediaProcessorDirection direction) {
    142     return true;
    143   }
    144   virtual bool UnregisterVoiceProcessor(uint32 ssrc,
    145                                         VoiceProcessor* processor,
    146                                         MediaProcessorDirection direction) {
    147     return true;
    148   }
    149   VideoFormat GetStartCaptureFormat() const {
    150     return VideoFormat();
    151   }
    152 
    153   virtual sigslot::repeater2<VideoCapturer*, CaptureState>&
    154       SignalVideoCaptureStateChange() {
    155     return signal_state_change_;
    156   }
    157 
    158  private:
    159   std::string voice_input_filename_;
    160   std::string voice_output_filename_;
    161   std::string video_input_filename_;
    162   std::string video_output_filename_;
    163   std::vector<AudioCodec> voice_codecs_;
    164   std::vector<VideoCodec> video_codecs_;
    165   std::vector<RtpHeaderExtension> audio_rtp_header_extensions_;
    166   std::vector<RtpHeaderExtension> video_rtp_header_extensions_;
    167   sigslot::repeater2<VideoCapturer*, CaptureState>
    168      signal_state_change_;
    169 
    170   DISALLOW_COPY_AND_ASSIGN(FileMediaEngine);
    171 };
    172 
    173 class RtpSenderReceiver;  // Forward declaration. Defined in the .cc file.
    174 
    175 class FileVoiceChannel : public VoiceMediaChannel {
    176  public:
    177   FileVoiceChannel(talk_base::StreamInterface* input_file_stream,
    178       talk_base::StreamInterface* output_file_stream);
    179   virtual ~FileVoiceChannel();
    180 
    181   // Implement pure virtual methods of VoiceMediaChannel.
    182   virtual bool SetRecvCodecs(const std::vector<AudioCodec>& codecs) {
    183     return true;
    184   }
    185   virtual bool SetSendCodecs(const std::vector<AudioCodec>& codecs);
    186   virtual bool SetRecvRtpHeaderExtensions(
    187       const std::vector<RtpHeaderExtension>& extensions) {
    188     return true;
    189   }
    190   virtual bool SetSendRtpHeaderExtensions(
    191       const std::vector<RtpHeaderExtension>& extensions) {
    192     return true;
    193   }
    194   virtual bool SetPlayout(bool playout) { return true; }
    195   virtual bool SetSend(SendFlags flag);
    196   virtual bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) {
    197     return false;
    198   }
    199   virtual bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) {
    200     return false;
    201   }
    202   virtual bool GetActiveStreams(AudioInfo::StreamList* actives) { return true; }
    203   virtual int GetOutputLevel() { return 0; }
    204   virtual int GetTimeSinceLastTyping() { return -1; }
    205   virtual void SetTypingDetectionParameters(int time_window,
    206     int cost_per_typing, int reporting_threshold, int penalty_decay,
    207     int type_event_delay) {}
    208 
    209   virtual bool SetOutputScaling(uint32 ssrc, double left, double right) {
    210     return false;
    211   }
    212   virtual bool GetOutputScaling(uint32 ssrc, double* left, double* right) {
    213     return false;
    214   }
    215   virtual bool SetRingbackTone(const char* buf, int len) { return true; }
    216   virtual bool PlayRingbackTone(uint32 ssrc, bool play, bool loop) {
    217     return true;
    218   }
    219   virtual bool InsertDtmf(uint32 ssrc, int event, int duration, int flags) {
    220     return false;
    221   }
    222   virtual bool GetStats(VoiceMediaInfo* info) { return true; }
    223 
    224   // Implement pure virtual methods of MediaChannel.
    225   virtual void OnPacketReceived(talk_base::Buffer* packet);
    226   virtual void OnRtcpReceived(talk_base::Buffer* packet) {}
    227   virtual void OnReadyToSend(bool ready) {}
    228   virtual bool AddSendStream(const StreamParams& sp);
    229   virtual bool RemoveSendStream(uint32 ssrc);
    230   virtual bool AddRecvStream(const StreamParams& sp) { return true; }
    231   virtual bool RemoveRecvStream(uint32 ssrc) { return true; }
    232   virtual bool MuteStream(uint32 ssrc, bool on) { return false; }
    233   virtual bool SetSendBandwidth(bool autobw, int bps) { return true; }
    234   virtual bool SetOptions(const AudioOptions& options) {
    235     options_ = options;
    236     return true;
    237   }
    238   virtual bool GetOptions(AudioOptions* options) const {
    239     *options = options_;
    240     return true;
    241   }
    242 
    243  private:
    244   uint32 send_ssrc_;
    245   talk_base::scoped_ptr<RtpSenderReceiver> rtp_sender_receiver_;
    246   AudioOptions options_;
    247 
    248   DISALLOW_COPY_AND_ASSIGN(FileVoiceChannel);
    249 };
    250 
    251 class FileVideoChannel : public VideoMediaChannel {
    252  public:
    253   FileVideoChannel(talk_base::StreamInterface* input_file_stream,
    254       talk_base::StreamInterface* output_file_stream);
    255   virtual ~FileVideoChannel();
    256 
    257   // Implement pure virtual methods of VideoMediaChannel.
    258   virtual bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) {
    259     return true;
    260   }
    261   virtual bool SetSendCodecs(const std::vector<VideoCodec>& codecs);
    262   virtual bool GetSendCodec(VideoCodec* send_codec) {
    263     *send_codec = VideoCodec();
    264     return true;
    265   }
    266   virtual bool SetSendStreamFormat(uint32 ssrc, const VideoFormat& format) {
    267     return true;
    268   }
    269   virtual bool SetRecvRtpHeaderExtensions(
    270       const std::vector<RtpHeaderExtension>& extensions) {
    271     return true;
    272   }
    273   virtual bool SetSendRtpHeaderExtensions(
    274       const std::vector<RtpHeaderExtension>& extensions) {
    275     return true;
    276   }
    277   virtual bool SetRender(bool render) { return true; }
    278   virtual bool SetSend(bool send);
    279   virtual bool SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
    280     return true;
    281   }
    282   virtual bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
    283     return false;
    284   }
    285   virtual bool GetStats(VideoMediaInfo* info) { return true; }
    286   virtual bool SendIntraFrame() { return false; }
    287   virtual bool RequestIntraFrame() { return false; }
    288 
    289   // Implement pure virtual methods of MediaChannel.
    290   virtual void OnPacketReceived(talk_base::Buffer* packet);
    291   virtual void OnRtcpReceived(talk_base::Buffer* packet) {}
    292   virtual void OnReadyToSend(bool ready) {}
    293   virtual bool AddSendStream(const StreamParams& sp);
    294   virtual bool RemoveSendStream(uint32 ssrc);
    295   virtual bool AddRecvStream(const StreamParams& sp) { return true; }
    296   virtual bool RemoveRecvStream(uint32 ssrc) { return true; }
    297   virtual bool MuteStream(uint32 ssrc, bool on) { return false; }
    298   virtual bool SetSendBandwidth(bool autobw, int bps) { return true; }
    299   virtual bool SetOptions(const VideoOptions& options) {
    300     options_ = options;
    301     return true;
    302   }
    303   virtual bool GetOptions(VideoOptions* options) const {
    304     *options = options_;
    305     return true;
    306   }
    307   virtual void UpdateAspectRatio(int ratio_w, int ratio_h) {}
    308 
    309  private:
    310   uint32 send_ssrc_;
    311   talk_base::scoped_ptr<RtpSenderReceiver> rtp_sender_receiver_;
    312   VideoOptions options_;
    313 
    314   DISALLOW_COPY_AND_ASSIGN(FileVideoChannel);
    315 };
    316 
    317 }  // namespace cricket
    318 
    319 #endif  // TALK_MEDIA_BASE_FILEMEDIAENGINE_H_
    320