sfxVorbisStream.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. #ifdef TORQUE_OGGVORBIS
  23. #include "sfx/media/sfxVorbisStream.h"
  24. #include "core/stream/stream.h"
  25. #include "console/console.h"
  26. SFXVorbisStream* SFXVorbisStream::create( Stream *stream )
  27. {
  28. SFXVorbisStream *sfxStream = new SFXVorbisStream();
  29. if ( sfxStream->open( stream, true ) )
  30. return sfxStream;
  31. delete sfxStream;
  32. return NULL;
  33. }
  34. SFXVorbisStream::SFXVorbisStream()
  35. : mVF( NULL ),
  36. mBytesRead( 0 )
  37. {
  38. }
  39. SFXVorbisStream::SFXVorbisStream( const SFXVorbisStream& cloneFrom )
  40. : Parent( cloneFrom )
  41. {
  42. if( !mStream->hasCapability( Stream::StreamPosition ) )
  43. {
  44. Con::errorf( "SFXVorbisStream::SFXVorbisStream() - Source stream does not allow seeking" );
  45. return;
  46. }
  47. mStream->setPosition( 0 );
  48. if( !_readHeader() )
  49. {
  50. Con::errorf( "SFXVorbisStream::SFXVorbisStream() - Opening Vorbis stream failed" );
  51. return;
  52. }
  53. ov_pcm_seek( mVF, ov_pcm_tell( cloneFrom.mVF ) );
  54. mBitstream = cloneFrom.mBitstream;
  55. mBytesRead = cloneFrom.mBytesRead;
  56. }
  57. SFXVorbisStream::~SFXVorbisStream()
  58. {
  59. // We must call close from our own destructor
  60. // and not the base class... as it causes a
  61. // pure virtual runtime assertion.
  62. close();
  63. }
  64. size_t SFXVorbisStream::_read_func( void *ptr, size_t size, size_t nmemb, void *datasource )
  65. {
  66. Stream *stream = reinterpret_cast<Stream*>( datasource );
  67. // Stream::read() returns true if any data was
  68. // read, so we must track the read bytes ourselves.
  69. U32 startByte = stream->getPosition();
  70. stream->read( size * nmemb, ptr );
  71. U32 endByte = stream->getPosition();
  72. // How many did we actually read?
  73. U32 readBytes = ( endByte - startByte );
  74. U32 readItems = readBytes / size;
  75. return readItems;
  76. }
  77. S32 SFXVorbisStream::_seek_func( void *datasource, ogg_int64_t offset, S32 whence )
  78. {
  79. Stream *stream = reinterpret_cast<Stream*>( datasource );
  80. U32 newPos = 0;
  81. if ( whence == SEEK_CUR )
  82. newPos = stream->getPosition() + (U32)offset;
  83. else if ( whence == SEEK_END )
  84. newPos = stream->getStreamSize() - (U32)offset;
  85. else
  86. newPos = (U32)offset;
  87. return stream->setPosition( newPos ) ? 0 : -1;
  88. }
  89. long SFXVorbisStream::_tell_func( void *datasource )
  90. {
  91. Stream *stream = reinterpret_cast<Stream*>( datasource );
  92. return stream->getPosition();
  93. }
  94. bool SFXVorbisStream::_openVorbis()
  95. {
  96. mVF = new OggVorbis_File;
  97. dMemset( mVF, 0, sizeof( OggVorbis_File ) );
  98. const bool canSeek = mStream->hasCapability( Stream::StreamPosition );
  99. ov_callbacks cb;
  100. cb.read_func = _read_func;
  101. cb.seek_func = canSeek ? _seek_func : NULL;
  102. cb.close_func = NULL;
  103. cb.tell_func = canSeek ? _tell_func : NULL;
  104. // Open it.
  105. S32 ovResult = ov_open_callbacks( mStream, mVF, NULL, 0, cb );
  106. if( ovResult != 0 )
  107. return false;
  108. return true;
  109. }
  110. bool SFXVorbisStream::_readHeader()
  111. {
  112. if( !_openVorbis() )
  113. return false;
  114. // Fill in the format info.
  115. const vorbis_info *vi = getInfo();
  116. mFormat.set( vi->channels, vi->channels * 16, vi->rate );
  117. // Set the sample count.
  118. mSamples = getPcmTotal();
  119. // Reset the bitstream.
  120. mBitstream = 0;
  121. return true;
  122. }
  123. void SFXVorbisStream::_close()
  124. {
  125. if ( !mVF )
  126. return;
  127. ov_clear( mVF );
  128. delete mVF;
  129. mVF = NULL;
  130. mBitstream = -1;
  131. }
  132. const vorbis_info* SFXVorbisStream::getInfo( S32 link )
  133. {
  134. AssertFatal( mVF, "SFXVorbisStream::getInfo() - Stream is closed!" );
  135. return ov_info( mVF, link );
  136. }
  137. const vorbis_comment* SFXVorbisStream::getComment( S32 link )
  138. {
  139. AssertFatal( mVF, "SFXVorbisStream::getComment() - Stream is closed!" );
  140. return ov_comment( mVF, link );
  141. }
  142. U64 SFXVorbisStream::getPcmTotal( S32 link )
  143. {
  144. AssertFatal( mVF, "SFXVorbisStream::getInfo() - Stream is closed!" );
  145. return ov_pcm_total( mVF, link );
  146. }
  147. S32 SFXVorbisStream::read( U8 *buffer,
  148. U32 length,
  149. S32 *bitstream )
  150. {
  151. AssertFatal( mVF, "SFXVorbisStream::read() - Stream is closed!" );
  152. mBitstream = *bitstream;
  153. #ifdef TORQUE_BIG_ENDIAN
  154. static const S32 isBigEndian = 1;
  155. #else
  156. static const S32 isBigEndian = 0;
  157. #endif
  158. // Vorbis doesn't seem to like reading
  159. // requests longer than this.
  160. const U32 MAXREAD = 4096;
  161. S64 bytesRead = 0;
  162. U32 offset = 0;
  163. U32 bytesToRead = 0;
  164. // Since it only returns the result of one packet
  165. // per call, you generally have to loop to read it all.
  166. while( offset < length )
  167. {
  168. if ( ( length - offset ) < MAXREAD )
  169. bytesToRead = length - offset;
  170. else
  171. bytesToRead = MAXREAD;
  172. bytesRead = ov_read( mVF, (char*)buffer, bytesToRead, isBigEndian, 2, 1, bitstream );
  173. if( bytesRead == 0 ) // EOF
  174. return offset;
  175. else if( bytesRead < 0 )
  176. {
  177. // We got an error... return the result.
  178. return bytesRead;
  179. }
  180. offset += bytesRead;
  181. buffer += bytesRead;
  182. mBytesRead += bytesRead;
  183. }
  184. // Return the total data read.
  185. return offset;
  186. }
  187. bool SFXVorbisStream::isEOS() const
  188. {
  189. return ( Parent::isEOS() || ( mStream && ov_pcm_tell( mVF ) == mSamples ) );
  190. }
  191. void SFXVorbisStream::reset()
  192. {
  193. AssertFatal( mVF, "SFXVorbisStream::reset() - Stream is closed!" );
  194. // There's a bug in libvorbis 1.2.0 that will cause the
  195. // ov_pcm_seek* functions to go into an infinite loop on
  196. // some files (apparently if they contain Theora streams).
  197. // Avoiding to seek when not necessary seems to do the trick
  198. // but if it deadlocks here, that's why.
  199. if( mBytesRead > 0 )
  200. {
  201. ov_pcm_seek_page( mVF, 0 );
  202. mBitstream = 0;
  203. mBytesRead = 0;
  204. }
  205. }
  206. U32 SFXVorbisStream::getPosition() const
  207. {
  208. AssertFatal( mVF, "SFXVorbisStream::getPosition() - Stream is closed!" );
  209. return U32( ov_pcm_tell( mVF ) ) * mFormat.getBytesPerSample();
  210. }
  211. void SFXVorbisStream::setPosition( U32 offset )
  212. {
  213. AssertFatal( mVF, "SFXVorbisStream::setPosition() - Stream is closed!" );
  214. ov_pcm_seek( mVF, offset / mFormat.getBytesPerSample() );
  215. }
  216. U32 SFXVorbisStream::read( U8 *buffer, U32 length )
  217. {
  218. S32 result = read( buffer, length, &mBitstream );
  219. if ( result < 0 )
  220. return 0;
  221. return result;
  222. }
  223. #endif // TORQUE_OGGVORBIS