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. | |
56 // |finished_callback| will be called to indicate whether the dump is valid. | |
57 // If this object is destroyed before the operation is finished, the callback | |
58 // will be canceled and the dump files will be deleted. | |
59 virtual void EndDump(RtpDumpType type, | |
60 const EndDumpCallback& finished_callback); | |
61 | |
62 size_t max_dump_size() const { return max_dump_size_; } | |
tommi (sloooow) - chröme
2014/05/17 10:18:37
missing dcheck for IO thread? Maybe this should j
jiayl
2014/05/19 17:32:59
Done.
| |
63 | |
64 private: | |
65 enum FlushResult { | |
66 // Flushing has succeeded and the dump size is under the max limit. | |
67 FLUSH_RESULT_SUCCESS, | |
68 // Nothing has been written to disk and the dump is empty. | |
69 FLUSH_RESULT_NO_DATA, | |
70 // Flushing has failed for other reasons. | |
71 FLUSH_RESULT_FAILURE | |
72 }; | |
73 | |
74 class FileThreadWorker; | |
75 | |
76 typedef base::Callback<void(bool)> FlushDoneCallback; | |
77 | |
78 struct EndDumpContext { | |
79 EndDumpContext(RtpDumpType type, const EndDumpCallback& callback); | |
80 ~EndDumpContext(); | |
81 | |
82 RtpDumpType type; | |
83 bool incoming_succeeded; | |
84 bool outgoing_succeeded; | |
85 EndDumpCallback callback; | |
86 }; | |
87 | |
88 void FlushBuffer(bool incoming, | |
89 bool end_stream, | |
90 const FlushDoneCallback& callback); | |
91 | |
92 void OnFlushDone(const FlushDoneCallback& callback, | |
93 const scoped_ptr<FlushResult>& result, | |
94 const scoped_ptr<size_t>& bytes_written); | |
95 | |
96 void OnDumpEnded(EndDumpContext context, bool incoming, bool success); | |
97 | |
98 // The max limit on the total size of incoming and outgoing dumps on disk. | |
99 const size_t max_dump_size_; | |
100 | |
101 // The callback to call when the max size limit is reached. | |
102 const base::Closure max_dump_size_reached_callback_; | |
103 | |
104 // The in-memory buffers for the uncompressed dumps. | |
105 std::vector<uint8> incoming_buffer_; | |
106 std::vector<uint8> outgoing_buffer_; | |
107 | |
108 // The time when the first packet is dumped. | |
109 base::TimeTicks start_time_; | |
110 | |
111 // The total on-disk size of the compressed incoming and outgoing dumps. | |
112 size_t total_dump_size_on_disk_; | |
113 | |
114 // File thread workers must be called and deleted on the FILE thread. | |
115 scoped_ptr<FileThreadWorker> incoming_file_thread_worker_; | |
116 scoped_ptr<FileThreadWorker> outgoing_file_thread_worker_; | |
117 | |
118 base::ThreadChecker thread_checker_; | |
119 | |
120 base::WeakPtrFactory<WebRtcRtpDumpWriter> weak_ptr_factory_; | |
121 | |
122 DISALLOW_COPY_AND_ASSIGN(WebRtcRtpDumpWriter); | |
123 }; | |
124 | |
125 #endif // CHROME_BROWSER_MEDIA_WEBRTC_RTP_DUMP_WRITER_H_ | |
OLD | NEW |