public void ReadFromCAR(Stream stream) { stream.Position = 0x10; for (int i = 0; i < 4; i++) { var wheelPosition = new WheelPosition(); wheelPosition.ReadFromCAR(stream); WheelPositions.Add(wheelPosition); } WheelPositions = new List <WheelPosition> { WheelPositions[2], WheelPositions[3], WheelPositions[0], WheelPositions[1] }; Unknown1 = stream.ReadUShort(); Unknown2 = stream.ReadUShort(); Unknown3 = stream.ReadUShort(); Unknown4 = stream.ReadUShort(); stream.Position += 0x04; ushort lodCount = stream.ReadUShort(); LODs = new List <LOD>(lodCount); stream.Position += 0x42; for (int i = 1; i <= lodCount; i++) { var lod = new LOD(); lod.ReadFromCAR(stream); LODs.Add(lod); if (i != lodCount) { stream.Position += 40; // gap between LODs } } Shadow = new Shadow(); Shadow.ReadFromCAR(stream); if (stream.Position != stream.Length) { throw new Exception($"{stream.Length - stream.Position} trailing bytes after shadow"); } }
public void ReadFromCDO(Stream stream) { ushort vertexCount = stream.ReadUShort(); // 3B40 ushort triangleCount = stream.ReadUShort(); ushort quadCount = stream.ReadUShort(); unknown = stream.ReadUShort(); lowBoundX = stream.ReadShort(); lowBoundY = stream.ReadShort(); lowBoundZ = stream.ReadShort(); lowBoundW = stream.ReadShort(); highBoundX = stream.ReadShort(); highBoundY = stream.ReadShort(); highBoundZ = stream.ReadShort(); highBoundW = stream.ReadShort(); Scale = stream.ReadUShort(); unknown2 = stream.ReadSingleByte(); unknown3 = stream.ReadSingleByte(); Vertices = new List <ShadowVertex>(vertexCount); // 3B5C Triangles = new List <ShadowPolygon>(triangleCount); // 3BBC Quads = new List <ShadowPolygon>(quadCount); for (int i = 0; i < vertexCount; i++) { var vertex = new ShadowVertex(); vertex.ReadFromCDO(stream); Vertices.Add(vertex); } for (int i = 0; i < triangleCount; i++) { var triangle = new ShadowPolygon(); triangle.ReadFromCDO(stream, false, Vertices); Triangles.Add(triangle); } for (int i = 0; i < quadCount; i++) { var quad = new ShadowPolygon(); quad.ReadFromCDO(stream, true, Vertices); Quads.Add(quad); } }
public static void Read(Stream file, long startPosition) { file.Position = startPosition; ushort stringCount = file.ReadUShort(); for (ushort i = 0; i < stringCount; i++) { strings.Add(ReadString(file)); } }
public static short ReadShort(this Stream stream) { int longValue = stream.ReadUShort(); if (longValue > short.MaxValue) { longValue -= ushort.MaxValue; } return((short)longValue); }
protected override void RawHandler(byte type, Stream data) { var flag = data.ReadUShort(); if (flag == 0x22) { return; } base.RawHandler(type, data); }
public static void Read(string filename) { using (Stream file = DecompressFile(filename)) { file.Position = 8; ushort stringCount = file.ReadUShort(); for (ushort i = 0; i < stringCount; i++) { int length = (file.ReadUShort() + 1) * 2; byte[] characterData = new byte[length]; file.Read(characterData, 0, length); strings.Add(binaryEncoding.GetString(characterData).TrimEnd('\0')); } } unusedStrings = new List <string>(strings); }
public static void Read(Stream file, uint blockStart, uint blockSize) { file.Position = blockStart; ushort stringCount = file.ReadUShort(); for (ushort i = 0; i < stringCount; i++) { Strings.Add(ReadString(file)); } }
public void LoadFromGameFile(Stream file) { ushort flags = file.ReadUShort(); for (int i = 0; i < 16; i++) { colours[i] = (flags & 1) == 1; flags = (ushort)(flags >> 1); } }
public MAPSettings(Stream stream) { CameraOrigin = stream.ReadIVector3(); CameraTranslation = stream.ReadIVector3(); Lights = new Light[3]; for (int i = 0; i < 3; ++i) { var lightPosition = stream.ReadIVector3(); var lightColor = stream.ReadIVector3(); Lights[i] = new Light(lightPosition, lightColor); } stream.Seek(12, SeekOrigin.Current); Zoom = stream.ReadUShort(); SpriteScale = stream.ReadUShort(); AreaLikeTypes = new int[4]; AreaLikeTypes[0] = stream.ReadInt(); AreaLikeTypes[1] = stream.ReadInt(); AreaLikeTypes[2] = stream.ReadInt(); AreaLikeTypes[3] = stream.ReadInt(); AreaDislikeTypes = new int[4]; AreaDislikeTypes[0] = stream.ReadInt(); AreaDislikeTypes[1] = stream.ReadInt(); AreaDislikeTypes[2] = stream.ReadInt(); AreaDislikeTypes[3] = stream.ReadInt(); MapTileWidth = stream.ReadInt(); MapTileHeight = stream.ReadInt(); MapTiles = new int[MapTileWidth, MapTileHeight]; for (int y = 0; y < MapTileHeight; ++y) { for (int x = 0; x < MapTileWidth; ++x) { MapTiles[x, y] = stream.ReadInt(); } } }
public TFSHeader(Stream stream) { Width = (byte)stream.ReadByte(); RW = (byte)stream.ReadByte(); Height = (byte)stream.ReadByte(); RH = (byte)stream.ReadByte(); PaletteCount = (byte)stream.ReadByte(); RP = (byte)stream.ReadByte(); Resl = stream.ReadUShort(); }
public static byte[] ReadNullableUShortLengthPrefixedByteArray(this Stream stream) { var length = stream.ReadUShort(); if (length == 0) { return(null); } return(stream.ReadByteArray(length)); }
public static string[] ReadStringList(this Stream stream) { ushort len = stream.ReadUShort(); string[] data = new string[len]; for (int i = 0; i < len; ++i) { data[i] = stream.ReadString(); } return(data); }
public MAPObjectInstance(Stream stream) { AnimationState = new ushort[8]; AnimationDuration = new ushort[8]; for (int i = 0; i < 8; ++i) { AnimationState[i] = stream.ReadUShort(); } for (int i = 0; i < 8; ++i) { AnimationDuration[i] = stream.ReadUShort(); } X = stream.ReadUShort(); Y = stream.ReadUShort(); stream.Seek(2, SeekOrigin.Current); }
public override void SendStream(Stream stream,int length) { var marker = stream.ReadByte() | 0xF0; var echoTime = marker == (Target == null ? 0xFE : 0xFD); stream.ReadUShort(); if (echoTime) stream.ReadUShort(); var type = stream.ReadByte(); if (type == 0x10) { var sizePos = stream.Position; var size = stream.ReadUShort(); var flags = stream.ReadByte(); var idFlow = stream.Read7BitLongValue(); var stage = stream.Read7BitLongValue(); if (idFlow == 2 && stage == 1) { var deltaNAck = stream.Read7BitLongValue(); var len = (ushort) stream.ReadByte(); stream.Position += len; stream.ReadByte(); stream.ReadByte();//type stream.ReadUInt();//timestamp var amfReader = new AMF0Reader(stream); var str = amfReader.ReadShortString(true); var num = amfReader.ReadAMFDouble(true); var pos = stream.Position; var connectionInfo = amfReader.ReadVariant(); connectionInfo["tcUrl"] = MiddleSession.QueryUrl; connectionInfo["app"] = MiddleSession.QueryUrl.Split('/').Last(); stream.Position = pos; var amfWriter = new AMF0Writer(stream); amfWriter.WriteObject(connectionInfo, true); length = (int) stream.Position; len = (ushort) (stream.Position - sizePos-2); stream.Position = sizePos; stream.Write(len); } } stream.Position = 6; base.SendStream(stream,length); }
public static string GetFileExtension(Stream data) { // Check to see if the file is an archive Archive archive = new Archive(data, null); if (archive.Format != ArchiveFormat.NULL) return archive.FileExtension; // Check to see if the file is an image Images images = new Images(data, null); if (images.Format != GraphicFormat.NULL) return images.FileExtension; // Check to see if the file is an ADX (special case) if (data.Length > 4 && data.ReadUShort(0x00) == 0x0080 && data.Length > data.ReadUShort(0x02).SwapEndian() + 4 && data.ReadString(data.ReadUShort(0x02).SwapEndian() - 0x02, 6, true) == "(c)CRI") return ".adx"; // Unknown return String.Empty; }
/// <remarks>Note how the read advances to the FileNameOffset and reads only FileNameLength bytes</remarks> public void Read(Stream stream) { var startOfRecord = stream.Position; RecordLength = stream.ReadUInt( ); MajorVersion = stream.ReadUShort( ); MinorVersion = stream.ReadUShort( ); FileReferenceNumber = stream.ReadULong( ); ParentFileReferenceNumber = stream.ReadULong( ); Usn = stream.ReadLong( ); TimeStamp = stream.ReadLong( ); Reason = ( UsnReason )stream.ReadUInt( ); SourceInfo = stream.ReadUInt( ); SecurityId = stream.ReadUInt( ); FileAttributes = stream.ReadUInt( ); FileNameLength = stream.ReadUShort( ); FileNameOffset = stream.ReadUShort( ); stream.Position = startOfRecord + FileNameOffset; FileName = Encoding.Unicode.GetString(stream.ReadBytes(FileNameLength)); stream.Position = startOfRecord + RecordLength; }
private static void ReadCarStatsAndWriteCSV(Stream file, ArcadeCar[] cars, int barsPosition, string csvName) { file.Position = barsPosition; foreach (ArcadeCar car in cars) { car.MaxSpeed = file.ReadSingleByte(); car.Handling = file.ReadSingleByte(); car.Acceleration = file.ReadSingleByte(); } file.MoveToNextMultipleOf(4); foreach (ArcadeCar car in cars) { car.Power = file.ReadUShort(); car.PowerRPM = file.ReadUShort(); car.Torque = file.ReadUShort(); car.TorqueRPM = file.ReadUShort(); car.Weight = file.ReadUShort(); } DumpClassToCSV(csvName, cars); }
public void PrepareForOBJRead(Stream unknownData) { Vertices = new List <ShadowVertex>(); Triangles = new List <ShadowPolygon>(); Quads = new List <ShadowPolygon>(); if (unknownData != null) { unknown = unknownData.ReadUShort(); unknown2 = unknownData.ReadSingleByte(); unknown3 = unknownData.ReadSingleByte(); } }
public void ReadFromCDO(Stream stream) { stream.Position = 0x08; Unknown1 = stream.ReadUShort(); if (Unknown1 == 0) { stream.Position = 0x18; Unknown1 = stream.ReadUShort(); } Unknown2 = stream.ReadUShort(); Unknown3 = stream.ReadUShort(); Scale = stream.ReadUShort(); for (int i = 0; i < 4; i++) { var wheelPosition = new WheelPosition(); wheelPosition.ReadFromCDO(stream); WheelPositions.Add(wheelPosition); } stream.Position += 0x828; ushort lodCount = stream.ReadUShort(); LODs = new List <LOD>(lodCount); stream.Position += 0x1A; for (int i = 0; i < lodCount; i++) { var lod = new LOD(); lod.ReadFromCDO(stream); LODs.Add(lod); } }
public void ReadFromCAR(Stream stream) { ushort unknown = stream.ReadUShort(); // always 0? ushort quadCount = stream.ReadUShort(); Scale = stream.ReadUShort(); ushort unknown2 = stream.ReadUShort(); // always 0? lowBoundX = stream.ReadShort(); lowBoundY = stream.ReadShort(); lowBoundZ = stream.ReadShort(); lowBoundW = stream.ReadShort(); highBoundX = stream.ReadShort(); highBoundY = stream.ReadShort(); highBoundZ = stream.ReadShort(); highBoundW = stream.ReadShort(); stream.Position += 8; int vertexCount = quadCount * 4; Vertices = new List <ShadowVertex>(vertexCount); Triangles = new List <ShadowPolygon>(); Quads = new List <ShadowPolygon>(quadCount); for (int i = 0; i < vertexCount; i++) { var vertex = new ShadowVertex(); vertex.ReadFromCAR(stream); Vertices.Add(vertex); } for (int i = 0; i < quadCount; i++) { var quad = new ShadowPolygon(); quad.ReadFromCAR(stream, Vertices, i); Quads.Add(quad); } }
public static void Read <TStructure>(this List <TStructure> structureList, Stream file) where TStructure : DataStructure, new() { TStructure structure = new TStructure(); Console.WriteLine($"Reading {structure.Name} structures from file..."); byte[] magic = new byte[4]; file.Read(magic); if (Encoding.ASCII.GetString(magic) != "GTDT") { Console.WriteLine("Not a GTDT table."); return; } uint unknown = file.ReadUInt(); uint structCount = file.ReadUShort(); uint structSize = file.ReadUShort(); if (structSize != structure.Size) { Console.WriteLine("Unexpected structure size."); return; } uint tableSize = file.ReadUInt(); if (file.Length != tableSize) { Console.WriteLine("Unexpected table size."); return; } for (int i = 0; i < structCount; i++) { TStructure newStructure = new TStructure(); newStructure.Read(file); structureList.Add(newStructure); } }
protected static IColumnSpec[] ReadColumnSpecs(int colCount, string keyspace, string table, bool globalTablesSpec, Stream stream) { IColumnSpec[] columnSpecs = new IColumnSpec[colCount]; for (int colIdx = 0; colIdx < colCount; ++colIdx) { string colKeyspace = keyspace; string colTable = table; if (!globalTablesSpec) { colKeyspace = stream.ReadString(); colTable = stream.ReadString(); } string colName = stream.ReadString(); ColumnType colType = (ColumnType)stream.ReadUShort(); string colCustom = null; ColumnType colKeyType = ColumnType.Custom; ColumnType colValueType = ColumnType.Custom; switch (colType) { case ColumnType.Custom: colCustom = stream.ReadString(); break; case ColumnType.List: case ColumnType.Set: colValueType = (ColumnType)stream.ReadUShort(); break; case ColumnType.Map: colKeyType = (ColumnType)stream.ReadUShort(); colValueType = (ColumnType)stream.ReadUShort(); break; } columnSpecs[colIdx] = new ColumnSpec(colIdx, colKeyspace, colTable, colName, colType, colCustom, colKeyType, colValueType); } return(columnSpecs); }
public List <byte> LoadFromGameFile(Stream file) { var coloursWithAlpha = new List <byte>(); for (byte i = 0; i < ColourCount; i++) { colours[i] = file.ReadUShort(); if (colours[i] != 0xFFFF && (colours[i] & AlphaBit) != 0) { coloursWithAlpha.Add(i); } } return(coloursWithAlpha); }
public static string ReadString(this Stream stream) { ushort len = stream.ReadUShort(); if (0 != len) { byte[] bufStr = new byte[len]; stream.ReadBuffer(bufStr); string data = Encoding.UTF8.GetString(bufStr); return(data); } return(string.Empty); }
public static Dictionary <string, string[]> ReadStringMultimap(this Stream stream) { ushort len = stream.ReadUShort(); Dictionary <string, string[]> data = new Dictionary <string, string[]>(len); for (int i = 0; i < len; ++i) { string key = stream.ReadString(); string[] value = stream.ReadStringList(); data.Add(key, value); } return(data); }
public static string ReadUTF8String(this Stream ms, bool isReverse = true) { try { ushort len = ms.ReadUShort(isReverse); byte[] arr = new byte[len]; ms.Read(arr, 0, len); return(Encoding.UTF8.GetString(arr)); } catch { return(string.Empty); } }
public static async Task <MinecraftHandshake> Read(Stream stream) { var handshake = new MinecraftHandshake(); handshake.ProtocolVersion = await stream.ReadVarInt(); handshake.ServerAddress = await stream.ReadVarString(); handshake.Port = await stream.ReadUShort(); handshake.NextState = (State)await stream.ReadVarInt(); return(handshake); }
public void Read(Stream stream, DbEnvironment env, IFormatToken previous) { Logger.Instance?.WriteLine($"<- {Type}"); var remainingLength = stream.ReadUShort(); using (var ts = new ReadablePartialStream(stream, remainingLength)) { for (var i = 0; i < previous.Formats.Length; i++) { var customFormatInfo = ts.ReadByteLengthPrefixedString(env.Encoding); if (!string.IsNullOrWhiteSpace(customFormatInfo)) { Logger.Instance?.WriteLine($" <- Fmt: {customFormatInfo}"); } } } }
public string ReadName(Stream stream, uint carNumber, byte colourCount) { if (stream == null) { return(null); } stream.Position = ((carNumber + 1) * 8) + 4; ushort index = stream.ReadUShort(); stream.Position = index + (colourCount * 3); byte stringLength = (byte)stream.ReadByte(); byte[] stringBytes = new byte[stringLength]; stream.Read(stringBytes); return(Encoding.Default.GetString(stringBytes).TrimEnd('\0').Replace(((char)0x7F).ToString(), "[R]")); }
private static List <byte[]> ReadMainStructs(Stream file) { ushort structCount = file.ReadUShort(); file.ReadUInt(); // always zero? uint structSize = file.ReadUInt(); var structs = new List <byte[]>(structCount); for (ushort i = 0; i < structCount; i++) { var buffer = new byte[structSize]; file.Read(buffer); structs.Add(buffer); } return(structs); }
private static object ReadTDS_UINTN(Stream stream, FormatItem format, DbEnvironment env) { switch (stream.ReadByte()) { case 0: return(DBNull.Value); case 1: return((byte)stream.ReadByte()); case 2: return(stream.ReadUShort()); case 4: return(stream.ReadUInt()); case 8: return(stream.ReadULong()); } return(DBNull.Value); }
public void Read(Stream stream, DbEnvironment env, IFormatToken previousFormatToken) { Logger.Instance?.WriteLine($"<- {Type}"); var remainingLength = stream.ReadUShort(); using (var ts = new ReadablePartialStream(stream, remainingLength)) { var formats = new List <FormatItem>(); var columnCount = ts.ReadUShort(); for (var i = 0; i < columnCount; i++) { formats.Add(FormatItem.ReadForRow(ts, env.Encoding, Type)); } Formats = formats.ToArray(); } }
/* Get the offsets, lengths, and filenames of all the files */ public override ArchiveFileList GetFileList(ref Stream data) { try { /* Get the offset of each section of the NARC file */ uint offset_fatb = data.ReadUShort(0xC); uint offset_fntb = offset_fatb + data.ReadUInt(offset_fatb + 0x4); uint offset_fimg = offset_fntb + data.ReadUInt(offset_fntb + 0x4); /* Stuff for filenames */ bool containsFilenames = (data.ReadUInt(offset_fntb + 0x8) == 8); uint offset_filename = offset_fntb + 0x10; /* Get the number of files */ uint files = data.ReadUInt(offset_fatb + 0x8); /* Create the array of files now */ ArchiveFileList fileList = new ArchiveFileList(files); /* Now we can get the file offsets, lengths, and filenames */ for (uint i = 0; i < files; i++) { /* Get the offset & length */ uint offset = data.ReadUInt(offset_fatb + 0x0C + (i * 0x8)); uint length = data.ReadUInt(offset_fatb + 0x10 + (i * 0x8)) - offset; /* Get the filename, if the NARC contains filenames */ string filename = String.Empty; if (containsFilenames) { /* Ok, since the NARC contains filenames, let's go grab it now */ byte filename_length = data.ReadByte(offset_filename); filename = data.ReadString(offset_filename + 1, filename_length); offset_filename += (uint)(filename_length + 1); } fileList.Entries[i] = new ArchiveFileList.Entry( offset + offset_fimg + 0x8, // Offset length, // Length filename // Filename ); } return fileList; } catch { /* Something went wrong, so return nothing */ return null; } }
private static Exception CreateExceptionFromError(Stream stream) { ErrorCodes code = (ErrorCodes) stream.ReadInt(); string msg = stream.ReadString(); switch (code) { case ErrorCodes.Unavailable: { ConsistencyLevel cl = (ConsistencyLevel) stream.ReadUShort(); int required = stream.ReadInt(); int alive = stream.ReadInt(); return new UnavailableException(msg, cl, required, alive); } case ErrorCodes.WriteTimeout: { ConsistencyLevel cl = (ConsistencyLevel) stream.ReadUShort(); int received = stream.ReadInt(); int blockFor = stream.ReadInt(); string writeType = stream.ReadString(); return new WriteTimeOutException(msg, cl, received, blockFor, writeType); } case ErrorCodes.ReadTimeout: { ConsistencyLevel cl = (ConsistencyLevel) stream.ReadUShort(); int received = stream.ReadInt(); int blockFor = stream.ReadInt(); bool dataPresent = 0 != stream.ReadByte(); return new ReadTimeOutException(msg, cl, received, blockFor, dataPresent); } case ErrorCodes.Syntax: return new SyntaxException(msg); case ErrorCodes.Unauthorized: return new UnauthorizedException(msg); case ErrorCodes.Invalid: return new InvalidException(msg); case ErrorCodes.AlreadyExists: { string keyspace = stream.ReadString(); string table = stream.ReadString(); return new AlreadyExistsException(msg, keyspace, table); } case ErrorCodes.Unprepared: { byte[] unknownId = stream.ReadShortBytes(); return new UnpreparedException(msg, unknownId); } default: return new CassandraException(code, msg); } }
protected override void RawHandler(byte type, Stream data) { var flag = data.ReadUShort(); if (flag == 0x22) return; base.RawHandler(type,data); }
/* To simplify the process greatly, we are going to convert * the GVM to a new format */ public override MemoryStream TranslateData(ref Stream stream) { try { /* Get the number of files, and format type in the stream */ ushort files = stream.ReadUShort(0xA).SwapEndian(); byte formatType = stream.ReadByte(0x9); /* Now let's see what information is contained inside the metadata */ bool containsFilename = (formatType & (1 << 3)) > 0; bool containsPixelFormat = (formatType & (1 << 2)) > 0; bool containsDimensions = (formatType & (1 << 1)) > 0; bool containsGlobalIndex = (formatType & (1 << 0)) > 0; /* Let's figure out the metadata size */ int size_filename = 0, size_pixelFormat = 0, size_dimensions = 0, size_globalIndex = 0; if (containsFilename) size_filename = 28; if (containsPixelFormat) size_pixelFormat = 2; if (containsDimensions) size_dimensions = 2; if (containsGlobalIndex) size_globalIndex = 4; int metaDataSize = 2 + size_filename + size_pixelFormat + size_dimensions + size_globalIndex; /* Now create the header */ MemoryStream data = new MemoryStream(); data.Write(files); /* Ok, try to find out data */ uint sourceOffset = stream.ReadUInt(0x4) + 0x8; /* Write each file in the header */ uint offset = 0x2 + ((uint)files * 0x24); for (int i = 0; i < files; i++) { /* Ok, get the size of the GVR file */ uint length = stream.ReadUInt(sourceOffset + 0x4) + 8; /* Make sure this is a valid file length */ if (sourceOffset + length > stream.Length) length -= 16; // For some reason some GVR files are like this. if (sourceOffset + length > stream.Length) throw new Exception(); /* Write the offset, file length, and filename */ data.Write(offset); // Offset data.Write(length + 16); // Length if (containsFilename) data.Write(stream.ReadString(0xE + (i * metaDataSize), 28), 28); // Filename else data.Position += 28; /* Add the GBIX header */ data.Position = offset; data.Write(GraphicHeader.GBIX); data.Write((int)0x8); /* Copy the global index */ if (containsGlobalIndex) data.Write(stream.ReadUInt(0xE + size_filename + size_pixelFormat + size_dimensions + (i * metaDataSize))); else data.Position += 4; /* Write out the 0x0 in the header */ data.Write(new byte[] { 0x0, 0x0, 0x0, 0x0 }); /* Now copy the file */ data.Write(stream, sourceOffset, length); data.Position = 0x26 + (i * 0x24); sourceOffset += length.RoundUp(16); /* Increment the offset */ offset += length + 16; } return data; } catch { /* Something went wrong, so send as blank stream */ return new MemoryStream(); } }
/* Get the offsets, lengths, and filenames of all the files */ public override ArchiveFileList GetFileList(ref Stream data) { try { /* Get the number of files */ ushort files = data.ReadUShort(0x0); /* Create the array of files now */ ArchiveFileList fileList = new ArchiveFileList(files); /* Now we can get the file offsets, lengths, and filenames */ for (int i = 0; i < files; i++) { /* Get the filename */ string filename = data.ReadString(0xA + (i * 0x24), 28); fileList.Entries[i] = new ArchiveFileList.Entry( data.ReadUInt(0x2 + (i * 0x24)), // Offset data.ReadUInt(0x6 + (i * 0x24)), // Length (filename == String.Empty ? String.Empty : filename + (filename.IsAllUpperCase() ? ".GVR" : ".gvr")) // Filename ); } return fileList; } catch { /* Something went wrong, so return nothing */ return null; } }
private bool FeedDataAudioMPEG4Generic(Stream pData, uint dataLength, uint processedLength, uint totalLength, uint absoluteTimestamp) { //1. Take care of chunked content first //this will update pData and dataLength if necessary if (dataLength != totalLength) { //2. This is chunked content. Test if this is the first chunk from the //packet if (processedLength == 0) { //3. This is the first chunk of the packet. //Empty the old buffer and store this new chunk _audioBuffer.IgnoreAll(); pData.CopyDataTo(_audioBuffer,(int) dataLength); return true; } else { //4. This is not the first chunk. Test to see if this is //the last chunk or not if (dataLength + processedLength < totalLength) { //5. This is not the last chunk of the packet. //Test and see if we have any previous data inside the buffer //if we don't, that means we didn't catch the beginning //of the packet so we discard everything if (_audioBuffer.GetAvaliableByteCounts() == 0) return true; //6. Store the data pData.CopyDataTo(_audioBuffer, (int) dataLength); //7. Done return true; } else { //8. This is the last chunk of the packet. //Test and see if we have any previous data inside the buffer //if we don't, that means we didn't catch the beginning //of the packet so we discard everything if (_audioBuffer.GetAvaliableByteCounts() == 0) return true; //9. Store the data pData.CopyDataTo(_audioBuffer, (int)dataLength); //10. Get the buffer and its length pData = _audioBuffer; dataLength = (uint) _audioBuffer.GetAvaliableByteCounts(); //11. Do a final test and see if we have all the data if (dataLength != totalLength) { FATAL("Invalid data length"); return false; } } } } var inStreamType = InStream.Type; if ((inStreamType == ST_IN_NET_RTMP) || (inStreamType == ST_IN_NET_RTP) || (inStreamType == ST_IN_NET_LIVEFLV)) { //2. Do we have enough data to read the RTMP header? if (dataLength <= 2) { WARN("Bogus AAC packet"); _audioBuffer.IgnoreAll(); return true; } var firstByte = pData.ReadByte(); var secondByte = pData.ReadByte(); //3. Take care of the RTMP headers if necessary if (inStreamType == ST_IN_NET_RTMP || inStreamType == ST_IN_NET_LIVEFLV) { //3. Is this a RTMP codec setup? If so, ignore it if (secondByte != 1) { _audioBuffer.IgnoreAll(); return true; } } //4. Skip the RTMP header dataLength -= 2; } //4. Do we have enough data to detect the ADTS header presence? if (dataLength <= 2) { WARN("Bogus AAC packet"); _audioBuffer.IgnoreAll(); return true; } //4. The packet might start with an ADTS header. Remove it if necessary var adtsHeaderLength = 0; var temp = pData.ReadUShort(); if ((temp >> 3) == 0x1fff) { adtsHeaderLength = 7; } /* 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |V=2|P|X| CC |M| PT | sequence number | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | timestamp | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | synchronization source (SSRC) identifier | +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ | contributing source (CSRC) identifiers | | .... | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+ |AU-headers-length|AU-header|AU-header| |AU-header|padding| | | (1) | (2) | | (n) | bits | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+ */ //5. counter _audioData.Buffers[0].Write(2, AudioCounter++); //6. Timestamp _audioData.Buffers[0].Write(4, absoluteTimestamp*Capabilities.Samplerate/1000); _audioData.Buffers[0].Write(12,(ushort)16); var auHeader = (dataLength - adtsHeaderLength) << 3; Array.Resize(ref _audioData.Buffers[1], 2); _audioData.Buffers[1].Write(0, (ushort)auHeader); //7. put the actual buffer _audioData.Buffers[2] = new byte[dataLength - adtsHeaderLength]; pData.Position += adtsHeaderLength; pData.Read(_audioData.Buffers[2], 0, _audioData.Buffers[2].Length); if (!Connectivity.FeedAudioData(ref _audioData, absoluteTimestamp)) { FATAL("Unable to feed data"); _audioBuffer.IgnoreAll(); return false; } _audioBuffer.IgnoreAll(); return true; }