dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / html / media_track_base.lucb
#==============================================================================================
#
# media_track_base - HTML::MediaTrackBase, what AudioTrack and VideoTrack share
#
# DESCRIPTION:
# Port of Libraries/LibWeb/HTML/MediaTrackBase.cpp (lines 1-55) and the inline accessors of
# MediaTrackBase.h: the DOM side of a media resource's track.
# Only a demuxer makes tracks (HTMLMediaElement::on_audio_track_added / on_video_track_added,
# from the PlaybackManager); without one (external/lib_media) none is made, so this runs
# when playback is ported.
#
# Copyright (c) 2025, Gregory Bertilson <gregory@ladybird.org>
# Copyright (c) 2026 the luce-browser authors.
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
## Ported from MediaTrackBase.cpp:13 Web::HTML::MediaTrackBase::MediaTrackBase.
func html_media_track_base_construct(this: HtmlMediaTrackBase*, realm: Realm*, media_element: HtmlMediaElement*, track: MediaTrack):
bind_platform_object_construct_realm(this.bind_platform_object(), realm)
this.m_media_element = media_element
this.m_track_in_playback_manager = track
this.m_id = ak.utf16_string_number_u64((u64)media_track_identifier(&track))
this.m_kind = media_track_kind(&track)
this.m_label = media_track_label(&track)
# https://html.spec.whatwg.org/multipage/media.html#dom-audiotrack-language
# https://html.spec.whatwg.org/multipage/media.html#dom-videotrack-language
# The AudioTrack language and VideoTrack language attributes must return the BCP 47 language tag of the language
# of the track, if it has one, or the empty string otherwise. If the user agent is not able to express that language
# as a BCP 47 language tag (for example because the language information in the media resource's format is a free-form
# string without a defined interpretation), then the method must return the empty string, as if the track had no
# language.
this.m_language = ak.Utf16String()
let language_utf8 = ak.utf16_string_base_to_utf8(&track.m_language.utf16_string_base)
if let locale = web_unicode.parse_unicode_locale_id(ak.string_bytes_as_string_view(&language_utf8)):
let language = web_unicode.locale_id_to_string(&locale)
# NOTE: We specifically want to exclude "und" here, as RFC 5646 says:
#
# The 'und' (Undetermined) primary language subtag identifies linguistic content whose language is not
# determined. This subtag SHOULD NOT be used unless a language tag is required and language information is
# not available or cannot be determined. Omitting the language tag (where permitted) is preferred. The 'und'
# subtag might be useful for protocols that require a language tag to be provided or where a primary language
# subtag is required (such as in "und-Latn"). The 'und' subtag MAY also be useful when matching language tags
# in certain situations.
#
# Matroska's TrackEntry->Language element is required, and will use "und" as a placeholder as mentioned above. We
# don't want to return anything when that placeholder is found:
if not ak.string_eq_string_view(&language, ak.sv("und")):
this.m_language = ak.utf16_string_from_utf8_without_validation(ak.string_bytes_as_string_view(&language))
## Ported from MediaTrackBase.cpp:52 Web::HTML::MediaTrackBase::visit_edges (override).
func html_media_track_base_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (HtmlMediaTrackBase*)base
bind_platform_object_visit_edges_impl(base, visitor)
gc.cell_visitor_visit_t(visitor, this.m_media_element)
## Ported from MediaTrackBase.h:20 Web::HTML::MediaTrackBase::media_element.
pub func html_media_track_base_media_element(this: HtmlMediaTrackBase*) -> HtmlMediaElement*:
return this.m_media_element
## Ported from MediaTrackBase.h:22 Web::HTML::MediaTrackBase::track_in_playback_manager.
pub func html_media_track_base_track_in_playback_manager(this: HtmlMediaTrackBase*) -> MediaTrack:
return this.m_track_in_playback_manager
## Ported from MediaTrackBase.h:24 Web::HTML::MediaTrackBase::id.
pub func html_media_track_base_id(this: HtmlMediaTrackBase*) -> ak.Utf16String:
return this.m_id
## Ported from MediaTrackBase.h:26 Web::HTML::MediaTrackBase::kind.
pub func html_media_track_base_kind(this: HtmlMediaTrackBase*) -> str:
return media_track_kind_to_string(this.m_kind)
## Ported from MediaTrackBase.h:28 Web::HTML::MediaTrackBase::label.
pub func html_media_track_base_label(this: HtmlMediaTrackBase*) -> ak.Utf16String:
return this.m_label
## Ported from MediaTrackBase.h:29 Web::HTML::MediaTrackBase::language.
pub func html_media_track_base_language(this: HtmlMediaTrackBase*) -> ak.Utf16String:
return this.m_language