Skip to content

Commit

Permalink
Reapply changes after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
uvjustin committed Sep 29, 2020
1 parent fe6786a commit d04191d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions homeassistant/components/stream/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
MIN_SEGMENT_DURATION = 1.5 # Each segment is at least this many seconds

PACKETS_TO_WAIT_FOR_AUDIO = 20 # Some streams have an audio stream with no audio
MAX_TIMESTAMP_GAP = 10000 # seconds - anything from 10 to 50000 is probably reasonable
27 changes: 22 additions & 5 deletions homeassistant/components/stream/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import av

from .const import MIN_SEGMENT_DURATION, PACKETS_TO_WAIT_FOR_AUDIO
from .const import MAX_TIMESTAMP_GAP, MIN_SEGMENT_DURATION, PACKETS_TO_WAIT_FOR_AUDIO
from .core import Segment, StreamBuffer

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -200,6 +200,12 @@ def mux_audio_packet(packet):
packet.stream = output_streams[audio_stream]
buffer.output.mux(packet)

def finalize_stream():
if not stream.keepalive:
# End of stream, clear listeners and stop thread
for fmt, _ in outputs.items():
hass.loop.call_soon_threadsafe(stream.outputs[fmt].put, None)

if not peek_first_pts():
container.close()
return
Expand All @@ -222,15 +228,26 @@ def mux_audio_packet(packet):
continue
last_packet_was_without_dts = False
except (av.AVError, StopIteration) as ex:
if not stream.keepalive:
# 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", str(ex))
finalize_stream()
break

# Discard packet if dts is not monotonic
if packet.dts <= last_dts[packet.stream]:
if (last_dts[packet.stream] - packet.dts) > (
packet.time_base * MAX_TIMESTAMP_GAP
):
_LOGGER.warning(
"Timestamp overflow detected: dts = %s, resetting stream",
packet.dts,
)
finalize_stream()
break
_LOGGER.warning(
"Dropping out of order packet: %s <= %s",
packet.dts,
last_dts[packet.stream],
)
continue

# Check for end of segment
Expand Down

0 comments on commit d04191d

Please sign in to comment.