Mercurial > mplayer.hg
annotate libao2/ao_oss.c @ 1025:b3dde72791ed
Kernel version checking improvements
author | nickols_k |
---|---|
date | Tue, 05 Jun 2001 08:12:00 +0000 |
parents | 72cacd3b8f30 |
children | b36fb1ae4b53 |
rev | line source |
---|---|
954 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 | |
4 #include <sys/ioctl.h> | |
5 #include <unistd.h> | |
6 #include <sys/time.h> | |
7 #include <sys/types.h> | |
8 #include <sys/stat.h> | |
9 #include <fcntl.h> | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
10 #ifdef __sun |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
11 #include <sys/audioio.h> |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
12 #else |
954 | 13 #include <sys/soundcard.h> |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
14 #endif |
954 | 15 |
16 #include "../config.h" | |
17 | |
18 #include "audio_out.h" | |
19 #include "audio_out_internal.h" | |
20 | |
21 static ao_info_t info = | |
22 { | |
23 "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
|
24 "oss", |
954 | 25 "A'rpi", |
26 "" | |
27 }; | |
28 | |
29 LIBAO_EXTERN(oss) | |
30 | |
31 // there are some globals: | |
32 // ao_samplerate | |
33 // ao_channels | |
34 // ao_format | |
35 // ao_bps | |
36 // ao_outburst | |
37 // ao_buffersize | |
38 | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
39 #ifdef __sun |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
40 static char *dsp="/dev/audio"; |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
41 static int queued_bursts = 0; |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
42 #else |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
43 static char *dsp="/dev/dsp"; |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
44 static audio_buf_info zz; |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
45 #endif |
954 | 46 static int audio_fd=-1; |
47 | |
48 // to set/get/query special features/parameters | |
49 static int control(int cmd,int arg){ | |
50 switch(cmd){ | |
51 case AOCONTROL_SET_DEVICE: | |
52 dsp=(char*)arg; | |
53 return CONTROL_OK; | |
54 case AOCONTROL_QUERY_FORMAT: | |
55 return CONTROL_TRUE; | |
56 } | |
57 return CONTROL_UNKNOWN; | |
58 } | |
59 | |
60 // open & setup audio device | |
61 // return: 1=success 0=fail | |
62 static int init(int rate,int channels,int format,int flags){ | |
63 | |
64 printf("ao2: %d Hz %d chans 0x%X\n",rate,channels,format); | |
65 | |
66 audio_fd=open(dsp, O_WRONLY); | |
67 if(audio_fd<0){ | |
68 printf("Can't open audio device %s -> nosound\n",dsp); | |
69 return 0; | |
70 } | |
71 | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
72 #ifdef __sun |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
73 { |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
74 audio_info_t info; |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
75 ioctl(audio_fd, AUDIO_GETINFO, &info); |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
76 ioctl(audio_fd, AUDIO_DRAIN, 0); |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
77 info.play.encoding = ao_format = format; |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
78 info.play.precision = (format==AUDIO_ENCODING_LINEAR? AUDIO_PRECISION_16:AUDIO_PRECISION_8); |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
79 info.play.channels = ao_channels = channels; |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
80 --ao_channels; |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
81 info.play.sample_rate = ao_samplerate = rate; |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
82 if(ioctl (audio_fd, AUDIO_SETINFO, &info)<0) |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
83 printf("audio_setup: your card doesn't support %d Hz samplerate\n",rate); |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
84 ao_outburst=8192; |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
85 queued_bursts = 0; |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
86 } |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
87 #else |
954 | 88 ao_format=format; |
89 ioctl (audio_fd, SNDCTL_DSP_SETFMT, &ao_format); | |
90 printf("audio_setup: sample format: 0x%X (requested: 0x%X)\n",ao_format,format); | |
91 | |
92 ao_channels=channels-1; | |
93 ioctl (audio_fd, SNDCTL_DSP_STEREO, &ao_channels); | |
94 | |
95 // set rate | |
96 ao_samplerate=rate; | |
97 ioctl (audio_fd, SNDCTL_DSP_SPEED, &ao_samplerate); | |
98 printf("audio_setup: using %d Hz samplerate (requested: %d)\n",ao_samplerate,rate); | |
99 | |
100 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)==-1){ | |
101 int r=0; | |
102 printf("audio_setup: driver doesn't support SNDCTL_DSP_GETOSPACE :-(\n"); | |
103 if(ioctl(audio_fd, SNDCTL_DSP_GETBLKSIZE, &r)==-1){ | |
104 printf("audio_setup: %d bytes/frag (config.h)\n",ao_outburst); | |
105 } else { | |
106 ao_outburst=r; | |
107 printf("audio_setup: %d bytes/frag (GETBLKSIZE)\n",ao_outburst); | |
108 } | |
109 } else { | |
110 printf("audio_setup: frags: %3d/%d (%d bytes/frag) free: %6d\n", | |
111 zz.fragments, zz.fragstotal, zz.fragsize, zz.bytes); | |
112 if(ao_buffersize==-1) ao_buffersize=zz.bytes; | |
113 ao_outburst=zz.fragsize; | |
114 } | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
115 #endif |
954 | 116 |
117 if(ao_buffersize==-1){ | |
118 // Measuring buffer size: | |
119 void* data; | |
120 ao_buffersize=0; | |
121 #ifdef HAVE_AUDIO_SELECT | |
122 data=malloc(ao_outburst); memset(data,0,ao_outburst); | |
123 while(ao_buffersize<0x40000){ | |
124 fd_set rfds; | |
125 struct timeval tv; | |
126 FD_ZERO(&rfds); FD_SET(audio_fd,&rfds); | |
127 tv.tv_sec=0; tv.tv_usec = 0; | |
128 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) break; | |
129 write(audio_fd,data,ao_outburst); | |
130 ao_buffersize+=ao_outburst; | |
131 } | |
132 free(data); | |
133 if(ao_buffersize==0){ | |
134 printf("\n *** Your audio driver DOES NOT support select() ***\n"); | |
135 printf("Recompile mplayer with #undef HAVE_AUDIO_SELECT in config.h !\n\n"); | |
136 return 0; | |
137 } | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
138 #ifdef __sun |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
139 ioctl(audio_fd, AUDIO_DRAIN, 0); |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
140 #endif |
954 | 141 #endif |
142 } | |
143 | |
144 return 1; | |
145 } | |
146 | |
147 // close audio device | |
148 static void uninit(){ | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
149 #ifdef SNDCTL_DSP_RESET |
954 | 150 ioctl(audio_fd, SNDCTL_DSP_RESET, NULL); |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
151 #endif |
954 | 152 close(audio_fd); |
153 } | |
154 | |
155 // stop playing and empty buffers (for seeking/pause) | |
156 static void reset(){ | |
157 uninit(); | |
158 audio_fd=open(dsp, O_WRONLY); | |
159 if(audio_fd<0){ | |
160 printf("\nFatal error: *** CANNOT RE-OPEN / RESET AUDIO DEVICE ***\n"); | |
161 return; | |
162 } | |
163 | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
164 #ifdef __sun |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
165 { |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
166 audio_info_t info; |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
167 ioctl(audio_fd, AUDIO_GETINFO, &info); |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
168 ioctl(audio_fd, AUDIO_DRAIN, 0); |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
169 info.play.encoding = ao_format; |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
170 info.play.precision = (ao_format==AUDIO_ENCODING_LINEAR? AUDIO_PRECISION_16:AUDIO_PRECISION_8); |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
171 info.play.channels = ao_channels+1; |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
172 info.play.sample_rate = ao_samplerate; |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
173 ioctl (audio_fd, AUDIO_SETINFO, &info); |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
174 queued_bursts = 0; |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
175 } |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
176 #else |
954 | 177 ioctl (audio_fd, SNDCTL_DSP_SETFMT, &ao_format); |
178 ioctl (audio_fd, SNDCTL_DSP_STEREO, &ao_channels); | |
179 ioctl (audio_fd, SNDCTL_DSP_SPEED, &ao_samplerate); | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
180 #endif |
954 | 181 |
182 } | |
183 | |
184 // return: how many bytes can be played without blocking | |
185 static int get_space(){ | |
186 int playsize=ao_outburst; | |
187 | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
188 #ifdef SNDCTL_DSP_GETOSPACE |
954 | 189 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)!=-1){ |
190 // calculate exact buffer space: | |
191 return zz.fragments*zz.fragsize; | |
192 } | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
193 #endif |
954 | 194 |
195 // check buffer | |
196 #ifdef HAVE_AUDIO_SELECT | |
197 { fd_set rfds; | |
198 struct timeval tv; | |
199 FD_ZERO(&rfds); | |
200 FD_SET(audio_fd, &rfds); | |
201 tv.tv_sec = 0; | |
202 tv.tv_usec = 0; | |
203 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) return 0; // not block! | |
204 } | |
205 #endif | |
206 | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
207 #ifdef __sun |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
208 { |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
209 audio_info_t info; |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
210 ioctl(audio_fd, AUDIO_GETINFO, &info); |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
211 if(queued_bursts - info.play.eof > 2) |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
212 return 0; |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
213 } |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
214 #endif |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
215 return ao_outburst; |
954 | 216 } |
217 | |
218 // plays 'len' bytes of 'data' | |
219 // it should round it down to outburst*n | |
220 // return: number of bytes played | |
221 static int play(void* data,int len,int flags){ | |
222 len/=ao_outburst; | |
223 len=write(audio_fd,data,len*ao_outburst); | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
224 #ifdef __sun |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
225 if(len>0) { |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
226 queued_bursts ++; |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
227 write(audio_fd,data,0); |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
228 } |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
229 #endif |
954 | 230 return len; |
231 } | |
232 | |
233 static int audio_delay_method=2; | |
234 | |
235 // return: how many unplayed bytes are in the buffer | |
236 static int get_delay(){ | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
237 #ifdef __sun |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
238 { |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
239 int q; |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
240 audio_info_t info; |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
241 ioctl(audio_fd, AUDIO_GETINFO, &info); |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
242 return (queued_bursts - info.play.eof) * ao_outburst; |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
243 } |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
244 #else |
954 | 245 if(audio_delay_method==2){ |
246 // | |
247 int r=0; | |
248 if(ioctl(audio_fd, SNDCTL_DSP_GETODELAY, &r)!=-1) | |
249 return r; | |
250 audio_delay_method=1; // fallback if not supported | |
251 } | |
252 if(audio_delay_method==1){ | |
253 // SNDCTL_DSP_GETOSPACE | |
254 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)!=-1) | |
255 return ao_buffersize-zz.bytes; | |
256 audio_delay_method=0; // fallback if not supported | |
257 } | |
258 return ao_buffersize; | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
259 #endif |
954 | 260 } |
261 |