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