OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_codec_loop.h" | 5 #include "media/base/android/media_codec_loop.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/logging.h" | 9 #include "base/logging.h" |
10 #include "media/base/bind_to_current_loop.h" | 10 #include "media/base/bind_to_current_loop.h" |
11 #include "media/base/timestamp_constants.h" | 11 #include "media/base/timestamp_constants.h" |
12 | 12 |
13 namespace media { | 13 namespace media { |
| 14 namespace { |
14 | 15 |
15 // Declaring these as constexpr variables doesn't work in windows -- they | 16 constexpr base::TimeDelta kDecodePollDelay = |
16 // always are 0. The exception is FromMicroseconds, which doesn't do any | 17 base::TimeDelta::FromMilliseconds(10); |
17 // conversion. However, declaring these as constexpr functions seesm to work | 18 constexpr base::TimeDelta kNoWaitTimeout = base::TimeDelta::FromMicroseconds(0); |
18 // fine everywhere. We care that this works in windows because our unit tests | 19 constexpr base::TimeDelta kIdleTimerTimeout = base::TimeDelta::FromSeconds(1); |
19 // run on non-android platforms. | |
20 constexpr base::TimeDelta DecodePollDelay() { | |
21 return base::TimeDelta::FromMilliseconds(10); | |
22 } | |
23 | 20 |
24 constexpr base::TimeDelta NoWaitTimeout() { | 21 } // namespace |
25 return base::TimeDelta::FromMicroseconds(0); | |
26 } | |
27 | |
28 constexpr base::TimeDelta IdleTimerTimeout() { | |
29 return base::TimeDelta::FromSeconds(1); | |
30 } | |
31 | 22 |
32 MediaCodecLoop::InputData::InputData() {} | 23 MediaCodecLoop::InputData::InputData() {} |
33 | 24 |
34 MediaCodecLoop::InputData::InputData(const InputData& other) | 25 MediaCodecLoop::InputData::InputData(const InputData& other) |
35 : memory(other.memory), | 26 : memory(other.memory), |
36 length(other.length), | 27 length(other.length), |
37 key_id(other.key_id), | 28 key_id(other.key_id), |
38 iv(other.iv), | 29 iv(other.iv), |
39 subsamples(other.subsamples), | 30 subsamples(other.subsamples), |
40 presentation_time(other.presentation_time), | 31 presentation_time(other.presentation_time), |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 // MediaCodec's QueueSecureInputBuffer(). | 141 // MediaCodec's QueueSecureInputBuffer(). |
151 if (pending_input_buf_index_ != kInvalidBufferIndex) { | 142 if (pending_input_buf_index_ != kInvalidBufferIndex) { |
152 InputBuffer result(pending_input_buf_index_, true); | 143 InputBuffer result(pending_input_buf_index_, true); |
153 pending_input_buf_index_ = kInvalidBufferIndex; | 144 pending_input_buf_index_ = kInvalidBufferIndex; |
154 return result; | 145 return result; |
155 } | 146 } |
156 | 147 |
157 int input_buf_index = kInvalidBufferIndex; | 148 int input_buf_index = kInvalidBufferIndex; |
158 | 149 |
159 media::MediaCodecStatus status = | 150 media::MediaCodecStatus status = |
160 media_codec_->DequeueInputBuffer(NoWaitTimeout(), &input_buf_index); | 151 media_codec_->DequeueInputBuffer(kNoWaitTimeout, &input_buf_index); |
161 switch (status) { | 152 switch (status) { |
162 case media::MEDIA_CODEC_DEQUEUE_INPUT_AGAIN_LATER: | 153 case media::MEDIA_CODEC_DEQUEUE_INPUT_AGAIN_LATER: |
163 break; | 154 break; |
164 | 155 |
165 case media::MEDIA_CODEC_ERROR: | 156 case media::MEDIA_CODEC_ERROR: |
166 DLOG(ERROR) << __func__ << ": MEDIA_CODEC_ERROR from DequeInputBuffer"; | 157 DLOG(ERROR) << __func__ << ": MEDIA_CODEC_ERROR from DequeInputBuffer"; |
167 SetState(STATE_ERROR); | 158 SetState(STATE_ERROR); |
168 break; | 159 break; |
169 | 160 |
170 case media::MEDIA_CODEC_OK: | 161 case media::MEDIA_CODEC_OK: |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 | 241 |
251 bool MediaCodecLoop::ProcessOneOutputBuffer() { | 242 bool MediaCodecLoop::ProcessOneOutputBuffer() { |
252 // TODO(liberato): When merging AVDA, we will also have to ask the client if | 243 // TODO(liberato): When merging AVDA, we will also have to ask the client if |
253 // it can accept another output buffer. | 244 // it can accept another output buffer. |
254 | 245 |
255 if (state_ == STATE_ERROR) | 246 if (state_ == STATE_ERROR) |
256 return false; | 247 return false; |
257 | 248 |
258 OutputBuffer out; | 249 OutputBuffer out; |
259 MediaCodecStatus status = media_codec_->DequeueOutputBuffer( | 250 MediaCodecStatus status = media_codec_->DequeueOutputBuffer( |
260 NoWaitTimeout(), &out.index, &out.offset, &out.size, &out.pts, | 251 kNoWaitTimeout, &out.index, &out.offset, &out.size, &out.pts, &out.is_eos, |
261 &out.is_eos, &out.is_key_frame); | 252 &out.is_key_frame); |
262 | 253 |
263 bool did_work = false; | 254 bool did_work = false; |
264 switch (status) { | 255 switch (status) { |
265 case MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED: | 256 case MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED: |
266 // Output buffers are replaced in MediaCodecBridge, nothing to do. | 257 // Output buffers are replaced in MediaCodecBridge, nothing to do. |
267 did_work = true; | 258 did_work = true; |
268 break; | 259 break; |
269 | 260 |
270 case MEDIA_CODEC_OUTPUT_FORMAT_CHANGED: | 261 case MEDIA_CODEC_OUTPUT_FORMAT_CHANGED: |
271 if (!client_->OnOutputFormatChanged()) | 262 if (!client_->OnOutputFormatChanged()) |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 void MediaCodecLoop::ManageTimer(bool did_work) { | 308 void MediaCodecLoop::ManageTimer(bool did_work) { |
318 bool should_be_running = true; | 309 bool should_be_running = true; |
319 | 310 |
320 // One might also use DefaultTickClock, but then ownership becomes harder. | 311 // One might also use DefaultTickClock, but then ownership becomes harder. |
321 base::TimeTicks now = (test_tick_clock_ ? test_tick_clock_->NowTicks() | 312 base::TimeTicks now = (test_tick_clock_ ? test_tick_clock_->NowTicks() |
322 : base::TimeTicks::Now()); | 313 : base::TimeTicks::Now()); |
323 if (did_work || idle_time_begin_ == base::TimeTicks()) { | 314 if (did_work || idle_time_begin_ == base::TimeTicks()) { |
324 idle_time_begin_ = now; | 315 idle_time_begin_ = now; |
325 } else { | 316 } else { |
326 // Make sure that we have done work recently enough, else stop the timer. | 317 // Make sure that we have done work recently enough, else stop the timer. |
327 if (now - idle_time_begin_ > IdleTimerTimeout()) | 318 if (now - idle_time_begin_ > kIdleTimerTimeout) |
328 should_be_running = false; | 319 should_be_running = false; |
329 } | 320 } |
330 | 321 |
331 if (should_be_running && !io_timer_.IsRunning()) { | 322 if (should_be_running && !io_timer_.IsRunning()) { |
332 io_timer_.Start(FROM_HERE, DecodePollDelay(), this, | 323 io_timer_.Start(FROM_HERE, kDecodePollDelay, this, |
333 &MediaCodecLoop::DoPendingWork); | 324 &MediaCodecLoop::DoPendingWork); |
334 } else if (!should_be_running && io_timer_.IsRunning()) { | 325 } else if (!should_be_running && io_timer_.IsRunning()) { |
335 io_timer_.Stop(); | 326 io_timer_.Stop(); |
336 } | 327 } |
337 } | 328 } |
338 | 329 |
339 void MediaCodecLoop::SetState(State new_state) { | 330 void MediaCodecLoop::SetState(State new_state) { |
340 const State old_state = state_; | 331 const State old_state = state_; |
341 state_ = new_state; | 332 state_ = new_state; |
342 if (old_state != new_state && new_state == STATE_ERROR) | 333 if (old_state != new_state && new_state == STATE_ERROR) |
(...skipping 26 matching lines...) Expand all Loading... |
369 RETURN_STRING(STATE_DRAINED); | 360 RETURN_STRING(STATE_DRAINED); |
370 RETURN_STRING(STATE_ERROR); | 361 RETURN_STRING(STATE_ERROR); |
371 } | 362 } |
372 #undef RETURN_STRING | 363 #undef RETURN_STRING |
373 | 364 |
374 NOTREACHED() << "Unknown state " << state; | 365 NOTREACHED() << "Unknown state " << state; |
375 return nullptr; | 366 return nullptr; |
376 } | 367 } |
377 | 368 |
378 } // namespace media | 369 } // namespace media |
OLD | NEW |