Fix erroneous getStreamTime() results in some cases.
authorCarl Hetherington <cth@carlh.net>
Mon, 15 Apr 2019 11:30:52 +0000 (12:30 +0100)
committerCarl Hetherington <cth@carlh.net>
Mon, 15 Apr 2019 11:30:52 +0000 (12:30 +0100)
Without this, if HAVE_GETTIMEOFDAY is defined getStreamTime() can return
a wrong value in the following case:

- start stream
- stop stream
- start stream
- call getStreamTime() immediately

In this case tickStreamTime() will not have been called since the
stream was restarted, hence lastTickTimestamp will refer to the
previous run of the stream.  getStreamTime() will therefore extrapolate
wrongly from stream._streamTime.  This patch only uses lastTickTimestamp
when it is valid.

RtAudio.cpp
RtAudio.h

index 6eebd5c98e67750d73396cad161fc325fcfe5173..f7dd7c024768dece2a00d92c2412c5c5310d336d 100644 (file)
@@ -409,7 +409,7 @@ double RtApi :: getStreamTime( void )
   struct timeval then;
   struct timeval now;
 
-  if ( stream_.state != STREAM_RUNNING || stream_.streamTime == 0.0 )
+  if ( stream_.state != STREAM_RUNNING || (stream_.lastTickTimestamp.tv_sec == 0 && stream_.lastTickTimestamp.tv_usec == 0) )
     return stream_.streamTime;
 
   gettimeofday( &now, NULL );
@@ -440,6 +440,14 @@ unsigned int RtApi :: getStreamSampleRate( void )
  return stream_.sampleRate;
 }
 
+void RtApi :: startStream( void )
+{
+#if defined( HAVE_GETTIMEOFDAY )
+  stream_.lastTickTimestamp.tv_sec = 0;
+  stream_.lastTickTimestamp.tv_usec = 0;
+#endif
+}
+
 
 // *************************************************** //
 //
@@ -1479,6 +1487,7 @@ void RtApiCore :: closeStream( void )
 void RtApiCore :: startStream( void )
 {
   verifyStream();
+  RtApi::startStream();
   if ( stream_.state == STREAM_RUNNING ) {
     errorText_ = "RtApiCore::startStream(): the stream is already running!";
     error( RtAudioError::WARNING );
@@ -2435,6 +2444,7 @@ void RtApiJack :: closeStream( void )
 void RtApiJack :: startStream( void )
 {
   verifyStream();
+  RtApi::startStream();
   if ( stream_.state == STREAM_RUNNING ) {
     errorText_ = "RtApiJack::startStream(): the stream is already running!";
     error( RtAudioError::WARNING );
@@ -3314,6 +3324,7 @@ bool stopThreadCalled = false;
 void RtApiAsio :: startStream()
 {
   verifyStream();
+  RtApi::startStream();
   if ( stream_.state == STREAM_RUNNING ) {
     errorText_ = "RtApiAsio::startStream(): the stream is already running!";
     error( RtAudioError::WARNING );
@@ -4413,6 +4424,7 @@ void RtApiWasapi::closeStream( void )
 void RtApiWasapi::startStream( void )
 {
   verifyStream();
+  RtApi::startStream();
 
   if ( stream_.state == STREAM_RUNNING ) {
     errorText_ = "RtApiWasapi::startStream: The stream is already running.";
@@ -6164,6 +6176,7 @@ void RtApiDs :: closeStream()
 void RtApiDs :: startStream()
 {
   verifyStream();
+  RtApi::startStream();
   if ( stream_.state == STREAM_RUNNING ) {
     errorText_ = "RtApiDs::startStream(): the stream is already running!";
     error( RtAudioError::WARNING );
@@ -6220,6 +6233,7 @@ void RtApiDs :: startStream()
 void RtApiDs :: stopStream()
 {
   verifyStream();
+  RtApi::startStream();
   if ( stream_.state == STREAM_STOPPED ) {
     errorText_ = "RtApiDs::stopStream(): the stream is already stopped!";
     error( RtAudioError::WARNING );
@@ -7841,6 +7855,7 @@ void RtApiAlsa :: startStream()
   // This method calls snd_pcm_prepare if the device isn't already in that state.
 
   verifyStream();
+  RtApi::startStream();
   if ( stream_.state == STREAM_RUNNING ) {
     errorText_ = "RtApiAlsa::startStream(): the stream is already running!";
     error( RtAudioError::WARNING );
@@ -8397,6 +8412,7 @@ void RtApiPulse::callbackEvent( void )
 
 void RtApiPulse::startStream( void )
 {
+  RtApi::startStream();
   PulseAudioHandle *pah = static_cast<PulseAudioHandle *>( stream_.apiHandle );
 
   if ( stream_.state == STREAM_CLOSED ) {
@@ -9337,6 +9353,7 @@ void RtApiOss :: closeStream()
 void RtApiOss :: startStream()
 {
   verifyStream();
+  RtApi::startStream();
   if ( stream_.state == STREAM_RUNNING ) {
     errorText_ = "RtApiOss::startStream(): the stream is already running!";
     error( RtAudioError::WARNING );
index a9fa6504a6c9069fd9c402c60ee0a81bd6a137f4..03b84b94accda66cf8a3cd4265aff17d6af4b82b 100644 (file)
--- a/RtAudio.h
+++ b/RtAudio.h
@@ -758,6 +758,9 @@ protected:
     double streamTime;         // Number of elapsed seconds since the stream started.
 
 #if defined(HAVE_GETTIMEOFDAY)
+    // The gettimeofday() when tickStreamTime was last called, or both
+    // fields at 0 if tickStreamTime has not been called since the last
+    // startStream().
     struct timeval lastTickTimestamp;
 #endif