소스 검색

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;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Net; using System.Net;
using System.Security.Principal; using System.Security.Principal;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks;


namespace JT1078.Gateway namespace JT1078.Gateway
{ {
@@ -42,6 +45,21 @@ namespace JT1078.Gateway
SessionManager = sessionManager; SessionManager = sessionManager;
Configuration = jT1078ConfigurationAccessor.Value; Configuration = jT1078ConfigurationAccessor.Value;
Logger = loggerFactory.CreateLogger<HLSRequestManager>(); 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> /// <summary>
/// 处理hls实时视频请求 /// 处理hls实时视频请求
@@ -71,7 +89,8 @@ namespace JT1078.Gateway
{ {
if (context.Response.ContentLength64 != 0) return; if (context.Response.ContentLength64 != 0) return;
//wwwroot\1234_2\live.m3u8 //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 sim = key.Split("_")[0];
var channel = int.Parse(key.Split("_")[1]); var channel = int.Parse(key.Split("_")[1]);
try try
@@ -135,21 +154,8 @@ namespace JT1078.Gateway
var jT1078HttpContext = new JT1078HttpContext(context, principal); var jT1078HttpContext = new JT1078HttpContext(context, principal);
jT1078HttpContext.Sim = sim; jT1078HttpContext.Sim = sim;
jT1078HttpContext.ChannelNo = int.Parse(channelNo); 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); var jT1078HttpContext = new JT1078HttpContext(context, wsContext,principal);
jT1078HttpContext.Sim = sim; jT1078HttpContext.Sim = sim;
jT1078HttpContext.ChannelNo = channelNo; jT1078HttpContext.ChannelNo = channelNo;
jT1078HttpContext.RTPVideoType = RTPVideoType.Ws_Flv;
SessionManager.TryAdd(jT1078HttpContext); SessionManager.TryAdd(jT1078HttpContext);
//这个发送出去,flv.js就报错了 //这个发送出去,flv.js就报错了
//await jT1078HttpContext.WebSocketSendHelloAsync(); //await jT1078HttpContext.WebSocketSendHelloAsync();
@@ -176,6 +177,7 @@ namespace JT1078.Gateway
{ {
var jT1078HttpContext = new JT1078HttpContext(context,principal); var jT1078HttpContext = new JT1078HttpContext(context,principal);
jT1078HttpContext.Sim = sim; jT1078HttpContext.Sim = sim;
jT1078HttpContext.RTPVideoType = RTPVideoType.Http_Flv;
jT1078HttpContext.ChannelNo = channelNo; jT1078HttpContext.ChannelNo = channelNo;
SessionManager.TryAdd(jT1078HttpContext); SessionManager.TryAdd(jT1078HttpContext);
} }


+ 7
- 0
src/JT1078.Gateway/Metadata/JT1078HttpContext.cs 파일 보기

@@ -16,6 +16,7 @@ namespace JT1078.Gateway.Metadata
[JsonIgnore] [JsonIgnore]
public HttpListenerWebSocketContext WebSocketContext { get; } public HttpListenerWebSocketContext WebSocketContext { get; }
public IPrincipal User { get; } public IPrincipal User { get; }
public RTPVideoType RTPVideoType { get; set; }
public string Sim { get; set; } public string Sim { get; set; }
public int ChannelNo { get; set; } public int ChannelNo { get; set; }
public bool IsWebSocket public bool IsWebSocket
@@ -45,4 +46,10 @@ namespace JT1078.Gateway.Metadata
FirstSend = false; 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) public bool TryAdd(JT1078HttpContext httpContext)
{ {
return Sessions.TryAdd(httpContext.SessionId, 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) public async void TryRemove(string sessionId)
{ {
if(Sessions.TryRemove(sessionId, out JT1078HttpContext session)) if(Sessions.TryRemove(sessionId, out JT1078HttpContext session))


불러오는 중...
취소
저장