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