File: devtools_video_consumer.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (103 lines) | stat: -rw-r--r-- 3,943 bytes parent folder | download | duplicates (6)
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_VIDEO_CONSUMER_H_
#define CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_VIDEO_CONSUMER_H_

#include <memory>

#include "base/time/time.h"
#include "components/viz/host/client_frame_sink_video_capturer.h"
#include "content/common/content_export.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "ui/gfx/geometry/size.h"

class SkBitmap;

namespace content {

// This class is the video consumer to FrameSinkVideoCapturerImpl. This class,
// in turn sends video frames to its host via the OnFrameCapturedCallback. Used
// when the VizDisplayCompositor feature is enabled.
// TODO(crbug.com/41391202): This class can probably be merged into
// viz::ClientFrameSinkVideoCapturer.
class CONTENT_EXPORT DevToolsVideoConsumer
    : public viz::mojom::FrameSinkVideoConsumer {
 public:
  using OnFrameCapturedCallback =
      base::RepeatingCallback<void(scoped_refptr<media::VideoFrame> frame)>;

  explicit DevToolsVideoConsumer(OnFrameCapturedCallback callback);

  DevToolsVideoConsumer(const DevToolsVideoConsumer&) = delete;
  DevToolsVideoConsumer& operator=(const DevToolsVideoConsumer&) = delete;

  ~DevToolsVideoConsumer() override;

  // Copies |frame| onto a SkBitmap and returns it.
  static SkBitmap GetSkBitmapFromFrame(scoped_refptr<media::VideoFrame> frame);

  // If not currently capturing, this creates the capturer and starts capturing.
  void StartCapture();

  // Stops capturing and resets |capturer_|.
  void StopCapture();

  // These functions cache the values passed to them and if we're currently
  // capturing, they call the corresponding |capturer_| functions.
  void SetFrameSinkId(const viz::FrameSinkId& frame_sink_id);
  void SetMinCapturePeriod(base::TimeDelta min_capture_period);
  void SetMinAndMaxFrameSize(gfx::Size min_frame_size,
                             gfx::Size max_frame_size);
  void SetFormat(media::VideoPixelFormat format);

 private:
  friend class DevToolsVideoConsumerTest;

  // Sets |capturer_|, sends capture parameters, and starts capture. Normally,
  // CreateCapturer produces the |capturer|, but unittests can provide a mock.
  void InnerStartCapture(
      std::unique_ptr<viz::ClientFrameSinkVideoCapturer> capturer);

  // Checks that |min_frame_size| and |max_frame_size| are in the expected
  // range. Limits are specified in media::limits.
  bool IsValidMinAndMaxFrameSize(gfx::Size min_frame_size,
                                 gfx::Size max_frame_size);

  // viz::mojom::FrameSinkVideoConsumer:
  void OnFrameCaptured(
      ::media::mojom::VideoBufferHandlePtr data,
      ::media::mojom::VideoFrameInfoPtr info,
      const gfx::Rect& content_rect,
      mojo::PendingRemote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks>
          callbacks) override;
  void OnNewSubCaptureTargetVersion(
      uint32_t sub_capture_target_version) override {}
  void OnFrameWithEmptyRegionCapture() override {}
  void OnStopped() override;
  void OnLog(const std::string& /*message*/) override {}

  // Default min frame size is 1x1, as otherwise, nothing would be captured.
  static constexpr gfx::Size kDefaultMinFrameSize = gfx::Size(1, 1);

  // Using an arbitrary default max frame size of 500x500.
  static constexpr gfx::Size kDefaultMaxFrameSize = gfx::Size(500, 500);

  // Callback that is run when a frame is received.
  const OnFrameCapturedCallback callback_;

  // Capture parameters.
  base::TimeDelta min_capture_period_;
  gfx::Size min_frame_size_;
  gfx::Size max_frame_size_;
  viz::FrameSinkId frame_sink_id_;
  media::VideoPixelFormat pixel_format_;

  // If |capturer_| is alive, then we are currently capturing.
  std::unique_ptr<viz::ClientFrameSinkVideoCapturer> capturer_;
};

}  // namespace content

#endif  // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_VIDEO_CONSUMER_H_