henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 2 | * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 4 | * 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 11 | #include "webrtc/media/base/rtpdump.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
| 13 | #include <ctype.h> |
| 14 | |
| 15 | #include <string> |
| 16 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 17 | #include "webrtc/base/byteorder.h" |
| 18 | #include "webrtc/base/logging.h" |
| 19 | #include "webrtc/base/timeutils.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 20 | #include "webrtc/media/base/rtputils.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 21 | |
| 22 | namespace { |
| 23 | static const int kRtpSsrcOffset = 8; |
| 24 | const int kWarnSlowWritesDelayMs = 50; |
| 25 | } // namespace |
| 26 | |
| 27 | namespace cricket { |
| 28 | |
| 29 | const char RtpDumpFileHeader::kFirstLine[] = "#!rtpplay1.0 0.0.0.0/0\n"; |
| 30 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 31 | RtpDumpFileHeader::RtpDumpFileHeader(uint32_t start_ms, uint32_t s, uint16_t p) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 32 | : start_sec(start_ms / 1000), |
| 33 | start_usec(start_ms % 1000 * 1000), |
| 34 | source(s), |
| 35 | port(p), |
| 36 | padding(0) { |
| 37 | } |
| 38 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame^] | 39 | void RtpDumpFileHeader::WriteToByteBuffer(rtc::ByteBufferWriter* buf) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 40 | 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öm | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 47 | static const uint32_t kDefaultTimeIncrease = 30; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 48 | |
| 49 | bool RtpDumpPacket::IsValidRtpPacket() const { |
| 50 | return original_data_len >= data.size() && |
| 51 | data.size() >= kMinRtpPacketLen; |
| 52 | } |
| 53 | |
| 54 | bool RtpDumpPacket::IsValidRtcpPacket() const { |
| 55 | return original_data_len == 0 && |
| 56 | data.size() >= kMinRtcpPacketLen; |
| 57 | } |
| 58 | |
| 59 | bool RtpDumpPacket::GetRtpPayloadType(int* pt) const { |
| 60 | return IsValidRtpPacket() && |
| 61 | cricket::GetRtpPayloadType(&data[0], data.size(), pt); |
| 62 | } |
| 63 | |
| 64 | bool RtpDumpPacket::GetRtpSeqNum(int* seq_num) const { |
| 65 | return IsValidRtpPacket() && |
| 66 | cricket::GetRtpSeqNum(&data[0], data.size(), seq_num); |
| 67 | } |
| 68 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 69 | bool RtpDumpPacket::GetRtpTimestamp(uint32_t* ts) const { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 70 | return IsValidRtpPacket() && |
| 71 | cricket::GetRtpTimestamp(&data[0], data.size(), ts); |
| 72 | } |
| 73 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 74 | bool RtpDumpPacket::GetRtpSsrc(uint32_t* ssrc) const { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 75 | return IsValidRtpPacket() && |
| 76 | cricket::GetRtpSsrc(&data[0], data.size(), ssrc); |
| 77 | } |
| 78 | |
| 79 | bool RtpDumpPacket::GetRtpHeaderLen(size_t* len) const { |
| 80 | return IsValidRtpPacket() && |
| 81 | cricket::GetRtpHeaderLen(&data[0], data.size(), len); |
| 82 | } |
| 83 | |
| 84 | bool 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öm | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 93 | void RtpDumpReader::SetSsrc(uint32_t ssrc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 94 | ssrc_override_ = ssrc; |
| 95 | } |
| 96 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 97 | rtc::StreamResult RtpDumpReader::ReadPacket(RtpDumpPacket* packet) { |
| 98 | if (!packet) return rtc::SR_ERROR; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 99 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 100 | rtc::StreamResult res = rtc::SR_SUCCESS; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 101 | // Read the file header if it has not been read yet. |
| 102 | if (!file_header_read_) { |
| 103 | res = ReadFileHeader(); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 104 | if (res != rtc::SR_SUCCESS) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 105 | 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.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 113 | if (res != rtc::SR_SUCCESS) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 114 | return res; |
| 115 | } |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame^] | 116 | rtc::ByteBufferReader buf(header, sizeof(header)); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 117 | uint16_t dump_packet_len; |
| 118 | uint16_t data_len; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 119 | // 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.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 136 | if (res == rtc::SR_SUCCESS && |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 137 | packet->IsValidRtpPacket() && |
| 138 | ssrc_override_ != 0) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 139 | rtc::SetBE32(&packet->data[kRtpSsrcOffset], ssrc_override_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | return res; |
| 143 | } |
| 144 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 145 | rtc::StreamResult RtpDumpReader::ReadFileHeader() { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 146 | // Read the first line. |
| 147 | std::string first_line; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 148 | rtc::StreamResult res = stream_->ReadLine(&first_line); |
| 149 | if (res != rtc::SR_SUCCESS) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 150 | return res; |
| 151 | } |
| 152 | if (!CheckFirstLine(first_line)) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 153 | return rtc::SR_ERROR; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | // Read the 16 byte file header. |
| 157 | char header[RtpDumpFileHeader::kHeaderLength]; |
| 158 | res = stream_->ReadAll(header, sizeof(header), NULL, NULL); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 159 | if (res == rtc::SR_SUCCESS) { |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame^] | 160 | rtc::ByteBufferReader buf(header, sizeof(header)); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 161 | uint32_t start_sec; |
| 162 | uint32_t start_usec; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 163 | 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 | |
| 172 | bool 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.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 190 | RtpDumpLoopReader::RtpDumpLoopReader(rtc::StreamInterface* stream) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 191 | : 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.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 206 | rtc::StreamResult RtpDumpLoopReader::ReadPacket(RtpDumpPacket* packet) { |
| 207 | if (!packet) return rtc::SR_ERROR; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 208 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 209 | rtc::StreamResult res = RtpDumpReader::ReadPacket(packet); |
| 210 | if (rtc::SR_SUCCESS == res) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 211 | if (0 == loop_count_) { |
| 212 | // During the first loop, we update the statistics of the input stream. |
| 213 | UpdateStreamStatistics(*packet); |
| 214 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 215 | } else if (rtc::SR_EOS == res) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 216 | 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.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 230 | if (rtc::SR_SUCCESS == res && loop_count_ > 0) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 231 | // 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 | |
| 240 | void 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öm | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 244 | uint32_t rtp_timestamp = 0; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 245 | 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 | |
| 264 | void 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 | |
| 279 | void 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öm | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 287 | uint32_t timestamp = 0; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 288 | packet->GetRtpTimestamp(×tamp); |
| 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. |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame^] | 293 | rtc::ByteBufferWriter buffer; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 294 | 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.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 304 | RtpDumpWriter::RtpDumpWriter(rtc::StreamInterface* stream) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 305 | : stream_(stream), |
| 306 | packet_filter_(PF_ALL), |
| 307 | file_header_written_(false), |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 308 | start_time_ms_(rtc::Time()), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 309 | warn_slow_writes_delay_(kWarnSlowWritesDelayMs) { |
| 310 | } |
| 311 | |
| 312 | void 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öm | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 317 | uint32_t RtpDumpWriter::GetElapsedTime() const { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 318 | return rtc::TimeSince(start_time_ms_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 319 | } |
| 320 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 321 | rtc::StreamResult RtpDumpWriter::WriteFileHeader() { |
| 322 | rtc::StreamResult res = WriteToStream( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 323 | RtpDumpFileHeader::kFirstLine, |
| 324 | strlen(RtpDumpFileHeader::kFirstLine)); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 325 | if (res != rtc::SR_SUCCESS) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 326 | return res; |
| 327 | } |
| 328 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame^] | 329 | rtc::ByteBufferWriter buf; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 330 | RtpDumpFileHeader file_header(rtc::Time(), 0, 0); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 331 | file_header.WriteToByteBuffer(&buf); |
| 332 | return WriteToStream(buf.Data(), buf.Length()); |
| 333 | } |
| 334 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 335 | rtc::StreamResult RtpDumpWriter::WritePacket(const void* data, |
| 336 | size_t data_len, |
| 337 | uint32_t elapsed, |
| 338 | bool rtcp) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 339 | if (!stream_ || !data || 0 == data_len) return rtc::SR_ERROR; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 340 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 341 | rtc::StreamResult res = rtc::SR_SUCCESS; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 342 | // Write the file header if it has not been written yet. |
| 343 | if (!file_header_written_) { |
| 344 | res = WriteFileHeader(); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 345 | if (res != rtc::SR_SUCCESS) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 346 | 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.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 354 | return rtc::SR_SUCCESS; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | // Write the dump packet header. |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame^] | 358 | rtc::ByteBufferWriter buf; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 359 | buf.WriteUInt16( |
| 360 | static_cast<uint16_t>(RtpDumpPacket::kHeaderLength + write_len)); |
| 361 | buf.WriteUInt16(static_cast<uint16_t>(rtcp ? 0 : data_len)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 362 | buf.WriteUInt32(elapsed); |
| 363 | res = WriteToStream(buf.Data(), buf.Length()); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 364 | if (res != rtc::SR_SUCCESS) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 365 | return res; |
| 366 | } |
| 367 | |
| 368 | // Write the header or full packet as indicated by write_len. |
| 369 | return WriteToStream(data, write_len); |
| 370 | } |
| 371 | |
| 372 | size_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.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 396 | rtc::StreamResult RtpDumpWriter::WriteToStream( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 397 | const void* data, size_t data_len) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 398 | uint32_t before = rtc::Time(); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 399 | rtc::StreamResult result = |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 400 | stream_->WriteAll(data, data_len, NULL, NULL); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 401 | uint32_t delay = rtc::TimeSince(before); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 402 | 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 |