Bug fix in Core, Jack, ASIO, and DS for internal draining in INPUT mode only; Added...
[rtaudio-cdist.git] / RtAudio.cpp
index 28e80202a8545508348da86c27d390b1a2336d2d..039b5d03a362d74fcbae609a7d2b534022549ccc 100644 (file)
@@ -1682,11 +1682,12 @@ bool RtApiCore :: callbackEvent( AudioDeviceID deviceId,
         }\r
       }\r
     }\r
+  }\r
 \r
-    if ( handle->drainCounter ) {\r
-      handle->drainCounter++;\r
-      goto unlock;\r
-    }\r
+  // Don't bother draining input\r
+  if ( handle->drainCounter ) {\r
+    handle->drainCounter++;\r
+    goto unlock;\r
   }\r
 \r
   AudioDeviceID inputDevice;\r
@@ -2572,11 +2573,12 @@ bool RtApiJack :: callbackEvent( unsigned long nframes )
         memcpy( jackbuffer, &stream_.userBuffer[0][i*bufferBytes], bufferBytes );\r
       }\r
     }\r
+  }\r
 \r
-    if ( handle->drainCounter ) {\r
-      handle->drainCounter++;\r
-      goto unlock;\r
-    }\r
+  // Don't bother draining input\r
+  if ( handle->drainCounter ) {\r
+    handle->drainCounter++;\r
+    goto unlock;\r
   }\r
 \r
   if ( stream_.mode == INPUT || stream_.mode == DUPLEX ) {\r
@@ -3403,11 +3405,12 @@ bool RtApiAsio :: callbackEvent( long bufferIndex )
       }\r
 \r
     }\r
+  }\r
 \r
-    if ( handle->drainCounter ) {\r
-      handle->drainCounter++;\r
-      goto unlock;\r
-    }\r
+  // Don't bother draining input\r
+  if ( handle->drainCounter ) {\r
+    handle->drainCounter++;\r
+    goto unlock;\r
   }\r
 \r
   if ( stream_.mode == INPUT || stream_.mode == DUPLEX ) {\r
@@ -6063,6 +6066,8 @@ void RtApiDs :: stopStream()
 \r
     stream_.state = STREAM_STOPPED;\r
 \r
+    MUTEX_LOCK( &stream_.mutex );\r
+\r
     // Stop the buffer and clear memory\r
     LPDIRECTSOUNDBUFFER buffer = (LPDIRECTSOUNDBUFFER) handle->buffer[0];\r
     result = buffer->Stop();\r
@@ -6103,6 +6108,9 @@ void RtApiDs :: stopStream()
 \r
     stream_.state = STREAM_STOPPED;\r
 \r
+    if ( stream_.mode != DUPLEX )\r
+      MUTEX_LOCK( &stream_.mutex );\r
+\r
     result = buffer->Stop();\r
     if ( FAILED( result ) ) {\r
       errorStream_ << "RtApiDs::stopStream: error (" << getErrorString( result ) << ") stopping input buffer!";\r
@@ -6136,6 +6144,8 @@ void RtApiDs :: stopStream()
 \r
  unlock:\r
   timeEndPeriod( 1 ); // revert to normal scheduler frequency on lesser windows.\r
+  MUTEX_UNLOCK( &stream_.mutex );\r
+\r
   if ( FAILED( result ) ) error( RtAudioError::SYSTEM_ERROR );\r
 }\r
 \r
@@ -6222,6 +6232,12 @@ void RtApiDs :: callbackEvent()
   char *buffer;\r
   long bufferBytes;\r
 \r
+  MUTEX_LOCK( &stream_.mutex );\r
+  if ( stream_.state == STREAM_STOPPED ) {\r
+    MUTEX_UNLOCK( &stream_.mutex );\r
+    return;\r
+  }\r
+\r
   if ( buffersRolling == false ) {\r
     if ( stream_.mode == DUPLEX ) {\r
       //assert( handle->dsBufferSize[0] == handle->dsBufferSize[1] );\r
@@ -6402,11 +6418,12 @@ void RtApiDs :: callbackEvent()
     }\r
     nextWritePointer = ( nextWritePointer + bufferSize1 + bufferSize2 ) % dsBufferSize;\r
     handle->bufferPointer[0] = nextWritePointer;\r
+  }\r
 \r
-    if ( handle->drainCounter ) {\r
-      handle->drainCounter++;\r
-      goto unlock;\r
-    }\r
+  // Don't bother draining input\r
+  if ( handle->drainCounter ) {\r
+    handle->drainCounter++;\r
+    goto unlock;\r
   }\r
 \r
   if ( stream_.mode == INPUT || stream_.mode == DUPLEX ) {\r
@@ -6545,6 +6562,7 @@ void RtApiDs :: callbackEvent()
   }\r
 \r
  unlock:\r
+  MUTEX_UNLOCK( &stream_.mutex );\r
   RtApi::tickStreamTime();\r
 }\r
 \r