Exemplo n.º 1
0
        /// <summary>
        /// Process the packet.
        /// </summary>
        /// <param name="byteData">The first byte of the packet.</param>
        /// <param name="transportPacket">The tranport packet containing this packet.</param>
        public void Process(byte[] byteData, TransportPacket transportPacket)
        {
            this.transportPacket = transportPacket;
            this.byteData        = byteData;
            lastIndex            = transportPacket.Index;

            try
            {
                if (transportPacket.StartIndicator)
                {
                    pointer = (int)byteData[lastIndex];
                    lastIndex++;
                }
                else
                {
                    pointer = 0;
                }
            }
            catch (IndexOutOfRangeException)
            {
                throw (new ArgumentOutOfRangeException("The Transport Packet is short"));
            }

            Validate();
        }
Exemplo n.º 2
0
        private Collection <Mpeg2Section> getNextSection()
        {
            if (!initialized)
            {
                initialize();
            }

            if (currentOffset >= maxOffset)
            {
                getMaxOffset();
                return(null);
            }

            byte[] buffer = new byte[188];
            Marshal.Copy(currentPointer, buffer, 0, 188);
            currentPointer = new IntPtr(currentPointer.ToInt64() + 188);
            currentOffset += 188;

            if (RunParameters.Instance.TraceIDs.Contains("TRANSPORTPACKETS"))
            {
                Logger.Instance.Dump("Transport Packet", buffer, buffer.Length);
            }

            TransportPacket transportPacket = new TransportPacket();

            try
            {
                transportPacket.Process(buffer);
            }
            catch (ArgumentOutOfRangeException)
            {
                Logger.Instance.Write("Transport packet parsing failed at current offset  " + currentOffset +
                                      " max offset: " + maxOffset);
                return(null);
            }

            if (transportPacket.IsNullPacket || transportPacket.ErrorIndicator)
            {
                return(null);
            }

            /*Logger.Instance.Write("Processing PID 0x" + transportPacket.PID.ToString("X") + " from offset " + currentOffset);*/
            PidHandler pidHandler = findPidHandler(transportPacket.PID);

            pidHandler.Process(buffer, transportPacket);

            /*Logger.Instance.Write("PID handler 0x" + pidHandler.Pid.ToString("X") + " has created " + pidHandler.Sections.Count + " sections");*/

            return(pidHandler.Sections);
        }
Exemplo n.º 3
0
        /// <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]);
            }
        }
Exemplo n.º 4
0
        /// <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;
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
        /// <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;
        }
Exemplo n.º 7
0
        private bool fillBuffer(FileStream fileStream)
        {
            if (pids == null)
            {
                Thread.Sleep(100);
                return (false);
            }

            if (size + 136 + packetSize > memorySize)
            {
                return (false);
            }

            byte[] buffer = new byte[packetSize];
            int readCount = getPacket(fileStream, buffer);
            if (readCount < packetSize)
                return (false);

            TransportPacket transportPacket = new TransportPacket();
            try
            {
                transportPacket.Process(buffer);
            }
            catch (ArgumentOutOfRangeException)
            {
                Logger.Instance.Write("Sync byte error");
                return (false);
            }

            if (RunParameters.Instance.TraceIDs.Contains("MPEG2PACKETS"))
            {
                if (pids[0] == -1 || pids.Contains(transportPacket.PID))
                    Logger.Instance.Dump("Simulation Packet for PID " + transportPacket.PID, buffer, buffer.Length);
            }

            if (transportPacket.ErrorIndicator)
                return (false);
            if (transportPacket.IsNullPacket)
                return (false);

            bool reply = resourceMutex.WaitOne(5000, true);

            if (pids[0] == -1 || pids.Contains(transportPacket.PID))
            {
                Marshal.Copy(buffer, 0, offset, buffer.Length);

                offset = new IntPtr(offset.ToInt64() + packetSize);
                size += packetSize;
                Marshal.WriteInt32(memoryBlock, size);
            }

            if (reply)
                resourceMutex.ReleaseMutex();

            return (false);
        }
        private bool fillBuffer(FileStream fileStream)
        {
            if (pids == null)
            {
                Thread.Sleep(100);
                return(false);
            }

            if (size + 136 + packetSize > memorySize)
            {
                return(false);
            }

            byte[] buffer    = new byte[packetSize];
            int    readCount = getPacket(fileStream, buffer);

            if (readCount < packetSize)
            {
                return(false);
            }

            TransportPacket transportPacket = new TransportPacket();

            try
            {
                transportPacket.Process(buffer);
            }
            catch (ArgumentOutOfRangeException)
            {
                Logger.Instance.Write("Sync byte error");
                return(false);
            }

            if (RunParameters.Instance.TraceIDs.Contains("MPEG2PACKETS"))
            {
                if (pids[0] == -1 || pids.Contains(transportPacket.PID))
                {
                    Logger.Instance.Dump("Simulation Packet for PID " + transportPacket.PID, buffer, buffer.Length);
                }
            }

            if (transportPacket.ErrorIndicator)
            {
                return(false);
            }
            if (transportPacket.IsNullPacket)
            {
                return(false);
            }

            bool reply = resourceMutex.WaitOne(5000, true);

            if (pids[0] == -1 || pids.Contains(transportPacket.PID))
            {
                Marshal.Copy(buffer, 0, offset, buffer.Length);

                offset = new IntPtr(offset.ToInt64() + packetSize);
                size  += packetSize;
                Marshal.WriteInt32(memoryBlock, size);
            }

            if (reply)
            {
                resourceMutex.ReleaseMutex();
            }

            return(false);
        }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
0
        private Collection<Mpeg2Section> getNextSection()
        {
            if (!initialized)
                initialize();

            if (currentOffset >= maxOffset)
            {
                getMaxOffset();
                return (null);
            }

            byte[] buffer = new byte[188];
            Marshal.Copy(currentPointer, buffer, 0, 188);
            currentPointer = new IntPtr(currentPointer.ToInt64() + 188);
            currentOffset += 188;

            if (RunParameters.Instance.TraceIDs.Contains("TRANSPORTPACKETS"))
                Logger.Instance.Dump("Transport Packet", buffer, buffer.Length);

            TransportPacket transportPacket = new TransportPacket();
            try
            {
                transportPacket.Process(buffer);
            }
            catch (ArgumentOutOfRangeException)
            {
                Logger.Instance.Write("Transport packet parsing failed at current offset  " + currentOffset +
                    " max offset: " + maxOffset);
                return (null);
            }

            if (transportPacket.IsNullPacket || transportPacket.ErrorIndicator)
                return (null);

            /*Logger.Instance.Write("Processing PID 0x" + transportPacket.PID.ToString("X") + " from offset " + currentOffset);*/
            PidHandler pidHandler = findPidHandler(transportPacket.PID);
            pidHandler.Process(buffer, transportPacket);

            /*Logger.Instance.Write("PID handler 0x" + pidHandler.Pid.ToString("X") + " has created " + pidHandler.Sections.Count + " sections");*/

            return (pidHandler.Sections);
        }
Exemplo n.º 11
0
        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 getData(ISampleDataProvider dataProvider, AnalysisParameters analysisParameters, BackgroundWorker worker)
        {
            Logger.Instance.Write("Starting analysis");

            analysisParameters.ScanningFrequency.CollectionType = CollectionType.MHEG5;
            FrequencyScanner frequencyScanner = new FrequencyScanner(dataProvider, worker);
            Collection<TVStation> stations = frequencyScanner.FindTVStations();

            pidList = new Collection<PidSpec>();

            dataProvider.ChangePidMapping(new int[] { -1 });

            IntPtr memoryPointer = dataProvider.BufferAddress;
            int currentOffset = 0;

            byte[] buffer = new byte[188];
            DateTime startTime = DateTime.Now;
            int packetCount = 0;
            int errorPackets = 0;
            int nullPackets = 0;

            while ((DateTime.Now - startTime).TotalSeconds < analysisParameters.DataCollectionTimeout && !worker.CancellationPending)
            {
                if (currentOffset >= dataProvider.BufferSpaceUsed)
                {
                    Thread.Sleep(2000);
                    if (currentOffset >= dataProvider.BufferSpaceUsed)
                    {
                        Logger.Instance.Write("Analysis resetting pid after " + packetCount + " packets (errors = " + errorPackets + " null = " + nullPackets + ")");
                        dataProvider.ChangePidMapping(new int[] { -1 });
                        currentOffset = 0;
                    }
                }
                else
                {
                    IntPtr currentPointer = new IntPtr(memoryPointer.ToInt64() + currentOffset + 136);
                    Marshal.Copy(currentPointer, buffer, 0, 188);
                    packetCount++;

                    /*if (dumpCount < 10000)
                    {
                        Logger.Instance.Dump("atsc " + dumpCount, buffer, buffer.Length);
                        dumpCount++;
                    }*/

                    TransportPacket transportPacket = new TransportPacket();

                    try
                    {
                        transportPacket.Process(buffer);

                        if (transportPacket.ErrorIndicator)
                            errorPackets++;
                        if (transportPacket.IsNullPacket)
                            nullPackets++;

                        if (!transportPacket.ErrorIndicator)
                        {
                            bool ignorePid = checkPid(transportPacket.PID, stations);
                            if (!ignorePid)
                            {
                                PidSpec pidSpec = findPidSpec(pidList, transportPacket.PID);
                                if (pidSpec == null)
                                {
                                    pidSpec = new PidSpec(transportPacket.PID);
                                    addPid(pidList, new PidSpec(transportPacket.PID));
                                }
                                pidSpec.ProcessPacket(buffer, transportPacket);
                            }
                        }
                        else
                            Logger.Instance.Write("Transport packet error in packet " + packetCount);

                    }
                    catch (ArgumentOutOfRangeException)
                    {
                        Logger.Instance.Write("Failed to parse packet " + packetCount);
                    }

                    currentOffset += buffer.Length;
                }
            }

            Logger.Instance.Write("Analysis completed: " + pidList.Count + " PID's loaded from " + packetCount + " packets");
        }
Exemplo n.º 13
0
        /// <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]);
        }
Exemplo n.º 14
0
        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);
        }
Exemplo n.º 15
0
        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);
        }
Exemplo n.º 16
0
        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);
        }
Exemplo n.º 17
0
        /// <summary>
        /// Process the packet.
        /// </summary>
        /// <param name="byteData">The first byte of the packet.</param>
        /// <param name="transportPacket">The tranport packet containing this packet.</param>
        public void Process(byte[] byteData, TransportPacket transportPacket)
        {
            this.transportPacket = transportPacket;
            this.byteData = byteData;
            lastIndex = transportPacket.Index;

            try
            {
                if (transportPacket.StartIndicator)
                {
                    pointer = (int)byteData[lastIndex];
                    lastIndex++;
                }
                else
                    pointer = 0;
            }
            catch (IndexOutOfRangeException)
            {
                throw (new ArgumentOutOfRangeException("The Transport Packet is short"));
            }

            Validate();
        }