forked from manio/vdr-plugin-dvbapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeCsaTSBuffer.cpp
120 lines (112 loc) · 2.8 KB
/
DeCsaTSBuffer.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
/*
* vdr-plugin-dvbapi - softcam dvbapi plugin for VDR
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "DeCsaTSBuffer.h"
#include "Log.h"
DeCsaTsBuffer::DeCsaTsBuffer(int File, int Size, int CardIndex, DeCSA *DeCsa, bool ScActive)
{
SetDescription("TS buffer on device %d", CardIndex);
f = File;
size = Size;
cardIndex = CardIndex;
decsa = DeCsa;
delivered = false;
ringBuffer = new cRingBufferLinear(Size, TS_SIZE, true, "FFdecsa-TS");
ringBuffer->SetTimeouts(100, 100);
if (decsa)
decsa->SetActive(true);
SetActive(ScActive);
Start();
}
DeCsaTsBuffer::~DeCsaTsBuffer()
{
Cancel(3);
if (decsa)
decsa->SetActive(false);
delete ringBuffer;
}
void DeCsaTsBuffer::SetActive(bool ScActive)
{
scActive = ScActive;
}
void DeCsaTsBuffer::Action(void)
{
if (ringBuffer)
{
bool firstRead = true;
cPoller Poller(f);
while (Running())
{
if (firstRead || Poller.Poll(100))
{
firstRead = false;
int r = ringBuffer->Read(f);
if (r < 0 && FATALERRNO)
{
if (errno == EOVERFLOW)
ERRORLOG("driver buffer overflow on device %d", cardIndex);
else
{
LOG_ERROR;
break;
}
}
}
}
}
}
uchar *DeCsaTsBuffer::Get(void)
{
int Count = 0;
if (delivered)
{
ringBuffer->Del(TS_SIZE);
delivered = false;
}
uchar *p = ringBuffer->Get(Count);
if (p && Count >= TS_SIZE)
{
if (*p != TS_SYNC_BYTE)
{
for (int i = 1; i < Count; i++)
if (p[i] == TS_SYNC_BYTE && (i + TS_SIZE == Count || (i + TS_SIZE > Count && p[i + TS_SIZE] == TS_SYNC_BYTE)))
{
Count = i;
break;
}
ringBuffer->Del(Count);
ERRORLOG("skipped %d bytes to sync on TS packet on device %d", Count, cardIndex);
return NULL;
}
if (scActive && (p[3] & 0xC0))
{
if (decsa)
{
if (!decsa->Decrypt(p, Count, false))
{
cCondWait::SleepMs(20);
return NULL;
}
}
else
p[3] &= ~0xC0; // FF hack
}
delivered = true;
return p;
}
return NULL;
}