File: audio_capturer.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (95 lines) | stat: -rw-r--r-- 4,041 bytes parent folder | download | duplicates (6)
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_CAPTURE_MODE_AUDIO_CAPTURER_H_
#define COMPONENTS_CAPTURE_MODE_AUDIO_CAPTURER_H_

#include <memory>
#include <string_view>

#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/time/time.h"
#include "components/capture_mode/capture_mode_export.h"
#include "media/base/audio_bus.h"
#include "media/base/audio_bus_pool.h"
#include "media/base/audio_capturer_source.h"
#include "media/base/audio_parameters.h"
#include "media/mojo/mojom/audio_stream_factory.mojom-forward.h"
#include "mojo/public/cpp/bindings/pending_remote.h"

namespace capture_mode {

// Defines the type of the callback that will be triggered repeatedly by the
// audio input device to deliver a stream of buffers containing the captured
// audio data. Each call will provide an `audio_bus` and the
// `audio_capture_time` when the first frame of that bus was captured.
// This callback will be invoked on a worker thread created by the audio input
// device (`media::AudioDeviceThread`). The provided `audio_bus` owns its own
// memory.
using OnAudioCapturedCallback =
    base::RepeatingCallback<void(std::unique_ptr<media::AudioBus> audio_bus,
                                 base::TimeTicks audio_capture_time)>;

// Defines an audio capturer that can capture an audio input device whose ID is
// `device_id`. The provided `audio_stream_factory` will be used so that the
// underlying `AudioInputDevice` can communicate with audio service via IPC. The
// provided `audio_params` will be used to initialize the underlying audio
// capturer. `callback` will be invoked according to the rules specified above.
class CAPTURE_MODE_EXPORT AudioCapturer
    : public media::AudioCapturerSource::CaptureCallback {
 public:
  AudioCapturer(std::string_view device_id,
                mojo::PendingRemote<media::mojom::AudioStreamFactory>
                    audio_stream_factory,
                const media::AudioParameters& audio_params,
                OnAudioCapturedCallback callback);
  AudioCapturer(const AudioCapturer&) = delete;
  AudioCapturer& operator=(const AudioCapturer&) = delete;
  ~AudioCapturer() override;

  // Starts and stops the audio capture.
  void Start();
  void Stop();

  // media::AudioCapturerSource::CaptureCallback:
  void OnCaptureStarted() override;
  void Capture(const media::AudioBus* audio_source,
               base::TimeTicks audio_capture_time,
               const media::AudioGlitchInfo& glitch_info,
               double volume) override;
  void OnCaptureError(media::AudioCapturerSource::ErrorCode code,
                      const std::string& message) override;
  void OnCaptureMuted(bool is_muted) override;

 private:
  // Will be called when the audio bus that we send to the client via
  // `on_audio_captured_callback_` (which in turn wraps the `backing_audio_bus`)
  // is destroyed. The `backing_audio_bus` can then be inserted back into the
  // `audio_bus_pool_`.
  void OnAudioBusDone(std::unique_ptr<media::AudioBus> backing_audio_bus);

  SEQUENCE_CHECKER(sequence_checker_);

  scoped_refptr<media::AudioCapturerSource> audio_capturer_;

  const OnAudioCapturedCallback on_audio_captured_callback_;

  // An audio bus pool which we use to avoid allocating audio buses on the real-
  // time audio thread when `Capture()` above is called.
  media::AudioBusPoolImpl audio_bus_pool_;

  // This will be initialized in the ctor as a weak ptr to `this`, and will be
  // used to bind a callback to `OnAudioBusDone()`. The creation of this weak
  // ptr, its invalidation, and the invocation of the callback bound to
  // `OnAudioBusDone()` will all be done on the same sequence guarded by the
  // above `sequence_checker_`.
  base::WeakPtr<AudioCapturer> weak_ptr_this_;

  base::WeakPtrFactory<AudioCapturer> weak_ptr_factory_{this};
};

}  // namespace capture_mode

#endif  // COMPONENTS_CAPTURE_MODE_AUDIO_CAPTURER_H_