Index: chrome/browser/media/webrtc_rtp_dump_writer.h |
diff --git a/chrome/browser/media/webrtc_rtp_dump_writer.h b/chrome/browser/media/webrtc_rtp_dump_writer.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0783d1c8c6484c10ab82075e47410187f411bb2f |
--- /dev/null |
+++ b/chrome/browser/media/webrtc_rtp_dump_writer.h |
@@ -0,0 +1,97 @@ |
+// 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. |
+ |
+#ifndef CHROME_BROWSER_MEDIA_WEBRTC_RTP_DUMP_WRITER_H_ |
+#define CHROME_BROWSER_MEDIA_WEBRTC_RTP_DUMP_WRITER_H_ |
+ |
+#include "base/basictypes.h" |
+#include "base/callback.h" |
+#include "base/files/file_path.h" |
+#include "base/memory/weak_ptr.h" |
+#include "base/threading/thread_checker.h" |
+#include "base/time/time.h" |
+ |
+// This class is responsible for creating the compressed RTP header dump file: |
+// - Adds the RTP headers to an in-memory buffer. |
+// - When the in-memory buffer is full, compresses it, and writes it to the |
+// disk. Drop the packet and notifies the client if the max file size is |
+// reached. |
+// - The uncompressed dump follows the standard RTPPlay format |
+// (http://www.cs.columbia.edu/irt/software/rtptools/). |
+// - The caller is always responsible for cleaning up the dump file in all |
+// cases. |
+// - WebRtcRtpDumpWriter does not stop writing to the dump after the max size |
+// limit is reached. The caller must stop calling WriteRtpPacket instead. |
+// |
+// This object must run on the IO thread. |
+class WebRtcRtpDumpWriter { |
+ public: |
+ // Returns the header length of the RTP packet. |
+ static size_t GetRtpHeaderLen(const uint8* packet, size_t length); |
+ |
+ // |dump_path| is the file path of the compressed dump file. |max_dump_size| |
+ // is the max size of the compressed dump file in bytes. |
+ // |max_dump_size_reached_callback| will be called when the on-disk file size |
+ // reaches |max_dump_size|. |
+ WebRtcRtpDumpWriter(const base::FilePath& incoming_dump_path, |
+ const base::FilePath& outgoing_dump_path, |
+ size_t max_dump_size, |
+ const base::Closure& max_dump_size_reached_callback); |
+ |
+ virtual ~WebRtcRtpDumpWriter(); |
+ |
+ // Adds a RTP packet to the dump. The caller must make sure it's a valid RTP |
+ // packet. No validation is done by this method. |
+ virtual void WriteRtpPacket(const uint8* packet_header, |
+ size_t header_length, |
+ size_t packet_length, |
+ bool incoming); |
+ |
+ // Flushes the in-memory buffer to the disk and ends the dump. |
+ // |finished_callback| will be called to indicate whether there is any error |
+ // in compressing or writing the dump file. |
+ // If this object is destroyed before the operation is finished, the callback |
+ // will be canceled and the dump files will be deleted. |
+ virtual void EndDump(const base::Callback<void(bool)>& finished_callback); |
+ |
+ size_t max_dump_size() const { return max_dump_size_; } |
+ |
+ private: |
+ enum FlushResult { |
+ // Flushing has succeeded and the dump size is under the max limit. |
+ FLUSH_RESULT_SUCCESS, |
+ // Flushing has failed due to the max limit reached. |
+ FLUSH_RESULT_MAX_SIZE_REACHED, |
+ // Flushing has failed for other reasons. |
+ FLUSH_RESULT_FAILURE |
+ }; |
+ |
+ class FileThreadWorker; |
+ |
+ void FlushBuffers(bool incoming, |
+ bool outgoing, |
+ bool end_stream, |
+ const base::Callback<void(bool)>& callback); |
+ |
+ void OnFlushDone(const base::Callback<void(bool)>& callback, |
+ const scoped_ptr<FlushResult>& result); |
+ |
+ size_t max_dump_size_; |
+ base::Closure max_dump_size_reached_callback_; |
+ |
+ std::vector<uint8> incoming_buffer_; |
+ std::vector<uint8> outgoing_buffer_; |
+ base::TimeTicks start_time_; |
+ |
+ // This must be called and deleted on the FILE thread. |
+ scoped_ptr<FileThreadWorker> file_thread_worker_; |
+ |
+ base::ThreadChecker thread_checker_; |
+ |
+ base::WeakPtrFactory<WebRtcRtpDumpWriter> weak_ptr_factory_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(WebRtcRtpDumpWriter); |
+}; |
+ |
+#endif // CHROME_BROWSER_MEDIA_WEBRTC_RTP_DUMP_WRITER_H_ |