/// <summary> /// Process a transport packet. /// </summary> /// <param name="buffer">The buffer containing the transport packet.</param> /// <param name="packet">The transport packet.</param> public void ProcessPacket(byte[] buffer, TransportPacket packet) { if (packet.ErrorIndicator || packet.IsNullPacket) { return; } if (!packet.StartIndicator) { return; } SIPacket siPacket = new SIPacket(); siPacket.Process(buffer, packet); if (siPacket.DataIndex < siPacket.ByteData.Length) { addTable((int)siPacket.ByteData[siPacket.DataIndex]); } }
private SIPacket getPacket(bool needPayloadStart) { if (eof) { return(null); } SIPacket siPacket = null; bool done = false; while (!done) { if (memoryHandle == 0) { bool reply = getMappedMemory(); if (!reply) { eof = true; return(null); } } if (currentOffset > maxOffset) { return(null); } byte[] buffer = new byte[188]; for (int index = 0; index < 188; index++) { buffer[index] = this.buffer[currentPointer + index]; } currentPointer += 188; currentOffset += 188; TransportPacket transportPacket = new TransportPacket(); transportPacket.Process(buffer); if (transportPacket.PID == pid) { Logger.Instance.Dump("File Packet", buffer, buffer.Length); if (!transportPacket.ErrorIndicator && !transportPacket.IsNullPacket) { if (needPayloadStart) { if (transportPacket.StartIndicator) { done = true; } } else { done = true; } if (done) { siPacket = new SIPacket(); siPacket.Process(buffer, transportPacket); } } } } return(siPacket); }
private void getSection(BackgroundParameters parameters) { if (siPacket == null) { siPacket = getPacket(true); if (siPacket == null) { return; } if (parameters.Table != 0xff && parameters.Table != siPacket.ByteData[siPacket.DataIndex]) { siPacket = null; return; } packetIndex = siPacket.DataIndex; } if (packetIndex > siPacket.ByteData.Length - 1) { siPacket = getPacket(false); if (siPacket == null) { Thread.Sleep(500); return; } packetIndex = siPacket.DataIndex; } byte table = siPacket.ByteData[packetIndex]; if (table == 0xff) { siPacket = null; return; } if (packetIndex == siPacket.ByteData.Length - 1) { siPacket = getPacket(false); if (siPacket == null) { Thread.Sleep(500); return; } packetIndex = siPacket.Index; } else { packetIndex++; } byte lengthByte1 = siPacket.ByteData[packetIndex]; if (packetIndex == siPacket.ByteData.Length - 1) { siPacket = getPacket(false); if (siPacket == null) { Thread.Sleep(500); return; } packetIndex = siPacket.Index; } else { packetIndex++; } byte lengthByte2 = siPacket.ByteData[packetIndex]; packetIndex++; int length = ((lengthByte1 & 0x0f) * 256) + lengthByte2; mpeg2Section = new Mpeg2Section(); mpeg2Section.Data = new byte[length + 3]; mpeg2Section.Length = length + 3; mpeg2Section.Data[0] = table; mpeg2Section.Data[1] = lengthByte1; mpeg2Section.Data[2] = lengthByte2; for (int index = 3; index < length + 3; index++) { if (packetIndex == siPacket.ByteData.Length) { siPacket = getPacket(false); if (siPacket == null) { Thread.Sleep(500); return; } packetIndex = siPacket.Index; } mpeg2Section.Data[index] = siPacket.ByteData[packetIndex]; packetIndex++; } if ((mpeg2Section.Data[1] & 0x80) == 1) { bool checkCRC = mpeg2Section.CheckCRC(); if (!checkCRC) { return; } } Logger.Instance.Dump("MPEG2 Section", mpeg2Section.Data, mpeg2Section.Data.Length); Lock("GetSection"); if (Sections.Count < parameters.MaxSections) { Sections.Add(mpeg2Section); } Release("GetSection"); }
private void processNewSection(byte[] byteData, TransportPacket transportPacket, Collection<Mpeg2Section> sections) { if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) Logger.Instance.Write(getPidID() + "Processing new section"); if (siPacket == null) { siPacket = new SIPacket(); siPacket.Process(byteData, transportPacket); packetIndex = siPacket.DataIndex; } table = siPacket.ByteData[packetIndex]; if (table == 0xff) { if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) Logger.Instance.Write(getPidID() + "Table: 0xff - rest of block ignored"); packetIndex = siPacket.ByteData.Length; status = handlerStatus.awaitingStart; return; } if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) Logger.Instance.Write(getPidID() + "Table: 0x" + table.ToString("X")); packetIndex++; if (packetIndex == siPacket.ByteData.Length) { status = handlerStatus.awaitingLengthByte1; return; } lengthByte1 = siPacket.ByteData[packetIndex]; if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) Logger.Instance.Write(getPidID() + "Length byte 1: " + lengthByte1); packetIndex++; if (packetIndex == siPacket.ByteData.Length) { status = handlerStatus.awaitingLengthByte2; return; } lengthByte2 = siPacket.ByteData[packetIndex]; packetIndex++; if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) Logger.Instance.Write(getPidID() + "Length byte 2: " + lengthByte2); length = ((lengthByte1 & 0x0f) * 256) + lengthByte2; if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) Logger.Instance.Write(getPidID() + "Table length: " + length); createMpeg2Section(sections); }
/// <summary> /// Process a sample. /// </summary> /// <param name="byteData">The sample data.</param> /// <param name="transportPacket">The transport packet header.</param> public void Process(byte[] byteData, TransportPacket transportPacket) { if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) { Logger.Instance.Dump(getPidID() + "Data Block", byteData, byteData.Length); Logger.Instance.Write(getPidID() + "Status: " + status); } if (continuityCount == -1) continuityCount = transportPacket.ContinuityCount; else { continuityCount++; if (continuityCount > 15) continuityCount = 0; if (transportPacket.ContinuityCount != continuityCount) { if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) Logger.Instance.Write(getPidID() + "Continuity count failure: " + continuityCount + ":" + transportPacket.ContinuityCount); continuityCount = transportPacket.ContinuityCount; status = handlerStatus.awaitingStart; discontinuities++; } } if (empty) { sections.Clear(); empty = false; } bool done = false; siPacket = new SIPacket(); siPacket.Process(byteData, transportPacket); if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLERSI")) Logger.Instance.Write(getPidID() + "Created new SI packet"); switch (status) { case handlerStatus.awaitingStart: if (!transportPacket.StartIndicator) { if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) Logger.Instance.Write(getPidID() + "Not start block"); return; } packetIndex = siPacket.DataIndex; while (packetIndex < siPacket.ByteData.Length) processNewSection(byteData, transportPacket, sections); break; case handlerStatus.awaitingLengthByte1: packetIndex = siPacket.Index; lengthByte1 = siPacket.ByteData[packetIndex]; if (packetIndex == siPacket.ByteData.Length - 1) { status = handlerStatus.awaitingLengthByte2; return; } packetIndex++; lengthByte2 = siPacket.ByteData[packetIndex]; packetIndex++; length = ((lengthByte1 & 0x0f) * 256) + lengthByte2; done = createMpeg2Section(sections); if (!done) return; status = handlerStatus.awaitingStart; while (packetIndex < siPacket.ByteData.Length) processNewSection(byteData, transportPacket, sections); break; case handlerStatus.awaitingLengthByte2: packetIndex = siPacket.Index; lengthByte2 = siPacket.ByteData[packetIndex]; packetIndex++; length = ((lengthByte1 & 0x0f) * 256) + lengthByte2; done = createMpeg2Section(sections); if (!done) return; status = handlerStatus.awaitingStart; while (packetIndex < siPacket.ByteData.Length) processNewSection(byteData, transportPacket, sections); break; case handlerStatus.awaitingMoreData: packetIndex = siPacket.Index; for (; sectionIndex < length + 3; sectionIndex++) { if (packetIndex == siPacket.ByteData.Length) { if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) Logger.Instance.Write(getPidID() + "Need more data"); status = handlerStatus.awaitingMoreData; return; } mpeg2Section.Data[sectionIndex] = siPacket.ByteData[packetIndex]; packetIndex++; } if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) Logger.Instance.Write(getPidID() + "Got table"); if ((mpeg2Section.Data[1] & 0x80) != 0) { bool checkCRC = mpeg2Section.CheckCRC(); if (!checkCRC) { if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) Logger.Instance.Write(getPidID() + "CRC failed"); } else { sections.Add(mpeg2Section); if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) Logger.Instance.Dump(getPidID() + "MPEG2 Section", mpeg2Section.Data, mpeg2Section.Data.Length); } } else { sections.Add(mpeg2Section); if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) Logger.Instance.Dump(getPidID() + "MPEG2 Section", mpeg2Section.Data, mpeg2Section.Data.Length); } status = handlerStatus.awaitingStart; while (packetIndex < siPacket.ByteData.Length) processNewSection(byteData, transportPacket, sections); break; default: break; } siPacket = null; }
private void processNewSection(byte[] byteData, TransportPacket transportPacket, Collection <Mpeg2Section> sections) { if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) { Logger.Instance.Write(getPidID() + "Processing new section"); } if (siPacket == null) { siPacket = new SIPacket(); siPacket.Process(byteData, transportPacket); packetIndex = siPacket.DataIndex; } table = siPacket.ByteData[packetIndex]; if (table == 0xff) { if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) { Logger.Instance.Write(getPidID() + "Table: 0xff - rest of block ignored"); } packetIndex = siPacket.ByteData.Length; status = handlerStatus.awaitingStart; return; } if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) { Logger.Instance.Write(getPidID() + "Table: 0x" + table.ToString("X")); } packetIndex++; if (packetIndex == siPacket.ByteData.Length) { status = handlerStatus.awaitingLengthByte1; return; } lengthByte1 = siPacket.ByteData[packetIndex]; if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) { Logger.Instance.Write(getPidID() + "Length byte 1: " + lengthByte1); } packetIndex++; if (packetIndex == siPacket.ByteData.Length) { status = handlerStatus.awaitingLengthByte2; return; } lengthByte2 = siPacket.ByteData[packetIndex]; packetIndex++; if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) { Logger.Instance.Write(getPidID() + "Length byte 2: " + lengthByte2); } length = ((lengthByte1 & 0x0f) * 256) + lengthByte2; if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) { Logger.Instance.Write(getPidID() + "Table length: " + length); } createMpeg2Section(sections); }
/// <summary> /// Process a sample. /// </summary> /// <param name="byteData">The sample data.</param> /// <param name="transportPacket">The transport packet header.</param> public void Process(byte[] byteData, TransportPacket transportPacket) { if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) { Logger.Instance.Dump(getPidID() + "Data Block", byteData, byteData.Length); Logger.Instance.Write(getPidID() + "Status: " + status); } if (continuityCount == -1) { continuityCount = transportPacket.ContinuityCount; } else { continuityCount++; if (continuityCount > 15) { continuityCount = 0; } if (transportPacket.ContinuityCount != continuityCount) { if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) { Logger.Instance.Write(getPidID() + "Continuity count failure: " + continuityCount + ":" + transportPacket.ContinuityCount); } continuityCount = transportPacket.ContinuityCount; status = handlerStatus.awaitingStart; discontinuities++; } } if (empty) { sections.Clear(); empty = false; } bool done = false; siPacket = new SIPacket(); siPacket.Process(byteData, transportPacket); if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLERSI")) { Logger.Instance.Write(getPidID() + "Created new SI packet"); } switch (status) { case handlerStatus.awaitingStart: if (!transportPacket.StartIndicator) { if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) { Logger.Instance.Write(getPidID() + "Not start block"); } return; } packetIndex = siPacket.DataIndex; while (packetIndex < siPacket.ByteData.Length) { processNewSection(byteData, transportPacket, sections); } break; case handlerStatus.awaitingLengthByte1: packetIndex = siPacket.Index; lengthByte1 = siPacket.ByteData[packetIndex]; if (packetIndex == siPacket.ByteData.Length - 1) { status = handlerStatus.awaitingLengthByte2; return; } packetIndex++; lengthByte2 = siPacket.ByteData[packetIndex]; packetIndex++; length = ((lengthByte1 & 0x0f) * 256) + lengthByte2; done = createMpeg2Section(sections); if (!done) { return; } status = handlerStatus.awaitingStart; while (packetIndex < siPacket.ByteData.Length) { processNewSection(byteData, transportPacket, sections); } break; case handlerStatus.awaitingLengthByte2: packetIndex = siPacket.Index; lengthByte2 = siPacket.ByteData[packetIndex]; packetIndex++; length = ((lengthByte1 & 0x0f) * 256) + lengthByte2; done = createMpeg2Section(sections); if (!done) { return; } status = handlerStatus.awaitingStart; while (packetIndex < siPacket.ByteData.Length) { processNewSection(byteData, transportPacket, sections); } break; case handlerStatus.awaitingMoreData: packetIndex = siPacket.Index; for (; sectionIndex < length + 3; sectionIndex++) { if (packetIndex == siPacket.ByteData.Length) { if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) { Logger.Instance.Write(getPidID() + "Need more data"); } status = handlerStatus.awaitingMoreData; return; } mpeg2Section.Data[sectionIndex] = siPacket.ByteData[packetIndex]; packetIndex++; } if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) { Logger.Instance.Write(getPidID() + "Got table"); } if ((mpeg2Section.Data[1] & 0x80) != 0) { bool checkCRC = mpeg2Section.CheckCRC(); if (!checkCRC) { if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) { Logger.Instance.Write(getPidID() + "CRC failed"); } } else { sections.Add(mpeg2Section); if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) { Logger.Instance.Dump(getPidID() + "MPEG2 Section", mpeg2Section.Data, mpeg2Section.Data.Length); } } } else { sections.Add(mpeg2Section); if (RunParameters.Instance.TraceIDs.Contains("PIDHANDLER")) { Logger.Instance.Dump(getPidID() + "MPEG2 Section", mpeg2Section.Data, mpeg2Section.Data.Length); } } status = handlerStatus.awaitingStart; while (packetIndex < siPacket.ByteData.Length) { processNewSection(byteData, transportPacket, sections); } break; default: break; } siPacket = null; }
private SIPacket getPacket(bool needPayloadStart) { if (eof) { return(null); } SIPacket siPacket = null; bool done = false; while (!done) { if (fileStream == null) { fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); } byte[] buffer = new byte[188]; int readCount = fileStream.Read(buffer, 0, 188); if (readCount < 188) { fileStream.Close(); eof = true; return(null); } blockCount++; if (RunParameters.Instance.TraceIDs.Contains("TSFILEPACKETS")) { Logger.Instance.Dump("File Packet " + blockCount, buffer, buffer.Length); } TransportPacket transportPacket = new TransportPacket(); try { transportPacket.Process(buffer); if (!transportPacket.ErrorIndicator && !transportPacket.IsNullPacket) { if (pids == null || (pids != null && pids.Contains((short)transportPacket.PID))) { if (RunParameters.Instance.TraceIDs.Contains("TSPIDPACKETS")) { Logger.Instance.Dump("File Packet", buffer, buffer.Length); } if (needPayloadStart) { if (transportPacket.StartIndicator) { continuityCount = transportPacket.ContinuityCount; done = true; } } else { if (continuityCount == 15) { continuityCount = 0; } else { continuityCount++; } if (transportPacket.ContinuityCount == continuityCount) { done = true; } else { if (RunParameters.Instance.TraceIDs.Contains("CONTINUITYERRORS")) { Logger.Instance.Write("Continuity error: expected " + continuityCount + " got " + transportPacket.ContinuityCount); } needPayloadStart = true; return(null); } } if (done) { siPacket = new SIPacket(); siPacket.Process(buffer, transportPacket); } } } } catch (ArgumentOutOfRangeException) { /*Logger.Instance.Write("Failed to parse transport packet");*/ } } return(siPacket); }
private void getSection(BackgroundParameters parameters) { if (siPacket == null) { siPacket = getPacket(true); if (siPacket == null) { Thread.Sleep(500); return; } packetIndex = siPacket.DataIndex; } if (packetIndex > siPacket.ByteData.Length - 1) { siPacket = null; return; } byte table = siPacket.ByteData[packetIndex]; if (table == 0xff) { siPacket = null; return; } if (packetIndex == siPacket.ByteData.Length - 1) { siPacket = getPacket(false); if (siPacket == null) { Thread.Sleep(500); return; } packetIndex = siPacket.Index; } else packetIndex++; byte lengthByte1 = siPacket.ByteData[packetIndex]; if (packetIndex == siPacket.ByteData.Length - 1) { siPacket = getPacket(false); if (siPacket == null) { Thread.Sleep(500); return; } packetIndex = siPacket.Index; } else packetIndex++; byte lengthByte2 = siPacket.ByteData[packetIndex]; packetIndex++; int length = ((lengthByte1 & 0x0f) * 256) + lengthByte2; if (length < 2) { packetIndex += length; return; } if (tables != null && !tables.Contains(table)) { packetIndex += length; return; } mpeg2Section = new Mpeg2Section(); mpeg2Section.Data = new byte[length + 3]; mpeg2Section.Length = length + 3; mpeg2Section.Data[0] = table; mpeg2Section.Data[1] = lengthByte1; mpeg2Section.Data[2] = lengthByte2; for (int index = 3; index < length + 3; index++) { if (packetIndex == siPacket.ByteData.Length) { siPacket = getPacket(false); if (siPacket == null) { Thread.Sleep(500); return; } packetIndex = siPacket.Index; } mpeg2Section.Data[index] = siPacket.ByteData[packetIndex]; packetIndex++; } if ((mpeg2Section.Data[1] & 0x80) == 1) { bool checkCRC = mpeg2Section.CheckCRC(); if (!checkCRC) return; } Lock("GetSection"); if (Sections.Count < parameters.MaxSections) Sections.Add(mpeg2Section); Release("GetSection"); }
private SIPacket getPacket(bool needPayloadStart) { if (eof) return (null); SIPacket siPacket = null; bool done = false; while (!done) { if (fileStream == null) fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); byte[] buffer = new byte[188]; int readCount = fileStream.Read(buffer, 0, 188); if (readCount < 188) { fileStream.Close(); eof = true; return (null); } blockCount++; if (RunParameters.Instance.TraceIDs.Contains("TSFILEPACKETS")) Logger.Instance.Dump("File Packet " + blockCount, buffer, buffer.Length); TransportPacket transportPacket = new TransportPacket(); try { transportPacket.Process(buffer); if (!transportPacket.ErrorIndicator && !transportPacket.IsNullPacket) { if (pids == null || (pids != null && pids.Contains((short)transportPacket.PID))) { if (RunParameters.Instance.TraceIDs.Contains("TSPIDPACKETS")) Logger.Instance.Dump("File Packet", buffer, buffer.Length); if (needPayloadStart) { if (transportPacket.StartIndicator) { continuityCount = transportPacket.ContinuityCount; done = true; } } else { if (continuityCount == 15) continuityCount = 0; else continuityCount++; if (transportPacket.ContinuityCount == continuityCount) done = true; else { if (RunParameters.Instance.TraceIDs.Contains("CONTINUITYERRORS")) Logger.Instance.Write("Continuity error: expected " + continuityCount + " got " + transportPacket.ContinuityCount); needPayloadStart = true; return (null); } } if (done) { siPacket = new SIPacket(); siPacket.Process(buffer, transportPacket); } } } } catch (ArgumentOutOfRangeException) { /*Logger.Instance.Write("Failed to parse transport packet");*/ } } return (siPacket); }
private SIPacket getPacket(bool needPayloadStart) { if (eof) return (null); SIPacket siPacket = null; bool done = false; while (!done) { if (memoryHandle == 0) { bool reply = getMappedMemory(); if (!reply) { eof = true; return(null); } } if (currentOffset > maxOffset) return (null); byte[] buffer = new byte[188]; for (int index = 0; index < 188; index++) buffer[index] = this.buffer[currentPointer + index]; currentPointer += 188; currentOffset += 188; TransportPacket transportPacket = new TransportPacket(); transportPacket.Process(buffer); if (transportPacket.PID == pid) { Logger.Instance.Dump("File Packet", buffer, buffer.Length); if (!transportPacket.ErrorIndicator && !transportPacket.IsNullPacket) { if (needPayloadStart) { if (transportPacket.StartIndicator) done = true; } else done = true; if (done) { siPacket = new SIPacket(); siPacket.Process(buffer, transportPacket); } } } } return (siPacket); }
/// <summary> /// Process a transport packet. /// </summary> /// <param name="buffer">The buffer containing the transport packet.</param> /// <param name="packet">The transport packet.</param> public void ProcessPacket(byte[] buffer, TransportPacket packet) { if (packet.ErrorIndicator || packet.IsNullPacket) return; if (!packet.StartIndicator) return; SIPacket siPacket = new SIPacket(); siPacket.Process(buffer, packet); if (siPacket.DataIndex < siPacket.ByteData.Length) addTable((int)siPacket.ByteData[siPacket.DataIndex]); }