Skip to content

Commit

Permalink
Merge pull request #1 from SuRGeoNix/master
Browse files Browse the repository at this point in the history
Fixing Thread.Abort() on decoder/demuxer
  • Loading branch information
robvdpol authored Apr 21, 2021
2 parents b9470c6 + dd19b73 commit 908e06e
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 89 deletions.
5 changes: 2 additions & 3 deletions FlyleafLib.Controls.WPF/Flyleaf.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</Storyboard>

<Storyboard x:Key="fadeOut">
<DoubleAnimation BeginTime="0:0:0" Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:2.0" AutoReverse="False"/>
<DoubleAnimation BeginTime="0:0:0" Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:2.0" AutoReverse="False"/>
</Storyboard>
</ResourceDictionary>
</ControlTemplate.Resources>
Expand All @@ -35,11 +35,10 @@
<Grid x:Name="PART_ContextMenuOwner" ContextMenu="{StaticResource PopUpMenu}">

<!--Pop Up Dialog (Settings/Set Values)-->
<materialDesign:DialogHost x:Name="PART_DialogSettings"/> <!--Identifier="DialogSettings"/>-->
<materialDesign:DialogHost x:Name="PART_DialogSettings"/>

<!--Subtitles-->
<TextBlock x:Name="PART_Subtitles" Margin="0 0 0 50" VerticalAlignment="Bottom" HorizontalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" FontSize="33" Foreground="White" FontWeight="Bold" Text="{Binding Session.SubsText}"/>
<!--<TextBlock x:Name="PART_Subtitles" Margin="0 0 0 50" VerticalAlignment="Bottom" HorizontalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" FontSize="33" Foreground="White" FontWeight="Bold" Text="fldaskjfldksajflksadj"/>-->

<!--Flyleaf Bar-->
<materialDesign:Card TextElement.FontSize="14" TextElement.FontWeight="Medium" TextElement.Foreground="{DynamicResource MaterialDesignBody}" FontFamily="{materialDesign:MaterialDesignFont}" VerticalAlignment="Bottom" Focusable="False" Background="{StaticResource RadialBrush}">
Expand Down
100 changes: 42 additions & 58 deletions FlyleafLib.Controls.WPF/Flyleaf.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,15 @@ public int IdleTimeout

#region Initialize
public TextBlock Subtitles { get; set; }
Thickness subsInitialMargin;

ContextMenu popUpMenu, popUpMenuSubtitles, popUpMenuVideo;
MenuItem popUpAspectRatio;
MenuItem popUpKeepAspectRatio;
MenuItem popUpCustomAspectRatio;
MenuItem popUpCustomAspectRatioSet;
string dialogSettingsIdentifier;
bool playerInitialized;

public Flyleaf()
{
Expand All @@ -85,55 +88,9 @@ public override void OnApplyTemplate()
if (IsDesignMode) return;

Initialize();
//if (Player != null)

//templateAppied = true;
}

bool templateAppied = false;
bool playerInitialized = false;

private void InitializePlayer()
{
if (playerInitialized) return;
playerInitialized = true;

FlyleafView.Resources = Resources;
FlyleafView.FontFamily = FontFamily;
FlyleafView.FontSize = FontSize;

// Keys
if (EnableKeyBindings)
{
WindowFront.KeyDown += Flyleaf_KeyDown;
FlyleafView.KeyDown += Flyleaf_KeyDown;
WindowFront.KeyUp += Flyleaf_KeyUp;
FlyleafView.KeyUp += Flyleaf_KeyUp;
}

// Mouse (this will not fire on mouse events use Control)
if (EnableMouseEvents)
{
WindowFront.MouseMove += Flyleaf_MouseMove;
Player.Control.MouseMove += Control_MouseMove;

Player.Control.DoubleClick += Control_DoubleClick;
Player.Control.MouseClick += Control_MouseClick;
}

// Drag & Drop
Player.Control.AllowDrop = true;
Player.Control.DragEnter += Control_DragEnter;
Player.Control.DragDrop += Control_DragDrop;

// Player / Session
Player.OpenCompleted += Player_OpenCompleted;
}

private void Initialize()
{
//Console.WriteLine(Player.PlayerId + " fldkasjflkdsajflksafdj lkdfasklfjdsalkfdjsa flkdsajfklsadjfklsa");

popUpMenu = ((FrameworkElement)Template.FindName("PART_ContextMenuOwner", this))?.ContextMenu;
popUpMenuSubtitles = ((FrameworkElement)Template.FindName("PART_ContextMenuOwner_Subtitles", this))?.ContextMenu;
popUpMenuVideo = ((FrameworkElement)Template.FindName("PART_ContextMenuOwner_Video", this))?.ContextMenu;
Expand Down Expand Up @@ -187,8 +144,6 @@ private void Initialize()
};
}



RegisterCommands();
if (Subtitles != null)
{
Expand All @@ -208,11 +163,45 @@ private void Initialize()
}

}
private void InitializePlayer()
{
if (playerInitialized) return;
playerInitialized = true;

FlyleafView.Resources = Resources;
FlyleafView.FontFamily = FontFamily;
FlyleafView.FontSize = FontSize;

// Keys
if (EnableKeyBindings)
{
WindowFront.KeyDown += Flyleaf_KeyDown;
FlyleafView.KeyDown += Flyleaf_KeyDown;
WindowFront.KeyUp += Flyleaf_KeyUp;
FlyleafView.KeyUp += Flyleaf_KeyUp;
}

// Mouse (this will not fire on mouse events use Control)
if (EnableMouseEvents)
{
WindowFront.MouseMove += Flyleaf_MouseMove;
Player.Control.MouseMove += Control_MouseMove;

Player.Control.DoubleClick += Control_DoubleClick;
Player.Control.MouseClick += Control_MouseClick;
}

// Drag & Drop
Player.Control.AllowDrop = true;
Player.Control.DragEnter += Control_DragEnter;
Player.Control.DragDrop += Control_DragDrop;

// Player / Session
Player.OpenCompleted += Player_OpenCompleted;
}
#endregion

#region ICommands
Thickness subsInitialMargin;

void RegisterCommands()
{
TogglePlayPause = new RelayCommand(TogglePlayPauseAction); //, p => Session.CanPlay);
Expand Down Expand Up @@ -246,27 +235,23 @@ void RegisterCommands()

public ICommand OpenStream { get; set; }
public void OpenStreamAction(object stream) { Player.Open((StreamBase)stream); }
public ICommand ResetSubsPositionY { get; set; }

public ICommand ResetSubsPositionY { get; set; }
public void ResetSubsPositionYAction(object obj = null) { Subtitles.Margin = subsInitialMargin; }

public ICommand ResetSubsDelayMs { get; set; }

public void ResetSubsDelayMsAction(object obj = null) { Subs.DelayTicks = 0; }

public ICommand ResetAudioDelayMs { get; set; }

public void ResetAudioDelayMsAction(object obj = null) { Audio.DelayTicks = 0; }

public ICommand SetSubsPositionY { get; set; }


public void SetSubsPositionYAction(object y) { Thickness t = Subtitles.Margin; t.Bottom += int.Parse(y.ToString()); Subtitles.Margin = t; Raise("Subtitles"); }

public ICommand SetAudioDelayMs { get; set; }
public void SetAudioDelayMsAction(object delay) { Audio.DelayTicks += (int.Parse(delay.ToString())) * (long)10000; }

public ICommand SetSubsDelayMs { get; set; }

public void SetSubsDelayMsAction(object delay) { Subs.DelayTicks += (int.Parse(delay.ToString())) * (long)10000; }

public ICommand OpenFromFileDialog { get; set; }
Expand Down Expand Up @@ -336,6 +321,7 @@ public void SetSubtitlesFontAction(object obj = null)
SubtitlesFontColor = Subtitles.Foreground;
}
}

public ICommand OpenSettings { get; set; }
public async void OpenSettingsAction(object obj = null)
{
Expand Down Expand Up @@ -372,7 +358,6 @@ public void TogglePlayPauseAction(object obj = null)
}

public ICommand ToggleFullscreen { get; set; }

public void ToggleFullscreenAction(object obj = null)
{
if (FlyleafView.IsFullScreen)
Expand All @@ -393,7 +378,6 @@ public void Open(string url)
}
public void Player_OpenCompleted(object sender, Player.OpenCompletedArgs e)
{
Console.WriteLine($"{Player.PlayerId} {e.type} {e.success} fldksajflksdajffdslakjfsdalkfjas");
switch (e.type)
{
case MediaType.Video:
Expand Down
2 changes: 1 addition & 1 deletion FlyleafLib.Plugins.YoutubeDL/YoutubeDL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public OpenVideoResults OpenVideo()
try
{
uri = new Uri(Session.InitialUrl);
if (uri.Scheme.ToLower() != "http" && uri.Scheme.ToLower() != "https") return null;
if ((uri.Scheme.ToLower() != "http" && uri.Scheme.ToLower() != "https") || Utils.GetUrlExtention(Session.InitialUrl).ToLower() == "m3u8") return null;
} catch (Exception) { return null; }

try
Expand Down
13 changes: 2 additions & 11 deletions FlyleafLib/Controls/Flyleaf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,8 @@ namespace FlyleafLib.Controls
public partial class Flyleaf : UserControl
{
public Player Player { get; internal set; }

public Flyleaf()
{
//Console.WriteLine("Control Created");
///*InitializeComponent();*/
//Player = new Player();
//Player.Control = this;
//BackColor = System.Drawing.Color.Black;
}

protected override void OnPaintBackground(PaintEventArgs pe) { Player?.renderer?.PresentFrame(); }
public Flyleaf() { BackColor = System.Drawing.Color.Black; }
protected override void OnPaintBackground(PaintEventArgs pe) { Player?.renderer?.PresentFrame(); }
protected override void OnPaint(PaintEventArgs pe) { Player?.renderer?.PresentFrame(); } // renderer?.PresentFrame(); }
}
}
4 changes: 3 additions & 1 deletion FlyleafLib/Controls/WPF/VideoView.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* This class is based on https://github.com/videolan/libvlcsharp/tree/3.x/src/LibVLCSharp.WPF */

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
Expand Down Expand Up @@ -46,6 +47,7 @@ private static void OnPlayerChanged(DependencyObject d, DependencyPropertyChange
Player Player = e.NewValue as Player;
Player.VideoView = VideoView;
Player.Control = VideoView.FlyleafWF;
Console.WriteLine("OnPlayerChanged() Sets Control to Player");
if (VideoView.ControlRequiresPlayer != null) VideoView.ControlRequiresPlayer.Player = Player;
}

Expand Down Expand Up @@ -79,6 +81,7 @@ public override void OnApplyTemplate()
{
Player.VideoView = this;
Player.Control = FlyleafWF;
Console.WriteLine("OnApplyTemplate() Sets Control to Player");
if (ControlRequiresPlayer != null) ControlRequiresPlayer.Player = Player;
}
}
Expand All @@ -96,7 +99,6 @@ protected override void OnContentChanged(object oldContent, object newContent)
}
}


public bool IsFullScreen { get ; private set; }

object oldContent, oldParent;
Expand Down
12 changes: 10 additions & 2 deletions FlyleafLib/MediaFramework/Decoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,14 @@ public void Flush()
}
public void Close()
{
if (decodeThread != null && decodeThread.IsAlive)
{
forcePause = true;
if (!isPlaying) decodeARE.Set();
Utils.EnsureThreadDone(decodeThread);
}

if (status == Status.None) return;
if (decodeThread.IsAlive) { forcePause = true; Thread.Sleep(20); if (decodeThread.IsAlive) decodeThread.Abort(); }

if (demuxer.enabledStreams.Contains(st->index))
{
Expand Down Expand Up @@ -270,6 +276,7 @@ public void Close()
info = null;
isEmbedded = false;
status = Status.None;
forcePause = false;
}

public void Decode()
Expand All @@ -282,8 +289,9 @@ public void Decode()
if (status != Status.Ended) status = Status.Paused;
decodeARE.Reset();
decodeARE.WaitOne();
status = Status.Playing;
if (forcePause) { forcePause = false; break; }
forcePause = false;
status = Status.Playing;
bool shouldStop = false;
int allowedErrors = decCtx.cfg.decoder.MaxErrors;
int ret = -1;
Expand Down
11 changes: 10 additions & 1 deletion FlyleafLib/MediaFramework/Demuxer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,14 @@ public void Close(bool closeExternals = true)
decCtx.sDecoder.Close();
}

if (demuxThread != null && demuxThread.IsAlive) demuxThread.Abort();
if (demuxThread != null && demuxThread.IsAlive)
{
forcePause = true;
if (!isPlaying) demuxARE.Set();
Utils.EnsureThreadDone(demuxThread);

demuxThread.Abort();
}

if (status == Status.None) return;

Expand All @@ -287,6 +294,7 @@ public void Close(bool closeExternals = true)
streams = null;
ioStream = null;
status = Status.None;
forcePause = false;
defaultAudioStream = -1;
}

Expand Down Expand Up @@ -387,6 +395,7 @@ public void Demux()
if (status != Status.Ended) status = Status.Paused;
demuxARE.Reset();
demuxARE.WaitOne();
if (forcePause) { forcePause = false; break; }
status = Status.Playing;
forcePause = false;
Log("Started");
Expand Down
2 changes: 1 addition & 1 deletion Wpf Samples/Sample1_Basic.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

xmlns:fl="clr-namespace:FlyleafLib.Controls.WPF;assembly=FlyleafLib"
xmlns:flwpf="clr-namespace:FlyleafLib.Controls.WPF;assembly=FlyleafLib.Controls.WPF"
Title="Flyleaf Sample 1 (Basic)" Height="450" Width="800">
Title="Flyleaf Sample 1 (Basic)" Height="450" Width="800" Background="Black">
<Grid>
<fl:VideoView Player="{Binding Player}">
<flwpf:Flyleaf/>
Expand Down
2 changes: 1 addition & 1 deletion Wpf Samples/Sample2_Custom.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>

<TextBox Margin="10" Width="200" Text="{Binding UserInput}"/>
<TextBlock Grid.Column="1" Margin="10" TextAlignment="Right" Text="{Binding Player.Status}" FontSize="22" Foreground="Red"/>

Expand Down
6 changes: 4 additions & 2 deletions Wpf Samples/Sample2_Custom.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ namespace Wpf_Samples
/// </summary>
public partial class Sample2_Custom : Window
{
Sample2_ViewModel s2vm = new Sample2_ViewModel();
Sample2_ViewModel s2vm;
Player player;

public Sample2_Custom()
{
Utils.FFmpeg.RegisterFFmpeg(":2");
InitializeComponent();

s2vm.Player = new Player();
player = new Player();
s2vm = new Sample2_ViewModel(player);
flview.DataContext = s2vm;
}
}
Expand Down
10 changes: 2 additions & 8 deletions Wpf Samples/Sample2_ViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,12 @@ public string UserInput
/// <summary>
/// ViewMode's Constructor
/// </summary>
public Sample2_ViewModel()
public Sample2_ViewModel(Player player)
{
Player = player;
OpenVideo = new RelayCommand(OpenVideoAction);
PauseVideo = new RelayCommand(PauseVideoAction);
PlayVideo = new RelayCommand(PlayVideoAction);
}

/// <summary>
/// ViewModel's Initialization as we have VideoView
/// </summary>
public void Initialize()
{
UserInput = "../../../Sample.mp4";
Player.OpenCompleted += Player_OpenCompleted;
}
Expand Down

0 comments on commit 908e06e

Please sign in to comment.