Messaging works, let's start there.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json;
|
||||
using RelayShared.Rtc;
|
||||
using RelayServer.Services.Rtc;
|
||||
|
||||
@@ -6,118 +6,108 @@ namespace RelayServer.Endpoints;
|
||||
|
||||
public static class RtcEndpoints
|
||||
{
|
||||
/// <summary>
|
||||
/// Maps all RTC-related HTTP endpoints used for storing offers and answers,
|
||||
/// writing ICE candidates, checking active calls, and leaving active calls.
|
||||
/// </summary>
|
||||
/// <param name="app">The web application to map endpoints onto.</param>
|
||||
public static void MapRtcEndpoints(this WebApplication app)
|
||||
{
|
||||
// Store or update the current SDP offer for a channel call.
|
||||
app.MapPost("/api/rtc/offer", async (RtcOffer request, RtcCallService rtcCallService) =>
|
||||
{
|
||||
await rtcCallService.WriteOfferAsync(request.ChannelId, request.Username, request.SessionDescription);
|
||||
await rtcCallService.WriteOfferAsync(
|
||||
request.ChannelId,
|
||||
request.Username,
|
||||
request.TargetUsername,
|
||||
request.SessionDescription);
|
||||
|
||||
RtcNotificationService.BroadcastToChannel(new RtcNotificationMessage
|
||||
{
|
||||
Type = SignalType.OfferUpdated,
|
||||
ChannelId = request.ChannelId,
|
||||
Username = request.Username
|
||||
Username = request.Username,
|
||||
TargetUsername = request.TargetUsername
|
||||
});
|
||||
|
||||
return Results.Ok();
|
||||
});
|
||||
|
||||
// List all offers.
|
||||
app.MapGet("/api/rtc/offers", async (RtcCallService rtcCallService) =>
|
||||
{
|
||||
return Results.Ok(await rtcCallService.GetOffersAsync());
|
||||
});
|
||||
|
||||
// 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));
|
||||
});
|
||||
|
||||
// Return the latest stored SDP offer for the specified channel.
|
||||
app.MapGet("/api/rtc/offers/{channelId}", async (string channelId, RtcCallService rtcCallService) =>
|
||||
app.MapGet("/api/rtc/participants/{channelId}", async (string channelId, RtcCallService rtcCallService) =>
|
||||
{
|
||||
var offer = await rtcCallService.GetOfferAsync(channelId);
|
||||
return Results.Ok(await rtcCallService.GetParticipantsAsync(channelId));
|
||||
});
|
||||
|
||||
app.MapGet("/api/rtc/offer/{channelId}/{fromUsername}/{targetUsername}", async (
|
||||
string channelId,
|
||||
string fromUsername,
|
||||
string targetUsername,
|
||||
RtcCallService rtcCallService) =>
|
||||
{
|
||||
var offer = await rtcCallService.GetOfferAsync(channelId, fromUsername, targetUsername);
|
||||
return offer is null ? Results.NotFound() : Results.Ok(offer);
|
||||
});
|
||||
|
||||
// Store a new SDP answer for the specified channel call.
|
||||
app.MapPost("/api/rtc/answer", async (RtcOffer request, RtcCallService rtcCallService) =>
|
||||
app.MapPost("/api/rtc/answer", async (RtcAnswer request, RtcCallService rtcCallService) =>
|
||||
{
|
||||
Console.WriteLine($"RTC answer received for channel {request.ChannelId} from {request.Username}");
|
||||
|
||||
await rtcCallService.WriteAnswerAsync(request.ChannelId, request.SessionDescription);
|
||||
|
||||
Console.WriteLine($"Broadcasting rtc_answer_updated for {request.ChannelId}");
|
||||
await rtcCallService.WriteAnswerAsync(
|
||||
request.ChannelId,
|
||||
request.Username,
|
||||
request.TargetUsername,
|
||||
request.SessionDescription);
|
||||
|
||||
RtcNotificationService.BroadcastToChannel(new RtcNotificationMessage
|
||||
{
|
||||
Type = SignalType.AnswerUpdated,
|
||||
ChannelId = request.ChannelId
|
||||
ChannelId = request.ChannelId,
|
||||
Username = request.Username,
|
||||
TargetUsername = request.TargetUsername
|
||||
});
|
||||
|
||||
return Results.Ok();
|
||||
});
|
||||
|
||||
// Return all answers stored for the specified channel.
|
||||
app.MapGet("/api/rtc/answers/{channelId}", async (string channelId, RtcCallService rtcCallService) =>
|
||||
app.MapGet("/api/rtc/answer/{channelId}/{fromUsername}/{targetUsername}", async (
|
||||
string channelId,
|
||||
string fromUsername,
|
||||
string targetUsername,
|
||||
RtcCallService rtcCallService) =>
|
||||
{
|
||||
return Results.Ok(await rtcCallService.GetAnswersAsync(channelId));
|
||||
});
|
||||
|
||||
// Return the latest answer stored for the specified channel.
|
||||
app.MapGet("/api/rtc/answer/{channelId}", async (string channelId, RtcCallService rtcCallService) =>
|
||||
{
|
||||
var answer = await rtcCallService.GetLatestAnswerAsync(channelId);
|
||||
var answer = await rtcCallService.GetAnswerAsync(channelId, fromUsername, targetUsername);
|
||||
return answer is null ? Results.NotFound() : Results.Ok(answer);
|
||||
});
|
||||
|
||||
// Store a new ICE candidate for the specified channel call.
|
||||
app.MapPost("/api/rtc/candidate", async (DBIceCandidate request, RtcCallService rtcCallService) =>
|
||||
{
|
||||
await rtcCallService.WriteIceCandidateAsync(
|
||||
request.ChannelId,
|
||||
request.Username,
|
||||
request.TargetUsername,
|
||||
request.Candidate.candidate,
|
||||
request.Candidate.sdpMid,
|
||||
request.Candidate.sdpMLineIndex
|
||||
);
|
||||
request.Candidate.sdpMLineIndex);
|
||||
|
||||
RtcNotificationService.BroadcastToChannel(new RtcNotificationMessage
|
||||
{
|
||||
Type = SignalType.CandidateAdded,
|
||||
ChannelId = request.ChannelId,
|
||||
Username = request.Username,
|
||||
TargetUsername = request.TargetUsername,
|
||||
Direction = JsonSerializer.Serialize(request.Candidate)
|
||||
});
|
||||
|
||||
return Results.Ok();
|
||||
});
|
||||
|
||||
// Return all ICE candidates stored for the specified channel.
|
||||
app.MapGet("/api/rtc/candidates/{channelId}", async (string channelId, RtcCallService rtcCallService) =>
|
||||
{
|
||||
return Results.Ok(await rtcCallService.GetIceCandidatesAsync(channelId));
|
||||
});
|
||||
|
||||
// Return ICE candidates for the specified channel that belong to other users
|
||||
// and match the requested direction.
|
||||
app.MapGet("/api/rtc/candidates/{channelId}/{username}/{direction}", async (
|
||||
string channelId,
|
||||
string username,
|
||||
string direction,
|
||||
RtcCallService rtcCallService) =>
|
||||
{
|
||||
return Results.Ok(await rtcCallService.GetIceCandidatesForOthersAsync(channelId, username, direction));
|
||||
});
|
||||
|
||||
// Leave the active call for the specified channel.
|
||||
app.MapPost("/api/rtc/leave", async (RtcLeaveRequest request, RtcCallService rtcCallService) =>
|
||||
{
|
||||
await rtcCallService.LeaveCallAsync(request.ChannelId, request.Username);
|
||||
@@ -132,4 +122,4 @@ public static class RtcEndpoints
|
||||
return Results.Ok();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using RelayShared.Rtc;
|
||||
using RelayShared.Rtc;
|
||||
using SurrealDb.Net;
|
||||
|
||||
namespace RelayServer.Services.Rtc;
|
||||
@@ -12,19 +12,12 @@ 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);
|
||||
var activeCall = await GetActiveCallAsync(channelId);
|
||||
return activeCall is not null && activeCall.IsActive;
|
||||
}
|
||||
|
||||
|
||||
public async Task<RtcActiveCall?> GetActiveCallAsync(string channelId)
|
||||
{
|
||||
var activeCalls = await _db.Select<RtcActiveCall>("rtc_active_calls");
|
||||
@@ -34,157 +27,70 @@ public sealed class RtcCallService
|
||||
.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.
|
||||
/// Otherwise, the existing active call timestamp is refreshed.
|
||||
/// </summary>
|
||||
/// <param name="channelId">The channel the offer belongs to.</param>
|
||||
/// <param name="username">The user creating the offer.</param>
|
||||
/// <param name="type">The RtcSession Type. </param>
|
||||
/// <param name="sdp">The SDP offer payload.</param>
|
||||
public async Task WriteOfferAsync(string channelId, string username, RtcSessionDescription sessionDescription)
|
||||
public async Task<List<string>> GetParticipantsAsync(string channelId)
|
||||
{
|
||||
var activeCalls = await _db.Select<RtcActiveCall>("rtc_active_calls");
|
||||
var activeCall = activeCalls.FirstOrDefault(x => x.ChannelId == channelId && x.IsActive);
|
||||
return RtcChannelPresenceService.GetUsersInChannel(channelId).ToList();
|
||||
}
|
||||
|
||||
if (activeCall is null)
|
||||
{
|
||||
await _db.Create("rtc_active_calls", new RtcActiveCall
|
||||
{
|
||||
ChannelId = channelId,
|
||||
OfferUser = username,
|
||||
Offer = new RtcSessionDescription
|
||||
{
|
||||
Type = sessionDescription.Type,
|
||||
Sdp = sessionDescription.Sdp
|
||||
},
|
||||
Answer = null,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
UpdatedAt = DateTime.UtcNow,
|
||||
IsActive = true
|
||||
});
|
||||
public async Task WriteOfferAsync(string channelId, string username, string targetUsername, RtcSessionDescription sessionDescription)
|
||||
{
|
||||
var activeCall = await EnsureActiveCallAsync(channelId);
|
||||
var participant = GetOrCreateParticipant(activeCall, username);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
activeCall.OfferUser = username;
|
||||
activeCall.Offer = new RtcSessionDescription
|
||||
{
|
||||
Type = sessionDescription.Type,
|
||||
Sdp = sessionDescription.Sdp
|
||||
};
|
||||
participant.Offer = CloneSessionDescription(sessionDescription);
|
||||
participant.Answer = null;
|
||||
activeCall.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await _db.Merge<RtcActiveCall, RtcActiveCall>(activeCall);
|
||||
await SaveActiveCallAsync(activeCall);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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 current offer for the active call, or null if no active call or offer exists.
|
||||
/// </returns>
|
||||
public async Task<RtcSessionDescription?> GetOfferAsync(string channelId)
|
||||
public async Task<RtcSessionDescription?> GetOfferAsync(string channelId, string fromUsername, string targetUsername)
|
||||
{
|
||||
var activeCall = await GetActiveCallAsync(channelId);
|
||||
return activeCall?.Offer;
|
||||
return activeCall?.Participants
|
||||
.FirstOrDefault(x => x.Username.Equals(fromUsername, StringComparison.OrdinalIgnoreCase))
|
||||
?.Offer;
|
||||
}
|
||||
|
||||
/// <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="sessionDescription">The SDP and type answer payload.</param>
|
||||
public async Task WriteAnswerAsync(string channelId, RtcSessionDescription sessionDescription)
|
||||
public async Task WriteAnswerAsync(string channelId, string username, string targetUsername, RtcSessionDescription sessionDescription)
|
||||
{
|
||||
var activeCalls = await _db.Select<RtcActiveCall>("rtc_active_calls");
|
||||
var activeCall = activeCalls.FirstOrDefault(x => x.ChannelId == channelId && x.IsActive);
|
||||
var activeCall = await EnsureActiveCallAsync(channelId);
|
||||
var participant = GetOrCreateParticipant(activeCall, username);
|
||||
|
||||
if (activeCall is null)
|
||||
return;
|
||||
|
||||
activeCall.Answer = new RtcSessionDescription
|
||||
{
|
||||
Type = sessionDescription.Type,
|
||||
Sdp = sessionDescription.Sdp
|
||||
};
|
||||
participant.Answer = CloneSessionDescription(sessionDescription);
|
||||
activeCall.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await _db.Merge<RtcActiveCall, RtcActiveCall>(activeCall);
|
||||
await SaveActiveCallAsync(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<RtcSessionDescription>> GetAnswersAsync(string channelId)
|
||||
public async Task<RtcSessionDescription?> GetAnswerAsync(string channelId, string fromUsername, string targetUsername)
|
||||
{
|
||||
var activeCall = await GetActiveCallAsync(channelId);
|
||||
|
||||
if (activeCall?.Answer is null)
|
||||
return [];
|
||||
|
||||
return [activeCall.Answer];
|
||||
return activeCall?.Participants
|
||||
.FirstOrDefault(x => x.Username.Equals(fromUsername, StringComparison.OrdinalIgnoreCase))
|
||||
?.Answer;
|
||||
}
|
||||
|
||||
/// <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<RtcSessionDescription?> GetLatestAnswerAsync(string channelId)
|
||||
{
|
||||
var activeCall = await GetActiveCallAsync(channelId);
|
||||
return activeCall?.Answer;
|
||||
}
|
||||
|
||||
/// <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 targetUsername,
|
||||
string candidate,
|
||||
string? sdpMid,
|
||||
int? sdpMLineIndex/*,
|
||||
string direction*/)
|
||||
int? sdpMLineIndex)
|
||||
{
|
||||
await _db.Create("rtc_ice_candidates", new RtcIceCandidate
|
||||
{
|
||||
ChannelId = channelId,
|
||||
Username = username,
|
||||
TargetUsername = targetUsername,
|
||||
Candidate = candidate,
|
||||
SdpMid = sdpMid,
|
||||
SdpMLineIndex = sdpMLineIndex,
|
||||
// Direction = direction,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
}
|
||||
|
||||
/// <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");
|
||||
@@ -194,59 +100,78 @@ 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");
|
||||
var activeCall = activeCalls.FirstOrDefault(x => x.ChannelId == channelId && x.IsActive);
|
||||
|
||||
var activeCall = await GetActiveCallAsync(channelId);
|
||||
if (activeCall is null)
|
||||
return;
|
||||
|
||||
if (activeCall.OfferUser == username)
|
||||
{
|
||||
activeCall.IsActive = false;
|
||||
activeCall.UpdatedAt = DateTime.UtcNow;
|
||||
await _db.Merge<RtcActiveCall, RtcActiveCall>(activeCall);
|
||||
}
|
||||
activeCall.Participants.RemoveAll(x => x.Username.Equals(username, StringComparison.OrdinalIgnoreCase));
|
||||
activeCall.IsActive = activeCall.Participants.Count > 0;
|
||||
activeCall.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await SaveActiveCallAsync(activeCall);
|
||||
}
|
||||
|
||||
/// <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 activeCalls = await _db.Select<RtcActiveCall>("rtc_active_calls");
|
||||
return activeCalls
|
||||
.Where(x => x.Offer is not null)
|
||||
.Where(x => x.Participants.Any(p => p.Offer is not null))
|
||||
.OrderByDescending(x => x.UpdatedAt)
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<RtcActiveCall> EnsureActiveCallAsync(string channelId)
|
||||
{
|
||||
var activeCall = await GetActiveCallAsync(channelId);
|
||||
if (activeCall is not null)
|
||||
return activeCall;
|
||||
|
||||
return await _db.Create("rtc_active_calls", new RtcActiveCall
|
||||
{
|
||||
ChannelId = channelId,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
UpdatedAt = DateTime.UtcNow,
|
||||
IsActive = true,
|
||||
Participants = []
|
||||
});
|
||||
}
|
||||
|
||||
private async Task SaveActiveCallAsync(RtcActiveCall activeCall)
|
||||
{
|
||||
if (activeCall.Id is null)
|
||||
{
|
||||
await _db.Create("rtc_active_calls", activeCall);
|
||||
return;
|
||||
}
|
||||
|
||||
await _db.Merge<RtcActiveCall, RtcActiveCall>(activeCall);
|
||||
}
|
||||
|
||||
private static RtcParticipantState GetOrCreateParticipant(RtcActiveCall activeCall, string username)
|
||||
{
|
||||
var participant = activeCall.Participants
|
||||
.FirstOrDefault(x => x.Username.Equals(username, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
if (participant is not null)
|
||||
return participant;
|
||||
|
||||
participant = new RtcParticipantState
|
||||
{
|
||||
Username = username
|
||||
};
|
||||
|
||||
activeCall.Participants.Add(participant);
|
||||
return participant;
|
||||
}
|
||||
|
||||
private static RtcSessionDescription CloneSessionDescription(RtcSessionDescription sessionDescription)
|
||||
{
|
||||
return new RtcSessionDescription
|
||||
{
|
||||
Type = sessionDescription.Type,
|
||||
Sdp = sessionDescription.Sdp
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user