Streaming work <clap,clap>
This commit is contained in:
parent
e91a5143dd
commit
d394f3a80f
@ -17,7 +17,10 @@ namespace Squirrowse.Client
|
|||||||
.ConfigureServices((hostContext, services) =>
|
.ConfigureServices((hostContext, services) =>
|
||||||
{
|
{
|
||||||
services.AddHostedService<Worker>();
|
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>();
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
29
Squirrowse.Client/Service/Camera.cs
Normal file
29
Squirrowse.Client/Service/Camera.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
12
Squirrowse.Client/Service/CameraFactory.cs
Normal file
12
Squirrowse.Client/Service/CameraFactory.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
@ -1,25 +1,34 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using OpenCvSharp;
|
using OpenCvSharp;
|
||||||
|
using Squirrowse.Core.Services;
|
||||||
|
|
||||||
namespace Squirrowse.Client.Service
|
namespace Squirrowse.Client.Service
|
||||||
{
|
{
|
||||||
public class CameraService : ICameraService
|
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);
|
_videoCapture = cam.GetCamera();
|
||||||
cam.Fps = fps;
|
|
||||||
cam.FrameHeight = height;
|
|
||||||
cam.FrameWidth = widght;
|
|
||||||
cam.IsEnabledDispose = disposable;
|
|
||||||
|
|
||||||
return cam;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
10
Squirrowse.Client/Service/ICameraFactory.cs
Normal file
10
Squirrowse.Client/Service/ICameraFactory.cs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Squirrowse.Client.Service
|
||||||
|
{
|
||||||
|
public interface ICameraFactory
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
@ -1,11 +1,13 @@
|
|||||||
using System.Threading.Tasks;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using OpenCvSharp;
|
using OpenCvSharp;
|
||||||
|
|
||||||
namespace Squirrowse.Client.Service
|
namespace Squirrowse.Client.Service
|
||||||
{
|
{
|
||||||
public interface ICameraService
|
public interface ICameraService
|
||||||
{
|
{
|
||||||
VideoCapture GetCamera(int height = 480, int widght = 640, double fps = 15f, bool disposable = false);
|
Task<Mat> GetFrame();
|
||||||
Task<Mat> GetFrame(VideoCapture video);
|
IAsyncEnumerable<byte[]> GetFramesAsyncEnumerator();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -17,7 +17,7 @@ namespace Squirrowse.Client.Service
|
|||||||
this.connectionManager = connectionManager;
|
this.connectionManager = connectionManager;
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
session = connectionManager.Connect();
|
session = connectionManager.Connect();
|
||||||
session.StartAsync().ConfigureAwait(false);
|
session.StartAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task SayHello()
|
public async Task SayHello()
|
||||||
@ -27,14 +27,11 @@ namespace Squirrowse.Client.Service
|
|||||||
|
|
||||||
public async Task SendStreamAsync(IAsyncEnumerable<byte[]> asb)
|
public async Task SendStreamAsync(IAsyncEnumerable<byte[]> asb)
|
||||||
{
|
{
|
||||||
try
|
while (true)
|
||||||
{
|
{
|
||||||
logger.LogInformation($"{nameof(SendStreamAsync)} Start stream");
|
|
||||||
await session.SendAsync("UploadByteStream", asb);
|
logger.LogInformation($"{nameof(SendStreamAsync)} Start stream");
|
||||||
}
|
await session.SendAsync("UploadByteStream", asb);
|
||||||
finally
|
|
||||||
{
|
|
||||||
logger.LogInformation($"{nameof(SendStreamAsync)} End stream");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,23 +9,27 @@ namespace Squirrowse.Client
|
|||||||
{
|
{
|
||||||
public class Worker : BackgroundService
|
public class Worker : BackgroundService
|
||||||
{
|
{
|
||||||
private readonly ILogger<Worker> _logger;
|
private readonly ILogger<Worker> logger;
|
||||||
private readonly IStreamService _streamService;
|
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;
|
this.logger = logger;
|
||||||
_streamService = streamService;
|
this.streamService = streamService;
|
||||||
|
this.camera = camera;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
await Task.Delay(1000, stoppingToken);
|
logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
||||||
}
|
|
||||||
|
|
||||||
|
await streamService.SendStreamAsync(camera.GetFramesAsyncEnumerator());
|
||||||
|
|
||||||
|
// await Task.Delay(1000, stoppingToken);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Hosting;
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Squirrowse.Core;
|
||||||
using Squirrowse.Service.Hubs;
|
using Squirrowse.Service.Hubs;
|
||||||
|
|
||||||
namespace Squirrowse.Service
|
namespace Squirrowse.Service
|
||||||
@ -23,6 +24,8 @@ namespace Squirrowse.Service
|
|||||||
{
|
{
|
||||||
services.AddControllers();
|
services.AddControllers();
|
||||||
services.AddMediatR(Assembly.GetAssembly(typeof(Startup)));
|
services.AddMediatR(Assembly.GetAssembly(typeof(Startup)));
|
||||||
|
services.AddTransient<IStreamHub, StreamHub>();
|
||||||
|
services.AddCoreModule();
|
||||||
services.AddSignalR()
|
services.AddSignalR()
|
||||||
.AddHubOptions<StreamHub
|
.AddHubOptions<StreamHub
|
||||||
>(opt => opt.MaximumReceiveMessageSize = 102400000) //~100mb per frame instead of 32kb default
|
>(opt => opt.MaximumReceiveMessageSize = 102400000) //~100mb per frame instead of 32kb default
|
||||||
|
Loading…
Reference in New Issue
Block a user