1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99

media / audio / aecdump_recording_manager.h [blame]

// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_AUDIO_AECDUMP_RECORDING_MANAGER_H_
#define MEDIA_AUDIO_AECDUMP_RECORDING_MANAGER_H_

#include <map>

#include "base/files/file.h"
#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "media/base/media_export.h"

namespace base {
class SingleThreadTaskRunner;
}

namespace media {

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 MEDIA_EXPORT AecdumpRecordingManager {
 public:
  using CreateFileCallback = base::RepeatingCallback<
      void(uint32_t id, base::OnceCallback<void(base::File)> reply_callback)>;

  explicit AecdumpRecordingManager(
      scoped_refptr<base::SingleThreadTaskRunner> 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. Overridden by tests.
  virtual void RegisterAecdumpSource(AecdumpRecordingSource* source);
  // Registers an aecdump recording source. If aecdump recording is currently
  // enabled, then StopAecdump will be called on the source. Overridden by
  // tests.
  virtual void DeregisterAecdumpSource(AecdumpRecordingSource* source);

  base::WeakPtr<AecdumpRecordingManager> AsWeakPtr() {
    return weak_factory_.GetWeakPtr();
  }

 private:
  // Forwards to StartRecording() if |manager| is valid, otherwise closes |file|
  // without blocking the thread.
  static void StartRecordingIfValidPointer(
      base::WeakPtr<AecdumpRecordingManager> 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<base::SingleThreadTaskRunner> task_runner_;

  // Recorders, one per source. Maps pointer to source id.
  std::map<AecdumpRecordingSource*, uint32_t> aecdump_recording_sources_;

  // Callback for creating aecdump files. When this is not null, debug
  // recording is enabled.
  CreateFileCallback create_file_callback_;

  // For managing debug recording cycles.
  base::WeakPtrFactory<AecdumpRecordingManager> weak_factory_{this};
};

}  // namespace media

#endif  // MEDIA_AUDIO_AECDUMP_RECORDING_MANAGER_H_