forked from zillevdr/vdr-plugin-softhddevice-drm
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpipreceiver.cpp
More file actions
435 lines (378 loc) · 10.2 KB
/
pipreceiver.cpp
File metadata and controls
435 lines (378 loc) · 10.2 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// SPDX-License-Identifier: AGPL-3.0-or-later
/**
* @file pipreceiver.cpp
* PiP (Picture-in-Picture) Interface
*
* @copyright 2025 - 2026 by Andreas Baierl. All Rights Reserved.
*
* @license{AGPL-3.0-or-later}
*/
#include <vdr/remux.h>
#include <vdr/skins.h>
#include "event.h"
#include "logger.h"
#include "pipreceiver.h"
#include "softhddevice.h"
/*****************************************************************************
* cPipReceiver class
****************************************************************************/
/**
* Create a new receiver for the pip stream handling only video pid
*
* @param channel channel to receive
* @param device pointer to cSoftHdDevice object
*/
cPipReceiver::cPipReceiver(const cChannel *channel, cSoftHdDevice *device)
: cReceiver(NULL, MINPRIORITY),
m_pDevice(device)
{
LOGDEBUG("pipreceiver: %s", __FUNCTION__);
AddPid(channel->Vpid());
}
/**
* Detach the pip receiver
*/
cPipReceiver::~cPipReceiver(void)
{
LOGDEBUG("pipreceiver: %s", __FUNCTION__);
Detach();
}
/**
* Called before the receiver gets attached or after it got detached
*
* @param on set on/off (unused)
*/
void cPipReceiver::Activate(bool on)
{
LOGDEBUG("pipreceiver: %s %s", __FUNCTION__, on ? "on" : "off");
m_pTsToPesVideo.Reset();
}
/**
* Receive data from the receiver
*
* This code is taken from VDRs cTransfer::Receive()
*/
void cPipReceiver::Receive(const uchar *data, int size)
{
const int MAXRETRIES = 20; // max. number of retries for a single TS packet
const int RETRYWAITMS = 5; // time between two retries
const int ERRORDELTASEC = 60; // seconds before reporting lost packages again
for (int i = 0; i < MAXRETRIES; i++) {
if (ParseTs(data, size) > 0)
return;
cCondWait::SleepMs(RETRYWAITMS);
}
m_numLostPackets++;
if (cTimeMs::Now() - m_lastErrorReport > ERRORDELTASEC) {
LOGWARNING("pipreceiver: %d TS packet(s) not accepted in pip stream", m_numLostPackets);
m_numLostPackets = 0;
m_lastErrorReport = cTimeMs::Now();
}
}
/**
* Parse the ts stream and send it to the pes player
*
* This code is taken from VDRs cDevice::PlayTs()
*/
int cPipReceiver::ParseTs(const uchar *data, int size)
{
int played = 0;
if (!data) {
LOGWARNING("pipreceiver: %s null data received, reset pes buffer!", __FUNCTION__);
m_pTsToPesVideo.Reset();
return 0;
}
if (size < TS_SIZE) {
LOGWARNING("pipreceiver: %s TS fragment received!", __FUNCTION__);
return size;
}
while (size >= TS_SIZE) {
if (int skipped = TS_SYNC(data, size)) {
LOGWARNING("pipreceiver: %s TS stream not in sync!", __FUNCTION__);
return played + skipped;
}
if (TsHasPayload(data)) {
int payloadOffset = TsPayloadOffset(data);
if (payloadOffset < TS_SIZE) {
int w = PlayTs(data, TS_SIZE);
if (w < 0)
return played ? played : w;
if (w == 0)
break;
}
}
played += TS_SIZE;
size -= TS_SIZE;
data += TS_SIZE;
}
return played;
}
/**
* Get the pes payload and send it to the player
*
* This code is taken from VDRs cDevice::PlayTsVideo()
*/
int cPipReceiver::PlayTs(const uchar *data, int size)
{
if (TsPayloadStart(data)) {
int length;
while (const uchar *pes = m_pTsToPesVideo.GetPes(length)) {
int w = m_pDevice->PlayPipVideo(pes, length);
if (w <= 0) {
m_pTsToPesVideo.SetRepeatLast();
return w;
}
}
m_pTsToPesVideo.Reset();
}
m_pTsToPesVideo.PutTs(data, size);
return size;
}
/*****************************************************************************
* cPipHandler class
****************************************************************************/
cPipHandler::cPipHandler(cSoftHdDevice *device)
: m_pDevice(device),
m_pEventReceiver(device)
{
}
cPipHandler::~cPipHandler(void)
{
Stop();
}
/*****************************************************************************
* Handle events
*
* The following functions are called from within the state change and must
* not trigger any new events. Otherwise we end up in a dead lock!
****************************************************************************/
/**
* Handle a pip event
*/
void cPipHandler::HandleEvent(enum PipState event)
{
switch (event) {
case PIPSTART:
HandleEnable(true);
break;
case PIPSTOP:
HandleEnable(false);
break;
case PIPTOGGLE:
HandleEnable(!m_active);
break;
case PIPCHANUP:
HandleChannelChange(1);
break;
case PIPCHANDOWN:
HandleChannelChange(-1);
break;
case PIPCHANSWAP:
Stop();
Start(0);
break;
case PIPSIZECHANGE:
m_pDevice->SetRenderPipSize();
break;
case PIPSWAPPOSITION:
m_pDevice->ToggleRenderPipPosition();
m_pDevice->SetRenderPipSize();
break;
default:
break;
}
}
/**
* Create a new pip receiver and render the pip stream
*
* @param channelNum number of the channel to be switched to
* 0 switches to the current main stream channel
*
* @retval 0 pip was enabled
* @retval -1 pip wasn't enabled, no device for channel available
*
* @note This function is called within the state change and must not trigger any new events!
*/
int cPipHandler::Start(int channelNum)
{
if (!channelNum)
channelNum = m_pDevice->CurrentChannel();
LOCK_CHANNELS_READ;
const cChannel *channel;
cDevice *device;
cPipReceiver *receiver;
if (channelNum && (channel = Channels->GetByNumber(channelNum)) &&
(device = m_pDevice->GetDevice(channel, 0, false, false))) {
Stop();
device->SwitchChannel(channel, false);
receiver = new cPipReceiver(channel, m_pDevice);
device->AttachReceiver(receiver);
m_pPipReceiver = receiver;
m_pPipChannel = channel;
m_pipChannelNum = channelNum;
LOGDEBUG("piphandler: %s: New receiver for channel (%d) %s", __FUNCTION__, channel->Number(), channel->Name());
m_active = true;
return 0;
}
LOGERROR("piphandler: %s: No receiver for channel num %d available", __FUNCTION__, channelNum);
return -1;
}
/**
* Delete the pip receiver, clear decoder and display buffers
* and disable rendering the pip window.
*
* We do not need to halt main stream decoder and display thread for this,
* so only halt the pip decoding thread here (in m_pDevice->ResetPipStream()) - not in OnEventReceived().
*
* @note This function is called within the state change and must not trigger any new events!
*/
void cPipHandler::Stop(void)
{
m_active = false;
if (!m_pPipReceiver)
return;
LOGDEBUG("piphandler: %s: deleting receiver for channel (%d) %s", __FUNCTION__, m_pPipChannel->Number(), m_pPipChannel->Name());
m_pDevice->ResetPipStream();
delete m_pPipReceiver;
m_pPipReceiver = nullptr;
m_pPipChannel = nullptr;
}
/**
* Enable/ disable picture-in-picture
*
* @param on true, if pip should be enabled
*
* @note This function is called within the state change and must not trigger any new events!
*/
void cPipHandler::HandleEnable(bool on)
{
if (on && m_active) {
LOGDEBUG("piphandler: %s: pip is already enabled", __FUNCTION__);
} else if (on && !m_active) {
LOGDEBUG("piphandler: %s: enabling pip (channel %d)", __FUNCTION__, m_pipChannelNum);
if (!Start(0))
m_pDevice->SetRenderPipActive(true);
} else if (!on && !m_active) {
LOGDEBUG("piphandler: %s: pip is already disabled", __FUNCTION__);
} else if (!on && m_active){
LOGDEBUG("piphandler: %s: disabling pip", __FUNCTION__);
m_pDevice->SetRenderPipActive(false);
Stop();
}
}
/**
* Change the pip channel
*
* @param direction 1: channel up, -1: channel down
*
* @note This function is called within the state change and must not trigger any new events!
*/
void cPipHandler::HandleChannelChange(int direction)
{
if (!m_active)
return;
const cChannel *channel;
const cChannel *first;
channel = m_pPipChannel;
first = channel;
Stop();
LOCK_CHANNELS_READ;
while (channel) {
bool ndr;
cDevice *device;
channel = direction > 0 ? Channels->Next(channel) : Channels->Prev(channel);
if (!channel && Setup.ChannelsWrap)
channel = direction > 0 ? Channels->First() : Channels->Last();
if (channel && !channel->GroupSep() && (device = cDevice::GetDevice(channel, 0, false, true)) &&
device->ProvidesChannel(channel, 0, &ndr) && !ndr) {
Start(channel->Number());
return;
}
if (channel == first) {
Skins.Message(mtError, tr("Channel not available!"));
break;
}
}
}
/*****************************************************************************
* Trigger events
*
* These (public) functions are wrapped by cSoftHdDevice and can be called
* to trigger a pip event.
****************************************************************************/
/**
* Start picture-in-picture
*/
void cPipHandler::Enable(void)
{
if (m_active)
return;
m_pEventReceiver->OnEventReceived(PipEvent{PIPSTART});
}
/**
* Stop picture-in-picture
*/
void cPipHandler::Disable(void)
{
if (!m_active)
return;
m_pEventReceiver->OnEventReceived(PipEvent{PIPSTOP});
}
/**
* Toggle picture-in-picture
*/
void cPipHandler::Toggle(void)
{
m_pEventReceiver->OnEventReceived(PipEvent{PIPTOGGLE});
}
/**
* Change the pip channel
*
* @param direction 1: channel up, -1: channel down
*/
void cPipHandler::ChannelChange(int direction)
{
if (!m_active)
return;
if (direction > 0)
m_pEventReceiver->OnEventReceived(PipEvent{PIPCHANUP});
else
m_pEventReceiver->OnEventReceived(PipEvent{PIPCHANDOWN});
}
/**
* Swap the pip channel with main live channel
*
* The channel switch of the main stream must be done out of OnEventReceived()
* because it triggers a SetPlayMode() which end in a deadlock otherwise.
*
* @param closePip close the pip window after the channel swap
*/
void cPipHandler::ChannelSwap(bool closePip)
{
if (!m_active)
return;
const cChannel *channel = m_pPipChannel;
if (!channel)
return;
if (closePip)
m_pEventReceiver->OnEventReceived(PipEvent{PIPSTOP});
else
m_pEventReceiver->OnEventReceived(PipEvent{PIPCHANSWAP}); // resets the pip channel to the current channel
LOCK_CHANNELS_READ;
LOGDEBUG("piphandler: %s: switch main stream to %d", __FUNCTION__, channel->Number());
Channels->SwitchTo(channel->Number());
}
/**
* Set size and position for the pip window
*/
void cPipHandler::SetSize(void)
{
m_pEventReceiver->OnEventReceived(PipEvent{PIPSIZECHANGE});
}
/**
* Swap pip between normal and alternative position
*/
void cPipHandler::SwapPosition(void)
{
m_pEventReceiver->OnEventReceived(PipEvent{PIPSWAPPOSITION});
}