File: devrplay.c

package info (click to toggle)
rplay 3.3.2-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,132 kB
  • sloc: ansic: 42,169; makefile: 1,282; perl: 742; tcl: 345; sh: 311; java: 220
file content (315 lines) | stat: -rw-r--r-- 6,666 bytes parent folder | download | duplicates (9)
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* $Id: devrplay.c,v 1.4 1999/03/21 00:44:48 boyns Exp $ */

/*
 * Copyright (C) 1993-99 Mark R. Boyns <boyns@doit.org>
 *
 * This file is part of rplay.
 *
 * rplay is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * rplay 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with rplay; see the file COPYING.  If not, write to the
 * Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

/*
 * based on esddsp.c
 * Copyright (C) 1998, 1999 Manish Singh <yosh@gimp.org>
 */

#define _GNU_SOURCE
#define __USE_GNU

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <stdio.h>
#include "rplay.h"

#define DEVRPLAY_SOUND "devrplay"

#ifdef linux

#include <sys/soundcard.h>
#include <dlfcn.h>
#define REAL_LIBC RTLD_NEXT

static int rplay_fd = -1;
static int spool_id = -1;
static int dsp_fmt;
static int dsp_speed;
static int dsp_channels;
static int dsp_speed;
static int dsp_blksize;
static int streaming;
static int opened;

static char *
getsound()
{
    char *p = getenv("DEVRPLAY_SOUND");
    return p ? p : DEVRPLAY_SOUND;
}

static char *
getinfo()
{
    char *p = getenv("DEVRPLAY_INFO");
    return p ? p : 0;
}

static char *
getopts()
{
    char *p = getenv("DEVRPLAY_OPTS");
    return p ? p : 0;
}

int
open(const char *pathname, int flags,...)
{
    static int (*func)(const char *, int, mode_t) = NULL;
    va_list args;
    mode_t mode;
    char response[RPTP_MAX_LINE];

    if (!func)
	func = (int (*)(const char *, int, mode_t)) dlsym(REAL_LIBC, "open");

    va_start(args, flags);
    mode = va_arg(args, mode_t);
    va_end(args);

    if (strcmp(pathname, "/dev/dsp") == 0)
    {
        char response[RPTP_MAX_LINE];
        char *acc;

	rplay_fd = rptp_open(rplay_default_host(), RPTP_PORT, response,
			     sizeof(response));
	if (rplay_fd < 0)
	{
	    rptp_perror(rplay_default_host());
	}
        else
        {
            rptp_putline(rplay_fd, "access");
            rptp_getline(rplay_fd, response, sizeof(response));
            acc = rptp_parse(response, "access");

            if (!acc || !strchr(acc, 'w'))
            {
                fprintf(stderr,
                        "RPLAY-ERROR: please add 'w' to rplay.hosts or man rplay.hosts\n");
                errno = EACCES;
                close(rplay_fd);
                rplay_fd = -1;
            }
            else
            {
                opened = 1;
            }
        }

	return rplay_fd;
    }
    else
    {
	return (*func)(pathname, flags, mode);
    }
}

static int
dspctl(int fd, int request, void *argp)
{
    int *arg = (int *) argp;

    switch (request)
    {
    case SNDCTL_DSP_SETFMT:
	dsp_fmt = *arg;
	break;

    case SNDCTL_DSP_SPEED:
	dsp_speed = *arg;
	break;

    case SNDCTL_DSP_STEREO:
	dsp_channels = *arg ? 2 : 1;
	break;

    case SNDCTL_DSP_CHANNELS:
	dsp_channels = *arg;
	break;

    case SNDCTL_DSP_GETBLKSIZE:
	*arg = 65535;
	break;

    case SNDCTL_DSP_GETFMTS:
	*arg = 0x38;
	break;

    case SNDCTL_DSP_GETCAPS:
	*arg = 0;
	break;

    case SNDCTL_DSP_GETOSPACE:
	{
	    audio_buf_info *info = (audio_buf_info *) argp;
	    info->fragments = 16;
	    info->fragstotal = 16;
	    info->fragsize = 4096;
	    info->bytes = 44100;
	}
	break;

    default:
	break;
    }

    /*
    if (spool_id == -1 && dsp_fmt && dsp_speed && dsp_channels)
    {
	char response[RPTP_MAX_LINE];

	streaming = 1;
	rptp_putline(rplay_fd,
		     "play input=flow input-info=%s,%d,%d,%d,%s %s sound=\"%s\"",
		     dsp_fmt == 16 ? "linear16" : "ulinear8",
		     dsp_speed,
		     dsp_fmt,
		     dsp_channels,
		     "little-endian",
		     getopts(),
		     getsound());
	rptp_getline(rplay_fd, response, sizeof(response));

        if (response[0] != RPTP_OK)
        {
            errno = EINVAL;
            return -1;
        }

	spool_id = atoi(1 + rptp_parse(response, "id"));

	rptp_putline(rplay_fd, "put id=#%d size=0", spool_id);
	rptp_getline(rplay_fd, response, sizeof(response));
    }
    */

    return 0;
}

int
ioctl(int fd, int request,...)
{
    static int (*func)(int, int, void *) = NULL;
    va_list args;
    void *argp;

    if (!func)
	func = (int (*)(int, int, void *)) dlsym(REAL_LIBC, "ioctl");
    va_start(args, request);
    argp = va_arg(args, void *);
    va_end(args);

    if (fd != rplay_fd)
    {
	return (*func)(fd, request, argp);
    }
    else if (fd == rplay_fd)
    {
	return dspctl(fd, request, argp);
    }
}

ssize_t
write(int fd, const void *buf, size_t count)
{
    static int (*func)(int, const void *, size_t) = NULL;

    if (!func)
	func = (int (*)(int, const void *, size_t)) dlsym(REAL_LIBC, "write");

    if (fd == rplay_fd && !streaming && opened)
    {
	char info[64];
	char response[RPTP_MAX_LINE];

	info[0] = '\0';

	/* use dsp defaults if something was specified */
	if (dsp_speed || dsp_fmt || dsp_channels)
	{
	    if (!dsp_speed) dsp_speed = 8000;
	    if (!dsp_fmt) dsp_fmt = 8;
	    if (!dsp_channels) dsp_channels = 1;
	    sprintf(info, "input-info=%s,%d,%d,%d,%s",
		    dsp_fmt == 16 ? "linear16" : "ulinear8",
		    dsp_speed,
		    dsp_fmt,
		    dsp_channels,
		    "little-endian");
	}
	/* try user specified sound info */
	else if (getinfo())
	{
	    strncpy(info, getinfo(), sizeof(info)-1);
	}
	/* otherwise let rplayd figure out the format using
	   the sound header and/or name. */

	streaming = 1;
	rptp_putline(rplay_fd, "play input=flow %s sound=\"%s\"",
		     info, getsound());
	rptp_getline(rplay_fd, response, sizeof(response));

	spool_id = atoi(1 + rptp_parse(response, "id"));

	rptp_putline(rplay_fd, "put id=#%d size=0", spool_id);
	rptp_getline(rplay_fd, response, sizeof(response));
    }

    return (*func)(fd, buf, count);
}

int
close(int fd)
{
    static int (*func)(int) = NULL;

    if (!func)
	func = (int (*)(int)) dlsym(REAL_LIBC, "close");

    if (fd == rplay_fd)
    {
	rplay_fd = -1;
	spool_id = -1;
	streaming = 0;
        opened = 0;
	dsp_fmt = dsp_speed = dsp_channels = dsp_speed = dsp_blksize = 0;
    }

    return (*func)(fd);
}

#endif /* linux */