Skip to content
Merged
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
5 changes: 4 additions & 1 deletion pyhap/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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(
Expand Down
17 changes: 12 additions & 5 deletions pyhap/camera_talkback.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import socket
import struct
import threading
from typing import Protocol, cast
from typing import Any, Protocol, cast

import pylibsrtp

Expand Down Expand Up @@ -138,8 +138,14 @@ 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."""
Expand Down Expand Up @@ -198,14 +204,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:
Expand Down
37 changes: 25 additions & 12 deletions tests/test_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
"qGeNmESTIojxNiAw78WkjTLtGv0waWnLo9gQEBAA=="
)

_SELECTED_STREAM_START_REQUEST = (
"ARUCAQEBEKzMbMEFY0UVjal0tFCQBpECNAEBAAIJAQEAAgEAAwEAAwsBAoAC"
"AgJoAQMBHgQXAQFjAgQr66FSAwKEAAQEAAAAPwUCYgUDLAEBAgIMAQEBAgEA"
"AwEBBAEeAxYBAW4CBMUInmQDAhgABAQAAKBABgENBAEA"
)

_SELECTED_STREAM_STOP_REQUEST = "ARUCAQABEKzMbMEFY0UVjal0tFCQBpE="

_OPTIONS = {
"stream_count": 4,
"video": {
Expand Down Expand Up @@ -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 = {
Expand All @@ -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()

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand All @@ -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")
Expand All @@ -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()


Expand Down
Loading