Skip to content

Commit 2411937

Browse files
committed
fix: capture wait logic
1 parent bbebe5d commit 2411937

File tree

1 file changed

+10
-23
lines changed

1 file changed

+10
-23
lines changed

StreamingCaptureBot.Core/Services/CaptureService.cs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -390,32 +390,21 @@ private void FlushDecoderBufferUnsafe(CancellationToken cancellationToken)
390390
/// <param name="cancellationToken"></param>
391391
/// <returns>是否成功,图片字节码</returns>
392392
public async Task<(bool, byte[]?)> CaptureImageAsync(CancellationToken cancellationToken = default)
393-
{
394-
await _semaphore.WaitAsync(cancellationToken);
395-
396-
// Check image cache.
397-
try
398-
{
399-
var captureTimeSpan = DateTime.Now - LastCaptureTime;
400-
if (LastCapturedImage != null && captureTimeSpan <= _streamOption.CacheTimeout)
401-
{
402-
_logger.LogInformation("Return image cached {time} ago.", captureTimeSpan);
403-
return (true, LastCapturedImage);
404-
}
405-
}
406-
finally
407-
{
408-
_semaphore.Release();
409-
}
410-
411-
// Capture new image and process it.
412-
var result = await Task.Run(async () =>
393+
=> await Task.Run(async () =>
413394
{
414395
await _semaphore.WaitAsync(cancellationToken);
415396

416-
_logger.LogInformation("Cache image expired, capture new.");
417397
try
418398
{
399+
var captureTimeSpan = DateTime.Now - LastCaptureTime;
400+
if (LastCapturedImage != null && captureTimeSpan <= _streamOption.CacheTimeout)
401+
{
402+
_logger.LogInformation("Return image cached {time} ago.", captureTimeSpan);
403+
return (true, LastCapturedImage);
404+
}
405+
406+
_logger.LogInformation("Image cached {time} ago has expired, capture new.", captureTimeSpan);
407+
419408
OpenInput();
420409

421410
var decodedFrame = DecodeNextFrameUnsafe();
@@ -450,6 +439,4 @@ private void FlushDecoderBufferUnsafe(CancellationToken cancellationToken)
450439
_semaphore.Release();
451440
}
452441
}, cancellationToken);
453-
return result;
454-
}
455442
}

0 commit comments

Comments
 (0)