Debian Codegraph @

src/lib/tsreader/DeMultiplexer.cpp

raw
1 /*
2 * Copyright (C) 2005-2013 Team Kodi
3 * https://kodi.tv
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 of the License, or
8 * (at your option) 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 this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 *************************************************************************
19 * This file is a modified version from Team MediaPortal's
20 * TsReader DirectShow filter
21 * MediaPortal is a GPL'ed HTPC-Application
22 * Copyright (C) 2005-2012 Team MediaPortal
23 * http://www.team-mediaportal.com
24 *
25 * Changes compared to Team MediaPortal's version:
26 * - Code cleanup for PVR addon usage
27 * - Code refactoring for cross platform usage
28 *************************************************************************
29 * This file originates from TSFileSource, a GPL directshow push
30 * source filter that provides an MPEG transport stream output.
31 * Copyright (C) 2005-2006 nate, bear
32 * http://forums.dvbowners.com/
33 */
34
35 #include "DeMultiplexer.h"
36 #include "utils.h" //UNUSED()
37 #include <kodi/General.h> //for kodi::Log
38 #include "TSReader.h"
39
40 #include <thread>
41
42 #define MAX_BUF_SIZE 8000
43 #define BUFFER_LENGTH 0x1000
44 #define READ_SIZE (1316*30)
45 #define INITIAL_READ_SIZE (READ_SIZE * 1024)
46
47 namespace MPTV
48 {
49 CDeMultiplexer::CDeMultiplexer(CTsReader& filter)
50 :m_filter(filter),
51 m_LastDataFromRtsp(0),
52 m_bEndOfFile(false),
53 m_reader(NULL),
54 m_iPatVersion(-1),
55 m_ReqPatVersion(-1),
56 m_WaitNewPatTmo(0),
57 m_receivedPackets(0),
58 m_bStarting(false),
59 m_bAudioAtEof(false),
60 m_bVideoAtEof(false),
61 m_bGotNewChannel(false)
62 {
63 m_patParser.SetCallBack(this);
64 }
65
66 CDeMultiplexer::~CDeMultiplexer()
67 {
68
69 }
70
71 /// Starts the demuxer
72 /// This method will read the file until we found the pat/sdt
73 /// with all the audio/video pids
74 void CDeMultiplexer::Start()
75 {
76 //reset some values
77 m_bStarting = true;
78 m_receivedPackets = 0;
79 m_bEndOfFile = false;
80 m_iPatVersion = -1;
81 m_ReqPatVersion = -1;
82 unsigned long long m_Time = GetTickCount64();
83
84 while ((GetTickCount64() - m_Time) < 5000 && m_bGotNewChannel == false)
85 {
86 size_t BytesRead = ReadFromFile();
87 if (0 == BytesRead)
88 std::this_thread::sleep_for(std::chrono::milliseconds(10));
89 }
90 m_bStarting = false;
91 }
92
93 void CDeMultiplexer::SetFileReader(FileReader* reader)
94 {
95 m_reader = reader;
96 }
97
98 /// This method reads the next READ_SIZE bytes from the file
99 /// and processes the raw data
100 /// When a TS packet has been discovered, OnTsPacket(byte* tsPacket) gets called
101 // which in its turn deals with the packet
102 size_t CDeMultiplexer::ReadFromFile()
103 {
104 if (m_filter.IsSeeking())
105 return 0; // Ambass : to check
106
107 std::lock_guard<std::mutex> lock(m_sectionRead);
108 if (NULL == m_reader)
109 return 0;
110
111 byte buffer[READ_SIZE];
112 size_t dwReadBytes = 0;
113
114 // if we are playing a RTSP stream
115 if (m_reader->IsBuffer())
116 {
117 // and, the current buffer holds data
118 size_t nBytesToRead = m_reader->HasData();
119
120 if (nBytesToRead > sizeof(buffer))
121 {
122 nBytesToRead = sizeof(buffer);
123 }
124 else
125 {
126 m_bAudioAtEof = true;
127 m_bVideoAtEof = true;
128 }
129
130 if (nBytesToRead)
131 {
132 // then read raw data from the buffer
133 if (SUCCEEDED(m_reader->Read(buffer, nBytesToRead, &dwReadBytes)))
134 {
135 if (dwReadBytes > 0)
136 {
137 // yes, then process the raw data
138 //result=true;
139 OnRawData(buffer, dwReadBytes);
140 m_LastDataFromRtsp = GetTickCount64();
141 }
142 }
143 else
144 {
145 kodi::Log(ADDON_LOG_DEBUG, "%s: Read failed...", __FUNCTION__);
146 }
147 }
148 else
149 {
150 if (!m_filter.IsTimeShifting())
151 {
152 kodi::Log(ADDON_LOG_DEBUG, "%s: endoffile... %llu", __FUNCTION__, GetTickCount64() - m_LastDataFromRtsp);
153 //set EOF flag and return
154 if (GetTickCount64() - m_LastDataFromRtsp > 2000 && m_filter.State() != State_Paused) // A bit crappy, but no better idea...
155 {
156 kodi::Log(ADDON_LOG_DEBUG, "%s: endoffile!", __FUNCTION__);
157 m_bEndOfFile = true;
158 return 0;
159 }
160 }
161 }
162 return dwReadBytes;
163 }
164 else
165 {
166 // playing a local file.
167 // read raw data from the file
168 if (SUCCEEDED(m_reader->Read(buffer, sizeof(buffer), &dwReadBytes)))
169 {
170 if ((m_filter.IsTimeShifting()) && (dwReadBytes < sizeof(buffer)))
171 {
172 m_bAudioAtEof = true;
173 m_bVideoAtEof = true;
174 }
175
176 if (dwReadBytes > 0)
177 {
178 // succeeded, process data
179 OnRawData(buffer, (int)dwReadBytes);
180 }
181 else
182 {
183 if (!m_filter.IsTimeShifting())
184 {
185 // set EOF flag and return
186 kodi::Log(ADDON_LOG_DEBUG, "%s: endoffile!", __FUNCTION__);
187 m_bEndOfFile = true;
188 return 0;
189 }
190 }
191
192 // and return
193 return dwReadBytes;
194 }
195 else
196 {
197 kodi::Log(ADDON_LOG_DEBUG, "%s: Read failed...", __FUNCTION__);
198 }
199 }
200 return 0;
201 }
202
203 /// This method gets called via ReadFile() when a new TS packet has been received
204 /// if will :
205 /// - decode any new pat/pmt/sdt
206 /// - decode any audio/video packets and put the PES packets in the appropiate buffers
207 void CDeMultiplexer::OnTsPacket(byte* tsPacket)
208 {
209 CTsHeader header(tsPacket);
210
211 m_patParser.OnTsPacket(tsPacket);
212
213 if (m_iPatVersion == -1)
214 {
215 // First Pat not found
216 return;
217 }
218
219 // Wait for new PAT if required.
220 if ((m_iPatVersion & 0x0F) != (m_ReqPatVersion & 0x0F))
221 {
222 if (m_ReqPatVersion == -1)
223 { // Now, unless channel change,
224 m_ReqPatVersion = m_iPatVersion; // Initialize Pat Request.
225 m_WaitNewPatTmo = GetTickCount64(); // Now, unless channel change request,timeout will be always true.
226 }
227 if (GetTickCount64() < m_WaitNewPatTmo)
228 {
229 // Timeout not reached.
230 return;
231 }
232 }
233 }
234
235 void CDeMultiplexer::RequestNewPat(void)
236 {
237 if (m_reader)
238 {
239 m_ReqPatVersion++;
240 m_ReqPatVersion &= 0x0F;
241 kodi::Log(ADDON_LOG_DEBUG, "Request new PAT = %d", m_ReqPatVersion);
242 m_WaitNewPatTmo = GetTickCount64() + 10000;
243
244 size_t dwBytesProcessed = 0;
245 unsigned long long m_Time = GetTickCount64();
246
247 m_bGotNewChannel = false;
248
249 while ((GetTickCount64() - m_Time) < 5000 && m_bGotNewChannel == false)
250 {
251 size_t BytesRead = ReadFromFile();
252 if (0 == BytesRead)
253 std::this_thread::sleep_for(std::chrono::milliseconds(10));
254 dwBytesProcessed += BytesRead;
255 }
256
257 kodi::Log(ADDON_LOG_DEBUG, "Found a new channel after processing %li bytes. File position: %I64d\n", dwBytesProcessed, m_reader->GetFilePointer());
258 }
259 }
260
261 /// This method gets called-back from the pat parser when a new PAT/PMT/SDT has been received
262 /// In this method we check if any audio/video/subtitle pid or format has changed
263 /// If not, we simply return
264 /// If something has changed we ask the MP to rebuild the graph
265 void CDeMultiplexer::OnNewChannel(CChannelInfo& UNUSED(info))
266 {
267 m_bGotNewChannel = true;
268 }
269 }