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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
// Copyright 2019 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_WEBCODECS_VIDEO_FRAME_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBCODECS_VIDEO_FRAME_H_
#include <stdint.h>
#include <optional>
#include "base/feature_list.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_typedefs.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_typedefs.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_video_pixel_format.h"
#include "third_party/blink/renderer/core/html/canvas/canvas_image_source.h"
#include "third_party/blink/renderer/core/imagebitmap/image_bitmap_source.h"
#include "third_party/blink/renderer/modules/canvas/canvas2d/canvas_image_source_util.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/modules/webcodecs/array_buffer_util.h"
#include "third_party/blink/renderer/modules/webcodecs/video_frame_handle.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/graphics/predefined_color_space.h"
#include "third_party/blink/renderer/platform/heap/member.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkRefCnt.h"
// Note: Don't include "media/base/video_frame.h" here without good reason,
// since it includes a lot of non-blink types which can pollute the namespace.
namespace media {
class VideoFrame;
}
namespace blink {
class CanvasImageSource;
class DOMRectReadOnly;
class ExceptionState;
class ExecutionContext;
class PlaneLayout;
class ScriptState;
class VideoColorSpace;
class VideoFrameBufferInit;
class VideoFrameCopyToOptions;
class VideoFrameInit;
class VideoFrameLayout;
class VideoFrameMetadata;
class MODULES_EXPORT VideoFrame final : public ScriptWrappable,
public CanvasImageSource,
public ImageBitmapSource {
DEFINE_WRAPPERTYPEINFO();
public:
// Creates a VideoFrame with a new VideoFrameHandle wrapping |frame|, and
// monitored using |monitoring_source_id|.
VideoFrame(scoped_refptr<media::VideoFrame> frame,
ExecutionContext*,
std::string monitoring_source_id = std::string(),
sk_sp<SkImage> sk_image = nullptr,
bool use_capture_timestamp = false);
// Creates a VideoFrame from an existing handle.
// All frames sharing |handle| will have their |handle_| invalidated if any of
// the frames receives a call to close().
explicit VideoFrame(scoped_refptr<VideoFrameHandle> handle);
~VideoFrame() override;
// video_frame.idl implementation.
static VideoFrame* Create(ScriptState* script_state,
const V8CanvasImageSource* source,
const VideoFrameInit* init,
ExceptionState& exception_state);
static VideoFrame* Create(ScriptState*,
const AllowSharedBufferSource*,
const VideoFrameBufferInit*,
ExceptionState&);
std::optional<V8VideoPixelFormat> format() const;
int64_t timestamp() const;
std::optional<uint64_t> duration() const;
uint32_t codedWidth() const;
uint32_t codedHeight() const;
DOMRectReadOnly* codedRect();
DOMRectReadOnly* visibleRect();
uint32_t rotation() const;
bool flip() const;
uint32_t displayWidth() const;
uint32_t displayHeight() const;
VideoColorSpace* colorSpace();
VideoFrameMetadata* metadata(ExceptionState&);
uint32_t allocationSize(VideoFrameCopyToOptions* options, ExceptionState&);
ScriptPromise<IDLSequence<PlaneLayout>> copyTo(
ScriptState* script_state,
const AllowSharedBufferSource* destination,
VideoFrameCopyToOptions* options,
ExceptionState& exception_state);
// Invalidates |handle_|, releasing underlying media::VideoFrame references.
// This effectively "destroys" all frames sharing the same Handle.
void close();
// Creates a clone of |this|, with a new Handle, referencing the same
// media::VideoFrame. The cloned frame will not be closed when |this| is,
// and its lifetime should be independently managed.
VideoFrame* clone(ExceptionState&);
// Convenience functions
scoped_refptr<VideoFrameHandle> handle() const { return handle_; }
scoped_refptr<media::VideoFrame> frame() const { return handle_->frame(); }
bool WouldTaintOrigin() const override;
// GarbageCollected override
void Trace(Visitor*) const override;
private:
// CanvasImageSource implementation
scoped_refptr<Image> GetSourceImageForCanvas(FlushReason,
SourceImageStatus*,
const gfx::SizeF&) override;
gfx::SizeF ElementSize(const gfx::SizeF&,
const RespectImageOrientationEnum) const override;
bool IsVideoFrame() const override;
bool IsOpaque() const override;
bool IsAccelerated() const override;
void ConvertAndCopyToRGB(scoped_refptr<media::VideoFrame> frame,
const gfx::Rect& src_rect,
const VideoFrameLayout& dest_layout,
base::span<uint8_t> buffer,
PredefinedColorSpace target_color_space);
bool CopyToAsync(ScriptPromiseResolver<IDLSequence<PlaneLayout>>*,
scoped_refptr<media::VideoFrame> frame,
gfx::Rect src_rect,
const AllowSharedBufferSource* destination,
const VideoFrameLayout& dest_layout);
// ImageBitmapSource implementation
static constexpr uint64_t kCpuEfficientFrameSize = 320u * 240u;
ImageBitmapSourceStatus CheckUsability() const override;
ScriptPromise<ImageBitmap> CreateImageBitmap(
ScriptState*,
std::optional<gfx::Rect> crop_rect,
const ImageBitmapOptions*,
ExceptionState&) override;
// Underlying frame
scoped_refptr<VideoFrameHandle> handle_;
// Caches
Member<DOMRectReadOnly> coded_rect_;
Member<DOMRectReadOnly> visible_rect_;
Member<VideoColorSpace> color_space_;
Member<VideoColorSpace> empty_color_space_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_WEBCODECS_VIDEO_FRAME_H_
|