OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/filters/decrypting_audio_decoder.h" | 5 #include "media/filters/decrypting_audio_decoder.h" |
6 | 6 |
7 #include <cstdlib> | 7 #include <cstdlib> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 return std::abs(timestamp_1.InMilliseconds() - timestamp_2.InMilliseconds()) > | 33 return std::abs(timestamp_1.InMilliseconds() - timestamp_2.InMilliseconds()) > |
34 kOutOfSyncThresholdInMilliseconds; | 34 kOutOfSyncThresholdInMilliseconds; |
35 } | 35 } |
36 | 36 |
37 DecryptingAudioDecoder::DecryptingAudioDecoder( | 37 DecryptingAudioDecoder::DecryptingAudioDecoder( |
38 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 38 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
39 const SetDecryptorReadyCB& set_decryptor_ready_cb) | 39 const SetDecryptorReadyCB& set_decryptor_ready_cb) |
40 : task_runner_(task_runner), | 40 : task_runner_(task_runner), |
41 weak_factory_(this), | 41 weak_factory_(this), |
42 state_(kUninitialized), | 42 state_(kUninitialized), |
43 demuxer_stream_(NULL), | |
44 set_decryptor_ready_cb_(set_decryptor_ready_cb), | 43 set_decryptor_ready_cb_(set_decryptor_ready_cb), |
45 decryptor_(NULL), | 44 decryptor_(NULL), |
46 key_added_while_decode_pending_(false), | 45 key_added_while_decode_pending_(false), |
47 bits_per_channel_(0), | 46 bits_per_channel_(0), |
48 channel_layout_(CHANNEL_LAYOUT_NONE), | 47 channel_layout_(CHANNEL_LAYOUT_NONE), |
49 samples_per_second_(0) { | 48 samples_per_second_(0) { |
50 } | 49 } |
51 | 50 |
52 void DecryptingAudioDecoder::Initialize( | 51 void DecryptingAudioDecoder::Initialize(const AudioDecoderConfig& config, |
53 DemuxerStream* stream, | 52 const PipelineStatusCB& status_cb) { |
54 const PipelineStatusCB& status_cb, | |
55 const StatisticsCB& statistics_cb) { | |
56 DVLOG(2) << "Initialize()"; | 53 DVLOG(2) << "Initialize()"; |
57 DCHECK(task_runner_->BelongsToCurrentThread()); | 54 DCHECK(task_runner_->BelongsToCurrentThread()); |
58 DCHECK_EQ(state_, kUninitialized) << state_; | 55 DCHECK_EQ(state_, kUninitialized) << state_; |
59 DCHECK(stream); | |
60 | 56 |
61 weak_this_ = weak_factory_.GetWeakPtr(); | 57 weak_this_ = weak_factory_.GetWeakPtr(); |
62 init_cb_ = BindToCurrentLoop(status_cb); | 58 init_cb_ = BindToCurrentLoop(status_cb); |
63 | 59 |
64 const AudioDecoderConfig& config = stream->audio_decoder_config(); | |
65 if (!config.IsValidConfig()) { | 60 if (!config.IsValidConfig()) { |
66 DLOG(ERROR) << "Invalid audio stream config."; | 61 DLOG(ERROR) << "Invalid audio stream config."; |
67 base::ResetAndReturn(&init_cb_).Run(PIPELINE_ERROR_DECODE); | 62 base::ResetAndReturn(&init_cb_).Run(PIPELINE_ERROR_DECODE); |
68 return; | 63 return; |
69 } | 64 } |
70 | 65 |
71 // DecryptingAudioDecoder only accepts potentially encrypted stream. | 66 // DecryptingAudioDecoder only accepts potentially encrypted stream. |
72 if (!config.is_encrypted()) { | 67 if (!config.is_encrypted()) { |
73 base::ResetAndReturn(&init_cb_).Run(DECODER_ERROR_NOT_SUPPORTED); | 68 base::ResetAndReturn(&init_cb_).Run(DECODER_ERROR_NOT_SUPPORTED); |
74 return; | 69 return; |
75 } | 70 } |
76 | 71 |
77 DCHECK(!demuxer_stream_); | 72 config_ = config; |
78 demuxer_stream_ = stream; | |
79 statistics_cb_ = statistics_cb; | |
80 | 73 |
81 state_ = kDecryptorRequested; | 74 state_ = kDecryptorRequested; |
82 set_decryptor_ready_cb_.Run(BindToCurrentLoop( | 75 set_decryptor_ready_cb_.Run(BindToCurrentLoop( |
83 base::Bind(&DecryptingAudioDecoder::SetDecryptor, weak_this_))); | 76 base::Bind(&DecryptingAudioDecoder::SetDecryptor, weak_this_))); |
84 } | 77 } |
85 | 78 |
86 void DecryptingAudioDecoder::Read(const ReadCB& read_cb) { | 79 void DecryptingAudioDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer, |
87 DVLOG(3) << "Read()"; | 80 const DecodeCB& decode_cb) { |
| 81 DVLOG(3) << "Decode()"; |
88 DCHECK(task_runner_->BelongsToCurrentThread()); | 82 DCHECK(task_runner_->BelongsToCurrentThread()); |
89 DCHECK(state_ == kIdle || state_ == kDecodeFinished) << state_; | 83 DCHECK(state_ == kIdle || state_ == kDecodeFinished) << state_; |
90 DCHECK(!read_cb.is_null()); | 84 DCHECK(!decode_cb.is_null()); |
91 CHECK(read_cb_.is_null()) << "Overlapping decodes are not supported."; | 85 CHECK(decode_cb_.is_null()) << "Overlapping decodes are not supported."; |
92 | 86 |
93 read_cb_ = BindToCurrentLoop(read_cb); | 87 decode_cb_ = BindToCurrentLoop(decode_cb); |
94 | 88 |
95 // Return empty (end-of-stream) frames if decoding has finished. | 89 // Return empty (end-of-stream) frames if decoding has finished. |
96 if (state_ == kDecodeFinished) { | 90 if (state_ == kDecodeFinished) { |
97 base::ResetAndReturn(&read_cb_).Run(kOk, AudioBuffer::CreateEOSBuffer()); | 91 base::ResetAndReturn(&decode_cb_).Run(kOk, AudioBuffer::CreateEOSBuffer()); |
98 return; | 92 return; |
99 } | 93 } |
100 | 94 |
101 if (!queued_audio_frames_.empty()) { | 95 if (!queued_audio_frames_.empty()) { |
102 base::ResetAndReturn(&read_cb_).Run(kOk, queued_audio_frames_.front()); | 96 DCHECK(!buffer); |
| 97 base::ResetAndReturn(&decode_cb_).Run(kOk, queued_audio_frames_.front()); |
103 queued_audio_frames_.pop_front(); | 98 queued_audio_frames_.pop_front(); |
104 return; | 99 return; |
105 } | 100 } |
106 | 101 |
107 state_ = kPendingDemuxerRead; | 102 pending_buffer_to_decode_ = buffer; |
108 ReadFromDemuxerStream(); | 103 state_ = kPendingDecode; |
| 104 DecodePendingBuffer(); |
109 } | 105 } |
110 | 106 |
111 void DecryptingAudioDecoder::Reset(const base::Closure& closure) { | 107 void DecryptingAudioDecoder::Reset(const base::Closure& closure) { |
112 DVLOG(2) << "Reset() - state: " << state_; | 108 DVLOG(2) << "Reset() - state: " << state_; |
113 DCHECK(task_runner_->BelongsToCurrentThread()); | 109 DCHECK(task_runner_->BelongsToCurrentThread()); |
114 DCHECK(state_ == kIdle || | 110 DCHECK(state_ == kIdle || |
115 state_ == kPendingConfigChange || | 111 state_ == kPendingConfigChange || |
116 state_ == kPendingDemuxerRead || | 112 state_ == kPendingDemuxerRead || |
117 state_ == kPendingDecode || | 113 state_ == kPendingDecode || |
118 state_ == kWaitingForKey || | 114 state_ == kWaitingForKey || |
119 state_ == kDecodeFinished) << state_; | 115 state_ == kDecodeFinished) << state_; |
120 DCHECK(init_cb_.is_null()); // No Reset() during pending initialization. | 116 DCHECK(init_cb_.is_null()); // No Reset() during pending initialization. |
121 DCHECK(reset_cb_.is_null()); | 117 DCHECK(reset_cb_.is_null()); |
122 | 118 |
123 reset_cb_ = BindToCurrentLoop(closure); | 119 reset_cb_ = BindToCurrentLoop(closure); |
124 | 120 |
125 decryptor_->ResetDecoder(Decryptor::kAudio); | 121 decryptor_->ResetDecoder(Decryptor::kAudio); |
126 | 122 |
127 // Reset() cannot complete if the read callback is still pending. | 123 // Reset() cannot complete if the read callback is still pending. |
128 // Defer the resetting process in this case. The |reset_cb_| will be fired | 124 // Defer the resetting process in this case. The |reset_cb_| will be fired |
129 // after the read callback is fired - see DecryptAndDecodeBuffer() and | 125 // after the read callback is fired - see DecryptAndDecodeBuffer() and |
130 // DeliverFrame(). | 126 // DeliverFrame(). |
131 if (state_ == kPendingConfigChange || | 127 if (state_ == kPendingConfigChange || state_ == kPendingDecode) { |
132 state_ == kPendingDemuxerRead || | 128 DCHECK(!decode_cb_.is_null()); |
133 state_ == kPendingDecode) { | |
134 DCHECK(!read_cb_.is_null()); | |
135 return; | 129 return; |
136 } | 130 } |
137 | 131 |
138 if (state_ == kWaitingForKey) { | 132 if (state_ == kWaitingForKey) { |
139 DCHECK(!read_cb_.is_null()); | 133 DCHECK(!decode_cb_.is_null()); |
140 pending_buffer_to_decode_ = NULL; | 134 pending_buffer_to_decode_ = NULL; |
141 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL); | 135 base::ResetAndReturn(&decode_cb_).Run(kAborted, NULL); |
142 } | 136 } |
143 | 137 |
144 DCHECK(read_cb_.is_null()); | 138 DCHECK(decode_cb_.is_null()); |
145 DoReset(); | 139 DoReset(); |
146 } | 140 } |
147 | 141 |
148 void DecryptingAudioDecoder::Stop(const base::Closure& closure) { | 142 void DecryptingAudioDecoder::Stop(const base::Closure& closure) { |
149 DVLOG(2) << "Stop() - state: " << state_; | 143 DVLOG(2) << "Stop() - state: " << state_; |
150 DCHECK(task_runner_->BelongsToCurrentThread()); | 144 DCHECK(task_runner_->BelongsToCurrentThread()); |
151 | 145 |
152 if (decryptor_) { | 146 if (decryptor_) { |
153 decryptor_->RegisterNewKeyCB(Decryptor::kAudio, Decryptor::NewKeyCB()); | 147 decryptor_->RegisterNewKeyCB(Decryptor::kAudio, Decryptor::NewKeyCB()); |
154 decryptor_->DeinitializeDecoder(Decryptor::kAudio); | 148 decryptor_->DeinitializeDecoder(Decryptor::kAudio); |
155 decryptor_ = NULL; | 149 decryptor_ = NULL; |
156 } | 150 } |
157 if (!set_decryptor_ready_cb_.is_null()) | 151 if (!set_decryptor_ready_cb_.is_null()) |
158 base::ResetAndReturn(&set_decryptor_ready_cb_).Run(DecryptorReadyCB()); | 152 base::ResetAndReturn(&set_decryptor_ready_cb_).Run(DecryptorReadyCB()); |
159 pending_buffer_to_decode_ = NULL; | 153 pending_buffer_to_decode_ = NULL; |
160 if (!init_cb_.is_null()) | 154 if (!init_cb_.is_null()) |
161 base::ResetAndReturn(&init_cb_).Run(DECODER_ERROR_NOT_SUPPORTED); | 155 base::ResetAndReturn(&init_cb_).Run(DECODER_ERROR_NOT_SUPPORTED); |
162 if (!read_cb_.is_null()) | 156 if (!decode_cb_.is_null()) |
163 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL); | 157 base::ResetAndReturn(&decode_cb_).Run(kAborted, NULL); |
164 if (!reset_cb_.is_null()) | 158 if (!reset_cb_.is_null()) |
165 base::ResetAndReturn(&reset_cb_).Run(); | 159 base::ResetAndReturn(&reset_cb_).Run(); |
166 state_ = kStopped; | 160 state_ = kStopped; |
167 task_runner_->PostTask(FROM_HERE, closure); | 161 task_runner_->PostTask(FROM_HERE, closure); |
168 } | 162 } |
169 | 163 |
170 int DecryptingAudioDecoder::bits_per_channel() { | 164 int DecryptingAudioDecoder::bits_per_channel() { |
171 DCHECK(task_runner_->BelongsToCurrentThread()); | 165 DCHECK(task_runner_->BelongsToCurrentThread()); |
172 return bits_per_channel_; | 166 return bits_per_channel_; |
173 } | 167 } |
174 | 168 |
175 ChannelLayout DecryptingAudioDecoder::channel_layout() { | 169 ChannelLayout DecryptingAudioDecoder::channel_layout() { |
176 DCHECK(task_runner_->BelongsToCurrentThread()); | 170 DCHECK(task_runner_->BelongsToCurrentThread()); |
177 return channel_layout_; | 171 return channel_layout_; |
178 } | 172 } |
179 | 173 |
180 int DecryptingAudioDecoder::samples_per_second() { | 174 int DecryptingAudioDecoder::samples_per_second() { |
181 DCHECK(task_runner_->BelongsToCurrentThread()); | 175 DCHECK(task_runner_->BelongsToCurrentThread()); |
182 return samples_per_second_; | 176 return samples_per_second_; |
183 } | 177 } |
184 | 178 |
| 179 bool DecryptingAudioDecoder::HasQueuedData() const { |
| 180 return !queued_audio_frames_.empty(); |
| 181 } |
| 182 |
185 DecryptingAudioDecoder::~DecryptingAudioDecoder() { | 183 DecryptingAudioDecoder::~DecryptingAudioDecoder() { |
186 DCHECK(state_ == kUninitialized || state_ == kStopped) << state_; | 184 DCHECK(state_ == kUninitialized || state_ == kStopped) << state_; |
187 } | 185 } |
188 | 186 |
189 void DecryptingAudioDecoder::SetDecryptor(Decryptor* decryptor) { | 187 void DecryptingAudioDecoder::SetDecryptor(Decryptor* decryptor) { |
190 DVLOG(2) << "SetDecryptor()"; | 188 DVLOG(2) << "SetDecryptor()"; |
191 DCHECK(task_runner_->BelongsToCurrentThread()); | 189 DCHECK(task_runner_->BelongsToCurrentThread()); |
192 | 190 |
193 if (state_ == kStopped) | 191 if (state_ == kStopped) |
194 return; | 192 return; |
195 | 193 |
196 DCHECK_EQ(state_, kDecryptorRequested) << state_; | 194 DCHECK_EQ(state_, kDecryptorRequested) << state_; |
197 DCHECK(!init_cb_.is_null()); | 195 DCHECK(!init_cb_.is_null()); |
198 DCHECK(!set_decryptor_ready_cb_.is_null()); | 196 DCHECK(!set_decryptor_ready_cb_.is_null()); |
199 | 197 |
200 set_decryptor_ready_cb_.Reset(); | 198 set_decryptor_ready_cb_.Reset(); |
201 | 199 |
202 if (!decryptor) { | 200 if (!decryptor) { |
203 base::ResetAndReturn(&init_cb_).Run(DECODER_ERROR_NOT_SUPPORTED); | 201 base::ResetAndReturn(&init_cb_).Run(DECODER_ERROR_NOT_SUPPORTED); |
204 // TODO(xhwang): Add kError state. See http://crbug.com/251503 | 202 // TODO(xhwang): Add kError state. See http://crbug.com/251503 |
205 state_ = kStopped; | 203 state_ = kStopped; |
206 return; | 204 return; |
207 } | 205 } |
208 | 206 |
209 decryptor_ = decryptor; | 207 decryptor_ = decryptor; |
210 | 208 |
211 const AudioDecoderConfig& input_config = | |
212 demuxer_stream_->audio_decoder_config(); | |
213 AudioDecoderConfig config; | |
214 config.Initialize(input_config.codec(), | |
215 kSampleFormatS16, | |
216 input_config.channel_layout(), | |
217 input_config.samples_per_second(), | |
218 input_config.extra_data(), | |
219 input_config.extra_data_size(), | |
220 input_config.is_encrypted(), | |
221 false, | |
222 base::TimeDelta(), | |
223 base::TimeDelta()); | |
224 | |
225 state_ = kPendingDecoderInit; | 209 state_ = kPendingDecoderInit; |
226 decryptor_->InitializeAudioDecoder( | 210 decryptor_->InitializeAudioDecoder( |
227 config, | 211 config_, |
228 BindToCurrentLoop(base::Bind( | 212 BindToCurrentLoop(base::Bind( |
229 &DecryptingAudioDecoder::FinishInitialization, weak_this_))); | 213 &DecryptingAudioDecoder::FinishInitialization, weak_this_))); |
230 } | 214 } |
231 | 215 |
232 void DecryptingAudioDecoder::FinishInitialization(bool success) { | 216 void DecryptingAudioDecoder::FinishInitialization(bool success) { |
233 DVLOG(2) << "FinishInitialization()"; | 217 DVLOG(2) << "FinishInitialization()"; |
234 DCHECK(task_runner_->BelongsToCurrentThread()); | 218 DCHECK(task_runner_->BelongsToCurrentThread()); |
235 | 219 |
236 if (state_ == kStopped) | 220 if (state_ == kStopped) |
237 return; | 221 return; |
238 | 222 |
239 DCHECK_EQ(state_, kPendingDecoderInit) << state_; | 223 DCHECK_EQ(state_, kPendingDecoderInit) << state_; |
240 DCHECK(!init_cb_.is_null()); | 224 DCHECK(!init_cb_.is_null()); |
241 DCHECK(reset_cb_.is_null()); // No Reset() before initialization finished. | 225 DCHECK(reset_cb_.is_null()); // No Reset() before initialization finished. |
242 DCHECK(read_cb_.is_null()); // No Read() before initialization finished. | 226 DCHECK(decode_cb_.is_null()); // No Decode() before initialization finished. |
243 | 227 |
244 if (!success) { | 228 if (!success) { |
245 base::ResetAndReturn(&init_cb_).Run(DECODER_ERROR_NOT_SUPPORTED); | 229 base::ResetAndReturn(&init_cb_).Run(DECODER_ERROR_NOT_SUPPORTED); |
246 state_ = kStopped; | 230 state_ = kStopped; |
247 return; | 231 return; |
248 } | 232 } |
249 | 233 |
250 // Success! | 234 // Success! |
251 UpdateDecoderConfig(); | 235 UpdateDecoderConfig(); |
252 | 236 |
253 decryptor_->RegisterNewKeyCB( | 237 decryptor_->RegisterNewKeyCB( |
254 Decryptor::kAudio, BindToCurrentLoop(base::Bind( | 238 Decryptor::kAudio, BindToCurrentLoop(base::Bind( |
255 &DecryptingAudioDecoder::OnKeyAdded, weak_this_))); | 239 &DecryptingAudioDecoder::OnKeyAdded, weak_this_))); |
256 | 240 |
257 state_ = kIdle; | 241 state_ = kIdle; |
258 base::ResetAndReturn(&init_cb_).Run(PIPELINE_OK); | 242 base::ResetAndReturn(&init_cb_).Run(PIPELINE_OK); |
259 } | 243 } |
260 | 244 |
261 void DecryptingAudioDecoder::FinishConfigChange(bool success) { | |
262 DVLOG(2) << "FinishConfigChange()"; | |
263 DCHECK(task_runner_->BelongsToCurrentThread()); | |
264 DCHECK_EQ(state_, kPendingConfigChange) << state_; | |
265 DCHECK(!read_cb_.is_null()); | |
266 | |
267 if (!success) { | |
268 base::ResetAndReturn(&read_cb_).Run(kDecodeError, NULL); | |
269 state_ = kDecodeFinished; | |
270 if (!reset_cb_.is_null()) | |
271 base::ResetAndReturn(&reset_cb_).Run(); | |
272 return; | |
273 } | |
274 | |
275 // Config change succeeded. | |
276 UpdateDecoderConfig(); | |
277 | |
278 if (!reset_cb_.is_null()) { | |
279 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL); | |
280 DoReset(); | |
281 return; | |
282 } | |
283 | |
284 state_ = kPendingDemuxerRead; | |
285 ReadFromDemuxerStream(); | |
286 } | |
287 | |
288 void DecryptingAudioDecoder::ReadFromDemuxerStream() { | |
289 DCHECK_EQ(state_, kPendingDemuxerRead) << state_; | |
290 DCHECK(!read_cb_.is_null()); | |
291 | |
292 demuxer_stream_->Read( | |
293 base::Bind(&DecryptingAudioDecoder::DecryptAndDecodeBuffer, weak_this_)); | |
294 } | |
295 | |
296 void DecryptingAudioDecoder::DecryptAndDecodeBuffer( | |
297 DemuxerStream::Status status, | |
298 const scoped_refptr<DecoderBuffer>& buffer) { | |
299 DVLOG(3) << "DecryptAndDecodeBuffer()"; | |
300 DCHECK(task_runner_->BelongsToCurrentThread()); | |
301 DCHECK_EQ(state_, kPendingDemuxerRead) << state_; | |
302 DCHECK(!read_cb_.is_null()); | |
303 DCHECK_EQ(buffer.get() != NULL, status == DemuxerStream::kOk) << status; | |
304 | |
305 if (status == DemuxerStream::kConfigChanged) { | |
306 DVLOG(2) << "DecryptAndDecodeBuffer() - kConfigChanged"; | |
307 | |
308 const AudioDecoderConfig& input_config = | |
309 demuxer_stream_->audio_decoder_config(); | |
310 AudioDecoderConfig config; | |
311 config.Initialize(input_config.codec(), | |
312 kSampleFormatS16, | |
313 input_config.channel_layout(), | |
314 input_config.samples_per_second(), | |
315 input_config.extra_data(), | |
316 input_config.extra_data_size(), | |
317 input_config.is_encrypted(), | |
318 false, | |
319 base::TimeDelta(), | |
320 base::TimeDelta()); | |
321 | |
322 state_ = kPendingConfigChange; | |
323 decryptor_->DeinitializeDecoder(Decryptor::kAudio); | |
324 decryptor_->InitializeAudioDecoder( | |
325 config, BindToCurrentLoop(base::Bind( | |
326 &DecryptingAudioDecoder::FinishConfigChange, weak_this_))); | |
327 return; | |
328 } | |
329 | |
330 if (!reset_cb_.is_null()) { | |
331 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL); | |
332 DoReset(); | |
333 return; | |
334 } | |
335 | |
336 if (status == DemuxerStream::kAborted) { | |
337 DVLOG(2) << "DecryptAndDecodeBuffer() - kAborted"; | |
338 state_ = kIdle; | |
339 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL); | |
340 return; | |
341 } | |
342 | |
343 DCHECK_EQ(status, DemuxerStream::kOk); | |
344 | |
345 // Initialize the |next_output_timestamp_| to be the timestamp of the first | |
346 // non-EOS buffer. | |
347 if (timestamp_helper_->base_timestamp() == kNoTimestamp() && | |
348 !buffer->end_of_stream()) { | |
349 timestamp_helper_->SetBaseTimestamp(buffer->timestamp()); | |
350 } | |
351 | |
352 pending_buffer_to_decode_ = buffer; | |
353 state_ = kPendingDecode; | |
354 DecodePendingBuffer(); | |
355 } | |
356 | |
357 void DecryptingAudioDecoder::DecodePendingBuffer() { | 245 void DecryptingAudioDecoder::DecodePendingBuffer() { |
358 DCHECK(task_runner_->BelongsToCurrentThread()); | 246 DCHECK(task_runner_->BelongsToCurrentThread()); |
359 DCHECK_EQ(state_, kPendingDecode) << state_; | 247 DCHECK_EQ(state_, kPendingDecode) << state_; |
360 | 248 |
361 int buffer_size = 0; | 249 int buffer_size = 0; |
362 if (!pending_buffer_to_decode_->end_of_stream()) { | 250 if (!pending_buffer_to_decode_->end_of_stream()) { |
363 buffer_size = pending_buffer_to_decode_->data_size(); | 251 buffer_size = pending_buffer_to_decode_->data_size(); |
364 } | 252 } |
365 | 253 |
366 decryptor_->DecryptAndDecodeAudio( | 254 decryptor_->DecryptAndDecodeAudio( |
367 pending_buffer_to_decode_, | 255 pending_buffer_to_decode_, |
368 BindToCurrentLoop(base::Bind( | 256 BindToCurrentLoop(base::Bind( |
369 &DecryptingAudioDecoder::DeliverFrame, weak_this_, buffer_size))); | 257 &DecryptingAudioDecoder::DeliverFrame, weak_this_, buffer_size))); |
370 } | 258 } |
371 | 259 |
372 void DecryptingAudioDecoder::DeliverFrame( | 260 void DecryptingAudioDecoder::DeliverFrame( |
373 int buffer_size, | 261 int buffer_size, |
374 Decryptor::Status status, | 262 Decryptor::Status status, |
375 const Decryptor::AudioBuffers& frames) { | 263 const Decryptor::AudioBuffers& frames) { |
376 DVLOG(3) << "DeliverFrame() - status: " << status; | 264 DVLOG(3) << "DeliverFrame() - status: " << status; |
377 DCHECK(task_runner_->BelongsToCurrentThread()); | 265 DCHECK(task_runner_->BelongsToCurrentThread()); |
378 | 266 |
379 if (state_ == kStopped) | 267 if (state_ == kStopped) |
380 return; | 268 return; |
381 | 269 |
382 DCHECK_EQ(state_, kPendingDecode) << state_; | 270 DCHECK_EQ(state_, kPendingDecode) << state_; |
383 DCHECK(!read_cb_.is_null()); | 271 DCHECK(!decode_cb_.is_null()); |
384 DCHECK(pending_buffer_to_decode_.get()); | 272 DCHECK(pending_buffer_to_decode_.get()); |
385 DCHECK(queued_audio_frames_.empty()); | 273 DCHECK(queued_audio_frames_.empty()); |
386 | 274 |
387 bool need_to_try_again_if_nokey_is_returned = key_added_while_decode_pending_; | 275 bool need_to_try_again_if_nokey_is_returned = key_added_while_decode_pending_; |
388 key_added_while_decode_pending_ = false; | 276 key_added_while_decode_pending_ = false; |
389 | 277 |
390 scoped_refptr<DecoderBuffer> scoped_pending_buffer_to_decode = | 278 scoped_refptr<DecoderBuffer> scoped_pending_buffer_to_decode = |
391 pending_buffer_to_decode_; | 279 pending_buffer_to_decode_; |
392 pending_buffer_to_decode_ = NULL; | 280 pending_buffer_to_decode_ = NULL; |
393 | 281 |
394 if (!reset_cb_.is_null()) { | 282 if (!reset_cb_.is_null()) { |
395 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL); | 283 base::ResetAndReturn(&decode_cb_).Run(kAborted, NULL); |
396 DoReset(); | 284 DoReset(); |
397 return; | 285 return; |
398 } | 286 } |
399 | 287 |
400 DCHECK_EQ(status == Decryptor::kSuccess, !frames.empty()); | 288 DCHECK_EQ(status == Decryptor::kSuccess, !frames.empty()); |
401 | 289 |
402 if (status == Decryptor::kError) { | 290 if (status == Decryptor::kError) { |
403 DVLOG(2) << "DeliverFrame() - kError"; | 291 DVLOG(2) << "DeliverFrame() - kError"; |
404 state_ = kDecodeFinished; | 292 state_ = kDecodeFinished; // TODO add kError state |
405 base::ResetAndReturn(&read_cb_).Run(kDecodeError, NULL); | 293 base::ResetAndReturn(&decode_cb_).Run(kDecodeError, NULL); |
406 return; | 294 return; |
407 } | 295 } |
408 | 296 |
409 if (status == Decryptor::kNoKey) { | 297 if (status == Decryptor::kNoKey) { |
410 DVLOG(2) << "DeliverFrame() - kNoKey"; | 298 DVLOG(2) << "DeliverFrame() - kNoKey"; |
411 // Set |pending_buffer_to_decode_| back as we need to try decoding the | 299 // Set |pending_buffer_to_decode_| back as we need to try decoding the |
412 // pending buffer again when new key is added to the decryptor. | 300 // pending buffer again when new key is added to the decryptor. |
413 pending_buffer_to_decode_ = scoped_pending_buffer_to_decode; | 301 pending_buffer_to_decode_ = scoped_pending_buffer_to_decode; |
414 | 302 |
415 if (need_to_try_again_if_nokey_is_returned) { | 303 if (need_to_try_again_if_nokey_is_returned) { |
416 // The |state_| is still kPendingDecode. | 304 // The |state_| is still kPendingDecode. |
417 DecodePendingBuffer(); | 305 DecodePendingBuffer(); |
418 return; | 306 return; |
419 } | 307 } |
420 | 308 |
421 state_ = kWaitingForKey; | 309 state_ = kWaitingForKey; |
422 return; | 310 return; |
423 } | 311 } |
424 | 312 |
425 // The buffer has been accepted by the decoder, let's report statistics. | |
426 if (buffer_size) { | |
427 PipelineStatistics statistics; | |
428 statistics.audio_bytes_decoded = buffer_size; | |
429 statistics_cb_.Run(statistics); | |
430 } | |
431 | |
432 if (status == Decryptor::kNeedMoreData) { | 313 if (status == Decryptor::kNeedMoreData) { |
433 DVLOG(2) << "DeliverFrame() - kNeedMoreData"; | 314 DVLOG(2) << "DeliverFrame() - kNeedMoreData"; |
434 if (scoped_pending_buffer_to_decode->end_of_stream()) { | 315 if (scoped_pending_buffer_to_decode->end_of_stream()) { |
435 state_ = kDecodeFinished; | 316 state_ = kDecodeFinished; |
436 base::ResetAndReturn(&read_cb_).Run(kOk, AudioBuffer::CreateEOSBuffer()); | 317 base::ResetAndReturn(&decode_cb_) |
| 318 .Run(kOk, AudioBuffer::CreateEOSBuffer()); |
437 return; | 319 return; |
438 } | 320 } |
439 | 321 |
440 state_ = kPendingDemuxerRead; | 322 state_ = kIdle; |
441 ReadFromDemuxerStream(); | 323 base::ResetAndReturn(&decode_cb_).Run(kNotEnoughData, NULL); |
442 return; | 324 return; |
443 } | 325 } |
444 | 326 |
445 DCHECK_EQ(status, Decryptor::kSuccess); | 327 DCHECK_EQ(status, Decryptor::kSuccess); |
446 DCHECK(!frames.empty()); | 328 DCHECK(!frames.empty()); |
447 EnqueueFrames(frames); | 329 EnqueueFrames(frames); |
448 | 330 |
449 state_ = kIdle; | 331 state_ = kIdle; |
450 base::ResetAndReturn(&read_cb_).Run(kOk, queued_audio_frames_.front()); | 332 base::ResetAndReturn(&decode_cb_).Run(kOk, queued_audio_frames_.front()); |
451 queued_audio_frames_.pop_front(); | 333 queued_audio_frames_.pop_front(); |
452 } | 334 } |
453 | 335 |
454 void DecryptingAudioDecoder::OnKeyAdded() { | 336 void DecryptingAudioDecoder::OnKeyAdded() { |
455 DCHECK(task_runner_->BelongsToCurrentThread()); | 337 DCHECK(task_runner_->BelongsToCurrentThread()); |
456 | 338 |
457 if (state_ == kPendingDecode) { | 339 if (state_ == kPendingDecode) { |
458 key_added_while_decode_pending_ = true; | 340 key_added_while_decode_pending_ = true; |
459 return; | 341 return; |
460 } | 342 } |
461 | 343 |
462 if (state_ == kWaitingForKey) { | 344 if (state_ == kWaitingForKey) { |
463 state_ = kPendingDecode; | 345 state_ = kPendingDecode; |
464 DecodePendingBuffer(); | 346 DecodePendingBuffer(); |
465 } | 347 } |
466 } | 348 } |
467 | 349 |
468 void DecryptingAudioDecoder::DoReset() { | 350 void DecryptingAudioDecoder::DoReset() { |
469 DCHECK(init_cb_.is_null()); | 351 DCHECK(init_cb_.is_null()); |
470 DCHECK(read_cb_.is_null()); | 352 DCHECK(decode_cb_.is_null()); |
471 timestamp_helper_->SetBaseTimestamp(kNoTimestamp()); | 353 timestamp_helper_->SetBaseTimestamp(kNoTimestamp()); |
472 state_ = kIdle; | 354 state_ = kIdle; |
473 base::ResetAndReturn(&reset_cb_).Run(); | 355 base::ResetAndReturn(&reset_cb_).Run(); |
474 } | 356 } |
475 | 357 |
476 void DecryptingAudioDecoder::UpdateDecoderConfig() { | 358 void DecryptingAudioDecoder::UpdateDecoderConfig() { |
477 const AudioDecoderConfig& config = demuxer_stream_->audio_decoder_config(); | |
478 bits_per_channel_ = kSupportedBitsPerChannel; | 359 bits_per_channel_ = kSupportedBitsPerChannel; |
479 channel_layout_ = config.channel_layout(); | 360 channel_layout_ = config_.channel_layout(); |
480 samples_per_second_ = config.samples_per_second(); | 361 samples_per_second_ = config_.samples_per_second(); |
481 timestamp_helper_.reset(new AudioTimestampHelper(samples_per_second_)); | 362 timestamp_helper_.reset(new AudioTimestampHelper(samples_per_second_)); |
482 } | 363 } |
483 | 364 |
484 void DecryptingAudioDecoder::EnqueueFrames( | 365 void DecryptingAudioDecoder::EnqueueFrames( |
485 const Decryptor::AudioBuffers& frames) { | 366 const Decryptor::AudioBuffers& frames) { |
486 queued_audio_frames_ = frames; | 367 queued_audio_frames_ = frames; |
487 | 368 |
488 for (Decryptor::AudioBuffers::iterator iter = queued_audio_frames_.begin(); | 369 for (Decryptor::AudioBuffers::iterator iter = queued_audio_frames_.begin(); |
489 iter != queued_audio_frames_.end(); | 370 iter != queued_audio_frames_.end(); |
490 ++iter) { | 371 ++iter) { |
(...skipping 11 matching lines...) Expand all Loading... |
502 } | 383 } |
503 | 384 |
504 frame->set_timestamp(current_time); | 385 frame->set_timestamp(current_time); |
505 frame->set_duration( | 386 frame->set_duration( |
506 timestamp_helper_->GetFrameDuration(frame->frame_count())); | 387 timestamp_helper_->GetFrameDuration(frame->frame_count())); |
507 timestamp_helper_->AddFrames(frame->frame_count()); | 388 timestamp_helper_->AddFrames(frame->frame_count()); |
508 } | 389 } |
509 } | 390 } |
510 | 391 |
511 } // namespace media | 392 } // namespace media |
OLD | NEW |