Ejemplo n.º 1
0
        public bool TryDecode()
        {
            int skip = 4;

            switch (adaptation_field_control)
            {
            case 0x0:                                        // reserved for future use by ISO/IEC
                return(false);

            case 0x1:                                        // 无调整字段,仅含有效负载
                skip = 4 + payload_unit_start_indicator;
                break;

            case 0x2:                                         // 仅含调整字段,无有效负载
                skip = SrcBuffer.Length;
                break;

            case 0x3:     // 调整字段后含有效负载
                AdaptationField = new AdaptationInfo();
                AdaptationField.SetBytes(SrcBuffer);
                skip = 4 + AdaptationField.adaptation_field_length + 1;
                break;

            default:
                break;
            }
            return(true);
        }
Ejemplo n.º 2
0
        public static byte[] MediaFrame2TSData(MediaFrame frame, TSProgramManage pm)
        {
            var  msOutput    = new MemoryStream();
            var  pes         = PESPacket.MediaFrame2PES(frame);
            var  pes_buffer  = pes.GetBytes();
            var  msInput     = new MemoryStream(pes_buffer);
            int  PID         = frame.nIsAudio == 1 ? 257 : 258;
            bool isFirstPack = true;

            do
            {
                var            payload_unit_start_indicator = 0;
                var            size         = (int)(msInput.Length - msInput.Position);
                var            max_data_len = 188 - 4;//4B为包头长度
                var            data_len     = 0;
                AdaptationInfo ai           = null;
                if ((isFirstPack || size < max_data_len))
                {
                    if (isFirstPack && (frame.nIsAudio == 0 || true))
                    {
                        max_data_len = 188 - 4 - 8;                                   //5B为包头长度 8B为adaptation长度
                        data_len     = Math.Min(max_data_len, size);
                        var adaptation_field_length = 188 - 4 - data_len - 1;         //1B为adaptation的头,这1B不算在adaptation_field_length中
                        var pcr_bas = (frame.nTimetick - pm.FirstFrameTimeTick) * 90; //这里为什么是*45我也不知道,
                        ai = new AdaptationInfo()
                        {
                            adaptation_field_length = (byte)adaptation_field_length,
                            random_access_indicator = 1,
                            PCR_flag = 1,
                            PCR_base = pcr_bas,
                        };
                        payload_unit_start_indicator = 1;
                    }
                    else
                    {
                        max_data_len = 188 - 4 - 1;//4B为包头长度 8B为adaptation长度
                        if (size < max_data_len)
                        {
                            data_len = Math.Min(max_data_len, size);
                            var adaptation_field_length = 188 - 4 - data_len - 1;
                            ai = new AdaptationInfo();
                            ai.adaptation_field_length = (byte)adaptation_field_length;
                        }
                        else
                        {
                            payload_unit_start_indicator = 0;
                            data_len = size;
                        }
                    }
                }
                else
                {
                    data_len = max_data_len;
                }
                byte[] data = new byte[data_len];
                msInput.Read(data, 0, data.Length);
                TSPacket pack_pat = new TSPacket()
                {
                    sync_byte = 0x47,
                    transport_error_indicator    = 0,
                    payload_unit_start_indicator = (byte)payload_unit_start_indicator,//一帧第一个packet或者这个packet需要fill字节时
                    transport_priority           = 0,
                    PID = (ushort)PID,
                    transport_scrambling_control = 0,
                    adaptation_field_control     = (byte)(ai != null ? 3 : 1),
                    continuity_counter           = pm.GetCounter(PID),
                    AdaptationField = ai,
                    data            = data
                };
                var buf = pack_pat.GetBytes();
                msOutput.Write(buf, 0, buf.Length);
                isFirstPack = false;
            } while (msInput.Position < msInput.Length);
            var result = msOutput.ToArray();

            return(result);
        }
Ejemplo n.º 3
0
        public void Decode()
        {
            //以下是整个程序的逻辑主要是以解析为主,实际应用可能要优化下方式以提高性能
            var buffer = SrcBuffer;
            int skip   = 4;

            switch (adaptation_field_control)
            {
            case 0x0:                                        // reserved for future use by ISO/IEC
                throw new Exception();

            case 0x1:        // 无调整字段,仅含有效负载
                skip = 4 + payload_unit_start_indicator;
                skip = 4;    //
                break;

            case 0x2:                                         // 仅含调整字段,无有效负载
                skip = SrcBuffer.Length;
                break;

            case 0x3:     // 调整字段后含有效负载
                AdaptationField = new AdaptationInfo();
                AdaptationField.SetBytes(SrcBuffer);
                skip = 4 + AdaptationField.adaptation_field_length + 1;
                break;

            default:
                break;
            }

            data = new byte[SrcBuffer.Length - skip];
            if (data.Length > 0)
            {
                Array.Copy(SrcBuffer, skip, data, 0, data.Length);
            }
            if (PID == 0x00)
            {
                PacketType = TSPacketType.PAT;
                var pat = new TS_PAT();
                pat.SetBytes(data);
                foreach (var p in pat.PATProgramList)
                {
                    ProgramManage.AddProgram(p);
                }
                _pat = pat;
            }
            else
            {
                if (ProgramManage.IsPMT_PID(PID))
                {
                    //PMT
                    PacketType = TSPacketType.PMT;
                    var pmt = new TS_PMT();
                    pmt.SetBytes(data);
                    foreach (var p in pmt.PMTStreamList)
                    {
                        ProgramManage.AddStream(p);
                    }
                    _pmt = pmt;
                }
                else if (ProgramManage.IsTSTable(PID))
                {
                    PacketType = TSPacketType.OTHER;
                    //其他表示,这里先不作处理
                }
                else if (ProgramManage.IsData(PID))
                {
                    //这里一般为 媒体数据
                    PacketType = TSPacketType.DATA;
                }
            }
        }