src/audio/openbsd/SDL_openbsdaudio.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 14 Dec 2001 13:13:20 +0000
changeset 254 4fc12b8edf74
parent 252 e8157fcb3114
child 297 f6ffac90895c
permissions -rw-r--r--
*** empty log message ***
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /*
    24  * Driver for native OpenBSD audio(4).
    25  * vedge@vedge.com.ar.
    26  */
    27 
    28 #include <stdlib.h>
    29 #include <stdio.h>
    30 #include <string.h>
    31 #include <errno.h>
    32 #include <unistd.h>
    33 #include <fcntl.h>
    34 #include <signal.h>
    35 #include <sys/time.h>
    36 #include <sys/ioctl.h>
    37 #include <sys/stat.h>
    38 #include <sys/types.h>
    39 #include <sys/audioio.h>
    40 
    41 #include "SDL_audio.h"
    42 #include "SDL_error.h"
    43 #include "SDL_audiomem.h"
    44 #include "SDL_audio_c.h"
    45 #include "SDL_timer.h"
    46 #include "SDL_audiodev_c.h"
    47 #include "SDL_openbsdaudio.h"
    48 
    49 /* The tag name used by OpenBSD audio */
    50 #define OBSD_DRIVER_NAME         "openbsd"
    51 
    52 /* Open the audio device for playback, and don't block if busy */
    53 /* #define USE_BLOCKING_WRITES */
    54 
    55 /* Use timer for synchronization */
    56 /* #define USE_TIMER_SYNC */
    57 
    58 /* #define DEBUG_AUDIO */
    59 /* #define DEBUG_AUDIO_STREAM */
    60 
    61 #ifdef USE_BLOCKING_WRITES
    62 #define OPEN_FLAGS	O_WRONLY
    63 #else
    64 #define OPEN_FLAGS	(O_WRONLY|O_NONBLOCK)
    65 #endif
    66 
    67 /* Audio driver functions */
    68 static void OBSD_WaitAudio(_THIS);
    69 static int OBSD_OpenAudio(_THIS, SDL_AudioSpec *spec);
    70 static void OBSD_PlayAudio(_THIS);
    71 static Uint8 *OBSD_GetAudioBuf(_THIS);
    72 static void OBSD_CloseAudio(_THIS);
    73 
    74 #ifdef DEBUG_AUDIO
    75 static void OBSD_Status(_THIS);
    76 #endif
    77 
    78 /* Audio driver bootstrap functions */
    79 
    80 static int
    81 Audio_Available(void)
    82 {
    83     int fd;
    84     int available;
    85 
    86     available = 0;
    87     fd = SDL_OpenAudioPath(NULL, 0, OPEN_FLAGS, 0);
    88     if(fd >= 0) {
    89 	available = 1;
    90 	close(fd);
    91     }
    92     return(available);
    93 }
    94 
    95 static void
    96 Audio_DeleteDevice(SDL_AudioDevice *device)
    97 {
    98     free(device->hidden);
    99     free(device);
   100 }
   101 
   102 static SDL_AudioDevice
   103 *Audio_CreateDevice(int devindex)
   104 {
   105     SDL_AudioDevice *this;
   106 
   107     /* Initialize all variables that we clean on shutdown */
   108     this = (SDL_AudioDevice*)malloc(sizeof(SDL_AudioDevice));
   109     if(this) {
   110 	memset(this, 0, (sizeof *this));
   111 	this->hidden =
   112 	    (struct SDL_PrivateAudioData*)malloc((sizeof *this->hidden));
   113     }
   114     if((this == NULL) || (this->hidden == NULL)) {
   115 	SDL_OutOfMemory();
   116 	if(this) free(this);
   117 	return(0);
   118     }
   119     memset(this->hidden, 0, (sizeof *this->hidden));
   120     audio_fd = -1;
   121 
   122     /* Set the function pointers */
   123     this->OpenAudio = OBSD_OpenAudio;
   124     this->WaitAudio = OBSD_WaitAudio;
   125     this->PlayAudio = OBSD_PlayAudio;
   126     this->GetAudioBuf = OBSD_GetAudioBuf;
   127     this->CloseAudio = OBSD_CloseAudio;
   128 
   129     this->free = Audio_DeleteDevice;
   130     
   131     return this;
   132 }
   133 
   134 AudioBootStrap OPENBSD_AUDIO_bootstrap = {
   135 	OBSD_DRIVER_NAME, "Native OpenBSD audio",
   136 	Audio_Available, Audio_CreateDevice
   137 };
   138 
   139 /* This function waits until it is possible to write a full sound buffer */
   140 static void
   141 OBSD_WaitAudio(_THIS)
   142 {
   143 	/* Check to see if the thread-parent process is still alive */
   144 	{ static int cnt = 0;
   145 		/* Note that this only works with thread implementations 
   146 		   that use a different process id for each thread.
   147 		*/
   148 		if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */
   149 			if ( kill(parent, 0) < 0 ) {
   150 				this->enabled = 0;
   151 			}
   152 		}
   153 	}
   154 
   155 #ifndef USE_BLOCKING_WRITES /* Not necessary when using blocking writes */
   156 	/* See if we need to use timed audio synchronization */
   157 	if ( frame_ticks ) {
   158 		/* Use timer for general audio synchronization */
   159 		Sint32 ticks;
   160 
   161 		ticks = ((Sint32)(next_frame - SDL_GetTicks()))-FUDGE_TICKS;
   162 		if ( ticks > 0 ) {
   163 			SDL_Delay(ticks);
   164 		}
   165 	} else {
   166 		/* Use select() for audio synchronization */
   167 		fd_set fdset;
   168 		struct timeval timeout;
   169 
   170 		FD_ZERO(&fdset);
   171 		FD_SET(audio_fd, &fdset);
   172 		timeout.tv_sec = 10;
   173 		timeout.tv_usec = 0;
   174 #ifdef DEBUG_AUDIO
   175 		fprintf(stderr, "Waiting for audio to get ready\n");
   176 #endif
   177 		if ( select(audio_fd+1, NULL, &fdset, NULL, &timeout) <= 0 ) {
   178 			const char *message =
   179 			"Audio timeout - buggy audio driver? (disabled)";
   180 			/* In general we should never print to the screen,
   181 			   but in this case we have no other way of letting
   182 			   the user know what happened.
   183 			*/
   184 			fprintf(stderr, "SDL: %s\n", message);
   185 			this->enabled = 0;
   186 			/* Don't try to close - may hang */
   187 			audio_fd = -1;
   188 #ifdef DEBUG_AUDIO
   189 			fprintf(stderr, "Done disabling audio\n");
   190 #endif
   191 		}
   192 #ifdef DEBUG_AUDIO
   193 		fprintf(stderr, "Ready!\n");
   194 #endif
   195 	}
   196 #endif /* !USE_BLOCKING_WRITES */
   197 }
   198 
   199 static void
   200 OBSD_PlayAudio(_THIS)
   201 {
   202 	int written, p=0;
   203 
   204 	/* Write the audio data, checking for EAGAIN on broken audio drivers */
   205 	do {
   206 		written = write(audio_fd, &mixbuf[p], mixlen-p);
   207 		if (written>0)
   208 		   p += written;
   209 		if (written == -1 && errno != 0 && errno != EAGAIN && errno != EINTR)
   210 		{
   211 		   /* Non recoverable error has occurred. It should be reported!!! */
   212 		   perror("audio");
   213 		   break;
   214 		}
   215 
   216 		if ( p < written || ((written < 0) && ((errno == 0) || (errno == EAGAIN))) ) {
   217 			SDL_Delay(1);	/* Let a little CPU time go by */
   218 		}
   219 	} while ( p < written );
   220 
   221 	/* If timer synchronization is enabled, set the next write frame */
   222 	if ( frame_ticks ) {
   223 		next_frame += frame_ticks;
   224 	}
   225 
   226 	/* If we couldn't write, assume fatal error for now */
   227 	if ( written < 0 ) {
   228 		this->enabled = 0;
   229 	}
   230 #ifdef DEBUG_AUDIO
   231 	fprintf(stderr, "Wrote %d bytes of audio data\n", written);
   232 #endif
   233 }
   234 
   235 static Uint8
   236 *OBSD_GetAudioBuf(_THIS)
   237 {
   238     return(mixbuf);
   239 }
   240 
   241 static void
   242 OBSD_CloseAudio(_THIS)
   243 {
   244     if(mixbuf != NULL) {
   245 	SDL_FreeAudioMem(mixbuf);
   246 	mixbuf = NULL;
   247     }
   248     if(audio_fd >= 0) {
   249 	close(audio_fd);
   250 	audio_fd = -1;
   251     }
   252 }
   253 
   254 #ifdef DEBUG_AUDIO
   255 void
   256 OBSD_Status(_THIS)
   257 {
   258     audio_info_t info;
   259 
   260     if(ioctl(audio_fd, AUDIO_GETINFO, &info) < 0) {
   261 	fprintf(stderr,"AUDIO_GETINFO failed.\n");
   262 	return;
   263     }
   264 
   265     fprintf(stderr,"
   266 [play/record info]
   267 buffer size	:   %d bytes
   268 sample rate	:   %i Hz
   269 channels	:   %i
   270 precision	:   %i-bit
   271 encoding	:   0x%x
   272 seek		:   %i
   273 sample count	:   %i
   274 EOF count	:   %i
   275 paused		:   %s
   276 error occured	:   %s
   277 waiting		:   %s
   278 active		:   %s
   279 ",
   280     info.play.buffer_size,
   281     info.play.sample_rate,
   282     info.play.channels,
   283     info.play.precision,
   284     info.play.encoding,
   285     info.play.seek,
   286     info.play.samples,
   287     info.play.eof,
   288     info.play.pause ? "yes" : "no",
   289     info.play.error ? "yes" : "no",
   290     info.play.waiting ? "yes" : "no",
   291     info.play.active ? "yes": "no");
   292 
   293     fprintf(stderr,"
   294 [audio info]
   295 monitor_gain	:   %i
   296 hw block size	:   %d bytes
   297 hi watermark	:   %i
   298 lo watermark	:   %i
   299 audio mode	:   %s
   300 ",  
   301     info.monitor_gain,
   302     info.blocksize,
   303     info.hiwat, info.lowat,
   304     (info.mode == AUMODE_PLAY) ? "PLAY"
   305     : (info.mode = AUMODE_RECORD) ? "RECORD"
   306     : (info.mode == AUMODE_PLAY_ALL ? "PLAY_ALL"
   307     : "???"));
   308 }
   309 #endif /* DEBUG_AUDIO */
   310 
   311 static int
   312 OBSD_OpenAudio(_THIS, SDL_AudioSpec *spec)
   313 {
   314     char audiodev[64];
   315     Uint16 format;
   316     audio_info_t info;
   317 
   318     AUDIO_INITINFO(&info);
   319     
   320     /* Calculate the final parameters for this audio specification */
   321     SDL_CalculateAudioSpec(spec);
   322 
   323 #ifdef USE_TIMER_SYNC
   324     frame_ticks = 0.0;
   325 #endif
   326 
   327     /* Open the audio device */
   328     audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 0);
   329     if(audio_fd < 0) {
   330 	SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno));
   331 	return(-1);
   332     }
   333     
   334     /* Set to play mode */
   335     info.mode = AUMODE_PLAY;
   336     if(ioctl(audio_fd, AUDIO_SETINFO, &info) < 0) {
   337 	SDL_SetError("Couldn't put device into play mode");
   338 	return(-1);
   339     }
   340     
   341     mixbuf = NULL;
   342     AUDIO_INITINFO(&info);
   343     for (format = SDL_FirstAudioFormat(spec->format); 
   344     	format; format = SDL_NextAudioFormat())
   345     {
   346 	switch(format) {
   347 	case AUDIO_U8:
   348 	    info.play.encoding = AUDIO_ENCODING_ULINEAR;
   349 	    info.play.precision = 8;
   350 	    break;
   351 	case AUDIO_S8:
   352 	    info.play.encoding = AUDIO_ENCODING_SLINEAR;
   353 	    info.play.precision = 8;
   354 	    break;
   355 	case AUDIO_S16LSB:
   356 	    info.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
   357 	    info.play.precision = 16;
   358 	    break;
   359 	case AUDIO_S16MSB:
   360 	    info.play.encoding = AUDIO_ENCODING_SLINEAR_BE;
   361 	    info.play.precision = 16;
   362 	    break;
   363 	case AUDIO_U16LSB:
   364 	    info.play.encoding = AUDIO_ENCODING_ULINEAR_LE;
   365 	    info.play.precision = 16;
   366 	    break;
   367 	case AUDIO_U16MSB:
   368 	    info.play.encoding = AUDIO_ENCODING_ULINEAR_BE;
   369 	    info.play.precision = 16;
   370 	    break;
   371 	default:
   372 	    continue;
   373 	}
   374 	if (ioctl(audio_fd, AUDIO_SETINFO, &info) == 0)
   375 	    break;
   376     }
   377 
   378     if(!format) {
   379 	SDL_SetError("No supported encoding for 0x%x", spec->format);
   380 	return(-1);
   381     }
   382 
   383     spec->format = format;
   384 
   385     AUDIO_INITINFO(&info);
   386     info.play.channels = spec->channels;
   387     if (ioctl(audio_fd, AUDIO_SETINFO, &info) == -1)
   388     	spec->channels = 1;
   389     AUDIO_INITINFO(&info);
   390     info.play.sample_rate = spec->freq;
   391     (void)ioctl(audio_fd, AUDIO_SETINFO, &info);
   392     (void)ioctl(audio_fd, AUDIO_GETINFO, &info);
   393     spec->freq  = info.play.sample_rate;
   394     /* Allocate mixing buffer */
   395     mixlen = spec->size;
   396     mixbuf = (Uint8*)SDL_AllocAudioMem(mixlen);
   397     if(mixbuf == NULL) {
   398 	return(-1);
   399     }
   400     memset(mixbuf, spec->silence, spec->size);
   401     
   402     /* Get the parent process id (we're the parent of the audio thread) */
   403     parent = getpid();
   404 
   405 #ifdef DEBUG_AUDIO
   406     OBSD_Status(this);
   407 #endif
   408 
   409     /* We're ready to rock and roll. :-) */
   410     return(0);
   411 }