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 || |
35 pixel_format == media::PIXEL_FORMAT_Y16); | 34 pixel_format == media::PIXEL_FORMAT_Y16); |
36 } | 35 } |
37 } | 36 } |
38 | 37 |
39 namespace media { | 38 namespace media { |
40 | 39 |
41 class BufferPoolProducerReservationReleaser | 40 template <typename ReleaseTraits> |
| 41 class ScopedBufferPoolReservation |
42 : public VideoCaptureDevice::Client::Buffer::ScopedAccessPermission { | 42 : public VideoCaptureDevice::Client::Buffer::ScopedAccessPermission { |
43 public: | 43 public: |
44 BufferPoolProducerReservationReleaser( | 44 ScopedBufferPoolReservation(scoped_refptr<VideoCaptureBufferPool> buffer_pool, |
45 scoped_refptr<VideoCaptureBufferPool> buffer_pool, | 45 int buffer_id) |
46 int buffer_id) | |
47 : buffer_pool_(std::move(buffer_pool)), buffer_id_(buffer_id) {} | 46 : buffer_pool_(std::move(buffer_pool)), buffer_id_(buffer_id) {} |
48 | 47 |
49 ~BufferPoolProducerReservationReleaser() override { | 48 ~ScopedBufferPoolReservation() override { |
50 buffer_pool_->RelinquishProducerReservation(buffer_id_); | 49 ReleaseTraits::Release(buffer_pool_, 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 ProducerReleaseTraits { |
| 58 public: |
| 59 static void Release(const scoped_refptr<VideoCaptureBufferPool>& buffer_pool, |
| 60 int buffer_id) { |
| 61 buffer_pool->RelinquishProducerReservation(buffer_id); |
| 62 } |
| 63 }; |
| 64 |
| 65 class ConsumerReleaseTraits { |
| 66 public: |
| 67 static void Release(const scoped_refptr<VideoCaptureBufferPool>& buffer_pool, |
| 68 int buffer_id) { |
| 69 buffer_pool->RelinquishConsumerHold(buffer_id, 1); |
| 70 } |
| 71 }; |
| 72 |
58 class BufferPoolBufferHandleProvider | 73 class BufferPoolBufferHandleProvider |
59 : public VideoCaptureDevice::Client::Buffer::HandleProvider { | 74 : public VideoCaptureDevice::Client::Buffer::HandleProvider { |
60 public: | 75 public: |
61 BufferPoolBufferHandleProvider( | 76 BufferPoolBufferHandleProvider( |
62 scoped_refptr<VideoCaptureBufferPool> buffer_pool, | 77 scoped_refptr<VideoCaptureBufferPool> buffer_pool, |
63 int buffer_id) | 78 int buffer_id) |
64 : buffer_pool_(std::move(buffer_pool)), buffer_id_(buffer_id) {} | 79 : buffer_pool_(std::move(buffer_pool)), buffer_id_(buffer_id) {} |
65 | 80 |
66 // Implementation of HandleProvider: | 81 // Implementation of HandleProvider: |
67 mojo::ScopedSharedBufferHandle GetHandleForInterProcessTransit() override { | 82 mojo::ScopedSharedBufferHandle GetHandleForInterProcessTransit() override { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 } | 116 } |
102 | 117 |
103 // static | 118 // static |
104 VideoCaptureDevice::Client::Buffer VideoCaptureDeviceClient::MakeBufferStruct( | 119 VideoCaptureDevice::Client::Buffer VideoCaptureDeviceClient::MakeBufferStruct( |
105 scoped_refptr<VideoCaptureBufferPool> buffer_pool, | 120 scoped_refptr<VideoCaptureBufferPool> buffer_pool, |
106 int buffer_id, | 121 int buffer_id, |
107 int frame_feedback_id) { | 122 int frame_feedback_id) { |
108 return Buffer( | 123 return Buffer( |
109 buffer_id, frame_feedback_id, | 124 buffer_id, frame_feedback_id, |
110 base::MakeUnique<BufferPoolBufferHandleProvider>(buffer_pool, buffer_id), | 125 base::MakeUnique<BufferPoolBufferHandleProvider>(buffer_pool, buffer_id), |
111 base::MakeUnique<BufferPoolProducerReservationReleaser>(buffer_pool, | 126 base::MakeUnique<ScopedBufferPoolReservation<ProducerReleaseTraits>>( |
112 buffer_id)); | 127 buffer_pool, buffer_id)); |
113 } | 128 } |
114 | 129 |
115 void VideoCaptureDeviceClient::OnIncomingCapturedData( | 130 void VideoCaptureDeviceClient::OnIncomingCapturedData( |
116 const uint8_t* data, | 131 const uint8_t* data, |
117 int length, | 132 int length, |
118 const VideoCaptureFormat& format, | 133 const VideoCaptureFormat& format, |
119 int rotation, | 134 int rotation, |
120 base::TimeTicks reference_time, | 135 base::TimeTicks reference_time, |
121 base::TimeDelta timestamp, | 136 base::TimeDelta timestamp, |
122 int frame_feedback_id) { | 137 int frame_feedback_id) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 media::PIXEL_STORAGE_CPU, frame_feedback_id); | 187 media::PIXEL_STORAGE_CPU, frame_feedback_id); |
173 #if DCHECK_IS_ON() | 188 #if DCHECK_IS_ON() |
174 dropped_frame_counter_ = buffer.is_valid() ? 0 : dropped_frame_counter_ + 1; | 189 dropped_frame_counter_ = buffer.is_valid() ? 0 : dropped_frame_counter_ + 1; |
175 if (dropped_frame_counter_ >= kMaxDroppedFrames) | 190 if (dropped_frame_counter_ >= kMaxDroppedFrames) |
176 OnError(FROM_HERE, "Too many frames dropped"); | 191 OnError(FROM_HERE, "Too many frames dropped"); |
177 #endif | 192 #endif |
178 // Failed to reserve I420 output buffer, so drop the frame. | 193 // Failed to reserve I420 output buffer, so drop the frame. |
179 if (!buffer.is_valid()) | 194 if (!buffer.is_valid()) |
180 return; | 195 return; |
181 | 196 |
182 auto buffer_access = buffer.handle_provider()->GetHandleForInProcessAccess(); | 197 auto buffer_access = buffer.handle_provider->GetHandleForInProcessAccess(); |
183 uint8_t *y_plane_data, *u_plane_data, *v_plane_data; | 198 uint8_t *y_plane_data, *u_plane_data, *v_plane_data; |
184 InitializeI420PlanePointers(dimensions, buffer_access->data(), &y_plane_data, | 199 InitializeI420PlanePointers(dimensions, buffer_access->data(), &y_plane_data, |
185 &u_plane_data, &v_plane_data); | 200 &u_plane_data, &v_plane_data); |
186 | 201 |
187 const int yplane_stride = dimensions.width(); | 202 const int yplane_stride = dimensions.width(); |
188 const int uv_plane_stride = yplane_stride / 2; | 203 const int uv_plane_stride = yplane_stride / 2; |
189 int crop_x = 0; | 204 int crop_x = 0; |
190 int crop_y = 0; | 205 int crop_y = 0; |
191 libyuv::FourCC origin_colorspace = libyuv::FOURCC_ANY; | 206 libyuv::FourCC origin_colorspace = libyuv::FOURCC_ANY; |
192 | 207 |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 | 349 |
335 void VideoCaptureDeviceClient::OnIncomingCapturedBufferExt( | 350 void VideoCaptureDeviceClient::OnIncomingCapturedBufferExt( |
336 Buffer buffer, | 351 Buffer buffer, |
337 const VideoCaptureFormat& format, | 352 const VideoCaptureFormat& format, |
338 base::TimeTicks reference_time, | 353 base::TimeTicks reference_time, |
339 base::TimeDelta timestamp, | 354 base::TimeDelta timestamp, |
340 gfx::Rect visible_rect, | 355 gfx::Rect visible_rect, |
341 const VideoFrameMetadata& additional_metadata) { | 356 const VideoFrameMetadata& additional_metadata) { |
342 DFAKE_SCOPED_RECURSIVE_LOCK(call_from_producer_); | 357 DFAKE_SCOPED_RECURSIVE_LOCK(call_from_producer_); |
343 | 358 |
344 if (!base::ContainsValue(buffer_ids_known_by_receiver_, buffer.id())) | 359 if (!base::ContainsValue(buffer_ids_known_by_receiver_, buffer.id)) { |
345 buffer_ids_known_by_receiver_.push_back(buffer.id()); | 360 receiver_->OnNewBufferHandle(buffer.id, std::move(buffer.handle_provider)); |
| 361 buffer_ids_known_by_receiver_.push_back(buffer.id); |
| 362 } |
346 | 363 |
347 auto buffer_access = buffer.handle_provider()->GetHandleForInProcessAccess(); | 364 VideoFrameMetadata metadata; |
348 scoped_refptr<media::VideoFrame> frame = | 365 metadata.MergeMetadataFrom(&additional_metadata); |
349 media::VideoFrame::WrapExternalSharedMemory( | 366 metadata.SetDouble(media::VideoFrameMetadata::FRAME_RATE, format.frame_rate); |
350 format.pixel_format, // format | 367 metadata.SetTimeTicks(media::VideoFrameMetadata::REFERENCE_TIME, |
351 format.frame_size, // coded_size | 368 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 | 369 |
365 receiver_->OnIncomingCapturedVideoFrame(std::move(buffer), std::move(frame)); | 370 mojom::VideoFrameInfoPtr info = mojom::VideoFrameInfo::New(); |
| 371 info->timestamp = timestamp; |
| 372 info->pixel_format = format.pixel_format; |
| 373 info->storage_type = format.pixel_storage; |
| 374 info->coded_size = format.frame_size; |
| 375 info->visible_rect = visible_rect; |
| 376 info->metadata = metadata.CopyInternalValues(); |
| 377 |
| 378 buffer_pool_->HoldForConsumers(buffer.id, 1); |
| 379 receiver_->OnFrameReadyInBuffer( |
| 380 buffer.id, buffer.frame_feedback_id, |
| 381 base::MakeUnique<ScopedBufferPoolReservation<ConsumerReleaseTraits>>( |
| 382 buffer_pool_, buffer.id), |
| 383 std::move(info)); |
366 } | 384 } |
367 | 385 |
368 media::VideoCaptureDevice::Client::Buffer | 386 media::VideoCaptureDevice::Client::Buffer |
369 VideoCaptureDeviceClient::ResurrectLastOutputBuffer( | 387 VideoCaptureDeviceClient::ResurrectLastOutputBuffer( |
370 const gfx::Size& dimensions, | 388 const gfx::Size& dimensions, |
371 media::VideoPixelFormat format, | 389 media::VideoPixelFormat format, |
372 media::VideoPixelStorage storage, | 390 media::VideoPixelStorage storage, |
373 int new_frame_feedback_id) { | 391 int new_frame_feedback_id) { |
374 DFAKE_SCOPED_RECURSIVE_LOCK(call_from_producer_); | 392 DFAKE_SCOPED_RECURSIVE_LOCK(call_from_producer_); |
375 const int buffer_id = | 393 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. | 453 // paddings and/or alignments, but it cannot be smaller. |
436 DCHECK_GE(static_cast<size_t>(length), format.ImageAllocationSize()); | 454 DCHECK_GE(static_cast<size_t>(length), format.ImageAllocationSize()); |
437 #if DCHECK_IS_ON() | 455 #if DCHECK_IS_ON() |
438 dropped_frame_counter_ = buffer.is_valid() ? 0 : dropped_frame_counter_ + 1; | 456 dropped_frame_counter_ = buffer.is_valid() ? 0 : dropped_frame_counter_ + 1; |
439 if (dropped_frame_counter_ >= kMaxDroppedFrames) | 457 if (dropped_frame_counter_ >= kMaxDroppedFrames) |
440 OnError(FROM_HERE, "Too many frames dropped"); | 458 OnError(FROM_HERE, "Too many frames dropped"); |
441 #endif | 459 #endif |
442 // Failed to reserve output buffer, so drop the frame. | 460 // Failed to reserve output buffer, so drop the frame. |
443 if (!buffer.is_valid()) | 461 if (!buffer.is_valid()) |
444 return; | 462 return; |
445 auto buffer_access = buffer.handle_provider()->GetHandleForInProcessAccess(); | 463 auto buffer_access = buffer.handle_provider->GetHandleForInProcessAccess(); |
446 memcpy(buffer_access->data(), data, length); | 464 memcpy(buffer_access->data(), data, length); |
447 const VideoCaptureFormat output_format = | 465 const VideoCaptureFormat output_format = |
448 VideoCaptureFormat(format.frame_size, format.frame_rate, | 466 VideoCaptureFormat(format.frame_size, format.frame_rate, |
449 media::PIXEL_FORMAT_Y16, media::PIXEL_STORAGE_CPU); | 467 media::PIXEL_FORMAT_Y16, media::PIXEL_STORAGE_CPU); |
450 OnIncomingCapturedBuffer(std::move(buffer), output_format, reference_time, | 468 OnIncomingCapturedBuffer(std::move(buffer), output_format, reference_time, |
451 timestamp); | 469 timestamp); |
452 } | 470 } |
453 | 471 |
454 } // namespace media | 472 } // namespace media |
OLD | NEW |