1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
// 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"
#include <limits.h>
#include "talk/base/buffer.h"
#include "talk/base/event.h"
#include "talk/base/logging.h"
#include "talk/base/pathutils.h"
#include "talk/base/stream.h"
#include "talk/media/base/rtpdump.h"
#include "talk/media/base/rtputils.h"
#include "talk/media/base/streamparams.h"
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() {
talk_base::FileStream* input_file_stream = NULL;
talk_base::FileStream* output_file_stream = NULL;
if (voice_input_filename_.empty() && voice_output_filename_.empty())
return NULL;
if (!voice_input_filename_.empty()) {
input_file_stream = talk_base::Filesystem::OpenFile(
talk_base::Pathname(voice_input_filename_), "rb");
if (!input_file_stream) {
LOG(LS_ERROR) << "Not able to open the input audio stream file.";
return NULL;
}
}
if (!voice_output_filename_.empty()) {
output_file_stream = talk_base::Filesystem::OpenFile(
talk_base::Pathname(voice_output_filename_), "wb");
if (!output_file_stream) {
delete input_file_stream;
LOG(LS_ERROR) << "Not able to open the output audio stream file.";
return NULL;
}
}
return new FileVoiceChannel(input_file_stream, output_file_stream,
rtp_sender_thread_);
}
VideoMediaChannel* FileMediaEngine::CreateVideoChannel(
VoiceMediaChannel* voice_ch) {
talk_base::FileStream* input_file_stream = NULL;
talk_base::FileStream* output_file_stream = NULL;
if (video_input_filename_.empty() && video_output_filename_.empty())
return NULL;
if (!video_input_filename_.empty()) {
input_file_stream = talk_base::Filesystem::OpenFile(
talk_base::Pathname(video_input_filename_), "rb");
if (!input_file_stream) {
LOG(LS_ERROR) << "Not able to open the input video stream file.";
return NULL;
}
}
if (!video_output_filename_.empty()) {
output_file_stream = talk_base::Filesystem::OpenFile(
talk_base::Pathname(video_output_filename_), "wb");
if (!output_file_stream) {
delete input_file_stream;
LOG(LS_ERROR) << "Not able to open the output video stream file.";
return NULL;
}
}
return new FileVideoChannel(input_file_stream, output_file_stream,
rtp_sender_thread_);
}
///////////////////////////////////////////////////////////////////////////
// Definition of RtpSenderReceiver.
///////////////////////////////////////////////////////////////////////////
class RtpSenderReceiver : public talk_base::MessageHandler {
public:
RtpSenderReceiver(MediaChannel* channel,
talk_base::StreamInterface* input_file_stream,
talk_base::StreamInterface* output_file_stream,
talk_base::Thread* sender_thread);
virtual ~RtpSenderReceiver();
// Called by media channel. Context: media channel thread.
bool SetSend(bool send);
void SetSendSsrc(uint32 ssrc);
void OnPacketReceived(talk_base::Buffer* packet);
// Override virtual method of parent MessageHandler. Context: Worker Thread.
virtual void OnMessage(talk_base::Message* pmsg);
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_;
talk_base::scoped_ptr<talk_base::StreamInterface> input_stream_;
talk_base::scoped_ptr<talk_base::StreamInterface> output_stream_;
talk_base::scoped_ptr<RtpDumpLoopReader> rtp_dump_reader_;
talk_base::scoped_ptr<RtpDumpWriter> rtp_dump_writer_;
talk_base::Thread* sender_thread_;
bool own_sender_thread_;
// 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,
talk_base::StreamInterface* input_file_stream,
talk_base::StreamInterface* output_file_stream,
talk_base::Thread* sender_thread)
: media_channel_(channel),
input_stream_(input_file_stream),
output_stream_(output_file_stream),
sending_(false),
first_packet_(true) {
if (sender_thread == NULL) {
sender_thread_ = new talk_base::Thread();
own_sender_thread_ = true;
} else {
sender_thread_ = sender_thread;
own_sender_thread_ = false;
}
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.
if (own_sender_thread_) {
sender_thread_->Start();
}
}
// Create a rtp dump writer for the output RTP dump stream.
if (output_stream_) {
rtp_dump_writer_.reset(new RtpDumpWriter(output_stream_.get()));
}
}
RtpSenderReceiver::~RtpSenderReceiver() {
if (own_sender_thread_) {
sender_thread_->Stop();
delete sender_thread_;
}
}
bool RtpSenderReceiver::SetSend(bool send) {
bool was_sending = sending_;
sending_ = send;
if (!was_sending && sending_) {
sender_thread_->PostDelayed(0, this); // Wake up the send thread.
start_send_time_ = talk_base::Time();
}
return true;
}
void RtpSenderReceiver::SetSendSsrc(uint32 ssrc) {
if (rtp_dump_reader_) {
rtp_dump_reader_->SetSsrc(ssrc);
}
}
void RtpSenderReceiver::OnPacketReceived(talk_base::Buffer* packet) {
if (rtp_dump_writer_) {
rtp_dump_writer_->WriteRtpPacket(packet->data(), packet->length());
}
}
void RtpSenderReceiver::OnMessage(talk_base::Message* pmsg) {
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_)) {
int wait = talk_base::TimeUntil(
start_send_time_ + rtp_dump_packet_.elapsed_time);
wait = talk_base::_max(0, wait);
sender_thread_->PostDelayed(wait, this);
} else {
sender_thread_->Quit();
}
}
bool RtpSenderReceiver::ReadNextPacket(RtpDumpPacket* packet) {
while (talk_base::SR_SUCCESS == rtp_dump_reader_->ReadPacket(packet)) {
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) {
if (!media_channel_)
return false;
talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
return media_channel_->SendPacket(&packet);
}
///////////////////////////////////////////////////////////////////////////
// Implementation of FileVoiceChannel.
///////////////////////////////////////////////////////////////////////////
FileVoiceChannel::FileVoiceChannel(
talk_base::StreamInterface* input_file_stream,
talk_base::StreamInterface* output_file_stream,
talk_base::Thread* rtp_sender_thread)
: send_ssrc_(0),
rtp_sender_receiver_(new RtpSenderReceiver(this, input_file_stream,
output_file_stream,
rtp_sender_thread)) {}
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;
}
void FileVoiceChannel::OnPacketReceived(
talk_base::Buffer* packet, const talk_base::PacketTime& packet_time) {
rtp_sender_receiver_->OnPacketReceived(packet);
}
///////////////////////////////////////////////////////////////////////////
// Implementation of FileVideoChannel.
///////////////////////////////////////////////////////////////////////////
FileVideoChannel::FileVideoChannel(
talk_base::StreamInterface* input_file_stream,
talk_base::StreamInterface* output_file_stream,
talk_base::Thread* rtp_sender_thread)
: send_ssrc_(0),
rtp_sender_receiver_(new RtpSenderReceiver(this, input_file_stream,
output_file_stream,
rtp_sender_thread)) {}
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;
}
void FileVideoChannel::OnPacketReceived(
talk_base::Buffer* packet, const talk_base::PacketTime& packet_time) {
rtp_sender_receiver_->OnPacketReceived(packet);
}
} // namespace cricket
|