aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_pes.c
blob: bd5a53ecb8dc665337809626325532445b1be44b (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
/* test_pes.c - Test for PES filters.
 * usage: DEMUX=/dev/dvb/adapterX/demuxX test_pes PID
 *
 * Copyright (C) 2002 convergence GmbH
 * Johannes Stezenbach <js@convergence.de>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * 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 Lesser 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.
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>

#include <linux/dvb/dmx.h>

#include "hex_dump.h"

#define MAX_PES_SIZE (4*1024)


void usage(void)
{
	fprintf(stderr, "usage: test_pes PID [filename]\n");
	fprintf(stderr, "       Print a hexdump of PES packets from PID to stdout.\n");
	fprintf(stderr, "  filename : Write binary PES data to file (no hexdump).\n");
	fprintf(stderr, "       The default demux device used can be changed\n");
	fprintf(stderr, "       using the DEMUX environment variable\n");
	exit(1);
}

void process_pes(int fd, FILE *out)
{
	uint8_t buf[MAX_PES_SIZE];
	int bytes;

	bytes = read(fd, buf, sizeof(buf));
	if (bytes < 0) {
		if (errno == EOVERFLOW) {
			fprintf(stderr, "read error: buffer overflow (%d)\n",
					EOVERFLOW);
			return;
		}
		else {
			perror("read");
			exit(1);
		}
	}
	if (out == stdout) {
		hex_dump(buf, bytes);
		printf("\n");
	}
	else {
		printf("got %d bytes\n", bytes);
		if (fwrite(buf, 1, bytes, out) == 0)
			perror("write output");
	}
}

int set_filter(int fd, unsigned int pid)
{
	struct dmx_pes_filter_params f;

	f.pid = (uint16_t) pid;
	f.input = DMX_IN_FRONTEND;
	f.output = DMX_OUT_TAP;
	f.pes_type = DMX_PES_OTHER;
	f.flags = DMX_IMMEDIATE_START;
	if (ioctl(fd, DMX_SET_PES_FILTER, &f) == -1) {
		perror("ioctl DMX_SET_PES_FILTER");
		return 1;
	}
	return 0;
}


int main(int argc, char *argv[])
{
	int dmxfd;
	unsigned long pid;
	char *dmxdev = "/dev/dvb/adapter0/demux0";
	FILE *out = stdout;

	if (argc != 2 && argc != 3)
		usage();

	pid = strtoul(argv[1], NULL, 0);
	if (pid > 0x1fff)
		usage();
	if (getenv("DEMUX"))
		dmxdev = getenv("DEMUX");

	fprintf(stderr, "test_pes: using '%s'\n", dmxdev);
	fprintf(stderr, "          PID 0x%04lx\n", pid);

	if (argc == 3) {
		out = fopen(argv[2], "wb");
		if (!out) {
			perror("open output file");
			exit(1);
		}
		fprintf(stderr, "          output to '%s'\n", argv[2]);
	}

	if ((dmxfd = open(dmxdev, O_RDWR)) < 0){
		perror("open");
		return 1;
	}

	if (set_filter(dmxfd, pid) != 0)
		return 1;

	for (;;) {
		process_pes(dmxfd, out);
	}

	close(dmxfd);
	return 0;
}