feat: add authenticated live podcasts and simplify native video playback
Include podcast capture, ingestion and replay, dependency and installer updates, and native video positioning after navigation. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
d9b183c500
commit
4fc91cec3e
45 changed files with 2330 additions and 378 deletions
|
|
@ -1,8 +1,108 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
#nullable enable
|
||||
|
||||
namespace Yavsc.ApiControllers
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Yavsc.Abstract.Streaming;
|
||||
using Yavsc.Api.Middleware;
|
||||
using Yavsc.Server.Helpers;
|
||||
using Yavsc.Services;
|
||||
|
||||
namespace Yavsc.ApiControllers;
|
||||
|
||||
[ApiController]
|
||||
[ApiExplorerSettings(GroupName = "podcasts")]
|
||||
[ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)]
|
||||
[Authorize(Policy = "ApiScope")]
|
||||
[Route(Constants.APIPrefix + "/podcasts")]
|
||||
[TypeFilter(typeof(PodcastExceptionFilter))]
|
||||
public sealed class PodcastController(ILiveProcessor processor) : ControllerBase
|
||||
{
|
||||
public class PodcastController : Controller
|
||||
/// <summary>Lists live broadcasts and retained replays for signed-in listeners.</summary>
|
||||
[HttpGet]
|
||||
[ProducesResponseType<IReadOnlyList<PodcastResponse>>(StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult<IReadOnlyList<PodcastResponse>>> List(CancellationToken ct)
|
||||
=> Ok(await processor.ListAsync(ct));
|
||||
|
||||
/// <summary>Starts a broadcast owned by the current user.</summary>
|
||||
[HttpPost]
|
||||
[ProducesResponseType<PodcastResponse>(StatusCodes.Status201Created)]
|
||||
[ProducesResponseType<ProblemDetails>(StatusCodes.Status409Conflict)]
|
||||
public async Task<ActionResult<PodcastResponse>> Create(CreatePodcastRequest request, CancellationToken ct)
|
||||
{
|
||||
var ownerId = User.GetUserId();
|
||||
if (string.IsNullOrWhiteSpace(ownerId)) return Unauthorized();
|
||||
var response = await processor.CreateAsync(ownerId, User.Identity?.Name ?? ownerId, request.Title, ct);
|
||||
return CreatedAtAction(nameof(Get), new { id = response.Id }, response);
|
||||
}
|
||||
|
||||
/// <summary>Returns metadata and ordered committed segments.</summary>
|
||||
[HttpGet("{id:guid}")]
|
||||
[ProducesResponseType<PodcastManifestResponse>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType<ProblemDetails>(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult<PodcastManifestResponse>> Get(Guid id, CancellationToken ct)
|
||||
=> Ok(await processor.GetAsync(id, ct));
|
||||
|
||||
/// <summary>Commits an MPEG-TS segment. Identical retries are idempotent.</summary>
|
||||
[HttpPut("{id:guid}/segments/{sequence:int}")]
|
||||
[Consumes("video/mp2t")]
|
||||
[RequestSizeLimit(8 * 1024 * 1024)]
|
||||
[ProducesResponseType<PodcastResponse>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType<ProblemDetails>(StatusCodes.Status409Conflict)]
|
||||
[ProducesResponseType<ProblemDetails>(StatusCodes.Status413PayloadTooLarge)]
|
||||
public async Task<ActionResult<PodcastResponse>> Append(Guid id, int sequence,
|
||||
[FromQuery] double duration, CancellationToken ct)
|
||||
{
|
||||
var ownerId = User.GetUserId();
|
||||
if (string.IsNullOrWhiteSpace(ownerId)) return Unauthorized();
|
||||
return Ok(await processor.AppendAsync(id, ownerId, sequence, duration, Request.Body, ct));
|
||||
}
|
||||
|
||||
/// <summary>Ends the broadcast and publishes its retained HLS replay.</summary>
|
||||
[HttpPost("{id:guid}/complete")]
|
||||
[ProducesResponseType<PodcastResponse>(StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult<PodcastResponse>> Complete(Guid id, CancellationToken ct)
|
||||
{
|
||||
var ownerId = User.GetUserId();
|
||||
if (string.IsNullOrWhiteSpace(ownerId)) return Unauthorized();
|
||||
return Ok(await processor.CompleteAsync(id, ownerId, ct));
|
||||
}
|
||||
|
||||
/// <summary>Serves the authenticated event playlist, terminated for completed or interrupted replays.</summary>
|
||||
[HttpGet("{id:guid}/index.m3u8")]
|
||||
[Produces("application/vnd.apple.mpegurl")]
|
||||
public async Task<IActionResult> Playlist(Guid id, CancellationToken ct)
|
||||
{
|
||||
var manifest = await processor.GetAsync(id, ct);
|
||||
Response.Headers.CacheControl = "no-store";
|
||||
var text = new StringBuilder("#EXTM3U\n#EXT-X-VERSION:3\n#EXT-X-TARGETDURATION:15\n");
|
||||
text.Append("#EXT-X-MEDIA-SEQUENCE:0\n#EXT-X-PLAYLIST-TYPE:EVENT\n");
|
||||
foreach (var segment in manifest.Segments)
|
||||
{
|
||||
text.Append(System.Globalization.CultureInfo.InvariantCulture, $"#EXTINF:{segment.DurationSeconds:0.######},\n");
|
||||
text.Append(System.Globalization.CultureInfo.InvariantCulture, $"segments/{segment.Sequence}.ts\n");
|
||||
}
|
||||
if (manifest.Podcast.State != PodcastState.Live) text.Append("#EXT-X-ENDLIST\n");
|
||||
return Content(text.ToString(), "application/vnd.apple.mpegurl", Encoding.UTF8);
|
||||
}
|
||||
|
||||
/// <summary>Reads one immutable committed segment. Authentication is required on every request.</summary>
|
||||
[HttpGet("{id:guid}/segments/{sequence:int}.ts")]
|
||||
[Produces("video/mp2t")]
|
||||
public async Task<IActionResult> Segment(Guid id, int sequence, CancellationToken ct)
|
||||
{
|
||||
Response.Headers.CacheControl = "private, no-store";
|
||||
return File(await processor.OpenSegmentAsync(id, sequence, ct), "video/mp2t");
|
||||
}
|
||||
|
||||
/// <summary>Deletes an ended broadcast and releases its owner's podcast quota.</summary>
|
||||
[HttpDelete("{id:guid}")]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
|
||||
{
|
||||
var ownerId = User.GetUserId();
|
||||
if (string.IsNullOrWhiteSpace(ownerId)) return Unauthorized();
|
||||
await processor.DeleteAsync(id, ownerId, ct);
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
21
src/Yavsc.Api/Middleware/PodcastExceptionFilter.cs
Normal file
21
src/Yavsc.Api/Middleware/PodcastExceptionFilter.cs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
using System.Text.Json;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Yavsc.Services;
|
||||
|
||||
namespace Yavsc.Api.Middleware;
|
||||
|
||||
public sealed class PodcastExceptionFilter(ILogger<PodcastExceptionFilter> logger) : IExceptionFilter
|
||||
{
|
||||
public void OnException(ExceptionContext context)
|
||||
{
|
||||
var exception = context.Exception;
|
||||
if (exception is not (PodcastException or IOException or JsonException))
|
||||
return;
|
||||
var status = exception is PodcastException podcast ? podcast.StatusCode : StatusCodes.Status503ServiceUnavailable;
|
||||
var detail = exception is PodcastException ? exception.Message : "Le stockage des podcasts est indisponible.";
|
||||
logger.LogWarning(exception, "Podcast request failed with status {Status}", status);
|
||||
context.Result = new ObjectResult(new ProblemDetails { Status = status, Title = detail }) { StatusCode = status };
|
||||
context.ExceptionHandled = true;
|
||||
}
|
||||
}
|
||||
49
src/Yavsc.Api/Podcast.http
Normal file
49
src/Yavsc.Api/Podcast.http
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
@baseUrl = https://localhost:5005
|
||||
@token = REPLACE_WITH_ACCESS_TOKEN
|
||||
@id = REPLACE_WITH_PODCAST_ID
|
||||
|
||||
### Create a live broadcast
|
||||
POST {{baseUrl}}/api/v1/podcasts
|
||||
Authorization: Bearer {{token}}
|
||||
Content-Type: application/json
|
||||
|
||||
{"title":"Concert en direct"}
|
||||
|
||||
### List live broadcasts and replays
|
||||
GET {{baseUrl}}/api/v1/podcasts
|
||||
Authorization: Bearer {{token}}
|
||||
|
||||
### Read committed metadata
|
||||
GET {{baseUrl}}/api/v1/podcasts/{{id}}
|
||||
Authorization: Bearer {{token}}
|
||||
|
||||
### Upload the first complete MPEG-TS segment (file produced by LibVLC)
|
||||
PUT {{baseUrl}}/api/v1/podcasts/{{id}}/segments/0?duration=4
|
||||
Authorization: Bearer {{token}}
|
||||
Content-Type: video/mp2t
|
||||
|
||||
< ./segment.ts
|
||||
|
||||
### Play the HLS event playlist (send Authorization on segment requests too)
|
||||
GET {{baseUrl}}/api/v1/podcasts/{{id}}/index.m3u8
|
||||
Authorization: Bearer {{token}}
|
||||
|
||||
### Read a committed segment
|
||||
GET {{baseUrl}}/api/v1/podcasts/{{id}}/segments/0.ts
|
||||
Authorization: Bearer {{token}}
|
||||
|
||||
### Finish and retain the replay
|
||||
POST {{baseUrl}}/api/v1/podcasts/{{id}}/complete
|
||||
Authorization: Bearer {{token}}
|
||||
|
||||
### Delete an ended podcast (owner only)
|
||||
DELETE {{baseUrl}}/api/v1/podcasts/{{id}}
|
||||
Authorization: Bearer {{token}}
|
||||
|
||||
### Unknown podcast: 404
|
||||
GET {{baseUrl}}/api/v1/podcasts/00000000-0000-0000-0000-000000000000
|
||||
Authorization: Bearer {{token}}
|
||||
|
||||
### OpenAPI in development (requires an Administrator token)
|
||||
GET {{baseUrl}}/openapi/podcasts.json
|
||||
Authorization: Bearer {{token}}
|
||||
|
|
@ -71,6 +71,7 @@ internal class Program
|
|||
// Binds the "Site" section (Bills, TempDir, Avatars, …) used by
|
||||
// the document-generation flow and BillingController.
|
||||
services.Configure<SiteSettings>(builder.Configuration.GetSection("Site"));
|
||||
services.AddPodcasts(builder.Configuration);
|
||||
|
||||
// accepts any access token issued by identity server
|
||||
services.AddAuthentication("Bearer")
|
||||
|
|
|
|||
14
src/Yavsc.Api/Services/PodcastServiceCollectionExtensions.cs
Normal file
14
src/Yavsc.Api/Services/PodcastServiceCollectionExtensions.cs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
using Yavsc.Services;
|
||||
|
||||
namespace Yavsc.Api.Services;
|
||||
|
||||
public static class PodcastServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddPodcasts(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.Configure<PodcastOptions>(configuration.GetSection("Podcast"));
|
||||
services.AddSingleton<ILiveProcessor, LiveProcessor>();
|
||||
services.AddOpenApi("podcasts", options => options.ShouldInclude = description => description.GroupName == "podcasts");
|
||||
return services;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue