Ejemplo n.º 1
0
        public PesPacket(byte[] buffer, int offset = 0)
        {
            int position = offset;
            if (buffer[position++] != 0 || buffer[position++] != 0 || buffer[position++] != 1)
                throw new ArgumentException("PES Packet does not start with 0x000001 Start Code Prefix.");
            StreamIdentifier = (StreamIdentifier) buffer[position++];

            PacketLength = (buffer[position++] << 8) | buffer[position++];
            if (PacketLength == 0) PacketLength = buffer.Length - offset - 6;

            if (StreamIdentifier != StreamIdentifier.ProgramStreamMap
                && StreamIdentifier != StreamIdentifier.PaddingStream
                && StreamIdentifier != StreamIdentifier.PrivateStream2
                && StreamIdentifier != StreamIdentifier.EcmStream
                && StreamIdentifier != StreamIdentifier.EmmStream
                && StreamIdentifier != StreamIdentifier.ProgramStreamDirectory
                && StreamIdentifier != StreamIdentifier.DsmCcStream
                && StreamIdentifier != StreamIdentifier.TypeEStream)
            {
                Header = new PESHeader(buffer, position);
                position += Header.Length;

                Data = new byte[PacketLength - Header.Length];
                // TODO: Something may be squirrely here.  position+PacketLength should never be > buffer.Length
                // Either ffmpeg is producing bad PES packets or we are reassembling units incorrectly.
                // Math.Min() here prevents the exception but doesn't fix the underlying problem.
                if (PacketLength > 0) Buffer.BlockCopy(buffer, position, Data, 0, Math.Min(Data.Length, buffer.Length - position));
               
            }
            else if (StreamIdentifier == StreamIdentifier.ProgramStreamMap
                     || StreamIdentifier == StreamIdentifier.PrivateStream2
                     || StreamIdentifier == StreamIdentifier.EcmStream
                     || StreamIdentifier == StreamIdentifier.EmmStream
                     || StreamIdentifier == StreamIdentifier.ProgramStreamDirectory
                     || StreamIdentifier == StreamIdentifier.DsmCcStream
                     || StreamIdentifier == StreamIdentifier.TypeEStream)
            {
                Data = new byte[PacketLength];
                if (PacketLength > 0) Buffer.BlockCopy(buffer, position, Data, 0, Math.Min(Data.Length, buffer.Length - position));
            }
            else if (StreamIdentifier == StreamIdentifier.PaddingStream)
            {
                Stuffing = new byte[PacketLength];
                if (PacketLength > 0) Buffer.BlockCopy(buffer, position, Data, 0, Math.Min(Data.Length, buffer.Length - position));
            }
#if DEBUG
            if(Data!=null && Data.Length > 0)
            {
                Debug.WriteLine("First 40 bytes of PES Data: " + String.Concat(Data.Take(40).Select((b) => b.ToString("X2")).ToArray()));
            }
#endif
        }
Ejemplo n.º 2
0
        public PesPacket(byte[] buffer, int offset = 0)
        {
            int position = offset;

            if (buffer[position++] != 0 || buffer[position++] != 0 || buffer[position++] != 1)
            {
                throw new ArgumentException("PES Packet does not start with 0x000001 Start Code Prefix.");
            }

            StreamIdentifier = (StreamIdentifier)buffer[position++];

            PacketLength = (buffer[position++] << 8) | buffer[position++];
            if (PacketLength == 0)
            {
                PacketLength = buffer.Length - offset - 6;
            }

            if (StreamIdentifier != StreamIdentifier.ProgramStreamMap &&
                StreamIdentifier != StreamIdentifier.PaddingStream &&
                StreamIdentifier != StreamIdentifier.PrivateStream2 &&
                StreamIdentifier != StreamIdentifier.EcmStream &&
                StreamIdentifier != StreamIdentifier.EmmStream &&
                StreamIdentifier != StreamIdentifier.ProgramStreamDirectory &&
                StreamIdentifier != StreamIdentifier.DsmCcStream &&
                StreamIdentifier != StreamIdentifier.TypeEStream)
            {
                Header    = new PESHeader(buffer, position);
                position += Header.Length;

                Data = new byte[PacketLength - Header.Length];
                // TODO: Something may be squirrely here.  position+PacketLength should never be > buffer.Length
                // Either ffmpeg is producing bad PES packets or we are reassembling units incorrectly.
                // Math.Min() here prevents the exception but doesn't fix the underlying problem.
                if (PacketLength > 0)
                {
                    Buffer.BlockCopy(buffer, position, Data, 0, Math.Min(Data.Length, buffer.Length - position));
                }
            }
            else if (StreamIdentifier == StreamIdentifier.ProgramStreamMap ||
                     StreamIdentifier == StreamIdentifier.PrivateStream2 ||
                     StreamIdentifier == StreamIdentifier.EcmStream ||
                     StreamIdentifier == StreamIdentifier.EmmStream ||
                     StreamIdentifier == StreamIdentifier.ProgramStreamDirectory ||
                     StreamIdentifier == StreamIdentifier.DsmCcStream ||
                     StreamIdentifier == StreamIdentifier.TypeEStream)
            {
                Data = new byte[PacketLength];
                if (PacketLength > 0)
                {
                    Buffer.BlockCopy(buffer, position, Data, 0, Math.Min(Data.Length, buffer.Length - position));
                }
            }
            else if (StreamIdentifier == StreamIdentifier.PaddingStream)
            {
                Stuffing = new byte[PacketLength];
                if (PacketLength > 0)
                {
                    Buffer.BlockCopy(buffer, position, Data, 0, Math.Min(Data.Length, buffer.Length - position));
                }
            }
#if DEBUG
            if (Data != null && Data.Length > 0)
            {
                Debug.WriteLine("First 40 bytes of PES Data: " + string.Concat(Data.Take(40).Select((b) => b.ToString("X2")).ToArray()));
            }
#endif
        }