Upload upstream chromium 108.0.5359.1
[platform/framework/web/chromium-efl.git] / media / filters / decrypting_audio_decoder.h
1 // Copyright 2012 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_FILTERS_DECRYPTING_AUDIO_DECODER_H_
6 #define MEDIA_FILTERS_DECRYPTING_AUDIO_DECODER_H_
7
8 #include <memory>
9
10 #include "base/callback.h"
11 #include "base/memory/raw_ptr.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "media/base/audio_decoder.h"
15 #include "media/base/callback_registry.h"
16 #include "media/base/cdm_context.h"
17 #include "media/base/decryptor.h"
18 #include "media/base/demuxer_stream.h"
19
20 namespace base {
21 class SequencedTaskRunner;
22 }
23
24 namespace media {
25
26 class AudioTimestampHelper;
27 class DecoderBuffer;
28 class Decryptor;
29 class MediaLog;
30
31 // Decryptor-based AudioDecoder implementation that can decrypt and decode
32 // encrypted audio buffers and return decrypted and decompressed audio frames.
33 // All public APIs and callbacks are trampolined to the |task_runner_| so
34 // that no locks are required for thread safety.
35 class MEDIA_EXPORT DecryptingAudioDecoder : public AudioDecoder {
36  public:
37   DecryptingAudioDecoder(
38       const scoped_refptr<base::SequencedTaskRunner>& task_runner,
39       MediaLog* media_log);
40
41   DecryptingAudioDecoder(const DecryptingAudioDecoder&) = delete;
42   DecryptingAudioDecoder& operator=(const DecryptingAudioDecoder&) = delete;
43
44   ~DecryptingAudioDecoder() override;
45
46   // Decoder implementation
47   bool SupportsDecryption() const override;
48   AudioDecoderType GetDecoderType() const override;
49
50   // AudioDecoder implementation.
51   void Initialize(const AudioDecoderConfig& config,
52                   CdmContext* cdm_context,
53                   InitCB init_cb,
54                   const OutputCB& output_cb,
55                   const WaitingCB& waiting_cb) override;
56   void Decode(scoped_refptr<DecoderBuffer> buffer, DecodeCB decode_cb) override;
57   void Reset(base::OnceClosure closure) override;
58
59  private:
60   // For a detailed state diagram please see this link: http://goo.gl/8jAok
61   // TODO(xhwang): Add a ASCII state diagram in this file after this class
62   // stabilizes.
63   // TODO(xhwang): Update this diagram for DecryptingAudioDecoder.
64   enum State {
65     kUninitialized = 0,
66     kPendingDecoderInit,
67     kIdle,
68     kPendingDecode,
69     kWaitingForKey,
70     kDecodeFinished,
71     kError
72   };
73
74   // Initializes the audio decoder on the |decryptor_| with |config_|.
75   void InitializeDecoder();
76
77   // Callback for Decryptor::InitializeAudioDecoder() during initialization.
78   void FinishInitialization(bool success);
79
80   void DecodePendingBuffer();
81
82   // Callback for Decryptor::DecryptAndDecodeAudio().
83   void DeliverFrame(int buffer_size,
84                     Decryptor::Status status,
85                     const Decryptor::AudioFrames& frames);
86
87   // Callback for the CDM to notify |this|.
88   void OnCdmContextEvent(CdmContext::Event event);
89
90   // Resets decoder and calls |reset_cb_|.
91   void DoReset();
92
93   // Sets timestamps for |frames| and then passes them to |output_cb_|.
94   void ProcessDecodedFrames(const Decryptor::AudioFrames& frames);
95
96   // Set in constructor.
97   scoped_refptr<base::SequencedTaskRunner> const task_runner_;
98   const raw_ptr<MediaLog> media_log_;
99
100   State state_ = kUninitialized;
101
102   InitCB init_cb_;
103   OutputCB output_cb_;
104   DecodeCB decode_cb_;
105   base::OnceClosure reset_cb_;
106   WaitingCB waiting_cb_;
107
108   // The current decoder configuration.
109   AudioDecoderConfig config_;
110
111   raw_ptr<Decryptor> decryptor_ = nullptr;
112
113   // The buffer that needs decrypting/decoding.
114   scoped_refptr<media::DecoderBuffer> pending_buffer_to_decode_;
115
116   // Indicates the situation where new key is added during pending decode
117   // (in other words, this variable can only be set in state kPendingDecode).
118   // If this variable is true and kNoKey is returned then we need to try
119   // decrypting/decoding again in case the newly added key is the correct
120   // decryption key.
121   bool key_added_while_decode_pending_ = false;
122
123   std::unique_ptr<AudioTimestampHelper> timestamp_helper_;
124
125   // Once Initialized() with encrypted content support, if the stream changes to
126   // clear content, we want to ensure this decoder remains used.
127   bool support_clear_content_ = false;
128
129   // To keep the CdmContext event callback registered.
130   std::unique_ptr<CallbackRegistration> event_cb_registration_;
131
132   base::WeakPtrFactory<DecryptingAudioDecoder> weak_factory_{this};
133 };
134
135 }  // namespace media
136
137 #endif  // MEDIA_FILTERS_DECRYPTING_AUDIO_DECODER_H_