2019-03-12 02:57:10 +00:00
|
|
|
"""Provides the worker thread needed for processing streams."""
|
2021-05-13 21:26:11 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-08-20 03:18:54 +00:00
|
|
|
from collections import deque
|
2021-06-13 16:41:21 +00:00
|
|
|
from collections.abc import Iterator, Mapping
|
|
|
|
from fractions import Fraction
|
2021-05-25 05:57:07 +00:00
|
|
|
from io import BytesIO
|
2019-03-12 02:57:10 +00:00
|
|
|
import logging
|
2021-06-13 16:41:21 +00:00
|
|
|
from threading import Event
|
|
|
|
from typing import Callable, cast
|
2019-03-12 02:57:10 +00:00
|
|
|
|
2019-10-14 21:20:18 +00:00
|
|
|
import av
|
|
|
|
|
2021-04-08 19:44:17 +00:00
|
|
|
from . import redact_credentials
|
2020-10-25 02:55:12 +00:00
|
|
|
from .const import (
|
2021-02-23 02:37:19 +00:00
|
|
|
AUDIO_CODECS,
|
2020-10-25 02:55:12 +00:00
|
|
|
MAX_MISSING_DTS,
|
|
|
|
MAX_TIMESTAMP_GAP,
|
|
|
|
MIN_SEGMENT_DURATION,
|
|
|
|
PACKETS_TO_WAIT_FOR_AUDIO,
|
2021-02-23 02:37:19 +00:00
|
|
|
SEGMENT_CONTAINER_FORMAT,
|
2021-05-30 03:41:23 +00:00
|
|
|
SOURCE_TIMEOUT,
|
2021-06-13 16:41:21 +00:00
|
|
|
TARGET_PART_DURATION,
|
2020-10-25 02:55:12 +00:00
|
|
|
)
|
2021-06-13 16:41:21 +00:00
|
|
|
from .core import Part, Segment, StreamOutput
|
2019-03-12 02:57:10 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2021-02-15 17:52:37 +00:00
|
|
|
class SegmentBuffer:
|
|
|
|
"""Buffer for writing a sequence of packets to the output as a segment."""
|
|
|
|
|
2021-06-13 16:41:21 +00:00
|
|
|
def __init__(
|
|
|
|
self, outputs_callback: Callable[[], Mapping[str, StreamOutput]]
|
|
|
|
) -> None:
|
2021-02-15 17:52:37 +00:00
|
|
|
"""Initialize SegmentBuffer."""
|
2021-06-13 16:41:21 +00:00
|
|
|
self._stream_id: int = 0
|
|
|
|
self._outputs_callback: Callable[
|
|
|
|
[], Mapping[str, StreamOutput]
|
|
|
|
] = outputs_callback
|
2021-05-27 03:22:31 +00:00
|
|
|
# sequence gets incremented before the first segment so the first segment
|
|
|
|
# has a sequence number of 0.
|
|
|
|
self._sequence = -1
|
2021-06-13 16:41:21 +00:00
|
|
|
self._segment_start_dts: int = cast(int, None)
|
2021-05-25 05:57:07 +00:00
|
|
|
self._memory_file: BytesIO = cast(BytesIO, None)
|
|
|
|
self._av_output: av.container.OutputContainer = None
|
|
|
|
self._input_video_stream: av.video.VideoStream = None
|
|
|
|
self._input_audio_stream = None # av.audio.AudioStream | None
|
|
|
|
self._output_video_stream: av.video.VideoStream = None
|
|
|
|
self._output_audio_stream = None # av.audio.AudioStream | None
|
2021-06-13 16:41:21 +00:00
|
|
|
self._segment: Segment | None = None
|
|
|
|
self._segment_last_write_pos: int = cast(int, None)
|
|
|
|
self._part_start_dts: int = cast(int, None)
|
|
|
|
self._part_has_keyframe = False
|
2021-05-25 05:57:07 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def make_new_av(
|
2021-05-26 08:19:09 +00:00
|
|
|
memory_file: BytesIO, sequence: int, input_vstream: av.video.VideoStream
|
2021-05-25 05:57:07 +00:00
|
|
|
) -> av.container.OutputContainer:
|
|
|
|
"""Make a new av OutputContainer."""
|
|
|
|
return av.open(
|
|
|
|
memory_file,
|
|
|
|
mode="w",
|
|
|
|
format=SEGMENT_CONTAINER_FORMAT,
|
|
|
|
container_options={
|
|
|
|
# Removed skip_sidx - see https://github.com/home-assistant/core/pull/39970
|
|
|
|
# "cmaf" flag replaces several of the movflags used, but too recent to use for now
|
2021-06-13 16:41:21 +00:00
|
|
|
"movflags": "empty_moov+default_base_moof+frag_discont+negative_cts_offsets+skip_trailer",
|
|
|
|
# Sometimes the first segment begins with negative timestamps, and this setting just
|
|
|
|
# adjusts the timestamps in the output from that segment to start from 0. Helps from
|
|
|
|
# having to make some adjustments in test_durations
|
|
|
|
"avoid_negative_ts": "make_non_negative",
|
2021-05-25 05:57:07 +00:00
|
|
|
"fragment_index": str(sequence + 1),
|
|
|
|
"video_track_timescale": str(int(1 / input_vstream.time_base)),
|
2021-06-13 16:41:21 +00:00
|
|
|
# Create a fragments every TARGET_PART_DURATION. The data from each fragment is stored in
|
|
|
|
# a "Part" that can be combined with the data from all the other "Part"s, plus an init
|
|
|
|
# section, to reconstitute the data in a "Segment".
|
|
|
|
"frag_duration": str(int(TARGET_PART_DURATION * 1e6)),
|
2021-05-25 05:57:07 +00:00
|
|
|
},
|
|
|
|
)
|
2021-02-15 17:52:37 +00:00
|
|
|
|
2021-05-25 05:57:07 +00:00
|
|
|
def set_streams(
|
|
|
|
self,
|
|
|
|
video_stream: av.video.VideoStream,
|
|
|
|
audio_stream,
|
|
|
|
# no type hint for audio_stream until https://github.com/PyAV-Org/PyAV/pull/775 is merged
|
|
|
|
) -> None:
|
2021-02-18 12:26:02 +00:00
|
|
|
"""Initialize output buffer with streams from container."""
|
2021-05-25 05:57:07 +00:00
|
|
|
self._input_video_stream = video_stream
|
|
|
|
self._input_audio_stream = audio_stream
|
2021-02-18 12:26:02 +00:00
|
|
|
|
2021-06-13 16:41:21 +00:00
|
|
|
def reset(self, video_dts: int) -> None:
|
2021-02-15 17:52:37 +00:00
|
|
|
"""Initialize a new stream segment."""
|
|
|
|
# Keep track of the number of segments we've processed
|
|
|
|
self._sequence += 1
|
2021-06-13 16:41:21 +00:00
|
|
|
self._segment_start_dts = self._part_start_dts = video_dts
|
|
|
|
self._segment = None
|
|
|
|
self._segment_last_write_pos = 0
|
2021-05-25 05:57:07 +00:00
|
|
|
self._memory_file = BytesIO()
|
|
|
|
self._av_output = self.make_new_av(
|
|
|
|
memory_file=self._memory_file,
|
|
|
|
sequence=self._sequence,
|
|
|
|
input_vstream=self._input_video_stream,
|
2021-02-23 02:37:19 +00:00
|
|
|
)
|
2021-05-25 05:57:07 +00:00
|
|
|
self._output_video_stream = self._av_output.add_stream(
|
|
|
|
template=self._input_video_stream
|
|
|
|
)
|
|
|
|
# Check if audio is requested
|
|
|
|
self._output_audio_stream = None
|
|
|
|
if self._input_audio_stream and self._input_audio_stream.name in AUDIO_CODECS:
|
|
|
|
self._output_audio_stream = self._av_output.add_stream(
|
|
|
|
template=self._input_audio_stream
|
|
|
|
)
|
2021-02-15 17:52:37 +00:00
|
|
|
|
2021-06-13 16:41:21 +00:00
|
|
|
def mux_packet(self, packet: av.Packet) -> None:
|
2021-05-25 05:57:07 +00:00
|
|
|
"""Mux a packet to the appropriate output stream."""
|
2021-02-15 17:52:37 +00:00
|
|
|
|
|
|
|
# Check for end of segment
|
2021-06-13 16:41:21 +00:00
|
|
|
if packet.stream == self._input_video_stream:
|
2021-02-15 17:52:37 +00:00
|
|
|
|
2021-06-13 16:41:21 +00:00
|
|
|
if (
|
|
|
|
packet.is_keyframe
|
|
|
|
and (
|
|
|
|
segment_duration := (packet.dts - self._segment_start_dts)
|
|
|
|
* packet.time_base
|
|
|
|
)
|
|
|
|
>= MIN_SEGMENT_DURATION
|
|
|
|
):
|
|
|
|
# Flush segment (also flushes the stub part segment)
|
|
|
|
self.flush(segment_duration, packet)
|
2021-02-15 17:52:37 +00:00
|
|
|
# Reinitialize
|
2021-06-13 16:41:21 +00:00
|
|
|
self.reset(packet.dts)
|
2021-02-15 17:52:37 +00:00
|
|
|
|
2021-06-13 16:41:21 +00:00
|
|
|
# Mux the packet
|
2021-05-25 05:57:07 +00:00
|
|
|
packet.stream = self._output_video_stream
|
|
|
|
self._av_output.mux(packet)
|
2021-06-13 16:41:21 +00:00
|
|
|
self.check_flush_part(packet)
|
|
|
|
self._part_has_keyframe |= packet.is_keyframe
|
|
|
|
|
2021-05-25 05:57:07 +00:00
|
|
|
elif packet.stream == self._input_audio_stream:
|
|
|
|
packet.stream = self._output_audio_stream
|
|
|
|
self._av_output.mux(packet)
|
2021-02-15 17:52:37 +00:00
|
|
|
|
2021-06-13 16:41:21 +00:00
|
|
|
def check_flush_part(self, packet: av.Packet) -> None:
|
|
|
|
"""Check for and mark a part segment boundary and record its duration."""
|
|
|
|
byte_position = self._memory_file.tell()
|
|
|
|
if self._segment_last_write_pos == byte_position:
|
|
|
|
return
|
|
|
|
if self._segment is None:
|
|
|
|
# We have our first non-zero byte position. This means the init has just
|
|
|
|
# been written. Create a Segment and put it to the queue of each output.
|
|
|
|
self._segment = Segment(
|
|
|
|
sequence=self._sequence,
|
|
|
|
stream_id=self._stream_id,
|
|
|
|
init=self._memory_file.getvalue(),
|
|
|
|
)
|
|
|
|
self._segment_last_write_pos = byte_position
|
|
|
|
# Fetch the latest StreamOutputs, which may have changed since the
|
|
|
|
# worker started.
|
|
|
|
for stream_output in self._outputs_callback().values():
|
|
|
|
stream_output.put(self._segment)
|
|
|
|
else: # These are the ends of the part segments
|
|
|
|
self._segment.parts.append(
|
|
|
|
Part(
|
|
|
|
duration=float(
|
|
|
|
(packet.dts - self._part_start_dts) * packet.time_base
|
|
|
|
),
|
|
|
|
has_keyframe=self._part_has_keyframe,
|
|
|
|
data=self._memory_file.getbuffer()[
|
|
|
|
self._segment_last_write_pos : byte_position
|
|
|
|
].tobytes(),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self._segment_last_write_pos = byte_position
|
|
|
|
self._part_start_dts = packet.dts
|
|
|
|
self._part_has_keyframe = False
|
|
|
|
|
|
|
|
def flush(self, duration: Fraction, packet: av.Packet) -> None:
|
2021-02-15 17:52:37 +00:00
|
|
|
"""Create a segment from the buffered packets and write to output."""
|
2021-05-25 05:57:07 +00:00
|
|
|
self._av_output.close()
|
2021-06-13 16:41:21 +00:00
|
|
|
assert self._segment
|
|
|
|
self._segment.duration = float(duration)
|
|
|
|
# Also flush the part segment (need to close the output above before this)
|
|
|
|
self._segment.parts.append(
|
|
|
|
Part(
|
|
|
|
duration=float((packet.dts - self._part_start_dts) * packet.time_base),
|
|
|
|
has_keyframe=self._part_has_keyframe,
|
|
|
|
data=self._memory_file.getbuffer()[
|
|
|
|
self._segment_last_write_pos :
|
|
|
|
].tobytes(),
|
|
|
|
)
|
2021-05-26 08:19:09 +00:00
|
|
|
)
|
2021-06-13 16:41:21 +00:00
|
|
|
self._memory_file.close() # We don't need the BytesIO object anymore
|
2021-02-18 12:26:02 +00:00
|
|
|
|
2021-06-13 16:41:21 +00:00
|
|
|
def discontinuity(self) -> None:
|
2021-02-18 12:26:02 +00:00
|
|
|
"""Mark the stream as having been restarted."""
|
|
|
|
# Preserving sequence and stream_id here keep the HLS playlist logic
|
|
|
|
# simple to check for discontinuity at output time, and to determine
|
|
|
|
# the discontinuity sequence number.
|
|
|
|
self._stream_id += 1
|
2021-02-15 17:52:37 +00:00
|
|
|
|
2021-06-13 16:41:21 +00:00
|
|
|
def close(self) -> None:
|
2021-02-23 02:37:19 +00:00
|
|
|
"""Close stream buffer."""
|
2021-05-25 05:57:07 +00:00
|
|
|
self._av_output.close()
|
2021-05-26 08:19:09 +00:00
|
|
|
self._memory_file.close()
|
2021-02-15 17:52:37 +00:00
|
|
|
|
|
|
|
|
2021-06-13 16:41:21 +00:00
|
|
|
def stream_worker( # noqa: C901
|
|
|
|
source: str, options: dict, segment_buffer: SegmentBuffer, quit_event: Event
|
|
|
|
) -> None:
|
2019-03-12 02:57:10 +00:00
|
|
|
"""Handle consuming streams."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2020-11-11 17:32:56 +00:00
|
|
|
try:
|
2021-05-30 03:41:23 +00:00
|
|
|
container = av.open(source, options=options, timeout=SOURCE_TIMEOUT)
|
2020-11-11 17:32:56 +00:00
|
|
|
except av.AVError:
|
2021-04-08 19:44:17 +00:00
|
|
|
_LOGGER.error("Error opening stream %s", redact_credentials(str(source)))
|
2020-11-11 17:32:56 +00:00
|
|
|
return
|
2019-03-12 02:57:10 +00:00
|
|
|
try:
|
|
|
|
video_stream = container.streams.video[0]
|
|
|
|
except (KeyError, IndexError):
|
|
|
|
_LOGGER.error("Stream has no video")
|
2020-09-10 19:55:55 +00:00
|
|
|
container.close()
|
2019-03-12 02:57:10 +00:00
|
|
|
return
|
2020-08-20 03:18:54 +00:00
|
|
|
try:
|
|
|
|
audio_stream = container.streams.audio[0]
|
|
|
|
except (KeyError, IndexError):
|
|
|
|
audio_stream = None
|
2020-09-10 19:55:55 +00:00
|
|
|
# These formats need aac_adtstoasc bitstream filter, but auto_bsf not
|
|
|
|
# compatible with empty_moov and manual bitstream filters not in PyAV
|
|
|
|
if container.format.name in {"hls", "mpegts"}:
|
|
|
|
audio_stream = None
|
2020-09-24 12:35:52 +00:00
|
|
|
# Some audio streams do not have a profile and throw errors when remuxing
|
|
|
|
if audio_stream and audio_stream.profile is None:
|
|
|
|
audio_stream = None
|
2019-03-12 02:57:10 +00:00
|
|
|
|
2020-10-25 02:55:12 +00:00
|
|
|
# Iterator for demuxing
|
2021-06-13 16:41:21 +00:00
|
|
|
container_packets: Iterator[av.Packet]
|
2020-08-20 03:18:54 +00:00
|
|
|
# The decoder timestamps of the latest packet in each stream we processed
|
2020-11-16 20:13:33 +00:00
|
|
|
last_dts = {video_stream: float("-inf"), audio_stream: float("-inf")}
|
2020-07-13 13:47:33 +00:00
|
|
|
# Keep track of consecutive packets without a dts to detect end of stream.
|
2020-10-25 02:55:12 +00:00
|
|
|
missing_dts = 0
|
2021-06-13 16:41:21 +00:00
|
|
|
# The video dts at the beginning of the segment
|
|
|
|
segment_start_dts: int | None = None
|
2020-08-20 03:18:54 +00:00
|
|
|
# Because of problems 1 and 2 below, we need to store the first few packets and replay them
|
2021-06-13 16:41:21 +00:00
|
|
|
initial_packets: deque[av.Packet] = deque()
|
2020-08-20 03:18:54 +00:00
|
|
|
|
|
|
|
# Have to work around two problems with RTSP feeds in ffmpeg
|
|
|
|
# 1 - first frame has bad pts/dts https://trac.ffmpeg.org/ticket/5018
|
|
|
|
# 2 - seeking can be problematic https://trac.ffmpeg.org/ticket/7815
|
|
|
|
|
2021-06-13 16:41:21 +00:00
|
|
|
def peek_first_dts() -> bool:
|
2020-11-16 20:13:33 +00:00
|
|
|
"""Initialize by peeking into the first few packets of the stream.
|
2020-08-20 03:18:54 +00:00
|
|
|
|
2020-11-16 20:13:33 +00:00
|
|
|
Deal with problem #1 above (bad first packet pts/dts) by recalculating using pts/dts from second packet.
|
2021-06-13 16:41:21 +00:00
|
|
|
Also load the first video keyframe dts into segment_start_dts and check if the audio stream really exists.
|
2020-11-16 20:13:33 +00:00
|
|
|
"""
|
2021-06-13 16:41:21 +00:00
|
|
|
nonlocal segment_start_dts, audio_stream, container_packets
|
2020-11-16 20:13:33 +00:00
|
|
|
missing_dts = 0
|
|
|
|
found_audio = False
|
2020-08-20 03:18:54 +00:00
|
|
|
try:
|
2020-10-25 02:55:12 +00:00
|
|
|
container_packets = container.demux((video_stream, audio_stream))
|
2020-11-16 20:13:33 +00:00
|
|
|
first_packet = None
|
2020-08-20 03:18:54 +00:00
|
|
|
# Get to first video keyframe
|
2020-11-16 20:13:33 +00:00
|
|
|
while first_packet is None:
|
2020-10-25 02:55:12 +00:00
|
|
|
packet = next(container_packets)
|
2020-09-21 01:26:24 +00:00
|
|
|
if (
|
2020-10-11 16:52:51 +00:00
|
|
|
packet.dts is None
|
2020-10-25 02:55:12 +00:00
|
|
|
): # Allow MAX_MISSING_DTS packets with no dts, raise error on the next one
|
|
|
|
if missing_dts >= MAX_MISSING_DTS:
|
|
|
|
raise StopIteration(
|
|
|
|
f"Invalid data - got {MAX_MISSING_DTS+1} packets with missing DTS while initializing"
|
|
|
|
)
|
|
|
|
missing_dts += 1
|
2020-10-11 16:52:51 +00:00
|
|
|
continue
|
2020-11-16 20:13:33 +00:00
|
|
|
if packet.stream == audio_stream:
|
|
|
|
found_audio = True
|
|
|
|
elif packet.is_keyframe: # video_keyframe
|
|
|
|
first_packet = packet
|
2020-08-20 03:18:54 +00:00
|
|
|
initial_packets.append(packet)
|
2021-06-13 16:41:21 +00:00
|
|
|
# Get first_dts from subsequent frame to first keyframe
|
|
|
|
while segment_start_dts is None or (
|
2020-11-16 20:13:33 +00:00
|
|
|
audio_stream
|
|
|
|
and not found_audio
|
|
|
|
and len(initial_packets) < PACKETS_TO_WAIT_FOR_AUDIO
|
|
|
|
):
|
2020-10-25 02:55:12 +00:00
|
|
|
packet = next(container_packets)
|
2020-10-11 16:52:51 +00:00
|
|
|
if (
|
|
|
|
packet.dts is None
|
2020-10-25 02:55:12 +00:00
|
|
|
): # Allow MAX_MISSING_DTS packet with no dts, raise error on the next one
|
|
|
|
if missing_dts >= MAX_MISSING_DTS:
|
|
|
|
raise StopIteration(
|
|
|
|
f"Invalid data - got {MAX_MISSING_DTS+1} packets with missing DTS while initializing"
|
|
|
|
)
|
|
|
|
missing_dts += 1
|
2020-10-11 16:52:51 +00:00
|
|
|
continue
|
2020-11-16 20:13:33 +00:00
|
|
|
if packet.stream == audio_stream:
|
2021-03-06 15:40:49 +00:00
|
|
|
# detect ADTS AAC and disable audio
|
|
|
|
if audio_stream.codec.name == "aac" and packet.size > 2:
|
|
|
|
with memoryview(packet) as packet_view:
|
|
|
|
if packet_view[0] == 0xFF and packet_view[1] & 0xF0 == 0xF0:
|
|
|
|
_LOGGER.warning(
|
|
|
|
"ADTS AAC detected - disabling audio stream"
|
|
|
|
)
|
|
|
|
container_packets = container.demux(video_stream)
|
|
|
|
audio_stream = None
|
|
|
|
continue
|
2020-11-16 20:13:33 +00:00
|
|
|
found_audio = True
|
|
|
|
elif (
|
2021-06-13 16:41:21 +00:00
|
|
|
segment_start_dts is None
|
|
|
|
): # This is the second video frame to calculate first_dts from
|
|
|
|
segment_start_dts = packet.dts - packet.duration
|
|
|
|
first_packet.pts = first_packet.dts = segment_start_dts
|
2020-08-20 03:18:54 +00:00
|
|
|
initial_packets.append(packet)
|
2020-11-16 20:13:33 +00:00
|
|
|
if audio_stream and not found_audio:
|
2020-08-20 03:18:54 +00:00
|
|
|
_LOGGER.warning(
|
|
|
|
"Audio stream not found"
|
|
|
|
) # Some streams declare an audio stream and never send any packets
|
|
|
|
audio_stream = None
|
|
|
|
|
|
|
|
except (av.AVError, StopIteration) as ex:
|
|
|
|
_LOGGER.error(
|
|
|
|
"Error demuxing stream while finding first packet: %s", str(ex)
|
|
|
|
)
|
2020-09-03 16:22:00 +00:00
|
|
|
return False
|
|
|
|
return True
|
2020-08-20 03:18:54 +00:00
|
|
|
|
2021-06-13 16:41:21 +00:00
|
|
|
if not peek_first_dts():
|
2020-09-03 16:22:00 +00:00
|
|
|
container.close()
|
|
|
|
return
|
2020-11-16 20:13:33 +00:00
|
|
|
|
2021-02-18 12:26:02 +00:00
|
|
|
segment_buffer.set_streams(video_stream, audio_stream)
|
2021-06-13 16:41:21 +00:00
|
|
|
assert isinstance(segment_start_dts, int)
|
|
|
|
segment_buffer.reset(segment_start_dts)
|
2019-03-12 02:57:10 +00:00
|
|
|
|
|
|
|
while not quit_event.is_set():
|
|
|
|
try:
|
2020-08-20 03:18:54 +00:00
|
|
|
if len(initial_packets) > 0:
|
|
|
|
packet = initial_packets.popleft()
|
|
|
|
else:
|
2020-10-25 02:55:12 +00:00
|
|
|
packet = next(container_packets)
|
2019-03-12 02:57:10 +00:00
|
|
|
if packet.dts is None:
|
2020-10-25 02:55:12 +00:00
|
|
|
# Allow MAX_MISSING_DTS consecutive packets without dts. Terminate the stream on the next one.
|
|
|
|
if missing_dts >= MAX_MISSING_DTS:
|
|
|
|
raise StopIteration(
|
|
|
|
f"No dts in {MAX_MISSING_DTS+1} consecutive packets"
|
|
|
|
)
|
|
|
|
missing_dts += 1
|
2020-07-13 13:47:33 +00:00
|
|
|
continue
|
2020-10-25 02:55:12 +00:00
|
|
|
missing_dts = 0
|
2019-03-12 02:57:10 +00:00
|
|
|
except (av.AVError, StopIteration) as ex:
|
2019-03-21 14:31:55 +00:00
|
|
|
_LOGGER.error("Error demuxing stream: %s", str(ex))
|
2019-03-12 02:57:10 +00:00
|
|
|
break
|
|
|
|
|
2020-08-20 03:18:54 +00:00
|
|
|
# Discard packet if dts is not monotonic
|
|
|
|
if packet.dts <= last_dts[packet.stream]:
|
2020-10-16 15:48:45 +00:00
|
|
|
if (
|
|
|
|
packet.time_base * (last_dts[packet.stream] - packet.dts)
|
|
|
|
> MAX_TIMESTAMP_GAP
|
2020-09-30 15:45:59 +00:00
|
|
|
):
|
2020-10-16 15:48:45 +00:00
|
|
|
_LOGGER.warning(
|
|
|
|
"Timestamp overflow detected: last dts %s, dts = %s, resetting stream",
|
|
|
|
last_dts[packet.stream],
|
|
|
|
packet.dts,
|
|
|
|
)
|
|
|
|
break
|
2019-03-21 14:31:55 +00:00
|
|
|
continue
|
2020-08-20 03:18:54 +00:00
|
|
|
|
|
|
|
# Update last_dts processed
|
|
|
|
last_dts[packet.stream] = packet.dts
|
2021-02-15 17:52:37 +00:00
|
|
|
|
|
|
|
# Mux packets, and possibly write a segment to the output stream.
|
|
|
|
# This mutates packet timestamps and stream
|
|
|
|
segment_buffer.mux_packet(packet)
|
2020-05-22 16:13:37 +00:00
|
|
|
|
|
|
|
# Close stream
|
2021-02-15 17:52:37 +00:00
|
|
|
segment_buffer.close()
|
2020-05-22 16:13:37 +00:00
|
|
|
container.close()
|