diff options
| author | vdatanet <joan@vdata.net> | 2026-08-04 19:52:29 +0200 |
|---|---|---|
| committer | vdatanet <joan@vdata.net> | 2026-08-04 19:52:29 +0200 |
| commit | 3578e9a332c64d1aeef6be5e4f2c1a8d271169a8 (patch) | |
| tree | 1ccb6d967e3be04c4c0b42c366af3ec971ff211b /MediaBrowser.Controller/MediaEncoding | |
| parent | 7fbc1ff8c0192b608a493324f2b7eafcf0a4383e (diff) | |
Fix PCM audio transcoding to wav returning HTTP 500 and headerless output
`GetProgressiveAudioFullCommandLine` forced the raw PCM muxer and a bogus
sample rate whenever the audio encoder was `pcm_*`, regardless of the
container the client asked for. Two separate failures came out of it:
- `-ar ` + `state.BaseRequest.AudioBitRate` used a *bitrate* as a *sample
rate*, and `AudioBitRate` is optional. When it is absent the argument
degrades to a bare `-ar`, ffmpeg aborts with `Expected number for ar but
found: -ar` / `Error opening output files: Invalid argument` (exit 234)
and the request fails with HTTP 500. Every `GET /Audio/{id}/stream.wav`
that does not carry an explicit `AudioBitRate` hits this.
The sample rate was already being set correctly a few lines below from
`OutputAudioSampleRate`, so the line is dropped rather than repaired.
- `-f s16le` overrode the muxer even for a real container. A request that
did supply a bitrate (`/Audio/{id}/universal` passes
`MaxStreamingBitrate`) survived the first bug but produced raw headerless
samples served under an `audio/wav` content type, so clients saw a body
with no RIFF header. The raw muxer is now only forced when the requested
container is actually raw PCM, which keeps the I2S/MCU route from #10321
working.
Also drop the `containerInternal = ".pcm"` assignment in
`StreamingHelpers.GetStreamingState`: it is written after
`state.OutputContainer` has already been read from the same variable and is
never read again, so it has no effect and only obscures where the output
container comes from.
Verified against ffmpeg 8.1.2 with a 96 kHz FLAC source: before, the wav
command line exits 234; after, it produces a valid `RIFF/WAVE` 48 kHz stereo
`pcm_s16le` file, while the raw `.pcm` route still yields exactly
2 s x 48000 x 2ch x 2 B = 384000 bytes of headerless samples.
Diffstat (limited to 'MediaBrowser.Controller/MediaEncoding')
| -rw-r--r-- | MediaBrowser.Controller/MediaEncoding/EncodingHelper.cs | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/MediaBrowser.Controller/MediaEncoding/EncodingHelper.cs b/MediaBrowser.Controller/MediaEncoding/EncodingHelper.cs index 1b0bbe9ea0..9a68889352 100644 --- a/MediaBrowser.Controller/MediaEncoding/EncodingHelper.cs +++ b/MediaBrowser.Controller/MediaEncoding/EncodingHelper.cs @@ -7864,10 +7864,16 @@ namespace MediaBrowser.Controller.MediaEncoding audioTranscodeParams.Add("-acodec " + GetAudioEncoder(state)); } - if (GetAudioEncoder(state).StartsWith("pcm_", StringComparison.Ordinal)) - { - audioTranscodeParams.Add(string.Concat("-f ", GetAudioEncoder(state).AsSpan(4))); - audioTranscodeParams.Add("-ar " + state.BaseRequest.AudioBitRate); + // The pcm_* encoders emit raw samples that carry no header of their own, so the header + // has to come from the muxer. Only force the matching raw muxer when the client actually + // asked for a raw container (added in #10321 for I2S/MCU clients): applying it to every + // pcm_* codec also strips the RIFF header from a `stream.wav` request, which then serves + // headerless PCM behind an audio/wav content type. + var audioEncoder = GetAudioEncoder(state); + if (audioEncoder.StartsWith("pcm_", StringComparison.Ordinal) + && string.Equals(state.OutputContainer, "pcm", StringComparison.OrdinalIgnoreCase)) + { + audioTranscodeParams.Add(string.Concat("-f ", audioEncoder.AsSpan(4))); } var sampleRate = state.OutputAudioSampleRate; |
