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