2013-07-10 02:45:36 +02:00
|
|
|
// libjingle
|
|
|
|
// Copyright 2004 Google Inc.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// 3. The name of the author may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
|
|
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#include "talk/media/base/filemediaengine.h"
|
|
|
|
|
2014-03-07 16:22:04 +01:00
|
|
|
#include <limits.h>
|
2013-07-10 02:45:36 +02:00
|
|
|
|
2014-08-13 19:26:08 +02:00
|
|
|
#include "talk/media/base/rtpdump.h"
|
|
|
|
#include "talk/media/base/rtputils.h"
|
|
|
|
#include "talk/media/base/streamparams.h"
|
2014-07-29 19:36:52 +02:00
|
|
|
#include "webrtc/base/buffer.h"
|
|
|
|
#include "webrtc/base/event.h"
|
|
|
|
#include "webrtc/base/logging.h"
|
|
|
|
#include "webrtc/base/pathutils.h"
|
|
|
|
#include "webrtc/base/stream.h"
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
namespace cricket {
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Implementation of FileMediaEngine.
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
int FileMediaEngine::GetCapabilities() {
|
|
|
|
int capabilities = 0;
|
|
|
|
if (!voice_input_filename_.empty()) {
|
|
|
|
capabilities |= AUDIO_SEND;
|
|
|
|
}
|
|
|
|
if (!voice_output_filename_.empty()) {
|
|
|
|
capabilities |= AUDIO_RECV;
|
|
|
|
}
|
|
|
|
if (!video_input_filename_.empty()) {
|
|
|
|
capabilities |= VIDEO_SEND;
|
|
|
|
}
|
|
|
|
if (!video_output_filename_.empty()) {
|
|
|
|
capabilities |= VIDEO_RECV;
|
|
|
|
}
|
|
|
|
return capabilities;
|
|
|
|
}
|
|
|
|
|
|
|
|
VoiceMediaChannel* FileMediaEngine::CreateChannel() {
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::FileStream* input_file_stream = NULL;
|
|
|
|
rtc::FileStream* output_file_stream = NULL;
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
if (voice_input_filename_.empty() && voice_output_filename_.empty())
|
|
|
|
return NULL;
|
|
|
|
if (!voice_input_filename_.empty()) {
|
2014-07-29 19:36:52 +02:00
|
|
|
input_file_stream = rtc::Filesystem::OpenFile(
|
|
|
|
rtc::Pathname(voice_input_filename_), "rb");
|
2013-07-10 02:45:36 +02:00
|
|
|
if (!input_file_stream) {
|
|
|
|
LOG(LS_ERROR) << "Not able to open the input audio stream file.";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!voice_output_filename_.empty()) {
|
2014-07-29 19:36:52 +02:00
|
|
|
output_file_stream = rtc::Filesystem::OpenFile(
|
|
|
|
rtc::Pathname(voice_output_filename_), "wb");
|
2013-07-10 02:45:36 +02:00
|
|
|
if (!output_file_stream) {
|
|
|
|
delete input_file_stream;
|
|
|
|
LOG(LS_ERROR) << "Not able to open the output audio stream file.";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-11 19:25:07 +01:00
|
|
|
return new FileVoiceChannel(input_file_stream, output_file_stream,
|
|
|
|
rtp_sender_thread_);
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
VideoMediaChannel* FileMediaEngine::CreateVideoChannel(
|
|
|
|
VoiceMediaChannel* voice_ch) {
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::FileStream* input_file_stream = NULL;
|
|
|
|
rtc::FileStream* output_file_stream = NULL;
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
if (video_input_filename_.empty() && video_output_filename_.empty())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!video_input_filename_.empty()) {
|
2014-07-29 19:36:52 +02:00
|
|
|
input_file_stream = rtc::Filesystem::OpenFile(
|
|
|
|
rtc::Pathname(video_input_filename_), "rb");
|
2013-07-10 02:45:36 +02:00
|
|
|
if (!input_file_stream) {
|
|
|
|
LOG(LS_ERROR) << "Not able to open the input video stream file.";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!video_output_filename_.empty()) {
|
2014-07-29 19:36:52 +02:00
|
|
|
output_file_stream = rtc::Filesystem::OpenFile(
|
|
|
|
rtc::Pathname(video_output_filename_), "wb");
|
2013-07-10 02:45:36 +02:00
|
|
|
if (!output_file_stream) {
|
|
|
|
delete input_file_stream;
|
|
|
|
LOG(LS_ERROR) << "Not able to open the output video stream file.";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-11 19:25:07 +01:00
|
|
|
return new FileVideoChannel(input_file_stream, output_file_stream,
|
|
|
|
rtp_sender_thread_);
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Definition of RtpSenderReceiver.
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2014-07-29 19:36:52 +02:00
|
|
|
class RtpSenderReceiver : public rtc::MessageHandler {
|
2013-07-10 02:45:36 +02:00
|
|
|
public:
|
|
|
|
RtpSenderReceiver(MediaChannel* channel,
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::StreamInterface* input_file_stream,
|
|
|
|
rtc::StreamInterface* output_file_stream,
|
|
|
|
rtc::Thread* sender_thread);
|
2013-10-18 18:27:26 +02:00
|
|
|
virtual ~RtpSenderReceiver();
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
// Called by media channel. Context: media channel thread.
|
|
|
|
bool SetSend(bool send);
|
|
|
|
void SetSendSsrc(uint32 ssrc);
|
2014-07-29 19:36:52 +02:00
|
|
|
void OnPacketReceived(rtc::Buffer* packet);
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
// Override virtual method of parent MessageHandler. Context: Worker Thread.
|
2014-07-29 19:36:52 +02:00
|
|
|
virtual void OnMessage(rtc::Message* pmsg);
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Read the next RTP dump packet, whose RTP SSRC is the same as first_ssrc_.
|
|
|
|
// Return true if successful.
|
|
|
|
bool ReadNextPacket(RtpDumpPacket* packet);
|
|
|
|
// Send a RTP packet to the network. The input parameter data points to the
|
|
|
|
// start of the RTP packet and len is the packet size. Return true if the sent
|
|
|
|
// size is equal to len.
|
|
|
|
bool SendRtpPacket(const void* data, size_t len);
|
|
|
|
|
|
|
|
MediaChannel* media_channel_;
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::scoped_ptr<rtc::StreamInterface> input_stream_;
|
|
|
|
rtc::scoped_ptr<rtc::StreamInterface> output_stream_;
|
|
|
|
rtc::scoped_ptr<RtpDumpLoopReader> rtp_dump_reader_;
|
|
|
|
rtc::scoped_ptr<RtpDumpWriter> rtp_dump_writer_;
|
|
|
|
rtc::Thread* sender_thread_;
|
2013-12-11 19:25:07 +01:00
|
|
|
bool own_sender_thread_;
|
2013-07-10 02:45:36 +02:00
|
|
|
// RTP dump packet read from the input stream.
|
|
|
|
RtpDumpPacket rtp_dump_packet_;
|
|
|
|
uint32 start_send_time_;
|
|
|
|
bool sending_;
|
|
|
|
bool first_packet_;
|
|
|
|
uint32 first_ssrc_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(RtpSenderReceiver);
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Implementation of RtpSenderReceiver.
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
RtpSenderReceiver::RtpSenderReceiver(
|
|
|
|
MediaChannel* channel,
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::StreamInterface* input_file_stream,
|
|
|
|
rtc::StreamInterface* output_file_stream,
|
|
|
|
rtc::Thread* sender_thread)
|
2013-07-10 02:45:36 +02:00
|
|
|
: media_channel_(channel),
|
2013-12-11 19:25:07 +01:00
|
|
|
input_stream_(input_file_stream),
|
|
|
|
output_stream_(output_file_stream),
|
2013-07-10 02:45:36 +02:00
|
|
|
sending_(false),
|
|
|
|
first_packet_(true) {
|
2013-12-11 19:25:07 +01:00
|
|
|
if (sender_thread == NULL) {
|
2014-07-29 19:36:52 +02:00
|
|
|
sender_thread_ = new rtc::Thread();
|
2013-12-11 19:25:07 +01:00
|
|
|
own_sender_thread_ = true;
|
|
|
|
} else {
|
|
|
|
sender_thread_ = sender_thread;
|
|
|
|
own_sender_thread_ = false;
|
|
|
|
}
|
|
|
|
|
2013-07-10 02:45:36 +02:00
|
|
|
if (input_stream_) {
|
|
|
|
rtp_dump_reader_.reset(new RtpDumpLoopReader(input_stream_.get()));
|
|
|
|
// Start the sender thread, which reads rtp dump records, waits based on
|
|
|
|
// the record timestamps, and sends the RTP packets to the network.
|
2013-12-11 19:25:07 +01:00
|
|
|
if (own_sender_thread_) {
|
|
|
|
sender_thread_->Start();
|
|
|
|
}
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a rtp dump writer for the output RTP dump stream.
|
|
|
|
if (output_stream_) {
|
|
|
|
rtp_dump_writer_.reset(new RtpDumpWriter(output_stream_.get()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-18 18:27:26 +02:00
|
|
|
RtpSenderReceiver::~RtpSenderReceiver() {
|
2013-12-11 19:25:07 +01:00
|
|
|
if (own_sender_thread_) {
|
|
|
|
sender_thread_->Stop();
|
|
|
|
delete sender_thread_;
|
|
|
|
}
|
2013-10-18 18:27:26 +02:00
|
|
|
}
|
|
|
|
|
2013-07-10 02:45:36 +02:00
|
|
|
bool RtpSenderReceiver::SetSend(bool send) {
|
|
|
|
bool was_sending = sending_;
|
|
|
|
sending_ = send;
|
|
|
|
if (!was_sending && sending_) {
|
2013-12-11 19:25:07 +01:00
|
|
|
sender_thread_->PostDelayed(0, this); // Wake up the send thread.
|
2014-07-29 19:36:52 +02:00
|
|
|
start_send_time_ = rtc::Time();
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RtpSenderReceiver::SetSendSsrc(uint32 ssrc) {
|
|
|
|
if (rtp_dump_reader_) {
|
|
|
|
rtp_dump_reader_->SetSsrc(ssrc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
void RtpSenderReceiver::OnPacketReceived(rtc::Buffer* packet) {
|
2013-07-10 02:45:36 +02:00
|
|
|
if (rtp_dump_writer_) {
|
|
|
|
rtp_dump_writer_->WriteRtpPacket(packet->data(), packet->length());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
void RtpSenderReceiver::OnMessage(rtc::Message* pmsg) {
|
2013-07-10 02:45:36 +02:00
|
|
|
if (!sending_) {
|
|
|
|
// If the sender thread is not sending, ignore this message. The thread goes
|
|
|
|
// to sleep until SetSend(true) wakes it up.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!first_packet_) {
|
|
|
|
// Send the previously read packet.
|
|
|
|
SendRtpPacket(&rtp_dump_packet_.data[0], rtp_dump_packet_.data.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ReadNextPacket(&rtp_dump_packet_)) {
|
2014-07-29 19:36:52 +02:00
|
|
|
int wait = rtc::TimeUntil(
|
2013-07-10 02:45:36 +02:00
|
|
|
start_send_time_ + rtp_dump_packet_.elapsed_time);
|
2014-07-29 19:36:52 +02:00
|
|
|
wait = rtc::_max(0, wait);
|
2013-12-11 19:25:07 +01:00
|
|
|
sender_thread_->PostDelayed(wait, this);
|
2013-07-10 02:45:36 +02:00
|
|
|
} else {
|
2013-12-11 19:25:07 +01:00
|
|
|
sender_thread_->Quit();
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RtpSenderReceiver::ReadNextPacket(RtpDumpPacket* packet) {
|
2014-07-29 19:36:52 +02:00
|
|
|
while (rtc::SR_SUCCESS == rtp_dump_reader_->ReadPacket(packet)) {
|
2013-07-10 02:45:36 +02:00
|
|
|
uint32 ssrc;
|
|
|
|
if (!packet->GetRtpSsrc(&ssrc)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (first_packet_) {
|
|
|
|
first_packet_ = false;
|
|
|
|
first_ssrc_ = ssrc;
|
|
|
|
}
|
|
|
|
if (ssrc == first_ssrc_) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RtpSenderReceiver::SendRtpPacket(const void* data, size_t len) {
|
2013-07-26 21:17:59 +02:00
|
|
|
if (!media_channel_)
|
2013-07-10 02:45:36 +02:00
|
|
|
return false;
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::Buffer packet(data, len, kMaxRtpPacketLen);
|
2013-07-26 21:17:59 +02:00
|
|
|
return media_channel_->SendPacket(&packet);
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Implementation of FileVoiceChannel.
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
FileVoiceChannel::FileVoiceChannel(
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::StreamInterface* input_file_stream,
|
|
|
|
rtc::StreamInterface* output_file_stream,
|
|
|
|
rtc::Thread* rtp_sender_thread)
|
2013-07-10 02:45:36 +02:00
|
|
|
: send_ssrc_(0),
|
|
|
|
rtp_sender_receiver_(new RtpSenderReceiver(this, input_file_stream,
|
2013-12-11 19:25:07 +01:00
|
|
|
output_file_stream,
|
|
|
|
rtp_sender_thread)) {}
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
FileVoiceChannel::~FileVoiceChannel() {}
|
|
|
|
|
|
|
|
bool FileVoiceChannel::SetSendCodecs(const std::vector<AudioCodec>& codecs) {
|
|
|
|
// TODO(whyuan): Check the format of RTP dump input.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileVoiceChannel::SetSend(SendFlags flag) {
|
|
|
|
return rtp_sender_receiver_->SetSend(flag != SEND_NOTHING);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileVoiceChannel::AddSendStream(const StreamParams& sp) {
|
|
|
|
if (send_ssrc_ != 0 || sp.ssrcs.size() != 1) {
|
|
|
|
LOG(LS_ERROR) << "FileVoiceChannel only supports one send stream.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
send_ssrc_ = sp.ssrcs[0];
|
|
|
|
rtp_sender_receiver_->SetSendSsrc(send_ssrc_);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileVoiceChannel::RemoveSendStream(uint32 ssrc) {
|
|
|
|
if (ssrc != send_ssrc_)
|
|
|
|
return false;
|
|
|
|
send_ssrc_ = 0;
|
|
|
|
rtp_sender_receiver_->SetSendSsrc(send_ssrc_);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-13 01:21:03 +01:00
|
|
|
void FileVoiceChannel::OnPacketReceived(
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
|
2013-07-10 02:45:36 +02:00
|
|
|
rtp_sender_receiver_->OnPacketReceived(packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Implementation of FileVideoChannel.
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
FileVideoChannel::FileVideoChannel(
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::StreamInterface* input_file_stream,
|
|
|
|
rtc::StreamInterface* output_file_stream,
|
|
|
|
rtc::Thread* rtp_sender_thread)
|
2013-07-10 02:45:36 +02:00
|
|
|
: send_ssrc_(0),
|
|
|
|
rtp_sender_receiver_(new RtpSenderReceiver(this, input_file_stream,
|
2013-12-11 19:25:07 +01:00
|
|
|
output_file_stream,
|
|
|
|
rtp_sender_thread)) {}
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
FileVideoChannel::~FileVideoChannel() {}
|
|
|
|
|
|
|
|
bool FileVideoChannel::SetSendCodecs(const std::vector<VideoCodec>& codecs) {
|
|
|
|
// TODO(whyuan): Check the format of RTP dump input.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileVideoChannel::SetSend(bool send) {
|
|
|
|
return rtp_sender_receiver_->SetSend(send);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileVideoChannel::AddSendStream(const StreamParams& sp) {
|
|
|
|
if (send_ssrc_ != 0 || sp.ssrcs.size() != 1) {
|
|
|
|
LOG(LS_ERROR) << "FileVideoChannel only support one send stream.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
send_ssrc_ = sp.ssrcs[0];
|
|
|
|
rtp_sender_receiver_->SetSendSsrc(send_ssrc_);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileVideoChannel::RemoveSendStream(uint32 ssrc) {
|
|
|
|
if (ssrc != send_ssrc_)
|
|
|
|
return false;
|
|
|
|
send_ssrc_ = 0;
|
|
|
|
rtp_sender_receiver_->SetSendSsrc(send_ssrc_);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-13 01:21:03 +01:00
|
|
|
void FileVideoChannel::OnPacketReceived(
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
|
2013-07-10 02:45:36 +02:00
|
|
|
rtp_sender_receiver_->OnPacketReceived(packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace cricket
|