/
SDL_openbsdaudio.c
406 lines (354 loc) · 9.68 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2004 Sam Lantinga
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
20
slouken@libsdl.org
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
*/
/*
* Driver for native OpenBSD audio(4).
* vedge@vedge.com.ar.
*/
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/audioio.h>
37
#include "SDL_timer.h"
38
#include "SDL_audio.h"
39
40
41
#include "../SDL_audiomem.h"
#include "../SDL_audio_c.h"
#include "../SDL_audiodev_c.h"
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
#include "SDL_openbsdaudio.h"
/* The tag name used by OpenBSD audio */
#define OBSD_DRIVER_NAME "openbsd"
/* Open the audio device for playback, and don't block if busy */
/* #define USE_BLOCKING_WRITES */
/* Use timer for synchronization */
/* #define USE_TIMER_SYNC */
/* #define DEBUG_AUDIO */
/* #define DEBUG_AUDIO_STREAM */
#ifdef USE_BLOCKING_WRITES
#define OPEN_FLAGS O_WRONLY
#else
#define OPEN_FLAGS (O_WRONLY|O_NONBLOCK)
#endif
/* Audio driver functions */
static void OBSD_WaitAudio(_THIS);
static int OBSD_OpenAudio(_THIS, SDL_AudioSpec *spec);
static void OBSD_PlayAudio(_THIS);
static Uint8 *OBSD_GetAudioBuf(_THIS);
static void OBSD_CloseAudio(_THIS);
#ifdef DEBUG_AUDIO
static void OBSD_Status(_THIS);
#endif
/* Audio driver bootstrap functions */
static int
Audio_Available(void)
{
int fd;
int available;
available = 0;
fd = SDL_OpenAudioPath(NULL, 0, OPEN_FLAGS, 0);
if(fd >= 0) {
available = 1;
close(fd);
}
return(available);
}
static void
Audio_DeleteDevice(SDL_AudioDevice *device)
{
93
94
SDL_free(device->hidden);
SDL_free(device);
95
96
97
98
99
100
101
102
}
static SDL_AudioDevice
*Audio_CreateDevice(int devindex)
{
SDL_AudioDevice *this;
/* Initialize all variables that we clean on shutdown */
103
this = (SDL_AudioDevice*)SDL_malloc(sizeof(SDL_AudioDevice));
104
if(this) {
105
SDL_memset(this, 0, (sizeof *this));
106
this->hidden =
107
(struct SDL_PrivateAudioData*)SDL_malloc((sizeof *this->hidden));
108
109
110
}
if((this == NULL) || (this->hidden == NULL)) {
SDL_OutOfMemory();
111
if(this) SDL_free(this);
112
113
return(0);
}
114
SDL_memset(this->hidden, 0, (sizeof *this->hidden));
115
116
117
118
119
120
121
122
123
124
125
126
127
128
audio_fd = -1;
/* Set the function pointers */
this->OpenAudio = OBSD_OpenAudio;
this->WaitAudio = OBSD_WaitAudio;
this->PlayAudio = OBSD_PlayAudio;
this->GetAudioBuf = OBSD_GetAudioBuf;
this->CloseAudio = OBSD_CloseAudio;
this->free = Audio_DeleteDevice;
return this;
}
129
AudioBootStrap OPENBSD_AUDIO_bootstrap = {
130
131
132
133
134
135
136
137
OBSD_DRIVER_NAME, "Native OpenBSD audio",
Audio_Available, Audio_CreateDevice
};
/* This function waits until it is possible to write a full sound buffer */
static void
OBSD_WaitAudio(_THIS)
{
138
139
140
141
142
143
144
145
146
147
/* Check to see if the thread-parent process is still alive */
{ static int cnt = 0;
/* Note that this only works with thread implementations
that use a different process id for each thread.
*/
if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */
if ( kill(parent, 0) < 0 ) {
this->enabled = 0;
}
}
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
#ifndef USE_BLOCKING_WRITES /* Not necessary when using blocking writes */
/* See if we need to use timed audio synchronization */
if ( frame_ticks ) {
/* Use timer for general audio synchronization */
Sint32 ticks;
ticks = ((Sint32)(next_frame - SDL_GetTicks()))-FUDGE_TICKS;
if ( ticks > 0 ) {
SDL_Delay(ticks);
}
} else {
/* Use select() for audio synchronization */
fd_set fdset;
struct timeval timeout;
FD_ZERO(&fdset);
FD_SET(audio_fd, &fdset);
timeout.tv_sec = 10;
timeout.tv_usec = 0;
#ifdef DEBUG_AUDIO
fprintf(stderr, "Waiting for audio to get ready\n");
#endif
if ( select(audio_fd+1, NULL, &fdset, NULL, &timeout) <= 0 ) {
const char *message =
"Audio timeout - buggy audio driver? (disabled)";
/* In general we should never print to the screen,
but in this case we have no other way of letting
the user know what happened.
*/
fprintf(stderr, "SDL: %s\n", message);
this->enabled = 0;
/* Don't try to close - may hang */
audio_fd = -1;
#ifdef DEBUG_AUDIO
fprintf(stderr, "Done disabling audio\n");
#endif
}
#ifdef DEBUG_AUDIO
fprintf(stderr, "Ready!\n");
189
190
191
192
193
194
195
196
#endif
}
#endif /* !USE_BLOCKING_WRITES */
}
static void
OBSD_PlayAudio(_THIS)
{
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
int written, p=0;
/* Write the audio data, checking for EAGAIN on broken audio drivers */
do {
written = write(audio_fd, &mixbuf[p], mixlen-p);
if (written>0)
p += written;
if (written == -1 && errno != 0 && errno != EAGAIN && errno != EINTR)
{
/* Non recoverable error has occurred. It should be reported!!! */
perror("audio");
break;
}
if ( p < written || ((written < 0) && ((errno == 0) || (errno == EAGAIN))) ) {
SDL_Delay(1); /* Let a little CPU time go by */
}
} while ( p < written );
/* If timer synchronization is enabled, set the next write frame */
if ( frame_ticks ) {
next_frame += frame_ticks;
}
220
221
222
223
224
225
226
/* If we couldn't write, assume fatal error for now */
if ( written < 0 ) {
this->enabled = 0;
}
#ifdef DEBUG_AUDIO
fprintf(stderr, "Wrote %d bytes of audio data\n", written);
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
#endif
}
static Uint8
*OBSD_GetAudioBuf(_THIS)
{
return(mixbuf);
}
static void
OBSD_CloseAudio(_THIS)
{
if(mixbuf != NULL) {
SDL_FreeAudioMem(mixbuf);
mixbuf = NULL;
}
if(audio_fd >= 0) {
close(audio_fd);
audio_fd = -1;
}
}
#ifdef DEBUG_AUDIO
void
OBSD_Status(_THIS)
{
audio_info_t info;
if(ioctl(audio_fd, AUDIO_GETINFO, &info) < 0) {
fprintf(stderr,"AUDIO_GETINFO failed.\n");
return;
}
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
fprintf(stderr,"\n"
"[play/record info]\n"
"buffer size : %d bytes\n"
"sample rate : %i Hz\n"
"channels : %i\n"
"precision : %i-bit\n"
"encoding : 0x%x\n"
"seek : %i\n"
"sample count : %i\n"
"EOF count : %i\n"
"paused : %s\n"
"error occured : %s\n"
"waiting : %s\n"
"active : %s\n"
"",
275
276
277
278
279
280
281
282
283
284
285
286
287
info.play.buffer_size,
info.play.sample_rate,
info.play.channels,
info.play.precision,
info.play.encoding,
info.play.seek,
info.play.samples,
info.play.eof,
info.play.pause ? "yes" : "no",
info.play.error ? "yes" : "no",
info.play.waiting ? "yes" : "no",
info.play.active ? "yes": "no");
288
289
290
291
292
293
294
295
fprintf(stderr,"\n"
"[audio info]\n"
"monitor_gain : %i\n"
"hw block size : %d bytes\n"
"hi watermark : %i\n"
"lo watermark : %i\n"
"audio mode : %s\n"
"",
296
297
298
299
300
301
info.monitor_gain,
info.blocksize,
info.hiwat, info.lowat,
(info.mode == AUMODE_PLAY) ? "PLAY"
: (info.mode = AUMODE_RECORD) ? "RECORD"
: (info.mode == AUMODE_PLAY_ALL ? "PLAY_ALL"
302
: "?"));
303
304
305
306
307
308
309
}
#endif /* DEBUG_AUDIO */
static int
OBSD_OpenAudio(_THIS, SDL_AudioSpec *spec)
{
char audiodev[64];
310
Uint16 format;
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
audio_info_t info;
AUDIO_INITINFO(&info);
/* Calculate the final parameters for this audio specification */
SDL_CalculateAudioSpec(spec);
#ifdef USE_TIMER_SYNC
frame_ticks = 0.0;
#endif
/* Open the audio device */
audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 0);
if(audio_fd < 0) {
SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno));
return(-1);
}
/* Set to play mode */
info.mode = AUMODE_PLAY;
if(ioctl(audio_fd, AUDIO_SETINFO, &info) < 0) {
SDL_SetError("Couldn't put device into play mode");
return(-1);
}
mixbuf = NULL;
337
338
339
AUDIO_INITINFO(&info);
for (format = SDL_FirstAudioFormat(spec->format);
format; format = SDL_NextAudioFormat())
340
{
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
switch(format) {
case AUDIO_U8:
info.play.encoding = AUDIO_ENCODING_ULINEAR;
info.play.precision = 8;
break;
case AUDIO_S8:
info.play.encoding = AUDIO_ENCODING_SLINEAR;
info.play.precision = 8;
break;
case AUDIO_S16LSB:
info.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
info.play.precision = 16;
break;
case AUDIO_S16MSB:
info.play.encoding = AUDIO_ENCODING_SLINEAR_BE;
info.play.precision = 16;
break;
case AUDIO_U16LSB:
info.play.encoding = AUDIO_ENCODING_ULINEAR_LE;
info.play.precision = 16;
break;
case AUDIO_U16MSB:
info.play.encoding = AUDIO_ENCODING_ULINEAR_BE;
info.play.precision = 16;
break;
default:
continue;
368
}
369
370
if (ioctl(audio_fd, AUDIO_SETINFO, &info) == 0)
break;
371
372
}
373
if(!format) {
374
375
376
377
SDL_SetError("No supported encoding for 0x%x", spec->format);
return(-1);
}
378
spec->format = format;
379
380
AUDIO_INITINFO(&info);
381
info.play.channels = spec->channels;
382
383
384
if (ioctl(audio_fd, AUDIO_SETINFO, &info) == -1)
spec->channels = 1;
AUDIO_INITINFO(&info);
385
info.play.sample_rate = spec->freq;
386
387
388
(void)ioctl(audio_fd, AUDIO_SETINFO, &info);
(void)ioctl(audio_fd, AUDIO_GETINFO, &info);
spec->freq = info.play.sample_rate;
389
390
391
392
393
394
/* Allocate mixing buffer */
mixlen = spec->size;
mixbuf = (Uint8*)SDL_AllocAudioMem(mixlen);
if(mixbuf == NULL) {
return(-1);
}
395
SDL_memset(mixbuf, spec->silence, spec->size);
396
397
398
399
400
401
402
403
404
405
406
/* Get the parent process id (we're the parent of the audio thread) */
parent = getpid();
#ifdef DEBUG_AUDIO
OBSD_Status(this);
#endif
/* We're ready to rock and roll. :-) */
return(0);
}