oggInputStream.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _OGGINPUTSTREAM_H_
  23. #define _OGGINPUTSTREAM_H_
  24. #ifndef _TVECTOR_H_
  25. #include "core/util/tVector.h"
  26. #endif
  27. #ifndef _TYPETRAITS_H_
  28. #include "platform/typetraits.h"
  29. #endif
  30. #ifndef _PLATFORM_THREADS_MUTEX_H_
  31. #include "platform/threads/mutex.h"
  32. #endif
  33. #ifndef _THREADSAFEREFCOUNT_H_
  34. #include "platform/threads/threadSafeRefCount.h"
  35. #endif
  36. #include "ogg/ogg.h"
  37. class Stream;
  38. class OggInputStream;
  39. /// Single substream in a multiplexed OGG stream.
  40. class OggDecoder
  41. {
  42. public:
  43. typedef void Parent;
  44. friend class OggInputStream;
  45. protected:
  46. /// The Ogg container stream.
  47. OggInputStream* mOggStream;
  48. /// The Ogg bitstream.
  49. ogg_stream_state mOggStreamState;
  50. /// Lock for synchronizing access to Ogg stream state.
  51. Mutex mMutex;
  52. /// Read the next packet in the stream.
  53. /// @return false if there is no next packet.
  54. bool _readNextPacket( ogg_packet* packet );
  55. ///
  56. bool _nextPacket();
  57. ///
  58. virtual bool _detect( ogg_page* startPage ) = 0;
  59. ///
  60. virtual bool _init() = 0;
  61. ///
  62. virtual bool _packetin( ogg_packet* packet ) = 0;
  63. ///
  64. void _setStartPage( ogg_page* startPage );
  65. public:
  66. ///
  67. OggDecoder( const ThreadSafeRef< OggInputStream >& stream );
  68. virtual ~OggDecoder();
  69. /// Return the serial number of the Ogg bitstream.
  70. U32 getStreamSerialNo() const { return mOggStreamState.serialno; }
  71. ///
  72. virtual const char* getName() const = 0;
  73. };
  74. /// A multiplexed OGG input stream feeding into stream decoders.
  75. class OggInputStream : public ThreadSafeRefCount< OggInputStream >
  76. {
  77. public:
  78. typedef void Parent;
  79. friend class OggDecoder; // _requestData
  80. protected:
  81. typedef OggDecoder* ( *Constructor )( const ThreadSafeRef< OggInputStream >& stream );
  82. template< typename T >
  83. struct _SpellItOutForGCC
  84. {
  85. static OggDecoder* _fn( const ThreadSafeRef< OggInputStream >& stream )
  86. {
  87. return constructSingle< T* >( stream );
  88. }
  89. };
  90. ///
  91. bool mIsAtEnd;
  92. ///
  93. Stream* mStream;
  94. ///
  95. Vector< Constructor > mConstructors;
  96. ///
  97. Vector< OggDecoder* > mDecoders;
  98. ///
  99. ogg_sync_state mOggSyncState;
  100. ///
  101. Mutex mMutex;
  102. /// Pull the next page from the OGG stream.
  103. bool _pullNextPage( ogg_page* page );
  104. /// Push the given page to the attached decoder streams.
  105. void _pushNextPage( ogg_page* page );
  106. ///
  107. bool _requestData();
  108. ///
  109. void _freeDecoders();
  110. public:
  111. ///
  112. /// @note Ownership of "stream" is transferred to OggInputStream.
  113. OggInputStream( Stream* stream );
  114. ~OggInputStream();
  115. /// Register a decoder class with the stream.
  116. template< class T >
  117. void addDecoder()
  118. {
  119. mConstructors.push_back( &_SpellItOutForGCC< T >::_fn );
  120. }
  121. ///
  122. OggDecoder* getDecoder( const String& name ) const;
  123. ///
  124. bool init();
  125. ///
  126. bool isAtEnd();
  127. };
  128. #endif // !_OGGINPUTSTREAM_H_