Streaming work <clap,clap>

This commit is contained in:
danielgrabowski 2019-11-04 14:28:45 +01:00
parent e91a5143dd
commit d394f3a80f
9 changed files with 101 additions and 32 deletions

View File

@ -17,7 +17,10 @@ namespace Squirrowse.Client
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
services.AddTransient<IConnectionManager, ConnectionManager>(); //keep as transient for now
services.AddSingleton<IConnectionManager>(x=> new ConnectionManager("http://localhost", 5000)); //keep as transient for now
services.AddSingleton< ICameraService, CameraService >(x=>new CameraService(new Camera()));
services.AddTransient<IStreamService, StreamService>();
});
}
}

View File

@ -0,0 +1,29 @@
using OpenCvSharp;
namespace Squirrowse.Client.Service
{
public class Camera : CameraFactory
{
private readonly bool Disposable;
private readonly double Fps;
private readonly int Height;
private readonly int Width;
public Camera(int height = 480, int width = 640, double fps = 15f)
{
Height = height;
Width = width;
Fps = fps;
}
public override VideoCapture GetCamera()
{
var cam = new VideoCapture(CaptureDevice.Any)
{
Fps = Fps, FrameHeight = Height, FrameWidth = Width
};
return cam;
}
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
using OpenCvSharp;
namespace Squirrowse.Client.Service
{
public abstract class CameraFactory
{
public abstract VideoCapture GetCamera();
}
}

View File

@ -1,25 +1,34 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using OpenCvSharp;
using Squirrowse.Core.Services;
namespace Squirrowse.Client.Service
{
public class CameraService : ICameraService
{
public VideoCapture GetCamera(int height = 480, int widght = 640, double fps = 15f, bool disposable = false)
private readonly VideoCapture _videoCapture;
public CameraService(CameraFactory cam)
{
var cam = new VideoCapture(CaptureDevice.Any);
cam.Fps = fps;
cam.FrameHeight = height;
cam.FrameWidth = widght;
cam.IsEnabledDispose = disposable;
return cam;
_videoCapture = cam.GetCamera();
}
public Task<Mat> GetFrame(VideoCapture video)
public async Task<Mat> GetFrame()
{
throw new NotImplementedException();
var video = _videoCapture.RetrieveMat();
return video;
}
public async IAsyncEnumerable<byte[]> GetFramesAsyncEnumerator()
{
while (true)
{
var fr = await GetFrame();
yield return fr.ConvertToJpgByte();
}
}
}
}

View File

@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Squirrowse.Client.Service
{
public interface ICameraFactory
{
}
}

View File

@ -1,11 +1,13 @@
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using OpenCvSharp;
namespace Squirrowse.Client.Service
{
public interface ICameraService
{
VideoCapture GetCamera(int height = 480, int widght = 640, double fps = 15f, bool disposable = false);
Task<Mat> GetFrame(VideoCapture video);
Task<Mat> GetFrame();
IAsyncEnumerable<byte[]> GetFramesAsyncEnumerator();
}
}

View File

@ -17,7 +17,7 @@ namespace Squirrowse.Client.Service
this.connectionManager = connectionManager;
this.logger = logger;
session = connectionManager.Connect();
session.StartAsync().ConfigureAwait(false);
session.StartAsync();
}
public async Task SayHello()
@ -27,14 +27,11 @@ namespace Squirrowse.Client.Service
public async Task SendStreamAsync(IAsyncEnumerable<byte[]> asb)
{
try
while (true)
{
logger.LogInformation($"{nameof(SendStreamAsync)} Start stream");
await session.SendAsync("UploadByteStream", asb);
}
finally
{
logger.LogInformation($"{nameof(SendStreamAsync)} End stream");
logger.LogInformation($"{nameof(SendStreamAsync)} Start stream");
await session.SendAsync("UploadByteStream", asb);
}
}
}

View File

@ -9,23 +9,27 @@ namespace Squirrowse.Client
{
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
private readonly IStreamService _streamService;
private readonly ILogger<Worker> logger;
private readonly IStreamService streamService;
private readonly ICameraService camera;
public Worker(ILogger<Worker> logger, IStreamService streamService)
public Worker(ILogger<Worker> logger, IStreamService streamService, ICameraService camera)
{
_logger = logger;
_streamService = streamService;
this.logger = logger;
this.streamService = streamService;
this.camera = camera;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
await Task.Delay(1000, stoppingToken);
}
logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
await streamService.SendStreamAsync(camera.GetFramesAsyncEnumerator());
// await Task.Delay(1000, stoppingToken);
}
}
}

View File

@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Squirrowse.Core;
using Squirrowse.Service.Hubs;
namespace Squirrowse.Service
@ -23,6 +24,8 @@ namespace Squirrowse.Service
{
services.AddControllers();
services.AddMediatR(Assembly.GetAssembly(typeof(Startup)));
services.AddTransient<IStreamHub, StreamHub>();
services.AddCoreModule();
services.AddSignalR()
.AddHubOptions<StreamHub
>(opt => opt.MaximumReceiveMessageSize = 102400000) //~100mb per frame instead of 32kb default