diff --git a/pyhap/media/recording.py b/pyhap/media/recording.py index afe6727b..59108d63 100644 --- a/pyhap/media/recording.py +++ b/pyhap/media/recording.py @@ -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) diff --git a/tests/test_media_recording.py b/tests/test_media_recording.py index b123f850..6969103a 100644 --- a/tests/test_media_recording.py +++ b/tests/test_media_recording.py @@ -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)