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 <stddef.h> | |
9 #include <stdint.h> | |
10 | |
11 #include <memory> | |
12 | |
13 #include "base/callback.h" | |
14 #include "base/files/file_path.h" | |
15 #include "base/macros.h" | |
16 #include "base/memory/weak_ptr.h" | |
17 #include "base/threading/thread_checker.h" | |
18 #include "base/time/time.h" | |
19 #include "chrome/browser/media/rtp_dump_type.h" | |
20 | |
21 // This class is responsible for creating the compressed RTP header dump file: | |
22 // - Adds the RTP headers to an in-memory buffer. | |
23 // - When the in-memory buffer is full, compresses it, and writes it to the | |
24 // disk. | |
25 // - Notifies the caller when the on-disk file size reaches the max limit. | |
26 // - The uncompressed dump follows the standard RTPPlay format | |
27 // (http://www.cs.columbia.edu/irt/software/rtptools/). | |
28 // - The caller is always responsible for cleaning up the dump file in all | |
29 // cases. | |
30 // - WebRtcRtpDumpWriter does not stop writing to the dump after the max size | |
31 // limit is reached. The caller must stop calling WriteRtpPacket instead. | |
32 // | |
33 // This object must run on the IO thread. | |
34 class WebRtcRtpDumpWriter { | |
35 public: | |
36 typedef base::Callback<void(bool incoming_succeeded, bool outgoing_succeeded)> | |
37 EndDumpCallback; | |
38 | |
39 // |incoming_dump_path| and |outgoing_dump_path| are the file paths of the | |
40 // compressed dump files for incoming and outgoing packets respectively. | |
41 // |max_dump_size| is the max size of the compressed dump file in bytes. | |
42 // |max_dump_size_reached_callback| will be called when the on-disk file size | |
43 // reaches |max_dump_size|. | |
44 WebRtcRtpDumpWriter(const base::FilePath& incoming_dump_path, | |
45 const base::FilePath& outgoing_dump_path, | |
46 size_t max_dump_size, | |
47 const base::Closure& max_dump_size_reached_callback); | |
48 | |
49 virtual ~WebRtcRtpDumpWriter(); | |
50 | |
51 // Adds a RTP packet to the dump. The caller must make sure it's a valid RTP | |
52 // packet. No validation is done by this method. | |
53 virtual void WriteRtpPacket(const uint8_t* packet_header, | |
54 size_t header_length, | |
55 size_t packet_length, | |
56 bool incoming); | |
57 | |
58 // Flushes the in-memory buffer to the disk and ends the dump. The caller must | |
59 // make sure the dump has not already been ended. | |
60 // |finished_callback| will be called to indicate whether the dump is valid. | |
61 // If this object is destroyed before the operation is finished, the callback | |
62 // will be canceled and the dump files will be deleted. | |
63 virtual void EndDump(RtpDumpType type, | |
64 const EndDumpCallback& finished_callback); | |
65 | |
66 size_t max_dump_size() const; | |
67 | |
68 private: | |
69 enum FlushResult { | |
70 // Flushing has succeeded and the dump size is under the max limit. | |
71 FLUSH_RESULT_SUCCESS, | |
72 // Nothing has been written to disk and the dump is empty. | |
73 FLUSH_RESULT_NO_DATA, | |
74 // Flushing has failed for other reasons. | |
75 FLUSH_RESULT_FAILURE | |
76 }; | |
77 | |
78 class FileThreadWorker; | |
79 | |
80 typedef base::Callback<void(bool)> FlushDoneCallback; | |
81 | |
82 // Used by EndDump to cache the input and intermediate results. | |
83 struct EndDumpContext { | |
84 EndDumpContext(RtpDumpType type, const EndDumpCallback& callback); | |
85 EndDumpContext(const EndDumpContext& other); | |
86 ~EndDumpContext(); | |
87 | |
88 RtpDumpType type; | |
89 bool incoming_succeeded; | |
90 bool outgoing_succeeded; | |
91 EndDumpCallback callback; | |
92 }; | |
93 | |
94 // Flushes the in-memory buffer to disk. If |incoming| is true, the incoming | |
95 // buffer will be flushed; otherwise, the outgoing buffer will be flushed. | |
96 // The dump file will be ended if |end_stream| is true. |callback| will be | |
97 // called when flushing is done. | |
98 void FlushBuffer(bool incoming, | |
99 bool end_stream, | |
100 const FlushDoneCallback& callback); | |
101 | |
102 // Called when FlushBuffer finishes. Checks the max dump size limit and | |
103 // maybe calls the |max_dump_size_reached_callback_|. Also calls |callback| | |
104 // with the flush result. | |
105 void OnFlushDone(const FlushDoneCallback& callback, | |
106 const std::unique_ptr<FlushResult>& result, | |
107 const std::unique_ptr<size_t>& bytes_written); | |
108 | |
109 // Called when one type of dump has been ended. It continues to end the other | |
110 // dump if needed based on |context| and |incoming|, or calls the callback in | |
111 // |context| if no more dump needs to be ended. | |
112 void OnDumpEnded(EndDumpContext context, bool incoming, bool success); | |
113 | |
114 // The max limit on the total size of incoming and outgoing dumps on disk. | |
115 const size_t max_dump_size_; | |
116 | |
117 // The callback to call when the max size limit is reached. | |
118 const base::Closure max_dump_size_reached_callback_; | |
119 | |
120 // The in-memory buffers for the uncompressed dumps. | |
121 std::vector<uint8_t> incoming_buffer_; | |
122 std::vector<uint8_t> outgoing_buffer_; | |
123 | |
124 // The time when the first packet is dumped. | |
125 base::TimeTicks start_time_; | |
126 | |
127 // The total on-disk size of the compressed incoming and outgoing dumps. | |
128 size_t total_dump_size_on_disk_; | |
129 | |
130 // File thread workers must be called and deleted on the FILE thread. | |
131 std::unique_ptr<FileThreadWorker> incoming_file_thread_worker_; | |
132 std::unique_ptr<FileThreadWorker> outgoing_file_thread_worker_; | |
133 | |
134 base::ThreadChecker thread_checker_; | |
135 | |
136 base::WeakPtrFactory<WebRtcRtpDumpWriter> weak_ptr_factory_; | |
137 | |
138 DISALLOW_COPY_AND_ASSIGN(WebRtcRtpDumpWriter); | |
139 }; | |
140 | |
141 #endif // CHROME_BROWSER_MEDIA_WEBRTC_RTP_DUMP_WRITER_H_ | |
OLD | NEW |