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 |
| 15 // This class is responsible for creating the compressed RTP header dump file: |
| 16 // - Adds the RTP headers to an in-memory buffer. |
| 17 // - When the in-memory buffer is full, compresses it, and writes it to the |
| 18 // disk. Drop the packet and notifies the client if the max file size is |
| 19 // reached. |
| 20 // - The uncompressed dump follows the standard RTPPlay format |
| 21 // (http://www.cs.columbia.edu/irt/software/rtptools/). |
| 22 // - The caller is always responsible for cleaning up the dump file in all |
| 23 // cases. |
| 24 // - WebRtcRtpDumpWriter does not stop writing to the dump after the max size |
| 25 // limit is reached. The caller must stop calling WriteRtpPacket instead. |
| 26 // |
| 27 // This object must run on the IO thread. |
| 28 class WebRtcRtpDumpWriter { |
| 29 public: |
| 30 // Returns the header length of the RTP packet. |
| 31 static size_t GetRtpHeaderLen(const uint8* packet, size_t length); |
| 32 |
| 33 // |dump_path| is the file path of the compressed dump file. |max_dump_size| |
| 34 // is the max size of the compressed dump file in bytes. |
| 35 // |max_dump_size_reached_callback| will be called when the on-disk file size |
| 36 // reaches |max_dump_size|. |
| 37 WebRtcRtpDumpWriter(const base::FilePath& incoming_dump_path, |
| 38 const base::FilePath& outgoing_dump_path, |
| 39 size_t max_dump_size, |
| 40 const base::Closure& max_dump_size_reached_callback); |
| 41 |
| 42 virtual ~WebRtcRtpDumpWriter(); |
| 43 |
| 44 // Adds a RTP packet to the dump. The caller must make sure it's a valid RTP |
| 45 // packet. No validation is done by this method. |
| 46 virtual void WriteRtpPacket(const uint8* packet_header, |
| 47 size_t header_length, |
| 48 size_t packet_length, |
| 49 bool incoming); |
| 50 |
| 51 // Flushes the in-memory buffer to the disk and ends the dump. |
| 52 // |finished_callback| will be called to indicate whether there is any error |
| 53 // in compressing or writing the dump file. |
| 54 // If this object is destroyed before the operation is finished, the callback |
| 55 // will be canceled and the dump files will be deleted. |
| 56 virtual void EndDump(const base::Callback<void(bool)>& finished_callback); |
| 57 |
| 58 size_t max_dump_size() const { return max_dump_size_; } |
| 59 |
| 60 private: |
| 61 enum FlushResult { |
| 62 // Flushing has succeeded and the dump size is under the max limit. |
| 63 FLUSH_RESULT_SUCCESS, |
| 64 // Flushing has failed due to the max limit reached. |
| 65 FLUSH_RESULT_MAX_SIZE_REACHED, |
| 66 // Flushing has failed for other reasons. |
| 67 FLUSH_RESULT_FAILURE |
| 68 }; |
| 69 |
| 70 class FileThreadWorker; |
| 71 |
| 72 void FlushBuffers(bool incoming, |
| 73 bool outgoing, |
| 74 bool end_stream, |
| 75 const base::Callback<void(bool)>& callback); |
| 76 |
| 77 void OnFlushDone(const base::Callback<void(bool)>& callback, |
| 78 const scoped_ptr<FlushResult>& result); |
| 79 |
| 80 size_t max_dump_size_; |
| 81 base::Closure max_dump_size_reached_callback_; |
| 82 |
| 83 std::vector<uint8> incoming_buffer_; |
| 84 std::vector<uint8> outgoing_buffer_; |
| 85 base::TimeTicks start_time_; |
| 86 |
| 87 // This must be called and deleted on the FILE thread. |
| 88 scoped_ptr<FileThreadWorker> file_thread_worker_; |
| 89 |
| 90 base::ThreadChecker thread_checker_; |
| 91 |
| 92 base::WeakPtrFactory<WebRtcRtpDumpWriter> weak_ptr_factory_; |
| 93 |
| 94 DISALLOW_COPY_AND_ASSIGN(WebRtcRtpDumpWriter); |
| 95 }; |
| 96 |
| 97 #endif // CHROME_BROWSER_MEDIA_WEBRTC_RTP_DUMP_WRITER_H_ |
OLD | NEW |