Refactor VlcVideoView for enhanced video playback performance and user interaction. Update README.md to clarify the new control's functionality, including frame handling and buffer management. Modify VideoPlayerView.axaml for improved layout and error message display. Enable unsafe code blocks in project settings to optimize frame copying process.

This commit is contained in:
Leonid Pershin
2026-08-09 06:40:44 +03:00
parent 7d88fe0a01
commit b13d0148df
4 changed files with 492 additions and 306 deletions
+13 -14
View File
@@ -78,20 +78,19 @@ dotnet test
только по «Сохранить», так что отмена не оставляет следов. Пересканирование запускается
только если изменилось то, что влияет на состав библиотеки, — смена темы или ширины кадра
его не вызывает.
- **Плеер — свой контрол `VlcVideoView` поверх LibVLCSharp.** Avalonia создаёт нативное
дочернее окно, VLC рисует прямо в него: ни один кадр не проходит через управляемую память.
Расплата — airspace: поверх видео ничего нарисовать нельзя, поэтому контролы и сообщения
об ошибках живут рядом с картинкой, а не на ней. Транспорт (позиция, длительность,
play/pause) — свойства самого контрола, поэтому им управляет code-behind страницы;
дублировать это состояние во вьюмодель значило бы держать вторую копию и синхронизировать её.
Закрытие страницы обнуляет `OpenedVideo`, вью уходит из дерева, `DestroyNativeControlCore`
гасит плеер.
По той же причине в полноэкранном режиме остаётся тонкая полоса управления внизу:
всплывающего оверлея поверх видео нативная поверхность не допускает, а движение мыши над
ней до Avalonia не доходитавтоскрытию не на что реагировать.
Готовый `MediaPlayer.Controls` пробовали до этого: декодер работал, но кадры до экрана
не доходили — чёрный экран и на GPU-, и на CPU-пути, при полностью рабочем в приложении
`OpenGlControlBase`. Свой контрол ни от чьей версии Avalonia не зависит.
- **Плеер — свой контрол `VlcVideoView` поверх LibVLCSharp.** VLC декодирует в память,
которую мы ему выдаём, а рисуем кадр сами: видео остаётся обычным контролом Avalonia —
участвует в hit-тесте, принимает жесты, поверх него можно класть что угодно. Цена — одно
копирование на показанный кадр, и на 4K оно становится основной стоимостью воспроизведения.
Буферов два: VLC декодирует в один, пока мы читаем другой; на `Display` они меняются
местами под коротким локом. Транспорт (позиция, длительность, play/pause) — свойства самого
контрола, поэтому им управляет code-behind страницы; дублировать это состояние во вьюмодель
значило бы держать вторую копию и синхронизировать её. Закрытие страницы обнуляет
`OpenedVideo`, вью уходит из дерева, и плеер гасится вместе с буферами.
До этого пробовали два готовых пути. `MediaPlayer.Controls`: декодер работал, но кадры до
экрана не доходиличёрный экран и на GPU-, и на CPU-пути, при полностью рабочем в
приложении `OpenGlControlBase`. Нативное окно VLC через `NativeControlHost`: картинка
появилась, но окно поверх поверхности Avalonia не пропускает ни клик, ни оверлей.
- **Кэш превью самовосстанавливается.** Диск — ключ `sha256(путь|размер|mtime)`, память — LRU
на 256 декодированных битмапов. Сканирование проверяет, что запомненный кадр физически
на месте (`IThumbnailGenerator.IsAvailable`), и перерисовывает удалённые; после полного
+237 -53
View File
@@ -1,5 +1,9 @@
using System.Runtime.InteropServices;
using System.Text;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using Avalonia.Threading;
using LibVLCSharp.Shared;
@@ -7,21 +11,20 @@ using LibVLCSharp.Shared;
namespace PLib.Desktop.Controls;
/// <summary>
/// A video surface backed by LibVLC.
/// A video surface backed by LibVLC that draws through Avalonia.
/// </summary>
/// <remarks>
/// VLC draws straight into a native child window that Avalonia creates for us, so no frame
/// ever crosses into managed memory — playback costs what the decoder costs and nothing more.
/// The price is airspace: this region is a separate window on top of the Avalonia surface, so
/// nothing can be drawn over the picture. The media page keeps its controls beside the video
/// rather than on it for exactly that reason.
/// VLC decodes into memory we hand it and we paint the result ourselves, so the video is an
/// ordinary control: it takes part in hit testing, gestures reach it, and anything can be
/// drawn on top. The alternative — letting VLC render into a native child window — costs
/// nothing per frame but puts the picture in its own window above the Avalonia surface,
/// where no click and no overlay can reach it.
/// <para>
/// The property surface deliberately mirrors what a media control is expected to expose —
/// source, position, duration, volume — so the page binds to it the same way it would to any
/// other player.
/// The price paid here is one copy per displayed frame. At 1080p that is a few megabytes;
/// at 4K it becomes the dominant cost of playback.
/// </para>
/// </remarks>
public sealed class VlcVideoView : NativeControlHost
public sealed class VlcVideoView : Control
{
public static readonly StyledProperty<Uri?> SourceProperty =
AvaloniaProperty.Register<VlcVideoView, Uri?>(nameof(Source));
@@ -48,13 +51,43 @@ public sealed class VlcVideoView : NativeControlHost
public static readonly StyledProperty<string?> LastErrorProperty =
AvaloniaProperty.Register<VlcVideoView, string?>(nameof(LastError));
private MediaPlayer? _player;
/// <summary>VLC's 32-bit packed BGRX; matches <see cref="PixelFormat.Bgra8888"/> byte for byte.</summary>
private const string Chroma = "RV32";
/// <summary>
/// True once VLC has been handed the native window. Playback cannot start before that,
/// or VLC opens a top-level window of its own.
/// Guards the frame buffers and the bitmap. Held only for a pointer swap on the decoder
/// side and for the copy on the render side, never across a call into VLC.
/// </summary>
private bool _surfaceReady;
private readonly Lock _gate = new();
// Kept in fields because they are handed to native code: a local delegate would be
// collected while VLC still holds the function pointer.
private readonly MediaPlayer.LibVLCVideoFormatCb _formatCallback;
private readonly MediaPlayer.LibVLCVideoCleanupCb _cleanupCallback;
private readonly MediaPlayer.LibVLCVideoLockCb _lockCallback;
private readonly MediaPlayer.LibVLCVideoDisplayCb _displayCallback;
private MediaPlayer? _player;
private WriteableBitmap? _bitmap;
/// <summary>The buffer VLC is decoding into right now.</summary>
private IntPtr _back;
/// <summary>The most recently completed frame, safe to read while VLC fills the other one.</summary>
private IntPtr _front;
private int _width;
private int _height;
private int _stride;
private bool _hasNewFrame;
public VlcVideoView()
{
_formatCallback = OnFormat;
_cleanupCallback = OnCleanup;
_lockCallback = OnLock;
_displayCallback = OnDisplay;
}
public Uri? Source
{
@@ -106,12 +139,7 @@ public sealed class VlcVideoView : NativeControlHost
public void Play()
{
if (_player is null)
{
return;
}
if (_player.Media is not null)
if (_player?.Media is not null)
{
_player.Play();
}
@@ -127,65 +155,80 @@ public sealed class VlcVideoView : NativeControlHost
public void Seek(TimeSpan position)
{
if (_player is { IsSeekable: true })
if (_player is { IsSeekable: true } player)
{
_player.Time = (long)position.TotalMilliseconds;
player.Time = (long)position.TotalMilliseconds;
}
}
protected override IPlatformHandle CreateNativeControlCore(IPlatformHandle parent)
public override void Render(DrawingContext context)
{
// Avalonia gives us an empty child window of the right platform kind; VLC renders
// into it once we hand over the handle.
var handle = base.CreateNativeControlCore(parent);
Rect source;
WriteableBitmap bitmap;
lock (_gate)
{
if (_width <= 0 || _height <= 0 || _front == IntPtr.Zero)
{
return;
}
EnsureBitmap();
bitmap = _bitmap!;
if (_hasNewFrame)
{
using var locked = bitmap.Lock();
CopyFrame(_front, _stride, locked.Address, locked.RowBytes, _height);
_hasNewFrame = false;
}
source = new Rect(0, 0, _width, _height);
}
context.DrawImage(bitmap, source, FitInto(source.Size, Bounds.Size));
}
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
_player = new MediaPlayer(VlcRuntime.Shared);
AttachPlayerEvents(_player);
if (OperatingSystem.IsWindows())
{
_player.Hwnd = handle.Handle;
}
else if (OperatingSystem.IsLinux())
{
_player.XWindow = (uint)handle.Handle;
}
else if (OperatingSystem.IsMacOS())
{
_player.NsObject = handle.Handle;
}
// VLC grabs mouse and keyboard on its own window by default, which swallows every
// gesture before Avalonia can see it. We do not need DVD menus, so hand input back.
_player.EnableMouseInput = false;
_player.EnableKeyInput = false;
_player.SetVideoFormatCallbacks(_formatCallback, _cleanupCallback);
_player.SetVideoCallbacks(_lockCallback, null, _displayCallback);
_player.Mute = IsMuted;
_player.Volume = ToVlcVolume(Volume);
_surfaceReady = true;
if (AutoPlay)
{
OpenCurrentSource();
}
return handle;
}
protected override void DestroyNativeControlCore(IPlatformHandle control)
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
// Tear the player down before the window it draws into disappears.
base.OnDetachedFromVisualTree(e);
if (_player is { } player)
{
_player = null;
_surfaceReady = false;
DetachPlayerEvents(player);
// Stop before releasing anything: the callbacks must not fire into freed memory.
player.Stop();
player.Dispose();
}
base.DestroyNativeControlCore(control);
lock (_gate)
{
ReleaseBuffers();
_bitmap?.Dispose();
_bitmap = null;
}
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
@@ -213,7 +256,7 @@ public sealed class VlcVideoView : NativeControlHost
private void OpenCurrentSource()
{
if (!_surfaceReady || _player is not { } player || Source is not { } source)
if (_player is not { } player || Source is not { } source)
{
return;
}
@@ -236,6 +279,73 @@ public sealed class VlcVideoView : NativeControlHost
}
}
// ======================= VLC video callbacks =======================
// All of these run on VLC's own threads. Nothing here may touch an Avalonia property
// directly, and nothing may block for long: the decoder is waiting.
private uint OnFormat(
ref IntPtr opaque,
IntPtr chroma,
ref uint width,
ref uint height,
ref uint pitches,
ref uint lines)
{
var frameWidth = (int)width;
var frameHeight = (int)height;
var stride = frameWidth * 4;
Marshal.Copy(Encoding.ASCII.GetBytes(Chroma), 0, chroma, 4);
pitches = (uint)stride;
lines = (uint)frameHeight;
lock (_gate)
{
ReleaseBuffers();
_width = frameWidth;
_height = frameHeight;
_stride = stride;
var size = stride * frameHeight;
_back = Marshal.AllocHGlobal(size);
_front = Marshal.AllocHGlobal(size);
_hasNewFrame = false;
}
// One buffer set; VLC hands it back through the lock callback.
return 1;
}
private void OnCleanup(ref IntPtr opaque)
{
lock (_gate)
{
ReleaseBuffers();
}
}
private IntPtr OnLock(IntPtr opaque, IntPtr planes)
{
Marshal.WriteIntPtr(planes, 0, _back);
return _back;
}
private void OnDisplay(IntPtr opaque, IntPtr picture)
{
lock (_gate)
{
// Swap rather than copy: the finished frame becomes readable and VLC carries on
// decoding into the buffer we were showing.
(_front, _back) = (_back, _front);
_hasNewFrame = true;
}
Dispatcher.UIThread.Post(InvalidateVisual, DispatcherPriority.Render);
}
// ======================= Player events =======================
private void AttachPlayerEvents(MediaPlayer player)
{
player.TimeChanged += OnTimeChanged;
@@ -258,8 +368,6 @@ public sealed class VlcVideoView : NativeControlHost
player.EncounteredError -= OnEncounteredError;
}
// Every VLC event arrives on one of its own threads, so nothing here may touch an
// Avalonia property directly.
private void OnTimeChanged(object? sender, MediaPlayerTimeChangedEventArgs e) =>
Post(() => Position = TimeSpan.FromMilliseconds(Math.Max(0, e.Time)));
@@ -273,6 +381,82 @@ public sealed class VlcVideoView : NativeControlHost
private void OnEncounteredError(object? sender, EventArgs e) =>
Post(() => LastError = "VLC сообщил об ошибке воспроизведения");
// ======================= Helpers =======================
/// <summary>Must be called with <see cref="_gate"/> held.</summary>
private void EnsureBitmap()
{
if (_bitmap is not null &&
_bitmap.PixelSize.Width == _width &&
_bitmap.PixelSize.Height == _height)
{
return;
}
_bitmap?.Dispose();
_bitmap = new WriteableBitmap(
new PixelSize(_width, _height),
new Vector(96, 96),
PixelFormat.Bgra8888,
AlphaFormat.Opaque);
}
/// <summary>Must be called with <see cref="_gate"/> held.</summary>
private void ReleaseBuffers()
{
if (_back != IntPtr.Zero)
{
Marshal.FreeHGlobal(_back);
_back = IntPtr.Zero;
}
if (_front != IntPtr.Zero)
{
Marshal.FreeHGlobal(_front);
_front = IntPtr.Zero;
}
_width = 0;
_height = 0;
_stride = 0;
_hasNewFrame = false;
}
private static unsafe void CopyFrame(IntPtr source, int sourceStride, IntPtr target, int targetStride, int rows)
{
var rowBytes = Math.Min(sourceStride, targetStride);
if (sourceStride == targetStride)
{
Buffer.MemoryCopy((void*)source, (void*)target, (long)targetStride * rows, (long)rowBytes * rows);
return;
}
for (var row = 0; row < rows; row++)
{
Buffer.MemoryCopy(
(void*)(source + (row * sourceStride)),
(void*)(target + (row * targetStride)),
targetStride,
rowBytes);
}
}
/// <summary>Largest rectangle of the source aspect ratio that fits, centred.</summary>
private static Rect FitInto(Size source, Size available)
{
if (source.Width <= 0 || source.Height <= 0 || available.Width <= 0 || available.Height <= 0)
{
return default;
}
var scale = Math.Min(available.Width / source.Width, available.Height / source.Height);
var width = source.Width * scale;
var height = source.Height * scale;
return new Rect((available.Width - width) / 2, (available.Height - height) / 2, width, height);
}
private static void Post(Action action) => Dispatcher.UIThread.Post(action, DispatcherPriority.Background);
private static int ToVlcVolume(double volume) => (int)Math.Round(Math.Clamp(volume, 0, 1) * 100);
+3
View File
@@ -7,6 +7,9 @@
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<ApplicationManifest>app.manifest</ApplicationManifest>
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
<!-- The video control copies decoded frames between unmanaged buffers and the bitmap;
a managed round trip would double the work on every frame. -->
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
+13 -13
View File
@@ -61,26 +61,26 @@
</Border>
<!-- ======================= Video ======================= -->
<!-- VLC paints into a native child window, which sits above the Avalonia surface, so
the error message lives in its own row instead of on top of the picture. -->
<Grid Grid.Row="1" RowDefinitions="*,Auto">
<Panel Grid.Row="0" Name="VideoArea" Background="Black">
<controls:VlcVideoView Name="Player"
Source="{Binding Source}"
AutoPlay="True"
Volume="0.8" />
</Panel>
<Panel Grid.Row="1" Name="VideoArea" Background="Black">
<controls:VlcVideoView Name="Player"
Source="{Binding Source}"
AutoPlay="True"
Volume="0.8" />
<Border Grid.Row="1"
Name="ErrorBar"
<Border Name="ErrorBar"
HorizontalAlignment="Center"
VerticalAlignment="Center"
MaxWidth="460"
CornerRadius="10"
Background="{DynamicResource SurfaceBrush}"
Padding="16,10"
Padding="16,12"
IsVisible="False">
<TextBlock Name="ErrorText"
TextWrapping="Wrap"
TextAlignment="Center"
Foreground="{DynamicResource TextSecondaryBrush}" />
</Border>
</Grid>
</Panel>
<!-- ======================= Transport ======================= -->
<Border Grid.Row="2" Classes="panelFooter" Padding="16,10">