Debian Codegraph @

examples/test-mpeg2.c

raw
1 /*
2 * libiec61883 - Linux IEEE 1394 streaming media library.
3 * Copyright (C) 2004 Dan Dennedy
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include "../src/iec61883.h"
25 #include <stdio.h>
26 #include <sys/select.h>
27 #include <signal.h>
28 #include <string.h>
29 #include <stdlib.h>
30
31 static int g_done = 0;
32
33 static int write_packet (unsigned char *data, int len, unsigned int dropped, void *callback_data)
34 {
35 FILE *f = (FILE*) callback_data;
36
37 if (dropped)
38 fprintf (stderr, "\a%d packets dropped!\n", dropped);
39 return (fwrite (data, len, 1, f) < 1) ? -1 : 0;
40 }
41
42 static int read_packet (unsigned char *data, int n_packets, unsigned int dropped, void *callback_data)
43 {
44 FILE *f = (FILE*) callback_data;
45 return (fread (data, IEC61883_MPEG2_TSP_SIZE * n_packets, 1, f) < 1) ? -1 : 0;
46 }
47
48 static void sighandler (int sig)
49 {
50 g_done = 1;
51 }
52
53 static void mpeg2_receive (raw1394handle_t handle, FILE *f, int channel)
54 {
55 iec61883_mpeg2_t mpeg = iec61883_mpeg2_recv_init (handle, write_packet,
56 (void *)f );
57
58 if ( mpeg && iec61883_mpeg2_recv_start (mpeg, channel) == 0)
59 {
60 int fd = raw1394_get_fd (handle);
61 struct timeval tv;
62 fd_set rfds;
63 int result = 0;
64
65 signal (SIGINT, sighandler);
66 signal (SIGTERM, sighandler);
67 signal (SIGPIPE, sighandler);
68 fprintf (stderr, "Starting to receive\n");
69
70 do {
71 FD_ZERO (&rfds);
72 FD_SET (fd, &rfds);
73 tv.tv_sec = 0;
74 tv.tv_usec = 20000;
75
76 if (select (fd + 1, &rfds, NULL, NULL, &tv) > 0)
77 result = raw1394_loop_iterate (handle);
78
79 } while (g_done == 0 && result == 0);
80
81 fprintf (stderr, "done.\n");
82 }
83 iec61883_mpeg2_close (mpeg);
84 }
85
86 static void mpeg2_transmit (raw1394handle_t handle, FILE *f, int pid, int channel)
87 {
88 iec61883_mpeg2_t mpeg;
89
90 mpeg = iec61883_mpeg2_xmit_init (handle, read_packet, (void *)f );
91 if ( mpeg && iec61883_mpeg2_xmit_start (mpeg, pid, channel) == 0)
92 {
93 int fd = raw1394_get_fd (handle);
94 struct timeval tv;
95 fd_set rfds;
96 int result = 0;
97
98 signal (SIGINT, sighandler);
99 signal (SIGTERM, sighandler);
100 signal (SIGPIPE, sighandler);
101 fprintf (stderr, "Starting to transmit\n");
102
103 do {
104 FD_ZERO (&rfds);
105 FD_SET (fd, &rfds);
106 tv.tv_sec = 0;
107 tv.tv_usec = 20000;
108
109 if (select (fd + 1, &rfds, NULL, NULL, &tv) > 0)
110 result = raw1394_loop_iterate (handle);
111
112 } while (g_done == 0 && result == 0);
113
114 fprintf (stderr, "done.\n");
115 }
116 iec61883_mpeg2_close (mpeg);
117 }
118
119 int main (int argc, char *argv[])
120 {
121 raw1394handle_t handle = raw1394_new_handle_on_port (0);
122 nodeid_t node = 0xffc0;
123 FILE *f = NULL;
124 int is_transmit = 0;
125 int node_specified = 0;
126 int i;
127 int channel;
128 int bandwidth = -1;
129 int pid = -1;
130
131 for (i = 1; i < argc; i++) {
132 if (strncmp (argv[i], "-h", 2) == 0 ||
133 strncmp (argv[i], "--h", 3) == 0)
134 {
135 fprintf (stderr,
136 "usage: %s [[-r | -t] node-id] [-p pid] [- | file]\n"
137 " Use - to transmit MPEG2-TS from stdin, or\n"
138 " supply a filename to transmit from a MPEG2-TS file.\n"
139 " Otherwise, capture MPEG2-TS to stdout.\n"
140 " The default PID for transmit is -1 (use first found).\n",
141 argv[0]);
142 raw1394_destroy_handle (handle);
143 return 1;
144 } else if (strncmp (argv[i], "-t", 2) == 0) {
145 node |= atoi (argv[++i]);
146 is_transmit = 1;
147 node_specified = 1;
148 } else if (strncmp (argv[i], "-r", 2) == 0) {
149 node |= atoi (argv[++i]);
150 is_transmit = 0;
151 node_specified = 1;
152 } else if (strncmp (argv[i], "-p", 2) == 0) {
153 pid = atoi (argv[++i]);
154 is_transmit = 1;
155 } else if (strcmp (argv[i], "-") != 0) {
156 if (node_specified && !is_transmit)
157 f = fopen (argv[i], "wb");
158 else {
159 f = fopen (argv[i], "rb");
160 is_transmit = 1;
161 }
162 } else if (!node_specified) {
163 is_transmit = 1;
164 }
165 }
166
167 if (handle) {
168 int oplug = -1, iplug = -1;
169
170 if (is_transmit) {
171 if (f == NULL)
172 f = stdin;
173 if (node_specified) {
174 channel = iec61883_cmp_connect (handle,
175 raw1394_get_local_id (handle), &oplug, node, &iplug,
176 &bandwidth);
177 if (channel > -1) {
178 mpeg2_transmit (handle, f, pid, channel);
179 iec61883_cmp_disconnect (handle,
180 raw1394_get_local_id (handle), oplug, node, iplug,
181 channel, bandwidth);
182 } else {
183 fprintf (stderr, "Connect failed, reverting to broadcast channel 63.\n");
184 mpeg2_transmit (handle, f, pid, 63);
185 }
186 } else {
187 mpeg2_transmit (handle, f, pid, 63);
188 }
189 if (f != stdin)
190 fclose (f);
191
192 } else {
193 if (f == NULL)
194 f = stdout;
195 if (node_specified) {
196 channel = iec61883_cmp_connect (handle, node, &oplug,
197 raw1394_get_local_id (handle), &iplug, &bandwidth);
198 if (channel > -1) {
199 mpeg2_receive (handle, f, channel);
200 iec61883_cmp_disconnect (handle, node, oplug,
201 raw1394_get_local_id (handle), iplug,
202 channel, bandwidth);
203 } else {
204 fprintf (stderr, "Connect failed, reverting to broadcast channel 63.\n");
205 mpeg2_receive (handle, f, 63);
206 }
207 } else {
208 mpeg2_receive (handle, f, 0);
209 }
210 if (f != stdout)
211 fclose (f);
212 }
213 raw1394_destroy_handle (handle);
214 } else {
215 fprintf (stderr, "Failed to get libraw1394 handle\n");
216 return -1;
217 }
218
219 return 0;
220 }