OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef MEDIA_AUDIO_AUDIO_DEBUG_RECORDING_HELPER_H_ |
| 6 #define MEDIA_AUDIO_AUDIO_DEBUG_RECORDING_HELPER_H_ |
| 7 |
| 8 #include <memory> |
| 9 |
| 10 #include "base/atomicops.h" |
| 11 #include "base/callback.h" |
| 12 #include "base/gtest_prod_util.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/threading/thread_checker.h" |
| 15 #include "media/audio/audio_debug_file_writer.h" |
| 16 #include "media/base/audio_parameters.h" |
| 17 #include "media/base/media_export.h" |
| 18 |
| 19 namespace base { |
| 20 class FilePath; |
| 21 class SingleThreadTaskRunner; |
| 22 } |
| 23 |
| 24 namespace media { |
| 25 |
| 26 class AudioBus; |
| 27 |
| 28 // Interface for feeding data to a recorder. |
| 29 class AudioDebugRecorder { |
| 30 public: |
| 31 virtual ~AudioDebugRecorder() {} |
| 32 |
| 33 // If debug recording is enabled, copies audio data and makes sure it's |
| 34 // written on the right thread. Otherwise ignores the data. Can be called on |
| 35 // any thread. |
| 36 virtual void OnData(const AudioBus* source) = 0; |
| 37 }; |
| 38 |
| 39 // A helper class for those who want to use AudioDebugFileWriter. It handles |
| 40 // copying AudioBus data, thread jump (OnData() can be called on any |
| 41 // thread), and creating and deleting the AudioDebugFileWriter at enable and |
| 42 // disable. All functions except OnData() must be called on the thread |
| 43 // |task_runner| belongs to. |
| 44 // TODO(grunell): When input debug recording is moved to AudioManager, it should |
| 45 // be possible to merge this class into AudioDebugFileWriter. One thread jump |
| 46 // could be skipped then. Currently we have |
| 47 // soundcard thread -> control thread -> file thread, |
| 48 // and with the merge we should be able to do |
| 49 // soundcard thread -> file thread. |
| 50 class MEDIA_EXPORT AudioDebugRecordingHelper |
| 51 : public NON_EXPORTED_BASE(AudioDebugRecorder) { |
| 52 public: |
| 53 AudioDebugRecordingHelper( |
| 54 const AudioParameters& params, |
| 55 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 56 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, |
| 57 base::OnceClosure on_destruction_closure); |
| 58 ~AudioDebugRecordingHelper() override; |
| 59 |
| 60 // Enable debug recording. The create callback is first run to create an |
| 61 // AudioDebugFileWriter. |
| 62 virtual void EnableDebugRecording(const base::FilePath& file_name); |
| 63 |
| 64 // Disable debug recording. The AudioDebugFileWriter is destroyed. |
| 65 virtual void DisableDebugRecording(); |
| 66 |
| 67 // AudioDebugRecorder implementation. Can be called on any thread. |
| 68 void OnData(const AudioBus* source) override; |
| 69 |
| 70 private: |
| 71 FRIEND_TEST_ALL_PREFIXES(AudioDebugRecordingHelperTest, EnableDisable); |
| 72 FRIEND_TEST_ALL_PREFIXES(AudioDebugRecordingHelperTest, OnData); |
| 73 |
| 74 // Writes debug data to |debug_writer_|. |
| 75 void DoWrite(std::unique_ptr<media::AudioBus> data); |
| 76 |
| 77 // Creates an AudioDebugFileWriter. Overridden by test. |
| 78 virtual std::unique_ptr<AudioDebugFileWriter> CreateAudioDebugFileWriter( |
| 79 const AudioParameters& params, |
| 80 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner); |
| 81 |
| 82 const AudioParameters params_; |
| 83 std::unique_ptr<AudioDebugFileWriter> debug_writer_; |
| 84 |
| 85 // Used as a flag to indicate if recording is enabled, accessed on different |
| 86 // threads. |
| 87 base::subtle::Atomic32 recording_enabled_; |
| 88 |
| 89 // The task runner for accessing |debug_writer_|. |
| 90 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 91 |
| 92 // Task runner passed to |debug_writer_| to do file output operations on. |
| 93 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; |
| 94 |
| 95 // Runs in destructor if set. |
| 96 base::OnceClosure on_destruction_closure_; |
| 97 |
| 98 base::WeakPtrFactory<AudioDebugRecordingHelper> weak_factory_; |
| 99 DISALLOW_COPY_AND_ASSIGN(AudioDebugRecordingHelper); |
| 100 }; |
| 101 |
| 102 } // namespace media |
| 103 |
| 104 #endif // MEDIA_AUDIO_AUDIO_DEBUG_RECORDING_HELPER_H_ |
OLD | NEW |