Truly independent web browser
/*
* Copyright (c) 2022-2025, Gregory Bertilson <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/AtomicRefCounted.h>
#include <AK/EnumBits.h>
#include <AK/Function.h>
#include <AK/HashMap.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/Time.h>
#include <LibCore/EventReceiver.h>
#include <LibMedia/IncrementallyPopulatedStream.h>
#include "DemuxerScanState.h"
#include "SeekMode.h"
#include "TimeRanges.h"
#include "Track.h"
#include <LibMediaCodecs/CodecID.h>
#include <LibMediaCodecs/CodedFrame.h>
#include <LibMediaCodecs/DecoderError.h>
namespace Media {
enum class DemuxerSeekOptions : u8 {
None = 0,
Force = 1 << 0,
NeedCodecConfiguration = 1 << 1,
};
AK_ENUM_BITWISE_OPERATORS(DemuxerSeekOptions);
enum class DemuxerSeekResult : u8 {
MovedPosition,
KeptCurrentPosition,
};
class Demuxer : public AtomicRefCounted<Demuxer> {
public:
virtual ~Demuxer() = default;
virtual DecoderErrorOr<void> create_context_for_track(Track const&) = 0;
virtual DecoderErrorOr<Vector<Track>> get_tracks_for_type(TrackType) = 0;
// Returns the container's preferred track for a given track type. This must return a value if any track of the
// given type is present.
virtual DecoderErrorOr<Optional<Track>> get_preferred_track_for_type(TrackType) = 0;
virtual DecoderErrorOr<CodedFrame> get_next_sample_for_track(Track const&) = 0;
virtual AK::Duration select_fast_seek_target_for_track(Track const&, AK::Duration target, SeekMode) = 0;
// Moves to the keyframe prior to the target. If the track is already reading within the GOP for that keyframe, it
// will remain where it was unless the seek is forced. Thus, it may return with the track reading a frame past the
// target, so the caller should ensure that a backwards seek is forced.
virtual DecoderErrorOr<DemuxerSeekResult> seek_to_most_recent_keyframe(Track const&, AK::Duration timestamp, DemuxerSeekOptions = DemuxerSeekOptions::None) = 0;
virtual DecoderErrorOr<AK::Duration> duration_of_track(Track const&) = 0;
virtual DecoderErrorOr<AK::Duration> total_duration() = 0;
// Returns the timeline offset if the media resource provides one.
virtual Optional<AK::UnixDateTime> start_time_realtime() const { return {}; }
// The earliest presentation time among the first samples of the audio and video tracks, never negative.
virtual AK::Duration earliest_possible_position() const { return AK::Duration::zero(); }
virtual DemuxerScanState const& scan_state() const LIFETIME_BOUND = 0;
// Also passes the time ranges of delivered data that has since been replaced or ended early, which the consumer
// must read again.
using ScanStateChangeHandler = Function<void(TimeRanges const& invalidated_ranges)>;
virtual void set_scan_state_change_handler(ScanStateChangeHandler) = 0;
virtual void set_blocking_reads_aborted_for_track(Track const&) = 0;
virtual void reset_blocking_reads_aborted_for_track(Track const&) = 0;
virtual void set_read_blocked_change_handler_for_track(Track const&, ReadBlockedChangeHandler) = 0;
};
}