-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathProgramEntry.cs
More file actions
209 lines (182 loc) · 7.6 KB
/
ProgramEntry.cs
File metadata and controls
209 lines (182 loc) · 7.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System;
using System.Globalization;
using System.Collections.Generic;
using System.Linq;
namespace JMS.DVB.EPG
{
/// <summary>
/// A single entry in a <see cref="Tables.PMT"/>.
/// </summary>
public class ProgramEntry : EntryBase
{
/// <summary>
/// Maps ISO language names to their native representation.
/// </summary>
private static Dictionary<string, CultureInfo> m_CultureMap = new Dictionary<string, CultureInfo>(StringComparer.InvariantCultureIgnoreCase);
/// <summary>
/// The constructor is private to make this class static.
/// </summary>
static ProgramEntry()
{
// Load all
foreach (CultureInfo info in CultureInfo.GetCultures(CultureTypes.NeutralCultures))
{
// Remember
m_CultureMap[info.ThreeLetterISOLanguageName] = info;
}
// Add special entries (bibliographic - see ISO639-2)
AddBibliographicShortcut("bod", "tib");
AddBibliographicShortcut("ces", "cze");
AddBibliographicShortcut("cym", "wel");
AddBibliographicShortcut("deu", "ger");
AddBibliographicShortcut("ell", "gre");
AddBibliographicShortcut("eus", "baq");
AddBibliographicShortcut("fas", "per");
AddBibliographicShortcut("fra", "fre");
AddBibliographicShortcut("hrv", "scr");
AddBibliographicShortcut("hye", "arm");
AddBibliographicShortcut("isl", "ice");
AddBibliographicShortcut("kat", "geo");
AddBibliographicShortcut("mkd", "mac");
AddBibliographicShortcut("mri", "mao");
AddBibliographicShortcut("msa", "may");
AddBibliographicShortcut("mya", "bur");
AddBibliographicShortcut("nld", "dut");
AddBibliographicShortcut("ron", "rum");
AddBibliographicShortcut("slk", "slo");
AddBibliographicShortcut("sqi", "alb");
AddBibliographicShortcut("srp", "scc");
AddBibliographicShortcut("zho", "chi");
}
/// <summary>
/// Create an alternate language map entry.
/// </summary>
/// <param name="terminologyCode">Official three letter code.</param>
/// <param name="bibliographicCode">Alternat (bibliographic) three letter code.</param>
private static void AddBibliographicShortcut(string terminologyCode, string bibliographicCode)
{
// See if code already exists
if (m_CultureMap.ContainsKey(bibliographicCode))
return;
// Load entry
CultureInfo terminologic;
if (!m_CultureMap.TryGetValue(terminologyCode, out terminologic))
return;
// Connect
m_CultureMap[bibliographicCode] = terminologic;
}
/// <summary>
/// The <see cref="Descriptor"/> instances related to this program.
/// </summary>
/// <remarks>
/// Please refer to the original documentation to find out which descriptor
/// type is allowed in a <see cref="Tables.PMT"/> table.
/// </remarks>
public readonly Descriptor[] Descriptors;
/// <summary>
/// Set if the program entry is consistent.
/// </summary>
public readonly bool IsValid = false;
/// <summary>
/// The total length of the entry in bytes.
/// </summary>
public readonly int Length;
/// <summary>
/// The PID of this program entry.
/// </summary>
public ushort ElementaryPID;
/// <summary>
/// The type of this stream
/// </summary>
public StreamTypes StreamType;
/// <summary>
/// Create a new program instance.
/// </summary>
/// <param name="table">The related <see cref="Tables.PMT"/> table.</param>
/// <param name="offset">The first byte of this program in the <see cref="EPG.Table.Section"/>
/// for the related <see cref="Table"/>.</param>
/// <param name="length">The maximum number of bytes available. If this number
/// is greater than the <see cref="Length"/> of this program another event will
/// follow in the same table.</param>
internal ProgramEntry(Table table, int offset, int length)
: base(table)
{
// Access section
Section section = Section;
// Load
ElementaryPID = (ushort)(0x1fff & Tools.MergeBytesToWord(section[offset + 2], section[offset + 1]));
StreamType = (StreamTypes)section[offset + 0];
// Read the length
int descrLength = 0xfff & Tools.MergeBytesToWord(section[offset + 4], section[offset + 3]);
// Calculate the total length
Length = 5 + descrLength;
// Verify
if (Length > length)
return;
// Try to load descriptors
Descriptors = Descriptor.Load(this, offset + 5, descrLength);
// Can use it
IsValid = true;
}
/// <summary>
/// Create a new program instance.
/// </summary>
/// <param name="table">The related <see cref="Tables.PMT"/> table.</param>
/// <param name="offset">The first byte of this service in the <see cref="EPG.Table.Section"/>
/// for the related <see cref="Table"/>.</param>
/// <param name="length">The maximum number of bytes available. If this number
/// is greater than the <see cref="Length"/> of this program entry another entry will
/// follow in the same table.</param>
/// <returns>A new service instance or <i>null</i> if there are less than
/// 5 bytes available.</returns>
static internal ProgramEntry Create(Table table, int offset, int length)
{
// Validate
if (length < 5)
return null;
// Create
return new ProgramEntry(table, offset, length);
}
/// <summary>
/// Ermittelt zu einem Datenstrom den ISO Namen der Sprache.
/// </summary>
public string ProgrammeName
{
get
{
// Check all descriptors
foreach (Descriptor descriptor in Descriptors)
{
// Check type
Descriptors.ISOLanguage language = descriptor as Descriptors.ISOLanguage;
// None
if ((null == language) || (language.Languages.Count < 1))
continue;
// Remember the first one set
string name = language.Languages[0].ISOLanguage;
if (string.IsNullOrEmpty(name))
continue;
// Convert
return GetLanguageFromISOLanguage(name);
}
// Use default
return string.Format("#{0}", ElementaryPID);
}
}
/// <summary>
/// Ermittelt zu einer ISO Kurzbezeichnung einer Sprache die zugehörige
/// Sprache.
/// </summary>
/// <param name="language">Eine ISO Kurzbezeichnung.</param>
/// <returns>Der Name der Sprache, ausgerückt in der Sprache selbst.</returns>
public static string GetLanguageFromISOLanguage(string language)
{
// Find
CultureInfo cult;
if (m_CultureMap.TryGetValue(language, out cult))
return cult.NativeName;
// Report as is
return language;
}
}
}