| 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__ << " " << configs; |
| 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(), |
| 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, |
| 120 // return error for now. |
| 121 // TODO(timav): Replace this with the following line together with |
| 122 // implementing the browser seek: |
| 123 // return CONFIG_KEY_FRAME_REQUIRED; |
| 124 return CONFIG_FAILURE; |
| 125 } |
| 126 |
| 127 // TODO(timav): implement DRM. |
| 128 // bool is_secure = is_content_encrypted() && drm_bridge() && |
| 129 // drm_bridge()->IsProtectedSurfaceRequired(); |
| 130 |
| 131 bool is_secure = false; // DRM is not implemented |
| 132 |
| 133 if (surface_.IsEmpty()) { |
| 134 DVLOG(0) << class_name() << "::" << __FUNCTION__ << " surface required"; |
| 135 return CONFIG_FAILURE; |
| 136 } |
| 137 |
| 138 media_codec_bridge_.reset(VideoCodecBridge::CreateDecoder( |
| 139 configs_.video_codec, |
| 140 is_secure, |
| 141 configs_.video_size, |
| 142 surface_.j_surface().obj(), |
| 143 GetMediaCrypto().obj())); |
| 144 |
| 145 if (!media_codec_bridge_) { |
| 146 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " failed"; |
| 147 return CONFIG_FAILURE; |
| 148 } |
| 149 |
| 150 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " succeeded"; |
| 151 |
| 152 media_task_runner_->PostTask(FROM_HERE, codec_created_cb_); |
| 153 |
| 154 return CONFIG_OK; |
| 155 } |
| 156 |
| 157 void MediaCodecVideoDecoder::SynchronizePTSWithTime( |
| 158 base::TimeDelta current_time) { |
| 159 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| 160 |
| 161 start_time_ticks_ = base::TimeTicks::Now(); |
| 162 start_pts_ = current_time; |
| 163 last_seen_pts_ = current_time; |
| 164 } |
| 165 |
| 166 void MediaCodecVideoDecoder::OnOutputFormatChanged() { |
| 167 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); |
| 168 |
| 169 gfx::Size prev_size = video_size_; |
| 170 |
| 171 // See b/18224769. The values reported from MediaCodecBridge::GetOutputFormat |
| 172 // correspond to the actual video frame size, but this is not necessarily the |
| 173 // size that should be output. |
| 174 video_size_ = configs_.video_size; |
| 175 if (video_size_ != prev_size) { |
| 176 media_task_runner_->PostTask( |
| 177 FROM_HERE, base::Bind(video_size_changed_cb_, video_size_)); |
| 178 } |
| 179 } |
| 180 |
| 181 void MediaCodecVideoDecoder::Render(int buffer_index, |
| 182 size_t size, |
| 183 bool render_output, |
| 184 base::TimeDelta pts, |
| 185 bool eos_encountered) { |
| 186 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); |
| 187 |
| 188 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts |
| 189 << " index:" << buffer_index << (eos_encountered ? " EOS" : ""); |
| 190 |
| 191 // Normally EOS comes as a separate access unit that does not have data, |
| 192 // the corresponding |size| will be 0. |
| 193 if (!size && eos_encountered) { |
| 194 // Stand-alone EOS |
| 195 // Discard the PTS that comes with it and ensure it is released last. |
| 196 pts = last_seen_pts_ + |
| 197 base::TimeDelta::FromMilliseconds(kDelayForStandAloneEOS); |
| 198 } else { |
| 199 // Keep track of last seen PTS |
| 200 last_seen_pts_ = pts; |
| 201 } |
| 202 |
| 203 if (!render_output) { |
| 204 ReleaseOutputBuffer(buffer_index, pts, false, eos_encountered); |
| 205 return; |
| 206 } |
| 207 |
| 208 base::TimeDelta time_to_render = |
| 209 pts - (base::TimeTicks::Now() - start_time_ticks_ + start_pts_); |
| 210 |
| 211 if (time_to_render < base::TimeDelta()) { |
| 212 // Skip late frames |
| 213 ReleaseOutputBuffer(buffer_index, pts, false, eos_encountered); |
| 214 return; |
| 215 } |
| 216 |
| 217 delayed_buffers_.insert(buffer_index); |
| 218 |
| 219 bool do_render = size > 0; |
| 220 decoder_thread_.task_runner()->PostDelayedTask( |
| 221 FROM_HERE, base::Bind(&MediaCodecVideoDecoder::ReleaseOutputBuffer, |
| 222 base::Unretained(this), buffer_index, pts, |
| 223 do_render, eos_encountered), |
| 224 time_to_render); |
| 225 } |
| 226 |
| 227 int MediaCodecVideoDecoder::NumDelayedRenderTasks() const { |
| 228 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); |
| 229 |
| 230 return delayed_buffers_.size(); |
| 231 } |
| 232 |
| 233 void MediaCodecVideoDecoder::ReleaseDelayedBuffers() { |
| 234 // Media thread |
| 235 // Called when there is no decoder thread |
| 236 for (int index : delayed_buffers_) |
| 237 media_codec_bridge_->ReleaseOutputBuffer(index, false); |
| 238 delayed_buffers_.clear(); |
| 239 } |
| 240 |
| 241 void MediaCodecVideoDecoder::ReleaseOutputBuffer(int buffer_index, |
| 242 base::TimeDelta pts, |
| 243 bool render, |
| 244 bool eos_encountered) { |
| 245 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); |
| 246 |
| 247 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts; |
| 248 |
| 249 media_codec_bridge_->ReleaseOutputBuffer(buffer_index, render); |
| 250 |
| 251 delayed_buffers_.erase(buffer_index); |
| 252 |
| 253 CheckLastFrame(eos_encountered, !delayed_buffers_.empty()); |
| 254 |
| 255 // |update_current_time_cb_| might be null if there is audio stream. |
| 256 // Do not update current time for EOS frames. |
| 257 if (!update_current_time_cb_.is_null() && !eos_encountered) { |
| 258 media_task_runner_->PostTask(FROM_HERE, |
| 259 base::Bind(update_current_time_cb_, pts, pts)); |
| 260 } |
| 261 } |
| 262 |
| 263 } // namespace media |
| OLD | NEW |