Debian Codegraph @

src/InputstreamDemux.cpp

raw
1 /*
2 * Copyright (C) 2010-2021 Team Kodi (https://kodi.tv)
3 * Copyright (C) 2010 Alwin Esch (Team Kodi)
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSE.md for more information.
7 */
8
9 #include "InputstreamDemux.h"
10
11 #include "ClientInstance.h"
12 #include "RequestPacket.h"
13 #include "ResponsePacket.h"
14 #include "Settings.h"
15 #include "Tools.h"
16 #include "vnsicommand.h"
17
18 #include <kodi/General.h>
19 #include <limits.h>
20 #include <stdint.h>
21 #include <string.h>
22 #include <time.h>
23
24 cVNSIDemux::cVNSIDemux(kodi::addon::CInstancePVRClient& instance)
25 : cVNSISession(instance), m_statusCon(instance), m_instance(instance)
26 {
27 }
28
29 cVNSIDemux::~cVNSIDemux()
30 {
31 Close();
32 }
33
34 void cVNSIDemux::Close()
35 {
36 if (IsOpen() && GetProtocol() >= 9)
37 {
38 kodi::Log(ADDON_LOG_DEBUG, "closing demuxer");
39
40 cRequestPacket vrp;
41 vrp.init(VNSI_CHANNELSTREAM_CLOSE);
42
43 try
44 {
45 auto resp = ReadResult(&vrp);
46 if (!resp)
47 {
48 kodi::Log(ADDON_LOG_ERROR, "%s - failed to close streaming", __func__);
49 }
50 }
51 catch (std::exception e)
52 {
53 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
54 }
55 }
56
57 cVNSISession::Close();
58 }
59
60 bool cVNSIDemux::OpenChannel(const kodi::addon::PVRChannel& channelinfo)
61 {
62 m_channelinfo = channelinfo;
63 if (!cVNSISession::Open(CVNSISettings::Get().GetHostname(), CVNSISettings::Get().GetPort()))
64 return false;
65
66 if (!cVNSISession::Login())
67 return false;
68
69 return SwitchChannel(m_channelinfo);
70 }
71
72 bool cVNSIDemux::GetStreamProperties(std::vector<kodi::addon::PVRStreamProperties>& props)
73 {
74 props = m_streams;
75 return true;
76 }
77
78 void cVNSIDemux::Abort()
79 {
80 m_streams.clear();
81 }
82
83 DEMUX_PACKET* cVNSIDemux::Read()
84 {
85 if (m_connectionLost)
86 {
87 return nullptr;
88 }
89
90 ReadStatus();
91
92 auto resp = ReadMessage(1000, CVNSISettings::Get().GetConnectTimeout() * 1000);
93
94 if (resp == nullptr)
95 return m_instance.AllocateDemuxPacket(0);
96
97 if (resp->getChannelID() != VNSI_CHANNEL_STREAM)
98 {
99 return nullptr;
100 }
101
102 if (resp->getOpCodeID() == VNSI_STREAM_CHANGE)
103 {
104 StreamChange(resp.get());
105 DEMUX_PACKET* pkt = m_instance.AllocateDemuxPacket(0);
106 pkt->iStreamId = DEMUX_SPECIALID_STREAMCHANGE;
107 return pkt;
108 }
109 else if (resp->getOpCodeID() == VNSI_STREAM_STATUS)
110 {
111 StreamStatus(resp.get());
112 }
113 else if (resp->getOpCodeID() == VNSI_STREAM_SIGNALINFO)
114 {
115 StreamSignalInfo(resp.get());
116 }
117 else if (resp->getOpCodeID() == VNSI_STREAM_CONTENTINFO)
118 {
119 // send stream updates only if there are changes
120 if (StreamContentInfo(resp.get()))
121 {
122 DEMUX_PACKET* pkt = m_instance.AllocateDemuxPacket(0);
123 pkt->iStreamId = DEMUX_SPECIALID_STREAMCHANGE;
124 return pkt;
125 }
126 }
127 else if (resp->getOpCodeID() == VNSI_STREAM_MUXPKT)
128 {
129 // figure out the stream id for this packet
130 int pid = resp->getStreamID();
131
132 // stream found ?
133 if (pid >= 0 && resp->getMuxSerial() == m_MuxPacketSerial)
134 {
135 DEMUX_PACKET* p = (DEMUX_PACKET*)resp->stealUserData();
136 p->iSize = resp->getUserDataLength();
137 p->duration = static_cast<double>(resp->getDuration() * STREAM_TIME_BASE / 1000000);
138 p->dts = static_cast<double>(resp->getDTS() * STREAM_TIME_BASE / 1000000);
139 p->pts = static_cast<double>(resp->getPTS() * STREAM_TIME_BASE / 1000000);
140 p->iStreamId = pid;
141
142 int idx = -1;
143 for (size_t i = 0; i < m_streams.size(); i++)
144 {
145 if (m_streams[i].GetPID() == pid)
146 idx = i;
147 }
148 PVR_CODEC_TYPE type = PVR_CODEC_TYPE_UNKNOWN;
149 if (idx >= 0)
150 type = m_streams[idx].GetCodecType();
151
152 return p;
153 }
154 else if (pid >= 0 && resp->getMuxSerial() != m_MuxPacketSerial)
155 {
156 // ignore silently, may happen after a seek
157 }
158 else
159 {
160 kodi::Log(ADDON_LOG_DEBUG, "stream id %i not found", resp->getStreamID());
161 }
162 }
163 else if (resp->getOpCodeID() == VNSI_STREAM_TIMES)
164 {
165 m_bTimeshift = resp->extract_U8();
166 m_ReferenceTime = resp->extract_U32();
167 m_ReferenceDTS = static_cast<double>(resp->extract_U64());
168 m_minPTS = static_cast<double>(resp->extract_U64());
169 m_maxPTS = static_cast<double>(resp->extract_U64());
170 }
171 else if (resp->getOpCodeID() == VNSI_STREAM_BUFFERSTATS)
172 {
173 m_bTimeshift = resp->extract_U8();
174 m_minPTS = (resp->extract_U32() - m_ReferenceTime) * STREAM_TIME_BASE + m_ReferenceDTS;
175 m_maxPTS = (resp->extract_U32() - m_ReferenceTime) * STREAM_TIME_BASE + m_ReferenceDTS;
176 }
177 else if (resp->getOpCodeID() == VNSI_STREAM_REFTIME)
178 {
179 m_ReferenceTime = resp->extract_U32();
180 m_ReferenceDTS = static_cast<double>(resp->extract_U64());
181 }
182
183 return m_instance.AllocateDemuxPacket(0);
184 }
185
186 void cVNSIDemux::ReadStatus()
187 {
188 if (m_connectionLost || !m_statusCon.IsConnected())
189 {
190 return;
191 }
192
193 while (true)
194 {
195 auto resp = m_statusCon.ReadStatus();
196 if (!resp)
197 {
198 if ((time(nullptr) - m_lastStatus) > 2)
199 {
200 cRequestPacket vrp;
201 vrp.init(VNSI_CHANNELSTREAM_STATUS_REQUEST);
202 if (!TransmitMessage(&vrp))
203 {
204 SignalConnectionLost();
205 }
206 }
207 break;
208 }
209
210 if (resp->getOpCodeID() == VNSI_STREAM_TIMES)
211 {
212 m_bTimeshift = resp->extract_U8();
213 m_ReferenceTime = resp->extract_U32();
214 m_ReferenceDTS = static_cast<double>(resp->extract_U64());
215 m_minPTS = static_cast<double>(resp->extract_U64());
216 m_maxPTS = static_cast<double>(resp->extract_U64());
217 }
218 else if (resp->getOpCodeID() == VNSI_STREAM_SIGNALINFO)
219 {
220 StreamSignalInfo(resp.get());
221 }
222 else if (resp->getOpCodeID() == VNSI_STREAM_STATUS)
223 {
224 StreamStatus(resp.get());
225 }
226
227 m_lastStatus = time(nullptr);
228 }
229 }
230
231 bool cVNSIDemux::GetStreamTimes(kodi::addon::PVRStreamTimes& times)
232 {
233 ReadStatus();
234
235 times.SetStartTime(m_ReferenceTime);
236 times.SetPTSStart(m_ReferenceDTS);
237 times.SetPTSBegin(m_minPTS);
238 times.SetPTSEnd(m_maxPTS);
239 return true;
240 }
241
242 bool cVNSIDemux::SeekTime(int time, bool backwards, double& startpts)
243 {
244 cRequestPacket vrp;
245
246 int64_t seek_pts = static_cast<int64_t>(time) * 1000;
247 startpts = seek_pts;
248
249 vrp.init(VNSI_CHANNELSTREAM_SEEK);
250 vrp.add_S64(seek_pts);
251 vrp.add_U8(backwards);
252
253 auto resp = ReadResult(&vrp);
254 if (!resp)
255 {
256 kodi::Log(ADDON_LOG_ERROR, "%s - failed to seek2", __func__);
257 return false;
258 }
259 uint32_t retCode = resp->extract_U32();
260 uint32_t serial = resp->extract_U32();
261
262 if (retCode == VNSI_RET_OK)
263 {
264 m_MuxPacketSerial = serial;
265 return true;
266 }
267 else
268 return false;
269 }
270
271 bool cVNSIDemux::SwitchChannel(const kodi::addon::PVRChannel& channelinfo)
272 {
273 kodi::Log(ADDON_LOG_DEBUG, "changing to channel %d", channelinfo.GetChannelNumber());
274
275 cRequestPacket vrp1;
276 vrp1.init(VNSI_GETSETUP);
277 vrp1.add_String(CONFNAME_TIMESHIFT);
278
279 auto resp = ReadResult(&vrp1);
280 if (!resp)
281 {
282 kodi::Log(ADDON_LOG_ERROR, "%s - failed to get timeshift mode", __func__);
283 return false;
284 }
285 m_bTimeshift = resp->extract_U32();
286
287 cRequestPacket vrp2;
288 vrp2.init(VNSI_CHANNELSTREAM_OPEN);
289 vrp2.add_U32(channelinfo.GetUniqueId());
290 vrp2.add_S32(CVNSISettings::Get().GetPriority());
291 vrp2.add_U8(CVNSISettings::Get().GetTimeshift());
292
293 if (!ReadSuccess(&vrp2))
294 {
295 kodi::Log(ADDON_LOG_ERROR, "%s - failed to set channel", __func__);
296 return false;
297 }
298
299 if (GetProtocol() >= 13)
300 {
301 int fd = m_statusCon.GetSocket();
302 if (fd >= 0)
303 {
304 cRequestPacket vrp;
305 vrp.init(VNSI_CHANNELSTREAM_STATUS_SOCKET);
306 vrp.add_S32(fd);
307 if (ReadSuccess(&vrp))
308 {
309 m_statusCon.ReleaseServerClient();
310 kodi::Log(ADDON_LOG_DEBUG, "%s - established status connection", __func__);
311 }
312 }
313 }
314
315 m_channelinfo = channelinfo;
316 m_MuxPacketSerial = 0;
317 m_ReferenceTime = 0;
318 m_minPTS = 0;
319 m_maxPTS = 0;
320
321 m_streams.clear();
322
323 return true;
324 }
325
326 bool cVNSIDemux::GetSignalStatus(kodi::addon::PVRSignalStatus& qualityinfo)
327 {
328 qualityinfo = m_Quality;
329 return true;
330 }
331
332 void cVNSIDemux::StreamChange(cResponsePacket* resp)
333 {
334 m_streams.clear();
335
336 while (resp->getRemainingLength() >= 4 + 1)
337 {
338 uint32_t pid = resp->extract_U32();
339 const char* type = resp->extract_String();
340
341 std::string codecName;
342 if (!strcmp(type, "MPEG2AUDIO"))
343 codecName = "MP2";
344 else if (!strcmp(type, "MPEGTS"))
345 codecName = "MPEG2VIDEO";
346 else if (!strcmp(type, "TEXTSUB"))
347 codecName = "TEXT";
348 else
349 codecName = type;
350
351 kodi::addon::PVRStreamProperties props;
352 kodi::addon::PVRCodec codecId = m_instance.GetCodecByName(codecName);
353
354 if (codecId.GetCodecType() != PVR_CODEC_TYPE_UNKNOWN)
355 {
356 props.SetPID(pid);
357 props.SetCodecType(codecId.GetCodecType());
358 props.SetCodecId(codecId.GetCodecId());
359 }
360 else
361 {
362 return;
363 }
364
365 if (codecId.GetCodecType() == PVR_CODEC_TYPE_AUDIO)
366 {
367 const char* language = resp->extract_String();
368
369 props.SetChannels(resp->extract_U32());
370 props.SetSampleRate(resp->extract_U32());
371 props.SetBlockAlign(resp->extract_U32());
372 props.SetBitRate(resp->extract_U32());
373 props.SetBitsPerSample(resp->extract_U32());
374 if (std::strlen(language) == 3)
375 props.SetLanguage(language);
376 }
377 else if (codecId.GetCodecType() == PVR_CODEC_TYPE_VIDEO)
378 {
379 props.SetFPSScale(resp->extract_U32());
380 props.SetFPSRate(resp->extract_U32());
381 props.SetHeight(resp->extract_U32());
382 props.SetWidth(resp->extract_U32());
383 props.SetAspect((float)resp->extract_Double());
384 }
385 else if (codecId.GetCodecType() == PVR_CODEC_TYPE_SUBTITLE)
386 {
387 const char* language = resp->extract_String();
388 uint32_t composition_id(resp->extract_U32());
389 uint32_t ancillary_id(resp->extract_U32());
390 if (std::strlen(language) == 3)
391 {
392 props.SetLanguage(language);
393 props.SetSubtitleInfo((composition_id & 0xffff) | ((ancillary_id & 0xffff) << 16));
394 }
395 }
396 else if (codecId.GetCodecType() == PVR_CODEC_TYPE_RDS)
397 {
398 const char* language = resp->extract_String();
399 uint32_t rel_channel_pid(resp->extract_U32());
400 if (std::strlen(language) == 3)
401 props.SetLanguage(language);
402 }
403 else
404 {
405 m_streams.clear();
406 return;
407 }
408 m_streams.push_back(props);
409 }
410 }
411
412 void cVNSIDemux::StreamStatus(cResponsePacket* resp)
413 {
414 const char* status = resp->extract_String();
415 if (status != nullptr)
416 {
417 kodi::Log(ADDON_LOG_DEBUG, "%s - %s", __func__, status);
418 kodi::QueueNotification(QUEUE_INFO, "", status);
419 }
420 }
421
422 void cVNSIDemux::StreamSignalInfo(cResponsePacket* resp)
423 {
424 m_Quality.SetAdapterName(resp->extract_String());
425 m_Quality.SetAdapterStatus(resp->extract_String());
426 m_Quality.SetSNR(resp->extract_U32());
427 m_Quality.SetSignal(resp->extract_U32());
428 m_Quality.SetBER(resp->extract_U32());
429 m_Quality.SetUNC(resp->extract_U32());
430 }
431
432 bool cVNSIDemux::StreamContentInfo(cResponsePacket* resp)
433 {
434 while (resp->getRemainingLength() >= 4)
435 {
436 uint32_t pid = resp->extract_U32();
437
438 kodi::addon::PVRStreamProperties* props = nullptr;
439 for (auto& stream : m_streams)
440 {
441 if (stream.GetPID() == pid)
442 {
443 props = &stream;
444 break;
445 }
446 }
447 if (props)
448 {
449 if (props->GetCodecType() == PVR_CODEC_TYPE_AUDIO)
450 {
451 const char* language(resp->extract_String());
452
453 props->SetChannels(resp->extract_U32());
454 props->SetSampleRate(resp->extract_U32());
455 props->SetBlockAlign(resp->extract_U32());
456 props->SetBitRate(resp->extract_U32());
457 props->SetBitsPerSample(resp->extract_U32());
458 props->SetLanguage(language);
459 }
460 else if (props->GetCodecType() == PVR_CODEC_TYPE_VIDEO)
461 {
462 props->SetFPSScale(resp->extract_U32());
463 props->SetFPSRate(resp->extract_U32());
464 props->SetHeight(resp->extract_U32());
465 props->SetWidth(resp->extract_U32());
466 props->SetAspect(static_cast<float>(resp->extract_Double()));
467 }
468 else if (props->GetCodecType() == PVR_CODEC_TYPE_SUBTITLE)
469 {
470 const char* language(resp->extract_String());
471 uint32_t composition_id(resp->extract_U32());
472 uint32_t ancillary_id(resp->extract_U32());
473
474 props->SetSubtitleInfo((composition_id & 0xffff) | ((ancillary_id & 0xffff) << 16));
475 props->SetLanguage(language);
476 }
477 }
478 else
479 {
480 kodi::Log(ADDON_LOG_ERROR, "%s - unknown stream id: %d", __func__, pid);
481 break;
482 }
483 }
484 return true;
485 }
486
487 //-----------------------------------------------------------------------------
488 //
489 //-----------------------------------------------------------------------------
490
491 CVNSIDemuxStatus::CVNSIDemuxStatus(kodi::addon::CInstancePVRClient& instance)
492 : cVNSISession(instance)
493 {
494 }
495
496 int CVNSIDemuxStatus::GetSocket()
497 {
498 Close();
499
500 if (!Open(CVNSISettings::Get().GetHostname(), CVNSISettings::Get().GetPort()))
501 {
502 return -1;
503 }
504
505 if (!Login())
506 {
507 return -1;
508 }
509
510 cRequestPacket vrp;
511 vrp.init(VNSI_GETSOCKET);
512 auto resp = ReadResult(&vrp);
513 if (!resp)
514 {
515 kodi::Log(ADDON_LOG_ERROR, "%s - failed to get socket", __func__);
516 return -1;
517 }
518 int fd = resp->extract_S32();
519 return fd;
520 }
521
522 void CVNSIDemuxStatus::ReleaseServerClient()
523 {
524 cRequestPacket vrp;
525 vrp.init(VNSI_INVALIDATESOCKET);
526 if (!ReadSuccess(&vrp))
527 {
528 kodi::Log(ADDON_LOG_ERROR, "%s - failed to release server client", __func__);
529 }
530 }
531
532 std::unique_ptr<cResponsePacket> CVNSIDemuxStatus::ReadStatus()
533 {
534 if (!IsOpen())
535 return nullptr;
536
537 return ReadMessage(1, 10000);
538 }
539
540 bool CVNSIDemuxStatus::IsConnected()
541 {
542 return IsOpen();
543 }