Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / external / lib_media / track.lucb

#==============================================================================================
#
#   track - LibMedia's Track and DecoderError
#
#   DESCRIPTION:
#       Port of Libraries/LibMedia/Track.h (lines 1-154) and DecoderError.h (lines 1-146):
#       a track of a media resource, as a demuxer describes it, and the error a demuxer or
#       decoder reports. They are LibMedia's, which this port keeps as a seam without demuxers
#       (playback_manager.lucb): the HTML media element names them, and the playback manager
#       reports its DecoderErrors.
#
#       DecoderError's description is a StringView (a literal) or a ByteString; here it is
#       always a ByteString. DECODER_TRY and the source-location constructors are for
#       decoders (not ported).
#
#       Copyright (c) 2022, Gregory Bertilson <zaggy1024@gmail.com>
#       Copyright (c) 2022-2026, Gregory Bertilson <gregory@ladybird.org>
#       Copyright (c) 2026 the luce-browser authors.
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: Track ===================================================================================

## Ported from Libraries/LibMedia/Track.h:50 Media::Track::Track.
pub func media_track_create(type_: MediaTrackType, identifier: usize, kind: MediaTrackKind, label: ak.Utf16String, language: ak.Utf16String) -> MediaTrack:
    var track = MediaTrack(m_type = type_, m_identifier = identifier, m_kind = kind, m_label = label, m_language = language, m_track_data = MediaTrackData.empty)
    match track.m_type:
        .video:
            track.m_track_data = MediaTrackData.video_data(MediaVideoData())
        .audio:
            track.m_track_data = MediaTrackData.audio_data(MediaAudioData())
        _:
            track.m_track_data = MediaTrackData.empty
    return track

## Ported from Libraries/LibMedia/Track.h:70 Media::Track::type.
pub func media_track_type(this: const MediaTrack*) -> MediaTrackType:
    return this.m_type

## Ported from Libraries/LibMedia/Track.h:71 Media::Track::identifier.
pub func media_track_identifier(this: const MediaTrack*) -> usize:
    return this.m_identifier

## Ported from Libraries/LibMedia/Track.h:72 Media::Track::kind.
pub func media_track_kind(this: const MediaTrack*) -> MediaTrackKind:
    return this.m_kind

## Ported from Libraries/LibMedia/Track.h:73 Media::Track::label.
pub func media_track_label(this: const MediaTrack*) -> ak.Utf16String:
    return this.m_label

## Ported from Libraries/LibMedia/Track.h:74 Media::Track::language.
pub func media_track_language(this: const MediaTrack*) -> ak.Utf16String:
    return this.m_language

## Ported from Libraries/LibMedia/Track.h:76 Media::Track::set_video_data.
pub func media_track_set_video_data(this: MediaTrack*, data: MediaVideoData):
    assert(this.m_type == MediaTrackType.video, "VERIFY: m_type == TrackType::Video")
    this.m_track_data = MediaTrackData.video_data(data)

## Ported from Libraries/LibMedia/Track.h:82 Media::Track::video_data.
pub func media_track_video_data(this: MediaTrack) -> MediaVideoData:
    assert(this.m_type == MediaTrackType.video, "VERIFY: m_type == TrackType::Video")
    match this.m_track_data:
        .video_data(data): return data
        _: trap("VERIFY: m_track_data.has<VideoData>()")

## Ported from Libraries/LibMedia/Track.h:100 Media::Track::operator==.
pub func media_track_eq(this: const MediaTrack*, other: const MediaTrack*) -> bool:
    return this.m_type == other.m_type and this.m_identifier == other.m_identifier

## Ported from Libraries/LibMedia/Track.h:105 Media::Track::hash.
pub func media_track_hash(this: const MediaTrack*) -> u32:
    return ak.pair_int_hash((u32)this.m_type, (u32)this.m_identifier)

## Ported from Libraries/LibMedia/Track.h:120 Media::track_kind_to_string.
pub func media_track_kind_to_string(kind: MediaTrackKind) -> str:
    return match kind:
        .none_ => ""
        .alternative => "alternative"
        .captions => "captions"
        .descriptions => "descriptions"
        .main => "main"
        .main_desc => "maindesc"
        .metadata => "metadata"
        .sign => "sign"
        .subtitles => "subtitles"
        .translation => "translation"
        .commentary => "commentary"
        _ => trap("VERIFY_NOT_REACHED")

# mark: DecoderError ============================================================================

## Media::DecoderErrorCategory (Libraries/LibMedia/DecoderError.h:22).
pub enum MediaDecoderErrorCategory as u8:
    aborted = 0
    unknown = 1
    io = 2
    needs_more_input = 3
    end_of_stream = 4
    memory = 5
    ## The input is corrupted.
    corrupted = 6
    ## Invalid call.
    invalid = 7
    ## The input uses features that are not yet implemented.
    not_implemented = 8

## Media::DecoderError (Libraries/LibMedia/DecoderError.h:37).
pub struct MediaDecoderError:
    pub var m_category: MediaDecoderErrorCategory  # C++ default: { DecoderErrorCategory::Unknown }
    pub var m_description: ak.ByteString

## Ported from Libraries/LibMedia/DecoderError.h:39 Media::DecoderError::with_description.
pub func media_decoder_error_with_description(category: MediaDecoderErrorCategory, description: str) -> MediaDecoderError:
    return MediaDecoderError(m_category = category, m_description = ak.byte_string_from_str(description))

## Ported from Libraries/LibMedia/DecoderError.h:66 Media::DecoderError::category.
pub func media_decoder_error_category(this: const MediaDecoderError*) -> MediaDecoderErrorCategory:
    return this.m_category

## Ported from Libraries/LibMedia/DecoderError.h:67 Media::DecoderError::description, as callers take it
## (`MUST(String::from_utf8(error.description()))`).
pub func media_decoder_error_description(this: const MediaDecoderError*) -> ak.String:
    return ak.string_from_byte_string(this.m_description) else trap("MUST: String::from_utf8")

## Ported from Libraries/LibMedia/DecoderError.h:86 Media::decoder_error_category_to_string.
pub func media_decoder_error_category_to_string(category: MediaDecoderErrorCategory) -> str:
    return match category:
        .aborted => "Aborted"
        .unknown => "Unknown"
        .io => "IO"
        .needs_more_input => "NeedsMoreInput"
        .end_of_stream => "EndOfStream"
        .memory => "Memory"
        .corrupted => "Corrupted"
        .invalid => "Invalid"
        .not_implemented => "NotImplemented"
        _ => "Invalid"