blob: e7956ecfac1b1c9afb842195f72410961b75c708 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001// 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:
wu@webrtc.org9caf2762013-12-11 18:25:07 +000053 FileMediaEngine() : rtp_sender_thread_(NULL) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054 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; }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000089 virtual AudioOptions GetAudioOptions() const { return AudioOptions(); }
90 virtual bool SetAudioOptions(const AudioOptions& options) { return true; }
91 virtual bool SetVideoOptions(const VideoOptions& options) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 virtual bool SetAudioDelayOffset(int offset) { return true; }
93 virtual bool SetDefaultVideoEncoderConfig(const VideoEncoderConfig& config) {
94 return true;
95 }
wu@webrtc.org78187522013-10-07 23:32:02 +000096 virtual VideoEncoderConfig GetDefaultVideoEncoderConfig() const {
97 return VideoEncoderConfig();
98 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 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) {}
wu@webrtc.orga9890802013-12-13 00:21:03 +0000136 virtual bool StartAecDump(FILE* file) { return false; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137
138 virtual bool RegisterVideoProcessor(VideoProcessor* processor) {
139 return true;
140 }
141 virtual bool UnregisterVideoProcessor(VideoProcessor* processor) {
142 return true;
143 }
144 virtual bool RegisterVoiceProcessor(uint32 ssrc,
145 VoiceProcessor* processor,
146 MediaProcessorDirection direction) {
147 return true;
148 }
149 virtual bool UnregisterVoiceProcessor(uint32 ssrc,
150 VoiceProcessor* processor,
151 MediaProcessorDirection direction) {
152 return true;
153 }
154 VideoFormat GetStartCaptureFormat() const {
155 return VideoFormat();
156 }
157
158 virtual sigslot::repeater2<VideoCapturer*, CaptureState>&
159 SignalVideoCaptureStateChange() {
160 return signal_state_change_;
161 }
162
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000163 void set_rtp_sender_thread(talk_base::Thread* thread) {
164 rtp_sender_thread_ = thread;
165 }
166
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 private:
168 std::string voice_input_filename_;
169 std::string voice_output_filename_;
170 std::string video_input_filename_;
171 std::string video_output_filename_;
172 std::vector<AudioCodec> voice_codecs_;
173 std::vector<VideoCodec> video_codecs_;
174 std::vector<RtpHeaderExtension> audio_rtp_header_extensions_;
175 std::vector<RtpHeaderExtension> video_rtp_header_extensions_;
176 sigslot::repeater2<VideoCapturer*, CaptureState>
177 signal_state_change_;
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000178 talk_base::Thread* rtp_sender_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179
180 DISALLOW_COPY_AND_ASSIGN(FileMediaEngine);
181};
182
183class RtpSenderReceiver; // Forward declaration. Defined in the .cc file.
184
185class FileVoiceChannel : public VoiceMediaChannel {
186 public:
187 FileVoiceChannel(talk_base::StreamInterface* input_file_stream,
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000188 talk_base::StreamInterface* output_file_stream,
189 talk_base::Thread* rtp_sender_thread);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 virtual ~FileVoiceChannel();
191
192 // Implement pure virtual methods of VoiceMediaChannel.
193 virtual bool SetRecvCodecs(const std::vector<AudioCodec>& codecs) {
194 return true;
195 }
196 virtual bool SetSendCodecs(const std::vector<AudioCodec>& codecs);
197 virtual bool SetRecvRtpHeaderExtensions(
198 const std::vector<RtpHeaderExtension>& extensions) {
199 return true;
200 }
201 virtual bool SetSendRtpHeaderExtensions(
202 const std::vector<RtpHeaderExtension>& extensions) {
203 return true;
204 }
205 virtual bool SetPlayout(bool playout) { return true; }
206 virtual bool SetSend(SendFlags flag);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000207 virtual bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) {
208 return false;
209 }
210 virtual bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211 return false;
212 }
213 virtual bool GetActiveStreams(AudioInfo::StreamList* actives) { return true; }
214 virtual int GetOutputLevel() { return 0; }
215 virtual int GetTimeSinceLastTyping() { return -1; }
216 virtual void SetTypingDetectionParameters(int time_window,
217 int cost_per_typing, int reporting_threshold, int penalty_decay,
218 int type_event_delay) {}
219
220 virtual bool SetOutputScaling(uint32 ssrc, double left, double right) {
221 return false;
222 }
223 virtual bool GetOutputScaling(uint32 ssrc, double* left, double* right) {
224 return false;
225 }
226 virtual bool SetRingbackTone(const char* buf, int len) { return true; }
227 virtual bool PlayRingbackTone(uint32 ssrc, bool play, bool loop) {
228 return true;
229 }
230 virtual bool InsertDtmf(uint32 ssrc, int event, int duration, int flags) {
231 return false;
232 }
233 virtual bool GetStats(VoiceMediaInfo* info) { return true; }
234
235 // Implement pure virtual methods of MediaChannel.
wu@webrtc.orga9890802013-12-13 00:21:03 +0000236 virtual void OnPacketReceived(talk_base::Buffer* packet,
237 const talk_base::PacketTime& packet_time);
238 virtual void OnRtcpReceived(talk_base::Buffer* packet,
239 const talk_base::PacketTime& packet_time) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240 virtual void OnReadyToSend(bool ready) {}
241 virtual bool AddSendStream(const StreamParams& sp);
242 virtual bool RemoveSendStream(uint32 ssrc);
243 virtual bool AddRecvStream(const StreamParams& sp) { return true; }
244 virtual bool RemoveRecvStream(uint32 ssrc) { return true; }
245 virtual bool MuteStream(uint32 ssrc, bool on) { return false; }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000246 virtual bool SetStartSendBandwidth(int bps) { return true; }
247 virtual bool SetMaxSendBandwidth(int bps) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248 virtual bool SetOptions(const AudioOptions& options) {
249 options_ = options;
250 return true;
251 }
252 virtual bool GetOptions(AudioOptions* options) const {
253 *options = options_;
254 return true;
255 }
256
257 private:
258 uint32 send_ssrc_;
259 talk_base::scoped_ptr<RtpSenderReceiver> rtp_sender_receiver_;
260 AudioOptions options_;
261
262 DISALLOW_COPY_AND_ASSIGN(FileVoiceChannel);
263};
264
265class FileVideoChannel : public VideoMediaChannel {
266 public:
267 FileVideoChannel(talk_base::StreamInterface* input_file_stream,
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000268 talk_base::StreamInterface* output_file_stream,
269 talk_base::Thread* rtp_sender_thread);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270 virtual ~FileVideoChannel();
271
272 // Implement pure virtual methods of VideoMediaChannel.
273 virtual bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) {
274 return true;
275 }
276 virtual bool SetSendCodecs(const std::vector<VideoCodec>& codecs);
277 virtual bool GetSendCodec(VideoCodec* send_codec) {
278 *send_codec = VideoCodec();
279 return true;
280 }
281 virtual bool SetSendStreamFormat(uint32 ssrc, const VideoFormat& format) {
282 return true;
283 }
284 virtual bool SetRecvRtpHeaderExtensions(
285 const std::vector<RtpHeaderExtension>& extensions) {
286 return true;
287 }
288 virtual bool SetSendRtpHeaderExtensions(
289 const std::vector<RtpHeaderExtension>& extensions) {
290 return true;
291 }
292 virtual bool SetRender(bool render) { return true; }
293 virtual bool SetSend(bool send);
294 virtual bool SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
295 return true;
296 }
297 virtual bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
298 return false;
299 }
300 virtual bool GetStats(VideoMediaInfo* info) { return true; }
301 virtual bool SendIntraFrame() { return false; }
302 virtual bool RequestIntraFrame() { return false; }
303
304 // Implement pure virtual methods of MediaChannel.
wu@webrtc.orga9890802013-12-13 00:21:03 +0000305 virtual void OnPacketReceived(talk_base::Buffer* packet,
306 const talk_base::PacketTime& packet_time);
307 virtual void OnRtcpReceived(talk_base::Buffer* packet,
308 const talk_base::PacketTime& packet_time) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309 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; }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000315 virtual bool SetStartSendBandwidth(int bps) { return true; }
316 virtual bool SetMaxSendBandwidth(int bps) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000317 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 talk_base::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_