blob: 9d53f016db48d24f2cef2b2db267819cf96d4ece [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// 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/bind.h"
#include "base/location.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/thread_task_runner_handle.h"
#include "build/build_config.h"
#include "content/browser/renderer_host/render_widget_host_view_base.h"
#include "content/public/browser/browser_task_traits.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/video_capture_types.h"
#include "mojo/public/cpp/bindings/callback_helpers.h"
#include "ui/base/layout.h"
namespace content {
WebContentsVideoCaptureDevice::WebContentsVideoCaptureDevice(
const GlobalRenderFrameHostId& id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
tracker_ = base::SequenceBound<WebContentsFrameTracker>(
GetUIThreadTaskRunner({}), base::ThreadTaskRunnerHandle::Get(),
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::Crop(
const base::Token& crop_id,
uint32_t crop_version,
base::OnceCallback<void(media::mojom::CropRequestResult)> callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(callback);
tracker_.AsyncCall(&WebContentsFrameTracker::Crop)
.WithArgs(crop_id, crop_version,
mojo::WrapCallbackWithDefaultInvokeIfNotRun(
std::move(callback),
media::mojom::CropRequestResult::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));
}
WebContentsVideoCaptureDevice::WebContentsVideoCaptureDevice() = default;
void WebContentsVideoCaptureDevice::WillStart() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
tracker_.AsyncCall(&WebContentsFrameTracker::WillStartCapturingWebContents)
.WithArgs(capture_params().SuggestConstraints().max_frame_size);
}
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