sfxBuffer.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. #include "sfx/sfxBuffer.h"
  23. #include "sfx/sfxVoice.h"
  24. #include "sfx/sfxDescription.h"
  25. #include "sfx/sfxInternal.h"
  26. //#define DEBUG_SPEW
  27. Signal< void( SFXBuffer* ) > SFXBuffer::smBufferDestroyedSignal;
  28. //-----------------------------------------------------------------------------
  29. SFXBuffer::SFXBuffer( const ThreadSafeRef< SFXStream >& stream, SFXDescription* description, bool createAsyncState )
  30. : mStatus( STATUS_Null ),
  31. mFormat( stream->getFormat() ),
  32. mDuration( stream->getDuration() ),
  33. mIsStreaming( description->mIsStreaming ),
  34. mIsLooping( description->mIsLooping ),
  35. mIsUnique( description->mIsStreaming ),
  36. mIsDead( false ),
  37. mUniqueVoice( NULL )
  38. {
  39. using namespace SFXInternal;
  40. if( createAsyncState )
  41. {
  42. U32 packetLength = description->mStreamPacketSize;
  43. U32 readAhead = description->mStreamReadAhead;
  44. ThreadSafeRef< SFXStream > streamRef( stream );
  45. mAsyncState = new AsyncState(
  46. new SFXAsyncStream
  47. ( streamRef, mIsStreaming, packetLength, readAhead,
  48. mIsStreaming ? description->mIsLooping : false ) );
  49. }
  50. }
  51. //-----------------------------------------------------------------------------
  52. SFXBuffer::SFXBuffer( SFXDescription* description )
  53. : mStatus( STATUS_Ready ),
  54. mDuration( 0 ), // Must be set by subclass.
  55. mIsStreaming( false ), // Not streaming through our system.
  56. mIsLooping( description->mIsLooping ),
  57. mIsUnique( false ), // Must be set by subclass.
  58. mIsDead( false ),
  59. mUniqueVoice( NULL )
  60. {
  61. }
  62. //-----------------------------------------------------------------------------
  63. SFXBuffer::~SFXBuffer()
  64. {
  65. smBufferDestroyedSignal.trigger( this );
  66. }
  67. //-----------------------------------------------------------------------------
  68. void SFXBuffer::load()
  69. {
  70. if( getStatus() == STATUS_Null )
  71. {
  72. AssertFatal( mAsyncState != NULL, "SFXBuffer::load() - no async state!" );
  73. _setStatus( STATUS_Loading );
  74. SFXInternal::UPDATE_LIST().add( this );
  75. mAsyncState->mStream->start();
  76. }
  77. }
  78. //-----------------------------------------------------------------------------
  79. bool SFXBuffer::update()
  80. {
  81. using namespace SFXInternal;
  82. if( isDead() )
  83. {
  84. // Buffer requested to finish its async operations.
  85. // Kill our async state and put us on the dead buffer list.
  86. mAsyncState->mStream->stop();
  87. mAsyncState = NULL;
  88. gDeadBufferList.pushFront( this );
  89. return false;
  90. }
  91. else if( isAtEnd() && isStreaming() )
  92. {
  93. // Streaming buffers remain in the update loop even if they
  94. // have played in full to allow for stream seeks.
  95. return true;
  96. }
  97. AssertFatal( mAsyncState != NULL, "SFXBuffer::update() - async state has already been released" );
  98. bool needFurtherUpdates = true;
  99. if( !isStreaming() )
  100. {
  101. // Not a streaming buffer. If the async stream has its data
  102. // ready, we load it and finish up on our async work.
  103. SFXStreamPacket* packet;
  104. while( mAsyncState->mStream->read( &packet, 1 ) )
  105. {
  106. bool isLast = packet->mIsLast;
  107. // Write packet data into buffer.
  108. write( &packet, 1 );
  109. packet = NULL;
  110. if( isLast )
  111. {
  112. // Release async state.
  113. mAsyncState = NULL;
  114. // Once loaded, non-streaming buffers disappear from the SFX
  115. // update thread.
  116. needFurtherUpdates = false;
  117. // Signal that we are ready.
  118. _setStatus( STATUS_Ready );
  119. #ifdef DEBUG_SPEW
  120. Platform::outputDebugString( "[SFXBuffer] Buffer ready" );
  121. #endif
  122. break;
  123. }
  124. }
  125. }
  126. else
  127. {
  128. // A streaming buffer.
  129. //
  130. // If we don't have a queue, construct one now. Note that when doing
  131. // a stream seek on us, SFXVoice will drop our async stream and queue.
  132. // Work on local copies of the pointers to allow having the async state
  133. // be switched in parallel.
  134. //
  135. // Note that despite us being a streamed buffer, our unique voice may
  136. // not yet have been assigned to us.
  137. AsyncStatePtr state = mAsyncState;
  138. if( !state->mQueue && !mUniqueVoice.isNull() )
  139. {
  140. // Make sure we have no data currently submitted to the device.
  141. // This will stop and discard an outdated feed if we've been
  142. // switching streams.
  143. _setStatus( STATUS_Loading );
  144. _flush();
  145. // Create a new queue.
  146. state->mQueue = new SFXAsyncQueue( mUniqueVoice, this, mIsLooping );
  147. }
  148. // Check the queue.
  149. if( state->mQueue != NULL )
  150. {
  151. // Feed the queue, if necessary and possible.
  152. while( state->mQueue->needPacket() )
  153. {
  154. // Try to read a packet.
  155. SFXStreamPacket* packet;
  156. if( !state->mStream->read( &packet, 1 ) )
  157. break;
  158. // Submit it.
  159. state->mQueue->submitPacket( packet, packet->getSampleCount() );
  160. #ifdef DEBUG_SPEW
  161. Platform::outputDebugString( "[SFXBuffer] Stream packet queued" );
  162. #endif
  163. // Signal that the buffer has data ready.
  164. _setStatus( STATUS_Ready );
  165. }
  166. // Detect buffer underrun and end-of-stream.
  167. if( !isReady() && state->mQueue->isEmpty() )
  168. {
  169. #ifdef DEBUG_SPEW
  170. Platform::outputDebugString( "[SFXBuffer] Stream blocked" );
  171. #endif
  172. _setStatus( STATUS_Blocked );
  173. }
  174. else if( state->mQueue->isAtEnd() )
  175. {
  176. #ifdef DEBUG_SPEW
  177. Platform::outputDebugString( "[SFXBuffer] Stream at end" );
  178. #endif
  179. _setStatus( STATUS_AtEnd );
  180. _flush();
  181. }
  182. }
  183. }
  184. return needFurtherUpdates;
  185. }
  186. //-----------------------------------------------------------------------------
  187. void SFXBuffer::destroySelf()
  188. {
  189. AssertFatal( !isDead(), "SFXBuffer::destroySelf() - buffer already dead" );
  190. mIsDead = true;
  191. if( !mAsyncState )
  192. {
  193. // Easy way. This buffer has finished all its async
  194. // processing, so we can just kill it.
  195. delete this;
  196. }
  197. else
  198. {
  199. // Hard way. We will have to make the buffer finish
  200. // all its concurrent stuff, so we mark it dead, make sure
  201. // to see an update, and then wait for the buffer to surface
  202. // on the dead buffer list.
  203. SFXInternal::TriggerUpdate();
  204. }
  205. }
  206. //-----------------------------------------------------------------------------
  207. void SFXBuffer::_setStatus( Status status )
  208. {
  209. if( mStatus != status )
  210. {
  211. mOnStatusChange.trigger( this, status );
  212. mStatus = status;
  213. }
  214. }
  215. //-----------------------------------------------------------------------------
  216. SFXBuffer::AsyncState::AsyncState()
  217. : mQueue( NULL )
  218. {
  219. }
  220. //-----------------------------------------------------------------------------
  221. SFXBuffer::AsyncState::AsyncState( SFXInternal::SFXAsyncStream* stream )
  222. : mStream( stream ), mQueue( NULL )
  223. {
  224. }
  225. //-----------------------------------------------------------------------------
  226. SFXBuffer::AsyncState::~AsyncState()
  227. {
  228. if( mQueue )
  229. SAFE_DELETE( mQueue );
  230. }