Hotword Audio Verification App: Adds to hotwordPrivate API
[chromium-blink-merge.git] / chrome / browser / media / webrtc_rtp_dump_writer.h
blob0901cc6b5fcf18e64a4e80ced88b719f6741dc79
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.
5 #ifndef CHROME_BROWSER_MEDIA_WEBRTC_RTP_DUMP_WRITER_H_
6 #define CHROME_BROWSER_MEDIA_WEBRTC_RTP_DUMP_WRITER_H_
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"
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.
20 // - Notifies the caller when the on-disk file size reaches the max limit.
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.
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;
34 // |incoming_dump_path| and |outgoing_dump_path| are the file paths of the
35 // compressed dump files for incoming and outgoing packets respectively.
36 // |max_dump_size| is the max size of the compressed dump file in bytes.
37 // |max_dump_size_reached_callback| will be called when the on-disk file size
38 // reaches |max_dump_size|.
39 WebRtcRtpDumpWriter(const base::FilePath& incoming_dump_path,
40 const base::FilePath& outgoing_dump_path,
41 size_t max_dump_size,
42 const base::Closure& max_dump_size_reached_callback);
44 virtual ~WebRtcRtpDumpWriter();
46 // Adds a RTP packet to the dump. The caller must make sure it's a valid RTP
47 // packet. No validation is done by this method.
48 virtual void WriteRtpPacket(const uint8* packet_header,
49 size_t header_length,
50 size_t packet_length,
51 bool incoming);
53 // Flushes the in-memory buffer to the disk and ends the dump. The caller must
54 // make sure the dump has not already been ended.
55 // |finished_callback| will be called to indicate whether the dump is valid.
56 // If this object is destroyed before the operation is finished, the callback
57 // will be canceled and the dump files will be deleted.
58 virtual void EndDump(RtpDumpType type,
59 const EndDumpCallback& finished_callback);
61 size_t max_dump_size() const;
63 private:
64 enum FlushResult {
65 // Flushing has succeeded and the dump size is under the max limit.
66 FLUSH_RESULT_SUCCESS,
67 // Nothing has been written to disk and the dump is empty.
68 FLUSH_RESULT_NO_DATA,
69 // Flushing has failed for other reasons.
70 FLUSH_RESULT_FAILURE
73 class FileThreadWorker;
75 typedef base::Callback<void(bool)> FlushDoneCallback;
77 // Used by EndDump to cache the input and intermediate results.
78 struct EndDumpContext {
79 EndDumpContext(RtpDumpType type, const EndDumpCallback& callback);
80 ~EndDumpContext();
82 RtpDumpType type;
83 bool incoming_succeeded;
84 bool outgoing_succeeded;
85 EndDumpCallback callback;
88 // Flushes the in-memory buffer to disk. If |incoming| is true, the incoming
89 // buffer will be flushed; otherwise, the outgoing buffer will be flushed.
90 // The dump file will be ended if |end_stream| is true. |callback| will be
91 // called when flushing is done.
92 void FlushBuffer(bool incoming,
93 bool end_stream,
94 const FlushDoneCallback& callback);
96 // Called when FlushBuffer finishes. Checks the max dump size limit and
97 // maybe calls the |max_dump_size_reached_callback_|. Also calls |callback|
98 // with the flush result.
99 void OnFlushDone(const FlushDoneCallback& callback,
100 const scoped_ptr<FlushResult>& result,
101 const scoped_ptr<size_t>& bytes_written);
103 // Called when one type of dump has been ended. It continues to end the other
104 // dump if needed based on |context| and |incoming|, or calls the callback in
105 // |context| if no more dump needs to be ended.
106 void OnDumpEnded(EndDumpContext context, bool incoming, bool success);
108 // The max limit on the total size of incoming and outgoing dumps on disk.
109 const size_t max_dump_size_;
111 // The callback to call when the max size limit is reached.
112 const base::Closure max_dump_size_reached_callback_;
114 // The in-memory buffers for the uncompressed dumps.
115 std::vector<uint8> incoming_buffer_;
116 std::vector<uint8> outgoing_buffer_;
118 // The time when the first packet is dumped.
119 base::TimeTicks start_time_;
121 // The total on-disk size of the compressed incoming and outgoing dumps.
122 size_t total_dump_size_on_disk_;
124 // File thread workers must be called and deleted on the FILE thread.
125 scoped_ptr<FileThreadWorker> incoming_file_thread_worker_;
126 scoped_ptr<FileThreadWorker> outgoing_file_thread_worker_;
128 base::ThreadChecker thread_checker_;
130 base::WeakPtrFactory<WebRtcRtpDumpWriter> weak_ptr_factory_;
132 DISALLOW_COPY_AND_ASSIGN(WebRtcRtpDumpWriter);
135 #endif // CHROME_BROWSER_MEDIA_WEBRTC_RTP_DUMP_WRITER_H_