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
25 changes: 20 additions & 5 deletions pyhap/camera_multi_tier.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ class MultiTierSession(StreamSession):
``address`` and the ports are where the controller receives, with the
SRTP keys it sent. The accessory sends from ``accessory_address`` and the
``accessory_*_port`` values, by default its address on the controller's
connection and the controller's port numbers. ``handle_prepare`` may
change them. ``video_ssrc`` and ``audio_ssrc`` are the accessory's SSRCs
connection (see ``MultiTierConfig.accessory_address``) and the
controller's port numbers. ``handle_prepare`` may change them.
``video_ssrc`` and ``audio_ssrc`` are the accessory's SSRCs
from the Setup Endpoints answer, the controller filters by them. The
``controller_*_ssrc`` values are what the controller sent with the last
start. ``video_tier`` and ``audio_tier`` are what streams once started.
Expand Down Expand Up @@ -171,6 +172,11 @@ class MultiTierConfig:
A started session ends unless :meth:`MultiTierSession.keep_alive` is
called at least every ``idle_timeout`` seconds, e.g. for each RTCP packet
of the controller. None turns that off.
``accessory_address`` is the address the Setup Endpoints answer announces
for sessions of its IP version, e.g. for an accessory with several
interfaces. Without it, and for sessions of the other version,
the answer has the accessory's address on the controller's connection.
``handle_prepare`` may still change it.
"""

delegate: MultiTierDelegate | None = None
Expand All @@ -180,6 +186,7 @@ class MultiTierConfig:
hook_timeout: float = DEFAULT_HOOK_TIMEOUT
setup_timeout: float = 30.0
idle_timeout: float | None = DEFAULT_IDLE_TIMEOUT
accessory_address: str | None = None

@classmethod
def from_option(cls, value: MultiTierConfig | Mapping[str, Any]) -> MultiTierConfig:
Expand All @@ -206,6 +213,11 @@ def __post_init__(self) -> None:
self.idle_timeout is not None and self.idle_timeout <= 0
):
raise ValueError("multi_tier timeouts must be positive")
address = self.accessory_address
if address is not None and not (
_valid_address(address, False) or _valid_address(address, True)
):
raise ValueError("multi_tier accessory_address must be an IP address")


def _supported_audio_tiers(audio: hksv3.AudioStreamTier) -> bytes:
Expand Down Expand Up @@ -360,9 +372,12 @@ async def _setup(
if self.delegate is None:
logger.warning("[%s] No multi-tier delegate, refusing", session_id)
return error
accessory_address = self.camera._stream_address_for(
client_addr, endpoint.is_ipv6
)
accessory_address = self.config.accessory_address
if not _valid_address(accessory_address, endpoint.is_ipv6):
# Not set, or of the other IP version.
accessory_address = self.camera._stream_address_for(
client_addr, endpoint.is_ipv6
)
if accessory_address is None:
logger.warning(
"[%s] No local address of the controller's IP version", session_id
Expand Down
45 changes: 43 additions & 2 deletions tests/test_camera_multi_tier.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ async def test_rtp_services_come_after(stream_camera: Callable[..., Camera]) ->
({"multi_tier": {"hook_timeout": 9}}, "hook_timeout"),
({"webrtc": {"remote_quality": 5}}, "remote_quality"),
({"multi_tier": {"audio": None}}, "exactly one audio tier"),
({"multi_tier": {"accessory_address": "cam.local"}}, "address must be"),
({"multi_tier": {"accessory_address": "fe80::1%eth0"}}, "address must be"),
({"multi_tier": {"accessory_address": "::ffff:1.2.3.4"}}, "address must be"),
],
)
async def test_invalid_options(
Expand Down Expand Up @@ -337,8 +340,45 @@ async def test_setup_answers_from_the_connection(
assert address[SETUP_ADDR_INFO["ADDRESS"]] == b"fd00::10"


IPV4 = (b"192.168.1.114", False, "192.168.1.10")
IPV6 = (b"fd00::1", True, "fd00::10")


@pytest.mark.parametrize(
("configured", "family", "announced"),
[
(None, IPV4, b"192.168.1.10"),
(None, IPV6, b"fd00::10"),
("192.0.2.10", IPV4, b"192.0.2.10"),
("192.0.2.10", IPV6, b"fd00::10"),
("fd00::20", IPV6, b"fd00::20"),
("fd00::20", IPV4, b"192.168.1.10"),
],
ids=["unset-v4", "unset-v6", "v4", "v4-on-v6", "v6", "v6-on-v4"],
)
async def test_setup_answers_from_the_configured_address(
stream_camera: Callable[..., Camera],
multi_tier_delegate: AsyncMock,
configured: str | None,
family: tuple[bytes, bool, str],
announced: bytes,
) -> None:
"""Test ``accessory_address`` is announced to controllers of its IP version."""
controller, is_ipv6, local = family
camera = stream_camera(
multi_tier={"delegate": multi_tier_delegate, "accessory_address": configured}
)
with patch.object(camera.driver, "_local_address", return_value=local):
answer = await _setup(camera, _setup_request(controller, is_ipv6))
address = tlv.decode(answer[SETUP_TYPES["ADDRESS"]])
assert address[SETUP_ADDR_INFO["ADDRESS"]] == announced


@pytest.mark.parametrize("configured", [None, "192.0.2.10"])
async def test_prepare_may_change_the_answer(
stream_camera: Callable[..., Camera], multi_tier_delegate: AsyncMock
stream_camera: Callable[..., Camera],
multi_tier_delegate: AsyncMock,
configured: str | None,
) -> None:
"""Test the delegate may answer with other addresses and ports."""

Expand All @@ -347,7 +387,8 @@ async def _prepare(session: MultiTierSession) -> None:
session.accessory_video_port = 40000

multi_tier_delegate.handle_prepare.side_effect = _prepare
answer = await _setup(stream_camera())
options = {"delegate": multi_tier_delegate, "accessory_address": configured}
answer = await _setup(stream_camera(multi_tier=options))
address = tlv.decode(answer[SETUP_TYPES["ADDRESS"]])
assert address[SETUP_ADDR_INFO["ADDRESS"]] == b"192.168.1.20"
assert address[SETUP_ADDR_INFO["VIDEO_RTP_PORT"]] == struct.pack("<H", 40000)
Expand Down
Loading