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
38namespace talk_base {
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(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 AudioOptions GetAudioOptions() const { return AudioOptions(); }
90  virtual bool SetAudioOptions(const AudioOptions& options) { return true; }
91  virtual bool SetVideoOptions(const VideoOptions& options) { return true; }
92  virtual bool SetAudioDelayOffset(int offset) { return true; }
93  virtual bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config) {
94    return true;
95  }
96  virtual VideoEncoderConfig GetDefaultVideoEncoderConfig() const {
97    return VideoEncoderConfig();
98  }
99  virtual bool SetSoundDevices(const Device* in_dev, const Device* out_dev) {
100    return true;
101  }
102  virtual bool SetVideoCaptureDevice(const Device* cam_device) { return true; }
103  virtual bool SetVideoCapturer(VideoCapturer* /*capturer*/) {
104    return true;
105  }
106  virtual VideoCapturer* GetVideoCapturer() const {
107    return NULL;
108  }
109  virtual bool GetOutputVolume(int* level) {
110    *level = 0;
111    return true;
112  }
113  virtual bool SetOutputVolume(int level) { return true; }
114  virtual int GetInputLevel() { return 0; }
115  virtual bool SetLocalMonitor(bool enable) { return true; }
116  virtual bool SetLocalRenderer(VideoRenderer* renderer) { return true; }
117  // TODO(whyuan): control channel send?
118  virtual bool SetVideoCapture(bool capture) { return true; }
119  virtual const std::vector<AudioCodec>& audio_codecs() {
120    return voice_codecs_;
121  }
122  virtual const std::vector<VideoCodec>& video_codecs() {
123    return video_codecs_;
124  }
125  virtual const std::vector<RtpHeaderExtension>& audio_rtp_header_extensions() {
126    return audio_rtp_header_extensions_;
127  }
128  virtual const std::vector<RtpHeaderExtension>& video_rtp_header_extensions() {
129    return video_rtp_header_extensions_;
130  }
131
132  virtual bool FindAudioCodec(const AudioCodec& codec) { return true; }
133  virtual bool FindVideoCodec(const VideoCodec& codec) { return true; }
134  virtual void SetVoiceLogging(int min_sev, const char* filter) {}
135  virtual void SetVideoLogging(int min_sev, const char* filter) {}
136
137  virtual bool RegisterVideoProcessor(VideoProcessor* processor) {
138    return true;
139  }
140  virtual bool UnregisterVideoProcessor(VideoProcessor* processor) {
141    return true;
142  }
143  virtual bool RegisterVoiceProcessor(uint32 ssrc,
144                                      VoiceProcessor* processor,
145                                      MediaProcessorDirection direction) {
146    return true;
147  }
148  virtual bool UnregisterVoiceProcessor(uint32 ssrc,
149                                        VoiceProcessor* processor,
150                                        MediaProcessorDirection direction) {
151    return true;
152  }
153  VideoFormat GetStartCaptureFormat() const {
154    return VideoFormat();
155  }
156
157  virtual sigslot::repeater2<VideoCapturer*, CaptureState>&
158      SignalVideoCaptureStateChange() {
159    return signal_state_change_;
160  }
161
162  void set_rtp_sender_thread(talk_base::Thread* thread) {
163    rtp_sender_thread_ = thread;
164  }
165
166 private:
167  std::string voice_input_filename_;
168  std::string voice_output_filename_;
169  std::string video_input_filename_;
170  std::string video_output_filename_;
171  std::vector<AudioCodec> voice_codecs_;
172  std::vector<VideoCodec> video_codecs_;
173  std::vector<RtpHeaderExtension> audio_rtp_header_extensions_;
174  std::vector<RtpHeaderExtension> video_rtp_header_extensions_;
175  sigslot::repeater2<VideoCapturer*, CaptureState>
176     signal_state_change_;
177  talk_base::Thread* rtp_sender_thread_;
178
179  DISALLOW_COPY_AND_ASSIGN(FileMediaEngine);
180};
181
182class RtpSenderReceiver;  // Forward declaration. Defined in the .cc file.
183
184class FileVoiceChannel : public VoiceMediaChannel {
185 public:
186  FileVoiceChannel(talk_base::StreamInterface* input_file_stream,
187      talk_base::StreamInterface* output_file_stream,
188      talk_base::Thread* rtp_sender_thread);
189  virtual ~FileVoiceChannel();
190
191  // Implement pure virtual methods of VoiceMediaChannel.
192  virtual bool SetRecvCodecs(const std::vector<AudioCodec>& codecs) {
193    return true;
194  }
195  virtual bool SetSendCodecs(const std::vector<AudioCodec>& codecs);
196  virtual bool SetRecvRtpHeaderExtensions(
197      const std::vector<RtpHeaderExtension>& extensions) {
198    return true;
199  }
200  virtual bool SetSendRtpHeaderExtensions(
201      const std::vector<RtpHeaderExtension>& extensions) {
202    return true;
203  }
204  virtual bool SetPlayout(bool playout) { return true; }
205  virtual bool SetSend(SendFlags flag);
206  virtual bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) {
207    return false;
208  }
209  virtual bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) {
210    return false;
211  }
212  virtual bool GetActiveStreams(AudioInfo::StreamList* actives) { return true; }
213  virtual int GetOutputLevel() { return 0; }
214  virtual int GetTimeSinceLastTyping() { return -1; }
215  virtual void SetTypingDetectionParameters(int time_window,
216    int cost_per_typing, int reporting_threshold, int penalty_decay,
217    int type_event_delay) {}
218
219  virtual bool SetOutputScaling(uint32 ssrc, double left, double right) {
220    return false;
221  }
222  virtual bool GetOutputScaling(uint32 ssrc, double* left, double* right) {
223    return false;
224  }
225  virtual bool SetRingbackTone(const char* buf, int len) { return true; }
226  virtual bool PlayRingbackTone(uint32 ssrc, bool play, bool loop) {
227    return true;
228  }
229  virtual bool InsertDtmf(uint32 ssrc, int event, int duration, int flags) {
230    return false;
231  }
232  virtual bool GetStats(VoiceMediaInfo* info) { return true; }
233
234  // Implement pure virtual methods of MediaChannel.
235  virtual void OnPacketReceived(talk_base::Buffer* packet,
236                                const talk_base::PacketTime& packet_time);
237  virtual void OnRtcpReceived(talk_base::Buffer* packet,
238                              const talk_base::PacketTime& packet_time) {}
239  virtual void OnReadyToSend(bool ready) {}
240  virtual bool AddSendStream(const StreamParams& sp);
241  virtual bool RemoveSendStream(uint32 ssrc);
242  virtual bool AddRecvStream(const StreamParams& sp) { return true; }
243  virtual bool RemoveRecvStream(uint32 ssrc) { return true; }
244  virtual bool MuteStream(uint32 ssrc, bool on) { return false; }
245  virtual bool SetSendBandwidth(bool autobw, 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  talk_base::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(talk_base::StreamInterface* input_file_stream,
266      talk_base::StreamInterface* output_file_stream,
267      talk_base::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(VideoMediaInfo* info) { return true; }
299  virtual bool SendIntraFrame() { return false; }
300  virtual bool RequestIntraFrame() { return false; }
301
302  // Implement pure virtual methods of MediaChannel.
303  virtual void OnPacketReceived(talk_base::Buffer* packet,
304                                const talk_base::PacketTime& packet_time);
305  virtual void OnRtcpReceived(talk_base::Buffer* packet,
306                              const talk_base::PacketTime& packet_time) {}
307  virtual void OnReadyToSend(bool ready) {}
308  virtual bool AddSendStream(const StreamParams& sp);
309  virtual bool RemoveSendStream(uint32 ssrc);
310  virtual bool AddRecvStream(const StreamParams& sp) { return true; }
311  virtual bool RemoveRecvStream(uint32 ssrc) { return true; }
312  virtual bool MuteStream(uint32 ssrc, bool on) { return false; }
313  virtual bool SetSendBandwidth(bool autobw, int bps) { return true; }
314  virtual bool SetOptions(const VideoOptions& options) {
315    options_ = options;
316    return true;
317  }
318  virtual bool GetOptions(VideoOptions* options) const {
319    *options = options_;
320    return true;
321  }
322  virtual void UpdateAspectRatio(int ratio_w, int ratio_h) {}
323
324 private:
325  uint32 send_ssrc_;
326  talk_base::scoped_ptr<RtpSenderReceiver> rtp_sender_receiver_;
327  VideoOptions options_;
328
329  DISALLOW_COPY_AND_ASSIGN(FileVideoChannel);
330};
331
332}  // namespace cricket
333
334#endif  // TALK_MEDIA_BASE_FILEMEDIAENGINE_H_
335