• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/media/base/codec.h"
33 #include "talk/media/base/mediachannel.h"
34 #include "talk/media/base/mediaengine.h"
35 #include "webrtc/base/scoped_ptr.h"
36 #include "webrtc/base/stream.h"
37 
38 namespace rtc {
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:
FileMediaEngine()53   FileMediaEngine() : rtp_sender_thread_(NULL) {}
~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.
set_voice_input_filename(const std::string & filename)58   void set_voice_input_filename(const std::string& filename) {
59     voice_input_filename_ = filename;
60   }
set_voice_output_filename(const std::string & filename)61   void set_voice_output_filename(const std::string& filename) {
62     voice_output_filename_ = filename;
63   }
set_video_input_filename(const std::string & filename)64   void set_video_input_filename(const std::string& filename) {
65     video_input_filename_ = filename;
66   }
set_video_output_filename(const std::string & filename)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.
set_voice_codecs(const std::vector<AudioCodec> & codecs)73   void set_voice_codecs(const std::vector<AudioCodec>& codecs) {
74     voice_codecs_ = codecs;
75   }
set_video_codecs(const std::vector<VideoCodec> & codecs)76   void set_video_codecs(const std::vector<VideoCodec>& codecs) {
77     video_codecs_ = codecs;
78   }
79 
80   // Implement pure virtual methods of MediaEngine.
Init(rtc::Thread * worker_thread)81   virtual bool Init(rtc::Thread* worker_thread) {
82     return true;
83   }
Terminate()84   virtual void Terminate() {}
85   virtual int GetCapabilities();
86   virtual VoiceMediaChannel* CreateChannel();
87   virtual VideoMediaChannel* CreateVideoChannel(VoiceMediaChannel* voice_ch);
CreateSoundclip()88   virtual SoundclipMedia* CreateSoundclip() { return NULL; }
GetAudioOptions()89   virtual AudioOptions GetAudioOptions() const { return AudioOptions(); }
SetAudioOptions(const AudioOptions & options)90   virtual bool SetAudioOptions(const AudioOptions& options) { return true; }
SetAudioDelayOffset(int offset)91   virtual bool SetAudioDelayOffset(int offset) { return true; }
SetDefaultVideoEncoderConfig(const VideoEncoderConfig & config)92   virtual bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config) {
93     return true;
94   }
GetDefaultVideoEncoderConfig()95   virtual VideoEncoderConfig GetDefaultVideoEncoderConfig() const {
96     return VideoEncoderConfig();
97   }
SetSoundDevices(const Device * in_dev,const Device * out_dev)98   virtual bool SetSoundDevices(const Device* in_dev, const Device* out_dev) {
99     return true;
100   }
SetVideoCaptureDevice(const Device * cam_device)101   virtual bool SetVideoCaptureDevice(const Device* cam_device) { return true; }
SetVideoCapturer(VideoCapturer *)102   virtual bool SetVideoCapturer(VideoCapturer* /*capturer*/) {
103     return true;
104   }
GetVideoCapturer()105   virtual VideoCapturer* GetVideoCapturer() const {
106     return NULL;
107   }
GetOutputVolume(int * level)108   virtual bool GetOutputVolume(int* level) {
109     *level = 0;
110     return true;
111   }
SetOutputVolume(int level)112   virtual bool SetOutputVolume(int level) { return true; }
GetInputLevel()113   virtual int GetInputLevel() { return 0; }
SetLocalMonitor(bool enable)114   virtual bool SetLocalMonitor(bool enable) { return true; }
115   // TODO(whyuan): control channel send?
SetVideoCapture(bool capture)116   virtual bool SetVideoCapture(bool capture) { return true; }
audio_codecs()117   virtual const std::vector<AudioCodec>& audio_codecs() {
118     return voice_codecs_;
119   }
video_codecs()120   virtual const std::vector<VideoCodec>& video_codecs() {
121     return video_codecs_;
122   }
audio_rtp_header_extensions()123   virtual const std::vector<RtpHeaderExtension>& audio_rtp_header_extensions() {
124     return audio_rtp_header_extensions_;
125   }
video_rtp_header_extensions()126   virtual const std::vector<RtpHeaderExtension>& video_rtp_header_extensions() {
127     return video_rtp_header_extensions_;
128   }
129 
FindAudioCodec(const AudioCodec & codec)130   virtual bool FindAudioCodec(const AudioCodec& codec) { return true; }
FindVideoCodec(const VideoCodec & codec)131   virtual bool FindVideoCodec(const VideoCodec& codec) { return true; }
SetVoiceLogging(int min_sev,const char * filter)132   virtual void SetVoiceLogging(int min_sev, const char* filter) {}
SetVideoLogging(int min_sev,const char * filter)133   virtual void SetVideoLogging(int min_sev, const char* filter) {}
StartAecDump(rtc::PlatformFile)134   virtual bool StartAecDump(rtc::PlatformFile) { return false; }
135 
RegisterVideoProcessor(VideoProcessor * processor)136   virtual bool RegisterVideoProcessor(VideoProcessor* processor) {
137     return true;
138   }
UnregisterVideoProcessor(VideoProcessor * processor)139   virtual bool UnregisterVideoProcessor(VideoProcessor* processor) {
140     return true;
141   }
RegisterVoiceProcessor(uint32 ssrc,VoiceProcessor * processor,MediaProcessorDirection direction)142   virtual bool RegisterVoiceProcessor(uint32 ssrc,
143                                       VoiceProcessor* processor,
144                                       MediaProcessorDirection direction) {
145     return true;
146   }
UnregisterVoiceProcessor(uint32 ssrc,VoiceProcessor * processor,MediaProcessorDirection direction)147   virtual bool UnregisterVoiceProcessor(uint32 ssrc,
148                                         VoiceProcessor* processor,
149                                         MediaProcessorDirection direction) {
150     return true;
151   }
GetStartCaptureFormat()152   VideoFormat GetStartCaptureFormat() const {
153     return VideoFormat();
154   }
155 
156   virtual sigslot::repeater2<VideoCapturer*, CaptureState>&
SignalVideoCaptureStateChange()157       SignalVideoCaptureStateChange() {
158     return signal_state_change_;
159   }
160 
set_rtp_sender_thread(rtc::Thread * thread)161   void set_rtp_sender_thread(rtc::Thread* thread) {
162     rtp_sender_thread_ = thread;
163   }
164 
165  private:
166   std::string voice_input_filename_;
167   std::string voice_output_filename_;
168   std::string video_input_filename_;
169   std::string video_output_filename_;
170   std::vector<AudioCodec> voice_codecs_;
171   std::vector<VideoCodec> video_codecs_;
172   std::vector<RtpHeaderExtension> audio_rtp_header_extensions_;
173   std::vector<RtpHeaderExtension> video_rtp_header_extensions_;
174   sigslot::repeater2<VideoCapturer*, CaptureState>
175      signal_state_change_;
176   rtc::Thread* rtp_sender_thread_;
177 
178   DISALLOW_COPY_AND_ASSIGN(FileMediaEngine);
179 };
180 
181 class RtpSenderReceiver;  // Forward declaration. Defined in the .cc file.
182 
183 class FileVoiceChannel : public VoiceMediaChannel {
184  public:
185   FileVoiceChannel(rtc::StreamInterface* input_file_stream,
186       rtc::StreamInterface* output_file_stream,
187       rtc::Thread* rtp_sender_thread);
188   virtual ~FileVoiceChannel();
189 
190   // Implement pure virtual methods of VoiceMediaChannel.
SetRecvCodecs(const std::vector<AudioCodec> & codecs)191   virtual bool SetRecvCodecs(const std::vector<AudioCodec>& codecs) {
192     return true;
193   }
194   virtual bool SetSendCodecs(const std::vector<AudioCodec>& codecs);
SetRecvRtpHeaderExtensions(const std::vector<RtpHeaderExtension> & extensions)195   virtual bool SetRecvRtpHeaderExtensions(
196       const std::vector<RtpHeaderExtension>& extensions) {
197     return true;
198   }
SetSendRtpHeaderExtensions(const std::vector<RtpHeaderExtension> & extensions)199   virtual bool SetSendRtpHeaderExtensions(
200       const std::vector<RtpHeaderExtension>& extensions) {
201     return true;
202   }
SetPlayout(bool playout)203   virtual bool SetPlayout(bool playout) { return true; }
204   virtual bool SetSend(SendFlags flag);
SetRemoteRenderer(uint32 ssrc,AudioRenderer * renderer)205   virtual bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) {
206     return false;
207   }
SetLocalRenderer(uint32 ssrc,AudioRenderer * renderer)208   virtual bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) {
209     return false;
210   }
GetActiveStreams(AudioInfo::StreamList * actives)211   virtual bool GetActiveStreams(AudioInfo::StreamList* actives) { return true; }
GetOutputLevel()212   virtual int GetOutputLevel() { return 0; }
GetTimeSinceLastTyping()213   virtual int GetTimeSinceLastTyping() { return -1; }
SetTypingDetectionParameters(int time_window,int cost_per_typing,int reporting_threshold,int penalty_decay,int type_event_delay)214   virtual void SetTypingDetectionParameters(int time_window,
215     int cost_per_typing, int reporting_threshold, int penalty_decay,
216     int type_event_delay) {}
217 
SetOutputScaling(uint32 ssrc,double left,double right)218   virtual bool SetOutputScaling(uint32 ssrc, double left, double right) {
219     return false;
220   }
GetOutputScaling(uint32 ssrc,double * left,double * right)221   virtual bool GetOutputScaling(uint32 ssrc, double* left, double* right) {
222     return false;
223   }
SetRingbackTone(const char * buf,int len)224   virtual bool SetRingbackTone(const char* buf, int len) { return true; }
PlayRingbackTone(uint32 ssrc,bool play,bool loop)225   virtual bool PlayRingbackTone(uint32 ssrc, bool play, bool loop) {
226     return true;
227   }
InsertDtmf(uint32 ssrc,int event,int duration,int flags)228   virtual bool InsertDtmf(uint32 ssrc, int event, int duration, int flags) {
229     return false;
230   }
GetStats(VoiceMediaInfo * info)231   virtual bool GetStats(VoiceMediaInfo* info) { return true; }
232 
233   // Implement pure virtual methods of MediaChannel.
234   virtual void OnPacketReceived(rtc::Buffer* packet,
235                                 const rtc::PacketTime& packet_time);
OnRtcpReceived(rtc::Buffer * packet,const rtc::PacketTime & packet_time)236   virtual void OnRtcpReceived(rtc::Buffer* packet,
237                               const rtc::PacketTime& packet_time) {}
OnReadyToSend(bool ready)238   virtual void OnReadyToSend(bool ready) {}
239   virtual bool AddSendStream(const StreamParams& sp);
240   virtual bool RemoveSendStream(uint32 ssrc);
AddRecvStream(const StreamParams & sp)241   virtual bool AddRecvStream(const StreamParams& sp) { return true; }
RemoveRecvStream(uint32 ssrc)242   virtual bool RemoveRecvStream(uint32 ssrc) { return true; }
MuteStream(uint32 ssrc,bool on)243   virtual bool MuteStream(uint32 ssrc, bool on) { return false; }
SetStartSendBandwidth(int bps)244   virtual bool SetStartSendBandwidth(int bps) { return true; }
SetMaxSendBandwidth(int bps)245   virtual bool SetMaxSendBandwidth(int bps) { return true; }
SetOptions(const AudioOptions & options)246   virtual bool SetOptions(const AudioOptions& options) {
247     options_ = options;
248     return true;
249   }
GetOptions(AudioOptions * options)250   virtual bool GetOptions(AudioOptions* options) const {
251     *options = options_;
252     return true;
253   }
254 
255  private:
256   uint32 send_ssrc_;
257   rtc::scoped_ptr<RtpSenderReceiver> rtp_sender_receiver_;
258   AudioOptions options_;
259 
260   DISALLOW_COPY_AND_ASSIGN(FileVoiceChannel);
261 };
262 
263 class FileVideoChannel : public VideoMediaChannel {
264  public:
265   FileVideoChannel(rtc::StreamInterface* input_file_stream,
266       rtc::StreamInterface* output_file_stream,
267       rtc::Thread* rtp_sender_thread);
268   virtual ~FileVideoChannel();
269 
270   // Implement pure virtual methods of VideoMediaChannel.
SetRecvCodecs(const std::vector<VideoCodec> & codecs)271   virtual bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) {
272     return true;
273   }
274   virtual bool SetSendCodecs(const std::vector<VideoCodec>& codecs);
GetSendCodec(VideoCodec * send_codec)275   virtual bool GetSendCodec(VideoCodec* send_codec) {
276     *send_codec = VideoCodec();
277     return true;
278   }
SetSendStreamFormat(uint32 ssrc,const VideoFormat & format)279   virtual bool SetSendStreamFormat(uint32 ssrc, const VideoFormat& format) {
280     return true;
281   }
SetRecvRtpHeaderExtensions(const std::vector<RtpHeaderExtension> & extensions)282   virtual bool SetRecvRtpHeaderExtensions(
283       const std::vector<RtpHeaderExtension>& extensions) {
284     return true;
285   }
SetSendRtpHeaderExtensions(const std::vector<RtpHeaderExtension> & extensions)286   virtual bool SetSendRtpHeaderExtensions(
287       const std::vector<RtpHeaderExtension>& extensions) {
288     return true;
289   }
SetRender(bool render)290   virtual bool SetRender(bool render) { return true; }
291   virtual bool SetSend(bool send);
SetRenderer(uint32 ssrc,VideoRenderer * renderer)292   virtual bool SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
293     return true;
294   }
SetCapturer(uint32 ssrc,VideoCapturer * capturer)295   virtual bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
296     return false;
297   }
GetStats(const StatsOptions & options,VideoMediaInfo * info)298   virtual bool GetStats(const StatsOptions& options, VideoMediaInfo* info) {
299     return true;
300   }
SendIntraFrame()301   virtual bool SendIntraFrame() { return false; }
RequestIntraFrame()302   virtual bool RequestIntraFrame() { return false; }
303 
304   // Implement pure virtual methods of MediaChannel.
305   virtual void OnPacketReceived(rtc::Buffer* packet,
306                                 const rtc::PacketTime& packet_time);
OnRtcpReceived(rtc::Buffer * packet,const rtc::PacketTime & packet_time)307   virtual void OnRtcpReceived(rtc::Buffer* packet,
308                               const rtc::PacketTime& packet_time) {}
OnReadyToSend(bool ready)309   virtual void OnReadyToSend(bool ready) {}
310   virtual bool AddSendStream(const StreamParams& sp);
311   virtual bool RemoveSendStream(uint32 ssrc);
AddRecvStream(const StreamParams & sp)312   virtual bool AddRecvStream(const StreamParams& sp) { return true; }
RemoveRecvStream(uint32 ssrc)313   virtual bool RemoveRecvStream(uint32 ssrc) { return true; }
MuteStream(uint32 ssrc,bool on)314   virtual bool MuteStream(uint32 ssrc, bool on) { return false; }
SetStartSendBandwidth(int bps)315   virtual bool SetStartSendBandwidth(int bps) { return true; }
SetMaxSendBandwidth(int bps)316   virtual bool SetMaxSendBandwidth(int bps) { return true; }
SetOptions(const VideoOptions & options)317   virtual bool SetOptions(const VideoOptions& options) {
318     options_ = options;
319     return true;
320   }
GetOptions(VideoOptions * options)321   virtual bool GetOptions(VideoOptions* options) const {
322     *options = options_;
323     return true;
324   }
UpdateAspectRatio(int ratio_w,int ratio_h)325   virtual void UpdateAspectRatio(int ratio_w, int ratio_h) {}
326 
327  private:
328   uint32 send_ssrc_;
329   rtc::scoped_ptr<RtpSenderReceiver> rtp_sender_receiver_;
330   VideoOptions options_;
331 
332   DISALLOW_COPY_AND_ASSIGN(FileVideoChannel);
333 };
334 
335 }  // namespace cricket
336 
337 #endif  // TALK_MEDIA_BASE_FILEMEDIAENGINE_H_
338