blob: a0a27948e2283c308733fc40adac27af630121e3 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2018 The Chromium Authors
Shridhar Sundarraj2af6bca2018-04-12 09:26:202// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_VIDEO_CONSUMER_H_
6#define CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_VIDEO_CONSUMER_H_
7
Yuri Wiitalafc5fe702018-06-20 06:14:008#include <memory>
9
Shridhar Sundarraj2af6bca2018-04-12 09:26:2010#include "base/time/time.h"
Saman Samia4c3ee72018-05-25 19:38:0711#include "components/viz/host/client_frame_sink_video_capturer.h"
Shridhar Sundarraj2af6bca2018-04-12 09:26:2012#include "content/common/content_export.h"
Mario Sanchez Prada90afa8ca2019-10-17 21:08:2613#include "mojo/public/cpp/bindings/pending_remote.h"
Shridhar Sundarraj2af6bca2018-04-12 09:26:2014#include "ui/gfx/geometry/size.h"
15
16class SkBitmap;
17
18namespace content {
19
20// This class is the video consumer to FrameSinkVideoCapturerImpl. This class,
21// in turn sends video frames to its host via the OnFrameCapturedCallback. Used
22// when the VizDisplayCompositor feature is enabled.
Alison Gale923a33e2024-04-22 23:34:2823// TODO(crbug.com/41391202): This class can probably be merged into
Saman Samia4c3ee72018-05-25 19:38:0724// viz::ClientFrameSinkVideoCapturer.
Shridhar Sundarraj2af6bca2018-04-12 09:26:2025class CONTENT_EXPORT DevToolsVideoConsumer
26 : public viz::mojom::FrameSinkVideoConsumer {
27 public:
28 using OnFrameCapturedCallback =
29 base::RepeatingCallback<void(scoped_refptr<media::VideoFrame> frame)>;
30
31 explicit DevToolsVideoConsumer(OnFrameCapturedCallback callback);
Peter Boström828b9022021-09-21 02:28:4332
33 DevToolsVideoConsumer(const DevToolsVideoConsumer&) = delete;
34 DevToolsVideoConsumer& operator=(const DevToolsVideoConsumer&) = delete;
35
Shridhar Sundarraj2af6bca2018-04-12 09:26:2036 ~DevToolsVideoConsumer() override;
37
38 // Copies |frame| onto a SkBitmap and returns it.
39 static SkBitmap GetSkBitmapFromFrame(scoped_refptr<media::VideoFrame> frame);
40
41 // If not currently capturing, this creates the capturer and starts capturing.
42 void StartCapture();
43
Saman Samia4c3ee72018-05-25 19:38:0744 // Stops capturing and resets |capturer_|.
Shridhar Sundarraj2af6bca2018-04-12 09:26:2045 void StopCapture();
46
47 // These functions cache the values passed to them and if we're currently
48 // capturing, they call the corresponding |capturer_| functions.
Shridhar Sundarraj2af6bca2018-04-12 09:26:2049 void SetFrameSinkId(const viz::FrameSinkId& frame_sink_id);
50 void SetMinCapturePeriod(base::TimeDelta min_capture_period);
51 void SetMinAndMaxFrameSize(gfx::Size min_frame_size,
52 gfx::Size max_frame_size);
Piotr Bialecki55fa5db2021-12-09 19:15:5253 void SetFormat(media::VideoPixelFormat format);
Shridhar Sundarraj2af6bca2018-04-12 09:26:2054
55 private:
56 friend class DevToolsVideoConsumerTest;
Shridhar Sundarraj2af6bca2018-04-12 09:26:2057
Saman Samia4c3ee72018-05-25 19:38:0758 // Sets |capturer_|, sends capture parameters, and starts capture. Normally,
59 // CreateCapturer produces the |capturer|, but unittests can provide a mock.
Shridhar Sundarraj2af6bca2018-04-12 09:26:2060 void InnerStartCapture(
Saman Samia4c3ee72018-05-25 19:38:0761 std::unique_ptr<viz::ClientFrameSinkVideoCapturer> capturer);
Shridhar Sundarraj2af6bca2018-04-12 09:26:2062
63 // Checks that |min_frame_size| and |max_frame_size| are in the expected
64 // range. Limits are specified in media::limits.
65 bool IsValidMinAndMaxFrameSize(gfx::Size min_frame_size,
66 gfx::Size max_frame_size);
67
68 // viz::mojom::FrameSinkVideoConsumer:
69 void OnFrameCaptured(
Piotr Bialecki55fa5db2021-12-09 19:15:5270 ::media::mojom::VideoBufferHandlePtr data,
Shridhar Sundarraj2af6bca2018-04-12 09:26:2071 ::media::mojom::VideoFrameInfoPtr info,
Shridhar Sundarraj2af6bca2018-04-12 09:26:2072 const gfx::Rect& content_rect,
Mario Sanchez Prada90afa8ca2019-10-17 21:08:2673 mojo::PendingRemote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks>
74 callbacks) override;
Elad Alonb3ecca842023-10-23 06:46:5575 void OnNewSubCaptureTargetVersion(
76 uint32_t sub_capture_target_version) override {}
Elad Alon726424f2022-02-15 19:14:0677 void OnFrameWithEmptyRegionCapture() override {}
Shridhar Sundarraj2af6bca2018-04-12 09:26:2078 void OnStopped() override;
Ilya Nikolaevskiy5dd4977b2020-04-03 11:42:2779 void OnLog(const std::string& /*message*/) override {}
Shridhar Sundarraj2af6bca2018-04-12 09:26:2080
81 // Default min frame size is 1x1, as otherwise, nothing would be captured.
82 static constexpr gfx::Size kDefaultMinFrameSize = gfx::Size(1, 1);
83
84 // Using an arbitrary default max frame size of 500x500.
85 static constexpr gfx::Size kDefaultMaxFrameSize = gfx::Size(500, 500);
86
87 // Callback that is run when a frame is received.
88 const OnFrameCapturedCallback callback_;
89
90 // Capture parameters.
91 base::TimeDelta min_capture_period_;
92 gfx::Size min_frame_size_;
93 gfx::Size max_frame_size_;
94 viz::FrameSinkId frame_sink_id_;
Patrick Brosset123ef882020-12-17 17:54:5495 media::VideoPixelFormat pixel_format_;
Shridhar Sundarraj2af6bca2018-04-12 09:26:2096
Saman Samia4c3ee72018-05-25 19:38:0797 // If |capturer_| is alive, then we are currently capturing.
98 std::unique_ptr<viz::ClientFrameSinkVideoCapturer> capturer_;
Shridhar Sundarraj2af6bca2018-04-12 09:26:2099};
100
101} // namespace content
102
103#endif // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_VIDEO_CONSUMER_H_