1 // Copyright 2017 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 
5 #ifndef MEDIA_MOJO_SERVICES_MOJO_AUDIO_OUTPUT_STREAM_PROVIDER_H_
6 #define MEDIA_MOJO_SERVICES_MOJO_AUDIO_OUTPUT_STREAM_PROVIDER_H_
7 
8 #include <memory>
9 #include <string>
10 
11 #include "base/sequence_checker.h"
12 #include "media/audio/audio_output_delegate.h"
13 #include "media/mojo/mojom/audio_output_stream.mojom.h"
14 #include "media/mojo/services/media_mojo_export.h"
15 #include "media/mojo/services/mojo_audio_output_stream.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"
20 
21 namespace media {
22 
23 // Provides a single AudioOutput, given the audio parameters to use.
24 class MEDIA_MOJO_EXPORT MojoAudioOutputStreamProvider
25     : public mojom::AudioOutputStreamProvider {
26  public:
27   using CreateDelegateCallback =
28       base::OnceCallback<std::unique_ptr<AudioOutputDelegate>(
29           const AudioParameters& params,
30           mojo::PendingRemote<mojom::AudioOutputStreamObserver>,
31           AudioOutputDelegate::EventHandler*)>;
32   using DeleterCallback = base::OnceCallback<void(AudioOutputStreamProvider*)>;
33 
34   // |create_delegate_callback| is used to obtain an AudioOutputDelegate for the
35   // AudioOutput when it's initialized and |deleter_callback| is called when
36   // this class should be removed (stream ended/error). |deleter_callback| is
37   // required to destroy |this| synchronously.
38   MojoAudioOutputStreamProvider(
39       mojo::PendingReceiver<mojom::AudioOutputStreamProvider> pending_receiver,
40       CreateDelegateCallback create_delegate_callback,
41       DeleterCallback deleter_callback,
42       std::unique_ptr<mojom::AudioOutputStreamObserver> observer);
43 
44   ~MojoAudioOutputStreamProvider() override;
45 
46  private:
47   // mojom::AudioOutputStreamProvider implementation.
48   void Acquire(const AudioParameters& params,
49                mojo::PendingRemote<mojom::AudioOutputStreamProviderClient>
50                    provider_client) override;
51 
52   // Called when |audio_output_| had an error.
53   void CleanUp(bool had_error);
54 
55   // Closes mojo connections, reports a bad message, and self-destructs.
56   void BadMessage(const std::string& error);
57 
58   SEQUENCE_CHECKER(sequence_checker_);
59 
60   mojo::Receiver<AudioOutputStreamProvider> receiver_;
61   CreateDelegateCallback create_delegate_callback_;
62   DeleterCallback deleter_callback_;
63   std::unique_ptr<mojom::AudioOutputStreamObserver> observer_;
64   mojo::Receiver<mojom::AudioOutputStreamObserver> observer_receiver_;
65   base::Optional<MojoAudioOutputStream> audio_output_;
66   mojo::Remote<mojom::AudioOutputStreamProviderClient> provider_client_;
67 
68   DISALLOW_COPY_AND_ASSIGN(MojoAudioOutputStreamProvider);
69 };
70 
71 }  // namespace media
72 
73 #endif  // MEDIA_MOJO_SERVICES_MOJO_AUDIO_OUTPUT_STREAM_PROVIDER_H_
74