Mercurial > mplayer.hg
annotate libao2/ao_oss.c @ 3206:588102344a68
DGA handling changed (Arpi's request)
correct options: --enable-dga => auto (default)
--enable-dga=1 or ...=2
--disable-dga
changed --enable/disable-select handling to behave as the other tests
author | pl |
---|---|
date | Thu, 29 Nov 2001 18:56:08 +0000 |
parents | c8edb0691f09 |
children | 66134af21278 |
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> | |
1532 | 10 //#include <sys/soundcard.h> |
954 | 11 |
12 #include "../config.h" | |
13 | |
1532 | 14 #include "afmt.h" |
15 | |
954 | 16 #include "audio_out.h" |
17 #include "audio_out_internal.h" | |
18 | |
1191 | 19 extern int verbose; |
20 | |
954 | 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 | |
3181
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
29 /* Support for >2 output channels added 2001-11-25 - Steve Davies <steve@daviesfam.org> */ |
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
30 |
954 | 31 LIBAO_EXTERN(oss) |
32 | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
33 static char *dsp="/dev/dsp"; |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
34 static audio_buf_info zz; |
954 | 35 static int audio_fd=-1; |
36 | |
1191 | 37 char *oss_mixer_device = "/dev/mixer"; |
38 int oss_mixer_usemaster = 0; | |
39 | |
954 | 40 // to set/get/query special features/parameters |
41 static int control(int cmd,int arg){ | |
42 switch(cmd){ | |
43 case AOCONTROL_SET_DEVICE: | |
44 dsp=(char*)arg; | |
45 return CONTROL_OK; | |
46 case AOCONTROL_QUERY_FORMAT: | |
47 return CONTROL_TRUE; | |
1191 | 48 case AOCONTROL_GET_VOLUME: |
49 case AOCONTROL_SET_VOLUME: | |
50 { | |
51 ao_control_vol_t *vol = (ao_control_vol_t *)arg; | |
52 int fd, v, mcmd, devs; | |
1528
a444bd456fcc
ac3/spdif patch by German Gomez Garcia <german@piraos.com>
arpi
parents:
1456
diff
changeset
|
53 |
3095 | 54 if(ao_data.format == AFMT_AC3) |
1528
a444bd456fcc
ac3/spdif patch by German Gomez Garcia <german@piraos.com>
arpi
parents:
1456
diff
changeset
|
55 return CONTROL_TRUE; |
1191 | 56 |
57 if ((fd = open("/dev/mixer", O_RDONLY)) > 0) | |
58 { | |
59 ioctl(fd, SOUND_MIXER_READ_DEVMASK, &devs); | |
60 if ((devs & SOUND_MASK_PCM) && (oss_mixer_usemaster == 0)) | |
61 if (cmd == AOCONTROL_GET_VOLUME) | |
62 mcmd = SOUND_MIXER_READ_PCM; | |
63 else | |
64 mcmd = SOUND_MIXER_WRITE_PCM; | |
65 else if ((devs & SOUND_MASK_VOLUME) && (oss_mixer_usemaster == 1)) | |
66 if (cmd == AOCONTROL_GET_VOLUME) | |
67 mcmd = SOUND_MIXER_READ_VOLUME; | |
68 else | |
69 mcmd = SOUND_MIXER_WRITE_VOLUME; | |
70 else | |
71 { | |
72 close(fd); | |
73 return CONTROL_ERROR; | |
74 } | |
75 | |
76 if (cmd == AOCONTROL_GET_VOLUME) | |
77 { | |
78 ioctl(fd, cmd, &v); | |
79 vol->right = (v & 0xFF00) >> 8; | |
80 vol->left = v & 0x00FF; | |
81 } | |
82 else | |
83 { | |
84 v = ((int)vol->right << 8) | (int)vol->left; | |
85 ioctl(fd, cmd, &v); | |
86 } | |
87 close(fd); | |
88 return CONTROL_OK; | |
89 } | |
90 } | |
91 return CONTROL_ERROR; | |
954 | 92 } |
93 return CONTROL_UNKNOWN; | |
94 } | |
95 | |
96 // open & setup audio device | |
97 // return: 1=success 0=fail | |
98 static int init(int rate,int channels,int format,int flags){ | |
99 | |
3181
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
100 printf("ao2: %d Hz %d chans %s\n",rate,channels, |
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
101 audio_out_format_name(format)); |
954 | 102 |
1191 | 103 if (ao_subdevice) |
104 dsp = ao_subdevice; | |
105 | |
106 if (verbose) | |
107 printf("audio_setup: using '%s' dsp device\n", dsp); | |
108 | |
954 | 109 audio_fd=open(dsp, O_WRONLY); |
110 if(audio_fd<0){ | |
111 printf("Can't open audio device %s -> nosound\n",dsp); | |
112 return 0; | |
113 } | |
114 | |
3137 | 115 ao_data.bps=channels*rate; |
3095 | 116 if(format != AFMT_U8 && format != AFMT_S8) |
117 ao_data.bps*=2; | |
118 | |
119 ao_data.format=format; | |
120 ioctl (audio_fd, SNDCTL_DSP_SETFMT, &ao_data.format); | |
121 if(format == AFMT_AC3 && ao_data.format != AFMT_AC3) { | |
1528
a444bd456fcc
ac3/spdif patch by German Gomez Garcia <german@piraos.com>
arpi
parents:
1456
diff
changeset
|
122 printf("Can't set audio device %s to AC3 output\n", dsp); |
a444bd456fcc
ac3/spdif patch by German Gomez Garcia <german@piraos.com>
arpi
parents:
1456
diff
changeset
|
123 return 0; |
a444bd456fcc
ac3/spdif patch by German Gomez Garcia <german@piraos.com>
arpi
parents:
1456
diff
changeset
|
124 } |
1079 | 125 printf("audio_setup: sample format: %s (requested: %s)\n", |
3095 | 126 audio_out_format_name(ao_data.format), audio_out_format_name(format)); |
954 | 127 |
1528
a444bd456fcc
ac3/spdif patch by German Gomez Garcia <german@piraos.com>
arpi
parents:
1456
diff
changeset
|
128 if(format != AFMT_AC3) { |
3181
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
129 // We only use SNDCTL_DSP_CHANNELS for >2 channels, in case some drivers don't have it |
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
130 ao_data.channels = channels; |
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
131 if (ao_data.channels > 2) { |
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
132 if (ioctl (audio_fd, SNDCTL_DSP_CHANNELS, &ao_data.channels) == -1) { |
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
133 printf("audio_setup: Failed to set audio device to %d channels\n", ao_data.channels); |
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
134 return 0; |
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
135 } |
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
136 } |
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
137 else { |
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
138 int c = ao_data.channels-1; |
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
139 if (ioctl (audio_fd, SNDCTL_DSP_STEREO, &c) == -1) { |
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
140 printf("audio_setup: Failed to set audio device to %d channels\n", ao_data.channels); |
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
141 return 0; |
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
142 } |
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
143 } |
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
144 printf("audio_setup: using %d channels (requested: %d)\n", ao_data.channels, ao_data.channels); |
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
145 // set rate |
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
146 ao_data.samplerate=rate; |
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
147 ioctl (audio_fd, SNDCTL_DSP_SPEED, &ao_data.samplerate); |
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
148 printf("audio_setup: using %d Hz samplerate (requested: %d)\n",ao_data.samplerate,rate); |
1528
a444bd456fcc
ac3/spdif patch by German Gomez Garcia <german@piraos.com>
arpi
parents:
1456
diff
changeset
|
149 } |
954 | 150 |
151 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)==-1){ | |
152 int r=0; | |
153 printf("audio_setup: driver doesn't support SNDCTL_DSP_GETOSPACE :-(\n"); | |
154 if(ioctl(audio_fd, SNDCTL_DSP_GETBLKSIZE, &r)==-1){ | |
3095 | 155 printf("audio_setup: %d bytes/frag (config.h)\n",ao_data.outburst); |
954 | 156 } else { |
3095 | 157 ao_data.outburst=r; |
158 printf("audio_setup: %d bytes/frag (GETBLKSIZE)\n",ao_data.outburst); | |
954 | 159 } |
160 } else { | |
161 printf("audio_setup: frags: %3d/%d (%d bytes/frag) free: %6d\n", | |
162 zz.fragments, zz.fragstotal, zz.fragsize, zz.bytes); | |
3095 | 163 if(ao_data.buffersize==-1) ao_data.buffersize=zz.bytes; |
164 ao_data.outburst=zz.fragsize; | |
954 | 165 } |
166 | |
3095 | 167 if(ao_data.buffersize==-1){ |
954 | 168 // Measuring buffer size: |
169 void* data; | |
3095 | 170 ao_data.buffersize=0; |
954 | 171 #ifdef HAVE_AUDIO_SELECT |
3095 | 172 data=malloc(ao_data.outburst); memset(data,0,ao_data.outburst); |
173 while(ao_data.buffersize<0x40000){ | |
954 | 174 fd_set rfds; |
175 struct timeval tv; | |
176 FD_ZERO(&rfds); FD_SET(audio_fd,&rfds); | |
177 tv.tv_sec=0; tv.tv_usec = 0; | |
178 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) break; | |
3095 | 179 write(audio_fd,data,ao_data.outburst); |
180 ao_data.buffersize+=ao_data.outburst; | |
954 | 181 } |
182 free(data); | |
3095 | 183 if(ao_data.buffersize==0){ |
954 | 184 printf("\n *** Your audio driver DOES NOT support select() ***\n"); |
185 printf("Recompile mplayer with #undef HAVE_AUDIO_SELECT in config.h !\n\n"); | |
186 return 0; | |
187 } | |
188 #endif | |
189 } | |
190 | |
191 return 1; | |
192 } | |
193 | |
194 // close audio device | |
195 static void uninit(){ | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
196 #ifdef SNDCTL_DSP_RESET |
954 | 197 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
|
198 #endif |
954 | 199 close(audio_fd); |
200 } | |
201 | |
202 // stop playing and empty buffers (for seeking/pause) | |
203 static void reset(){ | |
204 uninit(); | |
205 audio_fd=open(dsp, O_WRONLY); | |
206 if(audio_fd<0){ | |
207 printf("\nFatal error: *** CANNOT RE-OPEN / RESET AUDIO DEVICE ***\n"); | |
208 return; | |
209 } | |
210 | |
3095 | 211 ioctl (audio_fd, SNDCTL_DSP_SETFMT, &ao_data.format); |
212 if(ao_data.format != AFMT_AC3) { | |
3181
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
213 if (ao_data.channels > 2) |
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
214 ioctl (audio_fd, SNDCTL_DSP_CHANNELS, &ao_data.channels); |
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
215 else { |
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
216 int c = ao_data.channels-1; |
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
217 ioctl (audio_fd, SNDCTL_DSP_STEREO, &c); |
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
218 } |
c8edb0691f09
Extended oss output driver and libac3 to support 4 and 6 channel output mixes. added -channels command line option
steve
parents:
3137
diff
changeset
|
219 ioctl (audio_fd, SNDCTL_DSP_SPEED, &ao_data.samplerate); |
1528
a444bd456fcc
ac3/spdif patch by German Gomez Garcia <german@piraos.com>
arpi
parents:
1456
diff
changeset
|
220 } |
954 | 221 } |
222 | |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
223 // stop playing, keep buffers (for pause) |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
224 static void audio_pause() |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
225 { |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
226 // for now, just call reset(); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
227 reset(); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
228 } |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
229 |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
230 // resume playing, after audio_pause() |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
231 static void audio_resume() |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
232 { |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
233 } |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
234 |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
235 |
954 | 236 // return: how many bytes can be played without blocking |
237 static int get_space(){ | |
3095 | 238 int playsize=ao_data.outburst; |
954 | 239 |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
240 #ifdef SNDCTL_DSP_GETOSPACE |
954 | 241 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)!=-1){ |
242 // calculate exact buffer space: | |
243 return zz.fragments*zz.fragsize; | |
244 } | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
245 #endif |
954 | 246 |
247 // check buffer | |
248 #ifdef HAVE_AUDIO_SELECT | |
249 { fd_set rfds; | |
250 struct timeval tv; | |
251 FD_ZERO(&rfds); | |
252 FD_SET(audio_fd, &rfds); | |
253 tv.tv_sec = 0; | |
254 tv.tv_usec = 0; | |
255 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) return 0; // not block! | |
256 } | |
257 #endif | |
258 | |
3095 | 259 return ao_data.outburst; |
954 | 260 } |
261 | |
262 // plays 'len' bytes of 'data' | |
263 // it should round it down to outburst*n | |
264 // return: number of bytes played | |
265 static int play(void* data,int len,int flags){ | |
3095 | 266 len/=ao_data.outburst; |
267 len=write(audio_fd,data,len*ao_data.outburst); | |
954 | 268 return len; |
269 } | |
270 | |
271 static int audio_delay_method=2; | |
272 | |
3095 | 273 // return: delay in seconds between first and last sample in buffer |
274 static float get_delay(){ | |
275 /* Calculate how many bytes/second is sent out */ | |
954 | 276 if(audio_delay_method==2){ |
277 int r=0; | |
278 if(ioctl(audio_fd, SNDCTL_DSP_GETODELAY, &r)!=-1) | |
3095 | 279 return ((float)r)/(float)ao_data.bps; |
954 | 280 audio_delay_method=1; // fallback if not supported |
281 } | |
282 if(audio_delay_method==1){ | |
283 // SNDCTL_DSP_GETOSPACE | |
284 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)!=-1) | |
3095 | 285 return ((float)(ao_data.buffersize-zz.bytes))/(float)ao_data.bps; |
954 | 286 audio_delay_method=0; // fallback if not supported |
287 } | |
3095 | 288 return ((float)ao_data.buffersize)/(float)ao_data.bps; |
954 | 289 } |