Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

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

#==============================================================================================
#
#   time_ranges - LibMedia's TimeRanges: sorted, merged ranges of media time
#
#   DESCRIPTION:
#       Port of Libraries/LibMedia/TimeRanges.h (lines 1-95) and TimeRanges.cpp (lines
#       1-139): a sorted, non-overlapping collection of time ranges, merged when they overlap
#       or touch, the backing store of buffered ranges (PlaybackManager::buffered_time_ranges,
#       which HTMLMediaElement::buffered reads). The formatters are left out.
#
#       Copyright (c) 2026-present, the Ladybird developers.
#       Copyright (c) 2026 the luce-browser authors.
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Media::TimeRanges::Range (Libraries/LibMedia/TimeRanges.h:20).
pub struct MediaTimeRange:
    pub var start: ak.Duration
    pub var end: ak.Duration

## Media::TimeRanges (Libraries/LibMedia/TimeRanges.h:18).
pub struct MediaTimeRanges:
    ## Sorted by start time, non-overlapping.
    pub var ranges: ak.Vector[MediaTimeRange]

## `a < b` on durations.
func media_duration_lt(a: ak.Duration, b: ak.Duration) -> bool:
    return ak.duration_cmp(&a, b) < 0

## `min(a, b)` on durations.
func media_duration_min(a: ak.Duration, b: ak.Duration) -> ak.Duration:
    return a if media_duration_lt(a, b) else b

## `max(a, b)` on durations.
func media_duration_max(a: ak.Duration, b: ak.Duration) -> ak.Duration:
    return b if media_duration_lt(a, b) else a

## The comparator of add_range and range_at_or_after: `needle <=> range.start`.
func media_time_ranges_compare_start(context: void*?, needle: const ak.Duration*, range: const MediaTimeRange*) -> i32:
    _ = context
    return ak.duration_cmp(needle, range.start)

## binary_search(m_ranges, point, &index, compare_start): the index the search ends at.
func media_time_ranges_search(this: const MediaTimeRanges*, point: ak.Duration) -> usize:
    var index: usize = 0
    _ = ak.binary_search(ak.vector_span_const(&this.ranges), point, &index, ak.Function2[const ak.Duration*, const MediaTimeRange*, i32](call = media_time_ranges_compare_start, context = none))
    return index

## Ported from Libraries/LibMedia/TimeRanges.cpp:12 Media::TimeRanges::add_range.
pub func media_time_ranges_add_range(this: MediaTimeRanges*, start: ak.Duration, end: ak.Duration):
    if not media_duration_lt(start, end):
        return
    if ak.vector_is_empty(&this.ranges):
        ak.vector_append_t(&this.ranges, MediaTimeRange(start = start, end = end))
        return

    # Find the index we should insert at. This index would point to either the range overlapping the start of the new
    # range, or the range following it if there is no overlap.
    var insert_index = media_time_ranges_search(this, start)
    if media_duration_lt(ak.vector_at(&this.ranges, insert_index).end, start):
        insert_index += 1

    # Determine the range of ranges that will be made contiguous by this new range.
    var merged_start = start
    var merged_end = end
    var merge_end_index = insert_index

    if merge_end_index < ak.vector_size(&this.ranges):
        merged_start = media_duration_min(merged_start, ak.vector_at(&this.ranges, merge_end_index).start)

        while merge_end_index < ak.vector_size(&this.ranges) and not media_duration_lt(merged_end, ak.vector_at(&this.ranges, merge_end_index).start):
            merged_end = media_duration_max(merged_end, ak.vector_at(&this.ranges, merge_end_index).end)
            merge_end_index += 1

    # Replace the found range with our new merged range if applicable.
    if merge_end_index > insert_index:
        *ak.vector_at(&this.ranges, insert_index) = MediaTimeRange(start = merged_start, end = merged_end)
        for _ in 0..<merge_end_index - insert_index - 1:
            ak.vector_remove(&this.ranges, insert_index + 1)
    else:
        ak.vector_insert(&this.ranges, insert_index, MediaTimeRange(start = merged_start, end = merged_end))

## Ported from Libraries/LibMedia/TimeRanges.cpp:53 Media::TimeRanges::remove_range.
pub func media_time_ranges_remove_range(this: MediaTimeRanges*, start: ak.Duration, end: ak.Duration):
    if not media_duration_lt(start, end):
        return

    var new_ranges: ak.Vector[MediaTimeRange]
    for range in ak.vector_span(&this.ranges):
        if not media_duration_lt(start, range.end) or not media_duration_lt(range.start, end):
            ak.vector_append_t(&new_ranges, range)
            continue

        if media_duration_lt(range.start, start):
            ak.vector_append_t(&new_ranges, MediaTimeRange(start = range.start, end = start))
        if media_duration_lt(end, range.end):
            ak.vector_append_t(&new_ranges, MediaTimeRange(start = end, end = range.end))
    this.ranges = new_ranges

## Ported from Libraries/LibMedia/TimeRanges.h:39 Media::TimeRanges::size.
pub func media_time_ranges_size(this: const MediaTimeRanges*) -> usize:
    return ak.vector_size(&this.ranges)

## Ported from Libraries/LibMedia/TimeRanges.h:40 Media::TimeRanges::is_empty.
pub func media_time_ranges_is_empty(this: const MediaTimeRanges*) -> bool:
    return ak.vector_is_empty(&this.ranges)

## Ported from Libraries/LibMedia/TimeRanges.cpp:73 Media::TimeRanges::highest_end_time.
pub func media_time_ranges_highest_end_time(this: const MediaTimeRanges*) -> ak.Duration:
    if ak.vector_is_empty(&this.ranges):
        return ak.duration_zero()
    return ak.vector_at_const(&this.ranges, ak.vector_size(&this.ranges) - 1).end

## Ported from Libraries/LibMedia/TimeRanges.cpp:80 Media::TimeRanges::coalesced.
pub func media_time_ranges_coalesced(this: const MediaTimeRanges*, threshold: ak.Duration) -> MediaTimeRanges:
    var result = MediaTimeRanges()
    if ak.vector_is_empty(&this.ranges):
        return result

    ak.vector_append_t(&result.ranges, *ak.vector_at_const(&this.ranges, 0))
    for i in 1..<ak.vector_size(&this.ranges):
        let range = *ak.vector_at_const(&this.ranges, i)
        let last = ak.vector_at(&result.ranges, ak.vector_size(&result.ranges) - 1)
        let gap = ak.duration_sub(&range.start, last.end)
        if ak.duration_cmp(&gap, threshold) <= 0:
            last.end = media_duration_max(last.end, range.end)
        else:
            ak.vector_append_t(&result.ranges, range)
    return result

## Ported from Libraries/LibMedia/TimeRanges.cpp:98 Media::TimeRanges::intersection.
pub func media_time_ranges_intersection(this: const MediaTimeRanges*, other: const MediaTimeRanges*) -> MediaTimeRanges:
    var result = MediaTimeRanges()
    var i: usize = 0
    var j: usize = 0

    while i < ak.vector_size(&this.ranges) and j < ak.vector_size(&other.ranges):
        let a = *ak.vector_at_const(&this.ranges, i)
        let b = *ak.vector_at_const(&other.ranges, j)

        let inter_start = media_duration_max(a.start, b.start)
        let inter_end = media_duration_min(a.end, b.end)

        if media_duration_lt(inter_start, inter_end):
            ak.vector_append_t(&result.ranges, MediaTimeRange(start = inter_start, end = inter_end))

        # Advance the range that ends first.
        if media_duration_lt(a.end, b.end):
            i += 1
        else:
            j += 1

    return result

## Ported from Libraries/LibMedia/TimeRanges.cpp:124 Media::TimeRanges::range_at_or_after.
pub func media_time_ranges_range_at_or_after(this: const MediaTimeRanges*, point: ak.Duration) -> MediaTimeRange?:
    var index = media_time_ranges_search(this, point)
    if index >= ak.vector_size(&this.ranges):
        return none
    if not media_duration_lt(point, ak.vector_at_const(&this.ranges, index).end):
        index += 1
    if index >= ak.vector_size(&this.ranges):
        return none
    return *ak.vector_at_const(&this.ranges, index)