aecdump_recording_manager.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2022 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 SERVICES_AUDIO_AECDUMP_RECORDING_MANAGER_H_
  5. #define SERVICES_AUDIO_AECDUMP_RECORDING_MANAGER_H_
  6. #include <map>
  7. #include "base/callback.h"
  8. #include "base/files/file.h"
  9. #include "base/memory/weak_ptr.h"
  10. namespace base {
  11. class SingleThreadTaskRunner;
  12. }
  13. namespace audio {
  14. class AecdumpRecordingSource {
  15. public:
  16. // Starts recording an aecdump to |aecdump_file|. If a recording is already
  17. // ongoing, then that recording is stopped and and a new recording is started
  18. // to |aecdump_file|.
  19. virtual void StartAecdump(base::File aecdump_file) = 0;
  20. // Stops recording an aecdump and closes the file. Does nothing if no
  21. // recording is ongoing.
  22. virtual void StopAecdump() = 0;
  23. };
  24. // Manages diagnostic audio processing recordings (so-called aecdumps).
  25. // Aecdump recording sources implement the AecdumpRecordingSource interface and
  26. // register/deregister with the AecdumpRecordingManager.
  27. // All operations, including creation and destruction, must happen on the same
  28. // thread as the |task_runner| provided in the constructor.
  29. class AecdumpRecordingManager {
  30. public:
  31. using CreateFileCallback = base::RepeatingCallback<
  32. void(uint32_t id, base::OnceCallback<void(base::File)> reply_callback)>;
  33. explicit AecdumpRecordingManager(
  34. scoped_refptr<base::SingleThreadTaskRunner> task_runner);
  35. AecdumpRecordingManager(const AecdumpRecordingManager&) = delete;
  36. AecdumpRecordingManager& operator=(const AecdumpRecordingManager&) = delete;
  37. virtual ~AecdumpRecordingManager();
  38. // Starts and stops aecdump recording. Overridden by tests.
  39. virtual void EnableDebugRecording(CreateFileCallback create_file_callback);
  40. virtual void DisableDebugRecording();
  41. // Registers an aecdump recording source.
  42. void RegisterAecdumpSource(AecdumpRecordingSource* source);
  43. // Registers an aecdump recording source. If aecdump recording is currently
  44. // enabled, then StopAecdump will be called on the source.
  45. void DeregisterAecdumpSource(AecdumpRecordingSource* source);
  46. private:
  47. // Forwards to StartRecording() if |manager| is valid, otherwise closes |file|
  48. // without blocking the thread.
  49. static void StartRecordingIfValidPointer(
  50. base::WeakPtr<AecdumpRecordingManager> manager,
  51. AecdumpRecordingSource* source,
  52. base::File file);
  53. // Used as callback for |create_file_callback_|, to ensure the recording
  54. // source has not been deregistered during file creation.
  55. void StartRecording(AecdumpRecordingSource* source, base::File file);
  56. bool IsDebugRecordingEnabled() const;
  57. // Counter for recording source IDs.
  58. uint32_t recording_id_counter_ = 1;
  59. // The task runner this class lives on. Also used for file creation callbacks.
  60. scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  61. // Recorders, one per source. Maps pointer to source id.
  62. std::map<AecdumpRecordingSource*, uint32_t> aecdump_recording_sources_;
  63. // Callback for creating aecdump files. When this is not null, debug
  64. // recording is enabled.
  65. CreateFileCallback create_file_callback_;
  66. base::WeakPtrFactory<AecdumpRecordingManager> weak_factory_{this};
  67. };
  68. } // namespace audio
  69. #endif // SERVICES_AUDIO_AECDUMP_RECORDING_MANAGER_H_