Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/renderer_host/media/video_capture_controller.h" | 5 #include "content/browser/renderer_host/media/video_capture_controller.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <set> | 11 #include <set> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.h" |
| 17 #include "base/metrics/sparse_histogram.h" | 17 #include "base/metrics/sparse_histogram.h" |
| 18 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 19 #include "components/display_compositor/gl_helper.h" | 19 #include "components/display_compositor/gl_helper.h" |
| 20 #include "content/browser/renderer_host/media/media_stream_manager.h" | 20 #include "content/browser/renderer_host/media/media_stream_manager.h" |
| 21 #include "content/browser/renderer_host/media/video_capture_manager.h" | 21 #include "content/browser/renderer_host/media/video_capture_manager.h" |
| 22 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
| 23 #include "content/public/common/content_switches.h" | 23 #include "content/public/common/content_switches.h" |
| 24 #include "media/base/video_frame.h" | 24 #include "media/base/video_frame.h" |
| 25 #include "media/capture/video/video_capture_buffer_pool.h" | 25 #include "media/capture/video/video_capture_buffer_pool.h" |
| 26 #include "media/capture/video/video_capture_buffer_tracker_factory_impl.h" | 26 #include "media/capture/video/video_capture_buffer_tracker_factory_impl.h" |
| 27 #include "media/capture/video/video_capture_device_client.h" | 27 #include "media/capture/video/video_capture_device_client.h" |
| 28 #include "mojo/public/cpp/system/platform_handle.h" | |
| 28 | 29 |
| 29 #if !defined(OS_ANDROID) | 30 #if !defined(OS_ANDROID) |
| 30 #include "content/browser/compositor/image_transport_factory.h" | 31 #include "content/browser/compositor/image_transport_factory.h" |
| 31 #endif | 32 #endif |
| 32 | 33 |
| 33 using media::VideoCaptureFormat; | 34 using media::VideoCaptureFormat; |
| 34 using media::VideoFrame; | 35 using media::VideoFrame; |
| 35 using media::VideoFrameMetadata; | 36 using media::VideoFrameMetadata; |
| 36 | 37 |
| 37 namespace content { | 38 namespace content { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 88 | 89 |
| 89 // Indicates whether the client is paused, if true, VideoCaptureController | 90 // Indicates whether the client is paused, if true, VideoCaptureController |
| 90 // stops updating its buffer. | 91 // stops updating its buffer. |
| 91 bool paused; | 92 bool paused; |
| 92 }; | 93 }; |
| 93 | 94 |
| 94 VideoCaptureController::BufferState::BufferState( | 95 VideoCaptureController::BufferState::BufferState( |
| 95 int buffer_id, | 96 int buffer_id, |
| 96 int frame_feedback_id, | 97 int frame_feedback_id, |
| 97 media::VideoFrameConsumerFeedbackObserver* consumer_feedback_observer, | 98 media::VideoFrameConsumerFeedbackObserver* consumer_feedback_observer, |
| 98 media::FrameBufferPool* frame_buffer_pool, | 99 media::FrameBufferPool* frame_buffer_pool) |
| 99 scoped_refptr<media::VideoFrame> frame) | |
| 100 : buffer_id_(buffer_id), | 100 : buffer_id_(buffer_id), |
| 101 frame_feedback_id_(frame_feedback_id), | 101 frame_feedback_id_(frame_feedback_id), |
| 102 consumer_feedback_observer_(consumer_feedback_observer), | 102 consumer_feedback_observer_(consumer_feedback_observer), |
| 103 frame_buffer_pool_(frame_buffer_pool), | 103 frame_buffer_pool_(frame_buffer_pool), |
| 104 frame_(std::move(frame)), | |
| 105 max_consumer_utilization_( | 104 max_consumer_utilization_( |
| 106 media::VideoFrameConsumerFeedbackObserver::kNoUtilizationRecorded), | 105 media::VideoFrameConsumerFeedbackObserver::kNoUtilizationRecorded), |
| 107 consumer_hold_count_(0) {} | 106 consumer_hold_count_(0) {} |
| 108 | 107 |
| 109 VideoCaptureController::BufferState::~BufferState() = default; | 108 VideoCaptureController::BufferState::~BufferState() = default; |
| 110 | 109 |
| 111 VideoCaptureController::BufferState::BufferState( | 110 VideoCaptureController::BufferState::BufferState( |
| 112 const VideoCaptureController::BufferState& other) = default; | 111 const VideoCaptureController::BufferState& other) = default; |
| 113 | 112 |
| 114 void VideoCaptureController::BufferState::RecordConsumerUtilization( | 113 void VideoCaptureController::BufferState::RecordConsumerUtilization( |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 363 client->buffers_in_use.erase(buffers_in_use_entry_iter); | 362 client->buffers_in_use.erase(buffers_in_use_entry_iter); |
| 364 } | 363 } |
| 365 | 364 |
| 366 const media::VideoCaptureFormat& | 365 const media::VideoCaptureFormat& |
| 367 VideoCaptureController::GetVideoCaptureFormat() const { | 366 VideoCaptureController::GetVideoCaptureFormat() const { |
| 368 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 367 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 369 return video_capture_format_; | 368 return video_capture_format_; |
| 370 } | 369 } |
| 371 | 370 |
| 372 void VideoCaptureController::OnIncomingCapturedVideoFrame( | 371 void VideoCaptureController::OnIncomingCapturedVideoFrame( |
| 373 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> buffer, | 372 media::VideoCaptureDevice::Client::Buffer buffer, |
| 374 scoped_refptr<VideoFrame> frame) { | 373 scoped_refptr<VideoFrame> frame) { |
| 375 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 374 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 376 DCHECK(frame_buffer_pool_); | 375 const int buffer_id = buffer.id; |
|
miu
2016/12/20 22:25:37
If |frame_buffer_pool_| is null, should we return
chfremer
2016/12/22 19:01:20
No. Before this CL |frame_buffer_pool_| == null wa
miu
2016/12/27 23:38:48
Acknowledged.
| |
| 377 const int buffer_id = buffer->id(); | |
| 378 DCHECK_NE(buffer_id, media::VideoCaptureBufferPool::kInvalidId); | 376 DCHECK_NE(buffer_id, media::VideoCaptureBufferPool::kInvalidId); |
| 379 | 377 |
| 380 // Insert if not exists. | 378 // Insert if not exists. |
| 381 const auto it = | 379 const auto it = |
| 382 buffer_id_to_state_map_ | 380 buffer_id_to_state_map_ |
| 383 .insert(std::make_pair( | 381 .insert(std::make_pair( |
| 384 buffer_id, BufferState(buffer_id, buffer->frame_feedback_id(), | 382 buffer_id, BufferState(buffer_id, buffer.frame_feedback_id, |
| 385 consumer_feedback_observer_.get(), | 383 consumer_feedback_observer_.get(), |
| 386 frame_buffer_pool_.get(), frame))) | 384 frame_buffer_pool_.get()))) |
| 387 .first; | 385 .first; |
| 388 BufferState& buffer_state = it->second; | 386 BufferState& buffer_state = it->second; |
| 389 DCHECK(buffer_state.HasZeroConsumerHoldCount()); | 387 DCHECK(buffer_state.HasZeroConsumerHoldCount()); |
| 390 | 388 |
| 391 if (state_ == VIDEO_CAPTURE_STATE_STARTED) { | 389 if (state_ == VIDEO_CAPTURE_STATE_STARTED) { |
| 392 if (!frame->metadata()->HasKey(VideoFrameMetadata::FRAME_RATE)) { | 390 if (!frame->metadata()->HasKey(VideoFrameMetadata::FRAME_RATE)) { |
| 393 frame->metadata()->SetDouble(VideoFrameMetadata::FRAME_RATE, | 391 frame->metadata()->SetDouble(VideoFrameMetadata::FRAME_RATE, |
| 394 video_capture_format_.frame_rate); | 392 video_capture_format_.frame_rate); |
| 395 } | 393 } |
| 396 std::unique_ptr<base::DictionaryValue> metadata( | 394 std::unique_ptr<base::DictionaryValue> metadata( |
| 397 new base::DictionaryValue()); | 395 new base::DictionaryValue()); |
| 398 frame->metadata()->MergeInternalValuesInto(metadata.get()); | 396 frame->metadata()->MergeInternalValuesInto(metadata.get()); |
| 399 | 397 |
| 400 // Only I420 and Y16 pixel formats are currently supported. | 398 // Only I420 and Y16 pixel formats are currently supported. |
| 401 DCHECK(frame->format() == media::PIXEL_FORMAT_I420 || | 399 DCHECK(frame->format() == media::PIXEL_FORMAT_I420 || |
| 402 frame->format() == media::PIXEL_FORMAT_Y16) | 400 frame->format() == media::PIXEL_FORMAT_Y16) |
| 403 << "Unsupported pixel format: " | 401 << "Unsupported pixel format: " |
| 404 << media::VideoPixelFormatToString(frame->format()); | 402 << media::VideoPixelFormatToString(frame->format()); |
| 405 | 403 |
| 406 // Sanity-checks to confirm |frame| is actually being backed by |buffer|. | 404 // Sanity-checks to confirm |frame| is actually being backed by |buffer|. |
| 405 auto buffer_access = buffer.access_provider->GetReadWriteAccess(); | |
| 407 DCHECK(frame->storage_type() == media::VideoFrame::STORAGE_SHMEM); | 406 DCHECK(frame->storage_type() == media::VideoFrame::STORAGE_SHMEM); |
| 408 DCHECK(frame->data(media::VideoFrame::kYPlane) >= buffer->data(0) && | 407 DCHECK(frame->data(media::VideoFrame::kYPlane) >= buffer_access->data() && |
| 409 (frame->data(media::VideoFrame::kYPlane) < | 408 (frame->data(media::VideoFrame::kYPlane) < |
| 410 (reinterpret_cast<const uint8_t*>(buffer->data(0)) + | 409 (buffer_access->data() + buffer_access->mapped_size()))) |
| 411 buffer->mapped_size()))) | |
| 412 << "VideoFrame does not appear to be backed by Buffer"; | 410 << "VideoFrame does not appear to be backed by Buffer"; |
| 413 | 411 |
| 414 for (const auto& client : controller_clients_) { | 412 for (const auto& client : controller_clients_) { |
| 415 if (client->session_closed || client->paused) | 413 if (client->session_closed || client->paused) |
| 416 continue; | 414 continue; |
| 417 | 415 |
| 418 // On the first use of a buffer on a client, share the memory handles. | 416 // On the first use of a buffer on a client, share the memory handles. |
| 419 auto known_buffers_entry_iter = | 417 auto known_buffers_entry_iter = |
| 420 std::find(std::begin(client->known_buffers), | 418 std::find(std::begin(client->known_buffers), |
| 421 std::end(client->known_buffers), buffer_id); | 419 std::end(client->known_buffers), buffer_id); |
| 422 bool is_new_buffer = false; | 420 bool is_new_buffer = false; |
| 423 if (known_buffers_entry_iter == std::end(client->known_buffers)) { | 421 if (known_buffers_entry_iter == std::end(client->known_buffers)) { |
| 424 client->known_buffers.push_back(buffer_id); | 422 client->known_buffers.push_back(buffer_id); |
| 425 is_new_buffer = true; | 423 is_new_buffer = true; |
| 426 } | 424 } |
| 427 if (is_new_buffer) | 425 if (is_new_buffer) { |
| 428 DoNewBufferOnIOThread(client.get(), buffer.get(), frame); | 426 mojo::ScopedSharedBufferHandle handle = |
| 429 | 427 buffer.access_provider->GetHandleForTransit(); |
| 428 client->event_handler->OnBufferCreated( | |
| 429 client->controller_id, std::move(handle), | |
| 430 buffer_access->mapped_size(), buffer_id); | |
| 431 } | |
| 430 client->event_handler->OnBufferReady(client->controller_id, buffer_id, | 432 client->event_handler->OnBufferReady(client->controller_id, buffer_id, |
| 431 frame); | 433 frame); |
| 432 | 434 |
| 433 auto buffers_in_use_entry_iter = | 435 auto buffers_in_use_entry_iter = |
| 434 std::find(std::begin(client->buffers_in_use), | 436 std::find(std::begin(client->buffers_in_use), |
| 435 std::end(client->buffers_in_use), buffer_id); | 437 std::end(client->buffers_in_use), buffer_id); |
| 436 if (buffers_in_use_entry_iter == std::end(client->buffers_in_use)) | 438 if (buffers_in_use_entry_iter == std::end(client->buffers_in_use)) |
| 437 client->buffers_in_use.push_back(buffer_id); | 439 client->buffers_in_use.push_back(buffer_id); |
| 438 else | 440 else |
| 439 DCHECK(false) << "Unexpected duplicate buffer: " << buffer_id; | 441 DCHECK(false) << "Unexpected duplicate buffer: " << buffer_id; |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 470 } | 472 } |
| 471 } | 473 } |
| 472 | 474 |
| 473 void VideoCaptureController::OnLog(const std::string& message) { | 475 void VideoCaptureController::OnLog(const std::string& message) { |
| 474 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 476 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 475 MediaStreamManager::SendMessageToNativeLog("Video capture: " + message); | 477 MediaStreamManager::SendMessageToNativeLog("Video capture: " + message); |
| 476 } | 478 } |
| 477 | 479 |
| 478 void VideoCaptureController::OnBufferDestroyed(int buffer_id_to_drop) { | 480 void VideoCaptureController::OnBufferDestroyed(int buffer_id_to_drop) { |
| 479 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 481 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 480 DCHECK(frame_buffer_pool_); | |
| 481 | 482 |
| 482 for (const auto& client : controller_clients_) { | 483 for (const auto& client : controller_clients_) { |
| 483 if (client->session_closed) | 484 if (client->session_closed) |
| 484 continue; | 485 continue; |
| 485 | 486 |
| 486 auto known_buffers_entry_iter = | 487 auto known_buffers_entry_iter = |
| 487 std::find(std::begin(client->known_buffers), | 488 std::find(std::begin(client->known_buffers), |
| 488 std::end(client->known_buffers), buffer_id_to_drop); | 489 std::end(client->known_buffers), buffer_id_to_drop); |
| 489 if (known_buffers_entry_iter != std::end(client->known_buffers)) { | 490 if (known_buffers_entry_iter != std::end(client->known_buffers)) { |
| 490 client->known_buffers.erase(known_buffers_entry_iter); | 491 client->known_buffers.erase(known_buffers_entry_iter); |
| 491 client->event_handler->OnBufferDestroyed(client->controller_id, | 492 client->event_handler->OnBufferDestroyed(client->controller_id, |
| 492 buffer_id_to_drop); | 493 buffer_id_to_drop); |
| 493 } | 494 } |
| 494 } | 495 } |
| 495 | 496 |
| 496 buffer_id_to_state_map_.erase(buffer_id_to_drop); | 497 buffer_id_to_state_map_.erase(buffer_id_to_drop); |
| 497 } | 498 } |
| 498 | 499 |
| 499 void VideoCaptureController::DoNewBufferOnIOThread( | |
| 500 ControllerClient* client, | |
| 501 media::VideoCaptureDevice::Client::Buffer* buffer, | |
| 502 const scoped_refptr<media::VideoFrame>& frame) { | |
| 503 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 504 DCHECK_EQ(media::VideoFrame::STORAGE_SHMEM, frame->storage_type()); | |
| 505 | |
| 506 const int buffer_id = buffer->id(); | |
| 507 mojo::ScopedSharedBufferHandle handle = | |
| 508 frame_buffer_pool_->GetHandleForTransit(buffer_id); | |
| 509 client->event_handler->OnBufferCreated(client->controller_id, | |
| 510 std::move(handle), | |
| 511 buffer->mapped_size(), buffer_id); | |
| 512 } | |
| 513 | |
| 514 VideoCaptureController::ControllerClient* VideoCaptureController::FindClient( | 500 VideoCaptureController::ControllerClient* VideoCaptureController::FindClient( |
| 515 VideoCaptureControllerID id, | 501 VideoCaptureControllerID id, |
| 516 VideoCaptureControllerEventHandler* handler, | 502 VideoCaptureControllerEventHandler* handler, |
| 517 const ControllerClients& clients) { | 503 const ControllerClients& clients) { |
| 518 for (const auto& client : clients) { | 504 for (const auto& client : clients) { |
| 519 if (client->controller_id == id && client->event_handler == handler) | 505 if (client->controller_id == id && client->event_handler == handler) |
| 520 return client.get(); | 506 return client.get(); |
| 521 } | 507 } |
| 522 return nullptr; | 508 return nullptr; |
| 523 } | 509 } |
| 524 | 510 |
| 525 VideoCaptureController::ControllerClient* VideoCaptureController::FindClient( | 511 VideoCaptureController::ControllerClient* VideoCaptureController::FindClient( |
| 526 int session_id, | 512 int session_id, |
| 527 const ControllerClients& clients) { | 513 const ControllerClients& clients) { |
| 528 for (const auto& client : clients) { | 514 for (const auto& client : clients) { |
| 529 if (client->session_id == session_id) | 515 if (client->session_id == session_id) |
| 530 return client.get(); | 516 return client.get(); |
| 531 } | 517 } |
| 532 return nullptr; | 518 return nullptr; |
| 533 } | 519 } |
| 534 | 520 |
| 535 } // namespace content | 521 } // namespace content |
| OLD | NEW |