mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 07:11:10 +00:00
Adds seekable normalized audio track delivery
This commit is contained in:
1 parent
4e13b23f16
commit
45f9aa3bd5
57 files changed
+3452
-344
No files matched your search
@@ -1,6 +1,9 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
@@ -15,6 +18,21 @@ namespace Lumina.Presentation.Web.Common.Api;
|
||||
/// </summary>
|
||||
public sealed class ApiStreamResult : IAsyncDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// The response header that carries the number of segments of a normalized audio production. It must match the name the API sends.
|
||||
/// </summary>
|
||||
public const string SEGMENT_COUNT_HEADER_NAME = "X-Lumina-Segment-Count";
|
||||
|
||||
/// <summary>
|
||||
/// The response header that carries the exact start of a segment on the track timeline, in seconds. It must match the name the API sends.
|
||||
/// </summary>
|
||||
public const string SEGMENT_START_SECONDS_HEADER_NAME = "X-Segment-Start-Seconds";
|
||||
|
||||
/// <summary>
|
||||
/// The response header that carries the produced duration of the normalized track, in seconds. It must match the name the API sends.
|
||||
/// </summary>
|
||||
public const string TRACK_DURATION_SECONDS_HEADER_NAME = "X-Lumina-Track-Duration-Seconds";
|
||||
|
||||
private readonly HttpResponseMessage _response;
|
||||
|
||||
/// <summary>
|
||||
@@ -62,6 +80,22 @@ public sealed class ApiStreamResult : IAsyncDisposable
|
||||
/// </summary>
|
||||
public string? CacheControl { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of segments the normalized audio production holds, when the response is a segment of one and declares it.
|
||||
/// </summary>
|
||||
public int? SegmentCount { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the exact start of the segment on the track timeline, in seconds, as declared by the API, or <see langword="null"/> when the response
|
||||
/// declares none.
|
||||
/// </summary>
|
||||
public string? SegmentStartSeconds { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the produced duration of the normalized track, in seconds, as declared by the API, or <see langword="null"/> when the response declares none.
|
||||
/// </summary>
|
||||
public int? TrackDurationSeconds { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ApiStreamResult"/> class.
|
||||
/// </summary>
|
||||
@@ -79,6 +113,22 @@ public sealed class ApiStreamResult : IAsyncDisposable
|
||||
ETag = response.Headers.ETag?.ToString();
|
||||
LastModified = response.Content.Headers.LastModified;
|
||||
CacheControl = response.Headers.CacheControl?.ToString();
|
||||
// The count is optional, because the same result also represents a track stream, which has no segments. A malformed or negative
|
||||
// value is treated as absent instead of being passed to a caller that would use it to decide where the audio ends.
|
||||
SegmentCount = response.Headers.TryGetValues(SEGMENT_COUNT_HEADER_NAME, out IEnumerable<string>? segmentCountValues)
|
||||
&& int.TryParse(segmentCountValues.FirstOrDefault(), NumberStyles.None, CultureInfo.InvariantCulture, out int parsedSegmentCount)
|
||||
&& parsedSegmentCount >= 0
|
||||
? parsedSegmentCount
|
||||
: null;
|
||||
// The start is forwarded as the API wrote it, so the web hop never reinterprets a decimal with the culture of this application.
|
||||
SegmentStartSeconds = response.Headers.TryGetValues(SEGMENT_START_SECONDS_HEADER_NAME, out IEnumerable<string>? segmentStartValues)
|
||||
? segmentStartValues.FirstOrDefault()
|
||||
: null;
|
||||
TrackDurationSeconds = response.Headers.TryGetValues(TRACK_DURATION_SECONDS_HEADER_NAME, out IEnumerable<string>? trackDurationValues)
|
||||
&& int.TryParse(trackDurationValues.FirstOrDefault(), NumberStyles.None, CultureInfo.InvariantCulture, out int parsedTrackDuration)
|
||||
&& parsedTrackDuration > 0
|
||||
? parsedTrackDuration
|
||||
: null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
+16
-3
@@ -9,7 +9,7 @@ namespace Lumina.Presentation.Web.Common.DTO.AudioLibrary.MusicLibrary;
|
||||
/// <summary>
|
||||
/// Data transfer object for the playback manifest of a track, describing the format of its stored audio, the delivery the server decided on for the requesting player, and the path of its stream.
|
||||
/// </summary>
|
||||
[DebuggerDisplay("TrackId: {TrackId} DirectPlay: {DirectPlay}")]
|
||||
[DebuggerDisplay("TrackId: {TrackId} IsDirectPlay: {IsDirectPlay}")]
|
||||
public class PlaybackManifestDto
|
||||
{
|
||||
/// <summary>
|
||||
@@ -70,7 +70,7 @@ public class PlaybackManifestDto
|
||||
/// <summary>
|
||||
/// Gets or sets whether the player can play the stored audio as it is, without the server changing its container or its codec.
|
||||
/// </summary>
|
||||
public bool DirectPlay { get; set; }
|
||||
public bool IsDirectPlay { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the container of the audio the server delivers, when the delivery is not a direct play.
|
||||
@@ -95,11 +95,24 @@ public class PlaybackManifestDto
|
||||
/// <summary>
|
||||
/// Gets or sets whether the delivered audio can be seeked by the player.
|
||||
/// </summary>
|
||||
public bool Seekable { get; set; }
|
||||
public bool IsSeekable { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the path of the track stream. The API returns it relative to itself; this web application replaces it with
|
||||
/// the URL of its own streaming proxy, so that the browser never reaches the API server directly.
|
||||
/// </summary>
|
||||
public string StreamPath { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the opaque handle of the normalized delivery. The API returns it so the segment route can address exactly the delivery
|
||||
/// the manifest declared. It is <see langword="null"/> for a direct play.
|
||||
/// </summary>
|
||||
public string? DeliveryId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the path template of the segment route of the normalized delivery. The API returns it relative to itself with a
|
||||
/// <c>{deliveryId}</c> and a <c>{segmentIndex}</c> placeholder; this web application replaces it with the URL of its own segment proxy, so
|
||||
/// that the browser never reaches the API server directly. It is <see langword="null"/> for a direct play.
|
||||
/// </summary>
|
||||
public string? SegmentStreamPathTemplate { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Presentation.Web.Common.Api;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Common.Http;
|
||||
|
||||
/// <summary>
|
||||
/// Writes a streamed API response to the browser, so the direct track stream and the normalized segment proxy share the same status, header, caching and body forwarding rules and cannot drift apart.
|
||||
/// </summary>
|
||||
public static class ApiStreamProxy
|
||||
{
|
||||
/// <summary>
|
||||
/// Copies the streamed API response to the response sent to the browser, without buffering its content.
|
||||
/// </summary>
|
||||
/// <param name="response">The response sent to the browser.</param>
|
||||
/// <param name="streamResult">The streamed response of the API.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the copy when the client disconnects.</param>
|
||||
/// <returns>A <see cref="Task"/> that represents the asynchronous copy operation.</returns>
|
||||
public static async Task WriteAsync(HttpResponse response, ApiStreamResult streamResult, CancellationToken cancellationToken)
|
||||
{
|
||||
response.StatusCode = (int)streamResult.StatusCode;
|
||||
if (!string.IsNullOrEmpty(streamResult.ContentType))
|
||||
response.ContentType = streamResult.ContentType;
|
||||
if (streamResult.ContentLength.HasValue)
|
||||
response.ContentLength = streamResult.ContentLength.Value;
|
||||
SetHeader(response, "Content-Range", streamResult.ContentRange);
|
||||
SetHeader(response, "Accept-Ranges", streamResult.AcceptRanges);
|
||||
SetHeader(response, "ETag", streamResult.ETag);
|
||||
SetHeader(response, "Last-Modified", streamResult.LastModified?.ToString("R"));
|
||||
if (streamResult.SegmentCount.HasValue)
|
||||
SetHeader(response, ApiStreamResult.SEGMENT_COUNT_HEADER_NAME, streamResult.SegmentCount.Value.ToString());
|
||||
SetHeader(response, ApiStreamResult.SEGMENT_START_SECONDS_HEADER_NAME, streamResult.SegmentStartSeconds);
|
||||
if (streamResult.TrackDurationSeconds.HasValue)
|
||||
SetHeader(response, ApiStreamResult.TRACK_DURATION_SECONDS_HEADER_NAME, streamResult.TrackDurationSeconds.Value.ToString());
|
||||
// The response is private to the cookie that authenticates this hop, and it never becomes publicly cacheable, whatever the API declared.
|
||||
response.Headers["Cache-Control"] = BuildCacheControl(streamResult.CacheControl);
|
||||
response.Headers["Vary"] = "Cookie";
|
||||
await streamResult.Content.CopyToAsync(response.Body, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a response header to the given value, unless the value is missing.
|
||||
/// </summary>
|
||||
/// <param name="response">The response to set the header on.</param>
|
||||
/// <param name="name">The name of the header.</param>
|
||||
/// <param name="value">The value of the header, or <see langword="null"/> when the API did not send it.</param>
|
||||
private static void SetHeader(HttpResponse response, string name, string? value)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
response.Headers[name] = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds the caching instructions of the browser response from the instructions of the API.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A streamed media response is specific to the authenticated user, so it must never be stored by a shared cache. Every other instruction of
|
||||
/// the API, like a maximum age for the private cache of the browser or a no-store, is kept, so the web hop never silently ignores a caching
|
||||
/// decision the API made for the resource.
|
||||
/// </remarks>
|
||||
/// <param name="apiCacheControl">The caching instructions of the API response, or <see langword="null"/> when it declared none.</param>
|
||||
/// <returns>The caching instructions of the response sent to the browser.</returns>
|
||||
private static string BuildCacheControl(string? apiCacheControl)
|
||||
{
|
||||
if (!CacheControlHeaderValue.TryParse(apiCacheControl, out CacheControlHeaderValue? cacheControl))
|
||||
cacheControl = new CacheControlHeaderValue();
|
||||
cacheControl.Public = false;
|
||||
cacheControl.Private = true;
|
||||
return cacheControl.ToString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
namespace Lumina.Presentation.Web.Common.Playback;
|
||||
|
||||
/// <summary>
|
||||
/// Defines the format of the opaque delivery handle of a playback manifest.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The web application is a separate project that keeps its own contracts, so it repeats the API's rule rather than sharing a type: a handle is
|
||||
/// exactly 32 lowercase hexadecimal characters, the <c>N</c> format of a GUID. A value that does not match it is rejected, never normalized.
|
||||
/// </remarks>
|
||||
public static class PlaybackDeliveryId
|
||||
{
|
||||
/// <summary>
|
||||
/// The number of characters of a delivery handle.
|
||||
/// </summary>
|
||||
public const int CHARACTER_COUNT = 32;
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether a value has the format of a delivery handle.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to check.</param>
|
||||
/// <returns><see langword="true"/> when the value is exactly 32 lowercase hexadecimal characters, otherwise <see langword="false"/>.</returns>
|
||||
public static bool IsValid(string? value)
|
||||
{
|
||||
if (value is null || value.Length != CHARACTER_COUNT)
|
||||
return false;
|
||||
foreach (char character in value)
|
||||
if (!((character >= '0' && character <= '9') || (character >= 'a' && character <= 'f')))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Common.Requests.Library.AudioLibrary.MusicLibrary.Tracks;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a request to stream one segment of the normalized audio of a track.
|
||||
/// </summary>
|
||||
/// <param name="ArtistId">The unique identifier of the artist the album belongs to, taken from the route. Required.</param>
|
||||
/// <param name="AlbumId">The unique identifier of the album the track belongs to, taken from the route. Required.</param>
|
||||
/// <param name="TrackId">The unique identifier of the track to stream, taken from the route. Required.</param>
|
||||
/// <param name="DeliveryId">The opaque handle of the normalized delivery, taken from the route. Required.</param>
|
||||
/// <param name="SegmentIndex">The zero based index of the segment of the normalized audio, taken from the route. Required.</param>
|
||||
/// <param name="LibraryId">The Id of the media library the track belongs to. Required.</param>
|
||||
[DebuggerDisplay("TrackId: {TrackId} DeliveryId: {DeliveryId} SegmentIndex: {SegmentIndex}")]
|
||||
public record StreamMusicTrackSegmentRequest(
|
||||
string ArtistId,
|
||||
string AlbumId,
|
||||
string TrackId,
|
||||
string DeliveryId,
|
||||
int SegmentIndex,
|
||||
Guid LibraryId
|
||||
);
|
||||
@@ -25,6 +25,7 @@ public static partial class ApiRoutes
|
||||
public const string UPDATE_TRACK = "libraries/{libraryId}/artists/{artistId}/albums/{albumId}/tracks/{trackId}";
|
||||
public const string DELETE_TRACK = "libraries/{libraryId}/artists/{artistId}/albums/{albumId}/tracks/{trackId}";
|
||||
public const string STREAM_TRACK = "libraries/{libraryId}/artists/{artistId}/albums/{albumId}/tracks/{trackId}/stream";
|
||||
public const string STREAM_TRACK_SEGMENT = "libraries/{libraryId}/artists/{artistId}/albums/{albumId}/tracks/{trackId}/stream/deliveries/{deliveryId}/segments/{segmentIndex}";
|
||||
public const string GET_PLAYBACK_MANIFEST = "libraries/{libraryId}/artists/{artistId}/albums/{albumId}/tracks/{trackId}/playback-manifest";
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,7 @@ public static partial class WebRoutes
|
||||
public const string SAVE_TRACK = "{culture}/library/audio-library/music-library/artists/{artistId}/albums/{albumId}/tracks/{trackId}/api-save-track";
|
||||
public const string DELETE_TRACK = "{culture}/library/audio-library/music-library/artists/{artistId}/albums/{albumId}/tracks/{trackId}/api-delete-track";
|
||||
public const string STREAM_TRACK = "{culture}/library/audio-library/music-library/artists/{artistId}/albums/{albumId}/tracks/{trackId}/api-stream-track";
|
||||
public const string STREAM_TRACK_SEGMENT = "{culture}/library/audio-library/music-library/artists/{artistId}/albums/{albumId}/tracks/{trackId}/api-stream-track-segment/{deliveryId}/{segmentIndex}";
|
||||
public const string GET_PLAYBACK_MANIFEST = "{culture}/library/audio-library/music-library/artists/{artistId}/albums/{albumId}/tracks/{trackId}/api-get-playback-manifest";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user