This repository has been archived by the owner on Nov 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpacketbuffer.c
294 lines (265 loc) · 6.25 KB
/
packetbuffer.c
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
/*
* (c) BayCom GmbH, http://www.baycom.de, info@baycom.de
*
* See the COPYING file for copyright information and
* how to reach the author.
*
*/
#include <vdr/plugin.h>
#if 0
#include <sys/time.h>
#else
#include <sys/times.h>
#endif
#include "packetbuffer.h"
clock_t Now (void)
{
#if 0
struct timeval t;
gettimeofday (&t, NULL);
return (clock_t)t.tv_sec * 1000 + (clock_t)(t.tv_usec / 1000);
#else
#ifdef __linux__
return times(NULL);
#else
struct tms unused;
return times(&unused);
#endif
#endif
}
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
cMyPacketBuffer::cMyPacketBuffer (int Size, int Packets)
{
if (Packets == 0)
Packets = Size / 2048;
// Make Packets a power of 2 to avoid expensive modulo
int n = 1;
for (int i = 0; i < 16; i++) {
if (n >= Packets) {
Packets = n;
break;
}
n <<= 1;
}
dataBuffer = new uchar[Size];
memset (dataBuffer, 0, Size);
posBuffer = new posData[Packets];
pthread_mutex_init (&m_lock, NULL);
posSize = Packets;
dataSize = Size;
memset (posBuffer, 0, Packets * sizeof (posData));
rp = wp = 0;
posRead = NULL;
posWrite = NULL;
posReadNum = 0;
invalidate = 0;
putTimeout = getTimeout = 0;
}
//--------------------------------------------------------------------------
cMyPacketBuffer::~cMyPacketBuffer (void)
{
delete[] dataBuffer;
delete[] posBuffer;
}
//--------------------------------------------------------------------------
int cMyPacketBuffer::FindSpace (int size)
{
int wpm = (wp - 1) & (posSize - 1);
posData *pr, *pw;
if (wpm < 0)
wpm += posSize;
if (rp == wp) {
if (size > dataSize)
return -1;
return 0;
}
pr = posBuffer + rp;
pw = posBuffer + wpm;
if (pr->offset <= pw->offset) {
if (pw->offset + pw->realSize + size < dataSize) {
return pw->offset + pw->realSize;
}
if (size < pr->offset)
return 0;
return -1;
} else {
if (pw->offset + pw->realSize + size < dataSize)
return pw->offset + pw->realSize;
return -1;
}
return -1;
}
//--------------------------------------------------------------------------
uchar *cMyPacketBuffer::PutStart (int size)
{
clock_t starttime = 0;
int offset;
int nwp;
int rsize;
pthread_mutex_lock (&m_lock);
// rsize= (size+15)&~15;
rsize = size;
while (true) {
offset = FindSpace (rsize);
if (offset != -1)
break;
if (putTimeout && !starttime)
starttime = Now ();
if (!putTimeout || (Now () - starttime) > (clock_t) (putTimeout)) {
pthread_mutex_unlock (&m_lock);
return NULL;
}
usleep (5 * 1000);
}
nwp = (wp) & (posSize - 1);
posWrite = posBuffer + nwp;
posWrite->offset = offset;
posWrite->realSize = rsize;
// printf("PUTSTART wp %i, start %x\n",nwp, offset);
return dataBuffer + offset;
}
//--------------------------------------------------------------------------
void cMyPacketBuffer::PutEnd (int size, int flags, uint64_t timestamp)
{
if (!posWrite)
return;
if (size > posWrite->realSize)
size = posWrite->realSize;
posWrite->size = size;
posWrite->flags = flags;
posWrite->timestamp = timestamp;
wp = (wp + 1) & (posSize - 1);
pthread_mutex_unlock (&m_lock);
}
//--------------------------------------------------------------------------
uchar *cMyPacketBuffer::GetStartSub (int *readp, int timeout, int *size, int *flags, uint64_t * timestamp)
{
clock_t starttime = 0;
if (*readp == wp && timeout)
starttime = Now ();
// printf("GET rp %i wp %i\n",readp,wp);
while (*readp == wp) {
if (!timeout || (Now () - starttime) > (clock_t) (timeout))
return 0;
usleep (20 * 1000);
}
#if 0
if (readp > posSize) {
// Fixme sync
return 0;
}
#endif
posRead = posBuffer + *readp;
if (flags)
*flags = posRead->flags;
if (size)
*size = posRead->size;
if (timestamp)
*timestamp = posRead->timestamp;
// printf("GET rp %i, offset %x\n",readp,posRead->offset);
return dataBuffer + posRead->offset;
}
//--------------------------------------------------------------------------
uchar *cMyPacketBuffer::GetStart (int *size, int *flags, uint64_t * timestamp)
{
if (posRead) {
#if 1
if (flags)
*flags = posRead->flags;
if (size)
*size = posRead->size;
return dataBuffer + posRead->offset;
#else
GetEnd ();
#endif
}
if (invalidate) {
rp = wp;
invalidate = 0;
return 0;
}
posReadNum = 1;
return GetStartSub (&rp, getTimeout, size, flags, timestamp);
}
//--------------------------------------------------------------------------
void cMyPacketBuffer::GetEnd (void)
{
if (!posRead)
return;
rp = (rp + posReadNum) & (posSize - 1);
posRead = NULL;
posReadNum = 0;
}
//--------------------------------------------------------------------------
// Try to get multiple PES at once
uchar *cMyPacketBuffer::GetStartMultiple (int maxsize, int *size, int *flags, uint64_t * timestamp)
{
uchar *buf, *lastbuf, *startbuf;
int sz, fl;
int readp, packets;
int totalsize;
int startflags;
int timeout = getTimeout;
uint64_t tsp, starttsp;
#if 0
if (posRead)
GetEnd ();
#endif
//printf("fill %d \r", (wp>=rp) ? wp-rp : posSize-rp+wp);
if (invalidate) {
rp = wp;
invalidate = 0;
return 0;
}
readp = rp;
startbuf = NULL;
lastbuf = NULL;
totalsize = 0;
packets = 0;
startflags = 0;
starttsp = 0;
while (1) {
sz = 0;
buf = GetStartSub (&readp, timeout, &sz, &fl, &tsp);
// printf("GOT %x %i\n",buf,sz);
if (!startbuf) {
if (!buf)
return NULL;
startbuf = buf;
startflags = fl;
starttsp = tsp;
} else {
if (lastbuf + sz != buf || // Buffer wraparound or no buffer
(totalsize + sz) > maxsize || //
fl != 0) { // packet start
if (size)
*size = totalsize;
if (flags)
*flags = startflags;
if (timestamp)
*timestamp = starttsp;
posReadNum = packets;
return startbuf;
}
}
readp = (readp + 1) & (posSize - 1);
packets++;
totalsize += sz;
lastbuf = buf;
timeout = 0;
}
return NULL;
}
//--------------------------------------------------------------------------
void cMyPacketBuffer::SetTimeouts (int PutTimeout, int GetTimeout)
{
int tps=sysconf(_SC_CLK_TCK);
putTimeout = PutTimeout*tps/1000;
getTimeout = GetTimeout*tps/1000;
}
//--------------------------------------------------------------------------
void cMyPacketBuffer::Invalidate (void)
{
invalidate = 1;
}