From 3de7aaab872bf9a453c6d32bbb253fab5e18c296 Mon Sep 17 00:00:00 2001 From: ziomus5555 Date: Fri, 1 Nov 2019 19:04:24 +0100 Subject: [PATCH 1/3] stream service --- Squirrowse.Client/Service/CameraService.cs | 21 ++++++++++ .../Service/ConnectionManager.cs | 3 +- Squirrowse.Client/Service/ICameraService.cs | 14 +++++++ .../Service/IConnectionManager.cs | 2 +- Squirrowse.Client/Service/IStreamService.cs | 14 +++++++ Squirrowse.Client/Service/StreamService.cs | 38 +++++++++++++++++++ Squirrowse.Client/Worker.cs | 6 ++- 7 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 Squirrowse.Client/Service/CameraService.cs create mode 100644 Squirrowse.Client/Service/ICameraService.cs create mode 100644 Squirrowse.Client/Service/IStreamService.cs create mode 100644 Squirrowse.Client/Service/StreamService.cs diff --git a/Squirrowse.Client/Service/CameraService.cs b/Squirrowse.Client/Service/CameraService.cs new file mode 100644 index 0000000..669f63b --- /dev/null +++ b/Squirrowse.Client/Service/CameraService.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using OpenCvSharp; + +namespace Squirrowse.Client.Service +{ + public class CameraService : ICameraService + { + public VideoCapture GetCamera() + { + throw new NotImplementedException(); + } + + public Task GetFrame(VideoCapture video) + { + throw new NotImplementedException(); + } + } +} diff --git a/Squirrowse.Client/Service/ConnectionManager.cs b/Squirrowse.Client/Service/ConnectionManager.cs index 5ce3ae9..e2bf587 100644 --- a/Squirrowse.Client/Service/ConnectionManager.cs +++ b/Squirrowse.Client/Service/ConnectionManager.cs @@ -14,8 +14,9 @@ namespace Squirrowse.Client.Service .AddMessagePackProtocol() .WithAutomaticReconnect() .Build(); + } - public HubConnection EstablishHubConnection() => _connection; + public HubConnection Connect() => _connection; } } \ No newline at end of file diff --git a/Squirrowse.Client/Service/ICameraService.cs b/Squirrowse.Client/Service/ICameraService.cs new file mode 100644 index 0000000..ae3ad18 --- /dev/null +++ b/Squirrowse.Client/Service/ICameraService.cs @@ -0,0 +1,14 @@ +using OpenCvSharp; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace Squirrowse.Client.Service +{ + public interface ICameraService + { + public VideoCapture GetCamera(); + public Task GetFrame(VideoCapture video); + } +} diff --git a/Squirrowse.Client/Service/IConnectionManager.cs b/Squirrowse.Client/Service/IConnectionManager.cs index f5d208f..d014347 100644 --- a/Squirrowse.Client/Service/IConnectionManager.cs +++ b/Squirrowse.Client/Service/IConnectionManager.cs @@ -4,7 +4,7 @@ namespace Squirrowse.Client.Service { public interface IConnectionManager { - HubConnection EstablishHubConnection(); + HubConnection Connect(); } } \ No newline at end of file diff --git a/Squirrowse.Client/Service/IStreamService.cs b/Squirrowse.Client/Service/IStreamService.cs new file mode 100644 index 0000000..2e26fc0 --- /dev/null +++ b/Squirrowse.Client/Service/IStreamService.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace Squirrowse.Client.Service +{ + public interface IStreamService + { + Task SendStreamAsync(IAsyncEnumerable asb); + + + } +} diff --git a/Squirrowse.Client/Service/StreamService.cs b/Squirrowse.Client/Service/StreamService.cs new file mode 100644 index 0000000..0a3875a --- /dev/null +++ b/Squirrowse.Client/Service/StreamService.cs @@ -0,0 +1,38 @@ +using Microsoft.AspNetCore.SignalR.Client; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace Squirrowse.Client.Service +{ + public class StreamService : IStreamService + { + private readonly IConnectionManager connectionManager; + private readonly ILogger logger; + + public StreamService(ILogger logger, IConnectionManager connectionManager) + { + this.connectionManager = connectionManager; + this.logger = logger; + } + + public async Task SendStreamAsync(IAsyncEnumerable asb) + { + var con = connectionManager.Connect(); + try + { + logger.LogInformation($"{nameof(SendStreamAsync)} Start stream"); + await con.StartAsync(); + await con.SendAsync("UploadByteStream", asb, default); + + } + finally + { + logger.LogInformation($"{nameof(SendStreamAsync)} End stream"); + await con.StopAsync(); + } + } + } +} diff --git a/Squirrowse.Client/Worker.cs b/Squirrowse.Client/Worker.cs index 33a59d9..ac2a609 100644 --- a/Squirrowse.Client/Worker.cs +++ b/Squirrowse.Client/Worker.cs @@ -5,16 +5,19 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Squirrowse.Client.Service; namespace Squirrowse.Client { public class Worker : BackgroundService { private readonly ILogger _logger; + private readonly IStreamService _streamService; - public Worker(ILogger logger) + public Worker(ILogger logger, IStreamService streamService) { _logger = logger; + _streamService = streamService; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) @@ -22,6 +25,7 @@ namespace Squirrowse.Client while (!stoppingToken.IsCancellationRequested) { _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); + await Task.Delay(1000, stoppingToken); } } From 6fed61be4a31f71d314dfb71a763865abb6c37c3 Mon Sep 17 00:00:00 2001 From: ziomus5555 Date: Sat, 2 Nov 2019 20:36:31 +0100 Subject: [PATCH 2/3] stream hub manager prepare Interface for future hub development --- Squirrowse.Core/Models/Groups.cs | 13 +++++ Squirrowse.Service/Hubs/IStreamHub.cs | 11 ++++- Squirrowse.Service/Hubs/StreamHub.cs | 51 +++++++++++++++++++- Squirrowse.Service/IUserProvider.cs | 12 +++++ Squirrowse.Service/Squirrowse.Service.csproj | 4 ++ Squirrowse.Service/UserProvider.cs | 15 ++++++ 6 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 Squirrowse.Core/Models/Groups.cs create mode 100644 Squirrowse.Service/IUserProvider.cs create mode 100644 Squirrowse.Service/UserProvider.cs diff --git a/Squirrowse.Core/Models/Groups.cs b/Squirrowse.Core/Models/Groups.cs new file mode 100644 index 0000000..8285440 --- /dev/null +++ b/Squirrowse.Core/Models/Groups.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Squirrowse.Core.Models +{ + public enum Groups + { + debug=-1, + normal, + superUser + } +} diff --git a/Squirrowse.Service/Hubs/IStreamHub.cs b/Squirrowse.Service/Hubs/IStreamHub.cs index b73fa0b..81fb63c 100644 --- a/Squirrowse.Service/Hubs/IStreamHub.cs +++ b/Squirrowse.Service/Hubs/IStreamHub.cs @@ -1,10 +1,19 @@ -using System.Collections.Generic; +using Squirrowse.Core.Models; +using System; +using System.Collections.Generic; using System.Threading.Tasks; namespace Squirrowse.Service.Hubs { public interface IStreamHub { + Task AddClient(); Task UploadByteStream(IAsyncEnumerable stream); + Task Startstream(string userId); + Task StopStream(string userId); + Task ExecCommandOnAll(string command,object[] act);//gni + Task AddToGroup(Groups group,string user=""); + Task RemoveFromGroup(Groups group,string user=""); + } } \ No newline at end of file diff --git a/Squirrowse.Service/Hubs/StreamHub.cs b/Squirrowse.Service/Hubs/StreamHub.cs index 0db5054..fab0661 100644 --- a/Squirrowse.Service/Hubs/StreamHub.cs +++ b/Squirrowse.Service/Hubs/StreamHub.cs @@ -6,17 +6,63 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.SignalR; using Microsoft.Extensions.Logging; using NLog; +using Squirrowse.Core.Models; namespace Squirrowse.Service.Hubs { - public class StreamHub : Hub, IStreamHub + public class StreamHub : Hub, IStreamHub //fujka { private readonly ILogger logger; - + public StreamHub(ILogger logger) { this.logger = logger; } + + public async Task AddClient() + { + throw new NotImplementedException(); + } + + public async Task AddToGroup(Groups group, string user = "") + { + string connectionId = string.IsNullOrWhiteSpace(user) ? Context.ConnectionId : user; + await Groups.AddToGroupAsync(connectionId, group.ToString()); + logger.LogInformation($"{nameof(AddToGroup)}: {connectionId} joined to {group}"); + } + /// + /// USE ONLY FOR DEBUG + /// + /// + /// DESTRUCTION + public async Task ExecCommandOnAll(string command,object[] act) + { + await Clients.All.SendCoreAsync(command,act); + } + + public async Task RemoveFromGroup(Groups group, string user = "") + { + + string connectionId = string.IsNullOrWhiteSpace(user) ? Context.ConnectionId : user; + await Groups.RemoveFromGroupAsync(connectionId, group.ToString()); + logger.LogInformation($"{nameof(AddToGroup)}: {connectionId} joined to {group}"); + } + + public async Task Startstream(string clientId) + { + var client = Clients.Client(clientId); + + await client.SendAsync("Start"); + } + + + public async Task StopStream(string clientId) + { + var client = Clients.Client(clientId); + + await client.SendAsync("Stop"); + } + public async Task UploadByteStream(IAsyncEnumerable stream) { await foreach (var frame in stream) @@ -25,5 +71,6 @@ namespace Squirrowse.Service.Hubs await Task.Delay(100); //leave some delay for debug purpose } } + } } diff --git a/Squirrowse.Service/IUserProvider.cs b/Squirrowse.Service/IUserProvider.cs new file mode 100644 index 0000000..6c98471 --- /dev/null +++ b/Squirrowse.Service/IUserProvider.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Squirrowse.Service +{ + public interface IUserProvider + { + string GetUserId(); + } +} diff --git a/Squirrowse.Service/Squirrowse.Service.csproj b/Squirrowse.Service/Squirrowse.Service.csproj index 5d22ada..c705037 100644 --- a/Squirrowse.Service/Squirrowse.Service.csproj +++ b/Squirrowse.Service/Squirrowse.Service.csproj @@ -18,4 +18,8 @@ + + + + diff --git a/Squirrowse.Service/UserProvider.cs b/Squirrowse.Service/UserProvider.cs new file mode 100644 index 0000000..9b4279b --- /dev/null +++ b/Squirrowse.Service/UserProvider.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Squirrowse.Service +{ + public class UserProvider : IUserProvider + { + public string GetUserId() + { + throw new NotImplementedException(); + } + } +} From 93e692df9e4df4f5597ac6b2186ac3f578f5264f Mon Sep 17 00:00:00 2001 From: ziomus5555 Date: Sat, 2 Nov 2019 21:06:44 +0100 Subject: [PATCH 3/3] Camera settings --- Squirrowse.Client/Service/CameraService.cs | 11 +++++++++-- Squirrowse.Client/Service/ICameraService.cs | 2 +- Squirrowse.Client/Service/IStreamService.cs | 2 +- Squirrowse.Client/Service/StreamService.cs | 22 ++++++++++++++++----- Squirrowse.Client/Squirrowse.Client.csproj | 4 ++++ 5 files changed, 32 insertions(+), 9 deletions(-) diff --git a/Squirrowse.Client/Service/CameraService.cs b/Squirrowse.Client/Service/CameraService.cs index 669f63b..5cdbe0c 100644 --- a/Squirrowse.Client/Service/CameraService.cs +++ b/Squirrowse.Client/Service/CameraService.cs @@ -8,9 +8,16 @@ namespace Squirrowse.Client.Service { public class CameraService : ICameraService { - public VideoCapture GetCamera() + public VideoCapture GetCamera(int height=480,int widght=640,double fps=15f,bool disposable = false) { - throw new NotImplementedException(); + + var cam = new VideoCapture(CaptureDevice.Any); + cam.Fps = fps; + cam.FrameHeight = height; + cam.FrameWidth = widght; + cam.IsEnabledDispose = disposable; + + return cam; } public Task GetFrame(VideoCapture video) diff --git a/Squirrowse.Client/Service/ICameraService.cs b/Squirrowse.Client/Service/ICameraService.cs index ae3ad18..3849785 100644 --- a/Squirrowse.Client/Service/ICameraService.cs +++ b/Squirrowse.Client/Service/ICameraService.cs @@ -8,7 +8,7 @@ namespace Squirrowse.Client.Service { public interface ICameraService { - public VideoCapture GetCamera(); + public VideoCapture GetCamera(int height = 480, int widght = 640, double fps = 15f, bool disposable = false); public Task GetFrame(VideoCapture video); } } diff --git a/Squirrowse.Client/Service/IStreamService.cs b/Squirrowse.Client/Service/IStreamService.cs index 2e26fc0..a56662e 100644 --- a/Squirrowse.Client/Service/IStreamService.cs +++ b/Squirrowse.Client/Service/IStreamService.cs @@ -8,7 +8,7 @@ namespace Squirrowse.Client.Service public interface IStreamService { Task SendStreamAsync(IAsyncEnumerable asb); - + Task SayHello(); } } diff --git a/Squirrowse.Client/Service/StreamService.cs b/Squirrowse.Client/Service/StreamService.cs index 0a3875a..c9292f6 100644 --- a/Squirrowse.Client/Service/StreamService.cs +++ b/Squirrowse.Client/Service/StreamService.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.SignalR.Client; using Microsoft.Extensions.Logging; +using Squirrowse.Core.Models; using System; using System.Collections.Generic; using System.Text; @@ -11,27 +12,38 @@ namespace Squirrowse.Client.Service { private readonly IConnectionManager connectionManager; private readonly ILogger logger; - + private readonly HubConnection session; public StreamService(ILogger logger, IConnectionManager connectionManager) { this.connectionManager = connectionManager; this.logger = logger; + session = connectionManager.Connect(); + session.StartAsync().ConfigureAwait(false); + } + + public async Task SayHello() + { + try + { + await session.SendAsync("AddToGroup",Groups.debug); + } + finally + { + + } } public async Task SendStreamAsync(IAsyncEnumerable asb) { - var con = connectionManager.Connect(); try { logger.LogInformation($"{nameof(SendStreamAsync)} Start stream"); - await con.StartAsync(); - await con.SendAsync("UploadByteStream", asb, default); + await session.SendAsync("UploadByteStream", asb, default); } finally { logger.LogInformation($"{nameof(SendStreamAsync)} End stream"); - await con.StopAsync(); } } } diff --git a/Squirrowse.Client/Squirrowse.Client.csproj b/Squirrowse.Client/Squirrowse.Client.csproj index 81c0779..a134225 100644 --- a/Squirrowse.Client/Squirrowse.Client.csproj +++ b/Squirrowse.Client/Squirrowse.Client.csproj @@ -11,4 +11,8 @@ + + + +