From 29c9a9f988e90b95d0ed98fe3a9f88d37181dd8b Mon Sep 17 00:00:00 2001
From: "SmallChi(Koike)" <564952747@qq.com>
Date: Sun, 5 Jun 2022 23:51:30 +0800
Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4ws-fmp4=E6=92=AD=E6=94=BE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 +-
.../Configs/nlog.Win32NT.config | 4 +-
.../Program.cs | 2 +-
.../JT1078FMp4NormalMsgHostedService.cs | 107 ++-----
.../appsettings.json | 5 +-
.../wwwroot/fmp4_demo/index.html | 284 +++++++-----------
src/JT1078.Gateway.sln | 7 +
src/JT1078.Gateway/JT1078.Gateway.csproj | 7 +-
8 files changed, 151 insertions(+), 267 deletions(-)
diff --git a/README.md b/README.md
index c3a6453..3066bb9 100644
--- a/README.md
+++ b/README.md
@@ -15,7 +15,7 @@
| --- | ---| --- |---|---|
| flv | 😀| ☹ |😀|http-flv、ws-flv|
| m3u8 | 😀| ☹ |😀|http|
-| fmp4 | 😀| ☹ |☹|http-fmp4、ws-fmp4|
+| fmp4 | 😀| ☹ |😀(部分设备可用)|http-fmp4[X]、ws-fmp4[✔]|
## NuGet安装
diff --git a/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Configs/nlog.Win32NT.config b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Configs/nlog.Win32NT.config
index 78f7b08..67c4513 100644
--- a/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Configs/nlog.Win32NT.config
+++ b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Configs/nlog.Win32NT.config
@@ -41,8 +41,8 @@
-
-
+
+
\ No newline at end of file
diff --git a/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Program.cs b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Program.cs
index 61fd6fb..bb33ae3 100644
--- a/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Program.cs
+++ b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Program.cs
@@ -29,7 +29,7 @@ namespace JT1078.Gateway.TestNormalHosting
})
.ConfigureLogging((context, logging) =>
{
- logging.SetMinimumLevel(LogLevel.Trace);
+ //logging.SetMinimumLevel(LogLevel.Trace);
Console.WriteLine($"Environment.OSVersion.Platform:{Environment.OSVersion.Platform.ToString()}");
NLog.LogManager.LoadConfiguration($"Configs/nlog.{Environment.OSVersion.Platform.ToString()}.config");
logging.AddNLog(new NLogProviderOptions { CaptureMessageTemplates = true, CaptureMessageProperties = true });
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 7a1f245..888a2a6 100644
--- a/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Services/JT1078FMp4NormalMsgHostedService.cs
+++ b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Services/JT1078FMp4NormalMsgHostedService.cs
@@ -26,8 +26,7 @@ namespace JT1078.Gateway.TestNormalHosting.Services
private MessageDispatchDataService messageDispatchDataService;
private ConcurrentDictionary avFrameDict;
private H264Decoder H264Decoder;
- List NaluFilter;
- BlockingCollection<(string SIM, byte ChannelNo,byte[]FirstBuffer, byte[] OtherBuffer)> FMp4Blocking;
+ BlockingCollection<(string SIM, byte ChannelNo,byte[]FirstBuffer, byte[] AVFrameInfoBuffer, byte[] OtherBuffer)> FMp4Blocking;
public JT1078FMp4NormalMsgHostedService(
MessageDispatchDataService messageDispatchDataService,
ILoggerFactory loggerFactory,
@@ -41,12 +40,7 @@ namespace JT1078.Gateway.TestNormalHosting.Services
H264Decoder= h264Decoder;
this.messageDispatchDataService = messageDispatchDataService;
avFrameDict = new ConcurrentDictionary();
- FMp4Blocking=new BlockingCollection<(string SIM, byte ChannelNo, byte[] FirstBuffer, byte[] OtherBuffer)>();
- NaluFilter = new List();
- NaluFilter.Add(NalUnitType.SEI);
- NaluFilter.Add(NalUnitType.PPS);
- NaluFilter.Add(NalUnitType.SPS);
- NaluFilter.Add(NalUnitType.AUD);
+ FMp4Blocking=new BlockingCollection<(string SIM, byte ChannelNo, byte[] FirstBuffer, byte[] AVFrameInfoBuffer, byte[] OtherBuffer)>();
}
public Task StartAsync(CancellationToken cancellationToken)
@@ -57,74 +51,32 @@ namespace JT1078.Gateway.TestNormalHosting.Services
var data = await messageDispatchDataService.FMp4Channel.Reader.ReadAsync();
try
{
- if (Logger.IsEnabled(LogLevel.Debug))
+ //if (Logger.IsEnabled(LogLevel.Debug))
+ //{
+ // 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()}");
+ //}
+ bool keyframe = data.Label3.DataType == Protocol.Enums.JT1078DataType.视频I帧;
+ JT1078AVFrame avframe = H264Decoder.ParseAVFrame(data);
+ if (avframe.Nalus!= null && avframe.Nalus.Count>0)
{
- 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()}");
- }
- List h264NALUs = H264Decoder.ParseNALU(data);
- if (h264NALUs!=null && h264NALUs.Count>0)
- {
- if(!avFrameDict.TryGetValue(data.GetKey(),out FMp4AVContext cacheFrame))
+ if(!avFrameDict.TryGetValue(data.GetAVKey(),out FMp4AVContext cacheFrame))
{
cacheFrame=new FMp4AVContext();
- avFrameDict.TryAdd(data.GetKey(), cacheFrame);
+ avFrameDict.TryAdd(data.GetAVKey(), cacheFrame);
}
- foreach(var nalu in h264NALUs)
+ if(keyframe)
{
- if (NaluFilter.Contains(nalu.NALUHeader.NalUnitType))
+ if(avframe.SPS!=null && avframe.PPS != null)
{
- if (nalu.NALUHeader.NalUnitType== NalUnitType.SPS)
- {
- cacheFrame.SPSNalu=nalu;
- }
- else if (nalu.NALUHeader.NalUnitType== NalUnitType.PPS)
- {
- cacheFrame.PPSNalu=nalu;
- }
- }
- else
- {
- cacheFrame.NALUs.Add(nalu);
+ cacheFrame.AVFrameInfoBuffer = JsonSerializer.SerializeToUtf8Bytes(
+ new { Codecs = avframe .ToCodecs(), Width = avframe .Width, Height =avframe.Height});
+ cacheFrame.FirstCacheBuffer = FM4Encoder.FirstVideoBox(avframe);
}
}
- if (cacheFrame.NALUs.Count>1)
+ if (cacheFrame.FirstCacheBuffer != null)
{
- if (cacheFrame.FirstCacheBuffer==null)
- {
- cacheFrame.FirstCacheBuffer=FM4Encoder.FirstVideoBox(cacheFrame.SPSNalu, cacheFrame.PPSNalu);
- }
- List tmp = new List();
- int i = 0;
- foreach (var item in cacheFrame.NALUs)
- {
- if (item.NALUHeader.KeyFrame)
- {
- if (tmp.Count>0)
- {
- FMp4Blocking.Add((data.SIM, data.LogicChannelNumber, cacheFrame.FirstCacheBuffer, FM4Encoder.OtherVideoBox(tmp)));
- i+=tmp.Count;
- tmp.Clear();
- }
- tmp.Add(item);
- i+=tmp.Count;
- FMp4Blocking.Add((data.SIM, data.LogicChannelNumber, cacheFrame.FirstCacheBuffer, FM4Encoder.OtherVideoBox(tmp)));
- tmp.Clear();
- cacheFrame.PrevPrimaryNalu = item;
- continue;
- }
- if (cacheFrame.PrevPrimaryNalu!=null) //第一帧I帧
- {
- if (tmp.Count>1)
- {
- FMp4Blocking.Add((data.SIM, data.LogicChannelNumber, cacheFrame.FirstCacheBuffer, FM4Encoder.OtherVideoBox(tmp)));
- i+=tmp.Count;
- tmp.Clear();
- }
- tmp.Add(item);
- }
- }
- cacheFrame.NALUs.RemoveRange(0, i);
+ FMp4Blocking.Add((data.SIM, data.LogicChannelNumber, cacheFrame.FirstCacheBuffer, cacheFrame.AVFrameInfoBuffer,FM4Encoder.OtherVideoBox(avframe.Nalus, data.GetAVKey(), keyframe)));
}
}
}
@@ -132,13 +84,18 @@ namespace JT1078.Gateway.TestNormalHosting.Services
{
Logger.LogError(ex, $"{data.SIM},{data.SN},{data.LogicChannelNumber},{data.Label3.DataType.ToString()},{data.Label3.SubpackageType.ToString()},{data.Bodies.ToHexString()}");
}
-
}
});
Task.Run(() => {
+ //var filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "JT1078_7_4_4.mp4");
+ //if (File.Exists(filepath))
+ //{
+ // File.Delete(filepath);
+ //}
+ //using var fileStream = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write);
try
{
- foreach(var item in FMp4Blocking.GetConsumingEnumerable(cancellationToken))
+ foreach (var item in FMp4Blocking.GetConsumingEnumerable(cancellationToken))
{
var httpSessions = HttpSessionManager.GetAllBySimAndChannelNo(item.SIM.TrimStart('0'), item.ChannelNo);
var firstHttpSessions = httpSessions.Where(w => !w.FirstSend && (w.RTPVideoType == Metadata.RTPVideoType.Http_FMp4 || w.RTPVideoType == Metadata.RTPVideoType.Ws_FMp4)).ToList();
@@ -148,8 +105,11 @@ namespace JT1078.Gateway.TestNormalHosting.Services
//首帧
foreach (var session in firstHttpSessions)
{
- HttpSessionManager.SendAVData(session, item.FirstBuffer, true);
+ HttpSessionManager.SendAVData(session, item.AVFrameInfoBuffer, true);
+ HttpSessionManager.SendAVData(session, item.FirstBuffer, false);
+ //fileStream.Write(item.FirstBuffer);
HttpSessionManager.SendAVData(session, item.OtherBuffer, false);
+ //fileStream.Write(item.OtherBuffer);
}
}
if (otherHttpSessions.Count > 0)
@@ -158,6 +118,7 @@ namespace JT1078.Gateway.TestNormalHosting.Services
foreach (var session in otherHttpSessions)
{
HttpSessionManager.SendAVData(session, item.OtherBuffer, false);
+ //fileStream.Write(item.OtherBuffer);
}
}
}
@@ -166,6 +127,7 @@ namespace JT1078.Gateway.TestNormalHosting.Services
{
}
+ //fileStream.Close();
});
return Task.CompletedTask;
}
@@ -177,11 +139,8 @@ namespace JT1078.Gateway.TestNormalHosting.Services
public class FMp4AVContext
{
+ public byte[] AVFrameInfoBuffer { get; set; }
public byte[] FirstCacheBuffer { get; set; }
- public H264NALU PrevPrimaryNalu { get; set; }
- public H264NALU SPSNalu { get; set; }
- public H264NALU PPSNalu { get; set; }
- public List NALUs { get; set; } = new List();
}
}
}
diff --git a/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/appsettings.json b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/appsettings.json
index 18c352a..f7ce160 100644
--- a/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/appsettings.json
+++ b/src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/appsettings.json
@@ -1,14 +1,13 @@
{
"Logging": {
- "IncludeScopes": false,
"Debug": {
"LogLevel": {
- "Default": "Trace"
+ "Default": "Information"
}
},
"Console": {
"LogLevel": {
- "Default": "Trace"
+ "Default": "Information"
}
}
},
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 e0e6f1f..a5df29f 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
@@ -7,198 +7,116 @@
MSE FMp4 Demo
-