// Copyright 2022 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef SERVICES_AUDIO_AECDUMP_RECORDING_MANAGER_H_ #define SERVICES_AUDIO_AECDUMP_RECORDING_MANAGER_H_ #include #include "base/callback.h" #include "base/files/file.h" #include "base/memory/weak_ptr.h" namespace base { class SingleThreadTaskRunner; } namespace audio { class AecdumpRecordingSource { public: // Starts recording an aecdump to |aecdump_file|. If a recording is already // ongoing, then that recording is stopped and and a new recording is started // to |aecdump_file|. virtual void StartAecdump(base::File aecdump_file) = 0; // Stops recording an aecdump and closes the file. Does nothing if no // recording is ongoing. virtual void StopAecdump() = 0; }; // Manages diagnostic audio processing recordings (so-called aecdumps). // Aecdump recording sources implement the AecdumpRecordingSource interface and // register/deregister with the AecdumpRecordingManager. // All operations, including creation and destruction, must happen on the same // thread as the |task_runner| provided in the constructor. class AecdumpRecordingManager { public: using CreateFileCallback = base::RepeatingCallback< void(uint32_t id, base::OnceCallback reply_callback)>; explicit AecdumpRecordingManager( scoped_refptr task_runner); AecdumpRecordingManager(const AecdumpRecordingManager&) = delete; AecdumpRecordingManager& operator=(const AecdumpRecordingManager&) = delete; virtual ~AecdumpRecordingManager(); // Starts and stops aecdump recording. Overridden by tests. virtual void EnableDebugRecording(CreateFileCallback create_file_callback); virtual void DisableDebugRecording(); // Registers an aecdump recording source. void RegisterAecdumpSource(AecdumpRecordingSource* source); // Registers an aecdump recording source. If aecdump recording is currently // enabled, then StopAecdump will be called on the source. void DeregisterAecdumpSource(AecdumpRecordingSource* source); private: // Forwards to StartRecording() if |manager| is valid, otherwise closes |file| // without blocking the thread. static void StartRecordingIfValidPointer( base::WeakPtr manager, AecdumpRecordingSource* source, base::File file); // Used as callback for |create_file_callback_|, to ensure the recording // source has not been deregistered during file creation. void StartRecording(AecdumpRecordingSource* source, base::File file); bool IsDebugRecordingEnabled() const; // Counter for recording source IDs. uint32_t recording_id_counter_ = 1; // The task runner this class lives on. Also used for file creation callbacks. scoped_refptr task_runner_; // Recorders, one per source. Maps pointer to source id. std::map aecdump_recording_sources_; // Callback for creating aecdump files. When this is not null, debug // recording is enabled. CreateFileCallback create_file_callback_; base::WeakPtrFactory weak_factory_{this}; }; } // namespace audio #endif // SERVICES_AUDIO_AECDUMP_RECORDING_MANAGER_H_