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:
@@ -1,7 +1,10 @@
|
||||
#pragma warning disable CA1819 // XML serialization handles collections improperly, so we need to use arrays
|
||||
|
||||
#nullable disable
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Xml.Serialization;
|
||||
using MediaBrowser.Model.Entities;
|
||||
|
||||
namespace MediaBrowser.Model.Configuration;
|
||||
@@ -234,8 +237,27 @@ public class EncodingOptions
|
||||
/// <summary>
|
||||
/// Gets or sets the encoder preset.
|
||||
/// </summary>
|
||||
[XmlIgnore]
|
||||
public EncoderPreset EncoderPreset { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the encoder preset as it is stored in the configuration file. Files written before the preset
|
||||
/// list was validated can carry an empty element, which has to read back as the default rather than throw and
|
||||
/// take the rest of the configuration down with it.
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
[XmlElement(nameof(EncoderPreset))]
|
||||
public string EncoderPresetXml
|
||||
{
|
||||
get => EncoderPreset.ToString();
|
||||
|
||||
// TryParse also accepts a number, which would yield a preset value nothing maps to, so the result has to be
|
||||
// a declared one.
|
||||
set => EncoderPreset = Enum.TryParse<EncoderPreset>(value, true, out var encoderPreset) && Enum.IsDefined(encoderPreset)
|
||||
? encoderPreset
|
||||
: EncoderPreset.auto;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether the framerate is doubled when deinterlacing.
|
||||
/// </summary>
|
||||
|
||||
@@ -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