OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <linux/videodev2.h> | |
6 #include <sys/mman.h> | |
7 | |
8 #include "base/bind.h" | |
9 #include "base/thread_task_runner_handle.h" | |
10 #include "content/common/gpu/media/v4l2_jpeg_decode_accelerator.h" | |
11 | |
12 #define IOCTL_OR_ERROR_RETURN_VALUE(type, arg, value, type_name) \ | |
13 do { \ | |
14 if (device_->Ioctl(type, arg) != 0) { \ | |
15 PLOG(ERROR) << __func__ << "(): ioctl() failed: " << type_name; \ | |
16 return value; \ | |
17 } \ | |
18 } while (0) | |
19 | |
20 #define IOCTL_OR_ERROR_RETURN(type, arg) \ | |
21 IOCTL_OR_ERROR_RETURN_VALUE(type, arg, ((void)0), #type) | |
22 | |
23 #define IOCTL_OR_ERROR_RETURN_FALSE(type, arg) \ | |
24 IOCTL_OR_ERROR_RETURN_VALUE(type, arg, false, #type) | |
25 | |
26 #define IOCTL_OR_LOG_ERROR(type, arg) \ | |
27 do { \ | |
28 if (device_->Ioctl(type, arg) != 0) \ | |
29 PLOG(ERROR) << __func__ << "(): ioctl() failed: " << #type; \ | |
30 } while (0) | |
31 | |
32 namespace content { | |
33 | |
34 V4L2JpegDecodeAccelerator::BufferRecord::BufferRecord() | |
35 : address(nullptr), length(0), at_device(false) { | |
36 } | |
37 | |
38 V4L2JpegDecodeAccelerator::BufferRecord::~BufferRecord() { | |
39 } | |
40 | |
41 V4L2JpegDecodeAccelerator::JobRecord::JobRecord( | |
42 media::BitstreamBuffer bitstream_buffer, | |
43 scoped_refptr<media::VideoFrame> video_frame) | |
44 : bitstream_buffer(bitstream_buffer), out_frame(video_frame) { | |
45 } | |
46 | |
47 V4L2JpegDecodeAccelerator::JobRecord::~JobRecord() { | |
48 } | |
49 | |
50 V4L2JpegDecodeAccelerator::V4L2JpegDecodeAccelerator( | |
51 const scoped_refptr<V4L2Device>& device, | |
52 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) | |
53 : recreate_input_buffers_pending_(false), | |
54 recreate_output_buffers_pending_(false), | |
55 child_task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
56 io_task_runner_(io_task_runner), | |
57 client_(nullptr), | |
58 device_(device), | |
59 decoder_thread_("V4L2JpegDecodeThread"), | |
60 device_poll_thread_("V4L2JpegDecodeDevicePollThread"), | |
61 input_streamon_(false), | |
62 output_streamon_(false), | |
63 weak_factory_(this) { | |
64 } | |
65 | |
66 V4L2JpegDecodeAccelerator::~V4L2JpegDecodeAccelerator() { | |
67 DCHECK(child_task_runner_->BelongsToCurrentThread()); | |
68 | |
69 if (decoder_thread_.IsRunning()) { | |
70 decoder_task_runner_->PostTask( | |
71 FROM_HERE, base::Bind(&V4L2JpegDecodeAccelerator::DestroyTask, | |
72 base::Unretained(this))); | |
73 decoder_thread_.Stop(); | |
74 } | |
75 weak_factory_.InvalidateWeakPtrs(); | |
76 DCHECK(!device_poll_thread_.IsRunning()); | |
77 } | |
78 | |
79 void V4L2JpegDecodeAccelerator::DestroyTask() { | |
80 DCHECK(decoder_task_runner_->BelongsToCurrentThread()); | |
81 while (!input_jobs_.empty()) input_jobs_.pop(); | |
82 while (!running_jobs_.empty()) running_jobs_.pop(); | |
83 | |
84 // Stop streaming and the device_poll_thread_. | |
85 StopDevicePoll(); | |
86 | |
87 DestroyInputBuffers(); | |
88 DestroyOutputBuffers(); | |
89 } | |
90 | |
91 void V4L2JpegDecodeAccelerator::NotifyError(int32_t bitstream_buffer_id, | |
92 Error error) { | |
93 DCHECK(child_task_runner_->BelongsToCurrentThread()); | |
94 LOG(ERROR) << "Notifying of error " << error << " for buffer id " | |
95 << bitstream_buffer_id; | |
96 client_->NotifyError(bitstream_buffer_id, error); | |
97 } | |
98 | |
99 void V4L2JpegDecodeAccelerator::PostNotifyError( | |
100 int32_t bitstream_buffer_id, | |
101 Error error) { | |
102 child_task_runner_->PostTask( | |
103 FROM_HERE, | |
104 base::Bind(&V4L2JpegDecodeAccelerator::NotifyError, | |
105 weak_factory_.GetWeakPtr(), bitstream_buffer_id, error)); | |
106 } | |
107 | |
108 bool V4L2JpegDecodeAccelerator::Initialize(Client* client) { | |
109 DCHECK(child_task_runner_->BelongsToCurrentThread()); | |
110 | |
111 // Capabilities check. | |
112 struct v4l2_capability caps; | |
113 const __u32 kCapsRequired = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M; | |
114 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QUERYCAP, &caps); | |
115 if ((caps.capabilities & kCapsRequired) != kCapsRequired) { | |
116 LOG(ERROR) << "Initialize(): VIDIOC_QUERYCAP, caps check failed: 0x" | |
117 << std::hex << caps.capabilities; | |
118 return false; | |
119 } | |
120 | |
121 if (!decoder_thread_.Start()) { | |
122 LOG(ERROR) << "Initialize(): decoder thread failed to start"; | |
123 return false; | |
124 } | |
125 client_ = client; | |
126 decoder_task_runner_ = decoder_thread_.task_runner(); | |
127 | |
128 decoder_task_runner_->PostTask( | |
129 FROM_HERE, base::Bind(&V4L2JpegDecodeAccelerator::StartDevicePoll, | |
130 base::Unretained(this))); | |
131 | |
132 DVLOG(1) << "V4L2JpegDecodeAccelerator initialized."; | |
133 return true; | |
134 } | |
135 | |
136 void V4L2JpegDecodeAccelerator::Decode( | |
137 const media::BitstreamBuffer& bitstream_buffer, | |
138 const scoped_refptr<media::VideoFrame>& video_frame) { | |
139 DVLOG(1) << "Decode(): input_id=" << bitstream_buffer.id() | |
140 << ", size=" << bitstream_buffer.size(); | |
141 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
142 DCHECK_EQ(video_frame->format(), media::VideoFrame::I420); | |
143 | |
144 scoped_ptr<JobRecord> job_record( | |
145 new JobRecord(bitstream_buffer, video_frame)); | |
146 | |
147 decoder_task_runner_->PostTask( | |
148 FROM_HERE, base::Bind(&V4L2JpegDecodeAccelerator::DecodeTask, | |
149 base::Unretained(this), base::Passed(&job_record))); | |
150 } | |
151 | |
152 void V4L2JpegDecodeAccelerator::DecodeTask(scoped_ptr<JobRecord> job_record) { | |
153 DCHECK(decoder_task_runner_->BelongsToCurrentThread()); | |
154 job_record->shm.reset( | |
155 new base::SharedMemory(job_record->bitstream_buffer.handle(), true)); | |
156 if (!job_record->shm->Map(job_record->bitstream_buffer.size())) { | |
157 PLOG(ERROR) << "Decode(): could not map bitstream_buffer"; | |
158 PostNotifyError(job_record->bitstream_buffer.id(), UNREADABLE_INPUT); | |
159 return; | |
160 } | |
161 input_jobs_.push(make_linked_ptr(job_record.release())); | |
162 ServiceDeviceTask(); | |
163 } | |
164 | |
165 int V4L2JpegDecodeAccelerator::InputBufferQueuedCount() { | |
166 return input_buffer_map_.size() - free_input_buffers_.size(); | |
167 } | |
168 | |
169 int V4L2JpegDecodeAccelerator::OutputBufferQueuedCount() { | |
170 return output_buffer_map_.size() - free_output_buffers_.size(); | |
171 } | |
172 | |
173 bool V4L2JpegDecodeAccelerator::ShouldRecreateInputBuffers() { | |
174 DCHECK(decoder_task_runner_->BelongsToCurrentThread()); | |
175 if (input_jobs_.empty()) | |
176 return false; | |
177 | |
178 linked_ptr<JobRecord> job_record = input_jobs_.front(); | |
179 // Check input buffer size is enough | |
180 return (input_buffer_map_.empty() || | |
181 job_record->bitstream_buffer.size() > input_buffer_map_.front().length); | |
182 } | |
183 | |
184 bool V4L2JpegDecodeAccelerator::ShouldRecreateOutputBuffers() { | |
185 DCHECK(decoder_task_runner_->BelongsToCurrentThread()); | |
186 if (input_jobs_.empty()) | |
187 return false; | |
188 | |
189 linked_ptr<JobRecord> job_record = input_jobs_.front(); | |
190 // Check image resolution is the same as previous. | |
191 if (job_record->out_frame->coded_size() != image_coded_size_) { | |
192 size_t frame_size = media::VideoFrame::AllocationSize( | |
193 job_record->out_frame->format(), job_record->out_frame->coded_size()); | |
194 if (output_buffer_map_.empty() || | |
195 frame_size > output_buffer_map_.front().length) { | |
196 return true; | |
197 } | |
198 } | |
199 return false; | |
200 } | |
201 | |
202 bool V4L2JpegDecodeAccelerator::CreateBuffersIfNecessary() { | |
203 DVLOG(3) << __func__; | |
204 DCHECK(decoder_task_runner_->BelongsToCurrentThread()); | |
205 | |
206 recreate_input_buffers_pending_ = ShouldRecreateInputBuffers(); | |
207 recreate_output_buffers_pending_ = ShouldRecreateOutputBuffers(); | |
208 if (!recreate_input_buffers_pending_ && !recreate_output_buffers_pending_) | |
209 return true; | |
210 | |
211 // If queues are not empty, we should wait until pending frames finish. | |
212 if (InputBufferQueuedCount() || OutputBufferQueuedCount()) | |
213 return true; | |
214 | |
215 if (input_streamon_ || output_streamon_) { | |
216 if (!StopDevicePoll()) { | |
217 LOG(ERROR) << "Stop device poll thread failed when recreating buffers."; | |
218 return false; | |
219 } | |
220 if (recreate_input_buffers_pending_) | |
221 DestroyInputBuffers(); | |
222 | |
223 if (recreate_output_buffers_pending_) | |
224 DestroyOutputBuffers(); | |
225 } | |
226 | |
227 if (recreate_input_buffers_pending_ && !CreateInputBuffers()) { | |
228 LOG(ERROR) << "Create input buffers failed."; | |
229 return false; | |
230 } | |
231 if (recreate_output_buffers_pending_ && !CreateOutputBuffers()) { | |
232 LOG(ERROR) << "Create output buffers failed."; | |
233 return false; | |
234 } | |
235 | |
236 if (!device_poll_thread_.IsRunning()) | |
237 StartDevicePoll(); | |
238 return true; | |
239 } | |
240 | |
241 bool V4L2JpegDecodeAccelerator::CreateInputBuffers() { | |
242 DVLOG(3) << __func__; | |
243 DCHECK(decoder_task_runner_->BelongsToCurrentThread()); | |
244 DCHECK(!input_streamon_); | |
245 DCHECK(!input_jobs_.empty()); | |
246 linked_ptr<JobRecord> job_record = input_jobs_.front(); | |
247 // Reserve twice size to avoid recreating input buffer frequently. | |
248 size_t reserve_size = job_record->bitstream_buffer.size() * 2; | |
249 | |
250 struct v4l2_format format; | |
251 memset(&format, 0, sizeof(format)); | |
252 format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT; | |
253 format.fmt.pix.width = job_record->out_frame->coded_size().width(); | |
254 format.fmt.pix.height = job_record->out_frame->coded_size().height(); | |
255 format.fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG; | |
256 format.fmt.pix.sizeimage = reserve_size; | |
257 format.fmt.pix.field = V4L2_FIELD_ANY; | |
258 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_FMT, &format); | |
259 | |
260 struct v4l2_requestbuffers reqbufs; | |
261 memset(&reqbufs, 0, sizeof(reqbufs)); | |
262 reqbufs.count = kBufferCount; | |
263 reqbufs.type = V4L2_BUF_TYPE_VIDEO_OUTPUT; | |
264 reqbufs.memory = V4L2_MEMORY_MMAP; | |
265 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_REQBUFS, &reqbufs); | |
266 | |
267 DCHECK(input_buffer_map_.empty()); | |
268 input_buffer_map_.resize(reqbufs.count); | |
269 | |
270 for (size_t i = 0; i < input_buffer_map_.size(); ++i) { | |
271 free_input_buffers_.push_back(i); | |
272 | |
273 struct v4l2_buffer buffer; | |
274 memset(&buffer, 0, sizeof(buffer)); | |
275 buffer.index = i; | |
276 buffer.type = V4L2_BUF_TYPE_VIDEO_OUTPUT; | |
277 buffer.memory = V4L2_MEMORY_MMAP; | |
278 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QUERYBUF, &buffer); | |
279 void* address = device_->Mmap(NULL, buffer.length, PROT_READ | PROT_WRITE, | |
280 MAP_SHARED, buffer.m.offset); | |
281 if (address == MAP_FAILED) { | |
282 PLOG(ERROR) << "CreateInputBuffers(): mmap() failed"; | |
283 PostNotifyError(job_record->bitstream_buffer.id(), | |
284 media::JpegDecodeAccelerator::PLATFORM_FAILURE); | |
285 return false; | |
286 } | |
287 input_buffer_map_[i].address = address; | |
288 input_buffer_map_[i].length = buffer.length; | |
289 } | |
290 recreate_input_buffers_pending_ = false; | |
291 | |
292 return true; | |
293 } | |
294 | |
295 bool V4L2JpegDecodeAccelerator::CreateOutputBuffers() { | |
296 DVLOG(3) << __func__; | |
297 DCHECK(decoder_task_runner_->BelongsToCurrentThread()); | |
298 DCHECK(!output_streamon_); | |
299 DCHECK(!input_jobs_.empty()); | |
300 linked_ptr<JobRecord> job_record = input_jobs_.front(); | |
301 | |
302 size_t frame_size = media::VideoFrame::AllocationSize( | |
303 media::VideoFrame::I420, job_record->out_frame->coded_size()); | |
304 struct v4l2_format format; | |
305 memset(&format, 0, sizeof(format)); | |
306 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
307 format.fmt.pix.width = job_record->out_frame->coded_size().width(); | |
308 format.fmt.pix.height = job_record->out_frame->coded_size().height(); | |
309 format.fmt.pix.sizeimage = frame_size; | |
310 format.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420; | |
311 format.fmt.pix.field = V4L2_FIELD_ANY; | |
312 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_FMT, &format); | |
313 | |
314 struct v4l2_requestbuffers reqbufs; | |
315 memset(&reqbufs, 0, sizeof(reqbufs)); | |
316 reqbufs.count = kBufferCount; | |
317 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
318 reqbufs.memory = V4L2_MEMORY_MMAP; | |
319 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_REQBUFS, &reqbufs); | |
320 | |
321 DCHECK(output_buffer_map_.empty()); | |
322 output_buffer_map_.resize(reqbufs.count); | |
323 | |
324 for (size_t i = 0; i < output_buffer_map_.size(); ++i) { | |
325 free_output_buffers_.push_back(i); | |
326 | |
327 struct v4l2_buffer buffer; | |
328 memset(&buffer, 0, sizeof(buffer)); | |
329 buffer.index = i; | |
330 buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
331 buffer.memory = V4L2_MEMORY_MMAP; | |
332 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QUERYBUF, &buffer); | |
333 void* address = device_->Mmap(NULL, buffer.length, PROT_READ | PROT_WRITE, | |
334 MAP_SHARED, buffer.m.offset); | |
335 if (address == MAP_FAILED) { | |
336 PLOG(ERROR) << "CreateOutputBuffers(): mmap() failed"; | |
337 PostNotifyError(job_record->bitstream_buffer.id(), | |
338 media::JpegDecodeAccelerator::PLATFORM_FAILURE); | |
339 return false; | |
340 } | |
341 output_buffer_map_[i].address = address; | |
342 output_buffer_map_[i].length = buffer.length; | |
343 } | |
344 image_coded_size_ = job_record->out_frame->coded_size(); | |
345 recreate_output_buffers_pending_ = false; | |
346 | |
347 return true; | |
348 } | |
349 | |
350 void V4L2JpegDecodeAccelerator::DestroyInputBuffers() { | |
351 DCHECK(decoder_task_runner_->BelongsToCurrentThread()); | |
352 DCHECK(!input_streamon_); | |
353 | |
354 for (size_t buf = 0; buf < input_buffer_map_.size(); ++buf) { | |
355 BufferRecord& input_record = input_buffer_map_[buf]; | |
356 device_->Munmap(input_record.address, input_record.length); | |
357 } | |
358 | |
359 struct v4l2_requestbuffers reqbufs; | |
360 memset(&reqbufs, 0, sizeof(reqbufs)); | |
361 reqbufs.count = 0; | |
362 reqbufs.type = V4L2_BUF_TYPE_VIDEO_OUTPUT; | |
363 reqbufs.memory = V4L2_MEMORY_MMAP; | |
364 IOCTL_OR_LOG_ERROR(VIDIOC_REQBUFS, &reqbufs); | |
365 | |
366 input_buffer_map_.clear(); | |
367 free_input_buffers_.clear(); | |
368 } | |
369 | |
370 void V4L2JpegDecodeAccelerator::DestroyOutputBuffers() { | |
371 DCHECK(decoder_task_runner_->BelongsToCurrentThread()); | |
372 DCHECK(!output_streamon_); | |
373 | |
374 for (size_t buf = 0; buf < output_buffer_map_.size(); ++buf) { | |
375 BufferRecord& output_record = output_buffer_map_[buf]; | |
376 device_->Munmap(output_record.address, output_record.length); | |
377 } | |
378 | |
379 struct v4l2_requestbuffers reqbufs; | |
380 memset(&reqbufs, 0, sizeof(reqbufs)); | |
381 reqbufs.count = 0; | |
382 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
383 reqbufs.memory = V4L2_MEMORY_MMAP; | |
384 IOCTL_OR_LOG_ERROR(VIDIOC_REQBUFS, &reqbufs); | |
385 | |
386 output_buffer_map_.clear(); | |
387 free_output_buffers_.clear(); | |
388 } | |
389 | |
390 void V4L2JpegDecodeAccelerator::DevicePollTask() { | |
391 DCHECK(device_poll_task_runner_->BelongsToCurrentThread()); | |
392 | |
393 bool event_pending; | |
394 if (!device_->Poll(true, &event_pending)) { | |
395 PostNotifyError(-1, media::JpegDecodeAccelerator::PLATFORM_FAILURE); | |
396 return; | |
397 } | |
398 | |
399 // All processing should happen on ServiceDeviceTask(), since we shouldn't | |
400 // touch decoder state from this thread. | |
401 decoder_task_runner_->PostTask( | |
402 FROM_HERE, base::Bind(&V4L2JpegDecodeAccelerator::ServiceDeviceTask, | |
403 base::Unretained(this))); | |
404 } | |
405 | |
406 void V4L2JpegDecodeAccelerator::ServiceDeviceTask() { | |
407 DCHECK(decoder_task_runner_->BelongsToCurrentThread()); | |
408 // ServiceDeviceTask() should only ever be scheduled from DevicePollTask(), | |
409 // so either: | |
410 // * device_poll_thread_ is running normally | |
411 // * device_poll_thread_ scheduled us, but then a DestroyTask() shut it down, | |
412 // in which case we should early-out. | |
413 if (!device_poll_thread_.IsRunning()) | |
414 return; | |
415 | |
416 if (InputBufferQueuedCount() > 0 || OutputBufferQueuedCount() > 0) | |
417 Dequeue(); | |
418 if (!CreateBuffersIfNecessary()) return; | |
419 | |
420 // Stop enqueue input record when input/output buffers are required to | |
wuchengli
2015/06/26 23:42:48
s/Stop enqueue/Do not queue/
"Stop enqueuing" mea
henryhsu
2015/06/29 05:20:52
Done.
| |
421 // re-create until all pending frames are handled by device. | |
422 // Output record can be enqueued in this case since the number of | |
wuchengli
2015/06/26 23:42:48
This is not the reason why output can be queued. T
henryhsu
2015/06/29 05:20:52
Done.
| |
423 // enqueued output record may be smaller than the number of enqueued | |
424 // input record. | |
425 if (!recreate_input_buffers_pending_ && !recreate_output_buffers_pending_) | |
426 EnqueueInput(); | |
427 EnqueueOutput(); | |
428 | |
429 if (!device_->ClearDevicePollInterrupt()) return; | |
430 | |
431 if (InputBufferQueuedCount() > 0 || OutputBufferQueuedCount() > 0) { | |
432 device_poll_task_runner_->PostTask( | |
433 FROM_HERE, base::Bind(&V4L2JpegDecodeAccelerator::DevicePollTask, | |
434 base::Unretained(this))); | |
435 } | |
436 | |
437 DVLOG(2) << __func__ << ": buffer counts: INPUT[" | |
438 << input_jobs_.size() << "] => DEVICE[" | |
439 << free_input_buffers_.size() << "+" | |
440 << InputBufferQueuedCount() << "/" | |
441 << input_buffer_map_.size() << "->" | |
442 << free_output_buffers_.size() << "+" | |
443 << OutputBufferQueuedCount() << "/" | |
444 << output_buffer_map_.size() << "]"; | |
445 } | |
446 | |
447 void V4L2JpegDecodeAccelerator::EnqueueInput() { | |
448 DCHECK(decoder_task_runner_->BelongsToCurrentThread()); | |
449 while (!input_jobs_.empty() && !free_input_buffers_.empty()) { | |
450 if (!EnqueueInputRecord()) | |
451 return; | |
452 } | |
453 if (!input_streamon_ && InputBufferQueuedCount()) { | |
wuchengli
2015/06/26 23:42:48
Why this part is moved back here?
henryhsu
2015/06/29 03:35:02
Pawel said it depends on different kernel. This pa
| |
454 __u32 type = V4L2_BUF_TYPE_VIDEO_OUTPUT; | |
455 IOCTL_OR_ERROR_RETURN(VIDIOC_STREAMON, &type); | |
456 input_streamon_ = true; | |
457 } | |
458 } | |
459 | |
460 void V4L2JpegDecodeAccelerator::EnqueueOutput() { | |
461 DCHECK(decoder_task_runner_->BelongsToCurrentThread()); | |
462 while (!free_output_buffers_.empty()) { | |
463 if (!EnqueueOutputRecord()) | |
464 return; | |
465 } | |
466 if (!output_streamon_ && OutputBufferQueuedCount()) { | |
467 __u32 type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
468 IOCTL_OR_ERROR_RETURN(VIDIOC_STREAMON, &type); | |
469 output_streamon_ = true; | |
470 } | |
471 } | |
472 | |
473 void V4L2JpegDecodeAccelerator::Dequeue() { | |
474 DCHECK(decoder_task_runner_->BelongsToCurrentThread()); | |
475 | |
476 // Dequeue completed input (VIDEO_OUTPUT) buffers, | |
477 // and recycle to the free list. | |
478 struct v4l2_buffer dqbuf; | |
479 while (InputBufferQueuedCount() > 0) { | |
480 DCHECK(input_streamon_); | |
481 memset(&dqbuf, 0, sizeof(dqbuf)); | |
482 dqbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT; | |
483 dqbuf.memory = V4L2_MEMORY_MMAP; | |
484 if (device_->Ioctl(VIDIOC_DQBUF, &dqbuf) != 0) { | |
485 if (errno == EAGAIN) { | |
486 // EAGAIN if we're just out of buffers to dequeue. | |
487 break; | |
488 } | |
489 PLOG(ERROR) << "ioctl() failed: VIDIOC_DQBUF"; | |
490 PostNotifyError(dqbuf.index, | |
491 media::JpegDecodeAccelerator::PLATFORM_FAILURE); | |
492 return; | |
493 } | |
494 BufferRecord& input_record = input_buffer_map_[dqbuf.index]; | |
495 DCHECK(input_record.at_device); | |
496 input_record.at_device = false; | |
497 free_input_buffers_.push_back(dqbuf.index); | |
498 } | |
499 | |
500 // Dequeue completed output (VIDEO_CAPTURE) buffers, recycle to the free list. | |
501 // Return the finished buffer to the client via the job ready callback. | |
502 while (OutputBufferQueuedCount() > 0) { | |
503 DCHECK(output_streamon_); | |
504 memset(&dqbuf, 0, sizeof(dqbuf)); | |
505 dqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
506 dqbuf.memory = V4L2_MEMORY_MMAP; | |
507 if (device_->Ioctl(VIDIOC_DQBUF, &dqbuf) != 0) { | |
508 if (errno == EAGAIN) { | |
509 // EAGAIN if we're just out of buffers to dequeue. | |
510 break; | |
511 } | |
512 PLOG(ERROR) << "ioctl() failed: VIDIOC_DQBUF"; | |
513 PostNotifyError(dqbuf.index, | |
514 media::JpegDecodeAccelerator::PLATFORM_FAILURE); | |
515 return; | |
516 } | |
517 BufferRecord& output_record = output_buffer_map_[dqbuf.index]; | |
518 DCHECK(output_record.at_device); | |
519 output_record.at_device = false; | |
520 free_output_buffers_.push_back(dqbuf.index); | |
521 | |
522 // Jobs are always processed in FIFO order. | |
523 DCHECK(!running_jobs_.empty()); | |
524 linked_ptr<JobRecord> job_record = running_jobs_.front(); | |
525 running_jobs_.pop(); | |
526 | |
527 memcpy(job_record->out_frame->data(media::VideoFrame::kYPlane), | |
528 output_record.address, output_record.length); | |
529 | |
530 DVLOG(3) << "Decoding finished, returning frame, id=" | |
531 << job_record->bitstream_buffer.id(); | |
532 | |
533 client_->VideoFrameReady(job_record->bitstream_buffer.id()); | |
534 } | |
535 } | |
536 | |
537 bool V4L2JpegDecodeAccelerator::EnqueueInputRecord() { | |
538 DCHECK(!input_jobs_.empty()); | |
539 DCHECK(!free_input_buffers_.empty()); | |
540 | |
541 // Enqueue an input (VIDEO_OUTPUT) buffer for an input video frame. | |
542 linked_ptr<JobRecord> job_record = input_jobs_.front(); | |
543 input_jobs_.pop(); | |
544 const int index = free_input_buffers_.back(); | |
545 BufferRecord& input_record = input_buffer_map_[index]; | |
546 DCHECK(!input_record.at_device); | |
547 | |
548 struct v4l2_buffer qbuf; | |
549 memset(&qbuf, 0, sizeof(qbuf)); | |
550 memcpy(input_record.address, job_record->shm->memory(), | |
551 job_record->bitstream_buffer.size()); | |
552 qbuf.index = index; | |
553 qbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT; | |
554 qbuf.memory = V4L2_MEMORY_MMAP; | |
555 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QBUF, &qbuf); | |
556 input_record.at_device = true; | |
557 running_jobs_.push(job_record); | |
558 free_input_buffers_.pop_back(); | |
559 | |
560 DVLOG(3) << __func__ << ": enqueued frame id=" | |
561 << job_record->bitstream_buffer.id() << " to device."; | |
562 return true; | |
563 } | |
564 | |
565 bool V4L2JpegDecodeAccelerator::EnqueueOutputRecord() { | |
566 DCHECK(!free_output_buffers_.empty()); | |
567 | |
568 // Enqueue an output (VIDEO_CAPTURE) buffer. | |
569 const int index = free_output_buffers_.back(); | |
570 BufferRecord& output_record = output_buffer_map_[index]; | |
571 DCHECK(!output_record.at_device); | |
572 struct v4l2_buffer qbuf; | |
573 memset(&qbuf, 0, sizeof(qbuf)); | |
574 qbuf.index = index; | |
575 qbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
576 qbuf.memory = V4L2_MEMORY_MMAP; | |
577 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QBUF, &qbuf); | |
578 output_record.at_device = true; | |
579 free_output_buffers_.pop_back(); | |
580 return true; | |
581 } | |
582 | |
583 void V4L2JpegDecodeAccelerator::StartDevicePoll() { | |
584 DVLOG(3) << __func__ << ": starting device poll"; | |
585 DCHECK(decoder_task_runner_->BelongsToCurrentThread()); | |
586 DCHECK(!device_poll_thread_.IsRunning()); | |
587 | |
588 if (!device_poll_thread_.Start()) { | |
589 LOG(ERROR) << "StartDevicePoll(): Device thread failed to start"; | |
590 PostNotifyError(-1, media::JpegDecodeAccelerator::PLATFORM_FAILURE); | |
591 return; | |
592 } | |
593 device_poll_task_runner_ = device_poll_thread_.task_runner(); | |
594 } | |
595 | |
596 bool V4L2JpegDecodeAccelerator::StopDevicePoll() { | |
597 DVLOG(3) << __func__ << ": stopping device poll"; | |
598 if (decoder_thread_.IsRunning()) | |
599 DCHECK(decoder_task_runner_->BelongsToCurrentThread()); | |
600 | |
601 // Signal the DevicePollTask() to stop, and stop the device poll thread. | |
602 if (!device_->SetDevicePollInterrupt()) { | |
603 LOG(ERROR) << "StopDevicePoll(): SetDevicePollInterrupt failed."; | |
604 PostNotifyError(-1, media::JpegDecodeAccelerator::PLATFORM_FAILURE); | |
605 return false; | |
606 } | |
607 | |
608 device_poll_thread_.Stop(); | |
609 | |
610 // Clear the interrupt now, to be sure. | |
611 if (!device_->ClearDevicePollInterrupt()) | |
612 return false; | |
613 | |
614 if (input_streamon_) { | |
615 __u32 type = V4L2_BUF_TYPE_VIDEO_OUTPUT; | |
616 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_STREAMOFF, &type); | |
617 input_streamon_ = false; | |
618 } | |
619 | |
620 if (output_streamon_) { | |
621 __u32 type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
622 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_STREAMOFF, &type); | |
623 output_streamon_ = false; | |
624 } | |
625 | |
626 free_input_buffers_.clear(); | |
627 for (size_t i = 0; i < input_buffer_map_.size(); ++i) { | |
628 BufferRecord& input_record = input_buffer_map_[i]; | |
629 input_record.at_device = false; | |
630 free_input_buffers_.push_back(i); | |
631 } | |
632 | |
633 free_output_buffers_.clear(); | |
634 for (size_t i = 0; i < output_buffer_map_.size(); ++i) { | |
635 BufferRecord& output_record = output_buffer_map_[i]; | |
636 output_record.at_device = false; | |
637 free_output_buffers_.push_back(i); | |
638 } | |
639 | |
640 return true; | |
641 } | |
642 | |
643 } // namespace content | |
OLD | NEW |