OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_AUDIO_AUDIO_FILE_WRITER_H_ | 5 #ifndef MEDIA_AUDIO_AUDIO_DEBUG_FILE_WRITER_H_ |
6 #define MEDIA_AUDIO_AUDIO_FILE_WRITER_H_ | 6 #define MEDIA_AUDIO_AUDIO_DEBUG_FILE_WRITER_H_ |
7 | |
8 #include <stdint.h> | |
7 | 9 |
8 #include <memory> | 10 #include <memory> |
9 | 11 |
10 namespace base { | 12 #include "base/files/file.h" |
11 class FilePath; | 13 #include "base/macros.h" |
12 } | 14 #include "base/memory/ref_counted.h" |
15 #include "base/sequence_checker.h" | |
16 #include "base/single_thread_task_runner.h" | |
17 #include "media/base/audio_parameters.h" | |
18 #include "media/base/media_export.h" | |
13 | 19 |
14 namespace media { | 20 namespace media { |
15 | 21 |
16 class AudioBus; | 22 class AudioBus; |
17 | 23 |
18 // A writer interface used for writing audio data to file for debugging | 24 // Writes audio data used for debugging purposes. All operations are |
19 // purposes. | 25 // non-blocking. |
20 class AudioFileWriter { | 26 class MEDIA_EXPORT AudioDebugFileWriter { |
21 public: | 27 public: |
22 virtual ~AudioFileWriter() {} | 28 AudioDebugFileWriter( |
29 const media::AudioParameters& params, | |
o1ka
2017/02/21 12:07:44
nit: remove media prefix everywhere in media names
Henrik Grunell
2017/02/21 14:10:52
Missed that. Done.
| |
30 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner); | |
31 ~AudioDebugFileWriter(); | |
23 | 32 |
24 // Must be called before calling Write() for the first time after creation or | 33 // Must be called before calling Write() for the first time after creation or |
25 // Stop() call. Can be called on any sequence; Write() and Stop() must be | 34 // Stop() call. Can be called on any sequence; Write() and Stop() must be |
26 // called on the same sequence as Start(). | 35 // called on the same sequence as Start(). |
27 virtual void Start(const base::FilePath& file) = 0; | 36 void Start(const base::FilePath& file); |
28 | 37 |
29 // Must be called to finish recording. Each call to Start() requires a call to | 38 // Must be called to finish recording. Each call to Start() requires a call to |
30 // Stop(). Will be automatically called on destruction. | 39 // Stop(). Will be automatically called on destruction. |
31 virtual void Stop() = 0; | 40 void Stop(); |
32 | 41 |
33 // Write |data| to file. | 42 // Write |data| to file. |
34 virtual void Write(std::unique_ptr<AudioBus> data) = 0; | 43 void Write(std::unique_ptr<media::AudioBus> data); |
35 | 44 |
36 // Returns true if Write() call scheduled at this point will most likely write | 45 // Returns true if Write() call scheduled at this point will most likely write |
37 // data to the file, and false if it most likely will be a no-op. The result | 46 // data to the file, and false if it most likely will be a no-op. The result |
38 // may be ambigulous if Start() or Stop() is executed at the moment. Can be | 47 // may be ambigulous if Start() or Stop() is executed at the moment. Can be |
39 // called from any sequence. | 48 // called from any sequence. |
40 virtual bool WillWrite() = 0; | 49 bool WillWrite(); |
50 | |
51 private: | |
52 class AudioFileWriter; | |
53 struct DeleteOnFileThread; | |
o1ka
2017/02/21 12:07:45
Why do you need forward declarations if classes ar
Henrik Grunell
2017/02/21 14:10:52
Because of the
using AudioFileWriterUniquePtr ...
| |
54 | |
55 using AudioFileWriterUniquePtr = | |
56 std::unique_ptr<AudioFileWriter, DeleteOnFileThread>; | |
57 | |
58 // Manages the debug recording file and writes to it. Can be created on any | |
59 // thread. All the operations must be executed on |file_task_runner_|. Must be | |
60 // destroyed on |file_task_runner_|. | |
61 class AudioFileWriter { | |
o1ka
2017/02/21 12:07:44
Why is it needed in the header?
Henrik Grunell
2017/02/21 14:10:52
It was because of the deleter definition below (ca
| |
62 public: | |
63 static AudioFileWriterUniquePtr Create( | |
64 const base::FilePath& file_name, | |
65 const media::AudioParameters& params, | |
66 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner); | |
67 | |
68 ~AudioFileWriter(); | |
69 | |
70 // Write data from |data| to file. | |
71 void Write(const media::AudioBus* data); | |
72 | |
73 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner() { | |
74 return file_task_runner_; | |
75 } | |
76 | |
77 private: | |
78 AudioFileWriter( | |
79 const media::AudioParameters& params, | |
80 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner); | |
81 | |
82 // Write wave header to file. Called on the |file_task_runner_| twice: on | |
83 // construction | |
84 // of AudioFileWriter size of the wave data is unknown, so the header is | |
85 // written with zero sizes; then on destruction it is re-written with the | |
86 // actual size info accumulated throughout the object lifetime. | |
87 void WriteHeader(); | |
88 | |
89 void CreateRecordingFile(const base::FilePath& file_name); | |
90 | |
91 // The file to write to. | |
92 base::File file_; | |
93 | |
94 // Number of written samples. | |
95 uint64_t samples_; | |
96 | |
97 // Input audio parameters required to build wave header. | |
98 const media::AudioParameters params_; | |
99 | |
100 // Intermediate buffer to be written to file. Interleaved 16 bit audio data. | |
101 std::unique_ptr<int16_t[]> interleaved_data_; | |
102 int interleaved_data_size_; | |
103 | |
104 // The task runner this class operates on. | |
105 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; | |
o1ka
2017/02/21 12:07:45
just |task_runner_|?
Henrik Grunell
2017/02/21 14:10:52
Sounds good, done.
| |
106 }; | |
107 | |
108 // AudioFileWriter deleter. Inspired by | |
109 // content::BrowserThread::DeleteOnFileThread. | |
110 struct DeleteOnFileThread { | |
111 void operator()(AudioFileWriter* ptr) const { | |
112 if (!ptr->file_task_runner()->DeleteSoon(FROM_HERE, ptr)) { | |
o1ka
2017/02/21 12:07:45
Initialize struct with the task runner, instead of
o1ka
2017/02/21 12:07:45
Move implementation to cc?
Henrik Grunell
2017/02/21 14:10:52
I started writing this change but didn't have time
Henrik Grunell
2017/02/21 14:10:52
Ah, yes. Done. (The declaration needs to remain fo
Henrik Grunell
2017/02/22 10:47:42
Now done.
| |
113 #if defined(UNIT_TEST) | |
114 // Only logged under unit testing because leaks at shutdown | |
115 // are acceptable under normal circumstances. | |
116 LOG(ERROR) | |
117 << "DeleteSoon failed for AudioDebugFileWriter::AudioFileWriter"; | |
118 #endif | |
119 } | |
120 } | |
121 }; | |
122 | |
123 AudioFileWriterUniquePtr file_writer_; | |
124 const media::AudioParameters params_; | |
o1ka
2017/02/21 12:07:45
nit: media:: not needed
Henrik Grunell
2017/02/21 14:10:52
Done.
| |
125 base::SequenceChecker client_sequence_checker_; | |
126 | |
127 // The task runner to do file IO operations on. | |
o1ka
2017/02/21 12:07:44
just O operations, not I, right? :) we are only wr
Henrik Grunell
2017/02/21 14:10:52
:) Sure, but I'd claim IO isn't a literal term in
| |
128 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; | |
41 }; | 129 }; |
42 | 130 |
43 } // namespace media | 131 } // namspace media |
44 | 132 |
45 #endif // MEDIA_AUDIO_AUDIO_FILE_WRITER_H_ | 133 #endif // MEDIA_AUDIO_AUDIO_DEBUG_FILE_WRITER_H_ |
OLD | NEW |