Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions pyhap/media/recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,26 @@ def on_source_closed(self, reason: str) -> None:
def snapshot(self, seconds: float) -> list[BufferedMedia]:
"""The buffered media from the latest IRAP at least ``seconds`` ago.

Fewer seconds when the ring holds less; empty without an IRAP.
Fewer seconds when the ring holds less; empty without an IRAP. Media
from before a pause of the ring's seconds or longer is left out, e.g.
what came before the camera went offline.
"""
limit = self._clock() - seconds
start = 0
for index, item in enumerate(self._items):
if item.arrival > limit:
items = list(self._items)
later = self._clock()
limit = later - seconds
start = len(items)
for index in reversed(range(len(items))):
item = items[index]
if later - item.arrival >= self.seconds:
# No media for the ring's seconds after this item: it and all
# before it are older than the ring is meant to hold.
break
later = item.arrival
if item.keyframe:
start = index
return list(self._items)[start:]
if item.arrival <= limit:
break
return items[start:]

def _add(self, item: BufferedMedia) -> None:
self._items.append(item)
Expand Down
29 changes: 29 additions & 0 deletions tests/test_media_recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,35 @@ def test_ring_closed_source() -> None:
assert ring.snapshot(4.0) == []


@pytest.mark.parametrize(
("pause", "arrivals"),
[(3.0, [106.0, 107.0]), (4.0, []), (12.0, [])],
)
def test_ring_hands_out_no_stale_media(pause: float, arrivals: list[float]) -> None:
"""Verify media that stopped coming for the ring's seconds is not handed out."""
clock = Clock()
ring = PrebufferRing(4.0, clock=clock)
for second in range(8):
clock.now = 100.0 + second
ring.on_source_video(unit(second % 2 == 0, second), 1)
clock.now += pause
assert [item.arrival for item in ring.snapshot(4.0)] == arrivals


def test_ring_starts_over_at_an_irap_after_a_pause() -> None:
"""Verify media that comes after a pause is handed out from its first IRAP."""
clock = Clock()
ring = PrebufferRing(4.0, clock=clock)
ring.on_source_video(unit(True), 1)
clock.now = 112.0
ring.on_source_video(unit(False, 1), 1)
ring.on_source_audio(audio(1))
assert ring.snapshot(4.0) == []
ring.on_source_video(unit(True, 2), 1)
ring.on_source_video(unit(False, 3), 1)
assert [item.unit for item in ring.snapshot(4.0)] == [unit(True, 2), unit(False, 3)]


def test_timestamps_map_to_a_common_timeline() -> None:
"""Verify RTP timestamps wrap and re-anchor after a jump."""
video = _Timestamps(90000, origin=10.0)
Expand Down
Loading