1 // Copyright 2017 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.
5 #ifndef MEDIA_MOJO_SERVICES_MOJO_AUDIO_INPUT_STREAM_H_
6 #define MEDIA_MOJO_SERVICES_MOJO_AUDIO_INPUT_STREAM_H_
11 #include "base/sequence_checker.h"
12 #include "media/audio/audio_input_delegate.h"
13 #include "media/mojo/mojom/audio_data_pipe.mojom.h"
14 #include "media/mojo/mojom/audio_input_stream.mojom.h"
15 #include "media/mojo/services/media_mojo_export.h"
16 #include "mojo/public/cpp/bindings/pending_receiver.h"
17 #include "mojo/public/cpp/bindings/pending_remote.h"
18 #include "mojo/public/cpp/bindings/receiver.h"
19 #include "mojo/public/cpp/bindings/remote.h"
23 // This class handles IPC for single audio input stream by delegating method
24 // calls to its AudioInputDelegate.
25 class MEDIA_MOJO_EXPORT MojoAudioInputStream
26 : public mojom::AudioInputStream,
27 public AudioInputDelegate::EventHandler {
29 using StreamCreatedCallback =
30 base::OnceCallback<void(mojom::ReadOnlyAudioDataPipePtr, bool)>;
31 using CreateDelegateCallback =
32 base::OnceCallback<std::unique_ptr<AudioInputDelegate>(
33 AudioInputDelegate::EventHandler*)>;
35 // |create_delegate_callback| is used to obtain an AudioInputDelegate for the
36 // stream in the constructor. |stream_created_callback| is called when the
37 // stream has been initialized. |deleter_callback| is called when this class
38 // should be removed (stream ended/error). |deleter_callback| is required to
39 // destroy |this| synchronously.
41 mojo::PendingReceiver<mojom::AudioInputStream> receiver,
42 mojo::PendingRemote<mojom::AudioInputStreamClient> client,
43 CreateDelegateCallback create_delegate_callback,
44 StreamCreatedCallback stream_created_callback,
45 base::OnceClosure deleter_callback);
47 MojoAudioInputStream(const MojoAudioInputStream&) = delete;
48 MojoAudioInputStream& operator=(const MojoAudioInputStream&) = delete;
50 ~MojoAudioInputStream() override;
52 void SetOutputDeviceForAec(const std::string& raw_output_device_id);
55 // mojom::AudioInputStream implementation.
56 void Record() override;
57 void SetVolume(double volume) override;
59 // AudioInputDelegate::EventHandler implementation.
62 base::ReadOnlySharedMemoryRegion shared_memory_region,
63 std::unique_ptr<base::CancelableSyncSocket> foreign_socket,
64 bool initially_muted) override;
65 void OnStreamError(int stream_id) override;
67 // Closes connection to client and notifies owner.
70 SEQUENCE_CHECKER(sequence_checker_);
72 StreamCreatedCallback stream_created_callback_;
73 base::OnceClosure deleter_callback_;
74 mojo::Receiver<AudioInputStream> receiver_;
75 mojo::Remote<mojom::AudioInputStreamClient> client_;
76 std::unique_ptr<AudioInputDelegate> delegate_;
77 base::WeakPtrFactory<MojoAudioInputStream> weak_factory_{this};
82 #endif // MEDIA_MOJO_SERVICES_MOJO_AUDIO_INPUT_STREAM_H_