Skip to content

Truly independent web browser

3.0 KB
Raw Blame
/*
 * Copyright (c) 2025, Gregory Bertilson <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibUnicode/Locale.h>
#include <LibWeb/HTML/HTMLMediaElement.h>
#include <LibWeb/HTML/MediaTrackBase.h>
#include <LibWeb/MediaSourceExtensions/MediaSource.h>
#include <LibWeb/MediaSourceExtensions/SourceBuffer.h>

namespace Web::HTML {

MediaTrackBase::MediaTrackBase(GC::Ref<HTMLMediaElement> media_element, Media::Track const& track)
    : m_media_element(media_element)
    , m_track_in_playback_manager(track)
    , m_id(Utf16String::number(track.identifier()))
    , m_kind(track.kind())
    , m_label(track.label())
{
    // 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.
    m_language = [&] {
        auto locale = Unicode::parse_unicode_locale_id(track.language().utf16_view());
        if (!locale.has_value())
            return Utf16String {};
        auto language = locale->to_utf16_string();
        // 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 (language == "und"_utf16)
            return Utf16String {};
        return language;
    }();
}

MediaTrackBase::~MediaTrackBase() = default;

GC::Ptr<MediaSourceExtensions::SourceBuffer> MediaTrackBase::source_buffer_in_attached_media_source() const
{
    if (!m_source_buffer || m_source_buffer->media_source() != m_media_element->attached_media_source())
        return nullptr;
    return m_source_buffer;
}

void MediaTrackBase::visit_edges(GC::Cell::Visitor& visitor)
{
    Base::visit_edges(visitor);
    visitor.visit(m_media_element);
    visitor.visit(m_source_buffer);
}

}