| 1 |
/* |
| 2 |
* Copyright (C) 2006-2008 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 |
#include "os-dependent.h" |
| 23 |
#include <kodi/General.h> //for kodi::Log |
| 24 |
|
| 25 |
#include <stdio.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include "SectionDecoder.h" |
| 28 |
#include "TSHeader.h" |
| 29 |
#include "utils.h" |
| 30 |
|
| 31 |
namespace MPTV |
| 32 |
{ |
| 33 |
CSectionDecoder::CSectionDecoder(void) |
| 34 |
{ |
| 35 |
m_pid = -1; |
| 36 |
m_iContinuityCounter = 0; |
| 37 |
m_section.Reset(); |
| 38 |
m_pCallback = NULL; |
| 39 |
m_bLog = false; |
| 40 |
m_bCrcCheck = true; |
| 41 |
} |
| 42 |
|
| 43 |
CSectionDecoder::~CSectionDecoder(void) |
| 44 |
{ |
| 45 |
} |
| 46 |
|
| 47 |
void CSectionDecoder::EnableLogging(bool onOff) |
| 48 |
{ |
| 49 |
m_bLog = onOff; |
| 50 |
} |
| 51 |
void CSectionDecoder::SetCallBack(ISectionCallback* callback) |
| 52 |
{ |
| 53 |
m_pCallback = callback; |
| 54 |
} |
| 55 |
|
| 56 |
void CSectionDecoder::SetPid(int pid) |
| 57 |
{ |
| 58 |
m_pid = pid; |
| 59 |
} |
| 60 |
|
| 61 |
int CSectionDecoder::GetPid() |
| 62 |
{ |
| 63 |
return m_pid; |
| 64 |
} |
| 65 |
|
| 66 |
void CSectionDecoder::Reset() |
| 67 |
{ |
| 68 |
m_section.Reset(); |
| 69 |
} |
| 70 |
|
| 71 |
void CSectionDecoder::EnableCrcCheck(bool onOff) |
| 72 |
{ |
| 73 |
m_bCrcCheck = onOff; |
| 74 |
} |
| 75 |
|
| 76 |
void CSectionDecoder::OnTsPacket(byte* tsPacket) |
| 77 |
{ |
| 78 |
if (m_pid < 0) |
| 79 |
return; |
| 80 |
if (tsPacket == NULL) |
| 81 |
return; |
| 82 |
|
| 83 |
m_header.Decode(tsPacket); |
| 84 |
OnTsPacket(m_header, tsPacket); |
| 85 |
} |
| 86 |
|
| 87 |
int CSectionDecoder::StartNewSection(byte* tsPacket, int index, int sectionLen) |
| 88 |
{ |
| 89 |
int newstart = -1; |
| 90 |
int len = -1; |
| 91 |
|
| 92 |
if (sectionLen > -1) |
| 93 |
{ |
| 94 |
if (index + sectionLen < 185) |
| 95 |
{ |
| 96 |
len = sectionLen + 3; |
| 97 |
newstart = index + sectionLen + 3; |
| 98 |
} |
| 99 |
else |
| 100 |
{ |
| 101 |
newstart = 188; |
| 102 |
len = 188 - index; |
| 103 |
} |
| 104 |
} |
| 105 |
else |
| 106 |
{ |
| 107 |
newstart = 188; |
| 108 |
len = 188 - index; |
| 109 |
} |
| 110 |
m_section.Reset(); |
| 111 |
memcpy(m_section.Data, &tsPacket[index], len); |
| 112 |
m_section.BufferPos = len; |
| 113 |
m_section.DecodeHeader(); |
| 114 |
return newstart; |
| 115 |
} |
| 116 |
|
| 117 |
int CSectionDecoder::AppendSection(byte* tsPacket, int index, int sectionLen) |
| 118 |
{ |
| 119 |
int newstart = -1; |
| 120 |
int len = -1; |
| 121 |
|
| 122 |
if (index + sectionLen < 185) |
| 123 |
{ |
| 124 |
len = sectionLen + 3; |
| 125 |
newstart = index + sectionLen + 3; |
| 126 |
} |
| 127 |
else |
| 128 |
{ |
| 129 |
newstart = 188; |
| 130 |
len = 188 - index; |
| 131 |
} |
| 132 |
|
| 133 |
memcpy(&m_section.Data[m_section.BufferPos], &tsPacket[index], len); |
| 134 |
m_section.BufferPos += len; |
| 135 |
return newstart; |
| 136 |
} |
| 137 |
|
| 138 |
int CSectionDecoder::SnapshotSectionLength(byte* tsPacket, int start) |
| 139 |
{ |
| 140 |
if (start >= 184) |
| 141 |
return -1; |
| 142 |
return (int)(((tsPacket[start + 1] & 0xF) << 8) + tsPacket[start + 2]); |
| 143 |
} |
| 144 |
|
| 145 |
void CSectionDecoder::OnTsPacket(CTsHeader& header, byte* tsPacket) |
| 146 |
{ |
| 147 |
try |
| 148 |
{ |
| 149 |
if (header.TransportError) |
| 150 |
{ |
| 151 |
m_section.Reset(); // Will force us to wait for new PayloadUnitStart |
| 152 |
return; |
| 153 |
} |
| 154 |
if (m_pid >= 0x1fff) return; |
| 155 |
if (header.Pid != m_pid) return; |
| 156 |
if (!header.HasPayload) return; |
| 157 |
|
| 158 |
int start = header.PayLoadStart; |
| 159 |
int pointer_field = 0; |
| 160 |
|
| 161 |
if (header.PayloadUnitStart) |
| 162 |
{ |
| 163 |
if (start >= 188) |
| 164 |
return; |
| 165 |
|
| 166 |
pointer_field = start + tsPacket[start] + 1; |
| 167 |
if (m_section.BufferPos == 0) |
| 168 |
start += tsPacket[start] + 1; |
| 169 |
else |
| 170 |
start++; |
| 171 |
} |
| 172 |
|
| 173 |
int numloops = 0; |
| 174 |
while (start < 188) |
| 175 |
{ |
| 176 |
numloops++; |
| 177 |
if (m_section.BufferPos == 0) |
| 178 |
{ |
| 179 |
if (!header.PayloadUnitStart) return; |
| 180 |
if (tsPacket[start] == 0xFF) return; |
| 181 |
int section_length = SnapshotSectionLength(tsPacket, start); |
| 182 |
start = StartNewSection(tsPacket, start, section_length); |
| 183 |
} |
| 184 |
else |
| 185 |
{ |
| 186 |
if (m_section.section_length == -1) |
| 187 |
m_section.CalcSectionLength(tsPacket, start); |
| 188 |
if (m_section.section_length == 0) |
| 189 |
{ |
| 190 |
if (m_bLog) |
| 191 |
kodi::Log(ADDON_LOG_DEBUG, "!!! CSectionDecoder::OnTsPacket got a section with section length: 0 on pid: 0x%X tableid: 0x%X bufferpos: %d start: %d - Discarding whole packet.", header.Pid, m_section.Data[0], m_section.BufferPos, start); |
| 192 |
m_section.Reset(); |
| 193 |
return; |
| 194 |
} |
| 195 |
int len = m_section.section_length - m_section.BufferPos; |
| 196 |
if (pointer_field != 0 && ((start + len) > pointer_field)) |
| 197 |
{ |
| 198 |
// We have an incomplete section here |
| 199 |
len = pointer_field - start; |
| 200 |
start = AppendSection(tsPacket, start, len); |
| 201 |
m_section.section_length = m_section.BufferPos - 1; |
| 202 |
start = pointer_field; |
| 203 |
} |
| 204 |
else |
| 205 |
start = AppendSection(tsPacket, start, len); |
| 206 |
} |
| 207 |
if (m_section.SectionComplete() && m_section.section_length > 0) |
| 208 |
{ |
| 209 |
uint32_t crc = 0; |
| 210 |
|
| 211 |
// Only long syntax (section_syntax_indicator == 1) has a CRC |
| 212 |
// Short syntax may have CRC e.g. TOT, but that is part of the specific section |
| 213 |
if (m_section.section_syntax_indicator == 1) |
| 214 |
crc = crc32((char*)m_section.Data, m_section.section_length + 3); |
| 215 |
|
| 216 |
if (crc == 0 || (m_bCrcCheck == false)) |
| 217 |
{ |
| 218 |
OnNewSection(m_section); |
| 219 |
if (m_pCallback != NULL) |
| 220 |
m_pCallback->OnNewSection(header.Pid, m_section.table_id, m_section); |
| 221 |
} |
| 222 |
else |
| 223 |
{ |
| 224 |
// If the section is complete and the CRC fails, then this section is crap! |
| 225 |
m_section.Reset(); |
| 226 |
return; |
| 227 |
} |
| 228 |
|
| 229 |
m_section.Reset(); |
| 230 |
} |
| 231 |
pointer_field = 0; |
| 232 |
if (numloops > 100) |
| 233 |
{ |
| 234 |
kodi::Log(ADDON_LOG_DEBUG, "!!! CSectionDecoder::OnTsPacket Entered infinite loop. pid: %X start: %d BufferPos: %d SectionLength: %d - Discarding section and moving to next packet", |
| 235 |
header.Pid, start, m_section.BufferPos, m_section.section_length); |
| 236 |
m_section.Reset(); |
| 237 |
return; |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
catch (...) |
| 242 |
{ |
| 243 |
kodi::Log(ADDON_LOG_DEBUG, "exception in CSectionDecoder::OnTsPacket"); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
void CSectionDecoder::OnNewSection(CSection& UNUSED(section)) |
| 248 |
{ |
| 249 |
} |
| 250 |
} |