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.
This commit is contained in:
vdatanet
2026-08-04 19:52:29 +02:00
parent 7fbc1ff8c0
commit 3578e9a332
3 changed files with 49 additions and 9 deletions
@@ -223,12 +223,51 @@ public class EncodingHelperTests
Assert.Contains("-ar " + expectedSampleRate, args, StringComparison.Ordinal);
}
private static EncodingJobInfo BuildAudioState(string audioCodec, int requestedSampleRate)
[Theory]
[InlineData("wav")]
[InlineData("flac")]
[InlineData("mp3")]
public void GetProgressiveAudioFullCommandLine_PcmInRealContainer_KeepsContainerMuxer(string outputContainer)
{
// A pcm_* encoder must not drag the raw muxer into a container that writes its own header,
// or the client gets headerless PCM behind the container's content type.
var state = BuildAudioState("pcm_s16le", 48000, outputContainer);
var args = CreateHelper().GetProgressiveAudioFullCommandLine(state, new EncodingOptions(), "/tmp/out");
Assert.DoesNotContain("-f s16le", args, StringComparison.Ordinal);
}
[Fact]
public void GetProgressiveAudioFullCommandLine_PcmInPcmContainer_ForcesRawMuxer()
{
// The raw-PCM route added in #10321 for I2S/MCU clients must keep working.
var state = BuildAudioState("pcm_s16le", 48000, "pcm");
var args = CreateHelper().GetProgressiveAudioFullCommandLine(state, new EncodingOptions(), "/tmp/out");
Assert.Contains("-f s16le", args, StringComparison.Ordinal);
}
[Fact]
public void GetProgressiveAudioFullCommandLine_PcmWithoutBitrate_EmitsNoEmptySampleRate()
{
// AudioBitRate is optional; it used to be emitted as `-ar <null>`, producing a bare `-ar`
// that made ffmpeg abort with "Expected number for ar" and the request fail with HTTP 500.
var state = BuildAudioState("pcm_s16le", 48000, "wav");
state.BaseRequest.AudioBitRate = null;
var args = CreateHelper().GetProgressiveAudioFullCommandLine(state, new EncodingOptions(), "/tmp/out");
Assert.DoesNotContain("-ar -", args, StringComparison.Ordinal);
Assert.DoesNotContain("-ar ", args, StringComparison.Ordinal);
Assert.Contains("-ar 48000", args, StringComparison.Ordinal);
}
private static EncodingJobInfo BuildAudioState(string audioCodec, int requestedSampleRate, string? outputContainer = null)
{
var audio = new MediaStream { Index = 0, Type = MediaStreamType.Audio, Codec = "flac", SampleRate = 96000 };
return new EncodingJobInfo(TranscodingJobType.Progressive)
{
OutputContainer = outputContainer,
MediaSource = new MediaSourceInfo
{
Container = "flac",