/**************************************************************************/1/* ogg_packet_sequence.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/io/resource.h"33#include "core/variant/typed_array.h"34#include "core/variant/variant.h"3536#include <ogg/ogg.h>3738class OggPacketSequencePlayback;3940class OggPacketSequence : public Resource {41GDCLASS(OggPacketSequence, Resource);4243friend class OggPacketSequencePlayback;4445// List of pages, each of which is a list of packets on that page. The innermost PackedByteArrays contain complete ogg packets.46Vector<Vector<PackedByteArray>> page_data;4748// List of the granule position for each page.49Vector<uint64_t> page_granule_positions;5051// The page after the current last page. Similar semantics to an end() iterator.52int64_t end_page = 0;5354uint64_t data_version = 0;5556float sampling_rate = 0;57float length = 0;5859protected:60static void _bind_methods();6162public:63// Pushes information about all the pages that ended on this page.64// This should be called for each page, even for pages that no packets ended on.65void push_page(int64_t p_granule_pos, const Vector<PackedByteArray> &p_data);6667void set_packet_data(const TypedArray<Array> &p_data);68TypedArray<Array> get_packet_data() const;6970void set_packet_granule_positions(const PackedInt64Array &p_granule_positions);71PackedInt64Array get_packet_granule_positions() const;7273// Sets a sampling rate associated with this object. OggPacketSequence doesn't understand codecs,74// so this value is naively stored as a convenience.75void set_sampling_rate(float p_sampling_rate);7677// Returns a sampling rate previously set by set_sampling_rate().78float get_sampling_rate() const;7980// Returns a length previously set by set_length().81float get_length() const;8283// Returns the granule position of the last page in this sequence.84int64_t get_final_granule_pos() const;8586Ref<OggPacketSequencePlayback> instantiate_playback();8788OggPacketSequence() {}89virtual ~OggPacketSequence() {}90};9192class OggPacketSequencePlayback : public RefCounted {93GDCLASS(OggPacketSequencePlayback, RefCounted);9495friend class OggPacketSequence;9697Ref<OggPacketSequence> ogg_packet_sequence;9899mutable int64_t page_cursor = 0;100mutable int32_t packet_cursor = 0;101102mutable ogg_packet *packet;103104uint64_t data_version = 0;105106mutable int64_t packetno = 0;107108// Recursive bisection search for the correct page.109uint32_t seek_page_internal(int64_t granule, uint32_t after_page_inclusive, uint32_t before_page_inclusive);110111public:112// Calling functions must not modify this packet.113// Returns true on success, false on error or if there is no next packet.114bool next_ogg_packet(ogg_packet **p_packet) const;115116// Seeks to the page such that the previous page has a granule position less than or equal to this value,117// and the current page has a granule position greater than this value.118// Returns true on success, false on failure.119bool seek_page(int64_t p_granule_pos);120121// Gets the current page number.122int64_t get_page_number() const;123124// Moves to a specific page in the stream.125// Returns true on success, false if the page number is out of bounds.126bool set_page_number(int64_t p_page_number);127128OggPacketSequencePlayback();129virtual ~OggPacketSequencePlayback();130};131132133