Index: chrome/browser/media/webrtc_rtp_dump_handler.cc |
diff --git a/chrome/browser/media/webrtc_rtp_dump_handler.cc b/chrome/browser/media/webrtc_rtp_dump_handler.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..78b081204823f80f3e8466771c7bf18130404af6 |
--- /dev/null |
+++ b/chrome/browser/media/webrtc_rtp_dump_handler.cc |
@@ -0,0 +1,121 @@ |
+// 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_handler.h" |
+ |
+#include "base/logging.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "base/time/time.h" |
+#include "chrome/browser/media/webrtc_rtp_dump_writer.h" |
+#include "content/public/browser/browser_thread.h" |
+ |
+using content::BrowserThread; |
+ |
+WebRtcRtpDumpHandler::WebRtcRtpDumpHandler(const base::FilePath& dump_dir) |
+ : dump_dir_(dump_dir), |
+ incoming_state_(STATE_NONE), |
+ outgoing_state_(STATE_NONE) { |
+} |
+ |
+WebRtcRtpDumpHandler::~WebRtcRtpDumpHandler() { |
Henrik Grunell
2014/05/07 09:38:19
On which thread should this class be created and d
|
+ // TODO: delete the dump file if not released yet. |
+ |
+ if (dump_writer_) { |
+ if (BrowserThread::DeleteSoon(BrowserThread::IO, |
+ FROM_HERE, |
+ dump_writer_.get())) { |
+ ignore_result(dump_writer_.release()); |
+ } |
+ } |
+} |
+ |
+bool WebRtcRtpDumpHandler::StartDump(const PacketType& type) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ |
+ bool succeeded = false; |
+ if (type.incoming && incoming_state_ == STATE_NONE) { |
Henrik Grunell
2014/05/07 09:38:19
If both incoming and outgoing are set, incoming st
|
+ incoming_state_ = STATE_STARTED; |
+ succeeded = true; |
+ } |
+ if (type.outgoing && outgoing_state_ == STATE_NONE) { |
+ outgoing_state_ = STATE_STARTED; |
+ succeeded = true; |
+ } |
+ |
+ if (succeeded && !dump_writer_) { |
+ static const char kDumpFilePrefix[] = "rtpdump_"; |
+ static const char kDumpFileExtension[] = ".gz"; |
+ static const size_t kMaxDumpSize = 8 * 1024 * 1024; |
+ |
+ std::string dump_id = base::DoubleToString(base::Time::Now().ToDoubleT()); |
+ dump_path_ = dump_dir_.AppendASCII(std::string(kDumpFilePrefix) + dump_id) |
+ .AddExtension(FILE_PATH_LITERAL(kDumpFileExtension)); |
+ |
+ dump_writer_.reset(new WebRtcRtpDumpWriter( |
+ dump_path_, |
+ kMaxDumpSize, |
+ base::Bind(&WebRtcRtpDumpHandler::OnMaxDumpSizeReached, |
+ base::Unretained(this)))); |
+ } |
+ return succeeded; |
+} |
+ |
+bool WebRtcRtpDumpHandler::StopDump(const PacketType& type) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ |
+ bool succeeded = false; |
+ if (type.incoming && incoming_state_ == STATE_STARTED) { |
+ incoming_state_ = STATE_STOPPED; |
+ succeeded = true; |
+ } |
+ if (type.outgoing && outgoing_state_ == STATE_STARTED) { |
+ outgoing_state_ = STATE_STOPPED; |
+ succeeded = true; |
+ } |
+ return succeeded; |
+} |
+ |
+bool WebRtcRtpDumpHandler::ReleaseDump(const ReleaseDumpCallback& callback) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ |
+ // All types of dumps must have been stopped or not started. |
+ if (incoming_state_ == STATE_STARTED || outgoing_state_ == STATE_STARTED || |
+ (incoming_state_ == STATE_NONE && outgoing_state_ == STATE_NONE)) |
+ return false; |
+ |
+ incoming_state_ = STATE_NONE; |
+ outgoing_state_ = STATE_NONE; |
+ |
+ dump_writer_->Flush(base::Bind(&WebRtcRtpDumpHandler::OnWriterFlushDone, |
+ base::Unretained(this), |
+ callback)); |
+ |
+ return true; |
+} |
+ |
+void WebRtcRtpDumpHandler::OnRtpPacket(const uint8* packet, |
+ size_t length, |
+ bool incoming) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ |
+ if ((incoming && incoming_state_ != STATE_STARTED) || |
+ (!incoming && outgoing_state_ != STATE_STARTED)) |
+ return; |
+ |
+ dump_writer_->WriteRtpPacket(packet, length); |
+} |
+ |
+void WebRtcRtpDumpHandler::OnMaxDumpSizeReached() { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ |
+ PacketType type = {true, true}; |
+ StopDump(type); |
+} |
+ |
+void WebRtcRtpDumpHandler::OnWriterFlushDone( |
+ const ReleaseDumpCallback& callback, bool succeeded) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ |
+ callback.Run(succeeded, dump_path_); |
+} |