audio_debug_file_writer.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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
  3. // found in the LICENSE file.
  4. #ifndef MEDIA_AUDIO_AUDIO_DEBUG_FILE_WRITER_H_
  5. #define MEDIA_AUDIO_AUDIO_DEBUG_FILE_WRITER_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include "base/files/file.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/sequence_checker.h"
  11. #include "base/task/sequenced_task_runner.h"
  12. #include "base/task/thread_pool.h"
  13. #include "media/base/audio_parameters.h"
  14. #include "media/base/media_export.h"
  15. namespace media {
  16. class AudioBus;
  17. // Writes audio data to a 16 bit PCM WAVE file used for debugging purposes. All
  18. // operations are non-blocking.
  19. // Functions are virtual for the purpose of test mocking.
  20. class MEDIA_EXPORT AudioDebugFileWriter {
  21. public:
  22. // Number of channels and sample rate are used from |params|, the other
  23. // parameters are ignored. The number of channels in the data passed to
  24. // Write() must match |params|.
  25. explicit AudioDebugFileWriter(const AudioParameters& params);
  26. AudioDebugFileWriter(const AudioDebugFileWriter&) = delete;
  27. AudioDebugFileWriter& operator=(const AudioDebugFileWriter&) = delete;
  28. virtual ~AudioDebugFileWriter();
  29. // Must be called before calling Write() for the first time after creation or
  30. // Stop() call. Can be called on any sequence; Write() and Stop() must be
  31. // called on the same sequence as Start().
  32. virtual void Start(base::File file);
  33. // Must be called to finish recording. Each call to Start() requires a call to
  34. // Stop(). Will be automatically called on destruction.
  35. virtual void Stop();
  36. // Write |data| to file.
  37. virtual void Write(std::unique_ptr<AudioBus> data);
  38. // Returns true if Write() call scheduled at this point will most likely write
  39. // data to the file, and false if it most likely will be a no-op. The result
  40. // may be ambigulous if Start() or Stop() is executed at the moment. Can be
  41. // called from any sequence.
  42. virtual bool WillWrite();
  43. protected:
  44. const AudioParameters params_;
  45. private:
  46. class AudioFileWriter;
  47. using AudioFileWriterUniquePtr =
  48. std::unique_ptr<AudioFileWriter, base::OnTaskRunnerDeleter>;
  49. // The task runner to do file output operations on.
  50. const scoped_refptr<base::SequencedTaskRunner> file_task_runner_ =
  51. base::ThreadPool::CreateSequencedTaskRunner(
  52. {base::MayBlock(), base::TaskPriority::BEST_EFFORT,
  53. base::TaskShutdownBehavior::BLOCK_SHUTDOWN});
  54. AudioFileWriterUniquePtr file_writer_;
  55. SEQUENCE_CHECKER(client_sequence_checker_);
  56. };
  57. } // namespace media
  58. #endif // MEDIA_AUDIO_AUDIO_DEBUG_FILE_WRITER_H_