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

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

Issue 2573223002: [Mojo Video Capture] Simplify media::VideoCaptureDevice::Client:Buffer to a struct (Closed)
Patch Set: mcasas comments Created 3 years, 11 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 "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
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 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
362 client->buffers_in_use.erase(buffers_in_use_entry_iter); 361 client->buffers_in_use.erase(buffers_in_use_entry_iter);
363 } 362 }
364 363
365 const media::VideoCaptureFormat& 364 const media::VideoCaptureFormat&
366 VideoCaptureController::GetVideoCaptureFormat() const { 365 VideoCaptureController::GetVideoCaptureFormat() const {
367 DCHECK_CURRENTLY_ON(BrowserThread::IO); 366 DCHECK_CURRENTLY_ON(BrowserThread::IO);
368 return video_capture_format_; 367 return video_capture_format_;
369 } 368 }
370 369
371 void VideoCaptureController::OnIncomingCapturedVideoFrame( 370 void VideoCaptureController::OnIncomingCapturedVideoFrame(
372 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> buffer, 371 media::VideoCaptureDevice::Client::Buffer buffer,
373 scoped_refptr<VideoFrame> frame) { 372 scoped_refptr<VideoFrame> frame) {
374 DCHECK_CURRENTLY_ON(BrowserThread::IO); 373 DCHECK_CURRENTLY_ON(BrowserThread::IO);
375 DCHECK(frame_buffer_pool_); 374 const int buffer_id = buffer.id();
376 const int buffer_id = buffer->id();
377 DCHECK_NE(buffer_id, media::VideoCaptureBufferPool::kInvalidId); 375 DCHECK_NE(buffer_id, media::VideoCaptureBufferPool::kInvalidId);
378 376
379 // Insert if not exists. 377 // Insert if not exists.
380 const auto it = 378 const auto it =
381 buffer_id_to_state_map_ 379 buffer_id_to_state_map_
382 .insert(std::make_pair( 380 .insert(std::make_pair(
383 buffer_id, BufferState(buffer_id, buffer->frame_feedback_id(), 381 buffer_id, BufferState(buffer_id, buffer.frame_feedback_id(),
384 consumer_feedback_observer_.get(), 382 consumer_feedback_observer_.get(),
385 frame_buffer_pool_.get(), frame))) 383 frame_buffer_pool_.get())))
386 .first; 384 .first;
387 BufferState& buffer_state = it->second; 385 BufferState& buffer_state = it->second;
388 DCHECK(buffer_state.HasZeroConsumerHoldCount()); 386 DCHECK(buffer_state.HasZeroConsumerHoldCount());
389 387
390 if (state_ == VIDEO_CAPTURE_STATE_STARTED) { 388 if (state_ == VIDEO_CAPTURE_STATE_STARTED) {
391 if (!frame->metadata()->HasKey(VideoFrameMetadata::FRAME_RATE)) { 389 if (!frame->metadata()->HasKey(VideoFrameMetadata::FRAME_RATE)) {
392 frame->metadata()->SetDouble(VideoFrameMetadata::FRAME_RATE, 390 frame->metadata()->SetDouble(VideoFrameMetadata::FRAME_RATE,
393 video_capture_format_.frame_rate); 391 video_capture_format_.frame_rate);
394 } 392 }
395 std::unique_ptr<base::DictionaryValue> metadata = 393 std::unique_ptr<base::DictionaryValue> metadata =
396 frame->metadata()->CopyInternalValues(); 394 frame->metadata()->CopyInternalValues();
397 395
398 // Only I420 and Y16 pixel formats are currently supported. 396 // Only I420 and Y16 pixel formats are currently supported.
399 DCHECK(frame->format() == media::PIXEL_FORMAT_I420 || 397 DCHECK(frame->format() == media::PIXEL_FORMAT_I420 ||
400 frame->format() == media::PIXEL_FORMAT_Y16) 398 frame->format() == media::PIXEL_FORMAT_Y16)
401 << "Unsupported pixel format: " 399 << "Unsupported pixel format: "
402 << media::VideoPixelFormatToString(frame->format()); 400 << media::VideoPixelFormatToString(frame->format());
403 401
404 // Sanity-checks to confirm |frame| is actually being backed by |buffer|. 402 // Sanity-checks to confirm |frame| is actually being backed by |buffer|.
403 auto buffer_access =
404 buffer.handle_provider()->GetHandleForInProcessAccess();
405 DCHECK(frame->storage_type() == media::VideoFrame::STORAGE_SHMEM); 405 DCHECK(frame->storage_type() == media::VideoFrame::STORAGE_SHMEM);
406 DCHECK(frame->data(media::VideoFrame::kYPlane) >= buffer->data(0) && 406 DCHECK(frame->data(media::VideoFrame::kYPlane) >= buffer_access->data() &&
407 (frame->data(media::VideoFrame::kYPlane) < 407 (frame->data(media::VideoFrame::kYPlane) <
408 (reinterpret_cast<const uint8_t*>(buffer->data(0)) + 408 (buffer_access->data() + buffer_access->mapped_size())))
409 buffer->mapped_size())))
410 << "VideoFrame does not appear to be backed by Buffer"; 409 << "VideoFrame does not appear to be backed by Buffer";
411 410
412 for (const auto& client : controller_clients_) { 411 for (const auto& client : controller_clients_) {
413 if (client->session_closed || client->paused) 412 if (client->session_closed || client->paused)
414 continue; 413 continue;
415 414
416 // On the first use of a buffer on a client, share the memory handles. 415 // On the first use of a buffer on a client, share the memory handles.
417 auto known_buffers_entry_iter = 416 auto known_buffers_entry_iter =
418 std::find(std::begin(client->known_buffers), 417 std::find(std::begin(client->known_buffers),
419 std::end(client->known_buffers), buffer_id); 418 std::end(client->known_buffers), buffer_id);
420 bool is_new_buffer = false; 419 bool is_new_buffer = false;
421 if (known_buffers_entry_iter == std::end(client->known_buffers)) { 420 if (known_buffers_entry_iter == std::end(client->known_buffers)) {
422 client->known_buffers.push_back(buffer_id); 421 client->known_buffers.push_back(buffer_id);
423 is_new_buffer = true; 422 is_new_buffer = true;
424 } 423 }
425 if (is_new_buffer) 424 if (is_new_buffer) {
426 DoNewBufferOnIOThread(client.get(), buffer.get(), frame); 425 mojo::ScopedSharedBufferHandle handle =
427 426 buffer.handle_provider()->GetHandleForInterProcessTransit();
427 client->event_handler->OnBufferCreated(
428 client->controller_id, std::move(handle),
429 buffer_access->mapped_size(), buffer_id);
430 }
428 client->event_handler->OnBufferReady(client->controller_id, buffer_id, 431 client->event_handler->OnBufferReady(client->controller_id, buffer_id,
429 frame); 432 frame);
430 433
431 auto buffers_in_use_entry_iter = 434 auto buffers_in_use_entry_iter =
432 std::find(std::begin(client->buffers_in_use), 435 std::find(std::begin(client->buffers_in_use),
433 std::end(client->buffers_in_use), buffer_id); 436 std::end(client->buffers_in_use), buffer_id);
434 if (buffers_in_use_entry_iter == std::end(client->buffers_in_use)) 437 if (buffers_in_use_entry_iter == std::end(client->buffers_in_use))
435 client->buffers_in_use.push_back(buffer_id); 438 client->buffers_in_use.push_back(buffer_id);
436 else 439 else
437 DCHECK(false) << "Unexpected duplicate buffer: " << buffer_id; 440 DCHECK(false) << "Unexpected duplicate buffer: " << buffer_id;
(...skipping 30 matching lines...) Expand all
468 } 471 }
469 } 472 }
470 473
471 void VideoCaptureController::OnLog(const std::string& message) { 474 void VideoCaptureController::OnLog(const std::string& message) {
472 DCHECK_CURRENTLY_ON(BrowserThread::IO); 475 DCHECK_CURRENTLY_ON(BrowserThread::IO);
473 MediaStreamManager::SendMessageToNativeLog("Video capture: " + message); 476 MediaStreamManager::SendMessageToNativeLog("Video capture: " + message);
474 } 477 }
475 478
476 void VideoCaptureController::OnBufferDestroyed(int buffer_id_to_drop) { 479 void VideoCaptureController::OnBufferDestroyed(int buffer_id_to_drop) {
477 DCHECK_CURRENTLY_ON(BrowserThread::IO); 480 DCHECK_CURRENTLY_ON(BrowserThread::IO);
478 DCHECK(frame_buffer_pool_);
479 481
480 for (const auto& client : controller_clients_) { 482 for (const auto& client : controller_clients_) {
481 if (client->session_closed) 483 if (client->session_closed)
482 continue; 484 continue;
483 485
484 auto known_buffers_entry_iter = 486 auto known_buffers_entry_iter =
485 std::find(std::begin(client->known_buffers), 487 std::find(std::begin(client->known_buffers),
486 std::end(client->known_buffers), buffer_id_to_drop); 488 std::end(client->known_buffers), buffer_id_to_drop);
487 if (known_buffers_entry_iter != std::end(client->known_buffers)) { 489 if (known_buffers_entry_iter != std::end(client->known_buffers)) {
488 client->known_buffers.erase(known_buffers_entry_iter); 490 client->known_buffers.erase(known_buffers_entry_iter);
489 client->event_handler->OnBufferDestroyed(client->controller_id, 491 client->event_handler->OnBufferDestroyed(client->controller_id,
490 buffer_id_to_drop); 492 buffer_id_to_drop);
491 } 493 }
492 } 494 }
493 495
494 buffer_id_to_state_map_.erase(buffer_id_to_drop); 496 buffer_id_to_state_map_.erase(buffer_id_to_drop);
495 } 497 }
496 498
497 void VideoCaptureController::DoNewBufferOnIOThread(
498 ControllerClient* client,
499 media::VideoCaptureDevice::Client::Buffer* buffer,
500 const scoped_refptr<media::VideoFrame>& frame) {
501 DCHECK_CURRENTLY_ON(BrowserThread::IO);
502 DCHECK_EQ(media::VideoFrame::STORAGE_SHMEM, frame->storage_type());
503
504 const int buffer_id = buffer->id();
505 mojo::ScopedSharedBufferHandle handle =
506 frame_buffer_pool_->GetHandleForTransit(buffer_id);
507 client->event_handler->OnBufferCreated(client->controller_id,
508 std::move(handle),
509 buffer->mapped_size(), buffer_id);
510 }
511
512 VideoCaptureController::ControllerClient* VideoCaptureController::FindClient( 499 VideoCaptureController::ControllerClient* VideoCaptureController::FindClient(
513 VideoCaptureControllerID id, 500 VideoCaptureControllerID id,
514 VideoCaptureControllerEventHandler* handler, 501 VideoCaptureControllerEventHandler* handler,
515 const ControllerClients& clients) { 502 const ControllerClients& clients) {
516 for (const auto& client : clients) { 503 for (const auto& client : clients) {
517 if (client->controller_id == id && client->event_handler == handler) 504 if (client->controller_id == id && client->event_handler == handler)
518 return client.get(); 505 return client.get();
519 } 506 }
520 return nullptr; 507 return nullptr;
521 } 508 }
522 509
523 VideoCaptureController::ControllerClient* VideoCaptureController::FindClient( 510 VideoCaptureController::ControllerClient* VideoCaptureController::FindClient(
524 int session_id, 511 int session_id,
525 const ControllerClients& clients) { 512 const ControllerClients& clients) {
526 for (const auto& client : clients) { 513 for (const auto& client : clients) {
527 if (client->session_id == session_id) 514 if (client->session_id == session_id)
528 return client.get(); 515 return client.get();
529 } 516 }
530 return nullptr; 517 return nullptr;
531 } 518 }
532 519
533 } // namespace content 520 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698