Finished. Have at it.

This commit is contained in:
2026-04-02 17:16:05 -04:00
parent e4e7a70b2c
commit fe2473be21
11 changed files with 546 additions and 387 deletions

View File

@@ -12,6 +12,29 @@ public sealed class RtcCallService
_db = db;
}
/// <summary>
/// Checks whether the specified channel currently has an active RTC call.
/// </summary>
/// <param name="channelId">The channel to inspect.</param>
/// <returns>
/// True if the channel has an active call; otherwise, false.
/// </returns>
public async Task<bool> HasActiveCallAsync(string channelId)
{
var activeCalls = await _db.Select<RtcActiveCall>("rtc_active_calls");
return activeCalls.Any(x => x.ChannelId == channelId && x.IsActive);
}
/// <summary>
/// Joins a user to a channel call and determines whether they should become the offerer
/// or join an already active call.
/// </summary>
/// <param name="channelId">The channel being joined.</param>
/// <param name="username">The user joining the call.</param>
/// <returns>
/// A join response describing whether a call already exists, who the offer user is,
/// and whether the caller should act as the offerer.
/// </returns>
public async Task<RtcJoinResponse> JoinCallAsync(string channelId, string username)
{
var activeCalls = await _db.Select<RtcActiveCall>("rtc_active_calls");
@@ -54,6 +77,13 @@ public sealed class RtcCallService
};
}
/// <summary>
/// Creates or updates the current SDP offer for a user in the specified channel.
/// Also refreshes the active call timestamp when a matching active call exists.
/// </summary>
/// <param name="channelId">The channel the offer belongs to.</param>
/// <param name="username">The user creating the offer.</param>
/// <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");
@@ -69,14 +99,30 @@ public sealed class RtcCallService
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
});
return;
}
else
{
existing.Sdp = sdp;
existing.UpdatedAt = DateTime.UtcNow;
await _db.Merge<RtcOffer, RtcOffer>(existing);
}
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);
if (activeCall is not null)
{
activeCall.UpdatedAt = DateTime.UtcNow;
await _db.Merge<RtcActiveCall, RtcActiveCall>(activeCall);
}
}
/// <summary>
/// Gets the most recent SDP offer stored 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.
/// </returns>
public async Task<RtcOffer?> GetOfferAsync(string channelId)
{
var offers = await _db.Select<RtcOffer>("rtc_offers");
@@ -86,6 +132,14 @@ public sealed class RtcCallService
.FirstOrDefault();
}
/// <summary>
/// Writes a new SDP answer for the specified channel and refreshes the active call timestamp
/// when a matching active call exists.
/// </summary>
/// <param name="channelId">The channel the answer belongs to.</param>
/// <param name="offerUser">The original offer owner.</param>
/// <param name="answerUser">The user submitting the answer.</param>
/// <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
@@ -96,8 +150,23 @@ public sealed class RtcCallService
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)
{
activeCall.UpdatedAt = DateTime.UtcNow;
await _db.Merge<RtcActiveCall, RtcActiveCall>(activeCall);
}
}
/// <summary>
/// Gets all answers stored for the specified channel in creation order.
/// </summary>
/// <param name="channelId">The channel whose answers should be retrieved.</param>
/// <returns>
/// A list of answers for the channel ordered from oldest to newest.
/// </returns>
public async Task<List<RtcAnswer>> GetAnswersAsync(string channelId)
{
var answers = await _db.Select<RtcAnswer>("rtc_answers");
@@ -107,7 +176,40 @@ public sealed class RtcCallService
.ToList();
}
public async Task WriteIceCandidateAsync(string channelId, string username, string candidate, string? sdpMid, int? sdpMLineIndex, string direction)
/// <summary>
/// Gets the most recent answer stored for the specified channel.
/// </summary>
/// <param name="channelId">The channel whose latest answer should be retrieved.</param>
/// <returns>
/// The newest answer for the channel, or null if no answer exists.
/// </returns>
public async Task<RtcAnswer?> GetLatestAnswerAsync(string channelId)
{
var answers = await _db.Select<RtcAnswer>("rtc_answers");
return answers
.Where(x => x.ChannelId == channelId)
.OrderByDescending(x => x.CreatedAt)
.FirstOrDefault();
}
/// <summary>
/// Writes a new ICE candidate entry for the specified channel and user.
/// </summary>
/// <param name="channelId">The channel the ICE candidate belongs to.</param>
/// <param name="username">The user who produced the ICE candidate.</param>
/// <param name="candidate">The ICE candidate string.</param>
/// <param name="sdpMid">The SDP media identifier for the candidate, if any.</param>
/// <param name="sdpMLineIndex">The SDP media line index for the candidate, if any.</param>
/// <param name="direction">
/// The signaling direction the candidate belongs to, such as offer or answer.
/// </param>
public async Task WriteIceCandidateAsync(
string channelId,
string username,
string candidate,
string? sdpMid,
int? sdpMLineIndex,
string direction)
{
await _db.Create("rtc_ice_candidates", new RtcIceCandidate
{
@@ -121,6 +223,13 @@ public sealed class RtcCallService
});
}
/// <summary>
/// Gets all ICE candidates stored for the specified channel in creation order.
/// </summary>
/// <param name="channelId">The channel whose ICE candidates should be retrieved.</param>
/// <returns>
/// A list of ICE candidates for the channel ordered from oldest to newest.
/// </returns>
public async Task<List<RtcIceCandidate>> GetIceCandidatesAsync(string channelId)
{
var candidates = await _db.Select<RtcIceCandidate>("rtc_ice_candidates");
@@ -130,6 +239,31 @@ public sealed class RtcCallService
.ToList();
}
/// <summary>
/// Gets ICE candidates for the specified channel that were created by other users
/// and match the requested signaling direction.
/// </summary>
/// <param name="channelId">The channel whose ICE candidates should be retrieved.</param>
/// <param name="username">The user to exclude from the results.</param>
/// <param name="direction">The signaling direction to match.</param>
/// <returns>
/// A list of matching ICE candidates ordered from oldest to newest.
/// </returns>
public async Task<List<RtcIceCandidate>> GetIceCandidatesForOthersAsync(string channelId, string username, string direction)
{
var candidates = await _db.Select<RtcIceCandidate>("rtc_ice_candidates");
return candidates
.Where(x => x.ChannelId == channelId && x.Username != username && x.Direction == direction)
.OrderBy(x => x.CreatedAt)
.ToList();
}
/// <summary>
/// Leaves the active call for the specified channel. In the current implementation,
/// the call is only marked inactive when the offer user leaves.
/// </summary>
/// <param name="channelId">The channel whose call should be left.</param>
/// <param name="username">The user leaving the call.</param>
public async Task LeaveCallAsync(string channelId, string username)
{
var activeCalls = await _db.Select<RtcActiveCall>("rtc_active_calls");
@@ -140,7 +274,6 @@ public sealed class RtcCallService
if (activeCall.OfferUser == username)
{
//TODO: Fix to only make inactive if all users leave
activeCall.IsActive = false;
activeCall.UpdatedAt = DateTime.UtcNow;
await _db.Merge<RtcActiveCall, RtcActiveCall>(activeCall);