dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / html / html_video_element.lucb
#==============================================================================================
#
# html_video_element - HTML::HTMLVideoElement: <video>, its natural dimensions and poster
#
# DESCRIPTION:
# Port of LibWeb/HTML/HTMLVideoElement.cpp (lines 1-347) and the inline members of
# HTMLVideoElement.h: construction (which moves the mixin tables to HTMLVideoElement's,
# as C++'s vptrs move), initialize, finalize, visit_edges, the poster attribute, the
# VideoBox layout node, the intrinsic video dimensions and videoWidth / videoHeight, the
# natural dimensions (from the poster frame or the video), fetching the poster frame
# (phase 2: Fetch and image decoding), the representation and the current bitmap.
#
#==============================================================================================
# mark: Construction ============================================================================
## Ported from HTMLVideoElement.cpp:28 Web::HTML::HTMLVideoElement::HTMLVideoElement.
pub func html_video_element_construct(this: HtmlVideoElement*, document: DomDocument*, qualified_name: DomQualifiedName):
html_media_element_construct(this.html_media_element(), document, qualified_name)
# The vptrs of the mixin bases move to HTMLVideoElement's tables.
let element = this.html_element()
element.element.slottable_mixin.vtable = &html_video_element_slottable_mixin_vtable
element.element.aria_mixin.vtable = &html_video_element_aria_mixin_vtable
element.element.animatable.vtable = &html_video_element_animatable_vtable
element.global_event_handlers.vtable = &html_video_element_global_event_handlers_vtable
element.form_associated_element.vtable = &html_video_element_form_associated_element_vtable
## Ported from HTMLVideoElement.cpp:35 Web::HTML::HTMLVideoElement::initialize (override).
pub func html_video_element_initialize_impl(base: JsCell*, realm: Realm*):
web_set_prototype_for_interface(base, realm, BindInterfaceName.html_video_element)
html_media_element_initialize_impl(base, realm)
## Ported from HTMLVideoElement.cpp:41 Web::HTML::HTMLVideoElement::finalize (override).
pub func html_video_element_finalize_impl(base: gc.Cell*):
html_media_element_finalize_impl(base)
## Ported from HTMLVideoElement.cpp:46 Web::HTML::HTMLVideoElement::visit_edges (override).
pub func html_video_element_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (HtmlVideoElement*)base
html_media_element_visit_edges_impl(base, visitor)
gc.cell_visitor_visit_cell(visitor, (gc.Cell*?)this.m_video_track) # opaque in phase 1
gc.cell_visitor_visit_cell(visitor, (gc.Cell*?)this.m_fetch_controller) # opaque in phase 1
## Ported from HTMLVideoElement.cpp:53 Web::HTML::HTMLVideoElement::attribute_changed (override).
pub func html_video_element_attribute_changed_impl(base: DomElement*, name: ak.FlyString, old_value: ak.String?, value: ak.String?, namespace_: ak.FlyString?):
let this = (HtmlVideoElement*)base
html_media_element_attribute_changed_impl(base, name, old_value, value, namespace_)
if ak.fly_string_eq_fly_string(&name, &html_attribute_names_poster):
html_video_element_determine_element_poster_frame(this, value) else trap("FIXME: should propagate errors")
## Ported from HTMLVideoElement.cpp:62 Web::HTML::HTMLVideoElement::create_layout_node (override).
pub func html_video_element_create_layout_node_impl(base: DomElement*, style: CssComputedProperties*) -> LayoutNode*?:
# heap().allocate<Layout::VideoBox>(...): a layout node's initialize is JS::Cell's, which does nothing.
return realm_create_layout_video_box(dom_element_realm(base), dom_node_document(base.dom_node()), base, style).layout_node()
## Ported from HTMLVideoElement.cpp:67 Web::HTML::HTMLVideoElement::layout_node.
pub func html_video_element_layout_node(this: HtmlVideoElement*) -> LayoutVideoBox*?:
return (LayoutVideoBox*?)dom_node_layout_node(this.dom_node())
## Ported from HTMLVideoElement.cpp:72 Web::HTML::HTMLVideoElement::layout_node.
pub func html_video_element_layout_node_const(this: HtmlVideoElement*) -> LayoutVideoBox*?:
return (LayoutVideoBox*?)dom_node_layout_node(this.dom_node())
## Ported from HTMLVideoElement.h:91 Web::HTML::HTMLVideoElement::supports_dimension_attributes (override).
# https://html.spec.whatwg.org/multipage/media.html#the-video-element:dimension-attributes
pub func html_video_element_supports_dimension_attributes_impl(base: DomElement*) -> bool:
return true
# mark: Intrinsic and natural dimensions ========================================================
## Ported from HTMLVideoElement.cpp:77 Web::HTML::HTMLVideoElement::set_intrinsic_video_dimensions.
pub func html_video_element_set_intrinsic_video_dimensions(this: HtmlVideoElement*, dimensions: gfx.SizeOfU32?):
if this.m_intrinsic_video_dimensions == dimensions:
return
this.m_intrinsic_video_dimensions = dimensions
# Whenever the natural width or natural height of the video changes (including, for example, because the
# selected video track was changed), if the element's readyState attribute is not HAVE_NOTHING,
# AD-HOC: We also use this to set the resolution in the steps that advance the ready state to HAVE_METADATA,
# otherwise we could assert the readyState condition, since any other calls should already have metadata.
# Also, the spec is not explicit on whether to fire the event when the element is emptied, but the check
# for HAVE_NOTHING implies that it should not fire in that case. Therefore, skip the event if the
# dimensions are not available. This matches other browsers.
if dimensions != none:
# the user agent must queue a media element task given the media element to fire an event named resize at the media element.
html_media_element_queue_firing_an_event(this.html_media_element(), html_event_names_resize)
html_media_element_update_natural_dimensions(this.html_media_element())
## Ported from HTMLVideoElement.cpp:101 Web::HTML::HTMLVideoElement::video_width.
# https://html.spec.whatwg.org/multipage/media.html#dom-video-videowidth
pub func html_video_element_video_width(this: HtmlVideoElement*) -> u32:
# The videoWidth IDL attribute must return the intrinsic width of the video in CSS pixels. The videoHeight IDL
# attribute must return the intrinsic height of the video in CSS pixels. If the element's readyState attribute
# is HAVE_NOTHING, then the attributes must return 0.
if html_media_element_ready_state(this.html_media_element()) == HtmlMediaElementReadyState.have_nothing:
return 0
if let dimensions = this.m_intrinsic_video_dimensions:
return dimensions.m_width
# AD-HOC: If the natural dimensions are not available, return 0. The non-normative text says that it should
# return 0 if the dimensions are not known. The HAVE_NOTHING check is likely assumed to cover this
# possibility.
return 0
## Ported from HTMLVideoElement.cpp:117 Web::HTML::HTMLVideoElement::video_height.
# https://html.spec.whatwg.org/multipage/media.html#dom-video-videoheight
pub func html_video_element_video_height(this: HtmlVideoElement*) -> u32:
# The videoWidth IDL attribute must return the intrinsic width of the video in CSS pixels. The videoHeight IDL
# attribute must return the intrinsic height of the video in CSS pixels. If the element's readyState attribute
# is HAVE_NOTHING, then the attributes must return 0.
if html_media_element_ready_state(this.html_media_element()) == HtmlMediaElementReadyState.have_nothing:
return 0
if let dimensions = this.m_intrinsic_video_dimensions:
return dimensions.m_height
# AD-HOC: If the natural dimensions is not available, return 0. The non-normative text says that it should
# return 0 if the dimensions are not known. The HAVE_NOTHING check is likely assumed to cover this
# possibility.
return 0
## Ported from HTMLVideoElement.cpp:133 Web::HTML::HTMLVideoElement::update_intrinsic_video_dimensions (override).
pub func html_video_element_update_intrinsic_video_dimensions_impl(base: HtmlMediaElement*):
let this = (HtmlVideoElement*)base
let sink = html_media_element_selected_video_track_sink(base)
if sink == none:
html_video_element_set_intrinsic_video_dimensions(this, none)
return
let current_frame = media_displaying_video_sink_current_frame(sink else trap("unreachable")) else return
# current_frame->size().to_type<u32>()
let size = gfx.immutable_bitmap_size(current_frame)
let current_frame_size = gfx.SizeOfU32(m_width = (u32)size.m_width, m_height = (u32)size.m_height)
if this.m_intrinsic_video_dimensions == current_frame_size:
return
html_video_element_set_intrinsic_video_dimensions(this, current_frame_size)
## Ported from HTMLVideoElement.cpp:149 Web::HTML::HTMLVideoElement::update_natural_dimensions (override).
pub func html_video_element_update_natural_dimensions_impl(base: HtmlMediaElement*):
let this = (HtmlVideoElement*)base
# https://html.spec.whatwg.org/multipage/media.html#concept-video-intrinsic-width
# The natural width of a video element's playback area is the natural width of the poster frame, if that is
# available and the element currently represents its poster frame; otherwise, it is the natural width of the video
# resource, if that is available; otherwise the natural width is missing.
# The natural height of a video element's playback area is the natural height of the poster frame, if that is
# available and the element currently represents its poster frame; otherwise it is the natural height of the video
# resource, if that is available; otherwise the natural height is missing.
var natural_dimensions: gfx.CssPixelSize? = none
if let size = this.m_intrinsic_video_dimensions:
natural_dimensions = gfx.CssPixelSize(m_width = gfx.css_pixels_from_int((i64)size.m_width), m_height = gfx.css_pixels_from_int((i64)size.m_height))
if html_video_element_current_representation(this) == HtmlVideoElementRepresentation.poster_frame:
if let poster_frame = this.m_poster_frame:
let size = gfx.bitmap_size(poster_frame)
natural_dimensions = gfx.CssPixelSize(m_width = gfx.css_pixels_from_int((i64)size.m_width), m_height = gfx.css_pixels_from_int((i64)size.m_height))
if natural_dimensions == this.m_natural_dimensiosn:
return
dom_node_set_needs_layout_update(this.dom_node(), DomSetNeedsLayoutReason.html_video_element_natural_dimensions_changed)
this.m_natural_dimensiosn = natural_dimensions
## Ported from HTMLVideoElement.h:44 Web::HTML::HTMLVideoElement::natural_media_size.
pub func html_video_element_natural_media_size(this: HtmlVideoElement*) -> gfx.SizeOfU32?:
return this.m_intrinsic_video_dimensions
## Ported from HTMLVideoElement.h:45 Web::HTML::HTMLVideoElement::natural_element_size.
pub func html_video_element_natural_element_size(this: HtmlVideoElement*) -> gfx.CssPixelSize?:
return this.m_natural_dimensiosn
## Ported from HTMLVideoElement.h:47 Web::HTML::HTMLVideoElement::poster_frame.
pub func html_video_element_poster_frame(this: HtmlVideoElement*) -> gfx.Bitmap*?:
return this.m_poster_frame
# mark: The poster frame ========================================================================
## determine_element_poster_frame's `static constexpr auto finalize = [](HTMLVideoElement& self, RefPtr<Gfx::Bitmap> poster_frame) { ... }`.
func html_video_element_finalize_poster_frame(this: HtmlVideoElement*, poster_frame: gfx.Bitmap*?):
this.m_poster_frame = poster_frame
# self.m_load_event_delayer.clear(): the delayer's destructor stops delaying the load event.
if let delayer = this.m_load_event_delayer:
var released = delayer
dom_document_load_event_delayer_destruct(&released)
this.m_load_event_delayer = none
this.m_fetch_controller = none
html_media_element_update_natural_dimensions(this.html_media_element())
## The captures of the poster fetch's lambdas: `[weak_self = GC::Weak(*this)]`.
struct HtmlVideoElementPosterFetch:
var weak_self: gc.Weak[HtmlVideoElement]
## process_response: read the body of an image response, or finalize with no poster frame.
static func process_response(context: void*?, response_: FetchResponse*):
let captures = (HtmlVideoElementPosterFetch*)(context else trap("poster processResponse: no captures"))
let this = gc.weak_ptr(&captures.weak_self) else return
let realm = html_element_realm(this.html_element())
let global = realm_global_object(html_element_realm(this.html_element()))
if fetch_response_is_network_error(response_):
html_video_element_finalize_poster_frame(this, none)
return
var response = response_
let type_ = fetch_response_type(response)
if type_ == FetchResponseType.opaque or type_ == FetchResponseType.opaque_redirect:
# static_cast<Fetch::Infrastructure::FilteredResponse&>(*response).internal_response()
response = fetch_filtered_response_internal_response(response)
let heap = gc.cell_heap(this.cell())
let on_image_data_read = gc.create_function1(heap, ak.Function1[ak.ByteBuffer, unit](call = HtmlVideoElementPosterFetch.image_data_read, context = captures))
let body = fetch_response_body(response) else trap("VERIFY: response->body()")
let on_body_read_error = gc.create_function1(heap, ak.Function1[JsValue, unit](call = HtmlVideoElementPosterFetch.body_read_error, context = captures))
fetch_body_fully_read(body, realm, on_image_data_read, on_body_read_error, .js_object(global))
## on_image_data_read: decode the image; its first frame is the poster frame.
static func image_data_read(context: void*?, image_data: ak.ByteBuffer):
let captures = (HtmlVideoElementPosterFetch*)(context else trap("poster image data: no captures"))
if gc.weak_ptr(&captures.weak_self) == none:
return
# 6. If an image is thus obtained, the poster frame is that image. Otherwise, there is no poster frame.
_ = platform_image_codec_plugin_decode_image(platform_image_codec_plugin_the(), ak.byte_buffer_bytes_const(&image_data), ak.Function1[PlatformDecodedImage*, unit](call = HtmlVideoElementPosterFetch.image_decoded, context = captures), ak.Function1[ak.AkError*, unit](call = HtmlVideoElementPosterFetch.image_decode_failed, context = captures))
## decode_image's on_resolved.
static func image_decoded(context: void*?, image: PlatformDecodedImage*):
let captures = (HtmlVideoElementPosterFetch*)(context else trap("poster decoded: no captures"))
let this = gc.weak_ptr(&captures.weak_self) else return
var poster_frame: gfx.Bitmap*? = none
if not ak.vector_is_empty(&image.frames):
poster_frame = ak.vector_at(&image.frames, 0).bitmap
html_video_element_finalize_poster_frame(this, poster_frame)
## decode_image's on_rejected.
static func image_decode_failed(context: void*?, error_: ak.AkError*):
let captures = (HtmlVideoElementPosterFetch*)(context else trap("poster decode failed: no captures"))
let this = gc.weak_ptr(&captures.weak_self) else return
html_video_element_finalize_poster_frame(this, none)
## on_body_read_error.
static func body_read_error(context: void*?, error_: JsValue):
let captures = (HtmlVideoElementPosterFetch*)(context else trap("poster body error: no captures"))
let this = gc.weak_ptr(&captures.weak_self) else return
html_video_element_finalize_poster_frame(this, none)
## Ported from HTMLVideoElement.cpp:180 Web::HTML::HTMLVideoElement::determine_element_poster_frame.
# https://html.spec.whatwg.org/multipage/media.html#attr-video-poster
pub func html_video_element_determine_element_poster_frame(this: HtmlVideoElement*, poster: ak.String?) -> !:
let realm = html_element_realm(this.html_element())
let vm = realm_vm(realm)
let document = html_element_document(this.html_element())
# 1. If there is an existing instance of this algorithm running for this video element, abort that instance of
# this algorithm without changing the poster frame.
if let fetch_controller = this.m_fetch_controller:
fetch_controller_stop_fetch(fetch_controller)
# 2. If the poster attribute's value is the empty string or if the attribute is absent, then there is no poster
# frame; return.
if poster == none:
html_video_element_finalize_poster_frame(this, none)
return
let poster_value = poster else trap("unreachable")
if ak.string_is_empty(&poster_value):
html_video_element_finalize_poster_frame(this, none)
return
# 3. Let url be the result of encoding-parsing a URL given the poster attribute's value, relative to the element's node document.
let url_record = dom_document_encoding_parse_url(document, ak.string_bytes_as_string_view(&poster_value))
# 4. If url is failure, then return.
if url_record == none:
html_video_element_finalize_poster_frame(this, none)
return
# 5. Let request be a new request whose URL is the resulting URL record, client is the element's node document's
# relevant settings object, destination is "image", initiator type is "video", credentials mode is "include",
# and whose use-URL-credentials flag is set.
let request = fetch_request_create(vm)
fetch_request_set_url(request, url_record else trap("unreachable"))
fetch_request_set_client(request, dom_document_relevant_settings_object(document))
fetch_request_set_destination(request, FetchRequestDestination.image)
fetch_request_set_initiator_type(request, FetchRequestInitiatorType.video)
fetch_request_set_credentials_mode(request, FetchRequestCredentialsMode.include)
fetch_request_set_use_url_credentials(request, true)
# 6. Fetch request. This must delay the load event of the element's node document.
# m_load_event_delayer.emplace(document()): the previous delayer, if any, is destroyed first.
if let delayer = this.m_load_event_delayer:
var released = delayer
dom_document_load_event_delayer_destruct(&released)
var delayer: DomDocumentLoadEventDelayer = ---
dom_document_load_event_delayer_construct(&delayer, document)
this.m_load_event_delayer = delayer
# 7. If an image is thus obtained, the poster frame is that image. Otherwise, there is no poster frame.
let captures = new HtmlVideoElementPosterFetch(weak_self = gc.make_weak(this)) else trap("out of memory")
let algorithms = fetch_algorithms_create_with_process_response(vm, ak.Function1[FetchResponse*, unit](call = HtmlVideoElementPosterFetch.process_response, context = captures))
this.m_fetch_controller = fetch_fetching_fetch(realm, request, algorithms)
return
# mark: The representation ======================================================================
## Ported from HTMLVideoElement.cpp:279 Web::HTML::HTMLVideoElement::current_representation.
pub func html_video_element_current_representation(this: HtmlVideoElement*) -> HtmlVideoElementRepresentation:
let media_element = this.html_media_element()
# https://html.spec.whatwg.org/multipage/media.html#the-video-element:the-video-element-7
# A video element represents what is given for the first matching condition in the list below:
# -> When no video data is available (the element's readyState attribute is either HAVE_NOTHING, or HAVE_METADATA
# but no video data has yet been obtained at all, or the element's readyState attribute is any subsequent value
# but the media resource does not have a video channel)
if html_media_element_ready_state(media_element) == HtmlMediaElementReadyState.have_nothing or html_video_track_list_length(html_media_element_video_tracks(media_element)) == 0:
# The video element represents its poster frame, if any, or else transparent black with no intrinsic dimensions.
return HtmlVideoElementRepresentation.poster_frame if html_video_element_poster_frame(this) != none else HtmlVideoElementRepresentation.transparent_black
# -> When the video element is paused, the current playback position is the first frame of video, and the element's
# show poster flag is set
if html_media_element_paused(media_element) and html_media_element_current_playback_position(media_element) == 0.0 and html_media_element_show_poster(media_element):
# The video element represents its poster frame, if any, or else the first frame of the video.
return HtmlVideoElementRepresentation.poster_frame if html_video_element_poster_frame(this) != none else HtmlVideoElementRepresentation.first_video_frame
# -> When the video element is paused, and the frame of video corresponding to the current playback position
# is not available (e.g. because the video is seeking or buffering)
#
# The video element represents the last frame of the video to have been rendered.
#
# NOTE: We don't need to check this condition, as seeking is asynchronous, and the last available frame
# will be kept until the seek completes.
# -> When the video element is neither potentially playing nor paused (e.g. when seeking or stalled)
if not html_media_element_potentially_playing(media_element) and not html_media_element_paused(media_element):
# The video element represents the last frame of the video to have been rendered.
return HtmlVideoElementRepresentation.video_frame
# -> When the video element is paused
if html_media_element_paused(media_element):
# The video element represents the frame of video corresponding to the current playback position.
return HtmlVideoElementRepresentation.video_frame
# -> Otherwise (the video element has a video channel and is potentially playing)
#
# The video element represents the frame of video at the continuously increasing "current" position. When the
# current playback position changes such that the last frame rendered is no longer the frame corresponding to
# the current playback position in the video, the new frame must be rendered.
return HtmlVideoElementRepresentation.video_frame
## Ported from HTMLVideoElement.cpp:331 Web::HTML::HTMLVideoElement::bitmap.
pub func html_video_element_bitmap(this: HtmlVideoElement*) -> gfx.ImmutableBitmap*?:
let sink = html_media_element_selected_video_track_sink(this.html_media_element()) else return none
return media_displaying_video_sink_current_frame(sink)