Truly independent web browser
/*
* Copyright (c) 2024, Jelle Raaijmakers <[email protected]>
* Copyright (c) 2026, Gregory Bertilson <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/NonnullRefPtr.h>
#include <AK/Types.h>
#include <LibMedia/Track.h>
#include <LibMediaClient/Forward.h>
#include <LibWeb/Bindings/SourceBuffer.h>
#include <LibWeb/DOM/EventTarget.h>
#include <LibWeb/WebIDL/Buffers.h>
namespace Web::MediaSourceExtensions {
// https://w3c.github.io/media-source/#dom-sourcebuffer
class SourceBuffer : public DOM::EventTarget {
WEB_WRAPPABLE(SourceBuffer, DOM::EventTarget);
GC_DECLARE_ALLOCATOR(SourceBuffer);
public:
static GC::Ref<SourceBuffer> create(MediaSource&, GC::Ref<HTML::AudioTrackList>, GC::Ref<HTML::VideoTrackList>, GC::Ref<HTML::TextTrackList>);
void set_onupdatestart(GC::Ptr<WebIDL::CallbackType>);
GC::Ptr<WebIDL::CallbackType> onupdatestart();
void set_onupdate(GC::Ptr<WebIDL::CallbackType>);
GC::Ptr<WebIDL::CallbackType> onupdate();
void set_onupdateend(GC::Ptr<WebIDL::CallbackType>);
GC::Ptr<WebIDL::CallbackType> onupdateend();
void set_onerror(GC::Ptr<WebIDL::CallbackType>);
GC::Ptr<WebIDL::CallbackType> onerror();
void set_onabort(GC::Ptr<WebIDL::CallbackType>);
GC::Ptr<WebIDL::CallbackType> onabort();
// https://w3c.github.io/media-source/#dom-sourcebuffer-mode
Bindings::AppendMode mode() const;
WebIDL::ExceptionOr<void> set_mode(Bindings::AppendMode);
// https://w3c.github.io/media-source/#dom-sourcebuffer-timestampoffset
double timestamp_offset() const;
WebIDL::ExceptionOr<void> set_timestamp_offset(double);
// https://w3c.github.io/media-source/#dom-sourcebuffer-appendwindowstart
double append_window_start() const { return m_append_window_start; }
WebIDL::ExceptionOr<void> set_append_window_start(double);
// https://w3c.github.io/media-source/#dom-sourcebuffer-appendwindowend
double append_window_end() const { return m_append_window_end; }
WebIDL::ExceptionOr<void> set_append_window_end(double);
GC::Ref<MediaSource> media_source() const { return m_media_source; }
// https://w3c.github.io/media-source/#dom-sourcebuffer-updating
bool updating() const;
bool first_initialization_segment_received() const { return m_first_initialization_segment_received; }
// https://w3c.github.io/media-source/#dom-sourcebuffer-buffered
GC::Ref<HTML::TimeRanges> buffered();
// https://w3c.github.io/media-source/#dom-sourcebuffer-audiotracks
GC::Ref<HTML::AudioTrackList> audio_tracks() const { return m_audio_tracks; }
// https://w3c.github.io/media-source/#dom-sourcebuffer-videotracks
GC::Ref<HTML::VideoTrackList> video_tracks() const { return m_video_tracks; }
// https://w3c.github.io/media-source/#dom-sourcebuffer-texttracks
GC::Ref<HTML::TextTrackList> text_tracks() const { return m_text_tracks; }
bool is_associated_with_an_enabled_or_selected_track() const;
AK::Duration highest_presentation_timestamp() const;
AK::Duration highest_end_time() const;
void set_content_type(Utf16View type);
// https://w3c.github.io/media-source/#addsourcebuffer-method
WebIDL::ExceptionOr<void> append_buffer(WebIDL::BufferSourceVariant const&);
// https://w3c.github.io/media-source/#dom-sourcebuffer-abort
WebIDL::ExceptionOr<void> abort();
// https://w3c.github.io/media-source/#dom-sourcebuffer-changetype
WebIDL::ExceptionOr<void> change_type(Utf16String const& type);
// https://w3c.github.io/media-source/#dom-sourcebuffer-remove
WebIDL::ExceptionOr<void> remove(double start, double end);
void set_reached_end_of_stream(Badge<MediaSource>);
void clear_reached_end_of_stream(Badge<MediaSource>);
void abort_if_updating(Badge<MediaSource>);
protected:
SourceBuffer(MediaSource&, GC::Ref<HTML::AudioTrackList>, GC::Ref<HTML::VideoTrackList>, GC::Ref<HTML::TextTrackList>);
virtual ~SourceBuffer() override;
virtual void visit_edges(Cell::Visitor&) override;
private:
virtual GC::Ptr<Bindings::Wrappable> relevant_global_impl() const override;
WebIDL::ExceptionOr<void> prepare_append(size_t new_data_size);
void run_buffer_append_algorithm(u64 append_generation);
void abort_buffer_append_algorithm();
void run_range_removal(AK::Duration start, AK::Duration end);
void run_append_error_algorithm();
void on_first_initialization_segment_processed(Vector<Media::Track> const& audio_tracks, Vector<Media::Track> const& video_tracks, Vector<Media::Track> const& text_tracks);
void finish_range_removal();
void update_ready_state_and_duration_after_coded_frame_processing(AK::Duration group_end_timestamp);
void finish_buffer_append();
void set_updating(bool);
GC::Ref<MediaSource> m_media_source;
NonnullRefPtr<MediaClient::RemoteSourceBuffer> m_remote_source_buffer;
// https://w3c.github.io/media-source/#dom-sourcebuffer-updating
bool m_updating { false };
// https://w3c.github.io/media-source/#dom-sourcebuffer-timestampoffset
double m_timestamp_offset_assigned_by_script { 0 };
// https://w3c.github.io/media-source/#dom-sourcebuffer-appendwindowstart
double m_append_window_start { 0 };
// https://w3c.github.io/media-source/#dom-sourcebuffer-appendwindowend
double m_append_window_end { AK::Infinity<double> };
void send_append_window();
// NB: The generation of the current buffer-append run, captured by the run's task when it is queued and bumped by
// abort_buffer_append_algorithm(). A task whose captured generation no longer matches does nothing.
u64 m_append_generation { 0 };
// https://w3c.github.io/media-source/#sourcebuffer-range-removal
bool m_range_removal_running { false };
// https://w3c.github.io/media-source/#dfn-first-initialization-segment-received-flag
bool m_first_initialization_segment_received { false };
// https://w3c.github.io/media-source/#dom-sourcebuffer-audiotracks
GC::Ref<HTML::AudioTrackList> m_audio_tracks;
// https://w3c.github.io/media-source/#dom-sourcebuffer-videotracks
GC::Ref<HTML::VideoTrackList> m_video_tracks;
// https://w3c.github.io/media-source/#dom-sourcebuffer-texttracks
GC::Ref<HTML::TextTrackList> m_text_tracks;
};
}