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

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: miu's 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 = buffer.handle_provider->GetHandleForInProcessAccess();
405 DCHECK(frame->storage_type() == media::VideoFrame::STORAGE_SHMEM); 404 DCHECK(frame->storage_type() == media::VideoFrame::STORAGE_SHMEM);
406 DCHECK(frame->data(media::VideoFrame::kYPlane) >= buffer->data(0) && 405 DCHECK(frame->data(media::VideoFrame::kYPlane) >= buffer_access->data() &&
407 (frame->data(media::VideoFrame::kYPlane) < 406 (frame->data(media::VideoFrame::kYPlane) <
408 (reinterpret_cast<const uint8_t*>(buffer->data(0)) + 407 (buffer_access->data() + buffer_access->mapped_size())))
409 buffer->mapped_size())))
410 << "VideoFrame does not appear to be backed by Buffer"; 408 << "VideoFrame does not appear to be backed by Buffer";
411 409
412 for (const auto& client : controller_clients_) { 410 for (const auto& client : controller_clients_) {
413 if (client->session_closed || client->paused) 411 if (client->session_closed || client->paused)
414 continue; 412 continue;
415 413
416 // On the first use of a buffer on a client, share the memory handles. 414 // On the first use of a buffer on a client, share the memory handles.
417 auto known_buffers_entry_iter = 415 auto known_buffers_entry_iter =
418 std::find(std::begin(client->known_buffers), 416 std::find(std::begin(client->known_buffers),
419 std::end(client->known_buffers), buffer_id); 417 std::end(client->known_buffers), buffer_id);
420 bool is_new_buffer = false; 418 bool is_new_buffer = false;
421 if (known_buffers_entry_iter == std::end(client->known_buffers)) { 419 if (known_buffers_entry_iter == std::end(client->known_buffers)) {
422 client->known_buffers.push_back(buffer_id); 420 client->known_buffers.push_back(buffer_id);
423 is_new_buffer = true; 421 is_new_buffer = true;
424 } 422 }
425 if (is_new_buffer) 423 if (is_new_buffer) {
426 DoNewBufferOnIOThread(client.get(), buffer.get(), frame); 424 mojo::ScopedSharedBufferHandle handle =
427 425 buffer.handle_provider->GetHandleForInterProcessTransit();
426 client->event_handler->OnBufferCreated(
427 client->controller_id, std::move(handle),
428 buffer_access->mapped_size(), buffer_id);
429 }
428 client->event_handler->OnBufferReady(client->controller_id, buffer_id, 430 client->event_handler->OnBufferReady(client->controller_id, buffer_id,
429 frame); 431 frame);
430 432
431 auto buffers_in_use_entry_iter = 433 auto buffers_in_use_entry_iter =
432 std::find(std::begin(client->buffers_in_use), 434 std::find(std::begin(client->buffers_in_use),
433 std::end(client->buffers_in_use), buffer_id); 435 std::end(client->buffers_in_use), buffer_id);
434 if (buffers_in_use_entry_iter == std::end(client->buffers_in_use)) 436 if (buffers_in_use_entry_iter == std::end(client->buffers_in_use))
435 client->buffers_in_use.push_back(buffer_id); 437 client->buffers_in_use.push_back(buffer_id);
436 else 438 else
437 DCHECK(false) << "Unexpected duplicate buffer: " << buffer_id; 439 DCHECK(false) << "Unexpected duplicate buffer: " << buffer_id;
(...skipping 30 matching lines...) Expand all
468 } 470 }
469 } 471 }
470 472
471 void VideoCaptureController::OnLog(const std::string& message) { 473 void VideoCaptureController::OnLog(const std::string& message) {
472 DCHECK_CURRENTLY_ON(BrowserThread::IO); 474 DCHECK_CURRENTLY_ON(BrowserThread::IO);
473 MediaStreamManager::SendMessageToNativeLog("Video capture: " + message); 475 MediaStreamManager::SendMessageToNativeLog("Video capture: " + message);
474 } 476 }
475 477
476 void VideoCaptureController::OnBufferDestroyed(int buffer_id_to_drop) { 478 void VideoCaptureController::OnBufferDestroyed(int buffer_id_to_drop) {
477 DCHECK_CURRENTLY_ON(BrowserThread::IO); 479 DCHECK_CURRENTLY_ON(BrowserThread::IO);
478 DCHECK(frame_buffer_pool_);
479 480
480 for (const auto& client : controller_clients_) { 481 for (const auto& client : controller_clients_) {
481 if (client->session_closed) 482 if (client->session_closed)
482 continue; 483 continue;
483 484
484 auto known_buffers_entry_iter = 485 auto known_buffers_entry_iter =
485 std::find(std::begin(client->known_buffers), 486 std::find(std::begin(client->known_buffers),
486 std::end(client->known_buffers), buffer_id_to_drop); 487 std::end(client->known_buffers), buffer_id_to_drop);
487 if (known_buffers_entry_iter != std::end(client->known_buffers)) { 488 if (known_buffers_entry_iter != std::end(client->known_buffers)) {
488 client->known_buffers.erase(known_buffers_entry_iter); 489 client->known_buffers.erase(known_buffers_entry_iter);
489 client->event_handler->OnBufferDestroyed(client->controller_id, 490 client->event_handler->OnBufferDestroyed(client->controller_id,
490 buffer_id_to_drop); 491 buffer_id_to_drop);
491 } 492 }
492 } 493 }
493 494
494 buffer_id_to_state_map_.erase(buffer_id_to_drop); 495 buffer_id_to_state_map_.erase(buffer_id_to_drop);
495 } 496 }
496 497
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( 498 VideoCaptureController::ControllerClient* VideoCaptureController::FindClient(
513 VideoCaptureControllerID id, 499 VideoCaptureControllerID id,
514 VideoCaptureControllerEventHandler* handler, 500 VideoCaptureControllerEventHandler* handler,
515 const ControllerClients& clients) { 501 const ControllerClients& clients) {
516 for (const auto& client : clients) { 502 for (const auto& client : clients) {
517 if (client->controller_id == id && client->event_handler == handler) 503 if (client->controller_id == id && client->event_handler == handler)
518 return client.get(); 504 return client.get();
519 } 505 }
520 return nullptr; 506 return nullptr;
521 } 507 }
522 508
523 VideoCaptureController::ControllerClient* VideoCaptureController::FindClient( 509 VideoCaptureController::ControllerClient* VideoCaptureController::FindClient(
524 int session_id, 510 int session_id,
525 const ControllerClients& clients) { 511 const ControllerClients& clients) {
526 for (const auto& client : clients) { 512 for (const auto& client : clients) {
527 if (client->session_id == session_id) 513 if (client->session_id == session_id)
528 return client.get(); 514 return client.get();
529 } 515 }
530 return nullptr; 516 return nullptr;
531 } 517 }
532 518
533 } // namespace content 519 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698