Fix memory leaks while streaming

This commit is contained in:
danielgrabowski 2019-11-05 10:30:31 +01:00
parent d394f3a80f
commit ff77cbbe1d
6 changed files with 28 additions and 24 deletions

View File

@ -9,7 +9,7 @@ namespace Squirrowse.Client.Service
private readonly int Height; private readonly int Height;
private readonly int Width; 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; Height = height;
Width = width; Width = width;

View File

@ -1,6 +1,4 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading.Tasks; using System.Threading.Tasks;
using OpenCvSharp; using OpenCvSharp;
using Squirrowse.Core.Services; using Squirrowse.Core.Services;
@ -10,6 +8,7 @@ namespace Squirrowse.Client.Service
public class CameraService : ICameraService public class CameraService : ICameraService
{ {
private readonly VideoCapture _videoCapture; private readonly VideoCapture _videoCapture;
public CameraService(CameraFactory cam) public CameraService(CameraFactory cam)
{ {
_videoCapture = cam.GetCamera(); _videoCapture = cam.GetCamera();
@ -17,18 +16,16 @@ namespace Squirrowse.Client.Service
public async Task<Mat> GetFrame() public async Task<Mat> GetFrame()
{ {
var video = _videoCapture.RetrieveMat(); var video = _videoCapture.RetrieveMat();
return video; return video;
} }
public async IAsyncEnumerable<byte[]> GetFramesAsyncEnumerator() public async IAsyncEnumerable<byte[]> GetFramesAsyncEnumerator()
{ {
while (true) using var fr = await GetFrame();
{ yield return fr.ConvertToJpgByte();
var fr = await GetFrame(); //fr.Dispose();
yield return fr.ConvertToJpgByte();
}
} }
} }
} }

View File

@ -27,12 +27,10 @@ namespace Squirrowse.Client.Service
public async Task SendStreamAsync(IAsyncEnumerable<byte[]> asb) 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"); logger.LogInformation($"{nameof(SendStreamAsync)} End stream");
await session.SendAsync("UploadByteStream", asb);
}
} }
} }
} }

View File

@ -3,6 +3,7 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using OpenCvSharp;
using Squirrowse.Client.Service; using Squirrowse.Client.Service;
namespace Squirrowse.Client namespace Squirrowse.Client
@ -22,14 +23,16 @@ namespace Squirrowse.Client
protected override async Task ExecuteAsync(CancellationToken stoppingToken) protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{ {
while (!stoppingToken.IsCancellationRequested)
{
logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
await streamService.SendStreamAsync(camera.GetFramesAsyncEnumerator()); await streamService.SendStreamAsync(camera.GetFramesAsyncEnumerator());
// await Task.Delay(1000, stoppingToken); // await Task.Delay(50, stoppingToken);
}
} }
} }
} }

View File

@ -13,7 +13,7 @@ namespace Squirrowse.Core.Services
public static Mat ConvertByteToMat(this byte[] bytearr) 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(); return tempMat ?? new Mat();
} }
} }

View File

@ -3,7 +3,9 @@ using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using OpenCvSharp;
using Squirrowse.Core.Models; using Squirrowse.Core.Models;
using Squirrowse.Core.Services;
namespace Squirrowse.Service.Hubs namespace Squirrowse.Service.Hubs
{ {
@ -59,13 +61,17 @@ namespace Squirrowse.Service.Hubs
await client.SendAsync("Stop"); await client.SendAsync("Stop");
} }
public Window okno = new Window("test");
public async Task UploadByteStream(IAsyncEnumerable<byte[]> stream) public async Task UploadByteStream(IAsyncEnumerable<byte[]> stream)
{ {
await foreach (var frame in stream) await foreach (var frame in stream)
{ {
logger.LogInformation($"Got frame size: {frame.Length} "); using var imgbuffer = frame.ConvertByteToMat();
await Task.Delay(100); //leave some delay for debug purpose okno.ShowImage(imgbuffer);
Cv2.WaitKey(1);
// logger.LogInformation($"Got frame size: {frame.Length} ");
// await Task.Delay(100); //leave some delay for debug purpose
} }
} }
} }