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" |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 : receiver_(std::move(receiver)), | 88 : receiver_(std::move(receiver)), |
89 jpeg_decoder_factory_callback_(jpeg_decoder_factory), | 89 jpeg_decoder_factory_callback_(jpeg_decoder_factory), |
90 external_jpeg_decoder_initialized_(false), | 90 external_jpeg_decoder_initialized_(false), |
91 buffer_pool_(std::move(buffer_pool)), | 91 buffer_pool_(std::move(buffer_pool)), |
92 last_captured_pixel_format_(media::PIXEL_FORMAT_UNKNOWN) {} | 92 last_captured_pixel_format_(media::PIXEL_FORMAT_UNKNOWN) {} |
93 | 93 |
94 VideoCaptureDeviceClient::~VideoCaptureDeviceClient() { | 94 VideoCaptureDeviceClient::~VideoCaptureDeviceClient() { |
95 // This should be on the platform auxiliary thread since | 95 // This should be on the platform auxiliary thread since |
96 // |external_jpeg_decoder_| need to be destructed on the same thread as | 96 // |external_jpeg_decoder_| need to be destructed on the same thread as |
97 // OnIncomingCapturedData. | 97 // OnIncomingCapturedData. |
| 98 |
| 99 for (int buffer_id : buffer_ids_known_by_receiver_) |
| 100 receiver_->OnBufferRetired(buffer_id); |
98 } | 101 } |
99 | 102 |
100 // static | 103 // static |
101 VideoCaptureDevice::Client::Buffer VideoCaptureDeviceClient::MakeBufferStruct( | 104 VideoCaptureDevice::Client::Buffer VideoCaptureDeviceClient::MakeBufferStruct( |
102 scoped_refptr<VideoCaptureBufferPool> buffer_pool, | 105 scoped_refptr<VideoCaptureBufferPool> buffer_pool, |
103 int buffer_id, | 106 int buffer_id, |
104 int frame_feedback_id) { | 107 int frame_feedback_id) { |
105 return Buffer( | 108 return Buffer( |
106 buffer_id, frame_feedback_id, | 109 buffer_id, frame_feedback_id, |
107 base::MakeUnique<BufferPoolBufferHandleProvider>(buffer_pool, buffer_id), | 110 base::MakeUnique<BufferPoolBufferHandleProvider>(buffer_pool, buffer_id), |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 OnIncomingCapturedBuffer(std::move(buffer), output_format, reference_time, | 289 OnIncomingCapturedBuffer(std::move(buffer), output_format, reference_time, |
287 timestamp); | 290 timestamp); |
288 } | 291 } |
289 | 292 |
290 media::VideoCaptureDevice::Client::Buffer | 293 media::VideoCaptureDevice::Client::Buffer |
291 VideoCaptureDeviceClient::ReserveOutputBuffer( | 294 VideoCaptureDeviceClient::ReserveOutputBuffer( |
292 const gfx::Size& frame_size, | 295 const gfx::Size& frame_size, |
293 media::VideoPixelFormat pixel_format, | 296 media::VideoPixelFormat pixel_format, |
294 media::VideoPixelStorage pixel_storage, | 297 media::VideoPixelStorage pixel_storage, |
295 int frame_feedback_id) { | 298 int frame_feedback_id) { |
| 299 DFAKE_SCOPED_RECURSIVE_LOCK(call_from_producer_); |
296 DCHECK_GT(frame_size.width(), 0); | 300 DCHECK_GT(frame_size.width(), 0); |
297 DCHECK_GT(frame_size.height(), 0); | 301 DCHECK_GT(frame_size.height(), 0); |
298 DCHECK(IsFormatSupported(pixel_format)); | 302 DCHECK(IsFormatSupported(pixel_format)); |
299 | 303 |
300 int buffer_id_to_drop = VideoCaptureBufferPool::kInvalidId; | 304 int buffer_id_to_drop = VideoCaptureBufferPool::kInvalidId; |
301 const int buffer_id = | 305 const int buffer_id = |
302 buffer_pool_->ReserveForProducer(frame_size, pixel_format, pixel_storage, | 306 buffer_pool_->ReserveForProducer(frame_size, pixel_format, pixel_storage, |
303 frame_feedback_id, &buffer_id_to_drop); | 307 frame_feedback_id, &buffer_id_to_drop); |
304 if (buffer_id_to_drop != VideoCaptureBufferPool::kInvalidId) | 308 if (buffer_id_to_drop != VideoCaptureBufferPool::kInvalidId) { |
305 receiver_->OnBufferDestroyed(buffer_id_to_drop); | 309 // |buffer_pool_| has decided to release a buffer. Notify receiver in case |
| 310 // the buffer has already been shared with it. |
| 311 auto entry_iter = |
| 312 std::find(buffer_ids_known_by_receiver_.begin(), |
| 313 buffer_ids_known_by_receiver_.end(), buffer_id_to_drop); |
| 314 if (entry_iter != buffer_ids_known_by_receiver_.end()) { |
| 315 buffer_ids_known_by_receiver_.erase(entry_iter); |
| 316 receiver_->OnBufferRetired(buffer_id_to_drop); |
| 317 } |
| 318 } |
306 if (buffer_id == VideoCaptureBufferPool::kInvalidId) | 319 if (buffer_id == VideoCaptureBufferPool::kInvalidId) |
307 return Buffer(); | 320 return Buffer(); |
308 return MakeBufferStruct(buffer_pool_, buffer_id, frame_feedback_id); | 321 return MakeBufferStruct(buffer_pool_, buffer_id, frame_feedback_id); |
309 } | 322 } |
310 | 323 |
311 void VideoCaptureDeviceClient::OnIncomingCapturedBuffer( | 324 void VideoCaptureDeviceClient::OnIncomingCapturedBuffer( |
312 Buffer buffer, | 325 Buffer buffer, |
313 const VideoCaptureFormat& format, | 326 const VideoCaptureFormat& format, |
314 base::TimeTicks reference_time, | 327 base::TimeTicks reference_time, |
315 base::TimeDelta timestamp) { | 328 base::TimeDelta timestamp) { |
| 329 DFAKE_SCOPED_RECURSIVE_LOCK(call_from_producer_); |
316 OnIncomingCapturedBufferExt(std::move(buffer), format, reference_time, | 330 OnIncomingCapturedBufferExt(std::move(buffer), format, reference_time, |
317 timestamp, gfx::Rect(format.frame_size), | 331 timestamp, gfx::Rect(format.frame_size), |
318 VideoFrameMetadata()); | 332 VideoFrameMetadata()); |
319 } | 333 } |
320 | 334 |
321 void VideoCaptureDeviceClient::OnIncomingCapturedBufferExt( | 335 void VideoCaptureDeviceClient::OnIncomingCapturedBufferExt( |
322 Buffer buffer, | 336 Buffer buffer, |
323 const VideoCaptureFormat& format, | 337 const VideoCaptureFormat& format, |
324 base::TimeTicks reference_time, | 338 base::TimeTicks reference_time, |
325 base::TimeDelta timestamp, | 339 base::TimeDelta timestamp, |
326 gfx::Rect visible_rect, | 340 gfx::Rect visible_rect, |
327 const VideoFrameMetadata& additional_metadata) { | 341 const VideoFrameMetadata& additional_metadata) { |
| 342 DFAKE_SCOPED_RECURSIVE_LOCK(call_from_producer_); |
| 343 |
| 344 if (!base::ContainsValue(buffer_ids_known_by_receiver_, buffer.id())) |
| 345 buffer_ids_known_by_receiver_.push_back(buffer.id()); |
| 346 |
328 auto buffer_access = buffer.handle_provider()->GetHandleForInProcessAccess(); | 347 auto buffer_access = buffer.handle_provider()->GetHandleForInProcessAccess(); |
329 scoped_refptr<media::VideoFrame> frame = | 348 scoped_refptr<media::VideoFrame> frame = |
330 media::VideoFrame::WrapExternalSharedMemory( | 349 media::VideoFrame::WrapExternalSharedMemory( |
331 format.pixel_format, // format | 350 format.pixel_format, // format |
332 format.frame_size, // coded_size | 351 format.frame_size, // coded_size |
333 visible_rect, // visible_rect | 352 visible_rect, // visible_rect |
334 format.frame_size, // natural_size | 353 format.frame_size, // natural_size |
335 buffer_access->data(), // data | 354 buffer_access->data(), // data |
336 buffer_access->mapped_size(), // data_size | 355 buffer_access->mapped_size(), // data_size |
337 base::SharedMemory::NULLHandle(), // handle | 356 base::SharedMemory::NULLHandle(), // handle |
338 0u, // shared_memory_offset | 357 0u, // shared_memory_offset |
339 timestamp); // timestamp | 358 timestamp); // timestamp |
340 frame->metadata()->MergeMetadataFrom(&additional_metadata); | 359 frame->metadata()->MergeMetadataFrom(&additional_metadata); |
341 frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE, | 360 frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE, |
342 format.frame_rate); | 361 format.frame_rate); |
343 frame->metadata()->SetTimeTicks(media::VideoFrameMetadata::REFERENCE_TIME, | 362 frame->metadata()->SetTimeTicks(media::VideoFrameMetadata::REFERENCE_TIME, |
344 reference_time); | 363 reference_time); |
345 | 364 |
346 receiver_->OnIncomingCapturedVideoFrame(std::move(buffer), std::move(frame)); | 365 receiver_->OnIncomingCapturedVideoFrame(std::move(buffer), std::move(frame)); |
347 } | 366 } |
348 | 367 |
349 media::VideoCaptureDevice::Client::Buffer | 368 media::VideoCaptureDevice::Client::Buffer |
350 VideoCaptureDeviceClient::ResurrectLastOutputBuffer( | 369 VideoCaptureDeviceClient::ResurrectLastOutputBuffer( |
351 const gfx::Size& dimensions, | 370 const gfx::Size& dimensions, |
352 media::VideoPixelFormat format, | 371 media::VideoPixelFormat format, |
353 media::VideoPixelStorage storage, | 372 media::VideoPixelStorage storage, |
354 int new_frame_feedback_id) { | 373 int new_frame_feedback_id) { |
| 374 DFAKE_SCOPED_RECURSIVE_LOCK(call_from_producer_); |
355 const int buffer_id = | 375 const int buffer_id = |
356 buffer_pool_->ResurrectLastForProducer(dimensions, format, storage); | 376 buffer_pool_->ResurrectLastForProducer(dimensions, format, storage); |
357 if (buffer_id == VideoCaptureBufferPool::kInvalidId) | 377 if (buffer_id == VideoCaptureBufferPool::kInvalidId) |
358 return Buffer(); | 378 return Buffer(); |
359 return MakeBufferStruct(buffer_pool_, buffer_id, new_frame_feedback_id); | 379 return MakeBufferStruct(buffer_pool_, buffer_id, new_frame_feedback_id); |
360 } | 380 } |
361 | 381 |
362 void VideoCaptureDeviceClient::OnError( | 382 void VideoCaptureDeviceClient::OnError( |
363 const tracked_objects::Location& from_here, | 383 const tracked_objects::Location& from_here, |
364 const std::string& reason) { | 384 const std::string& reason) { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 auto buffer_access = buffer.handle_provider()->GetHandleForInProcessAccess(); | 445 auto buffer_access = buffer.handle_provider()->GetHandleForInProcessAccess(); |
426 memcpy(buffer_access->data(), data, length); | 446 memcpy(buffer_access->data(), data, length); |
427 const VideoCaptureFormat output_format = | 447 const VideoCaptureFormat output_format = |
428 VideoCaptureFormat(format.frame_size, format.frame_rate, | 448 VideoCaptureFormat(format.frame_size, format.frame_rate, |
429 media::PIXEL_FORMAT_Y16, media::PIXEL_STORAGE_CPU); | 449 media::PIXEL_FORMAT_Y16, media::PIXEL_STORAGE_CPU); |
430 OnIncomingCapturedBuffer(std::move(buffer), output_format, reference_time, | 450 OnIncomingCapturedBuffer(std::move(buffer), output_format, reference_time, |
431 timestamp); | 451 timestamp); |
432 } | 452 } |
433 | 453 |
434 } // namespace media | 454 } // namespace media |
OLD | NEW |