Skip to content

Commit

Permalink
Updates to v3.7.14 (FlyleafLib) / v1.2.21 (FlyleafME) / v1.0.7 (Flyle…
Browse files Browse the repository at this point in the history
…afHost.WinUI)

- AudioDecoder: Fixes a possible AccessViolation issue during re-allocation of the Circular Buffer
- AudioDecoder: Fixes a sync issue with audio filters and speed
- FlyleafHost.WinForms: Fixes an issue with the designer (when using FFmpeg.Autogen v6)
- FlyleafHost.Wpf: Adds support for grand childs and FlyleafHosts within other FlyleafHost's overlay
- FlyleafHost.Wpf: Fixes issues with windows activation and mouse events (mainly for detached windows)
- FlyleafHost.Wpf: Fixes issues and improves ratio resize
- FlyleafHost.Wpf: Fixes an issue with CornerRadius and background color
- Sample: Fixes issues when swapping players
  • Loading branch information
SuRGeoNix committed May 2, 2023
1 parent 068953b commit 98a8f15
Show file tree
Hide file tree
Showing 18 changed files with 270 additions and 115 deletions.
3 changes: 1 addition & 2 deletions FlyleafLib.Controls.WPF/FlyleafLib.Controls.WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net7.0-windows;net6.0-windows;net472</TargetFrameworks>
<UseWindowsForms>true</UseWindowsForms>
<UseWPF>true</UseWPF>
<Version>1.2.20</Version>
<Version>1.2.21</Version>
<Authors>SuRGeoNix</Authors>
<Copyright>SuRGeoNix © 2023</Copyright>
<PackageLicenseExpression>LGPL-3.0-or-later</PackageLicenseExpression>
Expand All @@ -14,7 +14,6 @@
<PackageTags>flyleaf flyleaflib flyleafme video audio wpf media player element control</PackageTags>
<Description>FlyleafME: A WPF Media Element Control (based on FlyleafLib)</Description>
<PackageReleaseNotes>
Adds Use Filters in Audio Settings
Updates FlyleafLib
</PackageReleaseNotes>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion FlyleafLib.Controls.WinUI/FlyleafLib.Controls.WinUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<UseWinUI>true</UseWinUI>
<Nullable>enable</Nullable>
<Authors>SuRGeoNix</Authors>
<Version>1.0.6</Version>
<Version>1.0.7</Version>
<Copyright>SuRGeoNix © 2023</Copyright>
<PackageProjectUrl>https://github.com/SuRGeoNix/Flyleaf</PackageProjectUrl>
<PackageIcon>Flyleaf.png</PackageIcon>
Expand Down
203 changes: 159 additions & 44 deletions FlyleafLib/Controls/WPF/FlyleafHost.cs

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions FlyleafLib/Controls/WinForms/FlyleafHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,9 @@ public void NormalScreen()
// TBR: Related to Renderer's WndProc
protected override void OnPaintBackground(PaintEventArgs pe)
{
if (Player != null && !Player.renderer.SCDisposed)
Player.renderer.Present();
else
if (Player == null || (Player != null && !Player.WFPresent()))
base.OnPaintBackground(pe);
}
}
protected override void OnPaint(PaintEventArgs pe) { }

public event PropertyChangedEventHandler PropertyChanged;
Expand Down
2 changes: 1 addition & 1 deletion FlyleafLib/Engine/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ public VideoConfig Clone()
/// Background color of the player's control
/// </summary>
public System.Windows.Media.Color
BackgroundColor { get => Utils.VorticeToWPFColor(_BackgroundColor); set { Set(ref _BackgroundColor, Utils.WPFToVorticeColor(value)); player?.renderer?.UpdateBackgroundColor(); } }
BackgroundColor { get => Utils.VorticeToWPFColor(_BackgroundColor); set { Set(ref _BackgroundColor, WPFToVorticeColor(value)); player?.renderer?.UpdateBackgroundColor(); } }
internal Vortice.Mathematics.Color _BackgroundColor = (Vortice.Mathematics.Color)Vortice.Mathematics.Colors.Black;

/// <summary>
Expand Down
10 changes: 8 additions & 2 deletions FlyleafLib/FlyleafLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageIconUrl />
<RepositoryUrl></RepositoryUrl>
<Description>Media Player .NET Library for WinUI 3/WPF/WinForms (based on FFmpeg/DirectX)</Description>
<Version>3.7.13</Version>
<Version>3.7.14</Version>
<Authors>SuRGeoNix</Authors>
<Copyright>SuRGeoNix © 2023</Copyright>
<PackageLicenseExpression>LGPL-3.0-or-later</PackageLicenseExpression>
Expand All @@ -17,7 +17,13 @@
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PackageReleaseNotes>
- FlyleafHost.WinForms: Another try to avoid designer issues
- AudioDecoder: Fixes a possible AccessViolation issue during re-allocation of the Circular Buffer
- AudioDecoder: Fixes a sync issue with audio filters and speed
- FlyleafHost.WinForms: Fixes an issue with the designer (when using FFmpeg.Autogen v6)
- FlyleafHost.Wpf: Adds support for grand childs and FlyleafHosts within other FlyleafHost's overlay
- FlyleafHost.Wpf: Fixes issues with windows activation and mouse events (mainly for detached windows)
- FlyleafHost.Wpf: Fixes issues and improves ratio resize
- FlyleafHost.Wpf: Fixes an issue with CornerRadius and background color
</PackageReleaseNotes>
</PropertyGroup>

Expand Down
15 changes: 8 additions & 7 deletions FlyleafLib/MediaFramework/MediaDecoder/AudioDecoder.Filters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,19 @@ private int SetupFilters()
if (speed != 1)
{
if (speed >= 0.5 && speed <= 2)
linkCtx = CreateFilter("atempo", $"tempo={speed.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture)}", linkCtx);
linkCtx = CreateFilter("atempo", $"tempo={speed.ToString("0.0000000000", System.Globalization.CultureInfo.InvariantCulture)}", linkCtx);
else if ((speed > 2 & speed <= 4) || (speed >= 0.25 && speed < 0.5))
{
linkCtx = CreateFilter("atempo", $"tempo={Math.Sqrt(speed).ToString("0.00", System.Globalization.CultureInfo.InvariantCulture)}", linkCtx);
linkCtx = CreateFilter("atempo", $"tempo={Math.Sqrt(speed).ToString("0.00", System.Globalization.CultureInfo.InvariantCulture)}", linkCtx);
var singleAtempoSpeed = Math.Sqrt(speed);
linkCtx = CreateFilter("atempo", $"tempo={singleAtempoSpeed.ToString("0.0000000000", System.Globalization.CultureInfo.InvariantCulture)}", linkCtx);
linkCtx = CreateFilter("atempo", $"tempo={singleAtempoSpeed.ToString("0.0000000000", System.Globalization.CultureInfo.InvariantCulture)}", linkCtx);
}
else if (speed > 4 || speed >= 0.125 && speed < 0.25)
{
linkCtx = CreateFilter("atempo", $"tempo={Math.Pow(speed, 1.0 / 3).ToString("0.00", System.Globalization.CultureInfo.InvariantCulture)}", linkCtx);
linkCtx = CreateFilter("atempo", $"tempo={Math.Pow(speed, 1.0 / 3).ToString("0.00", System.Globalization.CultureInfo.InvariantCulture)}", linkCtx);
linkCtx = CreateFilter("atempo", $"tempo={Math.Pow(speed, 1.0 / 3).ToString("0.00", System.Globalization.CultureInfo.InvariantCulture)}", linkCtx);
var singleAtempoSpeed = Math.Pow(speed, 1.0 / 3);
linkCtx = CreateFilter("atempo", $"tempo={singleAtempoSpeed.ToString("0.0000000000", System.Globalization.CultureInfo.InvariantCulture)}", linkCtx);
linkCtx = CreateFilter("atempo", $"tempo={singleAtempoSpeed.ToString("0.0000000000", System.Globalization.CultureInfo.InvariantCulture)}", linkCtx);
linkCtx = CreateFilter("atempo", $"tempo={singleAtempoSpeed.ToString("0.0000000000", System.Globalization.CultureInfo.InvariantCulture)}", linkCtx);
}
}

Expand Down Expand Up @@ -326,7 +328,6 @@ private void ProcessFilter(AVFrame* frame)
mFrame.dataLen = frame->nb_samples * ASampleBytes;
if (CanTrace) Log.Trace($"Processes {Utils.TicksToTime(mFrame.timestamp)}");

// samples without speed (x1) - timestamps will be fixed during screaming
var samplesSpeed1 = frame->nb_samples * speed;
missedSamples += samplesSpeed1 - (int)samplesSpeed1;
curSamples += (int)samplesSpeed1;
Expand Down
3 changes: 3 additions & 0 deletions FlyleafLib/MediaFramework/MediaDecoder/AudioDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public ConcurrentQueue<AudioFrame>
static int AOutChannels = AOutChannelLayout.nb_channels;
static int ASampleBytes = av_get_bytes_per_sample(AOutSampleFormat) * AOutChannels;

internal Action CBufAlloc; // Informs Audio player to clear buffer pointers to avoid access violation
static int cBufTimesSize = 4;
int cBufTimesCur = 1;
byte[] cBuf;
Expand Down Expand Up @@ -430,6 +431,8 @@ private void AllocateCircularBuffer(int samples)
int size = Config.Decoder.MaxAudioFrames * samples * ASampleBytes * cBufTimesSize;
Log.Debug($"Re-allocating circular buffer ({samples} > {cBufSamples}) with {size}bytes");

DisposeFrames(); // TODO: copy data
CBufAlloc?.Invoke();
cBuf = new byte[size];
cBufPos = 0;
cBufSamples = samples;
Expand Down
8 changes: 0 additions & 8 deletions FlyleafLib/MediaFramework/MediaRenderer/Renderer.Present.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ unsafe internal void PresentInternal(VideoFrame frame)
vc.VideoProcessorBlt(vp, vpov, 0, 1, vpsa);
swapChain.Present(Config.Video.VSync, PresentFlags.None);

if (dCompVisual != null)
dCompDevice.Commit();

vpiv.Dispose();
}
else
Expand All @@ -125,9 +122,6 @@ unsafe internal void PresentInternal(VideoFrame frame)
context.PSSetShaderResources(0, frame.srvs);
context.Draw(6, 0);
swapChain.Present(Config.Video.VSync, PresentFlags.None);

if (dCompVisual != null)
dCompDevice.Commit();
}
}

Expand All @@ -146,8 +140,6 @@ unsafe public void RefreshLayout()
{
context.ClearRenderTargetView(backBufferRtv, Config.Video._BackgroundColor);
swapChain.Present(Config.Video.VSync, PresentFlags.None);
if (dCompVisual != null)
dCompDevice.Commit();
}
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ unsafe internal void PresentOffline(VideoFrame frame, ID3D11RenderTargetView rtv
context.RSSetViewport(viewport);
context.PSSetShaderResources(0, frame.srvs);
context.Draw(6, 0);

if (dCompVisual != null)
dCompDevice.Commit();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,9 @@ void InitializeFilters()

internal void UpdateBackgroundColor()
{
D3D11VPBackgroundColor.Rgba.R = Utils.Scale(Config.Video.BackgroundColor.R, 0, 255, 0, 100) / 100.0f;
D3D11VPBackgroundColor.Rgba.G = Utils.Scale(Config.Video.BackgroundColor.G, 0, 255, 0, 100) / 100.0f;
D3D11VPBackgroundColor.Rgba.B = Utils.Scale(Config.Video.BackgroundColor.B, 0, 255, 0, 100) / 100.0f;
D3D11VPBackgroundColor.Rgba.R = Scale(Config.Video.BackgroundColor.R, 0, 255, 0, 100) / 100.0f;
D3D11VPBackgroundColor.Rgba.G = Scale(Config.Video.BackgroundColor.G, 0, 255, 0, 100) / 100.0f;
D3D11VPBackgroundColor.Rgba.B = Scale(Config.Video.BackgroundColor.B, 0, 255, 0, 100) / 100.0f;

vc?.VideoProcessorSetOutputBackgroundColor(vp, false, D3D11VPBackgroundColor);

Expand Down
22 changes: 11 additions & 11 deletions FlyleafLib/MediaPlayer/Audio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public int Volume
{
if (value > Config.Player.VolumeMax || value < 0)
return;

if (value == 0)
Mute = true;
else if (Mute)
Expand All @@ -96,9 +96,8 @@ public int Volume
}
else
{
lock (locker)
if (sourceVoice != null)
sourceVoice.Volume = Math.Max(0, value / 100.0f);
if (sourceVoice != null)
sourceVoice.Volume = Math.Max(0, value / 100.0f);
}

Set(ref _Volume, value, false);
Expand Down Expand Up @@ -227,25 +226,26 @@ internal void Initialize()
try
{
xaudio2 = XAudio2Create();

try
{
masteringVoice = xaudio2.CreateMasteringVoice(0, 0, AudioStreamCategory.GameEffects, _Device == Engine.Audio.DefaultDeviceName ? null : Engine.Audio.GetDeviceId(_Device));
} catch (Exception) // Win 7/8 compatibility issue https://social.msdn.microsoft.com/Forums/en-US/4989237b-814c-4a7a-8a35-00714d36b327/xaudio2-how-to-get-device-id-for-mastering-voice?forum=windowspro-audiodevelopment
}
catch (Exception) // Win 7/8 compatibility issue https://social.msdn.microsoft.com/Forums/en-US/4989237b-814c-4a7a-8a35-00714d36b327/xaudio2-how-to-get-device-id-for-mastering-voice?forum=windowspro-audiodevelopment
{
masteringVoice = xaudio2.CreateMasteringVoice(0, 0, AudioStreamCategory.GameEffects, _Device == Engine.Audio.DefaultDeviceName ? null : (@"\\?\swd#mmdevapi#" + Engine.Audio.GetDeviceId(_Device).ToLower() + @"#{e6327cad-dcec-4949-ae8a-991e976a79d2}"));
}

sourceVoice = xaudio2.CreateSourceVoice(waveFormat, false);
sourceVoice.SetSourceSampleRate(SampleRate);
sourceVoice.Start();

submittedSamples = 0;
deviceDelayTimebase = 1000 * 10000.0 / sampleRate;
masteringVoice.Volume = Config.Player.VolumeMax / 100.0f;
bool oldMute = mute;
Volume = _Volume;
Mute = oldMute;
submittedSamples= 0;

} catch (Exception e)
sourceVoice.Volume = mute ? 0 : Math.Max(0, _Volume / 100.0f);
}
catch (Exception e)
{
player.Log.Info($"Audio initialization failed ({e.Message})");
Config.Audio.Enabled = false;
Expand Down
4 changes: 2 additions & 2 deletions FlyleafLib/MediaPlayer/Player.Screamers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Diagnostics;
using System.Threading;

//using FFmpeg.AutoGen; Avoid using FFmpeg.Autogen in Player (all partial classes) as it can cause design issues to WinForms (when lib and app have different assembly versions)
using FFmpeg.AutoGen;

using FlyleafLib.MediaFramework.MediaDecoder;

Expand Down Expand Up @@ -570,7 +570,7 @@ private long GetBufferedDuration()
return 0;

var decoder = VideoDecoder.Frames.ToArray()[^1].timestamp - vFrame.timestamp;
var demuxer = VideoDemuxer.VideoPackets.LastTimestamp == -9223372036854775808 // AV_NOPTS_VALUE (to avoid designer issues)
var demuxer = VideoDemuxer.VideoPackets.LastTimestamp == ffmpeg.AV_NOPTS_VALUE
? 0 :
(VideoDemuxer.VideoPackets.LastTimestamp - VideoDemuxer.StartTime) - vFrame.timestamp;

Expand Down
8 changes: 6 additions & 2 deletions FlyleafLib/MediaPlayer/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,9 @@ public Player(Config config = null)
decoder.OpenExternalVideoStreamCompleted += Decoder_OpenExternalVideoStreamCompleted;
decoder.OpenExternalSubtitlesStreamCompleted += Decoder_OpenExternalSubtitlesStreamCompleted;

AudioDecoder.CodecChanged = Decoder_AudioCodecChanged;
VideoDecoder.CodecChanged = Decoder_VideoCodecChanged;
AudioDecoder.CBufAlloc = () => { Audio.ClearBuffer(); aFrame = null; };
AudioDecoder.CodecChanged = Decoder_AudioCodecChanged;
VideoDecoder.CodecChanged = Decoder_VideoCodecChanged;
decoder.RecordingCompleted += (o, e) => { IsRecording = false; };

status = Status.Stopped;
Expand Down Expand Up @@ -564,6 +565,9 @@ internal void UIAll()
public override bool Equals(object obj)
=> obj == null || !(obj is Player) ? false : ((Player)obj).PlayerId == PlayerId;
public override int GetHashCode() => PlayerId.GetHashCode();

// Avoid having this code in OnPaintBackground as it can cause designer issues (renderer will try to load FFmpeg.Autogen assembly because of HDR Data)
internal bool WFPresent() { if (renderer == null || renderer.SCDisposed) return false; renderer?.Present(); return true; }
}

public enum Status
Expand Down
2 changes: 1 addition & 1 deletion Samples/FlyleafPlayer (WPF Control) (WPF)/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
Title="Flyleaf" WindowStyle="None" ResizeMode="NoResize" AllowsTransparency="True" Background="Transparent"

MinWidth="150" MinHeight="80"
Height="450" Width="800" Loaded="Window_Loaded" Icon="/Flyleaf.ico">
Height="450" Width="800" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded" Icon="/Flyleaf.ico">

<Window.Resources>
<d:ResourceDictionary Source="pack://application:,,,/FlyleafLib.Controls.WPF;component/Resources/MaterialDesignColors.xaml"/>
Expand Down
Loading

0 comments on commit 98a8f15

Please sign in to comment.