summaryrefslogtreecommitdiff
path: root/p2pvr/scanner/p2scan.cpp
blob: 8fe35785213ed07b239a503314c3b4b4fedfbe68 (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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/*
 * tv_grab_dvb - dump dvb epg info in xmltv
 * Version 0.2 - 20/04/2004 - First Public Release
 *
 * Copyright (C) 2004 Mark Bryars <dvb at darkskiez d0t co d0t uk>
 *
 * DVB code Mercilessly ripped off from dvddate
 * dvbdate Copyright (C) Laurence Culhane 2002 <dvbdate@holmes.demon.co.uk>
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <errno.h>
#include <getopt.h>
#include <stdarg.h>
#include <stdint.h>
#include <signal.h>
#include <time.h>
#include <stdbool.h>
#include <assert.h>
#include <glibmm.h>
#include <boost/bind.hpp>
#include <boost/date_time/gregorian_calendar.hpp>

#include <linux/dvb/dmx.h>
#include "si_tables.h"
#include "tv_grab_dvb.h"

#include "variables.h"
#include "rowProcessor.h"

struct EitProgram {
	VariableType serviceID;
	VariableType eventID;
	VariableType title;
	VariableType titleLang;
	VariableType subtitle;
	VariableType descLang;
	VariableType desc1;
	VariableType desc2;
	VariableType desc3;
	VariableType videoAspect;
	VariableType videoFrameRate;
	VariableType videoHD;
	VariableType audioChannels;
	VariableType language;
	VariableType teletextSubtitleLang;
	VariableType category;
	VariableType dvbRating;
	VariableType contentItemID;
	VariableType contentSeriesID;
	VariableType contentRecommendation;
	VariableType startTime;
	VariableType stopTime;
};

Glib::ustring title("title");
#include "xmlObjectLoader.h"
#include "rowSet.h"
template <class C, class M>
const M &
getMember(M C::*t, C * p) {
	return p->*t;
}
SimpleMessageException(NoSuchAttribute);
class EitRows : public RowSet {
	public:
		EitRows(const xmlpp::Element * p);
		~EitRows();
		void execute(const RowProcessor *) const;
		void loadComplete(const CommonObjects *) {
		}
		void setFilter(const Glib::ustring &) {
		}
		unsigned int columnCount() const {
			return 1;
		}
		const Glib::ustring & getColumnName(unsigned int) const {
			return title;
		}
		VariableType getCurrentValue(const Glib::ustring &) const {
			return current->title;
		}
		VariableType getCurrentValue(unsigned int) const {
			return current->title;
		}
		bool isNull(unsigned int ) const {
			return false;
		}
		bool isNull(const Glib::ustring & ) const {
			return false;
		}
#define returnAttr(name) if (attrName == #name) return boost::bind(getMember<EitProgram, VariableType>, &EitProgram::name, boost::ref(current))
		RowAttribute resolveAttr(const Glib::ustring & attrName) const {
			returnAttr(serviceID);
			returnAttr(eventID);
			returnAttr(title);
			returnAttr(titleLang);
			returnAttr(subtitle);
			returnAttr(descLang);
			returnAttr(desc1);
			returnAttr(desc2);
			returnAttr(desc3);
			returnAttr(videoAspect);
			returnAttr(videoFrameRate);
			returnAttr(videoHD);
			returnAttr(audioChannels);
			returnAttr(language);
			returnAttr(teletextSubtitleLang);
			returnAttr(category);
			returnAttr(dvbRating);
			returnAttr(contentItemID);
			returnAttr(contentSeriesID);
			returnAttr(contentRecommendation);
			returnAttr(startTime);
			returnAttr(stopTime);
			throw NoSuchAttribute(attrName);
		}
	private:
		Variable demux;
		Variable timeout;
		void openInput() const;
		void closeInput() const;
		void readEventTables(const RowProcessor *) const;
		bool parseEIT(const u_char *data, size_t len, const RowProcessor *) const;
		void parseEventDescription(const u_char *data) const;
		void parseLongEventDescription(const u_char *data) const;
		void parseComponentDescription(const u_char *data) const;
		void parseContentDescription(const u_char *data) const;
		void parseRatingDescription(const u_char *data) const;
		void parseContentIdentifierDescription(const u_char *data) const;
		void parseDescription(const u_char * data, size_t len) const;

		VariableType convert(const char * txt, size_t len) const;

		mutable int fd_epg;
		mutable EitProgram * current;

		static const std::string ISO10646;
		static const std::string EitEncoding;
		static const std::string UTF8;
};

DECLARE_LOADER("eitrows", EitRows);

const std::string EitRows::ISO10646("ISO-10646");
const std::string EitRows::EitEncoding("ISO6937");
const std::string EitRows::UTF8("UTF8");

EitRows::EitRows(const xmlpp::Element * p) :
	RowSet(p),
	demux(p, "demux", false, "/dev/dvb/adapter0/demux0"),
	timeout(p, "timeout", false, 10000),
	fd_epg(0),
	current(NULL)
{
}

EitRows::~EitRows()
{
	closeInput();
}
void
EitRows::closeInput() const
{
	if (fd_epg) {
		close(fd_epg);
		fd_epg = 0;
	}
}

static int time_offset   = 0;
static int chan_filter	     = 0;
static int chan_filter_mask = 0;

/* Parse command line arguments. {{{ */
int do_options(int arg_count, char **arg_strings) {
	static const struct option Long_Options[] = {
		{"help", 0, 0, 'h'},
		{"chanidents", 1, 0, 'c'},
		{0, 0, 0, 0}
	};
	int Option_Index = 0;

	while (1) {
		int c = getopt_long(arg_count, arg_strings, "udscmpnht:o:f:i:e:", Long_Options, &Option_Index);
		switch (c) {
			case 'n':
				chan_filter = 0x4e;
				chan_filter_mask = 0xfe;
				break;
			case 'm':
				chan_filter = 0x4e;
				chan_filter_mask = 0xff;
				break;
			case 'p':
				chan_filter = 0x4f;
				chan_filter_mask = 0xff;
				break;
		}
	}
	return 0;
} /*}}}*/


VariableType
EitRows::convert(const char * txt, size_t len) const
{
	char enc[20];
	switch (*txt) {
		default:
			EitEncoding.copy(enc, EitEncoding.length());
			enc[EitEncoding.length()] = '\0';
			break;
		case 0x01 ... 0x05:
			snprintf(enc, sizeof(enc), "ISO-8859-%d\n", txt[0] + 4);
			txt += 1;
			len -= 1;
			break;
		case 0x10:
			snprintf(enc, sizeof(enc), "ISO-8859-%02x%02x\n", txt[1], txt[2]);
			txt += 3;
			len -= 3;
			break;
		case 0x11:
			ISO10646.copy(enc, ISO10646.length());
			enc[ISO10646.length()] = '\0';
			break;
		case 0x1F:
			// Values for the first byte of "0x00", "0x06" to "0x0F", and "0x12" to "0x1F" are reserved for future use.
			//fprintf(stderr, "Reserved encoding: %02x\n", txt[0]);
			//fprintf(stderr, "%d: %.*s\n", txt[1], len - 2, txt + 2);
		case 0x06 ... 0x0F:
		case 0x12 ... 0x1E:
		case 0x00: // empty string
			return boost::shared_ptr<Glib::ustring>(new Glib::ustring());
	}
	size_t used = 0, newlen = 0;
	GError * err = NULL;
	boost::shared_ptr<gchar> utf8 = boost::shared_ptr<gchar>(g_convert(txt, len, "utf-8", enc, &used, &newlen, &err), g_free);
	if (err) {
		throw Glib::ConvertError(err);
	}
	return boost::shared_ptr<Glib::ustring>(new Glib::ustring(utf8.get()));
}

void
EitRows::parseEventDescription(const u_char *data) const {
	assert(GetDescriptorTag(data) == 0x4D);
	const struct descr_short_event *evtdesc = reinterpret_cast<const struct descr_short_event *>(data);

	size_t evtlen = evtdesc->event_name_length;
	if (evtlen) {
		current->titleLang = Glib::ustring((const char *)&evtdesc->lang_code1, 3);
		current->title = convert((const char *)evtdesc->data, evtlen);
	}

	size_t dsclen = evtdesc->data[evtlen];
	if (dsclen) {
		current->subtitle = convert((const char *)evtdesc->data + evtlen + 1, dsclen);
	}
}

/* Parse 0x4E Extended Event Descriptor. {{{ */
void
EitRows::parseLongEventDescription(const u_char *data) const {
	assert(GetDescriptorTag(data) == 0x4E);
	const struct descr_extended_event *levt = reinterpret_cast<const struct descr_extended_event *>(data);
	bool non_empty = (levt->descriptor_number || levt->last_descriptor_number || levt->length_of_items || levt->data[0]);

	if (non_empty && levt->descriptor_number == 0) {
		current->descLang = Glib::ustring((const char *)&levt->lang_code1, 3);

		const u_char *p = reinterpret_cast<const u_char *>(&levt->data);
#ifndef NDEBUG
		const void *data_end = data + DESCR_GEN_LEN + GetDescriptorLength(data);
#endif
		while (p < levt->data + levt->length_of_items) {
			const struct item_extended_event *name = reinterpret_cast<const struct item_extended_event *>(p);
			size_t name_len = name->item_description_length;
			assert(p + ITEM_EXTENDED_EVENT_LEN + name_len < data_end);
			current->desc1 = convert((const char *)name->data, name_len);

			p += ITEM_EXTENDED_EVENT_LEN + name_len;

			const struct item_extended_event *value = reinterpret_cast<const struct item_extended_event *>(p);
			size_t value_len = value->item_description_length;
			assert(p + ITEM_EXTENDED_EVENT_LEN + value_len < data_end);
			current->desc2 = convert((const char *)value->data, value_len);

			p += ITEM_EXTENDED_EVENT_LEN + value_len;
		}
		const struct item_extended_event *text = reinterpret_cast<const struct item_extended_event *>(p);
		size_t len = text->item_description_length;
		if (non_empty && len) {
			current->desc3 = convert((const char *)text->data, len);
		}

	}
} /*}}}*/

/* Parse 0x50 Component Descriptor.  {{{
   video is a flag, 1=> output the video information, 0=> output the
   audio information.  seen is a pointer to a counter to ensure we
   only output the first one of each (XMLTV can't cope with more than
   one) */
void
EitRows::parseComponentDescription(const u_char *data) const {
	assert(GetDescriptorTag(data) == 0x50);
	const struct descr_component *dc = reinterpret_cast<const struct descr_component *>(data);

	switch (dc->stream_content) {
		case 0x01: // Video Info
				current->videoHD = ((dc->component_type - 1) & 0x08) ? 1 : 0;
				current->videoFrameRate = ((dc->component_type - 1) & 0x04) ? 30 : 25;
				current->videoAspect = ((dc->component_type - 1) & 0x03);
			break;
		case 0x02: // Audio Info
				current->audioChannels = dc->component_type;
					current->language = Glib::ustring((const char *)&dc->lang_code1, 3);
			break;
		case 0x03: // Teletext Info
				// FIXME: is there a suitable XMLTV output for this?
				// if ((dc->component_type)&0x10) //subtitles
				// if ((dc->component_type)&0x20) //subtitles for hard of hearing
				current->teletextSubtitleLang = Glib::ustring((const char *)&dc->lang_code1, 3);
			break;
			// case 0x04: // AC3 info
	}
} /*}}}*/

void
EitRows::parseContentDescription(const u_char *data) const {
	assert(GetDescriptorTag(data) == 0x54);
	const struct descr_content * dc = reinterpret_cast<const struct descr_content *>(data);
	for (const u_char * p = reinterpret_cast<const u_char*>(&dc->data); p < data + DESCR_GEN_LEN + dc->descriptor_length; p += NIBBLE_CONTENT_LEN) {
		const struct nibble_content *nc = reinterpret_cast<const nibble_content *>(p);
		int c1 = (nc->content_nibble_level_1 << 4) + nc->content_nibble_level_2;
		// This is weird in the uk, they use user but not content, and almost the same values
		current->category = c1 ? c1 : (nc->user_nibble_1 << 4) + nc->user_nibble_2;
	}
}

void
EitRows::parseRatingDescription(const u_char *data) const {
	assert(GetDescriptorTag(data) == 0x55);
	const struct descr_parental_rating * pr = reinterpret_cast<const struct descr_parental_rating *>(data);
	for (const u_char * p = reinterpret_cast<const u_char *>(&pr->data); p < data + DESCR_GEN_LEN + pr->descriptor_length; p += PARENTAL_RATING_ITEM_LEN) {
		const struct parental_rating_item *pr = reinterpret_cast<const struct parental_rating_item *>(p);
		switch (pr->rating) {
			case 0x00: /*undefined*/
				break;
			case 0x01 ... 0x0F:
				current->dvbRating = pr->rating + 3;
				break;
			case 0x10 ... 0xFF: /*broadcaster defined*/
				break;
		}
	}
}

int parsePrivateDataSpecifier(const u_char *data) {
	assert(GetDescriptorTag(data) == 0x5F);
	return GetPrivateDataSpecifier(data);
}

/* Parse 0x76 Content Identifier Descriptor. {{{ */
/* See ETSI TS 102 323, section 12 */
void
EitRows::parseContentIdentifierDescription(const u_char *data) const {
	assert(GetDescriptorTag(data) == 0x76);
	const struct descr_content_identifier *ci = reinterpret_cast<const struct descr_content_identifier *>(data);
	for (const u_char * p = reinterpret_cast<const u_char *>(&ci->data); p < data + DESCR_GEN_LEN + ci->descriptor_length; p += DESCR_GEN_LEN + ci->descriptor_length) {
		const struct descr_content_identifier_crid *crid = reinterpret_cast<const struct descr_content_identifier_crid *>(p);

		switch (crid->crid_location)
		{
			case 0x01: /* Carried in Content Identifier Table (CIT) */
			default:
				break;
			case 0x00: /* Carried explicitly within descriptor */
				struct descr_content_identifier_crid_local * crid_data = (descr_content_identifier_crid_local_t *)&crid->crid_ref_data;
				size_t len = crid_data->crid_length;
				switch (crid->crid_type) {
					case 0x01:
					case 0x31:
						current->contentItemID = Glib::ustring((const char *)crid_data->crid_byte, len);
						break;
					case 0x02:
					case 0x32:
						current->contentSeriesID = Glib::ustring((const char *)crid_data->crid_byte, len);
						break;
					case 0x03:
					case 0x33:
						current->contentRecommendation = Glib::ustring((const char *)crid_data->crid_byte, len);
						break;
				}
				break;
		}
	}
} /*}}}*/

/* Parse Descriptor. {{{
 * Tags should be output in this order:

'title', 'sub-title', 'desc', 'credits', 'date', 'category', 'language',
'orig-language', 'length', 'icon', 'url', 'country', 'episode-num',
'video', 'audio', 'previously-shown', 'premiere', 'last-chance',
'new', 'subtitles', 'rating', 'star-rating'
*/
void
EitRows::parseDescription(const u_char * data, size_t len) const {
	int pds = 0;
	for (const u_char * p = data; p < data + len; p += DESCR_GEN_LEN + GetDescriptorLength(p)) {
		const struct descr_gen *desc = reinterpret_cast<const struct descr_gen *>(p);
		switch (GetDescriptorTag(desc)) {
			case 0:
				break;
			case 0x4D: //short evt desc, [title] [sub-title]
				// there can be multiple language versions of these
				parseEventDescription(p);
				break;
			case 0x4E: //long evt descriptor [desc]
				parseLongEventDescription(p);
				break;
			case 0x50: //component desc [language] [video] [audio] [subtitles]
				parseComponentDescription(p);
				break;
			case 0x53: // CA Identifier Descriptor
				break;
			case 0x54: // content desc [category]
				parseContentDescription(p);
				break;
			case 0x55: // Parental Rating Descriptor [rating]
				parseRatingDescription(p);
				break;
			case 0x5f: // Private Data Specifier
				pds = parsePrivateDataSpecifier(p);
				break;
			case 0x64: // Data broadcast desc - Text Desc for Data components
				break;
			case 0x69: // Programm Identification Label
				break;
			case 0x81: // TODO ???
				if (pds == 5) // ARD_ZDF_ORF
					break;
			case 0x82: // VPS (ARD, ZDF, ORF)
				if (pds == 5) // ARD_ZDF_ORF
					// TODO: <programme @vps-start="???">
					break;
			case 0x4F: // Time Shifted Event
			case 0x52: // Stream Identifier Descriptor
			case 0x5E: // Multi Lingual Component Descriptor
			case 0x83: // Logical Channel Descriptor (some kind of news-ticker on ARD-MHP-Data?)
			case 0x84: // Preferred Name List Descriptor
			case 0x85: // Preferred Name Identifier Descriptor
			case 0x86: // Eacem Stream Identifier Descriptor
				break;
			case 0x76: // Content identifier descriptor
				parseContentIdentifierDescription(p);
				break;
			default:
				break;
		}
	}
} /*}}}*/

/* Check that program has at least a title as is required by xmltv.dtd. {{{ */
static bool validateDescription(const u_char *data, size_t len) {
	for (const u_char * p = data; p < data + len; p += DESCR_GEN_LEN + GetDescriptorLength(p)) {
		const struct descr_gen *desc = reinterpret_cast<const struct descr_gen *>(p);
		if (GetDescriptorTag(desc) == 0x4D) {
			const struct descr_short_event *evtdesc = reinterpret_cast<const struct descr_short_event *>(p);
			// make sure that title isn't empty
			if (evtdesc->event_name_length) return true;
		}
	}
	return false;
} /*}}}*/

class SeenProgram {
	public:
		SeenProgram(int sid, int eid);
		bool operator<(const SeenProgram &) const;
		const int sid;
		const int eid;
};
SeenProgram::SeenProgram(int s, int e) :
	sid(s),
	eid(e)
{
}
bool
SeenProgram::operator<(const SeenProgram & o) const
{
	return ((this->sid < o.sid) || ((this->sid == o.sid) && (this->eid < o.eid)));
}

typedef std::set<SeenProgram> SeenPrograms;
SeenPrograms seenPrograms;
bool
EitRows::parseEIT(const u_char *data, size_t len, const RowProcessor * rp) const {
	const struct eit *e = reinterpret_cast<const struct eit *>(data);

	len -= 4; //remove CRC

	// For each event listing
	bool found = false;
	for (const u_char *p = reinterpret_cast<const u_char *>(&e->data); p < data + len; p += EIT_EVENT_LEN + GetEITDescriptorsLoopLength(p)) {
		const struct eit_event *evt = reinterpret_cast<const struct eit_event *>(p);
		SeenProgram sp(HILO(e->service_id), HILO(evt->event_id));
		if (seenPrograms.find(sp) != seenPrograms.end()) {
			continue;
		}
		seenPrograms.insert(sp);

		// No program info at end! Just skip it
		if (GetEITDescriptorsLoopLength(evt) == 0) {
			continue;
		}

		boost::gregorian::date startDate(boost::gregorian::gregorian_calendar::from_modjulian_day_number(HILO(evt->mjd)));
		boost::posix_time::ptime startTime(startDate);
		startTime += boost::posix_time::time_duration(
					BcdCharToInt(evt->start_time_h) + time_offset,
					BcdCharToInt(evt->start_time_m),
					BcdCharToInt(evt->start_time_s));
		EitProgram results;
		current = &results;
		results.startTime = startTime;
		results.stopTime = startTime + boost::posix_time::time_duration(
				BcdCharToInt(evt->duration_h),
				BcdCharToInt(evt->duration_m),
				BcdCharToInt(evt->duration_s));

		// a program must have a title that isn't empty
		if (!validateDescription(reinterpret_cast<const u_char *>(&evt->data), GetEITDescriptorsLoopLength(evt))) {
			continue;
		}


		results.serviceID = HILO(e->service_id);
		results.eventID = HILO(evt->event_id);

		parseDescription(reinterpret_cast<const u_char *>(&evt->data), GetEITDescriptorsLoopLength(evt));
		rp->rowReady();
		rowNum += 1;
		found = true;
	}
	return found;
}

SimpleMessageException(ErrorReadingData);
SimpleMessageException(TimeoutReadingData);
void
EitRows::readEventTables(const RowProcessor * rp) const
{
	u_char buf[1<<12];
	struct pollfd ufd;
	memset(&ufd, 0, sizeof(pollfd));
	ufd.fd = fd_epg;
	ufd.events = POLLIN;
	size_t n;
	int prtn = 0;
	time_t lastuseful = time(NULL);
	while (((prtn = poll(&ufd, 1, timeout())) == 1) && (n = read(fd_epg, buf, sizeof(buf)))) {
		if (n < sizeof(struct si_tab))
			throw ErrorReadingData("Smaller that si_tab");
		struct si_tab *tab = (struct si_tab *)buf;
		size_t l = sizeof(struct si_tab) + GetSectionLength(tab);
		if (n < l)
			throw ErrorReadingData("Smaller that section length");
		if (_dvb_crc32(buf, l) == 0) {
			if (parseEIT(buf, l, rp)) {
				time(&lastuseful);
			}
			else {
				if (lastuseful < time(NULL) - 5) {
					break;
				}
			}
		}
	}
	if (prtn < 1) {
		throw TimeoutReadingData("Tuned to a multiplex?");
	}
}

SimpleMessageException(DemuxOpenFailure);
SimpleMessageException(DemuxSetFilterFailure);

void
EitRows::openInput() const
{
	if ((fd_epg = open(demux(), O_RDWR)) < 0) {
		throw DemuxOpenFailure(strerror(errno));
	}

	struct dmx_sct_filter_params sctFilterParams;
	memset(&sctFilterParams, 0, sizeof(dmx_sct_filter_params));
	sctFilterParams.pid = 18; // EIT data
	sctFilterParams.timeout = 0;
	sctFilterParams.flags =  DMX_IMMEDIATE_START;
	sctFilterParams.filter.filter[0] = chan_filter; // 4e is now/next this multiplex, 4f others
	sctFilterParams.filter.mask[0] = chan_filter_mask;

	if (ioctl(fd_epg, DMX_SET_FILTER, &sctFilterParams) < 0) {
		throw DemuxSetFilterFailure(strerror(errno));
	}
}

void
EitRows::execute(const RowProcessor * rp) const
{
	openInput();
	readEventTables(rp);
	closeInput();
}