henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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 | #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 | |
| 39 | namespace cricket { |
| 40 | |
| 41 | /////////////////////////////////////////////////////////////////////////// |
| 42 | // Implementation of FileMediaEngine. |
| 43 | /////////////////////////////////////////////////////////////////////////// |
| 44 | int 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 | |
| 61 | VoiceMediaChannel* 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 | |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 86 | return new FileVoiceChannel(input_file_stream, output_file_stream, |
| 87 | rtp_sender_thread_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | VideoMediaChannel* FileMediaEngine::CreateVideoChannel( |
| 91 | VoiceMediaChannel* voice_ch) { |
| 92 | talk_base::FileStream* input_file_stream = NULL; |
| 93 | talk_base::FileStream* output_file_stream = NULL; |
| 94 | |
| 95 | if (video_input_filename_.empty() && video_output_filename_.empty()) |
| 96 | return NULL; |
| 97 | |
| 98 | if (!video_input_filename_.empty()) { |
| 99 | input_file_stream = talk_base::Filesystem::OpenFile( |
| 100 | talk_base::Pathname(video_input_filename_), "rb"); |
| 101 | if (!input_file_stream) { |
| 102 | LOG(LS_ERROR) << "Not able to open the input video stream file."; |
| 103 | return NULL; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if (!video_output_filename_.empty()) { |
| 108 | output_file_stream = talk_base::Filesystem::OpenFile( |
| 109 | talk_base::Pathname(video_output_filename_), "wb"); |
| 110 | if (!output_file_stream) { |
| 111 | delete input_file_stream; |
| 112 | LOG(LS_ERROR) << "Not able to open the output video stream file."; |
| 113 | return NULL; |
| 114 | } |
| 115 | } |
| 116 | |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 117 | return new FileVideoChannel(input_file_stream, output_file_stream, |
| 118 | rtp_sender_thread_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /////////////////////////////////////////////////////////////////////////// |
| 122 | // Definition of RtpSenderReceiver. |
| 123 | /////////////////////////////////////////////////////////////////////////// |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 124 | class RtpSenderReceiver : public talk_base::MessageHandler { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 125 | public: |
| 126 | RtpSenderReceiver(MediaChannel* channel, |
| 127 | talk_base::StreamInterface* input_file_stream, |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 128 | talk_base::StreamInterface* output_file_stream, |
| 129 | talk_base::Thread* sender_thread); |
wu@webrtc.org | 3c5d2b4 | 2013-10-18 16:27:26 +0000 | [diff] [blame] | 130 | virtual ~RtpSenderReceiver(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 131 | |
| 132 | // Called by media channel. Context: media channel thread. |
| 133 | bool SetSend(bool send); |
| 134 | void SetSendSsrc(uint32 ssrc); |
| 135 | void OnPacketReceived(talk_base::Buffer* packet); |
| 136 | |
| 137 | // Override virtual method of parent MessageHandler. Context: Worker Thread. |
| 138 | virtual void OnMessage(talk_base::Message* pmsg); |
| 139 | |
| 140 | private: |
| 141 | // Read the next RTP dump packet, whose RTP SSRC is the same as first_ssrc_. |
| 142 | // Return true if successful. |
| 143 | bool ReadNextPacket(RtpDumpPacket* packet); |
| 144 | // Send a RTP packet to the network. The input parameter data points to the |
| 145 | // start of the RTP packet and len is the packet size. Return true if the sent |
| 146 | // size is equal to len. |
| 147 | bool SendRtpPacket(const void* data, size_t len); |
| 148 | |
| 149 | MediaChannel* media_channel_; |
| 150 | talk_base::scoped_ptr<talk_base::StreamInterface> input_stream_; |
| 151 | talk_base::scoped_ptr<talk_base::StreamInterface> output_stream_; |
| 152 | talk_base::scoped_ptr<RtpDumpLoopReader> rtp_dump_reader_; |
| 153 | talk_base::scoped_ptr<RtpDumpWriter> rtp_dump_writer_; |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 154 | talk_base::Thread* sender_thread_; |
| 155 | bool own_sender_thread_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 156 | // RTP dump packet read from the input stream. |
| 157 | RtpDumpPacket rtp_dump_packet_; |
| 158 | uint32 start_send_time_; |
| 159 | bool sending_; |
| 160 | bool first_packet_; |
| 161 | uint32 first_ssrc_; |
| 162 | |
| 163 | DISALLOW_COPY_AND_ASSIGN(RtpSenderReceiver); |
| 164 | }; |
| 165 | |
| 166 | /////////////////////////////////////////////////////////////////////////// |
| 167 | // Implementation of RtpSenderReceiver. |
| 168 | /////////////////////////////////////////////////////////////////////////// |
| 169 | RtpSenderReceiver::RtpSenderReceiver( |
| 170 | MediaChannel* channel, |
| 171 | talk_base::StreamInterface* input_file_stream, |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 172 | talk_base::StreamInterface* output_file_stream, |
| 173 | talk_base::Thread* sender_thread) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 174 | : media_channel_(channel), |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 175 | input_stream_(input_file_stream), |
| 176 | output_stream_(output_file_stream), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 177 | sending_(false), |
| 178 | first_packet_(true) { |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 179 | if (sender_thread == NULL) { |
| 180 | sender_thread_ = new talk_base::Thread(); |
| 181 | own_sender_thread_ = true; |
| 182 | } else { |
| 183 | sender_thread_ = sender_thread; |
| 184 | own_sender_thread_ = false; |
| 185 | } |
| 186 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 187 | if (input_stream_) { |
| 188 | rtp_dump_reader_.reset(new RtpDumpLoopReader(input_stream_.get())); |
| 189 | // Start the sender thread, which reads rtp dump records, waits based on |
| 190 | // the record timestamps, and sends the RTP packets to the network. |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 191 | if (own_sender_thread_) { |
| 192 | sender_thread_->Start(); |
| 193 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | // Create a rtp dump writer for the output RTP dump stream. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 197 | if (output_stream_) { |
| 198 | rtp_dump_writer_.reset(new RtpDumpWriter(output_stream_.get())); |
| 199 | } |
| 200 | } |
| 201 | |
wu@webrtc.org | 3c5d2b4 | 2013-10-18 16:27:26 +0000 | [diff] [blame] | 202 | RtpSenderReceiver::~RtpSenderReceiver() { |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 203 | if (own_sender_thread_) { |
| 204 | sender_thread_->Stop(); |
| 205 | delete sender_thread_; |
| 206 | } |
wu@webrtc.org | 3c5d2b4 | 2013-10-18 16:27:26 +0000 | [diff] [blame] | 207 | } |
| 208 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 209 | bool RtpSenderReceiver::SetSend(bool send) { |
| 210 | bool was_sending = sending_; |
| 211 | sending_ = send; |
| 212 | if (!was_sending && sending_) { |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 213 | sender_thread_->PostDelayed(0, this); // Wake up the send thread. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 214 | start_send_time_ = talk_base::Time(); |
| 215 | } |
| 216 | return true; |
| 217 | } |
| 218 | |
| 219 | void RtpSenderReceiver::SetSendSsrc(uint32 ssrc) { |
| 220 | if (rtp_dump_reader_) { |
| 221 | rtp_dump_reader_->SetSsrc(ssrc); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | void RtpSenderReceiver::OnPacketReceived(talk_base::Buffer* packet) { |
| 226 | if (rtp_dump_writer_) { |
| 227 | rtp_dump_writer_->WriteRtpPacket(packet->data(), packet->length()); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | void RtpSenderReceiver::OnMessage(talk_base::Message* pmsg) { |
| 232 | if (!sending_) { |
| 233 | // If the sender thread is not sending, ignore this message. The thread goes |
| 234 | // to sleep until SetSend(true) wakes it up. |
| 235 | return; |
| 236 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 237 | if (!first_packet_) { |
| 238 | // Send the previously read packet. |
| 239 | SendRtpPacket(&rtp_dump_packet_.data[0], rtp_dump_packet_.data.size()); |
| 240 | } |
| 241 | |
| 242 | if (ReadNextPacket(&rtp_dump_packet_)) { |
| 243 | int wait = talk_base::TimeUntil( |
| 244 | start_send_time_ + rtp_dump_packet_.elapsed_time); |
| 245 | wait = talk_base::_max(0, wait); |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 246 | sender_thread_->PostDelayed(wait, this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 247 | } else { |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 248 | sender_thread_->Quit(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| 252 | bool RtpSenderReceiver::ReadNextPacket(RtpDumpPacket* packet) { |
| 253 | while (talk_base::SR_SUCCESS == rtp_dump_reader_->ReadPacket(packet)) { |
| 254 | uint32 ssrc; |
| 255 | if (!packet->GetRtpSsrc(&ssrc)) { |
| 256 | return false; |
| 257 | } |
| 258 | if (first_packet_) { |
| 259 | first_packet_ = false; |
| 260 | first_ssrc_ = ssrc; |
| 261 | } |
| 262 | if (ssrc == first_ssrc_) { |
| 263 | return true; |
| 264 | } |
| 265 | } |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | bool RtpSenderReceiver::SendRtpPacket(const void* data, size_t len) { |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 270 | if (!media_channel_) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 271 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 272 | |
| 273 | talk_base::Buffer packet(data, len, kMaxRtpPacketLen); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 274 | return media_channel_->SendPacket(&packet); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /////////////////////////////////////////////////////////////////////////// |
| 278 | // Implementation of FileVoiceChannel. |
| 279 | /////////////////////////////////////////////////////////////////////////// |
| 280 | FileVoiceChannel::FileVoiceChannel( |
| 281 | talk_base::StreamInterface* input_file_stream, |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 282 | talk_base::StreamInterface* output_file_stream, |
| 283 | talk_base::Thread* rtp_sender_thread) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 284 | : send_ssrc_(0), |
| 285 | rtp_sender_receiver_(new RtpSenderReceiver(this, input_file_stream, |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 286 | output_file_stream, |
| 287 | rtp_sender_thread)) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 288 | |
| 289 | FileVoiceChannel::~FileVoiceChannel() {} |
| 290 | |
| 291 | bool FileVoiceChannel::SetSendCodecs(const std::vector<AudioCodec>& codecs) { |
| 292 | // TODO(whyuan): Check the format of RTP dump input. |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | bool FileVoiceChannel::SetSend(SendFlags flag) { |
| 297 | return rtp_sender_receiver_->SetSend(flag != SEND_NOTHING); |
| 298 | } |
| 299 | |
| 300 | bool FileVoiceChannel::AddSendStream(const StreamParams& sp) { |
| 301 | if (send_ssrc_ != 0 || sp.ssrcs.size() != 1) { |
| 302 | LOG(LS_ERROR) << "FileVoiceChannel only supports one send stream."; |
| 303 | return false; |
| 304 | } |
| 305 | send_ssrc_ = sp.ssrcs[0]; |
| 306 | rtp_sender_receiver_->SetSendSsrc(send_ssrc_); |
| 307 | return true; |
| 308 | } |
| 309 | |
| 310 | bool FileVoiceChannel::RemoveSendStream(uint32 ssrc) { |
| 311 | if (ssrc != send_ssrc_) |
| 312 | return false; |
| 313 | send_ssrc_ = 0; |
| 314 | rtp_sender_receiver_->SetSendSsrc(send_ssrc_); |
| 315 | return true; |
| 316 | } |
| 317 | |
wu@webrtc.org | 2018269 | 2013-12-12 22:54:25 +0000 | [diff] [blame] | 318 | void FileVoiceChannel::OnPacketReceived(talk_base::Buffer* packet) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 319 | rtp_sender_receiver_->OnPacketReceived(packet); |
| 320 | } |
| 321 | |
| 322 | /////////////////////////////////////////////////////////////////////////// |
| 323 | // Implementation of FileVideoChannel. |
| 324 | /////////////////////////////////////////////////////////////////////////// |
| 325 | FileVideoChannel::FileVideoChannel( |
| 326 | talk_base::StreamInterface* input_file_stream, |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 327 | talk_base::StreamInterface* output_file_stream, |
| 328 | talk_base::Thread* rtp_sender_thread) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 329 | : send_ssrc_(0), |
| 330 | rtp_sender_receiver_(new RtpSenderReceiver(this, input_file_stream, |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 331 | output_file_stream, |
| 332 | rtp_sender_thread)) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 333 | |
| 334 | FileVideoChannel::~FileVideoChannel() {} |
| 335 | |
| 336 | bool FileVideoChannel::SetSendCodecs(const std::vector<VideoCodec>& codecs) { |
| 337 | // TODO(whyuan): Check the format of RTP dump input. |
| 338 | return true; |
| 339 | } |
| 340 | |
| 341 | bool FileVideoChannel::SetSend(bool send) { |
| 342 | return rtp_sender_receiver_->SetSend(send); |
| 343 | } |
| 344 | |
| 345 | bool FileVideoChannel::AddSendStream(const StreamParams& sp) { |
| 346 | if (send_ssrc_ != 0 || sp.ssrcs.size() != 1) { |
| 347 | LOG(LS_ERROR) << "FileVideoChannel only support one send stream."; |
| 348 | return false; |
| 349 | } |
| 350 | send_ssrc_ = sp.ssrcs[0]; |
| 351 | rtp_sender_receiver_->SetSendSsrc(send_ssrc_); |
| 352 | return true; |
| 353 | } |
| 354 | |
| 355 | bool FileVideoChannel::RemoveSendStream(uint32 ssrc) { |
| 356 | if (ssrc != send_ssrc_) |
| 357 | return false; |
| 358 | send_ssrc_ = 0; |
| 359 | rtp_sender_receiver_->SetSendSsrc(send_ssrc_); |
| 360 | return true; |
| 361 | } |
| 362 | |
wu@webrtc.org | 2018269 | 2013-12-12 22:54:25 +0000 | [diff] [blame] | 363 | void FileVideoChannel::OnPacketReceived(talk_base::Buffer* packet) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 364 | rtp_sender_receiver_->OnPacketReceived(packet); |
| 365 | } |
| 366 | |
| 367 | } // namespace cricket |