From 6f1cc96c162c6c1a3adc928d82c2d3ec52f473e3 Mon Sep 17 00:00:00 2001 From: danielgrabowski Date: Fri, 8 Nov 2019 16:36:20 +0100 Subject: [PATCH] Rework stream --- Squirrowse.Client/Service/ActionDispatcher.cs | 20 ++++++++-------- Squirrowse.Client/Service/CameraService.cs | 9 ++++++-- Squirrowse.Service/Hubs/IStreamManager.cs | 1 + Squirrowse.Service/Hubs/StreamHub.cs | 18 +++++++++------ Squirrowse.Service/Hubs/StreamManager.cs | 6 ++++- Squirrowse.Service/Startup.cs | 2 +- Squirrowse.Web/Pages/Hub.razor | 23 +++++++++++++------ 7 files changed, 52 insertions(+), 27 deletions(-) diff --git a/Squirrowse.Client/Service/ActionDispatcher.cs b/Squirrowse.Client/Service/ActionDispatcher.cs index b54cf4d..2bd3df9 100644 --- a/Squirrowse.Client/Service/ActionDispatcher.cs +++ b/Squirrowse.Client/Service/ActionDispatcher.cs @@ -14,7 +14,7 @@ namespace Squirrowse.Client.Service private readonly IConnectionManager connectionManager; private readonly ILogger logger; private readonly HubConnection session; - + private bool streamOn = false; public ActionDispatcher(ILogger logger, IConnectionManager connectionManager, ICameraService camera) { @@ -22,13 +22,15 @@ namespace Squirrowse.Client.Service this.logger = logger; this.camera = camera; session = connectionManager.GetConnection().Result; - session.On("Start", StartStream); + session.On("Start", StartStream); + //session.On("StartEnum", StartStreamEnumerable); session.On("Stop", StopStream); } public Task StopStream() { - throw new NotImplementedException(); + streamOn = false; + return Task.CompletedTask; } public async Task SayHello() @@ -39,19 +41,19 @@ namespace Squirrowse.Client.Service public async Task SendStreamAsync(IAsyncEnumerable asb) { logger.LogInformation($"{nameof(SendStreamAsync)} Start stream"); + await session.SendAsync("UploadByteStream", asb); logger.LogInformation($"{nameof(SendStreamAsync)} End stream"); } + public async Task StartStream() { - while (true) - { - await SendStreamAsync(camera.GetFramesAsyncEnumerator()); - - } - + streamOn = true; + await SendStreamAsync(camera.GetFramesAsyncEnumerator()); } + + } } \ No newline at end of file diff --git a/Squirrowse.Client/Service/CameraService.cs b/Squirrowse.Client/Service/CameraService.cs index 3a3d148..ef3f3a9 100644 --- a/Squirrowse.Client/Service/CameraService.cs +++ b/Squirrowse.Client/Service/CameraService.cs @@ -23,8 +23,13 @@ namespace Squirrowse.Client.Service public async IAsyncEnumerable GetFramesAsyncEnumerator() { - using var fr = await GetFrame(); - yield return fr.ConvertToJpgByte(); + while (true) + { + using var fr = await GetFrame(); + yield return fr.ConvertToJpgByte(); + await Task.Delay(1000 / 30); + } + //fr.Dispose(); } } diff --git a/Squirrowse.Service/Hubs/IStreamManager.cs b/Squirrowse.Service/Hubs/IStreamManager.cs index a5f7220..cce229f 100644 --- a/Squirrowse.Service/Hubs/IStreamManager.cs +++ b/Squirrowse.Service/Hubs/IStreamManager.cs @@ -11,6 +11,7 @@ namespace Squirrowse.Service.Hubs Task RemoveUserByUserName(string agentName); IEnumerable getServerSideUsers(); bool CheckUser(string agentName); + bool StreamOn(); IEnumerable getClientSideUsers(); IEnumerable getAllUsers(); } diff --git a/Squirrowse.Service/Hubs/StreamHub.cs b/Squirrowse.Service/Hubs/StreamHub.cs index d244697..4145d18 100644 --- a/Squirrowse.Service/Hubs/StreamHub.cs +++ b/Squirrowse.Service/Hubs/StreamHub.cs @@ -12,6 +12,7 @@ namespace Squirrowse.Service.Hubs { private readonly ILogger logger; private readonly IStreamManager manager; + public StreamHub(ILogger logger, IStreamManager manager) { @@ -62,6 +63,7 @@ namespace Squirrowse.Service.Hubs public async Task Startstream(string clientId) { + manager.StreamOn() = true; //var client = Clients.Client(clientId); await Clients.Groups(Core.Models.Groups.normal.ToString()).SendAsync("Start"); // await client.SendAsync("Start"); @@ -70,20 +72,22 @@ namespace Squirrowse.Service.Hubs public async Task StopStream(string clientId) { - var client = Clients.Client(clientId); - - await client.SendAsync("Stop"); + streamOn = false; + //var client = Clients.Client(clientId); + // await Clients.Groups(Core.Models.Groups.normal.ToString()).SendAsync("Stop"); + //await client.SendAsync("Stop"); } public async Task UploadByteStream(IAsyncEnumerable stream) { - //await Clients.Groups(Core.Models.Groups.superUser.ToString()).SendAsync("DUPA", "beniz"); - //await Clients.Groups(Core.Models.Groups.superUser.ToString()).SendAsync("RecData", stream); await foreach (var frame in stream) { - await Clients.Groups(Core.Models.Groups.superUser.ToString()).SendAsync("RecData", frame); logger.LogInformation($"Got frame size: {frame.Length} "); - await Task.Delay(100); //leave some delay for debug purpose + if (!streamOn) continue; + logger.LogInformation($"Send frame of size: {frame.Length} to {Core.Models.Groups.superUser.ToString()}"); + await Clients.Groups(Core.Models.Groups.superUser.ToString()).SendAsync("RecData", frame); + + //await Task.Delay(100); //leave some delay for debug purpose } } diff --git a/Squirrowse.Service/Hubs/StreamManager.cs b/Squirrowse.Service/Hubs/StreamManager.cs index 1a6d88d..4063987 100644 --- a/Squirrowse.Service/Hubs/StreamManager.cs +++ b/Squirrowse.Service/Hubs/StreamManager.cs @@ -8,7 +8,11 @@ namespace Squirrowse.Service.Hubs public class StreamManager : IStreamManager { private readonly List _users = new List(); //temporary - + private bool stream; + public bool streamOn() + { + return stream; + } public Task AddUser(string connectionId, string userName, ConnectionType type) { _users.Add(new User(connectionId, userName, type)); diff --git a/Squirrowse.Service/Startup.cs b/Squirrowse.Service/Startup.cs index c43ab37..cdb1e64 100644 --- a/Squirrowse.Service/Startup.cs +++ b/Squirrowse.Service/Startup.cs @@ -24,7 +24,7 @@ namespace Squirrowse.Service { services.AddControllers(); services.AddMediatR(Assembly.GetAssembly(typeof(Startup))); - services.AddSingleton(); + //services.AddSingleton(); services.AddSingleton(); services.AddCoreModule(); services.AddSignalR() diff --git a/Squirrowse.Web/Pages/Hub.razor b/Squirrowse.Web/Pages/Hub.razor index 2b7f4ff..d9a4816 100644 --- a/Squirrowse.Web/Pages/Hub.razor +++ b/Squirrowse.Web/Pages/Hub.razor @@ -25,7 +25,7 @@ View cast - @@ -56,7 +56,6 @@ { await _connection.InitConnection(ConnectionType.Server); connection = await _connection.GetConnection(); - connection.On("DUPA", DuPa); connection.On("RecData", OnStreamDataReceived); await foreach (var user in connection.StreamAsync("GetListOfTypeUserAsync", ConnectionType.Client).ConfigureAwait(false)) @@ -73,11 +72,7 @@ return agentName == CurrentViewCastAgent; } - public void DuPa(string duuupa) - { - var s= "DuPa"; - } async Task OnStreamDataReceived(byte[] streamData) { //await foreach (var t in streamData) @@ -91,6 +86,20 @@ imageSource = String.Format("data:image/jpg;base64,{0}", base64); StateHasChanged(); + } + async Task OnStreamDataReceivedEn(byte[] streamData) + { + //await foreach (var t in streamData) + //{ + // var base64 = Convert.ToBase64String(t); + // imageSource = String.Format("data:image/jpg;base64,{0}", base64); + // StateHasChanged(); + //} + + var base64 = Convert.ToBase64String(streamData); + imageSource = String.Format("data:image/jpg;base64,{0}", base64); + StateHasChanged(); + } private async Task OnViewCastClicked(string agentName) @@ -103,7 +112,7 @@ private async Task OnStopViewCastClicked(string agentName) { CurrentViewCastAgent = null; - await connection.InvokeAsync("Stopstream", agentName); + await connection.InvokeAsync("StopStream", agentName); imageSource = null; StateHasChanged(); }