Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: content/common/gpu/media/android_video_decode_accelerator_base.cc

Issue 1313913003: Begin refactor of AVDA to support zero-copy. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: cl feedback Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/common/gpu/media/android_video_decode_accelerator.h" 5 #include "content/common/gpu/media/android_video_decode_accelerator_base.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
11 #include "content/common/gpu/gpu_channel.h" 11 #include "content/common/gpu/gpu_channel.h"
12 #include "content/common/gpu/media/avda_return_on_failure.h"
12 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" 13 #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
13 #include "media/base/bitstream_buffer.h" 14 #include "media/base/bitstream_buffer.h"
14 #include "media/base/limits.h" 15 #include "media/base/limits.h"
15 #include "media/base/video_decoder_config.h" 16 #include "media/base/video_decoder_config.h"
16 #include "media/video/picture.h" 17 #include "media/video/picture.h"
17 #include "ui/gl/android/scoped_java_surface.h" 18 #include "ui/gl/android/scoped_java_surface.h"
18 #include "ui/gl/android/surface_texture.h" 19 #include "ui/gl/android/surface_texture.h"
19 #include "ui/gl/gl_bindings.h" 20 #include "ui/gl/gl_bindings.h"
20 21
21 namespace content { 22 namespace content {
22 23
23 // Helper macros for dealing with failure. If |result| evaluates false, emit
24 // |log| to ERROR, register |error| with the decoder, and return.
25 #define RETURN_ON_FAILURE(result, log, error) \
26 do { \
27 if (!(result)) { \
28 DLOG(ERROR) << log; \
29 base::MessageLoop::current()->PostTask( \
30 FROM_HERE, \
31 base::Bind(&AndroidVideoDecodeAccelerator::NotifyError, \
32 weak_this_factory_.GetWeakPtr(), \
33 error)); \
34 state_ = ERROR; \
35 return; \
36 } \
37 } while (0)
38
39 // TODO(dwkang): We only need kMaxVideoFrames to pass media stack's prerolling
40 // phase, but 1 is added due to crbug.com/176036. This should be tuned when we
41 // have actual use case.
42 enum { kNumPictureBuffers = media::limits::kMaxVideoFrames + 1 };
43
44 // Max number of bitstreams notified to the client with 24 // Max number of bitstreams notified to the client with
45 // NotifyEndOfBitstreamBuffer() before getting output from the bitstream. 25 // NotifyEndOfBitstreamBuffer() before getting output from the bitstream.
46 enum { kMaxBitstreamsNotifiedInAdvance = 32 }; 26 enum { kMaxBitstreamsNotifiedInAdvance = 32 };
47 27
48 #if defined(ENABLE_MEDIA_PIPELINE_ON_ANDROID) 28 #if defined(ENABLE_MEDIA_PIPELINE_ON_ANDROID)
49 // MediaCodec is only guaranteed to support baseline, but some devices may 29 // MediaCodec is only guaranteed to support baseline, but some devices may
50 // support others. Advertise support for all H264 profiles and let the 30 // support others. Advertise support for all H264 profiles and let the
51 // MediaCodec fail when decoding if it's not actually supported. It's assumed 31 // MediaCodec fail when decoding if it's not actually supported. It's assumed
52 // that consumers won't have software fallback for H264 on Android anyway. 32 // that consumers won't have software fallback for H264 on Android anyway.
53 static const media::VideoCodecProfile kSupportedH264Profiles[] = { 33 static const media::VideoCodecProfile kSupportedH264Profiles[] = {
(...skipping 26 matching lines...) Expand all
80 // pictures have been fed to saturate any internal buffering). This is 60 // pictures have been fed to saturate any internal buffering). This is
81 // speculative and it's unclear that this would be a win (nor that there's a 61 // speculative and it's unclear that this would be a win (nor that there's a
82 // reasonably device-agnostic way to fill in the "believes" above). 62 // reasonably device-agnostic way to fill in the "believes" above).
83 return base::TimeDelta::FromMilliseconds(10); 63 return base::TimeDelta::FromMilliseconds(10);
84 } 64 }
85 65
86 static inline const base::TimeDelta NoWaitTimeOut() { 66 static inline const base::TimeDelta NoWaitTimeOut() {
87 return base::TimeDelta::FromMicroseconds(0); 67 return base::TimeDelta::FromMicroseconds(0);
88 } 68 }
89 69
90 AndroidVideoDecodeAccelerator::AndroidVideoDecodeAccelerator( 70 AndroidVideoDecodeAcceleratorBase::AndroidVideoDecodeAcceleratorBase(
91 const base::WeakPtr<gpu::gles2::GLES2Decoder> decoder, 71 const base::WeakPtr<gpu::gles2::GLES2Decoder> decoder,
92 const base::Callback<bool(void)>& make_context_current) 72 const base::Callback<bool(void)>& make_context_current)
93 : client_(NULL), 73 : client_(NULL),
94 make_context_current_(make_context_current), 74 make_context_current_(make_context_current),
95 codec_(media::kCodecH264), 75 codec_(media::kCodecH264),
96 state_(NO_ERROR), 76 state_(NO_ERROR),
97 surface_texture_id_(0), 77 surface_texture_id_(0),
98 picturebuffers_requested_(false), 78 picturebuffers_requested_(false),
99 gl_decoder_(decoder), 79 gl_decoder_(decoder),
100 weak_this_factory_(this) {} 80 weak_this_factory_(this) {}
101 81
102 AndroidVideoDecodeAccelerator::~AndroidVideoDecodeAccelerator() { 82 AndroidVideoDecodeAcceleratorBase::~AndroidVideoDecodeAcceleratorBase() {
103 DCHECK(thread_checker_.CalledOnValidThread()); 83 DCHECK(thread_checker_.CalledOnValidThread());
104 } 84 }
105 85
106 bool AndroidVideoDecodeAccelerator::Initialize(media::VideoCodecProfile profile, 86 bool AndroidVideoDecodeAcceleratorBase::Initialize(
107 Client* client) { 87 media::VideoCodecProfile profile,
88 Client* client) {
108 DCHECK(!media_codec_); 89 DCHECK(!media_codec_);
109 DCHECK(thread_checker_.CalledOnValidThread()); 90 DCHECK(thread_checker_.CalledOnValidThread());
110 91
111 client_ = client; 92 client_ = client;
112 codec_ = VideoCodecProfileToVideoCodec(profile); 93 codec_ = VideoCodecProfileToVideoCodec(profile);
113 94
114 bool profile_supported = codec_ == media::kCodecVP8; 95 bool profile_supported = codec_ == media::kCodecVP8;
115 #if defined(ENABLE_MEDIA_PIPELINE_ON_ANDROID) 96 #if defined(ENABLE_MEDIA_PIPELINE_ON_ANDROID)
116 profile_supported |= 97 profile_supported |=
117 (codec_ == media::kCodecVP9 || codec_ == media::kCodecH264); 98 (codec_ == media::kCodecVP9 || codec_ == media::kCodecH264);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 surface_texture_ = gfx::SurfaceTexture::Create(surface_texture_id_); 138 surface_texture_ = gfx::SurfaceTexture::Create(surface_texture_id_);
158 139
159 if (!ConfigureMediaCodec()) { 140 if (!ConfigureMediaCodec()) {
160 LOG(ERROR) << "Failed to create MediaCodec instance."; 141 LOG(ERROR) << "Failed to create MediaCodec instance.";
161 return false; 142 return false;
162 } 143 }
163 144
164 return true; 145 return true;
165 } 146 }
166 147
167 void AndroidVideoDecodeAccelerator::DoIOTask() { 148 void AndroidVideoDecodeAcceleratorBase::DoIOTask() {
168 DCHECK(thread_checker_.CalledOnValidThread()); 149 DCHECK(thread_checker_.CalledOnValidThread());
169 if (state_ == ERROR) { 150 if (state_ == ERROR) {
170 return; 151 return;
171 } 152 }
172 153
173 QueueInput(); 154 QueueInput();
174 DequeueOutput(); 155 DequeueOutput();
175 } 156 }
176 157
177 void AndroidVideoDecodeAccelerator::QueueInput() { 158 void AndroidVideoDecodeAcceleratorBase::QueueInput() {
178 DCHECK(thread_checker_.CalledOnValidThread()); 159 DCHECK(thread_checker_.CalledOnValidThread());
179 if (bitstreams_notified_in_advance_.size() > kMaxBitstreamsNotifiedInAdvance) 160 if (bitstreams_notified_in_advance_.size() > kMaxBitstreamsNotifiedInAdvance)
180 return; 161 return;
181 if (pending_bitstream_buffers_.empty()) 162 if (pending_bitstream_buffers_.empty())
182 return; 163 return;
183 164
184 int input_buf_index = 0; 165 int input_buf_index = 0;
185 media::MediaCodecStatus status = media_codec_->DequeueInputBuffer( 166 media::MediaCodecStatus status = media_codec_->DequeueInputBuffer(
186 NoWaitTimeOut(), &input_buf_index); 167 NoWaitTimeOut(), &input_buf_index);
187 if (status != media::MEDIA_CODEC_OK) { 168 if (status != media::MEDIA_CODEC_OK) {
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 207
227 // We should call NotifyEndOfBitstreamBuffer(), when no more decoded output 208 // We should call NotifyEndOfBitstreamBuffer(), when no more decoded output
228 // will be returned from the bitstream buffer. However, MediaCodec API is 209 // will be returned from the bitstream buffer. However, MediaCodec API is
229 // not enough to guarantee it. 210 // not enough to guarantee it.
230 // So, here, we calls NotifyEndOfBitstreamBuffer() in advance in order to 211 // So, here, we calls NotifyEndOfBitstreamBuffer() in advance in order to
231 // keep getting more bitstreams from the client, and throttle them by using 212 // keep getting more bitstreams from the client, and throttle them by using
232 // |bitstreams_notified_in_advance_|. 213 // |bitstreams_notified_in_advance_|.
233 // TODO(dwkang): check if there is a way to remove this workaround. 214 // TODO(dwkang): check if there is a way to remove this workaround.
234 base::MessageLoop::current()->PostTask( 215 base::MessageLoop::current()->PostTask(
235 FROM_HERE, 216 FROM_HERE,
236 base::Bind(&AndroidVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer, 217 base::Bind(&AndroidVideoDecodeAcceleratorBase::NotifyEndOfBitstreamBuffer,
237 weak_this_factory_.GetWeakPtr(), 218 weak_this_factory_.GetWeakPtr(),
238 bitstream_buffer.id())); 219 bitstream_buffer.id()));
239 bitstreams_notified_in_advance_.push_back(bitstream_buffer.id()); 220 bitstreams_notified_in_advance_.push_back(bitstream_buffer.id());
240 } 221 }
241 222
242 void AndroidVideoDecodeAccelerator::DequeueOutput() { 223 void AndroidVideoDecodeAcceleratorBase::DequeueOutput() {
243 DCHECK(thread_checker_.CalledOnValidThread()); 224 DCHECK(thread_checker_.CalledOnValidThread());
244 if (picturebuffers_requested_ && output_picture_buffers_.empty()) 225 if (picturebuffers_requested_ && output_picture_buffers_.empty())
245 return; 226 return;
246 227
247 if (!output_picture_buffers_.empty() && free_picture_ids_.empty()) { 228 if (!output_picture_buffers_.empty() && free_picture_ids_.empty()) {
248 // Don't have any picture buffer to send. Need to wait more. 229 // Don't have any picture buffer to send. Need to wait more.
249 return; 230 return;
250 } 231 }
251 232
252 bool eos = false; 233 bool eos = false;
(...skipping 12 matching lines...) Expand all
265 246
266 case media::MEDIA_CODEC_OUTPUT_FORMAT_CHANGED: { 247 case media::MEDIA_CODEC_OUTPUT_FORMAT_CHANGED: {
267 int32 width, height; 248 int32 width, height;
268 media_codec_->GetOutputFormat(&width, &height); 249 media_codec_->GetOutputFormat(&width, &height);
269 250
270 if (!picturebuffers_requested_) { 251 if (!picturebuffers_requested_) {
271 picturebuffers_requested_ = true; 252 picturebuffers_requested_ = true;
272 size_ = gfx::Size(width, height); 253 size_ = gfx::Size(width, height);
273 base::MessageLoop::current()->PostTask( 254 base::MessageLoop::current()->PostTask(
274 FROM_HERE, 255 FROM_HERE,
275 base::Bind(&AndroidVideoDecodeAccelerator::RequestPictureBuffers, 256 base::Bind(&AndroidVideoDecodeAcceleratorBase::
257 RequestPictureBuffers,
276 weak_this_factory_.GetWeakPtr())); 258 weak_this_factory_.GetWeakPtr()));
277 } else { 259 } else {
278 // Dynamic resolution change support is not specified by the Android 260 // Dynamic resolution change support is not specified by the Android
279 // platform at and before JB-MR1, so it's not possible to smoothly 261 // platform at and before JB-MR1, so it's not possible to smoothly
280 // continue playback at this point. Instead, error out immediately, 262 // continue playback at this point. Instead, error out immediately,
281 // expecting clients to Reset() as appropriate to avoid this. 263 // expecting clients to Reset() as appropriate to avoid this.
282 // b/7093648 264 // b/7093648
283 RETURN_ON_FAILURE(size_ == gfx::Size(width, height), 265 RETURN_ON_FAILURE(size_ == gfx::Size(width, height),
284 "Dynamic resolution change is not supported.", 266 "Dynamic resolution change is not supported.",
285 PLATFORM_FAILURE); 267 PLATFORM_FAILURE);
286 } 268 }
287 return; 269 return;
288 } 270 }
289 271
290 case media::MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED: 272 case media::MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED:
291 break; 273 break;
292 274
293 case media::MEDIA_CODEC_OK: 275 case media::MEDIA_CODEC_OK:
294 DCHECK_GE(buf_index, 0); 276 DCHECK_GE(buf_index, 0);
295 break; 277 break;
296 278
297 default: 279 default:
298 NOTREACHED(); 280 NOTREACHED();
299 break; 281 break;
300 } 282 }
301 } while (buf_index < 0); 283 } while (buf_index < 0);
302 284
303 // This ignores the emitted ByteBuffer and instead relies on rendering to the
304 // codec's SurfaceTexture and then copying from that texture to the client's
305 // PictureBuffer's texture. This means that each picture's data is written
306 // three times: once to the ByteBuffer, once to the SurfaceTexture, and once
307 // to the client's texture. It would be nicer to either:
308 // 1) Render directly to the client's texture from MediaCodec (one write); or
309 // 2) Upload the ByteBuffer to the client's texture (two writes).
310 // Unfortunately neither is possible:
311 // 1) MediaCodec's use of SurfaceTexture is a singleton, and the texture
312 // written to can't change during the codec's lifetime. b/11990461
313 // 2) The ByteBuffer is likely to contain the pixels in a vendor-specific,
314 // opaque/non-standard format. It's not possible to negotiate the decoder
315 // to emit a specific colorspace, even using HW CSC. b/10706245
316 // So, we live with these two extra copies per picture :(
317 media_codec_->ReleaseOutputBuffer(buf_index, true);
318
319 if (eos) { 285 if (eos) {
286 // TODO(liberato): Before refactoring into *Base, this was unconditionally
287 // done before the eos check, with render==true. However, since that
288 // frame wasn't sent anywhere in the eos case, we now do it here with
289 // render==false. We need to see if eos can actually deliver a valid
290 // frame with it.
291 media_codec_->ReleaseOutputBuffer(buf_index, false);
320 base::MessageLoop::current()->PostTask( 292 base::MessageLoop::current()->PostTask(
321 FROM_HERE, 293 FROM_HERE,
322 base::Bind(&AndroidVideoDecodeAccelerator::NotifyFlushDone, 294 base::Bind(&AndroidVideoDecodeAcceleratorBase::NotifyFlushDone,
323 weak_this_factory_.GetWeakPtr())); 295 weak_this_factory_.GetWeakPtr()));
324 } else { 296 } else {
325 int64 bitstream_buffer_id = timestamp.InMicroseconds(); 297 int64 bitstream_buffer_id = timestamp.InMicroseconds();
326 SendCurrentSurfaceToClient(static_cast<int32>(bitstream_buffer_id)); 298 SendCurrentSurfaceToClient(buf_index,
299 static_cast<int32>(bitstream_buffer_id));
327 300
328 // Removes ids former or equal than the id from decoder. Note that 301 // Removes ids former or equal than the id from decoder. Note that
329 // |bitstreams_notified_in_advance_| does not mean bitstream ids in decoder 302 // |bitstreams_notified_in_advance_| does not mean bitstream ids in decoder
330 // because of frame reordering issue. We just maintain this roughly and use 303 // because of frame reordering issue. We just maintain this roughly and use
331 // for the throttling purpose. 304 // for the throttling purpose.
332 std::list<int32>::iterator it; 305 std::list<int32>::iterator it;
333 for (it = bitstreams_notified_in_advance_.begin(); 306 for (it = bitstreams_notified_in_advance_.begin();
334 it != bitstreams_notified_in_advance_.end(); 307 it != bitstreams_notified_in_advance_.end();
335 ++it) { 308 ++it) {
336 if (*it == bitstream_buffer_id) { 309 if (*it == bitstream_buffer_id) {
337 bitstreams_notified_in_advance_.erase( 310 bitstreams_notified_in_advance_.erase(
338 bitstreams_notified_in_advance_.begin(), ++it); 311 bitstreams_notified_in_advance_.begin(), ++it);
339 break; 312 break;
340 } 313 }
341 } 314 }
342 } 315 }
343 } 316 }
344 317
345 void AndroidVideoDecodeAccelerator::SendCurrentSurfaceToClient( 318 void AndroidVideoDecodeAcceleratorBase::SendCurrentSurfaceToClient(
319 int32 codec_buffer_index,
346 int32 bitstream_id) { 320 int32 bitstream_id) {
347 DCHECK(thread_checker_.CalledOnValidThread()); 321 DCHECK(thread_checker_.CalledOnValidThread());
348 DCHECK_NE(bitstream_id, -1); 322 DCHECK_NE(bitstream_id, -1);
349 DCHECK(!free_picture_ids_.empty()); 323 DCHECK(!free_picture_ids_.empty());
350 324
351 RETURN_ON_FAILURE(make_context_current_.Run(), 325 RETURN_ON_FAILURE(make_context_current_.Run(),
352 "Failed to make this decoder's GL context current.", 326 "Failed to make this decoder's GL context current.",
353 PLATFORM_FAILURE); 327 PLATFORM_FAILURE);
354 328
355 int32 picture_buffer_id = free_picture_ids_.front(); 329 int32 picture_buffer_id = free_picture_ids_.front();
356 free_picture_ids_.pop(); 330 free_picture_ids_.pop();
357 331
358 float transfrom_matrix[16];
359 surface_texture_->UpdateTexImage();
360 surface_texture_->GetTransformMatrix(transfrom_matrix);
361
362 OutputBufferMap::const_iterator i = 332 OutputBufferMap::const_iterator i =
363 output_picture_buffers_.find(picture_buffer_id); 333 output_picture_buffers_.find(picture_buffer_id);
364 RETURN_ON_FAILURE(i != output_picture_buffers_.end(), 334 RETURN_ON_FAILURE(i != output_picture_buffers_.end(),
365 "Can't find a PictureBuffer for " << picture_buffer_id, 335 "Can't find a PictureBuffer for " << picture_buffer_id,
366 PLATFORM_FAILURE); 336 PLATFORM_FAILURE);
367 uint32 picture_buffer_texture_id = i->second.texture_id();
368 337
369 RETURN_ON_FAILURE(gl_decoder_.get(), 338 AssignCurrentSurfaceToPictureBuffer(codec_buffer_index, i->second);
370 "Failed to get gles2 decoder instance.",
371 ILLEGAL_STATE);
372 // Defer initializing the CopyTextureCHROMIUMResourceManager until it is
373 // needed because it takes 10s of milliseconds to initialize.
374 if (!copier_) {
375 copier_.reset(new gpu::CopyTextureCHROMIUMResourceManager());
376 copier_->Initialize(gl_decoder_.get());
377 }
378
379 // Here, we copy |surface_texture_id_| to the picture buffer instead of
380 // setting new texture to |surface_texture_| by calling attachToGLContext()
381 // because:
382 // 1. Once we call detachFrameGLContext(), it deletes the texture previous
383 // attached.
384 // 2. SurfaceTexture requires us to apply a transform matrix when we show
385 // the texture.
386 // TODO(hkuang): get the StreamTexture transform matrix in GPU process
387 // instead of using default matrix crbug.com/226218.
388 const static GLfloat default_matrix[16] = {1.0f, 0.0f, 0.0f, 0.0f,
389 0.0f, 1.0f, 0.0f, 0.0f,
390 0.0f, 0.0f, 1.0f, 0.0f,
391 0.0f, 0.0f, 0.0f, 1.0f};
392 copier_->DoCopyTextureWithTransform(gl_decoder_.get(),
393 GL_TEXTURE_EXTERNAL_OES,
394 surface_texture_id_,
395 picture_buffer_texture_id,
396 size_.width(),
397 size_.height(),
398 false,
399 false,
400 false,
401 default_matrix);
402 339
403 // TODO(henryhsu): Pass (0, 0) as visible size will cause several test 340 // TODO(henryhsu): Pass (0, 0) as visible size will cause several test
404 // cases failed. We should make sure |size_| is coded size or visible size. 341 // cases failed. We should make sure |size_| is coded size or visible size.
405 base::MessageLoop::current()->PostTask( 342 base::MessageLoop::current()->PostTask(
406 FROM_HERE, base::Bind(&AndroidVideoDecodeAccelerator::NotifyPictureReady, 343 FROM_HERE,
407 weak_this_factory_.GetWeakPtr(), 344 base::Bind(&AndroidVideoDecodeAcceleratorBase::NotifyPictureReady,
408 media::Picture(picture_buffer_id, bitstream_id, 345 weak_this_factory_.GetWeakPtr(),
409 gfx::Rect(size_), false))); 346 media::Picture(picture_buffer_id, bitstream_id,
347 gfx::Rect(size_), false)));
410 } 348 }
411 349
412 void AndroidVideoDecodeAccelerator::Decode( 350 void AndroidVideoDecodeAcceleratorBase::Decode(
413 const media::BitstreamBuffer& bitstream_buffer) { 351 const media::BitstreamBuffer& bitstream_buffer) {
414 DCHECK(thread_checker_.CalledOnValidThread()); 352 DCHECK(thread_checker_.CalledOnValidThread());
415 if (bitstream_buffer.id() != -1 && bitstream_buffer.size() == 0) { 353 if (bitstream_buffer.id() != -1 && bitstream_buffer.size() == 0) {
416 base::MessageLoop::current()->PostTask( 354 base::MessageLoop::current()->PostTask(
417 FROM_HERE, 355 FROM_HERE,
418 base::Bind(&AndroidVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer, 356 base::Bind(&AndroidVideoDecodeAcceleratorBase::
357 NotifyEndOfBitstreamBuffer,
419 weak_this_factory_.GetWeakPtr(), 358 weak_this_factory_.GetWeakPtr(),
420 bitstream_buffer.id())); 359 bitstream_buffer.id()));
421 return; 360 return;
422 } 361 }
423 362
424 pending_bitstream_buffers_.push( 363 pending_bitstream_buffers_.push(
425 std::make_pair(bitstream_buffer, base::Time::Now())); 364 std::make_pair(bitstream_buffer, base::Time::Now()));
426 365
427 DoIOTask(); 366 DoIOTask();
428 } 367 }
429 368
430 void AndroidVideoDecodeAccelerator::AssignPictureBuffers( 369 void AndroidVideoDecodeAcceleratorBase::RequestPictureBuffers() {
370 client_->ProvidePictureBuffers(GetNumPictureBuffers(), size_,
371 GetTextureTarget());
372 }
373
374 void AndroidVideoDecodeAcceleratorBase::AssignPictureBuffers(
431 const std::vector<media::PictureBuffer>& buffers) { 375 const std::vector<media::PictureBuffer>& buffers) {
432 DCHECK(thread_checker_.CalledOnValidThread()); 376 DCHECK(thread_checker_.CalledOnValidThread());
433 DCHECK(output_picture_buffers_.empty()); 377 DCHECK(output_picture_buffers_.empty());
434 DCHECK(free_picture_ids_.empty()); 378 DCHECK(free_picture_ids_.empty());
435 379
436 for (size_t i = 0; i < buffers.size(); ++i) { 380 for (size_t i = 0; i < buffers.size(); ++i) {
437 RETURN_ON_FAILURE(buffers[i].size() == size_, 381 RETURN_ON_FAILURE(buffers[i].size() == size_,
438 "Invalid picture buffer size was passed.", 382 "Invalid picture buffer size was passed.",
439 INVALID_ARGUMENT); 383 INVALID_ARGUMENT);
440 int32 id = buffers[i].id(); 384 int32 id = buffers[i].id();
441 output_picture_buffers_.insert(std::make_pair(id, buffers[i])); 385 output_picture_buffers_.insert(std::make_pair(id, buffers[i]));
442 free_picture_ids_.push(id); 386 free_picture_ids_.push(id);
443 // Since the client might be re-using |picture_buffer_id| values, forget 387 // Since the client might be re-using |picture_buffer_id| values, forget
444 // about previously-dismissed IDs now. See ReusePictureBuffer() comment 388 // about previously-dismissed IDs now. See ReusePictureBuffer() comment
445 // about "zombies" for why we maintain this set in the first place. 389 // about "zombies" for why we maintain this set in the first place.
446 dismissed_picture_ids_.erase(id); 390 dismissed_picture_ids_.erase(id);
447 } 391 }
448 392
449 RETURN_ON_FAILURE(output_picture_buffers_.size() >= kNumPictureBuffers, 393 RETURN_ON_FAILURE(output_picture_buffers_.size() >= GetNumPictureBuffers(),
450 "Invalid picture buffers were passed.", 394 "Invalid picture buffers were passed.",
451 INVALID_ARGUMENT); 395 INVALID_ARGUMENT);
452 396
453 DoIOTask(); 397 DoIOTask();
454 } 398 }
455 399
456 void AndroidVideoDecodeAccelerator::ReusePictureBuffer( 400 void AndroidVideoDecodeAcceleratorBase::ReusePictureBuffer(
457 int32 picture_buffer_id) { 401 int32 picture_buffer_id) {
458 DCHECK(thread_checker_.CalledOnValidThread()); 402 DCHECK(thread_checker_.CalledOnValidThread());
459 403
460 // This ReusePictureBuffer() might have been in a pipe somewhere (queued in 404 // This ReusePictureBuffer() might have been in a pipe somewhere (queued in
461 // IPC, or in a PostTask either at the sender or receiver) when we sent a 405 // IPC, or in a PostTask either at the sender or receiver) when we sent a
462 // DismissPictureBuffer() for this |picture_buffer_id|. Account for such 406 // DismissPictureBuffer() for this |picture_buffer_id|. Account for such
463 // potential "zombie" IDs here. 407 // potential "zombie" IDs here.
464 if (dismissed_picture_ids_.erase(picture_buffer_id)) 408 if (dismissed_picture_ids_.erase(picture_buffer_id))
465 return; 409 return;
466 410
467 free_picture_ids_.push(picture_buffer_id); 411 free_picture_ids_.push(picture_buffer_id);
468 412
469 DoIOTask(); 413 DoIOTask();
470 } 414 }
471 415
472 void AndroidVideoDecodeAccelerator::Flush() { 416 void AndroidVideoDecodeAcceleratorBase::Flush() {
473 DCHECK(thread_checker_.CalledOnValidThread()); 417 DCHECK(thread_checker_.CalledOnValidThread());
474 418
475 Decode(media::BitstreamBuffer(-1, base::SharedMemoryHandle(), 0)); 419 Decode(media::BitstreamBuffer(-1, base::SharedMemoryHandle(), 0));
476 } 420 }
477 421
478 bool AndroidVideoDecodeAccelerator::ConfigureMediaCodec() { 422 bool AndroidVideoDecodeAcceleratorBase::ConfigureMediaCodec() {
479 DCHECK(thread_checker_.CalledOnValidThread()); 423 DCHECK(thread_checker_.CalledOnValidThread());
480 DCHECK(surface_texture_.get()); 424 DCHECK(surface_texture_.get());
481 425
482 gfx::ScopedJavaSurface surface(surface_texture_.get()); 426 gfx::ScopedJavaSurface surface(surface_texture_.get());
483 427
484 // Pass a dummy 320x240 canvas size and let the codec signal the real size 428 // Pass a dummy 320x240 canvas size and let the codec signal the real size
485 // when it's known from the bitstream. 429 // when it's known from the bitstream.
486 media_codec_.reset(media::VideoCodecBridge::CreateDecoder( 430 media_codec_.reset(media::VideoCodecBridge::CreateDecoder(
487 codec_, false, gfx::Size(320, 240), surface.j_surface().obj(), NULL)); 431 codec_, false, gfx::Size(320, 240), surface.j_surface().obj(), NULL));
488 if (!media_codec_) 432 if (!media_codec_)
489 return false; 433 return false;
490 434
491 io_timer_.Start(FROM_HERE, 435 io_timer_.Start(FROM_HERE,
492 DecodePollDelay(), 436 DecodePollDelay(),
493 this, 437 this,
494 &AndroidVideoDecodeAccelerator::DoIOTask); 438 &AndroidVideoDecodeAcceleratorBase::DoIOTask);
495 return true; 439 return true;
496 } 440 }
497 441
498 void AndroidVideoDecodeAccelerator::Reset() { 442 void AndroidVideoDecodeAcceleratorBase::Reset() {
499 DCHECK(thread_checker_.CalledOnValidThread()); 443 DCHECK(thread_checker_.CalledOnValidThread());
500 444
501 while (!pending_bitstream_buffers_.empty()) { 445 while (!pending_bitstream_buffers_.empty()) {
502 int32 bitstream_buffer_id = pending_bitstream_buffers_.front().first.id(); 446 int32 bitstream_buffer_id = pending_bitstream_buffers_.front().first.id();
503 pending_bitstream_buffers_.pop(); 447 pending_bitstream_buffers_.pop();
504 448
505 if (bitstream_buffer_id != -1) { 449 if (bitstream_buffer_id != -1) {
506 base::MessageLoop::current()->PostTask( 450 base::MessageLoop::current()->PostTask(
507 FROM_HERE, 451 FROM_HERE,
508 base::Bind(&AndroidVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer, 452 base::Bind(&AndroidVideoDecodeAcceleratorBase::
453 NotifyEndOfBitstreamBuffer,
509 weak_this_factory_.GetWeakPtr(), 454 weak_this_factory_.GetWeakPtr(),
510 bitstream_buffer_id)); 455 bitstream_buffer_id));
511 } 456 }
512 } 457 }
513 bitstreams_notified_in_advance_.clear(); 458 bitstreams_notified_in_advance_.clear();
514 459
515 for (OutputBufferMap::iterator it = output_picture_buffers_.begin(); 460 for (OutputBufferMap::iterator it = output_picture_buffers_.begin();
516 it != output_picture_buffers_.end(); 461 it != output_picture_buffers_.end();
517 ++it) { 462 ++it) {
518 client_->DismissPictureBuffer(it->first); 463 client_->DismissPictureBuffer(it->first);
519 dismissed_picture_ids_.insert(it->first); 464 dismissed_picture_ids_.insert(it->first);
520 } 465 }
521 output_picture_buffers_.clear(); 466 output_picture_buffers_.clear();
522 std::queue<int32> empty; 467 std::queue<int32> empty;
523 std::swap(free_picture_ids_, empty); 468 std::swap(free_picture_ids_, empty);
524 CHECK(free_picture_ids_.empty()); 469 CHECK(free_picture_ids_.empty());
525 picturebuffers_requested_ = false; 470 picturebuffers_requested_ = false;
526 471
527 // On some devices, and up to at least JB-MR1, 472 // On some devices, and up to at least JB-MR1,
528 // - flush() can fail after EOS (b/8125974); and 473 // - flush() can fail after EOS (b/8125974); and
529 // - mid-stream resolution change is unsupported (b/7093648). 474 // - mid-stream resolution change is unsupported (b/7093648).
530 // To cope with these facts, we always stop & restart the codec on Reset(). 475 // To cope with these facts, we always stop & restart the codec on Reset().
531 io_timer_.Stop(); 476 io_timer_.Stop();
532 media_codec_->Stop(); 477 media_codec_->Stop();
533 ConfigureMediaCodec(); 478 ConfigureMediaCodec();
534 state_ = NO_ERROR; 479 state_ = NO_ERROR;
535 480
536 base::MessageLoop::current()->PostTask( 481 base::MessageLoop::current()->PostTask(
537 FROM_HERE, 482 FROM_HERE,
538 base::Bind(&AndroidVideoDecodeAccelerator::NotifyResetDone, 483 base::Bind(&AndroidVideoDecodeAcceleratorBase::NotifyResetDone,
539 weak_this_factory_.GetWeakPtr())); 484 weak_this_factory_.GetWeakPtr()));
540 } 485 }
541 486
542 void AndroidVideoDecodeAccelerator::Destroy() { 487 void AndroidVideoDecodeAcceleratorBase::Destroy() {
543 DCHECK(thread_checker_.CalledOnValidThread()); 488 DCHECK(thread_checker_.CalledOnValidThread());
544 489
545 weak_this_factory_.InvalidateWeakPtrs(); 490 weak_this_factory_.InvalidateWeakPtrs();
546 if (media_codec_) { 491 if (media_codec_) {
547 io_timer_.Stop(); 492 io_timer_.Stop();
548 media_codec_->Stop(); 493 media_codec_->Stop();
549 } 494 }
550 if (surface_texture_id_) 495 if (surface_texture_id_)
551 glDeleteTextures(1, &surface_texture_id_); 496 glDeleteTextures(1, &surface_texture_id_);
552 if (copier_)
553 copier_->Destroy();
554 delete this; 497 delete this;
555 } 498 }
556 499
557 bool AndroidVideoDecodeAccelerator::CanDecodeOnIOThread() { 500 bool AndroidVideoDecodeAcceleratorBase::CanDecodeOnIOThread() {
558 return false; 501 return false;
559 } 502 }
560 503
561 void AndroidVideoDecodeAccelerator::RequestPictureBuffers() { 504 media::VideoDecodeAccelerator::Client*
562 client_->ProvidePictureBuffers(kNumPictureBuffers, size_, GL_TEXTURE_2D); 505 AndroidVideoDecodeAcceleratorBase::GetClient() const {
506 return client_;
563 } 507 }
564 508
565 void AndroidVideoDecodeAccelerator::NotifyPictureReady( 509 const gfx::Size& AndroidVideoDecodeAcceleratorBase::GetSize() const {
510 return size_;
511 }
512
513 const base::ThreadChecker&
514 AndroidVideoDecodeAcceleratorBase::ThreadChecker() const {
515 return thread_checker_;
516 }
517
518 gfx::SurfaceTexture*
519 AndroidVideoDecodeAcceleratorBase::GetSurfaceTexture() const {
520 return surface_texture_.get();
521 }
522
523 uint32 AndroidVideoDecodeAcceleratorBase::GetSurfaceTextureId() const {
524 return surface_texture_id_;
525 }
526
527 gpu::gles2::GLES2Decoder*
528 AndroidVideoDecodeAcceleratorBase::GetGlDecoder() const {
529 return gl_decoder_.get();
530 }
531
532 media::VideoCodecBridge* AndroidVideoDecodeAcceleratorBase::GetMediaCodec() {
533 return media_codec_.get();
534 }
535
536 void AndroidVideoDecodeAcceleratorBase::PostError(
537 const ::tracked_objects::Location& from_here,
538 media::VideoDecodeAccelerator::Error error) {
539 base::MessageLoop::current()->PostTask(from_here,
540 base::Bind(&AndroidVideoDecodeAcceleratorBase::NotifyError,
541 weak_this_factory_.GetWeakPtr(),
542 error));
543 state_ = ERROR;
544 }
545
546 void AndroidVideoDecodeAcceleratorBase::NotifyPictureReady(
566 const media::Picture& picture) { 547 const media::Picture& picture) {
567 client_->PictureReady(picture); 548 client_->PictureReady(picture);
568 } 549 }
569 550
570 void AndroidVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer( 551 void AndroidVideoDecodeAcceleratorBase::NotifyEndOfBitstreamBuffer(
571 int input_buffer_id) { 552 int input_buffer_id) {
572 client_->NotifyEndOfBitstreamBuffer(input_buffer_id); 553 client_->NotifyEndOfBitstreamBuffer(input_buffer_id);
573 } 554 }
574 555
575 void AndroidVideoDecodeAccelerator::NotifyFlushDone() { 556 void AndroidVideoDecodeAcceleratorBase::NotifyFlushDone() {
576 client_->NotifyFlushDone(); 557 client_->NotifyFlushDone();
577 } 558 }
578 559
579 void AndroidVideoDecodeAccelerator::NotifyResetDone() { 560 void AndroidVideoDecodeAcceleratorBase::NotifyResetDone() {
580 client_->NotifyResetDone(); 561 client_->NotifyResetDone();
581 } 562 }
582 563
583 void AndroidVideoDecodeAccelerator::NotifyError( 564 void AndroidVideoDecodeAcceleratorBase::NotifyError(
584 media::VideoDecodeAccelerator::Error error) { 565 media::VideoDecodeAccelerator::Error error) {
585 client_->NotifyError(error); 566 client_->NotifyError(error);
586 } 567 }
587 568
588 // static 569 // static
589 media::VideoDecodeAccelerator::SupportedProfiles 570 media::VideoDecodeAccelerator::SupportedProfiles
590 AndroidVideoDecodeAccelerator::GetSupportedProfiles() { 571 AndroidVideoDecodeAcceleratorBase::GetSupportedProfiles() {
591 SupportedProfiles profiles; 572 SupportedProfiles profiles;
592 573
593 if (!media::VideoCodecBridge::IsKnownUnaccelerated( 574 if (!media::VideoCodecBridge::IsKnownUnaccelerated(
594 media::kCodecVP8, media::MEDIA_CODEC_DECODER)) { 575 media::kCodecVP8, media::MEDIA_CODEC_DECODER)) {
595 SupportedProfile profile; 576 SupportedProfile profile;
596 profile.profile = media::VP8PROFILE_ANY; 577 profile.profile = media::VP8PROFILE_ANY;
597 profile.min_resolution.SetSize(0, 0); 578 profile.min_resolution.SetSize(0, 0);
598 profile.max_resolution.SetSize(1920, 1088); 579 profile.max_resolution.SetSize(1920, 1088);
599 profiles.push_back(profile); 580 profiles.push_back(profile);
600 } 581 }
(...skipping 17 matching lines...) Expand all
618 // software fallback for H264 on Android anyway. 599 // software fallback for H264 on Android anyway.
619 profile.max_resolution.SetSize(3840, 2160); 600 profile.max_resolution.SetSize(3840, 2160);
620 profiles.push_back(profile); 601 profiles.push_back(profile);
621 } 602 }
622 #endif 603 #endif
623 604
624 return profiles; 605 return profiles;
625 } 606 }
626 607
627 } // namespace content 608 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698