public void 测试第一帧的数据() { JT1078Package Package = null; var lines = File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "JT1078_1.txt")); int mergeBodyLength = 0; foreach (var line in lines) { var data = line.Split(','); var bytes = data[6].ToHexBytes(); JT1078Package package = JT1078Serializer.Deserialize(bytes); mergeBodyLength += package.DataBodyLength; Package = JT1078Serializer.Merge(package); } H264Decoder decoder = new H264Decoder(); //7 8 6 5 1 1 1 1 1 7 8 6 5 1 1 1 1 var nalus = decoder.ParseNALU(Package); Assert.Equal(4, nalus.Count); FlvEncoder encoder = new FlvEncoder(); var contents = encoder.CreateFlvFrame(nalus); var filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "JT1078_1.flv"); if (File.Exists(filepath)) { File.Delete(filepath); } FileStream fileStream = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write); fileStream.Write(FlvEncoder.VideoFlvHeaderBuffer); fileStream.Write(contents); fileStream.Close(); }
public void Test4() { try { var hls_file_directory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "terminalno"); if (!File.Exists(hls_file_directory)) { Directory.CreateDirectory(hls_file_directory); } var m3u8_filename = Path.Combine(hls_file_directory, "live.m3u8"); TSEncoder tSEncoder = new TSEncoder(); var m3u8Manage = new M3U8FileManage(new Options.M3U8Option { HlsFileDirectory = hls_file_directory, M3U8FileName = m3u8_filename }, tSEncoder); var lines = File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "JT1078_3.txt")); foreach (var line in lines) { var data = line.Split(','); var bytes = data[6].ToHexBytes(); JT1078Package package = JT1078Serializer.Deserialize(bytes); JT1078Package fullpackage = JT1078Serializer.Merge(package); if (fullpackage != null) { m3u8Manage.CreateTsData(fullpackage); } } } catch (Exception ex) { //Assert.Throws<Exception>(() => { }); } }
public void 测试可以播放的Flv3() { FileStream fileStream = null; try { var filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "JT1078_3.flv"); var lines = File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "JT1078_3.txt")); fileStream = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write); bool isNeedFirstHeadler = true; FlvEncoder encoder = new FlvEncoder(); foreach (var line in lines) { var data = line.Split(','); var bytes = data[6].ToHexBytes(); JT1078Package package = JT1078Serializer.Deserialize(bytes); JT1078Package fullpackage = JT1078Serializer.Merge(package); if (fullpackage != null) { var videoTag = encoder.EncoderVideoTag(fullpackage, isNeedFirstHeadler); fileStream.Write(videoTag); isNeedFirstHeadler = false; } } } catch (Exception ex) { Assert.Throws <Exception>(() => { }); } finally { fileStream?.Close(); fileStream?.Dispose(); } }
public void FlvEncoder_Test_1() { JT1078Package Package = null; var lines = File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "JT1078_1.txt")); int mergeBodyLength = 0; foreach (var line in lines) { var data = line.Split(','); var bytes = data[6].ToHexBytes(); JT1078Package package = JT1078Serializer.Deserialize(bytes); mergeBodyLength += package.DataBodyLength; Package = JT1078Serializer.Merge(package); } Flv.H264.H264Decoder decoder = new Flv.H264.H264Decoder(); var nalus = decoder.ParseNALU(Package); Assert.Equal(4, nalus.Count); FlvEncoder encoder = new FlvEncoder(); var contents = encoder.CreateFlvFrame(nalus); var filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "JT1078_1.flv"); if (File.Exists(filepath)) { File.Delete(filepath); } File.WriteAllBytes(filepath, contents); }
public void Test() { var lines = File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "JT1078_1.txt")); JT1078Package merge = null; int mergeBodyLength = 0; foreach (var line in lines) { var data = line.Split(','); var bytes = data[6].ToHexBytes(); JT1078Package package = JT1078Serializer.Deserialize(bytes); mergeBodyLength += package.DataBodyLength; merge = JT1078Serializer.Merge(package); } var packages = merge.Bodies.ConvertVideo(merge.SIM, merge.LogicChannelNumber, merge.Label2.PT, merge.Label3.DataType, merge.Timestamp, merge.LastFrameInterval, merge.LastFrameInterval); for (int i = 0; i < packages.Count; i++) { var data = lines[i].Split(','); var bytes1 = data[6].ToHexBytes(); var bytes2 = JT1078Serializer.Serialize(packages[i]); Assert.Equal(bytes1, bytes2); } }
public void FlvEncoder_Test_4() { FileStream fileStream = null; Flv.H264.H264Decoder decoder = new Flv.H264.H264Decoder(); List <H264NALU> h264NALULs = new List <H264NALU>(); FlvEncoder encoder = new FlvEncoder(); try { var filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "JT1078_4.flv"); var lines = File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "JT1078_4.txt")); if (File.Exists(filepath)) { File.Delete(filepath); } JT1078Package Package = null; foreach (var line in lines) { var data = line.Split(','); var bytes = data[6].ToHexBytes(); JT1078Package package = JT1078Serializer.Deserialize(bytes); Package = JT1078Serializer.Merge(package); if (Package != null) { var tmp = decoder.ParseNALU(Package); if (tmp != null && tmp.Count > 0) { h264NALULs = h264NALULs.Concat(tmp).ToList(); } } } fileStream = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write); fileStream.Write(FlvEncoder.VideoFlvHeaderBuffer); var totalPage = (h264NALULs.Count + 10 - 1) / 10; for (var i = 0; i < totalPage; i++) { var flv2 = encoder.CreateFlvFrame(h264NALULs.Skip(i * 10).Take(10).ToList()); if (flv2.Length != 0) { fileStream.Write(flv2); } } } catch (Exception ex) { Assert.Throws <Exception>(() => { }); } finally { fileStream?.Close(); fileStream?.Dispose(); } }
public void a() { List <JT1078Package> packages = new List <JT1078Package>(); var lines = File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "jt1078_3.txt")); int mergeBodyLength = 0; foreach (var line in lines) { var data = line.Split(','); var bytes = data[6].ToHexBytes(); JT1078Package package = JT1078Serializer.Deserialize(bytes); mergeBodyLength += package.DataBodyLength; var packageMerge = JT1078Serializer.Merge(package); if (packageMerge != null) { packages.Add(packageMerge); } } List <byte[]> first = new List <byte[]>(); //var styp = fMp4Encoder.EncoderStypBox(); //first.Add(styp); //q.Enqueue(styp); var ftyp = fMp4Encoder.EncoderFtypBox(); //q.Enqueue(ftyp); first.Add(ftyp); var package1 = packages[0]; var nalus1 = h264Decoder.ParseNALU(package1); var moov = fMp4Encoder.EncoderMoovBox(nalus1, package1.Bodies.Length); //q.Enqueue(moov); first.Add(moov); q.Add(first.SelectMany(s => s).ToArray()); List <int> filter = new List <int>() { 6, 7, 8 }; foreach (var package in packages) { List <byte[]> other = new List <byte[]>(); var otherNalus = h264Decoder.ParseNALU(package); var filterNalus = otherNalus.Where(w => !filter.Contains(w.NALUHeader.NalUnitType)).ToList(); var flag = package.Label3.DataType == Protocol.Enums.JT1078DataType.视频I帧 ? 1u : 0u; var len = filterNalus.Sum(s => s.RawData.Length); var len1 = otherNalus.Sum(s => s.RawData.Length); var moofBuffer = fMp4Encoder.EncoderMoofBox(filterNalus, len, package.Timestamp, package.LastIFrameInterval, flag); //q.Enqueue(moofBuffer); other.Add(moofBuffer); var otherMdatBuffer = fMp4Encoder.EncoderMdatBox(filterNalus, len); //q.Enqueue(otherMdatBuffer); other.Add(otherMdatBuffer); q.Add(other.SelectMany(s => s).ToArray()); } }
public void ToBufferTest3() { FileStream fileStream = null; try { var filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "JT1078_3.ts"); File.Delete(filepath); var lines = File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "JT1078_3.txt")); fileStream = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write); bool isNeedFirstHeadler = true; TSEncoder tSEncoder = new TSEncoder(); foreach (var line in lines) { var data = line.Split(','); var bytes = data[6].ToHexBytes(); JT1078Package package = JT1078Serializer.Deserialize(bytes); JT1078Package fullpackage = JT1078Serializer.Merge(package); if (fullpackage != null) { if (isNeedFirstHeadler) { var sdt = tSEncoder.CreateSDT(); string sdtHEX = sdt.ToHexString(); fileStream.Write(sdt); var pat = tSEncoder.CreatePAT(); string patHEX = pat.ToHexString(); fileStream.Write(pat); var pmt = tSEncoder.CreatePMT(); fileStream.Write(pmt); var pes = tSEncoder.CreatePES(fullpackage, 18888); fileStream.Write(pes); isNeedFirstHeadler = false; } else { fileStream.Write(tSEncoder.CreatePES(fullpackage, 18888)); } } } } catch (Exception ex) { Assert.Throws <Exception>(() => { }); } finally { fileStream?.Close(); fileStream?.Dispose(); } }
public void ParseNALUTest2() { JT1078Package Package = null; var lines = File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "jt1078_1.txt")); int mergeBodyLength = 0; foreach (var line in lines) { var data = line.Split(','); var bytes = data[6].ToHexBytes(); JT1078Package package = JT1078Serializer.Deserialize(bytes); mergeBodyLength += package.DataBodyLength; Package = JT1078Serializer.Merge(package); } H264Decoder decoder = new H264Decoder(); var nalus = decoder.ParseNALU(Package); Assert.Equal(4, nalus.Count); //SPS -> 7 var spsNALU = nalus.FirstOrDefault(n => n.NALUHeader.NalUnitType == 7); Assert.NotNull(spsNALU); spsNALU.RawData = decoder.DiscardEmulationPreventionBytes(spsNALU.RawData); //"Z00AFJWoWCWQ" var spsRawDataHex = JsonConvert.SerializeObject(spsNALU.RawData); ExpGolombReader h264GolombReader = new ExpGolombReader(spsNALU.RawData); //(77, 20, 0, 352, 288) var spsInfo = h264GolombReader.ReadSPS(); Assert.Equal(77, spsInfo.profileIdc); Assert.Equal(20, spsInfo.levelIdc); Assert.Equal(0u, spsInfo.profileCompat); Assert.Equal(288, spsInfo.height); Assert.Equal(352, spsInfo.width); //PPS -> 8 var ppsNALU = nalus.FirstOrDefault(n => n.NALUHeader.NalUnitType == 8); Assert.NotNull(ppsNALU); //IDR -> 5 关键帧 var idrNALU = nalus.FirstOrDefault(n => n.NALUHeader.NalUnitType == 5); Assert.NotNull(idrNALU); //SEI -> 6 var seiNALU = nalus.FirstOrDefault(n => n.NALUHeader.NalUnitType == 6); Assert.NotNull(seiNALU); }
public void Setup() { var lines = File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "JT1078_1.txt")); foreach (var line in lines) { var data = line.Split(','); var bytes = data[6].ToHexBytes(); JT1078Package package = JT1078Serializer.Deserialize(bytes); Package = JT1078Serializer.Merge(package); } H264NALUs = h264Decoder.ParseNALU(Package); SPSNALu = H264NALUs.FirstOrDefault(f => f.NALUHeader.NalUnitType == 7); SPSNALu.RawData = h264Decoder.DiscardEmulationPreventionBytes(SPSNALu.RawData); }
public JT1078Package ParseNALUTest() { JT1078Package Package = null; var lines = File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "jt1078_1.txt")); int mergeBodyLength = 0; foreach (var line in lines) { var data = line.Split(','); var bytes = data[6].ToHexBytes(); JT1078Package package = JT1078Serializer.Deserialize(bytes); mergeBodyLength += package.DataBodyLength; Package = JT1078Serializer.Merge(package); } return(Package); }
public void EncoderVideoTagTest2() { try { var bytes = "3031636481E2022D0019013050370210000001749179C79D05C80050000E0000000161E1E2FF000003006840".ToHexBytes(); FlvEncoder encoder = new FlvEncoder(); JT1078Package package = JT1078Serializer.Deserialize(bytes); JT1078Package fullpackage = JT1078Serializer.Merge(package); if (fullpackage != null) { var videoTag = encoder.EncoderVideoTag(fullpackage, false); } } catch (Exception ex) { } }
public void EncoderVideoTagTest1() { try { var bytes = "3031636481E206AD0019013050370210000001749162905508480050000E0000000161E4025F0000030037A03031636481E206AE001901305037021000000174916290A508980050000E0000000161E4225F0000030037A0".ToHexBytes(); FlvEncoder encoder = new FlvEncoder(); JT1078Package package = JT1078Serializer.Deserialize(bytes); JT1078Package fullpackage = JT1078Serializer.Merge(package); if (fullpackage != null) { var videoTag = encoder.EncoderVideoTag(fullpackage, false); } } catch (Exception ex) { } }
public void MergeTest() { var lines = File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "JT1078.txt")); JT1078Package merge = null; int mergeBodyLength = 0; foreach (var line in lines) { var data = line.Split(','); var bytes = data[5].ToHexBytes(); JT1078Package package = JT1078Serializer.Deserialize(bytes); mergeBodyLength += package.DataBodyLength; merge = JT1078Serializer.Merge(package); } Assert.NotNull(merge); Assert.Equal(mergeBodyLength, merge.Bodies.Length); Assert.Equal(JT1078SubPackageType.分包处理时的第一个包, merge.Label3.SubpackageType); }
public Task <JT1078Response> Processor(JT1078Request request) { //var path=Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "h264", $"demo.h264"); //using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write)) //{ // fs.Write(request.Src); // fs.Close(); //} logger.LogInformation(JsonConvert.SerializeObject(request.Package)); //hexLogger.LogInformation($"{request.Package.SIM},{request.Package.Label3.DataType.ToString()},{request.Package.LastFrameInterval},{request.Package.LastIFrameInterval},{request.Package.Timestamp},{request.Package.SN},{request.Package.LogicChannelNumber},{request.Package.Label3.SubpackageType.ToString()},{ByteBufferUtil.HexDump(request.Src)}"); hexLogger.LogInformation($"{request.Package.SIM},{request.Package.SN},{request.Package.LogicChannelNumber},{request.Package.Label3.DataType.ToString()},{request.Package.Label3.SubpackageType.ToString()},{ByteBufferUtil.HexDump(request.Src)}"); var mergePackage = JT1078Serializer.Merge(request.Package); if (mergePackage != null) { jT1078WSFlvDataService.JT1078Packages.Add(mergePackage); } return(Task.FromResult <JT1078Response>(default));
public List <JT1078Package> ParseNALUTests() { List <JT1078Package> packages = new List <JT1078Package>(); var lines = File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "jt1078_3.txt")); int mergeBodyLength = 0; foreach (var line in lines) { var data = line.Split(','); var bytes = data[6].ToHexBytes(); JT1078Package package = JT1078Serializer.Deserialize(bytes); mergeBodyLength += package.DataBodyLength; var packageMerge = JT1078Serializer.Merge(package); if (packageMerge != null) { packages.Add(packageMerge); } } return(packages); }
protected override Task ExecuteAsync(CancellationToken stoppingToken) { JT1078MsgConsumer.OnMessage((Message) => { JT1078Package package = JT1078Serializer.Deserialize(Message.Data); if (Logger.IsEnabled(LogLevel.Debug)) { Logger.LogDebug(JsonSerializer.Serialize(HttpSessionManager.GetAll())); Logger.LogDebug($"{package.SIM},{package.SN},{package.LogicChannelNumber},{package.Label3.DataType.ToString()},{package.Label3.SubpackageType.ToString()},{package.Bodies.ToHexString()}"); } try { var merge = JT1078Serializer.Merge(package); if (merge == null) { return; } string key = $"{package.GetKey()}_{ikey}"; if (merge.Label3.DataType == Protocol.Enums.JT1078DataType.视频I帧) { memoryCache.Set(key, merge); } var httpSessions = HttpSessionManager.GetAllBySimAndChannelNo(package.SIM.TrimStart('0'), package.LogicChannelNumber); var firstHttpSessions = httpSessions.Where(w => !w.FirstSend).ToList(); if (firstHttpSessions.Count > 0) { if (memoryCache.TryGetValue(key, out JT1078Package idata)) { try { var flvVideoBuffer = FlvEncoder.EncoderVideoTag(idata, true); foreach (var session in firstHttpSessions) { HttpSessionManager.SendAVData(session, flvVideoBuffer, true); } } catch (Exception ex) { Logger.LogError(ex, $"{package.SIM},{true},{package.SN},{package.LogicChannelNumber},{package.Label3.DataType.ToString()},{package.Label3.SubpackageType.ToString()},{package.Bodies.ToHexString()}"); } } } var otherHttpSessions = httpSessions.Where(w => w.FirstSend).ToList(); if (otherHttpSessions.Count > 0) { try { var flvVideoBuffer = FlvEncoder.EncoderVideoTag(merge, false); foreach (var session in otherHttpSessions) { HttpSessionManager.SendAVData(session, flvVideoBuffer, false); } } catch (Exception ex) { Logger.LogError(ex, $"{package.SIM},{false},{package.SN},{package.LogicChannelNumber},{package.Label3.DataType.ToString()},{package.Label3.SubpackageType.ToString()},{package.Bodies.ToHexString()}"); } } } catch (Exception ex) { Logger.LogError(ex, $"{package.SIM},{package.SN},{package.LogicChannelNumber},{package.Label3.DataType.ToString()},{package.Label3.SubpackageType.ToString()},{package.Bodies.ToHexString()}"); } }); return(Task.CompletedTask); }
public void a() { List <JT1078Package> packages = new List <JT1078Package>(); var lines = File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "jt1078_3.txt")); int mergeBodyLength = 0; foreach (var line in lines) { var data = line.Split(','); var bytes = data[6].ToHexBytes(); JT1078Package package = JT1078Serializer.Deserialize(bytes); mergeBodyLength += package.DataBodyLength; var packageMerge = JT1078Serializer.Merge(package); if (packageMerge != null) { packages.Add(packageMerge); } } List <byte[]> first = new List <byte[]>(); //var styp = fMp4Encoder.EncoderStypBox(); //first.Add(styp); //q.Enqueue(styp); var ftyp = fMp4Encoder.EncoderFtypBox(); //q.Enqueue(ftyp); first.Add(ftyp); var package1 = packages[0]; var nalus1 = h264Decoder.ParseNALU(package1); var moov = fMp4Encoder.EncoderMoovBox( nalus1.FirstOrDefault(f => f.NALUHeader.NalUnitType == NalUnitType.SPS), nalus1.FirstOrDefault(f => f.NALUHeader.NalUnitType == NalUnitType.PPS)); //q.Enqueue(moov); first.Add(moov); q.Add(first.SelectMany(s => s).ToArray()); List <NalUnitType> filter = new List <NalUnitType>() { NalUnitType.SEI, NalUnitType.SPS, NalUnitType.PPS, NalUnitType.AUD }; List <H264NALU> nalus = new List <H264NALU>(); foreach (var package in packages) { List <H264NALU> h264NALUs = h264Decoder.ParseNALU(package); foreach (var nalu in h264NALUs) { if (nalu.Slice) { //H264 NALU slice first_mb_in_slice nalus.Add(nalu); } else { if (nalus.Count > 0) { var otherBuffer = fMp4Encoder.EncoderOtherVideoBox(nalus); q.Add(otherBuffer); nalus.Clear(); } nalus.Add(nalu); } } } }
public void Test4() { try { int file_count = 10; int file_max_second = 10; int first_serialno = 0; double file_real_second = 10; var lines = File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "JT1078_3.txt")); bool isNeedFirstHeadler = true; TSEncoder tSEncoder = new TSEncoder(); ulong init_seconds = 0; int duration = 0; int temp_seconds = 0; var m3u8Filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "JT1078_HLS.m3u8"); AppendM3U8Start(m3u8Filepath, file_max_second, first_serialno); byte[] fileData = new byte[1888888888]; int fileIndex = 0; foreach (var line in lines) { var data = line.Split(','); var bytes = data[6].ToHexBytes(); JT1078Package package = JT1078Serializer.Deserialize(bytes); JT1078Package fullpackage = JT1078Serializer.Merge(package); if (fullpackage != null) { if (temp_seconds / 1000 >= file_max_second) { file_real_second = temp_seconds / 1000.0;//秒 //生成一个文件 var file_name = $"JT1078_{first_serialno}.ts"; var ts_filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", file_name); CreateTsFile(ts_filepath, fileData); //更新m3u8文件 AppendTSFile(m3u8Filepath, file_real_second, file_name); //删除最早一个文件 var del_file_name = $"JT1078_{first_serialno- file_count}.ts"; var del_ts_filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", del_file_name); DeleteTsFile(del_ts_filepath); fileData = new byte[1888888888]; temp_seconds = 0; first_serialno = first_serialno + 1; } if (init_seconds == 0) { init_seconds = fullpackage.Timestamp; } else { duration = (int)(fullpackage.Timestamp - init_seconds); init_seconds = fullpackage.Timestamp; temp_seconds = Convert.ToInt32(temp_seconds) + duration; } if (isNeedFirstHeadler) { var sdt = tSEncoder.CreateSDT(fullpackage); string sdtHEX = sdt.ToHexString(); sdt.CopyTo(fileData, fileIndex); fileIndex = sdt.Length; var pat = tSEncoder.CreatePAT(fullpackage); string patHEX = pat.ToHexString(); pat.CopyTo(fileData, fileIndex); fileIndex = fileIndex + pat.Length; var pmt = tSEncoder.CreatePMT(fullpackage); pmt.CopyTo(fileData, fileIndex); fileIndex = fileIndex + pmt.Length; var pes = tSEncoder.CreatePES(fullpackage, 18888); pes.CopyTo(fileData, fileIndex); fileIndex = fileIndex + pes.Length; isNeedFirstHeadler = false; } else { var pes = tSEncoder.CreatePES(fullpackage, 18888); pes.CopyTo(fileData, fileIndex); fileIndex = fileIndex + pes.Length; } } } AppendM3U8End(m3u8Filepath); } catch (Exception ex) { Assert.Throws <Exception>(() => { }); } }
public void 测试主次码流切换() { FileStream fileStream = null; H264Decoder decoder = new H264Decoder(); List <H264NALU> h264NALULs = new List <H264NALU>(); FlvEncoder encoder = new FlvEncoder(); try { var filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "JT1078_5.flv"); var lines = File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "JT1078_5.txt")); if (File.Exists(filepath)) { File.Delete(filepath); } JT1078Package Package = null; foreach (var line in lines) { var data = line.Split(','); var bytes = data[6].ToHexBytes(); JT1078Package package = JT1078Serializer.Deserialize(bytes); Package = JT1078Serializer.Merge(package); if (Package != null) { var tmp = decoder.ParseNALU(Package); if (tmp != null && tmp.Count > 0) { h264NALULs = h264NALULs.Concat(tmp).ToList(); } } } var tmp1 = h264NALULs.Where(w => w.NALUHeader.NalUnitType == 7).ToList(); List <SPSInfo> tmpSpss = new List <SPSInfo>(); List <ulong> times = new List <ulong>(); List <ushort> lastIFrameIntervals = new List <ushort>(); List <ushort> lastFrameIntervals = new List <ushort>(); List <int> type = new List <int>(); foreach (var item in h264NALULs) { //type.Add(item.NALUHeader.NalUnitType); times.Add(item.Timestamp); lastFrameIntervals.Add(item.LastFrameInterval); lastIFrameIntervals.Add(item.LastIFrameInterval); if (item.NALUHeader.NalUnitType == 7) { ExpGolombReader expGolombReader = new ExpGolombReader(item.RawData); tmpSpss.Add(expGolombReader.ReadSPS()); } } fileStream = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write); var totalPage = (h264NALULs.Count + 10 - 1) / 10; for (var i = 0; i < totalPage; i++) { var flv2 = encoder.CreateFlvFrame(h264NALULs.Skip(i * 10).Take(10).ToList()); if (flv2.Length != 0) { //fileStream.Write(flv2); } } } catch (Exception ex) { Assert.Throws <Exception>(() => { }); } finally { fileStream?.Close(); fileStream?.Dispose(); } }
public void Setup() { var lines = File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "JT1078_1.txt")); foreach (var line in lines) { var data = line.Split(','); var bytes = data[6].ToHexBytes(); JT1078Package package = JT1078Serializer.Deserialize(bytes); Package = JT1078Serializer.Merge(package); } H264NALUs = h264Decoder.ParseNALU(Package); SPSNALu = H264NALUs.FirstOrDefault(f => f.NALUHeader.NalUnitType == NalUnitType.SPS); SPSNALu.RawData = h264Decoder.DiscardEmulationPreventionBytes(SPSNALu.RawData); List <JT1078Package> packages = new List <JT1078Package>(); var lines3 = File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "jt1078_3.txt")); int mergeBodyLength = 0; foreach (var line in lines3) { var data = line.Split(','); var bytes = data[6].ToHexBytes(); JT1078Package package = JT1078Serializer.Deserialize(bytes); mergeBodyLength += package.DataBodyLength; var packageMerge = JT1078Serializer.Merge(package); if (packageMerge != null) { packages.Add(packageMerge); } } List <H264NALU> nalus = new List <H264NALU>(); bool segmentFlag = false; foreach (var package in packages) { if (segmentFlag) { break; } List <H264NALU> h264NALUs = h264Decoder.ParseNALU(package); foreach (var nalu in h264NALUs) { if (nalu.Slice) { //H264 NALU slice first_mb_in_slice nalus.Add(nalu); } else { if (nalus.Count > 0) { FMp4H264NALUs = new List <H264NALU>(nalus); segmentFlag = true; nalus.Clear(); } nalus.Add(nalu); } } } }