@@ -11,6 +11,7 @@ using System.Buffers.Binary; | |||||
using JT1078.Protocol.Enums; | using JT1078.Protocol.Enums; | ||||
using System.Linq; | using System.Linq; | ||||
using System.Text; | using System.Text; | ||||
using System.IO; | |||||
namespace JT1078.Gateway.Test | namespace JT1078.Gateway.Test | ||||
{ | { | ||||
@@ -240,5 +241,15 @@ namespace JT1078.Gateway.Test | |||||
var empty = "000000000".TrimStart('0'); | var empty = "000000000".TrimStart('0'); | ||||
Assert.Equal("", empty); | 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('&'); | |||||
} | |||||
} | } | ||||
} | } |
@@ -7,6 +7,7 @@ | |||||
<ItemGroup> | <ItemGroup> | ||||
<PackageReference Include="JT1078.Flv" Version="1.0.0-preview6" /> | <PackageReference Include="JT1078.Flv" Version="1.0.0-preview6" /> | ||||
<PackageReference Include="JT1078.Hls" Version="1.0.0-preview1" /> | |||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.7" /> | <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.7" /> | ||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.7" /> | <PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.7" /> | ||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.7" /> | <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.7" /> | ||||
@@ -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; | |||||
} | |||||
} | |||||
} |
@@ -14,5 +14,11 @@ | |||||
}, | }, | ||||
"JT1078Configuration": { | "JT1078Configuration": { | ||||
"HttpPort": 15555 | "HttpPort": 15555 | ||||
}, | |||||
"M3U8Option": { | |||||
"TsFileCapacity": 10, | |||||
"TsFileMaxSecond": 10, | |||||
"M3U8FileName": "live.m3u8", | |||||
"HlsFileDirectory":"www/root/demo" | |||||
} | } | ||||
} | } |
@@ -98,24 +98,21 @@ namespace JT1078.Gateway | |||||
context.Http404(); | context.Http404(); | ||||
return; | 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")) | if (url.EndsWith(".m3u8") || url.EndsWith(".ts")) | ||||
{ | { | ||||
string filename = Path.GetFileName(url); | 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)) | if (!File.Exists(filepath)) | ||||
{ | { | ||||
context.Http404(); | context.Http404(); | ||||
return; | |||||
return; | |||||
} | } | ||||
try | try | ||||
{ | { | ||||
@@ -186,6 +186,11 @@ namespace JT1078.Gateway.Sessions | |||||
return Sessions.Select(s => s.Value).Where(w => w.Sim == sim && w.ChannelNo == channelNo).ToList(); | return Sessions.Select(s => s.Value).Where(w => w.Sim == sim && w.ChannelNo == channelNo).ToList(); | ||||
} | } | ||||
public List<JT1078HttpContext> GetAllHttpContextBySimAndChannelNo(string sim, int channelNo) | |||||
{ | |||||
return Sessions.Select(s => s.Value).Where(w => w.Sim == sim && w.ChannelNo == channelNo&&!w.IsWebSocket).ToList(); | |||||
} | |||||
public List<JT1078HttpContext> GetAll() | public List<JT1078HttpContext> GetAll() | ||||
{ | { | ||||
return Sessions.Select(s => s.Value).ToList(); | return Sessions.Select(s => s.Value).ToList(); | ||||