summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/video_rvfc/video_frame_request_callback_collection.h
blob: d66cf107f5becb4674f6c29927a9adf46a5ddb4a (plain)
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
// Copyright 2019 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.

#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_VIDEO_RVFC_VIDEO_FRAME_REQUEST_CALLBACK_COLLECTION_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_VIDEO_RVFC_VIDEO_FRAME_REQUEST_CALLBACK_COLLECTION_H_

#include "third_party/blink/renderer/bindings/modules/v8/v8_video_frame_metadata.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_video_frame_request_callback.h"
#include "third_party/blink/renderer/core/dom/dom_high_res_time_stamp.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/bindings/name_client.h"
#include "third_party/blink/renderer/platform/heap/handle.h"

namespace blink {

class ExecutionContext;

// Class that allows the registration and unregistration of generic
// VideoFrameCallbacks. Used to store to pending video.requestVideoFrameCallback
// requests, and to propagate the results of the request once it completes.
class MODULES_EXPORT VideoFrameRequestCallbackCollection final
    : public GarbageCollected<VideoFrameRequestCallbackCollection>,
      public NameClient {
 public:
  explicit VideoFrameRequestCallbackCollection(ExecutionContext*);
  ~VideoFrameRequestCallbackCollection() final = default;

  using CallbackId = int;

  // Abstract class that generalizes a video.rVFC callback.
  class MODULES_EXPORT VideoFrameCallback
      : public GarbageCollected<VideoFrameCallback>,
        public NameClient {
   public:
    virtual void Trace(Visitor*) const {}
    const char* NameInHeapSnapshot() const override {
      return "VideoFrameCallback";
    }
    ~VideoFrameCallback() override = default;

    virtual void Invoke(double, const VideoFrameMetadata*) = 0;

    int Id() const { return id_; }
    bool IsCancelled() const { return is_cancelled_; }
    void SetId(int id) { id_ = id; }
    void SetIsCancelled(bool is_cancelled) { is_cancelled_ = is_cancelled; }

   protected:
    VideoFrameCallback() = default;

   private:
    int id_ = 0;
    bool is_cancelled_ = false;
  };

  // Wrapper class that bridges the script-based V8 callback into a
  // VideoFrameCallback that can be stored and executed by this collection.
  class MODULES_EXPORT V8VideoFrameCallback : public VideoFrameCallback {
   public:
    void Trace(Visitor*) const override;
    const char* NameInHeapSnapshot() const override {
      return "V8VideoFrameCallback";
    }

    explicit V8VideoFrameCallback(V8VideoFrameRequestCallback*);
    ~V8VideoFrameCallback() override = default;

    void Invoke(double, const VideoFrameMetadata* metadata) override;

   private:
    Member<V8VideoFrameRequestCallback> callback_;
  };

  // Registers a callback and returns its ID.
  // NOTE: Callbacks registered during a call to ExecuteFrameCallbacks won't be
  // run until the next call to ExecuteFrameCallbacks.
  CallbackId RegisterFrameCallback(VideoFrameCallback*);

  // Cancels and removes the callback with the corresponding ID from the
  // collection, or noop if the ID isn't registered.
  void CancelFrameCallback(CallbackId);

  // Invokes all callbacks with the provided information.
  void ExecuteFrameCallbacks(double high_res_now_ms, const VideoFrameMetadata*);

  bool IsEmpty() const { return !frame_callbacks_.size(); }

  virtual void Trace(Visitor*) const;
  const char* NameInHeapSnapshot() const override {
    return "VideoFrameRequestCallbackCollection";
  }

 private:
  using CallbackList = HeapVector<Member<VideoFrameCallback>>;

  CallbackList frame_callbacks_;
  // only non-empty while inside ExecuteCallbacks.
  CallbackList callbacks_to_invoke_;

  CallbackId next_callback_id_ = 0;

  Member<ExecutionContext> context_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_VIDEO_RVFC_VIDEO_FRAME_REQUEST_CALLBACK_COLLECTION_H_