summaryrefslogtreecommitdiff
path: root/client/filter.c
blob: ffefa4762cae3b2f8fe95c8845ddb237a34c7145 (plain)
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
/*
 *  $Id: filter.c,v 1.14 2009/02/13 13:02:39 schmirl Exp $
 */

#include "client/filter.h"
#include "client/socket.h"
#include "tools/select.h"
#include "common.h"
#include <sys/ioctl.h>
#include <string.h>

#include <vdr/device.h>

#define PID_MASK_HI 0x1F
// --- cStreamdevFilter ------------------------------------------------------

static int FilterSockBufSize_warn = 0;

class cStreamdevFilter: public cListObject {
private:
	uchar              m_Buffer[8192];
	int                m_Used;
	int                m_Pipe[2];
	u_short            m_Pid;
	u_char             m_Tid;
	u_char             m_Mask;
#ifdef TIOCOUTQ
	unsigned long      m_maxq;
	unsigned long      m_flushed;
#endif

public:
	cStreamdevFilter(u_short Pid, u_char Tid, u_char Mask);
	virtual ~cStreamdevFilter();

	bool Matches(u_short Pid, u_char Tid);
	bool PutSection(const uchar *Data, int Length, bool Pusi);
	int  ReadPipe(void) const { return m_Pipe[0]; }

	void Reset(void);

	u_short Pid(void) const { return m_Pid; }
	u_char Tid(void) const { return m_Tid; }
	u_char Mask(void) const { return m_Mask; }
};

inline bool cStreamdevFilter::Matches(u_short Pid, u_char Tid) {
	return m_Pid == Pid && m_Tid == (Tid & m_Mask);
}

cStreamdevFilter::cStreamdevFilter(u_short Pid, u_char Tid, u_char Mask) {
	m_Used = 0;
	m_Pid  = Pid;
	m_Tid  = Tid;
	m_Mask = Mask;
	m_Pipe[0] = m_Pipe[1] = -1;
#ifdef TIOCOUTQ
	m_flushed = 0;
	m_maxq = 0;
#endif

#ifdef SOCK_SEQPACKET  
	// SOCK_SEQPACKET (since kernel 2.6.4)
	if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, m_Pipe) != 0) {
		esyslog("streamdev-client: socketpair(SOCK_SEQPACKET) failed: %m, trying SOCK_DGRAM");
	}
#endif
	if (m_Pipe[0] < 0 && socketpair(AF_UNIX, SOCK_DGRAM, 0, m_Pipe) != 0) {
		esyslog("streamdev-client: couldn't open section filter socket: %m");
	} 

	// Set buffer for socketpair. During certain situations, such as startup, channel/transponder
	// change, VDR may lag in reading data. Instead of discarding it, we can buffer it.
	// Buffer size required may be up to 4MByte.

	if(StreamdevClientSetup.FilterSockBufSize) {
		int sbs = StreamdevClientSetup.FilterSockBufSize;
		int sbs2;
		unsigned int sbss = sizeof(sbs);
		int r;

		r = setsockopt(m_Pipe[1], SOL_SOCKET, SO_SNDBUF, (char *)&sbs, sbss);

		if(r < 0) {
			isyslog("streamdev-client: setsockopt(SO_SNDBUF, %d) = %s", sbs, strerror(errno));
		}
		sbs2 = 0;
		r = getsockopt(m_Pipe[1], SOL_SOCKET, SO_SNDBUF, (char *)&sbs2, &sbss);
		if(r < 0 || !sbss || !sbs2) {
			isyslog("streamdev-client: getsockopt(SO_SNDBUF, &%d, &%d) = %s", sbs2, sbss, strerror(errno));
		} else {
			// Linux actually returns double the requested size
			// if everything works fine. And it actually buffers up to that double amount
			// as can be seen from observing TIOCOUTQ (kernel 3.7/2014).

			if(sbs2 > sbs)
				sbs2 /= 2;
			if(sbs2 < sbs) {
				if(FilterSockBufSize_warn != sbs2) {
					isyslog("streamdev-client: ******************************************************");
					isyslog("streamdev-client: getsockopt(SO_SNDBUF) = %d < %d (configured).", sbs2, sbs);
					isyslog("streamdev-client: Consider increasing system buffer size:");
					isyslog("streamdev-client: 'sysctl net.core.wmem_max=%d'", sbs);
					isyslog("streamdev-client: ******************************************************");
					FilterSockBufSize_warn = sbs2;
				}
			}
		}
	} 

	if(fcntl(m_Pipe[0], F_SETFL, O_NONBLOCK) != 0 ||
		fcntl(m_Pipe[1], F_SETFL, O_NONBLOCK) != 0) {
		esyslog("streamdev-client: couldn't set section filter socket to non-blocking mode: %m");
	}
}

cStreamdevFilter::~cStreamdevFilter() {
	Dprintf("~cStreamdevFilter %p\n", this);

	if (m_Pipe[0] >= 0) {
		close(m_Pipe[0]);
	}
	if (m_Pipe[1] >= 0) {
		close(m_Pipe[1]);
	}
}

bool cStreamdevFilter::PutSection(const uchar *Data, int Length, bool Pusi) {

	if (!m_Used && !Pusi) /* wait for payload unit start indicator */
		return true;
	if (m_Used && Pusi)   /* reset at payload unit start */
		Reset();

	if (m_Used + Length >= (int)sizeof(m_Buffer)) {
		esyslog("ERROR: Streamdev: Section handler buffer overflow (%d bytes lost)",
				Length);
		Reset();
		return true;
	}

	memcpy(m_Buffer + m_Used, Data, Length);
	m_Used += Length;
	if (m_Used > 3) {
		int length = (((m_Buffer[1] & 0x0F) << 8) | m_Buffer[2]) + 3;
		if (m_Used == length) {
			m_Used = 0;
#ifdef TIOCOUTQ
			// If we can determine the queue size of the socket,
			// we flush rather then let the socket drop random packets.
			// This ensures that we have more contiguous set of packets
			// on the receiver side.
			if(m_flushed) {
				unsigned long queue = 0;
				ioctl(m_Pipe[1], TIOCOUTQ, &queue);
				if(queue > m_maxq)
					m_maxq = queue;
				if(queue * 2 < m_maxq) {
					dsyslog("cStreamdevFilter::PutSection(Pid:%d Tid: %d): "
					        "Flushed %ld bytes, max queue: %ld",
					        m_Pid, m_Tid, m_flushed, m_maxq);
					m_flushed = m_maxq = 0;

				} else {
					m_flushed += length;
				}
			}
			if(!m_flushed)
#endif
			if(write(m_Pipe[1], m_Buffer, length) < 0) {
				if(errno != EAGAIN && errno != EWOULDBLOCK) {
					dsyslog("cStreamdevFilter::PutSection(Pid:%d Tid: %d): error: %s",
					         m_Pid, m_Tid, strerror(errno));
					return false;
				} else {
#ifdef TIOCOUTQ
					m_flushed += length;
#else
					dsyslog("cStreamdevFilter::PutSection(Pid:%d Tid: %d): "
					        "Dropping packet %ld bytes (queue overflow)",
					        m_Pid, m_Tid, length);
#endif
				}
			}
		}

		if (m_Used > length) {
			dsyslog("cStreamdevFilter::PutSection: m_Used > length !  Pid %2d, Tid%2d "
				"(len %3d, got %d/%d)", m_Pid, m_Tid, Length, m_Used, length);
			if(Length < TS_SIZE-5) {
				// TS packet not full -> this must be last TS packet of section data -> safe to reset now
				Reset();
			}
		}

	}
	return true;
}

void cStreamdevFilter::Reset(void) {
	if(m_Used)
		dsyslog("cStreamdevFilter::Reset skipping %d bytes", m_Used);
	m_Used = 0;
}

// --- cStreamdevFilters -----------------------------------------------------

cStreamdevFilters::cStreamdevFilters(cClientSocket *ClientSocket):
		cThread("streamdev-client: sections assembler") {
	m_ClientSocket = ClientSocket;
	m_TSBuffer = NULL;
}

cStreamdevFilters::~cStreamdevFilters() {
	SetConnection(-1);
}

int cStreamdevFilters::OpenFilter(u_short Pid, u_char Tid, u_char Mask) {
	cStreamdevFilter *f = new cStreamdevFilter(Pid, Tid, Mask);
	int fh = f->ReadPipe();

	LOCK_THREAD;
	Add(f);

	return fh;
}

void cStreamdevFilters::CloseFilter(int Handle) {
	LOCK_THREAD;

	for (cStreamdevFilter *fi = First(); fi; fi = Next(fi)) {
		if(fi->ReadPipe() == Handle) {
			// isyslog("cStreamdevFilters::CloseFilter(%d): Pid %4d, Tid %3d, Mask %2x (%d filters left)\n",
			//		Handle, (int)fi->Pid(), (int)fi->Tid(), fi->Mask(), Count()-1);
			Del(fi);
			return;
		}
	}
	esyslog("cStreamdevFilters::CloseFilter(%d): failed (%d filters left)\n", Handle, Count()-1);
}

bool cStreamdevFilters::ReActivateFilters(void)
{
	LOCK_THREAD;

	bool res = true;
	for (cStreamdevFilter *fi = First(); fi; fi = Next(fi)) {
		res = m_ClientSocket->SetFilter(fi->Pid(), fi->Tid(), fi->Mask(), true) && res;
		Dprintf("ReActivateFilters(%d, %d, %d) -> %s", fi->Pid(), fi->Tid(), fi->Mask(), res ? "Ok" :"FAIL");
	}
	return res;
}

void cStreamdevFilters::SetConnection(int Handle) {

	Cancel(2);
	DELETENULL(m_TSBuffer);

	if (Handle >= 0) {
		m_TSBuffer = new cTSBuffer(Handle, MEGABYTE(1), 1);
		ReActivateFilters();
		Start();
	}
}

void cStreamdevFilters::Action(void) {
	int fails = 0;

	while (Running()) {
		const uchar *block = m_TSBuffer->Get();
		if (block) {
			u_short pid = (((u_short)block[1] & PID_MASK_HI) << 8) | block[2];
			u_char tid = block[3];
			bool Pusi = block[1] & 0x40;
			// proprietary extension
			int len = block[4];
#if 0
			if (block[1] == 0xff &&
					block[2] == 0xff &&
					block[3] == 0xff &&
					block[4] == 0x7f)
				isyslog("*********** TRANSPONDER -> %s **********", block+5);
#endif
			LOCK_THREAD;
			cStreamdevFilter *f = First();
			while (f) {
				cStreamdevFilter *next = Next(f);
				if (f->Matches(pid, tid)) {

					if (f->PutSection(block + 5, len, Pusi))
						break;

					if (errno != ECONNREFUSED && 
							errno != ECONNRESET &&
							errno != EPIPE) {
						Dprintf("FATAL ERROR: %m\n");
						esyslog("streamdev-client: couldn't send section packet: %m");
					}
					m_ClientSocket->SetFilter(f->Pid(), f->Tid(), f->Mask(), false);
					Del(f);
					// Filter was closed.
					//  - need to check remaining filters for another match
				}
				f = next;
			}
		} else {
#if 1 // TODO: this should be fixed in vdr cTSBuffer
			// Check disconnection
			int fd = *m_ClientSocket->DataSocket(siLiveFilter);
			if(fd < 0)
				break;
			cPoller Poller(fd);
			if (Poller.Poll()) {
				char tmp[1];
				errno = 0;
				Dprintf("cStreamdevFilters::Action(): checking connection");
				if (recv(fd, tmp, 1, MSG_PEEK) == 0 && errno != EAGAIN) {
					++fails;
					if (fails >= 10) {
						esyslog("cStreamdevFilters::Action(): stream disconnected ?");
						m_ClientSocket->CloseDataConnection(siLiveFilter);
						break;
					}
				} else {
					fails = 0;
				}
			} else {
				fails = 0;
			}
			cCondWait::SleepMs(10);
#endif
		}
	}

	DELETENULL(m_TSBuffer);
	dsyslog("StreamdevFilters::Action() ended");
}