public AdaptationField(byte[] buffer, int offset = 4) { var position = offset; var length = Length = buffer[position++]; if (length <= 0) { return; } _flags = buffer[position++]; if (HasProgramClockReference) { _programClockReference = new byte[6]; Buffer.BlockCopy(buffer, position, _programClockReference, 0, 6); position += 6; Debug.WriteLine("Has Program Clock Reference: " + ProgramClockReference); } if (HasOriginalProgramClockReference) { Debug.WriteLine("Has Original Program Clock Reference."); _originalProgramClockReference = new byte[6]; Buffer.BlockCopy(buffer, position, _originalProgramClockReference, 0, 6); position += 6; } if (HasSpliceCountdown) { Debug.WriteLine("Has Splice Countdown."); SpliceCountdown = (sbyte)buffer[position++]; } if (HasPrivateData) { byte privateDataLength = buffer[position++]; Debug.WriteLine("Has " + privateDataLength + " byte of Private Data."); if (position > TsPacket.Length) { privateDataLength = 0; Debug.WriteLine("_position exceeds length by " + Math.Abs(buffer.Length - position) + " bytes."); throw new FormatException("Invalid private data length."); } else if (position + privateDataLength > TsPacket.Length) { Debug.WriteLine(" Private data exceeds length by " + Math.Abs(TsPacket.Length - position - privateDataLength) + " bytes!!!!!!!"); privateDataLength = (byte)(TsPacket.Length - position); Debug.WriteLine("Reading " + privateDataLength + " bytes of Private Data."); } if (privateDataLength > 0) { var privateData = new byte[privateDataLength]; Buffer.BlockCopy(buffer, position, privateData, 0, privateDataLength); PrivateData = privateData; position += privateDataLength; } } if (HasExtension) { Debug.WriteLine("Has Extension."); Extension = new AdaptationFieldExtension(buffer, position); position += Extension.Length; } int stuffingLength = length + 1 - (position - offset); if (stuffingLength > 0) { Debug.WriteLine("Has " + stuffingLength + " bytes of Adaptation Field stuffing."); Stuffing = new byte[stuffingLength]; Buffer.BlockCopy(buffer, position, Stuffing, 0, stuffingLength); } }
public AdaptationField(byte[] buffer, int offset = 4) { var position = offset; var length = Length = buffer[position++]; if (length <= 0) return; _flags = buffer[position++]; if (HasProgramClockReference) { _programClockReference = new byte[6]; Buffer.BlockCopy(buffer, position, _programClockReference, 0, 6); position += 6; Debug.WriteLine("Has Program Clock Reference: " + ProgramClockReference); } if (HasOriginalProgramClockReference) { Debug.WriteLine("Has Original Program Clock Reference."); _originalProgramClockReference = new byte[6]; Buffer.BlockCopy(buffer, position, _originalProgramClockReference, 0, 6); position += 6; } if (HasSpliceCountdown) { Debug.WriteLine("Has Splice Countdown."); SpliceCountdown = (sbyte) buffer[position++]; } if (HasPrivateData) { byte privateDataLength = buffer[position++]; Debug.WriteLine("Has " + privateDataLength + " byte of Private Data."); if (position > TsPacket.Length) { privateDataLength = 0; Debug.WriteLine("_position exceeds length by " + Math.Abs(buffer.Length - position) + " bytes."); throw new FormatException("Invalid private data length."); } else if (position + privateDataLength > TsPacket.Length) { Debug.WriteLine(" Private data exceeds length by " + Math.Abs(TsPacket.Length - position - privateDataLength) + " bytes!!!!!!!"); privateDataLength = (byte)(TsPacket.Length - position); Debug.WriteLine("Reading " + privateDataLength + " bytes of Private Data."); } if (privateDataLength > 0) { var privateData = new byte[privateDataLength]; Buffer.BlockCopy(buffer, position, privateData, 0, privateDataLength); PrivateData = privateData; position += privateDataLength; } } if (HasExtension) { Debug.WriteLine("Has Extension."); Extension = new AdaptationFieldExtension(buffer, position); position += Extension.Length; } int stuffingLength = length + 1 - (position - offset); if (stuffingLength > 0) { Debug.WriteLine("Has " + stuffingLength + " bytes of Adaptation Field stuffing."); Stuffing = new byte[stuffingLength]; Buffer.BlockCopy(buffer, position, Stuffing, 0, stuffingLength); } }