ladybird/Libraries/LibWeb/MediaSourceExtensions/ByteStreamParser.h
Zaggy1024 51c3f7c41e LibWeb: Implement appending and demuxing WebM MSE segments
The segments are parsed for the SourceBufferProcessor by the
WebMByteStreamParser. It parses the initialization segment to update
its internal set of tracks, then SourceBufferProcessor/SourceBuffer set
them up for playback. When a media segment is received, it also parses
as much of it as is available, returning all the coded frames found so
far. SourceBufferProcessor then tells TrackBufferDemuxer to remove any
overlapping frames and insert the new ones.

TrackBufferDemuxer implements the Demuxer interface in terms of the
coded frame store maintained by the SourceBufferProcessor. It returns
the frames in decode order when requested by a data provider. When a
is needed, it finds the keyframe prior to the target timestamp, and
checks that there are no gaps in data up to the target timestamp. If
there are any gaps, it blocks until the gaps are gone.
2026-04-01 02:54:22 -05:00

57 lines
1.9 KiB
C++

/*
* Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Forward.h>
#include <AK/Types.h>
#include <LibMedia/CodecID.h>
#include <LibMedia/CodedFrame.h>
#include <LibMedia/DecoderError.h>
#include <LibMedia/Forward.h>
namespace Web::MediaSourceExtensions {
enum class SegmentType : u8 {
Incomplete,
Unknown,
InitializationSegment,
MediaSegment,
};
struct DemuxedCodedFrame {
u64 track_number { 0 };
Media::CodedFrame coded_frame;
};
struct ParseMediaSegmentResult {
bool completed_segment { false };
Vector<DemuxedCodedFrame> coded_frames;
};
// https://w3c.github.io/media-source/#sourcebuffer-segment-parser-loop
// ByteStreamParser abstracts the format-specific parsing that the segment parser loop requires.
// All methods operate on a MediaStreamCursor which which will be used to determine what the last read data was when
// the loop needs to remove data from the input buffer.
class ByteStreamParser {
public:
virtual ~ByteStreamParser() = default;
virtual Media::DecoderErrorOr<void> skip_ignored_bytes(Media::MediaStreamCursor&) = 0;
virtual Media::DecoderErrorOr<SegmentType> sniff_segment_type(Media::MediaStreamCursor&) = 0;
virtual Media::DecoderErrorOr<void> parse_initialization_segment(Media::MediaStreamCursor&) = 0;
virtual Media::DecoderErrorOr<ParseMediaSegmentResult> parse_media_segment(Media::MediaStreamCursor&) = 0;
virtual Optional<AK::Duration> duration() const = 0;
virtual Media::CodecID codec_id_for_track(u64 track_number) const = 0;
virtual ReadonlyBytes codec_initialization_data_for_track(u64 track_number) const = 0;
virtual Vector<Media::Track> const& video_tracks() const = 0;
virtual Vector<Media::Track> const& audio_tracks() const = 0;
virtual Vector<Media::Track> const& text_tracks() const = 0;
};
}