From f24a255d12d01330a9ab9567ec26dd1b4911816b Mon Sep 17 00:00:00 2001 From: RuKira Date: Fri, 3 Apr 2026 15:22:53 -0400 Subject: [PATCH] Update: Edited to change JSON formatting. --- RelayServer/Endpoints/RtcEndpoints.cs | 3 +- RelayServer/Models/Rtc/RtcActiveCall.cs | 8 +- .../Models/Rtc/RtcSessionDescription.cs | 7 ++ RelayServer/Services/Rtc/RtcCallService.cs | 107 +++++++++--------- 4 files changed, 69 insertions(+), 56 deletions(-) create mode 100644 RelayServer/Models/Rtc/RtcSessionDescription.cs diff --git a/RelayServer/Endpoints/RtcEndpoints.cs b/RelayServer/Endpoints/RtcEndpoints.cs index d7d6891..ac9ebcd 100644 --- a/RelayServer/Endpoints/RtcEndpoints.cs +++ b/RelayServer/Endpoints/RtcEndpoints.cs @@ -28,7 +28,8 @@ public static class RtcEndpoints // Return whether the specified channel currently has an active call. app.MapGet("/api/rtc/active/{channelId}", async (string channelId, RtcCallService rtcCallService) => { - return Results.Ok(await rtcCallService.HasActiveCallAsync(channelId)); + var call = await rtcCallService.GetActiveCallAsync(channelId); + return call is null ? Results.NotFound() : Results.Ok(call); }); // Return the latest stored SDP offer for the specified channel. diff --git a/RelayServer/Models/Rtc/RtcActiveCall.cs b/RelayServer/Models/Rtc/RtcActiveCall.cs index f299cf8..c89f1a7 100644 --- a/RelayServer/Models/Rtc/RtcActiveCall.cs +++ b/RelayServer/Models/Rtc/RtcActiveCall.cs @@ -2,11 +2,13 @@ namespace RelayServer.Models.Rtc; -public class RtcActiveCall : Record +public sealed class RtcActiveCall : Record { public required string ChannelId { get; set; } - public required string OfferUser { get; set; } - public bool IsActive { get; set; } + public string? OfferUser { get; set; } + public RtcSessionDescription? Offer { get; set; } + public RtcSessionDescription? Answer { get; set; } public DateTime CreatedAt { get; set; } public DateTime UpdatedAt { get; set; } + public bool IsActive { get; set; } } \ No newline at end of file diff --git a/RelayServer/Models/Rtc/RtcSessionDescription.cs b/RelayServer/Models/Rtc/RtcSessionDescription.cs new file mode 100644 index 0000000..28907f6 --- /dev/null +++ b/RelayServer/Models/Rtc/RtcSessionDescription.cs @@ -0,0 +1,7 @@ +namespace RelayServer.Models.Rtc; + +public sealed class RtcSessionDescription +{ + public required string Type { get; set; } + public required string Sdp { get; set; } +} \ No newline at end of file diff --git a/RelayServer/Services/Rtc/RtcCallService.cs b/RelayServer/Services/Rtc/RtcCallService.cs index d57768f..c90429b 100644 --- a/RelayServer/Services/Rtc/RtcCallService.cs +++ b/RelayServer/Services/Rtc/RtcCallService.cs @@ -24,6 +24,15 @@ public sealed class RtcCallService var activeCalls = await _db.Select("rtc_active_calls"); return activeCalls.Any(x => x.ChannelId == channelId && x.IsActive); } + + public async Task GetActiveCallAsync(string channelId) + { + var activeCalls = await _db.Select("rtc_active_calls"); + return activeCalls + .Where(x => x.ChannelId == channelId && x.IsActive) + .OrderByDescending(x => x.UpdatedAt) + .FirstOrDefault(); + } /// /// Creates or updates the current SDP offer for a user in the specified channel. @@ -35,27 +44,6 @@ public sealed class RtcCallService /// The SDP offer payload. public async Task WriteOfferAsync(string channelId, string username, string sdp) { - var offers = await _db.Select("rtc_offers"); - var existing = offers.FirstOrDefault(x => x.ChannelId == channelId && x.Username == username); - - if (existing is null) - { - await _db.Create("rtc_offers", new RtcOffer - { - ChannelId = channelId, - Username = username, - Sdp = sdp, - CreatedAt = DateTime.UtcNow, - UpdatedAt = DateTime.UtcNow - }); - } - else - { - existing.Sdp = sdp; - existing.UpdatedAt = DateTime.UtcNow; - await _db.Merge(existing); - } - var activeCalls = await _db.Select("rtc_active_calls"); var activeCall = activeCalls.FirstOrDefault(x => x.ChannelId == channelId && x.IsActive); @@ -65,32 +53,42 @@ public sealed class RtcCallService { ChannelId = channelId, OfferUser = username, - IsActive = true, + Offer = new RtcSessionDescription + { + Type = "offer", + Sdp = sdp + }, + Answer = null, CreatedAt = DateTime.UtcNow, - UpdatedAt = DateTime.UtcNow + UpdatedAt = DateTime.UtcNow, + IsActive = true }); + + return; } - else + + activeCall.OfferUser = username; + activeCall.Offer = new RtcSessionDescription { - activeCall.UpdatedAt = DateTime.UtcNow; - await _db.Merge(activeCall); - } + Type = "offer", + Sdp = sdp + }; + activeCall.UpdatedAt = DateTime.UtcNow; + + await _db.Merge(activeCall); } /// - /// Gets the most recent SDP offer stored for the specified channel. + /// Gets the current offer stored on the active call for the specified channel. /// /// The channel whose offer should be retrieved. /// - /// The latest offer for the channel, or null if no offer exists. + /// The current offer for the active call, or null if no active call or offer exists. /// - public async Task GetOfferAsync(string channelId) + public async Task GetOfferAsync(string channelId) { - var offers = await _db.Select("rtc_offers"); - return offers - .Where(x => x.ChannelId == channelId) - .OrderByDescending(x => x.CreatedAt) - .FirstOrDefault(); + var activeCall = await GetActiveCallAsync(channelId); + return activeCall?.Offer; } /// @@ -103,22 +101,20 @@ public sealed class RtcCallService /// The SDP answer payload. public async Task WriteAnswerAsync(string channelId, string offerUser, string answerUser, string sdp) { - await _db.Create("rtc_answers", new RtcAnswer - { - ChannelId = channelId, - OfferUser = offerUser, - AnswerUser = answerUser, - Sdp = sdp, - CreatedAt = DateTime.UtcNow - }); - var activeCalls = await _db.Select("rtc_active_calls"); var activeCall = activeCalls.FirstOrDefault(x => x.ChannelId == channelId && x.IsActive); - if (activeCall is not null) + + if (activeCall is null) + return; + + activeCall.Answer = new RtcSessionDescription { - activeCall.UpdatedAt = DateTime.UtcNow; - await _db.Merge(activeCall); - } + Type = "answer", + Sdp = sdp + }; + activeCall.UpdatedAt = DateTime.UtcNow; + + await _db.Merge(activeCall); } /// @@ -241,11 +237,18 @@ public sealed class RtcCallService } } - public async Task> GetOffersAsync() + /// + /// Gets all active call records that currently contain an offer. + /// + /// + /// A list of active calls with offers, ordered from newest to oldest. + /// + public async Task> GetOffersAsync() { - var offers = await _db.Select("rtc_offers"); - return offers - .OrderByDescending(x => x.CreatedAt) + var activeCalls = await _db.Select("rtc_active_calls"); + return activeCalls + .Where(x => x.Offer is not null) + .OrderByDescending(x => x.UpdatedAt) .ToList(); } } \ No newline at end of file