sfxVorbisStream.cpp 7.4 KB

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