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

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, 12 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 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
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();
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 frame->metadata()->CopyInternalValues(); 395 frame->metadata()->CopyInternalValues();
398 396
399 // Only I420 and Y16 pixel formats are currently supported. 397 // Only I420 and Y16 pixel formats are currently supported.
400 DCHECK(frame->format() == media::PIXEL_FORMAT_I420 || 398 DCHECK(frame->format() == media::PIXEL_FORMAT_I420 ||
401 frame->format() == media::PIXEL_FORMAT_Y16) 399 frame->format() == media::PIXEL_FORMAT_Y16)
402 << "Unsupported pixel format: " 400 << "Unsupported pixel format: "
403 << media::VideoPixelFormatToString(frame->format()); 401 << media::VideoPixelFormatToString(frame->format());
404 402
405 // Sanity-checks to confirm |frame| is actually being backed by |buffer|. 403 // Sanity-checks to confirm |frame| is actually being backed by |buffer|.
404 auto buffer_access = buffer.access_provider->GetReadWriteAccess();
406 DCHECK(frame->storage_type() == media::VideoFrame::STORAGE_SHMEM); 405 DCHECK(frame->storage_type() == media::VideoFrame::STORAGE_SHMEM);
407 DCHECK(frame->data(media::VideoFrame::kYPlane) >= buffer->data(0) && 406 DCHECK(frame->data(media::VideoFrame::kYPlane) >= buffer_access->data() &&
408 (frame->data(media::VideoFrame::kYPlane) < 407 (frame->data(media::VideoFrame::kYPlane) <
409 (reinterpret_cast<const uint8_t*>(buffer->data(0)) + 408 (buffer_access->data() + buffer_access->mapped_size())))
410 buffer->mapped_size())))
411 << "VideoFrame does not appear to be backed by Buffer"; 409 << "VideoFrame does not appear to be backed by Buffer";
412 410
413 for (const auto& client : controller_clients_) { 411 for (const auto& client : controller_clients_) {
414 if (client->session_closed || client->paused) 412 if (client->session_closed || client->paused)
415 continue; 413 continue;
416 414
417 // 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.
418 auto known_buffers_entry_iter = 416 auto known_buffers_entry_iter =
419 std::find(std::begin(client->known_buffers), 417 std::find(std::begin(client->known_buffers),
420 std::end(client->known_buffers), buffer_id); 418 std::end(client->known_buffers), buffer_id);
421 bool is_new_buffer = false; 419 bool is_new_buffer = false;
422 if (known_buffers_entry_iter == std::end(client->known_buffers)) { 420 if (known_buffers_entry_iter == std::end(client->known_buffers)) {
423 client->known_buffers.push_back(buffer_id); 421 client->known_buffers.push_back(buffer_id);
424 is_new_buffer = true; 422 is_new_buffer = true;
425 } 423 }
426 if (is_new_buffer) 424 if (is_new_buffer) {
427 DoNewBufferOnIOThread(client.get(), buffer.get(), frame); 425 mojo::ScopedSharedBufferHandle handle =
428 426 buffer.access_provider->GetHandleForTransit();
427 client->event_handler->OnBufferCreated(
428 client->controller_id, std::move(handle),
429 buffer_access->mapped_size(), buffer_id);
430 }
429 client->event_handler->OnBufferReady(client->controller_id, buffer_id, 431 client->event_handler->OnBufferReady(client->controller_id, buffer_id,
430 frame); 432 frame);
431 433
432 auto buffers_in_use_entry_iter = 434 auto buffers_in_use_entry_iter =
433 std::find(std::begin(client->buffers_in_use), 435 std::find(std::begin(client->buffers_in_use),
434 std::end(client->buffers_in_use), buffer_id); 436 std::end(client->buffers_in_use), buffer_id);
435 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))
436 client->buffers_in_use.push_back(buffer_id); 438 client->buffers_in_use.push_back(buffer_id);
437 else 439 else
438 DCHECK(false) << "Unexpected duplicate buffer: " << buffer_id; 440 DCHECK(false) << "Unexpected duplicate buffer: " << buffer_id;
(...skipping 30 matching lines...) Expand all
469 } 471 }
470 } 472 }
471 473
472 void VideoCaptureController::OnLog(const std::string& message) { 474 void VideoCaptureController::OnLog(const std::string& message) {
473 DCHECK_CURRENTLY_ON(BrowserThread::IO); 475 DCHECK_CURRENTLY_ON(BrowserThread::IO);
474 MediaStreamManager::SendMessageToNativeLog("Video capture: " + message); 476 MediaStreamManager::SendMessageToNativeLog("Video capture: " + message);
475 } 477 }
476 478
477 void VideoCaptureController::OnBufferDestroyed(int buffer_id_to_drop) { 479 void VideoCaptureController::OnBufferDestroyed(int buffer_id_to_drop) {
478 DCHECK_CURRENTLY_ON(BrowserThread::IO); 480 DCHECK_CURRENTLY_ON(BrowserThread::IO);
479 DCHECK(frame_buffer_pool_);
480 481
481 for (const auto& client : controller_clients_) { 482 for (const auto& client : controller_clients_) {
482 if (client->session_closed) 483 if (client->session_closed)
483 continue; 484 continue;
484 485
485 auto known_buffers_entry_iter = 486 auto known_buffers_entry_iter =
486 std::find(std::begin(client->known_buffers), 487 std::find(std::begin(client->known_buffers),
487 std::end(client->known_buffers), buffer_id_to_drop); 488 std::end(client->known_buffers), buffer_id_to_drop);
488 if (known_buffers_entry_iter != std::end(client->known_buffers)) { 489 if (known_buffers_entry_iter != std::end(client->known_buffers)) {
489 client->known_buffers.erase(known_buffers_entry_iter); 490 client->known_buffers.erase(known_buffers_entry_iter);
490 client->event_handler->OnBufferDestroyed(client->controller_id, 491 client->event_handler->OnBufferDestroyed(client->controller_id,
491 buffer_id_to_drop); 492 buffer_id_to_drop);
492 } 493 }
493 } 494 }
494 495
495 buffer_id_to_state_map_.erase(buffer_id_to_drop); 496 buffer_id_to_state_map_.erase(buffer_id_to_drop);
496 } 497 }
497 498
498 void VideoCaptureController::DoNewBufferOnIOThread(
499 ControllerClient* client,
500 media::VideoCaptureDevice::Client::Buffer* buffer,
501 const scoped_refptr<media::VideoFrame>& frame) {
502 DCHECK_CURRENTLY_ON(BrowserThread::IO);
503 DCHECK_EQ(media::VideoFrame::STORAGE_SHMEM, frame->storage_type());
504
505 const int buffer_id = buffer->id();
506 mojo::ScopedSharedBufferHandle handle =
507 frame_buffer_pool_->GetHandleForTransit(buffer_id);
508 client->event_handler->OnBufferCreated(client->controller_id,
509 std::move(handle),
510 buffer->mapped_size(), buffer_id);
511 }
512
513 VideoCaptureController::ControllerClient* VideoCaptureController::FindClient( 499 VideoCaptureController::ControllerClient* VideoCaptureController::FindClient(
514 VideoCaptureControllerID id, 500 VideoCaptureControllerID id,
515 VideoCaptureControllerEventHandler* handler, 501 VideoCaptureControllerEventHandler* handler,
516 const ControllerClients& clients) { 502 const ControllerClients& clients) {
517 for (const auto& client : clients) { 503 for (const auto& client : clients) {
518 if (client->controller_id == id && client->event_handler == handler) 504 if (client->controller_id == id && client->event_handler == handler)
519 return client.get(); 505 return client.get();
520 } 506 }
521 return nullptr; 507 return nullptr;
522 } 508 }
523 509
524 VideoCaptureController::ControllerClient* VideoCaptureController::FindClient( 510 VideoCaptureController::ControllerClient* VideoCaptureController::FindClient(
525 int session_id, 511 int session_id,
526 const ControllerClients& clients) { 512 const ControllerClients& clients) {
527 for (const auto& client : clients) { 513 for (const auto& client : clients) {
528 if (client->session_id == session_id) 514 if (client->session_id == session_id)
529 return client.get(); 515 return client.get();
530 } 516 }
531 return nullptr; 517 return nullptr;
532 } 518 }
533 519
534 } // namespace content 520 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698