2024-11-01 14:03:15 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2024, Jelle Raaijmakers <jelle@ladybird.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
|
|
|
|
#include <LibWeb/Bindings/SourceBufferListPrototype.h>
|
2026-03-26 02:48:53 -05:00
|
|
|
#include <LibWeb/DOM/Event.h>
|
2024-11-18 12:13:48 +13:00
|
|
|
#include <LibWeb/MediaSourceExtensions/EventNames.h>
|
2026-03-26 02:48:53 -05:00
|
|
|
#include <LibWeb/MediaSourceExtensions/SourceBuffer.h>
|
2024-11-01 14:03:15 +01:00
|
|
|
#include <LibWeb/MediaSourceExtensions/SourceBufferList.h>
|
|
|
|
|
|
|
|
|
|
namespace Web::MediaSourceExtensions {
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DEFINE_ALLOCATOR(SourceBufferList);
|
2024-11-01 14:03:15 +01:00
|
|
|
|
|
|
|
|
SourceBufferList::SourceBufferList(JS::Realm& realm)
|
|
|
|
|
: DOM::EventTarget(realm)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SourceBufferList::~SourceBufferList() = default;
|
|
|
|
|
|
|
|
|
|
void SourceBufferList::initialize(JS::Realm& realm)
|
|
|
|
|
{
|
|
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(SourceBufferList);
|
2025-04-20 16:22:57 +02:00
|
|
|
Base::initialize(realm);
|
2024-11-01 14:03:15 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-26 02:48:53 -05:00
|
|
|
void SourceBufferList::visit_edges(Cell::Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
|
visitor.visit(m_buffers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SourceBufferList::append(GC::Ref<SourceBuffer> buffer)
|
|
|
|
|
{
|
|
|
|
|
// From https://w3c.github.io/media-source/#addsourcebuffer-method
|
|
|
|
|
// 8. Append buffer to this's sourceBuffers.
|
|
|
|
|
m_buffers.append(buffer);
|
|
|
|
|
|
|
|
|
|
// 9. Queue a task to fire an event named addsourcebuffer at this's sourceBuffers.
|
|
|
|
|
// FIXME: Should this have a task source? An event loop? A document?
|
|
|
|
|
HTML::queue_a_task(HTML::Task::Source::Unspecified, nullptr, nullptr, GC::create_function(heap(), [weak_self = GC::Weak(*this)] {
|
|
|
|
|
if (!weak_self)
|
|
|
|
|
return;
|
|
|
|
|
weak_self->dispatch_event(DOM::Event::create(weak_self->realm(), EventNames::addsourcebuffer));
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t SourceBufferList::length() const
|
|
|
|
|
{
|
|
|
|
|
return m_buffers.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GC::Ref<SourceBuffer> const& SourceBufferList::item(u32 index) const
|
|
|
|
|
{
|
|
|
|
|
return m_buffers[index];
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-18 12:13:48 +13:00
|
|
|
// https://w3c.github.io/media-source/#dom-sourcebufferlist-onaddsourcebuffer
|
|
|
|
|
void SourceBufferList::set_onaddsourcebuffer(GC::Ptr<WebIDL::CallbackType> event_handler)
|
|
|
|
|
{
|
|
|
|
|
set_event_handler_attribute(EventNames::addsourcebuffer, event_handler);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://w3c.github.io/media-source/#dom-sourcebufferlist-onaddsourcebuffer
|
|
|
|
|
GC::Ptr<WebIDL::CallbackType> SourceBufferList::onaddsourcebuffer()
|
|
|
|
|
{
|
|
|
|
|
return event_handler_attribute(EventNames::addsourcebuffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://w3c.github.io/media-source/#dom-sourcebufferlist-onremovesourcebuffer
|
|
|
|
|
void SourceBufferList::set_onremovesourcebuffer(GC::Ptr<WebIDL::CallbackType> event_handler)
|
|
|
|
|
{
|
|
|
|
|
set_event_handler_attribute(EventNames::removesourcebuffer, event_handler);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://w3c.github.io/media-source/#dom-sourcebufferlist-onremovesourcebuffer
|
|
|
|
|
GC::Ptr<WebIDL::CallbackType> SourceBufferList::onremovesourcebuffer()
|
|
|
|
|
{
|
|
|
|
|
return event_handler_attribute(EventNames::removesourcebuffer);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 16:19:25 -05:00
|
|
|
bool SourceBufferList::contains(SourceBuffer const& source_buffer) const
|
|
|
|
|
{
|
|
|
|
|
return m_buffers.contains([&](GC::Ref<SourceBuffer> const& contained_buffer) { return contained_buffer == &source_buffer; });
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-01 14:03:15 +01:00
|
|
|
}
|