提交 bd9461b2 编写于 作者: J Jerome Laban

chore: Adjust memory cleanup, initial video window position

上级 72125551
......@@ -49,6 +49,17 @@ public partial class GtkMediaPlayer : FrameworkElement
_ = Initialize();
}
~GtkMediaPlayer()
{
if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
{
this.Log().Debug("Collecting GtkMediaPlayer");
}
_mediaPlayer?.Dispose();
_libvlc?.Dispose();
}
public string Source
{
get => (string)GetValue(SourceProperty);
......
#nullable enable
using Windows.UI.Core;
using LibVLCSharp.GTK;
using LibVLCSharp.Shared;
using Windows.UI;
using Windows.UI.Xaml.Controls;
using Microsoft.UI.Xaml;
using System;
using Windows.UI.Xaml;
using System.Threading;
using System.Linq;
using Windows.UI.Notifications;
using Uno.Extensions;
using Uno.Logging;
using Pango;
using Windows.UI.Xaml.Media;
using System.Timers;
using Timer = System.Timers.Timer;
using System.Globalization;
using Windows.Media.Playback;
using System.Threading.Tasks;
using Uno.UI.Runtime.Skia;
using System.Runtime.CompilerServices;
using System.Diagnostics.CodeAnalysis;
namespace Uno.UI.Media;
......@@ -28,6 +17,8 @@ public partial class GtkMediaPlayer
{
private Task? _initializationTask;
private MediaPlayerElement? _mpe;
private static ConditionalWeakTable<object, WeakReference<GtkMediaPlayer>> _playerMap = new();
private static ConditionalWeakTable<object, WeakReference<GtkMediaPlayer>> _videoViewMap = new();
public event EventHandler<object>? OnSourceFailed;
public event EventHandler<object>? OnSourceEnded;
......@@ -62,6 +53,7 @@ public partial class GtkMediaPlayer
}
_mediaPlayer = new LibVLCSharp.Shared.MediaPlayer(_libvlc);
_playerMap.Add(_mediaPlayer, new WeakReference<GtkMediaPlayer>(this));
if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
{
......@@ -69,7 +61,15 @@ public partial class GtkMediaPlayer
}
_videoView = new LibVLCSharp.GTK.VideoView();
_videoView.VideoSurfaceInteraction += OnVideoViewVideoSurfaceInteraction;
_videoViewMap.Add(_videoView, new WeakReference<GtkMediaPlayer>(this));
_videoView.VideoSurfaceInteraction += static (s, e) =>
{
if (GetGtkPlayerForVlcPlayer(s, out var target))
{
target.OnVideoViewVideoSurfaceInteraction(s, e);
}
};
});
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
......@@ -92,10 +92,30 @@ public partial class GtkMediaPlayer
_videoView.Visible = true;
_videoView.MediaPlayer = _mediaPlayer;
_mediaPlayer.TimeChanged += OnMediaPlayerTimeChange;
_mediaPlayer.TimeChanged += OnMediaPlayerTimeChangeIsMediaParse;
_mediaPlayer.MediaChanged += MediaPlayerMediaChanged;
_mediaPlayer.Stopped += OnMediaPlayerStopped;
_mediaPlayer.TimeChanged += static (s, e) =>
{
if (GetGtkPlayerForVlcPlayer(s, out var target))
{
target.OnMediaPlayerTimeChange(s, e);
target.OnMediaPlayerTimeChangeIsMediaParse(s, e);
}
};
_mediaPlayer.MediaChanged += static (s, e) =>
{
if (GetGtkPlayerForVlcPlayer(s, out var target))
{
target.MediaPlayerMediaChanged(s, e);
}
};
_mediaPlayer.Stopped += static (s, e) =>
{
if (GetGtkPlayerForVlcPlayer(s, out var target))
{
target.OnMediaPlayerStopped(s, e);
}
};
_videoContainer.Content = _videoView;
AddChild(_videoContainer);
......@@ -111,6 +131,21 @@ public partial class GtkMediaPlayer
});
}
static bool GetGtkPlayerForVlcPlayer(
object? instance,
[NotNullWhen(true)] out GtkMediaPlayer? player)
{
if (instance is not null
&& _playerMap.TryGetValue(instance, out var weakTarget)
&& weakTarget.TryGetTarget(out player))
{
return true;
}
player = null;
return false;
}
private void OnVideoViewVideoSurfaceInteraction(object? sender, EventArgs e)
{
UpdateMediaPlayerElementReference();
......@@ -162,6 +197,7 @@ public partial class GtkMediaPlayer
media.Parse(MediaParseOptions.ParseNetwork);
_mediaPlayer.Media = media;
AddMediaEvents();
OnSourceLoaded?.Invoke(this, EventArgs.Empty);
UpdateVideoStretch();
......@@ -177,13 +213,12 @@ public partial class GtkMediaPlayer
private void AddMediaEvents()
{
if (_mediaPlayer?.Media is { IsParsed: true } media)
if (_mediaPlayer?.Media is { } media)
{
media.DurationChanged -= DurationChanged;
media.MetaChanged -= MetaChanged;
media.StateChanged -= StateChanged;
media.ParsedChanged -= ParsedChanged;
_mediaPlayer.TimeChanged -= OnMediaPlayerTimeChangeIsMediaParse;
media.DurationChanged += DurationChanged;
media.MetaChanged += MetaChanged;
......
......@@ -24,6 +24,7 @@ using System.Numerics;
using Uno.Logging;
using Windows.UI.Xaml;
using Atk;
using System.Runtime.CompilerServices;
[assembly: ApiExtension(typeof(IMediaPlayerExtension), typeof(Uno.UI.Media.MediaPlayerExtension))]
......@@ -31,7 +32,7 @@ namespace Uno.UI.Media;
public partial class MediaPlayerExtension : IMediaPlayerExtension
{
private static Dictionary<Windows.Media.Playback.MediaPlayer, MediaPlayerExtension> _instances = new();
private static ConditionalWeakTable<Windows.Media.Playback.MediaPlayer, MediaPlayerExtension> _instances = new();
private Uri? _uri;
private List<Uri>? _playlistItems;
......@@ -58,15 +59,7 @@ public partial class MediaPlayerExtension : IMediaPlayerExtension
lock (_instances)
{
_instances[_owner] = this;
}
}
~MediaPlayerExtension()
{
lock (_instances)
{
_instances.Remove(_owner);
_instances.Add(_owner, this);
}
}
......
......@@ -53,6 +53,7 @@ namespace LibVLCSharp.GTK
private MediaPlayer? _mediaPlayer;
private Gtk.Window? _videoWindow;
private Rectangle? _lastArrange;
internal event EventHandler? VideoSurfaceInteraction;
......@@ -79,6 +80,7 @@ namespace LibVLCSharp.GTK
if (Visible)
{
_videoWindow?.Show();
ApplyLastArrange();
}
else
{
......@@ -171,6 +173,8 @@ namespace LibVLCSharp.GTK
// Show the window once the ID has been associated in libVLC
_videoWindow.Show();
ApplyLastArrange();
}
}
......@@ -262,6 +266,14 @@ namespace LibVLCSharp.GTK
}
}
private void ApplyLastArrange()
{
if (_lastArrange is not null)
{
Arrange(_lastArrange.Value);
}
}
internal void Arrange(Gdk.Rectangle value)
{
if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Trace))
......@@ -269,6 +281,8 @@ namespace LibVLCSharp.GTK
this.Log().Trace($"Arranging child window to {value.X}x{value.Y} / {value.Width}x{value.Height}");
}
_lastArrange = value;
_videoWindow?.Window.MoveResize(value.X, value.Y, value.Width, value.Height);
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册