Adds the playback delivery decision service

This commit is contained in:
BinaryAlley committed 2026-10-15 09:19:36 +03:00
1 parent f97c5bed47
commit 7dc5953e1d
18 files changed
+952

No files matched your search

@@ -0,0 +1,16 @@
#region ========================================================================= USING =====================================================================================
using System.Diagnostics;
#endregion
namespace Lumina.Application.Common.DTO.MediaLibrary.AudioLibrary.MusicLibrary.Playback;
/// <summary>
/// Data transfer object for a single container and codec combination that a player advertises it can demux and decode directly.
/// </summary>
/// <param name="Container">The container the player can demux, for example MP3, FLAC, OGG, WAV or MP4.</param>
/// <param name="Codec">The codec the player can decode, for example MP3, FLAC, Opus, Vorbis, PCM or AAC.</param>
[DebuggerDisplay("Container: {Container} Codec: {Codec}")]
public sealed record PlaybackCapabilityDto(
string Container,
string Codec
);
@@ -0,0 +1,23 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.SharedKernel.Common.Enums.Playback;
using System.Diagnostics;
#endregion
namespace Lumina.Application.Common.DTO.MediaLibrary.AudioLibrary.MusicLibrary.Playback;
/// <summary>
/// Data transfer object for the decision of how the audio of a track is delivered to a player: as the original file, as the same codec repackaged, or re-encoded.
/// </summary>
/// <param name="Mode">The way the audio is delivered.</param>
/// <param name="Container">The container of the delivered audio, which is the stored container of the track for a direct play.</param>
/// <param name="Codec">The codec of the delivered audio, which is the stored codec of the track for a direct play.</param>
/// <param name="Shape">The shape the delivered audio is addressed and seeked by.</param>
/// <param name="SegmentDurationSeconds">The duration of each segment of the delivered audio, in seconds, or zero when the shape is not time addressed.</param>
[DebuggerDisplay("Mode: {Mode} Container: {Container} Codec: {Codec} Shape: {Shape}")]
public sealed record PlaybackDeliveryDecisionDto(
PlaybackDeliveryMode Mode,
string Container,
string Codec,
PlaybackDeliveryShape Shape,
int SegmentDurationSeconds
);
@@ -0,0 +1,16 @@
#region ========================================================================= USING =====================================================================================
using System.Diagnostics;
#endregion
namespace Lumina.Application.Common.DTO.MediaLibrary.AudioLibrary.MusicLibrary.Playback;
/// <summary>
/// Data transfer object for the stored format of a track's audio, that a playback delivery decision is made on.
/// </summary>
/// <param name="Container">The container of the audio file of the track, for example MP3, FLAC, OGG, MP4 or WAV.</param>
/// <param name="Codec">The codec of the audio of the track, as reported by the probe, for example AAC, FLAC, Opus or PCM.</param>
[DebuggerDisplay("Container: {Container} Codec: {Codec}")]
public sealed record PlaybackSourceFormatDto(
string? Container,
string? Codec
);
@@ -0,0 +1,25 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DTO.MediaLibrary.AudioLibrary.MusicLibrary.Playback;
using Lumina.Domain.Common.Primitives;
using System.Collections.Generic;
#endregion
namespace Lumina.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Playback;
/// <summary>
/// Interface for the service that decides whether the audio of a track can be played as stored by a player, or has to be
/// repackaged into another container or re-encoded into another codec before the player can play it.
/// </summary>
public interface IPlaybackFormatService
{
/// <summary>
/// Decides how the audio of a track, described by <paramref name="sourceFormat"/>, is delivered to a player that advertises <paramref name="capabilities"/>.
/// </summary>
/// <param name="sourceFormat">The container and codec of the audio of the track, read from its stored probe result.</param>
/// <param name="capabilities">The container and codec combinations the player advertises it can demux and decode directly.</param>
/// <returns>
/// An <see cref="Result{TValue}"/> containing either the delivery decision, or an error when the server cannot produce any
/// format the player advertises it supports.
/// </returns>
Result<PlaybackDeliveryDecisionDto> DecideDelivery(PlaybackSourceFormatDto sourceFormat, IReadOnlyList<PlaybackCapabilityDto> capabilities);
}
@@ -0,0 +1,76 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DTO.MediaLibrary.AudioLibrary.MusicLibrary.Playback;
using System;
using System.Collections.Generic;
#endregion
namespace Lumina.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Playback;
/// <summary>
/// Parses the capabilities input of a playback manifest request into the container and codec combinations a player advertises it
/// can demux and decode directly.
/// </summary>
/// <remarks>
/// The input is a comma separated list of <c>container/codec</c> pairs, for example
/// <c>mp3/mp3,flac/flac,ogg/opus,ogg/vorbis,wav/pcm,mp4/aac</c>. The container and the codec of a pair are matched case
/// insensitively, and a pair that does not name both, with a non-empty container and codec, is ignored. The number of pairs is
/// capped, so a request cannot force an unbounded amount of parsing work. When the input is absent or lists no valid pair, the
/// <see cref="Default"/> capabilities of the bundled player apply.
/// </remarks>
public static class PlaybackCapabilitiesParser
{
/// <summary>
/// The maximum number of capability pairs that are read from a single request, so that the parsing work stays bounded.
/// </summary>
public const int MAX_CAPABILITY_COUNT = 32;
/// <summary>
/// The character that separates the container from the codec of a single capability pair.
/// </summary>
public const char CONTAINER_CODEC_SEPARATOR = '/';
/// <summary>
/// The character that separates the capability pairs of a capability set.
/// </summary>
public const char CAPABILITY_SEPARATOR = ',';
/// <summary>
/// Gets the capability set the bundled player advertises, used when a request does not advertise any capability.
/// </summary>
public static IReadOnlyList<PlaybackCapabilityDto> Default { get; } =
[
new PlaybackCapabilityDto("MP3", "MP3"),
new PlaybackCapabilityDto("FLAC", "FLAC"),
new PlaybackCapabilityDto("OGG", "OPUS"),
new PlaybackCapabilityDto("OGG", "VORBIS"),
new PlaybackCapabilityDto("WAV", "PCM"),
new PlaybackCapabilityDto("AAC", "AAC")
];
/// <summary>
/// Parses the capabilities input of a playback manifest request into a capability set.
/// </summary>
/// <param name="capabilitiesInput">The value of the capabilities input, a comma separated list of <c>container/codec</c> pairs.</param>
/// <returns>The parsed capabilities, or <see cref="Default"/> when the input is absent, empty or lists no valid pair.</returns>
public static IReadOnlyList<PlaybackCapabilityDto> Parse(string? capabilitiesInput)
{
if (string.IsNullOrWhiteSpace(capabilitiesInput))
return Default;
List<PlaybackCapabilityDto> capabilities = [];
foreach (string entry in capabilitiesInput.Split(CAPABILITY_SEPARATOR, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
{
// The number of pairs is capped, so a single request cannot force an unbounded amount of parsing work.
if (capabilities.Count >= MAX_CAPABILITY_COUNT)
break;
// Both parts must be named: a container or a codec that is empty (for example "a//b" or "/wav") does not name a pair.
string[] parts = entry.Split(CONTAINER_CODEC_SEPARATOR, StringSplitOptions.TrimEntries);
if (parts.Length != 2 || parts[0].Length == 0 || parts[1].Length == 0)
continue;
capabilities.Add(new PlaybackCapabilityDto(parts[0], parts[1]));
}
return capabilities.Count == 0 ? Default : capabilities;
}
}