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