blob: a109f2d8e269bcfd135794a0c7847d2b8e51d79a [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#include "webrtc/media/base/rtpdump.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
13#include <ctype.h>
14
15#include <string>
16
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000017#include "webrtc/base/byteorder.h"
18#include "webrtc/base/logging.h"
19#include "webrtc/base/timeutils.h"
kjellandera96e2d72016-02-04 23:52:28 -080020#include "webrtc/media/base/rtputils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021
22namespace {
23static const int kRtpSsrcOffset = 8;
24const int kWarnSlowWritesDelayMs = 50;
25} // namespace
26
27namespace cricket {
28
29const char RtpDumpFileHeader::kFirstLine[] = "#!rtpplay1.0 0.0.0.0/0\n";
30
Peter Boström0c4e06b2015-10-07 12:23:21 +020031RtpDumpFileHeader::RtpDumpFileHeader(uint32_t start_ms, uint32_t s, uint16_t p)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032 : start_sec(start_ms / 1000),
33 start_usec(start_ms % 1000 * 1000),
34 source(s),
35 port(p),
36 padding(0) {
37}
38
jbauchf1f87202016-03-30 06:43:37 -070039void RtpDumpFileHeader::WriteToByteBuffer(rtc::ByteBufferWriter* buf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040 buf->WriteUInt32(start_sec);
41 buf->WriteUInt32(start_usec);
42 buf->WriteUInt32(source);
43 buf->WriteUInt16(port);
44 buf->WriteUInt16(padding);
45}
46
Peter Boström0c4e06b2015-10-07 12:23:21 +020047static const uint32_t kDefaultTimeIncrease = 30;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048
49bool RtpDumpPacket::IsValidRtpPacket() const {
50 return original_data_len >= data.size() &&
51 data.size() >= kMinRtpPacketLen;
52}
53
54bool RtpDumpPacket::IsValidRtcpPacket() const {
55 return original_data_len == 0 &&
56 data.size() >= kMinRtcpPacketLen;
57}
58
59bool RtpDumpPacket::GetRtpPayloadType(int* pt) const {
60 return IsValidRtpPacket() &&
61 cricket::GetRtpPayloadType(&data[0], data.size(), pt);
62}
63
64bool RtpDumpPacket::GetRtpSeqNum(int* seq_num) const {
65 return IsValidRtpPacket() &&
66 cricket::GetRtpSeqNum(&data[0], data.size(), seq_num);
67}
68
Peter Boström0c4e06b2015-10-07 12:23:21 +020069bool RtpDumpPacket::GetRtpTimestamp(uint32_t* ts) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070 return IsValidRtpPacket() &&
71 cricket::GetRtpTimestamp(&data[0], data.size(), ts);
72}
73
Peter Boström0c4e06b2015-10-07 12:23:21 +020074bool RtpDumpPacket::GetRtpSsrc(uint32_t* ssrc) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 return IsValidRtpPacket() &&
76 cricket::GetRtpSsrc(&data[0], data.size(), ssrc);
77}
78
79bool RtpDumpPacket::GetRtpHeaderLen(size_t* len) const {
80 return IsValidRtpPacket() &&
81 cricket::GetRtpHeaderLen(&data[0], data.size(), len);
82}
83
84bool RtpDumpPacket::GetRtcpType(int* type) const {
85 return IsValidRtcpPacket() &&
86 cricket::GetRtcpType(&data[0], data.size(), type);
87}
88
89///////////////////////////////////////////////////////////////////////////
90// Implementation of RtpDumpReader.
91///////////////////////////////////////////////////////////////////////////
92
Peter Boström0c4e06b2015-10-07 12:23:21 +020093void RtpDumpReader::SetSsrc(uint32_t ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 ssrc_override_ = ssrc;
95}
96
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000097rtc::StreamResult RtpDumpReader::ReadPacket(RtpDumpPacket* packet) {
98 if (!packet) return rtc::SR_ERROR;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000100 rtc::StreamResult res = rtc::SR_SUCCESS;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 // Read the file header if it has not been read yet.
102 if (!file_header_read_) {
103 res = ReadFileHeader();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000104 if (res != rtc::SR_SUCCESS) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105 return res;
106 }
107 file_header_read_ = true;
108 }
109
110 // Read the RTP dump packet header.
111 char header[RtpDumpPacket::kHeaderLength];
112 res = stream_->ReadAll(header, sizeof(header), NULL, NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000113 if (res != rtc::SR_SUCCESS) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114 return res;
115 }
jbauchf1f87202016-03-30 06:43:37 -0700116 rtc::ByteBufferReader buf(header, sizeof(header));
Peter Boström0c4e06b2015-10-07 12:23:21 +0200117 uint16_t dump_packet_len;
118 uint16_t data_len;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119 // Read the full length of the rtpdump packet, including the rtpdump header.
120 buf.ReadUInt16(&dump_packet_len);
121 packet->data.resize(dump_packet_len - sizeof(header));
122 // Read the size of the original packet, which may be larger than the size in
123 // the rtpdump file, in the event that only part of the packet (perhaps just
124 // the header) was recorded. Note that this field is set to zero for RTCP
125 // packets, which have their own internal length field.
126 buf.ReadUInt16(&data_len);
127 packet->original_data_len = data_len;
128 // Read the elapsed time for this packet (different than RTP timestamp).
129 buf.ReadUInt32(&packet->elapsed_time);
130
131 // Read the actual RTP or RTCP packet.
132 res = stream_->ReadAll(&packet->data[0], packet->data.size(), NULL, NULL);
133
134 // If the packet is RTP and we have specified a ssrc, replace the RTP ssrc
135 // with the specified ssrc.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000136 if (res == rtc::SR_SUCCESS &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137 packet->IsValidRtpPacket() &&
138 ssrc_override_ != 0) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000139 rtc::SetBE32(&packet->data[kRtpSsrcOffset], ssrc_override_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140 }
141
142 return res;
143}
144
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000145rtc::StreamResult RtpDumpReader::ReadFileHeader() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146 // Read the first line.
147 std::string first_line;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000148 rtc::StreamResult res = stream_->ReadLine(&first_line);
149 if (res != rtc::SR_SUCCESS) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150 return res;
151 }
152 if (!CheckFirstLine(first_line)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000153 return rtc::SR_ERROR;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154 }
155
156 // Read the 16 byte file header.
157 char header[RtpDumpFileHeader::kHeaderLength];
158 res = stream_->ReadAll(header, sizeof(header), NULL, NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000159 if (res == rtc::SR_SUCCESS) {
jbauchf1f87202016-03-30 06:43:37 -0700160 rtc::ByteBufferReader buf(header, sizeof(header));
Peter Boström0c4e06b2015-10-07 12:23:21 +0200161 uint32_t start_sec;
162 uint32_t start_usec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 buf.ReadUInt32(&start_sec);
164 buf.ReadUInt32(&start_usec);
165 start_time_ms_ = start_sec * 1000 + start_usec / 1000;
166 // Increase the length by 1 since first_line does not contain the ending \n.
167 first_line_and_file_header_len_ = first_line.size() + 1 + sizeof(header);
168 }
169 return res;
170}
171
172bool RtpDumpReader::CheckFirstLine(const std::string& first_line) {
173 // The first line is like "#!rtpplay1.0 address/port"
174 bool matched = (0 == first_line.find("#!rtpplay1.0 "));
175
176 // The address could be IP or hostname. We do not check it here. Instead, we
177 // check the port at the end.
178 size_t pos = first_line.find('/');
179 matched &= (pos != std::string::npos && pos < first_line.size() - 1);
180 for (++pos; pos < first_line.size() && matched; ++pos) {
181 matched &= (0 != isdigit(first_line[pos]));
182 }
183
184 return matched;
185}
186
187///////////////////////////////////////////////////////////////////////////
188// Implementation of RtpDumpLoopReader.
189///////////////////////////////////////////////////////////////////////////
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000190RtpDumpLoopReader::RtpDumpLoopReader(rtc::StreamInterface* stream)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191 : RtpDumpReader(stream),
192 loop_count_(0),
193 elapsed_time_increases_(0),
194 rtp_seq_num_increase_(0),
195 rtp_timestamp_increase_(0),
196 packet_count_(0),
197 frame_count_(0),
198 first_elapsed_time_(0),
199 first_rtp_seq_num_(0),
200 first_rtp_timestamp_(0),
201 prev_elapsed_time_(0),
202 prev_rtp_seq_num_(0),
203 prev_rtp_timestamp_(0) {
204}
205
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000206rtc::StreamResult RtpDumpLoopReader::ReadPacket(RtpDumpPacket* packet) {
207 if (!packet) return rtc::SR_ERROR;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000209 rtc::StreamResult res = RtpDumpReader::ReadPacket(packet);
210 if (rtc::SR_SUCCESS == res) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211 if (0 == loop_count_) {
212 // During the first loop, we update the statistics of the input stream.
213 UpdateStreamStatistics(*packet);
214 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000215 } else if (rtc::SR_EOS == res) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216 if (0 == loop_count_) {
217 // At the end of the first loop, calculate elapsed_time_increases_,
218 // rtp_seq_num_increase_, and rtp_timestamp_increase_, which will be
219 // used during the second and later loops.
220 CalculateIncreases();
221 }
222
223 // Rewind the input stream to the first dump packet and read again.
224 ++loop_count_;
225 if (RewindToFirstDumpPacket()) {
226 res = RtpDumpReader::ReadPacket(packet);
227 }
228 }
229
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000230 if (rtc::SR_SUCCESS == res && loop_count_ > 0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231 // During the second and later loops, we update the elapsed time of the dump
232 // packet. If the dumped packet is a RTP packet, we also update its RTP
233 // sequence number and timestamp.
234 UpdateDumpPacket(packet);
235 }
236
237 return res;
238}
239
240void RtpDumpLoopReader::UpdateStreamStatistics(const RtpDumpPacket& packet) {
241 // Get the RTP sequence number and timestamp of the dump packet.
242 int rtp_seq_num = 0;
243 packet.GetRtpSeqNum(&rtp_seq_num);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200244 uint32_t rtp_timestamp = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245 packet.GetRtpTimestamp(&rtp_timestamp);
246
247 // Set the timestamps and sequence number for the first dump packet.
248 if (0 == packet_count_++) {
249 first_elapsed_time_ = packet.elapsed_time;
250 first_rtp_seq_num_ = rtp_seq_num;
251 first_rtp_timestamp_ = rtp_timestamp;
252 // The first packet belongs to a new payload frame.
253 ++frame_count_;
254 } else if (rtp_timestamp != prev_rtp_timestamp_) {
255 // The current and previous packets belong to different payload frames.
256 ++frame_count_;
257 }
258
259 prev_elapsed_time_ = packet.elapsed_time;
260 prev_rtp_timestamp_ = rtp_timestamp;
261 prev_rtp_seq_num_ = rtp_seq_num;
262}
263
264void RtpDumpLoopReader::CalculateIncreases() {
265 // At this time, prev_elapsed_time_, prev_rtp_seq_num_, and
266 // prev_rtp_timestamp_ are values of the last dump packet in the input stream.
267 rtp_seq_num_increase_ = prev_rtp_seq_num_ - first_rtp_seq_num_ + 1;
268 // If we have only one packet or frame, we use the default timestamp
269 // increase. Otherwise, we use the difference between the first and the last
270 // packets or frames.
271 elapsed_time_increases_ = packet_count_ <= 1 ? kDefaultTimeIncrease :
272 (prev_elapsed_time_ - first_elapsed_time_) * packet_count_ /
273 (packet_count_ - 1);
274 rtp_timestamp_increase_ = frame_count_ <= 1 ? kDefaultTimeIncrease :
275 (prev_rtp_timestamp_ - first_rtp_timestamp_) * frame_count_ /
276 (frame_count_ - 1);
277}
278
279void RtpDumpLoopReader::UpdateDumpPacket(RtpDumpPacket* packet) {
280 // Increase the elapsed time of the dump packet.
281 packet->elapsed_time += loop_count_ * elapsed_time_increases_;
282
283 if (packet->IsValidRtpPacket()) {
284 // Get the old RTP sequence number and timestamp.
285 int sequence = 0;
286 packet->GetRtpSeqNum(&sequence);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200287 uint32_t timestamp = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288 packet->GetRtpTimestamp(&timestamp);
289 // Increase the RTP sequence number and timestamp.
290 sequence += loop_count_ * rtp_seq_num_increase_;
291 timestamp += loop_count_ * rtp_timestamp_increase_;
292 // Write the updated sequence number and timestamp back to the RTP packet.
jbauchf1f87202016-03-30 06:43:37 -0700293 rtc::ByteBufferWriter buffer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294 buffer.WriteUInt16(sequence);
295 buffer.WriteUInt32(timestamp);
296 memcpy(&packet->data[2], buffer.Data(), buffer.Length());
297 }
298}
299
300///////////////////////////////////////////////////////////////////////////
301// Implementation of RtpDumpWriter.
302///////////////////////////////////////////////////////////////////////////
303
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000304RtpDumpWriter::RtpDumpWriter(rtc::StreamInterface* stream)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305 : stream_(stream),
306 packet_filter_(PF_ALL),
307 file_header_written_(false),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000308 start_time_ms_(rtc::Time()),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309 warn_slow_writes_delay_(kWarnSlowWritesDelayMs) {
310}
311
312void RtpDumpWriter::set_packet_filter(int filter) {
313 packet_filter_ = filter;
314 LOG(LS_INFO) << "RtpDumpWriter set_packet_filter to " << packet_filter_;
315}
316
Peter Boström0c4e06b2015-10-07 12:23:21 +0200317uint32_t RtpDumpWriter::GetElapsedTime() const {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000318 return rtc::TimeSince(start_time_ms_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319}
320
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000321rtc::StreamResult RtpDumpWriter::WriteFileHeader() {
322 rtc::StreamResult res = WriteToStream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323 RtpDumpFileHeader::kFirstLine,
324 strlen(RtpDumpFileHeader::kFirstLine));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000325 if (res != rtc::SR_SUCCESS) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326 return res;
327 }
328
jbauchf1f87202016-03-30 06:43:37 -0700329 rtc::ByteBufferWriter buf;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000330 RtpDumpFileHeader file_header(rtc::Time(), 0, 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331 file_header.WriteToByteBuffer(&buf);
332 return WriteToStream(buf.Data(), buf.Length());
333}
334
Peter Boström0c4e06b2015-10-07 12:23:21 +0200335rtc::StreamResult RtpDumpWriter::WritePacket(const void* data,
336 size_t data_len,
337 uint32_t elapsed,
338 bool rtcp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000339 if (!stream_ || !data || 0 == data_len) return rtc::SR_ERROR;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000341 rtc::StreamResult res = rtc::SR_SUCCESS;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342 // Write the file header if it has not been written yet.
343 if (!file_header_written_) {
344 res = WriteFileHeader();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000345 if (res != rtc::SR_SUCCESS) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346 return res;
347 }
348 file_header_written_ = true;
349 }
350
351 // Figure out what to write.
352 size_t write_len = FilterPacket(data, data_len, rtcp);
353 if (write_len == 0) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000354 return rtc::SR_SUCCESS;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355 }
356
357 // Write the dump packet header.
jbauchf1f87202016-03-30 06:43:37 -0700358 rtc::ByteBufferWriter buf;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200359 buf.WriteUInt16(
360 static_cast<uint16_t>(RtpDumpPacket::kHeaderLength + write_len));
361 buf.WriteUInt16(static_cast<uint16_t>(rtcp ? 0 : data_len));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362 buf.WriteUInt32(elapsed);
363 res = WriteToStream(buf.Data(), buf.Length());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000364 if (res != rtc::SR_SUCCESS) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000365 return res;
366 }
367
368 // Write the header or full packet as indicated by write_len.
369 return WriteToStream(data, write_len);
370}
371
372size_t RtpDumpWriter::FilterPacket(const void* data, size_t data_len,
373 bool rtcp) {
374 size_t filtered_len = 0;
375 if (!rtcp) {
376 if ((packet_filter_ & PF_RTPPACKET) == PF_RTPPACKET) {
377 // RTP header + payload
378 filtered_len = data_len;
379 } else if ((packet_filter_ & PF_RTPHEADER) == PF_RTPHEADER) {
380 // RTP header only
381 size_t header_len;
382 if (GetRtpHeaderLen(data, data_len, &header_len)) {
383 filtered_len = header_len;
384 }
385 }
386 } else {
387 if ((packet_filter_ & PF_RTCPPACKET) == PF_RTCPPACKET) {
388 // RTCP header + payload
389 filtered_len = data_len;
390 }
391 }
392
393 return filtered_len;
394}
395
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000396rtc::StreamResult RtpDumpWriter::WriteToStream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397 const void* data, size_t data_len) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200398 uint32_t before = rtc::Time();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000399 rtc::StreamResult result =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000400 stream_->WriteAll(data, data_len, NULL, NULL);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200401 uint32_t delay = rtc::TimeSince(before);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402 if (delay >= warn_slow_writes_delay_) {
403 LOG(LS_WARNING) << "Slow RtpDump: took " << delay << "ms to write "
404 << data_len << " bytes.";
405 }
406 return result;
407}
408
409} // namespace cricket