Example #1
0
        private void AnalyzeThead()
        {
            long scr = 0;

            while (_isWorking)
            {
                bool findStartCode = false;
                byte flag          = 0;
                lock (_lock)
                {
                    if (ms.Length - ms.ReadPosition > 4)
                    {
                        findStartCode = ms.ReadByte() == 0x00 && ms.ReadByte() == 0x00 && ms.ReadByte() == 0x01;
                        if (findStartCode)
                        {
                            flag = (byte)ms.ReadByte();
                            if (flag == 0xBA || flag == 0xBB || flag == 0xBC || flag == 0xE0 || flag == 0xC0)
                            {
                                ms.Seek(-4, SeekOrigin.Current);
                            }
                        }
                    }
                }
                if (findStartCode)
                {
                    try
                    {
                        switch (flag)
                        {
                        case 0xBA:    //PS流包头
                            var ph = new PSPacketHeader(ms);
                            scr = ph.GetSCR();
                            break;

                        case 0xBB:    //PS流系统头,有些流没有该值
                            new PSSystemHeader(ms);
                            break;

                        case 0xBC:    //PS流map
                            var map = new PSMap(ms);
                            break;

                        case 0xE0:    //PES包(视频)
                            var pesVideo = new PESPacket();
                            pesVideo.SetBytes(ms);
                            var frame = AnalyzeNewFrame(pesVideo, false);
                            if (frame != null)
                            {
                                frame.NTimetick = scr;
                            }
                            if (frame != null && frame.IsKeyFrame == 1)
                            {
                                lock (_lock)
                                    ms = ms.Tolave();
                            }
                            break;

                        case 0xC0:    //PES包(音频)
                            var pesAudio = new PESPacket();
                            pesAudio.SetBytes(ms);
                            AnalyzeNewFrame(pesAudio, true);
                            break;

                        case 0xBD:
                            byte[] lenbuf = new byte[2];
                            ms.Read(lenbuf, 0, 2);
                            var len = BitConverter.ToInt16(lenbuf, 0);
                            len    = IPAddress.HostToNetworkOrder((short)len);
                            lenbuf = new byte[len];
                            ms.Read(lenbuf, 0, len);
                            break;
                        }
                    }
                    catch (Exception)
                    {
                        //if (_isWorking && SS.Base.AppConfig._D)
                        //{
                        //    //GLib.DebugEx.WriteLog(e);
                        //    throw;
                        //}
                    }
                }
            }
        }
Example #2
0
            public MediaFrame NextMediaFrame()
            {
                var success = CheckTSPack();

                if (!success)
                {
                    return(null);
                }

                if (TSPackets.Count == 0)
                {
                    return(null);
                }

                //验证PES包头
                if (TSPackets[0].data[0] != 0 || TSPackets[0].data[1] != 0 || TSPackets[0].data[2] != 1)
                {
                    TSPackets = new List <TSPacket>();
                    return(null);
                }
                var tmp_tspackets = TSPackets;

                TSPackets = new List <TSPacket>();


                var stream = new MemoryStream();

                foreach (var item in tmp_tspackets)
                {
                    stream.Write(item.data, 0, item.data.Length);
                }
                stream.Position = 0;
                var pes = new PESPacket();

                pes.SetBytes(stream);

                if (IsVideo)
                {
                    var esdata = pes.PES_Packet_Data;
                    //新的一帧
                    if (PESPackets.Count > 0 && esdata[0] == 0 && esdata[1] == 0 && esdata[2] == 0 && esdata[3] == 1)
                    {
                        stream = new MemoryStream();
                        foreach (var item in PESPackets)
                        {
                            stream.Write(item.PES_Packet_Data, 0, item.PES_Packet_Data.Length);
                        }
                        long tick = PESPackets.FirstOrDefault().GetVideoTimetick();
                        esdata = stream.ToArray();
                        var frame = CreateVideoMediaFrame(esdata, tick);;
                        PESPackets = new List <PESPacket>();
                        PESPackets.Add(pes);
                        return(frame);
                    }
                    else
                    {
                        PESPackets.Add(pes);
                        return(null);
                    }
                }
                else if (IsAudio)
                {
                    var esdata = pes.PES_Packet_Data;
                    //新的一帧
                    if (PESPackets.Count > 0 && esdata[0] == 0xFF && (esdata[1] & 0xF0) == 0xF0)
                    {
                        stream = new MemoryStream();
                        foreach (var item in PESPackets)
                        {
                            stream.Write(item.PES_Packet_Data, 0, item.PES_Packet_Data.Length);
                        }
                        long tick = PESPackets.FirstOrDefault().GetAudioTimetick();
                        PESPackets = new List <PESPacket>();
                        PESPackets.Add(pes);
                        esdata = stream.ToArray();
                        return(CreateAudioMediaFrame(esdata, tick));
                    }
                    else
                    {
                        PESPackets.Add(pes);
                        return(null);
                    }
                }
                return(null);
            }