fixup! [M120 Migration] Notify media device state to webbrowser
[platform/framework/web/chromium-efl.git] / components / capture_mode / audio_capturer.h
1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef COMPONENTS_CAPTURE_MODE_AUDIO_CAPTURER_H_
6 #define COMPONENTS_CAPTURE_MODE_AUDIO_CAPTURER_H_
7
8 #include <memory>
9
10 #include "base/memory/weak_ptr.h"
11 #include "base/sequence_checker.h"
12 #include "base/strings/string_piece_forward.h"
13 #include "base/time/time.h"
14 #include "components/capture_mode/capture_mode_export.h"
15 #include "media/base/audio_bus.h"
16 #include "media/base/audio_bus_pool.h"
17 #include "media/base/audio_capturer_source.h"
18 #include "media/base/audio_parameters.h"
19 #include "media/mojo/mojom/audio_stream_factory.mojom-forward.h"
20 #include "mojo/public/cpp/bindings/pending_remote.h"
21
22 namespace capture_mode {
23
24 // Defines the type of the callback that will be triggered repeatedly by the
25 // audio input device to deliver a stream of buffers containing the captured
26 // audio data. Each call will provide an `audio_bus` and the
27 // `audio_capture_time` when the first frame of that bus was captured.
28 // This callback will be invoked on a worker thread created by the audio input
29 // device (`media::AudioDeviceThread`). The provided `audio_bus` owns its own
30 // memory.
31 using OnAudioCapturedCallback =
32     base::RepeatingCallback<void(std::unique_ptr<media::AudioBus> audio_bus,
33                                  base::TimeTicks audio_capture_time)>;
34
35 // Defines an audio capturer that can capture an audio input device whose ID is
36 // `device_id`. The provided `audio_stream_factory` will be used so that the
37 // underlying `AudioInputDevice` can communicate with audio service via IPC. The
38 // provided `audio_params` will be used to initialize the underlying audio
39 // capturer. `callback` will be invoked according to the rules specified above.
40 class CAPTURE_MODE_EXPORT AudioCapturer
41     : public media::AudioCapturerSource::CaptureCallback {
42  public:
43   AudioCapturer(base::StringPiece device_id,
44                 mojo::PendingRemote<media::mojom::AudioStreamFactory>
45                     audio_stream_factory,
46                 const media::AudioParameters& audio_params,
47                 OnAudioCapturedCallback callback);
48   AudioCapturer(const AudioCapturer&) = delete;
49   AudioCapturer& operator=(const AudioCapturer&) = delete;
50   ~AudioCapturer() override;
51
52   // Starts and stops the audio capture.
53   void Start();
54   void Stop();
55
56   // media::AudioCapturerSource::CaptureCallback:
57   void OnCaptureStarted() override;
58   void Capture(const media::AudioBus* audio_source,
59                base::TimeTicks audio_capture_time,
60                double volume,
61                bool key_pressed) override;
62   void OnCaptureError(media::AudioCapturerSource::ErrorCode code,
63                       const std::string& message) override;
64   void OnCaptureMuted(bool is_muted) override;
65
66  private:
67   // Will be called when the audio bus that we send to the client via
68   // `on_audio_captured_callback_` (which in turn wraps the `backing_audio_bus`)
69   // is destroyed. The `backing_audio_bus` can then be inserted back into the
70   // `audio_bus_pool_`.
71   void OnAudioBusDone(std::unique_ptr<media::AudioBus> backing_audio_bus);
72
73   SEQUENCE_CHECKER(sequence_checker_);
74
75   scoped_refptr<media::AudioCapturerSource> audio_capturer_;
76
77   const OnAudioCapturedCallback on_audio_captured_callback_;
78
79   // An audio bus pool which we use to avoid allocating audio buses on the real-
80   // time audio thread when `Capture()` above is called.
81   media::AudioBusPoolImpl audio_bus_pool_;
82
83   // This will be initialized in the ctor as a weak ptr to `this`, and will be
84   // used to bind a callback to `OnAudioBusDone()`. The creation of this weak
85   // ptr, its invalidation, and the invocation of the callback bound to
86   // `OnAudioBusDone()` will all be done on the same sequence guarded by the
87   // above `sequence_checker_`.
88   base::WeakPtr<AudioCapturer> weak_ptr_this_;
89
90   base::WeakPtrFactory<AudioCapturer> weak_ptr_factory_{this};
91 };
92
93 }  // namespace capture_mode
94
95 #endif  // COMPONENTS_CAPTURE_MODE_AUDIO_CAPTURER_H_