diff --git a/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/JT1078.Gateway.TestNormalHosting.csproj b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/JT1078.Gateway.TestNormalHosting.csproj index 1ff238f..e6bb06e 100644 --- a/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/JT1078.Gateway.TestNormalHosting.csproj +++ b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/JT1078.Gateway.TestNormalHosting.csproj @@ -2,7 +2,7 @@ Exe - net5.0 + net6.0 diff --git a/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Services/JT1078FMp4NormalMsgHostedService.cs b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Services/JT1078FMp4NormalMsgHostedService.cs index b464ce3..053b0e0 100644 --- a/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Services/JT1078FMp4NormalMsgHostedService.cs +++ b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Services/JT1078FMp4NormalMsgHostedService.cs @@ -27,6 +27,7 @@ namespace JT1078.Gateway.TestNormalHosting.Services private MessageDispatchDataService messageDispatchDataService; private ConcurrentDictionary> avFrameDict; private H264Decoder H264Decoder; + List SegmentPackages = new List();// 一段包 以I帧为界 IPPPP , IPPPP 一组 public JT1078FMp4NormalMsgHostedService( MessageDispatchDataService messageDispatchDataService, IMemoryCache memoryCache, @@ -56,90 +57,56 @@ namespace JT1078.Gateway.TestNormalHosting.Services Logger.LogDebug(JsonSerializer.Serialize(HttpSessionManager.GetAll())); Logger.LogDebug($"{data.SIM},{data.SN},{data.LogicChannelNumber},{data.Label3.DataType.ToString()},{data.Label3.SubpackageType.ToString()},{data.Bodies.ToHexString()}"); } - var nalus = H264Decoder.ParseNALU(data); - string key = $"{data.GetKey()}_{ikey}"; + if (data.Label3.DataType == Protocol.Enums.JT1078DataType.视频I帧) { - var moovBuffer = FM4Encoder.VideoMoovBox( - nalus.FirstOrDefault(f => f.NALUHeader.NalUnitType == NalUnitType.SPS), - nalus.FirstOrDefault(f => f.NALUHeader.NalUnitType == NalUnitType.PPS)); - memoryCache.Set(key, moovBuffer); - } - //查找第一帧为I帧,否则不推送 - if (memoryCache.TryGetValue(key, out byte[] moov)) - { - var httpSessions = HttpSessionManager.GetAllBySimAndChannelNo(data.SIM.TrimStart('0'), data.LogicChannelNumber); - var firstHttpSessions = httpSessions.Where(w => !w.FirstSend && (w.RTPVideoType == Metadata.RTPVideoType.Http_FMp4 || w.RTPVideoType == Metadata.RTPVideoType.Ws_FMp4)).ToList(); - if (firstHttpSessions.Count > 0) + if (SegmentPackages.Count>0) { - try + //判断是否首帧 + //Logger.LogDebug($"时间1:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff")}"); + var httpSessions = HttpSessionManager.GetAllBySimAndChannelNo(data.SIM.TrimStart('0'), data.LogicChannelNumber); + var firstHttpSessions = httpSessions.Where(w => !w.FirstSend && (w.RTPVideoType == Metadata.RTPVideoType.Http_FMp4 || w.RTPVideoType == Metadata.RTPVideoType.Ws_FMp4)).ToList(); + var otherHttpSessions = httpSessions.Where(w => w.FirstSend && (w.RTPVideoType == Metadata.RTPVideoType.Http_FMp4 || w.RTPVideoType == Metadata.RTPVideoType.Ws_FMp4)).ToList(); + if (firstHttpSessions.Count > 0) { - try - { - var ftyp = FM4Encoder.FtypBox(); - foreach (var session in firstHttpSessions) - { - HttpSessionManager.SendAVData(session, ftyp.Concat(moov).ToArray(), true); - } - } - catch (Exception ex) + //唯一 + var ftyp = FM4Encoder.FtypBox(); + var package1 = SegmentPackages[0]; + var nalus1 = H264Decoder.ParseNALU(package1); + var moov = FM4Encoder.MoovBox( + nalus1.FirstOrDefault(f => f.NALUHeader.NalUnitType == NalUnitType.SPS), + nalus1.FirstOrDefault(f => f.NALUHeader.NalUnitType == NalUnitType.PPS)); + //首帧 + var styp = FM4Encoder.StypBox(); + var firstVideo = FM4Encoder.OtherVideoBox(SegmentPackages); + foreach (var session in firstHttpSessions) { - Logger.LogError(ex, $"{data.SIM},{true},{data.SN},{data.LogicChannelNumber},{data.Label3.DataType.ToString()},{data.Label3.SubpackageType.ToString()},{data.Bodies.ToHexString()}"); + HttpSessionManager.SendAVData(session, ftyp.Concat(moov).Concat(styp).Concat(firstVideo).ToArray(), true); + SegmentPackages.Clear();//发送完成后清理 } } - catch (Exception ex) - { - Logger.LogError(ex, $"{data.SIM},{true},{data.SN},{data.LogicChannelNumber},{data.Label3.DataType.ToString()},{data.Label3.SubpackageType.ToString()},{data.Bodies.ToHexString()}"); - } - } - var otherHttpSessions = httpSessions.Where(w => w.FirstSend && (w.RTPVideoType == Metadata.RTPVideoType.Http_FMp4 || w.RTPVideoType == Metadata.RTPVideoType.Ws_FMp4)).ToList(); - if (otherHttpSessions.Count > 0) - { - try + if (otherHttpSessions.Count > 0) { - //foreach (var session in otherHttpSessions) - //{ - // var fmp4VideoBuffer = FM4Encoder.OtherVideoBox(nalus); - // HttpSessionManager.SendAVData(session, FM4Encoder.StypBox().Concat(fmp4VideoBuffer).ToArray(), false); - //} - var firstNALU = nalus.FirstOrDefault(); - if (firstNALU == null) + //非首帧 + var styp = FM4Encoder.StypBox(); + var otherVideo = FM4Encoder.OtherVideoBox(SegmentPackages); + foreach (var session in otherHttpSessions) { - continue; + HttpSessionManager.SendAVData(session, styp.Concat(otherVideo).ToArray(), false); + SegmentPackages.Clear();//发送完成后清理 } - if (!avFrameDict.TryGetValue(firstNALU.GetKey(), out List cacheNALU)) - { - cacheNALU = new List(); - avFrameDict.TryAdd(firstNALU.GetKey(), cacheNALU); - } - foreach (var nalu in nalus) - { - if (nalu.Slice) - { - //H264 NALU slice first_mb_in_slice - cacheNALU.Add(nalu); - } - else - { - if (cacheNALU.Count > 0) - { - foreach (var session in otherHttpSessions) - { - var fmp4VideoBuffer = FM4Encoder.OtherVideoBox(cacheNALU); - HttpSessionManager.SendAVData(session, FM4Encoder.StypBox().Concat(fmp4VideoBuffer).ToArray(), false); - } - cacheNALU.Clear(); - } - cacheNALU.Add(nalu); - } - } - } - catch (Exception ex) - { - Logger.LogError(ex, $"{data.SIM},{false},{data.SN},{data.LogicChannelNumber},{data.Label3.DataType.ToString()},{data.Label3.SubpackageType.ToString()},{data.Bodies.ToHexString()}"); + } } + + if (SegmentPackages.Count==0) + SegmentPackages.Add(data); } + else { + if (SegmentPackages.Count!=0) { + SegmentPackages.Add(data); + } + } } catch (Exception ex) { diff --git a/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Services/JT1078FlvNormalMsgHostedService.cs b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Services/JT1078FlvNormalMsgHostedService.cs index 06189ca..58f7f63 100644 --- a/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Services/JT1078FlvNormalMsgHostedService.cs +++ b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Services/JT1078FlvNormalMsgHostedService.cs @@ -54,45 +54,28 @@ namespace JT1078.Gateway.TestNormalHosting.Services Logger.LogDebug(JsonSerializer.Serialize(HttpSessionManager.GetAll())); Logger.LogDebug($"{data.SIM},{data.SN},{data.LogicChannelNumber},{data.Label3.DataType.ToString()},{data.Label3.SubpackageType.ToString()},{data.Bodies.ToHexString()}"); } - string key = $"{data.GetKey()}_{ikey}"; - if (data.Label3.DataType == Protocol.Enums.JT1078DataType.视频I帧) - { - memoryCache.Set(key, data); - } + var httpSessions = HttpSessionManager.GetAllBySimAndChannelNo(data.SIM.TrimStart('0'), data.LogicChannelNumber); var firstHttpSessions = httpSessions.Where(w => !w.FirstSend && (w.RTPVideoType== Metadata.RTPVideoType.Http_Flv || w.RTPVideoType == Metadata.RTPVideoType.Ws_Flv)).ToList(); + var otherHttpSessions = httpSessions.Where(w => w.FirstSend && (w.RTPVideoType == Metadata.RTPVideoType.Http_Flv || w.RTPVideoType == Metadata.RTPVideoType.Ws_Flv)).ToList(); + if (firstHttpSessions.Count > 0) { - if (memoryCache.TryGetValue(key, out JT1078Package idata)) + if (data.Label3.DataType == Protocol.Enums.JT1078DataType.视频I帧) { - try - { - var flvVideoBuffer = FlvEncoder.EncoderVideoTag(idata, true); - foreach (var session in firstHttpSessions) - { - HttpSessionManager.SendAVData(session, flvVideoBuffer, true); - } - } - catch (Exception ex) + var flvVideoBuffer = FlvEncoder.EncoderVideoTag(data, true); + foreach (var session in firstHttpSessions) { - Logger.LogError(ex, $"{data.SIM},{true},{data.SN},{data.LogicChannelNumber},{data.Label3.DataType.ToString()},{data.Label3.SubpackageType.ToString()},{data.Bodies.ToHexString()}"); + HttpSessionManager.SendAVData(session, flvVideoBuffer, true); } } } - var otherHttpSessions = httpSessions.Where(w => w.FirstSend && (w.RTPVideoType == Metadata.RTPVideoType.Http_Flv || w.RTPVideoType == Metadata.RTPVideoType.Ws_Flv)).ToList(); if (otherHttpSessions.Count > 0) { - try - { - var flvVideoBuffer = FlvEncoder.EncoderVideoTag(data, false); - foreach (var session in otherHttpSessions) - { - HttpSessionManager.SendAVData(session, flvVideoBuffer, false); - } - } - catch (Exception ex) + var flvVideoBuffer = FlvEncoder.EncoderVideoTag(data, false); + foreach (var session in otherHttpSessions) { - Logger.LogError(ex, $"{data.SIM},{false},{data.SN},{data.LogicChannelNumber},{data.Label3.DataType.ToString()},{data.Label3.SubpackageType.ToString()},{data.Bodies.ToHexString()}"); + HttpSessionManager.SendAVData(session, flvVideoBuffer, false); } } } diff --git a/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Services/MessageDispatchHostedService.cs b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Services/MessageDispatchHostedService.cs index eedb362..c251f33 100644 --- a/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Services/MessageDispatchHostedService.cs +++ b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Services/MessageDispatchHostedService.cs @@ -33,9 +33,16 @@ namespace JT1078.Gateway.TestNormalHosting.Services var merge = JT1078.Protocol.JT1078Serializer.Merge(package); if (merge != null) { - await messageDispatchDataService.HlsChannel.Writer.WriteAsync(merge, stoppingToken); - await messageDispatchDataService.FlvChannel.Writer.WriteAsync(merge, stoppingToken); - await messageDispatchDataService.FMp4Channel.Writer.WriteAsync(merge, stoppingToken); + Parallel.Invoke( + async() => { + await messageDispatchDataService.FMp4Channel.Writer.WriteAsync(merge, stoppingToken); + }, + async () => { + await messageDispatchDataService.FlvChannel.Writer.WriteAsync(merge, stoppingToken); + }, + async () => { + await messageDispatchDataService.HlsChannel.Writer.WriteAsync(merge, stoppingToken); + }); } }); return Task.CompletedTask; diff --git a/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/appsettings.json b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/appsettings.json index 8b0f2a9..18c352a 100644 --- a/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/appsettings.json +++ b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/appsettings.json @@ -14,7 +14,7 @@ }, "JT1078Configuration": { "HttpPort": 15555, - "TcpPort": 10888 + "TcpPort": 1078 }, "M3U8Option": { "TsPathSimParamName": "sim", diff --git a/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/wwwroot/flv_demo/.vscode/settings.json b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/wwwroot/flv_demo/.vscode/settings.json new file mode 100644 index 0000000..6f3a291 --- /dev/null +++ b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/wwwroot/flv_demo/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "liveServer.settings.port": 5501 +} \ No newline at end of file diff --git a/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/wwwroot/flv_demo/index.html b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/wwwroot/flv_demo/index.html index 2bebff9..f79ff2a 100644 --- a/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/wwwroot/flv_demo/index.html +++ b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/wwwroot/flv_demo/index.html @@ -15,7 +15,7 @@ var flvPlayer = flvjs.createPlayer({ type: 'flv', isLive: true, - url: "http://127.0.0.1:15555/live.flv?sim=19019000001&channel=3&token=123456" + url: "http://49.235.89.102:15555/live.flv?sim=40281815788&channel=1&token=123456" }); flvPlayer.attachMediaElement(player); flvPlayer.load(); diff --git a/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/wwwroot/fmp4_demo/index.html b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/wwwroot/fmp4_demo/index.html index c9d89bf..e0e6f1f 100644 --- a/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/wwwroot/fmp4_demo/index.html +++ b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/wwwroot/fmp4_demo/index.html @@ -166,9 +166,9 @@ source_buffer.mode = 'sequence'; // source_buffer.mode = 'segments'; source_buffer.addEventListener("updateend", loadPacket); - //ws = new WebSocket("ws://49.235.89.102:15555/live.mp4?sim=19019000001&channel=3&token=123456"); + ws = new WebSocket("ws://49.235.89.102:15555/live.mp4?sim=40281815788&channel=1&token=123456"); - ws = new WebSocket("ws://49.235.89.102:15555/live.mp4?sim=1901305037&channel=2&token=123456"); + //ws = new WebSocket("ws://49.235.89.102:15555/live.mp4?sim=1901305037&channel=2&token=123456"); //ws = new WebSocket("ws://127.0.0.1:81/live/JT1078_8.live.mp4"); //创建WebSocket连接 ws.binaryType = 'arraybuffer'; ws.onmessage = function (e) { diff --git a/src/JT1078.Gateway/JT1078.Gateway.csproj b/src/JT1078.Gateway/JT1078.Gateway.csproj index 22e70e2..a84fb15 100644 --- a/src/JT1078.Gateway/JT1078.Gateway.csproj +++ b/src/JT1078.Gateway/JT1078.Gateway.csproj @@ -1,7 +1,7 @@  - netstandard2.1;net5.0; + net6.0; 9.0 Copyright 2019. SmallChi(Koike) @@ -39,11 +39,11 @@ - - - - - + + + + +