Chromium Code Reviews| 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/video_capture_device_client.h" | 5 #include "media/capture/video/video_capture_device_client.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "base/trace_event/trace_event.h" | 15 #include "base/trace_event/trace_event.h" |
| 16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 17 #include "media/base/bind_to_current_loop.h" | 17 #include "media/base/bind_to_current_loop.h" |
| 18 #include "media/base/video_frame.h" | 18 #include "media/base/video_frame.h" |
| 19 #include "media/capture/video/video_capture_buffer_handle.h" | 19 #include "media/capture/video/video_capture_buffer_handle.h" |
| 20 #include "media/capture/video/video_capture_buffer_pool.h" | 20 #include "media/capture/video/video_capture_buffer_pool.h" |
| 21 #include "media/capture/video/video_capture_jpeg_decoder.h" | 21 #include "media/capture/video/video_capture_jpeg_decoder.h" |
| 22 #include "media/capture/video/video_frame_receiver.h" | 22 #include "media/capture/video/video_frame_receiver.h" |
| 23 #include "media/capture/video_capture_types.h" | 23 #include "media/capture/video_capture_types.h" |
| 24 #include "mojo/public/cpp/system/platform_handle.h" | |
| 25 #include "third_party/libyuv/include/libyuv.h" | 24 #include "third_party/libyuv/include/libyuv.h" |
| 26 | 25 |
| 27 using media::VideoCaptureFormat; | 26 using media::VideoCaptureFormat; |
| 28 using media::VideoFrame; | 27 using media::VideoFrame; |
| 29 using media::VideoFrameMetadata; | 28 using media::VideoFrameMetadata; |
| 30 | 29 |
| 31 namespace { | 30 namespace { |
| 32 | 31 |
| 33 bool IsFormatSupported(media::VideoPixelFormat pixel_format) { | 32 bool IsFormatSupported(media::VideoPixelFormat pixel_format) { |
| 34 return (pixel_format == media::PIXEL_FORMAT_I420 || | 33 return (pixel_format == media::PIXEL_FORMAT_I420 || |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 48 | 47 |
| 49 ~BufferPoolProducerReservationReleaser() override { | 48 ~BufferPoolProducerReservationReleaser() override { |
| 50 buffer_pool_->RelinquishProducerReservation(buffer_id_); | 49 buffer_pool_->RelinquishProducerReservation(buffer_id_); |
| 51 } | 50 } |
| 52 | 51 |
| 53 private: | 52 private: |
| 54 const scoped_refptr<VideoCaptureBufferPool> buffer_pool_; | 53 const scoped_refptr<VideoCaptureBufferPool> buffer_pool_; |
| 55 const int buffer_id_; | 54 const int buffer_id_; |
| 56 }; | 55 }; |
| 57 | 56 |
| 57 class BufferPoolConsumerReservationReleaser | |
|
mcasas
2017/02/16 22:35:56
This name is a bit large and from what it does
it'
chfremer
2017/02/16 23:37:57
Done.
| |
| 58 : public VideoCaptureDevice::Client::Buffer::ScopedAccessPermission { | |
| 59 public: | |
| 60 BufferPoolConsumerReservationReleaser( | |
| 61 scoped_refptr<VideoCaptureBufferPool> buffer_pool, | |
| 62 int buffer_id) | |
| 63 : buffer_pool_(std::move(buffer_pool)), buffer_id_(buffer_id) {} | |
| 64 | |
| 65 ~BufferPoolConsumerReservationReleaser() override { | |
| 66 buffer_pool_->RelinquishConsumerHold(buffer_id_, 1); | |
| 67 } | |
| 68 | |
| 69 private: | |
| 70 const scoped_refptr<VideoCaptureBufferPool> buffer_pool_; | |
| 71 const int buffer_id_; | |
| 72 }; | |
| 73 | |
| 58 class BufferPoolBufferHandleProvider | 74 class BufferPoolBufferHandleProvider |
| 59 : public VideoCaptureDevice::Client::Buffer::HandleProvider { | 75 : public VideoCaptureDevice::Client::Buffer::HandleProvider { |
| 60 public: | 76 public: |
| 61 BufferPoolBufferHandleProvider( | 77 BufferPoolBufferHandleProvider( |
| 62 scoped_refptr<VideoCaptureBufferPool> buffer_pool, | 78 scoped_refptr<VideoCaptureBufferPool> buffer_pool, |
| 63 int buffer_id) | 79 int buffer_id) |
| 64 : buffer_pool_(std::move(buffer_pool)), buffer_id_(buffer_id) {} | 80 : buffer_pool_(std::move(buffer_pool)), buffer_id_(buffer_id) {} |
| 65 | 81 |
| 66 // Implementation of HandleProvider: | 82 // Implementation of HandleProvider: |
| 67 mojo::ScopedSharedBufferHandle GetHandleForInterProcessTransit() override { | 83 mojo::ScopedSharedBufferHandle GetHandleForInterProcessTransit() override { |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 172 media::PIXEL_STORAGE_CPU, frame_feedback_id); | 188 media::PIXEL_STORAGE_CPU, frame_feedback_id); |
| 173 #if DCHECK_IS_ON() | 189 #if DCHECK_IS_ON() |
| 174 dropped_frame_counter_ = buffer.is_valid() ? 0 : dropped_frame_counter_ + 1; | 190 dropped_frame_counter_ = buffer.is_valid() ? 0 : dropped_frame_counter_ + 1; |
| 175 if (dropped_frame_counter_ >= kMaxDroppedFrames) | 191 if (dropped_frame_counter_ >= kMaxDroppedFrames) |
| 176 OnError(FROM_HERE, "Too many frames dropped"); | 192 OnError(FROM_HERE, "Too many frames dropped"); |
| 177 #endif | 193 #endif |
| 178 // Failed to reserve I420 output buffer, so drop the frame. | 194 // Failed to reserve I420 output buffer, so drop the frame. |
| 179 if (!buffer.is_valid()) | 195 if (!buffer.is_valid()) |
| 180 return; | 196 return; |
| 181 | 197 |
| 182 auto buffer_access = buffer.handle_provider()->GetHandleForInProcessAccess(); | 198 auto buffer_access = buffer.handle_provider->GetHandleForInProcessAccess(); |
| 183 uint8_t *y_plane_data, *u_plane_data, *v_plane_data; | 199 uint8_t *y_plane_data, *u_plane_data, *v_plane_data; |
| 184 InitializeI420PlanePointers(dimensions, buffer_access->data(), &y_plane_data, | 200 InitializeI420PlanePointers(dimensions, buffer_access->data(), &y_plane_data, |
| 185 &u_plane_data, &v_plane_data); | 201 &u_plane_data, &v_plane_data); |
| 186 | 202 |
| 187 const int yplane_stride = dimensions.width(); | 203 const int yplane_stride = dimensions.width(); |
| 188 const int uv_plane_stride = yplane_stride / 2; | 204 const int uv_plane_stride = yplane_stride / 2; |
| 189 int crop_x = 0; | 205 int crop_x = 0; |
| 190 int crop_y = 0; | 206 int crop_y = 0; |
| 191 libyuv::FourCC origin_colorspace = libyuv::FOURCC_ANY; | 207 libyuv::FourCC origin_colorspace = libyuv::FOURCC_ANY; |
| 192 | 208 |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 334 | 350 |
| 335 void VideoCaptureDeviceClient::OnIncomingCapturedBufferExt( | 351 void VideoCaptureDeviceClient::OnIncomingCapturedBufferExt( |
| 336 Buffer buffer, | 352 Buffer buffer, |
| 337 const VideoCaptureFormat& format, | 353 const VideoCaptureFormat& format, |
| 338 base::TimeTicks reference_time, | 354 base::TimeTicks reference_time, |
| 339 base::TimeDelta timestamp, | 355 base::TimeDelta timestamp, |
| 340 gfx::Rect visible_rect, | 356 gfx::Rect visible_rect, |
| 341 const VideoFrameMetadata& additional_metadata) { | 357 const VideoFrameMetadata& additional_metadata) { |
| 342 DFAKE_SCOPED_RECURSIVE_LOCK(call_from_producer_); | 358 DFAKE_SCOPED_RECURSIVE_LOCK(call_from_producer_); |
| 343 | 359 |
| 344 if (!base::ContainsValue(buffer_ids_known_by_receiver_, buffer.id())) | 360 if (!base::ContainsValue(buffer_ids_known_by_receiver_, buffer.id)) { |
| 345 buffer_ids_known_by_receiver_.push_back(buffer.id()); | 361 receiver_->OnNewBufferHandle(buffer.id, std::move(buffer.handle_provider)); |
| 362 buffer_ids_known_by_receiver_.push_back(buffer.id); | |
| 363 } | |
| 346 | 364 |
| 347 auto buffer_access = buffer.handle_provider()->GetHandleForInProcessAccess(); | 365 VideoFrameMetadata metadata; |
| 348 scoped_refptr<media::VideoFrame> frame = | 366 metadata.MergeMetadataFrom(&additional_metadata); |
| 349 media::VideoFrame::WrapExternalSharedMemory( | 367 metadata.SetDouble(media::VideoFrameMetadata::FRAME_RATE, format.frame_rate); |
| 350 format.pixel_format, // format | 368 metadata.SetTimeTicks(media::VideoFrameMetadata::REFERENCE_TIME, |
| 351 format.frame_size, // coded_size | 369 reference_time); |
| 352 visible_rect, // visible_rect | |
| 353 format.frame_size, // natural_size | |
| 354 buffer_access->data(), // data | |
| 355 buffer_access->mapped_size(), // data_size | |
| 356 base::SharedMemory::NULLHandle(), // handle | |
| 357 0u, // shared_memory_offset | |
| 358 timestamp); // timestamp | |
| 359 frame->metadata()->MergeMetadataFrom(&additional_metadata); | |
| 360 frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE, | |
| 361 format.frame_rate); | |
| 362 frame->metadata()->SetTimeTicks(media::VideoFrameMetadata::REFERENCE_TIME, | |
| 363 reference_time); | |
| 364 | 370 |
| 365 receiver_->OnIncomingCapturedVideoFrame(std::move(buffer), std::move(frame)); | 371 mojom::VideoFrameInfoPtr info = mojom::VideoFrameInfo::New(); |
| 372 info->timestamp = timestamp; | |
| 373 info->pixel_format = format.pixel_format; | |
| 374 info->storage_type = format.pixel_storage; | |
| 375 info->coded_size = format.frame_size; | |
| 376 info->visible_rect = visible_rect; | |
| 377 info->metadata = metadata.CopyInternalValues(); | |
| 378 | |
| 379 buffer_pool_->HoldForConsumers(buffer.id, 1); | |
| 380 receiver_->OnFrameReadyInBuffer( | |
| 381 buffer.id, buffer.frame_feedback_id, | |
| 382 base::MakeUnique<BufferPoolConsumerReservationReleaser>(buffer_pool_, | |
| 383 buffer.id), | |
| 384 std::move(info)); | |
| 366 } | 385 } |
| 367 | 386 |
| 368 media::VideoCaptureDevice::Client::Buffer | 387 media::VideoCaptureDevice::Client::Buffer |
| 369 VideoCaptureDeviceClient::ResurrectLastOutputBuffer( | 388 VideoCaptureDeviceClient::ResurrectLastOutputBuffer( |
| 370 const gfx::Size& dimensions, | 389 const gfx::Size& dimensions, |
| 371 media::VideoPixelFormat format, | 390 media::VideoPixelFormat format, |
| 372 media::VideoPixelStorage storage, | 391 media::VideoPixelStorage storage, |
| 373 int new_frame_feedback_id) { | 392 int new_frame_feedback_id) { |
| 374 DFAKE_SCOPED_RECURSIVE_LOCK(call_from_producer_); | 393 DFAKE_SCOPED_RECURSIVE_LOCK(call_from_producer_); |
| 375 const int buffer_id = | 394 const int buffer_id = |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 435 // paddings and/or alignments, but it cannot be smaller. | 454 // paddings and/or alignments, but it cannot be smaller. |
| 436 DCHECK_GE(static_cast<size_t>(length), format.ImageAllocationSize()); | 455 DCHECK_GE(static_cast<size_t>(length), format.ImageAllocationSize()); |
| 437 #if DCHECK_IS_ON() | 456 #if DCHECK_IS_ON() |
| 438 dropped_frame_counter_ = buffer.is_valid() ? 0 : dropped_frame_counter_ + 1; | 457 dropped_frame_counter_ = buffer.is_valid() ? 0 : dropped_frame_counter_ + 1; |
| 439 if (dropped_frame_counter_ >= kMaxDroppedFrames) | 458 if (dropped_frame_counter_ >= kMaxDroppedFrames) |
| 440 OnError(FROM_HERE, "Too many frames dropped"); | 459 OnError(FROM_HERE, "Too many frames dropped"); |
| 441 #endif | 460 #endif |
| 442 // Failed to reserve output buffer, so drop the frame. | 461 // Failed to reserve output buffer, so drop the frame. |
| 443 if (!buffer.is_valid()) | 462 if (!buffer.is_valid()) |
| 444 return; | 463 return; |
| 445 auto buffer_access = buffer.handle_provider()->GetHandleForInProcessAccess(); | 464 auto buffer_access = buffer.handle_provider->GetHandleForInProcessAccess(); |
| 446 memcpy(buffer_access->data(), data, length); | 465 memcpy(buffer_access->data(), data, length); |
| 447 const VideoCaptureFormat output_format = | 466 const VideoCaptureFormat output_format = |
| 448 VideoCaptureFormat(format.frame_size, format.frame_rate, | 467 VideoCaptureFormat(format.frame_size, format.frame_rate, |
| 449 media::PIXEL_FORMAT_Y16, media::PIXEL_STORAGE_CPU); | 468 media::PIXEL_FORMAT_Y16, media::PIXEL_STORAGE_CPU); |
| 450 OnIncomingCapturedBuffer(std::move(buffer), output_format, reference_time, | 469 OnIncomingCapturedBuffer(std::move(buffer), output_format, reference_time, |
| 451 timestamp); | 470 timestamp); |
| 452 } | 471 } |
| 453 | 472 |
| 454 } // namespace media | 473 } // namespace media |
| OLD | NEW |