OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/capture/video/linux/v4l2_capture_delegate.h" | 5 #include "media/capture/video/linux/v4l2_capture_delegate.h" |
6 | 6 |
7 #include <poll.h> | 7 #include <poll.h> |
8 #include <sys/fcntl.h> | 8 #include <sys/fcntl.h> |
9 #include <sys/ioctl.h> | 9 #include <sys/ioctl.h> |
10 #include <sys/mman.h> | 10 #include <sys/mman.h> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/files/file_enumerator.h" | 14 #include "base/files/file_enumerator.h" |
15 #include "base/posix/eintr_wrapper.h" | 15 #include "base/posix/eintr_wrapper.h" |
16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
18 #include "media/base/bind_to_current_loop.h" | 18 #include "media/base/bind_to_current_loop.h" |
19 #include "media/capture/video/linux/video_capture_device_linux.h" | 19 #include "media/capture/video/linux/video_capture_device_linux.h" |
| 20 #include "third_party/libyuv/include/libyuv.h" |
| 21 #include "third_party/skia/include/core/SkImage.h" |
| 22 #include "ui/gfx/codec/png_codec.h" |
20 | 23 |
21 namespace media { | 24 namespace media { |
22 | 25 |
23 // Desired number of video buffers to allocate. The actual number of allocated | 26 // Desired number of video buffers to allocate. The actual number of allocated |
24 // buffers by v4l2 driver can be higher or lower than this number. | 27 // buffers by v4l2 driver can be higher or lower than this number. |
25 // kNumVideoBuffers should not be too small, or Chrome may not return enough | 28 // kNumVideoBuffers should not be too small, or Chrome may not return enough |
26 // buffers back to driver in time. | 29 // buffers back to driver in time. |
27 const uint32_t kNumVideoBuffers = 4; | 30 const uint32_t kNumVideoBuffers = 4; |
28 // Timeout in milliseconds v4l2_thread_ blocks waiting for a frame from the hw. | 31 // Timeout in milliseconds v4l2_thread_ blocks waiting for a frame from the hw. |
29 // This value has been fine tuned. Before changing or modifying it see | 32 // This value has been fine tuned. Before changing or modifying it see |
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 if (HANDLE_EINTR(ioctl(device_fd_.get(), VIDIOC_REQBUFS, &r_buffer)) < 0) | 317 if (HANDLE_EINTR(ioctl(device_fd_.get(), VIDIOC_REQBUFS, &r_buffer)) < 0) |
315 SetErrorState(FROM_HERE, "Failed to VIDIOC_REQBUFS with count = 0"); | 318 SetErrorState(FROM_HERE, "Failed to VIDIOC_REQBUFS with count = 0"); |
316 | 319 |
317 // At this point we can close the device. | 320 // At this point we can close the device. |
318 // This is also needed for correctly changing settings later via VIDIOC_S_FMT. | 321 // This is also needed for correctly changing settings later via VIDIOC_S_FMT. |
319 device_fd_.reset(); | 322 device_fd_.reset(); |
320 is_capturing_ = false; | 323 is_capturing_ = false; |
321 client_.reset(); | 324 client_.reset(); |
322 } | 325 } |
323 | 326 |
| 327 void V4L2CaptureDelegate::TakePhoto( |
| 328 VideoCaptureDevice::TakePhotoCallback callback) { |
| 329 DCHECK(v4l2_task_runner_->BelongsToCurrentThread()); |
| 330 take_photo_callbacks_.push(std::move(callback)); |
| 331 } |
| 332 |
324 void V4L2CaptureDelegate::SetRotation(int rotation) { | 333 void V4L2CaptureDelegate::SetRotation(int rotation) { |
325 DCHECK(v4l2_task_runner_->BelongsToCurrentThread()); | 334 DCHECK(v4l2_task_runner_->BelongsToCurrentThread()); |
326 DCHECK(rotation >= 0 && rotation < 360 && rotation % 90 == 0); | 335 DCHECK(rotation >= 0 && rotation < 360 && rotation % 90 == 0); |
327 rotation_ = rotation; | 336 rotation_ = rotation; |
328 } | 337 } |
329 | 338 |
330 V4L2CaptureDelegate::~V4L2CaptureDelegate() {} | 339 V4L2CaptureDelegate::~V4L2CaptureDelegate() {} |
331 | 340 |
332 bool V4L2CaptureDelegate::MapAndQueueBuffer(int index) { | 341 bool V4L2CaptureDelegate::MapAndQueueBuffer(int index) { |
333 v4l2_buffer buffer; | 342 v4l2_buffer buffer; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 const scoped_refptr<BufferTracker>& buffer_tracker = | 403 const scoped_refptr<BufferTracker>& buffer_tracker = |
395 buffer_tracker_pool_[buffer.index]; | 404 buffer_tracker_pool_[buffer.index]; |
396 | 405 |
397 base::TimeDelta timestamp = | 406 base::TimeDelta timestamp = |
398 base::TimeDelta::FromSeconds(buffer.timestamp.tv_sec) + | 407 base::TimeDelta::FromSeconds(buffer.timestamp.tv_sec) + |
399 base::TimeDelta::FromMicroseconds(buffer.timestamp.tv_usec); | 408 base::TimeDelta::FromMicroseconds(buffer.timestamp.tv_usec); |
400 client_->OnIncomingCapturedData( | 409 client_->OnIncomingCapturedData( |
401 buffer_tracker->start(), buffer_tracker->payload_size(), | 410 buffer_tracker->start(), buffer_tracker->payload_size(), |
402 capture_format_, rotation_, base::TimeTicks::Now(), timestamp); | 411 capture_format_, rotation_, base::TimeTicks::Now(), timestamp); |
403 | 412 |
| 413 while (!take_photo_callbacks_.empty()) { |
| 414 VideoCaptureDevice::TakePhotoCallback cb = |
| 415 std::move(take_photo_callbacks_.front()); |
| 416 take_photo_callbacks_.pop(); |
| 417 |
| 418 mojom::BlobPtr blob = GetPhotoBlob(buffer_tracker, buffer.bytesused); |
| 419 if (blob) |
| 420 cb.Run(std::move(blob)); |
| 421 } |
| 422 |
404 if (HANDLE_EINTR(ioctl(device_fd_.get(), VIDIOC_QBUF, &buffer)) < 0) { | 423 if (HANDLE_EINTR(ioctl(device_fd_.get(), VIDIOC_QBUF, &buffer)) < 0) { |
405 SetErrorState(FROM_HERE, "Failed to enqueue capture buffer"); | 424 SetErrorState(FROM_HERE, "Failed to enqueue capture buffer"); |
406 return; | 425 return; |
407 } | 426 } |
408 } | 427 } |
409 | 428 |
410 v4l2_task_runner_->PostTask( | 429 v4l2_task_runner_->PostTask( |
411 FROM_HERE, base::Bind(&V4L2CaptureDelegate::DoCapture, this)); | 430 FROM_HERE, base::Bind(&V4L2CaptureDelegate::DoCapture, this)); |
412 } | 431 } |
413 | 432 |
| 433 mojom::BlobPtr V4L2CaptureDelegate::GetPhotoBlob( |
| 434 const scoped_refptr<BufferTracker>& buffer_tracker, |
| 435 const uint32_t bytesused) { |
| 436 uint32 src_format; |
| 437 |
| 438 if (capture_format_.pixel_format == VideoPixelFormat::PIXEL_FORMAT_MJPEG) { |
| 439 mojom::BlobPtr blob = mojom::Blob::New(); |
| 440 blob->data.resize(bytesused); |
| 441 memcpy(blob->data.data(), buffer_tracker->start(), bytesused); |
| 442 blob->mime_type = "image/jpeg"; |
| 443 return blob; |
| 444 } else if (capture_format_.pixel_format == |
| 445 VideoPixelFormat::PIXEL_FORMAT_UYVY) { |
| 446 src_format = libyuv::FOURCC_UYVY; |
| 447 } else if (capture_format_.pixel_format == |
| 448 VideoPixelFormat::PIXEL_FORMAT_YUY2) { |
| 449 src_format = libyuv::FOURCC_YUY2; |
| 450 } else if (capture_format_.pixel_format == |
| 451 VideoPixelFormat::PIXEL_FORMAT_I420) { |
| 452 src_format = libyuv::FOURCC_I420; |
| 453 } else if (capture_format_.pixel_format == |
| 454 VideoPixelFormat::PIXEL_FORMAT_RGB24) { |
| 455 src_format = libyuv::FOURCC_24BG; |
| 456 } else { |
| 457 return nullptr; |
| 458 } |
| 459 |
| 460 std::unique_ptr<uint8_t[]> dst_argb(new uint8_t[VideoFrame::AllocationSize( |
| 461 PIXEL_FORMAT_ARGB, capture_format_.frame_size)]); |
| 462 if (ConvertToARGB(buffer_tracker->start(), bytesused, dst_argb.get(), |
| 463 capture_format_.frame_size.width() * 4, 0 /* crop_x_pos */, |
| 464 0 /* crop_y_pos */, capture_format_.frame_size.width(), |
| 465 capture_format_.frame_size.height(), |
| 466 capture_format_.frame_size.width(), |
| 467 capture_format_.frame_size.height(), |
| 468 libyuv::RotationMode::kRotate0, src_format) != 0) { |
| 469 return nullptr; |
| 470 } |
| 471 |
| 472 mojom::BlobPtr blob = mojom::Blob::New(); |
| 473 const gfx::PNGCodec::ColorFormat codec_color_format = |
| 474 (kN32_SkColorType == kRGBA_8888_SkColorType) ? gfx::PNGCodec::FORMAT_RGBA |
| 475 : gfx::PNGCodec::FORMAT_BGRA; |
| 476 const bool result = gfx::PNGCodec::Encode( |
| 477 dst_argb.get(), codec_color_format, capture_format_.frame_size, |
| 478 capture_format_.frame_size.width() * 4, true /* discard_transparency */, |
| 479 std::vector<gfx::PNGCodec::Comment>(), &blob->data); |
| 480 DCHECK(result); |
| 481 |
| 482 blob->mime_type = "image/png"; |
| 483 return blob; |
| 484 } |
| 485 |
414 void V4L2CaptureDelegate::SetErrorState( | 486 void V4L2CaptureDelegate::SetErrorState( |
415 const tracked_objects::Location& from_here, | 487 const tracked_objects::Location& from_here, |
416 const std::string& reason) { | 488 const std::string& reason) { |
417 DCHECK(v4l2_task_runner_->BelongsToCurrentThread()); | 489 DCHECK(v4l2_task_runner_->BelongsToCurrentThread()); |
418 is_capturing_ = false; | 490 is_capturing_ = false; |
419 client_->OnError(from_here, reason); | 491 client_->OnError(from_here, reason); |
420 } | 492 } |
421 | 493 |
422 V4L2CaptureDelegate::BufferTracker::BufferTracker() {} | 494 V4L2CaptureDelegate::BufferTracker::BufferTracker() {} |
423 | 495 |
(...skipping 14 matching lines...) Expand all Loading... |
438 DLOG(ERROR) << "Error mmap()ing a V4L2 buffer into userspace"; | 510 DLOG(ERROR) << "Error mmap()ing a V4L2 buffer into userspace"; |
439 return false; | 511 return false; |
440 } | 512 } |
441 start_ = static_cast<uint8_t*>(start); | 513 start_ = static_cast<uint8_t*>(start); |
442 length_ = buffer.length; | 514 length_ = buffer.length; |
443 payload_size_ = 0; | 515 payload_size_ = 0; |
444 return true; | 516 return true; |
445 } | 517 } |
446 | 518 |
447 } // namespace media | 519 } // namespace media |
OLD | NEW |