public void AddScheduledEventItem(int serviceId, EventItem eventItem) { if (!ScheduledEvents.ContainsKey(serviceId)) { ScheduledEvents[serviceId] = new List <EventItem>(); } foreach (var item in ScheduledEvents[serviceId]) { if (item.EventId == eventItem.EventId) { return; } } ScheduledEvents[serviceId].Add(eventItem); }
public override void Parse(List <byte> bytes) { if (bytes == null || bytes.Count < 5) { return; } var pointerFiled = bytes[0]; var pos = 1; if (pointerFiled != 0) { pos = pos + pointerFiled + 1; } if (bytes.Count < pos + 2) { return; } ID = bytes[pos]; SectionSyntaxIndicator = ((bytes[pos + 1] & 128) == 128); Private = ((bytes[pos + 1] & 64) == 64); Reserved = Convert.ToByte((bytes[pos + 1] & 48) >> 4); SectionLength = Convert.ToInt32(((bytes[pos + 1] & 15) << 8) + bytes[pos + 2]); Data = new byte[SectionLength]; CRC = new byte[4]; //if (!DVBTTable.CRCIsValid(1,1)) //{ // return null; //} if (bytes.Count < SectionLength + 4) { throw new IndexOutOfRangeException(); } bytes.CopyTo(0, Data, 0, SectionLength); bytes.CopyTo(SectionLength, CRC, 0, 4); pos = pos + 3; ServiceId = (bytes[pos + 0] << 8) + bytes[pos + 1]; Version = Convert.ToByte((bytes[pos + 2] & 62) >> 1); CurrentIndicator = (bytes[pos + 2] & 1) == 1; SectionNumber = bytes[pos + 3]; LastSectionNumber = bytes[pos + 4]; pos = pos + 5; TransportStreamID = (bytes[pos + 0] << 8) + bytes[pos + 1]; OriginalNetworkID = (bytes[pos + 2] << 8) + bytes[pos + 3]; pos = pos + 4; SegmentLastSectionNumber = bytes[pos + 0]; LastTableID = bytes[pos + 1]; pos = pos + 2; // pointer + table id + sect.length + descriptors - crc var posAfterDescriptors = 4 + SectionLength - 4; // reading descriptors while (pos < posAfterDescriptors) { var eventId = (bytes[pos + 0] << 8) + bytes[pos + 1]; pos = pos + 2; var startTime = ParseTime(bytes, pos); pos = pos + 5; var duration = ParseDuration(bytes, pos); var finishTime = startTime.AddSeconds(duration); pos = pos + 3; var running_status = (bytes[pos + 0] & 224) >> 5; var freeCAMode = (bytes[pos + 0] & 16) >> 4; var descriptorLength = ((bytes[pos + 0] & 15) << 8) + bytes[pos + 1]; pos = pos + 2; var descriptorData = new byte[descriptorLength]; bytes.CopyTo(pos, descriptorData, 0, descriptorLength); var descriptorTag = descriptorData[0]; if (descriptorTag == 77) { var shortDescriptor = ShortEventDescriptor.Parse(descriptorData); var eventItem = EventItem.Create(eventId, ServiceId, startTime, finishTime, shortDescriptor); EventItems.Add(eventItem); } else { // TODO: read other descriptors } pos = pos + descriptorLength; } }
/// <summary> /// Scheduled events supplemented with actual events /// </summary> /// <param name="date"></param> /// <param name="count">only 1 or 2 supporting</param> /// <returns>Key: Program MapPID</returns> public Dictionary <int, List <EventItem> > GetEvents(DateTime date, int count = 2) { var res = new Dictionary <int, List <EventItem> >(); // current events: foreach (var kvp in CurrentEvents) { if (kvp.Value.StartTime <= date && kvp.Value.FinishTime >= date && ProgramNumberToMapPID.ContainsKey(kvp.Key)) { var programMapPID = ProgramNumberToMapPID[kvp.Key]; res[programMapPID] = new List <EventItem>(); res[programMapPID].Add(kvp.Value); } } // scheduled events foreach (var serviceId in ScheduledEvents.Keys) { if (!ProgramNumberToMapPID.ContainsKey(serviceId)) { continue; } var programMapPID = ProgramNumberToMapPID[serviceId]; if (!res.ContainsKey(programMapPID)) { res[programMapPID] = new List <EventItem>(); } EventItem currentEvent = null; foreach (var ev in ScheduledEvents[serviceId]) { if (ev.StartTime <= date && ev.FinishTime >= date) { // actual running event found if (res[programMapPID].Count == 0) { // cuurrent event not added res[programMapPID].Add(ev); currentEvent = ev; } else { currentEvent = res[programMapPID][0]; } if (count == 1) { break; // second event not wanted } } if (currentEvent != null && currentEvent.FinishTime == ev.StartTime) { // found second event res[programMapPID].Add(ev); break; } } } return(res); }