Some orgnization, and cleanup to come.
This commit is contained in:
63
RelayServer/Endpoints/RtcEndpoints.cs
Normal file
63
RelayServer/Endpoints/RtcEndpoints.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using RelayServer.Models.Rtc;
|
||||
using RelayServer.Services.Rtc;
|
||||
|
||||
namespace RelayServer.Endpoints;
|
||||
|
||||
public static class RtcEndpoints
|
||||
{
|
||||
public static void MapRtcEndpoints(this WebApplication app)
|
||||
{
|
||||
app.MapPost("/api/rtc/join", async (RtcJoinRequest request, RtcCallService rtcCallService) =>
|
||||
{
|
||||
return Results.Ok(await rtcCallService.JoinCallAsync(request.ChannelId, request.Username));
|
||||
});
|
||||
|
||||
app.MapPost("/api/rtc/offer", async (RtcOffer request, RtcCallService rtcCallService) =>
|
||||
{
|
||||
await rtcCallService.WriteOfferAsync(request.ChannelId, request.Username, request.Sdp);
|
||||
return Results.Ok();
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
app.MapPost("/api/rtc/answer", async (RtcAnswer request, RtcCallService rtcCallService) =>
|
||||
{
|
||||
await rtcCallService.WriteAnswerAsync(request.ChannelId, request.OfferUser, request.AnswerUser, request.Sdp);
|
||||
return Results.Ok();
|
||||
});
|
||||
|
||||
app.MapGet("/api/rtc/answers/{channelId}", async (string channelId, RtcCallService rtcCallService) =>
|
||||
{
|
||||
return Results.Ok(await rtcCallService.GetAnswersAsync(channelId));
|
||||
});
|
||||
|
||||
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();
|
||||
});
|
||||
|
||||
app.MapGet("/api/rtc/candidates/{channelId}", async (string channelId, RtcCallService rtcCallService) =>
|
||||
{
|
||||
return Results.Ok(await rtcCallService.GetIceCandidatesAsync(channelId));
|
||||
});
|
||||
|
||||
app.MapPost("/api/rtc/leave", async (RtcLeaveRequest request, RtcCallService rtcCallService) =>
|
||||
{
|
||||
await rtcCallService.LeaveCallAsync(request.ChannelId, request.Username);
|
||||
return Results.Ok();
|
||||
});
|
||||
}
|
||||
}
|
||||
12
RelayServer/Models/Rtc/RtcActiveCall.cs
Normal file
12
RelayServer/Models/Rtc/RtcActiveCall.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using SurrealDb.Net.Models;
|
||||
|
||||
namespace RelayServer.Models.Rtc;
|
||||
|
||||
public class RtcActiveCall : Record
|
||||
{
|
||||
public required string ChannelId { get; set; }
|
||||
public required string OfferUser { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
public DateTime UpdatedAt { get; set; }
|
||||
}
|
||||
12
RelayServer/Models/Rtc/RtcAnswer.cs
Normal file
12
RelayServer/Models/Rtc/RtcAnswer.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using SurrealDb.Net.Models;
|
||||
|
||||
namespace RelayServer.Models.Rtc;
|
||||
|
||||
public class RtcAnswer : Record
|
||||
{
|
||||
public required string ChannelId { get; set; }
|
||||
public required string OfferUser { get; set; }
|
||||
public required string AnswerUser { get; set; }
|
||||
public required string Sdp { get; set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
}
|
||||
14
RelayServer/Models/Rtc/RtcIceCandidate.cs
Normal file
14
RelayServer/Models/Rtc/RtcIceCandidate.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using SurrealDb.Net.Models;
|
||||
|
||||
namespace RelayServer.Models.Rtc;
|
||||
|
||||
public class RtcIceCandidate : Record
|
||||
{
|
||||
public required string ChannelId { get; set; }
|
||||
public required string Username { get; set; }
|
||||
public required string Candidate { get; set; }
|
||||
public string? SdpMid { get; set; }
|
||||
public int? SdpMLineIndex { get; set; }
|
||||
public required string Direction { get; set; } // "offer" or "answer"
|
||||
public DateTime CreatedAt { get; set; }
|
||||
}
|
||||
7
RelayServer/Models/Rtc/RtcJoinRequest.cs
Normal file
7
RelayServer/Models/Rtc/RtcJoinRequest.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace RelayServer.Models.Rtc;
|
||||
|
||||
public class RtcJoinRequest
|
||||
{
|
||||
public required string ChannelId { get; set; }
|
||||
public required string Username { get; set; }
|
||||
}
|
||||
10
RelayServer/Models/Rtc/RtcJoinResponse.cs
Normal file
10
RelayServer/Models/Rtc/RtcJoinResponse.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace RelayServer.Models.Rtc;
|
||||
|
||||
public class RtcJoinResponse
|
||||
{
|
||||
public required string ChannelId { get; set; }
|
||||
public bool HasActiveCall { get; set; }
|
||||
public bool IsOfferer { get; set; }
|
||||
public string? OfferUser { get; set; }
|
||||
public string? OfferSdp { get; set; }
|
||||
}
|
||||
7
RelayServer/Models/Rtc/RtcLeaveRequest.cs
Normal file
7
RelayServer/Models/Rtc/RtcLeaveRequest.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace RelayServer.Models.Rtc;
|
||||
|
||||
public class RtcLeaveRequest
|
||||
{
|
||||
public required string ChannelId { get; set; }
|
||||
public required string Username { get; set; }
|
||||
}
|
||||
12
RelayServer/Models/Rtc/RtcOffer.cs
Normal file
12
RelayServer/Models/Rtc/RtcOffer.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using SurrealDb.Net.Models;
|
||||
|
||||
namespace RelayServer.Models.Rtc;
|
||||
|
||||
public class RtcOffer : Record
|
||||
{
|
||||
public required string ChannelId { get; set; }
|
||||
public required string Username { get; set; }
|
||||
public required string Sdp { get; set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
public DateTime UpdatedAt { get; set; }
|
||||
}
|
||||
@@ -2,7 +2,6 @@ using System.Text.Json;
|
||||
using RelayServer.Services;
|
||||
using WebSocketSharp.Server;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using RelayServer.Models;
|
||||
|
||||
var surrealService = new SurrealService();
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
using RelayServer.Models;
|
||||
using SurrealDb.Net;
|
||||
|
||||
namespace RelayServer.Services;
|
||||
|
||||
public sealed class ClientKeyService
|
||||
{
|
||||
private readonly SurrealDbClient _db;
|
||||
|
||||
public ClientKeyService(SurrealDbClient db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
|
||||
public async Task RegisterOrUpdateKeyAsync(string username, string publicKey)
|
||||
{
|
||||
var allKeys = await _db.Select<ClientPublicKeys>("client_public_keys");
|
||||
|
||||
var existing = allKeys.FirstOrDefault(x => x.Username == username);
|
||||
|
||||
if (existing is null)
|
||||
{
|
||||
await _db.Create("client_public_keys", new ClientPublicKeys
|
||||
{
|
||||
Username = username,
|
||||
PublicKey = publicKey,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
UpdatedAt = DateTime.UtcNow
|
||||
});
|
||||
|
||||
Console.WriteLine($"Stored public key for {username}");
|
||||
return;
|
||||
}
|
||||
|
||||
existing.PublicKey = publicKey;
|
||||
existing.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await _db.Merge<ClientPublicKeys, ClientPublicKeys>(new ClientPublicKeys
|
||||
{
|
||||
Id = existing.Id,
|
||||
Username = existing.Username,
|
||||
PublicKey = existing.PublicKey,
|
||||
CreatedAt = existing.CreatedAt,
|
||||
UpdatedAt = existing.UpdatedAt
|
||||
});
|
||||
|
||||
Console.WriteLine($"Updated public key for {username}");
|
||||
}
|
||||
|
||||
public async Task<ClientPublicKeys?> GetByUsernameAsync(string username)
|
||||
{
|
||||
var allKeys = await _db.Select<ClientPublicKeys>("client_public_keys");
|
||||
return allKeys.FirstOrDefault(x => x.Username == username);
|
||||
}
|
||||
|
||||
public async Task<List<ClientPublicKeys>> GetAllAsync()
|
||||
{
|
||||
var allKeys = await _db.Select<ClientPublicKeys>("client_public_keys");
|
||||
return allKeys.ToList();
|
||||
}
|
||||
}
|
||||
148
RelayServer/Services/Rtc/RtcCallService.cs
Normal file
148
RelayServer/Services/Rtc/RtcCallService.cs
Normal file
@@ -0,0 +1,148 @@
|
||||
using RelayServer.Models.Rtc;
|
||||
using SurrealDb.Net;
|
||||
|
||||
namespace RelayServer.Services.Rtc;
|
||||
|
||||
public sealed class RtcCallService
|
||||
{
|
||||
private readonly SurrealDbClient _db;
|
||||
|
||||
public RtcCallService(SurrealDbClient db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
|
||||
public async Task<RtcJoinResponse> JoinCallAsync(string channelId, string username)
|
||||
{
|
||||
var activeCalls = await _db.Select<RtcActiveCall>("rtc_active_calls");
|
||||
var activeCall = activeCalls.FirstOrDefault(x => x.ChannelId == channelId && x.IsActive);
|
||||
|
||||
if (activeCall is null)
|
||||
{
|
||||
await _db.Create("rtc_active_calls", new RtcActiveCall
|
||||
{
|
||||
ChannelId = channelId,
|
||||
OfferUser = username,
|
||||
IsActive = true,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
UpdatedAt = DateTime.UtcNow
|
||||
});
|
||||
|
||||
return new RtcJoinResponse
|
||||
{
|
||||
ChannelId = channelId,
|
||||
HasActiveCall = false,
|
||||
IsOfferer = true,
|
||||
OfferUser = username,
|
||||
OfferSdp = null
|
||||
};
|
||||
}
|
||||
|
||||
var offers = await _db.Select<RtcOffer>("rtc_offers");
|
||||
var offer = offers
|
||||
.Where(x => x.ChannelId == channelId)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.FirstOrDefault();
|
||||
|
||||
return new RtcJoinResponse
|
||||
{
|
||||
ChannelId = channelId,
|
||||
HasActiveCall = true,
|
||||
IsOfferer = false,
|
||||
OfferUser = activeCall.OfferUser,
|
||||
OfferSdp = offer?.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
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
existing.Sdp = sdp;
|
||||
existing.UpdatedAt = DateTime.UtcNow;
|
||||
await _db.Merge<RtcOffer, RtcOffer>(existing);
|
||||
}
|
||||
|
||||
public async Task<RtcOffer?> GetOfferAsync(string channelId)
|
||||
{
|
||||
var offers = await _db.Select<RtcOffer>("rtc_offers");
|
||||
return offers
|
||||
.Where(x => x.ChannelId == channelId)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
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
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<List<RtcAnswer>> GetAnswersAsync(string channelId)
|
||||
{
|
||||
var answers = await _db.Select<RtcAnswer>("rtc_answers");
|
||||
return answers
|
||||
.Where(x => x.ChannelId == channelId)
|
||||
.OrderBy(x => x.CreatedAt)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public async Task WriteIceCandidateAsync(string channelId, string username, string candidate, string? sdpMid, int? sdpMLineIndex, string direction)
|
||||
{
|
||||
await _db.Create("rtc_ice_candidates", new RtcIceCandidate
|
||||
{
|
||||
ChannelId = channelId,
|
||||
Username = username,
|
||||
Candidate = candidate,
|
||||
SdpMid = sdpMid,
|
||||
SdpMLineIndex = sdpMLineIndex,
|
||||
Direction = direction,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<List<RtcIceCandidate>> GetIceCandidatesAsync(string channelId)
|
||||
{
|
||||
var candidates = await _db.Select<RtcIceCandidate>("rtc_ice_candidates");
|
||||
return candidates
|
||||
.Where(x => x.ChannelId == channelId)
|
||||
.OrderBy(x => x.CreatedAt)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if (activeCall is null)
|
||||
return;
|
||||
|
||||
if (activeCall.OfferUser == username)
|
||||
{
|
||||
activeCall.IsActive = false;
|
||||
activeCall.UpdatedAt = DateTime.UtcNow;
|
||||
await _db.Merge<RtcActiveCall, RtcActiveCall>(activeCall);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
using SurrealDb.Net;
|
||||
using SurrealDb.Net.Models.Auth;
|
||||
|
||||
namespace RelayServer.Services;
|
||||
|
||||
public sealed class SurrealService
|
||||
{
|
||||
public async Task<SurrealDbClient> ConnectAsync()
|
||||
{
|
||||
var db = new SurrealDbClient("ws://127.0.0.1:8000/rpc");
|
||||
|
||||
await db.SignIn(new RootAuth
|
||||
{
|
||||
Username = "root",
|
||||
Password = "secret"
|
||||
});
|
||||
|
||||
await db.Use("test", "test");
|
||||
return db;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user