1 // Copyright 2016 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_DECODER_SERVICE_H_
6 #define MEDIA_MOJO_SERVICES_MOJO_AUDIO_DECODER_SERVICE_H_
12 #include "base/memory/raw_ptr.h"
13 #include "base/memory/scoped_refptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "media/base/audio_decoder.h"
16 #include "media/base/cdm_context.h"
17 #include "media/base/status.h"
18 #include "media/mojo/mojom/audio_decoder.mojom.h"
19 #include "media/mojo/services/media_mojo_export.h"
20 #include "mojo/public/cpp/bindings/associated_remote.h"
21 #include "mojo/public/cpp/bindings/pending_associated_remote.h"
22 #include "third_party/abseil-cpp/absl/types/optional.h"
25 class SingleThreadTaskRunner;
30 class MojoCdmServiceContext;
31 class MojoDecoderBufferReader;
32 class MojoMediaClient;
34 class MEDIA_MOJO_EXPORT MojoAudioDecoderService final
35 : public mojom::AudioDecoder {
37 MojoAudioDecoderService(
38 MojoMediaClient* mojo_media_client,
39 MojoCdmServiceContext* mojo_cdm_service_context,
40 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
42 MojoAudioDecoderService(const MojoAudioDecoderService&) = delete;
43 MojoAudioDecoderService& operator=(const MojoAudioDecoderService&) = delete;
45 ~MojoAudioDecoderService() final;
47 // mojom::AudioDecoder implementation
49 mojo::PendingAssociatedRemote<mojom::AudioDecoderClient> client,
50 mojo::PendingRemote<mojom::MediaLog> media_log) final;
51 void Initialize(const AudioDecoderConfig& config,
52 const absl::optional<base::UnguessableToken>& cdm_id,
53 InitializeCallback callback) final;
55 void SetDataSource(mojo::ScopedDataPipeConsumerHandle receive_pipe) final;
57 void Decode(mojom::DecoderBufferPtr buffer, DecodeCallback callback) final;
59 void Reset(ResetCallback callback) final;
62 // Called by |decoder_| upon finishing initialization.
63 void OnInitialized(InitializeCallback callback, DecoderStatus status);
65 // Called by |mojo_decoder_buffer_reader_| when read is finished.
66 void OnReadDone(DecodeCallback callback, scoped_refptr<DecoderBuffer> buffer);
68 // Called by |mojo_decoder_buffer_reader_| when reset is finished.
69 void OnReaderFlushDone(ResetCallback callback);
71 // Called by |decoder_| when DecoderBuffer is accepted or rejected.
72 void OnDecodeStatus(DecodeCallback callback, DecoderStatus status);
74 // Called by |decoder_| when reset sequence is finished.
75 void OnResetDone(ResetCallback callback);
77 // Called by |decoder_| for each decoded buffer.
78 void OnAudioBufferReady(scoped_refptr<AudioBuffer> audio_buffer);
80 // Called by |decoder_| when it's waiting because of |reason|, e.g. waiting
81 // for decryption key.
82 void OnWaiting(WaitingReason reason);
84 std::unique_ptr<MojoDecoderBufferReader> mojo_decoder_buffer_reader_;
86 const raw_ptr<MojoMediaClient> mojo_media_client_;
88 // A helper object required to get CDM from CDM id.
89 const raw_ptr<MojoCdmServiceContext> mojo_cdm_service_context_ = nullptr;
91 // The destination for the decoded buffers.
92 mojo::AssociatedRemote<mojom::AudioDecoderClient> client_;
94 // The CDM ID and the corresponding CdmContextRef, which must be held to keep
95 // the CdmContext alive for the lifetime of the |decoder_|.
96 absl::optional<base::UnguessableToken> cdm_id_;
97 std::unique_ptr<CdmContextRef> cdm_context_ref_;
99 // The AudioDecoder that does actual decoding work.
100 // This MUST be declared after |cdm_| to maintain correct destruction order.
101 // The |decoder_| may need to access the CDM to do some clean up work in its
103 std::unique_ptr<media::AudioDecoder> decoder_;
105 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
107 base::WeakPtr<MojoAudioDecoderService> weak_this_;
108 base::WeakPtrFactory<MojoAudioDecoderService> weak_factory_{this};
113 #endif // MEDIA_MOJO_SERVICES_MOJO_AUDIO_DECODER_SERVICE_H_