using RelayServer.Models.Rtc; using RelayServer.Services.Rtc; namespace RelayServer.Endpoints; public static class RtcEndpoints { /// /// Maps all RTC-related HTTP endpoints used for joining calls, storing offers and answers, /// writing ICE candidates, and leaving active calls. /// /// The web application to map endpoints onto. public static void MapRtcEndpoints(this WebApplication app) { // Join a channel call and determine whether the caller should become the offerer. app.MapPost("/api/rtc/join", async (RtcJoinRequest request, RtcCallService rtcCallService) => { return Results.Ok(await rtcCallService.JoinCallAsync(request.ChannelId, request.Username)); }); // 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.Sdp); return Results.Ok(); }); // 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/offer/{channelId}", async (string channelId, RtcCallService rtcCallService) => { var offer = await rtcCallService.GetOfferAsync(channelId); 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 (RtcAnswer request, RtcCallService rtcCallService) => { await rtcCallService.WriteAnswerAsync(request.ChannelId, request.OfferUser, request.AnswerUser, request.Sdp); return Results.Ok(); }); // Return all answers stored for the specified channel. app.MapGet("/api/rtc/answers/{channelId}", async (string channelId, 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); 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 (RtcIceCandidate request, RtcCallService rtcCallService) => { await rtcCallService.WriteIceCandidateAsync( request.ChannelId, request.Username, request.Candidate, request.SdpMid, request.SdpMLineIndex, request.Direction ); 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); return Results.Ok(); }); } }