mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2026-04-20 02:40:27 +00:00
119 lines
3.9 KiB
C++
119 lines
3.9 KiB
C++
/*
|
|
* Copyright (c) 2024, Jelle Raaijmakers <jelle@ladybird.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
|
#include <LibWeb/Bindings/MediaSourcePrototype.h>
|
|
#include <LibWeb/MediaSourceExtensions/EventNames.h>
|
|
#include <LibWeb/MediaSourceExtensions/MediaSource.h>
|
|
#include <LibWeb/MimeSniff/MimeType.h>
|
|
|
|
namespace Web::MediaSourceExtensions {
|
|
|
|
GC_DEFINE_ALLOCATOR(MediaSource);
|
|
|
|
WebIDL::ExceptionOr<GC::Ref<MediaSource>> MediaSource::construct_impl(JS::Realm& realm)
|
|
{
|
|
return realm.create<MediaSource>(realm);
|
|
}
|
|
|
|
MediaSource::MediaSource(JS::Realm& realm)
|
|
: DOM::EventTarget(realm)
|
|
{
|
|
}
|
|
|
|
MediaSource::~MediaSource() = default;
|
|
|
|
void MediaSource::initialize(JS::Realm& realm)
|
|
{
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(MediaSource);
|
|
Base::initialize(realm);
|
|
}
|
|
|
|
// https://w3c.github.io/media-source/#dom-mediasource-onsourceopen
|
|
void MediaSource::set_onsourceopen(GC::Ptr<WebIDL::CallbackType> event_handler)
|
|
{
|
|
set_event_handler_attribute(EventNames::sourceopen, event_handler);
|
|
}
|
|
|
|
// https://w3c.github.io/media-source/#dom-mediasource-onsourceopen
|
|
GC::Ptr<WebIDL::CallbackType> MediaSource::onsourceopen()
|
|
{
|
|
return event_handler_attribute(EventNames::sourceopen);
|
|
}
|
|
|
|
// https://w3c.github.io/media-source/#dom-mediasource-onsourceended
|
|
void MediaSource::set_onsourceended(GC::Ptr<WebIDL::CallbackType> event_handler)
|
|
{
|
|
set_event_handler_attribute(EventNames::sourceended, event_handler);
|
|
}
|
|
|
|
// https://w3c.github.io/media-source/#dom-mediasource-onsourceended
|
|
GC::Ptr<WebIDL::CallbackType> MediaSource::onsourceended()
|
|
{
|
|
return event_handler_attribute(EventNames::sourceended);
|
|
}
|
|
|
|
// https://w3c.github.io/media-source/#dom-mediasource-onsourceclose
|
|
void MediaSource::set_onsourceclose(GC::Ptr<WebIDL::CallbackType> event_handler)
|
|
{
|
|
set_event_handler_attribute(EventNames::sourceclose, event_handler);
|
|
}
|
|
|
|
// https://w3c.github.io/media-source/#dom-mediasource-onsourceclose
|
|
GC::Ptr<WebIDL::CallbackType> MediaSource::onsourceclose()
|
|
{
|
|
return event_handler_attribute(EventNames::sourceclose);
|
|
}
|
|
|
|
// https://w3c.github.io/media-source/#dom-mediasource-istypesupported
|
|
bool MediaSource::is_type_supported(JS::VM&, String const& type)
|
|
{
|
|
// 1. If type is an empty string, then return false.
|
|
if (type.is_empty())
|
|
return false;
|
|
|
|
// 2. If type does not contain a valid MIME type string, then return false.
|
|
auto mime_type = MimeSniff::MimeType::parse(type);
|
|
if (!mime_type.has_value())
|
|
return false;
|
|
|
|
// FIXME: Ask LibMedia about what it supports instead of hardcoding this.
|
|
|
|
// 3. If type contains a media type or media subtype that the MediaSource does not support, then
|
|
// return false.
|
|
auto type_and_subtype_are_supported = [&] {
|
|
if (mime_type->type() == "video" && mime_type->subtype() == "webm")
|
|
return true;
|
|
if (mime_type->type() == "audio" && mime_type->subtype() == "webm")
|
|
return true;
|
|
return false;
|
|
}();
|
|
if (!type_and_subtype_are_supported)
|
|
return false;
|
|
|
|
// 4. If type contains a codec that the MediaSource does not support, then return false.
|
|
// 5. If the MediaSource does not support the specified combination of media type, media
|
|
// subtype, and codecs then return false.
|
|
auto codecs_iter = mime_type->parameters().find("codecs"sv);
|
|
if (codecs_iter == mime_type->parameters().end())
|
|
return false;
|
|
auto codecs = codecs_iter->value.bytes_as_string_view();
|
|
auto had_unsupported_codec = false;
|
|
codecs.for_each_split_view(',', SplitBehavior::Nothing, [&](auto const& codec) {
|
|
if (!codec.starts_with("vp9"sv) && !codec.starts_with("vp09"sv) && !codec.starts_with("opus"sv)) {
|
|
had_unsupported_codec = true;
|
|
return IterationDecision::Break;
|
|
}
|
|
return IterationDecision::Continue;
|
|
});
|
|
if (had_unsupported_codec)
|
|
return false;
|
|
|
|
// 6. Return true.
|
|
return true;
|
|
}
|
|
|
|
}
|