Debian Codegraph @

examples/test-amdtp.c

raw
1 /*
2 * libiec61883 - Linux IEEE 1394 streaming media library.
3 * Copyright (C) 2004 Kristian Hogsberg and 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 void sighandler (int sig)
34 {
35 g_done = 1;
36 }
37
38 int fill_packet (iec61883_amdtp_t amdtp, unsigned char *data, int nevents,
39 unsigned int dbc, unsigned int dropped, void *callback_data)
40 {
41 FILE *fp = (FILE *) callback_data;
42 static int total_packets = 0;
43 int nsamples = nevents * 2; // stereo 16bit PCM
44 unsigned char buffer [nsamples * 2];
45
46 if (fread (buffer, 2, nsamples, fp) != nsamples) {
47 return -1;
48 } else {
49 // TODO: convert from additional formats (20 or 24-bits).
50 unsigned char *p = buffer;
51 quadlet_t *event = (quadlet_t*) data;
52 int i;
53
54 for (i = 0; i < nsamples; i++) {
55 // Shift to allow room for label
56 event[i] = (p[1] << 16) | (p[0] << 8);
57 p += 2;
58 }
59 }
60
61 total_packets++;
62 if ((total_packets & 0xfff) == 0) {
63 fprintf (stderr, "\r%10d packets", total_packets);
64 fflush (stderr);
65 }
66
67 return 0;
68 }
69
70 static int read_packet (iec61883_amdtp_t amdtp, unsigned char *data, int nsamples,
71 unsigned int dbc, unsigned int dropped, void *callback_data)
72 {
73 FILE *f = (FILE*) callback_data;
74 static int total_packets = 0;
75
76 if (total_packets == 0)
77 fprintf (stderr, "format=0x%x sample_format=0x%x channels=%d rate=%d\n",
78 iec61883_amdtp_get_format (amdtp),
79 iec61883_amdtp_get_sample_format (amdtp),
80 iec61883_amdtp_get_dimension (amdtp),
81 iec61883_amdtp_get_rate (amdtp));
82
83 // TODO: convert to additional formats (20 or 24-bits).
84 if (iec61883_amdtp_get_format (amdtp) == IEC61883_AMDTP_FORMAT_RAW &&
85 iec61883_amdtp_get_sample_format (amdtp) == IEC61883_AMDTP_INPUT_LE16 &&
86 iec61883_amdtp_get_dimension (amdtp) == 2)
87 {
88 unsigned char buffer [nsamples * 2];
89 unsigned char *p = buffer;
90 quadlet_t *sample = (quadlet_t *) data;
91 int i;
92
93 for (i = 0; i < nsamples; i++) {
94 // the first byte contains a label - skip it
95 *p++ = (sample[i] >> 8);
96 *p++ = (sample[i] >> 16);
97 }
98
99 if (fwrite (buffer, 2, nsamples, f) != nsamples) {
100 return -1;
101 } else {
102 total_packets++;
103 if ((total_packets & 0xfff) == 0) {
104 fprintf (stderr, "\r%10d packets", total_packets);
105 fflush (stderr);
106 }
107 }
108 }
109 return 0;
110 }
111
112 static void amdtp_receive (raw1394handle_t handle, FILE *f, int channel)
113 {
114 iec61883_amdtp_t amdtp = iec61883_amdtp_recv_init (handle, read_packet, (void *)f );
115
116 if ( amdtp && iec61883_amdtp_recv_start (amdtp, channel) == 0)
117 {
118 int fd = raw1394_get_fd (handle);
119 struct timeval tv;
120 fd_set rfds;
121 int result = 0;
122
123 signal (SIGINT, sighandler);
124 signal (SIGPIPE, sighandler);
125 fprintf (stderr, "Starting to receive\n");
126
127 do {
128 FD_ZERO (&rfds);
129 FD_SET (fd, &rfds);
130 tv.tv_sec = 0;
131 tv.tv_usec = 20000;
132
133 if (select (fd + 1, &rfds, NULL, NULL, &tv) > 0)
134 result = raw1394_loop_iterate (handle);
135
136 } while (g_done == 0 && result == 0);
137
138 fprintf (stderr, "\ndone.\n");
139 }
140 iec61883_amdtp_close (amdtp);
141 }
142
143 static void amdtp_transmit (raw1394handle_t handle, FILE *f, int channel)
144 {
145 iec61883_amdtp_t amdtp;
146
147 amdtp = iec61883_amdtp_xmit_init (handle, 44100, IEC61883_AMDTP_FORMAT_RAW,
148 IEC61883_AMDTP_INPUT_LE16, IEC61883_MODE_BLOCKING_EMPTY, 2, fill_packet,
149 (void *)f );
150
151 if (amdtp && iec61883_amdtp_xmit_start (amdtp, channel) == 0)
152 {
153 int fd = raw1394_get_fd (handle);
154 struct timeval tv;
155 fd_set rfds;
156 int result = 0;
157
158 signal (SIGINT, sighandler);
159 signal (SIGPIPE, sighandler);
160 fprintf (stderr, "Starting to transmit\n");
161
162 do {
163 FD_ZERO (&rfds);
164 FD_SET (fd, &rfds);
165 tv.tv_sec = 0;
166 tv.tv_usec = 20000;
167
168 if (select (fd + 1, &rfds, NULL, NULL, &tv) > 0)
169 result = raw1394_loop_iterate (handle);
170
171 } while (g_done == 0 && result == 0);
172
173 fprintf (stderr, "\ndone.\n");
174 }
175 iec61883_amdtp_close (amdtp);
176 }
177
178 int main (int argc, char *argv[])
179 {
180 raw1394handle_t handle = raw1394_new_handle_on_port (0);
181 nodeid_t node = 0xffc0;
182 FILE *f = NULL;
183 int is_transmit = 0;
184 int node_specified = 0;
185 int i;
186 int channel;
187 int bandwidth = -1;
188
189 for (i = 1; i < argc; i++) {
190
191 if (strncmp (argv[i], "-h", 2) == 0 ||
192 strncmp (argv[i], "--h", 3) == 0)
193 {
194 fprintf (stderr,
195 "usage: %s [[-r | -t] node-id] [- | file]\n"
196 " All audio data must be signed 16bit 44.1KHz stereo PCM\n"
197 " Use - to transmit raw PCM from stdin, or\n"
198 " supply a filename to to transmit from a raw PCM file.\n"
199 " Otherwise, capture raw PCM to stdout.\n", argv[0]);
200 raw1394_destroy_handle (handle);
201 return 1;
202 } else if (strncmp (argv[i], "-t", 2) == 0) {
203 node |= atoi (argv[++i]);
204 is_transmit = 1;
205 node_specified = 1;
206 } else if (strncmp (argv[i], "-r", 2) == 0) {
207 node |= atoi (argv[++i]);
208 is_transmit = 0;
209 node_specified = 1;
210 } else if (strcmp (argv[i], "-") != 0) {
211 if (node_specified && !is_transmit)
212 f = fopen (argv[i], "wb");
213 else {
214 f = fopen (argv[i], "rb");
215 is_transmit = 1;
216 }
217 } else if (!node_specified) {
218 is_transmit = 1;
219 }
220 }
221
222 if (handle) {
223 int oplug = -1, iplug = -1;
224
225 if (is_transmit) {
226 if (f == NULL)
227 f = stdin;
228 if (node_specified) {
229 channel = iec61883_cmp_connect (handle,
230 raw1394_get_local_id (handle), &oplug, node, &iplug,
231 &bandwidth);
232 if (channel > -1) {
233 amdtp_transmit (handle, f, channel);
234 iec61883_cmp_disconnect (handle,
235 raw1394_get_local_id (handle), oplug, node, iplug,
236 channel, bandwidth);
237 } else {
238 fprintf (stderr, "Connect failed, reverting to broadcast channel 63.\n");
239 amdtp_transmit (handle, f, 63);
240 }
241 } else {
242 amdtp_transmit (handle, f, 63);
243 }
244 if (f != stdin)
245 fclose (f);
246 } else {
247 if (f == NULL)
248 f = stdout;
249 if (node_specified) {
250 channel = iec61883_cmp_connect (handle, node, &oplug,
251 raw1394_get_local_id (handle), &iplug, &bandwidth);
252 if (channel > -1) {
253 amdtp_receive (handle, f, channel);
254 iec61883_cmp_disconnect (handle, node, oplug,
255 raw1394_get_local_id (handle), iplug,
256 channel, bandwidth);
257 } else {
258 fprintf (stderr, "Connect failed, reverting to broadcast channel 63.\n");
259 amdtp_receive (handle, f, 63);
260 }
261 } else {
262 amdtp_receive (handle, f, 63);
263 }
264 if (f != stdout)
265 fclose (f);
266 }
267 raw1394_destroy_handle (handle);
268 } else {
269 fprintf (stderr, "Failed to get libraw1394 handle\n");
270 return -1;
271 }
272
273 return 0;
274 }