public void Append(TransportPackets packets)
 {
     while (packets.Count > 0)
     {
         packetList.Add(packets.TakeFirst());
         Count++;
     }
 }
 public TransportPackets Packetize(long initialPTS, ushort PID, ref int ContinuityCounter)
 {
     if (GeneratePTSPacket)
     {
         TransportPackets result = new TransportPackets();
         result.AddPacket(GeneratePTSPadding(initialPTS, PID, ref ContinuityCounter));
         return(result);
     }
     else
     {
         return(PacketizeFile(initialPTS, PID, ref ContinuityCounter));
     }
 }
        public TransportPackets ReadMorePackets(long InitialPTS, ushort PID)
        {
            TransportPackets result = new TransportPackets();

            if (Count == 0)
            {
                return(result);
            }

            result.Append(this[0].Packetize(InitialPTS, PID, ref ContinuityCounter));
            RemoveAt(0);

            return(result);
        }
        private void BufferMore()
        {
            if (Packets.Count == 0)
            {
                Packets = SubtitleItems.RegionList.ReadMorePackets(InitialPtsInt, PID);

                /*if (SubtitleItems.Count > 0 && SubtitleItems[0] != null)
                 * {
                 *  SubtitleItem item = SubtitleItems[0];
                 *  SubtitleItems.RemoveAt(0);
                 *
                 *  while (lastPresentationTime > 0 && (item.PresentationTime - lastPresentationTime) >= 650)
                 *  {
                 *      lastPresentationTime += 650;
                 *      packetizePadding(lastPresentationTime);
                 *  }
                 *
                 *  packetizeFile(item.fileName, item.PresentationTime);
                 *  lastPresentationTime = item.PresentationTime;
                 * }*/
            }
        }
        private TransportPackets PacketizeFile(long InitialPTS, ushort PID, ref int ContinuityCounter)
        {
            TransportPackets result = new TransportPackets();

            FileStream      inputFileStream = new FileStream(SourceFile, FileMode.Open, FileAccess.Read);
            BigEndianReader reader          = new BigEndianReader(inputFileStream);

            long timeStamp = PresentationTimeStamp + InitialPTS;

            long bytesRemaining = ItemLength;

            int i = 0;

            while (bytesRemaining > 0)
            {
                long stuffingBytes = 0;
                int  headerSize    = (i == 0) ? 18 : 4;
                if (bytesRemaining < 188 - headerSize)
                {
                    stuffingBytes = 188 - headerSize - bytesRemaining;
                }

                ByteArray transportData = new ByteArray();
                transportData.MaxSize = 188;

                //------------------------------
                // Transport Packet Header
                //------------------------------
                // sync_byte = 0x47					(8 bits)
                transportData.Append((byte)0x47);
                transportData.EnterBitMode();
                // transport_error_indicator		(1 bit)
                transportData.AppendBit(0);
                // payload_unit_start_indicator		(1 bit)
                transportData.AppendBit((byte)((i == 0) ? 1 : 0));
                // transport_priority				(1 bit)
                transportData.AppendBit(0);
                // PID								(13 bits)
                transportData.AppendBits(PID, 12, 0);
                // transport_scrambling_code		(2 bits)
                transportData.AppendBits((byte)0x0, 1, 0);
                // adaptation_field_control			(2 bits)
                transportData.AppendBits((byte)((stuffingBytes > 0) ? 0x3 : 0x1), 1, 0);
                // continuity_counter				(4 bits)
                transportData.AppendBits(ContinuityCounter, 3, 0);
                ContinuityCounter++;
                transportData.LeaveBitMode();

                long used = 4;
                if (stuffingBytes > 0)
                {
                    // Adaptation field for stuffing
                    stuffingBytes--;
                    used++;
                    transportData.Append((byte)stuffingBytes);
                    if (stuffingBytes > 0)
                    {
                        used += stuffingBytes;
                        stuffingBytes--;
                        transportData.Append((byte)0x00);       // flags field

                        for (int k = 0; k < stuffingBytes; k++)
                        {
                            transportData.Append((byte)0xFF);   // stuffing byte
                        }
                    }
                    //else
                    //    throw new Exception("Adaptation Field length = 0");
                }

                if (i == 0)
                {
                    //-------------------------------
                    // PES Packet Header
                    //-------------------------------
                    // packet_start_code_prefix			(24 bits)	(24)
                    // stream_id						(8 bits)	(32)
                    transportData.Append((uint)0x000001BD);
                    long packetLengthPosition = transportData.length;

                    ushort packetLength = (ushort)(ItemLength + 8);
                    // Fill with dummy value
                    transportData.Append(packetLength);

                    // '10'								(2 bits)	(02)	0x8000
                    // PES_scrambling_code				(2 bits)	(04)	0x0000
                    // PES_priority						(1 bit)		(05)	0x0000
                    // data_alignment_indicator			(1 bit)		(06)	0x0400
                    // copyright						(1 bit)		(07)	0x0000
                    // original_or_copy					(1 bit)		(08)	0x0000
                    // PTS_DTS_flags = '10'				(2 bits)	(10)	0x0080
                    // ESCR_flag						(1 bit)		(11)	0x0000
                    // ES_rate_flag						(1 bit)		(12)	0x0000
                    // DSM_trick_mode_flag				(1 bit)		(13)	0x0000
                    // additional_copy_info				(1 bit)		(14)	0x0000
                    // PES_CRC_flag						(1 bit)		(15)	0x0000
                    // PES_extension_flag				(1 bit)		(16)	0x0000
                    //														0x8480
                    transportData.Append((ushort)0x8480);

                    // PES_header_data_length = 0x05	(8 bits)	(08)
                    transportData.Append((byte)0x05);

                    //   '0010'							(4 bits)	(76)
                    //   PTS[32..30]					(3 bits)	(79)
                    //   marker_bit						(1 bit)		(80)
                    //   PTS[29..15]					(15 bits)	(95)
                    //   marker_bit						(1 bit)		(96)
                    //   PTS[14..0]						(15 bits)	(111)
                    //   marker_bit						(1 bit)		(112)
                    transportData.EnterBitMode();
                    transportData.AppendBits((byte)0x2, 3, 0);
                    transportData.AppendBits(timeStamp, 32, 30);
                    transportData.AppendBit(1);
                    transportData.AppendBits(timeStamp, 29, 15);
                    transportData.AppendBit(1);
                    transportData.AppendBits(timeStamp, 14, 0);
                    transportData.AppendBit(1);
                    transportData.LeaveBitMode();

                    used += 14;
                }

                int payloadLength = TimelineRegionList.PacketSize - (int)used;
                bytesRemaining -= payloadLength;
                for (int k = 0; k < payloadLength; k++)
                {
                    transportData.Append((byte)reader.ReadByte());
                }

                long bytesIntoStream = ((PacketStart - 1) + i) * TimelineRegionList.PacketSize * 27000000 / TimelineRegionList.ByteRate;
                long streamTime      = bytesIntoStream + (InitialPTS * 300);

                TransportPacket newPacket = new TransportPacket(transportData.buffer);
                newPacket.StreamTime   = (ulong)streamTime;
                newPacket.DecoderStamp = (ulong)timeStamp * 300;
                newPacket.PID          = PID;

                if (newPacket.StreamTime >= newPacket.DecoderStamp)
                {
                    throw new Exception("Packet is scheduled for delivery after decoder time");
                }

                result.AddPacket(newPacket);
                i++;
            }
            reader.Close();
            inputFileStream.Close();

            return(result);
        }