Chromium Code Reviews| Index: chrome/browser/media/webrtc_rtp_dump_writer.cc |
| diff --git a/chrome/browser/media/webrtc_rtp_dump_writer.cc b/chrome/browser/media/webrtc_rtp_dump_writer.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cb928b529e3aa864dadc5411b1e589fdc09e21aa |
| --- /dev/null |
| +++ b/chrome/browser/media/webrtc_rtp_dump_writer.cc |
| @@ -0,0 +1,201 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/media/webrtc_rtp_dump_writer.h" |
| + |
| +#include "base/files/file.h" |
| +#include "base/logging.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +using content::BrowserThread; |
| + |
| +namespace { |
| + |
| +// The header of the dump file. |
| +struct RtpDumpFileHeader { |
| + static const unsigned char kFirstLine[]; |
| + |
| + explicit RtpDumpFileHeader(const base::TimeTicks& start) |
| + : start_sec(0), |
| + start_usec(0), |
| + source(0), |
| + port(0), |
| + padding(0) { |
| + base::TimeDelta interval(start - base::TimeTicks()); |
| + start_sec = interval.InSeconds(); |
| + start_usec = |
| + interval.InMilliseconds() * base::Time::kMicrosecondsPerMillisecond; |
| + } |
| + |
| + uint32 start_sec; // start of recording, the seconds part. |
| + uint32 start_usec; // start of recording, the microseconds part. |
| + uint32 source; // network source (multicast address). Always 0. |
| + uint16 port; // UDP port. Always 0. |
| + uint16 padding; // 2 bytes padding. |
| +}; |
| +const unsigned char RtpDumpFileHeader::kFirstLine[] = |
| + "#!rtpplay1.0 0.0.0.0/0\n"; |
| + |
| +// The header for each packet dump. |
| +struct PacketDumpHeader { |
| + PacketDumpHeader(const base::TimeTicks& start, |
| + uint16 dump_length, |
| + uint16 packet_length) |
| + : packet_dump_length(dump_length), |
| + packet_length(packet_length), |
| + offset_ms((base::TimeTicks::Now() - start).InMilliseconds()) {} |
| + |
| + // Length of the packet dump including this header. |
| + uint16 packet_dump_length; |
| + |
| + // Length of header + payload of the RTP packet. |
| + uint16 packet_length; |
| + |
| + // Milliseconds since the start of recording. |
| + uint32 offset_ms; |
| +}; |
| + |
| +bool AppendToBuffer( |
| + const uint8* src, size_t src_len, std::vector<uint8>* dest) { |
| + if (dest->capacity() < dest->size() + src_len) |
| + return false; |
| + |
| + for (size_t i = 0; i < src_len; ++i) { |
| + dest->push_back(src[i]); |
| + } |
| + return true; |
| +} |
| + |
| +uint16 GetBigEndian16(const uint8* memory) { |
| + return static_cast<uint16>((memory[0] << 8) | (memory[1] << 0)); |
| +} |
| + |
| +void GetRtpHeaderLen(const uint8* packet, size_t length, size_t* output) { |
| + static const size_t kMinRtpPacketLen = 12; |
| + |
| + DCHECK(packet && length >= kMinRtpPacketLen && output); |
| + |
| + // Get base header size + length of CSRCs (not counting extension yet). |
| + size_t header_size = kMinRtpPacketLen + (packet[0] & 0xF) * sizeof(uint32); |
| + DCHECK(length >= header_size); |
| + |
| + // If there's an extension, read and add the extension size. |
| + if (packet[0] & 0x10) { |
| + DCHECK(length >= header_size + sizeof(uint32)); |
| + |
| + header_size += ((GetBigEndian16(packet + header_size + 2) + 1) * |
| + sizeof(uint32)); |
| + DCHECK(length >= header_size); |
| + } |
| + *output = header_size; |
| +} |
| + |
| +} // namespace |
| + |
| +WebRtcRtpDumpWriter::WebRtcRtpDumpWriter( |
| + const base::FilePath& dump_path, |
| + size_t max_dump_size, |
| + const base::Closure& max_dump_size_reached_callback) |
| + : dump_path_(dump_path), |
| + max_dump_size_(max_dump_size), |
| + max_dump_size_reached_callback_(max_dump_size_reached_callback), |
| + weak_ptr_factory_(this) {} |
| + |
| +WebRtcRtpDumpWriter::~WebRtcRtpDumpWriter() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| +} |
| + |
| +void WebRtcRtpDumpWriter::WriteRtpPacket(const uint8* packet, size_t length) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + static const size_t kMaxInMemoryBufferSize = 65536; // 64KB |
| + |
| + bool succeeded = true; |
| + if (!buffer_.capacity()) { |
| + buffer_.reserve(std::min(kMaxInMemoryBufferSize, max_dump_size_)); |
| + |
| + start_time_ = base::TimeTicks::Now(); |
| + |
| + // Writes the dump file header. |
| + succeeded = AppendToBuffer(RtpDumpFileHeader::kFirstLine, |
| + arraysize(RtpDumpFileHeader::kFirstLine) - 1, |
| + &buffer_); |
| + DCHECK(succeeded); |
| + |
| + RtpDumpFileHeader header(start_time_); |
| + succeeded = AppendToBuffer(reinterpret_cast<uint8*>(&header), |
| + sizeof(header), |
| + &buffer_); |
| + DCHECK(succeeded); |
| + } |
| + |
| + size_t rtp_header_length = 0; |
| + GetRtpHeaderLen(packet, length, &rtp_header_length); |
| + if (!rtp_header_length) |
| + return; |
| + |
| + size_t packet_dump_length = sizeof(PacketDumpHeader) + rtp_header_length; |
| + |
| + // Flushes the buffer to disk if the buffer is full. |
| + if (buffer_.capacity() < buffer_.size() + packet_dump_length) |
| + Flush(base::Callback<void(bool)>()); |
| + |
| + // Writes the packet dump header. |
| + PacketDumpHeader packet_header(start_time_, packet_dump_length, length); |
| + |
| + succeeded = AppendToBuffer(reinterpret_cast<uint8*>(&packet_header), |
| + sizeof(packet_header), |
| + &buffer_); |
| + DCHECK(succeeded); |
| + |
| + // Writes the actual RTP packet header. |
| + succeeded = AppendToBuffer(packet, rtp_header_length, &buffer_); |
| + DCHECK(succeeded); |
| +} |
| + |
| +void WebRtcRtpDumpWriter::Flush( |
| + const base::Callback<void(bool)>& finished_callback) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + scoped_ptr<std::vector<uint8>> new_buffer(new std::vector<uint8>()); |
| + new_buffer->reserve(buffer_.capacity()); |
| + |
| + new_buffer->swap(buffer_); |
| + |
| + scoped_ptr<bool> result(new bool(false)); |
| + |
| + // OnFlushDone is necessary to avoid running the callback after this object is |
|
Henrik Grunell
2014/05/07 09:38:19
Will this object delete the file if this happens?
|
| + // gone. |
| + BrowserThread::PostTaskAndReply( |
| + BrowserThread::FILE, |
| + FROM_HERE, |
| + base::Bind(&WebRtcRtpDumpWriter::CompressAndWriteToFileOnFileThread, |
| + Passed(&new_buffer), |
| + dump_path_, |
| + result.get()), |
| + base::Bind(&WebRtcRtpDumpWriter::OnFlushDone, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + finished_callback, |
| + Passed(&result))); |
| +} |
| + |
| +// static |
| +void WebRtcRtpDumpWriter::CompressAndWriteToFileOnFileThread( |
| + const scoped_ptr<std::vector<uint8>>& buffer, |
| + const base::FilePath& dump_path, |
| + bool* result) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
|
Henrik Grunell
2014/05/07 09:38:19
OK, I see you're doing it on the FILE thread.
|
| + |
| + // TODO: implement compressing file and writing file. |
| + *result = true; |
| +} |
| + |
| +void WebRtcRtpDumpWriter::OnFlushDone( |
| + const base::Callback<void(bool)>& callback, |
| + const scoped_ptr<bool>& result) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + if(!callback.is_null()) |
| + callback.Run(*result); |
| +} |