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.

232 lines
8.7 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. #ifndef TALK_MEDIA_BASE_RTPDUMP_H_
  28. #define TALK_MEDIA_BASE_RTPDUMP_H_
  29. #include <cstring>
  30. #include <string>
  31. #include <vector>
  32. #include "talk/base/basictypes.h"
  33. #include "talk/base/bytebuffer.h"
  34. #include "talk/base/stream.h"
  35. namespace cricket {
  36. // We use the RTP dump file format compatible to the format used by rtptools
  37. // (http://www.cs.columbia.edu/irt/software/rtptools/) and Wireshark
  38. // (http://wiki.wireshark.org/rtpdump). In particular, the file starts with the
  39. // first line "#!rtpplay1.0 address/port\n", followed by a 16 byte file header.
  40. // For each packet, the file contains a 8 byte dump packet header, followed by
  41. // the actual RTP or RTCP packet.
  42. enum RtpDumpPacketFilter {
  43. PF_NONE = 0x0,
  44. PF_RTPHEADER = 0x1,
  45. PF_RTPPACKET = 0x3, // includes header
  46. // PF_RTCPHEADER = 0x4, // TODO(juberti)
  47. PF_RTCPPACKET = 0xC, // includes header
  48. PF_ALL = 0xF
  49. };
  50. struct RtpDumpFileHeader {
  51. RtpDumpFileHeader(uint32 start_ms, uint32 s, uint16 p);
  52. void WriteToByteBuffer(talk_base::ByteBuffer* buf);
  53. static const char kFirstLine[];
  54. static const size_t kHeaderLength = 16;
  55. uint32 start_sec; // start of recording, the seconds part.
  56. uint32 start_usec; // start of recording, the microseconds part.
  57. uint32 source; // network source (multicast address).
  58. uint16 port; // UDP port.
  59. uint16 padding; // 2 bytes padding.
  60. };
  61. struct RtpDumpPacket {
  62. RtpDumpPacket() {}
  63. RtpDumpPacket(const void* d, size_t s, uint32 elapsed, bool rtcp)
  64. : elapsed_time(elapsed),
  65. original_data_len((rtcp) ? 0 : s) {
  66. data.resize(s);
  67. memcpy(&data[0], d, s);
  68. }
  69. // In the rtpdump file format, RTCP packets have their data len set to zero,
  70. // since RTCP has an internal length field.
  71. bool is_rtcp() const { return original_data_len == 0; }
  72. bool IsValidRtpPacket() const;
  73. bool IsValidRtcpPacket() const;
  74. // Get the payload type, sequence number, timestampe, and SSRC of the RTP
  75. // packet. Return true and set the output parameter if successful.
  76. bool GetRtpPayloadType(int* pt) const;
  77. bool GetRtpSeqNum(int* seq_num) const;
  78. bool GetRtpTimestamp(uint32* ts) const;
  79. bool GetRtpSsrc(uint32* ssrc) const;
  80. bool GetRtpHeaderLen(size_t* len) const;
  81. // Get the type of the RTCP packet. Return true and set the output parameter
  82. // if successful.
  83. bool GetRtcpType(int* type) const;
  84. static const size_t kHeaderLength = 8;
  85. uint32 elapsed_time; // Milliseconds since the start of recording.
  86. std::vector<uint8> data; // The actual RTP or RTCP packet.
  87. size_t original_data_len; // The original length of the packet; may be
  88. // greater than data.size() if only part of the
  89. // packet was recorded.
  90. };
  91. class RtpDumpReader {
  92. public:
  93. explicit RtpDumpReader(talk_base::StreamInterface* stream)
  94. : stream_(stream),
  95. file_header_read_(false),
  96. first_line_and_file_header_len_(0),
  97. start_time_ms_(0),
  98. ssrc_override_(0) {
  99. }
  100. virtual ~RtpDumpReader() {}
  101. // Use the specified ssrc, rather than the ssrc from dump, for RTP packets.
  102. void SetSsrc(uint32 ssrc);
  103. virtual talk_base::StreamResult ReadPacket(RtpDumpPacket* packet);
  104. protected:
  105. talk_base::StreamResult ReadFileHeader();
  106. bool RewindToFirstDumpPacket() {
  107. return stream_->SetPosition(first_line_and_file_header_len_);
  108. }
  109. private:
  110. // Check if its matches "#!rtpplay1.0 address/port\n".
  111. bool CheckFirstLine(const std::string& first_line);
  112. talk_base::StreamInterface* stream_;
  113. bool file_header_read_;
  114. size_t first_line_and_file_header_len_;
  115. uint32 start_time_ms_;
  116. uint32 ssrc_override_;
  117. DISALLOW_COPY_AND_ASSIGN(RtpDumpReader);
  118. };
  119. // RtpDumpLoopReader reads RTP dump packets from the input stream and rewinds
  120. // the stream when it ends. RtpDumpLoopReader maintains the elapsed time, the
  121. // RTP sequence number and the RTP timestamp properly. RtpDumpLoopReader can
  122. // handle both RTP dump and RTCP dump. We assume that the dump does not mix
  123. // RTP packets and RTCP packets.
  124. class RtpDumpLoopReader : public RtpDumpReader {
  125. public:
  126. explicit RtpDumpLoopReader(talk_base::StreamInterface* stream);
  127. virtual talk_base::StreamResult ReadPacket(RtpDumpPacket* packet);
  128. private:
  129. // During the first loop, update the statistics, including packet count, frame
  130. // count, timestamps, and sequence number, of the input stream.
  131. void UpdateStreamStatistics(const RtpDumpPacket& packet);
  132. // At the end of first loop, calculate elapsed_time_increases_,
  133. // rtp_seq_num_increase_, and rtp_timestamp_increase_.
  134. void CalculateIncreases();
  135. // During the second and later loops, update the elapsed time of the dump
  136. // packet. If the dumped packet is a RTP packet, update its RTP sequence
  137. // number and timestamp as well.
  138. void UpdateDumpPacket(RtpDumpPacket* packet);
  139. int loop_count_;
  140. // How much to increase the elapsed time, RTP sequence number, RTP timestampe
  141. // for each loop. They are calcualted with the variables below during the
  142. // first loop.
  143. uint32 elapsed_time_increases_;
  144. int rtp_seq_num_increase_;
  145. uint32 rtp_timestamp_increase_;
  146. // How many RTP packets and how many payload frames in the input stream. RTP
  147. // packets belong to the same frame have the same RTP timestamp, different
  148. // dump timestamp, and different RTP sequence number.
  149. uint32 packet_count_;
  150. uint32 frame_count_;
  151. // The elapsed time, RTP sequence number, and RTP timestamp of the first and
  152. // the previous dump packets in the input stream.
  153. uint32 first_elapsed_time_;
  154. int first_rtp_seq_num_;
  155. uint32 first_rtp_timestamp_;
  156. uint32 prev_elapsed_time_;
  157. int prev_rtp_seq_num_;
  158. uint32 prev_rtp_timestamp_;
  159. DISALLOW_COPY_AND_ASSIGN(RtpDumpLoopReader);
  160. };
  161. class RtpDumpWriter {
  162. public:
  163. explicit RtpDumpWriter(talk_base::StreamInterface* stream);
  164. // Filter to control what packets we actually record.
  165. void set_packet_filter(int filter);
  166. // Write a RTP or RTCP packet. The parameters data points to the packet and
  167. // data_len is its length.
  168. talk_base::StreamResult WriteRtpPacket(const void* data, size_t data_len) {
  169. return WritePacket(data, data_len, GetElapsedTime(), false);
  170. }
  171. talk_base::StreamResult WriteRtcpPacket(const void* data, size_t data_len) {
  172. return WritePacket(data, data_len, GetElapsedTime(), true);
  173. }
  174. talk_base::StreamResult WritePacket(const RtpDumpPacket& packet) {
  175. return WritePacket(&packet.data[0], packet.data.size(), packet.elapsed_time,
  176. packet.is_rtcp());
  177. }
  178. uint32 GetElapsedTime() const;
  179. bool GetDumpSize(size_t* size) {
  180. // Note that we use GetPosition(), rather than GetSize(), to avoid flush the
  181. // stream per write.
  182. return stream_ && size && stream_->GetPosition(size);
  183. }
  184. protected:
  185. talk_base::StreamResult WriteFileHeader();
  186. private:
  187. talk_base::StreamResult WritePacket(const void* data, size_t data_len,
  188. uint32 elapsed, bool rtcp);
  189. size_t FilterPacket(const void* data, size_t data_len, bool rtcp);
  190. talk_base::StreamResult WriteToStream(const void* data, size_t data_len);
  191. talk_base::StreamInterface* stream_;
  192. int packet_filter_;
  193. bool file_header_written_;
  194. uint32 start_time_ms_; // Time when the record starts.
  195. // If writing to the stream takes longer than this many ms, log a warning.
  196. uint32 warn_slow_writes_delay_;
  197. DISALLOW_COPY_AND_ASSIGN(RtpDumpWriter);
  198. };
  199. } // namespace cricket
  200. #endif // TALK_MEDIA_BASE_RTPDUMP_H_