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