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 "base/stl_util.h" | 18 #include "base/stl_util.h" |
| 19 #include "build/build_config.h" | 19 #include "build/build_config.h" |
| 20 #include "components/display_compositor/gl_helper.h" | 20 #include "components/display_compositor/gl_helper.h" |
| 21 #include "content/browser/renderer_host/media/media_stream_manager.h" | 21 #include "content/browser/renderer_host/media/media_stream_manager.h" |
| 22 #include "content/browser/renderer_host/media/video_capture_buffer_pool.h" | 22 #include "content/browser/renderer_host/media/video_capture_buffer_pool.h" |
| 23 #include "content/browser/renderer_host/media/video_capture_device_client.h" | 23 #include "content/browser/renderer_host/media/video_capture_device_client.h" |
| 24 #include "content/browser/renderer_host/media/video_capture_gpu_jpeg_decoder.h" | |
| 24 #include "content/browser/renderer_host/media/video_capture_manager.h" | 25 #include "content/browser/renderer_host/media/video_capture_manager.h" |
| 25 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
| 26 #include "content/public/common/content_switches.h" | 27 #include "content/public/common/content_switches.h" |
| 27 #include "media/base/video_frame.h" | 28 #include "media/base/video_frame.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; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 72 // UpdateReleaseSyncToken() creates a new sync_token using |gl_helper|, so | 73 // UpdateReleaseSyncToken() creates a new sync_token using |gl_helper|, so |
| 73 // wait the given |sync_token| using |gl_helper|. | 74 // wait the given |sync_token| using |gl_helper|. |
| 74 if (gl_helper) { | 75 if (gl_helper) { |
| 75 gl_helper->WaitSyncToken(sync_token); | 76 gl_helper->WaitSyncToken(sync_token); |
| 76 SyncTokenClientImpl client(gl_helper); | 77 SyncTokenClientImpl client(gl_helper); |
| 77 video_frame->UpdateReleaseSyncToken(&client); | 78 video_frame->UpdateReleaseSyncToken(&client); |
| 78 } | 79 } |
| 79 #endif | 80 #endif |
| 80 } | 81 } |
| 81 | 82 |
| 83 class VideoCaptureGpuJpegDecoderFactory | |
| 84 : public VideoCaptureJpegDecoderFactory { | |
| 85 public: | |
| 86 VideoCaptureGpuJpegDecoderFactory( | |
| 87 const VideoCaptureJpegDecoder::DecodeDoneCB& decode_done_cb) | |
| 88 : decode_done_cb_(decode_done_cb) {} | |
| 89 | |
| 90 std::unique_ptr<VideoCaptureJpegDecoder> CreateJpegDecoder() override { | |
| 91 return base::MakeUnique<VideoCaptureGpuJpegDecoder>(decode_done_cb_); | |
| 92 } | |
| 93 | |
| 94 private: | |
| 95 VideoCaptureJpegDecoder::DecodeDoneCB decode_done_cb_; | |
| 96 }; | |
| 97 | |
| 98 // Decorator for VideoFrameReceiver that forwards all incoming calls | |
| 99 // to the Browser IO thread. | |
| 100 class PostOnBrowserIoThreadDecorator : public VideoFrameReceiver { | |
|
emircan
2016/09/08 19:57:56
VideoFrameReceiverOnIOThread?
mcasas
2016/09/08 20:25:45
IOThread is written with IO in uppercase.
(grep -
chfremer
2016/09/09 00:55:38
Thanks for both of the suggestions.
I hope you are
| |
| 101 public: | |
| 102 PostOnBrowserIoThreadDecorator( | |
| 103 const base::WeakPtr<VideoFrameReceiver>& receiver) | |
| 104 : receiver_(receiver) {} | |
| 105 | |
| 106 void OnIncomingCapturedVideoFrame( | |
| 107 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> buffer, | |
| 108 const scoped_refptr<media::VideoFrame>& frame) override { | |
| 109 BrowserThread::PostTask( | |
| 110 BrowserThread::IO, FROM_HERE, | |
| 111 base::Bind(&VideoFrameReceiver::OnIncomingCapturedVideoFrame, receiver_, | |
| 112 base::Passed(&buffer), frame)); | |
| 113 } | |
| 114 | |
| 115 void OnError() override { | |
| 116 BrowserThread::PostTask( | |
| 117 BrowserThread::IO, FROM_HERE, | |
| 118 base::Bind(&VideoFrameReceiver::OnError, receiver_)); | |
| 119 } | |
| 120 | |
| 121 void OnLog(const std::string& message) override { | |
| 122 BrowserThread::PostTask( | |
| 123 BrowserThread::IO, FROM_HERE, | |
| 124 base::Bind(&VideoFrameReceiver::OnLog, receiver_, message)); | |
| 125 } | |
| 126 | |
| 127 void OnBufferDestroyed(int buffer_id_to_drop) override { | |
| 128 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 129 base::Bind(&VideoFrameReceiver::OnBufferDestroyed, | |
| 130 receiver_, buffer_id_to_drop)); | |
| 131 } | |
| 132 | |
| 133 private: | |
| 134 base::WeakPtr<VideoFrameReceiver> receiver_; | |
| 135 }; | |
| 136 | |
| 82 } // anonymous namespace | 137 } // anonymous namespace |
| 83 | 138 |
| 84 struct VideoCaptureController::ControllerClient { | 139 struct VideoCaptureController::ControllerClient { |
| 85 ControllerClient(VideoCaptureControllerID id, | 140 ControllerClient(VideoCaptureControllerID id, |
| 86 VideoCaptureControllerEventHandler* handler, | 141 VideoCaptureControllerEventHandler* handler, |
| 87 base::ProcessHandle render_process, | 142 base::ProcessHandle render_process, |
| 88 media::VideoCaptureSessionId session_id, | 143 media::VideoCaptureSessionId session_id, |
| 89 const media::VideoCaptureParams& params) | 144 const media::VideoCaptureParams& params) |
| 90 : controller_id(id), | 145 : controller_id(id), |
| 91 event_handler(handler), | 146 event_handler(handler), |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 141 | 196 |
| 142 base::WeakPtr<VideoCaptureController> | 197 base::WeakPtr<VideoCaptureController> |
| 143 VideoCaptureController::GetWeakPtrForIOThread() { | 198 VideoCaptureController::GetWeakPtrForIOThread() { |
| 144 return weak_ptr_factory_.GetWeakPtr(); | 199 return weak_ptr_factory_.GetWeakPtr(); |
| 145 } | 200 } |
| 146 | 201 |
| 147 std::unique_ptr<media::VideoCaptureDevice::Client> | 202 std::unique_ptr<media::VideoCaptureDevice::Client> |
| 148 VideoCaptureController::NewDeviceClient() { | 203 VideoCaptureController::NewDeviceClient() { |
| 149 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 204 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 150 return base::MakeUnique<VideoCaptureDeviceClient>( | 205 return base::MakeUnique<VideoCaptureDeviceClient>( |
| 151 this->GetWeakPtrForIOThread(), buffer_pool_); | 206 base::MakeUnique<PostOnBrowserIoThreadDecorator>( |
| 207 this->GetWeakPtrForIOThread()), | |
| 208 buffer_pool_, | |
| 209 base::MakeUnique<VideoCaptureGpuJpegDecoderFactory>( | |
| 210 base::Bind(&VideoFrameReceiver::OnIncomingCapturedVideoFrame, | |
| 211 this->GetWeakPtrForIOThread())), | |
|
emircan
2016/09/08 19:57:56
Passing a callback would be easier to follow here
chfremer
2016/09/09 00:55:38
Interesting. I feel the exact opposite way about t
| |
| 212 base::Bind([]() { DCHECK_CURRENTLY_ON(BrowserThread::IO); })); | |
| 152 } | 213 } |
| 153 | 214 |
| 154 void VideoCaptureController::AddClient( | 215 void VideoCaptureController::AddClient( |
| 155 VideoCaptureControllerID id, | 216 VideoCaptureControllerID id, |
| 156 VideoCaptureControllerEventHandler* event_handler, | 217 VideoCaptureControllerEventHandler* event_handler, |
| 157 base::ProcessHandle render_process, | 218 base::ProcessHandle render_process, |
| 158 media::VideoCaptureSessionId session_id, | 219 media::VideoCaptureSessionId session_id, |
| 159 const media::VideoCaptureParams& params) { | 220 const media::VideoCaptureParams& params) { |
| 160 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 221 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 161 DVLOG(1) << "VideoCaptureController::AddClient() -- id=" << id | 222 DVLOG(1) << "VideoCaptureController::AddClient() -- id=" << id |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 347 VideoCaptureController::GetVideoCaptureFormat() const { | 408 VideoCaptureController::GetVideoCaptureFormat() const { |
| 348 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 409 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 349 return video_capture_format_; | 410 return video_capture_format_; |
| 350 } | 411 } |
| 351 | 412 |
| 352 VideoCaptureController::~VideoCaptureController() { | 413 VideoCaptureController::~VideoCaptureController() { |
| 353 base::STLDeleteContainerPointers(controller_clients_.begin(), | 414 base::STLDeleteContainerPointers(controller_clients_.begin(), |
| 354 controller_clients_.end()); | 415 controller_clients_.end()); |
| 355 } | 416 } |
| 356 | 417 |
| 357 void VideoCaptureController::DoIncomingCapturedVideoFrameOnIOThread( | 418 void VideoCaptureController::OnIncomingCapturedVideoFrame( |
| 358 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> buffer, | 419 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> buffer, |
| 359 const scoped_refptr<VideoFrame>& frame) { | 420 const scoped_refptr<VideoFrame>& frame) { |
| 360 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 421 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 361 const int buffer_id = buffer->id(); | 422 const int buffer_id = buffer->id(); |
| 362 DCHECK_NE(buffer_id, VideoCaptureBufferPool::kInvalidId); | 423 DCHECK_NE(buffer_id, VideoCaptureBufferPool::kInvalidId); |
| 363 | 424 |
| 364 int count = 0; | 425 int count = 0; |
| 365 if (state_ == VIDEO_CAPTURE_STATE_STARTED) { | 426 if (state_ == VIDEO_CAPTURE_STATE_STARTED) { |
| 366 if (!frame->metadata()->HasKey(VideoFrameMetadata::FRAME_RATE)) { | 427 if (!frame->metadata()->HasKey(VideoFrameMetadata::FRAME_RATE)) { |
| 367 frame->metadata()->SetDouble(VideoFrameMetadata::FRAME_RATE, | 428 frame->metadata()->SetDouble(VideoFrameMetadata::FRAME_RATE, |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 418 &frame_rate)) { | 479 &frame_rate)) { |
| 419 frame_rate = video_capture_format_.frame_rate; | 480 frame_rate = video_capture_format_.frame_rate; |
| 420 } | 481 } |
| 421 UMA_HISTOGRAM_COUNTS("Media.VideoCapture.FrameRate", frame_rate); | 482 UMA_HISTOGRAM_COUNTS("Media.VideoCapture.FrameRate", frame_rate); |
| 422 has_received_frames_ = true; | 483 has_received_frames_ = true; |
| 423 } | 484 } |
| 424 | 485 |
| 425 buffer_pool_->HoldForConsumers(buffer_id, count); | 486 buffer_pool_->HoldForConsumers(buffer_id, count); |
| 426 } | 487 } |
| 427 | 488 |
| 428 void VideoCaptureController::DoErrorOnIOThread() { | 489 void VideoCaptureController::OnError() { |
| 429 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 490 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 430 state_ = VIDEO_CAPTURE_STATE_ERROR; | 491 state_ = VIDEO_CAPTURE_STATE_ERROR; |
| 431 | 492 |
| 432 for (const auto* client : controller_clients_) { | 493 for (const auto* client : controller_clients_) { |
| 433 if (client->session_closed) | 494 if (client->session_closed) |
| 434 continue; | 495 continue; |
| 435 client->event_handler->OnError(client->controller_id); | 496 client->event_handler->OnError(client->controller_id); |
| 436 } | 497 } |
| 437 } | 498 } |
| 438 | 499 |
| 439 void VideoCaptureController::DoLogOnIOThread(const std::string& message) { | 500 void VideoCaptureController::OnLog(const std::string& message) { |
| 440 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 501 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 441 MediaStreamManager::SendMessageToNativeLog("Video capture: " + message); | 502 MediaStreamManager::SendMessageToNativeLog("Video capture: " + message); |
| 442 } | 503 } |
| 443 | 504 |
| 444 void VideoCaptureController::DoBufferDestroyedOnIOThread( | 505 void VideoCaptureController::OnBufferDestroyed(int buffer_id_to_drop) { |
| 445 int buffer_id_to_drop) { | |
| 446 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 506 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 447 | 507 |
| 448 for (auto* client : controller_clients_) { | 508 for (auto* client : controller_clients_) { |
| 449 if (client->session_closed) | 509 if (client->session_closed) |
| 450 continue; | 510 continue; |
| 451 | 511 |
| 452 if (client->known_buffers.erase(buffer_id_to_drop)) { | 512 if (client->known_buffers.erase(buffer_id_to_drop)) { |
| 453 client->event_handler->OnBufferDestroyed(client->controller_id, | 513 client->event_handler->OnBufferDestroyed(client->controller_id, |
| 454 buffer_id_to_drop); | 514 buffer_id_to_drop); |
| 455 } | 515 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 507 int session_id, | 567 int session_id, |
| 508 const ControllerClients& clients) { | 568 const ControllerClients& clients) { |
| 509 for (auto* client : clients) { | 569 for (auto* client : clients) { |
| 510 if (client->session_id == session_id) | 570 if (client->session_id == session_id) |
| 511 return client; | 571 return client; |
| 512 } | 572 } |
| 513 return NULL; | 573 return NULL; |
| 514 } | 574 } |
| 515 | 575 |
| 516 } // namespace content | 576 } // namespace content |
| OLD | NEW |