Skip to content

Commit

Permalink
chore: adjust video position
Browse files Browse the repository at this point in the history
  • Loading branch information
rafael-rosa-knowcode authored and jeromelaban committed May 25, 2023
1 parent 0690df0 commit 38d1519
Show file tree
Hide file tree
Showing 5 changed files with 309 additions and 81 deletions.
178 changes: 133 additions & 45 deletions src/AddIns/Uno.UI.MediaPlayer.Skia.Gtk/GTKMediaPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@
using System.Collections.Immutable;
using System.IO;
using Uno.Extensions;
using Windows.Foundation;
using Windows.UI.Xaml.Media;

namespace Uno.UI.Media;

public partial class GTKMediaPlayer : Button
{
private LibVLC? _libvlc;
private LibVLCSharp.Shared.MediaPlayer? _mediaPlayer;
private ContentControl _videoContainer;
private VideoView? _videoView;
private double _ratio;
//public int VideoHeight;
//public int VideoWidth;
public double Duration { get; set; }
private readonly ImmutableArray<string> audioTagAllowedFormats = ImmutableArray.Create(new string[] { ".MP3", ".WAV" });
private readonly ImmutableArray<string> videoTagAllowedFormats = ImmutableArray.Create(new string[] { ".MP4", ".WEBM", ".OGG" });
Expand Down Expand Up @@ -58,26 +63,36 @@ public GTKMediaPlayer()
// this.Log().Debug("Adding media elements");
//}

Loaded += OnLoaded;
Unloaded += OnUnloaded;
//Loaded += OnLoaded;
//Unloaded += OnUnloaded;
//RaiseLoaded();
Initialized();

SizeChanged += GTKMediaPlayer_SizeChanged;
}

private void GTKMediaPlayer_SizeChanged(object sender, SizeChangedEventArgs args)
{
UpdateVideoStretch();
}

public void Play()
{
if (_videoView != null && _mediaPlayer != null)
{
Console.WriteLine($"Play");
_mediaPlayer.Play();
_videoView.Visible = true;
//_videoView?.SizeAllocate(new(0, 0, (int)416, (int)240));
}
//UpdateVideoStretch();
}

public void Stop()
{
if (_videoView != null && _mediaPlayer != null)
{
Console.WriteLine($"Stop");
_mediaPlayer.Stop();
_videoView.Visible = false;
}
Expand All @@ -87,6 +102,7 @@ public void Pause()
{
if (_videoView != null && _mediaPlayer != null)
{
Console.WriteLine($"Pause");
_mediaPlayer.Pause();
_videoView.Visible = true;
}
Expand All @@ -96,6 +112,7 @@ public void SetVolume(int volume)
{
if (_videoView != null && _mediaPlayer != null)
{
Console.WriteLine($"SetVolume ({volume})");
_mediaPlayer.Volume = volume;
_videoView.Visible = true;
}
Expand All @@ -115,66 +132,137 @@ public string Source

internal void UpdateVideoStretch()
{
Console.WriteLine("UpdateVideoStretch");
//_ = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
//{
if (_videoView != null && _mediaPlayer != null && _mediaPlayer.Media != null)
_ = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
try
if (_videoView != null && _mediaPlayer != null && _mediaPlayer.Media != null)
{
_mediaPlayer.Media.Parse(MediaParseOptions.ParseNetwork);
var width = _videoView.AllocatedWidth;
var height = _videoView.AllocatedHeight;
// var parentRatio = (double)width / global::System.Math.Max(1, height);

Console.WriteLine($"MediaPlayer {width} x {width}");
while (_mediaPlayer.Media.Tracks != null && !_mediaPlayer.Media.Tracks.Any(track => track.TrackType == TrackType.Video))
try
{
Thread.Sleep(100);
}
var width = _videoView.AllocatedWidth;
var height = _videoView.AllocatedHeight;
var parentRatio = (double)width / global::System.Math.Max(1, height);
if (_mediaPlayer.Media.Tracks != null && _mediaPlayer.Media.Tracks.Any(track => track.TrackType == TrackType.Video))
{
var videoTrack = _mediaPlayer.Media.Tracks.FirstOrDefault(track => track.TrackType == TrackType.Video);
var videoSettings = videoTrack.Data.Video;
var videoWidth = videoSettings.Width;
var videoHeight = videoSettings.Height;
if (videoWidth == 0 || videoHeight == 0)
{
return;
}
if (videoWidth == 0 || videoHeight == 0)
_mediaPlayer.Media.Parse(MediaParseOptions.ParseNetwork);
while (!_mediaPlayer.Media.Tracks.Any(track => track.TrackType == TrackType.Video))
{
return;
Thread.Sleep(100);
}
if (width == 1)
// Obtém a faixa de vídeo
var videoTrack = _mediaPlayer.Media.Tracks.FirstOrDefault(track => track.TrackType == TrackType.Video);
if (_mediaPlayer.Media.Tracks.Any(track => track.TrackType == TrackType.Video))
{
width = (int)videoWidth;
}
Ratio = (double)videoWidth / global::System.Math.Max(1, videoHeight);
height = (int)(videoWidth / Ratio);
var videoSettings = videoTrack.Data.Video;
var videoWidth = videoSettings.Width;
var videoHeight = videoSettings.Height;
Console.WriteLine($"Video Ratio {Ratio}");
Console.WriteLine($"VideoView SizeAllocate after Video Ratio {width} x {width}");
_videoView?.SizeAllocate(new(100, 100, width, height));
if (videoWidth == 0 || videoHeight == 0)
{
return;
}
UIElement? container = VisualTreeHelper.GetParent(_videoContainer) as UIElement;
UIElement? containerGrid = VisualTreeHelper.GetParent(container) as UIElement;
if (container != null)
{
Console.WriteLine($"After VideoView SizeAllocate: {width}, Altura: {height}");
//Point relativeLocation = _videoContainer.TransformToVisual(new Point(0, 0), container!);
Point pagePosition = _videoContainer.TransformToVisual(container).TransformPoint(new Point(0, 0));
if (_videoView != null)
{
_videoView.Visible = true;
//relative to the page
pagePosition = container.TransformToVisual(null).TransformPoint(new Point(0, 0));
//containerGrid.Background = new SolidColorBrush(Colors.Yellow);
_videoView?.SizeAllocate(new((int)pagePosition.X, (int)pagePosition.Y, (int)videoWidth, (int)videoHeight));
}
//_videoView?.SetNaturalSize((int)videoHeight, (int)videoWidth);
_videoContainer.Height = (int)videoHeight;
_videoContainer.Width = (int)videoWidth;
//_videoView?.SizeAllocate(new(0, 0, (int)_videoContainer.Width, (int)_videoContainer.Height));
Console.WriteLine($"Largura: {width}, Altura: {height}");
}
}
finally
{
}
}
finally
{
}
}
//});
});
}

protected override Size ArrangeOverride(Size finalSize)
{
UpdateVideoStretch();
return base.ArrangeOverride(finalSize);
}

//internal void UpdateVideoStretch()
//{
// Console.WriteLine("UpdateVideoStretch");
// //_ = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
// //{
// if (_videoView != null && _mediaPlayer != null && _mediaPlayer.Media != null)
// {
// try
// {
// _mediaPlayer.Media.Parse(MediaParseOptions.ParseNetwork);
// var width = _videoView.AllocatedWidth;
// var height = _videoView.AllocatedHeight;
// // var parentRatio = (double)width / global::System.Math.Max(1, height);

// Console.WriteLine($"MediaPlayer {width} x {width}");
// while (_mediaPlayer.Media.Tracks != null && !_mediaPlayer.Media.Tracks.Any(track => track.TrackType == TrackType.Video))
// {
// Thread.Sleep(100);
// }


// if (_mediaPlayer.Media.Tracks != null && _mediaPlayer.Media.Tracks.Any(track => track.TrackType == TrackType.Video))
// {
// var videoTrack = _mediaPlayer.Media.Tracks.FirstOrDefault(track => track.TrackType == TrackType.Video);
// var videoSettings = videoTrack.Data.Video;
// var videoWidth = videoSettings.Width;
// var videoHeight = videoSettings.Height;

// if (videoWidth == 0 || videoHeight == 0)
// {
// return;
// }
// if (videoWidth == 0 || videoHeight == 0)
// {
// return;
// }
// if (width == 1)
// {
// width = (int)videoWidth;
// }

// Ratio = (double)videoWidth / global::System.Math.Max(1, videoHeight);
// height = (int)(videoWidth / Ratio);

// Console.WriteLine($"Video Ratio {Ratio}");
// Console.WriteLine($"VideoView SizeAllocate after Video Ratio {width} x {width}");
// _videoView?.SizeAllocate(new(100, 100, (int)width, (int)height));

// Console.WriteLine($"After VideoView SizeAllocate: {width}, Altura: {height}");

// if (_videoView != null)
// {
// _videoView.Visible = true;
// }
// }
// }
// finally
// {
// }
// }
// //});
//}


private double _playbackRate;
public double PlaybackRate
{
Expand Down
68 changes: 41 additions & 27 deletions src/AddIns/Uno.UI.MediaPlayer.Skia.Gtk/GTKMediaPlayer.events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Uno.Extensions;
using Uno.Logging;
using Pango;
using Windows.UI.Xaml.Media;

namespace Uno.UI.Media;

Expand Down Expand Up @@ -80,7 +81,10 @@ private void OnLoaded(object sender, object args)
//{
// this.Log().Debug($"GTKMediaPlayer Loaded");
//}

if (_mediaPlayer != null && _libvlc != null)
{
return;
}
Console.WriteLine("GTKMediaPlayer OnLoaded");
SourceLoaded += OnSourceVideoLoaded;
Console.WriteLine("Creating libvlc");
Expand Down Expand Up @@ -109,8 +113,6 @@ private void OnLoaded(object sender, object args)
};
Console.WriteLine("Content _videoView on Dispatcher");
Content = _videoView;
//_videoView?.SizeAllocate(new(0, 0, 800, 640));
UpdateVideoStretch();
Console.WriteLine("Created player");
});
Expand Down Expand Up @@ -143,38 +145,50 @@ private void Initialized()
Console.WriteLine("Creating VideoView");
_videoView = new LibVLCSharp.GTK.VideoView();

_videoContainer = new ContentControl
{
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
HorizontalContentAlignment = HorizontalAlignment.Stretch,
VerticalContentAlignment = VerticalAlignment.Stretch,
};

_ = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
Console.WriteLine("Set MediaPlayer");
_videoView.MediaPlayer = _mediaPlayer;
_ = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
_videoView.Visible = true;
_videoView.MediaPlayer = _mediaPlayer;
_mediaPlayer.Stopped += (sender, e) =>
{
Console.WriteLine("Set MediaPlayer");
_videoView.MediaPlayer = _mediaPlayer;
_videoView.Visible = false;
};
//Starts playing
var media = new LibVLCSharp.Shared.Media(
_libvlc,
new Uri("https://ia800201.us.archive.org/12/items/BigBuckBunny_328/BigBuckBunny_512kb.mp4")
);
_ = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
media.Parse(MediaParseOptions.ParseNetwork);
_videoView.MediaPlayer.Media = media;
_videoView.Visible = true;
_videoView.MediaPlayer = _mediaPlayer;
_mediaPlayer.Stopped += (sender, e) =>
{
_videoView.Visible = false;
};
//Starts playing
var media = new LibVLCSharp.Shared.Media(
_libvlc,
new Uri("https://ia800201.us.archive.org/12/items/BigBuckBunny_328/BigBuckBunny_512kb.mp4")
);
Console.WriteLine("Content _videoView on Dispatcher");
_videoContainer.Content = _videoView;
//_videoContainer.Height = 300;
//_videoContainer.Width = 450;
Content = _videoContainer;
media.Parse(MediaParseOptions.ParseNetwork);
_videoView.MediaPlayer.Media = media;
Console.WriteLine("Content _videoView on Dispatcher");
Content = _videoView;
_videoView?.SizeAllocate(new(0, 0, 800, 640));
UpdateVideoStretch();
Console.WriteLine("Created player");
});
});
UpdateVideoStretch();
Console.WriteLine("Created player");
});
});



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void OnPrepared(object? sender, object what)
//{
// this.Log().Debug($"OnPrepared: {mp.VideoWidth}x{mp.VideoHeight}");
//}

mp.UpdateVideoStretch();
Events?.RaiseVideoRatioChanged(global::System.Math.Max(1, (double)mp.Ratio));
}
catch { }
Expand Down
Loading

0 comments on commit 38d1519

Please sign in to comment.