public PESPacket nextPacket(MemoryStream outb) { MemoryStream dup = outb.duplicate(); while (!MPSUtils.psMarker(curMarker)) { if (!skipToMarker()) { return(null); } } MemoryStream fork = dup.duplicate(); readToNextMarker(dup); PESPacket pkt = MPSUtils.readPESHeader(fork, curPos()); if (pkt.length == 0) { while (!MPSUtils.psMarker(curMarker) && readToNextMarker(dup)) { ; } } else { read(dup, pkt.length - dup.position() + 6); } fork.limit(dup.position()); pkt.data = fork; return(pkt); }
private void doPsByte(byte[] psDate) { if (!(psDate[0] == 0 && psDate[1] == 0 && psDate[2] == 1 && psDate[3] == 0xBA)) { Console.WriteLine("出错了!!!!!!!!"); } long scr = 0; Stream msStream = new System.IO.MemoryStream(psDate); var ph = new PSPacketHeader(msStream); scr = ph.GetSCR(); List <PESPacket> videoPESList = new List <PESPacket>(); while (msStream.Length - msStream.Position > 4) { bool findStartCode = msStream.ReadByte() == 0x00 && msStream.ReadByte() == 0x00 && msStream.ReadByte() == 0x01 && msStream.ReadByte() == 0xE0; if (findStartCode) { msStream.Seek(-4, SeekOrigin.Current); var pesVideo = new PESPacket(); pesVideo.SetBytes(msStream); var esdata = pesVideo.PES_Packet_Data; videoPESList.Add(pesVideo); } } msStream.Close(); HandlES(videoPESList); }
private PESPacket getPacket() { if (m_pending.Count() > 0) { PESPacket r = m_pending[0]; m_pending.RemoveAt(0); return(r); } PESPacket pkt; while ((pkt = demux.nextPacket(demux.getBuffer())) != null) { if (pkt.streamId == streamId) { if (pkt.pts != -1) { es.curPts = pkt.pts; } return(pkt); } else { demux.addToStream(pkt); } } return(null); }
public int read(MemoryStream arg0) { PESPacket pes = null; if (m_pending.Count() > 0) { m_pending.RemoveAt(0); } else { pes = getPacket(); } if (pes == null || !pes.data.hasRemaining()) { return(-1); } int toRead = Math.Min(arg0.remaining(), pes.data.remaining()); arg0.put(StreamExtensions.read(pes.data, toRead)); if (pes.data.hasRemaining()) { m_pending.Insert(0, pes); } else { demux.putBack(pes.data); } return(toRead); }
public void pending(PESPacket pkt) { if (m_pending != null) { m_pending.Add(pkt); } //else //putBack(pkt.data); }
protected void findStreams() { for (int i = 0; i == 0 || i < 5 * streams.Count() && streams.Count() < 2; i++) { PESPacket nextPackets = nextPacket(getBuffer()); if (nextPackets == null) { break; } addToStream(nextPackets); } }
private void doPsByte(byte[] psDate) { Stream msStream = new System.IO.MemoryStream(psDate); List <PESPacket> videoPESList = new List <PESPacket>(); while (msStream.Length - msStream.Position > 4) { bool findStartCode = msStream.ReadByte() == 0x00 && msStream.ReadByte() == 0x00 && msStream.ReadByte() == 0x01 && msStream.ReadByte() == 0xE0; if (findStartCode) { msStream.Seek(-4, SeekOrigin.Current); var pesVideo = new PESPacket(); pesVideo.SetBytes(msStream); var esdata = pesVideo.PES_Packet_Data; videoPESList.Add(pesVideo); } } msStream.Close(); HandlES(videoPESList); }
private void OnAACBody(RTMPMessage msg) { var pts = msg.Timestamp - Math.Max(0, ptsBase); var raw_length = msg.Body.Length - 2; var header = new ADTSHeader(adtsHeader, raw_length + adtsHeader.Bytesize); var pes_payload = new MemoryStream(); using (pes_payload) { ADTSHeader.WriteTo(pes_payload, header); pes_payload.Write(msg.Body, 2, raw_length); } var pes = new PESPacket(0xC0, TSTimeStamp.FromMilliseconds(pts), null, pes_payload.ToArray()); var pes_packet = new MemoryStream(); using (pes_packet) { PESPacket.WriteTo(pes_packet, pes); } writer.WriteTSPackets( AudioPID, true, null, pes_packet.ToArray() ); }
private void addToStream(PESPacket pkt) { BaseTrack pes = (BaseTrack)streams[pkt.streamId]; if (pes == null) { if (isMPEG(pkt.data)) { pes = new MPEGTrack(pkt.streamId, pkt) { demux = this }; } else { pes = new PlainTrack(pkt.streamId, pkt); } streams.Add(pkt.streamId, pes); } else { pes.pending(pkt); } }
private void OnAVCBody(RTMPMessage msg, bool keyframe) { var pts = msg.Timestamp - Math.Max(0, ptsBase); var cts = msg.Body.Skip(2).Take(3).Aggregate(0, (r, v) => (r << 8) | v); if (cts >= 0x800000) { cts = 0x1000000 - cts; } var dts = pts; pts = pts + cts; var access_unit_delimiter = false; var idr = false; var nalbytestream = new MemoryStream(); int units = 0; using (nalbytestream) using (var body = new MemoryStream(msg.Body, 0, msg.Body.Length)) { body.Seek(5, SeekOrigin.Begin); while (body.Position < body.Length) { var len = body.ReadBytes(nalSizeLen).Aggregate(0, (r, v) => (r << 8) | v); var nalu = NALUnit.ReadFrom(body, len); if (nalu.NALUnitType == 9) { access_unit_delimiter = true; } if (!access_unit_delimiter) { NALUnit.WriteToByteStream(nalbytestream, NALUnit.AccessUnitDelimiter); access_unit_delimiter = true; } if (nalu.NALUnitType == 5) { idr = true; foreach (var unit in sps) { NALUnit.WriteToByteStream(nalbytestream, unit); } foreach (var unit in pps) { NALUnit.WriteToByteStream(nalbytestream, unit); } foreach (var unit in spsExt) { NALUnit.WriteToByteStream(nalbytestream, unit); } } NALUnit.WriteToByteStream(nalbytestream, nalu); units += 1; } } var pes = new PESPacket( 0xE0, TSTimeStamp.FromMilliseconds(pts), TSTimeStamp.FromMilliseconds(dts), nalbytestream.ToArray() ); var pes_packet = new MemoryStream(); using (pes_packet) { PESPacket.WriteTo(pes_packet, pes); } writer.WriteTSPackets( VideoPID, msg.IsKeyFrame() || idr, idr ? (TSTimeStamp?)TSTimeStamp.FromMilliseconds(dts) : null, pes_packet.ToArray() ); }
public static void WriteTo(Stream s, PESPacket pkt) { var pes_scrambling_control = 0; var pes_priority = false; var data_alignment_indicator = false; var copyright = false; var original_or_copy = false; var pts_dts_flags = (pkt.PTS.HasValue ? (pkt.DTS.HasValue ? 3 : 2) : 0); var escr_flag = false; var es_rate_flag = false; var dsm_trick_mode_flag = false; var additional_copy_info_flag = false; var pes_crc_flag = false; var pes_extension_flag = false; var pes_header_data = new byte[0]; if (pkt.PTS.HasValue && pkt.DTS.HasValue) { var pts = pkt.PTS.Value.Tick / 300; var dts = pkt.DTS.Value.Tick / 300; pes_header_data = new byte[10] { (byte)((0x3 << 4) | (((pts >> 30) & 0x0007) << 1) | 1), (byte)(((((pts >> 15) & 0x7FFF) << 1) | 1) >> 8), (byte)(((((pts >> 15) & 0x7FFF) << 1) | 1) & 0xFF), (byte)(((((pts >> 0) & 0x7FFF) << 1) | 1) >> 8), (byte)(((((pts >> 0) & 0x7FFF) << 1) | 1) & 0xFF), (byte)((0x1 << 4) | (((dts >> 30) & 0x0007) << 1) | 1), (byte)(((((dts >> 15) & 0x7FFF) << 1) | 1) >> 8), (byte)(((((dts >> 15) & 0x7FFF) << 1) | 1) & 0xFF), (byte)(((((dts >> 0) & 0x7FFF) << 1) | 1) >> 8), (byte)(((((dts >> 0) & 0x7FFF) << 1) | 1) & 0xFF), }; } else if (pkt.PTS.HasValue) { var pts = pkt.PTS.Value.Tick / 300; pes_header_data = new byte[5] { (byte)((0x2 << 4) | (((pts >> 30) & 0x0007) << 1) | 1), (byte)(((((pts >> 15) & 0x7FFF) << 1) | 1) >> 8), (byte)(((((pts >> 15) & 0x7FFF) << 1) | 1) & 0xFF), (byte)(((((pts >> 0) & 0x7FFF) << 1) | 1) >> 8), (byte)(((((pts >> 0) & 0x7FFF) << 1) | 1) & 0xFF), }; } var packet_length = pkt.Payload.Length + 3 + pes_header_data.Length; s.Write(PESStartPrefix, 0, PESStartPrefix.Length); s.WriteByte(pkt.StreamId); s.WriteUInt16BE(packet_length); var header_data1 = (0x2 << 6) | ((pes_scrambling_control & 0x3) << 4) | ((pes_priority ? 1 : 0) << 3) | ((data_alignment_indicator ? 1 : 0) << 2) | ((copyright ? 1 : 0) << 1) | ((original_or_copy ? 1 : 0) << 0); s.WriteByte((byte)header_data1); var header_data2 = ((pts_dts_flags & 0x3) << 6) | ((escr_flag ? 1 : 0) << 5) | ((es_rate_flag ? 1 : 0) << 4) | ((dsm_trick_mode_flag ? 1 : 0) << 3) | ((additional_copy_info_flag ? 1 : 0) << 2) | ((pes_crc_flag ? 1 : 0) << 1) | ((pes_extension_flag ? 1 : 0) << 0); s.WriteByte((byte)header_data2); s.WriteByte((byte)pes_header_data.Length); s.Write(pes_header_data, 0, pes_header_data.Length); s.Write(pkt.Payload, 0, pkt.Payload.Length); }
public BaseTrack(int streamId, PESPacket pkt) { this.streamId = streamId; this.m_pending.Add(pkt); }
public PlainTrack(int streamId, PESPacket pkt) : base(streamId, pkt) { }
public MPEGTrack(int streamId, PESPacket pkt) : base(streamId, pkt) { //FileStream? this.es = new MPEGES(es.channel); }