Debian Codegraph @

devrplay/devrplay.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /* $Id: devrplay.c,v 1.4 1999/03/21 00:44:48 boyns Exp $ */
2
3 /*
4 * Copyright (C) 1993-99 Mark R. Boyns <[email protected]>
5 *
6 * This file is part of rplay.
7 *
8 * rplay is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * rplay is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with rplay; see the file COPYING. If not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24 /*
25 * based on esddsp.c
26 * Copyright (C) 1998, 1999 Manish Singh <[email protected]>
27 */
28
•29 #define _GNU_SOURCE
30 #define __USE_GNU
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 #include <stdarg.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <fcntl.h>
•40 #include <sys/errno.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <sys/time.h>
44 #include <stdio.h>
45 #include "rplay.h"
46
47 #define DEVRPLAY_SOUND "devrplay"
48
49 #ifdef linux
50
51 #include <sys/soundcard.h>
52 #include <dlfcn.h>
53 #define REAL_LIBC RTLD_NEXT
54
55 static int rplay_fd = -1;
56 static int spool_id = -1;
57 static int dsp_fmt;
58 static int dsp_speed;
59 static int dsp_channels;
60 static int dsp_speed;
61 static int dsp_blksize;
62 static int streaming;
•63 static int opened;
64
65 static char *
66 getsound()
67 {
68 char *p = getenv("DEVRPLAY_SOUND");
69 return p ? p : DEVRPLAY_SOUND;
70 }
71
72 static char *
73 getinfo()
74 {
75 char *p = getenv("DEVRPLAY_INFO");
76 return p ? p : 0;
77 }
78
79 static char *
80 getopts()
81 {
82 char *p = getenv("DEVRPLAY_OPTS");
83 return p ? p : 0;
84 }
85
86 int
87 open(const char *pathname, int flags,...)
88 {
89 static int (*func)(const char *, int, mode_t) = NULL;
90 va_list args;
91 mode_t mode;
92 char response[RPTP_MAX_LINE];
93
94 if (!func)
95 func = (int (*)(const char *, int, mode_t)) dlsym(REAL_LIBC, "open");
96
97 va_start(args, flags);
98 mode = va_arg(args, mode_t);
99 va_end(args);
100
101 if (strcmp(pathname, "/dev/dsp") == 0)
102 {
•103 char response[RPTP_MAX_LINE];
104 char *acc;
105
106 rplay_fd = rptp_open(rplay_default_host(), RPTP_PORT, response,
107 sizeof(response));
108 if (rplay_fd < 0)
109 {
110 rptp_perror(rplay_default_host());
111 }
•112 else
113 {
114 rptp_putline(rplay_fd, "access");
115 rptp_getline(rplay_fd, response, sizeof(response));
116 acc = rptp_parse(response, "access");
117
118 if (!acc || !strchr(acc, 'w'))
119 {
120 fprintf(stderr,
121 "RPLAY-ERROR: please add 'w' to rplay.hosts or man rplay.hosts\n");
122 errno = EACCES;
123 close(rplay_fd);
124 rplay_fd = -1;
125 }
126 else
127 {
128 opened = 1;
129 }
130 }
131
132 return rplay_fd;
133 }
134 else
135 {
136 return (*func)(pathname, flags, mode);
137 }
138 }
139
140 static int
141 dspctl(int fd, int request, void *argp)
142 {
143 int *arg = (int *) argp;
144
145 switch (request)
146 {
147 case SNDCTL_DSP_SETFMT:
148 dsp_fmt = *arg;
149 break;
150
151 case SNDCTL_DSP_SPEED:
152 dsp_speed = *arg;
153 break;
154
155 case SNDCTL_DSP_STEREO:
156 dsp_channels = *arg ? 2 : 1;
157 break;
158
159 case SNDCTL_DSP_CHANNELS:
160 dsp_channels = *arg;
161 break;
162
163 case SNDCTL_DSP_GETBLKSIZE:
164 *arg = 65535;
165 break;
166
167 case SNDCTL_DSP_GETFMTS:
168 *arg = 0x38;
169 break;
170
171 case SNDCTL_DSP_GETCAPS:
172 *arg = 0;
173 break;
174
175 case SNDCTL_DSP_GETOSPACE:
176 {
177 audio_buf_info *info = (audio_buf_info *) argp;
178 info->fragments = 16;
179 info->fragstotal = 16;
180 info->fragsize = 4096;
181 info->bytes = 44100;
182 }
183 break;
184
185 default:
186 break;
187 }
188
•189 /*
190 if (spool_id == -1 && dsp_fmt && dsp_speed && dsp_channels)
191 {
192 char response[RPTP_MAX_LINE];
193
194 streaming = 1;
195 rptp_putline(rplay_fd,
196 "play input=flow input-info=%s,%d,%d,%d,%s %s sound=\"%s\"",
197 dsp_fmt == 16 ? "linear16" : "ulinear8",
198 dsp_speed,
199 dsp_fmt,
200 dsp_channels,
201 "little-endian",
202 getopts(),
203 getsound());
204 rptp_getline(rplay_fd, response, sizeof(response));
205
•206 if (response[0] != RPTP_OK)
207 {
208 errno = EINVAL;
209 return -1;
210 }
211
212 spool_id = atoi(1 + rptp_parse(response, "id"));
213
214 rptp_putline(rplay_fd, "put id=#%d size=0", spool_id);
215 rptp_getline(rplay_fd, response, sizeof(response));
216 }
•217 */
218
219 return 0;
220 }
221
222 int
223 ioctl(int fd, int request,...)
224 {
225 static int (*func)(int, int, void *) = NULL;
226 va_list args;
227 void *argp;
228
229 if (!func)
230 func = (int (*)(int, int, void *)) dlsym(REAL_LIBC, "ioctl");
231 va_start(args, request);
232 argp = va_arg(args, void *);
233 va_end(args);
234
235 if (fd != rplay_fd)
236 {
237 return (*func)(fd, request, argp);
238 }
239 else if (fd == rplay_fd)
240 {
241 return dspctl(fd, request, argp);
242 }
243 }
244
245 ssize_t
246 write(int fd, const void *buf, size_t count)
247 {
248 static int (*func)(int, const void *, size_t) = NULL;
249
250 if (!func)
251 func = (int (*)(int, const void *, size_t)) dlsym(REAL_LIBC, "write");
252
•253 if (fd == rplay_fd && !streaming && opened)
254 {
255 char info[64];
256 char response[RPTP_MAX_LINE];
257
258 info[0] = '\0';
259
260 /* use dsp defaults if something was specified */
261 if (dsp_speed || dsp_fmt || dsp_channels)
262 {
263 if (!dsp_speed) dsp_speed = 8000;
264 if (!dsp_fmt) dsp_fmt = 8;
265 if (!dsp_channels) dsp_channels = 1;
266 sprintf(info, "input-info=%s,%d,%d,%d,%s",
267 dsp_fmt == 16 ? "linear16" : "ulinear8",
268 dsp_speed,
269 dsp_fmt,
270 dsp_channels,
271 "little-endian");
272 }
273 /* try user specified sound info */
274 else if (getinfo())
275 {
276 strncpy(info, getinfo(), sizeof(info)-1);
277 }
278 /* otherwise let rplayd figure out the format using
279 the sound header and/or name. */
•280
281 streaming = 1;
282 rptp_putline(rplay_fd, "play input=flow %s sound=\"%s\"",
283 info, getsound());
284 rptp_getline(rplay_fd, response, sizeof(response));
285
286 spool_id = atoi(1 + rptp_parse(response, "id"));
287
288 rptp_putline(rplay_fd, "put id=#%d size=0", spool_id);
289 rptp_getline(rplay_fd, response, sizeof(response));
290 }
291
292 return (*func)(fd, buf, count);
293 }
294
295 int
296 close(int fd)
297 {
298 static int (*func)(int) = NULL;
299
300 if (!func)
301 func = (int (*)(int)) dlsym(REAL_LIBC, "close");
302
303 if (fd == rplay_fd)
304 {
305 rplay_fd = -1;
306 spool_id = -1;
307 streaming = 0;
•308 opened = 0;
309 dsp_fmt = dsp_speed = dsp_channels = dsp_speed = dsp_blksize = 0;
310 }
311
312 return (*func)(fd);
313 }
314
315 #endif /* linux */