Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        public static byte[] GetPMTPATData(bool isOnlyVideo = false)
        {
            if (_pmt_pat_tsdata == null)
            {
                var             msOutput = new MemoryStream();
                TSProgramManage ts_pm    = new TSProgramManage();

                TS_PAT pat      = new TS_PAT();
                var    pat_data = pat.GetBytes();

                TSPacket pack_pat = new TSPacket()
                {
                    sync_byte = 0x47,
                    transport_error_indicator    = 0,
                    payload_unit_start_indicator = 1,
                    transport_priority           = 0,
                    PID = 0,
                    transport_scrambling_control = 0,
                    adaptation_field_control     = 1,
                    continuity_counter           = ts_pm.GetCounter(0),
                    data = pat_data,
                };
                var pack_pat_data = pack_pat.GetBytes();
                var pack_pat_tmp  = new TSPacket()
                {
                    ProgramManage = ts_pm,
                };
                msOutput.Write(pack_pat_data, 0, pack_pat_data.Length);

                //pack_pat_tmp.SetBytes(pack_pat_data);
                //pack_pat_tmp.Decode();


                TS_PMT   pmt      = new TS_PMT();
                var      pmt_data = isOnlyVideo ? pmt.GetVideoBytes() : pmt.GetBytes();
                TSPacket pack_pmt = new TSPacket()
                {
                    sync_byte = 0x47,
                    transport_error_indicator    = 0,
                    payload_unit_start_indicator = 1,
                    transport_priority           = 0,
                    PID = 256,//PMT_PID
                    transport_scrambling_control = 0,
                    adaptation_field_control     = 1,
                    continuity_counter           = ts_pm.GetCounter(256),
                    data = pmt_data,
                };
                var pack_pmt_data = pack_pmt.GetBytes();
                var pack_pmt_tmp  = new TSPacket()
                {
                    ProgramManage = ts_pm,
                };
                msOutput.Write(pack_pmt_data, 0, pack_pmt_data.Length);

                //pack_pmt_tmp.SetBytes(pack_pmt_data);
                //pack_pmt_tmp.Decode();

                var _out = msOutput.ToArray();
                _pmt_pat_tsdata = _out;
            }
            return(_pmt_pat_tsdata);
        }