Chromium Code Reviews| 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..a832da2b70489217f027c8e45bcc05e9c9110a4a |
| --- /dev/null |
| +++ b/chrome/browser/media/webrtc_rtp_dump_handler.cc |
| @@ -0,0 +1,268 @@ |
| +// 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/file_util.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; |
| + |
| +namespace { |
| + |
| +// |path| must be a copy of FilePath, not a reference. |
| +void DeleteFileHelper(base::FilePath path) { |
| + base::DeleteFile(path, false); |
| +} |
| + |
| +static const size_t kMaxOngoingRtpDumpsAllowed = 5; |
| + |
| +// The browser process wide total number of ongoing (i.e. started and not |
| +// released) RTP dumps. Incoming and outgoing in one WebRtcDumpHandler are |
| +// counted as one dump. |
| +// Must be accessed on the browser IO thread. |
| +static size_t g_ongoing_rtp_dumps = 0; |
| + |
| +void FireGenericDoneCallback( |
| + const WebRtcRtpDumpHandler::GenericDoneCallback& callback, |
| + bool success, |
| + const std::string& error_message) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + DCHECK(!callback.is_null()); |
| + |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(callback, success, error_message)); |
| +} |
| + |
| +} // namespace |
| + |
| +WebRtcRtpDumpHandler::WebRtcRtpDumpHandler(const base::FilePath& dump_dir) |
| + : dump_dir_(dump_dir), |
| + incoming_state_(STATE_NONE), |
| + outgoing_state_(STATE_NONE) { |
| +} |
| + |
| +WebRtcRtpDumpHandler::~WebRtcRtpDumpHandler() { |
| + if (incoming_state_ != STATE_NONE && !incoming_dump_path_.empty()) { |
| + BrowserThread::PostTask(BrowserThread::FILE, |
| + FROM_HERE, |
| + base::Bind(&DeleteFileHelper, incoming_dump_path_)); |
| + } |
| + |
| + if (outgoing_state_ != STATE_NONE && !outgoing_dump_path_.empty()) { |
| + BrowserThread::PostTask(BrowserThread::FILE, |
| + FROM_HERE, |
| + base::Bind(&DeleteFileHelper, outgoing_dump_path_)); |
| + } |
| + |
| + if (dump_writer_) |
| + g_ongoing_rtp_dumps--; |
|
Henrik Grunell
2014/05/14 12:14:12
nit: --g_ongoing_rtp_dumps;
jiayl
2014/05/14 18:59:12
Done.
|
| +} |
| + |
| +bool WebRtcRtpDumpHandler::StartDump(const PacketType& type, |
| + std::string* error_message) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + bool succeeded = false; |
| + |
| + if (type.incoming && incoming_state_ == STATE_NONE) { |
| + incoming_state_ = STATE_STARTED; |
| + succeeded = true; |
| + } |
| + if (type.outgoing && outgoing_state_ == STATE_NONE) { |
| + outgoing_state_ = STATE_STARTED; |
| + succeeded = true; |
| + } |
| + |
| + if (!dump_writer_ && g_ongoing_rtp_dumps >= kMaxOngoingRtpDumpsAllowed) { |
| + *error_message = "Max RTP dump limit reached."; |
| + DVLOG(2) << *error_message; |
| + return false; |
| + } |
| + |
| + if (!succeeded) { |
| + *error_message = "RTP dump already started."; |
| + return false; |
| + } |
| + |
| + DVLOG(2) << "Start RTP dumping: incoming = " << type.incoming |
| + << ", outgoing = " << type.outgoing; |
| + |
| + if (!dump_writer_) { |
| + g_ongoing_rtp_dumps++; |
| + |
| + static const char kRecvDumpFilePrefix[] = "rtpdump_recv_"; |
| + static const char kSendDumpFilePrefix[] = "rtpdump_send_"; |
| + static const char kDumpFileExtension[] = ".gz"; |
| + static const size_t kMaxDumpSize = 5 * 1024 * 1024; // 5MB |
| + |
| + std::string dump_id = base::DoubleToString(base::Time::Now().ToDoubleT()); |
| + incoming_dump_path_ = |
| + dump_dir_.AppendASCII(std::string(kRecvDumpFilePrefix) + dump_id) |
| + .AddExtension(FILE_PATH_LITERAL(kDumpFileExtension)); |
| + |
| + outgoing_dump_path_ = |
| + dump_dir_.AppendASCII(std::string(kSendDumpFilePrefix) + dump_id) |
| + .AddExtension(FILE_PATH_LITERAL(kDumpFileExtension)); |
| + |
| + // WebRtcRtpDumpWriter does not support changing the dump path after it's |
| + // created. So we assign both incoming and outgoing dump path even if only |
| + // one type of dumping has been started. |
| + dump_writer_.reset(new WebRtcRtpDumpWriter( |
| + incoming_dump_path_, |
| + outgoing_dump_path_, |
| + kMaxDumpSize, |
| + base::Bind(&WebRtcRtpDumpHandler::OnMaxDumpSizeReached, |
| + base::Unretained(this)))); |
| + } |
| + |
| + return true; |
| +} |
| + |
| +void WebRtcRtpDumpHandler::StopDump(const PacketType& type, |
| + const GenericDoneCallback& callback) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + PacketType valid_type; |
| + if (type.incoming && incoming_state_ == STATE_STARTED) { |
| + incoming_state_ = STATE_STOPPING; |
| + valid_type.incoming = true; |
| + } |
| + |
| + if (type.outgoing && outgoing_state_ == STATE_STARTED) { |
| + outgoing_state_ = STATE_STOPPING; |
| + valid_type.outgoing = true; |
| + } |
| + |
| + if (!valid_type.incoming && !valid_type.outgoing) { |
| + FireGenericDoneCallback( |
| + callback, false, "RTP dump not started or already stopped."); |
| + return; |
| + } |
| + |
| + DVLOG(2) << "Stopping RTP dumping: incoming = " << type.incoming |
| + << ", outgoing = " << type.outgoing; |
| + |
| + if (valid_type.incoming) { |
| + dump_writer_->EndDump( |
| + true, |
| + base::Bind(&WebRtcRtpDumpHandler::OnDumpEnded, |
| + base::Unretained(this), |
| + valid_type.outgoing ? GenericDoneCallback() : callback, |
| + true)); |
| + } |
| + |
| + if (valid_type.outgoing) { |
| + dump_writer_->EndDump(false, |
| + base::Bind(&WebRtcRtpDumpHandler::OnDumpEnded, |
| + base::Unretained(this), |
| + callback, |
| + false)); |
| + } |
| + |
| + return; |
| +} |
| + |
| +WebRtcRtpDumpHandler::ReleasedDumps WebRtcRtpDumpHandler::ReleaseDumps() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + ReleasedDumps result; |
| + |
| + // All types of dumps must have been stopped, or one stopped and the other not |
| + // started. |
| + if (incoming_state_ == STATE_STARTED || incoming_state_ == STATE_STOPPING || |
| + outgoing_state_ == STATE_STARTED || outgoing_state_ == STATE_STOPPING || |
| + (incoming_state_ == STATE_NONE && outgoing_state_ == STATE_NONE)) { |
| + DVLOG(2) << "ReleaseDumps called in invalid state: incoming_state = " |
| + << incoming_state_ << ", outgoing_state = " << outgoing_state_; |
| + return result; |
| + } |
| + |
| + if (incoming_state_ == STATE_STOPPED) { |
| + DVLOG(2) << "Incoming RTP dumps released: " << incoming_dump_path_.value(); |
| + |
| + incoming_state_ = STATE_NONE; |
| + result.incoming_dump_path = incoming_dump_path_; |
| + } |
| + |
| + if (outgoing_state_ == STATE_STOPPED) { |
| + DVLOG(2) << "Outgoing RTP dumps released: " << outgoing_dump_path_.value(); |
| + |
| + outgoing_state_ = STATE_NONE; |
| + result.outgoing_dump_path = outgoing_dump_path_; |
| + } |
| + return result; |
| +} |
| + |
| +void WebRtcRtpDumpHandler::OnRtpPacket(const uint8* packet_header, |
| + size_t header_length, |
| + size_t packet_length, |
| + bool incoming) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + if ((incoming && incoming_state_ != STATE_STARTED) || |
| + (!incoming && outgoing_state_ != STATE_STARTED)) |
| + return; |
| + |
| + dump_writer_->WriteRtpPacket( |
| + packet_header, header_length, packet_length, incoming); |
| +} |
| + |
| +void WebRtcRtpDumpHandler::OnMaxDumpSizeReached() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + PacketType type(true, true); |
| + StopDump(type, GenericDoneCallback()); |
| +} |
| + |
| +void WebRtcRtpDumpHandler::OnDumpEnded(const GenericDoneCallback& callback, |
| + bool incoming, |
| + bool succeeded) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + if (incoming) { |
| + DCHECK_EQ(STATE_STOPPING, incoming_state_); |
| + incoming_state_ = STATE_STOPPED; |
| + } else { |
| + DCHECK_EQ(STATE_STOPPING, outgoing_state_); |
| + outgoing_state_ = STATE_STOPPED; |
| + } |
| + |
| + if (!succeeded) { |
| + base::FilePath* dump = |
| + incoming ? &incoming_dump_path_ : &outgoing_dump_path_; |
| + |
| + BrowserThread::PostTask( |
| + BrowserThread::FILE, FROM_HERE, base::Bind(&DeleteFileHelper, *dump)); |
| + |
| + DVLOG(2) << "Deleted invalid dump " << dump->value(); |
| + dump->clear(); |
| + } |
| + |
| + if (!callback.is_null()) |
| + callback.Run(true, ""); |
| + |
| + // Release the writer when it's no longer needed. |
| + if (incoming_state_ != STATE_STOPPING && outgoing_state_ != STATE_STOPPING && |
| + incoming_state_ != STATE_STARTED && outgoing_state_ != STATE_STARTED) { |
| + dump_writer_.reset(); |
| + g_ongoing_rtp_dumps--; |
| + } |
| +} |
| + |
| +void WebRtcRtpDumpHandler::SetDumpWriterForTesting( |
| + scoped_ptr<WebRtcRtpDumpWriter> writer) { |
| + DCHECK(!dump_writer_.get()); |
| + dump_writer_ = writer.Pass(); |
| + g_ongoing_rtp_dumps++; |
| + |
| + incoming_dump_path_ = dump_dir_.AppendASCII("recv"); |
| + outgoing_dump_path_ = dump_dir_.AppendASCII("send"); |
| +} |