forked from kodi-pvr/pvr.mediaportal.tvserver
/
DeMultiplexer.cpp
263 lines (236 loc) · 8.43 KB
/
DeMultiplexer.cpp
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
* Copyright (C) 2005-2013 Team XBMC
* http://www.xbmc.org
*
* 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 of the License, 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*************************************************************************
* This file is a modified version from Team MediaPortal's
* TsReader DirectShow filter
* MediaPortal is a GPL'ed HTPC-Application
* Copyright (C) 2005-2012 Team MediaPortal
* http://www.team-mediaportal.com
*
* Changes compared to Team MediaPortal's version:
* - Code cleanup for PVR addon usage
* - Code refactoring for cross platform usage
*************************************************************************
* This file originates from TSFileSource, a GPL directshow push
* source filter that provides an MPEG transport stream output.
* Copyright (C) 2005-2006 nate, bear
* http://forums.dvbowners.com/
*/
#include "DeMultiplexer.h"
#include "utils.h" //UNUSED()
#include "client.h" //XBMC->Log()
#include "TSReader.h"
#define MAX_BUF_SIZE 8000
#define BUFFER_LENGTH 0x1000
#define READ_SIZE (1316*30)
#define INITIAL_READ_SIZE (READ_SIZE * 1024)
using namespace ADDON;
namespace MPTV
{
CDeMultiplexer::CDeMultiplexer(CTsReader& filter)
:m_filter(filter),
m_LastDataFromRtsp(0),
m_bEndOfFile(false),
m_reader(NULL),
m_iPatVersion(-1),
m_ReqPatVersion(-1),
m_WaitNewPatTmo(0),
m_receivedPackets(0),
m_bStarting(false),
m_bAudioAtEof(false),
m_bVideoAtEof(false),
m_bGotNewChannel(false)
{
m_patParser.SetCallBack(this);
}
CDeMultiplexer::~CDeMultiplexer()
{
}
/// Starts the demuxer
/// This method will read the file until we found the pat/sdt
/// with all the audio/video pids
void CDeMultiplexer::Start()
{
//reset some values
m_bStarting = true;
m_receivedPackets = 0;
m_bEndOfFile = false;
m_iPatVersion = -1;
m_ReqPatVersion = -1;
unsigned long m_Time = GetTickCount();
while ((GetTickCount() - m_Time) < 5000 && m_bGotNewChannel == false)
{
int BytesRead = ReadFromFile();
if (0 == BytesRead)
usleep(10000);
}
m_bStarting = false;
}
void CDeMultiplexer::SetFileReader(FileReader* reader)
{
m_reader = reader;
}
/// This method reads the next READ_SIZE bytes from the file
/// and processes the raw data
/// When a TS packet has been discovered, OnTsPacket(byte* tsPacket) gets called
// which in its turn deals with the packet
int CDeMultiplexer::ReadFromFile()
{
if (m_filter.IsSeeking())
return 0; // Ambass : to check
PLATFORM::CLockObject lock(m_sectionRead);
if (NULL == m_reader)
return false;
byte buffer[READ_SIZE];
unsigned long dwReadBytes = 0;
// if we are playing a RTSP stream
if (m_reader->IsBuffer())
{
// and, the current buffer holds data
int nBytesToRead = m_reader->HasData();
if (nBytesToRead > (int) sizeof(buffer))
{
nBytesToRead = sizeof(buffer);
}
else
{
m_bAudioAtEof = true;
m_bVideoAtEof = true;
}
if (nBytesToRead)
{
// then read raw data from the buffer
m_reader->Read(buffer, nBytesToRead, (unsigned long*)&dwReadBytes);
if (dwReadBytes > 0)
{
// yes, then process the raw data
//result=true;
OnRawData(buffer, (int)dwReadBytes);
m_LastDataFromRtsp = GetTickCount();
}
}
else
{
if (!m_filter.IsTimeShifting())
{
XBMC->Log(LOG_DEBUG, "%s: endoffile... %d", __FUNCTION__, GetTickCount() - m_LastDataFromRtsp);
//set EOF flag and return
if (GetTickCount() - m_LastDataFromRtsp > 2000 && m_filter.State() != State_Paused) // A bit crappy, but no better idea...
{
XBMC->Log(LOG_DEBUG, "%s: endoffile!", __FUNCTION__, GetTickCount() - m_LastDataFromRtsp);
m_bEndOfFile = true;
return 0;
}
}
}
return dwReadBytes;
}
else
{
// playing a local file.
// read raw data from the file
if (SUCCEEDED(m_reader->Read(buffer, sizeof(buffer), (unsigned long*)&dwReadBytes)))
{
if ((m_filter.IsTimeShifting()) && (dwReadBytes < sizeof(buffer)))
{
m_bAudioAtEof = true;
m_bVideoAtEof = true;
}
if (dwReadBytes > 0)
{
// succeeded, process data
OnRawData(buffer, (int)dwReadBytes);
}
else
{
if (!m_filter.IsTimeShifting())
{
// set EOF flag and return
XBMC->Log(LOG_DEBUG, "%s: endoffile!", __FUNCTION__);
m_bEndOfFile = true;
return 0;
}
}
// and return
return dwReadBytes;
}
else
{
XBMC->Log(LOG_DEBUG, "%s: Read failed...", __FUNCTION__);
}
}
return 0;
}
/// This method gets called via ReadFile() when a new TS packet has been received
/// if will :
/// - decode any new pat/pmt/sdt
/// - decode any audio/video packets and put the PES packets in the appropiate buffers
void CDeMultiplexer::OnTsPacket(byte* tsPacket)
{
CTsHeader header(tsPacket);
m_patParser.OnTsPacket(tsPacket);
if (m_iPatVersion == -1)
{
// First Pat not found
return;
}
// Wait for new PAT if required.
if ((m_iPatVersion & 0x0F) != (m_ReqPatVersion & 0x0F))
{
if (m_ReqPatVersion == -1)
{ // Now, unless channel change,
m_ReqPatVersion = m_iPatVersion; // Initialize Pat Request.
m_WaitNewPatTmo = GetTickCount(); // Now, unless channel change request,timeout will be always true.
}
if (GetTickCount() < (unsigned long)m_WaitNewPatTmo)
{
// Timeout not reached.
return;
}
}
}
void CDeMultiplexer::RequestNewPat(void)
{
if (m_reader)
{
m_ReqPatVersion++;
m_ReqPatVersion &= 0x0F;
XBMC->Log(LOG_DEBUG, "Request new PAT = %d", m_ReqPatVersion);
m_WaitNewPatTmo = GetTickCount() + 10000;
unsigned long dwBytesProcessed = 0;
unsigned long m_Time = GetTickCount();
m_bGotNewChannel = false;
while ((GetTickCount() - m_Time) < 5000 && m_bGotNewChannel == false)
{
int BytesRead = ReadFromFile();
if (0 == BytesRead)
usleep(10000);
dwBytesProcessed += BytesRead;
}
XBMC->Log(LOG_DEBUG, "Found a new channel after processing %li bytes. File position: %I64d\n", dwBytesProcessed, m_reader->GetFilePointer());
}
}
/// This method gets called-back from the pat parser when a new PAT/PMT/SDT has been received
/// In this method we check if any audio/video/subtitle pid or format has changed
/// If not, we simply return
/// If something has changed we ask the MP to rebuild the graph
void CDeMultiplexer::OnNewChannel(CChannelInfo& UNUSED(info))
{
m_bGotNewChannel = true;
}
}