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 | |
| 10 #include "media/base/android/media_codec_bridge.h" | |
| 11 #include "media/base/demuxer_stream.h" | |
| 12 | |
| 13 namespace media { | |
| 14 | |
| 15 namespace { | |
| 16 const int DELAY_FOR_STAND_ALONE_EOS = 2; // milliseconds | |
| 17 } | |
| 18 | |
| 19 MediaCodecVideoDecoder::MediaCodecVideoDecoder( | |
| 20 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, | |
| 21 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner, | |
| 22 const base::Closure& request_data_cb, | |
| 23 const base::Closure& starvation_cb, | |
| 24 const base::Closure& stop_done_cb, | |
| 25 const base::Closure& error_cb, | |
| 26 const base::Closure& request_resources_cb) | |
| 27 : MediaCodecDecoder(media_task_runner, | |
| 28 ui_task_runner, | |
| 29 request_data_cb, | |
| 30 starvation_cb, | |
| 31 stop_done_cb, | |
| 32 error_cb, | |
| 33 "VideoDecoder"), | |
| 34 request_resources_cb_(request_resources_cb) | |
| 35 {} | |
| 36 | |
| 37 MediaCodecVideoDecoder::~MediaCodecVideoDecoder() | |
| 38 { | |
| 39 DVLOG(1) << "VideoDecoder::~VideoDecoder()"; | |
| 40 } | |
| 41 | |
| 42 void MediaCodecVideoDecoder::SetPendingSurface(gfx::ScopedJavaSurface surface) { | |
| 43 // Media thread | |
| 44 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 45 | |
| 46 surface_ = surface.Pass(); | |
| 47 | |
| 48 if (surface_.IsEmpty()) { | |
| 49 // Synchronously stop decoder thread and release MediaCodec | |
| 50 ReleaseDecoderResources(); | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 bool MediaCodecVideoDecoder::HasPendingSurface() const { | |
| 55 // Media thread | |
| 56 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 57 | |
| 58 return !surface_.IsEmpty(); | |
| 59 } | |
| 60 | |
| 61 bool MediaCodecVideoDecoder::HasStream() const { | |
| 62 // Media thread | |
| 63 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 64 | |
| 65 return configs_.video_codec != kUnknownVideoCodec; | |
| 66 } | |
| 67 | |
| 68 int MediaCodecVideoDecoder::GetVideoWidth() const { | |
| 69 // UI thread, Media thread | |
| 70 base::AutoLock lock(configs_lock_); | |
| 71 return configs_.video_size.width(); | |
|
qinmin
2015/05/19 19:26:26
output video width is different from config width,
Tima Vaisburd
2015/05/20 05:19:59
I've separated output video size from config, but
| |
| 72 } | |
| 73 | |
| 74 int MediaCodecVideoDecoder::GetVideoHeight() const { | |
| 75 // UI thread, Media thread | |
| 76 base::AutoLock lock(configs_lock_); | |
| 77 return configs_.video_size.height(); | |
| 78 } | |
| 79 | |
| 80 bool MediaCodecVideoDecoder::IsCodecReconfigureNeeded( | |
| 81 const DemuxerConfigs& curr, const DemuxerConfigs& next) const { | |
| 82 | |
| 83 if (curr.video_codec != next.video_codec || | |
| 84 curr.is_video_encrypted != next.is_video_encrypted) | |
| 85 return true; | |
| 86 | |
| 87 // Only size changes below this point | |
| 88 | |
| 89 if (curr.video_size.width() == next.video_size.width() && | |
| 90 curr.video_size.height() == next.video_size.height()) | |
| 91 return false; // i.e. curr == next | |
| 92 | |
| 93 return !static_cast<VideoCodecBridge*>(media_codec_bridge_.get())-> | |
| 94 IsAdaptivePlaybackSupported(next.video_size.width(), | |
| 95 next.video_size.height()); | |
| 96 } | |
| 97 | |
| 98 MediaCodecDecoder::ConfigStatus | |
| 99 MediaCodecVideoDecoder::ConfigureInternal() { | |
| 100 // Media thread | |
| 101 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 102 | |
| 103 DVLOG(1) << class_name() << "::" << __FUNCTION__; | |
| 104 | |
| 105 // If we cannot find a key frame in cache, the browser seek is needed. | |
| 106 if (!au_queue_.SkipToKeyFrame()) { | |
| 107 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " key frame required"; | |
| 108 | |
| 109 // The processing of CONFIG_KEY_FRAME_REQUIRED is not implemented yet, | |
| 110 // return error for now | |
| 111 //return CONFIG_KEY_FRAME_REQUIRED; | |
| 112 return CONFIG_FAILURE; | |
| 113 } | |
| 114 | |
| 115 //bool is_secure = is_content_encrypted() && drm_bridge() && | |
| 116 // drm_bridge()->IsProtectedSurfaceRequired(); | |
| 117 | |
| 118 bool is_secure = false; // DRM is not implemented | |
| 119 | |
| 120 DCHECK(!surface_.IsEmpty()); | |
| 121 | |
| 122 media_codec_bridge_.reset(VideoCodecBridge::CreateDecoder( | |
| 123 configs_.video_codec, | |
| 124 is_secure, | |
| 125 configs_.video_size, | |
| 126 surface_.j_surface().obj(), | |
| 127 GetMediaCrypto().obj())); | |
| 128 | |
| 129 if (!media_codec_bridge_) { | |
| 130 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " failed"; | |
| 131 return CONFIG_FAILURE; | |
| 132 } | |
| 133 | |
| 134 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " succeeded"; | |
| 135 | |
| 136 // This callback requests resources by releasing other players, | |
| 137 // post it to the UI thread. | |
| 138 ui_task_runner_->PostTask(FROM_HERE, request_resources_cb_); | |
| 139 | |
| 140 return CONFIG_OK; | |
| 141 } | |
| 142 | |
| 143 void MediaCodecVideoDecoder::ReleaseDecoderResources() { | |
| 144 // Media thread | |
| 145 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
| 146 | |
| 147 DVLOG(1) << class_name() << "::" << __FUNCTION__; | |
| 148 | |
| 149 MediaCodecDecoder::ReleaseDecoderResources(); | |
| 150 surface_ = gfx::ScopedJavaSurface(); | |
| 151 delayed_buffers_.clear(); | |
| 152 } | |
| 153 | |
| 154 void MediaCodecVideoDecoder::SynchronizePTSWithTime( | |
| 155 base::TimeDelta current_time) { | |
| 156 // Media thread | |
| 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::Render(int buffer_index, | |
| 165 size_t size, | |
| 166 bool render_output, | |
| 167 base::TimeDelta pts, | |
| 168 bool eos_encountered) { | |
| 169 // Decoder thread | |
| 170 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); | |
| 171 | |
| 172 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts | |
| 173 << " index:" << buffer_index << (eos_encountered ? " EOS" : ""); | |
| 174 | |
| 175 // EOS might come as a stand-alone frame with zero size | |
| 176 if (!size && eos_encountered) { | |
| 177 // Stand-alone EOS | |
| 178 // Discard the PTS that comes with it and ensure it is released last. | |
| 179 pts = last_seen_pts_ + | |
| 180 base::TimeDelta::FromMilliseconds(DELAY_FOR_STAND_ALONE_EOS); | |
| 181 } else { | |
| 182 // Keep track of last seen PTS | |
| 183 last_seen_pts_ = pts; | |
| 184 } | |
| 185 | |
| 186 if (!render_output) { | |
| 187 ReleaseOutputBuffer(buffer_index, false, eos_encountered); | |
| 188 return; | |
| 189 } | |
| 190 | |
| 191 base::TimeDelta time_to_render = | |
| 192 pts - (base::TimeTicks::Now() - start_time_ticks_ + start_pts_); | |
| 193 | |
| 194 if (time_to_render < base::TimeDelta()) { | |
| 195 // Skip late frames | |
| 196 ReleaseOutputBuffer(buffer_index, false, eos_encountered); | |
| 197 return; | |
| 198 } | |
| 199 | |
| 200 delayed_buffers_.insert(buffer_index); | |
| 201 | |
| 202 bool do_render = size > 0; | |
| 203 decoder_thread_.task_runner()->PostDelayedTask( | |
| 204 FROM_HERE, | |
| 205 base::Bind(&MediaCodecVideoDecoder::ReleaseOutputBuffer, | |
| 206 base::Unretained(this), | |
| 207 buffer_index, do_render, eos_encountered), | |
| 208 time_to_render); | |
| 209 } | |
| 210 | |
| 211 void MediaCodecVideoDecoder::ReleaseOutputBuffer( | |
| 212 int buffer_index, bool render, bool eos_encountered) { | |
| 213 // Decoder thread | |
| 214 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); | |
| 215 | |
| 216 DVLOG(2) << class_name() << "::" << __FUNCTION__; | |
| 217 | |
| 218 media_codec_bridge_->ReleaseOutputBuffer(buffer_index, render); | |
| 219 | |
| 220 delayed_buffers_.erase(buffer_index); | |
| 221 | |
| 222 CheckLastFrame(eos_encountered, !delayed_buffers_.empty()); | |
| 223 } | |
| 224 | |
| 225 int MediaCodecVideoDecoder::NumDelayedRenderTasks() const { | |
| 226 // Decoder thread | |
| 227 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); | |
| 228 | |
| 229 return delayed_buffers_.size(); | |
| 230 } | |
| 231 | |
| 232 void MediaCodecVideoDecoder::ReleaseDelayedBuffers() { | |
| 233 // Media thread | |
| 234 // Called when there is no decoder thread | |
| 235 for (int index : delayed_buffers_) | |
| 236 media_codec_bridge_->ReleaseOutputBuffer(index, false); | |
| 237 delayed_buffers_.clear(); | |
| 238 } | |
| 239 | |
| 240 } // namespace media | |
| OLD | NEW |