fix(config): read an unusable encoding.xml EncoderPreset as the default
An element the enum cannot parse throws, and the configuration manager catches that and returns defaults, so one bad preset discarded every other encoding setting. - serialize EncoderPreset through a string surrogate in XML - fall back to auto for an empty, unknown or out-of-range value
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
using System.IO;
|
||||
using System.Xml.Serialization;
|
||||
using MediaBrowser.Model.Configuration;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using Xunit;
|
||||
|
||||
namespace Jellyfin.Model.Tests.Configuration;
|
||||
|
||||
public class EncodingOptionsTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("<EncoderPreset></EncoderPreset>")]
|
||||
[InlineData("<EncoderPreset />")]
|
||||
[InlineData("<EncoderPreset> </EncoderPreset>")]
|
||||
[InlineData("<EncoderPreset>notapreset</EncoderPreset>")]
|
||||
[InlineData("<EncoderPreset>42</EncoderPreset>")]
|
||||
[InlineData("<EncoderPreset>-1</EncoderPreset>")]
|
||||
public void Deserialize_UnreadableEncoderPreset_FallsBackToDefault(string encoderPresetElement)
|
||||
{
|
||||
var options = Deserialize($"<EncodingOptions>{encoderPresetElement}<H264Crf>21</H264Crf></EncodingOptions>");
|
||||
|
||||
Assert.Equal(EncoderPreset.auto, options.EncoderPreset);
|
||||
|
||||
// The rest of the file has to survive: a throwing preset used to discard every other encoding setting.
|
||||
Assert.Equal(21, options.H264Crf);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Deserialize_KnownEncoderPreset_IsKept()
|
||||
{
|
||||
var options = Deserialize("<EncodingOptions><EncoderPreset>veryfast</EncoderPreset></EncodingOptions>");
|
||||
|
||||
Assert.Equal(EncoderPreset.veryfast, options.EncoderPreset);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Serialize_WritesTheEncoderPresetElement()
|
||||
{
|
||||
var serializer = new XmlSerializer(typeof(EncodingOptions));
|
||||
using var writer = new StringWriter();
|
||||
serializer.Serialize(writer, new EncodingOptions { EncoderPreset = EncoderPreset.slow });
|
||||
|
||||
Assert.Contains("<EncoderPreset>slow</EncoderPreset>", writer.ToString(), System.StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
private static EncodingOptions Deserialize(string xml)
|
||||
{
|
||||
var serializer = new XmlSerializer(typeof(EncodingOptions));
|
||||
using var reader = new StringReader(xml);
|
||||
return (EncodingOptions)serializer.Deserialize(reader)!;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user