mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-05-31 20:24:21 -04:00
Clean up EnumFlags serialization
This commit is contained in:
parent
9ebd521754
commit
c331e11c24
@ -224,7 +224,7 @@ namespace Jellyfin.Api.Controllers
|
|||||||
DeInterlace = false,
|
DeInterlace = false,
|
||||||
RequireNonAnamorphic = false,
|
RequireNonAnamorphic = false,
|
||||||
EnableMpegtsM2TsMode = false,
|
EnableMpegtsM2TsMode = false,
|
||||||
TranscodeReasons = mediaSource.TranscodeReasons == MediaBrowser.Model.Session.TranscodeReason.None ? null : mediaSource.TranscodeReasons.Serialize(),
|
TranscodeReasons = mediaSource.TranscodeReasons == TranscodeReason.None ? null : mediaSource.TranscodeReasons.ToString(),
|
||||||
Context = EncodingContext.Static,
|
Context = EncodingContext.Static,
|
||||||
StreamOptions = new Dictionary<string, string>(),
|
StreamOptions = new Dictionary<string, string>(),
|
||||||
EnableAdaptiveBitrateStreaming = true
|
EnableAdaptiveBitrateStreaming = true
|
||||||
@ -255,7 +255,7 @@ namespace Jellyfin.Api.Controllers
|
|||||||
CopyTimestamps = true,
|
CopyTimestamps = true,
|
||||||
StartTimeTicks = startTimeTicks,
|
StartTimeTicks = startTimeTicks,
|
||||||
SubtitleMethod = SubtitleDeliveryMethod.Embed,
|
SubtitleMethod = SubtitleDeliveryMethod.Embed,
|
||||||
TranscodeReasons = mediaSource.TranscodeReasons == MediaBrowser.Model.Session.TranscodeReason.None ? null : mediaSource.TranscodeReasons.Serialize(),
|
TranscodeReasons = mediaSource.TranscodeReasons == TranscodeReason.None ? null : mediaSource.TranscodeReasons.ToString(),
|
||||||
Context = EncodingContext.Static
|
Context = EncodingContext.Static
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -35,9 +35,6 @@ namespace MediaBrowser.Controller.MediaEncoding
|
|||||||
SupportedSubtitleCodecs = Array.Empty<string>();
|
SupportedSubtitleCodecs = Array.Empty<string>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public TranscodeReason[] TranscodeReasons => TranscodeReason.ToArray();
|
|
||||||
|
|
||||||
[JsonIgnore]
|
|
||||||
public TranscodeReason TranscodeReason
|
public TranscodeReason TranscodeReason
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
@ -915,13 +915,13 @@ namespace MediaBrowser.Model.Dlna
|
|||||||
}
|
}
|
||||||
|
|
||||||
_logger.LogInformation(
|
_logger.LogInformation(
|
||||||
"Transcode Result for Profile: {0}, Path: {1}, PlayMethod: {2}, AudioStreamIndex: {3}, SubtitleStreamIndex: {4}, Reasons: {5}",
|
"Transcode Result for Profile: {Profile}, Path: {Path}, PlayMethod: {PlayMethod}, AudioStreamIndex: {AudioStreamIndex}, SubtitleStreamIndex: {SubtitleStreamIndex}, Reasons: {TranscodeReason}",
|
||||||
options.Profile.Name ?? "Anonymous Profile",
|
options.Profile?.Name ?? "Anonymous Profile",
|
||||||
item.Path ?? "Unknown path",
|
item.Path ?? "Unknown path",
|
||||||
playlistItem.PlayMethod,
|
playlistItem?.PlayMethod,
|
||||||
audioStream.Index,
|
audioStream?.Index,
|
||||||
playlistItem.SubtitleStreamIndex,
|
playlistItem?.SubtitleStreamIndex,
|
||||||
playlistItem.TranscodeReasons);
|
playlistItem?.TranscodeReasons);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int GetDefaultAudioBitrate(string audioCodec, int? audioChannels)
|
private static int GetDefaultAudioBitrate(string audioCodec, int? audioChannels)
|
||||||
|
@ -798,7 +798,7 @@ namespace MediaBrowser.Model.Dlna
|
|||||||
|
|
||||||
if (!item.IsDirectStream)
|
if (!item.IsDirectStream)
|
||||||
{
|
{
|
||||||
list.Add(new NameValuePair("TranscodeReasons", item.TranscodeReasons.Serialize()));
|
list.Add(new NameValuePair("TranscodeReasons", item.TranscodeReasons.ToString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
|
@ -1,34 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace MediaBrowser.Model.Session
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Extension methods for serializing TranscodeReason.
|
|
||||||
/// </summary>
|
|
||||||
public static class TranscodeReasonExtensions
|
|
||||||
{
|
|
||||||
private static readonly TranscodeReason[] _values = Enum.GetValues<TranscodeReason>();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Serializes a TranscodeReason into a delimiter-separated string.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="reasons">The <see cref="TranscodeReason"/> enumeration.</param>
|
|
||||||
/// <param name="sep">The string separator to use. defualt <c>,</c>.</param>
|
|
||||||
/// <returns>string of transcode reasons delimited.</returns>
|
|
||||||
public static string Serialize(this TranscodeReason reasons, string sep = ",")
|
|
||||||
{
|
|
||||||
return string.Join(sep, reasons.ToArray());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Serializes a TranscodeReason into an array of individual TranscodeReason bits.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="reasons">The <see cref="TranscodeReason"/> enumeration.</param>
|
|
||||||
/// <returns>Array of <c>TranscodeReason</c>.</returns>
|
|
||||||
public static TranscodeReason[] ToArray(this TranscodeReason reasons)
|
|
||||||
{
|
|
||||||
return _values.Where(r => r != 0 && reasons.HasFlag(r)).ToArray();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,8 +1,6 @@
|
|||||||
#nullable disable
|
#nullable disable
|
||||||
#pragma warning disable CS1591
|
#pragma warning disable CS1591
|
||||||
|
|
||||||
using System.Text.Json.Serialization;
|
|
||||||
|
|
||||||
namespace MediaBrowser.Model.Session
|
namespace MediaBrowser.Model.Session
|
||||||
{
|
{
|
||||||
public class TranscodingInfo
|
public class TranscodingInfo
|
||||||
@ -31,9 +29,6 @@ namespace MediaBrowser.Model.Session
|
|||||||
|
|
||||||
public HardwareEncodingType? HardwareAccelerationType { get; set; }
|
public HardwareEncodingType? HardwareAccelerationType { get; set; }
|
||||||
|
|
||||||
public TranscodeReason[] TranscodeReasons => TranscodeReason.ToArray();
|
|
||||||
|
|
||||||
[JsonIgnore]
|
|
||||||
public TranscodeReason TranscodeReason { get; set; }
|
public TranscodeReason TranscodeReason { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,36 @@
|
|||||||
|
using System;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace Jellyfin.Extensions.Json.Converters;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Enum flag to json array converter.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of enum.</typeparam>
|
||||||
|
public class JsonFlagEnumConverter<T> : JsonConverter<T>
|
||||||
|
where T : Enum
|
||||||
|
{
|
||||||
|
private static readonly T[] _enumValues = (T[])Enum.GetValues(typeof(T));
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
writer.WriteStartArray();
|
||||||
|
foreach (var enumValue in _enumValues)
|
||||||
|
{
|
||||||
|
if (value.HasFlag(enumValue))
|
||||||
|
{
|
||||||
|
writer.WriteStringValue(enumValue.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.WriteEndArray();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
using System;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace Jellyfin.Extensions.Json.Converters;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Json flag enum converter factory.
|
||||||
|
/// </summary>
|
||||||
|
public class JsonFlagEnumConverterFactory : JsonConverterFactory
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override bool CanConvert(Type typeToConvert)
|
||||||
|
{
|
||||||
|
return typeToConvert.IsEnum && typeToConvert.IsDefined(typeof(FlagsAttribute));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
return (JsonConverter?)Activator.CreateInstance(typeof(JsonFlagEnumConverter<>).MakeGenericType(typeToConvert));
|
||||||
|
}
|
||||||
|
}
|
@ -36,6 +36,7 @@ namespace Jellyfin.Extensions.Json
|
|||||||
new JsonGuidConverter(),
|
new JsonGuidConverter(),
|
||||||
new JsonNullableGuidConverter(),
|
new JsonNullableGuidConverter(),
|
||||||
new JsonVersionConverter(),
|
new JsonVersionConverter(),
|
||||||
|
new JsonFlagEnumConverterFactory(),
|
||||||
new JsonStringEnumConverter(),
|
new JsonStringEnumConverter(),
|
||||||
new JsonNullableStructConverterFactory(),
|
new JsonNullableStructConverterFactory(),
|
||||||
new JsonBoolNumberConverter(),
|
new JsonBoolNumberConverter(),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user