Support avatar video sources for SRTP calls
Use the shared LibVLC audio-video pipeline for camera-free calls on Linux and Android. Add capability signaling, microphone-only Android capture, avatar lifecycle handling and regression tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
97e197996f
commit
f05dd48ac7
27 changed files with 604 additions and 86 deletions
|
|
@ -4,6 +4,7 @@ using System.Runtime.Versioning;
|
|||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Media.Imaging;
|
||||
using PostIt.Services;
|
||||
using Yavsc.Abstract.Chat.Calls;
|
||||
|
||||
|
|
@ -19,6 +20,7 @@ internal sealed class AndroidLanCallMedia : ILanCallMedia
|
|||
private readonly TaskCompletionSource _captureEnded = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
private readonly Task _receiving;
|
||||
private IPodcastCapture? _capture;
|
||||
private Bitmap? _avatar;
|
||||
private bool _disposed;
|
||||
|
||||
public Control LocalVideo => _local;
|
||||
|
|
@ -70,6 +72,20 @@ internal sealed class AndroidLanCallMedia : ILanCallMedia
|
|||
_ = ObserveCaptureAsync(_capture);
|
||||
}
|
||||
|
||||
public async Task StartAvatarSendingAsync(LanCallEndpoint remote, string imagePath, string microphone, CancellationToken ct)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
if (_capture != null) throw new InvalidOperationException("La capture de cet appel est deja demarree.");
|
||||
if (microphone != "microphone") throw new ArgumentException("Microphone Android invalide.", nameof(microphone));
|
||||
using var linked = CancellationTokenSource.CreateLinkedTokenSource(ct, _lifetime.Token);
|
||||
_avatar = new Bitmap(imagePath);
|
||||
_local.Content = new Image { Source = _avatar, Height = 140 };
|
||||
_capture = await AndroidPodcastCapture.StartAvatarCallAsync(
|
||||
(fd, token) => _media.StartAvatarSendingAsync(remote, imagePath, $"fd/aac://{fd}", token),
|
||||
_media.StopAsync, linked.Token);
|
||||
_ = ObserveCaptureAsync(_capture);
|
||||
}
|
||||
|
||||
private async Task ObserveCaptureAsync(IPodcastCapture capture)
|
||||
{
|
||||
try
|
||||
|
|
@ -100,6 +116,7 @@ internal sealed class AndroidLanCallMedia : ILanCallMedia
|
|||
finally
|
||||
{
|
||||
_local.Content = null;
|
||||
_avatar?.Dispose();
|
||||
try { await _media.StopAsync(); }
|
||||
finally
|
||||
{
|
||||
|
|
|
|||
|
|
@ -120,16 +120,29 @@ internal sealed class AndroidPodcastCapture : IPodcastCapture
|
|||
Func<int, CancellationToken, Task> output, Func<Task> stopOutput, CancellationToken ct) =>
|
||||
StartCoreAsync(camera, microphone, null, output, stopOutput, ct, false);
|
||||
|
||||
private static async Task<IPodcastCapture> StartCoreAsync(string camera, string? microphone,
|
||||
internal static Task<IPodcastCapture> StartAvatarCallAsync(
|
||||
Func<int, CancellationToken, Task> output, Func<Task> stopOutput, CancellationToken ct) =>
|
||||
StartCoreAsync(null, "microphone", null, output, stopOutput, ct, false);
|
||||
|
||||
private static async Task<IPodcastCapture> StartCoreAsync(string? camera, string? microphone,
|
||||
Func<Stream, CancellationToken, Task>? output,
|
||||
Func<int, CancellationToken, Task>? pipeOutput, Func<Task>? stopOutput, CancellationToken ct, bool blur)
|
||||
{
|
||||
await RequestPermissionsAsync(microphone, ct);
|
||||
if (camera != null)
|
||||
await RequestPermissionsAsync(microphone, ct);
|
||||
else
|
||||
{
|
||||
var current = MainActivity.Current ?? throw new InvalidOperationException("Activite Android indisponible.");
|
||||
await PodcastPermissions.EnsureAsync(current, true, ct, camera: false);
|
||||
if (!MainActivity.IsForeground)
|
||||
throw new InvalidOperationException("Revenez dans PostIt pour demarrer le microphone.");
|
||||
}
|
||||
var activity = MainActivity.Current ?? throw new InvalidOperationException("Activite Android indisponible.");
|
||||
if (!await CaptureGate.WaitAsync(0, ct))
|
||||
throw new InvalidOperationException("La camera est deja utilisee par un apercu, un podcast ou un appel.");
|
||||
throw new InvalidOperationException("La capture est deja utilisee par un apercu, un podcast ou un appel.");
|
||||
PodcastCaptureService service;
|
||||
try { service = await PodcastCaptureService.StartAsync(activity, microphone is not null, ct, pipeOutput != null); }
|
||||
try { service = await PodcastCaptureService.StartAsync(activity, microphone is not null, ct, pipeOutput != null,
|
||||
camera: camera != null); }
|
||||
catch
|
||||
{
|
||||
CaptureGate.Release();
|
||||
|
|
@ -140,7 +153,11 @@ internal sealed class AndroidPodcastCapture : IPodcastCapture
|
|||
try
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
await capture.StartNativeAsync(camera, microphone is not null, output, pipeOutput, ct, blur);
|
||||
if (camera != null)
|
||||
await capture.StartNativeAsync(camera, microphone is not null, output, pipeOutput, ct, blur);
|
||||
else
|
||||
await capture.StartMicrophoneAsync(pipeOutput
|
||||
?? throw new InvalidOperationException("Sortie microphone de l'appel indisponible."), ct);
|
||||
return capture;
|
||||
}
|
||||
catch
|
||||
|
|
@ -150,6 +167,42 @@ internal sealed class AndroidPodcastCapture : IPodcastCapture
|
|||
}
|
||||
}
|
||||
|
||||
private async Task StartMicrophoneAsync(Func<int, CancellationToken, Task> output, CancellationToken ct)
|
||||
{
|
||||
await _stopGate.WaitAsync(ct);
|
||||
try
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
if (_stopRequested) throw new System.OperationCanceledException("Demarrage de la capture annule.");
|
||||
#pragma warning disable CS0618
|
||||
_recorder = OperatingSystem.IsAndroidVersionAtLeast(31)
|
||||
? new MediaRecorder(_context) : new MediaRecorder();
|
||||
#pragma warning restore CS0618
|
||||
_recorder.Error += OnRecorderError;
|
||||
_recorder.SetAudioSource(AudioSource.Mic);
|
||||
_recorder.SetOutputFormat(OutputFormat.AacAdts);
|
||||
_recorder.SetAudioSamplingRate(48000);
|
||||
_recorder.SetAudioChannels(1);
|
||||
_recorder.SetAudioEncodingBitRate(96000);
|
||||
_recorder.SetAudioEncoder(AudioEncoder.Aac);
|
||||
var pipe = ParcelFileDescriptor.CreatePipe()
|
||||
?? throw new IOException("Creation du canal microphone impossible.");
|
||||
_readPipe = pipe[0];
|
||||
_writePipe = pipe[1];
|
||||
_recorder.SetOutputFile(_writePipe.FileDescriptor);
|
||||
await Task.Run(_recorder.Prepare, ct);
|
||||
await output(_readPipe.Fd, ct);
|
||||
ct.ThrowIfCancellationRequested();
|
||||
if (_stopRequested) throw new System.OperationCanceledException("Demarrage de la capture annule.");
|
||||
await Task.Run(_recorder.Start, ct);
|
||||
_started = true;
|
||||
_writePipe.Close();
|
||||
_writePipe.Dispose();
|
||||
_writePipe = null;
|
||||
}
|
||||
finally { _stopGate.Release(); }
|
||||
}
|
||||
|
||||
private async Task StartNativeAsync(string cameraId, bool microphone,
|
||||
Func<Stream, CancellationToken, Task>? output,
|
||||
Func<int, CancellationToken, Task>? pipeOutput, CancellationToken ct, bool blur)
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ public sealed class PodcastCaptureService : Service
|
|||
internal AndroidPodcastCapture? Capture { get; set; }
|
||||
|
||||
public static async Task<PodcastCaptureService> StartAsync(Activity activity, bool microphone,
|
||||
CancellationToken ct, bool call = false)
|
||||
CancellationToken ct, bool call = false, bool camera = true)
|
||||
{
|
||||
if (_pending is not null) throw new InvalidOperationException("Un direct Android est deja en cours.");
|
||||
var pending = new TaskCompletionSource<PodcastCaptureService>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
|
|
@ -30,7 +30,7 @@ public sealed class PodcastCaptureService : Service
|
|||
try
|
||||
{
|
||||
var intent = new Intent(activity, typeof(PodcastCaptureService))
|
||||
.PutExtra("microphone", microphone).PutExtra("call", call);
|
||||
.PutExtra("microphone", microphone).PutExtra("call", call).PutExtra("camera", camera);
|
||||
activity.StartForegroundService(intent);
|
||||
return await pending.Task.WaitAsync(TimeSpan.FromSeconds(10), ct);
|
||||
}
|
||||
|
|
@ -68,8 +68,10 @@ public sealed class PodcastCaptureService : Service
|
|||
var builder = new NotificationCompat.Builder(this, Channel);
|
||||
builder.SetSmallIcon(global::Android.Resource.Drawable.IcMenuCamera);
|
||||
var call = intent?.GetBooleanExtra("call", false) == true;
|
||||
builder.SetContentTitle(call ? "Appel audio-video LAN/VPN" : "Podcast en direct");
|
||||
builder.SetContentText(call ? "Camera et microphone actifs. Toucher pour revenir a l'appel."
|
||||
var camera = intent?.GetBooleanExtra("camera", true) != false;
|
||||
builder.SetContentTitle(call ? camera ? "Appel audio-video LAN/VPN" : "Appel audio LAN/VPN" : "Podcast en direct");
|
||||
builder.SetContentText(call ? camera ? "Camera et microphone actifs. Toucher pour revenir a l'appel."
|
||||
: "Avatar et microphone actifs. Toucher pour revenir a l'appel."
|
||||
: "Camera active. Toucher pour revenir au direct.");
|
||||
builder.SetContentIntent(open);
|
||||
builder.SetOngoing(true);
|
||||
|
|
@ -78,7 +80,7 @@ public sealed class PodcastCaptureService : Service
|
|||
var type = (ForegroundService)0;
|
||||
if (OperatingSystem.IsAndroidVersionAtLeast(30))
|
||||
{
|
||||
type = ForegroundService.TypeCamera;
|
||||
if (camera) type = ForegroundService.TypeCamera;
|
||||
if (intent?.GetBooleanExtra("microphone", false) == true) type |= ForegroundService.TypeMicrophone;
|
||||
}
|
||||
ServiceCompat.StartForeground(this, NotificationId, notification, (int)type);
|
||||
|
|
|
|||
|
|
@ -32,9 +32,11 @@ internal static class PodcastPermissions
|
|||
_pending = pending;
|
||||
ActivityCompat.RequestPermissions(activity, permissions, RequestCode);
|
||||
if (!await pending.Task.WaitAsync(ct))
|
||||
throw new UnauthorizedAccessException(notifications
|
||||
throw new UnauthorizedAccessException(!camera && microphone
|
||||
? notifications ? "Autorisez le microphone et les notifications pour l'appel."
|
||||
: "Autorisez le microphone pour l'appel."
|
||||
: notifications
|
||||
? "Autorisez la camera, les notifications et, si selectionne, le microphone pour diffuser."
|
||||
: microphone && !camera ? "Autorisez le microphone pour l'appel."
|
||||
: microphone ? "Autorisez la camera et le microphone pour l'appel."
|
||||
: "Autorisez la camera pour la previsualisation.");
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue