File: canvas_capture_handler.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (144 lines) | stat: -rw-r--r-- 5,568 bytes parent folder | download | duplicates (5)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIACAPTUREFROMELEMENT_CANVAS_CAPTURE_HANDLER_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIACAPTUREFROMELEMENT_CANVAS_CAPTURE_HANDLER_H_

#include <stddef.h>
#include <stdint.h>

#include <memory>
#include <optional>

#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/task/single_thread_task_runner.h"
#include "base/threading/thread_checker.h"
#include "base/time/time.h"
#include "gpu/GLES2/gl2extchromium.h"
#include "media/base/video_frame_pool.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/video_capture/video_capturer_source.h"
#include "third_party/skia/include/core/SkImageInfo.h"
#include "third_party/skia/include/gpu/ganesh/GrTypes.h"

class SkImage;

namespace base {
class ScopedClosureRunner;
}  // namespace base

namespace gfx {
class Size;
}  // namespace gfx

namespace blink {

class LocalFrame;
class MediaStreamComponent;

// CanvasCaptureHandler acts as the link between Blink side HTMLCanvasElement
// and Chrome side VideoCapturerSource. It is responsible for handling
// SkImage instances sent from the Blink side, convert them to
// media::VideoFrame and plug them to the MediaStreamTrack.
// CanvasCaptureHandler instance is owned by a blink::CanvasDrawListener which
// is owned by a CanvasCaptureMediaStreamTrack.
// All methods are called on the same thread as construction and destruction,
// i.e. the Main Render thread. Note that a CanvasCaptureHandlerDelegate is
// used to send back frames to |io_task_runner_|, i.e. IO thread.
class MODULES_EXPORT CanvasCaptureHandler {
 public:
  CanvasCaptureHandler(const CanvasCaptureHandler&) = delete;
  CanvasCaptureHandler& operator=(const CanvasCaptureHandler&) = delete;

  ~CanvasCaptureHandler();

  // Creates a CanvasCaptureHandler instance and updates UMA histogram.
  static std::unique_ptr<CanvasCaptureHandler> CreateCanvasCaptureHandler(
      LocalFrame* frame,
      const gfx::Size& size,
      double frame_rate,
      scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
      scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
      MediaStreamComponent** component);

  // Return a callback to provide a new frame. See the method
  // CanvasCaptureHandler::GetNewFrameCallback for more details.
  using NewFrameCallback =
      base::OnceCallback<void(scoped_refptr<media::VideoFrame>)>;
  NewFrameCallback GetNewFrameCallback();
  bool CanDiscardAlpha() const { return can_discard_alpha_; }
  bool NeedsNewFrame() const;

  // Functions called by VideoCapturerSource implementation.
  void StartVideoCapture(
      const media::VideoCaptureParams& params,
      VideoCaptureDeliverFrameCB new_frame_callback,
      VideoCapturerSource::VideoCaptureRunningCallbackCB running_callback);
  void RequestRefreshFrame();
  void StopVideoCapture();
  void SetCanDiscardAlpha(bool can_discard_alpha) {
    can_discard_alpha_ = can_discard_alpha;
  }

 private:
  // A VideoCapturerSource instance is created, which is responsible for handing
  // stop&start callbacks back to CanvasCaptureHandler. That VideoCapturerSource
  // is then plugged into a MediaStreamTrack passed as |track|, and it is owned
  // by the Blink side MediaStreamSource.
  CanvasCaptureHandler(
      LocalFrame* frame,
      const gfx::Size& size,
      double frame_rate,
      scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
      scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
      MediaStreamComponent** component);

  void OnNewFrameCallback(base::ScopedClosureRunner pending_call_tracker,
                          base::TimeTicks this_frame_ticks,
                          const gfx::ColorSpace& color_space,
                          scoped_refptr<media::VideoFrame> video_frame);

  void SendFrame(base::TimeTicks this_frame_ticks,
                 const gfx::ColorSpace& color_space,
                 scoped_refptr<media::VideoFrame> video_frame);

  void AddVideoCapturerSourceToVideoTrack(
      scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
      LocalFrame* frame,
      std::unique_ptr<VideoCapturerSource> source,
      MediaStreamComponent** component);

  // Send a refresh frame.
  void SendRefreshFrame();

  // Object that does all the work of running |new_frame_callback_|.
  // Destroyed on |frame_callback_task_runner_| after the class is destroyed.
  class CanvasCaptureHandlerDelegate;

  media::VideoCaptureFormat capture_format_;
  bool can_discard_alpha_ = false;
  bool ask_for_new_frame_ = false;
  std::optional<base::TimeTicks> first_frame_ticks_;
  scoped_refptr<media::VideoFrame> last_frame_;

  // The following attributes ensure that CanvasCaptureHandler emits
  // frames with monotonically increasing timestamps.
  bool deferred_request_refresh_frame_ = false;

  // The number of outsanding calls to SendNewFrame that have not made their
  // callback.
  uint32_t pending_send_new_frame_calls_ = 0;

  const scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
  std::unique_ptr<CanvasCaptureHandlerDelegate> delegate_;

  // Bound to Main Render thread.
  THREAD_CHECKER(main_render_thread_checker_);
  base::WeakPtrFactory<CanvasCaptureHandler> weak_ptr_factory_{this};
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIACAPTUREFROMELEMENT_CANVAS_CAPTURE_HANDLER_H_