Browse Source

1.第一版hls网关,还要优化

master
waterliu99 4 years ago
parent
commit
fa991b605a
6 changed files with 79 additions and 11 deletions
  1. +11
    -0
      src/JT1078.Gateway.Tests/JT1078.Gateway.Test/PipeTest.cs
  2. +1
    -0
      src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/JT1078.Gateway.TestNormalHosting.csproj
  3. +48
    -0
      src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Services/JT1078HlsNormalMsgHostedService.cs
  4. +6
    -0
      src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/appsettings.json
  5. +8
    -11
      src/JT1078.Gateway/JT1078HttpServer.cs
  6. +5
    -0
      src/JT1078.Gateway/Sessions/JT1078HttpSessionManager.cs

+ 11
- 0
src/JT1078.Gateway.Tests/JT1078.Gateway.Test/PipeTest.cs View File

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

+ 1
- 0
src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/JT1078.Gateway.TestNormalHosting.csproj View File

@@ -7,6 +7,7 @@
<ItemGroup>
<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.Hosting" Version="3.1.7" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.7" />


+ 48
- 0
src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Services/JT1078HlsNormalMsgHostedService.cs View File

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

+ 6
- 0
src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/appsettings.json View File

@@ -14,5 +14,11 @@
},
"JT1078Configuration": {
"HttpPort": 15555
},
"M3U8Option": {
"TsFileCapacity": 10,
"TsFileMaxSecond": 10,
"M3U8FileName": "live.m3u8",
"HlsFileDirectory":"www/root/demo"
}
}

+ 8
- 11
src/JT1078.Gateway/JT1078HttpServer.cs View File

@@ -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
{


+ 5
- 0
src/JT1078.Gateway/Sessions/JT1078HttpSessionManager.cs View File

@@ -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<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()
{
return Sessions.Select(s => s.Value).ToList();


Loading…
Cancel
Save