1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124

content / browser / media / capture / web_contents_video_capture_device.cc [blame]

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/media/capture/web_contents_video_capture_device.h"

#include <utility>

#include "base/functional/bind.h"
#include "base/location.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/task/single_thread_task_runner.h"
#include "build/build_config.h"
#include "content/browser/media/capture/mouse_cursor_overlay_controller.h"
#include "content/browser/renderer_host/render_widget_host_view_base.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "media/capture/mojom/video_capture_types.mojom.h"
#include "media/capture/video/video_capture_feedback.h"
#include "media/capture/video_capture_types.h"
#include "mojo/public/cpp/bindings/callback_helpers.h"

namespace content {

WebContentsVideoCaptureDevice::WebContentsVideoCaptureDevice(
    const GlobalRenderFrameHostId& id) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  tracker_ = base::SequenceBound<WebContentsFrameTracker>(
      GetUIThreadTaskRunner({}),
      base::SingleThreadTaskRunner::GetCurrentDefault(),
      weak_ptr_factory_.GetWeakPtr(), cursor_controller());
  tracker_
      .AsyncCall(
          &WebContentsFrameTracker::SetWebContentsAndContextFromRoutingId)
      .WithArgs(id);
}

WebContentsVideoCaptureDevice::~WebContentsVideoCaptureDevice() = default;

// static
std::unique_ptr<WebContentsVideoCaptureDevice>
WebContentsVideoCaptureDevice::Create(const std::string& device_id) {
  WebContentsMediaCaptureId media_id;
  if (!WebContentsMediaCaptureId::Parse(device_id, &media_id)) {
    return nullptr;
  }

  const GlobalRenderFrameHostId routing_id(media_id.render_process_id,
                                           media_id.main_render_frame_id);
  return std::make_unique<WebContentsVideoCaptureDevice>(routing_id);
}

void WebContentsVideoCaptureDevice::ApplySubCaptureTarget(
    media::mojom::SubCaptureTargetType type,
    const base::Token& target,
    uint32_t sub_capture_target_version,
    base::OnceCallback<void(media::mojom::ApplySubCaptureTargetResult)>
        callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(callback);

  tracker_.AsyncCall(&WebContentsFrameTracker::ApplySubCaptureTarget)
      .WithArgs(type, target, sub_capture_target_version,
                mojo::WrapCallbackWithDefaultInvokeIfNotRun(
                    std::move(callback),
                    media::mojom::ApplySubCaptureTargetResult::kErrorGeneric));
}

void WebContentsVideoCaptureDevice::OnFrameCaptured(
    media::mojom::VideoBufferHandlePtr data,
    media::mojom::VideoFrameInfoPtr info,
    const gfx::Rect& content_rect,
    mojo::PendingRemote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks>
        callbacks) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (info->metadata.source_size.has_value()) {
    const gfx::Size new_size = *info->metadata.source_size;
    // Only update the captured content size when the size changes. See also
    // the comment in WebContentsFrameTracker::SetCapturedContentSize which
    // expects that behavior.
    if (new_size != content_size_) {
      tracker_.AsyncCall(&WebContentsFrameTracker::SetCapturedContentSize)
          .WithArgs(new_size);
      content_size_ = new_size;
    }
  }

  FrameSinkVideoCaptureDevice::OnFrameCaptured(
      std::move(data), std::move(info), content_rect, std::move(callbacks));
}

void WebContentsVideoCaptureDevice::OnUtilizationReport(
    media::VideoCaptureFeedback feedback) {
  tracker_.AsyncCall(&WebContentsFrameTracker::OnUtilizationReport)
      .WithArgs(feedback);

  // We still want to capture the base class' behavior for utilization reports.
  FrameSinkVideoCaptureDevice::OnUtilizationReport(std::move(feedback));
}

WebContentsVideoCaptureDevice::WebContentsVideoCaptureDevice() = default;

void WebContentsVideoCaptureDevice::WillStart() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  tracker_.AsyncCall(&WebContentsFrameTracker::WillStartCapturingWebContents)
      .WithArgs(capture_params().SuggestConstraints().max_frame_size,
                capture_params().is_high_dpi_enabled);
}

void WebContentsVideoCaptureDevice::DidStop() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  tracker_.AsyncCall(&WebContentsFrameTracker::DidStopCapturingWebContents);

  // Currently, the video capture device is effectively a single-use object, so
  // resetting capture_size_ isn't strictly necessary, but it helps ensure that
  // SetCapturedContentSize works consistently in case the objects get reused in
  // the future.
  content_size_.SetSize(0, 0);
}

}  // namespace content