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, | |
| 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 | |
| 98 // Only size changes below this point | |
| 99 | |
| 100 if (curr.video_size.width() == next.video_size.width() && | |
| 101 curr.video_size.height() == next.video_size.height()) { | |
| 102 return false; // i.e. curr == next | |
| 103 } | |
| 104 | |
| 105 return !static_cast<VideoCodecBridge*>(media_codec_bridge_.get()) | |
| 106 ->IsAdaptivePlaybackSupported(next.video_size.width(), | |
|
qinmin
2015/06/19 06:57:40
nit: fix the indent
Tima Vaisburd
2015/06/20 02:32:31
I did although this was against |git cl format|.
| |
| 107 next.video_size.height()); | |
| 108 } | |
| 109 | |
| 110 MediaCodecDecoder::ConfigStatus MediaCodecVideoDecoder::ConfigureInternal() { | |
| 111 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 112 | |
| 113 DVLOG(1) << class_name() << "::" << __FUNCTION__; | |
| 114 | |
| 115 // If we cannot find a key frame in cache, the browser seek is needed. | |
| 116 if (!au_queue_.RewindToLastKeyFrame()) { | |
| 117 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " key frame required"; | |
| 118 | |
| 119 // The processing of CONFIG_KEY_FRAME_REQUIRED is not implemented yet, | |
|
wolenetz
2015/06/19 23:28:41
nit: add TODO / bug? (is this something the player
Tima Vaisburd
2015/06/20 02:32:31
Added TODO. This should be a part of the browser s
| |
| 120 // return error for now | |
| 121 // return CONFIG_KEY_FRAME_REQUIRED; | |
| 122 return CONFIG_FAILURE; | |
| 123 } | |
| 124 | |
| 125 // TODO(timav): implement DRM. | |
| 126 // bool is_secure = is_content_encrypted() && drm_bridge() && | |
| 127 // drm_bridge()->IsProtectedSurfaceRequired(); | |
| 128 | |
| 129 bool is_secure = false; // DRM is not implemented | |
| 130 | |
| 131 if (surface_.IsEmpty()) { | |
| 132 DVLOG(0) << class_name() << "::" << __FUNCTION__ << " surface required"; | |
| 133 return CONFIG_FAILURE; | |
| 134 } | |
| 135 | |
| 136 media_codec_bridge_.reset(VideoCodecBridge::CreateDecoder( | |
| 137 configs_.video_codec, | |
| 138 is_secure, | |
| 139 configs_.video_size, | |
| 140 surface_.j_surface().obj(), | |
| 141 GetMediaCrypto().obj())); | |
| 142 | |
| 143 if (!media_codec_bridge_) { | |
| 144 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " failed"; | |
| 145 return CONFIG_FAILURE; | |
| 146 } | |
| 147 | |
| 148 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " succeeded"; | |
| 149 | |
| 150 media_task_runner_->PostTask(FROM_HERE, codec_created_cb_); | |
| 151 | |
| 152 return CONFIG_OK; | |
| 153 } | |
| 154 | |
| 155 void MediaCodecVideoDecoder::SynchronizePTSWithTime( | |
| 156 base::TimeDelta current_time) { | |
| 157 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 158 | |
| 159 start_time_ticks_ = base::TimeTicks::Now(); | |
| 160 start_pts_ = current_time; | |
| 161 last_seen_pts_ = current_time; | |
| 162 } | |
| 163 | |
| 164 void MediaCodecVideoDecoder::OnOutputFormatChanged() { | |
| 165 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); | |
| 166 | |
| 167 gfx::Size prev_size = video_size_; | |
| 168 | |
| 169 // See b/18224769. The values reported from MediaCodecBridge::GetOutputFormat | |
| 170 // correspond to the actual video frame size, but this is not necessarily the | |
| 171 // size that should be output. | |
| 172 video_size_ = configs_.video_size; | |
| 173 if (video_size_ != prev_size) { | |
| 174 media_task_runner_->PostTask( | |
| 175 FROM_HERE, base::Bind(video_size_changed_cb_, video_size_)); | |
| 176 } | |
| 177 } | |
| 178 | |
| 179 void MediaCodecVideoDecoder::Render(int buffer_index, | |
| 180 size_t size, | |
| 181 bool render_output, | |
| 182 base::TimeDelta pts, | |
| 183 bool eos_encountered) { | |
| 184 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); | |
| 185 | |
| 186 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts | |
| 187 << " index:" << buffer_index << (eos_encountered ? " EOS" : ""); | |
| 188 | |
| 189 // EOS might come as a stand-alone frame with zero size | |
|
wolenetz
2015/06/19 23:28:41
in what case is the media_source_delegate (or chun
Tima Vaisburd
2015/06/20 02:32:31
MediaCodecBridge::QueueEOS generates these frames
Tima Vaisburd
2015/06/21 22:35:20
I checked again, this kind of EOS seems to always
wolenetz
2015/06/22 21:41:21
Thanks for the detail. I had confused myself: ::Re
| |
| 190 if (!size && eos_encountered) { | |
| 191 // Stand-alone EOS | |
| 192 // Discard the PTS that comes with it and ensure it is released last. | |
| 193 pts = last_seen_pts_ + | |
| 194 base::TimeDelta::FromMilliseconds(kDelayForStandAloneEOS); | |
| 195 } else { | |
| 196 // Keep track of last seen PTS | |
| 197 last_seen_pts_ = pts; | |
| 198 } | |
| 199 | |
| 200 if (!render_output) { | |
| 201 ReleaseOutputBuffer(buffer_index, pts, false, eos_encountered); | |
| 202 return; | |
| 203 } | |
| 204 | |
| 205 base::TimeDelta time_to_render = | |
| 206 pts - (base::TimeTicks::Now() - start_time_ticks_ + start_pts_); | |
| 207 | |
| 208 if (time_to_render < base::TimeDelta()) { | |
| 209 // Skip late frames | |
| 210 ReleaseOutputBuffer(buffer_index, pts, false, eos_encountered); | |
| 211 return; | |
| 212 } | |
| 213 | |
| 214 delayed_buffers_.insert(buffer_index); | |
| 215 | |
| 216 bool do_render = size > 0; | |
| 217 decoder_thread_.task_runner()->PostDelayedTask( | |
| 218 FROM_HERE, base::Bind(&MediaCodecVideoDecoder::ReleaseOutputBuffer, | |
| 219 base::Unretained(this), buffer_index, pts, | |
| 220 do_render, eos_encountered), | |
| 221 time_to_render); | |
| 222 } | |
| 223 | |
| 224 int MediaCodecVideoDecoder::NumDelayedRenderTasks() const { | |
| 225 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); | |
| 226 | |
| 227 return delayed_buffers_.size(); | |
| 228 } | |
| 229 | |
| 230 void MediaCodecVideoDecoder::ReleaseDelayedBuffers() { | |
| 231 // Media thread | |
| 232 // Called when there is no decoder thread | |
| 233 for (int index : delayed_buffers_) | |
| 234 media_codec_bridge_->ReleaseOutputBuffer(index, false); | |
| 235 delayed_buffers_.clear(); | |
| 236 } | |
| 237 | |
| 238 void MediaCodecVideoDecoder::ReleaseOutputBuffer(int buffer_index, | |
| 239 base::TimeDelta pts, | |
| 240 bool render, | |
| 241 bool eos_encountered) { | |
| 242 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); | |
| 243 | |
| 244 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts; | |
| 245 | |
| 246 media_codec_bridge_->ReleaseOutputBuffer(buffer_index, render); | |
| 247 | |
| 248 delayed_buffers_.erase(buffer_index); | |
| 249 | |
| 250 CheckLastFrame(eos_encountered, !delayed_buffers_.empty()); | |
| 251 | |
| 252 // |update_current_time_cb_| might be null if there is audio stream. | |
| 253 // Do not update current time for EOS frames. | |
| 254 if (!update_current_time_cb_.is_null() && !eos_encountered) { | |
| 255 media_task_runner_->PostTask(FROM_HERE, | |
| 256 base::Bind(update_current_time_cb_, pts, pts)); | |
| 257 } | |
| 258 } | |
| 259 | |
| 260 } // namespace media | |
| OLD | NEW |