You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

342 lines
11 KiB

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