Skip to content

Commit b8fb56d

Browse files
committed
fixing non-vsync synthetic waits for edge cases
1 parent 608f09a commit b8fb56d

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

Unosquare.FFME/Engine/BlockRenderingWorker.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,22 @@ private TimeSpan RemainingCycleTime
8888
{
8989
get
9090
{
91+
const double MaxFrameDuration = 50d;
92+
const double MinFrameDuration = 10d;
93+
9194
try
9295
{
9396
var frameDuration = Container.Components.MainMediaType == MediaType.Video && MediaCore.Blocks[MediaType.Video].Count > 0
9497
? MediaCore.Blocks[MediaType.Video].AverageBlockDuration
9598
: Constants.DefaultTimingPeriod;
9699

97-
// protect against too slow of a video framerate
100+
// protect against too slow or too fast of a video framerate
98101
// which might impact audio rendering.
99-
if (frameDuration.TotalMilliseconds > 50d)
100-
frameDuration = TimeSpan.FromMilliseconds(50);
102+
frameDuration = frameDuration.TotalMilliseconds > MaxFrameDuration
103+
? TimeSpan.FromMilliseconds(MaxFrameDuration)
104+
: frameDuration.TotalMilliseconds < MinFrameDuration
105+
? TimeSpan.FromMilliseconds(MinFrameDuration)
106+
: frameDuration;
101107

102108
return TimeSpan.FromTicks(frameDuration.Ticks - CurrentCycleElapsed.Ticks);
103109
}
@@ -202,7 +208,9 @@ private void RunQuantumThread(object state)
202208
else
203209
{
204210
// Perform a synthetic wait
205-
QuantumWaiter.Wait(Constants.DefaultTimingPeriod);
211+
var waitTime = RemainingCycleTime;
212+
if (waitTime.Ticks > 0)
213+
QuantumWaiter.Wait(waitTime);
206214
}
207215

208216
if (!TryBeginCycle())

0 commit comments

Comments
 (0)