Update: Edited to change JSON formatting.
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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; }
|
||||
}
|
||||
7
RelayServer/Models/Rtc/RtcSessionDescription.cs
Normal file
7
RelayServer/Models/Rtc/RtcSessionDescription.cs
Normal 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; }
|
||||
}
|
||||
@@ -25,6 +25,15 @@ public sealed class RtcCallService
|
||||
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>
|
||||
/// 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.
|
||||
@@ -35,27 +44,6 @@ public sealed class RtcCallService
|
||||
/// <param name="sdp">The SDP offer payload.</param>
|
||||
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 activeCall = activeCalls.FirstOrDefault(x => x.ChannelId == channelId && x.IsActive);
|
||||
|
||||
@@ -65,32 +53,42 @@ public sealed class RtcCallService
|
||||
{
|
||||
ChannelId = channelId,
|
||||
OfferUser = username,
|
||||
IsActive = true,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
UpdatedAt = DateTime.UtcNow
|
||||
});
|
||||
}
|
||||
else
|
||||
Offer = new RtcSessionDescription
|
||||
{
|
||||
activeCall.UpdatedAt = DateTime.UtcNow;
|
||||
await _db.Merge<RtcActiveCall, RtcActiveCall>(activeCall);
|
||||
Type = "offer",
|
||||
Sdp = sdp
|
||||
},
|
||||
Answer = null,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
UpdatedAt = DateTime.UtcNow,
|
||||
IsActive = true
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
activeCall.OfferUser = username;
|
||||
activeCall.Offer = new RtcSessionDescription
|
||||
{
|
||||
Type = "offer",
|
||||
Sdp = sdp
|
||||
};
|
||||
activeCall.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await _db.Merge<RtcActiveCall, RtcActiveCall>(activeCall);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
/// <param name="channelId">The channel whose offer should be retrieved.</param>
|
||||
/// <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>
|
||||
public async Task<RtcOffer?> GetOfferAsync(string channelId)
|
||||
public async Task<RtcSessionDescription?> GetOfferAsync(string channelId)
|
||||
{
|
||||
var offers = await _db.Select<RtcOffer>("rtc_offers");
|
||||
return offers
|
||||
.Where(x => x.ChannelId == channelId)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.FirstOrDefault();
|
||||
var activeCall = await GetActiveCallAsync(channelId);
|
||||
return activeCall?.Offer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -103,23 +101,21 @@ public sealed class RtcCallService
|
||||
/// <param name="sdp">The SDP answer payload.</param>
|
||||
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 activeCall = activeCalls.FirstOrDefault(x => x.ChannelId == channelId && x.IsActive);
|
||||
if (activeCall is not null)
|
||||
|
||||
if (activeCall is null)
|
||||
return;
|
||||
|
||||
activeCall.Answer = new RtcSessionDescription
|
||||
{
|
||||
Type = "answer",
|
||||
Sdp = sdp
|
||||
};
|
||||
activeCall.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await _db.Merge<RtcActiveCall, RtcActiveCall>(activeCall);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all answers stored for the specified channel in creation order.
|
||||
@@ -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");
|
||||
return offers
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
var activeCalls = await _db.Select<RtcActiveCall>("rtc_active_calls");
|
||||
return activeCalls
|
||||
.Where(x => x.Offer is not null)
|
||||
.OrderByDescending(x => x.UpdatedAt)
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user