Skip to content

Commit

Permalink
improve optimistic update
Browse files Browse the repository at this point in the history
  • Loading branch information
keifufu committed Jun 21, 2023
1 parent 111d9f3 commit 35c1e22
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/WNPReduxAdapterLibraryExtensions/WNPReduxNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public class WNPReduxNative
private static int port = 0;
static SessionManager manager = null;
static int lastPositionSeconds = 0;
static bool isOptimisticPositionThreadStarted = false;
private static Timer optimisticUpdateTimer;
private static readonly object timerLock = new object();
static readonly string idPrefix = "WNPReduxNativeWindows_";
static readonly Dictionary<string, Session> sessions = new Dictionary<string, Session>();
static readonly bool isWindows10 = Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.Build >= 19041 && Environment.OSVersion.Version.Build < 22000;
Expand All @@ -52,15 +53,16 @@ public static void Start(int port)
WNPReduxNative.port = port;
WNPHttpServer.OnMessageHook += OnMessageHook;
new Thread(StartWindowsAPIThreaded).Start();
new Thread(OptimisticPositionThread).Start();
optimisticUpdateTimer = new Timer(PerformOptimisticUpdate, null, Timeout.Infinite, Timeout.Infinite);
}

public static void Stop()
{
if (!isStarted) return;
isStarted = false;
WNPHttpServer.OnMessageHook -= OnMessageHook;
isOptimisticPositionThreadStarted = false;
optimisticUpdateTimer.Dispose();
optimisticUpdateTimer = null;
if (manager != null)
{
manager.SessionsChanged -= SessionsChanged_Native;
Expand All @@ -75,11 +77,11 @@ public static void Stop()
sessions.Clear();
}

private static void OptimisticPositionThread()
private static void PerformOptimisticUpdate(object state)
{
if (isOptimisticPositionThreadStarted) return;
isOptimisticPositionThreadStarted = true;
while (isStarted)
if (!isStarted) return;

lock (timerLock)
{
if (WNPRedux.MediaInfo._ID.StartsWith(idPrefix) && WNPRedux.MediaInfo.State == MediaInfo.StateMode.PLAYING)
{
Expand All @@ -95,7 +97,6 @@ private static void OptimisticPositionThread()
WNPRedux.MediaInfo.PositionPercent = 100;
}
}
Thread.Sleep(1000);
}
}

Expand Down Expand Up @@ -270,7 +271,7 @@ static async Task<string> WriteThumbnail(IRandomAccessStreamReference thumbnail,
return "";
}
}

private static void CropCover(string path, string savePath)
{
using (var b = new Bitmap(300, 300))
Expand Down Expand Up @@ -320,6 +321,8 @@ static void TimelinePropertiesChanged_Native(Session session, TimelineProperties
mediaInfo.DurationSeconds = Convert.ToInt32(info.EndTime.TotalSeconds);
mediaInfo.Duration = WNPRedux.TimeInSecondsToString(mediaInfo.DurationSeconds);

if (lastPositionSeconds == Convert.ToInt32(info.Position.TotalSeconds)) return;

lastPositionSeconds = Convert.ToInt32(info.Position.TotalSeconds);
mediaInfo.PositionSeconds = lastPositionSeconds;
mediaInfo.Position = WNPRedux.TimeInSecondsToString(mediaInfo.PositionSeconds);
Expand All @@ -333,6 +336,8 @@ static void TimelinePropertiesChanged_Native(Session session, TimelineProperties
mediaInfo.PositionPercent = 100;
}
WNPRedux.UpdateMediaInfo();

optimisticUpdateTimer.Change(1000, 1000);
}
}
}
Expand Down

0 comments on commit 35c1e22

Please sign in to comment.