Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "media/base/android/media_codec_video_decoder.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "media/base/android/media_codec_bridge.h" | |
| 10 #include "media/base/buffers.h" | |
| 11 #include "media/base/demuxer_stream.h" | |
| 12 | |
| 13 namespace media { | |
| 14 | |
| 15 namespace { | |
| 16 const int kDelayForStandAloneEOS = 2; // milliseconds | |
| 17 } | |
| 18 | |
| 19 MediaCodecVideoDecoder::MediaCodecVideoDecoder( | |
| 20 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, | |
| 21 const base::Closure& request_data_cb, | |
| 22 const base::Closure& starvation_cb, | |
| 23 const base::Closure& stop_done_cb, | |
| 24 const base::Closure& error_cb, | |
|
liberato (no reviews please)
2015/06/12 22:24:15
there are five error calls, but the callback has n
liberato (no reviews please)
2015/06/12 22:24:15
when the error cb is called, what sort of action i
Tima Vaisburd
2015/06/17 01:18:24
Since there is no way to recover from these errors
Tima Vaisburd
2015/06/17 01:18:24
Good point, I updated the documentation. The recov
liberato (no reviews please)
2015/06/17 14:11:11
Sounds like it was just a doc issue. If there's n
| |
| 25 const SetTimeCallback& update_current_time_cb, | |
| 26 const VideoSizeChangedCallback& video_size_changed_cb, | |
| 27 const base::Closure& codec_created_cb) | |
| 28 : MediaCodecDecoder(media_task_runner, | |
| 29 request_data_cb, | |
| 30 starvation_cb, | |
| 31 stop_done_cb, | |
| 32 error_cb, | |
| 33 "VideoDecoder"), | |
| 34 update_current_time_cb_(update_current_time_cb), | |
| 35 video_size_changed_cb_(video_size_changed_cb), | |
| 36 codec_created_cb_(codec_created_cb) { | |
| 37 } | |
| 38 | |
| 39 MediaCodecVideoDecoder::~MediaCodecVideoDecoder() { | |
| 40 DVLOG(1) << "VideoDecoder::~VideoDecoder()"; | |
| 41 } | |
| 42 | |
| 43 bool MediaCodecVideoDecoder::HasStream() const { | |
| 44 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 45 | |
| 46 return configs_.video_codec != kUnknownVideoCodec; | |
| 47 } | |
| 48 | |
| 49 void MediaCodecVideoDecoder::SetDemuxerConfigs(const DemuxerConfigs& configs) { | |
| 50 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 51 | |
| 52 DVLOG(1) << class_name() << "::" << __FUNCTION__; | |
| 53 | |
| 54 configs_ = configs; | |
| 55 | |
| 56 if (video_size_.IsEmpty()) { | |
| 57 video_size_ = configs_.video_size; | |
| 58 media_task_runner_->PostTask( | |
| 59 FROM_HERE, base::Bind(video_size_changed_cb_, video_size_)); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 void MediaCodecVideoDecoder::ReleaseDecoderResources() { | |
| 64 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 65 | |
| 66 DVLOG(1) << class_name() << "::" << __FUNCTION__; | |
| 67 | |
| 68 MediaCodecDecoder::ReleaseDecoderResources(); | |
| 69 surface_ = gfx::ScopedJavaSurface(); | |
| 70 delayed_buffers_.clear(); | |
| 71 } | |
| 72 | |
| 73 void MediaCodecVideoDecoder::SetPendingSurface(gfx::ScopedJavaSurface surface) { | |
| 74 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 75 | |
| 76 surface_ = surface.Pass(); | |
| 77 | |
| 78 if (surface_.IsEmpty()) { | |
| 79 // Synchronously stop decoder thread and release MediaCodec | |
| 80 ReleaseDecoderResources(); | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 bool MediaCodecVideoDecoder::HasPendingSurface() const { | |
| 85 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 86 | |
| 87 return !surface_.IsEmpty(); | |
| 88 } | |
| 89 | |
| 90 bool MediaCodecVideoDecoder::IsCodecReconfigureNeeded( | |
| 91 const DemuxerConfigs& curr, | |
| 92 const DemuxerConfigs& next) const { | |
| 93 if (curr.video_codec != next.video_codec || | |
| 94 curr.is_video_encrypted != next.is_video_encrypted) | |
| 95 return true; | |
| 96 | |
| 97 // Only size changes below this point | |
| 98 | |
| 99 if (curr.video_size.width() == next.video_size.width() && | |
| 100 curr.video_size.height() == next.video_size.height()) | |
| 101 return false; // i.e. curr == next | |
| 102 | |
| 103 return !static_cast<VideoCodecBridge*>(media_codec_bridge_.get()) | |
| 104 ->IsAdaptivePlaybackSupported(next.video_size.width(), | |
| 105 next.video_size.height()); | |
| 106 } | |
| 107 | |
| 108 MediaCodecDecoder::ConfigStatus MediaCodecVideoDecoder::ConfigureInternal() { | |
| 109 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 110 | |
| 111 DVLOG(1) << class_name() << "::" << __FUNCTION__; | |
| 112 | |
| 113 // If we cannot find a key frame in cache, the browser seek is needed. | |
| 114 if (!au_queue_.SkipToKeyFrame()) { | |
| 115 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " key frame required"; | |
| 116 | |
| 117 // The processing of CONFIG_KEY_FRAME_REQUIRED is not implemented yet, | |
| 118 // return error for now | |
| 119 // return CONFIG_KEY_FRAME_REQUIRED; | |
| 120 return CONFIG_FAILURE; | |
| 121 } | |
| 122 | |
| 123 // bool is_secure = is_content_encrypted() && drm_bridge() && | |
| 124 // drm_bridge()->IsProtectedSurfaceRequired(); | |
| 125 | |
| 126 bool is_secure = false; // DRM is not implemented | |
| 127 | |
| 128 if (surface_.IsEmpty()) { | |
| 129 DVLOG(0) << class_name() << "::" << __FUNCTION__ << " surface required"; | |
| 130 return CONFIG_FAILURE; | |
| 131 } | |
| 132 | |
| 133 media_codec_bridge_.reset(VideoCodecBridge::CreateDecoder( | |
| 134 configs_.video_codec, | |
| 135 is_secure, | |
| 136 configs_.video_size, | |
| 137 surface_.j_surface().obj(), | |
| 138 GetMediaCrypto().obj())); | |
| 139 | |
| 140 if (!media_codec_bridge_) { | |
| 141 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " failed"; | |
| 142 return CONFIG_FAILURE; | |
| 143 } | |
| 144 | |
| 145 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " succeeded"; | |
| 146 | |
| 147 media_task_runner_->PostTask(FROM_HERE, codec_created_cb_); | |
| 148 | |
| 149 return CONFIG_OK; | |
| 150 } | |
| 151 | |
| 152 void MediaCodecVideoDecoder::SynchronizePTSWithTime( | |
| 153 base::TimeDelta current_time) { | |
| 154 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 155 | |
| 156 start_time_ticks_ = base::TimeTicks::Now(); | |
| 157 start_pts_ = current_time; | |
| 158 last_seen_pts_ = current_time; | |
| 159 } | |
| 160 | |
| 161 void MediaCodecVideoDecoder::OnOutputFormatChanged() { | |
| 162 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); | |
| 163 | |
| 164 gfx::Size prev_size = video_size_; | |
| 165 | |
| 166 // See b/18224769. The values reported from MediaCodecBridge::GetOutputFormat | |
| 167 // correspond to the actual video frame size, but this is not necessarily the | |
| 168 // size that should be output. | |
| 169 video_size_ = configs_.video_size; | |
| 170 if (video_size_ != prev_size) | |
| 171 media_task_runner_->PostTask( | |
| 172 FROM_HERE, base::Bind(video_size_changed_cb_, video_size_)); | |
| 173 } | |
| 174 | |
| 175 void MediaCodecVideoDecoder::Render(int buffer_index, | |
| 176 size_t size, | |
| 177 bool render_output, | |
| 178 base::TimeDelta pts, | |
| 179 bool eos_encountered) { | |
| 180 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); | |
| 181 | |
| 182 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts | |
| 183 << " index:" << buffer_index << (eos_encountered ? " EOS" : ""); | |
| 184 | |
| 185 // EOS might come as a stand-alone frame with zero size | |
| 186 if (!size && eos_encountered) { | |
| 187 // Stand-alone EOS | |
| 188 // Discard the PTS that comes with it and ensure it is released last. | |
| 189 pts = last_seen_pts_ + | |
| 190 base::TimeDelta::FromMilliseconds(kDelayForStandAloneEOS); | |
| 191 } else { | |
| 192 // Keep track of last seen PTS | |
| 193 last_seen_pts_ = pts; | |
| 194 } | |
| 195 | |
| 196 if (!render_output) { | |
| 197 ReleaseOutputBuffer(buffer_index, pts, false, eos_encountered); | |
| 198 return; | |
| 199 } | |
| 200 | |
| 201 base::TimeDelta time_to_render = | |
| 202 pts - (base::TimeTicks::Now() - start_time_ticks_ + start_pts_); | |
| 203 | |
| 204 if (time_to_render < base::TimeDelta()) { | |
| 205 // Skip late frames | |
| 206 ReleaseOutputBuffer(buffer_index, pts, false, eos_encountered); | |
| 207 return; | |
| 208 } | |
| 209 | |
| 210 delayed_buffers_.insert(buffer_index); | |
| 211 | |
| 212 bool do_render = size > 0; | |
| 213 decoder_thread_.task_runner()->PostDelayedTask( | |
| 214 FROM_HERE, base::Bind(&MediaCodecVideoDecoder::ReleaseOutputBuffer, | |
| 215 base::Unretained(this), buffer_index, pts, | |
| 216 do_render, eos_encountered), | |
| 217 time_to_render); | |
| 218 } | |
| 219 | |
| 220 int MediaCodecVideoDecoder::NumDelayedRenderTasks() const { | |
| 221 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); | |
| 222 | |
| 223 return delayed_buffers_.size(); | |
| 224 } | |
| 225 | |
| 226 void MediaCodecVideoDecoder::ReleaseDelayedBuffers() { | |
| 227 // Media thread | |
| 228 // Called when there is no decoder thread | |
| 229 for (int index : delayed_buffers_) | |
| 230 media_codec_bridge_->ReleaseOutputBuffer(index, false); | |
| 231 delayed_buffers_.clear(); | |
| 232 } | |
| 233 | |
| 234 void MediaCodecVideoDecoder::ReleaseOutputBuffer(int buffer_index, | |
| 235 base::TimeDelta pts, | |
| 236 bool render, | |
| 237 bool eos_encountered) { | |
| 238 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); | |
| 239 | |
| 240 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts; | |
| 241 | |
| 242 media_codec_bridge_->ReleaseOutputBuffer(buffer_index, render); | |
| 243 | |
| 244 delayed_buffers_.erase(buffer_index); | |
| 245 | |
| 246 CheckLastFrame(eos_encountered, !delayed_buffers_.empty()); | |
| 247 | |
| 248 // |update_current_time_cb_| might be null if there is audio stream. | |
| 249 // Do not update current time for EOS frames. | |
| 250 if (!update_current_time_cb_.is_null() && !eos_encountered) { | |
| 251 media_task_runner_->PostTask(FROM_HERE, | |
| 252 base::Bind(update_current_time_cb_, pts, pts)); | |
| 253 } | |
| 254 } | |
| 255 | |
| 256 } // namespace media | |
| OLD | NEW |