Replace MediaMTX with encrypted SRT ingestion via FFmpeg.NET

Manage bounded UDP listeners directly in Yavsc.Api, expose secret SRT URLs in PostIt, and preserve quotas, revocation and replay. Update deployment and privacy documentation, remove relay configuration, and add real-media lifecycle regressions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Paul Schneider 2026-11-08 16:19:41 +00:00
commit e72df234bc
Signed by: notazof
GPG key ID: AD4B9B545FBD7D12
19 changed files with 681 additions and 645 deletions

View file

@ -44,14 +44,4 @@ public sealed class PodcastObsController(IPodcastObsService obs) : ControllerBas
return NoContent();
}
[AllowAnonymous, HttpPost("relay/auth")]
[RequestSizeLimit(4096), ResourceUsageExempt]
[EndpointSummary("Authentification externe MediaMTX : publication avec cle, lecture interne et administration privees")]
[ProducesResponseType<PodcastRelayAuthorization>(200)]
[ProducesResponseType(401)]
public async Task<IActionResult> Authenticate(PodcastRelayAuthentication request, CancellationToken ct)
{
var authorization = await obs.AuthenticateAsync(request, ct);
return authorization is null ? Unauthorized() : Ok(authorization);
}
}

View file

@ -81,7 +81,7 @@ Authorization: Bearer {{token}}
### OpenAPI in development (requires an Administrator token)
GET {{baseUrl}}/openapi/podcasts.json
Authorization: Bearer {{token}}
### Activate OBS for an owned planned podcast (returns a one-time secret)
### Activate OBS for an owned planned podcast (secret SRT ServerUrl; StreamKey is empty)
POST {{baseUrl}}/api/v1/podcasts/{{id}}/obs
Authorization: Bearer {{token}}
@ -94,5 +94,5 @@ DELETE {{baseUrl}}/api/v1/podcasts/{{id}}/obs
Authorization: Bearer {{token}}
# Activation: 403 other owner/closed account, 404 missing emission,
# 409 already started, 413 disk full, 429 consumption limit, 503 OBS disabled.
# relay/auth is called by MediaMTX, not by the end-user.
# 409 already started, 413 disk full, 429 consumption limit,
# 503 OBS disabled, FFmpeg unavailable or listener/UDP port capacity reached.

View file

@ -4,30 +4,25 @@ public sealed class PodcastObsOptions
{
public bool Enabled { get; set; }
public string ServerUrl { get; set; } = "";
public string RelayApiUrl { get; set; } = "http://127.0.0.1:9997/";
public string RelayReadUrl { get; set; } = "rtmp://127.0.0.1:1935/";
public string RelayUser { get; set; } = "yavsc";
public string RelayPassword { get; set; } = "";
public string ListenAddress { get; set; } = "0.0.0.0";
public int FirstPort { get; set; } = 10000;
public int PortCount { get; set; } = 16;
public int ActivationMinutes { get; set; } = 30;
public int MaxDurationMinutes { get; set; } = 120;
public int MaxConcurrentBroadcasts { get; set; } = 2;
public bool AllowUnencryptedRtmp { get; set; }
public void Validate()
{
if (!Enabled) return;
if (!Uri.TryCreate(ServerUrl, UriKind.Absolute, out var server)
|| (server.Scheme != "rtmps" && !(AllowUnencryptedRtmp && server.Scheme == "rtmp"))
|| server.Scheme != "srt" || !server.IsDefaultPort
|| server.UserInfo.Length != 0 || server.Query.Length != 0 || server.Fragment.Length != 0
|| server.AbsolutePath is not ("" or "/")
|| !Uri.TryCreate(RelayApiUrl, UriKind.Absolute, out var api) || api.Scheme is not ("http" or "https")
|| api.UserInfo.Length != 0 || api.Query.Length != 0 || api.Fragment.Length != 0
|| !Uri.TryCreate(RelayReadUrl, UriKind.Absolute, out var read) || read.Scheme != "rtmp"
|| read.UserInfo.Length != 0 || read.Query.Length != 0 || read.Fragment.Length != 0
|| read.AbsolutePath is not ("" or "/")
|| RelayPassword.Length < 32 || string.IsNullOrWhiteSpace(RelayUser)
|| !System.Net.IPAddress.TryParse(ListenAddress, out _)
|| FirstPort is < 1024 or > 65535 || PortCount is < 1 or > 128
|| (long)FirstPort + PortCount - 1 > 65535
|| ActivationMinutes is < 1 or > 1440 || MaxDurationMinutes is < 1 or > 1440
|| MaxConcurrentBroadcasts is < 1 or > 16)
throw new InvalidOperationException("Invalid Podcast:Obs relay configuration. Use RTMPS, private relay addresses and a random password of at least 32 characters.");
|| MaxConcurrentBroadcasts is < 1 or > 16 || MaxConcurrentBroadcasts > PortCount)
throw new InvalidOperationException("Invalid Podcast:Obs configuration. Use an SRT host without a port or query, a literal listen address and a bounded UDP port range.");
}
}

View file

@ -1,62 +0,0 @@
#nullable enable
using System.Net;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using Microsoft.Extensions.Options;
namespace Yavsc.Api.Services;
public sealed record PodcastObsPublisher(Guid PodcastId, Guid ConnectionId, bool Secure);
public interface IPodcastObsRelay
{
Task<IReadOnlyList<PodcastObsPublisher>> PublishersAsync(CancellationToken ct);
Task KickAsync(Guid id, CancellationToken ct, Guid? connectionId = null);
}
public sealed class PodcastObsRelay(HttpClient http, IOptions<PodcastObsOptions> options) : IPodcastObsRelay
{
private HttpRequestMessage Request(HttpMethod method, string path)
{
var settings = options.Value;
settings.Validate();
var request = new HttpRequestMessage(method, new Uri(new Uri(settings.RelayApiUrl.TrimEnd('/') + "/"), path));
request.Headers.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(Encoding.UTF8.GetBytes($"{settings.RelayUser}:{settings.RelayPassword}")));
return request;
}
public async Task<IReadOnlyList<PodcastObsPublisher>> PublishersAsync(CancellationToken ct)
{
var publishers = new List<PodcastObsPublisher>();
var page = 0;
while (true)
{
using var request = Request(HttpMethod.Get, $"v3/paths/list?itemsPerPage=100&page={page}");
using var response = await http.SendAsync(request, ct);
response.EnsureSuccessStatusCode();
using var document = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync(ct), cancellationToken: ct);
foreach (var item in document.RootElement.GetProperty("items").EnumerateArray())
if (item.GetProperty("ready").GetBoolean()
&& PodcastObsService.TryId(item.GetProperty("name").GetString() ?? "", out var id)
&& item.TryGetProperty("source", out var source) && source.ValueKind == JsonValueKind.Object
&& source.GetProperty("type").GetString() is "rtmpConn" or "rtmpsConn"
&& Guid.TryParse(source.GetProperty("id").GetString(), out var connection))
publishers.Add(new(id, connection, source.GetProperty("type").GetString() == "rtmpsConn"));
if (++page >= document.RootElement.GetProperty("pageCount").GetInt32()) break;
}
return publishers;
}
public async Task KickAsync(Guid id, CancellationToken ct, Guid? connectionId = null)
{
foreach (var publisher in (await PublishersAsync(ct)).Where(p => p.PodcastId == id
&& (!connectionId.HasValue || p.ConnectionId == connectionId)))
{
using var request = Request(HttpMethod.Post, $"v3/{(publisher.Secure ? "rtmps" : "rtmp")}/conns/kick/{publisher.ConnectionId}");
using var response = await http.SendAsync(request, ct);
if (response.StatusCode != HttpStatusCode.NotFound) response.EnsureSuccessStatusCode();
}
}
}

View file

@ -1,7 +1,6 @@
#nullable enable
using System.Security.Cryptography;
using System.Text;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Yavsc.Abstract.Streaming;
@ -18,43 +17,67 @@ public interface IPodcastObsService
Task<PodcastObsCredentials> ActivateAsync(Guid id, string ownerId, CancellationToken ct);
Task<PodcastObsStatus> StatusAsync(Guid id, string ownerId, CancellationToken ct);
Task DeactivateAsync(Guid id, string ownerId, CancellationToken ct);
Task<PodcastRelayAuthorization?> AuthenticateAsync(PodcastRelayAuthentication request, CancellationToken ct);
}
public sealed class PodcastObsService(ApplicationDbContext db, IOptions<PodcastObsOptions> options,
IOptions<ResourceUsageOptions> usageOptions, IPodcastObsRelay relay, TimeProvider clock,
IOptions<ResourceUsageOptions> usageOptions, PodcastObsWorker worker, TimeProvider clock,
ILogger<PodcastObsService> logger) : IPodcastObsService
{
private PodcastObsOptions Settings()
public async Task<PodcastObsCredentials> ActivateAsync(Guid id, string ownerId, CancellationToken ct)
{
var value = options.Value;
if (!value.Enabled) throw new PodcastException(503, "L'entree OBS n'est pas configuree sur cette instance.");
value.Validate();
return value;
}
public Task<PodcastObsCredentials> ActivateAsync(Guid id, string ownerId, CancellationToken ct)
=> ResourceUsageTracker.WithUserLockAsync(db, ownerId, async () =>
var settings = options.Value;
if (!settings.Enabled) throw new PodcastException(503, "L'entree OBS n'est pas configuree sur cette instance.");
settings.Validate();
await worker.WaitUntilReadyAsync(ct);
await worker.Lifecycle.WaitAsync(ct);
try
{
var settings = Settings();
var flow = await OwnedAsync(id, ownerId, ct);
if (flow.StartedAt is not null)
throw new PodcastException(409, "Cette emission a deja demarre. Preparez une nouvelle emission.");
if (flow.Owner.WithdrawalRequestedAt is not null)
throw new PodcastException(403, "Le compte est ferme.");
if (await QuotaReachedAsync(ownerId, ct))
throw new PodcastException(429, "Le seuil de consommation est atteint.");
if (flow.Owner.DiskUsage >= flow.Owner.GetEffectiveDiskQuota(clock.GetUtcNow()))
throw new PodcastException(413, "Le quota disque est atteint.");
var key = Convert.ToHexString(RandomNumberGenerator.GetBytes(32)).ToLowerInvariant();
flow.ObsKeyHash = Hash(key);
flow.ObsEnabledUntil = clock.GetUtcNow().AddMinutes(settings.ActivationMinutes);
flow.ObsConnectionId = null;
flow.ObsLastError = null;
await db.SaveChangesAsync(ct);
return new PodcastObsCredentials(settings.ServerUrl.TrimEnd('/'),
$"obs/{id:N}?key={key}", flow.ObsEnabledUntil.Value);
}, ct);
await ResourceUsageTracker.WithUserLockAsync(db, ownerId, async () =>
{
var flow = await EligibleAsync(id, ownerId, ct);
flow.ObsKeyHash = null;
flow.ObsEnabledUntil = null;
flow.ObsConnectionId = null;
await db.SaveChangesAsync(ct);
return true;
}, ct);
await worker.StopCaptureAsync(id, ct);
return await ResourceUsageTracker.WithUserLockAsync(db, ownerId, async () =>
{
var flow = await EligibleAsync(id, ownerId, ct);
var key = Convert.ToHexString(RandomNumberGenerator.GetBytes(32)).ToLowerInvariant();
var connectionId = Guid.NewGuid();
var expires = clock.GetUtcNow().AddMinutes(settings.ActivationMinutes);
flow.ObsKeyHash = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(key)));
flow.ObsEnabledUntil = expires;
flow.ObsConnectionId = connectionId;
flow.ObsLastError = null;
await db.SaveChangesAsync(ct);
try
{
var port = await worker.ArmAsync(id, ownerId, connectionId, key, expires, ct);
var url = new UriBuilder(settings.ServerUrl)
{
Port = port,
Query = $"mode=caller&passphrase={key}&pbkeylen=32&enforced_encryption=1&latency=200000"
};
return new PodcastObsCredentials(url.Uri.AbsoluteUri, "", expires);
}
catch (Exception ex)
{
flow.ObsKeyHash = null;
flow.ObsEnabledUntil = null;
flow.ObsConnectionId = null;
flow.ObsLastError = "Activation SRT impossible. Verifiez FFmpeg, les ports UDP et la capacite disponible.";
await db.SaveChangesAsync(CancellationToken.None);
logger.LogError("OBS listener activation failed ({ErrorType})", ex.GetType().Name);
if (ex is OperationCanceledException or PodcastException) throw;
throw new PodcastException(503, flow.ObsLastError);
}
}, ct);
}
finally { worker.Lifecycle.Release(); }
}
public async Task<PodcastObsStatus> StatusAsync(Guid id, string ownerId, CancellationToken ct)
{
@ -66,69 +89,39 @@ public sealed class PodcastObsService(ApplicationDbContext db, IOptions<PodcastO
public async Task DeactivateAsync(Guid id, string ownerId, CancellationToken ct)
{
var connectionId = await ResourceUsageTracker.WithUserLockAsync(db, ownerId, async () =>
await worker.Lifecycle.WaitAsync(ct);
try
{
var flow = await OwnedAsync(id, ownerId, ct);
var connection = flow.ObsConnectionId;
flow.ObsKeyHash = null;
flow.ObsEnabledUntil = null;
flow.ObsConnectionId = null;
await db.SaveChangesAsync(ct);
return connection;
}, ct);
if (connectionId.HasValue) await relay.KickAsync(id, ct, connectionId);
}
public async Task<PodcastRelayAuthorization?> AuthenticateAsync(PodcastRelayAuthentication request, CancellationToken ct)
{
var settings = Settings();
if (request.User == settings.RelayUser && EqualSecret(request.Password, settings.RelayPassword))
{
if (request.Action == "api" || request.Action == "read" && request.Protocol == "rtmp" && TryId(request.Path, out _))
return new(null);
}
if (request.Action == "publish" && request.Protocol == "rtmp" && TryId(request.Path, out var id))
{
var query = QueryHelpers.ParseQuery(request.Query ?? "");
if (query.TryGetValue("key", out var keys) && keys.Count == 1 && keys[0] is { Length: 64 } key
&& Guid.TryParse(request.Id, out var connectionId))
await ResourceUsageTracker.WithUserLockAsync(db, ownerId, async () =>
{
var flow = await db.LiveFlow.AsNoTracking().Include(f => f.Owner)
.SingleOrDefaultAsync(f => f.PodcastId == id && !f.PodcastDeleted, ct);
if (flow is not null)
return await ResourceUsageTracker.WithUserLockAsync<PodcastRelayAuthorization?>(db, flow.OwnerId, async () =>
{
var current = await db.LiveFlow.Include(f => f.Owner).SingleAsync(f => f.PodcastId == id, ct);
await db.Entry(current).ReloadAsync(ct);
await db.Entry(current.Owner).ReloadAsync(ct);
if (current is { StartedAt: null, ObsKeyHash: not null, PodcastDeleted: false }
&& (current.ObsConnectionId is null || current.ObsConnectionId == connectionId)
&& current.ObsEnabledUntil > clock.GetUtcNow() && current.Owner.WithdrawalRequestedAt is null
&& EqualSecret(Hash(key), current.ObsKeyHash)
&& !await QuotaReachedAsync(current.OwnerId, ct)
&& current.Owner.DiskUsage < current.Owner.GetEffectiveDiskQuota(clock.GetUtcNow()))
{
current.ObsConnectionId = connectionId;
await db.SaveChangesAsync(ct);
return new(clock.GetUtcNow().AddMinutes(Math.Min(current.PlannedDurationMinutes ?? 1, settings.MaxDurationMinutes)));
}
logger.LogWarning("OBS publication refused for emission {PodcastId}", id);
return null;
}, ct);
}
var flow = await OwnedAsync(id, ownerId, ct);
flow.ObsKeyHash = null;
flow.ObsEnabledUntil = null;
flow.ObsConnectionId = null;
await db.SaveChangesAsync(ct);
return true;
}, ct);
await worker.StopCaptureAsync(id, ct);
}
logger.LogWarning("OBS relay authentication refused for action {Action}", request.Action is "publish" or "read" or "api" ? request.Action : "other");
return null;
finally { worker.Lifecycle.Release(); }
}
private async Task<bool> QuotaReachedAsync(string ownerId, CancellationToken ct)
private async Task<LiveFlow> EligibleAsync(Guid id, string ownerId, CancellationToken ct)
{
var flow = await OwnedAsync(id, ownerId, ct);
if (flow.StartedAt is not null)
throw new PodcastException(409, "Cette emission a deja demarre. Preparez une nouvelle emission.");
if (flow.Owner.WithdrawalRequestedAt is not null)
throw new PodcastException(403, "Le compte est ferme.");
var record = await db.ResourceUsageRecords.AsNoTracking().Where(r => r.UserId == ownerId)
.OrderByDescending(r => r.RecordedAtUtc).ThenByDescending(r => r.Id).FirstOrDefaultAsync(ct);
var policy = usageOptions.Value;
var gross = record is null ? 0m : policy.Calculate(record.ApiCalls, record.CpuSeconds, record.BandwidthMb, record.StorageMb);
return ResourceUsageCredit.NetConsumption(gross, record?.CreditPoints ?? 0m) >= policy.GetLimit(ownerId);
if (ResourceUsageCredit.NetConsumption(gross, record?.CreditPoints ?? 0m) >= policy.GetLimit(ownerId))
throw new PodcastException(429, "Le seuil de consommation est atteint.");
if (flow.Owner.DiskUsage >= flow.Owner.GetEffectiveDiskQuota(clock.GetUtcNow()))
throw new PodcastException(413, "Le quota disque est atteint.");
return flow;
}
private async Task<LiveFlow> OwnedAsync(Guid id, string ownerId, CancellationToken ct, bool tracking = true)
@ -145,14 +138,4 @@ public sealed class PodcastObsService(ApplicationDbContext db, IOptions<PodcastO
if (flow.OwnerId != ownerId) throw new PodcastException(403, "Seul l'auteur peut activer OBS.");
return flow;
}
internal static bool TryId(string? path, out Guid id)
{
id = default;
return path is not null && path.StartsWith("obs/", StringComparison.Ordinal) && Guid.TryParseExact(path[4..], "N", out id);
}
private static string Hash(string value) => Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(value)));
private static bool EqualSecret(string? actual, string expected)
=> actual is not null && CryptographicOperations.FixedTimeEquals(Encoding.UTF8.GetBytes(actual), Encoding.UTF8.GetBytes(expected));
}

View file

@ -1,6 +1,8 @@
#nullable enable
using System.Diagnostics;
using System.Globalization;
using System.Net;
using System.Net.Sockets;
using FFmpeg.NET;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Yavsc.Models;
@ -11,222 +13,368 @@ using Yavsc.Settings;
namespace Yavsc.Api.Services;
public sealed class PodcastObsWorker(IServiceScopeFactory scopes, IOptions<PodcastObsOptions> options,
ILogger<PodcastObsWorker> logger) : BackgroundService
IOptions<PodcastOptions> mediaOptions, TimeProvider clock, ILogger<PodcastObsWorker> logger) : BackgroundService
{
private sealed record Capture(CancellationTokenSource Cancellation, Task Task);
private sealed class Capture(Guid id, string ownerId, Guid connectionId, int port,
DateTimeOffset expires, CancellationTokenSource cancellation)
{
public Guid Id { get; } = id;
public string OwnerId { get; } = ownerId;
public Guid ConnectionId { get; } = connectionId;
public int Port { get; } = port;
public DateTimeOffset Expires { get; } = expires;
public CancellationTokenSource Cancellation { get; } = cancellation;
public TaskCompletionSource Completion { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
public DateTimeOffset? Started { get; set; }
public string? Error { get; set; }
}
internal SemaphoreSlim Lifecycle { get; } = new(1, 1);
private readonly object gate = new();
private readonly Dictionary<Guid, Capture> captures = new();
private readonly TaskCompletionSource ready = new(TaskCreationOptions.RunContinuationsAsynchronously);
private CancellationToken stopping;
internal Task WaitUntilReadyAsync(CancellationToken ct) => ready.Task.WaitAsync(ct);
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
if (!options.Value.Enabled) return;
options.Value.Validate();
stopping = stoppingToken;
try
{
options.Value.Validate();
using (var scope = scopes.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
var stale = await db.LiveFlow.Where(f => f.ObsKeyHash != null).ToListAsync(stoppingToken);
foreach (var flow in stale)
await ResourceUsageTracker.WithUserLockAsync(db, flow.OwnerId, async () =>
{
flow.ObsKeyHash = null;
flow.ObsConnectionId = null;
flow.ObsEnabledUntil = null;
flow.ObsLastError = "L'API a redemarre ; reactivez OBS pour obtenir une nouvelle URL SRT.";
await db.SaveChangesAsync(stoppingToken);
return true;
}, stoppingToken);
}
ready.TrySetResult();
if (!options.Value.Enabled) return;
while (!stoppingToken.IsCancellationRequested)
{
try
{
using var scope = scopes.CreateScope();
var relay = scope.ServiceProvider.GetRequiredService<IPodcastObsRelay>();
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
foreach (var pair in captures.Where(c => c.Value.Task.IsCompleted).ToArray())
{
captures.Remove(pair.Key);
pair.Value.Cancellation.Dispose();
await pair.Value.Task;
}
foreach (var publisher in await relay.PublishersAsync(stoppingToken))
Capture[] active;
lock (gate) active = captures.Values.ToArray();
foreach (var capture in active)
{
var flow = await db.LiveFlow.AsNoTracking().Include(f => f.Owner)
.SingleOrDefaultAsync(f => f.PodcastId == publisher.PodcastId, stoppingToken);
var allowed = flow is { PodcastDeleted: false, ObsKeyHash: not null }
&& flow.ObsConnectionId == publisher.ConnectionId
&& flow.Owner.WithdrawalRequestedAt is null;
if (!allowed)
.SingleOrDefaultAsync(f => f.PodcastId == capture.Id, stoppingToken);
lock (gate)
{
if (captures.TryGetValue(publisher.PodcastId, out var running)) running.Cancellation.Cancel();
await relay.KickAsync(publisher.PodcastId, stoppingToken, publisher.ConnectionId);
continue;
if (!captures.ContainsKey(capture.Id)) continue;
if (capture.Completion.Task.IsCompleted)
{
captures.Remove(capture.Id);
capture.Cancellation.Dispose();
}
else if (flow is null || flow.PodcastDeleted || flow.ObsConnectionId != capture.ConnectionId
|| flow.ObsKeyHash is null || flow.Owner.WithdrawalRequestedAt is not null)
capture.Cancellation.Cancel();
}
if (captures.ContainsKey(publisher.PodcastId)) continue;
if (flow!.StartedAt is not null || flow.ObsEnabledUntil <= DateTimeOffset.UtcNow
|| captures.Count >= options.Value.MaxConcurrentBroadcasts)
{
await relay.KickAsync(publisher.PodcastId, stoppingToken, publisher.ConnectionId);
await FailActivationAsync(publisher.PodcastId, publisher.ConnectionId, "Emission deja demarree, activation expiree ou capacite OBS atteinte.", stoppingToken);
continue;
}
var cancellation = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken);
captures.Add(publisher.PodcastId, new(cancellation,
CaptureAsync(publisher.PodcastId, flow.OwnerId, publisher.ConnectionId, cancellation.Token)));
}
}
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested) { break; }
catch (Exception ex)
{
logger.LogError(ex, "OBS relay scan failed; retrying");
}
await Task.Delay(TimeSpan.FromSeconds(1), stoppingToken);
catch (Exception ex) { logger.LogError(ex, "OBS listener state check failed; retrying"); }
await Task.Delay(250, stoppingToken);
}
}
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested) { }
catch (Exception ex) { ready.TrySetException(ex); throw; }
finally
{
foreach (var capture in captures.Values) capture.Cancellation.Cancel();
await Task.WhenAll(captures.Values.Select(c => c.Task));
foreach (var capture in captures.Values) capture.Cancellation.Dispose();
Capture[] active;
lock (gate)
{
active = captures.Values.ToArray();
foreach (var capture in active) capture.Cancellation.Cancel();
}
await Task.WhenAll(active.Select(c => c.Completion.Task));
lock (gate)
{
foreach (var capture in captures.Values) capture.Cancellation.Dispose();
captures.Clear();
}
}
}
private async Task CaptureAsync(Guid id, string ownerId, Guid connectionId, CancellationToken ct)
internal async Task<int> ArmAsync(Guid id, string ownerId, Guid connectionId, string key,
DateTimeOffset expires, CancellationToken ct)
{
using var scope = scopes.CreateScope();
var services = scope.ServiceProvider;
var db = services.GetRequiredService<ApplicationDbContext>();
var processor = services.GetRequiredService<ILiveProcessor>();
var relay = services.GetRequiredService<IPodcastObsRelay>();
var media = services.GetRequiredService<IOptions<PodcastOptions>>().Value;
var directory = Path.Combine(media.RootPath, "obs-capture-" + id.ToString("N"));
stopping.ThrowIfCancellationRequested();
Capture capture;
lock (gate)
{
foreach (var ended in captures.Values.Where(c => c.Completion.Task.IsCompleted).ToArray())
{
captures.Remove(ended.Id);
ended.Cancellation.Dispose();
}
if (captures.Count >= options.Value.MaxConcurrentBroadcasts)
throw new PodcastException(503, "Capacite OBS atteinte ; revoquez une activation inutilisee ou reessayez plus tard.");
var port = Enumerable.Range(options.Value.FirstPort, options.Value.PortCount)
.FirstOrDefault(p => captures.Values.All(c => c.Port != p) && !PortBound(p));
if (port == 0) throw new PodcastException(503, "Aucun port UDP OBS disponible.");
capture = new(id, ownerId, connectionId, port, expires,
CancellationTokenSource.CreateLinkedTokenSource(stopping));
captures.Add(id, capture);
}
_ = CaptureAsync(capture, key);
try
{
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(ct, stopping);
timeout.CancelAfter(TimeSpan.FromSeconds(10));
while (!PortBound(capture.Port))
{
if (capture.Error is not null || capture.Completion.Task.IsCompleted)
throw new PodcastException(503, capture.Error ?? "L'ecoute SRT n'a pas demarre.");
await Task.Delay(50, timeout.Token);
}
return capture.Port;
}
catch
{
// Do not wait here: activation owns the accounting lock needed by capture cleanup.
lock (gate) capture.Cancellation.Cancel();
throw;
}
}
internal async Task StopCaptureAsync(Guid id, CancellationToken ct)
{
Capture? capture;
lock (gate)
{
if (!captures.TryGetValue(id, out capture)) return;
capture.Cancellation.Cancel();
}
await capture.Completion.Task.WaitAsync(ct);
if (PortBound(capture.Port))
throw new PodcastException(503, "L'ecoute SRT ne s'est pas arretee ; intervention de l'administration requise.");
lock (gate)
if (captures.Remove(id)) capture.Cancellation.Dispose();
}
private bool PortBound(int port)
{
var address = IPAddress.Parse(options.Value.ListenAddress);
using var socket = new Socket(address.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
try { socket.Bind(new IPEndPoint(address, port)); return false; }
catch (SocketException ex) when (ex.SocketErrorCode == SocketError.AddressAlreadyInUse) { return true; }
}
private async Task CaptureAsync(Capture capture, string key)
{
var media = mediaOptions.Value;
var directory = Path.Combine(media.RootPath, $"obs-capture-{capture.Id:N}-{capture.ConnectionId:N}");
string? error = null;
var started = false;
try
{
Directory.CreateDirectory(directory);
var podcast = await services.GetRequiredService<IPodcastScheduleService>().StartAsync(id, ownerId, ct, connectionId);
started = true;
using var deadline = CancellationTokenSource.CreateLinkedTokenSource(ct);
deadline.CancelAfter(TimeSpan.FromMinutes(Math.Min(podcast.PlannedDurationMinutes ?? 1, options.Value.MaxDurationMinutes)));
await ReceiveAsync(id, ownerId, connectionId, directory, db, processor, media, services, deadline.Token);
using var scope = scopes.CreateScope();
using var receive = CancellationTokenSource.CreateLinkedTokenSource(capture.Cancellation.Token);
var guard = GuardBufferAsync(directory, media.MaxSegmentBytes, receive.Token);
var ingest = ReceiveAsync(capture, key, directory, scope.ServiceProvider, receive.Token);
try
{
if (await Task.WhenAny(ingest, guard) == guard) await guard;
await ingest;
}
finally
{
receive.Cancel();
try { await ingest; }
catch (OperationCanceledException) when (receive.IsCancellationRequested) { }
try { await guard; }
catch (OperationCanceledException) when (receive.IsCancellationRequested) { }
}
}
catch (OperationCanceledException)
{
logger.LogInformation("OBS capture {PodcastId} stopped or reached its duration limit", id);
logger.LogInformation("OBS capture {PodcastId} stopped", capture.Id);
}
catch (Exception ex)
{
// FFmpeg diagnostics and request URLs can contain credentials: never persist or log them.
error = ex is PodcastException or ResourceUsageLimitException ? ex.Message : "Reception OBS impossible. Verifiez le relais, FFmpeg et l'encodage H.264/AAC.";
logger.LogError("OBS capture {PodcastId} failed ({ErrorType})", id, ex.GetType().Name);
error = ex is PodcastException or ResourceUsageLimitException ? ex.Message
: "Reception SRT impossible. Verifiez FFmpeg, la connexion et l'encodage H.264/AAC.";
logger.LogError("OBS capture {PodcastId} failed ({ErrorType})", capture.Id, ex.GetType().Name);
}
finally
{
capture.Error = error;
try
{
using var scope = scopes.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(20));
await relay.KickAsync(id, timeout.Token, connectionId);
db.ChangeTracker.Clear();
var flow = await db.LiveFlow.Include(f => f.Owner).SingleOrDefaultAsync(f => f.PodcastId == id, timeout.Token);
if (flow is not null)
await ResourceUsageTracker.WithUserLockAsync(db, capture.OwnerId, async () =>
{
if (flow.ObsKeyHash is null && flow.ObsConnectionId is null) error = null;
if (started && flow.Owner.WithdrawalRequestedAt is null)
await processor.CompleteAsync(id, ownerId, timeout.Token);
if (flow.ObsConnectionId == connectionId || flow.ObsKeyHash is null)
var flow = await db.LiveFlow.Include(f => f.Owner)
.SingleOrDefaultAsync(f => f.PodcastId == capture.Id, timeout.Token);
if (flow is not null)
{
flow.ObsKeyHash = null;
flow.ObsEnabledUntil = null;
flow.ObsConnectionId = null;
flow.ObsLastError = error;
if (capture.Started is not null && flow.Owner.WithdrawalRequestedAt is null)
{
try
{
await scope.ServiceProvider.GetRequiredService<ILiveProcessor>()
.CompleteAsync(capture.Id, capture.OwnerId, timeout.Token);
}
catch (Exception ex)
{
error = "Finalisation OBS impossible ; intervention de l'administration requise.";
capture.Error = error;
logger.LogError("OBS replay finalization failed for {PodcastId} ({ErrorType})",
capture.Id, ex.GetType().Name);
}
}
if (flow.ObsConnectionId == capture.ConnectionId)
{
flow.ObsKeyHash = null;
flow.ObsEnabledUntil = null;
flow.ObsConnectionId = null;
flow.ObsLastError = error;
await db.SaveChangesAsync(timeout.Token);
}
}
await db.SaveChangesAsync(timeout.Token);
}
return true;
}, timeout.Token);
}
catch (Exception ex)
{
logger.LogError("OBS capture {PodcastId} cleanup failed ({ErrorType}); operator intervention required", id, ex.GetType().Name);
await FailActivationAsync(id, connectionId, "Finalisation OBS impossible ; intervention de l'administration requise.", CancellationToken.None);
logger.LogError("OBS capture {PodcastId} finalization failed ({ErrorType}); operator intervention required",
capture.Id, ex.GetType().Name);
capture.Error = "Finalisation OBS impossible ; intervention de l'administration requise.";
}
if (Directory.Exists(directory)) Directory.Delete(directory, recursive: true);
try { if (Directory.Exists(directory)) Directory.Delete(directory, recursive: true); }
catch (Exception ex)
{
logger.LogError("OBS temporary files cleanup failed for {PodcastId} ({ErrorType})",
capture.Id, ex.GetType().Name);
}
capture.Completion.TrySetResult();
}
}
private async Task ReceiveAsync(Guid id, string ownerId, Guid connectionId, string directory, ApplicationDbContext db,
ILiveProcessor processor, PodcastOptions media, IServiceProvider services, CancellationToken ct)
private async Task ReceiveAsync(Capture capture, string key, string directory, IServiceProvider services, CancellationToken ct)
{
var settings = options.Value;
var input = new UriBuilder(new Uri(new Uri(settings.RelayReadUrl.TrimEnd('/') + "/"), $"obs/{id:N}"))
{ Query = $"user={Uri.EscapeDataString(settings.RelayUser)}&pass={Uri.EscapeDataString(settings.RelayPassword)}" };
var start = new ProcessStartInfo(media.FfmpegPath)
var input = new UriBuilder("srt", options.Value.ListenAddress, capture.Port)
{
UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true,
CreateNoWindow = true, WorkingDirectory = directory
Query = $"mode=listener&passphrase={key}&pbkeylen=32&enforced_encryption=1"
+ $"&listen_timeout={options.Value.ActivationMinutes * 60000000L}&timeout=20000000&latency=200000"
};
foreach (var argument in new[]
{
"-nostdin", "-hide_banner", "-loglevel", "error", "-rw_timeout", "15000000",
"-i", input.Uri.AbsoluteUri, "-map", "0:v:0", "-map", "0:a:0?", "-c", "copy",
"-f", "segment", "-segment_format", "mpegts", "-segment_time", "2",
"-segment_list", "pipe:1", "-segment_list_type", "csv", "segment-%08d.ts"
}) start.ArgumentList.Add(argument);
using var process = Process.Start(start) ?? throw new IOException("FFmpeg could not start.");
var diagnostics = DrainAsync(process.StandardError);
using var monitoring = CancellationTokenSource.CreateLinkedTokenSource(ct);
var guard = GuardBufferAsync(directory, media.MaxSegmentBytes, monitoring.Token);
var engine = new Engine(mediaOptions.Value.FfmpegPath);
var failed = false;
engine.Error += (_, _) => Volatile.Write(ref failed, true);
using var processCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct);
var process = engine.ExecuteAsync(
$"-nostdin -hide_banner -loglevel error -i \"{input.Uri.AbsoluteUri}\" -map 0:v:0 -map 0:a:0? -c copy"
+ " -f segment -segment_format mpegts -segment_time 2 -segment_list segments.csv"
+ " -segment_list_type csv segment-%08d.ts", directory, processCancellation.Token);
var db = services.GetRequiredService<ApplicationDbContext>();
var processor = services.GetRequiredService<ILiveProcessor>();
var next = 0;
var total = 0L;
var revoked = false;
var lastSegment = clock.GetUtcNow();
try
{
while (true)
{
using var idle = CancellationTokenSource.CreateLinkedTokenSource(ct);
idle.CancelAfter(TimeSpan.FromSeconds(20));
var read = process.StandardOutput.ReadLineAsync(idle.Token).AsTask();
if (await Task.WhenAny(read, guard) == guard) await guard;
var line = await read;
if (line is null) break;
var fields = line.Split(',');
if (fields.Length != 3 || fields[0] != $"segment-{next:D8}.ts"
|| !double.TryParse(fields[1], CultureInfo.InvariantCulture, out var begin)
|| !double.TryParse(fields[2], CultureInfo.InvariantCulture, out var end)
|| !double.IsFinite(end - begin) || end - begin <= 0 || end - begin > 15)
throw new PodcastException(400, "Segments OBS invalides ; configurez H.264/AAC et des images cles toutes les deux secondes.");
var path = Path.Combine(directory, fields[0]);
var bytes = new FileInfo(path).Length;
if (bytes == 0 || bytes > media.MaxSegmentBytes)
throw new PodcastException(413, "Segment OBS trop volumineux ; reduisez le debit et l'intervalle des images cles.");
total = checked(total + bytes);
await ResourceUsageTracker.RecordStorageAsync(db, ownerId, ownerId, $"podcast/{id:N}", total, ct,
write: async () =>
ct.ThrowIfCancellationRequested();
var now = clock.GetUtcNow();
if (capture.Started is null && now >= capture.Expires)
throw new PodcastException(408, "Activation OBS expiree ; obtenez une nouvelle URL SRT.");
if (capture.Started is not null)
{
if (now - capture.Started >= TimeSpan.FromMinutes(options.Value.MaxDurationMinutes))
break;
if (now - lastSegment > TimeSpan.FromSeconds(20))
throw new PodcastException(408, "Aucun segment OBS recu depuis vingt secondes ; verifiez la connexion.");
}
var playlist = Path.Combine(directory, "segments.csv");
var text = File.Exists(playlist) ? await File.ReadAllTextAsync(playlist, ct) : "";
var lines = text.Split('\n');
for (; next < lines.Length - 1; next++)
{
if (next >= mediaOptions.Value.MaxSegments) throw new PodcastException(413, "Nombre maximal de segments atteint.");
var fields = lines[next].TrimEnd('\r').Split(',');
if (fields.Length != 3 || fields[0] != $"segment-{next:D8}.ts"
|| !double.TryParse(fields[1], CultureInfo.InvariantCulture, out var begin)
|| !double.TryParse(fields[2], CultureInfo.InvariantCulture, out var end)
|| !double.IsFinite(end - begin) || end - begin <= 0 || end - begin > 15)
throw new PodcastException(400, "Segments OBS invalides ; configurez H.264/AAC et des images cles toutes les deux secondes.");
if (capture.Started is null)
{
var user = await db.Users.SingleAsync(u => u.Id == ownerId, ct);
await db.Entry(user).ReloadAsync(ct);
var flow = await db.LiveFlow.SingleAsync(f => f.PodcastId == id, ct);
await db.Entry(flow).ReloadAsync(ct);
if (user.WithdrawalRequestedAt is not null || flow.ObsKeyHash is null
|| flow.ObsConnectionId != connectionId || flow.PodcastDeleted)
var podcast = await services.GetRequiredService<IPodcastScheduleService>()
.StartAsync(capture.Id, capture.OwnerId, ct, capture.ConnectionId);
capture.Started = now;
processCancellation.CancelAfter(TimeSpan.FromMinutes(
Math.Min(podcast.PlannedDurationMinutes ?? 1, options.Value.MaxDurationMinutes)));
}
var path = Path.Combine(directory, fields[0]);
var bytes = new FileInfo(path).Length;
if (bytes == 0 || bytes > mediaOptions.Value.MaxSegmentBytes)
throw new PodcastException(413, "Segment OBS trop volumineux ; reduisez le debit.");
total = checked(total + bytes);
await ResourceUsageTracker.RecordStorageAsync(db, capture.OwnerId, capture.OwnerId,
$"podcast/{capture.Id:N}", total, ct, write: async () =>
{
revoked = true;
throw new OperationCanceledException("OBS activation revoked or account closed.", ct);
}
if (bytes > user.GetEffectiveDiskQuota(DateTimeOffset.UtcNow) - user.DiskUsage)
throw new PodcastException(413, "Quota disque atteint.");
await using var stream = File.OpenRead(path);
await processor.AppendAsync(id, ownerId, next, end - begin, stream, ct);
user.DiskUsage = checked(user.DiskUsage + bytes);
},
policy: services.GetRequiredService<IOptions<ResourceUsageOptions>>().Value,
bandwidthMb: (decimal)bytes / (1024m * 1024m));
File.Delete(path);
next++;
var user = await db.Users.SingleAsync(u => u.Id == capture.OwnerId, ct);
await db.Entry(user).ReloadAsync(ct);
var flow = await db.LiveFlow.SingleAsync(f => f.PodcastId == capture.Id, ct);
await db.Entry(flow).ReloadAsync(ct);
if (user.WithdrawalRequestedAt is not null || flow.ObsKeyHash is null
|| flow.ObsConnectionId != capture.ConnectionId || flow.PodcastDeleted)
throw new OperationCanceledException("OBS activation revoked or account closed.", ct);
if (bytes > user.GetEffectiveDiskQuota(now) - user.DiskUsage)
throw new PodcastException(413, "Quota disque atteint.");
await using var stream = File.OpenRead(path);
await processor.AppendAsync(capture.Id, capture.OwnerId, next, end - begin, stream, ct);
user.DiskUsage = checked(user.DiskUsage + bytes);
}, policy: services.GetRequiredService<IOptions<ResourceUsageOptions>>().Value,
bandwidthMb: (decimal)bytes / (1024m * 1024m));
File.Delete(path);
lastSegment = now;
}
if (process.IsCompleted)
{
await process;
if (Volatile.Read(ref failed) || next == 0)
throw new IOException("FFmpeg SRT listener failed or produced no media.");
break;
}
await Task.Delay(100, ct);
}
await process.WaitForExitAsync(ct);
if (process.ExitCode != 0 || next == 0) throw new IOException("FFmpeg ingest failed or produced no media.");
}
catch (OperationCanceledException) when (!ct.IsCancellationRequested && !revoked)
{
throw new PodcastException(408, "Aucun segment OBS recu depuis vingt secondes ; verifiez l'encodeur et la connexion.");
}
finally
{
if (!process.HasExited) process.Kill(entireProcessTree: true);
await process.WaitForExitAsync(CancellationToken.None);
await diagnostics;
monitoring.Cancel();
try { await guard; }
catch (OperationCanceledException) when (monitoring.IsCancellationRequested) { }
processCancellation.Cancel();
try { await process; }
catch (OperationCanceledException) when (processCancellation.IsCancellationRequested) { }
// FFmpeg.NET cancels/kills without waiting for OS socket teardown.
using var released = new CancellationTokenSource(TimeSpan.FromSeconds(5));
try
{
while (PortBound(capture.Port))
await Task.Delay(25, released.Token);
}
catch (OperationCanceledException) when (released.IsCancellationRequested)
{
throw new IOException("SRT listener did not release its UDP port after cancellation.");
}
}
}
private static async Task GuardBufferAsync(string directory, int maxSegmentBytes, CancellationToken ct)
@ -245,23 +393,4 @@ public sealed class PodcastObsWorker(IServiceScopeFactory scopes, IOptions<Podca
await Task.Delay(100, ct);
}
}
private static async Task DrainAsync(StreamReader reader)
{
var buffer = new char[1024];
while (await reader.ReadAsync(buffer) != 0) { }
}
private async Task FailActivationAsync(Guid id, Guid connectionId, string message, CancellationToken ct)
{
using var scope = scopes.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
var flow = await db.LiveFlow.SingleOrDefaultAsync(f => f.PodcastId == id, ct);
if (flow is null || flow.ObsKeyHash is not null && flow.ObsConnectionId != connectionId) return;
flow.ObsKeyHash = null;
flow.ObsEnabledUntil = null;
flow.ObsConnectionId = null;
flow.ObsLastError = message;
await db.SaveChangesAsync(ct);
}
}

View file

@ -9,9 +9,8 @@ public static class PodcastServiceCollectionExtensions
services.Configure<PodcastOptions>(configuration.GetSection("Podcast"));
services.Configure<PodcastObsOptions>(configuration.GetSection("Podcast:Obs"));
services.AddScoped<IPodcastObsService, PodcastObsService>();
services.AddHttpClient<IPodcastObsRelay, PodcastObsRelay>(client => client.Timeout = TimeSpan.FromSeconds(10))
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { AllowAutoRedirect = false });
services.AddHostedService<PodcastObsWorker>();
services.AddSingleton<PodcastObsWorker>();
services.AddHostedService(provider => provider.GetRequiredService<PodcastObsWorker>());
services.AddSingleton<ILiveProcessor, LiveProcessor>();
services.AddScoped<IPodcastScheduleService, PodcastScheduleService>();
services.AddSingleton<IPodcastReplayRemuxer, FfmpegPodcastReplayRemuxer>();

View file

@ -13,6 +13,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" />
<PackageReference Include="xFFmpeg.NET" />
<ProjectReference Include="../Yavsc.Server/Yavsc.Server.csproj" />
</ItemGroup>
</Project>