Update: Edited to change JSON formatting.

This commit is contained in:
2026-04-03 15:22:53 -04:00
parent 776889932e
commit f24a255d12
4 changed files with 69 additions and 56 deletions

View File

@@ -28,7 +28,8 @@ public static class RtcEndpoints
// Return whether the specified channel currently has an active call. // Return whether the specified channel currently has an active call.
app.MapGet("/api/rtc/active/{channelId}", async (string channelId, RtcCallService rtcCallService) => 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. // Return the latest stored SDP offer for the specified channel.

View File

@@ -2,11 +2,13 @@
namespace RelayServer.Models.Rtc; namespace RelayServer.Models.Rtc;
public class RtcActiveCall : Record public sealed class RtcActiveCall : Record
{ {
public required string ChannelId { get; set; } public required string ChannelId { get; set; }
public required string OfferUser { get; set; } public string? OfferUser { get; set; }
public bool IsActive { get; set; } public RtcSessionDescription? Offer { get; set; }
public RtcSessionDescription? Answer { get; set; }
public DateTime CreatedAt { get; set; } public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; } public DateTime UpdatedAt { get; set; }
public bool IsActive { get; set; }
} }

View File

@@ -0,0 +1,7 @@
namespace RelayServer.Models.Rtc;
public sealed class RtcSessionDescription
{
public required string Type { get; set; }
public required string Sdp { get; set; }
}

View File

@@ -25,6 +25,15 @@ public sealed class RtcCallService
return activeCalls.Any(x => x.ChannelId == channelId && x.IsActive); return activeCalls.Any(x => x.ChannelId == channelId && x.IsActive);
} }
public async Task<RtcActiveCall?> GetActiveCallAsync(string channelId)
{
var activeCalls = await _db.Select<RtcActiveCall>("rtc_active_calls");
return activeCalls
.Where(x => x.ChannelId == channelId && x.IsActive)
.OrderByDescending(x => x.UpdatedAt)
.FirstOrDefault();
}
/// <summary> /// <summary>
/// Creates or updates the current SDP offer for a user in the specified channel. /// Creates or updates the current SDP offer for a user in the specified channel.
/// If no active call exists for the channel, a new active call is created. /// If no active call exists for the channel, a new active call is created.
@@ -35,27 +44,6 @@ public sealed class RtcCallService
/// <param name="sdp">The SDP offer payload.</param> /// <param name="sdp">The SDP offer payload.</param>
public async Task WriteOfferAsync(string channelId, string username, string sdp) public async Task WriteOfferAsync(string channelId, string username, string sdp)
{ {
var offers = await _db.Select<RtcOffer>("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<RtcOffer, RtcOffer>(existing);
}
var activeCalls = await _db.Select<RtcActiveCall>("rtc_active_calls"); var activeCalls = await _db.Select<RtcActiveCall>("rtc_active_calls");
var activeCall = activeCalls.FirstOrDefault(x => x.ChannelId == channelId && x.IsActive); var activeCall = activeCalls.FirstOrDefault(x => x.ChannelId == channelId && x.IsActive);
@@ -65,32 +53,42 @@ public sealed class RtcCallService
{ {
ChannelId = channelId, ChannelId = channelId,
OfferUser = username, OfferUser = username,
IsActive = true, Offer = new RtcSessionDescription
{
Type = "offer",
Sdp = sdp
},
Answer = null,
CreatedAt = DateTime.UtcNow, CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow UpdatedAt = DateTime.UtcNow,
IsActive = true
}); });
return;
} }
else
activeCall.OfferUser = username;
activeCall.Offer = new RtcSessionDescription
{ {
activeCall.UpdatedAt = DateTime.UtcNow; Type = "offer",
await _db.Merge<RtcActiveCall, RtcActiveCall>(activeCall); Sdp = sdp
} };
activeCall.UpdatedAt = DateTime.UtcNow;
await _db.Merge<RtcActiveCall, RtcActiveCall>(activeCall);
} }
/// <summary> /// <summary>
/// Gets the most recent SDP offer stored for the specified channel. /// Gets the current offer stored on the active call for the specified channel.
/// </summary> /// </summary>
/// <param name="channelId">The channel whose offer should be retrieved.</param> /// <param name="channelId">The channel whose offer should be retrieved.</param>
/// <returns> /// <returns>
/// 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.
/// </returns> /// </returns>
public async Task<RtcOffer?> GetOfferAsync(string channelId) public async Task<RtcSessionDescription?> GetOfferAsync(string channelId)
{ {
var offers = await _db.Select<RtcOffer>("rtc_offers"); var activeCall = await GetActiveCallAsync(channelId);
return offers return activeCall?.Offer;
.Where(x => x.ChannelId == channelId)
.OrderByDescending(x => x.CreatedAt)
.FirstOrDefault();
} }
/// <summary> /// <summary>
@@ -103,22 +101,20 @@ public sealed class RtcCallService
/// <param name="sdp">The SDP answer payload.</param> /// <param name="sdp">The SDP answer payload.</param>
public async Task WriteAnswerAsync(string channelId, string offerUser, string answerUser, string sdp) 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<RtcActiveCall>("rtc_active_calls"); var activeCalls = await _db.Select<RtcActiveCall>("rtc_active_calls");
var activeCall = activeCalls.FirstOrDefault(x => x.ChannelId == channelId && x.IsActive); 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; Type = "answer",
await _db.Merge<RtcActiveCall, RtcActiveCall>(activeCall); Sdp = sdp
} };
activeCall.UpdatedAt = DateTime.UtcNow;
await _db.Merge<RtcActiveCall, RtcActiveCall>(activeCall);
} }
/// <summary> /// <summary>
@@ -241,11 +237,18 @@ public sealed class RtcCallService
} }
} }
public async Task<List<RtcOffer>> GetOffersAsync() /// <summary>
/// Gets all active call records that currently contain an offer.
/// </summary>
/// <returns>
/// A list of active calls with offers, ordered from newest to oldest.
/// </returns>
public async Task<List<RtcActiveCall>> GetOffersAsync()
{ {
var offers = await _db.Select<RtcOffer>("rtc_offers"); var activeCalls = await _db.Select<RtcActiveCall>("rtc_active_calls");
return offers return activeCalls
.OrderByDescending(x => x.CreatedAt) .Where(x => x.Offer is not null)
.OrderByDescending(x => x.UpdatedAt)
.ToList(); .ToList();
} }
} }