From 29676586b214ba02e79bccce64c92b6f5a8af4df Mon Sep 17 00:00:00 2001
From: "SmallChi(Koike)" <564952747@qq.com>
Date: Thu, 15 Apr 2021 18:22:23 +0800
Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4http=E6=9C=8D=E5=8A=A1?=
=?UTF-8?q?=E5=99=A8=E5=A2=9E=E5=8A=A0fmp4?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Extensions/JT1078HttpContextExtensions.cs | 17 +++---
src/JT1078.Gateway/JT1078HttpServer.cs | 4 +-
.../Services/HLSRequestManager.cs | 52 ++++++-------------
3 files changed, 27 insertions(+), 46 deletions(-)
diff --git a/src/JT1078.Gateway/Extensions/JT1078HttpContextExtensions.cs b/src/JT1078.Gateway/Extensions/JT1078HttpContextExtensions.cs
index 5ca5564..113d0b7 100644
--- a/src/JT1078.Gateway/Extensions/JT1078HttpContextExtensions.cs
+++ b/src/JT1078.Gateway/Extensions/JT1078HttpContextExtensions.cs
@@ -1,6 +1,7 @@
using JT1078.Gateway.Metadata;
using System;
using System.Collections.Generic;
+using System.IO;
using System.Net;
using System.Net.WebSockets;
using System.Text;
@@ -66,17 +67,17 @@ namespace JT1078.Gateway.Extensions
/// 返回m3u8响应
///
///
- ///
+ ///
///
- public static async ValueTask HttpM3U8Async(this HttpListenerContext context, ReadOnlyMemory buffer)
+ public static async ValueTask HttpM3U8Async(this HttpListenerContext context, Stream stream)
{
context.Response.AddHeader("Access-Control-Allow-Headers", "*");
context.Response.AppendHeader("Access-Control-Allow-Origin", "*");
context.Response.ContentType = "application/x-mpegURL";
context.Response.StatusCode = (int)HttpStatusCode.OK;
- context.Response.ContentLength64 = buffer.Length;
+ context.Response.ContentLength64 = stream.Length;
context.Response.KeepAlive = false;
- await context.Response.OutputStream.WriteAsync(buffer);
+ await stream.CopyToAsync(context.Response.OutputStream);
context.Response.OutputStream.Close();
context.Response.Close();
}
@@ -84,17 +85,17 @@ namespace JT1078.Gateway.Extensions
/// 返回ts响应数
///
///
- ///
+ ///
///
- public static async ValueTask HttpTsAsync(this HttpListenerContext context, ReadOnlyMemory buffer)
+ public static async ValueTask HttpTsAsync(this HttpListenerContext context, Stream stream)
{
context.Response.AddHeader("Access-Control-Allow-Headers", "*");
context.Response.AppendHeader("Access-Control-Allow-Origin", "*");
context.Response.ContentType = "video/MP2T";
context.Response.StatusCode = (int)HttpStatusCode.OK;
- context.Response.ContentLength64 = buffer.Length;
+ context.Response.ContentLength64 = stream.Length;
context.Response.KeepAlive = false;
- await context.Response.OutputStream.WriteAsync(buffer);
+ await stream.CopyToAsync(context.Response.OutputStream);
context.Response.OutputStream.Close();
context.Response.Close();
}
diff --git a/src/JT1078.Gateway/JT1078HttpServer.cs b/src/JT1078.Gateway/JT1078HttpServer.cs
index 167fe25..0187edf 100644
--- a/src/JT1078.Gateway/JT1078HttpServer.cs
+++ b/src/JT1078.Gateway/JT1078HttpServer.cs
@@ -143,14 +143,14 @@ namespace JT1078.Gateway
{
if (authorization.Authorization(context, out IPrincipal principal))
{
- hLSRequestManager.HandleHlsRequest(context, principal);
+ hLSRequestManager.HandleHlsRequest(context, principal, jT1078AVInfo);
}
}
private void ProcessTs(HttpListenerContext context, JT1078AVInfo jT1078AVInfo)
{
//ts 无需验证
- hLSRequestManager.HandleHlsRequest(context, default);
+ hLSRequestManager.HandleHlsRequest(context, default, jT1078AVInfo);
}
private async void ProcessFlv(HttpListenerContext context, JT1078AVInfo jT1078AVInfo)
diff --git a/src/JT1078.Gateway/Services/HLSRequestManager.cs b/src/JT1078.Gateway/Services/HLSRequestManager.cs
index 3b6ea34..0b7e03c 100644
--- a/src/JT1078.Gateway/Services/HLSRequestManager.cs
+++ b/src/JT1078.Gateway/Services/HLSRequestManager.cs
@@ -29,17 +29,11 @@ namespace JT1078.Gateway.Services
private readonly HLSPathStorage hLSPathStorage;
private readonly ILogger Logger;
- private readonly IServiceProvider serviceProvider;
- //private FileSystemWatcher fileSystemWatcher;
-
- public HLSRequestManager(
- IOptions jT1078ConfigurationAccessor,
- JT1078HttpSessionManager httpSessionManager,
- HLSPathStorage hLSPathStorage,
- IServiceProvider serviceProvider,
- ILoggerFactory loggerFactory)
+ public HLSRequestManager(IOptions jT1078ConfigurationAccessor,
+ JT1078HttpSessionManager httpSessionManager,
+ HLSPathStorage hLSPathStorage,
+ ILoggerFactory loggerFactory)
{
- this.serviceProvider = serviceProvider;
HttpSessionManager = httpSessionManager;
this.hLSPathStorage = hLSPathStorage;
Configuration = jT1078ConfigurationAccessor.Value;
@@ -50,34 +44,24 @@ namespace JT1078.Gateway.Services
///
///
///
- public async void HandleHlsRequest(HttpListenerContext context, IPrincipal principal)
+ ///
+ public async void HandleHlsRequest(HttpListenerContext context, IPrincipal principal, JT1078AVInfo jT1078AVInfo)
{
- if (context.Request.QueryString.Count < 2)
- {
- context.Http404();
- return;
- }
- string sim = context.Request.QueryString.Get("sim");//终端sim卡号
- string channelNo = context.Request.QueryString.Get("channelNo");//通道号
- string key = $"{sim}_{channelNo}";
string filename = Path.GetFileName(context.Request.Url.AbsolutePath.ToString());
- string filepath = Path.Combine(Configuration.HlsRootDirectory, key, filename);
-
+ string filepath = Path.Combine(Configuration.HlsRootDirectory, jT1078AVInfo.ToString(), filename);
if (hLSPathStorage.ExsitPath(filepath))
{
try
{
using (FileStream sr = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
- MemoryStream ms = new MemoryStream();
- sr.CopyTo(ms);
if (filename.Contains("m3u8"))
{
- await context.HttpM3U8Async(ms.ToArray());
+ await context.HttpM3U8Async(sr);
}
else if (filename.Contains("ts"))
{
- await context.HttpTsAsync(ms.ToArray());
+ await context.HttpTsAsync(sr);
}
else
{
@@ -97,7 +81,7 @@ namespace JT1078.Gateway.Services
{
if (filename.ToLower().Contains("m3u8"))
{
- var directory = Path.Combine(Configuration.HlsRootDirectory, key);
+ var directory = Path.Combine(Configuration.HlsRootDirectory, jT1078AVInfo.ToString());
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
@@ -120,9 +104,7 @@ namespace JT1078.Gateway.Services
using (FileStream sr = new FileStream(arg.FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
hLSPathStorage.AddPath(arg.FullPath, key);
- MemoryStream ms = new MemoryStream();
- sr.CopyTo(ms);
- await context.HttpM3U8Async(ms.ToArray());
+ await context.HttpM3U8Async(sr);
}
}
catch (Exception ex)
@@ -147,18 +129,16 @@ namespace JT1078.Gateway.Services
}
else
{
- hLSPathStorage.AddPath(filepath, key);
+ hLSPathStorage.AddPath(filepath, jT1078AVInfo.ToString());
using (FileStream sr = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
- MemoryStream ms = new MemoryStream();
- sr.CopyTo(ms);
if (filename.Contains("m3u8"))
{
- await context.HttpM3U8Async(ms.ToArray());
+ await context.HttpM3U8Async(sr);
}
else if (filename.Contains("ts"))
{
- await context.HttpTsAsync(ms.ToArray());
+ await context.HttpTsAsync(sr);
}
else
{
@@ -167,8 +147,8 @@ namespace JT1078.Gateway.Services
}
}
var jT1078HttpContext = new JT1078HttpContext(context, principal);
- jT1078HttpContext.Sim = sim;
- jT1078HttpContext.ChannelNo = int.Parse(channelNo);
+ jT1078HttpContext.Sim = jT1078AVInfo.Sim;
+ jT1078HttpContext.ChannelNo = jT1078AVInfo.ChannelNo;
jT1078HttpContext.RTPVideoType = RTPVideoType.Http_Hls;
HttpSessionManager.AddOrUpdateHlsSession(jT1078HttpContext);
}