Exemplo n.º 1
0
        public static List <T> CreateAllFromPackets <T>(List <MPEGTransportStreamPacket> packets, long PID, int tableId) where T : DVBTTable, new()
        {
            var res = new List <T>();

            var filteredPackets = MPEGTransportStreamPacket.GetAllPacketsPayloadBytesByPID(packets, PID);

            foreach (var kvp in filteredPackets)
            {
                var t = new T();

                try
                {
                    t.Parse(kvp.Value);

                    if (t.CRCIsValid())
                    {
                        if (t.ID == tableId)
                        {
                            res.Add(t);
                        }
                        else
                        {
                            // not SDT table
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }

            return(res);
        }
Exemplo n.º 2
0
        public EITScanResult Scan(List <byte> bytes)
        {
            if (bytes == null || bytes.Count == 0)
            {
                return(new EITScanResult()
                {
                    OK = false
                });
            }

            var packets = MPEGTransportStreamPacket.Parse(bytes);

            return(Scan(packets));
        }
Exemplo n.º 3
0
        public static T CreateFromPackets <T>(List <MPEGTransportStreamPacket> packets, long PID) where T : DVBTTable, new()
        {
            var filteredPackets = MPEGTransportStreamPacket.GetAllPacketsPayloadBytesByPID(packets, PID);

            foreach (var kvp in filteredPackets)
            {
                var t = new T();

                try
                {
                    t.Parse(kvp.Value);

                    if (t.CRCIsValid())
                    {
                        return(t);
                    }
                } catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }

            return(null);
        }
        public static List <MPEGTransportStreamPacket> Parse(List <byte> bytes, int PIDFilter = -1)
        {
            var pos = FindSyncBytePosition(bytes);

            var res = new List <MPEGTransportStreamPacket>();

            if (pos == -1)
            {
                return(res);
            }

            while (pos + 188 < bytes.Count)
            {
                var buff = new byte[188];
                for (var i = 0; i < 188; i++)
                {
                    buff[i] = bytes[pos + i];
                }

                var packet = new MPEGTransportStreamPacket();
                packet.Parse(buff);

                if (
                    (PIDFilter == -1)      // add all packets
                    ||
                    ((PIDFilter != -1) && (packet.PID == PIDFilter))
                    )
                {
                    res.Add(packet);
                }

                pos += 188;
            }

            return(res);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Scanning actual and scheduled events for actual TS
        /// </summary>
        /// <param name="packets"></param>
        public EITScanResult Scan(List <MPEGTransportStreamPacket> packets)
        {
            _log.Debug($"Scanning EIT from packets");

            var res = new EITScanResult()
            {
                OK = true
            };

            try
            {
                var eitData = MPEGTransportStreamPacket.GetAllPacketsPayloadBytesByPID(packets, 18);

                _log.Debug($"EIT packets count: {eitData.Count}");

                var eventIDs = new Dictionary <int, Dictionary <int, EventItem> >(); // ServiceID -> (event id -> event item )

                var currentEventsCountFound   = 0;
                var scheduledEventsCountFound = 0;

                foreach (var kvp in eitData)
                {
                    try
                    {
                        var eit = DVBTTable.Create <EITTable>(kvp.Value);

                        if (eit == null || !eit.CRCIsValid())
                        {
                            continue;
                        }

                        if (eit.ID == 78) // actual TS, present/following event information = table_id = 0x4E;
                        {
                            foreach (var item in eit.EventItems)
                            {
                                if (item.StartTime < DateTime.Now &&
                                    item.FinishTime > DateTime.Now)
                                {
                                    // reading only the actual event
                                    // there can be event that start in future!

                                    CurrentEvents[eit.ServiceId] = item;

                                    currentEventsCountFound++;

                                    break;
                                }
                            }
                        }
                        else
                        if (eit.ID >= 80 && eit.ID <= 95) // actual TS, event schedule information = table_id = 0x50 to 0x5F;
                        {
                            foreach (var item in eit.EventItems)
                            {
                                if (!eventIDs.ContainsKey(eit.ServiceId))
                                {
                                    eventIDs[eit.ServiceId] = new Dictionary <int, EventItem>();
                                }

                                var serviceItems = eventIDs[eit.ServiceId];

                                if (!serviceItems.ContainsKey(item.EventId))
                                {
                                    serviceItems.Add(item.EventId, item);

                                    scheduledEventsCountFound++;
                                }
                            }
                        }
                    }
                    catch (MPEGTSUnsupportedEncodingException)
                    {
                        res.UnsupportedEncoding = true;
                    }
                    catch (Exception ex)
                    {
                        // Bad data EIT data
                    }
                }

                _log.Debug($"Scheduled Events found: {scheduledEventsCountFound}");
                _log.Debug($"Current Events found: {currentEventsCountFound}");

                // transform to List and sorting:

                foreach (var kvp in eventIDs)
                {
                    foreach (var eventItem in kvp.Value)
                    {
                        AddScheduledEventItem(kvp.Key, eventItem.Value);
                    }
                    ScheduledEvents[kvp.Key].Sort();
                }

                var psiTable = DVBTTable.CreateFromPackets <PSITable>(packets, 0);
                if (psiTable != null && psiTable.ProgramAssociations != null)
                {
                    foreach (var kvp in psiTable.ProgramAssociations)
                    {
                        _log.Debug($"Associate  program number {kvp.ProgramNumber} to PID {kvp.ProgramMapPID}");
                        ProgramNumberToMapPID[kvp.ProgramNumber] = kvp.ProgramMapPID;
                    }
                }
                else
                {
                    _log.Debug($"No PSI table found");
                }
            } catch (Exception e)
            {
                _log.Error(e);
                res.OK = false;
            }

            return(res);
        }