annotate libao2/ao_oss.c @ 993:dfa641c44d4a

Increased sample size to solve sound problems one some systems.
author atmosfear
date Mon, 04 Jun 2001 14:56:32 +0000
parents a6cecd9a1bad
children 72cacd3b8f30
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
954
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
1 #include <stdio.h>
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
2 #include <stdlib.h>
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
3
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
4 #include <sys/ioctl.h>
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
5 #include <unistd.h>
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
6 #include <sys/time.h>
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
7 #include <sys/types.h>
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
8 #include <sys/stat.h>
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
9 #include <fcntl.h>
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
10 #include <sys/soundcard.h>
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
11
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
12 #include "../config.h"
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
13
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
14 #include "audio_out.h"
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
15 #include "audio_out_internal.h"
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
16
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
17 static ao_info_t info =
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
18 {
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
19 "OSS/ioctl audio output",
956
a6cecd9a1bad '-ao' switch (including '-ao help'), fixing Arpi's bug (short name 'null' for both of oss and null driver ;)
lgb
parents: 954
diff changeset
20 "oss",
954
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
21 "A'rpi",
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
22 ""
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
23 };
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
24
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
25 LIBAO_EXTERN(oss)
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
26
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
27 // there are some globals:
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
28 // ao_samplerate
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
29 // ao_channels
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
30 // ao_format
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
31 // ao_bps
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
32 // ao_outburst
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
33 // ao_buffersize
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
34
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
35 static char* dsp="/dev/dsp";
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
36 static int audio_fd=-1;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
37 static audio_buf_info zz;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
38
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
39 // to set/get/query special features/parameters
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
40 static int control(int cmd,int arg){
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
41 switch(cmd){
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
42 case AOCONTROL_SET_DEVICE:
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
43 dsp=(char*)arg;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
44 return CONTROL_OK;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
45 case AOCONTROL_QUERY_FORMAT:
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
46 return CONTROL_TRUE;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
47 }
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
48 return CONTROL_UNKNOWN;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
49 }
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
50
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
51 // open & setup audio device
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
52 // return: 1=success 0=fail
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
53 static int init(int rate,int channels,int format,int flags){
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
54
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
55 printf("ao2: %d Hz %d chans 0x%X\n",rate,channels,format);
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
56
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
57 audio_fd=open(dsp, O_WRONLY);
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
58 if(audio_fd<0){
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
59 printf("Can't open audio device %s -> nosound\n",dsp);
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
60 return 0;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
61 }
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
62
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
63 ao_format=format;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
64 ioctl (audio_fd, SNDCTL_DSP_SETFMT, &ao_format);
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
65 printf("audio_setup: sample format: 0x%X (requested: 0x%X)\n",ao_format,format);
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
66
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
67 ao_channels=channels-1;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
68 ioctl (audio_fd, SNDCTL_DSP_STEREO, &ao_channels);
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
69
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
70 // set rate
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
71 ao_samplerate=rate;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
72 ioctl (audio_fd, SNDCTL_DSP_SPEED, &ao_samplerate);
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
73 printf("audio_setup: using %d Hz samplerate (requested: %d)\n",ao_samplerate,rate);
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
74
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
75 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)==-1){
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
76 int r=0;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
77 printf("audio_setup: driver doesn't support SNDCTL_DSP_GETOSPACE :-(\n");
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
78 if(ioctl(audio_fd, SNDCTL_DSP_GETBLKSIZE, &r)==-1){
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
79 printf("audio_setup: %d bytes/frag (config.h)\n",ao_outburst);
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
80 } else {
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
81 ao_outburst=r;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
82 printf("audio_setup: %d bytes/frag (GETBLKSIZE)\n",ao_outburst);
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
83 }
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
84 } else {
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
85 printf("audio_setup: frags: %3d/%d (%d bytes/frag) free: %6d\n",
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
86 zz.fragments, zz.fragstotal, zz.fragsize, zz.bytes);
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
87 if(ao_buffersize==-1) ao_buffersize=zz.bytes;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
88 ao_outburst=zz.fragsize;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
89 }
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
90
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
91 if(ao_buffersize==-1){
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
92 // Measuring buffer size:
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
93 void* data;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
94 ao_buffersize=0;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
95 #ifdef HAVE_AUDIO_SELECT
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
96 data=malloc(ao_outburst); memset(data,0,ao_outburst);
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
97 while(ao_buffersize<0x40000){
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
98 fd_set rfds;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
99 struct timeval tv;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
100 FD_ZERO(&rfds); FD_SET(audio_fd,&rfds);
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
101 tv.tv_sec=0; tv.tv_usec = 0;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
102 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) break;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
103 write(audio_fd,data,ao_outburst);
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
104 ao_buffersize+=ao_outburst;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
105 }
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
106 free(data);
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
107 if(ao_buffersize==0){
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
108 printf("\n *** Your audio driver DOES NOT support select() ***\n");
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
109 printf("Recompile mplayer with #undef HAVE_AUDIO_SELECT in config.h !\n\n");
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
110 return 0;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
111 }
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
112 #endif
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
113 }
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
114
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
115 return 1;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
116 }
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
117
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
118 // close audio device
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
119 static void uninit(){
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
120 ioctl(audio_fd, SNDCTL_DSP_RESET, NULL);
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
121 close(audio_fd);
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
122 }
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
123
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
124 // stop playing and empty buffers (for seeking/pause)
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
125 static void reset(){
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
126 uninit();
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
127 audio_fd=open(dsp, O_WRONLY);
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
128 if(audio_fd<0){
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
129 printf("\nFatal error: *** CANNOT RE-OPEN / RESET AUDIO DEVICE ***\n");
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
130 return;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
131 }
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
132
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
133 ioctl (audio_fd, SNDCTL_DSP_SETFMT, &ao_format);
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
134 ioctl (audio_fd, SNDCTL_DSP_STEREO, &ao_channels);
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
135 ioctl (audio_fd, SNDCTL_DSP_SPEED, &ao_samplerate);
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
136
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
137 }
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
138
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
139 // return: how many bytes can be played without blocking
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
140 static int get_space(){
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
141 int playsize=ao_outburst;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
142
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
143 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)!=-1){
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
144 // calculate exact buffer space:
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
145 return zz.fragments*zz.fragsize;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
146 }
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
147
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
148 // check buffer
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
149 #ifdef HAVE_AUDIO_SELECT
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
150 { fd_set rfds;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
151 struct timeval tv;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
152 FD_ZERO(&rfds);
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
153 FD_SET(audio_fd, &rfds);
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
154 tv.tv_sec = 0;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
155 tv.tv_usec = 0;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
156 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) return 0; // not block!
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
157 }
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
158 #endif
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
159
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
160 return ao_outburst;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
161 }
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
162
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
163 // plays 'len' bytes of 'data'
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
164 // it should round it down to outburst*n
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
165 // return: number of bytes played
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
166 static int play(void* data,int len,int flags){
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
167 len/=ao_outburst;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
168 len=write(audio_fd,data,len*ao_outburst);
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
169 return len;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
170 }
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
171
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
172 static int audio_delay_method=2;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
173
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
174 // return: how many unplayed bytes are in the buffer
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
175 static int get_delay(){
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
176 if(audio_delay_method==2){
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
177 //
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
178 int r=0;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
179 if(ioctl(audio_fd, SNDCTL_DSP_GETODELAY, &r)!=-1)
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
180 return r;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
181 audio_delay_method=1; // fallback if not supported
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
182 }
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
183 if(audio_delay_method==1){
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
184 // SNDCTL_DSP_GETOSPACE
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
185 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)!=-1)
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
186 return ao_buffersize-zz.bytes;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
187 audio_delay_method=0; // fallback if not supported
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
188 }
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
189 return ao_buffersize;
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
190 }
db20b90dd34d audio out drivers
arpi_esp
parents:
diff changeset
191