OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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 "content/common/gpu/media/android_video_decode_accelerator.h" | |
6 | |
7 #include <jni.h> | |
8 | |
9 #include "base/android/jni_android.h" | |
10 #include "base/android/scoped_java_ref.h" | |
11 #include "base/bind.h" | |
12 #include "base/logging.h" | |
13 #include "base/message_loop.h" | |
14 #include "content/common/android/surface_callback.h" | |
15 #include "content/common/gpu/gpu_channel.h" | |
16 #include "content/common/gpu/media/gles2_external_texture_copier.h" | |
17 #include "media/base/bitstream_buffer.h" | |
18 #include "media/video/picture.h" | |
19 | |
20 // XXX: apply the scheme for GL access. http://crbug.com/169433 | |
Ami GONE FROM CHROMIUM
2013/02/13 18:07:11
Yay
| |
21 #include "third_party/angle/include/GLES2/gl2.h" | |
22 #include "third_party/angle/include/GLES2/gl2ext.h" | |
23 | |
24 using base::android::MethodID; | |
25 using base::android::ScopedJavaLocalRef; | |
26 | |
27 namespace content { | |
28 | |
29 // XXX: drop the below before submitting. | |
30 #define LOG_LINE() LOG(INFO) << __FUNCTION__ | |
31 | |
32 // Helper macros for dealing with failure. If |result| evaluates false, emit | |
33 // |log| to ERROR, register |error| with the decoder, and return. | |
34 #define RETURN_ON_FAILURE(result, log, error) \ | |
35 do { \ | |
36 if (!(result)) { \ | |
37 DLOG(ERROR) << log; \ | |
38 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( \ | |
39 &AndroidVideoDecodeAccelerator::NotifyError, \ | |
40 base::AsWeakPtr(this), error)); \ | |
41 state_ = ERROR; \ | |
42 return; \ | |
43 } \ | |
44 } while (0) | |
45 | |
46 enum { kNumPictureBuffers = 4 }; | |
47 | |
48 // static | |
49 const base::TimeDelta AndroidVideoDecodeAccelerator::kDecodePollDelay = | |
50 base::TimeDelta::FromMilliseconds(10); | |
51 | |
52 AndroidVideoDecodeAccelerator::AndroidVideoDecodeAccelerator( | |
53 media::VideoDecodeAccelerator::Client* client, | |
54 const base::Callback<bool(void)>& make_context_current) | |
55 : client_(client), | |
56 make_context_current_(make_context_current), | |
57 codec_(media::MediaCodecBridge::VIDEO_H264), | |
58 state_(NO_ERROR), | |
59 surface_texture_id_(-1), | |
60 picturebuffers_requested_(false), | |
61 io_task_is_posted_(false), | |
62 decoder_met_eos_(false), | |
63 num_bytes_used_in_the_pending_buffer_(0) { | |
64 LOG_LINE(); | |
65 } | |
66 | |
67 AndroidVideoDecodeAccelerator::~AndroidVideoDecodeAccelerator() { | |
68 LOG_LINE(); | |
69 DCHECK(thread_checker_.CalledOnValidThread()); | |
70 } | |
71 | |
72 bool AndroidVideoDecodeAccelerator::Initialize( | |
73 media::VideoCodecProfile profile) { | |
74 LOG_LINE(); | |
75 DCHECK(!media_codec_); | |
76 DCHECK(thread_checker_.CalledOnValidThread()); | |
77 | |
78 if (profile == media::VP8PROFILE_MAIN) { | |
79 codec_ = media::MediaCodecBridge::VIDEO_VP8; | |
80 } else if (profile >= media::H264PROFILE_MIN && | |
81 profile <= media::H264PROFILE_MAX) { | |
82 codec_ = media::MediaCodecBridge::VIDEO_H264; | |
83 } else { | |
84 LOG(ERROR) << "Unsupported profile: " << profile; | |
85 return false; | |
86 } | |
87 | |
88 if (!make_context_current_.Run()) { | |
89 LOG(ERROR) << "Failed to make this decoder's GL context current."; | |
90 return false; | |
91 } | |
92 | |
93 glGenTextures(1, &surface_texture_id_); | |
94 glActiveTexture(GL_TEXTURE0); | |
95 glBindTexture(GL_TEXTURE_EXTERNAL_OES, surface_texture_id_); | |
96 | |
97 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
98 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
99 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, | |
100 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
101 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, | |
102 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
103 | |
104 surface_texture_ = new SurfaceTextureBridge(surface_texture_id_); | |
105 | |
106 ConfigureMediaCodec(); | |
107 | |
108 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | |
109 &AndroidVideoDecodeAccelerator::NotifyInitializeDone, | |
110 base::AsWeakPtr(this))); | |
111 return true; | |
112 } | |
113 | |
114 void AndroidVideoDecodeAccelerator::DoIOTask() { | |
115 io_task_is_posted_ = false; | |
116 if (state_ == ERROR) { | |
117 return; | |
118 } | |
119 | |
120 DequeueOutput(); | |
121 QueueInput(); | |
122 | |
123 if (!pending_bitstream_buffers_.empty() || | |
124 !free_picture_ids_.empty()) { | |
125 io_task_is_posted_ = true; | |
126 MessageLoop::current()->PostDelayedTask( | |
127 FROM_HERE, | |
128 base::Bind( | |
129 &AndroidVideoDecodeAccelerator::DoIOTask, base::AsWeakPtr(this)), | |
130 kDecodePollDelay); | |
131 } | |
132 } | |
133 | |
134 void AndroidVideoDecodeAccelerator::QueueInput() { | |
135 if (pending_bitstream_buffers_.empty()) | |
Ami GONE FROM CHROMIUM
2013/02/07 19:40:16
No it does not. You can either include braces or
dwkang1
2013/02/13 12:57:19
Thanks for the explanation.
| |
136 return; | |
137 | |
138 int input_buf_index = media_codec_->DequeueInputBuffer( | |
139 media::MediaCodecBridge::kTimeOutNoWait); | |
140 if (input_buf_index < 0) { | |
141 DCHECK_EQ(input_buf_index, media::MediaCodecBridge::INFO_TRY_AGAIN_LATER); | |
142 return; | |
143 } | |
144 media::BitstreamBuffer& bitstream_buffer = | |
145 pending_bitstream_buffers_.front(); | |
146 | |
147 int flags = 0; | |
148 if (bitstream_buffer.id() == -1) { | |
149 flags |= media::MediaCodecBridge::BUFFER_FLAG_END_OF_STREAM; | |
150 } | |
151 int bytes_written = 0; | |
152 if (bitstream_buffer.size() > 0) { | |
153 scoped_ptr<base::SharedMemory> shm( | |
154 new base::SharedMemory(bitstream_buffer.handle(), true)); | |
155 | |
156 RETURN_ON_FAILURE(shm->Map(bitstream_buffer.size()), | |
157 "Failed to SharedMemory::Map()", | |
158 UNREADABLE_INPUT); | |
159 | |
160 | |
161 const size_t offset = num_bytes_used_in_the_pending_buffer_; | |
162 bytes_written = media_codec_->PutToInputBuffer( | |
163 input_buf_index, | |
164 static_cast<const uint8*>(shm->memory()) + offset, | |
165 bitstream_buffer.size() - offset); | |
166 num_bytes_used_in_the_pending_buffer_ += bytes_written; | |
167 CHECK_LE(num_bytes_used_in_the_pending_buffer_, bitstream_buffer.size()); | |
168 } | |
169 | |
170 if (num_bytes_used_in_the_pending_buffer_ == bitstream_buffer.size()) { | |
171 num_bytes_used_in_the_pending_buffer_ = 0; | |
172 pending_bitstream_buffers_.pop(); | |
173 } | |
174 | |
175 // Abuse the presentation time argument to propagate the bitstream | |
176 // buffer ID to the output, so we can report it back to the client in | |
177 // PictureReady(). | |
178 base::TimeDelta timestamp = | |
179 base::TimeDelta::FromMicroseconds(bitstream_buffer.id()); | |
180 media_codec_->QueueInputBuffer( | |
181 input_buf_index, 0, | |
182 bytes_written, | |
183 timestamp, flags); | |
184 | |
185 if (bitstream_buffer.id() != -1) { | |
Ami GONE FROM CHROMIUM
2013/02/07 19:40:16
&& num_bytes_used_in_the_pending_buffer_ == bitstr
dwkang1
2013/02/13 12:57:19
Correct. will fix in the next patch set.
| |
186 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | |
187 &AndroidVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer, | |
188 base::AsWeakPtr(this), bitstream_buffer.id())); | |
189 } | |
190 } | |
191 | |
192 void AndroidVideoDecodeAccelerator::DequeueOutput() { | |
193 if (picturebuffers_requested_ && output_picture_buffers_.empty()) | |
194 return; | |
195 | |
196 if (!output_picture_buffers_.empty() && free_picture_ids_.empty()) { | |
197 // Don't have any picture buffer to send. Need to wait more. | |
198 return; | |
199 } | |
200 | |
201 int32 flag = 0; | |
202 base::TimeDelta timestamp; | |
203 int32 buf_index = 0; | |
204 do { | |
205 int32 offset = 0; | |
206 int32 size = 0; | |
207 buf_index = media_codec_->DequeueOutputBuffer( | |
208 media::MediaCodecBridge::kTimeOutNoWait, | |
209 &offset, &size, ×tamp, &flag); | |
210 switch (buf_index) { | |
211 case media::MediaCodecBridge::INFO_TRY_AGAIN_LATER: | |
212 return; | |
213 | |
214 case media::MediaCodecBridge::INFO_OUTPUT_FORMAT_CHANGED: { | |
215 int32 width, height; | |
216 media_codec_->GetOutputFormat(&width, &height); | |
217 | |
218 if (!picturebuffers_requested_) { | |
219 picturebuffers_requested_ = true; | |
220 size_ = gfx::Size(width, height); | |
221 texture_copier_.reset(new Gles2ExternalTextureCopier()); | |
222 texture_copier_->Init(width, height); | |
223 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | |
224 &AndroidVideoDecodeAccelerator::RequestPictureBuffers, | |
225 base::AsWeakPtr(this))); | |
226 | |
227 } else { | |
228 // TODO(dwkang): support the dynamic resolution change. | |
229 RETURN_ON_FAILURE(size_ == gfx::Size(width, height), | |
Ami GONE FROM CHROMIUM
2013/02/07 19:40:16
I think you missed my point. If the == is true, t
dwkang1
2013/02/13 12:57:19
Agreed with you. But I realized that this should p
| |
230 "Dynamic resolution change is not supported.", | |
231 PLATFORM_FAILURE); | |
232 } | |
233 return; | |
234 } | |
235 | |
236 case media::MediaCodecBridge::INFO_OUTPUT_BUFFERS_CHANGED: | |
237 media_codec_->GetOutputBuffers(); | |
238 break; | |
239 } | |
240 } while (buf_index < 0); | |
241 | |
242 if (flag & media::MediaCodecBridge::BUFFER_FLAG_END_OF_STREAM) { | |
243 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | |
244 &AndroidVideoDecodeAccelerator::NotifyFlushDone, | |
245 base::AsWeakPtr(this))); | |
246 } | |
247 | |
248 media_codec_->ReleaseOutputBuffer(buf_index, true); | |
249 | |
250 int64 bitstream_buffer_id = timestamp.InMicroseconds(); | |
251 if (bitstream_buffer_id != -1) | |
252 SendCurrentSurfaceToClient(static_cast<int32>(bitstream_buffer_id)); | |
253 else | |
254 decoder_met_eos_ = true; | |
255 } | |
256 | |
257 void AndroidVideoDecodeAccelerator::SendCurrentSurfaceToClient( | |
258 int32 bitstream_id) { | |
259 LOG_LINE(); | |
260 DCHECK(thread_checker_.CalledOnValidThread()); | |
261 DCHECK_NE(bitstream_id, -1); | |
262 DCHECK(!free_picture_ids_.empty()); | |
263 | |
264 RETURN_ON_FAILURE(make_context_current_.Run(), | |
265 "Failed to make this decoder's GL context current.", | |
266 PLATFORM_FAILURE); | |
267 | |
268 int32 picture_buffer_id = free_picture_ids_.front(); | |
269 free_picture_ids_.pop(); | |
270 | |
271 float transfrom_matrix[16]; | |
272 surface_texture_->UpdateTexImage(); | |
273 surface_texture_->GetTransformMatrix(transfrom_matrix); | |
274 | |
275 OutputBufferMap::const_iterator i = | |
276 output_picture_buffers_.find(picture_buffer_id); | |
277 RETURN_ON_FAILURE(i != output_picture_buffers_.end(), | |
278 "Can't find a PictureBuffer for " << picture_buffer_id, | |
279 PLATFORM_FAILURE); | |
280 uint32 picture_buffer_texture_id = i->second.texture_id(); | |
281 | |
282 // Here, we copy |surface_texture_id_| to the picture buffer instead of | |
283 // setting new texture to |surface_texture_| by calling attachToGLContext() | |
284 // because: | |
285 // 1. Once we call detachFrameGLContext(), it deletes the texture previous | |
286 // attached. | |
287 // 2. SurfaceTexture requires us to apply a transform matrix when we show | |
288 // the texture. | |
289 texture_copier_->Copy( | |
290 surface_texture_id_, picture_buffer_texture_id, transfrom_matrix); | |
291 | |
292 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | |
293 &AndroidVideoDecodeAccelerator::NotifyPictureReady, | |
294 base::AsWeakPtr(this), media::Picture(picture_buffer_id, bitstream_id))); | |
295 } | |
296 | |
297 void AndroidVideoDecodeAccelerator::Decode( | |
298 const media::BitstreamBuffer& bitstream_buffer) { | |
299 LOG_LINE(); | |
300 DCHECK(thread_checker_.CalledOnValidThread()); | |
301 pending_bitstream_buffers_.push(bitstream_buffer); | |
302 | |
303 if (!io_task_is_posted_) | |
304 DoIOTask(); | |
305 } | |
306 | |
307 void AndroidVideoDecodeAccelerator::AssignPictureBuffers( | |
308 const std::vector<media::PictureBuffer>& buffers) { | |
309 LOG_LINE(); | |
310 DCHECK(thread_checker_.CalledOnValidThread()); | |
311 DCHECK(output_picture_buffers_.empty()); | |
312 | |
313 for (size_t i = 0; i < buffers.size(); ++i) { | |
314 output_picture_buffers_.insert(std::make_pair(buffers[i].id(), buffers[i])); | |
315 free_picture_ids_.push(buffers[i].id()); | |
316 } | |
317 | |
318 RETURN_ON_FAILURE(output_picture_buffers_.size() == kNumPictureBuffers, | |
319 "Invalid picture buffers were passed.", | |
320 INVALID_ARGUMENT); | |
321 | |
322 if (!io_task_is_posted_) | |
323 DoIOTask(); | |
324 } | |
325 | |
326 void AndroidVideoDecodeAccelerator::ReusePictureBuffer( | |
327 int32 picture_buffer_id) { | |
328 DCHECK(thread_checker_.CalledOnValidThread()); | |
329 free_picture_ids_.push(picture_buffer_id); | |
330 | |
331 if (!io_task_is_posted_) | |
332 DoIOTask(); | |
333 } | |
334 | |
335 void AndroidVideoDecodeAccelerator::Flush() { | |
336 LOG_LINE(); | |
337 DCHECK(thread_checker_.CalledOnValidThread()); | |
338 | |
339 Decode(media::BitstreamBuffer(-1, base::SharedMemoryHandle(), 0)); | |
340 } | |
341 | |
342 void AndroidVideoDecodeAccelerator::ConfigureMediaCodec() { | |
343 DCHECK(surface_texture_.get()); | |
344 | |
345 media_codec_.reset(new media::MediaCodecBridge(codec_)); | |
346 | |
347 JNIEnv* env = base::android::AttachCurrentThread(); | |
348 CHECK(env); | |
349 ScopedJavaLocalRef<jclass> cls( | |
350 base::android::GetClass(env, "android/view/Surface")); | |
351 jmethodID constructor = MethodID::Get<MethodID::TYPE_INSTANCE>( | |
352 env, cls.obj(), "<init>", "(Landroid/graphics/SurfaceTexture;)V"); | |
353 ScopedJavaLocalRef<jobject> j_surface( | |
354 env, env->NewObject( | |
355 cls.obj(), constructor, | |
356 surface_texture_->j_surface_texture().obj())); | |
357 | |
358 // VDA does not pass the container indicated resolution in the initialization | |
359 // phase. Here, we set 720p by default. | |
360 // TODO(dwkang): find out a way to remove the following hard-coded value. | |
361 media_codec_->StartVideo(codec_, gfx::Size(1280, 720), j_surface.obj()); | |
362 content::ReleaseSurface(j_surface.obj()); | |
363 media_codec_->GetOutputBuffers(); | |
364 } | |
365 | |
366 void AndroidVideoDecodeAccelerator::Reset() { | |
367 LOG_LINE(); | |
368 DCHECK(thread_checker_.CalledOnValidThread()); | |
369 | |
370 while(!pending_bitstream_buffers_.empty()) { | |
371 media::BitstreamBuffer& bitstream_buffer = | |
372 pending_bitstream_buffers_.front(); | |
373 pending_bitstream_buffers_.pop(); | |
374 | |
375 if (bitstream_buffer.id() != -1) { | |
376 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | |
377 &AndroidVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer, | |
378 base::AsWeakPtr(this), bitstream_buffer.id())); | |
379 } | |
380 } | |
381 | |
382 if (!decoder_met_eos_) { | |
383 media_codec_->Reset(); | |
384 } else { | |
385 // MediaCodec should be usable after meeting EOS, but it is not on some | |
386 // devices. b/8125974 To avoid the case, we recreate a new one. | |
387 media_codec_->Stop(); | |
388 ConfigureMediaCodec(); | |
389 } | |
390 decoder_met_eos_ = false; | |
391 num_bytes_used_in_the_pending_buffer_ = 0; | |
392 state_ = NO_ERROR; | |
393 | |
394 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | |
395 &AndroidVideoDecodeAccelerator::NotifyResetDone, base::AsWeakPtr(this))); | |
396 } | |
397 | |
398 void AndroidVideoDecodeAccelerator::Destroy() { | |
399 LOG_LINE(); | |
400 DCHECK(thread_checker_.CalledOnValidThread()); | |
401 | |
402 if (media_codec_) media_codec_->Stop(); | |
403 delete this; | |
404 } | |
405 | |
406 void AndroidVideoDecodeAccelerator::NotifyInitializeDone() { | |
407 client_->NotifyInitializeDone(); | |
408 } | |
409 | |
410 void AndroidVideoDecodeAccelerator::RequestPictureBuffers() { | |
411 client_->ProvidePictureBuffers(kNumPictureBuffers, size_, GL_TEXTURE_2D); | |
412 } | |
413 | |
414 void AndroidVideoDecodeAccelerator::NotifyPictureReady( | |
415 const media::Picture& picture) { | |
416 client_->PictureReady(picture); | |
417 } | |
418 | |
419 void AndroidVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer( | |
420 int input_buffer_id) { | |
421 client_->NotifyEndOfBitstreamBuffer(input_buffer_id); | |
422 } | |
423 | |
424 void AndroidVideoDecodeAccelerator::NotifyFlushDone() { | |
425 client_->NotifyFlushDone(); | |
426 } | |
427 | |
428 void AndroidVideoDecodeAccelerator::NotifyResetDone() { | |
429 client_->NotifyResetDone(); | |
430 } | |
431 | |
432 void AndroidVideoDecodeAccelerator::NotifyError( | |
433 media::VideoDecodeAccelerator::Error error) { | |
434 client_->NotifyError(error); | |
435 } | |
436 | |
437 } // namespace content | |
OLD | NEW |