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