Skip to content

homekit: two-way audio for cameras with Opus - #2551

Open
Djoko-cli wants to merge 2 commits into
AlexxIT:masterfrom
Djoko-cli:pr/homekit-backchannel
Open

Djoko-cli wants to merge 2 commits into
AlexxIT:masterfrom
Djoko-cli:pr/homekit-backchannel

Conversation

@Djoko-cli

Copy link
Copy Markdown

Adds two-way audio to the HomeKit client (homekit:// source), only for cameras that support Opus. The speaker of such a camera can be fed by any go2rtc consumer that sends Opus: RTSP ?backchannel=1, WebRTC, or stream to camera.

The first commit is a separate, pre-existing fix that two-way audio needs: audio-only consumers did not work. trackToVideo() picked a resolution only when a consumer asked for video. So a stream started by rtsp://…?audio (or by a consumer that only sends audio) asked the camera for 0×0 video at 0 fps. A Circle View then sends nothing at all and returns no error, so the producer stops after the 5 s deadline and reconnects in a loop. The resolution is now picked in every case, with the same rules as before.

Scope, given the earlier discussions

  • Opus cameras only. In Adds two way audio and support for Abode IOTA via HomeKit #1429 you noted that most cameras don't support Opus. Here the sendonly media is added only when the camera lists Opus in SupportedAudioStreamConfiguration. For other cameras GetMedias() is unchanged, so streams never matches or calls AddTrack for them, and stream to camera fails with the same error as before.
  • No changes to core, streams or ffmpeg, and no custom ffmpeg (see Add bidirectional audio support for HomeKit cameras #1981). There is no transcoding: the consumer's Opus packets are sent as they are, with a rewritten RTP header. (opus.RepackToHAP is not reused: it assumes 20 ms packets at 16 kHz, while the clock rate here is whatever the camera selected.)
  • #backchannel=0 disables it. Unlike the RTSP source, which keeps the backchannel only with #backchannel=1 once any # parameter is present, here only =0 disables it, so #bitrate=... alone keeps two-way audio.

How it works

  • GetMedias() adds a sendonly OPUS/48000/2 media when the camera supports Opus and Backchannel is set. streamHandler sets it by default.
  • Start():
    • applies the payload type and RTCP interval that go2rtc selected in SelectedRTPStreamConfiguration to the audio session, before AddSession;
    • uses the session for sending only if it carries Opus and was actually added to the SRTP server;
    • also starts a stream whose only consumer sends audio: it requests Opus from the camera, and the camera video keeps the stream alive. This relies on the first commit.
  • AddTrack() uses one core.Sender for all consumers, the same pattern as the Tapo backchannel. Its handler gives each packet:
    • its own continuous RTP sequence;
    • timestamps rescaled from 48 kHz to the camera clock rate, continuous across talk spurts (gap > 500 ms), with the marker bit on the first packet of a spurt;
    • the SSRC, payload type and SRTP keys of the stream's audio session.
  • srtp.Session gets a mutex, because the local pion Context has no concurrency protection. It is used by WriteRTP (sender goroutine) and by WriteRTCP (server goroutine, receiver reports). AddSession takes it while it sets up the session. A write before AddSession is ignored instead of panicking on a nil conn.

Tests

  • pkg/homekit/backchannel_test.go: a fake camera endpoint decrypts with the controller key. It checks:
    • payload type, SSRC and payload;
    • a continuous sequence across talk spurts, timestamp rescaling and continuity, and marker bits;
    • nothing is sent after Stop();
    • a single sender for two consumers;
    • non-Opus sessions and sessions that were not added are ignored.
  • pkg/srtp/session_test.go: no panic before AddSession, and concurrent WriteRTP and WriteRTCP under -race.
  • go test -race ./pkg/homekit/ ./pkg/srtp/ passes.
  • Hardware: two Logitech Circle View cameras (Opus, 8 kHz), in production since 2026-10-07 on a build of this code without the #backchannel option and without the start with only an audio-sending consumer. Two paths play correctly on the camera speakers:
    • speech from the Apple Home app, through Scrypted. I wrote a small Scrypted plugin that implements Intercom by sending the iPhone audio to rtsp://…?backchannel=1;
    • stream to camera (ffmpeg:file#audio=opus).
  • Idle stream, test build of this PR on master + homekit: fix audio timestamps of Opus cameras #2550 (timekeeper fix, independent of this PR):
    • stream to camera of a 20 s file: 1,033 packets were sent to each camera, with 0 drops;
    • an audio-only RTSP consumer received 300 packets, with no reconnect loop. Before the first commit, the same request looped every ~5.5 s.

Limitations

  • Several consumers on one camera. Consumers share a single sender, as with Tapo. When one disconnects, the sender is detached from the consumer that remains (core.Node has a single parent). If two consumers talk at the same time, their packets are interleaved.
  • The camera audio must be Opus. If a consumer requests the camera audio in another codec, the session is not Opus and two-way audio stays silent.
  • WebRTC from the browser was not tested. It sends Opus 48 kHz, which matches the offered media.

🤖 Generated with Claude Code

Djoko-cli added a commit to Djoko-cli/haptool that referenced this pull request Oct 7, 2026
…r, horodatage)

La conclusion « piste matérielle » pour le micro était inexacte : firmware
figé, rétabli par un redémarrage électrique. Audio vers le haut-parleur et
horodatage audio corrigés dans go2rtc (AlexxIT/go2rtc#2551 et #2550).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AlexxIT AlexxIT self-assigned this Oct 8, 2026
@AlexxIT AlexxIT added kind/feature Proposes or implements a new capability or enhancement area/homekit HomeKit camera pairing, streaming, and accessory support ai Submission contains text or code generated by AI labels Oct 8, 2026
Djoko-cli added a commit to Djoko-cli/haptool that referenced this pull request Oct 12, 2026
…r, horodatage)

La conclusion « piste matérielle » pour le micro était inexacte : firmware
figé, rétabli par un redémarrage électrique. Audio vers le haut-parleur et
horodatage audio corrigés dans go2rtc (AlexxIT/go2rtc#2551 et #2550).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AlexxIT AlexxIT added the size/XL PR with 500-999 lines changed, excluding generated files. label Nov 26, 2026
Djoko-cli and others added 2 commits November 28, 2026 12:11
trackToVideo() selected the resolution only when a consumer requested
the video track. A stream started by an audio only consumer (e.g.
rtsp://...?audio) sent SelectedRTPStreamConfiguration with a 0x0, 0 fps
video, and a Logitech Circle View then sends nothing at all, without
any error: the producer stops after the 5 s deadline and reconnects in
a loop.

The resolution is now selected in every case, with the same rules
(largest one within maxwidth/maxheight).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The HomeKit producer now offers a sendonly OPUS/48000/2 media when the
camera lists Opus in its SupportedAudioStreamConfiguration. For other
cameras GetMedias() is unchanged. Consumer audio is sent to the camera
speaker over the audio SRTP session of the stream:

- the payload type and RTCP interval selected by go2rtc in
  SelectedRTPStreamConfiguration are applied to the audio session
  before AddSession;
- the session is used only if it carries Opus and was actually added
  to the SRTP server;
- a stream with only a two-way audio consumer starts too: Opus is
  requested from the camera and the camera video keeps it alive;
- one sender for all consumers keeps the RTP sequence continuous; RTP
  timestamps are rescaled from 48 kHz to the camera clock rate and stay
  continuous across talk spurts (gap > 500 ms), with the marker bit on
  the first packet of a spurt.

srtp.Session gets a mutex: the local pion Context has no concurrency
protection and is used by WriteRTP (sender goroutine) and WriteRTCP
(server goroutine, receiver reports). AddSession takes it while it sets
up the session, and writes before AddSession are ignored instead of
panicking on a nil conn.

Two-way audio is on by default and `#backchannel=0` disables it.

Tested with two Logitech Circle View (Opus, 8 kHz).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Djoko-cli
Djoko-cli force-pushed the pr/homekit-backchannel branch from 0072b91 to 1d0b29d Compare November 28, 2026 08:13

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai Submission contains text or code generated by AI area/homekit HomeKit camera pairing, streaming, and accessory support kind/feature Proposes or implements a new capability or enhancement size/XL PR with 500-999 lines changed, excluding generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants