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

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

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

Powered by Google App Engine
This is Rietveld 408576698