コード例 #1
0
        public void Test4()
        {
            FMp4Encoder fMp4Encoder = new FMp4Encoder();
            H264Decoder h264Decoder = new H264Decoder();
            var         packages    = ParseNALUTests();
            var         filepath    = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "JT1078_4.mp4");

            if (File.Exists(filepath))
            {
                File.Delete(filepath);
            }
            using var fileStream = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write);
            var ftyp = fMp4Encoder.EncoderFtypBox();

            fileStream.Write(ftyp);
            var package1 = packages[0];
            var nalus1   = h264Decoder.ParseNALU(package1);
            var moov     = fMp4Encoder.EncoderMoovBox(nalus1, package1.Bodies.Length);

            fileStream.Write(moov);
            foreach (var package in packages)
            {
                var otherNalus      = h264Decoder.ParseNALU(package);
                var flag            = package.Label3.DataType == Protocol.Enums.JT1078DataType.视频I帧 ? 1u : 0u;
                var otherMoofBuffer = fMp4Encoder.EncoderMoofBox(otherNalus, package.Bodies.Length, package.Timestamp, package.LastIFrameInterval, flag);
                var otherMdatBuffer = fMp4Encoder.EncoderMdatBox(otherNalus, package.Bodies.Length);
                fileStream.Write(otherMoofBuffer);
                fileStream.Write(otherMdatBuffer);
            }
            fileStream.Close();
        }
コード例 #2
0
ファイル: ToWebSocketService.cs プロジェクト: buzz1k/JT1078
        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());
            }
        }
コード例 #3
0
        public void Test4()
        {
            FMp4Encoder fMp4Encoder = new FMp4Encoder();
            H264Decoder h264Decoder = new H264Decoder();
            var         packages    = ParseNALUTests();
            var         filepath    = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "H264", "JT1078_5.mp4");

            if (File.Exists(filepath))
            {
                File.Delete(filepath);
            }

            using var fileStream = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write);
            var ftyp = fMp4Encoder.EncoderFtypBox();

            fileStream.Write(ftyp);

            var iNalus = h264Decoder.ParseNALU(packages[0]);
            //判断第一帧是否关键帧
            var moov = fMp4Encoder.EncoderMoovBox(
                iNalus.FirstOrDefault(f => f.NALUHeader.NalUnitType == NalUnitType.SPS),
                iNalus.FirstOrDefault(f => f.NALUHeader.NalUnitType == NalUnitType.PPS));

            fileStream.Write(moov);

            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);
                            fileStream.Write(otherBuffer);
                            nalus.Clear();
                        }
                        nalus.Add(nalu);
                    }
                }
            }
            fileStream.Close();
        }
コード例 #4
0
        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);
                    }
                }
            }
        }
コード例 #5
0
        protected async override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                var data = await messageDispatchDataService.FlvChannel.Reader.ReadAsync();

                try
                {
                    var nalus = H264Decoder.ParseNALU(data);
                    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()}");
                    }
                    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);
                    }
                    var httpSessions      = HttpSessionManager.GetAllBySimAndChannelNo(data.SIM.TrimStart('0'), data.LogicChannelNumber);
                    var firstHttpSessions = httpSessions.Where(w => !w.FirstSend).ToList();
                    if (firstHttpSessions.Count > 0)
                    {
                        try
                        {
                            var flvVideoBuffer = FM4Encoder.EncoderFtypBox();
                            memoryCache.TryGetValue(key, out byte[] moovBuffer);
                            foreach (var session in firstHttpSessions)
                            {
                                HttpSessionManager.SendAVData(session, flvVideoBuffer, true);
                                if (moovBuffer != null)
                                {
                                    HttpSessionManager.SendAVData(session, moovBuffer, false);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.LogError(ex, $"{data.SIM},{true},{data.SN},{data.LogicChannelNumber},{data.Label3.DataType.ToString()},{data.Label3.SubpackageType.ToString()},{data.Bodies.ToHexString()}");
                        }
                    }
                    var otherHttpSessions = httpSessions.Where(w => w.FirstSend).ToList();
                    if (otherHttpSessions.Count > 0)
                    {
                        try
                        {
                            if (!avFrameDict.TryGetValue(key, out List <H264NALU> frames))
                            {
                                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);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.LogError(ex, $"{data.SIM},{false},{data.SN},{data.LogicChannelNumber},{data.Label3.DataType.ToString()},{data.Label3.SubpackageType.ToString()},{data.Bodies.ToHexString()}");
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex, $"{data.SIM},{data.SN},{data.LogicChannelNumber},{data.Label3.DataType.ToString()},{data.Label3.SubpackageType.ToString()},{data.Bodies.ToHexString()}");
                }
            }
            await Task.CompletedTask;
        }