From fa991b605a94d514aa292fe09165d9d9a062f94a Mon Sep 17 00:00:00 2001 From: waterliu99 Date: Mon, 24 Aug 2020 18:48:16 +0800 Subject: [PATCH] =?UTF-8?q?1.=E7=AC=AC=E4=B8=80=E7=89=88hls=E7=BD=91?= =?UTF-8?q?=E5=85=B3=EF=BC=8C=E8=BF=98=E8=A6=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../JT1078.Gateway.Test/PipeTest.cs | 11 +++++ .../JT1078.Gateway.TestNormalHosting.csproj | 1 + .../JT1078HlsNormalMsgHostedService.cs | 48 +++++++++++++++++++ .../appsettings.json | 6 +++ src/JT1078.Gateway/JT1078HttpServer.cs | 19 ++++---- .../Sessions/JT1078HttpSessionManager.cs | 5 ++ 6 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Services/JT1078HlsNormalMsgHostedService.cs diff --git a/src/JT1078.Gateway.Tests/JT1078.Gateway.Test/PipeTest.cs b/src/JT1078.Gateway.Tests/JT1078.Gateway.Test/PipeTest.cs index 37a5957..9be4e3b 100644 --- a/src/JT1078.Gateway.Tests/JT1078.Gateway.Test/PipeTest.cs +++ b/src/JT1078.Gateway.Tests/JT1078.Gateway.Test/PipeTest.cs @@ -11,6 +11,7 @@ using System.Buffers.Binary; using JT1078.Protocol.Enums; using System.Linq; using System.Text; +using System.IO; namespace JT1078.Gateway.Test { @@ -240,5 +241,15 @@ namespace JT1078.Gateway.Test var empty = "000000000".TrimStart('0'); Assert.Equal("", empty); } + [Fact] + public void Test6() + { + string url = "https://www.baidu.com:8080/live.m3u8?aa=aa&bb=bb"; + var uri = new Uri(url); + string filename = Path.GetFileName(uri.AbsolutePath); + var name = Path.GetFileNameWithoutExtension(filename); + var extension = Path.GetExtension(filename); + var queryParams = uri.Query.Substring(1, uri.Query.Length - 1).Split('&'); + } } } diff --git a/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/JT1078.Gateway.TestNormalHosting.csproj b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/JT1078.Gateway.TestNormalHosting.csproj index ad156de..613c7e1 100644 --- a/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/JT1078.Gateway.TestNormalHosting.csproj +++ b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/JT1078.Gateway.TestNormalHosting.csproj @@ -7,6 +7,7 @@ + diff --git a/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Services/JT1078HlsNormalMsgHostedService.cs b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Services/JT1078HlsNormalMsgHostedService.cs new file mode 100644 index 0000000..bae5a20 --- /dev/null +++ b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Services/JT1078HlsNormalMsgHostedService.cs @@ -0,0 +1,48 @@ +using JT1078.Gateway.Abstractions; +using JT1078.Gateway.Sessions; +using JT1078.Hls; +using Microsoft.Extensions.Hosting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace JT1078.Gateway.TestNormalHosting.Services +{ + public class JT1078HlsNormalMsgHostedService : BackgroundService + { + private IJT1078PackageConsumer PackageConsumer; + private JT1078HttpSessionManager HttpSessionManager; + private M3U8FileManage M3U8FileManage; + public JT1078HlsNormalMsgHostedService( + M3U8FileManage M3U8FileManage, + JT1078HttpSessionManager httpSessionManager, + IJT1078PackageConsumer packageConsumer) + { + PackageConsumer = packageConsumer; + HttpSessionManager = httpSessionManager; + this.M3U8FileManage = M3U8FileManage; + } + protected override Task ExecuteAsync(CancellationToken stoppingToken) + { + PackageConsumer.OnMessage((Message) => + { + var merge = JT1078.Protocol.JT1078Serializer.Merge(Message.Data); + if (merge != null) + { + var hasHttpSessionn = HttpSessionManager.GetAllHttpContextBySimAndChannelNo(merge.SIM, merge.LogicChannelNumber); + if (hasHttpSessionn.Count>0) + { + M3U8FileManage.CreateTsData(merge); + } + else { + M3U8FileManage.Clear(merge.SIM, merge.LogicChannelNumber); + } + } + }); + return Task.CompletedTask; + } + } +} \ No newline at end of file diff --git a/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/appsettings.json b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/appsettings.json index 92898f8..d58cb25 100644 --- a/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/appsettings.json +++ b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/appsettings.json @@ -14,5 +14,11 @@ }, "JT1078Configuration": { "HttpPort": 15555 + }, + "M3U8Option": { + "TsFileCapacity": 10, + "TsFileMaxSecond": 10, + "M3U8FileName": "live.m3u8", + "HlsFileDirectory":"www/root/demo" } } diff --git a/src/JT1078.Gateway/JT1078HttpServer.cs b/src/JT1078.Gateway/JT1078HttpServer.cs index bc9c078..40f5f0c 100644 --- a/src/JT1078.Gateway/JT1078HttpServer.cs +++ b/src/JT1078.Gateway/JT1078HttpServer.cs @@ -98,24 +98,21 @@ namespace JT1078.Gateway context.Http404(); return; } - var queryStringIndex = context.Request.RawUrl.IndexOf("?"); - string url = ""; - if (queryStringIndex > 0) - { - url = context.Request.RawUrl.Substring(1, queryStringIndex-1); - } - else - { - url = context.Request.RawUrl; + var uri = new Uri(context.Request.RawUrl); + string url = uri.AbsolutePath; + var queryParams = uri.Query.Substring(1, uri.Query.Length - 1).Split('&'); + if (queryParams.Length < 2) { + context.Http404(); + return; } if (url.EndsWith(".m3u8") || url.EndsWith(".ts")) { string filename = Path.GetFileName(url); - string filepath = Path.Combine(Configuration.HlsRootDirectory, Path.GetFileNameWithoutExtension(filename), filename); + string filepath = Path.Combine(Configuration.HlsRootDirectory, $"{queryParams[0].Split('=')[1]}_{queryParams[1].Split('=')[1]}", filename);//默认queryParams第一个参数是终端号,第二个参数是通道号 if (!File.Exists(filepath)) { context.Http404(); - return; + return; } try { diff --git a/src/JT1078.Gateway/Sessions/JT1078HttpSessionManager.cs b/src/JT1078.Gateway/Sessions/JT1078HttpSessionManager.cs index e31d0c3..1c19de9 100644 --- a/src/JT1078.Gateway/Sessions/JT1078HttpSessionManager.cs +++ b/src/JT1078.Gateway/Sessions/JT1078HttpSessionManager.cs @@ -186,6 +186,11 @@ namespace JT1078.Gateway.Sessions return Sessions.Select(s => s.Value).Where(w => w.Sim == sim && w.ChannelNo == channelNo).ToList(); } + public List GetAllHttpContextBySimAndChannelNo(string sim, int channelNo) + { + return Sessions.Select(s => s.Value).Where(w => w.Sim == sim && w.ChannelNo == channelNo&&!w.IsWebSocket).ToList(); + } + public List GetAll() { return Sessions.Select(s => s.Value).ToList();