swapping to webview webrtc setup as temp solution

will build a custom C# webrtc implementation later
This commit is contained in:
2026-03-26 03:32:58 -04:00
parent 3d5c35fb15
commit a5772d7579
9 changed files with 266 additions and 149 deletions

View File

@@ -2,13 +2,10 @@
using RelayClient.Models;
using WebSocketSharp;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.MixedReality.WebRTC;
using System.IO;
using System.Collections.Generic;
namespace RelayClient;
public partial class MainPage : ContentPage
{
private readonly string _username;
@@ -19,14 +16,14 @@ public partial class MainPage : ContentPage
private readonly Dictionary<string, List<ChatMessage>> _messagesByChannel = new();
private readonly List<ChannelItem> _channels = new();
public MainPage(string username)
{
InitializeComponent();
_username = username;
UserLabel.Text = $"Logged in as: {_username}";
if (!KeyStorage.HasKeys(_username))
{
var keys = E2EeHelper.GenerateRsaKeyPair();
@@ -38,13 +35,12 @@ public partial class MainPage : ContentPage
_wsc.OnMessage += WscOnMessage;
_wsc.Connect();
var publicKey = KeyStorage.LoadPublicKey(_username);
_wsc.Send($"REGISTER_KEY|{_username}|{publicKey}");
_wsc.Send("GET_SERVER_KEY");
_wsc.Send("GET_CHANNELS");
_ = webRTCTest();
}
private void SendButton_OnClicked(object? sender, EventArgs e)
@@ -217,7 +213,7 @@ public partial class MainPage : ContentPage
_wsc.Close();
base.OnDisappearing();
}
private void RenderChannelList()
{
SidebarList.Children.Clear();
@@ -246,7 +242,7 @@ public partial class MainPage : ContentPage
SidebarList.Children.Add(button);
}
}
private void RenderCurrentChannelMessages()
{
MessagesLayout.Children.Clear();
@@ -262,7 +258,7 @@ public partial class MainPage : ContentPage
RenderSingleMessage(message);
}
}
private async void RenderSingleMessage(ChatMessage message)
{
bool isOwnMessage = message.SenderUsername == _username;
@@ -293,146 +289,20 @@ public partial class MainPage : ContentPage
await MessagesScrollView.ScrollToAsync(MessagesLayout, ScrollToPosition.End, true);
}
private async Task webRTCTest()
private void SwapView_OnClicked(object? sender, EventArgs e)
{
var path = Path.GetFullPath(
Path.Combine(AppContext.BaseDirectory, @"..\..\..\debug.txt")
);
void Log(string msg)
if (RtcView.IsVisible)
{
File.AppendAllText(path, msg + Environment.NewLine);
Console.WriteLine(msg);
MessagesScrollView.IsVisible = true;
RtcView.IsVisible = false;
ViewSwapped.Text = "Swap to Message View";
}
AudioTrackSource? microphoneSource = null;
DeviceVideoTrackSource? webcamSource = null;
Transceiver? audioTransceiver = null;
Transceiver? videoTransceiver = null;
LocalAudioTrack? localAudioTrack = null;
LocalVideoTrack? localVideoTrack = null;
Log("=== WebRTC TEST START ===");
IReadOnlyList<VideoCaptureDevice>? deviceList = null;
try
else
{
Log("Getting video devices...");
deviceList = await DeviceVideoTrackSource.GetCaptureDevicesAsync();
foreach (var device in deviceList)
{
Log($"Found device: {device.name} (id: {device.id})");
}
Log("Device enumeration complete.");
}
catch (Exception ex)
{
Log("Device enumeration FAILED:");
Log(ex.ToString());
return;
}
try
{
Log("Creating PeerConnection...");
using var pc = new PeerConnection();
Log("PeerConnection created.");
var config = new PeerConnectionConfiguration
{
IceServers =
{
new IceServer { Urls = { "stun:stun.l.google.com:19302" } }
}
};
Log("Config created.");
Log("Calling InitializeAsync...");
await pc.InitializeAsync(config);
Log("InitializeAsync SUCCESS.");
if (deviceList.Count > 0)
{
try
{
Log($"Getting formats for first device: {deviceList[0].name}");
var formats = await DeviceVideoTrackSource.GetCaptureFormatsAsync(deviceList[0].id);
foreach (var f in formats)
{
Log($"Format: {f.width}x{f.height} @ {f.framerate}");
}
}
catch (Exception ex)
{
Log("Format enumeration FAILED:");
Log(ex.ToString());
}
}
Log("Creating webcam source (default device)...");
webcamSource = await DeviceVideoTrackSource.CreateAsync();
Log("Webcam source created.");
Log("Creating local video track...");
var videoTrackConfig = new LocalVideoTrackInitConfig
{
trackName = "webcam_track"
};
localVideoTrack = LocalVideoTrack.CreateFromSource(webcamSource, videoTrackConfig);
Log("Local video track created.");
Log("Creating microphone source...");
microphoneSource = await DeviceAudioTrackSource.CreateAsync();
Log("Microphone source created.");
Log("Creating local audio track...");
var audioTrackConfig = new LocalAudioTrackInitConfig
{
trackName = "microphone_track"
};
localAudioTrack = LocalAudioTrack.CreateFromSource(microphoneSource, audioTrackConfig);
Log("Local audio track created.");
Log("Adding video transceiver...");
videoTransceiver = pc.AddTransceiver(MediaKind.Video);
videoTransceiver.LocalVideoTrack = localVideoTrack;
videoTransceiver.DesiredDirection = Transceiver.Direction.SendReceive;
Log("Video transceiver configured.");
Log("Adding audio transceiver...");
audioTransceiver = pc.AddTransceiver(MediaKind.Audio);
audioTransceiver.LocalAudioTrack = localAudioTrack;
audioTransceiver.DesiredDirection = Transceiver.Direction.SendReceive;
Log("Audio transceiver configured.");
Log("WebRTC setup complete.");
}
catch (Exception ex)
{
Log("WebRTC setup FAILED:");
Log(ex.ToString());
}
finally
{
Log("Disposing WebRTC resources...");
localAudioTrack?.Dispose();
localVideoTrack?.Dispose();
microphoneSource?.Dispose();
webcamSource?.Dispose();
Log("Resource disposal complete.");
Log("=== WebRTC TEST END ===");
MessagesScrollView.IsVisible = false;
RtcView.IsVisible = true;
ViewSwapped.Text = "Swap to Web View";
}
}
}