GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add speaker talkback to smartcam Camera module by juherr · Pull Request #1784 · python-kasa/python-kasa

Conversation

@juherr

@juherr juherr commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

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.

All reactions
juherr added 4 commits October 5, 2026 10:53
@juherr
feat(smartcam): add minimal MPEG-TS writer for talkback audio
ba92107
@juherr
feat(smartcam): add talk session client for the camera media port
8c8bcf2
@juherr
feat(smartcam): add Camera.play_audio for speaker talkback
b456ac4
@juherr
refactor(smartcam): simplify talk session lifecycle and MPEG-TS writer
85386e6
github-advanced-security[bot]
github-advanced-security Bot found potential problems Oct 5, 2026
View reviewed changes
Comment thread kasa/smartcam/media.py


def _md5_hex(value: str) -> str:
return hashlib.md5(value.encode()).hexdigest() # noqa: S324
Comment thread kasa/smartcam/media.py
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()
Comment thread tests/smartcam/test_media.py
[
pytest.param(
', encrypt_type="3"',
hashlib.sha256(PASSWORD.encode()).hexdigest().upper(),
Comment thread tests/smartcam/test_media.py
),
pytest.param(
"",
hashlib.md5(PASSWORD.encode()).hexdigest().upper(), # noqa: S324
Comment thread tests/smartcam/test_media.py
),
pytest.param(
', encrypt_type="2"',
hashlib.md5(PASSWORD.encode()).hexdigest().upper(), # noqa: S324
@codecov

codecov Bot commented Oct 5, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Codecov Report

✅ 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.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

Reviewers

1 more reviewer
@github-advanced-security github-advanced-security[bot] github-advanced-security[bot] left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

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.

2 participants

@juherr @github-advanced-security
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.