File: test-dv.c

package info (click to toggle)
libiec61883 1.2.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,600 kB
  • sloc: sh: 10,147; ansic: 4,614; makefile: 56
file content (219 lines) | stat: -rw-r--r-- 5,656 bytes parent folder | download | duplicates (7)
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
/*
 * libiec61883 - Linux IEEE 1394 streaming media library.
 * Copyright (C) 2004 Dan Dennedy
 *
 * This library 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 library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "../src/iec61883.h"
#include <stdio.h>
#include <sys/poll.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>

static int g_done = 0;

static int write_frame (unsigned char *data, int len, int complete, void *callback_data)
{
	FILE *f = (FILE*) callback_data;

	if (complete == 0)
		fprintf (stderr, "Error: incomplete frame received!\n");
	return (fwrite (data, len, 1, f) < 1) ? -1 : 0;
}

static int read_frame (unsigned char *data, int n, unsigned int dropped, void *callback_data)
{
	FILE *f = (FILE*) callback_data;

	if (n == 1)
		if (fread (data, 480, 1, f) < 1)
			return -1;
		else
			return 0;
	else
		return 0;
}

static void sighandler (int sig)
{
	g_done = 1;
}

static void dv_receive( raw1394handle_t handle, FILE *f, int channel)
{	
	iec61883_dv_fb_t frame = iec61883_dv_fb_init (handle, write_frame, (void *)f );
		
	if (frame && iec61883_dv_fb_start (frame, channel) == 0)
	{
		struct pollfd pfd = {
			fd: raw1394_get_fd (handle),
			events: POLLIN | POLLPRI,
			revents: 0
		};
		int result = 0;
		
		signal (SIGINT, sighandler);
		signal (SIGPIPE, sighandler);
		fprintf (stderr, "Starting to receive\n");

		do {
			if (poll (&pfd, 1, 100) > 0 && (pfd.revents & POLLIN))
				result = raw1394_loop_iterate (handle);
			
		} while (g_done == 0 && result == 0);
		
		fprintf (stderr, "done.\n");
	}
	iec61883_dv_fb_close (frame);
}

static void dv_transmit( raw1394handle_t handle, FILE *f, int channel)
{	
	iec61883_dv_t dv;
	unsigned char data[480];
	int ispal;
	
	fread (data, 480, 1, f);
	ispal = (data[ 3 ] & 0x80) != 0;
	dv = iec61883_dv_xmit_init (handle, ispal, read_frame, (void *)f );
	
	if (dv && iec61883_dv_xmit_start (dv, channel) == 0)
	{
		struct pollfd pfd = {
			fd: raw1394_get_fd (handle),
			events: POLLIN,
			revents: 0
		};
		int result = 0;
		
		signal (SIGINT, sighandler);
		signal (SIGPIPE, sighandler);
		fprintf (stderr, "Starting to transmit %s\n", ispal ? "PAL" : "NTSC");

		do {
			int r = 0;
			if ((r = poll (&pfd, 1, 100)) > 0 && (pfd.revents & POLLIN))
				result = raw1394_loop_iterate (handle);
			
		} while (g_done == 0 && result == 0);
		
		fprintf (stderr, "done.\n");
	}
	iec61883_dv_close (dv);
}

int main (int argc, char *argv[])
{
	raw1394handle_t handle = raw1394_new_handle_on_port (0);
	nodeid_t node = 0xffc0;
	FILE *f = NULL;
	int is_transmit = 0;
	int node_specified = 0;
	int i;
	int channel;
	int bandwidth = -1;
	
	for (i = 1; i < argc; i++) {
		
		if (strncmp (argv[i], "-h", 2) == 0 || 
			strncmp (argv[i], "--h", 3) == 0)
		{
			fprintf (stderr, 
			"usage: %s [[-r | -t] node-id] [- | file]\n"
			"       Use - to transmit raw DV from stdin, or\n"
			"       supply a filename to to transmit from a raw DV file.\n"
			"       Otherwise, capture raw DV to stdout.\n", argv[0]);
			raw1394_destroy_handle (handle);
			return 1;
		} else if (strncmp (argv[i], "-t", 2) == 0) {
			node |= atoi (argv[++i]);
			is_transmit = 1;
			node_specified = 1;
		} else if (strncmp (argv[i], "-r", 2) == 0) {
			node |= atoi (argv[++i]);
			is_transmit = 0;
			node_specified = 1;
		} else if (strcmp (argv[i], "-") != 0) {
			if (node_specified && !is_transmit)
				f = fopen (argv[i], "wb");
			else {
				f = fopen (argv[i], "rb");
				is_transmit = 1;
			}
		} else if (!node_specified) {
			is_transmit = 1;
		}
	}
		
	if (handle) {
		int oplug = -1, iplug = -1;
		
		if (is_transmit) {
			if (f == NULL)
				f = stdin;
			if (node_specified) {
				channel = iec61883_cmp_connect (handle,
					raw1394_get_local_id (handle), &oplug, node, &iplug,
					&bandwidth);
				if (channel > -1) {
					fprintf (stderr, "Connect succeeded, transmitting on channel %d.\n", channel);
					dv_transmit (handle, f, channel);
					iec61883_cmp_disconnect (handle,
						raw1394_get_local_id (handle), oplug, node, iplug,
						channel, bandwidth);
				} else {
					fprintf (stderr, "Connect failed, reverting to broadcast channel 63.\n");
					dv_transmit (handle, f, 63);
				}
			} else {
				dv_transmit (handle, f, 63);
			}
			if (f != stdin)
				fclose (f);
		} else {
			if (f == NULL)
				f = stdout;
			if (node_specified) {
				channel = iec61883_cmp_connect (handle, node, &oplug, 
					raw1394_get_local_id (handle), &iplug, &bandwidth);
				if (channel > -1) {
					dv_receive (handle, f, channel);
					iec61883_cmp_disconnect (handle, node, oplug, 
						raw1394_get_local_id (handle), iplug,
						channel, bandwidth);
				} else {
					fprintf (stderr, "Connect failed, reverting to broadcast channel 63.\n");
					dv_receive (handle, f, 63);
				}
			} else {
				dv_receive (handle, f, 63);
			}
			if (f != stdout)
				fclose (f);
		}
		raw1394_destroy_handle (handle);
	} else {
		fprintf (stderr, "Failed to get libraw1394 handle\n");
		return -1;
	}
	
	return 0;
}