add delay before stream fallback

This commit is contained in:
zuckerberg 2021-06-10 12:59:24 -04:00
parent 9ee5d9547a
commit 1b83257ee3
2 changed files with 18 additions and 3 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
logs
radio.egg-info
__pycache__

View File

@ -1,6 +1,15 @@
from threading import Thread, main_thread from threading import Thread, main_thread
from util import non_block_read, non_block_peek from util import non_block_read, non_block_peek
from time import sleep from time import sleep, time
# time (ms) to wait before switching over to backup source
FALLBACK_TIME = 500
def current_milli_time():
return round(time() * 1000)
def empty(output):
return output == None or output == b''
# #
# In a new thread, listens for new data on the stream # In a new thread, listens for new data on the stream
@ -17,6 +26,7 @@ class StreamListener(Thread):
self.setDownstream(downstream) self.setDownstream(downstream)
self.setBackupUpstream(backupUpstream) self.setBackupUpstream(backupUpstream)
self.quit = False self.quit = False
self.lastData = 0
def setUpstream(self, upstream): def setUpstream(self, upstream):
if not upstream is None: if not upstream is None:
@ -51,9 +61,11 @@ class StreamListener(Thread):
output = None output = None
if not self.stream is None: if not self.stream is None:
output = non_block_read(self.stream) output = non_block_read(self.stream)
if (output == None or output == b'') and not self.backupStream is None: if empty(output) and not self.backupStream is None and current_milli_time() - self.lastData > FALLBACK_TIME:
output = non_block_read(self.backupStream) output = non_block_read(self.backupStream)
if output == None or output == b'': else:
self.lastData = current_milli_time()
if empty(output):
break break
self.listener.write(output) self.listener.write(output)
sleep(0.1) sleep(0.1)