feat(postit): add native WebRTC calls with TURN IP masking

Replace the direct chat call panel with native Android and Linux WebRTC engines, enforce relay-only privacy, and cover consent, lifecycle, rendering and coturn interoperability. Document deployment and upgrade Avalonia to 12.1.6.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Paul Schneider 2026-11-11 09:24:59 +00:00
commit d812266ee0
Signed by: notazof
GPG key ID: AD4B9B545FBD7D12
28 changed files with 2568 additions and 61 deletions

View file

@ -0,0 +1,164 @@
using System;
using System.Diagnostics;
using System.Runtime.Versioning;
using System.Threading;
using System.Threading.Tasks;
using Android.Content;
using Android.Runtime;
using Avalonia.Android;
using Avalonia.Controls;
using Avalonia.Platform;
using PostIt.Services;
namespace PostIt.Android.Services;
[SupportedOSPlatform("android26.0")]
internal sealed class AndroidWebRtcMedia : WebRtcNativeMedia
{
private readonly CancellationTokenSource _lifetime = new();
private readonly Peer _peer;
private readonly Surface _local;
private readonly Surface _remote;
private readonly Task _poll;
private readonly TaskCompletionSource _closed = new(TaskCreationOptions.RunContinuationsAsynchronously);
private bool _disposed;
public override Control LocalVideo => _local;
public override Control RemoteVideo => _remote;
public AndroidWebRtcMedia()
{
_peer = new Peer(MainActivity.Current ?? throw new InvalidOperationException("Activité Android indisponible."));
_local = new Surface(_peer.GetView("localView"));
_remote = new Surface(_peer.GetView("remoteView"));
_poll = Task.Run(PollAsync);
}
protected override async Task StartCoreAsync(WebRtcMediaConfiguration configuration, CancellationToken ct)
{
var activity = MainActivity.Current ?? throw new InvalidOperationException("Activité Android indisponible.");
await PodcastPermissions.EnsureAsync(activity, microphone: true, ct, notifications: false);
if (!MainActivity.IsForeground)
throw new InvalidOperationException("Revenez dans PostIt pour demarrer l'appel.");
await Task.WhenAll(_local.Ready, _remote.Ready).WaitAsync(TimeSpan.FromSeconds(10), ct);
await SendCommandAsync(StartCommand(configuration), ct);
}
protected override Task SendCommandAsync(string json, CancellationToken ct)
{
ct.ThrowIfCancellationRequested();
_peer.Command(json);
return Task.CompletedTask;
}
private async Task PollAsync()
{
var stats = Stopwatch.StartNew();
var failed = false;
try
{
while (!_lifetime.IsCancellationRequested)
{
for (var i = 0; i < 64 && _peer.Poll() is { } message; i++)
{
if (message == "{\"type\":\"closed\"}") _closed.TrySetResult();
if (!failed)
{
try { ProcessMessage(message); }
catch (Exception error)
{
failed = true;
Fail(error);
if (_disposed) _closed.TrySetException(error);
}
}
}
if (!_disposed && !failed && Configuration is not null && stats.Elapsed >= TimeSpan.FromSeconds(1))
{
_peer.Command("{\"command\":\"stats\"}");
stats.Restart();
}
await Task.Delay(20, _lifetime.Token);
}
}
catch (OperationCanceledException) when (_lifetime.IsCancellationRequested) { }
catch (Exception error) { Fail(error); _closed.TrySetException(error); }
}
public override async ValueTask DisposeAsync()
{
if (_disposed) return;
_disposed = true;
try
{
_peer.Command("{\"command\":\"close\"}");
await _closed.Task.WaitAsync(TimeSpan.FromSeconds(10));
}
finally
{
_lifetime.Cancel();
await _poll;
_peer.Dispose();
_lifetime.Dispose();
Closed();
}
}
private sealed class Surface(global::Android.Views.View view) : NativeControlHost
{
private readonly TaskCompletionSource _ready = new(TaskCreationOptions.RunContinuationsAsynchronously);
public Task Ready => _ready.Task;
protected override IPlatformHandle CreateNativeControlCore(IPlatformHandle parent)
{
_ready.TrySetResult();
return new AndroidViewControlHandle(view);
}
}
// Only this small Java bridge crosses JNI. SDP and ICE callbacks stay in Java,
// where the native SDK owns their lifetime and thread affinity.
private sealed class Peer : IDisposable
{
private readonly Java.Lang.Class _type;
private readonly Java.Lang.Object _instance;
private readonly Java.Lang.Reflect.Method _command;
private readonly Java.Lang.Reflect.Method _poll;
public Peer(Context context)
{
_type = Java.Lang.Class.ForName("fr.pschneider.postit.WebRtcPeer")!;
using var contextType = Java.Lang.Class.FromType(typeof(Context));
using var constructor = _type.GetConstructor(contextType);
_instance = constructor!.NewInstance(context)!;
using var stringType = Java.Lang.Class.FromType(typeof(Java.Lang.String));
_command = _type.GetMethod("command", stringType)!;
_poll = _type.GetMethod("poll")!;
}
public global::Android.Views.View GetView(string method)
{
using var getter = _type.GetMethod(method);
return getter!.Invoke(_instance)!.JavaCast<global::Android.Views.View>()!;
}
public void Command(string message)
{
using var value = new Java.Lang.String(message);
_command.Invoke(_instance, value);
}
public string? Poll()
{
using var result = _poll.Invoke(_instance);
return result?.ToString();
}
public void Dispose()
{
_command.Dispose();
_poll.Dispose();
_instance.Dispose();
_type.Dispose();
}
}
}

View file

@ -32,6 +32,7 @@ internal static class PodcastPermissions
if (!await pending.Task.WaitAsync(ct))
throw new UnauthorizedAccessException(notifications
? "Autorisez la camera, les notifications et, si selectionne, le microphone pour diffuser."
: microphone ? "Autorisez la camera et le microphone pour l'appel."
: "Autorisez la camera pour la previsualisation.");
}

View file

@ -0,0 +1,323 @@
package fr.pschneider.postit;
import android.content.Context;
import org.json.JSONArray;
import org.json.JSONObject;
import org.webrtc.*;
import org.webrtc.audio.JavaAudioDeviceModule;
import java.util.*;
import java.util.concurrent.*;
/** Keeps all SDK operations and asynchronous SDP callbacks on one executor. */
public final class WebRtcPeer {
private final Context context;
private final EglBase egl = EglBase.create();
private final SurfaceViewRenderer local;
private final SurfaceViewRenderer remote;
private final ExecutorService executor = Executors.newSingleThreadExecutor();
private final ArrayBlockingQueue<String> events = new ArrayBlockingQueue<>(64);
private final List<IceCandidate> pendingLocal = new ArrayList<>();
private final List<IceCandidate> pendingRemote = new ArrayList<>();
private PeerConnectionFactory factory;
private PeerConnection peer;
private JavaAudioDeviceModule audioModule;
private VideoCapturer capturer;
private SurfaceTextureHelper texture;
private VideoSource videoSource;
private AudioSource audioSource;
private VideoTrack videoTrack;
private AudioTrack audioTrack;
private VideoTrack remoteTrack;
private boolean localDescriptionSent;
private boolean remoteDescriptionSet;
private volatile boolean closed;
private volatile boolean localRendered;
private volatile boolean remoteRendered;
public WebRtcPeer(Context context) {
this.context = context.getApplicationContext();
local = new SurfaceViewRenderer(context);
remote = new SurfaceViewRenderer(context);
local.init(egl.getEglBaseContext(), new RendererCommon.RendererEvents() {
public void onFirstFrameRendered() { localRendered = true; }
public void onFrameResolutionChanged(int width, int height, int rotation) {}
});
local.setMirror(true);
remote.init(egl.getEglBaseContext(), new RendererCommon.RendererEvents() {
public void onFirstFrameRendered() { remoteRendered = true; }
public void onFrameResolutionChanged(int width, int height, int rotation) {}
});
local.setEnableHardwareScaler(true);
remote.setEnableHardwareScaler(true);
}
public android.view.View localView() { return local; }
public android.view.View remoteView() { return remote; }
public String poll() { return events.poll(); }
private void enqueue(Runnable action) {
try {
executor.execute(() -> {
try { action.run(); }
catch (Exception failure) { error(failure.getMessage()); }
});
} catch (RejectedExecutionException failure) {
if (!closed) throw failure;
}
}
public void command(String json) {
enqueue(() -> {
try {
JSONObject message = new JSONObject(json);
switch (message.getString("command")) {
case "start": start(message.getJSONObject("configuration")); break;
case "signal": signal(message.getJSONObject("signal")); break;
case "stats": if (peer != null && !closed) peer.getStats(this::stats); break;
case "close": close(); break;
default: throw new IllegalArgumentException("Unknown WebRTC command");
}
} catch (Exception error) { error(error.getMessage()); }
});
}
private void emit(JSONObject value) {
if (!events.offer(value.toString())) {
events.clear();
events.offer("{\"type\":\"error\",\"message\":\"WebRTC event queue overflow\"}");
}
}
private void error(String message) {
try { emit(new JSONObject().put("type", "error").put("message", message)); }
catch (org.json.JSONException impossible) { throw new IllegalStateException(impossible); }
}
private void start(JSONObject configuration) throws Exception {
if (factory != null || closed) throw new IllegalStateException("WebRTC already started or closed");
PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions.builder(context)
.setEnableInternalTracer(false).createInitializationOptions());
audioModule = JavaAudioDeviceModule.builder(context).createAudioDeviceModule();
factory = PeerConnectionFactory.builder().setAudioDeviceModule(audioModule)
.setVideoEncoderFactory(new DefaultVideoEncoderFactory(egl.getEglBaseContext(), true, true))
.setVideoDecoderFactory(new DefaultVideoDecoderFactory(egl.getEglBaseContext())).createPeerConnectionFactory();
List<PeerConnection.IceServer> servers = new ArrayList<>();
JSONArray configuredServers = configuration.getJSONArray("iceServers");
boolean relay = configuration.getBoolean("relayOnly");
for (int i = 0; i < configuredServers.length(); i++) {
JSONObject item = configuredServers.getJSONObject(i);
JSONArray urls = item.getJSONArray("urls");
for (int j = 0; j < urls.length(); j++) {
String url = urls.getString(j);
if (relay && !url.startsWith("turn:") && !url.startsWith("turns:")) continue;
PeerConnection.IceServer.Builder server = PeerConnection.IceServer.builder(url);
if (!item.isNull("username")) server.setUsername(item.getString("username"));
if (!item.isNull("credential")) server.setPassword(item.getString("credential"));
servers.add(server.createIceServer());
}
}
PeerConnection.RTCConfiguration rtc = new PeerConnection.RTCConfiguration(servers);
rtc.sdpSemantics = PeerConnection.SdpSemantics.UNIFIED_PLAN;
rtc.bundlePolicy = PeerConnection.BundlePolicy.MAXBUNDLE;
rtc.rtcpMuxPolicy = PeerConnection.RtcpMuxPolicy.REQUIRE;
rtc.iceTransportsType = relay ? PeerConnection.IceTransportsType.RELAY : PeerConnection.IceTransportsType.ALL;
peer = factory.createPeerConnection(rtc, new Observer());
if (peer == null) throw new IllegalStateException("Cannot create native WebRTC peer");
Camera2Enumerator cameras = new Camera2Enumerator(context);
String camera = configuration.getString("camera");
capturer = cameras.createCapturer(camera, new CameraVideoCapturer.CameraEventsHandler() {
public void onCameraError(String message) { error("Camera: " + message); }
public void onCameraDisconnected() { if (!closed) error("Camera disconnected"); }
public void onCameraFreezed(String message) { error("Camera frozen: " + message); }
public void onCameraOpening(String name) {}
public void onFirstFrameAvailable() {}
public void onCameraClosed() { if (!closed) error("Camera closed unexpectedly"); }
});
if (capturer == null) throw new IllegalArgumentException("Camera unavailable");
texture = SurfaceTextureHelper.create("PostIt-WebRTC-camera", egl.getEglBaseContext());
videoSource = factory.createVideoSource(false);
capturer.initialize(texture, context, videoSource.getCapturerObserver());
videoTrack = factory.createVideoTrack("postit-video", videoSource);
videoTrack.addSink(local);
audioSource = factory.createAudioSource(new MediaConstraints());
audioTrack = factory.createAudioTrack("postit-audio", audioSource);
peer.addTrack(audioTrack, Collections.singletonList("postit"));
peer.addTrack(videoTrack, Collections.singletonList("postit"));
capturer.startCapture(640, 480, 24);
if (configuration.getBoolean("caller")) peer.createOffer(new CreateDescription("offer"), new MediaConstraints());
}
private abstract class SdpCallback implements SdpObserver {
public void onCreateSuccess(SessionDescription description) {}
public void onSetSuccess() {}
public void onCreateFailure(String message) { error("SDP creation failed: " + message); }
public void onSetFailure(String message) { error("SDP application failed: " + message); }
}
private final class CreateDescription extends SdpCallback {
private final String kind;
CreateDescription(String kind) { this.kind = kind; }
public void onCreateSuccess(SessionDescription description) {
enqueue(() -> {
if (closed) return;
peer.setLocalDescription(new SdpCallback() {
public void onSetSuccess() {
enqueue(() -> {
if (closed) return;
try {
emit(new JSONObject().put("type", "signal").put("signal",
new JSONObject().put("kind", kind).put("sdp", description.description)));
localDescriptionSent = true;
for (IceCandidate candidate : pendingLocal) sendCandidate(candidate);
pendingLocal.clear();
} catch (Exception failure) { error(failure.getMessage()); }
});
}
}, description);
});
}
}
private void signal(JSONObject signal) throws Exception {
if (closed || peer == null) throw new IllegalStateException("WebRTC peer not running");
String kind = signal.getString("kind");
if (kind.equals("candidate")) {
IceCandidate candidate = new IceCandidate(signal.getString("sdpMid"),
signal.getInt("sdpMLineIndex"), signal.getString("candidate"));
if (remoteDescriptionSet) addCandidate(candidate);
else {
if (pendingRemote.size() >= 256) throw new IllegalStateException("Too many pending ICE candidates");
pendingRemote.add(candidate);
}
} else if (kind.equals("offer") || kind.equals("answer")) {
peer.setRemoteDescription(new SdpCallback() {
public void onSetSuccess() {
enqueue(() -> {
if (closed) return;
remoteDescriptionSet = true;
for (IceCandidate candidate : pendingRemote) addCandidate(candidate);
pendingRemote.clear();
if (kind.equals("offer")) peer.createAnswer(new CreateDescription("answer"), new MediaConstraints());
});
}
}, new SessionDescription(kind.equals("offer") ? SessionDescription.Type.OFFER : SessionDescription.Type.ANSWER,
signal.getString("sdp")));
} else throw new IllegalArgumentException("Invalid WebRTC signal");
}
private void addCandidate(IceCandidate candidate) {
peer.addIceCandidate(candidate, new AddIceObserver() {
public void onAddSuccess() {}
public void onAddFailure(String message) { error("ICE candidate rejected: " + message); }
});
}
private void sendCandidate(IceCandidate candidate) {
try {
emit(new JSONObject().put("type", "signal").put("signal", new JSONObject()
.put("kind", "candidate").put("candidate", candidate.sdp)
.put("sdpMid", candidate.sdpMid).put("sdpMLineIndex", candidate.sdpMLineIndex)));
} catch (Exception failure) { error(failure.getMessage()); }
}
private void stats(RTCStatsReport report) {
enqueue(() -> {
if (closed) return;
try {
Map<String, RTCStats> all = report.getStatsMap();
String selectedId = null, localType = null;
long audioPackets = 0, videoFrames = 0;
for (RTCStats item : all.values()) {
Map<String, Object> values = item.getMembers();
if (item.getType().equals("transport") && values.get("selectedCandidatePairId") instanceof String)
selectedId = (String)values.get("selectedCandidatePairId");
if (item.getType().equals("inbound-rtp")) {
String media = String.valueOf(values.getOrDefault("kind", values.get("mediaType")));
if (media.equals("audio")) audioPackets += number(values.get("packetsReceived"));
if (media.equals("video")) videoFrames += number(values.get("framesDecoded"));
}
}
RTCStats pair = all.get(selectedId);
if (pair != null) {
RTCStats candidate = all.get(String.valueOf(pair.getMembers().get("localCandidateId")));
if (candidate != null) localType = String.valueOf(candidate.getMembers().get("candidateType"));
}
String state = peer.connectionState().toString().toLowerCase(Locale.ROOT);
if (state.equals("connected") && localType == null) state = "checking";
emit(new JSONObject().put("type", "status").put("status", new JSONObject()
.put("connectionState", state).put("localCandidateType", localType)
.put("audioPackets", audioPackets).put("videoFrames", videoFrames)
.put("localVideoRendered", localRendered).put("remoteVideoRendered", remoteRendered)));
} catch (Exception failure) { error(failure.getMessage()); }
});
}
private static long number(Object value) { return value instanceof Number ? ((Number)value).longValue() : 0; }
private final class Observer implements PeerConnection.Observer {
public void onSignalingChange(PeerConnection.SignalingState state) {}
public void onIceConnectionChange(PeerConnection.IceConnectionState state) {}
public void onIceConnectionReceivingChange(boolean receiving) {}
public void onIceGatheringChange(PeerConnection.IceGatheringState state) {}
public void onIceCandidatesRemoved(IceCandidate[] candidates) {}
public void onAddStream(MediaStream stream) {}
public void onRemoveStream(MediaStream stream) {}
public void onDataChannel(DataChannel channel) {}
public void onRenegotiationNeeded() {}
public void onIceCandidate(IceCandidate candidate) {
enqueue(() -> {
if (closed) return;
if (localDescriptionSent) sendCandidate(candidate);
else if (pendingLocal.size() < 256) pendingLocal.add(candidate);
else error("Too many local ICE candidates");
});
}
public void onTrack(RtpTransceiver transceiver) {
MediaStreamTrack track = transceiver.getReceiver().track();
if (track instanceof VideoTrack) {
enqueue(() -> {
if (closed) return;
remoteTrack = (VideoTrack)track;
remoteTrack.addSink(remote);
});
}
}
}
private interface Cleanup { void run() throws Exception; }
private void release(Cleanup cleanup, List<String> failures) {
try { cleanup.run(); }
catch (Exception failure) { failures.add(failure.toString()); }
}
private void close() {
if (closed) return;
closed = true;
List<String> failures = new ArrayList<>();
if (capturer != null) release(() -> capturer.stopCapture(), failures);
if (remoteTrack != null) release(() -> remoteTrack.removeSink(remote), failures);
if (videoTrack != null) release(() -> videoTrack.removeSink(local), failures);
if (peer != null) {
release(() -> peer.close(), failures);
release(() -> peer.dispose(), failures);
}
if (videoTrack != null) release(() -> videoTrack.dispose(), failures);
if (audioTrack != null) release(() -> audioTrack.dispose(), failures);
if (capturer != null) release(() -> capturer.dispose(), failures);
if (texture != null) release(() -> texture.dispose(), failures);
if (videoSource != null) release(() -> videoSource.dispose(), failures);
if (audioSource != null) release(() -> audioSource.dispose(), failures);
if (factory != null) release(() -> factory.dispose(), failures);
if (audioModule != null) release(() -> audioModule.release(), failures);
new android.os.Handler(android.os.Looper.getMainLooper()).post(() -> {
release(() -> local.release(), failures);
release(() -> remote.release(), failures);
release(() -> egl.release(), failures);
if (!failures.isEmpty()) error("WebRTC cleanup failed: " + String.join("; ", failures));
try { emit(new JSONObject().put("type", "closed")); }
catch (Exception failure) { error(failure.getMessage()); }
});
executor.shutdown();
}
}