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
38namespace rtc {
39class StreamInterface;
40}
41
42namespace 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.
51class FileMediaEngine : public MediaEngineInterface {
52 public:
53  FileMediaEngine() : rtp_sender_thread_(NULL) {}
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(rtc::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 AudioOptions GetAudioOptions() const { return AudioOptions(); }
90  virtual bool SetAudioOptions(const AudioOptions& options) { return true; }
91  virtual bool SetAudioDelayOffset(int offset) { return true; }
92  virtual bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config) {
93    return true;
94  }
95  virtual VideoEncoderConfig GetDefaultVideoEncoderConfig() const {
96    return VideoEncoderConfig();
97  }
98  virtual bool SetSoundDevices(const Device* in_dev, const Device* out_dev) {
99    return true;
100  }
101  virtual bool SetVideoCaptureDevice(const Device* cam_device) { return true; }
102  virtual bool SetVideoCapturer(VideoCapturer* /*capturer*/) {
103    return true;
104  }
105  virtual VideoCapturer* GetVideoCapturer() const {
106    return NULL;
107  }
108  virtual bool GetOutputVolume(int* level) {
109    *level = 0;
110    return true;
111  }
112  virtual bool SetOutputVolume(int level) { return true; }
113  virtual int GetInputLevel() { return 0; }
114  virtual bool SetLocalMonitor(bool enable) { return true; }
115  // TODO(whyuan): control channel send?
116  virtual bool SetVideoCapture(bool capture) { return true; }
117  virtual const std::vector<AudioCodec>& audio_codecs() {
118    return voice_codecs_;
119  }
120  virtual const std::vector<VideoCodec>& video_codecs() {
121    return video_codecs_;
122  }
123  virtual const std::vector<RtpHeaderExtension>& audio_rtp_header_extensions() {
124    return audio_rtp_header_extensions_;
125  }
126  virtual const std::vector<RtpHeaderExtension>& video_rtp_header_extensions() {
127    return video_rtp_header_extensions_;
128  }
129
130  virtual bool FindAudioCodec(const AudioCodec& codec) { return true; }
131  virtual bool FindVideoCodec(const VideoCodec& codec) { return true; }
132  virtual void SetVoiceLogging(int min_sev, const char* filter) {}
133  virtual void SetVideoLogging(int min_sev, const char* filter) {}
134  virtual bool StartAecDump(rtc::PlatformFile) { return false; }
135
136  virtual bool RegisterVideoProcessor(VideoProcessor* processor) {
137    return true;
138  }
139  virtual bool UnregisterVideoProcessor(VideoProcessor* processor) {
140    return true;
141  }
142  virtual bool RegisterVoiceProcessor(uint32 ssrc,
143                                      VoiceProcessor* processor,
144                                      MediaProcessorDirection direction) {
145    return true;
146  }
147  virtual bool UnregisterVoiceProcessor(uint32 ssrc,
148                                        VoiceProcessor* processor,
149                                        MediaProcessorDirection direction) {
150    return true;
151  }
152  VideoFormat GetStartCaptureFormat() const {
153    return VideoFormat();
154  }
155
156  virtual sigslot::repeater2<VideoCapturer*, CaptureState>&
157      SignalVideoCaptureStateChange() {
158    return signal_state_change_;
159  }
160
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
181class RtpSenderReceiver;  // Forward declaration. Defined in the .cc file.
182
183class 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.
191  virtual bool SetRecvCodecs(const std::vector<AudioCodec>& codecs) {
192    return true;
193  }
194  virtual bool SetSendCodecs(const std::vector<AudioCodec>& codecs);
195  virtual bool SetRecvRtpHeaderExtensions(
196      const std::vector<RtpHeaderExtension>& extensions) {
197    return true;
198  }
199  virtual bool SetSendRtpHeaderExtensions(
200      const std::vector<RtpHeaderExtension>& extensions) {
201    return true;
202  }
203  virtual bool SetPlayout(bool playout) { return true; }
204  virtual bool SetSend(SendFlags flag);
205  virtual bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) {
206    return false;
207  }
208  virtual bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) {
209    return false;
210  }
211  virtual bool GetActiveStreams(AudioInfo::StreamList* actives) { return true; }
212  virtual int GetOutputLevel() { return 0; }
213  virtual int GetTimeSinceLastTyping() { return -1; }
214  virtual void SetTypingDetectionParameters(int time_window,
215    int cost_per_typing, int reporting_threshold, int penalty_decay,
216    int type_event_delay) {}
217
218  virtual bool SetOutputScaling(uint32 ssrc, double left, double right) {
219    return false;
220  }
221  virtual bool GetOutputScaling(uint32 ssrc, double* left, double* right) {
222    return false;
223  }
224  virtual bool SetRingbackTone(const char* buf, int len) { return true; }
225  virtual bool PlayRingbackTone(uint32 ssrc, bool play, bool loop) {
226    return true;
227  }
228  virtual bool InsertDtmf(uint32 ssrc, int event, int duration, int flags) {
229    return false;
230  }
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);
236  virtual void OnRtcpReceived(rtc::Buffer* packet,
237                              const rtc::PacketTime& packet_time) {}
238  virtual void OnReadyToSend(bool ready) {}
239  virtual bool AddSendStream(const StreamParams& sp);
240  virtual bool RemoveSendStream(uint32 ssrc);
241  virtual bool AddRecvStream(const StreamParams& sp) { return true; }
242  virtual bool RemoveRecvStream(uint32 ssrc) { return true; }
243  virtual bool MuteStream(uint32 ssrc, bool on) { return false; }
244  virtual bool SetStartSendBandwidth(int bps) { return true; }
245  virtual bool SetMaxSendBandwidth(int bps) { return true; }
246  virtual bool SetOptions(const AudioOptions& options) {
247    options_ = options;
248    return true;
249  }
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
263class 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.
271  virtual bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) {
272    return true;
273  }
274  virtual bool SetSendCodecs(const std::vector<VideoCodec>& codecs);
275  virtual bool GetSendCodec(VideoCodec* send_codec) {
276    *send_codec = VideoCodec();
277    return true;
278  }
279  virtual bool SetSendStreamFormat(uint32 ssrc, const VideoFormat& format) {
280    return true;
281  }
282  virtual bool SetRecvRtpHeaderExtensions(
283      const std::vector<RtpHeaderExtension>& extensions) {
284    return true;
285  }
286  virtual bool SetSendRtpHeaderExtensions(
287      const std::vector<RtpHeaderExtension>& extensions) {
288    return true;
289  }
290  virtual bool SetRender(bool render) { return true; }
291  virtual bool SetSend(bool send);
292  virtual bool SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
293    return true;
294  }
295  virtual bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
296    return false;
297  }
298  virtual bool GetStats(const StatsOptions& options, VideoMediaInfo* info) {
299    return true;
300  }
301  virtual bool SendIntraFrame() { return false; }
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);
307  virtual void OnRtcpReceived(rtc::Buffer* packet,
308                              const rtc::PacketTime& packet_time) {}
309  virtual void OnReadyToSend(bool ready) {}
310  virtual bool AddSendStream(const StreamParams& sp);
311  virtual bool RemoveSendStream(uint32 ssrc);
312  virtual bool AddRecvStream(const StreamParams& sp) { return true; }
313  virtual bool RemoveRecvStream(uint32 ssrc) { return true; }
314  virtual bool MuteStream(uint32 ssrc, bool on) { return false; }
315  virtual bool SetStartSendBandwidth(int bps) { return true; }
316  virtual bool SetMaxSendBandwidth(int bps) { return true; }
317  virtual bool SetOptions(const VideoOptions& options) {
318    options_ = options;
319    return true;
320  }
321  virtual bool GetOptions(VideoOptions* options) const {
322    *options = options_;
323    return true;
324  }
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