1 // Copyright (c) 2012 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.
5 #include "media/filters/decrypting_audio_decoder.h"
10 #include "base/callback_helpers.h"
11 #include "base/location.h"
12 #include "base/logging.h"
13 #include "base/single_thread_task_runner.h"
14 #include "media/base/audio_buffer.h"
15 #include "media/base/audio_decoder_config.h"
16 #include "media/base/audio_timestamp_helper.h"
17 #include "media/base/bind_to_current_loop.h"
18 #include "media/base/buffers.h"
19 #include "media/base/decoder_buffer.h"
20 #include "media/base/decryptor.h"
21 #include "media/base/demuxer_stream.h"
22 #include "media/base/pipeline.h"
26 static inline bool IsOutOfSync(const base::TimeDelta
& timestamp_1
,
27 const base::TimeDelta
& timestamp_2
) {
28 // Out of sync of 100ms would be pretty noticeable and we should keep any
30 const int64 kOutOfSyncThresholdInMilliseconds
= 100;
31 return std::abs(timestamp_1
.InMilliseconds() - timestamp_2
.InMilliseconds()) >
32 kOutOfSyncThresholdInMilliseconds
;
35 DecryptingAudioDecoder::DecryptingAudioDecoder(
36 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
37 const SetDecryptorReadyCB
& set_decryptor_ready_cb
,
38 const base::Closure
& waiting_for_decryption_key_cb
)
39 : task_runner_(task_runner
),
40 state_(kUninitialized
),
41 waiting_for_decryption_key_cb_(waiting_for_decryption_key_cb
),
42 set_decryptor_ready_cb_(set_decryptor_ready_cb
),
44 key_added_while_decode_pending_(false),
48 std::string
DecryptingAudioDecoder::GetDisplayName() const {
49 return "DecryptingAudioDecoder";
52 void DecryptingAudioDecoder::Initialize(const AudioDecoderConfig
& config
,
53 const PipelineStatusCB
& status_cb
,
54 const OutputCB
& output_cb
) {
55 DVLOG(2) << "Initialize()";
56 DCHECK(task_runner_
->BelongsToCurrentThread());
57 DCHECK(decode_cb_
.is_null());
58 DCHECK(reset_cb_
.is_null());
60 weak_this_
= weak_factory_
.GetWeakPtr();
61 init_cb_
= BindToCurrentLoop(status_cb
);
62 output_cb_
= BindToCurrentLoop(output_cb
);
64 if (!config
.IsValidConfig()) {
65 DLOG(ERROR
) << "Invalid audio stream config.";
66 base::ResetAndReturn(&init_cb_
).Run(PIPELINE_ERROR_DECODE
);
70 // DecryptingAudioDecoder only accepts potentially encrypted stream.
71 if (!config
.is_encrypted()) {
72 base::ResetAndReturn(&init_cb_
).Run(DECODER_ERROR_NOT_SUPPORTED
);
78 if (state_
== kUninitialized
) {
79 state_
= kDecryptorRequested
;
80 set_decryptor_ready_cb_
.Run(BindToCurrentLoop(
81 base::Bind(&DecryptingAudioDecoder::SetDecryptor
, weak_this_
)));
85 // Reinitialization (i.e. upon a config change)
86 decryptor_
->DeinitializeDecoder(Decryptor::kAudio
);
90 void DecryptingAudioDecoder::Decode(const scoped_refptr
<DecoderBuffer
>& buffer
,
91 const DecodeCB
& decode_cb
) {
92 DVLOG(3) << "Decode()";
93 DCHECK(task_runner_
->BelongsToCurrentThread());
94 DCHECK(state_
== kIdle
|| state_
== kDecodeFinished
) << state_
;
95 DCHECK(!decode_cb
.is_null());
96 CHECK(decode_cb_
.is_null()) << "Overlapping decodes are not supported.";
98 decode_cb_
= BindToCurrentLoop(decode_cb
);
100 // Return empty (end-of-stream) frames if decoding has finished.
101 if (state_
== kDecodeFinished
) {
102 output_cb_
.Run(AudioBuffer::CreateEOSBuffer());
103 base::ResetAndReturn(&decode_cb_
).Run(kOk
);
107 // Initialize the |next_output_timestamp_| to be the timestamp of the first
109 if (timestamp_helper_
->base_timestamp() == kNoTimestamp() &&
110 !buffer
->end_of_stream()) {
111 timestamp_helper_
->SetBaseTimestamp(buffer
->timestamp());
114 pending_buffer_to_decode_
= buffer
;
115 state_
= kPendingDecode
;
116 DecodePendingBuffer();
119 void DecryptingAudioDecoder::Reset(const base::Closure
& closure
) {
120 DVLOG(2) << "Reset() - state: " << state_
;
121 DCHECK(task_runner_
->BelongsToCurrentThread());
122 DCHECK(state_
== kIdle
||
123 state_
== kPendingDecode
||
124 state_
== kWaitingForKey
||
125 state_
== kDecodeFinished
) << state_
;
126 DCHECK(init_cb_
.is_null()); // No Reset() during pending initialization.
127 DCHECK(reset_cb_
.is_null());
129 reset_cb_
= BindToCurrentLoop(closure
);
131 decryptor_
->ResetDecoder(Decryptor::kAudio
);
133 // Reset() cannot complete if the read callback is still pending.
134 // Defer the resetting process in this case. The |reset_cb_| will be fired
135 // after the read callback is fired - see DecryptAndDecodeBuffer() and
137 if (state_
== kPendingDecode
) {
138 DCHECK(!decode_cb_
.is_null());
142 if (state_
== kWaitingForKey
) {
143 DCHECK(!decode_cb_
.is_null());
144 pending_buffer_to_decode_
= NULL
;
145 base::ResetAndReturn(&decode_cb_
).Run(kAborted
);
148 DCHECK(decode_cb_
.is_null());
152 DecryptingAudioDecoder::~DecryptingAudioDecoder() {
153 DVLOG(2) << __FUNCTION__
;
154 DCHECK(task_runner_
->BelongsToCurrentThread());
156 if (state_
== kUninitialized
)
160 decryptor_
->DeinitializeDecoder(Decryptor::kAudio
);
163 if (!set_decryptor_ready_cb_
.is_null())
164 base::ResetAndReturn(&set_decryptor_ready_cb_
).Run(DecryptorReadyCB());
165 pending_buffer_to_decode_
= NULL
;
166 if (!init_cb_
.is_null())
167 base::ResetAndReturn(&init_cb_
).Run(DECODER_ERROR_NOT_SUPPORTED
);
168 if (!decode_cb_
.is_null())
169 base::ResetAndReturn(&decode_cb_
).Run(kAborted
);
170 if (!reset_cb_
.is_null())
171 base::ResetAndReturn(&reset_cb_
).Run();
174 void DecryptingAudioDecoder::SetDecryptor(
175 Decryptor
* decryptor
,
176 const DecryptorAttachedCB
& decryptor_attached_cb
) {
177 DVLOG(2) << "SetDecryptor()";
178 DCHECK(task_runner_
->BelongsToCurrentThread());
179 DCHECK_EQ(state_
, kDecryptorRequested
) << state_
;
180 DCHECK(!init_cb_
.is_null());
181 DCHECK(!set_decryptor_ready_cb_
.is_null());
183 set_decryptor_ready_cb_
.Reset();
186 base::ResetAndReturn(&init_cb_
).Run(DECODER_ERROR_NOT_SUPPORTED
);
188 decryptor_attached_cb
.Run(false);
192 decryptor_
= decryptor
;
195 decryptor_attached_cb
.Run(true);
198 void DecryptingAudioDecoder::InitializeDecoder() {
199 state_
= kPendingDecoderInit
;
200 decryptor_
->InitializeAudioDecoder(
202 BindToCurrentLoop(base::Bind(
203 &DecryptingAudioDecoder::FinishInitialization
, weak_this_
)));
206 void DecryptingAudioDecoder::FinishInitialization(bool success
) {
207 DVLOG(2) << "FinishInitialization()";
208 DCHECK(task_runner_
->BelongsToCurrentThread());
209 DCHECK(state_
== kPendingDecoderInit
) << state_
;
210 DCHECK(!init_cb_
.is_null());
211 DCHECK(reset_cb_
.is_null()); // No Reset() before initialization finished.
212 DCHECK(decode_cb_
.is_null()); // No Decode() before initialization finished.
215 base::ResetAndReturn(&init_cb_
).Run(DECODER_ERROR_NOT_SUPPORTED
);
222 timestamp_helper_
.reset(
223 new AudioTimestampHelper(config_
.samples_per_second()));
225 decryptor_
->RegisterNewKeyCB(
228 base::Bind(&DecryptingAudioDecoder::OnKeyAdded
, weak_this_
)));
231 base::ResetAndReturn(&init_cb_
).Run(PIPELINE_OK
);
234 void DecryptingAudioDecoder::DecodePendingBuffer() {
235 DCHECK(task_runner_
->BelongsToCurrentThread());
236 DCHECK_EQ(state_
, kPendingDecode
) << state_
;
239 if (!pending_buffer_to_decode_
->end_of_stream()) {
240 buffer_size
= pending_buffer_to_decode_
->data_size();
243 decryptor_
->DecryptAndDecodeAudio(
244 pending_buffer_to_decode_
,
245 BindToCurrentLoop(base::Bind(
246 &DecryptingAudioDecoder::DeliverFrame
, weak_this_
, buffer_size
)));
249 void DecryptingAudioDecoder::DeliverFrame(
251 Decryptor::Status status
,
252 const Decryptor::AudioFrames
& frames
) {
253 DVLOG(3) << "DeliverFrame() - status: " << status
;
254 DCHECK(task_runner_
->BelongsToCurrentThread());
255 DCHECK_EQ(state_
, kPendingDecode
) << state_
;
256 DCHECK(!decode_cb_
.is_null());
257 DCHECK(pending_buffer_to_decode_
.get());
259 bool need_to_try_again_if_nokey_is_returned
= key_added_while_decode_pending_
;
260 key_added_while_decode_pending_
= false;
262 scoped_refptr
<DecoderBuffer
> scoped_pending_buffer_to_decode
=
263 pending_buffer_to_decode_
;
264 pending_buffer_to_decode_
= NULL
;
266 if (!reset_cb_
.is_null()) {
267 base::ResetAndReturn(&decode_cb_
).Run(kAborted
);
272 DCHECK_EQ(status
== Decryptor::kSuccess
, !frames
.empty());
274 if (status
== Decryptor::kError
) {
275 DVLOG(2) << "DeliverFrame() - kError";
276 state_
= kDecodeFinished
; // TODO add kError state
277 base::ResetAndReturn(&decode_cb_
).Run(kDecodeError
);
281 if (status
== Decryptor::kNoKey
) {
282 DVLOG(2) << "DeliverFrame() - kNoKey";
283 // Set |pending_buffer_to_decode_| back as we need to try decoding the
284 // pending buffer again when new key is added to the decryptor.
285 pending_buffer_to_decode_
= scoped_pending_buffer_to_decode
;
287 if (need_to_try_again_if_nokey_is_returned
) {
288 // The |state_| is still kPendingDecode.
289 DecodePendingBuffer();
293 state_
= kWaitingForKey
;
294 waiting_for_decryption_key_cb_
.Run();
298 if (status
== Decryptor::kNeedMoreData
) {
299 DVLOG(2) << "DeliverFrame() - kNeedMoreData";
300 state_
= scoped_pending_buffer_to_decode
->end_of_stream() ? kDecodeFinished
302 base::ResetAndReturn(&decode_cb_
).Run(kOk
);
306 DCHECK_EQ(status
, Decryptor::kSuccess
);
307 DCHECK(!frames
.empty());
308 ProcessDecodedFrames(frames
);
310 if (scoped_pending_buffer_to_decode
->end_of_stream()) {
311 // Set |pending_buffer_to_decode_| back as we need to keep flushing the
312 // decryptor until kNeedMoreData is returned.
313 pending_buffer_to_decode_
= scoped_pending_buffer_to_decode
;
314 DecodePendingBuffer();
319 base::ResetAndReturn(&decode_cb_
).Run(kOk
);
322 void DecryptingAudioDecoder::OnKeyAdded() {
323 DCHECK(task_runner_
->BelongsToCurrentThread());
325 if (state_
== kPendingDecode
) {
326 key_added_while_decode_pending_
= true;
330 if (state_
== kWaitingForKey
) {
331 state_
= kPendingDecode
;
332 DecodePendingBuffer();
336 void DecryptingAudioDecoder::DoReset() {
337 DCHECK(init_cb_
.is_null());
338 DCHECK(decode_cb_
.is_null());
339 timestamp_helper_
->SetBaseTimestamp(kNoTimestamp());
341 base::ResetAndReturn(&reset_cb_
).Run();
344 void DecryptingAudioDecoder::ProcessDecodedFrames(
345 const Decryptor::AudioFrames
& frames
) {
346 for (Decryptor::AudioFrames::const_iterator iter
= frames
.begin();
347 iter
!= frames
.end();
349 scoped_refptr
<AudioBuffer
> frame
= *iter
;
351 DCHECK(!frame
->end_of_stream()) << "EOS frame returned.";
352 DCHECK_GT(frame
->frame_count(), 0) << "Empty frame returned.";
354 base::TimeDelta current_time
= timestamp_helper_
->GetTimestamp();
355 if (IsOutOfSync(current_time
, frame
->timestamp())) {
356 DVLOG(1) << "Timestamp returned by the decoder ("
357 << frame
->timestamp().InMilliseconds() << " ms)"
358 << " does not match the input timestamp and number of samples"
359 << " decoded (" << current_time
.InMilliseconds() << " ms).";
362 frame
->set_timestamp(current_time
);
363 timestamp_helper_
->AddFrames(frame
->frame_count());
365 output_cb_
.Run(frame
);