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.

424 lines
14 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2010 Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/media/base/rtpdump.h"
  28. #include <ctype.h>
  29. #include <string>
  30. #include "talk/base/byteorder.h"
  31. #include "talk/base/logging.h"
  32. #include "talk/base/timeutils.h"
  33. #include "talk/media/base/rtputils.h"
  34. namespace {
  35. static const int kRtpSsrcOffset = 8;
  36. const int kWarnSlowWritesDelayMs = 50;
  37. } // namespace
  38. namespace cricket {
  39. const char RtpDumpFileHeader::kFirstLine[] = "#!rtpplay1.0 0.0.0.0/0\n";
  40. RtpDumpFileHeader::RtpDumpFileHeader(uint32 start_ms, uint32 s, uint16 p)
  41. : start_sec(start_ms / 1000),
  42. start_usec(start_ms % 1000 * 1000),
  43. source(s),
  44. port(p),
  45. padding(0) {
  46. }
  47. void RtpDumpFileHeader::WriteToByteBuffer(talk_base::ByteBuffer* buf) {
  48. buf->WriteUInt32(start_sec);
  49. buf->WriteUInt32(start_usec);
  50. buf->WriteUInt32(source);
  51. buf->WriteUInt16(port);
  52. buf->WriteUInt16(padding);
  53. }
  54. static const uint32 kDefaultTimeIncrease = 30;
  55. bool RtpDumpPacket::IsValidRtpPacket() const {
  56. return original_data_len >= data.size() &&
  57. data.size() >= kMinRtpPacketLen;
  58. }
  59. bool RtpDumpPacket::IsValidRtcpPacket() const {
  60. return original_data_len == 0 &&
  61. data.size() >= kMinRtcpPacketLen;
  62. }
  63. bool RtpDumpPacket::GetRtpPayloadType(int* pt) const {
  64. return IsValidRtpPacket() &&
  65. cricket::GetRtpPayloadType(&data[0], data.size(), pt);
  66. }
  67. bool RtpDumpPacket::GetRtpSeqNum(int* seq_num) const {
  68. return IsValidRtpPacket() &&
  69. cricket::GetRtpSeqNum(&data[0], data.size(), seq_num);
  70. }
  71. bool RtpDumpPacket::GetRtpTimestamp(uint32* ts) const {
  72. return IsValidRtpPacket() &&
  73. cricket::GetRtpTimestamp(&data[0], data.size(), ts);
  74. }
  75. bool RtpDumpPacket::GetRtpSsrc(uint32* ssrc) const {
  76. return IsValidRtpPacket() &&
  77. cricket::GetRtpSsrc(&data[0], data.size(), ssrc);
  78. }
  79. bool RtpDumpPacket::GetRtpHeaderLen(size_t* len) const {
  80. return IsValidRtpPacket() &&
  81. cricket::GetRtpHeaderLen(&data[0], data.size(), len);
  82. }
  83. bool RtpDumpPacket::GetRtcpType(int* type) const {
  84. return IsValidRtcpPacket() &&
  85. cricket::GetRtcpType(&data[0], data.size(), type);
  86. }
  87. ///////////////////////////////////////////////////////////////////////////
  88. // Implementation of RtpDumpReader.
  89. ///////////////////////////////////////////////////////////////////////////
  90. void RtpDumpReader::SetSsrc(uint32 ssrc) {
  91. ssrc_override_ = ssrc;
  92. }
  93. talk_base::StreamResult RtpDumpReader::ReadPacket(RtpDumpPacket* packet) {
  94. if (!packet) return talk_base::SR_ERROR;
  95. talk_base::StreamResult res = talk_base::SR_SUCCESS;
  96. // Read the file header if it has not been read yet.
  97. if (!file_header_read_) {
  98. res = ReadFileHeader();
  99. if (res != talk_base::SR_SUCCESS) {
  100. return res;
  101. }
  102. file_header_read_ = true;
  103. }
  104. // Read the RTP dump packet header.
  105. char header[RtpDumpPacket::kHeaderLength];
  106. res = stream_->ReadAll(header, sizeof(header), NULL, NULL);
  107. if (res != talk_base::SR_SUCCESS) {
  108. return res;
  109. }
  110. talk_base::ByteBuffer buf(header, sizeof(header));
  111. uint16 dump_packet_len;
  112. uint16 data_len;
  113. // Read the full length of the rtpdump packet, including the rtpdump header.
  114. buf.ReadUInt16(&dump_packet_len);
  115. packet->data.resize(dump_packet_len - sizeof(header));
  116. // Read the size of the original packet, which may be larger than the size in
  117. // the rtpdump file, in the event that only part of the packet (perhaps just
  118. // the header) was recorded. Note that this field is set to zero for RTCP
  119. // packets, which have their own internal length field.
  120. buf.ReadUInt16(&data_len);
  121. packet->original_data_len = data_len;
  122. // Read the elapsed time for this packet (different than RTP timestamp).
  123. buf.ReadUInt32(&packet->elapsed_time);
  124. // Read the actual RTP or RTCP packet.
  125. res = stream_->ReadAll(&packet->data[0], packet->data.size(), NULL, NULL);
  126. // If the packet is RTP and we have specified a ssrc, replace the RTP ssrc
  127. // with the specified ssrc.
  128. if (res == talk_base::SR_SUCCESS &&
  129. packet->IsValidRtpPacket() &&
  130. ssrc_override_ != 0) {
  131. talk_base::SetBE32(&packet->data[kRtpSsrcOffset], ssrc_override_);
  132. }
  133. return res;
  134. }
  135. talk_base::StreamResult RtpDumpReader::ReadFileHeader() {
  136. // Read the first line.
  137. std::string first_line;
  138. talk_base::StreamResult res = stream_->ReadLine(&first_line);
  139. if (res != talk_base::SR_SUCCESS) {
  140. return res;
  141. }
  142. if (!CheckFirstLine(first_line)) {
  143. return talk_base::SR_ERROR;
  144. }
  145. // Read the 16 byte file header.
  146. char header[RtpDumpFileHeader::kHeaderLength];
  147. res = stream_->ReadAll(header, sizeof(header), NULL, NULL);
  148. if (res == talk_base::SR_SUCCESS) {
  149. talk_base::ByteBuffer buf(header, sizeof(header));
  150. uint32 start_sec;
  151. uint32 start_usec;
  152. buf.ReadUInt32(&start_sec);
  153. buf.ReadUInt32(&start_usec);
  154. start_time_ms_ = start_sec * 1000 + start_usec / 1000;
  155. // Increase the length by 1 since first_line does not contain the ending \n.
  156. first_line_and_file_header_len_ = first_line.size() + 1 + sizeof(header);
  157. }
  158. return res;
  159. }
  160. bool RtpDumpReader::CheckFirstLine(const std::string& first_line) {
  161. // The first line is like "#!rtpplay1.0 address/port"
  162. bool matched = (0 == first_line.find("#!rtpplay1.0 "));
  163. // The address could be IP or hostname. We do not check it here. Instead, we
  164. // check the port at the end.
  165. size_t pos = first_line.find('/');
  166. matched &= (pos != std::string::npos && pos < first_line.size() - 1);
  167. for (++pos; pos < first_line.size() && matched; ++pos) {
  168. matched &= (0 != isdigit(first_line[pos]));
  169. }
  170. return matched;
  171. }
  172. ///////////////////////////////////////////////////////////////////////////
  173. // Implementation of RtpDumpLoopReader.
  174. ///////////////////////////////////////////////////////////////////////////
  175. RtpDumpLoopReader::RtpDumpLoopReader(talk_base::StreamInterface* stream)
  176. : RtpDumpReader(stream),
  177. loop_count_(0),
  178. elapsed_time_increases_(0),
  179. rtp_seq_num_increase_(0),
  180. rtp_timestamp_increase_(0),
  181. packet_count_(0),
  182. frame_count_(0),
  183. first_elapsed_time_(0),
  184. first_rtp_seq_num_(0),
  185. first_rtp_timestamp_(0),
  186. prev_elapsed_time_(0),
  187. prev_rtp_seq_num_(0),
  188. prev_rtp_timestamp_(0) {
  189. }
  190. talk_base::StreamResult RtpDumpLoopReader::ReadPacket(RtpDumpPacket* packet) {
  191. if (!packet) return talk_base::SR_ERROR;
  192. talk_base::StreamResult res = RtpDumpReader::ReadPacket(packet);
  193. if (talk_base::SR_SUCCESS == res) {
  194. if (0 == loop_count_) {
  195. // During the first loop, we update the statistics of the input stream.
  196. UpdateStreamStatistics(*packet);
  197. }
  198. } else if (talk_base::SR_EOS == res) {
  199. if (0 == loop_count_) {
  200. // At the end of the first loop, calculate elapsed_time_increases_,
  201. // rtp_seq_num_increase_, and rtp_timestamp_increase_, which will be
  202. // used during the second and later loops.
  203. CalculateIncreases();
  204. }
  205. // Rewind the input stream to the first dump packet and read again.
  206. ++loop_count_;
  207. if (RewindToFirstDumpPacket()) {
  208. res = RtpDumpReader::ReadPacket(packet);
  209. }
  210. }
  211. if (talk_base::SR_SUCCESS == res && loop_count_ > 0) {
  212. // During the second and later loops, we update the elapsed time of the dump
  213. // packet. If the dumped packet is a RTP packet, we also update its RTP
  214. // sequence number and timestamp.
  215. UpdateDumpPacket(packet);
  216. }
  217. return res;
  218. }
  219. void RtpDumpLoopReader::UpdateStreamStatistics(const RtpDumpPacket& packet) {
  220. // Get the RTP sequence number and timestamp of the dump packet.
  221. int rtp_seq_num = 0;
  222. packet.GetRtpSeqNum(&rtp_seq_num);
  223. uint32 rtp_timestamp = 0;
  224. packet.GetRtpTimestamp(&rtp_timestamp);
  225. // Set the timestamps and sequence number for the first dump packet.
  226. if (0 == packet_count_++) {
  227. first_elapsed_time_ = packet.elapsed_time;
  228. first_rtp_seq_num_ = rtp_seq_num;
  229. first_rtp_timestamp_ = rtp_timestamp;
  230. // The first packet belongs to a new payload frame.
  231. ++frame_count_;
  232. } else if (rtp_timestamp != prev_rtp_timestamp_) {
  233. // The current and previous packets belong to different payload frames.
  234. ++frame_count_;
  235. }
  236. prev_elapsed_time_ = packet.elapsed_time;
  237. prev_rtp_timestamp_ = rtp_timestamp;
  238. prev_rtp_seq_num_ = rtp_seq_num;
  239. }
  240. void RtpDumpLoopReader::CalculateIncreases() {
  241. // At this time, prev_elapsed_time_, prev_rtp_seq_num_, and
  242. // prev_rtp_timestamp_ are values of the last dump packet in the input stream.
  243. rtp_seq_num_increase_ = prev_rtp_seq_num_ - first_rtp_seq_num_ + 1;
  244. // If we have only one packet or frame, we use the default timestamp
  245. // increase. Otherwise, we use the difference between the first and the last
  246. // packets or frames.
  247. elapsed_time_increases_ = packet_count_ <= 1 ? kDefaultTimeIncrease :
  248. (prev_elapsed_time_ - first_elapsed_time_) * packet_count_ /
  249. (packet_count_ - 1);
  250. rtp_timestamp_increase_ = frame_count_ <= 1 ? kDefaultTimeIncrease :
  251. (prev_rtp_timestamp_ - first_rtp_timestamp_) * frame_count_ /
  252. (frame_count_ - 1);
  253. }
  254. void RtpDumpLoopReader::UpdateDumpPacket(RtpDumpPacket* packet) {
  255. // Increase the elapsed time of the dump packet.
  256. packet->elapsed_time += loop_count_ * elapsed_time_increases_;
  257. if (packet->IsValidRtpPacket()) {
  258. // Get the old RTP sequence number and timestamp.
  259. int sequence = 0;
  260. packet->GetRtpSeqNum(&sequence);
  261. uint32 timestamp = 0;
  262. packet->GetRtpTimestamp(&timestamp);
  263. // Increase the RTP sequence number and timestamp.
  264. sequence += loop_count_ * rtp_seq_num_increase_;
  265. timestamp += loop_count_ * rtp_timestamp_increase_;
  266. // Write the updated sequence number and timestamp back to the RTP packet.
  267. talk_base::ByteBuffer buffer;
  268. buffer.WriteUInt16(sequence);
  269. buffer.WriteUInt32(timestamp);
  270. memcpy(&packet->data[2], buffer.Data(), buffer.Length());
  271. }
  272. }
  273. ///////////////////////////////////////////////////////////////////////////
  274. // Implementation of RtpDumpWriter.
  275. ///////////////////////////////////////////////////////////////////////////
  276. RtpDumpWriter::RtpDumpWriter(talk_base::StreamInterface* stream)
  277. : stream_(stream),
  278. packet_filter_(PF_ALL),
  279. file_header_written_(false),
  280. start_time_ms_(talk_base::Time()),
  281. warn_slow_writes_delay_(kWarnSlowWritesDelayMs) {
  282. }
  283. void RtpDumpWriter::set_packet_filter(int filter) {
  284. packet_filter_ = filter;
  285. LOG(LS_INFO) << "RtpDumpWriter set_packet_filter to " << packet_filter_;
  286. }
  287. uint32 RtpDumpWriter::GetElapsedTime() const {
  288. return talk_base::TimeSince(start_time_ms_);
  289. }
  290. talk_base::StreamResult RtpDumpWriter::WriteFileHeader() {
  291. talk_base::StreamResult res = WriteToStream(
  292. RtpDumpFileHeader::kFirstLine,
  293. strlen(RtpDumpFileHeader::kFirstLine));
  294. if (res != talk_base::SR_SUCCESS) {
  295. return res;
  296. }
  297. talk_base::ByteBuffer buf;
  298. RtpDumpFileHeader file_header(talk_base::Time(), 0, 0);
  299. file_header.WriteToByteBuffer(&buf);
  300. return WriteToStream(buf.Data(), buf.Length());
  301. }
  302. talk_base::StreamResult RtpDumpWriter::WritePacket(
  303. const void* data, size_t data_len, uint32 elapsed, bool rtcp) {
  304. if (!stream_ || !data || 0 == data_len) return talk_base::SR_ERROR;
  305. talk_base::StreamResult res = talk_base::SR_SUCCESS;
  306. // Write the file header if it has not been written yet.
  307. if (!file_header_written_) {
  308. res = WriteFileHeader();
  309. if (res != talk_base::SR_SUCCESS) {
  310. return res;
  311. }
  312. file_header_written_ = true;
  313. }
  314. // Figure out what to write.
  315. size_t write_len = FilterPacket(data, data_len, rtcp);
  316. if (write_len == 0) {
  317. return talk_base::SR_SUCCESS;
  318. }
  319. // Write the dump packet header.
  320. talk_base::ByteBuffer buf;
  321. buf.WriteUInt16(static_cast<uint16>(
  322. RtpDumpPacket::kHeaderLength + write_len));
  323. buf.WriteUInt16(static_cast<uint16>(rtcp ? 0 : data_len));
  324. buf.WriteUInt32(elapsed);
  325. res = WriteToStream(buf.Data(), buf.Length());
  326. if (res != talk_base::SR_SUCCESS) {
  327. return res;
  328. }
  329. // Write the header or full packet as indicated by write_len.
  330. return WriteToStream(data, write_len);
  331. }
  332. size_t RtpDumpWriter::FilterPacket(const void* data, size_t data_len,
  333. bool rtcp) {
  334. size_t filtered_len = 0;
  335. if (!rtcp) {
  336. if ((packet_filter_ & PF_RTPPACKET) == PF_RTPPACKET) {
  337. // RTP header + payload
  338. filtered_len = data_len;
  339. } else if ((packet_filter_ & PF_RTPHEADER) == PF_RTPHEADER) {
  340. // RTP header only
  341. size_t header_len;
  342. if (GetRtpHeaderLen(data, data_len, &header_len)) {
  343. filtered_len = header_len;
  344. }
  345. }
  346. } else {
  347. if ((packet_filter_ & PF_RTCPPACKET) == PF_RTCPPACKET) {
  348. // RTCP header + payload
  349. filtered_len = data_len;
  350. }
  351. }
  352. return filtered_len;
  353. }
  354. talk_base::StreamResult RtpDumpWriter::WriteToStream(
  355. const void* data, size_t data_len) {
  356. uint32 before = talk_base::Time();
  357. talk_base::StreamResult result =
  358. stream_->WriteAll(data, data_len, NULL, NULL);
  359. uint32 delay = talk_base::TimeSince(before);
  360. if (delay >= warn_slow_writes_delay_) {
  361. LOG(LS_WARNING) << "Slow RtpDump: took " << delay << "ms to write "
  362. << data_len << " bytes.";
  363. }
  364. return result;
  365. }
  366. } // namespace cricket