[email protected] | cc3933d | 2013-11-26 03:41:02 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | ce553ab | 2009-02-24 01:25:14 | [diff] [blame] | 4 | |
servolk | f54f5c8f | 2015-02-24 20:32:39 | [diff] [blame] | 5 | #include "media/renderers/video_renderer_impl.h" |
[email protected] | f7c0ada0 | 2012-07-23 08:42:50 | [diff] [blame] | 6 | |
[email protected] | c369bb8 | 2011-04-27 22:45:43 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 8 | #include "base/callback.h" |
[email protected] | 1192339c | 2012-03-24 20:37:27 | [diff] [blame] | 9 | #include "base/callback_helpers.h" |
[email protected] | b639a83 | 2013-12-20 21:12:39 | [diff] [blame] | 10 | #include "base/location.h" |
| 11 | #include "base/single_thread_task_runner.h" |
[email protected] | f214f879 | 2011-01-01 02:17:08 | [diff] [blame] | 12 | #include "base/threading/platform_thread.h" |
dalecurtis | 5755042e | 2015-03-26 20:31:22 | [diff] [blame^] | 13 | #include "base/time/default_tick_clock.h" |
ssid | 9525f467 | 2015-01-28 12:13:15 | [diff] [blame] | 14 | #include "base/trace_event/trace_event.h" |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 15 | #include "media/base/bind_to_current_loop.h" |
[email protected] | ce553ab | 2009-02-24 01:25:14 | [diff] [blame] | 16 | #include "media/base/buffers.h" |
[email protected] | 34d14848 | 2010-09-02 00:15:55 | [diff] [blame] | 17 | #include "media/base/limits.h" |
[email protected] | d399866c | 2012-01-18 09:30:32 | [diff] [blame] | 18 | #include "media/base/pipeline.h" |
[email protected] | 34f99356 | 2010-03-23 22:34:24 | [diff] [blame] | 19 | #include "media/base/video_frame.h" |
[email protected] | ce553ab | 2009-02-24 01:25:14 | [diff] [blame] | 20 | |
| 21 | namespace media { |
| 22 | |
[email protected] | cc3933d | 2013-11-26 03:41:02 | [diff] [blame] | 23 | VideoRendererImpl::VideoRendererImpl( |
[email protected] | b639a83 | 2013-12-20 21:12:39 | [diff] [blame] | 24 | const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 25 | ScopedVector<VideoDecoder> decoders, |
xhwang | ff459d4e | 2014-09-09 04:55:47 | [diff] [blame] | 26 | bool drop_frames, |
| 27 | const scoped_refptr<MediaLog>& media_log) |
[email protected] | b639a83 | 2013-12-20 21:12:39 | [diff] [blame] | 28 | : task_runner_(task_runner), |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 29 | video_frame_stream_( |
| 30 | new VideoFrameStream(task_runner, decoders.Pass(), media_log)), |
[email protected] | a29fd9b | 2014-05-02 17:43:37 | [diff] [blame] | 31 | low_delay_(false), |
[email protected] | 6eda4e4 | 2013-02-28 21:59:54 | [diff] [blame] | 32 | received_end_of_stream_(false), |
[email protected] | 2267bfe8 | 2014-05-09 18:03:58 | [diff] [blame] | 33 | rendered_end_of_stream_(false), |
[email protected] | 1276cc16 | 2012-12-07 21:55:33 | [diff] [blame] | 34 | frame_available_(&lock_), |
[email protected] | 3545897 | 2009-07-25 02:12:11 | [diff] [blame] | 35 | state_(kUninitialized), |
[email protected] | 54e6ff8 | 2013-05-22 00:01:38 | [diff] [blame] | 36 | thread_(), |
[email protected] | f23676a | 2011-11-04 02:04:09 | [diff] [blame] | 37 | pending_read_(false), |
[email protected] | b3c47179 | 2012-03-17 01:34:26 | [diff] [blame] | 38 | drop_frames_(drop_frames), |
[email protected] | 28bc175c | 2014-07-01 19:39:17 | [diff] [blame] | 39 | buffering_state_(BUFFERING_HAVE_NOTHING), |
[email protected] | f54e768 | 2013-10-10 21:58:26 | [diff] [blame] | 40 | frames_decoded_(0), |
[email protected] | de39b02 | 2014-03-18 21:36:22 | [diff] [blame] | 41 | frames_dropped_(0), |
[email protected] | 47affc7 | 2014-07-23 01:49:15 | [diff] [blame] | 42 | is_shutting_down_(false), |
dalecurtis | 5755042e | 2015-03-26 20:31:22 | [diff] [blame^] | 43 | tick_clock_(new base::DefaultTickClock()), |
[email protected] | de39b02 | 2014-03-18 21:36:22 | [diff] [blame] | 44 | weak_factory_(this) { |
[email protected] | ce553ab | 2009-02-24 01:25:14 | [diff] [blame] | 45 | } |
| 46 | |
[email protected] | cc3933d | 2013-11-26 03:41:02 | [diff] [blame] | 47 | VideoRendererImpl::~VideoRendererImpl() { |
[email protected] | 47affc7 | 2014-07-23 01:49:15 | [diff] [blame] | 48 | DCHECK(task_runner_->BelongsToCurrentThread()); |
| 49 | |
| 50 | { |
| 51 | base::AutoLock auto_lock(lock_); |
| 52 | is_shutting_down_ = true; |
| 53 | frame_available_.Signal(); |
| 54 | } |
| 55 | |
| 56 | if (!thread_.is_null()) |
| 57 | base::PlatformThread::Join(thread_); |
[email protected] | 5a10c79 | 2014-07-23 09:52:20 | [diff] [blame] | 58 | |
| 59 | if (!init_cb_.is_null()) |
| 60 | base::ResetAndReturn(&init_cb_).Run(PIPELINE_ERROR_ABORT); |
| 61 | |
| 62 | if (!flush_cb_.is_null()) |
| 63 | base::ResetAndReturn(&flush_cb_).Run(); |
[email protected] | 30ef6b394 | 2013-02-05 00:56:27 | [diff] [blame] | 64 | } |
| 65 | |
[email protected] | cc3933d | 2013-11-26 03:41:02 | [diff] [blame] | 66 | void VideoRendererImpl::Flush(const base::Closure& callback) { |
scherkus | a62dfabc6 | 2014-09-03 06:46:20 | [diff] [blame] | 67 | DVLOG(1) << __FUNCTION__; |
[email protected] | b639a83 | 2013-12-20 21:12:39 | [diff] [blame] | 68 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 69 | base::AutoLock auto_lock(lock_); |
[email protected] | 28bc175c | 2014-07-01 19:39:17 | [diff] [blame] | 70 | DCHECK_EQ(state_, kPlaying); |
[email protected] | a4dae8e | 2012-03-15 23:35:31 | [diff] [blame] | 71 | flush_cb_ = callback; |
[email protected] | 971db8c | 2013-07-12 01:52:36 | [diff] [blame] | 72 | state_ = kFlushing; |
[email protected] | 3d54fc6 | 2010-08-11 23:24:58 | [diff] [blame] | 73 | |
[email protected] | 971db8c | 2013-07-12 01:52:36 | [diff] [blame] | 74 | // This is necessary if the |video_frame_stream_| has already seen an end of |
| 75 | // stream and needs to drain it before flushing it. |
[email protected] | f8b4734 | 2013-03-27 12:20:21 | [diff] [blame] | 76 | ready_frames_.clear(); |
[email protected] | 28bc175c | 2014-07-01 19:39:17 | [diff] [blame] | 77 | if (buffering_state_ != BUFFERING_HAVE_NOTHING) { |
| 78 | buffering_state_ = BUFFERING_HAVE_NOTHING; |
| 79 | buffering_state_cb_.Run(BUFFERING_HAVE_NOTHING); |
| 80 | } |
[email protected] | f8b4734 | 2013-03-27 12:20:21 | [diff] [blame] | 81 | received_end_of_stream_ = false; |
[email protected] | 2267bfe8 | 2014-05-09 18:03:58 | [diff] [blame] | 82 | rendered_end_of_stream_ = false; |
[email protected] | 28bc175c | 2014-07-01 19:39:17 | [diff] [blame] | 83 | |
[email protected] | 4beec40 | 2014-07-17 08:32:52 | [diff] [blame] | 84 | video_frame_stream_->Reset( |
[email protected] | de39b02 | 2014-03-18 21:36:22 | [diff] [blame] | 85 | base::Bind(&VideoRendererImpl::OnVideoFrameStreamResetDone, |
| 86 | weak_factory_.GetWeakPtr())); |
[email protected] | 3545897 | 2009-07-25 02:12:11 | [diff] [blame] | 87 | } |
| 88 | |
scherkus | b6f074ea | 2014-09-16 03:51:32 | [diff] [blame] | 89 | void VideoRendererImpl::StartPlayingFrom(base::TimeDelta timestamp) { |
| 90 | DVLOG(1) << __FUNCTION__ << "(" << timestamp.InMicroseconds() << ")"; |
[email protected] | b639a83 | 2013-12-20 21:12:39 | [diff] [blame] | 91 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | e1d69d76 | 2011-12-13 05:12:18 | [diff] [blame] | 92 | base::AutoLock auto_lock(lock_); |
[email protected] | 28bc175c | 2014-07-01 19:39:17 | [diff] [blame] | 93 | DCHECK_EQ(state_, kFlushed); |
| 94 | DCHECK(!pending_read_); |
| 95 | DCHECK(ready_frames_.empty()); |
| 96 | DCHECK_EQ(buffering_state_, BUFFERING_HAVE_NOTHING); |
[email protected] | 4b6e4a9e | 2013-12-04 01:11:10 | [diff] [blame] | 97 | |
[email protected] | 28bc175c | 2014-07-01 19:39:17 | [diff] [blame] | 98 | state_ = kPlaying; |
scherkus | b6f074ea | 2014-09-16 03:51:32 | [diff] [blame] | 99 | start_timestamp_ = timestamp; |
[email protected] | e1d69d76 | 2011-12-13 05:12:18 | [diff] [blame] | 100 | AttemptRead_Locked(); |
[email protected] | ce553ab | 2009-02-24 01:25:14 | [diff] [blame] | 101 | } |
| 102 | |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 103 | void VideoRendererImpl::Initialize( |
| 104 | DemuxerStream* stream, |
| 105 | const PipelineStatusCB& init_cb, |
| 106 | const SetDecryptorReadyCB& set_decryptor_ready_cb, |
| 107 | const StatisticsCB& statistics_cb, |
| 108 | const BufferingStateCB& buffering_state_cb, |
| 109 | const PaintCB& paint_cb, |
| 110 | const base::Closure& ended_cb, |
| 111 | const PipelineStatusCB& error_cb, |
dalecurtis | 5755042e | 2015-03-26 20:31:22 | [diff] [blame^] | 112 | const WallClockTimeCB& wall_clock_time_cb, |
jrummell | 74fc4f94 | 2015-03-02 22:48:27 | [diff] [blame] | 113 | const base::Closure& waiting_for_decryption_key_cb) { |
[email protected] | b639a83 | 2013-12-20 21:12:39 | [diff] [blame] | 114 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 115 | base::AutoLock auto_lock(lock_); |
[email protected] | 873da26 | 2012-08-10 22:02:47 | [diff] [blame] | 116 | DCHECK(stream); |
[email protected] | 873da26 | 2012-08-10 22:02:47 | [diff] [blame] | 117 | DCHECK_EQ(stream->type(), DemuxerStream::VIDEO); |
[email protected] | f7c0ada0 | 2012-07-23 08:42:50 | [diff] [blame] | 118 | DCHECK(!init_cb.is_null()); |
[email protected] | ad8b08d | 2012-03-10 00:38:57 | [diff] [blame] | 119 | DCHECK(!statistics_cb.is_null()); |
[email protected] | 28bc175c | 2014-07-01 19:39:17 | [diff] [blame] | 120 | DCHECK(!buffering_state_cb.is_null()); |
xhwang | 94c0bd3 | 2014-11-13 20:49:31 | [diff] [blame] | 121 | DCHECK(!paint_cb.is_null()); |
[email protected] | f7c0ada0 | 2012-07-23 08:42:50 | [diff] [blame] | 122 | DCHECK(!ended_cb.is_null()); |
dalecurtis | 5755042e | 2015-03-26 20:31:22 | [diff] [blame^] | 123 | DCHECK(!wall_clock_time_cb.is_null()); |
[email protected] | 3545897 | 2009-07-25 02:12:11 | [diff] [blame] | 124 | DCHECK_EQ(kUninitialized, state_); |
[email protected] | 1f500f5 | 2009-06-22 22:23:19 | [diff] [blame] | 125 | |
xhwang | 2f7e4aca | 2014-11-14 18:37:36 | [diff] [blame] | 126 | low_delay_ = (stream->liveness() == DemuxerStream::LIVENESS_LIVE); |
[email protected] | a29fd9b | 2014-05-02 17:43:37 | [diff] [blame] | 127 | |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 128 | // Always post |init_cb_| because |this| could be destroyed if initialization |
| 129 | // failed. |
| 130 | init_cb_ = BindToCurrentLoop(init_cb); |
| 131 | |
[email protected] | ad8b08d | 2012-03-10 00:38:57 | [diff] [blame] | 132 | statistics_cb_ = statistics_cb; |
[email protected] | 28bc175c | 2014-07-01 19:39:17 | [diff] [blame] | 133 | buffering_state_cb_ = buffering_state_cb; |
xhwang | 94c0bd3 | 2014-11-13 20:49:31 | [diff] [blame] | 134 | paint_cb_ = paint_cb, |
[email protected] | f7c0ada0 | 2012-07-23 08:42:50 | [diff] [blame] | 135 | ended_cb_ = ended_cb; |
| 136 | error_cb_ = error_cb; |
dalecurtis | 5755042e | 2015-03-26 20:31:22 | [diff] [blame^] | 137 | wall_clock_time_cb_ = wall_clock_time_cb; |
[email protected] | a1f7ace | 2013-02-22 06:14:07 | [diff] [blame] | 138 | state_ = kInitializing; |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 139 | |
[email protected] | 4beec40 | 2014-07-17 08:32:52 | [diff] [blame] | 140 | video_frame_stream_->Initialize( |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 141 | stream, base::Bind(&VideoRendererImpl::OnVideoFrameStreamInitialized, |
| 142 | weak_factory_.GetWeakPtr()), |
jrummell | 74fc4f94 | 2015-03-02 22:48:27 | [diff] [blame] | 143 | set_decryptor_ready_cb, statistics_cb, waiting_for_decryption_key_cb); |
[email protected] | 873da26 | 2012-08-10 22:02:47 | [diff] [blame] | 144 | } |
| 145 | |
xhwang | dc965d9 | 2014-12-18 23:21:11 | [diff] [blame] | 146 | void VideoRendererImpl::CreateVideoThread() { |
| 147 | // This may fail and cause a crash if there are too many threads created in |
| 148 | // the current process. See http://crbug.com/443291 |
| 149 | CHECK(base::PlatformThread::Create(0, this, &thread_)); |
| 150 | |
| 151 | #if defined(OS_WIN) |
| 152 | // Bump up our priority so our sleeping is more accurate. |
| 153 | // TODO(scherkus): find out if this is necessary, but it seems to help. |
| 154 | ::SetThreadPriority(thread_.platform_handle(), THREAD_PRIORITY_ABOVE_NORMAL); |
| 155 | #endif // defined(OS_WIN) |
| 156 | } |
| 157 | |
[email protected] | 75e145a | 2014-04-15 17:44:32 | [diff] [blame] | 158 | void VideoRendererImpl::OnVideoFrameStreamInitialized(bool success) { |
[email protected] | b639a83 | 2013-12-20 21:12:39 | [diff] [blame] | 159 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 873da26 | 2012-08-10 22:02:47 | [diff] [blame] | 160 | base::AutoLock auto_lock(lock_); |
[email protected] | a1f7ace | 2013-02-22 06:14:07 | [diff] [blame] | 161 | DCHECK_EQ(state_, kInitializing); |
| 162 | |
[email protected] | 84fb5a06 | 2013-03-24 04:06:44 | [diff] [blame] | 163 | if (!success) { |
[email protected] | 7004b73 | 2012-12-17 23:07:06 | [diff] [blame] | 164 | state_ = kUninitialized; |
| 165 | base::ResetAndReturn(&init_cb_).Run(DECODER_ERROR_NOT_SUPPORTED); |
[email protected] | 873da26 | 2012-08-10 22:02:47 | [diff] [blame] | 166 | return; |
| 167 | } |
| 168 | |
[email protected] | 34d14848 | 2010-09-02 00:15:55 | [diff] [blame] | 169 | // We're all good! Consider ourselves flushed. (ThreadMain() should never |
[email protected] | 3545897 | 2009-07-25 02:12:11 | [diff] [blame] | 170 | // see us in the kUninitialized state). |
[email protected] | de8e9b6 | 2012-07-24 19:25:17 | [diff] [blame] | 171 | // Since we had an initial Preroll(), we consider ourself flushed, because we |
[email protected] | 34d14848 | 2010-09-02 00:15:55 | [diff] [blame] | 172 | // have not populated any buffers yet. |
| 173 | state_ = kFlushed; |
[email protected] | 3545897 | 2009-07-25 02:12:11 | [diff] [blame] | 174 | |
xhwang | dc965d9 | 2014-12-18 23:21:11 | [diff] [blame] | 175 | CreateVideoThread(); |
[email protected] | 1f500f5 | 2009-06-22 22:23:19 | [diff] [blame] | 176 | |
[email protected] | 873da26 | 2012-08-10 22:02:47 | [diff] [blame] | 177 | base::ResetAndReturn(&init_cb_).Run(PIPELINE_OK); |
| 178 | } |
| 179 | |
[email protected] | 1f500f5 | 2009-06-22 22:23:19 | [diff] [blame] | 180 | // PlatformThread::Delegate implementation. |
[email protected] | cc3933d | 2013-11-26 03:41:02 | [diff] [blame] | 181 | void VideoRendererImpl::ThreadMain() { |
[email protected] | f214f879 | 2011-01-01 02:17:08 | [diff] [blame] | 182 | base::PlatformThread::SetName("CrVideoRenderer"); |
[email protected] | 3ad9d5b | 2011-12-22 16:43:26 | [diff] [blame] | 183 | |
| 184 | // The number of milliseconds to idle when we do not have anything to do. |
| 185 | // Nothing special about the value, other than we're being more OS-friendly |
| 186 | // than sleeping for 1 millisecond. |
| 187 | // |
[email protected] | 971db8c | 2013-07-12 01:52:36 | [diff] [blame] | 188 | // TODO(scherkus): switch to pure event-driven frame timing instead of this |
[email protected] | 3ad9d5b | 2011-12-22 16:43:26 | [diff] [blame] | 189 | // kIdleTimeDelta business http://crbug.com/106874 |
| 190 | const base::TimeDelta kIdleTimeDelta = |
| 191 | base::TimeDelta::FromMilliseconds(10); |
[email protected] | d3d04877 | 2010-07-14 21:00:25 | [diff] [blame] | 192 | |
[email protected] | e58c5e33 | 2014-08-15 04:43:52 | [diff] [blame] | 193 | // If we have no frames and haven't painted any frame for certain amount of |
| 194 | // time, declare BUFFERING_HAVE_NOTHING. |
| 195 | const base::TimeDelta kTimeToDeclareHaveNothing = |
| 196 | base::TimeDelta::FromSeconds(3); |
| 197 | |
[email protected] | 1f500f5 | 2009-06-22 22:23:19 | [diff] [blame] | 198 | for (;;) { |
[email protected] | d957e43 | 2012-12-21 00:35:33 | [diff] [blame] | 199 | base::AutoLock auto_lock(lock_); |
| 200 | |
| 201 | // Thread exit condition. |
[email protected] | 47affc7 | 2014-07-23 01:49:15 | [diff] [blame] | 202 | if (is_shutting_down_) |
[email protected] | d957e43 | 2012-12-21 00:35:33 | [diff] [blame] | 203 | return; |
| 204 | |
[email protected] | 3ad9d5b | 2011-12-22 16:43:26 | [diff] [blame] | 205 | // Remain idle as long as we're not playing. |
[email protected] | bd22a7e | 2014-07-14 23:14:45 | [diff] [blame] | 206 | if (state_ != kPlaying || buffering_state_ != BUFFERING_HAVE_ENOUGH) { |
[email protected] | f54e768 | 2013-10-10 21:58:26 | [diff] [blame] | 207 | UpdateStatsAndWait_Locked(kIdleTimeDelta); |
[email protected] | 3ad9d5b | 2011-12-22 16:43:26 | [diff] [blame] | 208 | continue; |
[email protected] | 1f500f5 | 2009-06-22 22:23:19 | [diff] [blame] | 209 | } |
[email protected] | ce553ab | 2009-02-24 01:25:14 | [diff] [blame] | 210 | |
dalecurtis | 5755042e | 2015-03-26 20:31:22 | [diff] [blame^] | 211 | base::TimeTicks now = tick_clock_->NowTicks(); |
[email protected] | e58c5e33 | 2014-08-15 04:43:52 | [diff] [blame] | 212 | |
[email protected] | 3ad9d5b | 2011-12-22 16:43:26 | [diff] [blame] | 213 | // Remain idle until we have the next frame ready for rendering. |
| 214 | if (ready_frames_.empty()) { |
[email protected] | 5f60f910 | 2014-07-16 21:05:21 | [diff] [blame] | 215 | if (received_end_of_stream_) { |
| 216 | if (!rendered_end_of_stream_) { |
| 217 | rendered_end_of_stream_ = true; |
[email protected] | 9dc5c0b | 2014-07-31 13:16:38 | [diff] [blame] | 218 | task_runner_->PostTask(FROM_HERE, ended_cb_); |
[email protected] | 5f60f910 | 2014-07-16 21:05:21 | [diff] [blame] | 219 | } |
dalecurtis | 5755042e | 2015-03-26 20:31:22 | [diff] [blame^] | 220 | } else if (!last_painted_time_.is_null() && |
| 221 | now - last_painted_time_ >= kTimeToDeclareHaveNothing) { |
[email protected] | 5f60f910 | 2014-07-16 21:05:21 | [diff] [blame] | 222 | buffering_state_ = BUFFERING_HAVE_NOTHING; |
| 223 | task_runner_->PostTask( |
| 224 | FROM_HERE, base::Bind(buffering_state_cb_, BUFFERING_HAVE_NOTHING)); |
[email protected] | 6eda4e4 | 2013-02-28 21:59:54 | [diff] [blame] | 225 | } |
| 226 | |
[email protected] | f54e768 | 2013-10-10 21:58:26 | [diff] [blame] | 227 | UpdateStatsAndWait_Locked(kIdleTimeDelta); |
[email protected] | 3ad9d5b | 2011-12-22 16:43:26 | [diff] [blame] | 228 | continue; |
| 229 | } |
[email protected] | 34d14848 | 2010-09-02 00:15:55 | [diff] [blame] | 230 | |
dalecurtis | 5755042e | 2015-03-26 20:31:22 | [diff] [blame^] | 231 | base::TimeTicks target_paint_time = |
| 232 | wall_clock_time_cb_.Run(ready_frames_.front()->timestamp()); |
| 233 | |
| 234 | // If media time has stopped, don't attempt to paint any more frames. |
| 235 | if (target_paint_time.is_null()) { |
| 236 | UpdateStatsAndWait_Locked(kIdleTimeDelta); |
| 237 | continue; |
| 238 | } |
| 239 | |
| 240 | base::TimeTicks latest_possible_paint_time; |
[email protected] | dfb28d8 | 2014-07-14 02:48:56 | [diff] [blame] | 241 | |
[email protected] | bd22a7e | 2014-07-14 23:14:45 | [diff] [blame] | 242 | // Deadline is defined as the duration between this frame and the next |
[email protected] | e002586 | 2013-02-05 00:35:16 | [diff] [blame] | 243 | // frame, using the delta between this frame and the previous frame as the |
| 244 | // assumption for frame duration. |
[email protected] | 3ad9d5b | 2011-12-22 16:43:26 | [diff] [blame] | 245 | // |
[email protected] | e002586 | 2013-02-05 00:35:16 | [diff] [blame] | 246 | // TODO(scherkus): This can be vastly improved. Use a histogram to measure |
| 247 | // the accuracy of our frame timing code. http://crbug.com/149829 |
dalecurtis | 5755042e | 2015-03-26 20:31:22 | [diff] [blame^] | 248 | if (last_media_time_.is_null()) { |
| 249 | latest_possible_paint_time = now; |
[email protected] | bd22a7e | 2014-07-14 23:14:45 | [diff] [blame] | 250 | } else { |
dalecurtis | 5755042e | 2015-03-26 20:31:22 | [diff] [blame^] | 251 | base::TimeDelta duration = target_paint_time - last_media_time_; |
| 252 | latest_possible_paint_time = target_paint_time + duration; |
[email protected] | bd22a7e | 2014-07-14 23:14:45 | [diff] [blame] | 253 | } |
[email protected] | 3ad9d5b | 2011-12-22 16:43:26 | [diff] [blame] | 254 | |
[email protected] | bd22a7e | 2014-07-14 23:14:45 | [diff] [blame] | 255 | // Remain idle until we've reached our target paint window. |
dalecurtis | 5755042e | 2015-03-26 20:31:22 | [diff] [blame^] | 256 | if (now < target_paint_time) { |
lionel.g.landwerlin | f509661 | 2015-03-25 00:09:59 | [diff] [blame] | 257 | UpdateStatsAndWait_Locked( |
dalecurtis | 5755042e | 2015-03-26 20:31:22 | [diff] [blame^] | 258 | std::min(target_paint_time - now, kIdleTimeDelta)); |
[email protected] | bd22a7e | 2014-07-14 23:14:45 | [diff] [blame] | 259 | continue; |
| 260 | } |
| 261 | |
dalecurtis | 5755042e | 2015-03-26 20:31:22 | [diff] [blame^] | 262 | if (ready_frames_.size() > 1 && now > latest_possible_paint_time && |
lionel.g.landwerlin | f509661 | 2015-03-25 00:09:59 | [diff] [blame] | 263 | drop_frames_) { |
[email protected] | bd22a7e | 2014-07-14 23:14:45 | [diff] [blame] | 264 | DropNextReadyFrame_Locked(); |
| 265 | continue; |
[email protected] | e5bd91e3 | 2011-08-25 06:31:32 | [diff] [blame] | 266 | } |
[email protected] | 119ec3ec | 2011-11-04 00:07:47 | [diff] [blame] | 267 | |
| 268 | // Congratulations! You've made it past the video frame timing gauntlet. |
| 269 | // |
[email protected] | e002586 | 2013-02-05 00:35:16 | [diff] [blame] | 270 | // At this point enough time has passed that the next frame that ready for |
| 271 | // rendering. |
| 272 | PaintNextReadyFrame_Locked(); |
[email protected] | ce553ab | 2009-02-24 01:25:14 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
dalecurtis | 5755042e | 2015-03-26 20:31:22 | [diff] [blame^] | 276 | void VideoRendererImpl::SetTickClockForTesting( |
| 277 | scoped_ptr<base::TickClock> tick_clock) { |
| 278 | tick_clock_.swap(tick_clock); |
| 279 | } |
| 280 | |
[email protected] | cc3933d | 2013-11-26 03:41:02 | [diff] [blame] | 281 | void VideoRendererImpl::PaintNextReadyFrame_Locked() { |
[email protected] | e002586 | 2013-02-05 00:35:16 | [diff] [blame] | 282 | lock_.AssertAcquired(); |
| 283 | |
| 284 | scoped_refptr<VideoFrame> next_frame = ready_frames_.front(); |
[email protected] | e4ee4911 | 2012-08-02 22:42:52 | [diff] [blame] | 285 | ready_frames_.pop_front(); |
[email protected] | f54e768 | 2013-10-10 21:58:26 | [diff] [blame] | 286 | frames_decoded_++; |
[email protected] | e4ee4911 | 2012-08-02 22:42:52 | [diff] [blame] | 287 | |
dalecurtis | 5755042e | 2015-03-26 20:31:22 | [diff] [blame^] | 288 | last_media_time_ = last_painted_time_ = |
| 289 | wall_clock_time_cb_.Run(next_frame->timestamp()); |
[email protected] | e002586 | 2013-02-05 00:35:16 | [diff] [blame] | 290 | |
[email protected] | e002586 | 2013-02-05 00:35:16 | [diff] [blame] | 291 | paint_cb_.Run(next_frame); |
| 292 | |
[email protected] | de39b02 | 2014-03-18 21:36:22 | [diff] [blame] | 293 | task_runner_->PostTask( |
| 294 | FROM_HERE, |
| 295 | base::Bind(&VideoRendererImpl::AttemptRead, weak_factory_.GetWeakPtr())); |
[email protected] | e4ee4911 | 2012-08-02 22:42:52 | [diff] [blame] | 296 | } |
| 297 | |
[email protected] | cc3933d | 2013-11-26 03:41:02 | [diff] [blame] | 298 | void VideoRendererImpl::DropNextReadyFrame_Locked() { |
| 299 | TRACE_EVENT0("media", "VideoRendererImpl:frameDropped"); |
[email protected] | 3397216 | 2013-11-12 19:06:52 | [diff] [blame] | 300 | |
[email protected] | e002586 | 2013-02-05 00:35:16 | [diff] [blame] | 301 | lock_.AssertAcquired(); |
[email protected] | 5e83826 | 2010-06-24 19:15:07 | [diff] [blame] | 302 | |
dalecurtis | 5755042e | 2015-03-26 20:31:22 | [diff] [blame^] | 303 | last_media_time_ = |
| 304 | wall_clock_time_cb_.Run(ready_frames_.front()->timestamp()); |
| 305 | |
[email protected] | e002586 | 2013-02-05 00:35:16 | [diff] [blame] | 306 | ready_frames_.pop_front(); |
[email protected] | f54e768 | 2013-10-10 21:58:26 | [diff] [blame] | 307 | frames_decoded_++; |
| 308 | frames_dropped_++; |
[email protected] | b3766a2 | 2010-12-22 17:34:13 | [diff] [blame] | 309 | |
[email protected] | de39b02 | 2014-03-18 21:36:22 | [diff] [blame] | 310 | task_runner_->PostTask( |
| 311 | FROM_HERE, |
| 312 | base::Bind(&VideoRendererImpl::AttemptRead, weak_factory_.GetWeakPtr())); |
[email protected] | 6cac5e0d | 2012-05-18 21:37:04 | [diff] [blame] | 313 | } |
| 314 | |
[email protected] | cc3933d | 2013-11-26 03:41:02 | [diff] [blame] | 315 | void VideoRendererImpl::FrameReady(VideoFrameStream::Status status, |
[email protected] | 1da0dae | 2012-06-20 17:11:08 | [diff] [blame] | 316 | const scoped_refptr<VideoFrame>& frame) { |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 317 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 318 | base::AutoLock auto_lock(lock_); |
[email protected] | 119ec3ec | 2011-11-04 00:07:47 | [diff] [blame] | 319 | DCHECK_NE(state_, kUninitialized); |
[email protected] | 971db8c | 2013-07-12 01:52:36 | [diff] [blame] | 320 | DCHECK_NE(state_, kFlushed); |
[email protected] | 34d14848 | 2010-09-02 00:15:55 | [diff] [blame] | 321 | |
[email protected] | f139973 | 2012-04-10 23:56:38 | [diff] [blame] | 322 | CHECK(pending_read_); |
[email protected] | f23676a | 2011-11-04 02:04:09 | [diff] [blame] | 323 | pending_read_ = false; |
[email protected] | 34d14848 | 2010-09-02 00:15:55 | [diff] [blame] | 324 | |
[email protected] | e835f533 | 2013-07-26 13:36:31 | [diff] [blame] | 325 | if (status == VideoFrameStream::DECODE_ERROR || |
| 326 | status == VideoFrameStream::DECRYPT_ERROR) { |
[email protected] | 5b7ca428 | 2013-06-03 00:19:16 | [diff] [blame] | 327 | DCHECK(!frame.get()); |
[email protected] | 647f1a4 | 2013-07-17 22:21:36 | [diff] [blame] | 328 | PipelineStatus error = PIPELINE_ERROR_DECODE; |
[email protected] | e835f533 | 2013-07-26 13:36:31 | [diff] [blame] | 329 | if (status == VideoFrameStream::DECRYPT_ERROR) |
[email protected] | 647f1a4 | 2013-07-17 22:21:36 | [diff] [blame] | 330 | error = PIPELINE_ERROR_DECRYPT; |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 331 | task_runner_->PostTask(FROM_HERE, base::Bind(error_cb_, error)); |
[email protected] | 8d1e8fb | 2012-04-28 03:12:46 | [diff] [blame] | 332 | return; |
| 333 | } |
| 334 | |
[email protected] | 971db8c | 2013-07-12 01:52:36 | [diff] [blame] | 335 | // Already-queued VideoFrameStream ReadCB's can fire after various state |
| 336 | // transitions have happened; in that case just drop those frames immediately. |
[email protected] | 47affc7 | 2014-07-23 01:49:15 | [diff] [blame] | 337 | if (state_ == kFlushing) |
[email protected] | f139973 | 2012-04-10 23:56:38 | [diff] [blame] | 338 | return; |
| 339 | |
[email protected] | 28bc175c | 2014-07-01 19:39:17 | [diff] [blame] | 340 | DCHECK_EQ(state_, kPlaying); |
[email protected] | ddb5e56 | 2013-03-20 12:07:10 | [diff] [blame] | 341 | |
[email protected] | 28bc175c | 2014-07-01 19:39:17 | [diff] [blame] | 342 | // Can happen when demuxers are preparing for a new Seek(). |
dcheng | d048bd27 | 2014-08-27 02:25:08 | [diff] [blame] | 343 | if (!frame.get()) { |
[email protected] | 28bc175c | 2014-07-01 19:39:17 | [diff] [blame] | 344 | DCHECK_EQ(status, VideoFrameStream::DEMUXER_READ_ABORTED); |
[email protected] | b7f5fe96 | 2012-01-31 04:48:33 | [diff] [blame] | 345 | return; |
| 346 | } |
| 347 | |
[email protected] | 876937d | 2013-11-08 05:32:29 | [diff] [blame] | 348 | if (frame->end_of_stream()) { |
[email protected] | ddb5e56 | 2013-03-20 12:07:10 | [diff] [blame] | 349 | DCHECK(!received_end_of_stream_); |
| 350 | received_end_of_stream_ = true; |
[email protected] | 28bc175c | 2014-07-01 19:39:17 | [diff] [blame] | 351 | } else { |
| 352 | // Maintain the latest frame decoded so the correct frame is displayed after |
| 353 | // prerolling has completed. |
| 354 | if (frame->timestamp() <= start_timestamp_) |
| 355 | ready_frames_.clear(); |
| 356 | AddReadyFrame_Locked(frame); |
[email protected] | 930c747 | 2010-07-28 00:07:54 | [diff] [blame] | 357 | } |
[email protected] | 1f500f5 | 2009-06-22 22:23:19 | [diff] [blame] | 358 | |
[email protected] | 28bc175c | 2014-07-01 19:39:17 | [diff] [blame] | 359 | // Signal buffering state if we've met our conditions for having enough data. |
| 360 | if (buffering_state_ != BUFFERING_HAVE_ENOUGH && HaveEnoughData_Locked()) |
| 361 | TransitionToHaveEnough_Locked(); |
[email protected] | f23676a | 2011-11-04 02:04:09 | [diff] [blame] | 362 | |
| 363 | // Always request more decoded video if we have capacity. This serves two |
| 364 | // purposes: |
| 365 | // 1) Prerolling while paused |
| 366 | // 2) Keeps decoding going if video rendering thread starts falling behind |
[email protected] | ddb5e56 | 2013-03-20 12:07:10 | [diff] [blame] | 367 | AttemptRead_Locked(); |
[email protected] | ce553ab | 2009-02-24 01:25:14 | [diff] [blame] | 368 | } |
| 369 | |
[email protected] | 28bc175c | 2014-07-01 19:39:17 | [diff] [blame] | 370 | bool VideoRendererImpl::HaveEnoughData_Locked() { |
| 371 | DCHECK_EQ(state_, kPlaying); |
| 372 | return received_end_of_stream_ || |
[email protected] | 4beec40 | 2014-07-17 08:32:52 | [diff] [blame] | 373 | !video_frame_stream_->CanReadWithoutStalling() || |
[email protected] | 28bc175c | 2014-07-01 19:39:17 | [diff] [blame] | 374 | ready_frames_.size() >= static_cast<size_t>(limits::kMaxVideoFrames) || |
| 375 | (low_delay_ && ready_frames_.size() > 0); |
| 376 | } |
| 377 | |
| 378 | void VideoRendererImpl::TransitionToHaveEnough_Locked() { |
[email protected] | 9dc5c0b | 2014-07-31 13:16:38 | [diff] [blame] | 379 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 28bc175c | 2014-07-01 19:39:17 | [diff] [blame] | 380 | DCHECK_EQ(buffering_state_, BUFFERING_HAVE_NOTHING); |
| 381 | |
[email protected] | 28bc175c | 2014-07-01 19:39:17 | [diff] [blame] | 382 | if (!ready_frames_.empty()) { |
[email protected] | 28bc175c | 2014-07-01 19:39:17 | [diff] [blame] | 383 | // Because the clock might remain paused in for an undetermined amount |
| 384 | // of time (e.g., seeking while paused), paint the first frame. |
| 385 | PaintNextReadyFrame_Locked(); |
| 386 | } |
| 387 | |
| 388 | buffering_state_ = BUFFERING_HAVE_ENOUGH; |
| 389 | buffering_state_cb_.Run(BUFFERING_HAVE_ENOUGH); |
[email protected] | 4b6e4a9e | 2013-12-04 01:11:10 | [diff] [blame] | 390 | } |
| 391 | |
[email protected] | cc3933d | 2013-11-26 03:41:02 | [diff] [blame] | 392 | void VideoRendererImpl::AddReadyFrame_Locked( |
[email protected] | 61b52d1 | 2013-02-28 05:28:22 | [diff] [blame] | 393 | const scoped_refptr<VideoFrame>& frame) { |
[email protected] | 9dc5c0b | 2014-07-31 13:16:38 | [diff] [blame] | 394 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 61b52d1 | 2013-02-28 05:28:22 | [diff] [blame] | 395 | lock_.AssertAcquired(); |
[email protected] | 876937d | 2013-11-08 05:32:29 | [diff] [blame] | 396 | DCHECK(!frame->end_of_stream()); |
[email protected] | 61b52d1 | 2013-02-28 05:28:22 | [diff] [blame] | 397 | |
[email protected] | ddb5e56 | 2013-03-20 12:07:10 | [diff] [blame] | 398 | ready_frames_.push_back(frame); |
| 399 | DCHECK_LE(ready_frames_.size(), |
| 400 | static_cast<size_t>(limits::kMaxVideoFrames)); |
| 401 | |
[email protected] | 15f96db | 2013-02-28 18:54:52 | [diff] [blame] | 402 | // Avoid needlessly waking up |thread_| unless playing. |
| 403 | if (state_ == kPlaying) |
| 404 | frame_available_.Signal(); |
[email protected] | 23f720d | 2012-07-31 21:10:55 | [diff] [blame] | 405 | } |
| 406 | |
[email protected] | cc3933d | 2013-11-26 03:41:02 | [diff] [blame] | 407 | void VideoRendererImpl::AttemptRead() { |
[email protected] | 1276cc16 | 2012-12-07 21:55:33 | [diff] [blame] | 408 | base::AutoLock auto_lock(lock_); |
| 409 | AttemptRead_Locked(); |
| 410 | } |
| 411 | |
[email protected] | cc3933d | 2013-11-26 03:41:02 | [diff] [blame] | 412 | void VideoRendererImpl::AttemptRead_Locked() { |
[email protected] | b639a83 | 2013-12-20 21:12:39 | [diff] [blame] | 413 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 3545897 | 2009-07-25 02:12:11 | [diff] [blame] | 414 | lock_.AssertAcquired(); |
[email protected] | f23676a | 2011-11-04 02:04:09 | [diff] [blame] | 415 | |
[email protected] | 6eda4e4 | 2013-02-28 21:59:54 | [diff] [blame] | 416 | if (pending_read_ || received_end_of_stream_ || |
| 417 | ready_frames_.size() == static_cast<size_t>(limits::kMaxVideoFrames)) { |
[email protected] | 8d1e8fb | 2012-04-28 03:12:46 | [diff] [blame] | 418 | return; |
| 419 | } |
[email protected] | f23676a | 2011-11-04 02:04:09 | [diff] [blame] | 420 | |
[email protected] | 7f0456c | 2012-12-17 23:31:22 | [diff] [blame] | 421 | switch (state_) { |
[email protected] | 7f0456c | 2012-12-17 23:31:22 | [diff] [blame] | 422 | case kPlaying: |
| 423 | pending_read_ = true; |
[email protected] | 4beec40 | 2014-07-17 08:32:52 | [diff] [blame] | 424 | video_frame_stream_->Read(base::Bind(&VideoRendererImpl::FrameReady, |
| 425 | weak_factory_.GetWeakPtr())); |
[email protected] | 7f0456c | 2012-12-17 23:31:22 | [diff] [blame] | 426 | return; |
| 427 | |
| 428 | case kUninitialized: |
[email protected] | a1f7ace | 2013-02-22 06:14:07 | [diff] [blame] | 429 | case kInitializing: |
[email protected] | 971db8c | 2013-07-12 01:52:36 | [diff] [blame] | 430 | case kFlushing: |
[email protected] | 7f0456c | 2012-12-17 23:31:22 | [diff] [blame] | 431 | case kFlushed: |
[email protected] | 7f0456c | 2012-12-17 23:31:22 | [diff] [blame] | 432 | return; |
| 433 | } |
[email protected] | d3d04877 | 2010-07-14 21:00:25 | [diff] [blame] | 434 | } |
| 435 | |
[email protected] | cc3933d | 2013-11-26 03:41:02 | [diff] [blame] | 436 | void VideoRendererImpl::OnVideoFrameStreamResetDone() { |
[email protected] | d5c6444 | 2012-04-25 19:39:49 | [diff] [blame] | 437 | base::AutoLock auto_lock(lock_); |
[email protected] | f23676a | 2011-11-04 02:04:09 | [diff] [blame] | 438 | DCHECK_EQ(kFlushing, state_); |
[email protected] | 971db8c | 2013-07-12 01:52:36 | [diff] [blame] | 439 | DCHECK(!pending_read_); |
[email protected] | f8b4734 | 2013-03-27 12:20:21 | [diff] [blame] | 440 | DCHECK(ready_frames_.empty()); |
| 441 | DCHECK(!received_end_of_stream_); |
[email protected] | 2267bfe8 | 2014-05-09 18:03:58 | [diff] [blame] | 442 | DCHECK(!rendered_end_of_stream_); |
[email protected] | 28bc175c | 2014-07-01 19:39:17 | [diff] [blame] | 443 | DCHECK_EQ(buffering_state_, BUFFERING_HAVE_NOTHING); |
[email protected] | 34d14848 | 2010-09-02 00:15:55 | [diff] [blame] | 444 | |
[email protected] | e002586 | 2013-02-05 00:35:16 | [diff] [blame] | 445 | state_ = kFlushed; |
dalecurtis | 5755042e | 2015-03-26 20:31:22 | [diff] [blame^] | 446 | last_media_time_ = last_painted_time_ = base::TimeTicks(); |
[email protected] | e002586 | 2013-02-05 00:35:16 | [diff] [blame] | 447 | base::ResetAndReturn(&flush_cb_).Run(); |
[email protected] | 1f500f5 | 2009-06-22 22:23:19 | [diff] [blame] | 448 | } |
| 449 | |
[email protected] | cc3933d | 2013-11-26 03:41:02 | [diff] [blame] | 450 | void VideoRendererImpl::UpdateStatsAndWait_Locked( |
[email protected] | f54e768 | 2013-10-10 21:58:26 | [diff] [blame] | 451 | base::TimeDelta wait_duration) { |
| 452 | lock_.AssertAcquired(); |
| 453 | DCHECK_GE(frames_decoded_, 0); |
| 454 | DCHECK_LE(frames_dropped_, frames_decoded_); |
| 455 | |
| 456 | if (frames_decoded_) { |
| 457 | PipelineStatistics statistics; |
| 458 | statistics.video_frames_decoded = frames_decoded_; |
| 459 | statistics.video_frames_dropped = frames_dropped_; |
[email protected] | 9dc5c0b | 2014-07-31 13:16:38 | [diff] [blame] | 460 | task_runner_->PostTask(FROM_HERE, base::Bind(statistics_cb_, statistics)); |
[email protected] | f54e768 | 2013-10-10 21:58:26 | [diff] [blame] | 461 | |
| 462 | frames_decoded_ = 0; |
| 463 | frames_dropped_ = 0; |
| 464 | } |
| 465 | |
| 466 | frame_available_.TimedWait(wait_duration); |
| 467 | } |
| 468 | |
[email protected] | 1f500f5 | 2009-06-22 22:23:19 | [diff] [blame] | 469 | } // namespace media |