Fix media_player.play_media by using go2rtc /api/ffmpeg#942
Open
mayerwin wants to merge 1 commit intoAlexxIT:masterfrom
Open
Fix media_player.play_media by using go2rtc /api/ffmpeg#942mayerwin wants to merge 1 commit intoAlexxIT:masterfrom
mayerwin wants to merge 1 commit intoAlexxIT:masterfrom
Conversation
The existing implementation POSTs to /api/streams with a hardcoded audio
codec from the user's `audio:` config:
src = f"ffmpeg:{media_id}#audio={self.audio}#input=file"
When the codec doesn't match what the camera's backchannel advertises
(e.g. `audio: pcma` in config vs PCMU/8000 on a Reolink), go2rtc's
stream.Play falls through matchMedia and returns HTTP 500
"can't find consumer" — a misleading error whose actual meaning is
"no codec match between producer offer and backchannel consumer".
/api/ffmpeg is a go2rtc helper that wraps the same stream.Play path
but forces `audio=auto` on the ffmpeg producer, so the producer
advertises the full codec menu (Opus 48k, L16/PCMA/PCMU at 16k then 8k,
AAC) and matchMedia picks whichever the camera actually supports. For
the WebRTC integration's async_play_media use case, /api/ffmpeg is a
strict superset of /api/streams.
`audio:` is now Optional (kept for backwards compat with existing
configs, but unused). The src-fragment string and its URL-encoding
pitfalls are gone.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
media_player.play_mediaonplatform: webrtcfails with HTTP 500"can't find consumer"on many real setups (reported across multiple issues by Reolink / Amcrest / Dahua users trying to use the entity for TTS or alarm playback).Root cause: the integration sends
so the ffmpeg producer advertises one hardcoded codec (whatever the user put in
audio:). If the camera's backchannel does not accept that exact codec — e.g. the user configuresaudio: pcmabut the Reolink backchannel negotiatesPCMU/8000— go2rtc'smatchMediareturns false andstream.Playreturns"can't find consumer"at play.go#L108. The error message is misleading; its actual meaning is "no codec match between producer offer and backchannel consumer."Fix
Switch
async_play_mediato go2rtc's/api/ffmpeghelper. Per ffmpeg/api.go#L27, this endpoint hardcodes#audio=autoon the ffmpeg producer, so the producer advertises the full codec menu (Opus 48k, L16/PCMA/PCMU at 16k then 8k, AAC) and go2rtc picks whichever the camera's backchannel actually supports.Both endpoints ultimately call the same
stream.Play(src)—/api/ffmpegjust buildssrcwithaudio=autoinstead of passing through whatever the caller supplied, making it a strict superset forasync_play_media's use case.audio:config option is nowOptional(kept for backwards compat so existing YAML still validates) but unused — codec selection is entirely auto-negotiated by go2rtc. The old#-fragment source string is gone, which incidentally removes some subtle URL-encoding risk forfile=values that contain query params.Diff summary
custom_components/webrtc/media_player.py: schemaaudioRequired → Optional;async_play_mediausesutils.api_ffmpegwithparams={dst, file}.custom_components/webrtc/utils.py:api_ffmpeg()helper mirroringapi_streams().Total: +15 / -10 lines.
Verified on
HTTP 500 "can't find consumer"on every codec value tried (pcma,pcma/8000,pcmu,pcmu/8000,opus,opus/48000/2,aac). After:media_player.play_mediawith TTS (Piper via HA media_source) and with a plain mp3 URL both succeed within ~5s, audio plays at the camera speaker.producers/consumersstate after a successful/api/ffmpegcall confirmed the newly-added consumer advertisesaudio, sendonly, PCMU/8000— i.e. the camera's actual preferred codec, not the pcma we'd been forcing.Compatibility
Existing configs with
audio: pcma(or similar) continue to validate and work — the field is now simply ignored. No user-facing breaking change.