3578e9a332
`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.
343 lines
12 KiB
C#
343 lines
12 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using Jellyfin.Data.Enums;
|
|
using MediaBrowser.Common.Configuration;
|
|
using MediaBrowser.Controller.IO;
|
|
using MediaBrowser.Controller.Library;
|
|
using MediaBrowser.Controller.MediaEncoding;
|
|
using MediaBrowser.Controller.Streaming;
|
|
using MediaBrowser.Model.Configuration;
|
|
using MediaBrowser.Model.Dlna;
|
|
using MediaBrowser.Model.Dto;
|
|
using MediaBrowser.Model.Entities;
|
|
using MediaBrowser.Model.MediaInfo;
|
|
using Moq;
|
|
using Xunit;
|
|
|
|
using IConfiguration = Microsoft.Extensions.Configuration.IConfiguration;
|
|
|
|
namespace Jellyfin.Controller.Tests.MediaEncoding;
|
|
|
|
public class EncodingHelperTests
|
|
{
|
|
[Fact]
|
|
public void GetMapArgs_NoSubtitle_ExcludesAllSubs()
|
|
{
|
|
var state = BuildState(subtitle: null, deliveryMethod: null);
|
|
var args = CreateHelper().GetMapArgs(state);
|
|
|
|
Assert.Contains("-map -0:s", args, StringComparison.Ordinal);
|
|
Assert.DoesNotContain("-map 1:", args, StringComparison.Ordinal);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetMapArgs_InternalSrt_MapsFromPrimaryInput()
|
|
{
|
|
var sub = new MediaStream { Index = 2, Type = MediaStreamType.Subtitle, Codec = "srt" };
|
|
var state = BuildState(sub, SubtitleDeliveryMethod.Embed);
|
|
var args = CreateHelper().GetMapArgs(state);
|
|
|
|
Assert.Contains("-map 0:2", args, StringComparison.Ordinal);
|
|
Assert.DoesNotContain("-map 1:", args, StringComparison.Ordinal);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetMapArgs_InternalSubAtHigherIndex_MapsCorrectIndex()
|
|
{
|
|
var sub0 = new MediaStream { Index = 2, Type = MediaStreamType.Subtitle, Codec = "srt" };
|
|
var sub1 = new MediaStream { Index = 3, Type = MediaStreamType.Subtitle, Codec = "ass" };
|
|
var state = BuildState(sub1, SubtitleDeliveryMethod.Embed, additionalStreams: [sub0, sub1]);
|
|
var args = CreateHelper().GetMapArgs(state);
|
|
|
|
Assert.Contains("-map 0:3", args, StringComparison.Ordinal);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetMapArgs_ExternalSrt_MapsFirstStreamFromInput1()
|
|
{
|
|
var sub = new MediaStream
|
|
{
|
|
Index = 2,
|
|
Type = MediaStreamType.Subtitle,
|
|
Codec = "srt",
|
|
IsExternal = true,
|
|
SupportsExternalStream = true,
|
|
Path = "/media/movie.en.srt"
|
|
};
|
|
var state = BuildState(sub, SubtitleDeliveryMethod.Embed);
|
|
var args = CreateHelper().GetMapArgs(state);
|
|
|
|
Assert.Contains("-map 1:0", args, StringComparison.Ordinal);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetMapArgs_SecondExternalSrt_StillMaps1Colon0()
|
|
{
|
|
// Two separate .srt files — selecting the second one still maps 1:0
|
|
// because Jellyfin feeds only the selected file as ffmpeg input 1.
|
|
var ext1 = new MediaStream
|
|
{
|
|
Index = 2,
|
|
Type = MediaStreamType.Subtitle,
|
|
Codec = "srt",
|
|
IsExternal = true,
|
|
SupportsExternalStream = true,
|
|
Path = "/media/movie.en.srt"
|
|
};
|
|
var ext2 = new MediaStream
|
|
{
|
|
Index = 3,
|
|
Type = MediaStreamType.Subtitle,
|
|
Codec = "srt",
|
|
IsExternal = true,
|
|
SupportsExternalStream = true,
|
|
Path = "/media/movie.fr.srt"
|
|
};
|
|
var state = BuildState(ext2, SubtitleDeliveryMethod.Embed, additionalStreams: [ext1, ext2]);
|
|
var args = CreateHelper().GetMapArgs(state);
|
|
|
|
Assert.Contains("-map 1:0", args, StringComparison.Ordinal);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetMapArgs_MksFirstTrack_MapsInFileIndex0()
|
|
{
|
|
var mks0 = new MediaStream
|
|
{
|
|
Index = 2,
|
|
Type = MediaStreamType.Subtitle,
|
|
Codec = "subrip",
|
|
IsExternal = true,
|
|
SupportsExternalStream = true,
|
|
Path = "/media/movie.mks"
|
|
};
|
|
var mks1 = new MediaStream
|
|
{
|
|
Index = 3,
|
|
Type = MediaStreamType.Subtitle,
|
|
Codec = "ass",
|
|
IsExternal = true,
|
|
SupportsExternalStream = true,
|
|
Path = "/media/movie.mks"
|
|
};
|
|
var state = BuildState(mks0, SubtitleDeliveryMethod.Embed, additionalStreams: [mks0, mks1]);
|
|
var args = CreateHelper().GetMapArgs(state);
|
|
|
|
Assert.Contains("-map 1:0", args, StringComparison.Ordinal);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetMapArgs_MksSecondTrack_MapsInFileIndex1()
|
|
{
|
|
var mks0 = new MediaStream
|
|
{
|
|
Index = 2,
|
|
Type = MediaStreamType.Subtitle,
|
|
Codec = "subrip",
|
|
IsExternal = true,
|
|
SupportsExternalStream = true,
|
|
Path = "/media/movie.mks"
|
|
};
|
|
var mks1 = new MediaStream
|
|
{
|
|
Index = 3,
|
|
Type = MediaStreamType.Subtitle,
|
|
Codec = "ass",
|
|
IsExternal = true,
|
|
SupportsExternalStream = true,
|
|
Path = "/media/movie.mks"
|
|
};
|
|
var mks2 = new MediaStream
|
|
{
|
|
Index = 4,
|
|
Type = MediaStreamType.Subtitle,
|
|
Codec = "subrip",
|
|
IsExternal = true,
|
|
SupportsExternalStream = true,
|
|
Path = "/media/movie.mks"
|
|
};
|
|
var state = BuildState(mks1, SubtitleDeliveryMethod.Embed, additionalStreams: [mks0, mks1, mks2]);
|
|
var args = CreateHelper().GetMapArgs(state);
|
|
|
|
Assert.Contains("-map 1:1", args, StringComparison.Ordinal);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(SubtitleDeliveryMethod.Embed, true, "movie.idx")]
|
|
[InlineData(SubtitleDeliveryMethod.Encode, true, "movie.idx")]
|
|
[InlineData(SubtitleDeliveryMethod.Embed, false, "movie.sub")]
|
|
[InlineData(SubtitleDeliveryMethod.Encode, false, "movie.sub")]
|
|
public void GetInputArgument_VobSub_UsesCorrectPath(
|
|
SubtitleDeliveryMethod deliveryMethod,
|
|
bool createIdxFile,
|
|
string expectedFilename)
|
|
{
|
|
var tempDir = Directory.CreateTempSubdirectory("jellyfin-test-");
|
|
try
|
|
{
|
|
var subFile = Path.Combine(tempDir.FullName, "movie.sub");
|
|
File.WriteAllText(subFile, "dummy");
|
|
|
|
if (createIdxFile)
|
|
{
|
|
File.WriteAllText(Path.Combine(tempDir.FullName, "movie.idx"), "dummy");
|
|
}
|
|
|
|
var sub = new MediaStream
|
|
{
|
|
Index = 2,
|
|
Type = MediaStreamType.Subtitle,
|
|
Codec = "dvdsub",
|
|
IsExternal = true,
|
|
SupportsExternalStream = true,
|
|
Path = subFile
|
|
};
|
|
var state = BuildState(sub, deliveryMethod);
|
|
var inputArgs = CreateHelper().GetInputArgument(state, new EncodingOptions(), null);
|
|
|
|
Assert.Contains(expectedFilename, inputArgs, StringComparison.Ordinal);
|
|
}
|
|
finally
|
|
{
|
|
tempDir.Delete(true);
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("aac", 44100, 44100)] // non-opus: requested rate must be preserved (issue #17026)
|
|
[InlineData("aac", 48000, 48000)]
|
|
[InlineData("mp3", 22050, 22050)]
|
|
[InlineData("flac", 96000, 96000)]
|
|
[InlineData("opus", 44100, 48000)] // opus: must snap to a libopus-supported rate
|
|
[InlineData("opus", 22050, 24000)]
|
|
[InlineData("opus", 8000, 8000)]
|
|
public void GetProgressiveAudioFullCommandLine_SampleRate_OnlyClampedForOpus(
|
|
string audioCodec,
|
|
int requestedSampleRate,
|
|
int expectedSampleRate)
|
|
{
|
|
var state = BuildAudioState(audioCodec, requestedSampleRate);
|
|
var args = CreateHelper().GetProgressiveAudioFullCommandLine(state, new EncodingOptions(), "/tmp/out");
|
|
|
|
Assert.Contains("-ar " + expectedSampleRate, args, StringComparison.Ordinal);
|
|
}
|
|
|
|
[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",
|
|
MediaStreams = new List<MediaStream> { audio },
|
|
Path = "/media/track.flac",
|
|
Protocol = MediaProtocol.File,
|
|
},
|
|
AudioStream = audio,
|
|
OutputAudioCodec = audioCodec,
|
|
BaseRequest = new VideoRequestDto
|
|
{
|
|
AudioCodec = audioCodec,
|
|
AudioSampleRate = requestedSampleRate,
|
|
},
|
|
IsVideoRequest = false,
|
|
IsInputVideo = false,
|
|
};
|
|
}
|
|
|
|
private static EncodingJobInfo BuildState(
|
|
MediaStream? subtitle,
|
|
SubtitleDeliveryMethod? deliveryMethod,
|
|
MediaStream[]? additionalStreams = null)
|
|
{
|
|
var video = new MediaStream { Index = 0, Type = MediaStreamType.Video, Codec = "h264" };
|
|
var audio = new MediaStream { Index = 1, Type = MediaStreamType.Audio, Codec = "aac" };
|
|
var streams = new List<MediaStream> { video, audio };
|
|
|
|
if (additionalStreams is not null)
|
|
{
|
|
streams.AddRange(additionalStreams);
|
|
}
|
|
else if (subtitle is not null)
|
|
{
|
|
streams.Add(subtitle);
|
|
}
|
|
|
|
return new EncodingJobInfo(TranscodingJobType.Progressive)
|
|
{
|
|
MediaSource = new MediaSourceInfo
|
|
{
|
|
Container = "mkv",
|
|
MediaStreams = streams,
|
|
},
|
|
VideoStream = video,
|
|
AudioStream = audio,
|
|
SubtitleStream = subtitle,
|
|
SubtitleDeliveryMethod = deliveryMethod ?? SubtitleDeliveryMethod.Drop,
|
|
BaseRequest = new VideoRequestDto(),
|
|
IsVideoRequest = true,
|
|
IsInputVideo = true,
|
|
};
|
|
}
|
|
|
|
private static EncodingHelper CreateHelper()
|
|
{
|
|
var appPaths = Mock.Of<IApplicationPaths>();
|
|
var mediaEncoder = new Mock<IMediaEncoder>();
|
|
var subtitleEncoder = new Mock<ISubtitleEncoder>();
|
|
var config = new Mock<IConfiguration>();
|
|
var configurationManager = new Mock<IConfigurationManager>();
|
|
var pathManager = new Mock<IPathManager>();
|
|
|
|
return new EncodingHelper(
|
|
appPaths,
|
|
mediaEncoder.Object,
|
|
subtitleEncoder.Object,
|
|
config.Object,
|
|
configurationManager.Object,
|
|
pathManager.Object);
|
|
}
|
|
}
|