ogg_packet_sequence.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*************************************************************************/
  2. /* ogg_packet_sequence.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #ifndef OGG_PACKET_SEQUENCE_H
  31. #define OGG_PACKET_SEQUENCE_H
  32. #include "core/io/resource.h"
  33. #include "core/object/gdvirtual.gen.inc"
  34. #include "core/variant/native_ptr.h"
  35. #include "core/variant/typed_array.h"
  36. #include "core/variant/variant.h"
  37. #include "thirdparty/libogg/ogg/ogg.h"
  38. class OggPacketSequencePlayback;
  39. class OggPacketSequence : public Resource {
  40. GDCLASS(OggPacketSequence, Resource);
  41. friend class OggPacketSequencePlayback;
  42. // List of pages, each of which is a list of packets on that page. The innermost PackedByteArrays contain complete ogg packets.
  43. Vector<Vector<PackedByteArray>> page_data;
  44. // List of the granule position for each page.
  45. Vector<uint64_t> page_granule_positions;
  46. // The page after the current last page. Similar semantics to an end() iterator.
  47. int64_t end_page = 0;
  48. uint64_t data_version = 0;
  49. float sampling_rate = 0;
  50. float length = 0;
  51. protected:
  52. static void _bind_methods();
  53. public:
  54. // Pushes information about all the pages that ended on this page.
  55. // This should be called for each page, even for pages that no packets ended on.
  56. void push_page(int64_t p_granule_pos, const Vector<PackedByteArray> &p_data);
  57. void set_packet_data(const Array &p_data);
  58. Array get_packet_data() const;
  59. void set_packet_granule_positions(const Array &p_granule_positions);
  60. Array get_packet_granule_positions() const;
  61. // Sets a sampling rate associated with this object. OggPacketSequence doesn't understand codecs,
  62. // so this value is naively stored as a convenience.
  63. void set_sampling_rate(float p_sampling_rate);
  64. // Returns a sampling rate previously set by set_sampling_rate().
  65. float get_sampling_rate() const;
  66. // Returns a length previously set by set_length().
  67. float get_length() const;
  68. // Returns the granule position of the last page in this sequence.
  69. int64_t get_final_granule_pos() const;
  70. Ref<OggPacketSequencePlayback> instantiate_playback();
  71. OggPacketSequence() {}
  72. virtual ~OggPacketSequence() {}
  73. };
  74. class OggPacketSequencePlayback : public RefCounted {
  75. GDCLASS(OggPacketSequencePlayback, RefCounted);
  76. friend class OggPacketSequence;
  77. Ref<OggPacketSequence> ogg_packet_sequence;
  78. mutable int64_t page_cursor = 0;
  79. mutable int32_t packet_cursor = 0;
  80. mutable ogg_packet *packet;
  81. uint64_t data_version = 0;
  82. mutable int64_t packetno = 0;
  83. // Recursive bisection search for the correct page.
  84. uint32_t seek_page_internal(int64_t granule, uint32_t after_page_inclusive, uint32_t before_page_inclusive);
  85. public:
  86. // Calling functions must not modify this packet.
  87. // Returns true on success, false on error or if there is no next packet.
  88. bool next_ogg_packet(ogg_packet **p_packet) const;
  89. // Seeks to the page such that the previous page has a granule position less than or equal to this value,
  90. // and the current page has a granule position greater than this value.
  91. // Returns true on success, false on failure.
  92. bool seek_page(int64_t p_granule_pos);
  93. OggPacketSequencePlayback();
  94. virtual ~OggPacketSequencePlayback();
  95. };
  96. #endif // OGG_PACKET_SEQUENCE_H