Ensure birdseye pipe does not get corrupted

This commit is contained in:
Nick Mowen 2023-02-15 07:12:47 -07:00
parent 1715e2e09d
commit e98120d2ee

View File

@ -38,16 +38,10 @@ class FFMpegConverter:
quality: int, quality: int,
birdseye_rtsp: bool = False, birdseye_rtsp: bool = False,
): ):
if birdseye_rtsp: self.bd_pipe = None
if os.path.exists(BIRDSEYE_PIPE):
os.remove(BIRDSEYE_PIPE)
os.mkfifo(BIRDSEYE_PIPE, mode=0o777) if birdseye_rtsp:
stdin = os.open(BIRDSEYE_PIPE, os.O_RDONLY | os.O_NONBLOCK) self.recreate_birdseye_pipe()
self.bd_pipe = os.open(BIRDSEYE_PIPE, os.O_WRONLY)
os.close(stdin)
else:
self.bd_pipe = None
ffmpeg_cmd = [ ffmpeg_cmd = [
"ffmpeg", "ffmpeg",
@ -80,14 +74,34 @@ class FFMpegConverter:
start_new_session=True, start_new_session=True,
) )
def recreate_birdseye_pipe(self) -> None:
if self.bd_pipe:
os.close(self.bd_pipe)
if os.path.exists(BIRDSEYE_PIPE):
os.remove(BIRDSEYE_PIPE)
os.mkfifo(BIRDSEYE_PIPE, mode=0o777)
stdin = os.open(BIRDSEYE_PIPE, os.O_RDONLY | os.O_NONBLOCK)
self.bd_pipe = os.open(BIRDSEYE_PIPE, os.O_WRONLY)
os.close(stdin)
self.reading_birdseye = False
def write(self, b) -> None: def write(self, b) -> None:
self.process.stdin.write(b) self.process.stdin.write(b)
if self.bd_pipe: if self.bd_pipe:
try: try:
os.write(self.bd_pipe, b) os.write(self.bd_pipe, b)
self.reading_birdseye = True
except BrokenPipeError: except BrokenPipeError:
# catch error when no one is listening if self.reading_birdseye:
# we know the pipe was being read from and now it is not
# so we should recreate the pipe to ensure no partially-read
# frames exist
logger.error("Recreating the pipe because it was read from and now is not")
self.recreate_birdseye_pipe()
return return
def read(self, length): def read(self, length):