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
11 changes: 8 additions & 3 deletions pyhap/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -2133,12 +2133,17 @@ async def start_recording_transport(self) -> None:
"""Start the HDS listener on the HAP server's listen address(es).

``run()`` calls this. Call it directly when ``run()`` is not awaited.
Does nothing without the recording service.
Does nothing without the recording service. If
:meth:`stop_recording_transport` is called while this still starts,
nothing is left listening or armed.
"""
if self._recording_service is not None:
generation = self._hds_generation
await self._ensure_hds_listener()
self._recording_media_running = True
self._arm_recording_media()
# A stop that came in meanwhile has closed the delegate already.
if generation == self._hds_generation:
self._recording_media_running = True
self._arm_recording_media()

@property
def recording_transport_port(self) -> int | None:
Expand Down
29 changes: 22 additions & 7 deletions tests/test_hksv_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import os
import socket
from typing import Any
from unittest.mock import MagicMock, patch
from unittest.mock import AsyncMock, MagicMock, patch

import pytest

Expand Down Expand Up @@ -57,7 +57,7 @@ async def handle_recording_stream(
self.cleaned.set()


def _make_camera(camera_class: Any = Camera) -> Any:
def _make_camera(camera_class: Any = Camera, recording: Any = True) -> Any:
with (
patch("pyhap.accessory_driver.AccessoryDriver.persist"),
patch("pyhap.accessory_driver.AccessoryDriver.load"),
Expand All @@ -76,7 +76,7 @@ def _make_camera(camera_class: Any = Camera) -> Any:
"audio": {"codecs": [{"type": "OPUS", "samplerate": 24}]},
"srtp": True,
"address": "127.0.0.1",
"recording": True,
"recording": recording,
}
return camera_class(options, driver, "Cam")

Expand Down Expand Up @@ -371,8 +371,17 @@ async def test_run_and_stop_drive_recording_transport(camera: Any) -> None:
assert camera._hds_listener is None


@pytest.mark.parametrize(
("start", "stop"),
[("start_recording_transport", "stop_recording_transport"), ("run", "stop")],
)
@pytest.mark.asyncio
async def test_stop_during_start_does_not_publish_listener(camera: Any) -> None:
async def test_stop_during_start_leaves_nothing_running(start: str, stop: str) -> None:
"""A start that a stop overtakes publishes no listener and arms no delegate."""
delegate = AsyncMock(spec=hds_recording.RecordingProducer, prebuffer_ms=0)
camera = _make_camera(recording={"delegate": delegate})
# Recording is on, as restored after a restart.
camera._recording_service.get_characteristic("Active").set_value(1)
release = asyncio.Event()
original_start = hds_server.HDSListener.start
binding = []
Expand All @@ -383,13 +392,19 @@ async def slow_start(self: Any, host: str) -> Any:
return await original_start(self, host)

with patch.object(hds_server.HDSListener, "start", slow_start):
start = asyncio.ensure_future(camera.start_recording_transport())
starting = asyncio.ensure_future(getattr(camera, start)())
await asyncio.sleep(0)
await camera.stop_recording_transport()
await getattr(camera, stop)()
release.set()
await start
await starting
# A task of the start to arm the delegate would run now.
await asyncio.sleep(0)
assert camera._hds_listener is None
assert not binding[0]._servers
# Running would let a later hub write arm the closed delegate.
assert camera._recording_media_running is False
delegate.set_armed.assert_not_awaited()
delegate.close.assert_awaited_once_with()


@pytest.mark.asyncio
Expand Down
Loading