Esempio n. 1
0
        /// <summary>
        /// Parses PAT structure.
        /// </summary>
        /// <param name="br"></param>
        public void ParsePAT(ref Dictionary<uint, TSProgram> programs)
        {
            if (_payload_unit_start_indicator != 1)
            {
                throw new NotImplementedException("PAT spanning multiple packets");
            }

            _bitstream.SkipBits(8);
            _headerSize++;

            _bitstream.SkipBits(12);
            uint section_length = _bitstream.ReadUBits(12);
            _bitstream.SkipBits(18);
            uint ver = _bitstream.ReadUBits(5);
            _bitstream.SkipBits(17);
            section_length -= 9;
            int count = (int)section_length / 4;
            int residul = (int)section_length % 4;
            for (int i = 0; i < count; i++)
            {
                uint programNumber = _bitstream.ReadUBits(16);
                _bitstream.SkipBits(3);
                if (programNumber != 0)
                {
                    uint programPid = _bitstream.ReadUBits(13);
                    programs[programPid] = new TSProgram(programPid, programNumber);
                }
                else
                {
                    _bitstream.SkipBits(13);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Parses PMT structure.
        /// </summary>
        /// <param name="program"></param>
        /// <param name="br"></param>
        private void ParsePMT(TSPacket tsPacket, TSProgram program)
        {
            tsPacket.ParsePMTHeader();

            uint streamTypeCode = 0;
            uint elementary_PID = 0;
            uint ES_info_length = 0;
            while (tsPacket.ParseNextPMTStream(ref streamTypeCode, ref elementary_PID, ref ES_info_length))
            {
                // Allocate stream and payload handlers
                if (_streams == null)
                    _streams = new Dictionary<uint, TSStream>();

                TSStream stream;
                bool discontinuity;

                HLSTrace.WriteLineLow("PMT: type {0} pid {1} len {2}", streamTypeCode.ToString("X"), elementary_PID.ToString("X"), ES_info_length.ToString("X"));

                if (_streams.TryGetValue(elementary_PID, out stream))
                {
                    discontinuity = false;
                }
                else
                {
                    if (streamTypeCode == AVC_STREAM_TYPE_CODE)
                    {
                        stream = new TSStream(elementary_PID, TSStream.StreamContetType.Video);

                        if (_videoFormatParser == null)
                            _videoFormatParser = new H264Parser(_videoBuffer, _metadata, _stream.HLSStream );
                        else
                            _videoFormatParser.Flush();

                        _videoFormatParser.Bitrate = _bitrate;

                        stream.Parser = new PESParser(_videoFormatParser);
                    }
                    else if (streamTypeCode == AAC_STREAM_TYPE_CODE || streamTypeCode == DDPLUS_STREAM_TYPE_CODE)
                    {
                        stream = new TSStream(elementary_PID, TSStream.StreamContetType.Audio);

                        bool discardThisStream = false;

                        foreach (KeyValuePair<uint, TSStream> s in _streams)
                        {
                            if (s.Value.StreamType == TSStream.StreamContetType.Audio)
                            {
                                if (s.Key > elementary_PID)
                                {
                                    if (_nullFormatParser == null)
                                        _nullFormatParser = new NullParser(null);

                                    s.Value.Parser = new PESParser(_nullFormatParser);
                                }
                                else
                                {
                                    discardThisStream = true;
                                    break;
                                }
                            }
                        }

                        if (discardThisStream)
                        {
                            if (_nullFormatParser == null)
                                _nullFormatParser = new NullParser(null);

                            stream.Parser = new PESParser(_nullFormatParser);
                            stream.StreamType = TSStream.StreamContetType.Unknown;
                        }
                        else
                        {
                            if (null != _audioFormatParser)
                            {
                                _audioFormatParser.Flush();
                            }
                            if (streamTypeCode == AAC_STREAM_TYPE_CODE)
                                _audioFormatParser = new ADTSParser(_audioBuffer, _stream.HLSStream);
                            else
                                _audioFormatParser = new DDPlusParser(_audioBuffer, _stream.HLSStream);

                            stream.Parser = new PESParser(_audioFormatParser);
                            stream.StreamType = TSStream.StreamContetType.Audio;
                        }
                    }
                    else
                    {
                        stream = new TSStream(elementary_PID, TSStream.StreamContetType.Unknown);
                        HLSTrace.WriteLine("Ignoring unknown stream type {0}", streamTypeCode);
                        if (_nullFormatParser == null)
                            _nullFormatParser = new NullParser(null);
                        stream.Parser = new PESParser(_nullFormatParser);
                    }

                    _streams[elementary_PID] = stream;
                    discontinuity = true;
                }

                program.AddStream(stream);
                stream.Parser.StartSegment(discontinuity);
            }
        }