Allow player to exit cleanly

- can't emit Terminate opcode in the middle of the bytestream
- pad the TCP stream to next 2k boundary when emitting terminate opcode,
  since the player will block until receiving this much data.
This commit is contained in:
kris 2019-03-23 21:46:35 +00:00
parent 8ffc8efaac
commit 531a6ae345
2 changed files with 15 additions and 9 deletions

View File

@ -29,12 +29,12 @@ def main(args):
m = movie.Movie(
filename,
every_n_video_frames=args.every_n_video_frames,
audio_normalization=args.audio_normalization)
audio_normalization=args.audio_normalization,
max_bytes_out = 1024. * 1024 * args.max_output_mb
)
print("Input frame rate = %f" % m.video.input_frame_rate)
max_bytes_out = 1024. * 1024 * args.max_output_mb
if args.output:
out_filename = args.output
else:
@ -45,11 +45,6 @@ def main(args):
for bytes_out, b in enumerate(m.emit_stream(m.encode())):
out.write(bytearray([b]))
if max_bytes_out and bytes_out >= max_bytes_out:
break
out.write(bytes(m.done()))
if __name__ == "__main__":
main(parser.parse_args())

View File

@ -12,9 +12,13 @@ class Movie:
def __init__(
self, filename: str,
every_n_video_frames: int = 1,
audio_normalization: float = None):
audio_normalization: float = None,
max_bytes_out: int = None
):
self.filename = filename # type: str
self.every_n_video_frames = every_n_video_frames # type: int
self.max_bytes_out = max_bytes_out # type: int
self.audio = audio.Audio(
filename, normalization=audio_normalization) # type: audio.Audio
self.video = video.Video(filename) # type: video.Video
@ -75,6 +79,9 @@ class Movie:
:return:
"""
for op in ops:
if self.stream_pos >= self.max_bytes_out:
yield from self.done()
return
# Keep track of where we are in TCP client socket buffer
socket_pos = self.stream_pos % 2048
if socket_pos >= 2044:
@ -88,3 +95,7 @@ class Movie:
:return:
"""
yield from self._emit_bytes(opcodes.Terminate())
# Player expects to fill 2K TCP buffer so pad it out
for _ in range(2048 - (self.stream_pos % 2048)):
yield 0x00