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