From 56cbe8a73f02a989ad0eab026c52441d804124b2 Mon Sep 17 00:00:00 2001 From: Jason Hunter Date: Thu, 21 Mar 2019 10:31:55 -0400 Subject: [PATCH] Stream fixes (#22238) * fix issues with out of order packets, and empty first packet on some IP camera models * do not skip the first packet --- homeassistant/components/stream/worker.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/stream/worker.py b/homeassistant/components/stream/worker.py index 3a3e19d970330a..d0196761968b38 100644 --- a/homeassistant/components/stream/worker.py +++ b/homeassistant/components/stream/worker.py @@ -63,21 +63,29 @@ def stream_worker(hass, stream, quit_event): first_packet = True sequence = 1 audio_packets = {} + last_dts = None while not quit_event.is_set(): try: packet = next(container.demux(video_stream)) if packet.dts is None: + if first_packet: + continue # If we get a "flushing" packet, the stream is done - raise StopIteration + raise StopIteration("No dts in packet") except (av.AVError, StopIteration) as ex: # End of stream, clear listeners and stop thread for fmt, _ in outputs.items(): hass.loop.call_soon_threadsafe( stream.outputs[fmt].put, None) - _LOGGER.error("Error demuxing stream: %s", ex) + _LOGGER.error("Error demuxing stream: %s", str(ex)) break + # Skip non monotonically increasing dts in feed + if not first_packet and last_dts >= packet.dts: + continue + last_dts = packet.dts + # Reset segment on every keyframe if packet.is_keyframe: # Save segment to outputs