blob: e5183f6a95407c854787aac7d69eff3814e2c0ab [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#include "talk/media/base/filemediaengine.h"
27
28#include <climits>
29
30#include "talk/base/buffer.h"
31#include "talk/base/event.h"
32#include "talk/base/logging.h"
33#include "talk/base/pathutils.h"
34#include "talk/base/stream.h"
35#include "talk/media/base/rtpdump.h"
36#include "talk/media/base/rtputils.h"
37#include "talk/media/base/streamparams.h"
38
39namespace cricket {
40
41///////////////////////////////////////////////////////////////////////////
42// Implementation of FileMediaEngine.
43///////////////////////////////////////////////////////////////////////////
44int FileMediaEngine::GetCapabilities() {
45 int capabilities = 0;
46 if (!voice_input_filename_.empty()) {
47 capabilities |= AUDIO_SEND;
48 }
49 if (!voice_output_filename_.empty()) {
50 capabilities |= AUDIO_RECV;
51 }
52 if (!video_input_filename_.empty()) {
53 capabilities |= VIDEO_SEND;
54 }
55 if (!video_output_filename_.empty()) {
56 capabilities |= VIDEO_RECV;
57 }
58 return capabilities;
59}
60
61VoiceMediaChannel* FileMediaEngine::CreateChannel() {
62 talk_base::FileStream* input_file_stream = NULL;
63 talk_base::FileStream* output_file_stream = NULL;
64
65 if (voice_input_filename_.empty() && voice_output_filename_.empty())
66 return NULL;
67 if (!voice_input_filename_.empty()) {
68 input_file_stream = talk_base::Filesystem::OpenFile(
69 talk_base::Pathname(voice_input_filename_), "rb");
70 if (!input_file_stream) {
71 LOG(LS_ERROR) << "Not able to open the input audio stream file.";
72 return NULL;
73 }
74 }
75
76 if (!voice_output_filename_.empty()) {
77 output_file_stream = talk_base::Filesystem::OpenFile(
78 talk_base::Pathname(voice_output_filename_), "wb");
79 if (!output_file_stream) {
80 delete input_file_stream;
81 LOG(LS_ERROR) << "Not able to open the output audio stream file.";
82 return NULL;
83 }
84 }
85
86 return new FileVoiceChannel(input_file_stream, output_file_stream);
87}
88
89VideoMediaChannel* FileMediaEngine::CreateVideoChannel(
90 VoiceMediaChannel* voice_ch) {
91 talk_base::FileStream* input_file_stream = NULL;
92 talk_base::FileStream* output_file_stream = NULL;
93
94 if (video_input_filename_.empty() && video_output_filename_.empty())
95 return NULL;
96
97 if (!video_input_filename_.empty()) {
98 input_file_stream = talk_base::Filesystem::OpenFile(
99 talk_base::Pathname(video_input_filename_), "rb");
100 if (!input_file_stream) {
101 LOG(LS_ERROR) << "Not able to open the input video stream file.";
102 return NULL;
103 }
104 }
105
106 if (!video_output_filename_.empty()) {
107 output_file_stream = talk_base::Filesystem::OpenFile(
108 talk_base::Pathname(video_output_filename_), "wb");
109 if (!output_file_stream) {
110 delete input_file_stream;
111 LOG(LS_ERROR) << "Not able to open the output video stream file.";
112 return NULL;
113 }
114 }
115
116 return new FileVideoChannel(input_file_stream, output_file_stream);
117}
118
119///////////////////////////////////////////////////////////////////////////
120// Definition of RtpSenderReceiver.
121///////////////////////////////////////////////////////////////////////////
122class RtpSenderReceiver
123 : public talk_base::Thread, public talk_base::MessageHandler {
124 public:
125 RtpSenderReceiver(MediaChannel* channel,
126 talk_base::StreamInterface* input_file_stream,
127 talk_base::StreamInterface* output_file_stream);
wu@webrtc.org3c5d2b42013-10-18 16:27:26 +0000128 virtual ~RtpSenderReceiver();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129
130 // Called by media channel. Context: media channel thread.
131 bool SetSend(bool send);
132 void SetSendSsrc(uint32 ssrc);
133 void OnPacketReceived(talk_base::Buffer* packet);
134
135 // Override virtual method of parent MessageHandler. Context: Worker Thread.
136 virtual void OnMessage(talk_base::Message* pmsg);
137
138 private:
139 // Read the next RTP dump packet, whose RTP SSRC is the same as first_ssrc_.
140 // Return true if successful.
141 bool ReadNextPacket(RtpDumpPacket* packet);
142 // Send a RTP packet to the network. The input parameter data points to the
143 // start of the RTP packet and len is the packet size. Return true if the sent
144 // size is equal to len.
145 bool SendRtpPacket(const void* data, size_t len);
146
147 MediaChannel* media_channel_;
148 talk_base::scoped_ptr<talk_base::StreamInterface> input_stream_;
149 talk_base::scoped_ptr<talk_base::StreamInterface> output_stream_;
150 talk_base::scoped_ptr<RtpDumpLoopReader> rtp_dump_reader_;
151 talk_base::scoped_ptr<RtpDumpWriter> rtp_dump_writer_;
152 // RTP dump packet read from the input stream.
153 RtpDumpPacket rtp_dump_packet_;
154 uint32 start_send_time_;
155 bool sending_;
156 bool first_packet_;
157 uint32 first_ssrc_;
158
159 DISALLOW_COPY_AND_ASSIGN(RtpSenderReceiver);
160};
161
162///////////////////////////////////////////////////////////////////////////
163// Implementation of RtpSenderReceiver.
164///////////////////////////////////////////////////////////////////////////
165RtpSenderReceiver::RtpSenderReceiver(
166 MediaChannel* channel,
167 talk_base::StreamInterface* input_file_stream,
168 talk_base::StreamInterface* output_file_stream)
169 : media_channel_(channel),
170 sending_(false),
171 first_packet_(true) {
172 input_stream_.reset(input_file_stream);
173 if (input_stream_) {
174 rtp_dump_reader_.reset(new RtpDumpLoopReader(input_stream_.get()));
175 // Start the sender thread, which reads rtp dump records, waits based on
176 // the record timestamps, and sends the RTP packets to the network.
177 Thread::Start();
178 }
179
180 // Create a rtp dump writer for the output RTP dump stream.
181 output_stream_.reset(output_file_stream);
182 if (output_stream_) {
183 rtp_dump_writer_.reset(new RtpDumpWriter(output_stream_.get()));
184 }
185}
186
wu@webrtc.org3c5d2b42013-10-18 16:27:26 +0000187RtpSenderReceiver::~RtpSenderReceiver() {
188 Stop();
189}
190
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191bool RtpSenderReceiver::SetSend(bool send) {
192 bool was_sending = sending_;
193 sending_ = send;
194 if (!was_sending && sending_) {
195 PostDelayed(0, this); // Wake up the send thread.
196 start_send_time_ = talk_base::Time();
197 }
198 return true;
199}
200
201void RtpSenderReceiver::SetSendSsrc(uint32 ssrc) {
202 if (rtp_dump_reader_) {
203 rtp_dump_reader_->SetSsrc(ssrc);
204 }
205}
206
207void RtpSenderReceiver::OnPacketReceived(talk_base::Buffer* packet) {
208 if (rtp_dump_writer_) {
209 rtp_dump_writer_->WriteRtpPacket(packet->data(), packet->length());
210 }
211}
212
213void RtpSenderReceiver::OnMessage(talk_base::Message* pmsg) {
214 if (!sending_) {
215 // If the sender thread is not sending, ignore this message. The thread goes
216 // to sleep until SetSend(true) wakes it up.
217 return;
218 }
219
220 if (!first_packet_) {
221 // Send the previously read packet.
222 SendRtpPacket(&rtp_dump_packet_.data[0], rtp_dump_packet_.data.size());
223 }
224
225 if (ReadNextPacket(&rtp_dump_packet_)) {
226 int wait = talk_base::TimeUntil(
227 start_send_time_ + rtp_dump_packet_.elapsed_time);
228 wait = talk_base::_max(0, wait);
229 PostDelayed(wait, this);
230 } else {
231 Quit();
232 }
233}
234
235bool RtpSenderReceiver::ReadNextPacket(RtpDumpPacket* packet) {
236 while (talk_base::SR_SUCCESS == rtp_dump_reader_->ReadPacket(packet)) {
237 uint32 ssrc;
238 if (!packet->GetRtpSsrc(&ssrc)) {
239 return false;
240 }
241 if (first_packet_) {
242 first_packet_ = false;
243 first_ssrc_ = ssrc;
244 }
245 if (ssrc == first_ssrc_) {
246 return true;
247 }
248 }
249 return false;
250}
251
252bool RtpSenderReceiver::SendRtpPacket(const void* data, size_t len) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000253 if (!media_channel_)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255
256 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000257 return media_channel_->SendPacket(&packet);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258}
259
260///////////////////////////////////////////////////////////////////////////
261// Implementation of FileVoiceChannel.
262///////////////////////////////////////////////////////////////////////////
263FileVoiceChannel::FileVoiceChannel(
264 talk_base::StreamInterface* input_file_stream,
265 talk_base::StreamInterface* output_file_stream)
266 : send_ssrc_(0),
267 rtp_sender_receiver_(new RtpSenderReceiver(this, input_file_stream,
268 output_file_stream)) {}
269
270FileVoiceChannel::~FileVoiceChannel() {}
271
272bool FileVoiceChannel::SetSendCodecs(const std::vector<AudioCodec>& codecs) {
273 // TODO(whyuan): Check the format of RTP dump input.
274 return true;
275}
276
277bool FileVoiceChannel::SetSend(SendFlags flag) {
278 return rtp_sender_receiver_->SetSend(flag != SEND_NOTHING);
279}
280
281bool FileVoiceChannel::AddSendStream(const StreamParams& sp) {
282 if (send_ssrc_ != 0 || sp.ssrcs.size() != 1) {
283 LOG(LS_ERROR) << "FileVoiceChannel only supports one send stream.";
284 return false;
285 }
286 send_ssrc_ = sp.ssrcs[0];
287 rtp_sender_receiver_->SetSendSsrc(send_ssrc_);
288 return true;
289}
290
291bool FileVoiceChannel::RemoveSendStream(uint32 ssrc) {
292 if (ssrc != send_ssrc_)
293 return false;
294 send_ssrc_ = 0;
295 rtp_sender_receiver_->SetSendSsrc(send_ssrc_);
296 return true;
297}
298
299void FileVoiceChannel::OnPacketReceived(talk_base::Buffer* packet) {
300 rtp_sender_receiver_->OnPacketReceived(packet);
301}
302
303///////////////////////////////////////////////////////////////////////////
304// Implementation of FileVideoChannel.
305///////////////////////////////////////////////////////////////////////////
306FileVideoChannel::FileVideoChannel(
307 talk_base::StreamInterface* input_file_stream,
308 talk_base::StreamInterface* output_file_stream)
309 : send_ssrc_(0),
310 rtp_sender_receiver_(new RtpSenderReceiver(this, input_file_stream,
311 output_file_stream)) {}
312
313FileVideoChannel::~FileVideoChannel() {}
314
315bool FileVideoChannel::SetSendCodecs(const std::vector<VideoCodec>& codecs) {
316 // TODO(whyuan): Check the format of RTP dump input.
317 return true;
318}
319
320bool FileVideoChannel::SetSend(bool send) {
321 return rtp_sender_receiver_->SetSend(send);
322}
323
324bool FileVideoChannel::AddSendStream(const StreamParams& sp) {
325 if (send_ssrc_ != 0 || sp.ssrcs.size() != 1) {
326 LOG(LS_ERROR) << "FileVideoChannel only support one send stream.";
327 return false;
328 }
329 send_ssrc_ = sp.ssrcs[0];
330 rtp_sender_receiver_->SetSendSsrc(send_ssrc_);
331 return true;
332}
333
334bool FileVideoChannel::RemoveSendStream(uint32 ssrc) {
335 if (ssrc != send_ssrc_)
336 return false;
337 send_ssrc_ = 0;
338 rtp_sender_receiver_->SetSendSsrc(send_ssrc_);
339 return true;
340}
341
342void FileVideoChannel::OnPacketReceived(talk_base::Buffer* packet) {
343 rtp_sender_receiver_->OnPacketReceived(packet);
344}
345
346} // namespace cricket