summaryrefslogtreecommitdiff
path: root/cmd/vmixctl/vmixctl.c
blob: cb487c56ee6c8416afdd7993b08576c06a2bd45d (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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * Purpose: Utility to control the vmix subsystem of Open Sound System
 */

/*
 *
 * This file is part of Open Sound System.
 *
 * Copyright (C) 4Front Technologies 1996-2008.
 *
 * This this source file is released under GPL v2 license (no other versions).
 * See the COPYING file included in the main directory of this source
 * distribution for the license terms and conditions.
 *
 */

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

char *cmdname=NULL;

#ifndef CONFIG_OSS_VMIX
int
main(int argc, char *argv[])
{
	fprintf (stderr, "%s: Virtual mixer is not included in this version of OSS\n", argv[0]);
	exit(1);
}
#else

static void
usage(void)
{
	fprintf (stdout, "Usage:\n");
	fprintf (stdout, "%s attach [attach_options...] devname\n", cmdname);
	fprintf (stdout, "%s attach [attach_options...] devname inputdev\n", cmdname);
	fprintf (stdout, "%s detach devname\n", cmdname);
	fprintf (stdout, "%s rate devname samplerate\n", cmdname);
	fprintf (stdout, "%s remap devname channels\n", cmdname);
	fprintf (stdout, "\n");
	fprintf (stdout, "Use ossinfo -a to find out the devname and inputdev parameters\n");
	fprintf (stdout, "Use ossinfo -a -v2 to find out a suitable sample rate.\n");
	fprintf (stdout, "\n");
	fprintf (stdout, "attach_options:\n");
	fprintf (stdout, "\n");
	fprintf (stdout, "\t-r\tDisable recording\n");
	fprintf (stdout, "\t-p\tDo not preallocate client engines\n");
	fprintf (stdout, "\t-M\tUse more fragments\n");
	fprintf (stdout, "\t-V\tMake clients visible by creating device files for them.\n");
	fprintf (stdout, "\t-c<N>\tPrecreate <N> client engines (see -p).\n");

	exit(-1);
}

static int
find_audiodev(char *fname, int mode, int *fd_)
{
	int fd;
	oss_audioinfo ai;

	if ((fd=*fd_=open(fname, mode | O_EXCL, 0))==-1)
	{
		perror(fname);
		exit(-1);
	}

	ai.dev=-1;
	if (ioctl(fd, SNDCTL_ENGINEINFO, &ai)==-1)
	{
		perror("SNDCTL_ENGINEINFO");
		fprintf (stderr, "Cannot get engine info for %s\n", fname);
		exit(-1);
	}

	return ai.dev;
}

static int
vmix_attach(int argc, char **argv)
{
	int masterfd, inputfd=-1;
	int masterdev, inputdev=-1;
	int c, n;
	int relink_devices = 0;
  	extern int optind;
  	extern char * optarg;

	vmixctl_attach_t att={0};

	att.attach_flags = VMIX_INSTALL_MANUAL;
/*
 * Simple command line switch handling.
 */
	argv++;argc--; /* Skip the initial command ("attach") */

  	while ((c = getopt (argc, argv, "MVc:pr")) != EOF)
    	   {
      		switch (c)
        	{
		case 'r': /* No input */
			att.attach_flags |= VMIX_INSTALL_NOINPUT;
			break;

		case 'p': /* No client engine preallocation */
			att.attach_flags |= VMIX_INSTALL_NOPREALLOC;
			break;

		case 'V': /* Allocate private device files for all clients */
			att.attach_flags |= VMIX_INSTALL_VISIBLE;
			relink_devices=1;
			break;

		case 'M': /* Use more fragments */
			att.attach_flags |= VMIX_MULTIFRAG;
			break;

		case 'c': /* Force prealloc of N client devices */
			n = atoi(optarg);
			if (n>255)n=255;
			if (n<0)n=0;
			att.attach_flags |= n;
			break;		

		default:
			usage();
		}
	   }

	if (optind >= argc)
	   usage();

	masterdev=find_audiodev(argv[optind], O_WRONLY, &masterfd);

	optind++;

	if (optind<argc)
	   inputdev=find_audiodev(argv[optind], O_RDONLY, &inputfd);
	   
	att.masterdev=masterdev;
	att.inputdev=inputdev;

	if (ioctl(masterfd, VMIXCTL_ATTACH, &att)==-1)
	{
		perror("VMIXCTL_ATTACH");
		exit(-1);
	}

	fprintf (stdout, "Virtual mixer attached to device.\n");

	if (relink_devices)
	{
		n = system("ossdetect -d");
		n = system("ossdevlinks");
	}

	return 0;
}

static int
vmix_detach(int argc, char **argv)
{
	int masterfd;
	int masterdev;

	vmixctl_attach_t att;

	masterdev=find_audiodev(argv[2], O_WRONLY, &masterfd);

	att.masterdev=masterdev;
	att.inputdev=-1;

	if (ioctl(masterfd, VMIXCTL_DETACH, &att)==-1)
	{
		perror("VMIXCTL_DETACH");
		exit(-1);
	}

	fprintf (stdout, "Virtual mixer detached from device.\n");

	return 0;
}

static int
vmix_rate(int argc, char **argv)
{
	int masterfd;
	int masterdev;

	vmixctl_rate_t rate;

	if (argc<4)
	{
		usage ();
	}

	masterdev=find_audiodev(argv[2], O_WRONLY, &masterfd);

	rate.masterdev=masterdev;
	rate.rate=atoi(argv[3]);

	if (ioctl(masterfd, VMIXCTL_RATE, &rate)==-1)
	{
		perror("VMIXCTL_RATE");
		exit(-1);
	}

	fprintf (stdout, "Virtual mixer rate change requested.\n");

	return 0;
}

static int
vmix_remap(int argc, char **argv)
{
	int i;
	int masterfd;
	int masterdev;

	vmixctl_map_t map;

	if (argc<4)
	{
		usage ();
	}

	masterdev=find_audiodev(argv[2], O_WRONLY, &masterfd);
	memset (&map, 0, sizeof (map));

	map.masterdev=masterdev;
	map.map[0]=atoi(argv[3]);

        for (i=4; i<argc; i++)
	{
		map.map[i-3] = atoi(argv[i]);
	}

	if (ioctl(masterfd, VMIXCTL_REMAP, &map)==-1)
	{
		perror("VMIXCTL_REMAP");
		exit(-1);
	}

	fprintf (stdout, "Virtual mixer map change requested.\n");

	return 0;
}

int
main(int argc, char **argv)
{
	cmdname=argv[0];

	if (argc < 3)
	{
		usage ();
	}

	if (strcmp(argv[1], "attach")==0)
	   exit(vmix_attach(argc, argv));

	if (strcmp(argv[1], "detach")==0)
	   exit(vmix_detach(argc, argv));

	if (strcmp(argv[1], "rate")==0)
	   exit(vmix_rate(argc, argv));

	if (strcmp(argv[1], "remap")==0)
	   exit(vmix_remap(argc, argv));

	usage();
	exit(0);
}
#endif