Merge branch 'Stream' of s421801/Squirrowse into master

This commit is contained in:
Daniel Grabowski 2019-11-04 08:04:44 +00:00 committed by Gogs
commit 0f05560c45
14 changed files with 221 additions and 6 deletions

View File

@ -0,0 +1,28 @@
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(int height=480,int widght=640,double fps=15f,bool disposable = false)
{
var cam = new VideoCapture(CaptureDevice.Any);
cam.Fps = fps;
cam.FrameHeight = height;
cam.FrameWidth = widght;
cam.IsEnabledDispose = disposable;
return cam;
}
public Task<Mat> GetFrame(VideoCapture video)
{
throw new NotImplementedException();
}
}
}

View File

@ -14,8 +14,9 @@ namespace Squirrowse.Client.Service
.AddMessagePackProtocol()
.WithAutomaticReconnect()
.Build();
}
public HubConnection EstablishHubConnection() => _connection;
public HubConnection Connect() => _connection;
}
}

View File

@ -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(int height = 480, int widght = 640, double fps = 15f, bool disposable = false);
public Task<Mat> GetFrame(VideoCapture video);
}
}

View File

@ -4,7 +4,7 @@ namespace Squirrowse.Client.Service
{
public interface IConnectionManager
{
HubConnection EstablishHubConnection();
HubConnection Connect();
}
}

View File

@ -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<byte[]> asb);
Task SayHello();
}
}

View File

@ -0,0 +1,50 @@
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Logging;
using Squirrowse.Core.Models;
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<StreamService> logger;
private readonly HubConnection session;
public StreamService(ILogger<StreamService> 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<byte[]> asb)
{
try
{
logger.LogInformation($"{nameof(SendStreamAsync)} Start stream");
await session.SendAsync("UploadByteStream", asb, default);
}
finally
{
logger.LogInformation($"{nameof(SendStreamAsync)} End stream");
}
}
}
}

View File

@ -11,4 +11,8 @@
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="3.0.0" />
<PackageReference Include="OpenCvSharp4.Windows" Version="4.1.1.20191021" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Squirrowse.Core\Squirrowse.Core.csproj" />
</ItemGroup>
</Project>

View File

@ -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<Worker> _logger;
private readonly IStreamService _streamService;
public Worker(ILogger<Worker> logger)
public Worker(ILogger<Worker> 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);
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Squirrowse.Core.Models
{
public enum Groups
{
debug=-1,
normal,
superUser
}
}

View File

@ -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<byte[]> 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="");
}
}

View File

@ -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<StreamHub> logger;
public StreamHub(ILogger<StreamHub> 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}");
}
/// <summary>
/// USE ONLY FOR DEBUG
/// </summary>
/// <param name="act"></param>
/// <returns>DESTRUCTION</returns>
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<byte[]> 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
}
}
}
}

View File

@ -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();
}
}

View File

@ -18,4 +18,8 @@
<PackageReference Include="OpenCvSharp4.Windows" Version="4.1.1.20191021" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Squirrowse.Core\Squirrowse.Core.csproj" />
</ItemGroup>
</Project>

View File

@ -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();
}
}
}