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();
            }
        }
Exemple #2
0
        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>(() => { });
            }
        }