GeezerVison is showing github.com — Add speaker talkback to smartcam Camera module by juherr · Pull Request #1784 · python-kasa/python-kasa
Conversation
Adds speaker-only talkback for Tapo cameras, the first step proposed in #1763.
camera = dev.modules[Module.Camera]
await camera.play_audio(audio_stream) # AsyncIterable[bytes]
audio_stream must already be encoded as G.711 A-law (PCMA), 8 kHz, mono, raw bytes (8000 bytes = 1 s).
python-kasa does no decoding, resampling or transcoding; the caller (e.g. Home Assistant with FFmpeg)
produces the camera-compatible audio. There are no new dependencies.
The call returns when the input is exhausted and its playback time has elapsed. Cancelling the task stops
playback and closes the session. Only one playback per camera can be active; a second call raises
KasaException. The camera never acknowledges playback, so returning does not guarantee the audio was heard.
Implementation
The camera media server is a second connection, separate from the management API, so it is not a
BaseTransport:
kasa/smartcam/media.py: _TalkSession
- opens TCP 8800 and sends
POST /stream
- HTTP Digest auth: user
admin, password = SHA-256 or MD5 (uppercase hex) of the cloud password, chosen
from the challenge's encrypt_type
- opens a
talk session (mode: aec)
- sends multipart
audio/mp2t parts with X-If-Encrypt: 0 and X-Session-Id
kasa/smartcam/_mpegts.py: the minimal MPEG-TS writer needed here (PAT, PMT, PES with PTS; stream type
0x90, PID 0x100)
Camera.play_audio: lives next to stream_rtsp_url and onvif_url and reuses the module's credential
handling. It rejects hub children, devices without the audio component, and missing credentials
(AuthenticationError).
Notes on the sending side:
- Pacing is mandatory: audio sent faster than real time is not played. Audio goes out in 20 ms frames on an
absolute monotonic schedule, independent of the caller's chunk sizes. The schedule restarts instead of
bursting if the producer stalls.
- Writes are awaited with
drain() and there is no internal queue, so the producer is consumed at playback
rate.
- On cancellation or error the connection is aborted; after a normal end it is closed gracefully.
The protocol follows go2rtc's pkg/tapo and was
checked against a C200. Microphone/preview, codec negotiation and other codecs are out of scope.
Tests
Tests are deterministic, with no sockets and a fake clock. They cover:
- the MPEG-TS writer: byte layout, CRC, continuity counters, adaptation-field stuffing
- Digest auth: MD5 and SHA-256, unsupported challenges, auth failure
- talk-session setup and error responses
- the exact multipart wire format
- pacing for chunk sizes from 1 to 16384 B, producer stalls, backpressure
- cancellation during pacing and during writes, producer errors, connection reset, timeouts
- the
Camera guards and concurrency
The generated stream was also checked with ffprobe.
Hardware validation
Tested on a Tapo C200 (HW 1.0, FW 1.3.17 Build 260112) over Wi-Fi (RSSI -55 dBm), client wired.
Audio was raw PCMA 8 kHz mono, generated tones and a macOS TTS sample converted with
ffmpeg -ar 8000 -ac 1 -f alaw.
| Scenario |
Result |
| 2 s tone |
Heard |
| 5 consecutive playbacks |
All heard |
| Short beeps, 5 sessions |
All beeps heard, no start truncation |
| ~10 s speech (TTS) |
Intelligible and complete |
| Chunk sizes 160 / 1024 / 4096 / 16384 B |
Same output, same timing |
| Back-to-back playbacks |
Both heard |
| Cancel after 2 s, then new playback |
Stops promptly, next playback starts normally |
| 60 s tone |
Plays to the end, no timing drift (60.0 s audio in 60.5 s incl. setup) |
| 60 s tone during Tapo app live view |
Works alongside the video stream; audio is also heard in the app |
Observations:
- Closing the connection frees the talk session. Opening a new session takes ~0.4 s, or ~0.85 s right after a
previous one ended.
- Sender timing is exact: parts leave every 19–21 ms, writes never block, nothing is queued.
- Known limitation: light, occasional choppiness, much worse while live view is active. It correlates with
camera-side TCP latency spikes (median RTT ~10 ms, peaks ~330–380 ms, no retransmissions; ~4 spikes per
10 s idle, ~9 with live view).
- Sending ahead of real time did not help. In a blind A/B with a 600 ms lead it sounded worse (the initial
burst caused retransmissions), so strict real-time pacing is kept.
@rytilahti I haven't tested on a TC65 yet. If you can try it with yours, that would help; a small test script
is available on request.
Part of #1763.
Development of this PR was driven by Claude AI. I reviewed the changes and validated them on the hardware above.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
|
|
|
|
|
|
def _md5_hex(value: str) -> str:
|
|
|
return hashlib.md5(value.encode()).hexdigest() # noqa: S324
|
|
|
def _digest_password(cloud_password: str, encrypt_type: str | None) -> str:
|
|
|
"""Return the camera media password derived from the cloud password."""
|
|
|
hash_func = hashlib.sha256 if encrypt_type == "3" else hashlib.md5
|
|
|
return hash_func(cloud_password.encode()).hexdigest().upper()
|
|
|
[
|
|
|
pytest.param(
|
|
|
', encrypt_type="3"',
|
|
|
hashlib.sha256(PASSWORD.encode()).hexdigest().upper(),
|
|
|
),
|
|
|
pytest.param(
|
|
|
"",
|
|
|
hashlib.md5(PASSWORD.encode()).hexdigest().upper(), # noqa: S324
|
|
|
),
|
|
|
pytest.param(
|
|
|
', encrypt_type="2"',
|
|
|
hashlib.md5(PASSWORD.encode()).hexdigest().upper(), # noqa: S324
|
codecov
Bot
commented
Oct 5, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.58%. Comparing base (ff26187) to head (85386e6).
Additional details and impacted files
@@ Coverage Diff @@
## master #1784 +/- ##
==========================================
+ Coverage 93.41% 93.58% +0.17%
==========================================
Files 159 161 +2
Lines 10113 10390 +277
Branches 1043 1076 +33
==========================================
+ Hits 9447 9724 +277
Misses 472 472
Partials 194 194
☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.
🚀 New features to boost your workflow:
- ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
1 more reviewer
Reviewers whose approvals may not affect merge requirements
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|