audio_input_device.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright (c) 2012 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. // Low-latency audio capturing class utilizing audio input stream provided
  5. // by a server process by use of an IPC interface.
  6. //
  7. // Relationship of classes:
  8. //
  9. // AudioInputController AudioInputDevice
  10. // ^ ^
  11. // | |
  12. // v IPC v
  13. // MojoAudioInputStream <-----------> AudioInputIPC
  14. // ^ (MojoAudioInputIPC)
  15. // |
  16. // v
  17. // AudioInputDeviceManager
  18. //
  19. // Transportation of audio samples from the browser to the render process
  20. // is done by using shared memory in combination with a SyncSocket.
  21. // The AudioInputDevice user registers an AudioInputDevice::CaptureCallback by
  22. // calling Initialize(). The callback will be called with recorded audio from
  23. // the underlying audio layers.
  24. // The session ID is used by the RenderFrameAudioInputStreamFactory to start
  25. // the device referenced by this ID.
  26. //
  27. // State sequences:
  28. //
  29. // Start -> CreateStream ->
  30. // <- OnStreamCreated <-
  31. // -> RecordStream ->
  32. //
  33. // AudioInputDevice::Capture => low latency audio transport on audio thread =>
  34. //
  35. // Stop -> CloseStream -> Close
  36. //
  37. // This class depends on the audio transport thread. That thread is responsible
  38. // for calling the CaptureCallback and feeding it audio samples from the server
  39. // side audio layer using a socket and shared memory.
  40. //
  41. // Implementation notes:
  42. // - The user must call Stop() before deleting the class instance.
  43. #ifndef MEDIA_AUDIO_AUDIO_INPUT_DEVICE_H_
  44. #define MEDIA_AUDIO_AUDIO_INPUT_DEVICE_H_
  45. #include <memory>
  46. #include <string>
  47. #include "base/compiler_specific.h"
  48. #include "base/memory/raw_ptr.h"
  49. #include "base/memory/read_only_shared_memory_region.h"
  50. #include "base/sequence_checker.h"
  51. #include "base/threading/platform_thread.h"
  52. #include "media/audio/alive_checker.h"
  53. #include "media/audio/audio_device_thread.h"
  54. #include "media/audio/audio_input_ipc.h"
  55. #include "media/base/audio_capturer_source.h"
  56. #include "media/base/audio_parameters.h"
  57. #include "media/base/media_export.h"
  58. #include "third_party/abseil-cpp/absl/types/optional.h"
  59. namespace media {
  60. class MEDIA_EXPORT AudioInputDevice : public AudioCapturerSource,
  61. public AudioInputIPCDelegate {
  62. public:
  63. enum Purpose : int8_t { kUserInput, kLoopback };
  64. enum class DeadStreamDetection : bool { kDisabled = false, kEnabled = true };
  65. AudioInputDevice() = delete;
  66. // NOTE: Clients must call Initialize() before using.
  67. // |enable_uma| controls logging of UMA stats. It is used to ensure that
  68. // stats are not logged for mirroring service streams.
  69. // |detect_dead_stream| controls the dead stream detection.
  70. AudioInputDevice(std::unique_ptr<AudioInputIPC> ipc,
  71. Purpose purpose,
  72. DeadStreamDetection detect_dead_stream);
  73. AudioInputDevice(const AudioInputDevice&) = delete;
  74. AudioInputDevice& operator=(const AudioInputDevice&) = delete;
  75. // AudioCapturerSource implementation.
  76. void Initialize(const AudioParameters& params,
  77. CaptureCallback* callback) override;
  78. void Start() override;
  79. void Stop() override;
  80. void SetVolume(double volume) override;
  81. void SetAutomaticGainControl(bool enabled) override;
  82. void SetOutputDeviceForAec(const std::string& output_device_id) override;
  83. private:
  84. friend class base::RefCountedThreadSafe<AudioInputDevice>;
  85. // Our audio thread callback class. See source file for details.
  86. class AudioThreadCallback;
  87. // Note: The ordering of members in this enum is critical to correct behavior!
  88. enum State {
  89. IPC_CLOSED, // No more IPCs can take place.
  90. IDLE, // Not started.
  91. CREATING_STREAM, // Waiting for OnStreamCreated() to be called back.
  92. RECORDING, // Receiving audio data.
  93. };
  94. // This enum is used for UMA, so the only allowed operation on this definition
  95. // is to add new states to the bottom, update kMaxValue, and update the
  96. // histogram "Media.Audio.Capture.StreamCallbackError2".
  97. enum Error {
  98. kNoError = 0,
  99. kErrorDuringCreation = 1,
  100. kErrorDuringCapture = 2,
  101. kMaxValue = kErrorDuringCapture
  102. };
  103. ~AudioInputDevice() override;
  104. // AudioInputIPCDelegate implementation.
  105. void OnStreamCreated(base::ReadOnlySharedMemoryRegion shared_memory_region,
  106. base::SyncSocket::ScopedHandle socket_handle,
  107. bool initially_muted) override;
  108. void OnError(AudioCapturerSource::ErrorCode code) override;
  109. void OnMuted(bool is_muted) override;
  110. void OnIPCClosed() override;
  111. // This is called by |alive_checker_| if it detects that the input stream is
  112. // dead.
  113. void DetectedDeadInputStream();
  114. AudioParameters audio_parameters_;
  115. const base::ThreadType thread_type_;
  116. const bool enable_uma_;
  117. raw_ptr<CaptureCallback> callback_;
  118. // A pointer to the IPC layer that takes care of sending requests over to
  119. // the stream implementation. Only valid when state_ != IPC_CLOSED.
  120. std::unique_ptr<AudioInputIPC> ipc_;
  121. // Current state. See comments for State enum above.
  122. State state_;
  123. // For UMA stats. May only be accessed on the IO thread.
  124. Error had_error_ = kNoError;
  125. // Stores the Automatic Gain Control state. Default is false.
  126. bool agc_is_enabled_;
  127. // Controls the dead stream detection. Only the DSP hotword devices set this
  128. // to kDisabled to disable dead stream detection.
  129. const DeadStreamDetection detect_dead_stream_;
  130. // Checks regularly that the input stream is alive and notifies us if it
  131. // isn't by calling DetectedDeadInputStream(). Must outlive |audio_callback_|.
  132. std::unique_ptr<AliveChecker> alive_checker_;
  133. std::unique_ptr<AudioInputDevice::AudioThreadCallback> audio_callback_;
  134. std::unique_ptr<AudioDeviceThread> audio_thread_;
  135. SEQUENCE_CHECKER(sequence_checker_);
  136. // Cache the output device used for AEC in case it's called before the stream
  137. // is created.
  138. absl::optional<std::string> output_device_id_for_aec_;
  139. };
  140. } // namespace media
  141. #endif // MEDIA_AUDIO_AUDIO_INPUT_DEVICE_H_