浏览代码

hls修改

master
waterliu99 4 年前
父节点
当前提交
01996c1c38
共有 4 个文件被更改,包括 43 次插入16 次删除
  1. +22
    -16
      src/JT1078.Gateway/HLSRequestManager.cs
  2. +2
    -0
      src/JT1078.Gateway/JT1078HttpServer.cs
  3. +7
    -0
      src/JT1078.Gateway/Metadata/JT1078HttpContext.cs
  4. +12
    -0
      src/JT1078.Gateway/Sessions/JT1078HttpSessionManager.cs

+ 22
- 16
src/JT1078.Gateway/HLSRequestManager.cs 查看文件

@@ -8,9 +8,12 @@ using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace JT1078.Gateway
{
@@ -42,6 +45,21 @@ namespace JT1078.Gateway
SessionManager = sessionManager;
Configuration = jT1078ConfigurationAccessor.Value;
Logger = loggerFactory.CreateLogger<HLSRequestManager>();

Task.Run(()=> {
while (true)
{
var expireds= HttpSessionManager.GetAll().Where(m => DateTime.Now.Subtract(m.StartTime).TotalSeconds > 20).ToList();
foreach (var item in expireds)
{
//移除httpsession
HttpSessionManager.TryRemoveBySim(item.Sim);
//移除tcpsession
SessionManager.RemoveByTerminalPhoneNo(item.Sim);
}
Thread.Sleep(TimeSpan.FromSeconds(10));
}
});
}
/// <summary>
/// 处理hls实时视频请求
@@ -71,7 +89,8 @@ namespace JT1078.Gateway
{
if (context.Response.ContentLength64 != 0) return;
//wwwroot\1234_2\live.m3u8
var key = arg.FullPath.Replace(arg.Name, "").Substring(arg.FullPath.Replace(arg.Name, "").IndexOf("\\")).Replace("\\", "");
//var key = arg.FullPath.Replace(arg.Name, "").Substring(arg.FullPath.Replace(arg.Name, "").IndexOf("\\")).Replace("\\", "");
var key = arg.FullPath.Substring(arg.FullPath.IndexOf("\\")+1,arg.FullPath.LastIndexOf("\\"));
var sim = key.Split("_")[0];
var channel = int.Parse(key.Split("_")[1]);
try
@@ -135,21 +154,8 @@ namespace JT1078.Gateway
var jT1078HttpContext = new JT1078HttpContext(context, principal);
jT1078HttpContext.Sim = sim;
jT1078HttpContext.ChannelNo = int.Parse(channelNo);
HttpSessionManager.TryAdd(jT1078HttpContext);
//如果过了30s,还未收到浏览器请求,则移除掉session
memoryCache.Set(key, DateTime.Now, new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(10)
}.RegisterPostEvictionCallback((key, value, reason, state) =>
{
if (reason != EvictionReason.Expired) return;
//当清空httpssion时,同时清除tcpsseion
var removeSim = key.ToString().Split('_')[0];
//移除httpsession
HttpSessionManager.TryRemoveBySim(removeSim);
//移除tcpsession
SessionManager.RemoveByTerminalPhoneNo(removeSim);
}));
jT1078HttpContext.RTPVideoType = RTPVideoType.Http_Hls;
HttpSessionManager.AddOrUpdate(jT1078HttpContext);
}
}
}

+ 2
- 0
src/JT1078.Gateway/JT1078HttpServer.cs 查看文件

@@ -131,6 +131,7 @@ namespace JT1078.Gateway
var jT1078HttpContext = new JT1078HttpContext(context, wsContext,principal);
jT1078HttpContext.Sim = sim;
jT1078HttpContext.ChannelNo = channelNo;
jT1078HttpContext.RTPVideoType = RTPVideoType.Ws_Flv;
SessionManager.TryAdd(jT1078HttpContext);
//这个发送出去,flv.js就报错了
//await jT1078HttpContext.WebSocketSendHelloAsync();
@@ -176,6 +177,7 @@ namespace JT1078.Gateway
{
var jT1078HttpContext = new JT1078HttpContext(context,principal);
jT1078HttpContext.Sim = sim;
jT1078HttpContext.RTPVideoType = RTPVideoType.Http_Flv;
jT1078HttpContext.ChannelNo = channelNo;
SessionManager.TryAdd(jT1078HttpContext);
}


+ 7
- 0
src/JT1078.Gateway/Metadata/JT1078HttpContext.cs 查看文件

@@ -16,6 +16,7 @@ namespace JT1078.Gateway.Metadata
[JsonIgnore]
public HttpListenerWebSocketContext WebSocketContext { get; }
public IPrincipal User { get; }
public RTPVideoType RTPVideoType { get; set; }
public string Sim { get; set; }
public int ChannelNo { get; set; }
public bool IsWebSocket
@@ -45,4 +46,10 @@ namespace JT1078.Gateway.Metadata
FirstSend = false;
}
}
public enum RTPVideoType
{
Http_Flv,
Ws_Flv,
Http_Hls,
}
}

+ 12
- 0
src/JT1078.Gateway/Sessions/JT1078HttpSessionManager.cs 查看文件

@@ -25,9 +25,21 @@ namespace JT1078.Gateway.Sessions

public bool TryAdd(JT1078HttpContext httpContext)
{
return Sessions.TryAdd(httpContext.SessionId, httpContext);
}

public void AddOrUpdate(JT1078HttpContext httpContext) {
var session = Sessions.FirstOrDefault(m => m.Value.Sim == httpContext.Sim && m.Value.ChannelNo == httpContext.ChannelNo);
if (string.IsNullOrEmpty(session.Key))
{
Sessions.TryAdd(httpContext.SessionId, httpContext);
}
else {
Sessions.TryUpdate(session.Key, httpContext, session.Value);
}
}

public async void TryRemove(string sessionId)
{
if(Sessions.TryRemove(sessionId, out JT1078HttpContext session))


正在加载...
取消
保存