[M120 Migration][MM][CAPI] Fix the logic for media using capi player.
[platform/framework/web/chromium-efl.git] / media / mojo / services / mojo_audio_decoder_service.h
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.
4
5 #ifndef MEDIA_MOJO_SERVICES_MOJO_AUDIO_DECODER_SERVICE_H_
6 #define MEDIA_MOJO_SERVICES_MOJO_AUDIO_DECODER_SERVICE_H_
7
8 #include <stdint.h>
9
10 #include <memory>
11
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"
23
24 namespace base {
25 class SingleThreadTaskRunner;
26 }  // namespace base
27
28 namespace media {
29
30 class MojoCdmServiceContext;
31 class MojoDecoderBufferReader;
32 class MojoMediaClient;
33
34 class MEDIA_MOJO_EXPORT MojoAudioDecoderService final
35     : public mojom::AudioDecoder {
36  public:
37   MojoAudioDecoderService(
38       MojoMediaClient* mojo_media_client,
39       MojoCdmServiceContext* mojo_cdm_service_context,
40       scoped_refptr<base::SingleThreadTaskRunner> task_runner);
41
42   MojoAudioDecoderService(const MojoAudioDecoderService&) = delete;
43   MojoAudioDecoderService& operator=(const MojoAudioDecoderService&) = delete;
44
45   ~MojoAudioDecoderService() final;
46
47   // mojom::AudioDecoder implementation
48   void Construct(
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;
54
55   void SetDataSource(mojo::ScopedDataPipeConsumerHandle receive_pipe) final;
56
57   void Decode(mojom::DecoderBufferPtr buffer, DecodeCallback callback) final;
58
59   void Reset(ResetCallback callback) final;
60
61  private:
62   // Called by |decoder_| upon finishing initialization.
63   void OnInitialized(InitializeCallback callback, DecoderStatus status);
64
65   // Called by |mojo_decoder_buffer_reader_| when read is finished.
66   void OnReadDone(DecodeCallback callback, scoped_refptr<DecoderBuffer> buffer);
67
68   // Called by |mojo_decoder_buffer_reader_| when reset is finished.
69   void OnReaderFlushDone(ResetCallback callback);
70
71   // Called by |decoder_| when DecoderBuffer is accepted or rejected.
72   void OnDecodeStatus(DecodeCallback callback, DecoderStatus status);
73
74   // Called by |decoder_| when reset sequence is finished.
75   void OnResetDone(ResetCallback callback);
76
77   // Called by |decoder_| for each decoded buffer.
78   void OnAudioBufferReady(scoped_refptr<AudioBuffer> audio_buffer);
79
80   // Called by |decoder_| when it's waiting because of |reason|, e.g. waiting
81   // for decryption key.
82   void OnWaiting(WaitingReason reason);
83
84   std::unique_ptr<MojoDecoderBufferReader> mojo_decoder_buffer_reader_;
85
86   const raw_ptr<MojoMediaClient> mojo_media_client_;
87
88   // A helper object required to get CDM from CDM id.
89   const raw_ptr<MojoCdmServiceContext> mojo_cdm_service_context_ = nullptr;
90
91   // The destination for the decoded buffers.
92   mojo::AssociatedRemote<mojom::AudioDecoderClient> client_;
93
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_;
98
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
102   // own destructor.
103   std::unique_ptr<media::AudioDecoder> decoder_;
104
105   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
106
107   base::WeakPtr<MojoAudioDecoderService> weak_this_;
108   base::WeakPtrFactory<MojoAudioDecoderService> weak_factory_{this};
109 };
110
111 }  // namespace media
112
113 #endif  // MEDIA_MOJO_SERVICES_MOJO_AUDIO_DECODER_SERVICE_H_