OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 <dlfcn.h> | |
6 #include <errno.h> | |
7 #include <fcntl.h> | |
8 #include <linux/videodev2.h> | |
9 #include <poll.h> | |
10 #include <string.h> | |
11 #include <sys/eventfd.h> | |
12 #include <sys/ioctl.h> | |
13 #include <sys/mman.h> | |
14 | |
15 #include "base/bind.h" | |
16 #include "base/command_line.h" | |
17 #include "base/macros.h" | |
18 #include "base/message_loop/message_loop.h" | |
19 #include "base/numerics/safe_conversions.h" | |
20 #include "base/thread_task_runner_handle.h" | |
21 #include "base/trace_event/trace_event.h" | |
22 #include "build/build_config.h" | |
23 #include "content/common/gpu/media/shared_memory_region.h" | |
24 #include "content/common/gpu/media/v4l2_video_decode_accelerator.h" | |
25 #include "media/base/media_switches.h" | |
26 #include "media/filters/h264_parser.h" | |
27 #include "ui/gfx/geometry/rect.h" | |
28 #include "ui/gl/gl_context.h" | |
29 #include "ui/gl/scoped_binders.h" | |
30 | |
31 #define NOTIFY_ERROR(x) \ | |
32 do { \ | |
33 LOG(ERROR) << "Setting error state:" << x; \ | |
34 SetErrorState(x); \ | |
35 } while (0) | |
36 | |
37 #define IOCTL_OR_ERROR_RETURN_VALUE(type, arg, value, type_str) \ | |
38 do { \ | |
39 if (device_->Ioctl(type, arg) != 0) { \ | |
40 PLOG(ERROR) << __func__ << "(): ioctl() failed: " << type_str; \ | |
41 NOTIFY_ERROR(PLATFORM_FAILURE); \ | |
42 return value; \ | |
43 } \ | |
44 } while (0) | |
45 | |
46 #define IOCTL_OR_ERROR_RETURN(type, arg) \ | |
47 IOCTL_OR_ERROR_RETURN_VALUE(type, arg, ((void)0), #type) | |
48 | |
49 #define IOCTL_OR_ERROR_RETURN_FALSE(type, arg) \ | |
50 IOCTL_OR_ERROR_RETURN_VALUE(type, arg, false, #type) | |
51 | |
52 #define IOCTL_OR_LOG_ERROR(type, arg) \ | |
53 do { \ | |
54 if (device_->Ioctl(type, arg) != 0) \ | |
55 PLOG(ERROR) << __func__ << "(): ioctl() failed: " << #type; \ | |
56 } while (0) | |
57 | |
58 namespace content { | |
59 | |
60 // static | |
61 const uint32_t V4L2VideoDecodeAccelerator::supported_input_fourccs_[] = { | |
62 V4L2_PIX_FMT_H264, V4L2_PIX_FMT_VP8, V4L2_PIX_FMT_VP9, | |
63 }; | |
64 | |
65 struct V4L2VideoDecodeAccelerator::BitstreamBufferRef { | |
66 BitstreamBufferRef( | |
67 base::WeakPtr<Client>& client, | |
68 scoped_refptr<base::SingleThreadTaskRunner>& client_task_runner, | |
69 std::unique_ptr<SharedMemoryRegion> shm, | |
70 int32_t input_id); | |
71 ~BitstreamBufferRef(); | |
72 const base::WeakPtr<Client> client; | |
73 const scoped_refptr<base::SingleThreadTaskRunner> client_task_runner; | |
74 const std::unique_ptr<SharedMemoryRegion> shm; | |
75 size_t bytes_used; | |
76 const int32_t input_id; | |
77 }; | |
78 | |
79 struct V4L2VideoDecodeAccelerator::EGLSyncKHRRef { | |
80 EGLSyncKHRRef(EGLDisplay egl_display, EGLSyncKHR egl_sync); | |
81 ~EGLSyncKHRRef(); | |
82 EGLDisplay const egl_display; | |
83 EGLSyncKHR egl_sync; | |
84 }; | |
85 | |
86 struct V4L2VideoDecodeAccelerator::PictureRecord { | |
87 PictureRecord(bool cleared, const media::Picture& picture); | |
88 ~PictureRecord(); | |
89 bool cleared; // Whether the texture is cleared and safe to render from. | |
90 media::Picture picture; // The decoded picture. | |
91 }; | |
92 | |
93 V4L2VideoDecodeAccelerator::BitstreamBufferRef::BitstreamBufferRef( | |
94 base::WeakPtr<Client>& client, | |
95 scoped_refptr<base::SingleThreadTaskRunner>& client_task_runner, | |
96 std::unique_ptr<SharedMemoryRegion> shm, | |
97 int32_t input_id) | |
98 : client(client), | |
99 client_task_runner(client_task_runner), | |
100 shm(std::move(shm)), | |
101 bytes_used(0), | |
102 input_id(input_id) {} | |
103 | |
104 V4L2VideoDecodeAccelerator::BitstreamBufferRef::~BitstreamBufferRef() { | |
105 if (input_id >= 0) { | |
106 client_task_runner->PostTask( | |
107 FROM_HERE, | |
108 base::Bind(&Client::NotifyEndOfBitstreamBuffer, client, input_id)); | |
109 } | |
110 } | |
111 | |
112 V4L2VideoDecodeAccelerator::EGLSyncKHRRef::EGLSyncKHRRef( | |
113 EGLDisplay egl_display, EGLSyncKHR egl_sync) | |
114 : egl_display(egl_display), | |
115 egl_sync(egl_sync) { | |
116 } | |
117 | |
118 V4L2VideoDecodeAccelerator::EGLSyncKHRRef::~EGLSyncKHRRef() { | |
119 // We don't check for eglDestroySyncKHR failures, because if we get here | |
120 // with a valid sync object, something went wrong and we are getting | |
121 // destroyed anyway. | |
122 if (egl_sync != EGL_NO_SYNC_KHR) | |
123 eglDestroySyncKHR(egl_display, egl_sync); | |
124 } | |
125 | |
126 V4L2VideoDecodeAccelerator::InputRecord::InputRecord() | |
127 : at_device(false), | |
128 address(NULL), | |
129 length(0), | |
130 bytes_used(0), | |
131 input_id(-1) { | |
132 } | |
133 | |
134 V4L2VideoDecodeAccelerator::InputRecord::~InputRecord() { | |
135 } | |
136 | |
137 V4L2VideoDecodeAccelerator::OutputRecord::OutputRecord() | |
138 : at_device(false), | |
139 at_client(false), | |
140 egl_image(EGL_NO_IMAGE_KHR), | |
141 egl_sync(EGL_NO_SYNC_KHR), | |
142 picture_id(-1), | |
143 cleared(false) { | |
144 } | |
145 | |
146 V4L2VideoDecodeAccelerator::OutputRecord::~OutputRecord() {} | |
147 | |
148 V4L2VideoDecodeAccelerator::PictureRecord::PictureRecord( | |
149 bool cleared, | |
150 const media::Picture& picture) | |
151 : cleared(cleared), picture(picture) {} | |
152 | |
153 V4L2VideoDecodeAccelerator::PictureRecord::~PictureRecord() {} | |
154 | |
155 V4L2VideoDecodeAccelerator::V4L2VideoDecodeAccelerator( | |
156 EGLDisplay egl_display, | |
157 const GetGLContextCallback& get_gl_context_cb, | |
158 const MakeGLContextCurrentCallback& make_context_current_cb, | |
159 const scoped_refptr<V4L2Device>& device) | |
160 : child_task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
161 decoder_thread_("V4L2DecoderThread"), | |
162 decoder_state_(kUninitialized), | |
163 device_(device), | |
164 decoder_delay_bitstream_buffer_id_(-1), | |
165 decoder_current_input_buffer_(-1), | |
166 decoder_decode_buffer_tasks_scheduled_(0), | |
167 decoder_frames_at_client_(0), | |
168 decoder_flushing_(false), | |
169 resolution_change_reset_pending_(false), | |
170 decoder_partial_frame_pending_(false), | |
171 input_streamon_(false), | |
172 input_buffer_queued_count_(0), | |
173 output_streamon_(false), | |
174 output_buffer_queued_count_(0), | |
175 output_dpb_size_(0), | |
176 output_planes_count_(0), | |
177 picture_clearing_count_(0), | |
178 pictures_assigned_(false, false), | |
179 device_poll_thread_("V4L2DevicePollThread"), | |
180 egl_display_(egl_display), | |
181 get_gl_context_cb_(get_gl_context_cb), | |
182 make_context_current_cb_(make_context_current_cb), | |
183 video_profile_(media::VIDEO_CODEC_PROFILE_UNKNOWN), | |
184 output_format_fourcc_(0), | |
185 weak_this_factory_(this) { | |
186 weak_this_ = weak_this_factory_.GetWeakPtr(); | |
187 } | |
188 | |
189 V4L2VideoDecodeAccelerator::~V4L2VideoDecodeAccelerator() { | |
190 DCHECK(!decoder_thread_.IsRunning()); | |
191 DCHECK(!device_poll_thread_.IsRunning()); | |
192 | |
193 DestroyInputBuffers(); | |
194 DestroyOutputBuffers(); | |
195 | |
196 // These maps have members that should be manually destroyed, e.g. file | |
197 // descriptors, mmap() segments, etc. | |
198 DCHECK(input_buffer_map_.empty()); | |
199 DCHECK(output_buffer_map_.empty()); | |
200 } | |
201 | |
202 bool V4L2VideoDecodeAccelerator::Initialize(const Config& config, | |
203 Client* client) { | |
204 DVLOG(3) << "Initialize()"; | |
205 DCHECK(child_task_runner_->BelongsToCurrentThread()); | |
206 DCHECK_EQ(decoder_state_, kUninitialized); | |
207 | |
208 if (get_gl_context_cb_.is_null() || make_context_current_cb_.is_null()) { | |
209 NOTREACHED() << "GL callbacks are required for this VDA"; | |
210 return false; | |
211 } | |
212 | |
213 if (config.is_encrypted) { | |
214 NOTREACHED() << "Encrypted streams are not supported for this VDA"; | |
215 return false; | |
216 } | |
217 | |
218 if (!device_->SupportsDecodeProfileForV4L2PixelFormats( | |
219 config.profile, arraysize(supported_input_fourccs_), | |
220 supported_input_fourccs_)) { | |
221 DVLOG(1) << "Initialize(): unsupported profile=" << config.profile; | |
222 return false; | |
223 } | |
224 | |
225 client_ptr_factory_.reset(new base::WeakPtrFactory<Client>(client)); | |
226 client_ = client_ptr_factory_->GetWeakPtr(); | |
227 // If we haven't been set up to decode on separate thread via | |
228 // TryToSetupDecodeOnSeparateThread(), use the main thread/client for | |
229 // decode tasks. | |
230 if (!decode_task_runner_) { | |
231 decode_task_runner_ = child_task_runner_; | |
232 DCHECK(!decode_client_); | |
233 decode_client_ = client_; | |
234 } | |
235 | |
236 video_profile_ = config.profile; | |
237 | |
238 if (egl_display_ == EGL_NO_DISPLAY) { | |
239 LOG(ERROR) << "Initialize(): could not get EGLDisplay"; | |
240 return false; | |
241 } | |
242 | |
243 // We need the context to be initialized to query extensions. | |
244 if (!make_context_current_cb_.Run()) { | |
245 LOG(ERROR) << "Initialize(): could not make context current"; | |
246 return false; | |
247 } | |
248 | |
249 // TODO(posciak): crbug.com/450898. | |
250 #if defined(ARCH_CPU_ARMEL) | |
251 if (!gfx::g_driver_egl.ext.b_EGL_KHR_fence_sync) { | |
252 LOG(ERROR) << "Initialize(): context does not have EGL_KHR_fence_sync"; | |
253 return false; | |
254 } | |
255 #endif | |
256 | |
257 // Capabilities check. | |
258 struct v4l2_capability caps; | |
259 const __u32 kCapsRequired = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING; | |
260 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QUERYCAP, &caps); | |
261 if ((caps.capabilities & kCapsRequired) != kCapsRequired) { | |
262 LOG(ERROR) << "Initialize(): ioctl() failed: VIDIOC_QUERYCAP" | |
263 ", caps check failed: 0x" << std::hex << caps.capabilities; | |
264 return false; | |
265 } | |
266 | |
267 if (!SetupFormats()) | |
268 return false; | |
269 | |
270 // Subscribe to the resolution change event. | |
271 struct v4l2_event_subscription sub; | |
272 memset(&sub, 0, sizeof(sub)); | |
273 sub.type = V4L2_EVENT_SOURCE_CHANGE; | |
274 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_SUBSCRIBE_EVENT, &sub); | |
275 | |
276 if (video_profile_ >= media::H264PROFILE_MIN && | |
277 video_profile_ <= media::H264PROFILE_MAX) { | |
278 decoder_h264_parser_.reset(new media::H264Parser()); | |
279 } | |
280 | |
281 if (!CreateInputBuffers()) | |
282 return false; | |
283 | |
284 if (!decoder_thread_.Start()) { | |
285 LOG(ERROR) << "Initialize(): decoder thread failed to start"; | |
286 return false; | |
287 } | |
288 | |
289 decoder_state_ = kInitialized; | |
290 | |
291 // StartDevicePoll will NOTIFY_ERROR on failure, so IgnoreResult is fine here. | |
292 decoder_thread_.message_loop()->PostTask( | |
293 FROM_HERE, | |
294 base::Bind( | |
295 base::IgnoreResult(&V4L2VideoDecodeAccelerator::StartDevicePoll), | |
296 base::Unretained(this))); | |
297 | |
298 return true; | |
299 } | |
300 | |
301 void V4L2VideoDecodeAccelerator::Decode( | |
302 const media::BitstreamBuffer& bitstream_buffer) { | |
303 DVLOG(1) << "Decode(): input_id=" << bitstream_buffer.id() | |
304 << ", size=" << bitstream_buffer.size(); | |
305 DCHECK(decode_task_runner_->BelongsToCurrentThread()); | |
306 | |
307 if (bitstream_buffer.id() < 0) { | |
308 LOG(ERROR) << "Invalid bitstream_buffer, id: " << bitstream_buffer.id(); | |
309 if (base::SharedMemory::IsHandleValid(bitstream_buffer.handle())) | |
310 base::SharedMemory::CloseHandle(bitstream_buffer.handle()); | |
311 NOTIFY_ERROR(INVALID_ARGUMENT); | |
312 return; | |
313 } | |
314 | |
315 // DecodeTask() will take care of running a DecodeBufferTask(). | |
316 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
317 &V4L2VideoDecodeAccelerator::DecodeTask, base::Unretained(this), | |
318 bitstream_buffer)); | |
319 } | |
320 | |
321 void V4L2VideoDecodeAccelerator::AssignPictureBuffers( | |
322 const std::vector<media::PictureBuffer>& buffers) { | |
323 DVLOG(3) << "AssignPictureBuffers(): buffer_count=" << buffers.size(); | |
324 DCHECK(child_task_runner_->BelongsToCurrentThread()); | |
325 | |
326 const uint32_t req_buffer_count = | |
327 output_dpb_size_ + kDpbOutputBufferExtraCount; | |
328 | |
329 if (buffers.size() < req_buffer_count) { | |
330 LOG(ERROR) << "AssignPictureBuffers(): Failed to provide requested picture" | |
331 " buffers. (Got " << buffers.size() | |
332 << ", requested " << req_buffer_count << ")"; | |
333 NOTIFY_ERROR(INVALID_ARGUMENT); | |
334 return; | |
335 } | |
336 | |
337 gfx::GLContext* gl_context = get_gl_context_cb_.Run(); | |
338 if (!gl_context || !make_context_current_cb_.Run()) { | |
339 LOG(ERROR) << "AssignPictureBuffers(): could not make context current"; | |
340 NOTIFY_ERROR(PLATFORM_FAILURE); | |
341 return; | |
342 } | |
343 | |
344 gfx::ScopedTextureBinder bind_restore(GL_TEXTURE_EXTERNAL_OES, 0); | |
345 | |
346 // It's safe to manipulate all the buffer state here, because the decoder | |
347 // thread is waiting on pictures_assigned_. | |
348 | |
349 // Allocate the output buffers. | |
350 struct v4l2_requestbuffers reqbufs; | |
351 memset(&reqbufs, 0, sizeof(reqbufs)); | |
352 reqbufs.count = buffers.size(); | |
353 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
354 reqbufs.memory = V4L2_MEMORY_MMAP; | |
355 IOCTL_OR_ERROR_RETURN(VIDIOC_REQBUFS, &reqbufs); | |
356 | |
357 if (reqbufs.count != buffers.size()) { | |
358 DLOG(ERROR) << "Could not allocate enough output buffers"; | |
359 NOTIFY_ERROR(PLATFORM_FAILURE); | |
360 return; | |
361 } | |
362 | |
363 output_buffer_map_.resize(buffers.size()); | |
364 | |
365 DCHECK(free_output_buffers_.empty()); | |
366 for (size_t i = 0; i < output_buffer_map_.size(); ++i) { | |
367 DCHECK(buffers[i].size() == coded_size_); | |
368 | |
369 OutputRecord& output_record = output_buffer_map_[i]; | |
370 DCHECK(!output_record.at_device); | |
371 DCHECK(!output_record.at_client); | |
372 DCHECK_EQ(output_record.egl_image, EGL_NO_IMAGE_KHR); | |
373 DCHECK_EQ(output_record.egl_sync, EGL_NO_SYNC_KHR); | |
374 DCHECK_EQ(output_record.picture_id, -1); | |
375 DCHECK_EQ(output_record.cleared, false); | |
376 DCHECK_LE(1u, buffers[i].texture_ids().size()); | |
377 | |
378 EGLImageKHR egl_image = device_->CreateEGLImage( | |
379 egl_display_, gl_context->GetHandle(), buffers[i].texture_ids()[0], | |
380 coded_size_, i, output_format_fourcc_, output_planes_count_); | |
381 if (egl_image == EGL_NO_IMAGE_KHR) { | |
382 LOG(ERROR) << "AssignPictureBuffers(): could not create EGLImageKHR"; | |
383 // Ownership of EGLImages allocated in previous iterations of this loop | |
384 // has been transferred to output_buffer_map_. After we error-out here | |
385 // the destructor will handle their cleanup. | |
386 NOTIFY_ERROR(PLATFORM_FAILURE); | |
387 return; | |
388 } | |
389 | |
390 output_record.egl_image = egl_image; | |
391 output_record.picture_id = buffers[i].id(); | |
392 free_output_buffers_.push(i); | |
393 DVLOG(3) << "AssignPictureBuffers(): buffer[" << i | |
394 << "]: picture_id=" << output_record.picture_id; | |
395 } | |
396 | |
397 pictures_assigned_.Signal(); | |
398 } | |
399 | |
400 void V4L2VideoDecodeAccelerator::ReusePictureBuffer(int32_t picture_buffer_id) { | |
401 DVLOG(3) << "ReusePictureBuffer(): picture_buffer_id=" << picture_buffer_id; | |
402 // Must be run on child thread, as we'll insert a sync in the EGL context. | |
403 DCHECK(child_task_runner_->BelongsToCurrentThread()); | |
404 | |
405 if (!make_context_current_cb_.Run()) { | |
406 LOG(ERROR) << "ReusePictureBuffer(): could not make context current"; | |
407 NOTIFY_ERROR(PLATFORM_FAILURE); | |
408 return; | |
409 } | |
410 | |
411 EGLSyncKHR egl_sync = EGL_NO_SYNC_KHR; | |
412 // TODO(posciak): crbug.com/450898. | |
413 #if defined(ARCH_CPU_ARMEL) | |
414 egl_sync = eglCreateSyncKHR(egl_display_, EGL_SYNC_FENCE_KHR, NULL); | |
415 if (egl_sync == EGL_NO_SYNC_KHR) { | |
416 LOG(ERROR) << "ReusePictureBuffer(): eglCreateSyncKHR() failed"; | |
417 NOTIFY_ERROR(PLATFORM_FAILURE); | |
418 return; | |
419 } | |
420 #endif | |
421 | |
422 std::unique_ptr<EGLSyncKHRRef> egl_sync_ref( | |
423 new EGLSyncKHRRef(egl_display_, egl_sync)); | |
424 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
425 &V4L2VideoDecodeAccelerator::ReusePictureBufferTask, | |
426 base::Unretained(this), picture_buffer_id, base::Passed(&egl_sync_ref))); | |
427 } | |
428 | |
429 void V4L2VideoDecodeAccelerator::Flush() { | |
430 DVLOG(3) << "Flush()"; | |
431 DCHECK(child_task_runner_->BelongsToCurrentThread()); | |
432 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
433 &V4L2VideoDecodeAccelerator::FlushTask, base::Unretained(this))); | |
434 } | |
435 | |
436 void V4L2VideoDecodeAccelerator::Reset() { | |
437 DVLOG(3) << "Reset()"; | |
438 DCHECK(child_task_runner_->BelongsToCurrentThread()); | |
439 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
440 &V4L2VideoDecodeAccelerator::ResetTask, base::Unretained(this))); | |
441 } | |
442 | |
443 void V4L2VideoDecodeAccelerator::Destroy() { | |
444 DVLOG(3) << "Destroy()"; | |
445 DCHECK(child_task_runner_->BelongsToCurrentThread()); | |
446 | |
447 // We're destroying; cancel all callbacks. | |
448 client_ptr_factory_.reset(); | |
449 weak_this_factory_.InvalidateWeakPtrs(); | |
450 | |
451 // If the decoder thread is running, destroy using posted task. | |
452 if (decoder_thread_.IsRunning()) { | |
453 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
454 &V4L2VideoDecodeAccelerator::DestroyTask, base::Unretained(this))); | |
455 pictures_assigned_.Signal(); | |
456 // DestroyTask() will cause the decoder_thread_ to flush all tasks. | |
457 decoder_thread_.Stop(); | |
458 } else { | |
459 // Otherwise, call the destroy task directly. | |
460 DestroyTask(); | |
461 } | |
462 | |
463 delete this; | |
464 } | |
465 | |
466 bool V4L2VideoDecodeAccelerator::TryToSetupDecodeOnSeparateThread( | |
467 const base::WeakPtr<Client>& decode_client, | |
468 const scoped_refptr<base::SingleThreadTaskRunner>& decode_task_runner) { | |
469 decode_client_ = decode_client_; | |
470 decode_task_runner_ = decode_task_runner; | |
471 return true; | |
472 } | |
473 | |
474 // static | |
475 media::VideoDecodeAccelerator::SupportedProfiles | |
476 V4L2VideoDecodeAccelerator::GetSupportedProfiles() { | |
477 scoped_refptr<V4L2Device> device = V4L2Device::Create(V4L2Device::kDecoder); | |
478 if (!device) | |
479 return SupportedProfiles(); | |
480 | |
481 return device->GetSupportedDecodeProfiles(arraysize(supported_input_fourccs_), | |
482 supported_input_fourccs_); | |
483 } | |
484 | |
485 void V4L2VideoDecodeAccelerator::DecodeTask( | |
486 const media::BitstreamBuffer& bitstream_buffer) { | |
487 DVLOG(3) << "DecodeTask(): input_id=" << bitstream_buffer.id(); | |
488 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
489 DCHECK_NE(decoder_state_, kUninitialized); | |
490 TRACE_EVENT1("Video Decoder", "V4L2VDA::DecodeTask", "input_id", | |
491 bitstream_buffer.id()); | |
492 | |
493 std::unique_ptr<BitstreamBufferRef> bitstream_record(new BitstreamBufferRef( | |
494 decode_client_, decode_task_runner_, | |
495 std::unique_ptr<SharedMemoryRegion>( | |
496 new SharedMemoryRegion(bitstream_buffer, true)), | |
497 bitstream_buffer.id())); | |
498 if (!bitstream_record->shm->Map()) { | |
499 LOG(ERROR) << "Decode(): could not map bitstream_buffer"; | |
500 NOTIFY_ERROR(UNREADABLE_INPUT); | |
501 return; | |
502 } | |
503 DVLOG(3) << "DecodeTask(): mapped at=" << bitstream_record->shm->memory(); | |
504 | |
505 if (decoder_state_ == kResetting || decoder_flushing_) { | |
506 // In the case that we're resetting or flushing, we need to delay decoding | |
507 // the BitstreamBuffers that come after the Reset() or Flush() call. When | |
508 // we're here, we know that this DecodeTask() was scheduled by a Decode() | |
509 // call that came after (in the client thread) the Reset() or Flush() call; | |
510 // thus set up the delay if necessary. | |
511 if (decoder_delay_bitstream_buffer_id_ == -1) | |
512 decoder_delay_bitstream_buffer_id_ = bitstream_record->input_id; | |
513 } else if (decoder_state_ == kError) { | |
514 DVLOG(2) << "DecodeTask(): early out: kError state"; | |
515 return; | |
516 } | |
517 | |
518 decoder_input_queue_.push( | |
519 linked_ptr<BitstreamBufferRef>(bitstream_record.release())); | |
520 decoder_decode_buffer_tasks_scheduled_++; | |
521 DecodeBufferTask(); | |
522 } | |
523 | |
524 void V4L2VideoDecodeAccelerator::DecodeBufferTask() { | |
525 DVLOG(3) << "DecodeBufferTask()"; | |
526 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
527 DCHECK_NE(decoder_state_, kUninitialized); | |
528 TRACE_EVENT0("Video Decoder", "V4L2VDA::DecodeBufferTask"); | |
529 | |
530 decoder_decode_buffer_tasks_scheduled_--; | |
531 | |
532 if (decoder_state_ == kResetting) { | |
533 DVLOG(2) << "DecodeBufferTask(): early out: kResetting state"; | |
534 return; | |
535 } else if (decoder_state_ == kError) { | |
536 DVLOG(2) << "DecodeBufferTask(): early out: kError state"; | |
537 return; | |
538 } else if (decoder_state_ == kChangingResolution) { | |
539 DVLOG(2) << "DecodeBufferTask(): early out: resolution change pending"; | |
540 return; | |
541 } | |
542 | |
543 if (decoder_current_bitstream_buffer_ == NULL) { | |
544 if (decoder_input_queue_.empty()) { | |
545 // We're waiting for a new buffer -- exit without scheduling a new task. | |
546 return; | |
547 } | |
548 linked_ptr<BitstreamBufferRef>& buffer_ref = decoder_input_queue_.front(); | |
549 if (decoder_delay_bitstream_buffer_id_ == buffer_ref->input_id) { | |
550 // We're asked to delay decoding on this and subsequent buffers. | |
551 return; | |
552 } | |
553 | |
554 // Setup to use the next buffer. | |
555 decoder_current_bitstream_buffer_.reset(buffer_ref.release()); | |
556 decoder_input_queue_.pop(); | |
557 const auto& shm = decoder_current_bitstream_buffer_->shm; | |
558 if (shm) { | |
559 DVLOG(3) << "DecodeBufferTask(): reading input_id=" | |
560 << decoder_current_bitstream_buffer_->input_id | |
561 << ", addr=" << shm->memory() << ", size=" << shm->size(); | |
562 } else { | |
563 DCHECK_EQ(decoder_current_bitstream_buffer_->input_id, kFlushBufferId); | |
564 DVLOG(3) << "DecodeBufferTask(): reading input_id=kFlushBufferId"; | |
565 } | |
566 } | |
567 bool schedule_task = false; | |
568 size_t decoded_size = 0; | |
569 const auto& shm = decoder_current_bitstream_buffer_->shm; | |
570 if (!shm) { | |
571 // This is a dummy buffer, queued to flush the pipe. Flush. | |
572 DCHECK_EQ(decoder_current_bitstream_buffer_->input_id, kFlushBufferId); | |
573 // Enqueue a buffer guaranteed to be empty. To do that, we flush the | |
574 // current input, enqueue no data to the next frame, then flush that down. | |
575 schedule_task = true; | |
576 if (decoder_current_input_buffer_ != -1 && | |
577 input_buffer_map_[decoder_current_input_buffer_].input_id != | |
578 kFlushBufferId) | |
579 schedule_task = FlushInputFrame(); | |
580 | |
581 if (schedule_task && AppendToInputFrame(NULL, 0) && FlushInputFrame()) { | |
582 DVLOG(2) << "DecodeBufferTask(): enqueued flush buffer"; | |
583 decoder_partial_frame_pending_ = false; | |
584 schedule_task = true; | |
585 } else { | |
586 // If we failed to enqueue the empty buffer (due to pipeline | |
587 // backpressure), don't advance the bitstream buffer queue, and don't | |
588 // schedule the next task. This bitstream buffer queue entry will get | |
589 // reprocessed when the pipeline frees up. | |
590 schedule_task = false; | |
591 } | |
592 } else if (shm->size() == 0) { | |
593 // This is a buffer queued from the client that has zero size. Skip. | |
594 schedule_task = true; | |
595 } else { | |
596 // This is a buffer queued from the client, with actual contents. Decode. | |
597 const uint8_t* const data = | |
598 reinterpret_cast<const uint8_t*>(shm->memory()) + | |
599 decoder_current_bitstream_buffer_->bytes_used; | |
600 const size_t data_size = | |
601 shm->size() - decoder_current_bitstream_buffer_->bytes_used; | |
602 if (!AdvanceFrameFragment(data, data_size, &decoded_size)) { | |
603 NOTIFY_ERROR(UNREADABLE_INPUT); | |
604 return; | |
605 } | |
606 // AdvanceFrameFragment should not return a size larger than the buffer | |
607 // size, even on invalid data. | |
608 CHECK_LE(decoded_size, data_size); | |
609 | |
610 switch (decoder_state_) { | |
611 case kInitialized: | |
612 case kAfterReset: | |
613 schedule_task = DecodeBufferInitial(data, decoded_size, &decoded_size); | |
614 break; | |
615 case kDecoding: | |
616 schedule_task = DecodeBufferContinue(data, decoded_size); | |
617 break; | |
618 default: | |
619 NOTIFY_ERROR(ILLEGAL_STATE); | |
620 return; | |
621 } | |
622 } | |
623 if (decoder_state_ == kError) { | |
624 // Failed during decode. | |
625 return; | |
626 } | |
627 | |
628 if (schedule_task) { | |
629 decoder_current_bitstream_buffer_->bytes_used += decoded_size; | |
630 if ((shm ? shm->size() : 0) == | |
631 decoder_current_bitstream_buffer_->bytes_used) { | |
632 // Our current bitstream buffer is done; return it. | |
633 int32_t input_id = decoder_current_bitstream_buffer_->input_id; | |
634 DVLOG(3) << "DecodeBufferTask(): finished input_id=" << input_id; | |
635 // BitstreamBufferRef destructor calls NotifyEndOfBitstreamBuffer(). | |
636 decoder_current_bitstream_buffer_.reset(); | |
637 } | |
638 ScheduleDecodeBufferTaskIfNeeded(); | |
639 } | |
640 } | |
641 | |
642 bool V4L2VideoDecodeAccelerator::AdvanceFrameFragment(const uint8_t* data, | |
643 size_t size, | |
644 size_t* endpos) { | |
645 if (video_profile_ >= media::H264PROFILE_MIN && | |
646 video_profile_ <= media::H264PROFILE_MAX) { | |
647 // For H264, we need to feed HW one frame at a time. This is going to take | |
648 // some parsing of our input stream. | |
649 decoder_h264_parser_->SetStream(data, size); | |
650 media::H264NALU nalu; | |
651 media::H264Parser::Result result; | |
652 *endpos = 0; | |
653 | |
654 // Keep on peeking the next NALs while they don't indicate a frame | |
655 // boundary. | |
656 for (;;) { | |
657 bool end_of_frame = false; | |
658 result = decoder_h264_parser_->AdvanceToNextNALU(&nalu); | |
659 if (result == media::H264Parser::kInvalidStream || | |
660 result == media::H264Parser::kUnsupportedStream) | |
661 return false; | |
662 if (result == media::H264Parser::kEOStream) { | |
663 // We've reached the end of the buffer before finding a frame boundary. | |
664 decoder_partial_frame_pending_ = true; | |
665 return true; | |
666 } | |
667 switch (nalu.nal_unit_type) { | |
668 case media::H264NALU::kNonIDRSlice: | |
669 case media::H264NALU::kIDRSlice: | |
670 if (nalu.size < 1) | |
671 return false; | |
672 // For these two, if the "first_mb_in_slice" field is zero, start a | |
673 // new frame and return. This field is Exp-Golomb coded starting on | |
674 // the eighth data bit of the NAL; a zero value is encoded with a | |
675 // leading '1' bit in the byte, which we can detect as the byte being | |
676 // (unsigned) greater than or equal to 0x80. | |
677 if (nalu.data[1] >= 0x80) { | |
678 end_of_frame = true; | |
679 break; | |
680 } | |
681 break; | |
682 case media::H264NALU::kSEIMessage: | |
683 case media::H264NALU::kSPS: | |
684 case media::H264NALU::kPPS: | |
685 case media::H264NALU::kAUD: | |
686 case media::H264NALU::kEOSeq: | |
687 case media::H264NALU::kEOStream: | |
688 case media::H264NALU::kReserved14: | |
689 case media::H264NALU::kReserved15: | |
690 case media::H264NALU::kReserved16: | |
691 case media::H264NALU::kReserved17: | |
692 case media::H264NALU::kReserved18: | |
693 // These unconditionally signal a frame boundary. | |
694 end_of_frame = true; | |
695 break; | |
696 default: | |
697 // For all others, keep going. | |
698 break; | |
699 } | |
700 if (end_of_frame) { | |
701 if (!decoder_partial_frame_pending_ && *endpos == 0) { | |
702 // The frame was previously restarted, and we haven't filled the | |
703 // current frame with any contents yet. Start the new frame here and | |
704 // continue parsing NALs. | |
705 } else { | |
706 // The frame wasn't previously restarted and/or we have contents for | |
707 // the current frame; signal the start of a new frame here: we don't | |
708 // have a partial frame anymore. | |
709 decoder_partial_frame_pending_ = false; | |
710 return true; | |
711 } | |
712 } | |
713 *endpos = (nalu.data + nalu.size) - data; | |
714 } | |
715 NOTREACHED(); | |
716 return false; | |
717 } else { | |
718 DCHECK_GE(video_profile_, media::VP8PROFILE_MIN); | |
719 DCHECK_LE(video_profile_, media::VP9PROFILE_MAX); | |
720 // For VP8/9, we can just dump the entire buffer. No fragmentation needed, | |
721 // and we never return a partial frame. | |
722 *endpos = size; | |
723 decoder_partial_frame_pending_ = false; | |
724 return true; | |
725 } | |
726 } | |
727 | |
728 void V4L2VideoDecodeAccelerator::ScheduleDecodeBufferTaskIfNeeded() { | |
729 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
730 | |
731 // If we're behind on tasks, schedule another one. | |
732 int buffers_to_decode = decoder_input_queue_.size(); | |
733 if (decoder_current_bitstream_buffer_ != NULL) | |
734 buffers_to_decode++; | |
735 if (decoder_decode_buffer_tasks_scheduled_ < buffers_to_decode) { | |
736 decoder_decode_buffer_tasks_scheduled_++; | |
737 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
738 &V4L2VideoDecodeAccelerator::DecodeBufferTask, | |
739 base::Unretained(this))); | |
740 } | |
741 } | |
742 | |
743 bool V4L2VideoDecodeAccelerator::DecodeBufferInitial( | |
744 const void* data, size_t size, size_t* endpos) { | |
745 DVLOG(3) << "DecodeBufferInitial(): data=" << data << ", size=" << size; | |
746 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
747 DCHECK_NE(decoder_state_, kUninitialized); | |
748 DCHECK_NE(decoder_state_, kDecoding); | |
749 // Initial decode. We haven't been able to get output stream format info yet. | |
750 // Get it, and start decoding. | |
751 | |
752 // Copy in and send to HW. | |
753 if (!AppendToInputFrame(data, size)) | |
754 return false; | |
755 | |
756 // If we only have a partial frame, don't flush and process yet. | |
757 if (decoder_partial_frame_pending_) | |
758 return true; | |
759 | |
760 if (!FlushInputFrame()) | |
761 return false; | |
762 | |
763 // Recycle buffers. | |
764 Dequeue(); | |
765 | |
766 // Check and see if we have format info yet. | |
767 struct v4l2_format format; | |
768 gfx::Size visible_size; | |
769 bool again = false; | |
770 if (!GetFormatInfo(&format, &visible_size, &again)) | |
771 return false; | |
772 | |
773 *endpos = size; | |
774 | |
775 if (again) { | |
776 // Need more stream to decode format, return true and schedule next buffer. | |
777 return true; | |
778 } | |
779 | |
780 // Run this initialization only on first startup. | |
781 if (decoder_state_ == kInitialized) { | |
782 DVLOG(3) << "DecodeBufferInitial(): running initialization"; | |
783 // Success! Setup our parameters. | |
784 if (!CreateBuffersForFormat(format, visible_size)) | |
785 return false; | |
786 } | |
787 | |
788 decoder_state_ = kDecoding; | |
789 ScheduleDecodeBufferTaskIfNeeded(); | |
790 return true; | |
791 } | |
792 | |
793 bool V4L2VideoDecodeAccelerator::DecodeBufferContinue( | |
794 const void* data, size_t size) { | |
795 DVLOG(3) << "DecodeBufferContinue(): data=" << data << ", size=" << size; | |
796 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
797 DCHECK_EQ(decoder_state_, kDecoding); | |
798 | |
799 // Both of these calls will set kError state if they fail. | |
800 // Only flush the frame if it's complete. | |
801 return (AppendToInputFrame(data, size) && | |
802 (decoder_partial_frame_pending_ || FlushInputFrame())); | |
803 } | |
804 | |
805 bool V4L2VideoDecodeAccelerator::AppendToInputFrame( | |
806 const void* data, size_t size) { | |
807 DVLOG(3) << "AppendToInputFrame()"; | |
808 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
809 DCHECK_NE(decoder_state_, kUninitialized); | |
810 DCHECK_NE(decoder_state_, kResetting); | |
811 DCHECK_NE(decoder_state_, kError); | |
812 // This routine can handle data == NULL and size == 0, which occurs when | |
813 // we queue an empty buffer for the purposes of flushing the pipe. | |
814 | |
815 // Flush if we're too big | |
816 if (decoder_current_input_buffer_ != -1) { | |
817 InputRecord& input_record = | |
818 input_buffer_map_[decoder_current_input_buffer_]; | |
819 if (input_record.bytes_used + size > input_record.length) { | |
820 if (!FlushInputFrame()) | |
821 return false; | |
822 decoder_current_input_buffer_ = -1; | |
823 } | |
824 } | |
825 | |
826 // Try to get an available input buffer | |
827 if (decoder_current_input_buffer_ == -1) { | |
828 if (free_input_buffers_.empty()) { | |
829 // See if we can get more free buffers from HW | |
830 Dequeue(); | |
831 if (free_input_buffers_.empty()) { | |
832 // Nope! | |
833 DVLOG(2) << "AppendToInputFrame(): stalled for input buffers"; | |
834 return false; | |
835 } | |
836 } | |
837 decoder_current_input_buffer_ = free_input_buffers_.back(); | |
838 free_input_buffers_.pop_back(); | |
839 InputRecord& input_record = | |
840 input_buffer_map_[decoder_current_input_buffer_]; | |
841 DCHECK_EQ(input_record.bytes_used, 0); | |
842 DCHECK_EQ(input_record.input_id, -1); | |
843 DCHECK(decoder_current_bitstream_buffer_ != NULL); | |
844 input_record.input_id = decoder_current_bitstream_buffer_->input_id; | |
845 } | |
846 | |
847 DCHECK(data != NULL || size == 0); | |
848 if (size == 0) { | |
849 // If we asked for an empty buffer, return now. We return only after | |
850 // getting the next input buffer, since we might actually want an empty | |
851 // input buffer for flushing purposes. | |
852 return true; | |
853 } | |
854 | |
855 // Copy in to the buffer. | |
856 InputRecord& input_record = | |
857 input_buffer_map_[decoder_current_input_buffer_]; | |
858 if (size > input_record.length - input_record.bytes_used) { | |
859 LOG(ERROR) << "AppendToInputFrame(): over-size frame, erroring"; | |
860 NOTIFY_ERROR(UNREADABLE_INPUT); | |
861 return false; | |
862 } | |
863 memcpy(reinterpret_cast<uint8_t*>(input_record.address) + | |
864 input_record.bytes_used, | |
865 data, size); | |
866 input_record.bytes_used += size; | |
867 | |
868 return true; | |
869 } | |
870 | |
871 bool V4L2VideoDecodeAccelerator::FlushInputFrame() { | |
872 DVLOG(3) << "FlushInputFrame()"; | |
873 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
874 DCHECK_NE(decoder_state_, kUninitialized); | |
875 DCHECK_NE(decoder_state_, kResetting); | |
876 DCHECK_NE(decoder_state_, kError); | |
877 | |
878 if (decoder_current_input_buffer_ == -1) | |
879 return true; | |
880 | |
881 InputRecord& input_record = | |
882 input_buffer_map_[decoder_current_input_buffer_]; | |
883 DCHECK_NE(input_record.input_id, -1); | |
884 DCHECK(input_record.input_id != kFlushBufferId || | |
885 input_record.bytes_used == 0); | |
886 // * if input_id >= 0, this input buffer was prompted by a bitstream buffer we | |
887 // got from the client. We can skip it if it is empty. | |
888 // * if input_id < 0 (should be kFlushBufferId in this case), this input | |
889 // buffer was prompted by a flush buffer, and should be queued even when | |
890 // empty. | |
891 if (input_record.input_id >= 0 && input_record.bytes_used == 0) { | |
892 input_record.input_id = -1; | |
893 free_input_buffers_.push_back(decoder_current_input_buffer_); | |
894 decoder_current_input_buffer_ = -1; | |
895 return true; | |
896 } | |
897 | |
898 // Queue it. | |
899 input_ready_queue_.push(decoder_current_input_buffer_); | |
900 decoder_current_input_buffer_ = -1; | |
901 DVLOG(3) << "FlushInputFrame(): submitting input_id=" | |
902 << input_record.input_id; | |
903 // Enqueue once since there's new available input for it. | |
904 Enqueue(); | |
905 | |
906 return (decoder_state_ != kError); | |
907 } | |
908 | |
909 void V4L2VideoDecodeAccelerator::ServiceDeviceTask(bool event_pending) { | |
910 DVLOG(3) << "ServiceDeviceTask()"; | |
911 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
912 DCHECK_NE(decoder_state_, kUninitialized); | |
913 TRACE_EVENT0("Video Decoder", "V4L2VDA::ServiceDeviceTask"); | |
914 | |
915 if (decoder_state_ == kResetting) { | |
916 DVLOG(2) << "ServiceDeviceTask(): early out: kResetting state"; | |
917 return; | |
918 } else if (decoder_state_ == kError) { | |
919 DVLOG(2) << "ServiceDeviceTask(): early out: kError state"; | |
920 return; | |
921 } else if (decoder_state_ == kChangingResolution) { | |
922 DVLOG(2) << "ServiceDeviceTask(): early out: kChangingResolution state"; | |
923 return; | |
924 } | |
925 | |
926 bool resolution_change_pending = false; | |
927 if (event_pending) | |
928 resolution_change_pending = DequeueResolutionChangeEvent(); | |
929 Dequeue(); | |
930 Enqueue(); | |
931 | |
932 // Clear the interrupt fd. | |
933 if (!device_->ClearDevicePollInterrupt()) { | |
934 NOTIFY_ERROR(PLATFORM_FAILURE); | |
935 return; | |
936 } | |
937 | |
938 bool poll_device = false; | |
939 // Add fd, if we should poll on it. | |
940 // Can be polled as soon as either input or output buffers are queued. | |
941 if (input_buffer_queued_count_ + output_buffer_queued_count_ > 0) | |
942 poll_device = true; | |
943 | |
944 // ServiceDeviceTask() should only ever be scheduled from DevicePollTask(), | |
945 // so either: | |
946 // * device_poll_thread_ is running normally | |
947 // * device_poll_thread_ scheduled us, but then a ResetTask() or DestroyTask() | |
948 // shut it down, in which case we're either in kResetting or kError states | |
949 // respectively, and we should have early-outed already. | |
950 DCHECK(device_poll_thread_.message_loop()); | |
951 // Queue the DevicePollTask() now. | |
952 device_poll_thread_.message_loop()->PostTask( | |
953 FROM_HERE, | |
954 base::Bind(&V4L2VideoDecodeAccelerator::DevicePollTask, | |
955 base::Unretained(this), | |
956 poll_device)); | |
957 | |
958 DVLOG(1) << "ServiceDeviceTask(): buffer counts: DEC[" | |
959 << decoder_input_queue_.size() << "->" | |
960 << input_ready_queue_.size() << "] => DEVICE[" | |
961 << free_input_buffers_.size() << "+" | |
962 << input_buffer_queued_count_ << "/" | |
963 << input_buffer_map_.size() << "->" | |
964 << free_output_buffers_.size() << "+" | |
965 << output_buffer_queued_count_ << "/" | |
966 << output_buffer_map_.size() << "] => VDA[" | |
967 << decoder_frames_at_client_ << "]"; | |
968 | |
969 ScheduleDecodeBufferTaskIfNeeded(); | |
970 if (resolution_change_pending) | |
971 StartResolutionChange(); | |
972 } | |
973 | |
974 void V4L2VideoDecodeAccelerator::Enqueue() { | |
975 DVLOG(3) << "Enqueue()"; | |
976 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
977 DCHECK_NE(decoder_state_, kUninitialized); | |
978 TRACE_EVENT0("Video Decoder", "V4L2VDA::Enqueue"); | |
979 | |
980 // Drain the pipe of completed decode buffers. | |
981 const int old_inputs_queued = input_buffer_queued_count_; | |
982 while (!input_ready_queue_.empty()) { | |
983 if (!EnqueueInputRecord()) | |
984 return; | |
985 } | |
986 if (old_inputs_queued == 0 && input_buffer_queued_count_ != 0) { | |
987 // We just started up a previously empty queue. | |
988 // Queue state changed; signal interrupt. | |
989 if (!device_->SetDevicePollInterrupt()) { | |
990 PLOG(ERROR) << "SetDevicePollInterrupt(): failed"; | |
991 NOTIFY_ERROR(PLATFORM_FAILURE); | |
992 return; | |
993 } | |
994 // Start VIDIOC_STREAMON if we haven't yet. | |
995 if (!input_streamon_) { | |
996 __u32 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
997 IOCTL_OR_ERROR_RETURN(VIDIOC_STREAMON, &type); | |
998 input_streamon_ = true; | |
999 } | |
1000 } | |
1001 | |
1002 // Enqueue all the outputs we can. | |
1003 const int old_outputs_queued = output_buffer_queued_count_; | |
1004 while (!free_output_buffers_.empty()) { | |
1005 if (!EnqueueOutputRecord()) | |
1006 return; | |
1007 } | |
1008 if (old_outputs_queued == 0 && output_buffer_queued_count_ != 0) { | |
1009 // We just started up a previously empty queue. | |
1010 // Queue state changed; signal interrupt. | |
1011 if (!device_->SetDevicePollInterrupt()) { | |
1012 PLOG(ERROR) << "SetDevicePollInterrupt(): failed"; | |
1013 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1014 return; | |
1015 } | |
1016 // Start VIDIOC_STREAMON if we haven't yet. | |
1017 if (!output_streamon_) { | |
1018 __u32 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1019 IOCTL_OR_ERROR_RETURN(VIDIOC_STREAMON, &type); | |
1020 output_streamon_ = true; | |
1021 } | |
1022 } | |
1023 } | |
1024 | |
1025 bool V4L2VideoDecodeAccelerator::DequeueResolutionChangeEvent() { | |
1026 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1027 DCHECK_NE(decoder_state_, kUninitialized); | |
1028 DVLOG(3) << "DequeueResolutionChangeEvent()"; | |
1029 | |
1030 struct v4l2_event ev; | |
1031 memset(&ev, 0, sizeof(ev)); | |
1032 | |
1033 while (device_->Ioctl(VIDIOC_DQEVENT, &ev) == 0) { | |
1034 if (ev.type == V4L2_EVENT_SOURCE_CHANGE) { | |
1035 if (ev.u.src_change.changes & V4L2_EVENT_SRC_CH_RESOLUTION) { | |
1036 DVLOG(3) | |
1037 << "DequeueResolutionChangeEvent(): got resolution change event."; | |
1038 return true; | |
1039 } | |
1040 } else { | |
1041 LOG(ERROR) << "DequeueResolutionChangeEvent(): got an event (" << ev.type | |
1042 << ") we haven't subscribed to."; | |
1043 } | |
1044 } | |
1045 return false; | |
1046 } | |
1047 | |
1048 void V4L2VideoDecodeAccelerator::Dequeue() { | |
1049 DVLOG(3) << "Dequeue()"; | |
1050 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1051 DCHECK_NE(decoder_state_, kUninitialized); | |
1052 TRACE_EVENT0("Video Decoder", "V4L2VDA::Dequeue"); | |
1053 | |
1054 // Dequeue completed input (VIDEO_OUTPUT) buffers, and recycle to the free | |
1055 // list. | |
1056 while (input_buffer_queued_count_ > 0) { | |
1057 DCHECK(input_streamon_); | |
1058 struct v4l2_buffer dqbuf; | |
1059 struct v4l2_plane planes[1]; | |
1060 memset(&dqbuf, 0, sizeof(dqbuf)); | |
1061 memset(planes, 0, sizeof(planes)); | |
1062 dqbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1063 dqbuf.memory = V4L2_MEMORY_MMAP; | |
1064 dqbuf.m.planes = planes; | |
1065 dqbuf.length = 1; | |
1066 if (device_->Ioctl(VIDIOC_DQBUF, &dqbuf) != 0) { | |
1067 if (errno == EAGAIN) { | |
1068 // EAGAIN if we're just out of buffers to dequeue. | |
1069 break; | |
1070 } | |
1071 PLOG(ERROR) << "Dequeue(): ioctl() failed: VIDIOC_DQBUF"; | |
1072 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1073 return; | |
1074 } | |
1075 InputRecord& input_record = input_buffer_map_[dqbuf.index]; | |
1076 DCHECK(input_record.at_device); | |
1077 free_input_buffers_.push_back(dqbuf.index); | |
1078 input_record.at_device = false; | |
1079 input_record.bytes_used = 0; | |
1080 input_record.input_id = -1; | |
1081 input_buffer_queued_count_--; | |
1082 } | |
1083 | |
1084 // Dequeue completed output (VIDEO_CAPTURE) buffers, and queue to the | |
1085 // completed queue. | |
1086 while (output_buffer_queued_count_ > 0) { | |
1087 DCHECK(output_streamon_); | |
1088 struct v4l2_buffer dqbuf; | |
1089 std::unique_ptr<struct v4l2_plane[]> planes( | |
1090 new v4l2_plane[output_planes_count_]); | |
1091 memset(&dqbuf, 0, sizeof(dqbuf)); | |
1092 memset(planes.get(), 0, sizeof(struct v4l2_plane) * output_planes_count_); | |
1093 dqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1094 dqbuf.memory = V4L2_MEMORY_MMAP; | |
1095 dqbuf.m.planes = planes.get(); | |
1096 dqbuf.length = output_planes_count_; | |
1097 if (device_->Ioctl(VIDIOC_DQBUF, &dqbuf) != 0) { | |
1098 if (errno == EAGAIN) { | |
1099 // EAGAIN if we're just out of buffers to dequeue. | |
1100 break; | |
1101 } | |
1102 PLOG(ERROR) << "Dequeue(): ioctl() failed: VIDIOC_DQBUF"; | |
1103 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1104 return; | |
1105 } | |
1106 OutputRecord& output_record = output_buffer_map_[dqbuf.index]; | |
1107 DCHECK(output_record.at_device); | |
1108 DCHECK(!output_record.at_client); | |
1109 DCHECK_NE(output_record.egl_image, EGL_NO_IMAGE_KHR); | |
1110 DCHECK_NE(output_record.picture_id, -1); | |
1111 output_record.at_device = false; | |
1112 if (dqbuf.m.planes[0].bytesused == 0) { | |
1113 // This is an empty output buffer returned as part of a flush. | |
1114 free_output_buffers_.push(dqbuf.index); | |
1115 } else { | |
1116 DCHECK_GE(dqbuf.timestamp.tv_sec, 0); | |
1117 output_record.at_client = true; | |
1118 DVLOG(3) << "Dequeue(): returning input_id=" << dqbuf.timestamp.tv_sec | |
1119 << " as picture_id=" << output_record.picture_id; | |
1120 const media::Picture& picture = | |
1121 media::Picture(output_record.picture_id, dqbuf.timestamp.tv_sec, | |
1122 gfx::Rect(visible_size_), false); | |
1123 pending_picture_ready_.push( | |
1124 PictureRecord(output_record.cleared, picture)); | |
1125 SendPictureReady(); | |
1126 output_record.cleared = true; | |
1127 decoder_frames_at_client_++; | |
1128 } | |
1129 output_buffer_queued_count_--; | |
1130 } | |
1131 | |
1132 NotifyFlushDoneIfNeeded(); | |
1133 } | |
1134 | |
1135 bool V4L2VideoDecodeAccelerator::EnqueueInputRecord() { | |
1136 DVLOG(3) << "EnqueueInputRecord()"; | |
1137 DCHECK(!input_ready_queue_.empty()); | |
1138 | |
1139 // Enqueue an input (VIDEO_OUTPUT) buffer. | |
1140 const int buffer = input_ready_queue_.front(); | |
1141 InputRecord& input_record = input_buffer_map_[buffer]; | |
1142 DCHECK(!input_record.at_device); | |
1143 struct v4l2_buffer qbuf; | |
1144 struct v4l2_plane qbuf_plane; | |
1145 memset(&qbuf, 0, sizeof(qbuf)); | |
1146 memset(&qbuf_plane, 0, sizeof(qbuf_plane)); | |
1147 qbuf.index = buffer; | |
1148 qbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1149 qbuf.timestamp.tv_sec = input_record.input_id; | |
1150 qbuf.memory = V4L2_MEMORY_MMAP; | |
1151 qbuf.m.planes = &qbuf_plane; | |
1152 qbuf.m.planes[0].bytesused = input_record.bytes_used; | |
1153 qbuf.length = 1; | |
1154 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QBUF, &qbuf); | |
1155 input_ready_queue_.pop(); | |
1156 input_record.at_device = true; | |
1157 input_buffer_queued_count_++; | |
1158 DVLOG(3) << "EnqueueInputRecord(): enqueued input_id=" | |
1159 << input_record.input_id << " size=" << input_record.bytes_used; | |
1160 return true; | |
1161 } | |
1162 | |
1163 bool V4L2VideoDecodeAccelerator::EnqueueOutputRecord() { | |
1164 DVLOG(3) << "EnqueueOutputRecord()"; | |
1165 DCHECK(!free_output_buffers_.empty()); | |
1166 | |
1167 // Enqueue an output (VIDEO_CAPTURE) buffer. | |
1168 const int buffer = free_output_buffers_.front(); | |
1169 OutputRecord& output_record = output_buffer_map_[buffer]; | |
1170 DCHECK(!output_record.at_device); | |
1171 DCHECK(!output_record.at_client); | |
1172 DCHECK_NE(output_record.egl_image, EGL_NO_IMAGE_KHR); | |
1173 DCHECK_NE(output_record.picture_id, -1); | |
1174 if (output_record.egl_sync != EGL_NO_SYNC_KHR) { | |
1175 TRACE_EVENT0("Video Decoder", | |
1176 "V4L2VDA::EnqueueOutputRecord: eglClientWaitSyncKHR"); | |
1177 // If we have to wait for completion, wait. Note that | |
1178 // free_output_buffers_ is a FIFO queue, so we always wait on the | |
1179 // buffer that has been in the queue the longest. | |
1180 if (eglClientWaitSyncKHR(egl_display_, output_record.egl_sync, 0, | |
1181 EGL_FOREVER_KHR) == EGL_FALSE) { | |
1182 // This will cause tearing, but is safe otherwise. | |
1183 DVLOG(1) << __func__ << " eglClientWaitSyncKHR failed!"; | |
1184 } | |
1185 if (eglDestroySyncKHR(egl_display_, output_record.egl_sync) != EGL_TRUE) { | |
1186 LOG(ERROR) << __func__ << " eglDestroySyncKHR failed!"; | |
1187 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1188 return false; | |
1189 } | |
1190 output_record.egl_sync = EGL_NO_SYNC_KHR; | |
1191 } | |
1192 struct v4l2_buffer qbuf; | |
1193 std::unique_ptr<struct v4l2_plane[]> qbuf_planes( | |
1194 new v4l2_plane[output_planes_count_]); | |
1195 memset(&qbuf, 0, sizeof(qbuf)); | |
1196 memset( | |
1197 qbuf_planes.get(), 0, sizeof(struct v4l2_plane) * output_planes_count_); | |
1198 qbuf.index = buffer; | |
1199 qbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1200 qbuf.memory = V4L2_MEMORY_MMAP; | |
1201 qbuf.m.planes = qbuf_planes.get(); | |
1202 qbuf.length = output_planes_count_; | |
1203 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QBUF, &qbuf); | |
1204 free_output_buffers_.pop(); | |
1205 output_record.at_device = true; | |
1206 output_buffer_queued_count_++; | |
1207 return true; | |
1208 } | |
1209 | |
1210 void V4L2VideoDecodeAccelerator::ReusePictureBufferTask( | |
1211 int32_t picture_buffer_id, | |
1212 std::unique_ptr<EGLSyncKHRRef> egl_sync_ref) { | |
1213 DVLOG(3) << "ReusePictureBufferTask(): picture_buffer_id=" | |
1214 << picture_buffer_id; | |
1215 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1216 TRACE_EVENT0("Video Decoder", "V4L2VDA::ReusePictureBufferTask"); | |
1217 | |
1218 // We run ReusePictureBufferTask even if we're in kResetting. | |
1219 if (decoder_state_ == kError) { | |
1220 DVLOG(2) << "ReusePictureBufferTask(): early out: kError state"; | |
1221 return; | |
1222 } | |
1223 | |
1224 if (decoder_state_ == kChangingResolution) { | |
1225 DVLOG(2) << "ReusePictureBufferTask(): early out: kChangingResolution"; | |
1226 return; | |
1227 } | |
1228 | |
1229 size_t index; | |
1230 for (index = 0; index < output_buffer_map_.size(); ++index) | |
1231 if (output_buffer_map_[index].picture_id == picture_buffer_id) | |
1232 break; | |
1233 | |
1234 if (index >= output_buffer_map_.size()) { | |
1235 // It's possible that we've already posted a DismissPictureBuffer for this | |
1236 // picture, but it has not yet executed when this ReusePictureBuffer was | |
1237 // posted to us by the client. In that case just ignore this (we've already | |
1238 // dismissed it and accounted for that) and let the sync object get | |
1239 // destroyed. | |
1240 DVLOG(4) << "ReusePictureBufferTask(): got picture id= " | |
1241 << picture_buffer_id << " not in use (anymore?)."; | |
1242 return; | |
1243 } | |
1244 | |
1245 OutputRecord& output_record = output_buffer_map_[index]; | |
1246 if (output_record.at_device || !output_record.at_client) { | |
1247 LOG(ERROR) << "ReusePictureBufferTask(): picture_buffer_id not reusable"; | |
1248 NOTIFY_ERROR(INVALID_ARGUMENT); | |
1249 return; | |
1250 } | |
1251 | |
1252 DCHECK_EQ(output_record.egl_sync, EGL_NO_SYNC_KHR); | |
1253 DCHECK(!output_record.at_device); | |
1254 output_record.at_client = false; | |
1255 output_record.egl_sync = egl_sync_ref->egl_sync; | |
1256 free_output_buffers_.push(index); | |
1257 decoder_frames_at_client_--; | |
1258 // Take ownership of the EGLSync. | |
1259 egl_sync_ref->egl_sync = EGL_NO_SYNC_KHR; | |
1260 // We got a buffer back, so enqueue it back. | |
1261 Enqueue(); | |
1262 } | |
1263 | |
1264 void V4L2VideoDecodeAccelerator::FlushTask() { | |
1265 DVLOG(3) << "FlushTask()"; | |
1266 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1267 TRACE_EVENT0("Video Decoder", "V4L2VDA::FlushTask"); | |
1268 | |
1269 // Flush outstanding buffers. | |
1270 if (decoder_state_ == kInitialized || decoder_state_ == kAfterReset) { | |
1271 // There's nothing in the pipe, so return done immediately. | |
1272 DVLOG(3) << "FlushTask(): returning flush"; | |
1273 child_task_runner_->PostTask(FROM_HERE, | |
1274 base::Bind(&Client::NotifyFlushDone, client_)); | |
1275 return; | |
1276 } else if (decoder_state_ == kError) { | |
1277 DVLOG(2) << "FlushTask(): early out: kError state"; | |
1278 return; | |
1279 } | |
1280 | |
1281 // We don't support stacked flushing. | |
1282 DCHECK(!decoder_flushing_); | |
1283 | |
1284 // Queue up an empty buffer -- this triggers the flush. | |
1285 decoder_input_queue_.push( | |
1286 linked_ptr<BitstreamBufferRef>(new BitstreamBufferRef( | |
1287 decode_client_, decode_task_runner_, nullptr, kFlushBufferId))); | |
1288 decoder_flushing_ = true; | |
1289 SendPictureReady(); // Send all pending PictureReady. | |
1290 | |
1291 ScheduleDecodeBufferTaskIfNeeded(); | |
1292 } | |
1293 | |
1294 void V4L2VideoDecodeAccelerator::NotifyFlushDoneIfNeeded() { | |
1295 if (!decoder_flushing_) | |
1296 return; | |
1297 | |
1298 // Pipeline is empty when: | |
1299 // * Decoder input queue is empty of non-delayed buffers. | |
1300 // * There is no currently filling input buffer. | |
1301 // * Input holding queue is empty. | |
1302 // * All input (VIDEO_OUTPUT) buffers are returned. | |
1303 if (!decoder_input_queue_.empty()) { | |
1304 if (decoder_input_queue_.front()->input_id != | |
1305 decoder_delay_bitstream_buffer_id_) | |
1306 return; | |
1307 } | |
1308 if (decoder_current_input_buffer_ != -1) | |
1309 return; | |
1310 if ((input_ready_queue_.size() + input_buffer_queued_count_) != 0) | |
1311 return; | |
1312 | |
1313 // TODO(posciak): crbug.com/270039. Exynos requires a streamoff-streamon | |
1314 // sequence after flush to continue, even if we are not resetting. This would | |
1315 // make sense, because we don't really want to resume from a non-resume point | |
1316 // (e.g. not from an IDR) if we are flushed. | |
1317 // MSE player however triggers a Flush() on chunk end, but never Reset(). One | |
1318 // could argue either way, or even say that Flush() is not needed/harmful when | |
1319 // transitioning to next chunk. | |
1320 // For now, do the streamoff-streamon cycle to satisfy Exynos and not freeze | |
1321 // when doing MSE. This should be harmless otherwise. | |
1322 if (!(StopDevicePoll() && StopOutputStream() && StopInputStream())) | |
1323 return; | |
1324 | |
1325 if (!StartDevicePoll()) | |
1326 return; | |
1327 | |
1328 decoder_delay_bitstream_buffer_id_ = -1; | |
1329 decoder_flushing_ = false; | |
1330 DVLOG(3) << "NotifyFlushDoneIfNeeded(): returning flush"; | |
1331 child_task_runner_->PostTask(FROM_HERE, | |
1332 base::Bind(&Client::NotifyFlushDone, client_)); | |
1333 | |
1334 // While we were flushing, we early-outed DecodeBufferTask()s. | |
1335 ScheduleDecodeBufferTaskIfNeeded(); | |
1336 } | |
1337 | |
1338 void V4L2VideoDecodeAccelerator::ResetTask() { | |
1339 DVLOG(3) << "ResetTask()"; | |
1340 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1341 TRACE_EVENT0("Video Decoder", "V4L2VDA::ResetTask"); | |
1342 | |
1343 if (decoder_state_ == kError) { | |
1344 DVLOG(2) << "ResetTask(): early out: kError state"; | |
1345 return; | |
1346 } | |
1347 | |
1348 // If we are in the middle of switching resolutions, postpone reset until | |
1349 // it's done. We don't have to worry about timing of this wrt to decoding, | |
1350 // because output pipe is already stopped if we are changing resolution. | |
1351 // We will come back here after we are done with the resolution change. | |
1352 DCHECK(!resolution_change_reset_pending_); | |
1353 if (decoder_state_ == kChangingResolution) { | |
1354 resolution_change_reset_pending_ = true; | |
1355 return; | |
1356 } | |
1357 | |
1358 // After the output stream is stopped, the codec should not post any | |
1359 // resolution change events. So we dequeue the resolution change event | |
1360 // afterwards. The event could be posted before or while stopping the output | |
1361 // stream. The codec will expect the buffer of new size after the seek, so | |
1362 // we need to handle the resolution change event first. | |
1363 if (!(StopDevicePoll() && StopOutputStream())) | |
1364 return; | |
1365 | |
1366 if (DequeueResolutionChangeEvent()) { | |
1367 resolution_change_reset_pending_ = true; | |
1368 StartResolutionChange(); | |
1369 return; | |
1370 } | |
1371 | |
1372 if (!StopInputStream()) | |
1373 return; | |
1374 | |
1375 decoder_current_bitstream_buffer_.reset(); | |
1376 while (!decoder_input_queue_.empty()) | |
1377 decoder_input_queue_.pop(); | |
1378 | |
1379 decoder_current_input_buffer_ = -1; | |
1380 | |
1381 // If we were flushing, we'll never return any more BitstreamBuffers or | |
1382 // PictureBuffers; they have all been dropped and returned by now. | |
1383 NotifyFlushDoneIfNeeded(); | |
1384 | |
1385 // Mark that we're resetting, then enqueue a ResetDoneTask(). All intervening | |
1386 // jobs will early-out in the kResetting state. | |
1387 decoder_state_ = kResetting; | |
1388 SendPictureReady(); // Send all pending PictureReady. | |
1389 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
1390 &V4L2VideoDecodeAccelerator::ResetDoneTask, base::Unretained(this))); | |
1391 } | |
1392 | |
1393 void V4L2VideoDecodeAccelerator::ResetDoneTask() { | |
1394 DVLOG(3) << "ResetDoneTask()"; | |
1395 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1396 TRACE_EVENT0("Video Decoder", "V4L2VDA::ResetDoneTask"); | |
1397 | |
1398 if (decoder_state_ == kError) { | |
1399 DVLOG(2) << "ResetDoneTask(): early out: kError state"; | |
1400 return; | |
1401 } | |
1402 | |
1403 if (!StartDevicePoll()) | |
1404 return; | |
1405 | |
1406 // Reset format-specific bits. | |
1407 if (video_profile_ >= media::H264PROFILE_MIN && | |
1408 video_profile_ <= media::H264PROFILE_MAX) { | |
1409 decoder_h264_parser_.reset(new media::H264Parser()); | |
1410 } | |
1411 | |
1412 // Jobs drained, we're finished resetting. | |
1413 DCHECK_EQ(decoder_state_, kResetting); | |
1414 if (output_buffer_map_.empty()) { | |
1415 // We must have gotten Reset() before we had a chance to request buffers | |
1416 // from the client. | |
1417 decoder_state_ = kInitialized; | |
1418 } else { | |
1419 decoder_state_ = kAfterReset; | |
1420 } | |
1421 | |
1422 decoder_partial_frame_pending_ = false; | |
1423 decoder_delay_bitstream_buffer_id_ = -1; | |
1424 child_task_runner_->PostTask(FROM_HERE, | |
1425 base::Bind(&Client::NotifyResetDone, client_)); | |
1426 | |
1427 // While we were resetting, we early-outed DecodeBufferTask()s. | |
1428 ScheduleDecodeBufferTaskIfNeeded(); | |
1429 } | |
1430 | |
1431 void V4L2VideoDecodeAccelerator::DestroyTask() { | |
1432 DVLOG(3) << "DestroyTask()"; | |
1433 TRACE_EVENT0("Video Decoder", "V4L2VDA::DestroyTask"); | |
1434 | |
1435 // DestroyTask() should run regardless of decoder_state_. | |
1436 | |
1437 StopDevicePoll(); | |
1438 StopOutputStream(); | |
1439 StopInputStream(); | |
1440 | |
1441 decoder_current_bitstream_buffer_.reset(); | |
1442 decoder_current_input_buffer_ = -1; | |
1443 decoder_decode_buffer_tasks_scheduled_ = 0; | |
1444 decoder_frames_at_client_ = 0; | |
1445 while (!decoder_input_queue_.empty()) | |
1446 decoder_input_queue_.pop(); | |
1447 decoder_flushing_ = false; | |
1448 | |
1449 // Set our state to kError. Just in case. | |
1450 decoder_state_ = kError; | |
1451 } | |
1452 | |
1453 bool V4L2VideoDecodeAccelerator::StartDevicePoll() { | |
1454 DVLOG(3) << "StartDevicePoll()"; | |
1455 DCHECK(!device_poll_thread_.IsRunning()); | |
1456 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1457 | |
1458 // Start up the device poll thread and schedule its first DevicePollTask(). | |
1459 if (!device_poll_thread_.Start()) { | |
1460 LOG(ERROR) << "StartDevicePoll(): Device thread failed to start"; | |
1461 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1462 return false; | |
1463 } | |
1464 device_poll_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
1465 &V4L2VideoDecodeAccelerator::DevicePollTask, | |
1466 base::Unretained(this), | |
1467 0)); | |
1468 | |
1469 return true; | |
1470 } | |
1471 | |
1472 bool V4L2VideoDecodeAccelerator::StopDevicePoll() { | |
1473 DVLOG(3) << "StopDevicePoll()"; | |
1474 | |
1475 if (!device_poll_thread_.IsRunning()) | |
1476 return true; | |
1477 | |
1478 if (decoder_thread_.IsRunning()) | |
1479 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1480 | |
1481 // Signal the DevicePollTask() to stop, and stop the device poll thread. | |
1482 if (!device_->SetDevicePollInterrupt()) { | |
1483 PLOG(ERROR) << "SetDevicePollInterrupt(): failed"; | |
1484 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1485 return false; | |
1486 } | |
1487 device_poll_thread_.Stop(); | |
1488 // Clear the interrupt now, to be sure. | |
1489 if (!device_->ClearDevicePollInterrupt()) { | |
1490 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1491 return false; | |
1492 } | |
1493 DVLOG(3) << "StopDevicePoll(): device poll stopped"; | |
1494 return true; | |
1495 } | |
1496 | |
1497 bool V4L2VideoDecodeAccelerator::StopOutputStream() { | |
1498 DVLOG(3) << "StopOutputStream()"; | |
1499 if (!output_streamon_) | |
1500 return true; | |
1501 | |
1502 __u32 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1503 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_STREAMOFF, &type); | |
1504 output_streamon_ = false; | |
1505 | |
1506 // Reset accounting info for output. | |
1507 while (!free_output_buffers_.empty()) | |
1508 free_output_buffers_.pop(); | |
1509 | |
1510 for (size_t i = 0; i < output_buffer_map_.size(); ++i) { | |
1511 OutputRecord& output_record = output_buffer_map_[i]; | |
1512 DCHECK(!(output_record.at_client && output_record.at_device)); | |
1513 | |
1514 // After streamoff, the device drops ownership of all buffers, even if | |
1515 // we don't dequeue them explicitly. | |
1516 output_buffer_map_[i].at_device = false; | |
1517 // Some of them may still be owned by the client however. | |
1518 // Reuse only those that aren't. | |
1519 if (!output_record.at_client) { | |
1520 DCHECK_EQ(output_record.egl_sync, EGL_NO_SYNC_KHR); | |
1521 free_output_buffers_.push(i); | |
1522 } | |
1523 } | |
1524 output_buffer_queued_count_ = 0; | |
1525 return true; | |
1526 } | |
1527 | |
1528 bool V4L2VideoDecodeAccelerator::StopInputStream() { | |
1529 DVLOG(3) << "StopInputStream()"; | |
1530 if (!input_streamon_) | |
1531 return true; | |
1532 | |
1533 __u32 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1534 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_STREAMOFF, &type); | |
1535 input_streamon_ = false; | |
1536 | |
1537 // Reset accounting info for input. | |
1538 while (!input_ready_queue_.empty()) | |
1539 input_ready_queue_.pop(); | |
1540 free_input_buffers_.clear(); | |
1541 for (size_t i = 0; i < input_buffer_map_.size(); ++i) { | |
1542 free_input_buffers_.push_back(i); | |
1543 input_buffer_map_[i].at_device = false; | |
1544 input_buffer_map_[i].bytes_used = 0; | |
1545 input_buffer_map_[i].input_id = -1; | |
1546 } | |
1547 input_buffer_queued_count_ = 0; | |
1548 | |
1549 return true; | |
1550 } | |
1551 | |
1552 void V4L2VideoDecodeAccelerator::StartResolutionChange() { | |
1553 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1554 DCHECK_NE(decoder_state_, kUninitialized); | |
1555 DCHECK_NE(decoder_state_, kResetting); | |
1556 | |
1557 DVLOG(3) << "Initiate resolution change"; | |
1558 | |
1559 if (!(StopDevicePoll() && StopOutputStream())) | |
1560 return; | |
1561 | |
1562 decoder_state_ = kChangingResolution; | |
1563 | |
1564 // Post a task to clean up buffers on child thread. This will also ensure | |
1565 // that we won't accept ReusePictureBuffer() anymore after that. | |
1566 child_task_runner_->PostTask( | |
1567 FROM_HERE, | |
1568 base::Bind(&V4L2VideoDecodeAccelerator::ResolutionChangeDestroyBuffers, | |
1569 weak_this_)); | |
1570 } | |
1571 | |
1572 void V4L2VideoDecodeAccelerator::FinishResolutionChange() { | |
1573 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1574 DCHECK_EQ(decoder_state_, kChangingResolution); | |
1575 DVLOG(3) << "FinishResolutionChange()"; | |
1576 | |
1577 if (decoder_state_ == kError) { | |
1578 DVLOG(2) << "FinishResolutionChange(): early out: kError state"; | |
1579 return; | |
1580 } | |
1581 | |
1582 struct v4l2_format format; | |
1583 bool again; | |
1584 gfx::Size visible_size; | |
1585 bool ret = GetFormatInfo(&format, &visible_size, &again); | |
1586 if (!ret || again) { | |
1587 LOG(ERROR) << "Couldn't get format information after resolution change"; | |
1588 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1589 return; | |
1590 } | |
1591 | |
1592 if (!CreateBuffersForFormat(format, visible_size)) { | |
1593 LOG(ERROR) << "Couldn't reallocate buffers after resolution change"; | |
1594 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1595 return; | |
1596 } | |
1597 | |
1598 decoder_state_ = kDecoding; | |
1599 | |
1600 if (resolution_change_reset_pending_) { | |
1601 resolution_change_reset_pending_ = false; | |
1602 ResetTask(); | |
1603 return; | |
1604 } | |
1605 | |
1606 if (!StartDevicePoll()) | |
1607 return; | |
1608 | |
1609 Enqueue(); | |
1610 ScheduleDecodeBufferTaskIfNeeded(); | |
1611 } | |
1612 | |
1613 void V4L2VideoDecodeAccelerator::DevicePollTask(bool poll_device) { | |
1614 DVLOG(3) << "DevicePollTask()"; | |
1615 DCHECK_EQ(device_poll_thread_.message_loop(), base::MessageLoop::current()); | |
1616 TRACE_EVENT0("Video Decoder", "V4L2VDA::DevicePollTask"); | |
1617 | |
1618 bool event_pending = false; | |
1619 | |
1620 if (!device_->Poll(poll_device, &event_pending)) { | |
1621 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1622 return; | |
1623 } | |
1624 | |
1625 // All processing should happen on ServiceDeviceTask(), since we shouldn't | |
1626 // touch decoder state from this thread. | |
1627 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
1628 &V4L2VideoDecodeAccelerator::ServiceDeviceTask, | |
1629 base::Unretained(this), event_pending)); | |
1630 } | |
1631 | |
1632 void V4L2VideoDecodeAccelerator::NotifyError(Error error) { | |
1633 DVLOG(2) << "NotifyError()"; | |
1634 | |
1635 if (!child_task_runner_->BelongsToCurrentThread()) { | |
1636 child_task_runner_->PostTask( | |
1637 FROM_HERE, base::Bind(&V4L2VideoDecodeAccelerator::NotifyError, | |
1638 weak_this_, error)); | |
1639 return; | |
1640 } | |
1641 | |
1642 if (client_) { | |
1643 client_->NotifyError(error); | |
1644 client_ptr_factory_.reset(); | |
1645 } | |
1646 } | |
1647 | |
1648 void V4L2VideoDecodeAccelerator::SetErrorState(Error error) { | |
1649 // We can touch decoder_state_ only if this is the decoder thread or the | |
1650 // decoder thread isn't running. | |
1651 if (decoder_thread_.message_loop() != NULL && | |
1652 decoder_thread_.message_loop() != base::MessageLoop::current()) { | |
1653 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
1654 &V4L2VideoDecodeAccelerator::SetErrorState, | |
1655 base::Unretained(this), error)); | |
1656 return; | |
1657 } | |
1658 | |
1659 // Post NotifyError only if we are already initialized, as the API does | |
1660 // not allow doing so before that. | |
1661 if (decoder_state_ != kError && decoder_state_ != kUninitialized) | |
1662 NotifyError(error); | |
1663 | |
1664 decoder_state_ = kError; | |
1665 } | |
1666 | |
1667 bool V4L2VideoDecodeAccelerator::GetFormatInfo(struct v4l2_format* format, | |
1668 gfx::Size* visible_size, | |
1669 bool* again) { | |
1670 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1671 | |
1672 *again = false; | |
1673 memset(format, 0, sizeof(*format)); | |
1674 format->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1675 if (device_->Ioctl(VIDIOC_G_FMT, format) != 0) { | |
1676 if (errno == EINVAL) { | |
1677 // EINVAL means we haven't seen sufficient stream to decode the format. | |
1678 *again = true; | |
1679 return true; | |
1680 } else { | |
1681 PLOG(ERROR) << __func__ << "(): ioctl() failed: VIDIOC_G_FMT"; | |
1682 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1683 return false; | |
1684 } | |
1685 } | |
1686 | |
1687 // Make sure we are still getting the format we set on initialization. | |
1688 if (format->fmt.pix_mp.pixelformat != output_format_fourcc_) { | |
1689 LOG(ERROR) << "Unexpected format from G_FMT on output"; | |
1690 return false; | |
1691 } | |
1692 | |
1693 gfx::Size coded_size(format->fmt.pix_mp.width, format->fmt.pix_mp.height); | |
1694 if (visible_size != nullptr) | |
1695 *visible_size = GetVisibleSize(coded_size); | |
1696 | |
1697 return true; | |
1698 } | |
1699 | |
1700 bool V4L2VideoDecodeAccelerator::CreateBuffersForFormat( | |
1701 const struct v4l2_format& format, | |
1702 const gfx::Size& visible_size) { | |
1703 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1704 output_planes_count_ = format.fmt.pix_mp.num_planes; | |
1705 coded_size_.SetSize(format.fmt.pix_mp.width, format.fmt.pix_mp.height); | |
1706 visible_size_ = visible_size; | |
1707 DVLOG(3) << "CreateBuffersForFormat(): new resolution: " | |
1708 << coded_size_.ToString() << ", visible size: " | |
1709 << visible_size_.ToString(); | |
1710 | |
1711 return CreateOutputBuffers(); | |
1712 } | |
1713 | |
1714 gfx::Size V4L2VideoDecodeAccelerator::GetVisibleSize( | |
1715 const gfx::Size& coded_size) { | |
1716 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
1717 | |
1718 struct v4l2_crop crop_arg; | |
1719 memset(&crop_arg, 0, sizeof(crop_arg)); | |
1720 crop_arg.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1721 | |
1722 if (device_->Ioctl(VIDIOC_G_CROP, &crop_arg) != 0) { | |
1723 PLOG(ERROR) << "GetVisibleSize(): ioctl() VIDIOC_G_CROP failed"; | |
1724 return coded_size; | |
1725 } | |
1726 | |
1727 gfx::Rect rect(crop_arg.c.left, crop_arg.c.top, crop_arg.c.width, | |
1728 crop_arg.c.height); | |
1729 DVLOG(3) << "visible rectangle is " << rect.ToString(); | |
1730 if (!gfx::Rect(coded_size).Contains(rect)) { | |
1731 DLOG(ERROR) << "visible rectangle " << rect.ToString() | |
1732 << " is not inside coded size " << coded_size.ToString(); | |
1733 return coded_size; | |
1734 } | |
1735 if (rect.IsEmpty()) { | |
1736 DLOG(ERROR) << "visible size is empty"; | |
1737 return coded_size; | |
1738 } | |
1739 | |
1740 // Chrome assume picture frame is coded at (0, 0). | |
1741 if (!rect.origin().IsOrigin()) { | |
1742 DLOG(ERROR) << "Unexpected visible rectangle " << rect.ToString() | |
1743 << ", top-left is not origin"; | |
1744 return coded_size; | |
1745 } | |
1746 | |
1747 return rect.size(); | |
1748 } | |
1749 | |
1750 bool V4L2VideoDecodeAccelerator::CreateInputBuffers() { | |
1751 DVLOG(3) << "CreateInputBuffers()"; | |
1752 // We always run this as we prepare to initialize. | |
1753 DCHECK_EQ(decoder_state_, kUninitialized); | |
1754 DCHECK(!input_streamon_); | |
1755 DCHECK(input_buffer_map_.empty()); | |
1756 | |
1757 struct v4l2_requestbuffers reqbufs; | |
1758 memset(&reqbufs, 0, sizeof(reqbufs)); | |
1759 reqbufs.count = kInputBufferCount; | |
1760 reqbufs.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1761 reqbufs.memory = V4L2_MEMORY_MMAP; | |
1762 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_REQBUFS, &reqbufs); | |
1763 input_buffer_map_.resize(reqbufs.count); | |
1764 for (size_t i = 0; i < input_buffer_map_.size(); ++i) { | |
1765 free_input_buffers_.push_back(i); | |
1766 | |
1767 // Query for the MEMORY_MMAP pointer. | |
1768 struct v4l2_plane planes[1]; | |
1769 struct v4l2_buffer buffer; | |
1770 memset(&buffer, 0, sizeof(buffer)); | |
1771 memset(planes, 0, sizeof(planes)); | |
1772 buffer.index = i; | |
1773 buffer.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1774 buffer.memory = V4L2_MEMORY_MMAP; | |
1775 buffer.m.planes = planes; | |
1776 buffer.length = 1; | |
1777 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QUERYBUF, &buffer); | |
1778 void* address = device_->Mmap(NULL, | |
1779 buffer.m.planes[0].length, | |
1780 PROT_READ | PROT_WRITE, | |
1781 MAP_SHARED, | |
1782 buffer.m.planes[0].m.mem_offset); | |
1783 if (address == MAP_FAILED) { | |
1784 PLOG(ERROR) << "CreateInputBuffers(): mmap() failed"; | |
1785 return false; | |
1786 } | |
1787 input_buffer_map_[i].address = address; | |
1788 input_buffer_map_[i].length = buffer.m.planes[0].length; | |
1789 } | |
1790 | |
1791 return true; | |
1792 } | |
1793 | |
1794 bool V4L2VideoDecodeAccelerator::SetupFormats() { | |
1795 // We always run this as we prepare to initialize. | |
1796 DCHECK_EQ(decoder_state_, kUninitialized); | |
1797 DCHECK(!input_streamon_); | |
1798 DCHECK(!output_streamon_); | |
1799 | |
1800 __u32 input_format_fourcc = | |
1801 V4L2Device::VideoCodecProfileToV4L2PixFmt(video_profile_, false); | |
1802 if (!input_format_fourcc) { | |
1803 NOTREACHED(); | |
1804 return false; | |
1805 } | |
1806 | |
1807 size_t input_size; | |
1808 gfx::Size max_resolution, min_resolution; | |
1809 device_->GetSupportedResolution(input_format_fourcc, &min_resolution, | |
1810 &max_resolution); | |
1811 if (max_resolution.width() > 1920 && max_resolution.height() > 1088) | |
1812 input_size = kInputBufferMaxSizeFor4k; | |
1813 else | |
1814 input_size = kInputBufferMaxSizeFor1080p; | |
1815 | |
1816 struct v4l2_fmtdesc fmtdesc; | |
1817 memset(&fmtdesc, 0, sizeof(fmtdesc)); | |
1818 fmtdesc.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1819 bool is_format_supported = false; | |
1820 while (device_->Ioctl(VIDIOC_ENUM_FMT, &fmtdesc) == 0) { | |
1821 if (fmtdesc.pixelformat == input_format_fourcc) { | |
1822 is_format_supported = true; | |
1823 break; | |
1824 } | |
1825 ++fmtdesc.index; | |
1826 } | |
1827 | |
1828 if (!is_format_supported) { | |
1829 DVLOG(1) << "Input fourcc " << input_format_fourcc | |
1830 << " not supported by device."; | |
1831 return false; | |
1832 } | |
1833 | |
1834 struct v4l2_format format; | |
1835 memset(&format, 0, sizeof(format)); | |
1836 format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1837 format.fmt.pix_mp.pixelformat = input_format_fourcc; | |
1838 format.fmt.pix_mp.plane_fmt[0].sizeimage = input_size; | |
1839 format.fmt.pix_mp.num_planes = 1; | |
1840 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_FMT, &format); | |
1841 | |
1842 // We have to set up the format for output, because the driver may not allow | |
1843 // changing it once we start streaming; whether it can support our chosen | |
1844 // output format or not may depend on the input format. | |
1845 memset(&fmtdesc, 0, sizeof(fmtdesc)); | |
1846 fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1847 while (device_->Ioctl(VIDIOC_ENUM_FMT, &fmtdesc) == 0) { | |
1848 if (device_->CanCreateEGLImageFrom(fmtdesc.pixelformat)) { | |
1849 output_format_fourcc_ = fmtdesc.pixelformat; | |
1850 break; | |
1851 } | |
1852 ++fmtdesc.index; | |
1853 } | |
1854 | |
1855 if (output_format_fourcc_ == 0) { | |
1856 LOG(ERROR) << "Could not find a usable output format"; | |
1857 return false; | |
1858 } | |
1859 | |
1860 // Just set the fourcc for output; resolution, etc., will come from the | |
1861 // driver once it extracts it from the stream. | |
1862 memset(&format, 0, sizeof(format)); | |
1863 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1864 format.fmt.pix_mp.pixelformat = output_format_fourcc_; | |
1865 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_FMT, &format); | |
1866 | |
1867 return true; | |
1868 } | |
1869 | |
1870 bool V4L2VideoDecodeAccelerator::CreateOutputBuffers() { | |
1871 DVLOG(3) << "CreateOutputBuffers()"; | |
1872 DCHECK(decoder_state_ == kInitialized || | |
1873 decoder_state_ == kChangingResolution); | |
1874 DCHECK(!output_streamon_); | |
1875 DCHECK(output_buffer_map_.empty()); | |
1876 | |
1877 // Number of output buffers we need. | |
1878 struct v4l2_control ctrl; | |
1879 memset(&ctrl, 0, sizeof(ctrl)); | |
1880 ctrl.id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE; | |
1881 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_G_CTRL, &ctrl); | |
1882 output_dpb_size_ = ctrl.value; | |
1883 | |
1884 // Output format setup in Initialize(). | |
1885 | |
1886 const uint32_t buffer_count = output_dpb_size_ + kDpbOutputBufferExtraCount; | |
1887 DVLOG(3) << "CreateOutputBuffers(): ProvidePictureBuffers(): " | |
1888 << "buffer_count=" << buffer_count | |
1889 << ", coded_size=" << coded_size_.ToString(); | |
1890 child_task_runner_->PostTask( | |
1891 FROM_HERE, | |
1892 base::Bind(&Client::ProvidePictureBuffers, client_, buffer_count, 1, | |
1893 coded_size_, device_->GetTextureTarget())); | |
1894 | |
1895 // Wait for the client to call AssignPictureBuffers() on the Child thread. | |
1896 // We do this, because if we continue decoding without finishing buffer | |
1897 // allocation, we may end up Resetting before AssignPictureBuffers arrives, | |
1898 // resulting in unnecessary complications and subtle bugs. | |
1899 // For example, if the client calls Decode(Input1), Reset(), Decode(Input2) | |
1900 // in a sequence, and Decode(Input1) results in us getting here and exiting | |
1901 // without waiting, we might end up running Reset{,Done}Task() before | |
1902 // AssignPictureBuffers is scheduled, thus cleaning up and pushing buffers | |
1903 // to the free_output_buffers_ map twice. If we somehow marked buffers as | |
1904 // not ready, we'd need special handling for restarting the second Decode | |
1905 // task and delaying it anyway. | |
1906 // Waiting here is not very costly and makes reasoning about different | |
1907 // situations much simpler. | |
1908 pictures_assigned_.Wait(); | |
1909 | |
1910 Enqueue(); | |
1911 return true; | |
1912 } | |
1913 | |
1914 void V4L2VideoDecodeAccelerator::DestroyInputBuffers() { | |
1915 DVLOG(3) << "DestroyInputBuffers()"; | |
1916 DCHECK(child_task_runner_->BelongsToCurrentThread()); | |
1917 DCHECK(!input_streamon_); | |
1918 | |
1919 for (size_t i = 0; i < input_buffer_map_.size(); ++i) { | |
1920 if (input_buffer_map_[i].address != NULL) { | |
1921 device_->Munmap(input_buffer_map_[i].address, | |
1922 input_buffer_map_[i].length); | |
1923 } | |
1924 } | |
1925 | |
1926 struct v4l2_requestbuffers reqbufs; | |
1927 memset(&reqbufs, 0, sizeof(reqbufs)); | |
1928 reqbufs.count = 0; | |
1929 reqbufs.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1930 reqbufs.memory = V4L2_MEMORY_MMAP; | |
1931 IOCTL_OR_LOG_ERROR(VIDIOC_REQBUFS, &reqbufs); | |
1932 | |
1933 input_buffer_map_.clear(); | |
1934 free_input_buffers_.clear(); | |
1935 } | |
1936 | |
1937 bool V4L2VideoDecodeAccelerator::DestroyOutputBuffers() { | |
1938 DVLOG(3) << "DestroyOutputBuffers()"; | |
1939 DCHECK(child_task_runner_->BelongsToCurrentThread()); | |
1940 DCHECK(!output_streamon_); | |
1941 bool success = true; | |
1942 | |
1943 for (size_t i = 0; i < output_buffer_map_.size(); ++i) { | |
1944 OutputRecord& output_record = output_buffer_map_[i]; | |
1945 | |
1946 if (output_record.egl_image != EGL_NO_IMAGE_KHR) { | |
1947 if (device_->DestroyEGLImage(egl_display_, output_record.egl_image) != | |
1948 EGL_TRUE) { | |
1949 DVLOG(1) << __func__ << " DestroyEGLImage failed."; | |
1950 success = false; | |
1951 } | |
1952 } | |
1953 | |
1954 if (output_record.egl_sync != EGL_NO_SYNC_KHR) { | |
1955 if (eglDestroySyncKHR(egl_display_, output_record.egl_sync) != EGL_TRUE) { | |
1956 DVLOG(1) << __func__ << " eglDestroySyncKHR failed."; | |
1957 success = false; | |
1958 } | |
1959 } | |
1960 | |
1961 DVLOG(1) << "DestroyOutputBuffers(): dismissing PictureBuffer id=" | |
1962 << output_record.picture_id; | |
1963 child_task_runner_->PostTask( | |
1964 FROM_HERE, base::Bind(&Client::DismissPictureBuffer, client_, | |
1965 output_record.picture_id)); | |
1966 } | |
1967 | |
1968 struct v4l2_requestbuffers reqbufs; | |
1969 memset(&reqbufs, 0, sizeof(reqbufs)); | |
1970 reqbufs.count = 0; | |
1971 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1972 reqbufs.memory = V4L2_MEMORY_MMAP; | |
1973 if (device_->Ioctl(VIDIOC_REQBUFS, &reqbufs) != 0) { | |
1974 PLOG(ERROR) << "DestroyOutputBuffers() ioctl() failed: VIDIOC_REQBUFS"; | |
1975 success = false; | |
1976 } | |
1977 | |
1978 output_buffer_map_.clear(); | |
1979 while (!free_output_buffers_.empty()) | |
1980 free_output_buffers_.pop(); | |
1981 | |
1982 return success; | |
1983 } | |
1984 | |
1985 void V4L2VideoDecodeAccelerator::ResolutionChangeDestroyBuffers() { | |
1986 DCHECK(child_task_runner_->BelongsToCurrentThread()); | |
1987 DVLOG(3) << "ResolutionChangeDestroyBuffers()"; | |
1988 | |
1989 if (!DestroyOutputBuffers()) { | |
1990 LOG(ERROR) << __func__ << " Failed destroying output buffers."; | |
1991 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1992 return; | |
1993 } | |
1994 | |
1995 // Finish resolution change on decoder thread. | |
1996 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
1997 &V4L2VideoDecodeAccelerator::FinishResolutionChange, | |
1998 base::Unretained(this))); | |
1999 } | |
2000 | |
2001 void V4L2VideoDecodeAccelerator::SendPictureReady() { | |
2002 DVLOG(3) << "SendPictureReady()"; | |
2003 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
2004 bool resetting_or_flushing = | |
2005 (decoder_state_ == kResetting || decoder_flushing_); | |
2006 while (pending_picture_ready_.size() > 0) { | |
2007 bool cleared = pending_picture_ready_.front().cleared; | |
2008 const media::Picture& picture = pending_picture_ready_.front().picture; | |
2009 if (cleared && picture_clearing_count_ == 0) { | |
2010 // This picture is cleared. It can be posted to a thread different than | |
2011 // the main GPU thread to reduce latency. This should be the case after | |
2012 // all pictures are cleared at the beginning. | |
2013 decode_task_runner_->PostTask( | |
2014 FROM_HERE, | |
2015 base::Bind(&Client::PictureReady, decode_client_, picture)); | |
2016 pending_picture_ready_.pop(); | |
2017 } else if (!cleared || resetting_or_flushing) { | |
2018 DVLOG(3) << "SendPictureReady()" | |
2019 << ". cleared=" << pending_picture_ready_.front().cleared | |
2020 << ", decoder_state_=" << decoder_state_ | |
2021 << ", decoder_flushing_=" << decoder_flushing_ | |
2022 << ", picture_clearing_count_=" << picture_clearing_count_; | |
2023 // If the picture is not cleared, post it to the child thread because it | |
2024 // has to be cleared in the child thread. A picture only needs to be | |
2025 // cleared once. If the decoder is resetting or flushing, send all | |
2026 // pictures to ensure PictureReady arrive before reset or flush done. | |
2027 child_task_runner_->PostTaskAndReply( | |
2028 FROM_HERE, base::Bind(&Client::PictureReady, client_, picture), | |
2029 // Unretained is safe. If Client::PictureReady gets to run, |this| is | |
2030 // alive. Destroy() will wait the decode thread to finish. | |
2031 base::Bind(&V4L2VideoDecodeAccelerator::PictureCleared, | |
2032 base::Unretained(this))); | |
2033 picture_clearing_count_++; | |
2034 pending_picture_ready_.pop(); | |
2035 } else { | |
2036 // This picture is cleared. But some pictures are about to be cleared on | |
2037 // the child thread. To preserve the order, do not send this until those | |
2038 // pictures are cleared. | |
2039 break; | |
2040 } | |
2041 } | |
2042 } | |
2043 | |
2044 void V4L2VideoDecodeAccelerator::PictureCleared() { | |
2045 DVLOG(3) << "PictureCleared(). clearing count=" << picture_clearing_count_; | |
2046 DCHECK_EQ(decoder_thread_.message_loop(), base::MessageLoop::current()); | |
2047 DCHECK_GT(picture_clearing_count_, 0); | |
2048 picture_clearing_count_--; | |
2049 SendPictureReady(); | |
2050 } | |
2051 | |
2052 } // namespace content | |
OLD | NEW |