OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_MEDIA_WEBRTC_RTP_DUMP_WRITER_H_ |
| 6 #define CHROME_BROWSER_MEDIA_WEBRTC_RTP_DUMP_WRITER_H_ |
| 7 |
| 8 #include "base/basictypes.h" |
| 9 #include "base/callback.h" |
| 10 #include "base/files/file_path.h" |
| 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/threading/thread_checker.h" |
| 13 #include "base/time/time.h" |
| 14 #include "chrome/browser/media/rtp_dump_type.h" |
| 15 |
| 16 // This class is responsible for creating the compressed RTP header dump file: |
| 17 // - Adds the RTP headers to an in-memory buffer. |
| 18 // - When the in-memory buffer is full, compresses it, and writes it to the |
| 19 // disk. Drop the packet and notifies the client if the max file size is |
| 20 // reached. |
| 21 // - The uncompressed dump follows the standard RTPPlay format |
| 22 // (http://www.cs.columbia.edu/irt/software/rtptools/). |
| 23 // - The caller is always responsible for cleaning up the dump file in all |
| 24 // cases. |
| 25 // - WebRtcRtpDumpWriter does not stop writing to the dump after the max size |
| 26 // limit is reached. The caller must stop calling WriteRtpPacket instead. |
| 27 // |
| 28 // This object must run on the IO thread. |
| 29 class WebRtcRtpDumpWriter { |
| 30 public: |
| 31 typedef base::Callback<void(bool incoming_succeeded, bool outgoing_succeeded)> |
| 32 EndDumpCallback; |
| 33 |
| 34 // Returns the header length of the RTP packet. |
| 35 static size_t GetRtpHeaderLen(const uint8* packet, size_t length); |
| 36 |
| 37 // |dump_path| is the file path of the compressed dump file. |max_dump_size| |
| 38 // is the max size of the compressed dump file in bytes. |
| 39 // |max_dump_size_reached_callback| will be called when the on-disk file size |
| 40 // reaches |max_dump_size|. |
| 41 WebRtcRtpDumpWriter(const base::FilePath& incoming_dump_path, |
| 42 const base::FilePath& outgoing_dump_path, |
| 43 size_t max_dump_size, |
| 44 const base::Closure& max_dump_size_reached_callback); |
| 45 |
| 46 virtual ~WebRtcRtpDumpWriter(); |
| 47 |
| 48 // Adds a RTP packet to the dump. The caller must make sure it's a valid RTP |
| 49 // packet. No validation is done by this method. |
| 50 virtual void WriteRtpPacket(const uint8* packet_header, |
| 51 size_t header_length, |
| 52 size_t packet_length, |
| 53 bool incoming); |
| 54 |
| 55 // Flushes the in-memory buffer to the disk and ends the dump. Ignored if the |
| 56 // dumps are already ended. |
| 57 // |finished_callback| will be called to indicate whether the dump is valid. |
| 58 // If this object is destroyed before the operation is finished, the callback |
| 59 // will be canceled and the dump files will be deleted. |
| 60 virtual void EndDump(RtpDumpType type, |
| 61 const EndDumpCallback& finished_callback); |
| 62 |
| 63 size_t max_dump_size() const; |
| 64 |
| 65 private: |
| 66 enum FlushResult { |
| 67 // Flushing has succeeded and the dump size is under the max limit. |
| 68 FLUSH_RESULT_SUCCESS, |
| 69 // Nothing has been written to disk and the dump is empty. |
| 70 FLUSH_RESULT_NO_DATA, |
| 71 // Flushing has failed for other reasons. |
| 72 FLUSH_RESULT_FAILURE |
| 73 }; |
| 74 |
| 75 class FileThreadWorker; |
| 76 |
| 77 typedef base::Callback<void(bool)> FlushDoneCallback; |
| 78 |
| 79 struct EndDumpContext { |
| 80 EndDumpContext(RtpDumpType type, const EndDumpCallback& callback); |
| 81 ~EndDumpContext(); |
| 82 |
| 83 RtpDumpType type; |
| 84 bool incoming_succeeded; |
| 85 bool outgoing_succeeded; |
| 86 EndDumpCallback callback; |
| 87 }; |
| 88 |
| 89 void FlushBuffer(bool incoming, |
| 90 bool end_stream, |
| 91 const FlushDoneCallback& callback); |
| 92 |
| 93 void OnFlushDone(const FlushDoneCallback& callback, |
| 94 const scoped_ptr<FlushResult>& result, |
| 95 const scoped_ptr<size_t>& bytes_written); |
| 96 |
| 97 void OnDumpEnded(EndDumpContext context, bool incoming, bool success); |
| 98 |
| 99 // The max limit on the total size of incoming and outgoing dumps on disk. |
| 100 const size_t max_dump_size_; |
| 101 |
| 102 // The callback to call when the max size limit is reached. |
| 103 const base::Closure max_dump_size_reached_callback_; |
| 104 |
| 105 // The in-memory buffers for the uncompressed dumps. |
| 106 std::vector<uint8> incoming_buffer_; |
| 107 std::vector<uint8> outgoing_buffer_; |
| 108 |
| 109 // The time when the first packet is dumped. |
| 110 base::TimeTicks start_time_; |
| 111 |
| 112 // The total on-disk size of the compressed incoming and outgoing dumps. |
| 113 size_t total_dump_size_on_disk_; |
| 114 |
| 115 // File thread workers must be called and deleted on the FILE thread. |
| 116 scoped_ptr<FileThreadWorker> incoming_file_thread_worker_; |
| 117 scoped_ptr<FileThreadWorker> outgoing_file_thread_worker_; |
| 118 |
| 119 base::ThreadChecker thread_checker_; |
| 120 |
| 121 base::WeakPtrFactory<WebRtcRtpDumpWriter> weak_ptr_factory_; |
| 122 |
| 123 DISALLOW_COPY_AND_ASSIGN(WebRtcRtpDumpWriter); |
| 124 }; |
| 125 |
| 126 #endif // CHROME_BROWSER_MEDIA_WEBRTC_RTP_DUMP_WRITER_H_ |
OLD | NEW |