Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/ogg/ogg_packet_sequence.h
10277 views
1
/**************************************************************************/
2
/* ogg_packet_sequence.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/io/resource.h"
34
#include "core/variant/typed_array.h"
35
#include "core/variant/variant.h"
36
37
#include <ogg/ogg.h>
38
39
class OggPacketSequencePlayback;
40
41
class OggPacketSequence : public Resource {
42
GDCLASS(OggPacketSequence, Resource);
43
44
friend class OggPacketSequencePlayback;
45
46
// List of pages, each of which is a list of packets on that page. The innermost PackedByteArrays contain complete ogg packets.
47
Vector<Vector<PackedByteArray>> page_data;
48
49
// List of the granule position for each page.
50
Vector<uint64_t> page_granule_positions;
51
52
// The page after the current last page. Similar semantics to an end() iterator.
53
int64_t end_page = 0;
54
55
uint64_t data_version = 0;
56
57
float sampling_rate = 0;
58
float length = 0;
59
60
protected:
61
static void _bind_methods();
62
63
public:
64
// Pushes information about all the pages that ended on this page.
65
// This should be called for each page, even for pages that no packets ended on.
66
void push_page(int64_t p_granule_pos, const Vector<PackedByteArray> &p_data);
67
68
void set_packet_data(const TypedArray<Array> &p_data);
69
TypedArray<Array> get_packet_data() const;
70
71
void set_packet_granule_positions(const PackedInt64Array &p_granule_positions);
72
PackedInt64Array get_packet_granule_positions() const;
73
74
// Sets a sampling rate associated with this object. OggPacketSequence doesn't understand codecs,
75
// so this value is naively stored as a convenience.
76
void set_sampling_rate(float p_sampling_rate);
77
78
// Returns a sampling rate previously set by set_sampling_rate().
79
float get_sampling_rate() const;
80
81
// Returns a length previously set by set_length().
82
float get_length() const;
83
84
// Returns the granule position of the last page in this sequence.
85
int64_t get_final_granule_pos() const;
86
87
Ref<OggPacketSequencePlayback> instantiate_playback();
88
89
OggPacketSequence() {}
90
virtual ~OggPacketSequence() {}
91
};
92
93
class OggPacketSequencePlayback : public RefCounted {
94
GDCLASS(OggPacketSequencePlayback, RefCounted);
95
96
friend class OggPacketSequence;
97
98
Ref<OggPacketSequence> ogg_packet_sequence;
99
100
mutable int64_t page_cursor = 0;
101
mutable int32_t packet_cursor = 0;
102
103
mutable ogg_packet *packet;
104
105
uint64_t data_version = 0;
106
107
mutable int64_t packetno = 0;
108
109
// Recursive bisection search for the correct page.
110
uint32_t seek_page_internal(int64_t granule, uint32_t after_page_inclusive, uint32_t before_page_inclusive);
111
112
public:
113
// Calling functions must not modify this packet.
114
// Returns true on success, false on error or if there is no next packet.
115
bool next_ogg_packet(ogg_packet **p_packet) const;
116
117
// Seeks to the page such that the previous page has a granule position less than or equal to this value,
118
// and the current page has a granule position greater than this value.
119
// Returns true on success, false on failure.
120
bool seek_page(int64_t p_granule_pos);
121
122
// Gets the current page number.
123
int64_t get_page_number() const;
124
125
// Moves to a specific page in the stream.
126
// Returns true on success, false if the page number is out of bounds.
127
bool set_page_number(int64_t p_page_number);
128
129
OggPacketSequencePlayback();
130
virtual ~OggPacketSequencePlayback();
131
};
132
133