From b5dee4eb97f68be6a48d00ce33ddad508de84f4e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Schl=C3=BCter?= <jan.schluter@lis-lab.fr>
Date: Tue, 18 Jun 2019 20:25:50 +0200
Subject: [PATCH] Exit with an error if no audio data is received for a while

---
 CMakeLists.txt | 2 +-
 src/main.cpp   | 9 +++++++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3eaad61..aa8d40e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -4,7 +4,7 @@ if(POLICY CMP0063)
 endif()
 
 project(jasonrec)
-set(JASONREC_VERSION "1.2")
+set(JASONREC_VERSION "1.3")
 add_definitions(-DJASONREC_VERSION="${JASONREC_VERSION}")
 
 find_path(LIBUSB_INCLUDE_DIR
diff --git a/src/main.cpp b/src/main.cpp
index 20c7756..3e6e06b 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -56,6 +56,7 @@ int record(size_t channels, size_t rate, std::string &filename, float chunklen,
         allow_clean_exit();
         size_t total_samples_wanted = totallen * rate;
         size_t total_samples_read = 0;
+        size_t failed_attempts = 0;
         std::vector<std::int16_t> samples;
         try {
             recorder.start_recording();
@@ -74,6 +75,14 @@ int record(size_t channels, size_t rate, std::string &filename, float chunklen,
                     }
                     // pass it on to the file writer
                     filewriter->write(samples);
+                    failed_attempts = 0;
+                }
+                else {
+                    // if we received no message or no audio data 20x in a row, abort
+                    failed_attempts += 1;
+                    if (failed_attempts >= 20) {
+                        throw std::runtime_error("Device does not send audio data.");
+                    }
                 }
             }
             recorder.stop_recording();
-- 
GitLab