| 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 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 return nullptr; | 288 return nullptr; |
| 290 return base::WrapUnique<Buffer>( | 289 return base::WrapUnique<Buffer>( |
| 291 new AutoReleaseBuffer(buffer_pool_, buffer_id, frame_feedback_id)); | 290 new AutoReleaseBuffer(buffer_pool_, buffer_id, frame_feedback_id)); |
| 292 } | 291 } |
| 293 | 292 |
| 294 void VideoCaptureDeviceClient::OnIncomingCapturedBuffer( | 293 void VideoCaptureDeviceClient::OnIncomingCapturedBuffer( |
| 295 std::unique_ptr<Buffer> buffer, | 294 std::unique_ptr<Buffer> buffer, |
| 296 const VideoCaptureFormat& format, | 295 const VideoCaptureFormat& format, |
| 297 base::TimeTicks reference_time, | 296 base::TimeTicks reference_time, |
| 298 base::TimeDelta timestamp) { | 297 base::TimeDelta timestamp) { |
| 299 OnIncomingCapturedBufferExt(std::move(buffer), format, reference_time, | 298 DCHECK(IsFormatSupported(format.pixel_format)); |
| 300 timestamp, gfx::Rect(format.frame_size), | 299 DCHECK_EQ(media::PIXEL_STORAGE_CPU, format.pixel_storage); |
| 301 VideoFrameMetadata()); | |
| 302 } | |
| 303 | 300 |
| 304 void VideoCaptureDeviceClient::OnIncomingCapturedBufferExt( | 301 scoped_refptr<VideoFrame> frame; |
| 305 std::unique_ptr<Buffer> buffer, | 302 if (buffer->IsBackedByVideoFrame()) { |
| 306 const VideoCaptureFormat& format, | 303 frame = buffer->GetVideoFrame(); |
| 307 base::TimeTicks reference_time, | 304 frame->set_timestamp(timestamp); |
| 308 base::TimeDelta timestamp, | 305 } else { |
| 309 gfx::Rect visible_rect, | 306 frame = VideoFrame::WrapExternalSharedMemory( |
| 310 const VideoFrameMetadata& additional_metadata) { | 307 format.pixel_format, format.frame_size, gfx::Rect(format.frame_size), |
| 311 const int buffer_id = buffer->id(); | 308 format.frame_size, reinterpret_cast<uint8_t*>(buffer->data()), |
| 312 | 309 VideoFrame::AllocationSize(format.pixel_format, format.frame_size), |
| 313 auto buffer_mojo_handle = buffer_pool_->GetHandleForTransit(buffer_id); | 310 base::SharedMemory::NULLHandle(), 0u, timestamp); |
| 314 base::SharedMemoryHandle memory_handle; | 311 } |
| 315 size_t memory_size = 0; | 312 if (!frame) |
| 316 bool read_only_flag = false; | 313 return; |
| 317 const MojoResult unwrap_result_code = mojo::UnwrapSharedMemoryHandle( | |
| 318 std::move(buffer_mojo_handle), &memory_handle, &memory_size, | |
| 319 &read_only_flag); | |
| 320 DCHECK_EQ(MOJO_RESULT_OK, unwrap_result_code); | |
| 321 | |
| 322 scoped_refptr<media::VideoFrame> frame = | |
| 323 media::VideoFrame::WrapExternalSharedMemory( | |
| 324 format.pixel_format, // format | |
| 325 format.frame_size, // coded_size | |
| 326 visible_rect, // visible_rect | |
| 327 format.frame_size, // natural_size | |
| 328 static_cast<uint8_t*>(buffer->data()), // data | |
| 329 buffer->mapped_size(), // data_size | |
| 330 memory_handle, // handle | |
| 331 0, // shared_memory_offset | |
| 332 timestamp); // timestamp | |
| 333 frame->metadata()->MergeMetadataFrom(&additional_metadata); | |
| 334 frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE, | 314 frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE, |
| 335 format.frame_rate); | 315 format.frame_rate); |
| 336 frame->metadata()->SetTimeTicks(media::VideoFrameMetadata::REFERENCE_TIME, | 316 frame->metadata()->SetTimeTicks(media::VideoFrameMetadata::REFERENCE_TIME, |
| 337 reference_time); | 317 reference_time); |
| 318 OnIncomingCapturedVideoFrame(std::move(buffer), std::move(frame)); |
| 319 } |
| 338 | 320 |
| 321 void VideoCaptureDeviceClient::OnIncomingCapturedVideoFrame( |
| 322 std::unique_ptr<Buffer> buffer, |
| 323 scoped_refptr<VideoFrame> frame) { |
| 339 receiver_->OnIncomingCapturedVideoFrame(std::move(buffer), std::move(frame)); | 324 receiver_->OnIncomingCapturedVideoFrame(std::move(buffer), std::move(frame)); |
| 340 } | 325 } |
| 341 | 326 |
| 342 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> | 327 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> |
| 343 VideoCaptureDeviceClient::ResurrectLastOutputBuffer( | 328 VideoCaptureDeviceClient::ResurrectLastOutputBuffer( |
| 344 const gfx::Size& dimensions, | 329 const gfx::Size& dimensions, |
| 345 media::VideoPixelFormat format, | 330 media::VideoPixelFormat format, |
| 346 media::VideoPixelStorage storage, | 331 media::VideoPixelStorage storage, |
| 347 int new_frame_feedback_id) { | 332 int new_frame_feedback_id) { |
| 348 const int buffer_id = | 333 const int buffer_id = |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 return; | 411 return; |
| 427 memcpy(buffer->data(), data, length); | 412 memcpy(buffer->data(), data, length); |
| 428 const VideoCaptureFormat output_format = | 413 const VideoCaptureFormat output_format = |
| 429 VideoCaptureFormat(format.frame_size, format.frame_rate, | 414 VideoCaptureFormat(format.frame_size, format.frame_rate, |
| 430 media::PIXEL_FORMAT_Y16, media::PIXEL_STORAGE_CPU); | 415 media::PIXEL_FORMAT_Y16, media::PIXEL_STORAGE_CPU); |
| 431 OnIncomingCapturedBuffer(std::move(buffer), output_format, reference_time, | 416 OnIncomingCapturedBuffer(std::move(buffer), output_format, reference_time, |
| 432 timestamp); | 417 timestamp); |
| 433 } | 418 } |
| 434 | 419 |
| 435 } // namespace media | 420 } // namespace media |
| OLD | NEW |