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 DELAY_FOR_STAND_ALONE_EOS = 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 VideoSizeChangedCallback& video_size_changed_cb, | |
26 const base::Closure& codec_created_cb) | |
27 : MediaCodecDecoder(media_task_runner, | |
28 request_data_cb, | |
29 starvation_cb, | |
30 stop_done_cb, | |
31 error_cb, | |
32 "VideoDecoder"), | |
33 video_size_changed_cb_(video_size_changed_cb), | |
34 codec_created_cb_(codec_created_cb) | |
35 {} | |
36 | |
37 MediaCodecVideoDecoder::~MediaCodecVideoDecoder() | |
38 { | |
39 DVLOG(1) << "VideoDecoder::~VideoDecoder()"; | |
watk
2015/06/04 20:56:43
Why aren't you using "MediaCodecVideoDecoder"? IMO
Tima Vaisburd
2015/06/05 04:17:47
It corresponds to what class_name() returns. I'd l
| |
40 } | |
41 | |
42 bool MediaCodecVideoDecoder::HasStream() const { | |
43 // Media thread | |
44 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
45 | |
46 return configs_.video_codec != kUnknownVideoCodec; | |
47 } | |
48 | |
49 void MediaCodecVideoDecoder::SetDemuxerConfigs(const DemuxerConfigs& configs) { | |
50 // Media thread | |
51 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
52 | |
53 DVLOG(1) << class_name() << "::" << __FUNCTION__; | |
54 | |
55 configs_ = configs; | |
56 | |
57 if (video_size_.IsEmpty()) { | |
58 video_size_ = configs_.video_size; | |
59 media_task_runner_->PostTask( | |
60 FROM_HERE, base::Bind(video_size_changed_cb_, video_size_)); | |
61 } | |
62 } | |
63 | |
64 void MediaCodecVideoDecoder::ReleaseDecoderResources() { | |
65 // Media thread | |
66 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
67 | |
68 DVLOG(1) << class_name() << "::" << __FUNCTION__; | |
69 | |
70 MediaCodecDecoder::ReleaseDecoderResources(); | |
71 surface_ = gfx::ScopedJavaSurface(); | |
72 delayed_buffers_.clear(); | |
73 } | |
74 | |
75 void MediaCodecVideoDecoder::SetPendingSurface(gfx::ScopedJavaSurface surface) { | |
76 // Media thread | |
77 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
78 | |
79 surface_ = surface.Pass(); | |
80 | |
81 if (surface_.IsEmpty()) { | |
82 // Synchronously stop decoder thread and release MediaCodec | |
83 ReleaseDecoderResources(); | |
84 } | |
85 } | |
86 | |
87 bool MediaCodecVideoDecoder::HasPendingSurface() const { | |
88 // Media thread | |
89 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
90 | |
91 return !surface_.IsEmpty(); | |
92 } | |
93 | |
94 bool MediaCodecVideoDecoder::IsCodecReconfigureNeeded( | |
95 const DemuxerConfigs& curr, const DemuxerConfigs& next) const { | |
96 | |
97 if (curr.video_codec != next.video_codec || | |
98 curr.is_video_encrypted != next.is_video_encrypted) | |
99 return true; | |
100 | |
101 // Only size changes below this point | |
102 | |
103 if (curr.video_size.width() == next.video_size.width() && | |
104 curr.video_size.height() == next.video_size.height()) | |
105 return false; // i.e. curr == next | |
106 | |
107 return !static_cast<VideoCodecBridge*>(media_codec_bridge_.get())-> | |
108 IsAdaptivePlaybackSupported(next.video_size.width(), | |
109 next.video_size.height()); | |
110 } | |
111 | |
112 MediaCodecDecoder::ConfigStatus | |
113 MediaCodecVideoDecoder::ConfigureInternal() { | |
114 // Media thread | |
115 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
116 | |
117 DVLOG(1) << class_name() << "::" << __FUNCTION__; | |
118 | |
119 // If we cannot find a key frame in cache, the browser seek is needed. | |
120 if (!au_queue_.SkipToKeyFrame()) { | |
121 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " key frame required"; | |
122 | |
123 // The processing of CONFIG_KEY_FRAME_REQUIRED is not implemented yet, | |
124 // return error for now | |
125 //return CONFIG_KEY_FRAME_REQUIRED; | |
126 return CONFIG_FAILURE; | |
127 } | |
128 | |
129 //bool is_secure = is_content_encrypted() && drm_bridge() && | |
130 // drm_bridge()->IsProtectedSurfaceRequired(); | |
131 | |
132 bool is_secure = false; // DRM is not implemented | |
133 | |
134 DCHECK(!surface_.IsEmpty()); | |
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 // Media thread | |
158 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
159 | |
160 start_time_ticks_ = base::TimeTicks::Now(); | |
161 start_pts_ = current_time; | |
162 last_seen_pts_ = current_time; | |
163 } | |
164 | |
165 void MediaCodecVideoDecoder::OnOutputFormatChanged() { | |
166 // Decoder thread | |
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 void MediaCodecVideoDecoder::Render(int buffer_index, | |
181 size_t size, | |
182 bool render_output, | |
183 base::TimeDelta pts, | |
184 bool eos_encountered) { | |
185 // Decoder thread | |
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 // EOS might come as a stand-alone frame with zero size | |
192 if (!size && eos_encountered) { | |
193 // Stand-alone EOS | |
194 // Discard the PTS that comes with it and ensure it is released last. | |
195 pts = last_seen_pts_ + | |
196 base::TimeDelta::FromMilliseconds(DELAY_FOR_STAND_ALONE_EOS); | |
197 } else { | |
198 // Keep track of last seen PTS | |
199 last_seen_pts_ = pts; | |
200 } | |
201 | |
202 if (!render_output) { | |
203 ReleaseOutputBuffer(buffer_index, false, eos_encountered); | |
204 return; | |
205 } | |
206 | |
207 base::TimeDelta time_to_render = | |
208 pts - (base::TimeTicks::Now() - start_time_ticks_ + start_pts_); | |
209 | |
210 if (time_to_render < base::TimeDelta()) { | |
211 // Skip late frames | |
212 ReleaseOutputBuffer(buffer_index, false, eos_encountered); | |
213 return; | |
214 } | |
215 | |
216 delayed_buffers_.insert(buffer_index); | |
217 | |
218 bool do_render = size > 0; | |
219 decoder_thread_.task_runner()->PostDelayedTask( | |
220 FROM_HERE, | |
221 base::Bind(&MediaCodecVideoDecoder::ReleaseOutputBuffer, | |
222 base::Unretained(this), | |
223 buffer_index, do_render, eos_encountered), | |
224 time_to_render); | |
225 } | |
226 | |
227 int MediaCodecVideoDecoder::NumDelayedRenderTasks() const { | |
228 // Decoder thread | |
229 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); | |
230 | |
231 return delayed_buffers_.size(); | |
232 } | |
233 | |
234 void MediaCodecVideoDecoder::ReleaseDelayedBuffers() { | |
235 // Media thread | |
236 // Called when there is no decoder thread | |
237 for (int index : delayed_buffers_) | |
238 media_codec_bridge_->ReleaseOutputBuffer(index, false); | |
239 delayed_buffers_.clear(); | |
240 } | |
241 | |
242 void MediaCodecVideoDecoder::ReleaseOutputBuffer( | |
243 int buffer_index, bool render, bool eos_encountered) { | |
244 // Decoder thread | |
245 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); | |
246 | |
247 DVLOG(2) << class_name() << "::" << __FUNCTION__; | |
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 | |
256 } // namespace media | |
OLD | NEW |