Debian Codegraph @

src/lib/tsreader/PatParser.cpp

raw
1 /*
2 * Copyright (C) 2006 Team MediaPortal
3 * http://www.team-mediaportal.com
4 *
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
9 *
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with GNU Make; see the file COPYING. If not, write to
17 * the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
18 * MA 02110-1335 USA
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 */
22
23 #include "os-dependent.h"
24 #include <stdint.h>
25 #include <kodi/General.h> //for kodi::Log
26 #include <time.h>
27 #include "PatParser.h"
28 #include "TSHeader.h"
29
30 namespace MPTV
31 {
32 CPatParser::CPatParser(void)
33 {
34 m_packetsToSkip = 0;
35 m_packetsReceived = 0;
36 m_pCallback = NULL;
37 Reset();
38 SetPid(0);
39 m_iState = Idle;
40 }
41
42 CPatParser::~CPatParser(void)
43 {
44 CleanUp();
45 }
46
47 void CPatParser::SetCallBack(IPatParserCallback* callback)
48 {
49 m_pCallback = callback;
50 }
51
52 void CPatParser::CleanUp()
53 {
54 for (size_t i = 0; i < m_pmtParsers.size(); ++i)
55 {
56 CPmtParser* parser = m_pmtParsers[i];
57 delete parser;
58 }
59 m_pmtParsers.clear();
60 m_iPatTableVersion = -1;
61 }
62
63 void CPatParser::Reset()
64 {
65 // Dump();
66 kodi::Log(ADDON_LOG_DEBUG, "PatParser:Reset()");
67 CSectionDecoder::Reset();
68 CleanUp();
69 m_packetsReceived = 0;
70 m_iPatTableVersion = -1;
71 m_iState = Parsing;
72 }
73
74
75 size_t CPatParser::Count()
76 {
77 size_t count = m_pmtParsers.size();
78
79 if (count == 0)
80 return 0;
81
82 for (size_t i = 0; i < m_pmtParsers.size(); ++i)
83 {
84 CPmtParser* parser = m_pmtParsers[i];
85 if (true == parser->IsReady())
86 {
87 return count;
88 }
89 }
90 return 0;
91 }
92
93 bool CPatParser::GetChannel(size_t index, CChannelInfo& info)
94 {
95 static CChannelInfo unknownChannel;
96
97 if (index > Count())
98 {
99 return false;
100 }
101
102 CPmtParser* parser = m_pmtParsers[index];
103
104 if (false == parser->IsReady())
105 {
106 return false;
107 }
108
109 info.PidTable = parser->GetPidInfo();
110 m_iState = Idle;
111 return true;
112 }
113
114 void CPatParser::SkipPacketsAtStart(int64_t packets)
115 {
116 m_packetsToSkip = packets;
117 m_packetsReceived = 0;
118 }
119
120 void CPatParser::OnTsPacket(byte* tsPacket)
121 {
122 m_packetsReceived++;
123
124 if (m_packetsReceived > m_packetsToSkip)
125 {
126 for (size_t i = 0; i < m_pmtParsers.size(); ++i)
127 {
128 CPmtParser* parser = m_pmtParsers[i];
129 parser->OnTsPacket(tsPacket);
130 }
131 CSectionDecoder::OnTsPacket(tsPacket);
132 }
133
134 if (m_iState == Parsing && m_pCallback != NULL)
135 {
136 for (size_t i = 0; i < m_pmtParsers.size(); ++i)
137 {
138 CPmtParser* parser = m_pmtParsers[i];
139 if (true == parser->IsReady())
140 {
141 CChannelInfo info;
142 if (GetChannel(i, info))
143 {
144 m_iState = Idle;
145
146 info.PatVersion = m_iPatTableVersion;
147 m_pCallback->OnNewChannel(info);
148 m_iState = Parsing;
149 return;
150 }
151 }
152 }
153 }
154 }
155
156 void CPatParser::OnNewSection(CSection& section)
157 {
158 if (section.table_id != 0)
159 return;
160
161 try
162 {
163 //int section_syntax_indicator = (section.Data[1]>>7) & 1;
164 //int transport_stream_id = section.table_id_extension;
165
166 if (section.version_number != m_iPatTableVersion)
167 {
168 kodi::Log(ADDON_LOG_DEBUG, "PatParser: new pat table %d->%d", m_iPatTableVersion, section.version_number); //was commented out
169 CleanUp();
170 m_iPatTableVersion = section.version_number;
171 m_iState = Parsing;
172 }
173 //kodi::Log(ADDON_LOG_DEBUG, "DecodePat %d section:%d lastsection:%d sectionlen:%d",
174 // version_number,section_number,last_section_number,section_length);
175
176 int loop = (section.section_length - 9) / 4;
177 for (int i = 0; i < loop; i++)
178 {
179 int offset = (8 + (i * 4));
180 int pmtPid = ((section.Data[offset + 2] & 0x1F) << 8) + section.Data[offset + 3];
181 if (pmtPid < 0x10 || pmtPid >= 0x1fff)
182 {
183 //invalid pmt pid
184 return;
185 }
186
187 bool found = false;
188 for (size_t idx = 0; idx < m_pmtParsers.size(); idx++)
189 {
190 CPmtParser* pmtParser = m_pmtParsers[idx];
191 if (pmtParser->GetPid() == pmtPid)
192 {
193 found = true;
194 break;
195 }
196 }
197 if (!found && pmtPid >= 0x10)
198 {
199 CPmtParser* pmtParser = new CPmtParser();
200 pmtParser->SetPid(pmtPid);
201 //pmtParser->SetPmtCallBack(this);
202 m_pmtParsers.push_back(pmtParser);
203 kodi::Log(ADDON_LOG_DEBUG, "PatParser: add pmt# %u pid: %x", (unsigned int) m_pmtParsers.size(), pmtPid);
204 }
205 }
206 }
207 catch (...)
208 {
209 kodi::Log(ADDON_LOG_DEBUG, "Exception in PatParser");
210 }
211 }
212
213 void CPatParser::Dump()
214 {
215 for (size_t i = 0; i < Count(); ++i)
216 {
217 CChannelInfo info;
218 if (GetChannel(i, info))
219 {
220 kodi::Log(ADDON_LOG_DEBUG, "%u) onid:%x tsid:%x sid:%x major:%d minor:%x freq:%x type:%d provider:%s service:%s", (unsigned int) i,
221 info.NetworkId, info.TransportId, info.ServiceId, info.MajorChannel, info.MinorChannel, info.Frequency, info.ServiceType, info.ProviderName, info.ServiceName);
222 info.PidTable.LogPIDs();
223 }
224 else
225 {
226 kodi::Log(ADDON_LOG_DEBUG, "%u) not found", (unsigned int) i);
227 }
228 }
229 }
230 }
231