private byte[] CreateTsData(JT1078Package jt1078Package, bool isNeedHeader, Span <byte> span) { var buff = TSArrayPool.Rent(jt1078Package.Bodies.Length + 2048); TSMessagePackWriter tSMessagePackWriter = new TSMessagePackWriter(buff); if (TsFirst1078PackageDic.TryGetValue(jt1078Package.SIM, out var firstTimespan)) { if ((jt1078Package.Timestamp - firstTimespan) > 10 * 1000) { //按设定的时间(默认为10秒)切分ts文件 } var pes = tSEncoder.CreatePES(jt1078Package, 188); tSMessagePackWriter.WriteArray(pes); } else { var sdt = tSEncoder.CreateSDT(jt1078Package); tSMessagePackWriter.WriteArray(sdt); var pat = tSEncoder.CreatePAT(jt1078Package); tSMessagePackWriter.WriteArray(pat); var pmt = tSEncoder.CreatePMT(jt1078Package); tSMessagePackWriter.WriteArray(pmt); var pes = tSEncoder.CreatePES(jt1078Package, 188); tSMessagePackWriter.WriteArray(pes); } return(buff); }
/// <summary> /// 生成ts和m3u8文件 /// </summary> /// <param name="jt1078Package"></param> public void CreateTsData(JT1078Package jt1078Package) { string key = jt1078Package.GetKey(); string hlsFileDirectory = m3U8Option.HlsFileDirectory; string m3u8FileName = Path.Combine(hlsFileDirectory, key, m3U8Option.M3U8FileName); if (!File.Exists(m3u8FileName)) { File.Create(m3u8FileName); //创建m3u8文件 } var buff = TSArrayPool.Rent(jt1078Package.Bodies.Length + 1024); TSMessagePackWriter tSMessagePackWriter = new TSMessagePackWriter(buff); try { var curTsFileInfo = CreateTsFileInfo(key); if (!curTsFileInfo.IsCreateTsFile) { var pes = tSEncoder.CreatePES(jt1078Package); tSMessagePackWriter.WriteArray(pes); CreateTsFile(curTsFileInfo.FileName, key, tSMessagePackWriter.FlushAndGetArray()); curTsFileInfo.Duration = (jt1078Package.Timestamp - curTsFileInfo.TsFirst1078PackageTimeStamp) / 1000.0; //按设定的时间(默认为10秒)切分ts文件 if (curTsFileInfo.Duration > m3U8Option.TsFileMaxSecond) { var tsFileInfoQueue = ManageTsFileInfo(key, curTsFileInfo); CreateM3U8File(curTsFileInfo, tsFileInfoQueue); var newTsFileInfo = new TsFileInfo { IsCreateTsFile = true, Duration = 0, TsFileSerialNo = ++curTsFileInfo.TsFileSerialNo }; curTsFileInfoDic.TryUpdate(key, newTsFileInfo, curTsFileInfo); } } else { curTsFileInfo.IsCreateTsFile = false; curTsFileInfo.TsFirst1078PackageTimeStamp = jt1078Package.Timestamp; curTsFileInfo.FileName = $"{curTsFileInfo.TsFileSerialNo}.ts"; var sdt = tSEncoder.CreateSDT(jt1078Package); tSMessagePackWriter.WriteArray(sdt); var pat = tSEncoder.CreatePAT(jt1078Package); tSMessagePackWriter.WriteArray(pat); var pmt = tSEncoder.CreatePMT(jt1078Package); tSMessagePackWriter.WriteArray(pmt); var pes = tSEncoder.CreatePES(jt1078Package); tSMessagePackWriter.WriteArray(pes); CreateTsFile(curTsFileInfo.FileName, key, tSMessagePackWriter.FlushAndGetArray()); } } finally { TSArrayPool.Return(buff); } }
public void ToBuffer(ref TSMessagePackWriter writer) { writer.WriteArray(NALU0x09); if (NALUs != null) { foreach (var nalu in NALUs) { writer.WriteArray(nalu); } } }
public void ToBuffer(ref TSMessagePackWriter writer) { writer.WriteArray(PESStartCode); writer.WriteByte(StreamId); writer.WriteUInt16(PESPacketLength); writer.WriteByte(Flag1); writer.WriteByte((byte)PTS_DTS_Flag); if (PTS_DTS_Flag == PTS_DTS_Flags.all) { writer.WriteByte(10); writer.WriteInt5(ToPTS()); writer.WriteInt5(ToDTS()); } else if (PTS_DTS_Flag == PTS_DTS_Flags.pts) { writer.WriteByte(5); writer.WriteInt5(ToPTS()); } else if (PTS_DTS_Flag == PTS_DTS_Flags.dts) { writer.WriteByte(5); writer.WriteInt5(ToDTS()); } Payload.ToBuffer(ref writer); }
public void ToBuffer(ref TSMessagePackWriter writer) { Header.ToBuffer(ref writer); writer.WriteByte(TableId); //SectionSyntaxIndicator Zero Reserved1 SectionLength //1 0 11 0000 0000 0000 //(ushort)(0b_1011_0000_0000_0000 | SectionLength) writer.Skip(2, out int SectionLengthPosition); writer.WriteUInt16(TransportStreamId); //Reserved2 VersionNumber CurrentNextIndicator //11 00000 1 var a = 0xC0 & (Reserved2 << 6); var b = 0x3E & (VersionNumber << 3); var c = (byte)(a | b | CurrentNextIndicator); writer.WriteByte(c); writer.WriteByte(SectionNumber); writer.WriteByte(LastSectionNumber); if (Programs != null) { foreach (var program in Programs) { program.ToBuffer(ref writer); } } const int crcLength = 4; writer.WriteUInt16Return((ushort)(0b_1011_0000_0000_0000 | (ushort)(writer.GetCurrentPosition() - SectionLengthPosition - 2) + crcLength), SectionLengthPosition); //打包ts流时PAT和PMT表是没有adaptation field的,不够的长度直接补0xff即可。 //ts header(4B) + adaptation field length(1) writer.WriteCRC32(5); var size = TSConstants.FiexdPackageLength - writer.GetCurrentPosition(); writer.WriteArray(Enumerable.Range(0, size).Select(s => (byte)0xFF).ToArray()); }
public void ToBuffer(ref TSMessagePackWriter writer) { Header.PackageType = PackageType.SDT; Header.ToBuffer(ref writer); writer.WriteByte(TableId); writer.Skip(2, out int SectionLengthPosition); writer.WriteUInt16(TransportStreamId); writer.WriteByte((byte)(Reserved2 << 6 | VersionNumber << 1 | CurrentNextIndicator)); writer.WriteByte(SectionNumber); writer.WriteByte(LastSectionNumber); writer.WriteUInt16(OriginalNetworkId); writer.WriteByte(ReservedFutureUse2); foreach (var service in Services) { service.ToBuffer(ref writer); } ushort servicesLength = (ushort)(writer.GetCurrentPosition() - SectionLengthPosition - 2); const int crcLength = 4; writer.WriteUInt16Return((ushort)(SectionSyntaxIndicator << 15 | ReservedFutureUse1 << 14 | Reserved1 << 12 | servicesLength + crcLength), SectionLengthPosition); writer.WriteCRC32(5); var size = TSConstants.FiexdPackageLength - writer.GetCurrentPosition(); writer.WriteArray(Enumerable.Range(0, size).Select(s => (byte)0xFF).ToArray()); }
public void ToBuffer(ref TSMessagePackWriter writer) { writer.WriteByte((byte)PCRIncluded); if (PCRIncluded == PCRInclude.包含) { writer.WriteInt6(ToPCR()); } if (FillSize > 0) { writer.WriteArray(Enumerable.Range(0, FillSize).Select(s => (byte)0xFF).ToArray()); } }
public void ToBuffer(ref TSMessagePackWriter writer) { Header.ToBuffer(ref writer); writer.WriteArray(Payload); }