sfxVorbisStream.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. #if defined(TORQUE_OS_XENON)
  97. // For some reason the datasource pointer passed to the callbacks is not the
  98. // same as it is when passed in to ov_open_callbacks
  99. #pragma message("There is a strange bug in ov_open_callbacks as it compiles on the Xbox360. Use FMOD resource loading.")
  100. AssertWarn(false, "There is a strange bug in ov_open_callbacks as it compiles on the Xbox360. Use FMOD resource loading.");
  101. return false;
  102. #endif
  103. mVF = new OggVorbis_File;
  104. dMemset( mVF, 0, sizeof( OggVorbis_File ) );
  105. const bool canSeek = mStream->hasCapability( Stream::StreamPosition );
  106. ov_callbacks cb;
  107. cb.read_func = _read_func;
  108. cb.seek_func = canSeek ? _seek_func : NULL;
  109. cb.close_func = NULL;
  110. cb.tell_func = canSeek ? _tell_func : NULL;
  111. // Open it.
  112. S32 ovResult = ov_open_callbacks( mStream, mVF, NULL, 0, cb );
  113. if( ovResult != 0 )
  114. return false;
  115. return true;
  116. }
  117. bool SFXVorbisStream::_readHeader()
  118. {
  119. if( !_openVorbis() )
  120. return false;
  121. // Fill in the format info.
  122. const vorbis_info *vi = getInfo();
  123. mFormat.set( vi->channels, vi->channels * 16, vi->rate );
  124. // Set the sample count.
  125. mSamples = getPcmTotal();
  126. // Reset the bitstream.
  127. mBitstream = 0;
  128. return true;
  129. }
  130. void SFXVorbisStream::_close()
  131. {
  132. if ( !mVF )
  133. return;
  134. ov_clear( mVF );
  135. delete mVF;
  136. mVF = NULL;
  137. mBitstream = -1;
  138. }
  139. const vorbis_info* SFXVorbisStream::getInfo( S32 link )
  140. {
  141. AssertFatal( mVF, "SFXVorbisStream::getInfo() - Stream is closed!" );
  142. return ov_info( mVF, link );
  143. }
  144. const vorbis_comment* SFXVorbisStream::getComment( S32 link )
  145. {
  146. AssertFatal( mVF, "SFXVorbisStream::getComment() - Stream is closed!" );
  147. return ov_comment( mVF, link );
  148. }
  149. U64 SFXVorbisStream::getPcmTotal( S32 link )
  150. {
  151. AssertFatal( mVF, "SFXVorbisStream::getInfo() - Stream is closed!" );
  152. return ov_pcm_total( mVF, link );
  153. }
  154. S32 SFXVorbisStream::read( U8 *buffer,
  155. U32 length,
  156. S32 *bitstream )
  157. {
  158. AssertFatal( mVF, "SFXVorbisStream::read() - Stream is closed!" );
  159. mBitstream = *bitstream;
  160. #ifdef TORQUE_BIG_ENDIAN
  161. static const S32 isBigEndian = 1;
  162. #else
  163. static const S32 isBigEndian = 0;
  164. #endif
  165. // Vorbis doesn't seem to like reading
  166. // requests longer than this.
  167. const U32 MAXREAD = 4096;
  168. U32 bytesRead = 0;
  169. U32 offset = 0;
  170. U32 bytesToRead = 0;
  171. // Since it only returns the result of one packet
  172. // per call, you generally have to loop to read it all.
  173. while( offset < length )
  174. {
  175. if ( ( length - offset ) < MAXREAD )
  176. bytesToRead = length - offset;
  177. else
  178. bytesToRead = MAXREAD;
  179. bytesRead = ov_read( mVF, (char*)buffer, bytesToRead, isBigEndian, 2, 1, bitstream );
  180. if( bytesRead == 0 ) // EOF
  181. return offset;
  182. else if( bytesRead < 0 )
  183. {
  184. // We got an error... return the result.
  185. return bytesRead;
  186. }
  187. offset += bytesRead;
  188. buffer += bytesRead;
  189. mBytesRead += bytesRead;
  190. }
  191. // Return the total data read.
  192. return offset;
  193. }
  194. bool SFXVorbisStream::isEOS() const
  195. {
  196. return ( Parent::isEOS() || ( mStream && ov_pcm_tell( mVF ) == mSamples ) );
  197. }
  198. void SFXVorbisStream::reset()
  199. {
  200. AssertFatal( mVF, "SFXVorbisStream::reset() - Stream is closed!" );
  201. // There's a bug in libvorbis 1.2.0 that will cause the
  202. // ov_pcm_seek* functions to go into an infinite loop on
  203. // some files (apparently if they contain Theora streams).
  204. // Avoiding to seek when not necessary seems to do the trick
  205. // but if it deadlocks here, that's why.
  206. if( mBytesRead > 0 )
  207. {
  208. ov_pcm_seek_page( mVF, 0 );
  209. mBitstream = 0;
  210. mBytesRead = 0;
  211. }
  212. }
  213. U32 SFXVorbisStream::getPosition() const
  214. {
  215. AssertFatal( mVF, "SFXVorbisStream::getPosition() - Stream is closed!" );
  216. return U32( ov_pcm_tell( mVF ) ) * mFormat.getBytesPerSample();
  217. }
  218. void SFXVorbisStream::setPosition( U32 offset )
  219. {
  220. AssertFatal( mVF, "SFXVorbisStream::setPosition() - Stream is closed!" );
  221. ov_pcm_seek( mVF, offset / mFormat.getBytesPerSample() );
  222. }
  223. U32 SFXVorbisStream::read( U8 *buffer, U32 length )
  224. {
  225. S32 result = read( buffer, length, &mBitstream );
  226. if ( result < 0 )
  227. return 0;
  228. return result;
  229. }
  230. #endif // TORQUE_OGGVORBIS