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