| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/base/android/media_decoder_job.h" | 5 #include "media/base/android/media_decoder_job.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
| 9 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
| 10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
| 11 #include "media/base/android/media_codec_bridge.h" | 11 #include "media/base/android/media_codec_bridge.h" |
| 12 #include "media/base/android/media_drm_bridge.h" |
| 12 #include "media/base/bind_to_current_loop.h" | 13 #include "media/base/bind_to_current_loop.h" |
| 13 #include "media/base/buffers.h" | 14 #include "media/base/buffers.h" |
| 14 | 15 |
| 15 namespace media { | 16 namespace media { |
| 16 | 17 |
| 17 // Timeout value for media codec operations. Because the first | 18 // Timeout value for media codec operations. Because the first |
| 18 // DequeInputBuffer() can take about 150 milliseconds, use 250 milliseconds | 19 // DequeInputBuffer() can take about 150 milliseconds, use 250 milliseconds |
| 19 // here. See http://b/9357571. | 20 // here. See http://b/9357571. |
| 20 static const int kMediaCodecTimeoutInMilliseconds = 250; | 21 static const int kMediaCodecTimeoutInMilliseconds = 250; |
| 21 | 22 |
| 22 MediaDecoderJob::MediaDecoderJob( | 23 MediaDecoderJob::MediaDecoderJob( |
| 23 const scoped_refptr<base::SingleThreadTaskRunner>& decoder_task_runner, | 24 const scoped_refptr<base::SingleThreadTaskRunner>& decoder_task_runner, |
| 24 MediaCodecBridge* media_codec_bridge, | 25 const base::Closure& request_data_cb, |
| 25 const base::Closure& request_data_cb) | 26 const base::Closure& config_changed_cb) |
| 26 : ui_task_runner_(base::MessageLoopProxy::current()), | 27 : ui_task_runner_(base::MessageLoopProxy::current()), |
| 27 decoder_task_runner_(decoder_task_runner), | 28 decoder_task_runner_(decoder_task_runner), |
| 28 media_codec_bridge_(media_codec_bridge), | |
| 29 needs_flush_(false), | 29 needs_flush_(false), |
| 30 input_eos_encountered_(false), | 30 input_eos_encountered_(false), |
| 31 output_eos_encountered_(false), | 31 output_eos_encountered_(false), |
| 32 skip_eos_enqueue_(true), | 32 skip_eos_enqueue_(true), |
| 33 prerolling_(true), | 33 prerolling_(true), |
| 34 request_data_cb_(request_data_cb), | 34 request_data_cb_(request_data_cb), |
| 35 config_changed_cb_(config_changed_cb), |
| 35 current_demuxer_data_index_(0), | 36 current_demuxer_data_index_(0), |
| 36 input_buf_index_(-1), | 37 input_buf_index_(-1), |
| 38 is_content_encrypted_(false), |
| 37 stop_decode_pending_(false), | 39 stop_decode_pending_(false), |
| 38 destroy_pending_(false), | 40 destroy_pending_(false), |
| 39 is_requesting_demuxer_data_(false), | 41 is_requesting_demuxer_data_(false), |
| 40 is_incoming_data_invalid_(false), | 42 is_incoming_data_invalid_(false), |
| 41 weak_factory_(this) { | 43 release_resources_pending_(false), |
| 44 drm_bridge_(NULL), |
| 45 drain_decoder_(false) { |
| 42 InitializeReceivedData(); | 46 InitializeReceivedData(); |
| 47 eos_unit_.end_of_stream = true; |
| 43 } | 48 } |
| 44 | 49 |
| 45 MediaDecoderJob::~MediaDecoderJob() {} | 50 MediaDecoderJob::~MediaDecoderJob() { |
| 51 ReleaseMediaCodecBridge(); |
| 52 } |
| 46 | 53 |
| 47 void MediaDecoderJob::OnDataReceived(const DemuxerData& data) { | 54 void MediaDecoderJob::OnDataReceived(const DemuxerData& data) { |
| 48 DVLOG(1) << __FUNCTION__ << ": " << data.access_units.size() << " units"; | 55 DVLOG(1) << __FUNCTION__ << ": " << data.access_units.size() << " units"; |
| 49 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 56 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 50 DCHECK(NoAccessUnitsRemainingInChunk(false)); | 57 DCHECK(NoAccessUnitsRemainingInChunk(false)); |
| 51 | 58 |
| 52 TRACE_EVENT_ASYNC_END2( | 59 TRACE_EVENT_ASYNC_END2( |
| 53 "media", "MediaDecoderJob::RequestData", this, | 60 "media", "MediaDecoderJob::RequestData", this, |
| 54 "Data type", data.type == media::DemuxerStream::AUDIO ? "AUDIO" : "VIDEO", | 61 "Data type", data.type == media::DemuxerStream::AUDIO ? "AUDIO" : "VIDEO", |
| 55 "Units read", data.access_units.size()); | 62 "Units read", data.access_units.size()); |
| 56 | 63 |
| 57 if (is_incoming_data_invalid_) { | 64 if (is_incoming_data_invalid_) { |
| 58 is_incoming_data_invalid_ = false; | 65 is_incoming_data_invalid_ = false; |
| 59 | 66 |
| 60 // If there is a pending callback, need to request the data again to get | 67 // If there is a pending callback, need to request the data again to get |
| 61 // valid data. | 68 // valid data. |
| 62 if (!on_data_received_cb_.is_null()) | 69 if (!data_received_cb_.is_null()) |
| 63 request_data_cb_.Run(); | 70 request_data_cb_.Run(); |
| 64 else | 71 else |
| 65 is_requesting_demuxer_data_ = false; | 72 is_requesting_demuxer_data_ = false; |
| 66 return; | 73 return; |
| 67 } | 74 } |
| 68 | 75 |
| 69 size_t next_demuxer_data_index = inactive_demuxer_data_index(); | 76 size_t next_demuxer_data_index = inactive_demuxer_data_index(); |
| 70 received_data_[next_demuxer_data_index] = data; | 77 received_data_[next_demuxer_data_index] = data; |
| 71 access_unit_index_[next_demuxer_data_index] = 0; | 78 access_unit_index_[next_demuxer_data_index] = 0; |
| 72 is_requesting_demuxer_data_ = false; | 79 is_requesting_demuxer_data_ = false; |
| 73 | 80 |
| 74 base::Closure done_cb = base::ResetAndReturn(&on_data_received_cb_); | 81 base::Closure done_cb = base::ResetAndReturn(&data_received_cb_); |
| 75 | 82 |
| 76 // If this data request is for the inactive chunk, or |on_data_received_cb_| | 83 // If this data request is for the inactive chunk, or |data_received_cb_| |
| 77 // was set to null by ClearData() or Release(), do nothing. | 84 // was set to null by Flush() or Release(), do nothing. |
| 78 if (done_cb.is_null()) | 85 if (done_cb.is_null()) |
| 79 return; | 86 return; |
| 80 | 87 |
| 81 if (stop_decode_pending_) { | 88 if (stop_decode_pending_) { |
| 82 DCHECK(is_decoding()); | 89 DCHECK(is_decoding()); |
| 83 OnDecodeCompleted(MEDIA_CODEC_STOPPED, kNoTimestamp(), kNoTimestamp()); | 90 OnDecodeCompleted(MEDIA_CODEC_STOPPED, kNoTimestamp(), kNoTimestamp()); |
| 84 return; | 91 return; |
| 85 } | 92 } |
| 86 | 93 |
| 87 done_cb.Run(); | 94 done_cb.Run(); |
| 88 } | 95 } |
| 89 | 96 |
| 90 void MediaDecoderJob::Prefetch(const base::Closure& prefetch_cb) { | 97 void MediaDecoderJob::Prefetch(const base::Closure& prefetch_cb) { |
| 91 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 98 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 92 DCHECK(on_data_received_cb_.is_null()); | 99 DCHECK(data_received_cb_.is_null()); |
| 93 DCHECK(decode_cb_.is_null()); | 100 DCHECK(decode_cb_.is_null()); |
| 94 | 101 |
| 95 if (HasData()) { | 102 if (HasData()) { |
| 96 DVLOG(1) << __FUNCTION__ << " : using previously received data"; | 103 DVLOG(1) << __FUNCTION__ << " : using previously received data"; |
| 97 ui_task_runner_->PostTask(FROM_HERE, prefetch_cb); | 104 ui_task_runner_->PostTask(FROM_HERE, prefetch_cb); |
| 98 return; | 105 return; |
| 99 } | 106 } |
| 100 | 107 |
| 101 DVLOG(1) << __FUNCTION__ << " : requesting data"; | 108 DVLOG(1) << __FUNCTION__ << " : requesting data"; |
| 102 RequestData(prefetch_cb); | 109 RequestData(prefetch_cb); |
| 103 } | 110 } |
| 104 | 111 |
| 105 scoped_ptr<DemuxerConfigs> MediaDecoderJob::Decode( | 112 bool MediaDecoderJob::Decode( |
| 106 base::TimeTicks start_time_ticks, | 113 base::TimeTicks start_time_ticks, |
| 107 base::TimeDelta start_presentation_timestamp, | 114 base::TimeDelta start_presentation_timestamp, |
| 108 const DecoderCallback& callback) { | 115 const DecoderCallback& callback) { |
| 109 DCHECK(decode_cb_.is_null()); | 116 DCHECK(decode_cb_.is_null()); |
| 110 DCHECK(on_data_received_cb_.is_null()); | 117 DCHECK(data_received_cb_.is_null()); |
| 111 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 118 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 112 | 119 |
| 120 if (!media_codec_bridge_ || need_to_reconfig_decoder_job_) { |
| 121 need_to_reconfig_decoder_job_ = !CreateMediaCodecBridge(); |
| 122 if (drain_decoder_) { |
| 123 // Decoder has been recreated, stop draining. |
| 124 drain_decoder_ = false; |
| 125 input_eos_encountered_ = false; |
| 126 output_eos_encountered_ = false; |
| 127 access_unit_index_[current_demuxer_data_index_]++; |
| 128 } |
| 129 skip_eos_enqueue_ = true; |
| 130 if (need_to_reconfig_decoder_job_) |
| 131 return false; |
| 132 } |
| 133 |
| 113 decode_cb_ = callback; | 134 decode_cb_ = callback; |
| 114 | 135 |
| 115 if (!HasData()) { | 136 if (!HasData()) { |
| 116 RequestData(base::Bind(&MediaDecoderJob::DecodeCurrentAccessUnit, | 137 RequestData(base::Bind(&MediaDecoderJob::DecodeCurrentAccessUnit, |
| 117 base::Unretained(this), | 138 base::Unretained(this), |
| 118 start_time_ticks, | 139 start_time_ticks, |
| 119 start_presentation_timestamp)); | 140 start_presentation_timestamp)); |
| 120 return scoped_ptr<DemuxerConfigs>(); | 141 return true; |
| 121 } | |
| 122 | |
| 123 if (DemuxerStream::kConfigChanged == CurrentAccessUnit().status) { | |
| 124 decode_cb_.Reset(); | |
| 125 size_t index = CurrentReceivedDataChunkIndex(); | |
| 126 CHECK_EQ(1u, received_data_[index].demuxer_configs.size()); | |
| 127 return scoped_ptr<DemuxerConfigs>(new DemuxerConfigs( | |
| 128 received_data_[index].demuxer_configs[0])); | |
| 129 } | 142 } |
| 130 | 143 |
| 131 DecodeCurrentAccessUnit(start_time_ticks, start_presentation_timestamp); | 144 DecodeCurrentAccessUnit(start_time_ticks, start_presentation_timestamp); |
| 132 return scoped_ptr<DemuxerConfigs>(); | 145 return true; |
| 133 } | 146 } |
| 134 | 147 |
| 135 void MediaDecoderJob::StopDecode() { | 148 void MediaDecoderJob::StopDecode() { |
| 136 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 149 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 137 DCHECK(is_decoding()); | 150 DCHECK(is_decoding()); |
| 138 stop_decode_pending_ = true; | 151 stop_decode_pending_ = true; |
| 139 } | 152 } |
| 140 | 153 |
| 154 bool MediaDecoderJob::OutputEOSReached() const { |
| 155 return !drain_decoder_ && output_eos_encountered_; |
| 156 } |
| 157 |
| 158 void MediaDecoderJob::SetDrmBridge(MediaDrmBridge* drm_bridge) { |
| 159 drm_bridge_ = drm_bridge; |
| 160 need_to_reconfig_decoder_job_ = true; |
| 161 } |
| 162 |
| 141 void MediaDecoderJob::Flush() { | 163 void MediaDecoderJob::Flush() { |
| 164 DVLOG(1) << __FUNCTION__; |
| 165 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 166 DCHECK(data_received_cb_.is_null()); |
| 142 DCHECK(decode_cb_.is_null()); | 167 DCHECK(decode_cb_.is_null()); |
| 143 | 168 |
| 169 // Clean up the received data. |
| 170 current_demuxer_data_index_ = 0; |
| 171 InitializeReceivedData(); |
| 172 if (is_requesting_demuxer_data_) |
| 173 is_incoming_data_invalid_ = true; |
| 174 input_eos_encountered_ = false; |
| 175 output_eos_encountered_ = false; |
| 176 drain_decoder_ = false; |
| 177 |
| 144 // Do nothing, flush when the next Decode() happens. | 178 // Do nothing, flush when the next Decode() happens. |
| 145 needs_flush_ = true; | 179 needs_flush_ = true; |
| 146 ClearData(); | |
| 147 } | 180 } |
| 148 | 181 |
| 149 void MediaDecoderJob::BeginPrerolling(base::TimeDelta preroll_timestamp) { | 182 void MediaDecoderJob::BeginPrerolling(base::TimeDelta preroll_timestamp) { |
| 150 DVLOG(1) << __FUNCTION__ << "(" << preroll_timestamp.InSecondsF() << ")"; | 183 DVLOG(1) << __FUNCTION__ << "(" << preroll_timestamp.InSecondsF() << ")"; |
| 151 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 184 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 152 DCHECK(!is_decoding()); | 185 DCHECK(!is_decoding()); |
| 153 | 186 |
| 154 preroll_timestamp_ = preroll_timestamp; | 187 preroll_timestamp_ = preroll_timestamp; |
| 155 prerolling_ = true; | 188 prerolling_ = true; |
| 156 } | 189 } |
| 157 | 190 |
| 191 void MediaDecoderJob::ReleaseDecoderResources() { |
| 192 DVLOG(1) << __FUNCTION__; |
| 193 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 194 if (decode_cb_.is_null()) { |
| 195 DCHECK(!drain_decoder_); |
| 196 // Since the decoder job is not decoding data, we can safely destroy |
| 197 // |media_codec_bridge_|. |
| 198 ReleaseMediaCodecBridge(); |
| 199 return; |
| 200 } |
| 201 |
| 202 // Release |media_codec_bridge_| once decoding is completed. |
| 203 release_resources_pending_ = true; |
| 204 } |
| 205 |
| 206 bool MediaDecoderJob::SetDemuxerConfigs(const DemuxerConfigs& configs) { |
| 207 bool config_changed = AreDemuxerConfigsChanged(configs); |
| 208 if (config_changed) |
| 209 UpdateDemuxerConfigs(configs); |
| 210 return config_changed; |
| 211 } |
| 212 |
| 213 base::android::ScopedJavaLocalRef<jobject> MediaDecoderJob::GetMediaCrypto() { |
| 214 base::android::ScopedJavaLocalRef<jobject> media_crypto; |
| 215 if (drm_bridge_) |
| 216 media_crypto = drm_bridge_->GetMediaCrypto(); |
| 217 return media_crypto; |
| 218 } |
| 219 |
| 158 void MediaDecoderJob::Release() { | 220 void MediaDecoderJob::Release() { |
| 159 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 221 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 160 DVLOG(1) << __FUNCTION__; | 222 DVLOG(1) << __FUNCTION__; |
| 161 | 223 |
| 162 // If the decoder job is not waiting for data, and is still decoding, we | 224 // If the decoder job is still decoding, we cannot delete the job immediately. |
| 163 // cannot delete the job immediately. | 225 destroy_pending_ = is_decoding(); |
| 164 destroy_pending_ = on_data_received_cb_.is_null() && is_decoding(); | |
| 165 | 226 |
| 166 request_data_cb_.Reset(); | 227 request_data_cb_.Reset(); |
| 167 on_data_received_cb_.Reset(); | 228 data_received_cb_.Reset(); |
| 168 decode_cb_.Reset(); | 229 decode_cb_.Reset(); |
| 169 | 230 |
| 170 if (destroy_pending_) { | 231 if (destroy_pending_) { |
| 171 DVLOG(1) << __FUNCTION__ << " : delete is pending decode completion"; | 232 DVLOG(1) << __FUNCTION__ << " : delete is pending decode completion"; |
| 172 return; | 233 return; |
| 173 } | 234 } |
| 174 | 235 |
| 175 delete this; | 236 delete this; |
| 176 } | 237 } |
| 177 | 238 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 // |current_demuxer_data_index_| must be pointing to an EOS unit. | 292 // |current_demuxer_data_index_| must be pointing to an EOS unit. |
| 232 // We'll reuse this unit to flush the decoder until we hit output EOS. | 293 // We'll reuse this unit to flush the decoder until we hit output EOS. |
| 233 DCHECK(!input_eos_encountered_ || !NoAccessUnitsRemainingInChunk(true)); | 294 DCHECK(!input_eos_encountered_ || !NoAccessUnitsRemainingInChunk(true)); |
| 234 return !NoAccessUnitsRemainingInChunk(true) || | 295 return !NoAccessUnitsRemainingInChunk(true) || |
| 235 !NoAccessUnitsRemainingInChunk(false); | 296 !NoAccessUnitsRemainingInChunk(false); |
| 236 } | 297 } |
| 237 | 298 |
| 238 void MediaDecoderJob::RequestData(const base::Closure& done_cb) { | 299 void MediaDecoderJob::RequestData(const base::Closure& done_cb) { |
| 239 DVLOG(1) << __FUNCTION__; | 300 DVLOG(1) << __FUNCTION__; |
| 240 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 301 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 241 DCHECK(on_data_received_cb_.is_null()); | 302 DCHECK(data_received_cb_.is_null()); |
| 242 DCHECK(!input_eos_encountered_); | 303 DCHECK(!input_eos_encountered_); |
| 243 DCHECK(NoAccessUnitsRemainingInChunk(false)); | 304 DCHECK(NoAccessUnitsRemainingInChunk(false)); |
| 244 | 305 |
| 245 TRACE_EVENT_ASYNC_BEGIN0("media", "MediaDecoderJob::RequestData", this); | 306 TRACE_EVENT_ASYNC_BEGIN0("media", "MediaDecoderJob::RequestData", this); |
| 246 | 307 |
| 247 on_data_received_cb_ = done_cb; | 308 data_received_cb_ = done_cb; |
| 248 | 309 |
| 249 // If we are already expecting new data, just set the callback and do | 310 // If we are already expecting new data, just set the callback and do |
| 250 // nothing. | 311 // nothing. |
| 251 if (is_requesting_demuxer_data_) | 312 if (is_requesting_demuxer_data_) |
| 252 return; | 313 return; |
| 253 | 314 |
| 254 // The new incoming data will be stored as the next demuxer data chunk, since | 315 // The new incoming data will be stored as the next demuxer data chunk, since |
| 255 // the decoder might still be decoding the current one. | 316 // the decoder might still be decoding the current one. |
| 256 size_t next_demuxer_data_index = inactive_demuxer_data_index(); | 317 size_t next_demuxer_data_index = inactive_demuxer_data_index(); |
| 257 received_data_[next_demuxer_data_index] = DemuxerData(); | 318 received_data_[next_demuxer_data_index] = DemuxerData(); |
| 258 access_unit_index_[next_demuxer_data_index] = 0; | 319 access_unit_index_[next_demuxer_data_index] = 0; |
| 259 is_requesting_demuxer_data_ = true; | 320 is_requesting_demuxer_data_ = true; |
| 260 | 321 |
| 261 request_data_cb_.Run(); | 322 request_data_cb_.Run(); |
| 262 } | 323 } |
| 263 | 324 |
| 264 void MediaDecoderJob::DecodeCurrentAccessUnit( | 325 void MediaDecoderJob::DecodeCurrentAccessUnit( |
| 265 base::TimeTicks start_time_ticks, | 326 base::TimeTicks start_time_ticks, |
| 266 base::TimeDelta start_presentation_timestamp) { | 327 base::TimeDelta start_presentation_timestamp) { |
| 267 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 328 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 268 DCHECK(!decode_cb_.is_null()); | 329 DCHECK(!decode_cb_.is_null()); |
| 269 | 330 |
| 270 RequestCurrentChunkIfEmpty(); | 331 RequestCurrentChunkIfEmpty(); |
| 271 const AccessUnit& access_unit = CurrentAccessUnit(); | 332 const AccessUnit& access_unit = CurrentAccessUnit(); |
| 272 // If the first access unit is a config change, request the player to dequeue | 333 if (CurrentAccessUnit().status == DemuxerStream::kConfigChanged) { |
| 273 // the input buffer again so that it can request config data. | 334 int index = CurrentReceivedDataChunkIndex(); |
| 274 if (access_unit.status == DemuxerStream::kConfigChanged) { | 335 bool config_changed = SetDemuxerConfigs( |
| 275 ui_task_runner_->PostTask(FROM_HERE, | 336 received_data_[index].demuxer_configs[0]); |
| 276 base::Bind(&MediaDecoderJob::OnDecodeCompleted, | 337 if (config_changed) |
| 277 base::Unretained(this), | 338 config_changed_cb_.Run(); |
| 278 MEDIA_CODEC_DEQUEUE_INPUT_AGAIN_LATER, | 339 if (!drain_decoder_) { |
| 279 kNoTimestamp(), kNoTimestamp())); | 340 // If we haven't decoded any data yet, just skip the current access unit |
| 280 return; | 341 // and request the MediaCodec to be recreated on next Decode(). |
| 342 if (skip_eos_enqueue_ || !config_changed) { |
| 343 need_to_reconfig_decoder_job_ = |
| 344 need_to_reconfig_decoder_job_ || config_changed; |
| 345 ui_task_runner_->PostTask(FROM_HERE, base::Bind( |
| 346 &MediaDecoderJob::OnDecodeCompleted, base::Unretained(this), |
| 347 MEDIA_CODEC_OUTPUT_FORMAT_CHANGED, kNoTimestamp(), kNoTimestamp())); |
| 348 return; |
| 349 } |
| 350 // Start draining the decoder so that all the remaining frames are |
| 351 // rendered. |
| 352 drain_decoder_ = true; |
| 353 } |
| 281 } | 354 } |
| 282 | 355 |
| 356 DCHECK(!(needs_flush_ && drain_decoder_)); |
| 283 decoder_task_runner_->PostTask(FROM_HERE, base::Bind( | 357 decoder_task_runner_->PostTask(FROM_HERE, base::Bind( |
| 284 &MediaDecoderJob::DecodeInternal, base::Unretained(this), | 358 &MediaDecoderJob::DecodeInternal, base::Unretained(this), |
| 285 access_unit, | 359 drain_decoder_ ? eos_unit_ : access_unit, |
| 286 start_time_ticks, start_presentation_timestamp, needs_flush_, | 360 start_time_ticks, start_presentation_timestamp, needs_flush_, |
| 287 media::BindToCurrentLoop(base::Bind( | 361 media::BindToCurrentLoop(base::Bind( |
| 288 &MediaDecoderJob::OnDecodeCompleted, base::Unretained(this))))); | 362 &MediaDecoderJob::OnDecodeCompleted, base::Unretained(this))))); |
| 289 needs_flush_ = false; | 363 needs_flush_ = false; |
| 290 } | 364 } |
| 291 | 365 |
| 292 void MediaDecoderJob::DecodeInternal( | 366 void MediaDecoderJob::DecodeInternal( |
| 293 const AccessUnit& unit, | 367 const AccessUnit& unit, |
| 294 base::TimeTicks start_time_ticks, | 368 base::TimeTicks start_time_ticks, |
| 295 base::TimeDelta start_presentation_timestamp, | 369 base::TimeDelta start_presentation_timestamp, |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 !media_codec_bridge_->GetOutputBuffers()) { | 441 !media_codec_bridge_->GetOutputBuffers()) { |
| 368 status = MEDIA_CODEC_ERROR; | 442 status = MEDIA_CODEC_ERROR; |
| 369 } | 443 } |
| 370 callback.Run(status, kNoTimestamp(), kNoTimestamp()); | 444 callback.Run(status, kNoTimestamp(), kNoTimestamp()); |
| 371 return; | 445 return; |
| 372 } | 446 } |
| 373 | 447 |
| 374 // TODO(xhwang/qinmin): This logic is correct but strange. Clean it up. | 448 // TODO(xhwang/qinmin): This logic is correct but strange. Clean it up. |
| 375 if (output_eos_encountered_) | 449 if (output_eos_encountered_) |
| 376 status = MEDIA_CODEC_OUTPUT_END_OF_STREAM; | 450 status = MEDIA_CODEC_OUTPUT_END_OF_STREAM; |
| 377 else if (input_status == MEDIA_CODEC_INPUT_END_OF_STREAM) | |
| 378 status = MEDIA_CODEC_INPUT_END_OF_STREAM; | |
| 379 | 451 |
| 380 bool render_output = presentation_timestamp >= preroll_timestamp_ && | 452 bool render_output = presentation_timestamp >= preroll_timestamp_ && |
| 381 (status != MEDIA_CODEC_OUTPUT_END_OF_STREAM || size != 0u); | 453 (status != MEDIA_CODEC_OUTPUT_END_OF_STREAM || size != 0u); |
| 382 base::TimeDelta time_to_render; | 454 base::TimeDelta time_to_render; |
| 383 DCHECK(!start_time_ticks.is_null()); | 455 DCHECK(!start_time_ticks.is_null()); |
| 384 if (render_output && ComputeTimeToRender()) { | 456 if (render_output && ComputeTimeToRender()) { |
| 385 time_to_render = presentation_timestamp - (base::TimeTicks::Now() - | 457 time_to_render = presentation_timestamp - (base::TimeTicks::Now() - |
| 386 start_time_ticks + start_presentation_timestamp); | 458 start_time_ticks + start_presentation_timestamp); |
| 387 } | 459 } |
| 388 | 460 |
| 389 if (time_to_render > base::TimeDelta()) { | 461 if (time_to_render > base::TimeDelta()) { |
| 390 decoder_task_runner_->PostDelayedTask( | 462 decoder_task_runner_->PostDelayedTask( |
| 391 FROM_HERE, | 463 FROM_HERE, |
| 392 base::Bind(&MediaDecoderJob::ReleaseOutputBuffer, | 464 base::Bind(&MediaDecoderJob::ReleaseOutputBuffer, |
| 393 weak_factory_.GetWeakPtr(), | 465 base::Unretained(this), |
| 394 buffer_index, | 466 buffer_index, |
| 395 size, | 467 size, |
| 396 render_output, | 468 render_output, |
| 397 presentation_timestamp, | 469 presentation_timestamp, |
| 398 base::Bind(callback, status)), | 470 base::Bind(callback, status)), |
| 399 time_to_render); | 471 time_to_render); |
| 400 return; | 472 return; |
| 401 } | 473 } |
| 402 | 474 |
| 403 // TODO(qinmin): The codec is lagging behind, need to recalculate the | 475 // TODO(qinmin): The codec is lagging behind, need to recalculate the |
| (...skipping 19 matching lines...) Expand all Loading... |
| 423 MediaCodecStatus status, base::TimeDelta current_presentation_timestamp, | 495 MediaCodecStatus status, base::TimeDelta current_presentation_timestamp, |
| 424 base::TimeDelta max_presentation_timestamp) { | 496 base::TimeDelta max_presentation_timestamp) { |
| 425 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 497 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 426 | 498 |
| 427 if (destroy_pending_) { | 499 if (destroy_pending_) { |
| 428 DVLOG(1) << __FUNCTION__ << " : completing pending deletion"; | 500 DVLOG(1) << __FUNCTION__ << " : completing pending deletion"; |
| 429 delete this; | 501 delete this; |
| 430 return; | 502 return; |
| 431 } | 503 } |
| 432 | 504 |
| 505 if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM) |
| 506 output_eos_encountered_ = true; |
| 507 |
| 433 DCHECK(!decode_cb_.is_null()); | 508 DCHECK(!decode_cb_.is_null()); |
| 434 | 509 |
| 435 // If output was queued for rendering, then we have completed prerolling. | 510 // If output was queued for rendering, then we have completed prerolling. |
| 436 if (current_presentation_timestamp != kNoTimestamp()) | 511 if (current_presentation_timestamp != kNoTimestamp()) |
| 437 prerolling_ = false; | 512 prerolling_ = false; |
| 438 | 513 |
| 439 switch (status) { | 514 switch (status) { |
| 440 case MEDIA_CODEC_OK: | 515 case MEDIA_CODEC_OK: |
| 441 case MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER: | 516 case MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER: |
| 442 case MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED: | 517 case MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED: |
| 443 case MEDIA_CODEC_OUTPUT_FORMAT_CHANGED: | 518 case MEDIA_CODEC_OUTPUT_FORMAT_CHANGED: |
| 444 case MEDIA_CODEC_OUTPUT_END_OF_STREAM: | 519 case MEDIA_CODEC_OUTPUT_END_OF_STREAM: |
| 445 if (!input_eos_encountered_) | 520 if (!input_eos_encountered_) { |
| 521 CurrentDataConsumed( |
| 522 CurrentAccessUnit().status == DemuxerStream::kConfigChanged); |
| 446 access_unit_index_[current_demuxer_data_index_]++; | 523 access_unit_index_[current_demuxer_data_index_]++; |
| 524 } |
| 447 break; | 525 break; |
| 448 | 526 |
| 449 case MEDIA_CODEC_DEQUEUE_INPUT_AGAIN_LATER: | 527 case MEDIA_CODEC_DEQUEUE_INPUT_AGAIN_LATER: |
| 450 case MEDIA_CODEC_INPUT_END_OF_STREAM: | 528 case MEDIA_CODEC_INPUT_END_OF_STREAM: |
| 451 case MEDIA_CODEC_NO_KEY: | 529 case MEDIA_CODEC_NO_KEY: |
| 452 case MEDIA_CODEC_STOPPED: | 530 case MEDIA_CODEC_STOPPED: |
| 453 case MEDIA_CODEC_ERROR: | 531 case MEDIA_CODEC_ERROR: |
| 454 // Do nothing. | 532 // Do nothing. |
| 455 break; | 533 break; |
| 456 }; | 534 }; |
| 457 | 535 |
| 536 if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM && drain_decoder_) { |
| 537 OnDecoderDrained(); |
| 538 status = MEDIA_CODEC_OK; |
| 539 } |
| 540 |
| 541 if (release_resources_pending_) { |
| 542 ReleaseMediaCodecBridge(); |
| 543 release_resources_pending_ = false; |
| 544 if (drain_decoder_) |
| 545 OnDecoderDrained(); |
| 546 } |
| 547 |
| 458 stop_decode_pending_ = false; | 548 stop_decode_pending_ = false; |
| 459 base::ResetAndReturn(&decode_cb_).Run( | 549 base::ResetAndReturn(&decode_cb_).Run( |
| 460 status, current_presentation_timestamp, max_presentation_timestamp); | 550 status, current_presentation_timestamp, max_presentation_timestamp); |
| 461 } | 551 } |
| 462 | 552 |
| 463 const AccessUnit& MediaDecoderJob::CurrentAccessUnit() const { | 553 const AccessUnit& MediaDecoderJob::CurrentAccessUnit() const { |
| 464 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 554 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 465 DCHECK(HasData()); | 555 DCHECK(HasData()); |
| 466 size_t index = CurrentReceivedDataChunkIndex(); | 556 size_t index = CurrentReceivedDataChunkIndex(); |
| 467 return received_data_[index].access_units[access_unit_index_[index]]; | 557 return received_data_[index].access_units[access_unit_index_[index]]; |
| 468 } | 558 } |
| 469 | 559 |
| 470 size_t MediaDecoderJob::CurrentReceivedDataChunkIndex() const { | 560 size_t MediaDecoderJob::CurrentReceivedDataChunkIndex() const { |
| 471 return NoAccessUnitsRemainingInChunk(true) ? | 561 return NoAccessUnitsRemainingInChunk(true) ? |
| 472 inactive_demuxer_data_index() : current_demuxer_data_index_; | 562 inactive_demuxer_data_index() : current_demuxer_data_index_; |
| 473 } | 563 } |
| 474 | 564 |
| 475 bool MediaDecoderJob::NoAccessUnitsRemainingInChunk( | 565 bool MediaDecoderJob::NoAccessUnitsRemainingInChunk( |
| 476 bool is_active_chunk) const { | 566 bool is_active_chunk) const { |
| 477 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 567 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 478 size_t index = is_active_chunk ? current_demuxer_data_index_ : | 568 size_t index = is_active_chunk ? current_demuxer_data_index_ : |
| 479 inactive_demuxer_data_index(); | 569 inactive_demuxer_data_index(); |
| 480 return received_data_[index].access_units.size() <= access_unit_index_[index]; | 570 return received_data_[index].access_units.size() <= access_unit_index_[index]; |
| 481 } | 571 } |
| 482 | 572 |
| 483 void MediaDecoderJob::ClearData() { | |
| 484 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
| 485 current_demuxer_data_index_ = 0; | |
| 486 InitializeReceivedData(); | |
| 487 on_data_received_cb_.Reset(); | |
| 488 if (is_requesting_demuxer_data_) | |
| 489 is_incoming_data_invalid_ = true; | |
| 490 input_eos_encountered_ = false; | |
| 491 } | |
| 492 | |
| 493 void MediaDecoderJob::RequestCurrentChunkIfEmpty() { | 573 void MediaDecoderJob::RequestCurrentChunkIfEmpty() { |
| 494 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 574 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 495 DCHECK(HasData()); | 575 DCHECK(HasData()); |
| 496 if (!NoAccessUnitsRemainingInChunk(true)) | 576 if (!NoAccessUnitsRemainingInChunk(true)) |
| 497 return; | 577 return; |
| 498 | 578 |
| 499 // Requests new data if the the last access unit of the next chunk is not EOS. | 579 // Requests new data if the the last access unit of the next chunk is not EOS. |
| 500 current_demuxer_data_index_ = inactive_demuxer_data_index(); | 580 current_demuxer_data_index_ = inactive_demuxer_data_index(); |
| 501 const AccessUnit last_access_unit = | 581 const AccessUnit last_access_unit = |
| 502 received_data_[current_demuxer_data_index_].access_units.back(); | 582 received_data_[current_demuxer_data_index_].access_units.back(); |
| 503 if (!last_access_unit.end_of_stream && | 583 if (!last_access_unit.end_of_stream && |
| 504 last_access_unit.status != DemuxerStream::kConfigChanged && | |
| 505 last_access_unit.status != DemuxerStream::kAborted) { | 584 last_access_unit.status != DemuxerStream::kAborted) { |
| 506 RequestData(base::Closure()); | 585 RequestData(base::Closure()); |
| 507 } | 586 } |
| 508 } | 587 } |
| 509 | 588 |
| 510 void MediaDecoderJob::InitializeReceivedData() { | 589 void MediaDecoderJob::InitializeReceivedData() { |
| 511 for (size_t i = 0; i < 2; ++i) { | 590 for (size_t i = 0; i < 2; ++i) { |
| 512 received_data_[i] = DemuxerData(); | 591 received_data_[i] = DemuxerData(); |
| 513 access_unit_index_[i] = 0; | 592 access_unit_index_[i] = 0; |
| 514 } | 593 } |
| 515 } | 594 } |
| 516 | 595 |
| 596 void MediaDecoderJob::OnDecoderDrained() { |
| 597 DVLOG(1) << __FUNCTION__; |
| 598 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 599 DCHECK(drain_decoder_); |
| 600 |
| 601 input_eos_encountered_ = false; |
| 602 output_eos_encountered_ = false; |
| 603 drain_decoder_ = false; |
| 604 ReleaseMediaCodecBridge(); |
| 605 // Increase the access unit index so that the new decoder will not handle |
| 606 // the config change again. |
| 607 access_unit_index_[current_demuxer_data_index_]++; |
| 608 CurrentDataConsumed(true); |
| 609 } |
| 610 |
| 611 bool MediaDecoderJob::CreateMediaCodecBridge() { |
| 612 DVLOG(1) << __FUNCTION__; |
| 613 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 614 DCHECK(decode_cb_.is_null()); |
| 615 |
| 616 if (!HasStream()) { |
| 617 ReleaseMediaCodecBridge(); |
| 618 return false; |
| 619 } |
| 620 |
| 621 // Create |media_codec_bridge_| only if config changes. |
| 622 if (media_codec_bridge_ && !need_to_reconfig_decoder_job_) |
| 623 return true; |
| 624 |
| 625 base::android::ScopedJavaLocalRef<jobject> media_crypto = GetMediaCrypto(); |
| 626 if (is_content_encrypted_ && media_crypto.is_null()) |
| 627 return false; |
| 628 |
| 629 ReleaseMediaCodecBridge(); |
| 630 DVLOG(1) << __FUNCTION__ << " : creating new media codec bridge"; |
| 631 |
| 632 return CreateMediaCodecBridgeInternal(); |
| 633 } |
| 634 |
| 635 void MediaDecoderJob::ReleaseMediaCodecBridge() { |
| 636 if (!media_codec_bridge_) |
| 637 return; |
| 638 |
| 639 media_codec_bridge_.reset(); |
| 640 OnMediaCodecBridgeReleased(); |
| 641 } |
| 642 |
| 517 } // namespace media | 643 } // namespace media |
| OLD | NEW |