blob: 4ce479277afd193beabb32755595d71652f19977 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
kjellandera96e2d72016-02-04 23:52:28 -080011#ifndef WEBRTC_MEDIA_BASE_RTPDUMP_H_
12#define WEBRTC_MEDIA_BASE_RTPDUMP_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
pbos@webrtc.org371243d2014-03-07 15:22:04 +000014#include <string.h>
15
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016#include <string>
17#include <vector>
18
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000019#include "webrtc/base/basictypes.h"
20#include "webrtc/base/bytebuffer.h"
21#include "webrtc/base/stream.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022
23namespace cricket {
24
25// We use the RTP dump file format compatible to the format used by rtptools
26// (http://www.cs.columbia.edu/irt/software/rtptools/) and Wireshark
27// (http://wiki.wireshark.org/rtpdump). In particular, the file starts with the
28// first line "#!rtpplay1.0 address/port\n", followed by a 16 byte file header.
29// For each packet, the file contains a 8 byte dump packet header, followed by
30// the actual RTP or RTCP packet.
31
32enum RtpDumpPacketFilter {
33 PF_NONE = 0x0,
34 PF_RTPHEADER = 0x1,
35 PF_RTPPACKET = 0x3, // includes header
36 // PF_RTCPHEADER = 0x4, // TODO(juberti)
37 PF_RTCPPACKET = 0xC, // includes header
38 PF_ALL = 0xF
39};
40
41struct RtpDumpFileHeader {
Peter Boström0c4e06b2015-10-07 12:23:21 +020042 RtpDumpFileHeader(uint32_t start_ms, uint32_t s, uint16_t p);
jbauchf1f87202016-03-30 06:43:37 -070043 void WriteToByteBuffer(rtc::ByteBufferWriter* buf);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
45 static const char kFirstLine[];
46 static const size_t kHeaderLength = 16;
Peter Boström0c4e06b2015-10-07 12:23:21 +020047 uint32_t start_sec; // start of recording, the seconds part.
48 uint32_t start_usec; // start of recording, the microseconds part.
49 uint32_t source; // network source (multicast address).
50 uint16_t port; // UDP port.
51 uint16_t padding; // 2 bytes padding.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052};
53
54struct RtpDumpPacket {
55 RtpDumpPacket() {}
56
Peter Boström0c4e06b2015-10-07 12:23:21 +020057 RtpDumpPacket(const void* d, size_t s, uint32_t elapsed, bool rtcp)
58 : elapsed_time(elapsed), original_data_len((rtcp) ? 0 : s) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059 data.resize(s);
60 memcpy(&data[0], d, s);
61 }
62
63 // In the rtpdump file format, RTCP packets have their data len set to zero,
64 // since RTCP has an internal length field.
65 bool is_rtcp() const { return original_data_len == 0; }
66 bool IsValidRtpPacket() const;
67 bool IsValidRtcpPacket() const;
68 // Get the payload type, sequence number, timestampe, and SSRC of the RTP
69 // packet. Return true and set the output parameter if successful.
70 bool GetRtpPayloadType(int* pt) const;
71 bool GetRtpSeqNum(int* seq_num) const;
Peter Boström0c4e06b2015-10-07 12:23:21 +020072 bool GetRtpTimestamp(uint32_t* ts) const;
73 bool GetRtpSsrc(uint32_t* ssrc) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074 bool GetRtpHeaderLen(size_t* len) const;
75 // Get the type of the RTCP packet. Return true and set the output parameter
76 // if successful.
77 bool GetRtcpType(int* type) const;
78
79 static const size_t kHeaderLength = 8;
Peter Boström0c4e06b2015-10-07 12:23:21 +020080 uint32_t elapsed_time; // Milliseconds since the start of recording.
81 std::vector<uint8_t> data; // The actual RTP or RTCP packet.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082 size_t original_data_len; // The original length of the packet; may be
83 // greater than data.size() if only part of the
84 // packet was recorded.
85};
86
87class RtpDumpReader {
88 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000089 explicit RtpDumpReader(rtc::StreamInterface* stream)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 : stream_(stream),
91 file_header_read_(false),
92 first_line_and_file_header_len_(0),
93 start_time_ms_(0),
94 ssrc_override_(0) {
95 }
96 virtual ~RtpDumpReader() {}
97
98 // Use the specified ssrc, rather than the ssrc from dump, for RTP packets.
Peter Boström0c4e06b2015-10-07 12:23:21 +020099 void SetSsrc(uint32_t ssrc);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000100 virtual rtc::StreamResult ReadPacket(RtpDumpPacket* packet);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101
102 protected:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000103 rtc::StreamResult ReadFileHeader();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104 bool RewindToFirstDumpPacket() {
105 return stream_->SetPosition(first_line_and_file_header_len_);
106 }
107
108 private:
109 // Check if its matches "#!rtpplay1.0 address/port\n".
110 bool CheckFirstLine(const std::string& first_line);
111
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000112 rtc::StreamInterface* stream_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 bool file_header_read_;
114 size_t first_line_and_file_header_len_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200115 uint32_t start_time_ms_;
116 uint32_t ssrc_override_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117
henrikg3c089d72015-09-16 05:37:44 -0700118 RTC_DISALLOW_COPY_AND_ASSIGN(RtpDumpReader);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119};
120
121// RtpDumpLoopReader reads RTP dump packets from the input stream and rewinds
122// the stream when it ends. RtpDumpLoopReader maintains the elapsed time, the
123// RTP sequence number and the RTP timestamp properly. RtpDumpLoopReader can
124// handle both RTP dump and RTCP dump. We assume that the dump does not mix
125// RTP packets and RTCP packets.
126class RtpDumpLoopReader : public RtpDumpReader {
127 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000128 explicit RtpDumpLoopReader(rtc::StreamInterface* stream);
129 virtual rtc::StreamResult ReadPacket(RtpDumpPacket* packet);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130
131 private:
132 // During the first loop, update the statistics, including packet count, frame
133 // count, timestamps, and sequence number, of the input stream.
134 void UpdateStreamStatistics(const RtpDumpPacket& packet);
135
136 // At the end of first loop, calculate elapsed_time_increases_,
137 // rtp_seq_num_increase_, and rtp_timestamp_increase_.
138 void CalculateIncreases();
139
140 // During the second and later loops, update the elapsed time of the dump
141 // packet. If the dumped packet is a RTP packet, update its RTP sequence
142 // number and timestamp as well.
143 void UpdateDumpPacket(RtpDumpPacket* packet);
144
145 int loop_count_;
146 // How much to increase the elapsed time, RTP sequence number, RTP timestampe
147 // for each loop. They are calcualted with the variables below during the
148 // first loop.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200149 uint32_t elapsed_time_increases_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150 int rtp_seq_num_increase_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200151 uint32_t rtp_timestamp_increase_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152 // How many RTP packets and how many payload frames in the input stream. RTP
153 // packets belong to the same frame have the same RTP timestamp, different
154 // dump timestamp, and different RTP sequence number.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200155 uint32_t packet_count_;
156 uint32_t frame_count_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157 // The elapsed time, RTP sequence number, and RTP timestamp of the first and
158 // the previous dump packets in the input stream.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200159 uint32_t first_elapsed_time_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160 int first_rtp_seq_num_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200161 uint32_t first_rtp_timestamp_;
162 uint32_t prev_elapsed_time_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 int prev_rtp_seq_num_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200164 uint32_t prev_rtp_timestamp_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165
henrikg3c089d72015-09-16 05:37:44 -0700166 RTC_DISALLOW_COPY_AND_ASSIGN(RtpDumpLoopReader);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167};
168
169class RtpDumpWriter {
170 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000171 explicit RtpDumpWriter(rtc::StreamInterface* stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172
173 // Filter to control what packets we actually record.
174 void set_packet_filter(int filter);
175 // Write a RTP or RTCP packet. The parameters data points to the packet and
176 // data_len is its length.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000177 rtc::StreamResult WriteRtpPacket(const void* data, size_t data_len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178 return WritePacket(data, data_len, GetElapsedTime(), false);
179 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000180 rtc::StreamResult WriteRtcpPacket(const void* data, size_t data_len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 return WritePacket(data, data_len, GetElapsedTime(), true);
182 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000183 rtc::StreamResult WritePacket(const RtpDumpPacket& packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184 return WritePacket(&packet.data[0], packet.data.size(), packet.elapsed_time,
185 packet.is_rtcp());
186 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200187 uint32_t GetElapsedTime() const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188
189 bool GetDumpSize(size_t* size) {
190 // Note that we use GetPosition(), rather than GetSize(), to avoid flush the
191 // stream per write.
192 return stream_ && size && stream_->GetPosition(size);
193 }
194
195 protected:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000196 rtc::StreamResult WriteFileHeader();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197
198 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200199 rtc::StreamResult WritePacket(const void* data,
200 size_t data_len,
201 uint32_t elapsed,
202 bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203 size_t FilterPacket(const void* data, size_t data_len, bool rtcp);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000204 rtc::StreamResult WriteToStream(const void* data, size_t data_len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000206 rtc::StreamInterface* stream_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207 int packet_filter_;
208 bool file_header_written_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200209 uint32_t start_time_ms_; // Time when the record starts.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210 // If writing to the stream takes longer than this many ms, log a warning.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200211 uint32_t warn_slow_writes_delay_;
henrikg3c089d72015-09-16 05:37:44 -0700212 RTC_DISALLOW_COPY_AND_ASSIGN(RtpDumpWriter);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213};
214
215} // namespace cricket
216
kjellandera96e2d72016-02-04 23:52:28 -0800217#endif // WEBRTC_MEDIA_BASE_RTPDUMP_H_