Fix memory leaks while streaming
This commit is contained in:
parent
d394f3a80f
commit
ff77cbbe1d
@ -9,7 +9,7 @@ namespace Squirrowse.Client.Service
|
||||
private readonly int Height;
|
||||
private readonly int Width;
|
||||
|
||||
public Camera(int height = 480, int width = 640, double fps = 15f)
|
||||
public Camera(int height = 480, int width = 640, double fps = 30f)
|
||||
{
|
||||
Height = height;
|
||||
Width = width;
|
||||
|
@ -1,6 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using OpenCvSharp;
|
||||
using Squirrowse.Core.Services;
|
||||
@ -10,6 +8,7 @@ namespace Squirrowse.Client.Service
|
||||
public class CameraService : ICameraService
|
||||
{
|
||||
private readonly VideoCapture _videoCapture;
|
||||
|
||||
public CameraService(CameraFactory cam)
|
||||
{
|
||||
_videoCapture = cam.GetCamera();
|
||||
@ -24,11 +23,9 @@ namespace Squirrowse.Client.Service
|
||||
|
||||
public async IAsyncEnumerable<byte[]> GetFramesAsyncEnumerator()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
var fr = await GetFrame();
|
||||
yield return fr.ConvertToJpgByte();
|
||||
}
|
||||
using var fr = await GetFrame();
|
||||
yield return fr.ConvertToJpgByte();
|
||||
//fr.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@ -27,12 +27,10 @@ namespace Squirrowse.Client.Service
|
||||
|
||||
public async Task SendStreamAsync(IAsyncEnumerable<byte[]> asb)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
logger.LogInformation($"{nameof(SendStreamAsync)} Start stream");
|
||||
await session.SendAsync("UploadByteStream", asb);
|
||||
|
||||
logger.LogInformation($"{nameof(SendStreamAsync)} Start stream");
|
||||
await session.SendAsync("UploadByteStream", asb);
|
||||
}
|
||||
logger.LogInformation($"{nameof(SendStreamAsync)} End stream");
|
||||
}
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OpenCvSharp;
|
||||
using Squirrowse.Client.Service;
|
||||
|
||||
namespace Squirrowse.Client
|
||||
@ -23,13 +24,15 @@ namespace Squirrowse.Client
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
||||
|
||||
|
||||
await streamService.SendStreamAsync(camera.GetFramesAsyncEnumerator());
|
||||
|
||||
// await Task.Delay(1000, stoppingToken);
|
||||
|
||||
// await Task.Delay(50, stoppingToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -13,7 +13,7 @@ namespace Squirrowse.Core.Services
|
||||
|
||||
public static Mat ConvertByteToMat(this byte[] bytearr)
|
||||
{
|
||||
using var tempMat = Cv2.ImDecode(bytearr, ImreadModes.Unchanged); //keep as disposable
|
||||
var tempMat = Cv2.ImDecode(bytearr, ImreadModes.Unchanged); //keep as disposable
|
||||
return tempMat ?? new Mat();
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,9 @@ using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OpenCvSharp;
|
||||
using Squirrowse.Core.Models;
|
||||
using Squirrowse.Core.Services;
|
||||
|
||||
namespace Squirrowse.Service.Hubs
|
||||
{
|
||||
@ -59,13 +61,17 @@ namespace Squirrowse.Service.Hubs
|
||||
|
||||
await client.SendAsync("Stop");
|
||||
}
|
||||
|
||||
public Window okno = new Window("test");
|
||||
public async Task UploadByteStream(IAsyncEnumerable<byte[]> stream)
|
||||
{
|
||||
|
||||
await foreach (var frame in stream)
|
||||
{
|
||||
logger.LogInformation($"Got frame size: {frame.Length} ");
|
||||
await Task.Delay(100); //leave some delay for debug purpose
|
||||
using var imgbuffer = frame.ConvertByteToMat();
|
||||
okno.ShowImage(imgbuffer);
|
||||
Cv2.WaitKey(1);
|
||||
// logger.LogInformation($"Got frame size: {frame.Length} ");
|
||||
// await Task.Delay(100); //leave some delay for debug purpose
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user