Refactor video playback implementation in PLib video library manager. Replace MediaPlayer.Controls with LibVLCSharp for improved performance and compatibility. Update VideoPlayerView and associated ViewModels to accommodate the new video control, ensuring seamless integration with the existing UI. Revise README.md to document changes and new player features.

This commit is contained in:
Leonid Pershin
2026-08-08 15:19:14 +03:00
parent 09d2a553a8
commit 45814cf4d2
11 changed files with 530 additions and 217 deletions
+6 -3
View File
@@ -12,9 +12,12 @@
<PackageVersion Include="Avalonia.Fonts.Inter" Version="12.1.1" />
<PackageVersion Include="Semi.Avalonia" Version="12.1.0.1" />
<PackageVersion Include="Material.Icons.Avalonia" Version="3.0.2" />
<PackageVersion Include="MediaPlayer.Controls" Version="12.0.0" />
<!-- Native LibVLC runtime: without it the player falls back to Media Foundation,
which cannot open MKV, AVI or WebM — most of the library. -->
</ItemGroup>
<ItemGroup Label="Playback">
<!-- LibVLCSharp is UI-agnostic; the video surface is our own control, so nothing here
is tied to a particular Avalonia release. -->
<PackageVersion Include="LibVLCSharp" Version="3.10.1" />
<PackageVersion Include="VideoLAN.LibVLC.Windows" Version="3.0.23.1" />
</ItemGroup>
+9 -6
View File
@@ -78,14 +78,17 @@ dotnet test
только по «Сохранить», так что отмена не оставляет следов. Пересканирование запускается
только если изменилось то, что влияет на состав библиотеки, — смена темы или ширины кадра
его не вызывает.
- **Плеер — `GpuMediaPlayer` из `MediaPlayer.Controls`.** Он наследует `OpenGlControlBase`,
то есть рисует внутрь композиции Avalonia, а не в нативное дочернее окно: контролы можно
класть поверх видео, чего дал бы не всякий плеер. Транспорт (позиция, длительность,
- **Плеер — свой контрол `VlcVideoView` поверх LibVLCSharp.** Avalonia создаёт нативное
дочернее окно, VLC рисует прямо в него: ни один кадр не проходит через управляемую память.
Расплата — airspace: поверх видео ничего нарисовать нельзя, поэтому контролы и сообщения
об ошибках живут рядом с картинкой, а не на ней. Транспорт (позиция, длительность,
play/pause) — свойства самого контрола, поэтому им управляет code-behind страницы;
дублировать это состояние во вьюмодель значило бы держать вторую копию и синхронизировать её.
Закрытие страницы обнуляет `OpenedVideo`, вью уходит из дерева — и декодер останавливается.
- **Нативный LibVLC подключён намеренно.** Без него бэкенд откатывается на Media Foundation,
который не открывает MKV, AVI и WebM — то есть половину того, что сканер кладёт в библиотеку.
Закрытие страницы обнуляет `OpenedVideo`, вью уходит из дерева, `DestroyNativeControlCore`
гасит плеер.
Готовый `MediaPlayer.Controls` пробовали до этого: декодер работал, но кадры до экрана
не доходили — чёрный экран и на GPU-, и на CPU-пути, при полностью рабочем в приложении
`OpenGlControlBase`. Свой контрол ни от чьей версии Avalonia не зависит.
- **Кэш превью самовосстанавливается.** Диск — ключ `sha256(путь|размер|mtime)`, память — LRU
на 256 декодированных битмапов. Сканирование проверяет, что запомненный кадр физически
на месте (`IThumbnailGenerator.IsAvailable`), и перерисовывает удалённые; после полного
+29
View File
@@ -0,0 +1,29 @@
using LibVLCSharp.Shared;
namespace PLib.Desktop.Controls;
/// <summary>
/// The one <see cref="LibVLC"/> instance the application uses.
/// </summary>
/// <remarks>
/// Creating a LibVLC instance spins up a whole media framework, so it is shared rather than
/// made per player view. A static holder rather than a DI service because the consumer is a
/// control, and controls are built by the XAML runtime with no container in reach.
/// </remarks>
internal static class VlcRuntime
{
private static readonly Lazy<LibVLC> Instance = new(
() =>
{
// Locates the native binaries that VideoLAN.LibVLC.Windows drops next to the app.
Core.Initialize();
return new LibVLC(
"--no-osd",
"--no-video-title-show",
"--no-snapshot-preview");
},
LazyThreadSafetyMode.ExecutionAndPublication);
public static LibVLC Shared => Instance.Value;
}
+274
View File
@@ -0,0 +1,274 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Platform;
using Avalonia.Threading;
using LibVLCSharp.Shared;
namespace PLib.Desktop.Controls;
/// <summary>
/// A video surface backed by LibVLC.
/// </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.
/// <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.
/// </para>
/// </remarks>
public sealed class VlcVideoView : NativeControlHost
{
public static readonly StyledProperty<Uri?> SourceProperty =
AvaloniaProperty.Register<VlcVideoView, Uri?>(nameof(Source));
public static readonly StyledProperty<bool> AutoPlayProperty =
AvaloniaProperty.Register<VlcVideoView, bool>(nameof(AutoPlay), defaultValue: true);
public static readonly StyledProperty<TimeSpan> PositionProperty =
AvaloniaProperty.Register<VlcVideoView, TimeSpan>(nameof(Position));
public static readonly StyledProperty<TimeSpan> DurationProperty =
AvaloniaProperty.Register<VlcVideoView, TimeSpan>(nameof(Duration));
public static readonly StyledProperty<bool> IsPlayingProperty =
AvaloniaProperty.Register<VlcVideoView, bool>(nameof(IsPlaying));
public static readonly StyledProperty<bool> IsMutedProperty =
AvaloniaProperty.Register<VlcVideoView, bool>(nameof(IsMuted));
/// <summary>Volume as a fraction; LibVLC works in percent and is converted on the way in.</summary>
public static readonly StyledProperty<double> VolumeProperty =
AvaloniaProperty.Register<VlcVideoView, double>(nameof(Volume), defaultValue: 0.8);
public static readonly StyledProperty<string?> LastErrorProperty =
AvaloniaProperty.Register<VlcVideoView, string?>(nameof(LastError));
private MediaPlayer? _player;
/// <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.
/// </summary>
private bool _surfaceReady;
public Uri? Source
{
get => GetValue(SourceProperty);
set => SetValue(SourceProperty, value);
}
public bool AutoPlay
{
get => GetValue(AutoPlayProperty);
set => SetValue(AutoPlayProperty, value);
}
public TimeSpan Position
{
get => GetValue(PositionProperty);
private set => SetValue(PositionProperty, value);
}
public TimeSpan Duration
{
get => GetValue(DurationProperty);
private set => SetValue(DurationProperty, value);
}
public bool IsPlaying
{
get => GetValue(IsPlayingProperty);
private set => SetValue(IsPlayingProperty, value);
}
public bool IsMuted
{
get => GetValue(IsMutedProperty);
set => SetValue(IsMutedProperty, value);
}
public double Volume
{
get => GetValue(VolumeProperty);
set => SetValue(VolumeProperty, value);
}
public string? LastError
{
get => GetValue(LastErrorProperty);
private set => SetValue(LastErrorProperty, value);
}
public void Play()
{
if (_player is null)
{
return;
}
if (_player.Media is not null)
{
_player.Play();
}
else
{
OpenCurrentSource();
}
}
public void Pause() => _player?.SetPause(true);
public void Stop() => _player?.Stop();
public void Seek(TimeSpan position)
{
if (_player is { IsSeekable: true })
{
_player.Time = (long)position.TotalMilliseconds;
}
}
protected override IPlatformHandle CreateNativeControlCore(IPlatformHandle parent)
{
// 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);
_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;
}
_player.Mute = IsMuted;
_player.Volume = ToVlcVolume(Volume);
_surfaceReady = true;
if (AutoPlay)
{
OpenCurrentSource();
}
return handle;
}
protected override void DestroyNativeControlCore(IPlatformHandle control)
{
// Tear the player down before the window it draws into disappears.
if (_player is { } player)
{
_player = null;
_surfaceReady = false;
DetachPlayerEvents(player);
player.Stop();
player.Dispose();
}
base.DestroyNativeControlCore(control);
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (_player is not { } player)
{
return;
}
if (change.Property == SourceProperty)
{
OpenCurrentSource();
}
else if (change.Property == VolumeProperty)
{
player.Volume = ToVlcVolume(Volume);
}
else if (change.Property == IsMutedProperty)
{
player.Mute = IsMuted;
}
}
private void OpenCurrentSource()
{
if (!_surfaceReady || _player is not { } player || Source is not { } source)
{
return;
}
try
{
LastError = null;
// The media object only has to survive the call: VLC takes its own reference.
using var media = new Media(VlcRuntime.Shared, source);
if (!player.Play(media))
{
LastError = "VLC не смог открыть файл";
}
}
catch (Exception ex)
{
LastError = ex.Message;
}
}
private void AttachPlayerEvents(MediaPlayer player)
{
player.TimeChanged += OnTimeChanged;
player.LengthChanged += OnLengthChanged;
player.Playing += OnPlaying;
player.Paused += OnStoppedPlaying;
player.Stopped += OnStoppedPlaying;
player.EndReached += OnStoppedPlaying;
player.EncounteredError += OnEncounteredError;
}
private void DetachPlayerEvents(MediaPlayer player)
{
player.TimeChanged -= OnTimeChanged;
player.LengthChanged -= OnLengthChanged;
player.Playing -= OnPlaying;
player.Paused -= OnStoppedPlaying;
player.Stopped -= OnStoppedPlaying;
player.EndReached -= OnStoppedPlaying;
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)));
private void OnLengthChanged(object? sender, MediaPlayerLengthChangedEventArgs e) =>
Post(() => Duration = TimeSpan.FromMilliseconds(Math.Max(0, e.Length)));
private void OnPlaying(object? sender, EventArgs e) => Post(() => IsPlaying = true);
private void OnStoppedPlaying(object? sender, EventArgs e) => Post(() => IsPlaying = false);
private void OnEncounteredError(object? sender, EventArgs e) =>
Post(() => LastError = "VLC сообщил об ошибке воспроизведения");
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);
}
+1 -1
View File
@@ -23,7 +23,7 @@
<PackageReference Include="Avalonia.Fonts.Inter" />
<PackageReference Include="Semi.Avalonia" />
<PackageReference Include="Material.Icons.Avalonia" />
<PackageReference Include="MediaPlayer.Controls" />
<PackageReference Include="LibVLCSharp" />
<PackageReference Include="VideoLAN.LibVLC.Windows" Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'true'" />
<PackageReference Include="ReactiveUI.Avalonia" />
<PackageReference Include="ReactiveUI.SourceGenerators">
@@ -5,9 +5,9 @@ namespace PLib.Desktop.ViewModels;
internal static class DisposableExtensions
{
/// <summary>
/// Parks a subscription in the owner's bag so it dies with the owner. ReactiveUI 24 moved
/// its own <c>DisposeWith</c> into a namespace whose operator set collides with
/// System.Reactive's, so this project keeps its own two-line version instead.
/// Parks a subscription in the owner's bag so it dies with the owner. Spelled out here
/// rather than pulled from an Rx extension namespace, because more than one library in
/// this project ships a <c>DisposeWith</c> and importing either invites ambiguity.
/// </summary>
public static void AddTo(this IDisposable disposable, CompositeDisposable subscriptions) =>
subscriptions.Add(disposable);
@@ -14,8 +14,8 @@ using PLib.Desktop.Services;
using PLib.Desktop.Settings;
using ReactiveUI;
using ReactiveUI.SourceGenerators;
// Type alias, not a namespace import: pulling in ReactiveUI.Primitives would put a second
// set of Rx operators next to System.Reactive's and make every Select/Subscribe ambiguous.
// Alias for readability: ReactiveCommand<Unit, Unit> says nothing, and the name survived
// a swap of the underlying void type when the ReactiveUI version changed.
using RxVoid = ReactiveUI.Primitives.RxVoid;
namespace PLib.Desktop.ViewModels;
@@ -48,9 +48,9 @@ public sealed partial class MainWindowViewModel : ViewModelBase
private readonly SourceCache<VideoCardViewModel, Guid> _library = new(card => card.Id);
/// <summary>
/// DynamicData is built on System.Reactive, whose schedulers are a different abstraction
/// from ReactiveUI 24's. Avalonia's synchronisation context bridges the two: posting to
/// it is posting to the dispatcher.
/// The scheduler the DynamicData chain hops to before touching the bound collection.
/// Built on Avalonia's synchronisation context, so posting to it is posting to the
/// dispatcher — named explicitly rather than taken from ambient state.
/// </summary>
private readonly IScheduler _uiScheduler =
new SynchronizationContextScheduler(new AvaloniaSynchronizationContext());
+17 -13
View File
@@ -1,7 +1,7 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:icons="clr-namespace:Material.Icons.Avalonia;assembly=Material.Icons.Avalonia"
xmlns:media="clr-namespace:MediaPlayer.Controls;assembly=MediaPlayer.Controls"
xmlns:controls="clr-namespace:PLib.Desktop.Controls"
xmlns:vm="clr-namespace:PLib.Desktop.ViewModels"
x:Class="PLib.Desktop.Views.VideoPlayerView"
x:DataType="vm:VideoPlayerViewModel">
@@ -61,23 +61,27 @@
</Border>
<!-- ======================= Video ======================= -->
<Panel Grid.Row="1" Background="Black">
<media:GpuMediaPlayer Name="Player"
<!-- 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" Background="Black">
<controls:VlcVideoView Name="Player"
Source="{Binding Source}"
AutoPlay="True"
LayoutMode="Fit"
Volume="0.8" />
<TextBlock Name="ErrorText"
HorizontalAlignment="Center"
VerticalAlignment="Center"
MaxWidth="440"
TextWrapping="Wrap"
TextAlignment="Center"
IsVisible="False"
Foreground="{DynamicResource TextSecondaryBrush}" />
</Panel>
<Border Grid.Row="1"
Name="ErrorBar"
Background="{DynamicResource SurfaceBrush}"
Padding="16,10"
IsVisible="False">
<TextBlock Name="ErrorText"
TextWrapping="Wrap"
Foreground="{DynamicResource TextSecondaryBrush}" />
</Border>
</Grid>
<!-- ======================= Transport ======================= -->
<Border Grid.Row="2" Classes="panelFooter" Padding="16,10">
<Grid ColumnDefinitions="Auto,Auto,*,Auto,Auto,Auto" ColumnSpacing="10">
@@ -3,7 +3,7 @@ using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using MediaPlayer.Controls;
using PLib.Desktop.Controls;
using PLib.Desktop.ViewModels;
namespace PLib.Desktop.Views;
@@ -12,7 +12,7 @@ namespace PLib.Desktop.Views;
/// Transport controls for the media page.
/// </summary>
/// <remarks>
/// Playback state lives on <see cref="GpuMediaPlayer"/> itself — position, duration and
/// Playback state lives on <see cref="VlcVideoView"/> itself — position, duration and
/// whether it is playing are all its own properties, and seeking is a method call. Mirroring
/// that into the view model would buy nothing but a second copy to keep in sync, so this
/// code-behind wires the buttons straight to the control. Everything the page knows about
@@ -36,10 +36,10 @@ public sealed partial class VideoPlayerView : UserControl
VolumeSlider.PropertyChanged += OnVolumeChanged;
Player.GetObservable(GpuMediaPlayer.PositionProperty).Subscribe(OnPositionChanged);
Player.GetObservable(GpuMediaPlayer.DurationProperty).Subscribe(OnDurationChanged);
Player.GetObservable(GpuMediaPlayer.IsPlayingProperty).Subscribe(OnIsPlayingChanged);
Player.GetObservable(GpuMediaPlayer.LastErrorProperty).Subscribe(OnErrorChanged);
Player.GetObservable(VlcVideoView.PositionProperty).Subscribe(OnPositionChanged);
Player.GetObservable(VlcVideoView.DurationProperty).Subscribe(OnDurationChanged);
Player.GetObservable(VlcVideoView.IsPlayingProperty).Subscribe(OnIsPlayingChanged);
Player.GetObservable(VlcVideoView.LastErrorProperty).Subscribe(OnErrorChanged);
}
@@ -114,7 +114,7 @@ public sealed partial class VideoPlayerView : UserControl
? null
: $"Не удалось воспроизвести файл: {error}";
ErrorText.IsVisible = !string.IsNullOrWhiteSpace(error);
ErrorBar.IsVisible = !string.IsNullOrWhiteSpace(error);
}
private static Material.Icons.MaterialIconKind VolumeIconFor(double volume, bool isMuted) =>