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.
5 #ifndef MEDIA_FILTERS_DECRYPTING_AUDIO_DECODER_H_
6 #define MEDIA_FILTERS_DECRYPTING_AUDIO_DECODER_H_
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"
21 class SequencedTaskRunner;
26 class AudioTimestampHelper;
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 {
37 DecryptingAudioDecoder(
38 const scoped_refptr<base::SequencedTaskRunner>& task_runner,
41 DecryptingAudioDecoder(const DecryptingAudioDecoder&) = delete;
42 DecryptingAudioDecoder& operator=(const DecryptingAudioDecoder&) = delete;
44 ~DecryptingAudioDecoder() override;
46 // Decoder implementation
47 bool SupportsDecryption() const override;
48 AudioDecoderType GetDecoderType() const override;
50 // AudioDecoder implementation.
51 void Initialize(const AudioDecoderConfig& config,
52 CdmContext* cdm_context,
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;
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
63 // TODO(xhwang): Update this diagram for DecryptingAudioDecoder.
74 // Initializes the audio decoder on the |decryptor_| with |config_|.
75 void InitializeDecoder();
77 // Callback for Decryptor::InitializeAudioDecoder() during initialization.
78 void FinishInitialization(bool success);
80 void DecodePendingBuffer();
82 // Callback for Decryptor::DecryptAndDecodeAudio().
83 void DeliverFrame(int buffer_size,
84 Decryptor::Status status,
85 const Decryptor::AudioFrames& frames);
87 // Callback for the CDM to notify |this|.
88 void OnCdmContextEvent(CdmContext::Event event);
90 // Resets decoder and calls |reset_cb_|.
93 // Sets timestamps for |frames| and then passes them to |output_cb_|.
94 void ProcessDecodedFrames(const Decryptor::AudioFrames& frames);
96 // Set in constructor.
97 scoped_refptr<base::SequencedTaskRunner> const task_runner_;
98 const raw_ptr<MediaLog> media_log_;
100 State state_ = kUninitialized;
105 base::OnceClosure reset_cb_;
106 WaitingCB waiting_cb_;
108 // The current decoder configuration.
109 AudioDecoderConfig config_;
111 raw_ptr<Decryptor> decryptor_ = nullptr;
113 // The buffer that needs decrypting/decoding.
114 scoped_refptr<media::DecoderBuffer> pending_buffer_to_decode_;
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
121 bool key_added_while_decode_pending_ = false;
123 std::unique_ptr<AudioTimestampHelper> timestamp_helper_;
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;
129 // To keep the CdmContext event callback registered.
130 std::unique_ptr<CallbackRegistration> event_cb_registration_;
132 base::WeakPtrFactory<DecryptingAudioDecoder> weak_factory_{this};
137 #endif // MEDIA_FILTERS_DECRYPTING_AUDIO_DECODER_H_