////////////////////////////////////////////////////////////////////////////////// // // // Copyright © 2005-2020 nzsjb // // // // This Program is free software; you can redistribute it and/or modify // // it under the terms of the GNU General Public License as published by // // the Free Software Foundation; either version 2, or (at your option) // // any later version. // // // // This Program is distributed in the hope that it will be useful, // // but WITHOUT ANY WARRANTY; without even the implied warranty of // // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // // GNU General Public License for more details. // // // // You should have received a copy of the GNU General Public License // // along with GNU Make; see the file COPYING. If not, write to // // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. // // http://www.gnu.org/copyleft/gpl.html // // // ////////////////////////////////////////////////////////////////////////////////// using System; using System.Collections.ObjectModel; using DomainObjects; namespace DVBServices { /// /// The class that describes Open TV title data. /// public class OpenTVTitleData { /// /// Get the event identification. /// public int EventID { get { return (eventID); } } /// /// Get the start time of the event. /// public DateTime StartTime { get { OpenTVTitleDataRecord record = (OpenTVTitleDataRecord)getRecord(OpenTVTitleDataRecord.TagValue); return (baseDate + record.StartTimeOffset); } } /// /// Get the duration of the event. /// public TimeSpan Duration { get { OpenTVTitleDataRecord record = (OpenTVTitleDataRecord)getRecord(OpenTVTitleDataRecord.TagValue); return (record.Duration); } } /// /// Get the theme identification of the event. /// public int CategoryID { get { OpenTVTitleDataRecord record = (OpenTVTitleDataRecord)getRecord(OpenTVTitleDataRecord.TagValue); return (record.CategoryID); } } /// /// Get the name of the event. /// public string EventName { get { OpenTVTitleDataRecord record = (OpenTVTitleDataRecord)getRecord(OpenTVTitleDataRecord.TagValue); return (record.DecodedEventName); } } /// /// Get the raw bytes of the event name. /// public byte[] EventNameBytes { get { OpenTVTitleDataRecord record = (OpenTVTitleDataRecord)getRecord(OpenTVTitleDataRecord.TagValue); return (record.EventName); } } /// /// Get the flags field of the event. /// public byte[] Flags { get { OpenTVTitleDataRecord record = (OpenTVTitleDataRecord)getRecord(OpenTVTitleDataRecord.TagValue); return (record.Flags); } } /// /// Get the collection of records for this title section. /// internal Collection Records { get { if (records == null) records = new Collection(); return (records); } } /// /// Get the collection of undefined records for this title section. /// internal Collection UndefinedRecords { get { if (records == null) return (null); Collection undefinedRecords = new Collection(); foreach (OpenTVRecordBase record in records) { if (record.IsUndefined) undefinedRecords.Add(record); } return (undefinedRecords); } } /// /// Return true if the entry is empty; false otherwise. /// public bool IsEmpty { get { return (records == null || records.Count == 0); } } /// /// Get the PID of the section containing the data. /// public int PID { get { return (pid); } } /// /// Get the table ID of the section containing the data. /// public int Table { get { return (table); } } /// /// Get the timestamp when the data arrived. /// public DateTime TimeStamp { get { return (timeStamp); } } /// /// Get the index of the next byte in the MPEG2 section following the title data. /// /// /// The title data has not been processed. /// public int Index { get { if (lastIndex == -1) throw (new InvalidOperationException("OpenTVTitleData: Index requested before block processed")); return (lastIndex); } } private int eventID; private int length; private DateTime baseDate; private int pid; private int table; private DateTime timeStamp; private Collection records; private int lastIndex = -1; /// /// Initialize a new instance of the OpenTVTitleData class. /// public OpenTVTitleData() { } /// /// Parse the title data. /// /// The MPEG2 section containing the title data. /// Index of the first byte of the title data in the MPEG2 section. /// The base date for the programs. /// The channel for the data. /// The PID of the section. /// The table ID of the section. internal void Process(byte[] byteData, int index, DateTime baseDate, int channel, int pid, int table) { lastIndex = index; this.pid = pid; this.table = table; this.baseDate = baseDate; try { eventID = Utils.Convert2BytesToInt(byteData, lastIndex); lastIndex += 2; length = ((byteData[lastIndex] & 0x0f) * 256) + byteData[lastIndex + 1]; lastIndex += 2; int recordLength = length; while (recordLength != 0) { OpenTVRecordBase record = OpenTVRecordBase.Instance(byteData, lastIndex); Records.Add(record); lastIndex += record.TotalLength; recordLength -= record.TotalLength; } timeStamp = DateTime.Now; Validate(); } catch (IndexOutOfRangeException) { throw (new ArgumentOutOfRangeException("lastIndex = " + lastIndex)); } } /// /// Validate the title data fields. /// /// /// A title data field is not valid. /// public void Validate() { } /// /// Log the title data fields. /// public void LogMessage() { if (Logger.ProtocolLogger == null) return; Logger.ProtocolLogger.Write(Logger.ProtocolIndent + "OPENTV TITLE DATA: Event ID: " + eventID + " Length: " + length); if (records != null) { Logger.IncrementProtocolIndent(); foreach (OpenTVRecordBase record in records) record.LogMessage(); Logger.DecrementProtocolIndent(); } } private OpenTVRecordBase getRecord(int tag) { if (records == null) return(null); foreach (OpenTVRecordBase record in records) { if (record.Tag == tag) return(record); } return(null); } } }