瀏覽代碼

集成mp4视频协议

master
waterliu99 3 年之前
父節點
當前提交
6ec31f5e78
共有 7 個文件被更改,包括 50 次插入53 次删除
  1. +2
    -2
      src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Program.cs
  2. +22
    -48
      src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Services/JT1078FMp4NormalMsgHostedService.cs
  3. +2
    -1
      src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/wwwroot/fmp4_demo/index.html
  4. +12
    -0
      src/JT1078.Gateway.sln
  5. +1
    -1
      src/JT1078.Gateway/JT1078.Gateway.csproj
  6. +5
    -0
      src/JT1078.Gateway/JT1078.Gateway.xml
  7. +6
    -1
      src/JT1078.Gateway/Metadata/JT1078HttpContext.cs

+ 2
- 2
src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Program.cs 查看文件

@@ -42,14 +42,14 @@ namespace JT1078.Gateway.TestNormalHosting
//hls视频解码器
services.AddSingleton<TSEncoder>();
services.AddSingleton<M3U8FileManage>();
services.AddSingleton<H264Decoder>();
services.AddSingleton<FMp4Encoder>();
//添加hls依赖项
services.AddHlsGateway(hostContext.Configuration);
services.Configure<M3U8Option>(hostContext.Configuration.GetSection("M3U8Option"));
var m3u8Option = services.BuildServiceProvider().GetRequiredService<IOptions<M3U8Option>>().Value;
services.AddSingleton(m3u8Option);

services.AddSingleton<FMp4Encoder>();

//使用内存队列实现会话通知
services.AddJT1078Gateway(hostContext.Configuration)
.AddTcp()


+ 22
- 48
src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/Services/JT1078FMp4NormalMsgHostedService.cs 查看文件

@@ -1,22 +1,19 @@
using JT1078.Gateway.Abstractions;
using JT1078.Gateway.Sessions;
using JT1078.Flv;
using JT1078.Gateway.Sessions;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using Microsoft.Extensions.Logging;
using JT1078.Flv.Extensions;
using Microsoft.Extensions.Caching.Memory;
using JT1078.Protocol;
using System.Text.Json;
using System.Text.Json.Serialization;
using JT1078.FMp4;
using JT1078.Protocol.H264;
using System.Collections.Concurrent;
using JT1078.FMp4;
using JT1078.Protocol.Extensions;
using System.IO;

namespace JT1078.Gateway.TestNormalHosting.Services
{
@@ -24,7 +21,6 @@ namespace JT1078.Gateway.TestNormalHosting.Services
{
private JT1078HttpSessionManager HttpSessionManager;
private FMp4Encoder FM4Encoder;
private readonly H264Decoder H264Decoder;
private ILogger Logger;
private IMemoryCache memoryCache;
private const string ikey = "IFMp4KEY";
@@ -35,14 +31,12 @@ namespace JT1078.Gateway.TestNormalHosting.Services
MessageDispatchDataService messageDispatchDataService,
IMemoryCache memoryCache,
ILoggerFactory loggerFactory,
H264Decoder h264Decoder,
FMp4Encoder fM4Encoder,
JT1078HttpSessionManager httpSessionManager)
{
Logger = loggerFactory.CreateLogger<JT1078FMp4NormalMsgHostedService>();
HttpSessionManager = httpSessionManager;
FM4Encoder = fM4Encoder;
H264Decoder = h264Decoder;
this.memoryCache = memoryCache;
this.messageDispatchDataService = messageDispatchDataService;
avFrameDict = new ConcurrentDictionary<string, List<H264NALU>>();
@@ -51,10 +45,10 @@ namespace JT1078.Gateway.TestNormalHosting.Services
{
while (!stoppingToken.IsCancellationRequested)
{
var data = await messageDispatchDataService.FlvChannel.Reader.ReadAsync();
var data = await messageDispatchDataService.FMp4Channel.Reader.ReadAsync();

try
{
var nalus = H264Decoder.ParseNALU(data);
if (Logger.IsEnabled(LogLevel.Debug))
{
Logger.LogDebug(JsonSerializer.Serialize(HttpSessionManager.GetAll()));
@@ -63,9 +57,7 @@ namespace JT1078.Gateway.TestNormalHosting.Services
string key = $"{data.GetKey()}_{ikey}";
if (data.Label3.DataType == Protocol.Enums.JT1078DataType.视频I帧)
{
var moov = FM4Encoder.EncoderMoovBox(nalus.FirstOrDefault(f => f.NALUHeader.NalUnitType == NalUnitType.SPS),
nalus.FirstOrDefault(f => f.NALUHeader.NalUnitType == NalUnitType.PPS));
memoryCache.Set(key, moov);
memoryCache.Set(key, data);
}
var httpSessions = HttpSessionManager.GetAllBySimAndChannelNo(data.SIM.TrimStart('0'), data.LogicChannelNumber);
var firstHttpSessions = httpSessions.Where(w => !w.FirstSend).ToList();
@@ -73,14 +65,19 @@ namespace JT1078.Gateway.TestNormalHosting.Services
{
try
{
var flvVideoBuffer = FM4Encoder.EncoderFtypBox();
memoryCache.TryGetValue(key, out byte[] moovBuffer);
foreach (var session in firstHttpSessions)
if (memoryCache.TryGetValue(key, out JT1078Package idata))
{
HttpSessionManager.SendAVData(session, flvVideoBuffer, true);
if (moovBuffer != null)
try
{
foreach (var session in firstHttpSessions)
{
var fmp4VideoBuffer = FM4Encoder.EncoderVideo(idata, session.FMp4EncoderInfo, true);
HttpSessionManager.SendAVData(session, fmp4VideoBuffer, true);
}
}
catch (Exception ex)
{
HttpSessionManager.SendAVData(session, moovBuffer, false);
Logger.LogError(ex, $"{data.SIM},{true},{data.SN},{data.LogicChannelNumber},{data.Label3.DataType.ToString()},{data.Label3.SubpackageType.ToString()},{data.Bodies.ToHexString()}");
}
}
}
@@ -94,34 +91,10 @@ namespace JT1078.Gateway.TestNormalHosting.Services
{
try
{
if(!avFrameDict.TryGetValue(key, out List<H264NALU> frames))
foreach (var session in otherHttpSessions)
{
frames = new List<H264NALU>();
avFrameDict.TryAdd(key, frames);
}
foreach (var nalu in nalus)
{
if (nalu.Slice)
{
//H264 NALU slice first_mb_in_slice
frames.Add(nalu);
}
else
{
if (nalus.Count > 0)
{
var otherBuffer = FM4Encoder.EncoderOtherVideoBox(frames);
foreach (var session in otherHttpSessions)
{
if (otherBuffer != null)
{
HttpSessionManager.SendAVData(session, otherBuffer, false);
}
}
frames.Clear();
}
frames.Add(nalu);
}
var fmp4VideoBuffer = FM4Encoder.EncoderVideo(data, session.FMp4EncoderInfo, false);
HttpSessionManager.SendAVData(session, fmp4VideoBuffer, false);
}
}
catch (Exception ex)
@@ -134,6 +107,7 @@ 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()}");
}
}
await Task.CompletedTask;
}


+ 2
- 1
src/JT1078.Gateway.Tests/JT1078.Gateway.TestNormalHosting/wwwroot/fmp4_demo/index.html 查看文件

@@ -168,7 +168,8 @@
// source_buffer.mode = 'segments';
source_buffer.addEventListener("updateend", loadPacket);

ws = new WebSocket("ws://127.0.0.1:15555/live.mp4?sim=12345678901&channel=3&token=123456"); //创建WebSocket连接
ws = new WebSocket("ws://127.0.0.1:15555/live.mp4?sim=1901305037&channel=1&token=123456"); //创建WebSocket连接
ws.binaryType = 'arraybuffer';
ws.onmessage = function (e) {
//当客户端收到服务端发来的消息时,触发onmessage事件,参数e.data包含server传递过来的数据
console.log(e.data);


+ 12
- 0
src/JT1078.Gateway.sln 查看文件

@@ -17,6 +17,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT1078.Gateway", "JT1078.Ga
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT1078.Gateway.InMemoryMQ", "JT1078.Gateway.InMemoryMQ\JT1078.Gateway.InMemoryMQ.csproj", "{011934D6-BEE7-4CDA-9F67-4D6D7D672D6C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT1078.FMp4", "..\..\JT1078\src\JT1078.FMp4\JT1078.FMp4.csproj", "{FDE3681A-46D2-451A-91A0-E5F63E80737F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT1078.Protocol", "..\..\JT1078\src\JT1078.Protocol\JT1078.Protocol.csproj", "{D8714E62-5ECB-4886-8A97-11F215F8D60F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -47,6 +51,14 @@ Global
{011934D6-BEE7-4CDA-9F67-4D6D7D672D6C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{011934D6-BEE7-4CDA-9F67-4D6D7D672D6C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{011934D6-BEE7-4CDA-9F67-4D6D7D672D6C}.Release|Any CPU.Build.0 = Release|Any CPU
{FDE3681A-46D2-451A-91A0-E5F63E80737F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FDE3681A-46D2-451A-91A0-E5F63E80737F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FDE3681A-46D2-451A-91A0-E5F63E80737F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FDE3681A-46D2-451A-91A0-E5F63E80737F}.Release|Any CPU.Build.0 = Release|Any CPU
{D8714E62-5ECB-4886-8A97-11F215F8D60F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D8714E62-5ECB-4886-8A97-11F215F8D60F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D8714E62-5ECB-4886-8A97-11F215F8D60F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D8714E62-5ECB-4886-8A97-11F215F8D60F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE


+ 1
- 1
src/JT1078.Gateway/JT1078.Gateway.csproj 查看文件

@@ -39,9 +39,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="JT1078.FMp4" Version="1.0.0-preview2" />
<PackageReference Include="JT1078.Hls" Version="1.1.0-preview3" />
<PackageReference Include="JT1078.Flv" Version="1.1.0" />
<PackageReference Include="JT1078.FMp4" Version="1.0.0-preview1" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="5.0.0" />
<PackageReference Include="System.IO.Pipelines" Version="5.0.1" />
</ItemGroup>


+ 5
- 0
src/JT1078.Gateway/JT1078.Gateway.xml 查看文件

@@ -195,6 +195,11 @@
会话Id
</summary>
</member>
<member name="P:JT1078.Gateway.Metadata.JT1078HttpContext.FMp4EncoderInfo">
<summary>
FMp4编码信息
</summary>
</member>
<member name="P:JT1078.Gateway.Metadata.JT1078HttpContext.Context">
<summary>
http上下文


+ 6
- 1
src/JT1078.Gateway/Metadata/JT1078HttpContext.cs 查看文件

@@ -1,4 +1,5 @@
using System;
using JT1078.FMp4;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.WebSockets;
@@ -18,6 +19,10 @@ namespace JT1078.Gateway.Metadata
/// </summary>
public string SessionId { get; }
/// <summary>
/// FMp4编码信息
/// </summary>
public FMp4EncoderInfo FMp4EncoderInfo { get; set; } = new FMp4EncoderInfo();
/// <summary>
/// http上下文
/// </summary>
[JsonIgnore]


Loading…
取消
儲存