Пример #1
0
        public async Task <SearchMapPIDsResult> SearchProgramMapPIDs(bool tunePID0and17 = true)
        {
            _log.Debug($"Searching Program Map PIDs");

            var res = new SearchMapPIDsResult();

            try
            {
                if (tunePID0and17)
                {
                    // setting PID filter

                    var pids = new List <long>()
                    {
                        0, 16, 17
                    };
                    var pidRes = await SetPIDs(pids);

                    if (!pidRes.SuccessFlag)
                    {
                        res.Result = SearchProgramResultEnum.Error;
                        return(res);
                    }
                }

                StartReadBuffer();

                var timeoutForReadingBuffer = 15; //  15 secs
                var startTime = DateTime.Now;

                SDTTable sdtTable = null;
                PSITable psiTable = null;
                Dictionary <ServiceDescriptor, long> serviceDescriptors = null;

                while ((DateTime.Now - startTime).TotalSeconds < timeoutForReadingBuffer)
                {
                    // searching for PID 0 (PSI) and 17 (SDT) packets ..

                    var packets = MPEGTransportStreamPacket.Parse(Buffer);

                    sdtTable = DVBTTable.CreateFromPackets <SDTTable>(packets, 17);
                    psiTable = DVBTTable.CreateFromPackets <PSITable>(packets, 0);

                    if (sdtTable != null && psiTable != null)
                    {
                        // does SDT table belongs to this frequency?
                        serviceDescriptors = MPEGTransportStreamPacket.GetAvailableServicesMapPIDs(sdtTable, psiTable);

                        if (serviceDescriptors.Count > 0)
                        {
                            break;
                        }
                        else
                        {
                            _log.Debug($"Wrong SDTTable in buffer!");
                            ClearReadBuffer();
                        }
                    }

                    await Task.Delay(500);
                }

                StopReadBuffer();

                // debug save buffer
                //SaveBuffer($"Buffer.{_lastTunedFreq}", Buffer.ToArray());

                if (sdtTable == null || psiTable == null)
                {
                    _log.Debug($"No SDT or PSI table found");

                    res.Result = SearchProgramResultEnum.Error;
                    return(res);
                }


                res.ServiceDescriptors = serviceDescriptors;
                res.Result             = SearchProgramResultEnum.OK;

                _log.Debug($"SDT descriptors:");
                foreach (var sdt in sdtTable.ServiceDescriptors)
                {
                    _log.Debug($"Name: {sdt.ProviderName}, provider: {sdt.ProviderName}, number: {sdt.ProgramNumber}");
                }

                _log.Debug($"PSI association:");
                foreach (var pr in psiTable.ProgramAssociations)
                {
                    _log.Debug($"MapPID: {pr.ProgramMapPID}, number: {pr.ProgramNumber}");
                }

                _log.Debug($"Searching Program Map PIDS response: {res.Result}");

                return(res);
            } catch (Exception ex)
            {
                _log.Error(ex);

                res.Result = SearchProgramResultEnum.Error;
                return(res);
            }
        }
Пример #2
0
        public async Task <SearchAllPIDsResult> SearchProgramPIDs(List <long> MapPIDs)
        {
            var PIDsAsString = String.Join(",", MapPIDs);

            _log.Debug($"Searching PIDS of Map PIDs: {PIDsAsString}");

            var res = new SearchAllPIDsResult();

            try
            {
                // setting PIDs filter

                var pidRes = await SetPIDs(MapPIDs);

                if (!pidRes.SuccessFlag)
                {
                    _log.Debug($"Setting PIDs {PIDsAsString} failed");
                    res.Result = SearchProgramResultEnum.Error;
                    return(res);
                }

                // getting status

                var status = await GetStatus();

                if (!status.SuccessFlag)
                {
                    _log.Debug($"Getting status failed");
                    res.Result = SearchProgramResultEnum.Error;
                    return(res);
                }

                if (status.hasSignal != 1 || status.hasSync != 1 || status.hasLock != 1)
                {
                    _log.Debug($"No signal");
                    res.Result = SearchProgramResultEnum.NoSignal;
                    return(res);
                }

                var pmtTables = new Dictionary <long, PMTTable>();

                try
                {
                    StartReadBuffer();

                    // waiting
                    await Task.Delay(1000);

                    var timeoutForReadingBuffer = 15; //  seconds timeout for getting PMT
                    var startTime = DateTime.Now;

                    while ((DateTime.Now - startTime).TotalSeconds < timeoutForReadingBuffer)
                    {
                        var allPackets = MPEGTransportStreamPacket.Parse(Buffer);

                        foreach (var mapPID in MapPIDs)
                        {
                            var tbl = DVBTTable.CreateFromPackets <PMTTable>(allPackets, mapPID);
                            if (tbl != null)
                            {
                                pmtTables[mapPID] = tbl;
                            }
                        }

                        if (pmtTables.Count == MapPIDs.Count)
                        {
                            break;
                        }

                        await Task.Delay(500);
                    }
                }
                finally
                {
                    StopReadBuffer();
                }

                if (pmtTables.Count == 0)
                {
                    _log.Debug($"No PMT found");
                    res.Result = SearchProgramResultEnum.Error;
                    return(res);
                }

                //SaveBuffer($"ProgramPID.{MapPID.ToString()}", pmtPacketBytes.ToArray());

                res.Result = SearchProgramResultEnum.OK;

                foreach (var kvp in pmtTables)
                {
                    res.PIDs[kvp.Key] = new List <long>();

                    foreach (var stream in kvp.Value.Streams)
                    {
                        res.PIDs[kvp.Key].Add(stream.PID);
                    }
                }

                _log.Debug($"Searching PIDS response: {res}");

                return(res);
            }
            catch (Exception ex)
            {
                _log.Error(ex);
                res.Result = SearchProgramResultEnum.Error;
                return(res);
            }
        }
Пример #3
0
        public static void AnalyzeMPEGTSPackets(string path, bool includeEIT = true)
        {
            var logger = new FileLoggingService(LoggingLevelEnum.Debug);

            logger.LogFilename = "Log.log";

            Console.Write($"Reading ....... ");

            var bytes   = LoadBytesFromFile(path);
            var packets = MPEGTransportStreamPacket.Parse(bytes);

            Console.WriteLine($" {packets.Count} packets found");

            var packetsByPID = new SortedDictionary <long, List <MPEGTransportStreamPacket> >();

            foreach (var packet in packets)
            {
                if (!packetsByPID.ContainsKey(packet.PID))
                {
                    packetsByPID.Add(packet.PID, new List <MPEGTransportStreamPacket>());
                }

                packetsByPID[packet.PID].Add(packet);
            }

            Console.WriteLine();
            Console.WriteLine($"PID:             Packets count");
            Console.WriteLine("-------------------------------");

            SDTTable sDTTable = null;
            PSITable psiTable = null;

            foreach (var kvp in packetsByPID)
            {
                Console.WriteLine($"{kvp.Key,6} ({"0x" + Convert.ToString(kvp.Key, 16),6}): {kvp.Value.Count,8}");
            }

            if (packetsByPID.ContainsKey(17))
            {
                Console.WriteLine();
                Console.WriteLine($"Service Description Table(SDT):");
                Console.WriteLine($"------------------------------");

                sDTTable = DVBTTable.CreateFromPackets <SDTTable>(packetsByPID[17], 17);  // PID 0x11, Service Description Table (SDT)

                if (sDTTable != null)
                {
                    sDTTable.WriteToConsole();
                }
            }

            if (packetsByPID.ContainsKey(16))
            {
                Console.WriteLine();
                Console.WriteLine($"Network Information Table (NIT):");
                Console.WriteLine($"--------------------------------");


                var niTable = DVBTTable.CreateFromPackets <NITTable>(packetsByPID[16], 16);

                if (niTable != null)
                {
                    niTable.WriteToConsole();
                }
            }

            if (packetsByPID.ContainsKey(0))
            {
                Console.WriteLine();
                Console.WriteLine($"Program Specific Information(PSI):");
                Console.WriteLine($"----------------------------------");

                psiTable = DVBTTable.CreateFromPackets <PSITable>(packetsByPID[0], 0);

                if (psiTable != null)
                {
                    psiTable.WriteToConsole();
                }
            }

            if ((psiTable != null) &&
                (sDTTable != null))
            {
                Console.WriteLine();
                Console.WriteLine($"Program Map Table (PMT):");
                Console.WriteLine($"----------------------------------");
                Console.WriteLine();

                var servicesMapPIDs = MPEGTransportStreamPacket.GetAvailableServicesMapPIDs(sDTTable, psiTable);

                Console.WriteLine($"{"Program name".PadRight(40,' '),40} {"Program number",14} {"     PID",8}");
                Console.WriteLine($"{"------------".PadRight(40,' '),40} {"--------------",14} {"--------"}");

                // scan PMT for each program number
                foreach (var kvp in servicesMapPIDs)
                {
                    Console.WriteLine($"{kvp.Key.ServiceName.PadRight(40, ' ')} {kvp.Key.ProgramNumber,14} {kvp.Value,8}");

                    if (packetsByPID.ContainsKey(Convert.ToInt32(kvp.Value)))
                    {
                        // stream contains this Map PID

                        if (packetsByPID.ContainsKey(kvp.Value))
                        {
                            var mptPacket = DVBTTable.CreateFromPackets <PMTTable>(packetsByPID[kvp.Value], kvp.Value);
                            mptPacket.WriteToConsole();
                        }
                    }
                }
            }


            if (includeEIT)
            {
                if (packetsByPID.ContainsKey(18))
                {
                    Console.WriteLine();
                    Console.WriteLine($"Event Information Table (EIT):");
                    Console.WriteLine($"------------------------------");

                    var eitManager = new EITManager(logger);

                    var packetsEITwithSDT = new List <MPEGTransportStreamPacket>();
                    packetsEITwithSDT.AddRange(packetsByPID[18]);

                    if (packetsByPID.ContainsKey(0))
                    {
                        packetsEITwithSDT.AddRange(packetsByPID[0]);
                    }

                    var eitScanRes = eitManager.Scan(packetsEITwithSDT);

                    if (eitScanRes.UnsupportedEncoding)
                    {
                        Console.WriteLine();
                        Console.WriteLine("Unsupported encoding");
                        Console.WriteLine();
                    }
                    else
                    {
                        Console.WriteLine();
                        Console.WriteLine("Current events");
                        Console.WriteLine();

                        Console.WriteLine($"{"Program number",14} {"Date".PadRight(10, ' '),10} {"From "}-{" To  "} Text");
                        Console.WriteLine($"{"--------------",14} {"----".PadRight(10, '-'),10} {"-----"}-{"-----"} -------------------------------");

                        foreach (var kvp in eitManager.CurrentEvents)
                        {
                            Console.WriteLine(kvp.Value.WriteToString());
                        }


                        Console.WriteLine();
                        Console.WriteLine("Scheduled events");
                        Console.WriteLine();

                        foreach (var programNumber in eitManager.ScheduledEvents.Keys)
                        {
                            foreach (var ev in eitManager.ScheduledEvents[programNumber])
                            {
                                Console.WriteLine(ev.WriteToString());
                            }
                        }

                        Console.WriteLine();
                        Console.WriteLine("Present Events");
                        Console.WriteLine();

                        foreach (var kvp in eitManager.GetEvents(DateTime.Now))
                        {
                            Console.WriteLine($"Program Map PID: {kvp.Key}");

                            foreach (var ev in kvp.Value)
                            {
                                Console.WriteLine(ev.WriteToString());
                            }
                        }
                    }
                }
            }
        }