Debian Codegraph @

examples/test-dv.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/poll.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_frame (unsigned char *data, int len, int complete, void *callback_data)
34 {
35 FILE *f = (FILE*) callback_data;
36
37 if (complete == 0)
38 fprintf (stderr, "Error: incomplete frame received!\n");
39 return (fwrite (data, len, 1, f) < 1) ? -1 : 0;
40 }
41
42 static int read_frame (unsigned char *data, int n, unsigned int dropped, void *callback_data)
43 {
44 FILE *f = (FILE*) callback_data;
45
46 if (n == 1)
47 if (fread (data, 480, 1, f) < 1)
48 return -1;
49 else
50 return 0;
51 else
52 return 0;
53 }
54
55 static void sighandler (int sig)
56 {
57 g_done = 1;
58 }
59
60 static void dv_receive( raw1394handle_t handle, FILE *f, int channel)
61 {
62 iec61883_dv_fb_t frame = iec61883_dv_fb_init (handle, write_frame, (void *)f );
63
64 if (frame && iec61883_dv_fb_start (frame, channel) == 0)
65 {
66 struct pollfd pfd = {
67 fd: raw1394_get_fd (handle),
68 events: POLLIN | POLLPRI,
69 revents: 0
70 };
71 int result = 0;
72
73 signal (SIGINT, sighandler);
74 signal (SIGPIPE, sighandler);
75 fprintf (stderr, "Starting to receive\n");
76
77 do {
78 if (poll (&pfd, 1, 100) > 0 && (pfd.revents & POLLIN))
79 result = raw1394_loop_iterate (handle);
80
81 } while (g_done == 0 && result == 0);
82
83 fprintf (stderr, "done.\n");
84 }
85 iec61883_dv_fb_close (frame);
86 }
87
88 static void dv_transmit( raw1394handle_t handle, FILE *f, int channel)
89 {
90 iec61883_dv_t dv;
91 unsigned char data[480];
92 int ispal;
93
94 fread (data, 480, 1, f);
95 ispal = (data[ 3 ] & 0x80) != 0;
96 dv = iec61883_dv_xmit_init (handle, ispal, read_frame, (void *)f );
97
98 if (dv && iec61883_dv_xmit_start (dv, channel) == 0)
99 {
100 struct pollfd pfd = {
101 fd: raw1394_get_fd (handle),
102 events: POLLIN,
103 revents: 0
104 };
105 int result = 0;
106
107 signal (SIGINT, sighandler);
108 signal (SIGPIPE, sighandler);
109 fprintf (stderr, "Starting to transmit %s\n", ispal ? "PAL" : "NTSC");
110
111 do {
112 int r = 0;
113 if ((r = poll (&pfd, 1, 100)) > 0 && (pfd.revents & POLLIN))
114 result = raw1394_loop_iterate (handle);
115
116 } while (g_done == 0 && result == 0);
117
118 fprintf (stderr, "done.\n");
119 }
120 iec61883_dv_close (dv);
121 }
122
123 int main (int argc, char *argv[])
124 {
125 raw1394handle_t handle = raw1394_new_handle_on_port (0);
126 nodeid_t node = 0xffc0;
127 FILE *f = NULL;
128 int is_transmit = 0;
129 int node_specified = 0;
130 int i;
131 int channel;
132 int bandwidth = -1;
133
134 for (i = 1; i < argc; i++) {
135
136 if (strncmp (argv[i], "-h", 2) == 0 ||
137 strncmp (argv[i], "--h", 3) == 0)
138 {
139 fprintf (stderr,
140 "usage: %s [[-r | -t] node-id] [- | file]\n"
141 " Use - to transmit raw DV from stdin, or\n"
142 " supply a filename to to transmit from a raw DV file.\n"
143 " Otherwise, capture raw DV to stdout.\n", argv[0]);
144 raw1394_destroy_handle (handle);
145 return 1;
146 } else if (strncmp (argv[i], "-t", 2) == 0) {
147 node |= atoi (argv[++i]);
148 is_transmit = 1;
149 node_specified = 1;
150 } else if (strncmp (argv[i], "-r", 2) == 0) {
151 node |= atoi (argv[++i]);
152 is_transmit = 0;
153 node_specified = 1;
154 } else if (strcmp (argv[i], "-") != 0) {
155 if (node_specified && !is_transmit)
156 f = fopen (argv[i], "wb");
157 else {
158 f = fopen (argv[i], "rb");
159 is_transmit = 1;
160 }
161 } else if (!node_specified) {
162 is_transmit = 1;
163 }
164 }
165
166 if (handle) {
167 int oplug = -1, iplug = -1;
168
169 if (is_transmit) {
170 if (f == NULL)
171 f = stdin;
172 if (node_specified) {
173 channel = iec61883_cmp_connect (handle,
174 raw1394_get_local_id (handle), &oplug, node, &iplug,
175 &bandwidth);
176 if (channel > -1) {
177 fprintf (stderr, "Connect succeeded, transmitting on channel %d.\n", channel);
178 dv_transmit (handle, f, 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 dv_transmit (handle, f, 63);
185 }
186 } else {
187 dv_transmit (handle, f, 63);
188 }
189 if (f != stdin)
190 fclose (f);
191 } else {
192 if (f == NULL)
193 f = stdout;
194 if (node_specified) {
195 channel = iec61883_cmp_connect (handle, node, &oplug,
196 raw1394_get_local_id (handle), &iplug, &bandwidth);
197 if (channel > -1) {
198 dv_receive (handle, f, channel);
199 iec61883_cmp_disconnect (handle, node, oplug,
200 raw1394_get_local_id (handle), iplug,
201 channel, bandwidth);
202 } else {
203 fprintf (stderr, "Connect failed, reverting to broadcast channel 63.\n");
204 dv_receive (handle, f, 63);
205 }
206 } else {
207 dv_receive (handle, f, 63);
208 }
209 if (f != stdout)
210 fclose (f);
211 }
212 raw1394_destroy_handle (handle);
213 } else {
214 fprintf (stderr, "Failed to get libraw1394 handle\n");
215 return -1;
216 }
217
218 return 0;
219 }