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
|
/*
* Derived from MPlayer (libao2 - ao_oss).
*
* Copyright (c) FAUMachine Team.
* Copyright (c) MPlayer Team.
*
* This program is free software. You can redistribute it and/or modify it
* under the terms of the GNU General Public License, either version 2 of
* the License, or (at your option) any later version. See COPYING.
*/
/*
* OSS audio output driver for FAUmachine
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include "config.h"
#include "glue-audio_internal.h"
static ao_info_t info = {
"OSS/ioctl audio output",
"oss",
"A'rpi",
""
};
/* Support for >2 output channels added 2001-11-25 - Steve Davies <steve@daviesfam.org> */
LIBAO_EXTERN(oss);
static int oss_outburst = OUTBURST;
static int oss_buffersize = 0;
static int oss_bps = 0;
static audio_buf_info zz;
static int audio_fd=-1;
static int audio_delay_method=2;
/* open & setup audio device
* return: 1=success 0=fail
*/
static int
init(void)
{
#ifdef LINUX
audio_fd=open(PATH_DEV_DSP, O_WRONLY | O_NONBLOCK);
#else
audio_fd=open(PATH_DEV_DSP, O_WRONLY);
#endif
if(audio_fd<0){
return 0;
}
#ifdef LINUX
/* Remove the non-blocking flag */
if(fcntl(audio_fd, F_SETFL, 0) < 0) {
return 0;
}
#endif
#if defined(FD_CLOEXEC) && defined(F_SETFD)
fcntl(audio_fd, F_SETFD, FD_CLOEXEC);
#endif
int format=GLUE_AUDIO_FORMAT;
if(ioctl(audio_fd, SNDCTL_DSP_SETFMT, &format)<0 ||
format != GLUE_AUDIO_FORMAT) {
return 0;
}
int channels = GLUE_AUDIO_CHANNELS - 1;
if (ioctl (audio_fd, SNDCTL_DSP_STEREO, &channels) == -1) {
return 0;
}
// set rate
int samplerate = GLUE_AUDIO_RATE;
ioctl (audio_fd, SNDCTL_DSP_SPEED, &samplerate);
if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)==-1){
int r=0;
if(ioctl(audio_fd, SNDCTL_DSP_GETBLKSIZE, &r)!=-1){
oss_outburst=r;
}
} else {
if(oss_buffersize==-1) oss_buffersize=zz.bytes;
oss_outburst=zz.fragsize;
}
if(oss_buffersize==-1){
// Measuring buffer size:
oss_buffersize=0;
#ifdef HAVE_AUDIO_SELECT
void* data;
data=malloc(oss_outburst); memset(data,0,oss_outburst);
while(oss_buffersize<0x40000){
fd_set rfds;
struct timeval tv;
FD_ZERO(&rfds); FD_SET(audio_fd,&rfds);
tv.tv_sec=0; tv.tv_usec = 0;
if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) break;
write(audio_fd,data,oss_outburst);
oss_buffersize+=oss_outburst;
}
free(data);
if(oss_buffersize==0){
return 0;
}
#endif
}
oss_bps=channels * GLUE_AUDIO_FORMAT_BYTES;
oss_outburst-=oss_outburst % oss_bps; // round down
oss_bps*=samplerate;
return 1;
}
/* close audio device */
static void
uninit(int immed)
{
if(audio_fd == -1) return;
#ifdef SNDCTL_DSP_SYNC
// to get the buffer played
if (!immed)
ioctl(audio_fd, SNDCTL_DSP_SYNC, NULL);
#endif
#ifdef SNDCTL_DSP_RESET
if (immed)
ioctl(audio_fd, SNDCTL_DSP_RESET, NULL);
#endif
close(audio_fd);
audio_fd = -1;
}
/* return: how many bytes can be played without blocking */
static int
get_space()
{
int playsize=oss_outburst;
#ifdef SNDCTL_DSP_GETOSPACE
if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)!=-1){
// calculate exact buffer space:
playsize = zz.fragments*zz.fragsize;
if (playsize > MAX_OUTBURST)
playsize = (MAX_OUTBURST / zz.fragsize) * zz.fragsize;
return playsize;
}
#endif
// check buffer
#ifdef HAVE_AUDIO_SELECT
{ fd_set rfds;
struct timeval tv;
FD_ZERO(&rfds);
FD_SET(audio_fd, &rfds);
tv.tv_sec = 0;
tv.tv_usec = 0;
if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) return 0; // not block!
}
#endif
return oss_outburst;
}
/* plays 'len' bytes of 'data'
* it should round it down to outburst*n
* return: number of bytes played
*/
static int
play(void* data,int len)
{
len/=oss_outburst;
len=write(audio_fd,data,len*oss_outburst);
return len;
}
/* return: delay in seconds between first and last sample in buffer */
static float
get_delay()
{
/* Calculate how many bytes/second is sent out */
if(audio_delay_method==2){
#ifdef SNDCTL_DSP_GETODELAY
int r=0;
if(ioctl(audio_fd, SNDCTL_DSP_GETODELAY, &r)!=-1)
return ((float)r)/(float)oss_bps;
#endif
audio_delay_method=1; // fallback if not supported
}
if(audio_delay_method==1){
// SNDCTL_DSP_GETOSPACE
if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)!=-1)
return ((float)(oss_buffersize-zz.bytes))/(float)oss_bps;
audio_delay_method=0; // fallback if not supported
}
return ((float)oss_buffersize)/(float)oss_bps;
}
|