/// <summary> /// Special constructor for creating new demux for next stream in sequence /// </summary> /// <param name="stream"></param> /// <param name="previousDemux"></param> public TSDemux(EncryptedStream stream, TSDemux previousDemux, IContainerMetadata metadata, uint bitrate, BandwidthHistory BWHistory) { if (previousDemux == null) throw new ArgumentNullException("previousDemux"); CommonConstruct(stream, previousDemux._audioBuffer, previousDemux._videoBuffer, metadata, bitrate, BWHistory); if (!stream.Discontinuity) _streams = previousDemux._streams; _audioFormatParser = previousDemux._audioFormatParser; _videoFormatParser = previousDemux._videoFormatParser; _audioFormatParser.HLSStream = stream.HLSStream; _videoFormatParser.HLSStream = stream.HLSStream; }
/// <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); } }
/// <summary> /// Default constructor. /// </summary> /// <param name="sampler">Output sampler</param> public PESParser(MediaFormatParser formatParser) { _output = formatParser; _byteBuffer = new byte[1024]; _bitstream = new BitstreamReader(); }