| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_DEBUG_FILE_WRITER_H_ | 5 #ifndef MEDIA_AUDIO_AUDIO_DEBUG_FILE_WRITER_H_ |
| 6 #define MEDIA_AUDIO_AUDIO_DEBUG_FILE_WRITER_H_ | 6 #define MEDIA_AUDIO_AUDIO_DEBUG_FILE_WRITER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 | 11 |
| 12 #include "base/files/file.h" | 12 #include "base/files/file.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/sequence_checker.h" | 15 #include "base/sequence_checker.h" |
| 16 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
| 17 #include "media/base/audio_parameters.h" | 17 #include "media/base/audio_parameters.h" |
| 18 #include "media/base/media_export.h" | 18 #include "media/base/media_export.h" |
| 19 | 19 |
| 20 namespace media { | 20 namespace media { |
| 21 | 21 |
| 22 class AudioBus; | 22 class AudioBus; |
| 23 | 23 |
| 24 // Writes audio data used for debugging purposes. All operations are | 24 // Writes audio data to a 16 bit PCM WAVE file used for debugging purposes. All |
| 25 // non-blocking. | 25 // operations are non-blocking. |
| 26 // Functions are virtual for the purpose of test mocking. |
| 26 class MEDIA_EXPORT AudioDebugFileWriter { | 27 class MEDIA_EXPORT AudioDebugFileWriter { |
| 27 public: | 28 public: |
| 29 // Number of channels and sample rate are used from |params|, the other |
| 30 // parameters are ignored. The number of channels in the data passed to |
| 31 // Write() must match |params|. |
| 28 AudioDebugFileWriter( | 32 AudioDebugFileWriter( |
| 29 const AudioParameters& params, | 33 const AudioParameters& params, |
| 30 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner); | 34 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner); |
| 31 ~AudioDebugFileWriter(); | 35 |
| 36 virtual ~AudioDebugFileWriter(); |
| 32 | 37 |
| 33 // Must be called before calling Write() for the first time after creation or | 38 // Must be called before calling Write() for the first time after creation or |
| 34 // Stop() call. Can be called on any sequence; Write() and Stop() must be | 39 // Stop() call. Can be called on any sequence; Write() and Stop() must be |
| 35 // called on the same sequence as Start(). | 40 // called on the same sequence as Start(). |
| 36 void Start(const base::FilePath& file); | 41 virtual void Start(const base::FilePath& file); |
| 37 | 42 |
| 38 // Must be called to finish recording. Each call to Start() requires a call to | 43 // Must be called to finish recording. Each call to Start() requires a call to |
| 39 // Stop(). Will be automatically called on destruction. | 44 // Stop(). Will be automatically called on destruction. |
| 40 void Stop(); | 45 virtual void Stop(); |
| 41 | 46 |
| 42 // Write |data| to file. | 47 // Write |data| to file. |
| 43 void Write(std::unique_ptr<AudioBus> data); | 48 virtual void Write(std::unique_ptr<AudioBus> data); |
| 44 | 49 |
| 45 // Returns true if Write() call scheduled at this point will most likely write | 50 // Returns true if Write() call scheduled at this point will most likely write |
| 46 // data to the file, and false if it most likely will be a no-op. The result | 51 // data to the file, and false if it most likely will be a no-op. The result |
| 47 // may be ambigulous if Start() or Stop() is executed at the moment. Can be | 52 // may be ambigulous if Start() or Stop() is executed at the moment. Can be |
| 48 // called from any sequence. | 53 // called from any sequence. |
| 49 bool WillWrite(); | 54 virtual bool WillWrite(); |
| 55 |
| 56 // Gets the extension for the file type the as a string, for example "wav". |
| 57 // Can be called before calling Start() to add the appropriate extension to |
| 58 // the filename. |
| 59 virtual const base::FilePath::CharType* GetFileNameExtension(); |
| 60 |
| 61 protected: |
| 62 const AudioParameters params_; |
| 50 | 63 |
| 51 private: | 64 private: |
| 52 class AudioFileWriter; | 65 class AudioFileWriter; |
| 53 | 66 |
| 54 // Deleter for AudioFileWriter. | 67 // Deleter for AudioFileWriter. |
| 55 struct OnThreadDeleter { | 68 struct OnThreadDeleter { |
| 56 public: | 69 public: |
| 57 OnThreadDeleter(); | 70 OnThreadDeleter(); |
| 58 OnThreadDeleter(const OnThreadDeleter& other); | 71 OnThreadDeleter(const OnThreadDeleter& other); |
| 59 OnThreadDeleter(scoped_refptr<base::SingleThreadTaskRunner> task_runner); | 72 OnThreadDeleter(scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
| 60 ~OnThreadDeleter(); | 73 ~OnThreadDeleter(); |
| 61 void operator()(AudioFileWriter* ptr) const; | 74 void operator()(AudioFileWriter* ptr) const; |
| 62 | 75 |
| 63 private: | 76 private: |
| 64 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 77 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 65 }; | 78 }; |
| 66 | 79 |
| 67 using AudioFileWriterUniquePtr = | 80 using AudioFileWriterUniquePtr = |
| 68 std::unique_ptr<AudioFileWriter, OnThreadDeleter>; | 81 std::unique_ptr<AudioFileWriter, OnThreadDeleter>; |
| 69 | 82 |
| 70 AudioFileWriterUniquePtr file_writer_; | 83 AudioFileWriterUniquePtr file_writer_; |
| 71 const AudioParameters params_; | |
| 72 base::SequenceChecker client_sequence_checker_; | 84 base::SequenceChecker client_sequence_checker_; |
| 73 | 85 |
| 74 // The task runner to do file output operations on. | 86 // The task runner to do file output operations on. |
| 75 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; | 87 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; |
| 76 | 88 |
| 77 DISALLOW_COPY_AND_ASSIGN(AudioDebugFileWriter); | 89 DISALLOW_COPY_AND_ASSIGN(AudioDebugFileWriter); |
| 78 }; | 90 }; |
| 79 | 91 |
| 80 } // namspace media | 92 } // namspace media |
| 81 | 93 |
| 82 #endif // MEDIA_AUDIO_AUDIO_DEBUG_FILE_WRITER_H_ | 94 #endif // MEDIA_AUDIO_AUDIO_DEBUG_FILE_WRITER_H_ |
| OLD | NEW |