e36a924ebdf2 — Chris Cannam a month ago
Add an overall limit
2 files changed, 29 insertions(+), 12 deletions(-)

M src/SimpleWavFileReadStream.cpp
M src/SimpleWavFileReadStream.h
M src/SimpleWavFileReadStream.cpp +27 -11
@@ 65,7 65,8 @@ SimpleWavFileReadStream::SimpleWavFileRe
     m_dataChunkOffset(0),
     m_dataChunkSize(0),
     m_dataReadOffset(0),
-    m_dataReadStart(0)
+    m_dataReadStart(0),
+    m_retryCount(0)
 {
     m_file = new std::ifstream(filename.c_str(),
                                std::ios::in | std::ios::binary);

          
@@ 307,10 308,10 @@ SimpleWavFileReadStream::performSeek(siz
 }
 
 bool
-SimpleWavFileReadStream::shouldTryAgain(int justReadBytes)
+SimpleWavFileReadStream::shouldRetry(int justReadBytes)
 {
 #ifdef DEBUG_SIMPLE_WAV_FILE_READ_STREAM
-    std::cerr << "SimpleWavFileReadStream::shouldTryAgain: m_dataReadOffset = "
+    std::cerr << "SimpleWavFileReadStream::shouldRetry: m_dataReadOffset = "
               << m_dataReadOffset << ", m_dataChunkSize = " << m_dataChunkSize
               << ", justReadBytes = " << justReadBytes << std::endl;
 #endif

          
@@ 320,15 321,25 @@ SimpleWavFileReadStream::shouldTryAgain(
     }
     if (m_file->bad()) {
 #ifdef DEBUG_SIMPLE_WAV_FILE_READ_STREAM
-        std::cerr << "SimpleWavFileReadStream::shouldTryAgain: file is bad"
+        std::cerr << "SimpleWavFileReadStream::shouldRetry: file is bad"
                   << std::endl;
 #endif
         return false;
     }
 
+    int retryTimeMs = 50;
+    int totalTimeoutMs = 1000;
+    int permittedRetryCount = totalTimeoutMs / retryTimeMs;
+    
     uint32_t location = m_file->tellg();
 
     if (m_file->eof()) {
+        if (m_retryCount > permittedRetryCount) {
+#ifdef DEBUG_SIMPLE_WAV_FILE_READ_STREAM
+            std::cerr << "SimpleWavFileReadStream::shouldRetry: permitted retry limit of " << permittedRetryCount << " exceeded" << std::endl;
+#endif
+            return false;
+        }
         std::this_thread::sleep_for(std::chrono::milliseconds(100));
         m_file->clear();
         m_file->seekg(m_dataChunkOffset, std::ios::beg);

          
@@ 336,23 347,24 @@ SimpleWavFileReadStream::shouldTryAgain(
         m_file->seekg(location - justReadBytes, std::ios::beg);
         if (m_file->fail()) {
 #ifdef DEBUG_SIMPLE_WAV_FILE_READ_STREAM
-            std::cerr << "SimpleWavFileReadStream::shouldTryAgain: seek to "
+            std::cerr << "SimpleWavFileReadStream::shouldRetry: seek to "
                       << location - justReadBytes << " failed" << std::endl;
 #endif
             return false;
         }
 #ifdef DEBUG_SIMPLE_WAV_FILE_READ_STREAM
-        std::cerr << "SimpleWavFileReadStream::shouldTryAgain: returning true" << std::endl;
-#endif        
+        std::cerr << "SimpleWavFileReadStream::shouldRetry: returning true" << std::endl;
+#endif
+        ++m_retryCount;
         return true;
     } else {
 #ifdef DEBUG_SIMPLE_WAV_FILE_READ_STREAM
-        std::cerr << "SimpleWavFileReadStream::shouldTryAgain: after sync, file is at eof" << std::endl;
-#endif        
+        std::cerr << "SimpleWavFileReadStream::shouldRetry: file is not bad or at eof, something else must be wrong" << std::endl;
+#endif
     }
     
 #ifdef DEBUG_SIMPLE_WAV_FILE_READ_STREAM
-    std::cerr << "SimpleWavFileReadStream::shouldTryAgain: returning false" << std::endl;
+    std::cerr << "SimpleWavFileReadStream::shouldRetry: returning false" << std::endl;
 #endif        
     return false;
 }

          
@@ 373,7 385,7 @@ SimpleWavFileReadStream::getFrames(size_
         int gotHere = getBytes(sampleSize, buf);
         m_dataReadOffset += gotHere;
         if (gotHere < sampleSize) {
-            if (m_dataChunkSize == 0 && shouldTryAgain(gotHere)) {
+            if (m_dataChunkSize == 0 && shouldRetry(gotHere)) {
                 continue;
             }
             break;

          
@@ 397,6 409,10 @@ SimpleWavFileReadStream::getFrames(size_
     if (m_file->eof() && !m_file->bad()) {
         m_file->clear();
     }
+
+    if (got > 0) {
+        m_retryCount = 0;
+    }
     
     return got / m_channelCount;
 }

          
M src/SimpleWavFileReadStream.h +2 -1
@@ 81,7 81,8 @@ private:
     uint32_t readChunkSizeAfterTag();
     uint32_t readMandatoryNumber(int length);
 
-    bool shouldTryAgain(int justRead);
+    int m_retryCount;
+    bool shouldRetry(int justRead);
     
     float convertSample8(const std::vector<uint8_t> &);
     float convertSample16(const std::vector<uint8_t> &);