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

@ -5,6 +5,8 @@
### Added ### Added
* [Podcasts/OBS/PostIt] Replace the MediaMTX relay with direct encrypted SRT ingestion managed by [FFmpeg.NET](https://github.com/cmxl/FFmpeg.NET). Reserve bounded UDP listeners on activation, display a secret SRT URL with an empty OBS stream-key field, start on received media, preserve revocation/expiry/quotas and authenticated replay, and invalidate in-memory activations after restart. Remove relay configuration and its authentication endpoint; update deployment, privacy and real-media regressions. Use separate request connections to the shared SQLite test database for reliable concurrent podcast requests.
* [Podcasts/OBS/PostIt] Add opt-in MediaMTX RTMPS ingestion for programmed emissions, activated explicitly from PostIt with one-time scoped, expiring and revocable keys. Start on publisher connection, remux H.264/AAC into the existing authenticated segments and MP4 replay workflow, bound concurrency/duration/buffering, enforce consumption and effective disk quotas, account storage with deletion refunds, and report owner-only ingest errors. Add relay configuration, installation instructions and real synthetic RTMP/RTMPS-to-replay regressions, including active revocation. * [Podcasts/OBS/PostIt] Add opt-in MediaMTX RTMPS ingestion for programmed emissions, activated explicitly from PostIt with one-time scoped, expiring and revocable keys. Start on publisher connection, remux H.264/AAC into the existing authenticated segments and MP4 replay workflow, bound concurrency/duration/buffering, enforce consumption and effective disk quotas, account storage with deletion refunds, and report owner-only ingest errors. Add relay configuration, installation instructions and real synthetic RTMP/RTMPS-to-replay regressions, including active revocation.
* [Yavsc/PostIt/Privacy] Close and pseudonymize withdrawn accounts, cancel PayPal subscriptions before confirming withdrawal, erase managed personal files, avatars and podcasts, and reject old sessions across hosts. Purge eligible technical payment residues one year after effective withdrawal automatically and through a confirmed administrator-only PostIt action; preserve accounting records separately. Clarify the privacy policy, recipients, third-party integrations, withdrawal failures and deployment responsibilities. Make the unique email constraint mutable to support pseudonymization without deleting accounting identities. * [Yavsc/PostIt/Privacy] Close and pseudonymize withdrawn accounts, cancel PayPal subscriptions before confirming withdrawal, erase managed personal files, avatars and podcasts, and reject old sessions across hosts. Purge eligible technical payment residues one year after effective withdrawal automatically and through a confirmed administrator-only PostIt action; preserve accounting records separately. Clarify the privacy policy, recipients, third-party integrations, withdrawal failures and deployment responsibilities. Make the unique email constraint mutable to support pseudonymization without deleting accounting identities.

View file

@ -5,6 +5,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageVersion Include="DotMarkdown" Version="0.3.0" /> <PackageVersion Include="DotMarkdown" Version="0.3.0" />
<PackageVersion Include="xFFmpeg.NET" Version="7.4.0" />
<PackageVersion Include="Markdig" Version="1.4.1" /> <PackageVersion Include="Markdig" Version="1.4.1" />
<PackageVersion Include="System.Security.Cryptography.Pkcs" Version="10.0.13" /> <PackageVersion Include="System.Security.Cryptography.Pkcs" Version="10.0.13" />
<PackageVersion Include="HigginsSoft.IdentityServer8" Version="$(IdentityServer8Version)" /> <PackageVersion Include="HigginsSoft.IdentityServer8" Version="$(IdentityServer8Version)" />

View file

@ -290,17 +290,14 @@ or PayPal account eligibility.
### OBS Studio for planned emissions ### OBS Studio for planned emissions
Deploy MediaMTX **v1.21.3** beside the **single Yavsc.Api media instance** and OBS publishes **encrypted SRT directly to Yavsc.Api**, using
FFmpeg on that API host. The generated configuration in [FFmpeg.NET](https://github.com/cmxl/FFmpeg.NET) (`xFFmpeg.NET`) to manage FFmpeg
[`contrib/mediamtx-podcasts.yml`](contrib/mediamtx-podcasts.yml) enables only the listeners. No MediaMTX, external media server, authentication callback or RTMPS
OBS publishing paths, RTMPS for publishers and private RTMP/control listeners. certificate is required. Install **FFmpeg with libsrt, H.264/AAC and MPEG-TS
Adjust its `authHTTPAddress` to the API's internal HTTP listener; this listener support** on the **single Yavsc.Api media host** (`ffmpeg -h protocol=srt`).
must accept the local authentication callback without redirecting it to HTTPS. OBS supports SRT in its Custom streaming service; see its
Alternatively, use an internal HTTPS URL with a valid certificate. [SRT streaming guide](https://obsproject.com/kb/srt-protocol-streaming-guide).
Install the RTMPS TLS certificate and key at the configured paths. Keep the This replaces the previous RTMP/RTMPS transport, not the authenticated playback API.
control API and internal RTMP ports on loopback (or an isolated authenticated network),
never on the public Internet. Do not enable anonymous HLS/RTMP reads on the relay.
MediaMTX must not record independent copies: Yavsc owns storage and retention.
Apply the `PodcastObsIngestion` migration on the shared database. Configure Apply the `PodcastObsIngestion` migration on the shared database. Configure
`Podcast:Obs` on **Yavsc.Api only**, for example: `Podcast:Obs` on **Yavsc.Api only**, for example:
@ -310,10 +307,10 @@ Apply the `PodcastObsIngestion` migration on the shared database. Configure
"Podcast": { "Podcast": {
"Obs": { "Obs": {
"Enabled": true, "Enabled": true,
"ServerUrl": "rtmps://obs.example.org:1936", "ServerUrl": "srt://obs.example.org",
"RelayApiUrl": "http://127.0.0.1:9997/", "ListenAddress": "0.0.0.0",
"RelayReadUrl": "rtmp://127.0.0.1:1935/", "FirstPort": 10000,
"RelayUser": "yavsc", "PortCount": 16,
"ActivationMinutes": 30, "ActivationMinutes": 30,
"MaxDurationMinutes": 120, "MaxDurationMinutes": 120,
"MaxConcurrentBroadcasts": 2 "MaxConcurrentBroadcasts": 2
@ -322,63 +319,75 @@ Apply the `PodcastObsIngestion` migration on the shared database. Configure
} }
``` ```
Set `Podcast__Obs__RelayPassword` through the deployment's secret mechanism to `ServerUrl` contains the public SRT host only, with no port, path, secret or query.
a random password of at least 32 characters. It authenticates Yavsc's private `ListenAddress` is a literal local IP address (IPv4 or IPv6); configure a matching
reader/control client, not OBS users. Keep process arguments and credentials public address family. Open/forward **UDP 10000-10015** in this example.
accessible only to the service administrator. The public RTMPS URL must have An ordinary HTTP reverse proxy does not carry SRT. NAT forwarding must preserve
no `/live` suffix: the stream key includes the full `obs/<emission-id>` path. the advertised port numbers.
Plain RTMP for the public OBS URL requires the explicit `AllowUnencryptedRtmp: true` option and should One FFmpeg process and UDP port are reserved per armed emission, even before OBS
only be used for local testing or inside a VPN. Limit publisher network bitrate connects. Armed and running emissions share `MaxConcurrentBroadcasts`; an exhausted
at the relay/network level as well as configuring OBS; application quotas are pool returns HTTP 503. The port range is bounded (at most 128 ports) and must be
checked on completed media segments, not on every incoming RTMP packet. large enough for the concurrency limit. Listener startup failure is reported,
not treated as successful activation.
Keep process arguments and credentials accessible only to the service administrator.
Limit ingress bitrate at the network level and in OBS: application quotas are checked
on completed media segments, not on every incoming packet.
In PostIt: In PostIt:
1. Prepare an emission and select it. 1. Prepare an emission and select it.
2. Choose **Activer OBS / renouveler la cle**. 2. Choose **Activer OBS / renouveler la cle**.
3. In OBS, select **Parametres > Stream > Service : Personnalise**, then copy 3. In OBS, select **Parametres > Stream > Service : Personnalise**, then copy
the displayed server and stream key. Use **H.264 video**, **AAC audio**, the complete displayed **secret SRT URL as Server**, leaving **Stream Key empty**.
The URL includes caller mode, a random passphrase and enforced AES-256 encryption.
Use **H.264 video**, **AAC audio**,
a **2-second keyframe interval** and a bitrate producing segments below 8 MiB. a **2-second keyframe interval** and a bitrate producing segments below 8 MiB.
4. Start streaming in OBS before the activation expires. The actual emission 4. Start streaming in OBS before the activation expires. The actual emission
starts when the publisher connects, even if the scheduled date is later. starts when the first complete media segment arrives, even if the scheduled date is later.
No camera capture in PostIt is required; PostIt may be closed afterwards. No camera capture in PostIt is required; PostIt may be closed afterwards.
5. Stop OBS or choose **Revoquer OBS et arreter le diffuseur** in PostIt. 5. Stop OBS or choose **Revoquer OBS et arreter le diffuseur** in PostIt.
Committed segments remain available and the normal MP4 worker prepares replay. Committed segments remain available and the normal MP4 worker prepares replay.
Keys are scoped to one emission, stored only as SHA-256 hashes, displayed once, Passphrases are scoped to one emission, displayed once and rotated on activation.
rotated on each activation and erased after completion. PostIt never persists Only a SHA-256 hash and lifecycle metadata are stored in the database; FFmpeg needs
them; navigation/logout clears the displayed key but does not revoke a running the actual passphrase in memory/process arguments for SRT encryption. It is never
included in status responses or persisted in PostIt. Navigation/logout clears
the displayed secret URL but does not revoke a running
external emission. Revocation is available at the consumption limit. external emission. Revocation is available at the consumption limit.
After disconnect, restart or timeout, the emission ends rather than silently After disconnect, restart or timeout, the emission ends rather than silently
resuming under a new publisher: prepare a new emission for another broadcast. resuming under a new publisher: prepare a new emission for another broadcast.
An API restart invalidates all old activations; an unstarted emission must be
activated again because its passphrase is not recoverable from the database.
An invalid handshake can end an armed listener; no podcast is created without
media, and the owner can activate again. Rate-limit connection attempts at deployment.
The worker polls the authenticated relay once a second and starts bounded FFmpeg The worker manages bounded FFmpeg.NET listener/remuxing tasks. It rejects ended,
remuxing tasks. It rejects ended, deleted, revoked or withdrawn accounts and caps deleted, revoked or withdrawn accounts and caps
concurrent captures and duration (the lower of planned duration and configured concurrent captures and duration (the lower of planned duration and configured
maximum). MediaMTX also receives a session expiry from authentication, so the maximum). Revocation cancels the corresponding FFmpeg process and frees its port.
publisher is disconnected at that maximum lifetime even if the API stops. Activation expiry stops unused listeners. The passphrase is required before SRT
media can be received; a stream name alone is not an authentication mechanism.
Committed segments are charged as transferred and stored MiB under the same Committed segments are charged as transferred and stored MiB under the same
consumption rule; their storage receipts are refunded on podcast deletion. consumption rule; their storage receipts are refunded on podcast deletion.
OBS storage counts towards the user's effective disk quota and the existing OBS storage counts towards the user's effective disk quota and the existing
podcast media limits. Pending capture files are monitored and bounded; enforcement podcast media limits. Pending capture files are monitored and bounded; enforcement
can overshoot by one polling interval of incoming data. Processing errors are can overshoot by one polling interval of incoming data. Processing errors are
reported in the owner-only OBS status endpoint, without exposing keys or FFmpeg reported in the owner-only OBS status endpoint, without exposing secret URLs or
diagnostics. Monitor scan/cleanup failures and do not restore old activation keys FFmpeg diagnostics. Monitor process/cleanup failures and drain active captures
from backups. A prolonged relay/API outage requires operational intervention. before maintenance. An API outage ends ingestion and requires reactivation.
The normal API listens/authenticates playback; OBS stream credentials never grant The normal HTTP API authenticates playback; OBS stream credentials never grant
listener access. If the relay is separately hosted, include it in the deployment's viewer or account access.
privacy/provider information.
For the real media regression, set `YAVSC_TEST_MEDIAMTX` to the installed MediaMTX For the real media regressions, install FFmpeg with libsrt and run:
v1.21.3 executable and run:
```bash ```bash
dotnet test --project src/test/Yavsc.Api.Tests/Yavsc.Api.Tests.csproj --filter-class '*PodcastObsTests' dotnet test --project src/test/Yavsc.Api.Tests/Yavsc.Api.Tests.csproj --filter-class '*PodcastObsTests'
``` ```
The test launches loopback-only listeners, generates synthetic H.264/AAC media The tests launch loopback-only listeners, generate synthetic H.264/AAC media
with FFmpeg, verifies RTMP/RTMPS authentication, revocation during streaming, with FFmpeg, verify encrypted SRT through FFmpeg.NET, wrong-passphrase rejection,
revocation during streaming,
AAC audio, HLS segments, MP4 replay and refund, AAC audio, HLS segments, MP4 replay and refund,
then stops its own processes and removes its temporary files. then stops its own processes and removes its temporary files.

View file

@ -1,26 +0,0 @@
# MediaMTX v1.21.3. Run beside the single Yavsc.Api media host.
# Replace the auth URL port and TLS certificate paths for the deployment.
logLevel: warn
authMethod: http
authHTTPAddress: http://127.0.0.1:5103/api/v1/podcasts/relay/auth
authHTTPExclude: []
api: true
apiAddress: 127.0.0.1:9997
rtsp: false
rtmp: true
rtmpEncryption: optional
rtmpAddress: 127.0.0.1:1935
rtmpsAddress: :1936
rtmpServerKey: /etc/yavsc/obs/server.key
rtmpServerCert: /etc/yavsc/obs/server.crt
hls: false
webrtc: false
srt: false
playback: false
pathDefaults:
source: publisher
overridePublisher: false
maxReaders: 1
record: false
paths:
"~^obs/[0-9a-f]{32}$":

View file

@ -27,14 +27,16 @@ public sealed class PodcastScheduleTests
var activation = vm.ActivateObsCommand.ExecuteAsync(null); var activation = vm.ActivateObsCommand.ExecuteAsync(null);
vm.SelectedPodcast = api.Plan with { Id = Guid.NewGuid() }; vm.SelectedPodcast = api.Plan with { Id = Guid.NewGuid() };
vm.SelectedPodcast = api.Plan; vm.SelectedPodcast = api.Plan;
api.PendingActivation.SetResult(new("rtmps://obs.example:1936", "secret", DateTimeOffset.UtcNow.AddMinutes(30))); api.PendingActivation.SetResult(new("srt://obs.example:10000?passphrase=secret", "", DateTimeOffset.UtcNow.AddMinutes(30)));
await activation; await activation;
Assert.Empty(vm.ObsServerUrl);
Assert.Empty(vm.ObsStreamKey); Assert.Empty(vm.ObsStreamKey);
api.PendingActivation = new(TaskCreationOptions.RunContinuationsAsynchronously); api.PendingActivation = new(TaskCreationOptions.RunContinuationsAsynchronously);
activation = vm.ActivateObsCommand.ExecuteAsync(null); activation = vm.ActivateObsCommand.ExecuteAsync(null);
await vm.DeactivateAsync(); await vm.DeactivateAsync();
api.PendingActivation.SetResult(new("rtmps://obs.example:1936", "secret", DateTimeOffset.UtcNow.AddMinutes(30))); api.PendingActivation.SetResult(new("srt://obs.example:10000?passphrase=secret", "", DateTimeOffset.UtcNow.AddMinutes(30)));
await activation; await activation;
Assert.Empty(vm.ObsServerUrl);
Assert.Empty(vm.ObsStreamKey); Assert.Empty(vm.ObsStreamKey);
} }
finally { File.Delete(path); File.Delete(path + ".settings"); } finally { File.Delete(path); File.Delete(path + ".settings"); }
@ -56,17 +58,19 @@ public sealed class PodcastScheduleTests
vm.SelectedPodcast = api.Plan; vm.SelectedPodcast = api.Plan;
Assert.True(vm.ActivateObsCommand.CanExecute(null)); Assert.True(vm.ActivateObsCommand.CanExecute(null));
await vm.ActivateObsCommand.ExecuteAsync(null); await vm.ActivateObsCommand.ExecuteAsync(null);
Assert.Equal("rtmps://obs.example:1936", vm.ObsServerUrl); Assert.Equal("srt://obs.example:10000?passphrase=stream-secret", vm.ObsServerUrl);
Assert.Equal("obs/stream-secret", vm.ObsStreamKey); Assert.Empty(vm.ObsStreamKey);
Assert.False(vm.IsBroadcasting); Assert.False(vm.IsBroadcasting);
api.Settings.Save(); api.Settings.Save();
Assert.DoesNotContain("stream-secret", await File.ReadAllTextAsync(path + ".settings")); Assert.DoesNotContain("stream-secret", await File.ReadAllTextAsync(path + ".settings"));
vm.SelectedPodcast = api.Plan with { Id = Guid.NewGuid(), OwnerId = "other" }; vm.SelectedPodcast = api.Plan with { Id = Guid.NewGuid(), OwnerId = "other" };
Assert.Empty(vm.ObsServerUrl);
Assert.Empty(vm.ObsStreamKey); Assert.Empty(vm.ObsStreamKey);
Assert.False(vm.ActivateObsCommand.CanExecute(null)); Assert.False(vm.ActivateObsCommand.CanExecute(null));
vm.SelectedPodcast = api.Plan; vm.SelectedPodcast = api.Plan;
await vm.ActivateObsCommand.ExecuteAsync(null); await vm.ActivateObsCommand.ExecuteAsync(null);
await vm.DeactivateAsync(); await vm.DeactivateAsync();
Assert.Empty(vm.ObsServerUrl);
Assert.Empty(vm.ObsStreamKey); Assert.Empty(vm.ObsStreamKey);
Assert.True(api.ObsEnabled); Assert.True(api.ObsEnabled);
await vm.ActivateAsync(); await vm.ActivateAsync();
@ -74,6 +78,7 @@ public sealed class PodcastScheduleTests
await vm.ActivateObsCommand.ExecuteAsync(null); await vm.ActivateObsCommand.ExecuteAsync(null);
await vm.DeactivateObsCommand.ExecuteAsync(null); await vm.DeactivateObsCommand.ExecuteAsync(null);
Assert.False(api.ObsEnabled); Assert.False(api.ObsEnabled);
Assert.Empty(vm.ObsServerUrl);
Assert.Empty(vm.ObsStreamKey); Assert.Empty(vm.ObsStreamKey);
Assert.EndsWith("/obs", api.RevokedPath); Assert.EndsWith("/obs", api.RevokedPath);
await vm.DeactivateAsync(); await vm.DeactivateAsync();
@ -156,7 +161,7 @@ public sealed class PodcastScheduleTests
{ {
ObsEnabled = true; ObsEnabled = true;
response = PendingActivation is null response = PendingActivation is null
? new PodcastObsCredentials("rtmps://obs.example:1936", "obs/stream-secret", DateTimeOffset.UtcNow.AddMinutes(30)) ? new PodcastObsCredentials("srt://obs.example:10000?passphrase=stream-secret", "", DateTimeOffset.UtcNow.AddMinutes(30))
: await PendingActivation.Task; : await PendingActivation.Task;
} }
else response = new PodcastObsStatus(ObsEnabled, DateTimeOffset.UtcNow.AddMinutes(30), null); else response = new PodcastObsStatus(ObsEnabled, DateTimeOffset.UtcNow.AddMinutes(30), null);

View file

@ -307,7 +307,7 @@ public partial class PodcastsPageViewModel : ViewModelBase
if (SelectedPodcast is { FlowId: not null } selected && selected.OwnerId == _ownerId) if (SelectedPodcast is { FlowId: not null } selected && selected.OwnerId == _ownerId)
{ {
var obs = await _client.ObsStatusAsync(selected.Id); var obs = await _client.ObsStatusAsync(selected.Id);
ObsStatus = obs.Error ?? (obs.Enabled ? "Entree OBS activee. Le direct commence a la connexion du diffuseur." : "Entree OBS desactivee."); ObsStatus = obs.Error ?? (obs.Enabled ? "Entree SRT activee. Le direct commence a la reception des premiers medias." : "Entree OBS desactivee.");
if (!obs.Enabled) ClearObsCredentials(clearStatus: false); if (!obs.Enabled) ClearObsCredentials(clearStatus: false);
} }
Status = "Directs et replays reserves aux utilisateurs connectes."; Status = "Directs et replays reserves aux utilisateurs connectes.";
@ -410,7 +410,7 @@ public partial class PodcastsPageViewModel : ViewModelBase
_obsPodcastId = podcast.Id; _obsPodcastId = podcast.Id;
ObsServerUrl = credentials.ServerUrl; ObsServerUrl = credentials.ServerUrl;
ObsStreamKey = credentials.StreamKey; ObsStreamKey = credentials.StreamKey;
ObsStatus = $"Connectez OBS avant {credentials.ExpiresAt.ToLocalTime():g}. Cette cle remplace toute cle precedente."; ObsStatus = $"Copiez l'URL SRT secrete dans OBS avant {credentials.ExpiresAt.ToLocalTime():g}, avec la cle de stream vide. Cette URL remplace la precedente.";
} }
catch (Exception ex) { Status = $"Activation OBS impossible : {ex.Message}"; } catch (Exception ex) { Status = $"Activation OBS impossible : {ex.Message}"; }

View file

@ -99,16 +99,14 @@
<Button Content="Arreter et publier le replay" Command="{Binding StopCommand}" /> <Button Content="Arreter et publier le replay" Command="{Binding StopCommand}" />
<StackPanel Spacing="8"> <StackPanel Spacing="8">
<TextBlock Text="Diffuser l'emission programmee avec OBS Studio" FontWeight="SemiBold" /> <TextBlock Text="Diffuser l'emission programmee avec OBS Studio" FontWeight="SemiBold" />
<TextBlock Text="Selectionnez votre emission puis activez OBS. Dans OBS : service Personnalise, video H.264, audio AAC, images cles toutes les 2 secondes. Le direct commence lorsque OBS se connecte, pas a l'heure prevue." <TextBlock Text="Selectionnez votre emission puis activez OBS. Dans OBS : service Personnalise, URL SRT ci-dessous, cle de stream vide, video H.264, audio AAC, images cles toutes les 2 secondes. Le direct commence a la reception des premiers medias, pas a l'heure prevue."
TextWrapping="Wrap" /> TextWrapping="Wrap" />
<WrapPanel> <WrapPanel>
<Button Content="Activer OBS / renouveler la cle" Command="{Binding ActivateObsCommand}" Margin="0,0,8,8" /> <Button Content="Activer OBS / renouveler la cle" Command="{Binding ActivateObsCommand}" Margin="0,0,8,8" />
<Button Content="Revoquer OBS et arreter le diffuseur" Command="{Binding DeactivateObsCommand}" /> <Button Content="Revoquer OBS et arreter le diffuseur" Command="{Binding DeactivateObsCommand}" />
</WrapPanel> </WrapPanel>
<TextBlock Text="Serveur OBS" /> <TextBlock Text="URL SRT secrete (ne pas partager)" />
<TextBox Text="{Binding ObsServerUrl, Mode=OneWay}" IsReadOnly="True" /> <TextBox Text="{Binding ObsServerUrl, Mode=OneWay}" IsReadOnly="True" />
<TextBlock Text="Cle de stream (secrete : ne pas partager)" />
<TextBox Text="{Binding ObsStreamKey, Mode=OneWay}" IsReadOnly="True" />
<TextBlock Text="{Binding ObsStatus}" TextWrapping="Wrap" /> <TextBlock Text="{Binding ObsStatus}" TextWrapping="Wrap" />
<TextBlock Text="Les identifiants ne sont pas sauvegardes dans PostIt. Quitter la page masque la cle, mais n'arrete pas OBS : utilisez la revocation ou arretez OBS." <TextBlock Text="Les identifiants ne sont pas sauvegardes dans PostIt. Quitter la page masque la cle, mais n'arrete pas OBS : utilisez la revocation ou arretez OBS."
TextWrapping="Wrap" /> TextWrapping="Wrap" />

View file

@ -63,18 +63,3 @@ public sealed record PodcastObsCredentials(string ServerUrl, string StreamKey, D
/// <summary>Owner-only state of the external broadcasting activation.</summary> /// <summary>Owner-only state of the external broadcasting activation.</summary>
public sealed record PodcastObsStatus(bool Enabled, DateTimeOffset? ExpiresAt, string? Error); public sealed record PodcastObsStatus(bool Enabled, DateTimeOffset? ExpiresAt, string? Error);
/// <summary>MediaMTX external authentication request; credentials are never logged or echoed.</summary>
public sealed record PodcastRelayAuthentication
{
public string User { get; init; } = "";
public string Password { get; init; } = "";
public string Action { get; init; } = "";
public string Path { get; init; } = "";
public string Protocol { get; init; } = "";
public string Query { get; init; } = "";
public string Id { get; init; } = "";
}
/// <summary>Maximum lifetime of an authenticated media relay session.</summary>
public sealed record PodcastRelayAuthorization(DateTimeOffset? ExpireAt);

View file

@ -44,14 +44,4 @@ public sealed class PodcastObsController(IPodcastObsService obs) : ControllerBas
return NoContent(); 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) ### OpenAPI in development (requires an Administrator token)
GET {{baseUrl}}/openapi/podcasts.json GET {{baseUrl}}/openapi/podcasts.json
Authorization: Bearer {{token}} 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 POST {{baseUrl}}/api/v1/podcasts/{{id}}/obs
Authorization: Bearer {{token}} Authorization: Bearer {{token}}
@ -94,5 +94,5 @@ DELETE {{baseUrl}}/api/v1/podcasts/{{id}}/obs
Authorization: Bearer {{token}} Authorization: Bearer {{token}}
# Activation: 403 other owner/closed account, 404 missing emission, # Activation: 403 other owner/closed account, 404 missing emission,
# 409 already started, 413 disk full, 429 consumption limit, 503 OBS disabled. # 409 already started, 413 disk full, 429 consumption limit,
# relay/auth is called by MediaMTX, not by the end-user. # 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 bool Enabled { get; set; }
public string ServerUrl { get; set; } = ""; public string ServerUrl { get; set; } = "";
public string RelayApiUrl { get; set; } = "http://127.0.0.1:9997/"; public string ListenAddress { get; set; } = "0.0.0.0";
public string RelayReadUrl { get; set; } = "rtmp://127.0.0.1:1935/"; public int FirstPort { get; set; } = 10000;
public string RelayUser { get; set; } = "yavsc"; public int PortCount { get; set; } = 16;
public string RelayPassword { get; set; } = "";
public int ActivationMinutes { get; set; } = 30; public int ActivationMinutes { get; set; } = 30;
public int MaxDurationMinutes { get; set; } = 120; public int MaxDurationMinutes { get; set; } = 120;
public int MaxConcurrentBroadcasts { get; set; } = 2; public int MaxConcurrentBroadcasts { get; set; } = 2;
public bool AllowUnencryptedRtmp { get; set; }
public void Validate() public void Validate()
{ {
if (!Enabled) return; if (!Enabled) return;
if (!Uri.TryCreate(ServerUrl, UriKind.Absolute, out var server) 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.UserInfo.Length != 0 || server.Query.Length != 0 || server.Fragment.Length != 0
|| server.AbsolutePath is not ("" or "/") || server.AbsolutePath is not ("" or "/")
|| !Uri.TryCreate(RelayApiUrl, UriKind.Absolute, out var api) || api.Scheme is not ("http" or "https") || !System.Net.IPAddress.TryParse(ListenAddress, out _)
|| api.UserInfo.Length != 0 || api.Query.Length != 0 || api.Fragment.Length != 0 || FirstPort is < 1024 or > 65535 || PortCount is < 1 or > 128
|| !Uri.TryCreate(RelayReadUrl, UriKind.Absolute, out var read) || read.Scheme != "rtmp" || (long)FirstPort + PortCount - 1 > 65535
|| read.UserInfo.Length != 0 || read.Query.Length != 0 || read.Fragment.Length != 0
|| read.AbsolutePath is not ("" or "/")
|| RelayPassword.Length < 32 || string.IsNullOrWhiteSpace(RelayUser)
|| ActivationMinutes is < 1 or > 1440 || MaxDurationMinutes is < 1 or > 1440 || ActivationMinutes is < 1 or > 1440 || MaxDurationMinutes is < 1 or > 1440
|| MaxConcurrentBroadcasts is < 1 or > 16) || MaxConcurrentBroadcasts is < 1 or > 16 || MaxConcurrentBroadcasts > PortCount)
throw new InvalidOperationException("Invalid Podcast:Obs relay configuration. Use RTMPS, private relay addresses and a random password of at least 32 characters."); 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 #nullable enable
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Yavsc.Abstract.Streaming; using Yavsc.Abstract.Streaming;
@ -18,43 +17,67 @@ public interface IPodcastObsService
Task<PodcastObsCredentials> ActivateAsync(Guid id, string ownerId, CancellationToken ct); Task<PodcastObsCredentials> ActivateAsync(Guid id, string ownerId, CancellationToken ct);
Task<PodcastObsStatus> StatusAsync(Guid id, string ownerId, CancellationToken ct); Task<PodcastObsStatus> StatusAsync(Guid id, string ownerId, CancellationToken ct);
Task DeactivateAsync(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, 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 ILogger<PodcastObsService> logger) : IPodcastObsService
{ {
private PodcastObsOptions Settings() public async Task<PodcastObsCredentials> ActivateAsync(Guid id, string ownerId, CancellationToken ct)
{ {
var value = options.Value; var settings = options.Value;
if (!value.Enabled) throw new PodcastException(503, "L'entree OBS n'est pas configuree sur cette instance."); if (!settings.Enabled) throw new PodcastException(503, "L'entree OBS n'est pas configuree sur cette instance.");
value.Validate(); settings.Validate();
return value; await worker.WaitUntilReadyAsync(ct);
} await worker.Lifecycle.WaitAsync(ct);
try
public Task<PodcastObsCredentials> ActivateAsync(Guid id, string ownerId, CancellationToken ct)
=> ResourceUsageTracker.WithUserLockAsync(db, ownerId, async () =>
{ {
var settings = Settings(); await ResourceUsageTracker.WithUserLockAsync(db, ownerId, async () =>
var flow = await OwnedAsync(id, ownerId, ct); {
if (flow.StartedAt is not null) var flow = await EligibleAsync(id, ownerId, ct);
throw new PodcastException(409, "Cette emission a deja demarre. Preparez une nouvelle emission."); flow.ObsKeyHash = null;
if (flow.Owner.WithdrawalRequestedAt is not null) flow.ObsEnabledUntil = null;
throw new PodcastException(403, "Le compte est ferme."); flow.ObsConnectionId = null;
if (await QuotaReachedAsync(ownerId, ct)) await db.SaveChangesAsync(ct);
throw new PodcastException(429, "Le seuil de consommation est atteint."); return true;
if (flow.Owner.DiskUsage >= flow.Owner.GetEffectiveDiskQuota(clock.GetUtcNow())) }, ct);
throw new PodcastException(413, "Le quota disque est atteint."); await worker.StopCaptureAsync(id, ct);
var key = Convert.ToHexString(RandomNumberGenerator.GetBytes(32)).ToLowerInvariant(); return await ResourceUsageTracker.WithUserLockAsync(db, ownerId, async () =>
flow.ObsKeyHash = Hash(key); {
flow.ObsEnabledUntil = clock.GetUtcNow().AddMinutes(settings.ActivationMinutes); var flow = await EligibleAsync(id, ownerId, ct);
flow.ObsConnectionId = null; var key = Convert.ToHexString(RandomNumberGenerator.GetBytes(32)).ToLowerInvariant();
flow.ObsLastError = null; var connectionId = Guid.NewGuid();
await db.SaveChangesAsync(ct); var expires = clock.GetUtcNow().AddMinutes(settings.ActivationMinutes);
return new PodcastObsCredentials(settings.ServerUrl.TrimEnd('/'), flow.ObsKeyHash = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(key)));
$"obs/{id:N}?key={key}", flow.ObsEnabledUntil.Value); flow.ObsEnabledUntil = expires;
}, ct); 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) 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) 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); await ResourceUsageTracker.WithUserLockAsync(db, ownerId, async () =>
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))
{ {
var flow = await db.LiveFlow.AsNoTracking().Include(f => f.Owner) var flow = await OwnedAsync(id, ownerId, ct);
.SingleOrDefaultAsync(f => f.PodcastId == id && !f.PodcastDeleted, ct); flow.ObsKeyHash = null;
if (flow is not null) flow.ObsEnabledUntil = null;
return await ResourceUsageTracker.WithUserLockAsync<PodcastRelayAuthorization?>(db, flow.OwnerId, async () => flow.ObsConnectionId = null;
{ await db.SaveChangesAsync(ct);
var current = await db.LiveFlow.Include(f => f.Owner).SingleAsync(f => f.PodcastId == id, ct); return true;
await db.Entry(current).ReloadAsync(ct); }, ct);
await db.Entry(current.Owner).ReloadAsync(ct); await worker.StopCaptureAsync(id, 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);
}
} }
logger.LogWarning("OBS relay authentication refused for action {Action}", request.Action is "publish" or "read" or "api" ? request.Action : "other"); finally { worker.Lifecycle.Release(); }
return null;
} }
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) var record = await db.ResourceUsageRecords.AsNoTracking().Where(r => r.UserId == ownerId)
.OrderByDescending(r => r.RecordedAtUtc).ThenByDescending(r => r.Id).FirstOrDefaultAsync(ct); .OrderByDescending(r => r.RecordedAtUtc).ThenByDescending(r => r.Id).FirstOrDefaultAsync(ct);
var policy = usageOptions.Value; var policy = usageOptions.Value;
var gross = record is null ? 0m : policy.Calculate(record.ApiCalls, record.CpuSeconds, record.BandwidthMb, record.StorageMb); 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) 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."); if (flow.OwnerId != ownerId) throw new PodcastException(403, "Seul l'auteur peut activer OBS.");
return flow; 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 #nullable enable
using System.Diagnostics;
using System.Globalization; using System.Globalization;
using System.Net;
using System.Net.Sockets;
using FFmpeg.NET;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Yavsc.Models; using Yavsc.Models;
@ -11,222 +13,368 @@ using Yavsc.Settings;
namespace Yavsc.Api.Services; namespace Yavsc.Api.Services;
public sealed class PodcastObsWorker(IServiceScopeFactory scopes, IOptions<PodcastObsOptions> options, 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 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) protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{ {
if (!options.Value.Enabled) return; stopping = stoppingToken;
options.Value.Validate();
try 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) while (!stoppingToken.IsCancellationRequested)
{ {
try try
{ {
using var scope = scopes.CreateScope(); using var scope = scopes.CreateScope();
var relay = scope.ServiceProvider.GetRequiredService<IPodcastObsRelay>();
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
foreach (var pair in captures.Where(c => c.Value.Task.IsCompleted).ToArray()) Capture[] active;
{ lock (gate) active = captures.Values.ToArray();
captures.Remove(pair.Key); foreach (var capture in active)
pair.Value.Cancellation.Dispose();
await pair.Value.Task;
}
foreach (var publisher in await relay.PublishersAsync(stoppingToken))
{ {
var flow = await db.LiveFlow.AsNoTracking().Include(f => f.Owner) var flow = await db.LiveFlow.AsNoTracking().Include(f => f.Owner)
.SingleOrDefaultAsync(f => f.PodcastId == publisher.PodcastId, stoppingToken); .SingleOrDefaultAsync(f => f.PodcastId == capture.Id, stoppingToken);
var allowed = flow is { PodcastDeleted: false, ObsKeyHash: not null } lock (gate)
&& flow.ObsConnectionId == publisher.ConnectionId
&& flow.Owner.WithdrawalRequestedAt is null;
if (!allowed)
{ {
if (captures.TryGetValue(publisher.PodcastId, out var running)) running.Cancellation.Cancel(); if (!captures.ContainsKey(capture.Id)) continue;
await relay.KickAsync(publisher.PodcastId, stoppingToken, publisher.ConnectionId); if (capture.Completion.Task.IsCompleted)
continue; {
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 (OperationCanceledException) when (stoppingToken.IsCancellationRequested) { break; }
catch (Exception ex) catch (Exception ex) { logger.LogError(ex, "OBS listener state check failed; retrying"); }
{ await Task.Delay(250, stoppingToken);
logger.LogError(ex, "OBS relay scan failed; retrying");
}
await Task.Delay(TimeSpan.FromSeconds(1), stoppingToken);
} }
} }
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested) { } catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested) { }
catch (Exception ex) { ready.TrySetException(ex); throw; }
finally finally
{ {
foreach (var capture in captures.Values) capture.Cancellation.Cancel(); Capture[] active;
await Task.WhenAll(captures.Values.Select(c => c.Task)); lock (gate)
foreach (var capture in captures.Values) capture.Cancellation.Dispose(); {
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(); stopping.ThrowIfCancellationRequested();
var services = scope.ServiceProvider; Capture capture;
var db = services.GetRequiredService<ApplicationDbContext>(); lock (gate)
var processor = services.GetRequiredService<ILiveProcessor>(); {
var relay = services.GetRequiredService<IPodcastObsRelay>(); foreach (var ended in captures.Values.Where(c => c.Completion.Task.IsCompleted).ToArray())
var media = services.GetRequiredService<IOptions<PodcastOptions>>().Value; {
var directory = Path.Combine(media.RootPath, "obs-capture-" + id.ToString("N")); 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; string? error = null;
var started = false;
try try
{ {
Directory.CreateDirectory(directory); Directory.CreateDirectory(directory);
var podcast = await services.GetRequiredService<IPodcastScheduleService>().StartAsync(id, ownerId, ct, connectionId); using var scope = scopes.CreateScope();
started = true; using var receive = CancellationTokenSource.CreateLinkedTokenSource(capture.Cancellation.Token);
using var deadline = CancellationTokenSource.CreateLinkedTokenSource(ct); var guard = GuardBufferAsync(directory, media.MaxSegmentBytes, receive.Token);
deadline.CancelAfter(TimeSpan.FromMinutes(Math.Min(podcast.PlannedDurationMinutes ?? 1, options.Value.MaxDurationMinutes))); var ingest = ReceiveAsync(capture, key, directory, scope.ServiceProvider, receive.Token);
await ReceiveAsync(id, ownerId, connectionId, directory, db, processor, media, services, deadline.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) 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) catch (Exception ex)
{ {
// FFmpeg diagnostics and request URLs can contain credentials: never persist or log them. error = ex is PodcastException or ResourceUsageLimitException ? ex.Message
error = ex is PodcastException or ResourceUsageLimitException ? ex.Message : "Reception OBS impossible. Verifiez le relais, FFmpeg et l'encodage H.264/AAC."; : "Reception SRT impossible. Verifiez FFmpeg, la connexion et l'encodage H.264/AAC.";
logger.LogError("OBS capture {PodcastId} failed ({ErrorType})", id, ex.GetType().Name); logger.LogError("OBS capture {PodcastId} failed ({ErrorType})", capture.Id, ex.GetType().Name);
} }
finally finally
{ {
capture.Error = error;
try try
{ {
using var scope = scopes.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(20)); using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(20));
await relay.KickAsync(id, timeout.Token, connectionId); await ResourceUsageTracker.WithUserLockAsync(db, capture.OwnerId, async () =>
db.ChangeTracker.Clear();
var flow = await db.LiveFlow.Include(f => f.Owner).SingleOrDefaultAsync(f => f.PodcastId == id, timeout.Token);
if (flow is not null)
{ {
if (flow.ObsKeyHash is null && flow.ObsConnectionId is null) error = null; var flow = await db.LiveFlow.Include(f => f.Owner)
if (started && flow.Owner.WithdrawalRequestedAt is null) .SingleOrDefaultAsync(f => f.PodcastId == capture.Id, timeout.Token);
await processor.CompleteAsync(id, ownerId, timeout.Token); if (flow is not null)
if (flow.ObsConnectionId == connectionId || flow.ObsKeyHash is null)
{ {
flow.ObsKeyHash = null; if (capture.Started is not null && flow.Owner.WithdrawalRequestedAt is null)
flow.ObsEnabledUntil = null; {
flow.ObsConnectionId = null; try
flow.ObsLastError = error; {
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) catch (Exception ex)
{ {
logger.LogError("OBS capture {PodcastId} cleanup failed ({ErrorType}); operator intervention required", id, ex.GetType().Name); logger.LogError("OBS capture {PodcastId} finalization failed ({ErrorType}); operator intervention required",
await FailActivationAsync(id, connectionId, "Finalisation OBS impossible ; intervention de l'administration requise.", CancellationToken.None); 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, private async Task ReceiveAsync(Capture capture, string key, string directory, IServiceProvider services, CancellationToken ct)
ILiveProcessor processor, PodcastOptions media, IServiceProvider services, CancellationToken ct)
{ {
var settings = options.Value; var input = new UriBuilder("srt", options.Value.ListenAddress, capture.Port)
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)
{ {
UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, Query = $"mode=listener&passphrase={key}&pbkeylen=32&enforced_encryption=1"
CreateNoWindow = true, WorkingDirectory = directory + $"&listen_timeout={options.Value.ActivationMinutes * 60000000L}&timeout=20000000&latency=200000"
}; };
foreach (var argument in new[] var engine = new Engine(mediaOptions.Value.FfmpegPath);
{ var failed = false;
"-nostdin", "-hide_banner", "-loglevel", "error", "-rw_timeout", "15000000", engine.Error += (_, _) => Volatile.Write(ref failed, true);
"-i", input.Uri.AbsoluteUri, "-map", "0:v:0", "-map", "0:a:0?", "-c", "copy", using var processCancellation = CancellationTokenSource.CreateLinkedTokenSource(ct);
"-f", "segment", "-segment_format", "mpegts", "-segment_time", "2", var process = engine.ExecuteAsync(
"-segment_list", "pipe:1", "-segment_list_type", "csv", "segment-%08d.ts" $"-nostdin -hide_banner -loglevel error -i \"{input.Uri.AbsoluteUri}\" -map 0:v:0 -map 0:a:0? -c copy"
}) start.ArgumentList.Add(argument); + " -f segment -segment_format mpegts -segment_time 2 -segment_list segments.csv"
using var process = Process.Start(start) ?? throw new IOException("FFmpeg could not start."); + " -segment_list_type csv segment-%08d.ts", directory, processCancellation.Token);
var diagnostics = DrainAsync(process.StandardError); var db = services.GetRequiredService<ApplicationDbContext>();
using var monitoring = CancellationTokenSource.CreateLinkedTokenSource(ct); var processor = services.GetRequiredService<ILiveProcessor>();
var guard = GuardBufferAsync(directory, media.MaxSegmentBytes, monitoring.Token);
var next = 0; var next = 0;
var total = 0L; var total = 0L;
var revoked = false; var lastSegment = clock.GetUtcNow();
try try
{ {
while (true) while (true)
{ {
using var idle = CancellationTokenSource.CreateLinkedTokenSource(ct); ct.ThrowIfCancellationRequested();
idle.CancelAfter(TimeSpan.FromSeconds(20)); var now = clock.GetUtcNow();
var read = process.StandardOutput.ReadLineAsync(idle.Token).AsTask(); if (capture.Started is null && now >= capture.Expires)
if (await Task.WhenAny(read, guard) == guard) await guard; throw new PodcastException(408, "Activation OBS expiree ; obtenez une nouvelle URL SRT.");
var line = await read; if (capture.Started is not null)
if (line is null) break; {
var fields = line.Split(','); if (now - capture.Started >= TimeSpan.FromMinutes(options.Value.MaxDurationMinutes))
if (fields.Length != 3 || fields[0] != $"segment-{next:D8}.ts" break;
|| !double.TryParse(fields[1], CultureInfo.InvariantCulture, out var begin) if (now - lastSegment > TimeSpan.FromSeconds(20))
|| !double.TryParse(fields[2], CultureInfo.InvariantCulture, out var end) throw new PodcastException(408, "Aucun segment OBS recu depuis vingt secondes ; verifiez la connexion.");
|| !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 playlist = Path.Combine(directory, "segments.csv");
var path = Path.Combine(directory, fields[0]); var text = File.Exists(playlist) ? await File.ReadAllTextAsync(playlist, ct) : "";
var bytes = new FileInfo(path).Length; var lines = text.Split('\n');
if (bytes == 0 || bytes > media.MaxSegmentBytes) for (; next < lines.Length - 1; next++)
throw new PodcastException(413, "Segment OBS trop volumineux ; reduisez le debit et l'intervalle des images cles."); {
total = checked(total + bytes); if (next >= mediaOptions.Value.MaxSegments) throw new PodcastException(413, "Nombre maximal de segments atteint.");
await ResourceUsageTracker.RecordStorageAsync(db, ownerId, ownerId, $"podcast/{id:N}", total, ct, var fields = lines[next].TrimEnd('\r').Split(',');
write: async () => 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); var podcast = await services.GetRequiredService<IPodcastScheduleService>()
await db.Entry(user).ReloadAsync(ct); .StartAsync(capture.Id, capture.OwnerId, ct, capture.ConnectionId);
var flow = await db.LiveFlow.SingleAsync(f => f.PodcastId == id, ct); capture.Started = now;
await db.Entry(flow).ReloadAsync(ct); processCancellation.CancelAfter(TimeSpan.FromMinutes(
if (user.WithdrawalRequestedAt is not null || flow.ObsKeyHash is null Math.Min(podcast.PlannedDurationMinutes ?? 1, options.Value.MaxDurationMinutes)));
|| flow.ObsConnectionId != connectionId || flow.PodcastDeleted) }
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; var user = await db.Users.SingleAsync(u => u.Id == capture.OwnerId, ct);
throw new OperationCanceledException("OBS activation revoked or account closed.", ct); await db.Entry(user).ReloadAsync(ct);
} var flow = await db.LiveFlow.SingleAsync(f => f.PodcastId == capture.Id, ct);
if (bytes > user.GetEffectiveDiskQuota(DateTimeOffset.UtcNow) - user.DiskUsage) await db.Entry(flow).ReloadAsync(ct);
throw new PodcastException(413, "Quota disque atteint."); if (user.WithdrawalRequestedAt is not null || flow.ObsKeyHash is null
await using var stream = File.OpenRead(path); || flow.ObsConnectionId != capture.ConnectionId || flow.PodcastDeleted)
await processor.AppendAsync(id, ownerId, next, end - begin, stream, ct); throw new OperationCanceledException("OBS activation revoked or account closed.", ct);
user.DiskUsage = checked(user.DiskUsage + bytes); if (bytes > user.GetEffectiveDiskQuota(now) - user.DiskUsage)
}, throw new PodcastException(413, "Quota disque atteint.");
policy: services.GetRequiredService<IOptions<ResourceUsageOptions>>().Value, await using var stream = File.OpenRead(path);
bandwidthMb: (decimal)bytes / (1024m * 1024m)); await processor.AppendAsync(capture.Id, capture.OwnerId, next, end - begin, stream, ct);
File.Delete(path); user.DiskUsage = checked(user.DiskUsage + bytes);
next++; }, 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 finally
{ {
if (!process.HasExited) process.Kill(entireProcessTree: true); processCancellation.Cancel();
await process.WaitForExitAsync(CancellationToken.None); try { await process; }
await diagnostics; catch (OperationCanceledException) when (processCancellation.IsCancellationRequested) { }
monitoring.Cancel(); // FFmpeg.NET cancels/kills without waiting for OS socket teardown.
try { await guard; } using var released = new CancellationTokenSource(TimeSpan.FromSeconds(5));
catch (OperationCanceledException) when (monitoring.IsCancellationRequested) { } 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) 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); 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<PodcastOptions>(configuration.GetSection("Podcast"));
services.Configure<PodcastObsOptions>(configuration.GetSection("Podcast:Obs")); services.Configure<PodcastObsOptions>(configuration.GetSection("Podcast:Obs"));
services.AddScoped<IPodcastObsService, PodcastObsService>(); services.AddScoped<IPodcastObsService, PodcastObsService>();
services.AddHttpClient<IPodcastObsRelay, PodcastObsRelay>(client => client.Timeout = TimeSpan.FromSeconds(10)) services.AddSingleton<PodcastObsWorker>();
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { AllowAutoRedirect = false }); services.AddHostedService(provider => provider.GetRequiredService<PodcastObsWorker>());
services.AddHostedService<PodcastObsWorker>();
services.AddSingleton<ILiveProcessor, LiveProcessor>(); services.AddSingleton<ILiveProcessor, LiveProcessor>();
services.AddScoped<IPodcastScheduleService, PodcastScheduleService>(); services.AddScoped<IPodcastScheduleService, PodcastScheduleService>();
services.AddSingleton<IPodcastReplayRemuxer, FfmpegPodcastReplayRemuxer>(); services.AddSingleton<IPodcastReplayRemuxer, FfmpegPodcastReplayRemuxer>();

View file

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

View file

@ -41,10 +41,12 @@ La confidentialité ne signifie pas qu'aucune donnée n'est transmise :
et les paramètres de connexion sont partagés avec l'interlocuteur pour établir et les paramètres de connexion sont partagés avec l'interlocuteur pour établir
la liaison. L'audio et, si activée, la vidéo sont transmis aux participants ; la liaison. L'audio et, si activée, la vidéo sont transmis aux participants ;
les diffusions de podcasts transmettent aussi les médias aux personnes autorisées. les diffusions de podcasts transmettent aussi les médias aux personnes autorisées.
Pour une émission diffusée avec OBS ou un autre encodeur externe, le relais média Pour une émission diffusée avec OBS ou un autre encodeur externe, l'API reçoit
configuré reçoit le flux, l'adresse réseau du diffuseur et une clé technique directement le flux SRT chiffré et l'adresse réseau du diffuseur. L'URL technique
limitée à l'émission. Cette clé permet de diffuser, pas d'accéder aux données contient une phrase secrète limitée à l'émission, conservée en mémoire pendant
du compte ni de regarder les podcasts ; elle peut être révoquée dans PostIt. l'activation et nécessaire au chiffrement. Elle permet de diffuser, pas d'accéder
aux données du compte ni de regarder les podcasts ; elle peut être révoquée dans
PostIt. Aucun relais média tiers n'est requis.
* Les prestataires d'hébergement et de messagerie traitent les données nécessaires * Les prestataires d'hébergement et de messagerie traitent les données nécessaires
au stockage, au fonctionnement et à l'acheminement des messages. au stockage, au fonctionnement et à l'acheminement des messages.
* PayPal reçoit les informations nécessaires au paiement et à l'abonnement : * PayPal reçoit les informations nécessaires au paiement et à l'abonnement :

View file

@ -62,8 +62,9 @@ public sealed class ApiWebServerFixture : WebHostFixture
sharedConnection = _sharedSqliteConnection; sharedConnection = _sharedSqliteConnection;
} }
// The anchor keeps the database alive; each request needs its own connection.
builder.Services.AddDbContext<ApplicationDbContext>(opt => builder.Services.AddDbContext<ApplicationDbContext>(opt =>
opt.UseSqlite(sharedConnection)); opt.UseSqlite(sharedConnection.ConnectionString));
} }
builder.Services.AddControllers() builder.Services.AddControllers()

View file

@ -1,12 +1,8 @@
using System.Diagnostics; using System.Diagnostics;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text.Json; using System.Text.Json;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.WebUtilities;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Data.Sqlite; using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -21,27 +17,29 @@ using Yavsc.Settings;
namespace Yavsc.Api.Test; namespace Yavsc.Api.Test;
public sealed class PodcastObsTests : IDisposable public sealed class PodcastObsTests : IAsyncDisposable
{ {
private static CancellationToken Ct => TestContext.Current.CancellationToken; private static CancellationToken Ct => TestContext.Current.CancellationToken;
private readonly string root = Path.Combine(Path.GetTempPath(), "yavsc-obs-" + Guid.NewGuid().ToString("N")); private readonly string root = Path.Combine(Path.GetTempPath(), "yavsc-srt-" + Guid.NewGuid().ToString("N"));
private readonly SqliteConnection connection; private readonly SqliteConnection connection;
private readonly ServiceProvider provider; private readonly ServiceProvider provider;
private readonly PodcastObsOptions settings = new() private readonly PodcastObsOptions settings = new()
{ {
Enabled = true, ServerUrl = "rtmps://obs.example.test:1936", Enabled = true, ServerUrl = "srt://127.0.0.1", ListenAddress = "127.0.0.1",
RelayPassword = new string('x', 48), MaxDurationMinutes = 1 FirstPort = FreePort(), PortCount = 1, MaxConcurrentBroadcasts = 1, MaxDurationMinutes = 1
}; };
private readonly Clock clock = new(); private readonly Clock clock = new();
private sealed class Clock : TimeProvider private sealed class Clock : TimeProvider
{ {
public DateTimeOffset Now { get; set; } = DateTimeOffset.UtcNow; private readonly object gate = new();
private DateTimeOffset now = DateTimeOffset.UtcNow;
public DateTimeOffset Now { get { lock (gate) return now; } set { lock (gate) now = value; } }
public override DateTimeOffset GetUtcNow() => Now; public override DateTimeOffset GetUtcNow() => Now;
} }
public PodcastObsTests() public PodcastObsTests()
{ {
var connectionString = $"Data Source=obs-{Guid.NewGuid():N};Mode=Memory;Cache=Shared;Default Timeout=30"; var connectionString = $"Data Source=srt-{Guid.NewGuid():N};Mode=Memory;Cache=Shared;Default Timeout=30";
connection = new SqliteConnection(connectionString); connection = new SqliteConnection(connectionString);
connection.Open(); connection.Open();
var services = new ServiceCollection(); var services = new ServiceCollection();
@ -54,7 +52,7 @@ public sealed class PodcastObsTests : IDisposable
services.AddSingleton<ILiveProcessor, LiveProcessor>(); services.AddSingleton<ILiveProcessor, LiveProcessor>();
services.AddScoped<IPodcastScheduleService, PodcastScheduleService>(); services.AddScoped<IPodcastScheduleService, PodcastScheduleService>();
services.AddScoped<IPodcastObsService, PodcastObsService>(); services.AddScoped<IPodcastObsService, PodcastObsService>();
services.AddHttpClient<IPodcastObsRelay, PodcastObsRelay>(c => c.Timeout = TimeSpan.FromSeconds(5)); services.AddSingleton<PodcastObsWorker>();
provider = services.BuildServiceProvider(); provider = services.BuildServiceProvider();
using var scope = provider.CreateScope(); using var scope = provider.CreateScope();
scope.ServiceProvider.GetRequiredService<ApplicationDbContext>().Database.EnsureCreated(); scope.ServiceProvider.GetRequiredService<ApplicationDbContext>().Database.EnsureCreated();
@ -62,6 +60,7 @@ public sealed class PodcastObsTests : IDisposable
private async Task<PodcastResponse> PlanAsync(IServiceProvider services) private async Task<PodcastResponse> PlanAsync(IServiceProvider services)
{ {
await provider.GetRequiredService<PodcastObsWorker>().StartAsync(Ct);
var db = services.GetRequiredService<ApplicationDbContext>(); var db = services.GetRequiredService<ApplicationDbContext>();
db.Users.Add(new() { Id = "owner", UserName = "owner", Email = "owner@example.test" }); db.Users.Add(new() { Id = "owner", UserName = "owner", Email = "owner@example.test" });
await db.SaveChangesAsync(Ct); await db.SaveChangesAsync(Ct);
@ -71,61 +70,37 @@ public sealed class PodcastObsTests : IDisposable
}, null, Ct); }, null, Ct);
} }
private static PodcastRelayAuthentication Publish(PodcastObsCredentials credentials)
{
var split = credentials.StreamKey.Split('?');
return new() { Action = "publish", Protocol = "rtmp", Path = split[0], Query = split[1], Id = Guid.NewGuid().ToString() };
}
[Fact] [Fact]
public async Task Activation_is_owned_rotatable_expiring_and_never_persists_the_plain_key() public async Task Activation_is_owned_rotatable_expiring_and_keeps_secrets_only_in_memory()
{ {
using var scope = provider.CreateScope(); using var scope = provider.CreateScope();
var plan = await PlanAsync(scope.ServiceProvider); var plan = await PlanAsync(scope.ServiceProvider);
var obs = scope.ServiceProvider.GetRequiredService<IPodcastObsService>(); var obs = scope.ServiceProvider.GetRequiredService<IPodcastObsService>();
var denied = await Assert.ThrowsAsync<PodcastException>(() => obs.ActivateAsync(plan.Id, "other", Ct)); Assert.Equal(403, (await Assert.ThrowsAsync<PodcastException>(() => obs.ActivateAsync(plan.Id, "other", Ct))).StatusCode);
Assert.Equal(403, denied.StatusCode);
var credentials = await obs.ActivateAsync(plan.Id, "owner", Ct); var credentials = await obs.ActivateAsync(plan.Id, "owner", Ct);
Assert.Equal(settings.ServerUrl, credentials.ServerUrl); var secret = QueryHelpers.ParseQuery(new Uri(credentials.ServerUrl).Query)["passphrase"].ToString();
Assert.Empty(credentials.StreamKey);
Assert.Equal(64, secret.Length);
Assert.Equal(clock.Now.AddMinutes(30), credentials.ExpiresAt); Assert.Equal(clock.Now.AddMinutes(30), credentials.ExpiresAt);
var request = Publish(credentials); Assert.Contains("pbkeylen=32", credentials.ServerUrl);
var flow = await scope.ServiceProvider.GetRequiredService<ApplicationDbContext>().LiveFlow.SingleAsync(Ct);
Assert.DoesNotContain(request.Query[4..], flow.ObsKeyHash!);
Assert.NotNull(await obs.AuthenticateAsync(request, Ct));
Assert.Null(await obs.AuthenticateAsync(request with { Id = Guid.NewGuid().ToString() }, Ct));
Assert.Null(await obs.AuthenticateAsync(request with { Path = "obs/" + Guid.NewGuid().ToString("N") }, Ct));
Assert.Null(await obs.AuthenticateAsync(request with { Query = request.Query + "&" + request.Query }, Ct));
var renewed = await obs.ActivateAsync(plan.Id, "owner", Ct);
Assert.Null(await obs.AuthenticateAsync(request, Ct));
Assert.NotNull(await obs.AuthenticateAsync(Publish(renewed), Ct));
clock.Now = renewed.ExpiresAt;
Assert.Null(await obs.AuthenticateAsync(Publish(renewed), Ct));
Assert.False((await obs.StatusAsync(plan.Id, "owner", Ct)).Enabled);
}
[Fact]
public async Task Publisher_cannot_read_media_or_control_relay_and_closed_accounts_cannot_publish()
{
using var scope = provider.CreateScope();
var plan = await PlanAsync(scope.ServiceProvider);
var obs = scope.ServiceProvider.GetRequiredService<IPodcastObsService>();
var publish = Publish(await obs.ActivateAsync(plan.Id, "owner", Ct));
Assert.Null(await obs.AuthenticateAsync(publish with { Action = "read", Protocol = "rtsp" }, Ct));
Assert.Null(await obs.AuthenticateAsync(publish with { Action = "api" }, Ct));
Assert.NotNull(await obs.AuthenticateAsync(new()
{ Action = "api", User = settings.RelayUser, Password = settings.RelayPassword }, Ct));
Assert.NotNull(await obs.AuthenticateAsync(new()
{ Action = "read", Protocol = "rtmp", Path = publish.Path, User = settings.RelayUser, Password = settings.RelayPassword }, Ct));
Assert.Null(await obs.AuthenticateAsync(new()
{ Action = "read", Protocol = "hls", Path = publish.Path, User = settings.RelayUser, Password = settings.RelayPassword }, Ct));
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
(await db.Users.SingleAsync(Ct)).WithdrawalRequestedAt = clock.Now; var flow = await db.LiveFlow.AsNoTracking().SingleAsync(Ct);
await db.SaveChangesAsync(Ct); Assert.DoesNotContain(secret, flow.ObsKeyHash!);
Assert.Null(await obs.AuthenticateAsync(publish, Ct)); Assert.NotNull(flow.ObsConnectionId);
Assert.DoesNotContain(secret, JsonSerializer.Serialize(flow));
Assert.Empty(await provider.GetRequiredService<ILiveProcessor>().ListAsync(Ct));
var renewed = await obs.ActivateAsync(plan.Id, "owner", Ct);
Assert.NotEqual(credentials.ServerUrl, renewed.ServerUrl);
clock.Now = renewed.ExpiresAt;
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(Ct);
timeout.CancelAfter(TimeSpan.FromSeconds(10));
await WaitAsync(async () => !(await obs.StatusAsync(plan.Id, "owner", timeout.Token)).Enabled, timeout.Token);
await WaitAsync(async () => (await obs.StatusAsync(plan.Id, "owner", timeout.Token)).Error is not null, timeout.Token);
Assert.Empty(await provider.GetRequiredService<ILiveProcessor>().ListAsync(Ct));
} }
[Fact] [Fact]
public async Task Activation_blocks_exhausted_quotas_and_direct_capture_cannot_steal_an_armed_emission() public async Task Activation_blocks_exhausted_quotas_direct_capture_and_excess_listeners()
{ {
using var scope = provider.CreateScope(); using var scope = provider.CreateScope();
var services = scope.ServiceProvider; var services = scope.ServiceProvider;
@ -141,155 +116,201 @@ public sealed class PodcastObsTests : IDisposable
await obs.ActivateAsync(plan.Id, "owner", Ct); await obs.ActivateAsync(plan.Id, "owner", Ct);
Assert.Equal(409, (await Assert.ThrowsAsync<PodcastException>(() => Assert.Equal(409, (await Assert.ThrowsAsync<PodcastException>(() =>
services.GetRequiredService<IPodcastScheduleService>().StartAsync(plan.Id, "owner", Ct))).StatusCode); services.GetRequiredService<IPodcastScheduleService>().StartAsync(plan.Id, "owner", Ct))).StatusCode);
var another = await services.GetRequiredService<IPodcastScheduleService>().PrepareAsync("owner", new()
{ Title = "Other", ScheduledStartAt = clock.Now.AddDays(1), PlannedDurationMinutes = 1 }, null, Ct);
Assert.Equal(503, (await Assert.ThrowsAsync<PodcastException>(() => obs.ActivateAsync(another.Id, "owner", Ct))).StatusCode);
await ResourceUsageTracker.RecordAsync(db, "owner", "owner", cpuSeconds: 20000, cancellationToken: Ct); await ResourceUsageTracker.RecordAsync(db, "owner", "owner", cpuSeconds: 20000, cancellationToken: Ct);
Assert.Equal(429, (await Assert.ThrowsAsync<PodcastException>(() => obs.ActivateAsync(plan.Id, "owner", Ct))).StatusCode); Assert.Equal(429, (await Assert.ThrowsAsync<PodcastException>(() => obs.ActivateAsync(plan.Id, "owner", Ct))).StatusCode);
await obs.DeactivateAsync(plan.Id, "owner", Ct);
Assert.False((await obs.StatusAsync(plan.Id, "owner", Ct)).Enabled);
AssertPortFree(settings.FirstPort);
} }
[Fact] [Fact]
public void Public_transport_must_be_encrypted_unless_explicitly_opted_out() public async Task Unavailable_FFmpeg_and_occupied_ports_fail_activation_without_leaving_a_key()
{
using var scope = provider.CreateScope();
var services = scope.ServiceProvider;
var plan = await PlanAsync(services);
var obs = services.GetRequiredService<IPodcastObsService>();
using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
socket.Bind(new IPEndPoint(IPAddress.Loopback, settings.FirstPort));
Assert.Equal(503, (await Assert.ThrowsAsync<PodcastException>(() => obs.ActivateAsync(plan.Id, "owner", Ct))).StatusCode);
}
var media = services.GetRequiredService<IOptions<PodcastOptions>>().Value;
media.FfmpegPath = Path.Combine(root, "ffmpeg-does-not-exist");
Assert.Equal(503, (await Assert.ThrowsAsync<PodcastException>(() => obs.ActivateAsync(plan.Id, "owner", Ct))).StatusCode);
Assert.False((await obs.StatusAsync(plan.Id, "owner", Ct)).Enabled);
await obs.DeactivateAsync(plan.Id, "owner", Ct);
AssertPortFree(settings.FirstPort);
}
[Fact]
public async Task Restart_invalidates_persisted_activations_and_requires_a_new_secret()
{
using var scope = provider.CreateScope();
var services = scope.ServiceProvider;
var plan = await PlanAsync(services);
var obs = services.GetRequiredService<IPodcastObsService>();
var original = await obs.ActivateAsync(plan.Id, "owner", Ct);
await provider.GetRequiredService<PodcastObsWorker>().StopAsync(Ct);
var db = services.GetRequiredService<ApplicationDbContext>();
db.ChangeTracker.Clear();
var flow = await db.LiveFlow.SingleAsync(Ct);
flow.ObsKeyHash = new string('A', 64);
flow.ObsEnabledUntil = clock.Now.AddMinutes(30);
flow.ObsConnectionId = Guid.NewGuid();
await db.SaveChangesAsync(Ct);
using var restarted = new PodcastObsWorker(provider.GetRequiredService<IServiceScopeFactory>(),
Options.Create(settings), services.GetRequiredService<IOptions<PodcastOptions>>(), clock,
services.GetRequiredService<ILogger<PodcastObsWorker>>());
await restarted.StartAsync(Ct);
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(Ct);
timeout.CancelAfter(TimeSpan.FromSeconds(5));
await WaitAsync(async () => !(await obs.StatusAsync(plan.Id, "owner", timeout.Token)).Enabled, timeout.Token);
Assert.Contains("redemarre", (await obs.StatusAsync(plan.Id, "owner", Ct)).Error);
var rearmed = new PodcastObsService(db, Options.Create(settings),
services.GetRequiredService<IOptions<ResourceUsageOptions>>(), restarted, clock,
services.GetRequiredService<ILogger<PodcastObsService>>());
var replacement = await rearmed.ActivateAsync(plan.Id, "owner", Ct);
Assert.NotEqual(original.ServerUrl, replacement.ServerUrl);
await restarted.StopAsync(Ct);
AssertPortFree(settings.FirstPort);
}
[Fact]
public void Configuration_requires_SRT_and_a_bounded_UDP_range()
{ {
settings.ServerUrl = "rtmp://obs.example.test:1935";
Assert.Throws<InvalidOperationException>(settings.Validate);
settings.AllowUnencryptedRtmp = true;
settings.Validate(); settings.Validate();
settings.ServerUrl = "rtmp://obs.example.test/live"; settings.ServerUrl = "rtmp://obs.example.test";
Assert.Throws<InvalidOperationException>(settings.Validate);
settings.ServerUrl = "srt://obs.example.test?passphrase=unsafe";
Assert.Throws<InvalidOperationException>(settings.Validate);
settings.ServerUrl = "srt://obs.example.test";
settings.FirstPort = 65535;
settings.PortCount = 2;
Assert.Throws<InvalidOperationException>(settings.Validate); Assert.Throws<InvalidOperationException>(settings.Validate);
} }
[Theory] [Fact]
[InlineData(false, false)] public async Task Wrong_passphrase_never_starts_a_podcast_and_closed_accounts_cannot_arm()
[InlineData(true, false)]
[InlineData(true, true)]
public async Task Real_rtmp_publisher_becomes_authenticated_HLS_and_MP4_with_storage_refund(bool revoke, bool secure)
{ {
var binary = Environment.GetEnvironmentVariable("YAVSC_TEST_MEDIAMTX"); using var scope = provider.CreateScope();
if (string.IsNullOrWhiteSpace(binary)) Assert.Skip("Set YAVSC_TEST_MEDIAMTX to the MediaMTX v1.21.3 binary to run the real OBS-compatible media test."); var services = scope.ServiceProvider;
var apiPort = FreePort(); var plan = await PlanAsync(services);
var rtmpPort = FreePort(); var obs = services.GetRequiredService<IPodcastObsService>();
var rtmpsPort = FreePort(); var credentials = await obs.ActivateAsync(plan.Id, "owner", Ct);
var authPort = FreePort(); var uri = new UriBuilder(credentials.ServerUrl) { Query = "mode=caller&passphrase=incorrect-test-passphrase&pbkeylen=32&enforced_encryption=1&connect_timeout=1000" };
settings.ServerUrl = secure ? $"rtmps://127.0.0.1:{rtmpsPort}" : $"rtmp://127.0.0.1:{rtmpPort}"; using (var publisher = Publisher(uri.Uri.AbsoluteUri))
settings.AllowUnencryptedRtmp = !secure;
settings.RelayApiUrl = $"http://127.0.0.1:{apiPort}/";
settings.RelayReadUrl = $"rtmp://127.0.0.1:{rtmpPort}/";
Directory.CreateDirectory(root);
var tlsConfig = "";
if (secure)
{ {
using var key = RSA.Create(2048); var diagnostics = publisher.StandardError.ReadToEndAsync(Ct);
var request = new CertificateRequest("CN=localhost", key, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); await publisher.WaitForExitAsync(Ct).WaitAsync(TimeSpan.FromSeconds(10), Ct);
using var certificate = request.CreateSelfSigned(DateTimeOffset.UtcNow.AddMinutes(-1), DateTimeOffset.UtcNow.AddHours(1)); Assert.NotEqual(0, publisher.ExitCode);
var keyPath = Path.Combine(root, "server.key"); await diagnostics;
var certPath = Path.Combine(root, "server.crt");
await File.WriteAllTextAsync(keyPath, key.ExportPkcs8PrivateKeyPem(), Ct);
await File.WriteAllTextAsync(certPath, certificate.ExportCertificatePem(), Ct);
tlsConfig = $"rtmpEncryption: optional\nrtmpsAddress: 127.0.0.1:{rtmpsPort}\n"
+ $"rtmpServerKey: {JsonSerializer.Serialize(keyPath)}\nrtmpServerCert: {JsonSerializer.Serialize(certPath)}";
} }
var config = Path.Combine(root, "mediamtx.yml"); Assert.Empty(await provider.GetRequiredService<ILiveProcessor>().ListAsync(Ct));
await File.WriteAllTextAsync(config, $$""" var db = services.GetRequiredService<ApplicationDbContext>();
logLevel: warn var user = await db.Users.SingleAsync(Ct);
authMethod: http user.WithdrawalRequestedAt = clock.Now;
authHTTPAddress: http://127.0.0.1:{{authPort}}/auth await db.SaveChangesAsync(Ct);
authHTTPExclude: [] await obs.DeactivateAsync(plan.Id, "owner", Ct);
api: true Assert.Equal(403, (await Assert.ThrowsAsync<PodcastException>(() => obs.ActivateAsync(plan.Id, "owner", Ct))).StatusCode);
apiAddress: 127.0.0.1:{{apiPort}} Assert.Empty(await db.ResourceStorageRecords.ToListAsync(Ct));
rtsp: false }
rtmpAddress: 127.0.0.1:{{rtmpPort}}
{{tlsConfig}} [Fact]
hls: false public Task Real_encrypted_SRT_disconnect_produces_HLS_and_MP4_with_refund() => RealMediaAsync("disconnect");
webrtc: false
srt: false [Fact]
playback: false public Task Real_encrypted_SRT_revocation_stops_ingestion_and_finalizes_replay() => RealMediaAsync("revoke");
pathDefaults:
overridePublisher: false [Fact]
maxReaders: 1 public Task Real_encrypted_SRT_withdrawal_stops_ingestion() => RealMediaAsync("withdraw");
paths:
"~^obs/[0-9a-f]{32}$": [Fact]
""", Ct); public Task Real_encrypted_SRT_consumption_limit_stops_ingestion_and_allows_refund() => RealMediaAsync("limit");
var builder = WebApplication.CreateBuilder();
builder.WebHost.UseUrls($"http://127.0.0.1:{authPort}"); [Fact]
await using var app = builder.Build(); public Task Real_encrypted_SRT_disk_limit_stops_ingestion_and_allows_refund() => RealMediaAsync("disk");
app.MapPost("/auth", async (PodcastRelayAuthentication request) =>
{ [Fact]
using var scope = provider.CreateScope(); public Task Real_encrypted_SRT_duration_limit_stops_ingestion_and_finalizes_replay() => RealMediaAsync("duration");
var result = await scope.ServiceProvider.GetRequiredService<IPodcastObsService>().AuthenticateAsync(request, Ct);
return result is null ? Results.Unauthorized() : Results.Ok(result); private async Task RealMediaAsync(string end)
}); {
await app.StartAsync(Ct); using var scope = provider.CreateScope();
using var relayProcess = StartProcess(binary!, config); var services = scope.ServiceProvider;
var relayOutput = relayProcess.StandardOutput.ReadToEndAsync(); var plan = await PlanAsync(services);
var relayErrors = relayProcess.StandardError.ReadToEndAsync(); var obs = services.GetRequiredService<IPodcastObsService>();
var worker = new PodcastObsWorker(provider.GetRequiredService<IServiceScopeFactory>(), Options.Create(settings), var credentials = await obs.ActivateAsync(plan.Id, "owner", Ct);
provider.GetRequiredService<ILogger<PodcastObsWorker>>()); var processor = services.GetRequiredService<ILiveProcessor>();
Process? publisher = null; using var timeout = CancellationTokenSource.CreateLinkedTokenSource(Ct);
Task<string>? publisherErrors = null; timeout.CancelAfter(TimeSpan.FromSeconds(60));
using var publisher = Publisher(credentials.ServerUrl);
var diagnostics = publisher.StandardError.ReadToEndAsync(Ct);
try try
{ {
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(Ct); await WaitAsync(async () => (await processor.ListAsync(timeout.Token))
timeout.CancelAfter(TimeSpan.FromSeconds(75)); .Any(p => p.Id == plan.Id && p.SegmentCount >= 2), timeout.Token);
using var scope = provider.CreateScope(); if (end == "revoke") await obs.DeactivateAsync(plan.Id, "owner", timeout.Token);
var services = scope.ServiceProvider; else if (end == "withdraw")
var plan = await PlanAsync(services);
var credentials = await services.GetRequiredService<IPodcastObsService>().ActivateAsync(plan.Id, "owner", Ct);
using var http = new HttpClient();
// Probe the listener independently of media authorization before starting the publisher.
await WaitAsync(async () =>
{ {
try { using var response = await http.GetAsync(settings.RelayApiUrl, timeout.Token); return true; } using var dbScope = provider.CreateScope();
catch (HttpRequestException) { return false; } var withdrawalDb = dbScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
}, timeout.Token); (await withdrawalDb.Users.SingleAsync(timeout.Token)).WithdrawalRequestedAt = clock.Now;
await worker.StartAsync(Ct); await withdrawalDb.SaveChangesAsync(timeout.Token);
publisher = StartProcess("ffmpeg", "-nostdin", "-hide_banner", "-loglevel", "error", "-re",
"-f", "lavfi", "-i", "testsrc=size=160x120:rate=10", "-re", "-f", "lavfi", "-i", "sine=frequency=440:sample_rate=48000",
"-t", "20", "-c:v", "libx264", "-pix_fmt", "yuv420p", "-preset", "ultrafast", "-g", "20",
"-c:a", "aac", "-b:v", "180k", "-b:a", "32k", "-f", "flv", $"{credentials.ServerUrl}/{credentials.StreamKey}");
publisherErrors = publisher.StandardError.ReadToEndAsync();
var processor = services.GetRequiredService<ILiveProcessor>();
if (revoke)
{
await WaitAsync(async () => (await processor.ListAsync(timeout.Token))
.Any(p => p.Id == plan.Id && p.SegmentCount >= 2), timeout.Token);
await services.GetRequiredService<IPodcastObsService>().DeactivateAsync(plan.Id, "owner", timeout.Token);
} }
await WaitAsync(async () => else if (end == "limit")
{ {
if (!revoke && publisher.HasExited && publisher.ExitCode != 0) using var dbScope = provider.CreateScope();
throw new InvalidOperationException("Synthetic publisher failed: " + var usageDb = dbScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
(await publisherErrors).Replace(credentials.StreamKey, "[redacted]", StringComparison.Ordinal)); await ResourceUsageTracker.RecordAsync(usageDb, "owner", "owner", cpuSeconds: 20000, cancellationToken: timeout.Token);
var status = await services.GetRequiredService<IPodcastObsService>().StatusAsync(plan.Id, "owner", timeout.Token); }
if (status.Error is not null) throw new InvalidOperationException(status.Error); else if (end == "disk")
return (await processor.ListAsync(timeout.Token)).Any(p => {
p.Id == plan.Id && p.State == PodcastState.Completed && p.SegmentCount > 0); using var dbScope = provider.CreateScope();
}, timeout.Token); var diskDb = dbScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
var user = await diskDb.Users.SingleAsync(timeout.Token);
user.DiskQuota = user.DiskUsage;
await diskDb.SaveChangesAsync(timeout.Token);
}
else if (end == "duration") clock.Now = clock.Now.AddMinutes(settings.MaxDurationMinutes);
await WaitAsync(async () => !(await obs.StatusAsync(plan.Id, "owner", timeout.Token)).Enabled, timeout.Token);
if (end == "withdraw")
{
await publisher.WaitForExitAsync(timeout.Token);
return;
}
await WaitAsync(async () => (await processor.GetAsync(plan.Id, timeout.Token)).Podcast.State == PodcastState.Completed, timeout.Token);
var manifest = await processor.GetAsync(plan.Id, Ct); var manifest = await processor.GetAsync(plan.Id, Ct);
Assert.True(manifest.Podcast.DurationSeconds >= (revoke ? 2 : 8)); Assert.True(manifest.Podcast.DurationSeconds >= 2);
Assert.True(manifest.Podcast.Bytes > 0); Assert.True(manifest.Podcast.Bytes > 0);
using (var probe = StartProcess("ffprobe", "-v", "error", "-show_streams", "-of", "json", using (var probe = StartProcess("ffprobe", "-v", "error", "-show_streams", "-of", "json",
Path.Combine(root, plan.Id.ToString("N"), "00000000.ts"))) Path.Combine(root, plan.Id.ToString("N"), "00000000.ts")))
{ {
var probeOutput = await probe.StandardOutput.ReadToEndAsync(Ct); var output = await probe.StandardOutput.ReadToEndAsync(Ct);
var probeError = await probe.StandardError.ReadToEndAsync(Ct); var probeError = await probe.StandardError.ReadToEndAsync(Ct);
await probe.WaitForExitAsync(Ct); await probe.WaitForExitAsync(Ct);
Assert.Equal(0, probe.ExitCode); Assert.Equal(0, probe.ExitCode);
using var probeDocument = JsonDocument.Parse(probeOutput); Assert.Empty(probeError);
var audio = Assert.Single(probeDocument.RootElement.GetProperty("streams").EnumerateArray(), using var document = JsonDocument.Parse(output);
var audio = Assert.Single(document.RootElement.GetProperty("streams").EnumerateArray(),
s => s.GetProperty("codec_type").GetString() == "audio"); s => s.GetProperty("codec_type").GetString() == "audio");
Assert.Equal("aac", audio.GetProperty("codec_name").GetString()); Assert.Equal("aac", audio.GetProperty("codec_name").GetString());
Assert.True(string.IsNullOrEmpty(probeError), probeError);
Assert.Equal("48000", audio.GetProperty("sample_rate").GetString()); Assert.Equal("48000", audio.GetProperty("sample_rate").GetString());
} }
var db = services.GetRequiredService<ApplicationDbContext>(); var db = services.GetRequiredService<ApplicationDbContext>();
var receipt = await db.ResourceStorageRecords.AsNoTracking().SingleAsync(Ct); var receipt = await db.ResourceStorageRecords.AsNoTracking().SingleAsync(Ct);
Assert.Equal(manifest.Podcast.Bytes, receipt.Bytes); Assert.Equal(manifest.Podcast.Bytes, receipt.Bytes);
var user = await db.Users.AsNoTracking().SingleAsync(Ct); Assert.Equal(receipt.Bytes, (await db.Users.AsNoTracking().SingleAsync(Ct)).DiskUsage);
Assert.Equal(receipt.Bytes, user.DiskUsage);
var usage = await db.ResourceUsageRecords.AsNoTracking().SingleAsync(Ct); var usage = await db.ResourceUsageRecords.AsNoTracking().SingleAsync(Ct);
Assert.Equal((decimal)receipt.Bytes / (1024m * 1024m), usage.StorageMb); Assert.Equal((decimal)receipt.Bytes / (1024m * 1024m), usage.StorageMb);
Assert.True(usage.BandwidthMb >= usage.StorageMb); Assert.True(usage.BandwidthMb >= usage.StorageMb);
if (!revoke) Assert.Equal(usage.StorageMb, usage.BandwidthMb); var state = await obs.StatusAsync(plan.Id, "owner", Ct);
Assert.False((await services.GetRequiredService<IPodcastObsService>().StatusAsync(plan.Id, "owner", Ct)).Enabled); if (end == "limit") Assert.Contains("seuil", state.Error);
Assert.Null((await services.GetRequiredService<IPodcastObsService>().StatusAsync(plan.Id, "owner", Ct)).Error); else if (end == "disk") Assert.Contains("Quota disque", state.Error);
else Assert.Null(state.Error);
Assert.True(await processor.PrepareNextReplayAsync(new FfmpegPodcastReplayRemuxer( Assert.True(await processor.PrepareNextReplayAsync(new FfmpegPodcastReplayRemuxer(
services.GetRequiredService<IOptions<PodcastOptions>>()), Ct)); services.GetRequiredService<IOptions<PodcastOptions>>()), Ct));
Assert.Equal(PodcastReplayState.Ready, (await processor.GetAsync(plan.Id, Ct)).Podcast.Replay?.State); Assert.Equal(PodcastReplayState.Ready, (await processor.GetAsync(plan.Id, Ct)).Podcast.Replay?.State);
@ -303,46 +324,49 @@ public sealed class PodcastObsTests : IDisposable
} }
finally finally
{ {
await worker.StopAsync(CancellationToken.None); if (!publisher.HasExited) publisher.Kill(entireProcessTree: true);
worker.Dispose(); await publisher.WaitForExitAsync(CancellationToken.None);
if (publisher is not null) await diagnostics;
{
if (!publisher.HasExited) publisher.Kill(entireProcessTree: true);
await publisher.WaitForExitAsync(CancellationToken.None);
await publisherErrors!;
publisher.Dispose();
}
if (!relayProcess.HasExited) relayProcess.Kill(entireProcessTree: true);
await relayProcess.WaitForExitAsync(CancellationToken.None);
await relayOutput;
await relayErrors;
await app.StopAsync(CancellationToken.None);
} }
} }
private static Process Publisher(string url) => StartProcess("ffmpeg",
"-nostdin", "-hide_banner", "-loglevel", "error", "-re",
"-f", "lavfi", "-i", "testsrc=size=160x120:rate=10", "-re", "-f", "lavfi", "-i", "sine=frequency=440:sample_rate=48000",
"-t", "12", "-c:v", "libx264", "-pix_fmt", "yuv420p", "-preset", "ultrafast", "-g", "20",
"-c:a", "aac", "-f", "mpegts", url + "&timeout=3000000");
private static int FreePort() private static int FreePort()
{ {
using var listener = new TcpListener(IPAddress.Loopback, 0); using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
listener.Start(); socket.Bind(new IPEndPoint(IPAddress.Loopback, 0));
return ((IPEndPoint)listener.LocalEndpoint).Port; return ((IPEndPoint)socket.LocalEndPoint!).Port;
} }
private static Process StartProcess(string file, params string[] args) private static void AssertPortFree(int port)
{ {
var start = new ProcessStartInfo(file) { UseShellExecute = false, RedirectStandardError = true, RedirectStandardOutput = true }; using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
foreach (var arg in args) start.ArgumentList.Add(arg); socket.Bind(new IPEndPoint(IPAddress.Loopback, port));
return Process.Start(start) ?? throw new IOException("Could not start test media process."); }
private static Process StartProcess(string executable, params string[] arguments)
{
var start = new ProcessStartInfo(executable)
{ RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false };
foreach (var argument in arguments) start.ArgumentList.Add(argument);
return Process.Start(start) ?? throw new InvalidOperationException("Test process could not start.");
} }
private static async Task WaitAsync(Func<Task<bool>> condition, CancellationToken ct) private static async Task WaitAsync(Func<Task<bool>> condition, CancellationToken ct)
{ {
while (!await condition()) await Task.Delay(200, ct); while (!await condition()) await Task.Delay(100, ct);
} }
public void Dispose() public async ValueTask DisposeAsync()
{ {
provider.Dispose(); await provider.GetRequiredService<PodcastObsWorker>().StopAsync(CancellationToken.None);
connection.Dispose(); await provider.DisposeAsync();
await connection.DisposeAsync();
if (Directory.Exists(root)) Directory.Delete(root, recursive: true); if (Directory.Exists(root)) Directory.Delete(root, recursive: true);
} }
} }