Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(91)

Side by Side Diff: content/browser/renderer_host/media/video_capture_controller.cc

Issue 2308533003: Break tight coupling of VideoCaptureDeviceClient to renderer_host (Closed)
Patch Set: mcasas' comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 std::unique_ptr<VideoCaptureJpegDecoder> CreateGpuJpegDecoder(
84 const VideoCaptureJpegDecoder::DecodeDoneCB& decode_done_cb) {
85 return base::MakeUnique<VideoCaptureGpuJpegDecoder>(decode_done_cb);
86 }
87
88 // Decorator for VideoFrameReceiver that forwards all incoming calls
89 // to the Browser IO thread.
90 class PostOnIOThreadVideoFrameReceiver : public VideoFrameReceiver {
91 public:
92 PostOnIOThreadVideoFrameReceiver(
miu 2016/09/13 00:33:29 Sorry, didn't see this the last time around: Pleas
chfremer 2016/09/14 16:26:01 Done.
93 const base::WeakPtr<VideoFrameReceiver>& receiver)
94 : receiver_(receiver) {}
95
96 void OnIncomingCapturedVideoFrame(
miu 2016/09/12 20:16:46 Please use base::ThreadChecker to ensure all these
chfremer 2016/09/12 22:23:33 Hmm, I am not sure I agree that this would be an i
miu 2016/09/13 00:33:29 Generally, in Chromium code, we try to adhere to t
chfremer 2016/09/14 16:26:01 Thanks. This insight is super useful for me learni
miu 2016/09/16 21:10:25 There is variance in styles/attitudes, depending o
chfremer 2016/09/17 00:09:21 Thanks for this explanation, which I find helpful.
97 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> buffer,
98 const scoped_refptr<media::VideoFrame>& frame) override {
99 BrowserThread::PostTask(
100 BrowserThread::IO, FROM_HERE,
101 base::Bind(&VideoFrameReceiver::OnIncomingCapturedVideoFrame, receiver_,
102 base::Passed(&buffer), frame));
103 }
104
105 void OnError() override {
106 BrowserThread::PostTask(
107 BrowserThread::IO, FROM_HERE,
108 base::Bind(&VideoFrameReceiver::OnError, receiver_));
109 }
110
111 void OnLog(const std::string& message) override {
112 BrowserThread::PostTask(
113 BrowserThread::IO, FROM_HERE,
114 base::Bind(&VideoFrameReceiver::OnLog, receiver_, message));
115 }
116
117 void OnBufferDestroyed(int buffer_id_to_drop) override {
118 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
miu 2016/09/12 20:16:46 This makes me wonder whether receiver_ needs to be
chfremer 2016/09/12 22:23:33 Thanks. This is an interesting question. I haven't
chfremer 2016/09/14 16:26:01 After thinking about it more carefully, I don't th
miu 2016/09/16 21:10:25 What I was thinking (I now realize is not quite ri
chfremer 2016/09/17 00:09:21 Yes, that is exactly right. Adding the decorator b
119 base::Bind(&VideoFrameReceiver::OnBufferDestroyed,
120 receiver_, buffer_id_to_drop));
121 }
122
123 private:
124 base::WeakPtr<VideoFrameReceiver> receiver_;
125 };
126
82 } // anonymous namespace 127 } // anonymous namespace
83 128
84 struct VideoCaptureController::ControllerClient { 129 struct VideoCaptureController::ControllerClient {
85 ControllerClient(VideoCaptureControllerID id, 130 ControllerClient(VideoCaptureControllerID id,
86 VideoCaptureControllerEventHandler* handler, 131 VideoCaptureControllerEventHandler* handler,
87 base::ProcessHandle render_process, 132 base::ProcessHandle render_process,
88 media::VideoCaptureSessionId session_id, 133 media::VideoCaptureSessionId session_id,
89 const media::VideoCaptureParams& params) 134 const media::VideoCaptureParams& params)
90 : controller_id(id), 135 : controller_id(id),
91 event_handler(handler), 136 event_handler(handler),
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 186
142 base::WeakPtr<VideoCaptureController> 187 base::WeakPtr<VideoCaptureController>
143 VideoCaptureController::GetWeakPtrForIOThread() { 188 VideoCaptureController::GetWeakPtrForIOThread() {
144 return weak_ptr_factory_.GetWeakPtr(); 189 return weak_ptr_factory_.GetWeakPtr();
145 } 190 }
146 191
147 std::unique_ptr<media::VideoCaptureDevice::Client> 192 std::unique_ptr<media::VideoCaptureDevice::Client>
148 VideoCaptureController::NewDeviceClient() { 193 VideoCaptureController::NewDeviceClient() {
149 DCHECK_CURRENTLY_ON(BrowserThread::IO); 194 DCHECK_CURRENTLY_ON(BrowserThread::IO);
150 return base::MakeUnique<VideoCaptureDeviceClient>( 195 return base::MakeUnique<VideoCaptureDeviceClient>(
151 this->GetWeakPtrForIOThread(), buffer_pool_); 196 base::MakeUnique<PostOnIOThreadVideoFrameReceiver>(
197 this->GetWeakPtrForIOThread()),
198 buffer_pool_,
199 base::Bind(&CreateGpuJpegDecoder,
200 base::Bind(&VideoFrameReceiver::OnIncomingCapturedVideoFrame,
201 this->GetWeakPtrForIOThread())));
152 } 202 }
153 203
154 void VideoCaptureController::AddClient( 204 void VideoCaptureController::AddClient(
155 VideoCaptureControllerID id, 205 VideoCaptureControllerID id,
156 VideoCaptureControllerEventHandler* event_handler, 206 VideoCaptureControllerEventHandler* event_handler,
157 base::ProcessHandle render_process, 207 base::ProcessHandle render_process,
158 media::VideoCaptureSessionId session_id, 208 media::VideoCaptureSessionId session_id,
159 const media::VideoCaptureParams& params) { 209 const media::VideoCaptureParams& params) {
160 DCHECK_CURRENTLY_ON(BrowserThread::IO); 210 DCHECK_CURRENTLY_ON(BrowserThread::IO);
161 DVLOG(1) << "VideoCaptureController::AddClient() -- id=" << id 211 DVLOG(1) << "VideoCaptureController::AddClient() -- id=" << id
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 VideoCaptureController::GetVideoCaptureFormat() const { 397 VideoCaptureController::GetVideoCaptureFormat() const {
348 DCHECK_CURRENTLY_ON(BrowserThread::IO); 398 DCHECK_CURRENTLY_ON(BrowserThread::IO);
349 return video_capture_format_; 399 return video_capture_format_;
350 } 400 }
351 401
352 VideoCaptureController::~VideoCaptureController() { 402 VideoCaptureController::~VideoCaptureController() {
353 base::STLDeleteContainerPointers(controller_clients_.begin(), 403 base::STLDeleteContainerPointers(controller_clients_.begin(),
354 controller_clients_.end()); 404 controller_clients_.end());
355 } 405 }
356 406
357 void VideoCaptureController::DoIncomingCapturedVideoFrameOnIOThread( 407 void VideoCaptureController::OnIncomingCapturedVideoFrame(
358 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> buffer, 408 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> buffer,
359 const scoped_refptr<VideoFrame>& frame) { 409 const scoped_refptr<VideoFrame>& frame) {
360 DCHECK_CURRENTLY_ON(BrowserThread::IO); 410 DCHECK_CURRENTLY_ON(BrowserThread::IO);
361 const int buffer_id = buffer->id(); 411 const int buffer_id = buffer->id();
362 DCHECK_NE(buffer_id, VideoCaptureBufferPool::kInvalidId); 412 DCHECK_NE(buffer_id, VideoCaptureBufferPool::kInvalidId);
363 413
364 int count = 0; 414 int count = 0;
365 if (state_ == VIDEO_CAPTURE_STATE_STARTED) { 415 if (state_ == VIDEO_CAPTURE_STATE_STARTED) {
366 if (!frame->metadata()->HasKey(VideoFrameMetadata::FRAME_RATE)) { 416 if (!frame->metadata()->HasKey(VideoFrameMetadata::FRAME_RATE)) {
367 frame->metadata()->SetDouble(VideoFrameMetadata::FRAME_RATE, 417 frame->metadata()->SetDouble(VideoFrameMetadata::FRAME_RATE,
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
418 &frame_rate)) { 468 &frame_rate)) {
419 frame_rate = video_capture_format_.frame_rate; 469 frame_rate = video_capture_format_.frame_rate;
420 } 470 }
421 UMA_HISTOGRAM_COUNTS("Media.VideoCapture.FrameRate", frame_rate); 471 UMA_HISTOGRAM_COUNTS("Media.VideoCapture.FrameRate", frame_rate);
422 has_received_frames_ = true; 472 has_received_frames_ = true;
423 } 473 }
424 474
425 buffer_pool_->HoldForConsumers(buffer_id, count); 475 buffer_pool_->HoldForConsumers(buffer_id, count);
426 } 476 }
427 477
428 void VideoCaptureController::DoErrorOnIOThread() { 478 void VideoCaptureController::OnError() {
429 DCHECK_CURRENTLY_ON(BrowserThread::IO); 479 DCHECK_CURRENTLY_ON(BrowserThread::IO);
430 state_ = VIDEO_CAPTURE_STATE_ERROR; 480 state_ = VIDEO_CAPTURE_STATE_ERROR;
431 481
432 for (const auto* client : controller_clients_) { 482 for (const auto* client : controller_clients_) {
433 if (client->session_closed) 483 if (client->session_closed)
434 continue; 484 continue;
435 client->event_handler->OnError(client->controller_id); 485 client->event_handler->OnError(client->controller_id);
436 } 486 }
437 } 487 }
438 488
439 void VideoCaptureController::DoLogOnIOThread(const std::string& message) { 489 void VideoCaptureController::OnLog(const std::string& message) {
440 DCHECK_CURRENTLY_ON(BrowserThread::IO); 490 DCHECK_CURRENTLY_ON(BrowserThread::IO);
441 MediaStreamManager::SendMessageToNativeLog("Video capture: " + message); 491 MediaStreamManager::SendMessageToNativeLog("Video capture: " + message);
442 } 492 }
443 493
444 void VideoCaptureController::DoBufferDestroyedOnIOThread( 494 void VideoCaptureController::OnBufferDestroyed(int buffer_id_to_drop) {
445 int buffer_id_to_drop) {
446 DCHECK_CURRENTLY_ON(BrowserThread::IO); 495 DCHECK_CURRENTLY_ON(BrowserThread::IO);
447 496
448 for (auto* client : controller_clients_) { 497 for (auto* client : controller_clients_) {
449 if (client->session_closed) 498 if (client->session_closed)
450 continue; 499 continue;
451 500
452 if (client->known_buffers.erase(buffer_id_to_drop)) { 501 if (client->known_buffers.erase(buffer_id_to_drop)) {
453 client->event_handler->OnBufferDestroyed(client->controller_id, 502 client->event_handler->OnBufferDestroyed(client->controller_id,
454 buffer_id_to_drop); 503 buffer_id_to_drop);
455 } 504 }
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
507 int session_id, 556 int session_id,
508 const ControllerClients& clients) { 557 const ControllerClients& clients) {
509 for (auto* client : clients) { 558 for (auto* client : clients) {
510 if (client->session_id == session_id) 559 if (client->session_id == session_id)
511 return client; 560 return client;
512 } 561 }
513 return NULL; 562 return NULL;
514 } 563 }
515 564
516 } // namespace content 565 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698