From abcd1f8020c1ca54d35ea4e624e8a39fb4b2380c Mon Sep 17 00:00:00 2001 From: Naka-Masa19 Date: Wed, 25 Nov 2026 16:17:07 +0900 Subject: [PATCH 1/2] Start talkback receiver after stream configuration --- pyhap/camera.py | 5 ++++- pyhap/camera_talkback.py | 15 ++++++++++----- tests/test_camera.py | 37 +++++++++++++++++++++++++------------ 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/pyhap/camera.py b/pyhap/camera.py index 28eb218d..9c675bc8 100644 --- a/pyhap/camera.py +++ b/pyhap/camera.py @@ -1742,6 +1742,10 @@ async def _start_stream(self, objs: dict[bytes, Any], reconfigure: bool) -> None success = await self.reconfigure_stream(session_info, opts) else: success = await self.start_stream(session_info, opts) + if success and self.support_talkback: + session_info["audio_backchannel"].start( + {key: opts[key] for key in ("a_codec",)} + ) if success: self._streaming_status[stream_idx] = STREAMING_STATUS["STREAMING"] @@ -2041,7 +2045,6 @@ def set_endpoints(self, value: str, stream_idx: int | None = None) -> str: is_ipv6=bool(is_ipv6), controller_address=address, ) - audio_backchannel.start() audio_rtp_port = audio_backchannel.local_port res_address_tlv = tlv.encode( diff --git a/pyhap/camera_talkback.py b/pyhap/camera_talkback.py index a2f869d6..4838f2f0 100644 --- a/pyhap/camera_talkback.py +++ b/pyhap/camera_talkback.py @@ -29,7 +29,7 @@ import socket import struct import threading -from typing import Protocol, cast +from typing import Any, Protocol, cast import pylibsrtp @@ -138,8 +138,12 @@ def local_port(self) -> int: that the controller knows where to send the talkback audio. """ - def start(self) -> None: - """Start listening for incoming audio.""" + def start(self, audio_config: dict[str, Any]) -> None: + """Start listening for incoming audio. + :param audio_config: Audio configuration, as negotiated with the HAP client. + Implementations can only use part of these. Available key: + - a_codec - Audio codec + """ def stop(self) -> None: """Stop listening and release the socket.""" @@ -198,14 +202,15 @@ def local_port(self) -> int: """ return cast(int, self._sock.getsockname()[1]) - def start(self) -> None: + def start(self, audio_config: dict[str, Any]) -> None: """Start listening for incoming audio in a background thread.""" self._thread.start() def stop(self) -> None: """Stop listening and release the socket.""" self._stop_event.set() - self._thread.join(timeout=2) + if self._thread.ident is not None: + self._thread.join(timeout=2) self._sock.close() def _run(self) -> None: diff --git a/tests/test_camera.py b/tests/test_camera.py index 4ac62d27..0f44551d 100644 --- a/tests/test_camera.py +++ b/tests/test_camera.py @@ -23,6 +23,14 @@ "qGeNmESTIojxNiAw78WkjTLtGv0waWnLo9gQEBAA==" ) +_SELECTED_STREAM_START_REQUEST = ( + "ARUCAQEBEKzMbMEFY0UVjal0tFCQBpECNAEBAAIJAQEAAgEAAwEAAwsBAoAC" + "AgJoAQMBHgQXAQFjAgQr66FSAwKEAAQEAAAAPwUCYgUDLAEBAgIMAQEBAgEA" + "AwEBBAEeAxYBAW4CBMUInmQDAhgABAQAAKBABgENBAEA" +) + +_SELECTED_STREAM_STOP_REQUEST = "ARUCAQABEKzMbMEFY0UVjal0tFCQBpE=" + _OPTIONS = { "stream_count": 4, "video": { @@ -140,12 +148,6 @@ async def subprocess_exec(*args: Any, **kwargs: Any) -> Mock: process_mock.wait = wait return process_mock - selected_config_req = ( - "ARUCAQEBEKzMbMEFY0UVjal0tFCQBpECNAEBAAIJAQEAAgEAAwEAAwsBAoAC" - "AgJoAQMBHgQXAQFjAgQr66FSAwKEAAQEAAAAPwUCYgUDLAEBAgIMAQEBAgEA" - "AwEBBAEeAxYBAW4CBMUInmQDAhgABAQAAKBABgENBAEA" - ) - session_id = UUID("accc6cc1-0563-4515-8da9-74b450900691") session_info = { @@ -166,12 +168,11 @@ async def subprocess_exec(*args: Any, **kwargs: Any) -> Mock: patcher = patch("asyncio.create_subprocess_exec", new=subprocess_exec) patcher.start() - acc.set_selected_stream_configuration(selected_config_req) + acc.set_selected_stream_configuration(_SELECTED_STREAM_START_REQUEST) assert acc.streaming_status == camera.STREAMING_STATUS["STREAMING"] - selected_config_stop_req = "ARUCAQABEKzMbMEFY0UVjal0tFCQBpE=" - acc.set_selected_stream_configuration(selected_config_stop_req) + acc.set_selected_stream_configuration(_SELECTED_STREAM_STOP_REQUEST) patcher.stop() @@ -298,6 +299,7 @@ def talkback_audio_received(self, session_id: Any, payload: Any) -> None: # The test vector advertises 192.168.1.114 as the controller; pretend the # controller is local so the loopback send passes the source filter. receiver._controller_address = "127.0.0.1" + receiver.start({}) key_and_salt = _audio_srtp_key_and_salt(_SETUP_ENDPOINTS_REQUEST) rtp_payload = b"opus-frame-payload" @@ -344,7 +346,7 @@ def local_port(self) -> int: local_port_mock() return 12345 - def start(self) -> None: + def start(self, audio_config: dict[str, Any]) -> None: start_mock() def stop(self) -> None: @@ -360,6 +362,14 @@ def create_talkback_receiver( ) -> TalkbackReceiverProtocol: return CustomReceiver() + async def start_stream( + self, session_info: dict[str, Any], stream_config: dict[str, Any] + ) -> bool: + return True + + async def stop_stream(self, session_info: dict[str, Any]) -> None: + pass + options = dict(_OPTIONS, talkback=True) acc = CustomCamera(options, mock_driver, "Camera") management = acc.get_service("CameraRTPStreamManagement") @@ -370,10 +380,13 @@ def create_talkback_receiver( session_id = list(acc.sessions)[0] receiver = acc.sessions[session_id]["audio_backchannel"] assert isinstance(receiver, CustomReceiver) - acc._close_talkback(acc.sessions[session_id]) - local_port_mock.assert_called() + start_mock.assert_not_called() + + acc.set_selected_stream_configuration(_SELECTED_STREAM_START_REQUEST) start_mock.assert_called_once() + + acc.set_selected_stream_configuration(_SELECTED_STREAM_STOP_REQUEST) stop_mock.assert_called_once() From fb1ec5681252cd7c707079358e8033c2172db7c7 Mon Sep 17 00:00:00 2001 From: Naka-Masa19 Date: Wed, 25 Nov 2026 17:29:00 +0900 Subject: [PATCH 2/2] fix docs --- pyhap/camera_talkback.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pyhap/camera_talkback.py b/pyhap/camera_talkback.py index 4838f2f0..590090a8 100644 --- a/pyhap/camera_talkback.py +++ b/pyhap/camera_talkback.py @@ -140,8 +140,10 @@ def local_port(self) -> int: def start(self, audio_config: dict[str, Any]) -> None: """Start listening for incoming audio. + :param audio_config: Audio configuration, as negotiated with the HAP client. Implementations can only use part of these. Available key: + - a_codec - Audio codec """