Mercurial > mplayer.hg
annotate libao2/ao_oss.c @ 1964:41aa7a6c2252
similar to 1.53, but not finished
author | jaf |
---|---|
date | Tue, 25 Sep 2001 23:12:52 +0000 |
parents | cb9bc07f3d4c |
children | f91ad6d23ce9 |
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 else | |
97 return CONTROL_ERROR; | |
98 } | |
99 return CONTROL_ERROR; | |
100 default: | |
954 | 101 } |
102 return CONTROL_UNKNOWN; | |
103 } | |
104 | |
105 // open & setup audio device | |
106 // return: 1=success 0=fail | |
107 static int init(int rate,int channels,int format,int flags){ | |
108 | |
1456
8c57a5a3c645
printfs cleanup - moved to higher -v level or moved to stderr
arpi
parents:
1191
diff
changeset
|
109 // 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
|
110 // audio_out_format_name(format)); |
954 | 111 |
1191 | 112 if (ao_subdevice) |
113 dsp = ao_subdevice; | |
114 | |
115 if (verbose) | |
116 printf("audio_setup: using '%s' dsp device\n", dsp); | |
117 | |
954 | 118 audio_fd=open(dsp, O_WRONLY); |
119 if(audio_fd<0){ | |
120 printf("Can't open audio device %s -> nosound\n",dsp); | |
121 return 0; | |
122 } | |
123 | |
124 ao_format=format; | |
125 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
|
126 if(format == AFMT_AC3 && ao_format != AFMT_AC3) { |
a444bd456fcc
ac3/spdif patch by German Gomez Garcia <german@piraos.com>
arpi
parents:
1456
diff
changeset
|
127 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
|
128 return 0; |
a444bd456fcc
ac3/spdif patch by German Gomez Garcia <german@piraos.com>
arpi
parents:
1456
diff
changeset
|
129 } |
1079 | 130 printf("audio_setup: sample format: %s (requested: %s)\n", |
131 audio_out_format_name(ao_format), audio_out_format_name(format)); | |
954 | 132 |
1528
a444bd456fcc
ac3/spdif patch by German Gomez Garcia <german@piraos.com>
arpi
parents:
1456
diff
changeset
|
133 if(format != AFMT_AC3) { |
954 | 134 ao_channels=channels-1; |
135 ioctl (audio_fd, SNDCTL_DSP_STEREO, &ao_channels); | |
136 | |
137 // set rate | |
138 ao_samplerate=rate; | |
139 ioctl (audio_fd, SNDCTL_DSP_SPEED, &ao_samplerate); | |
140 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
|
141 } |
954 | 142 |
143 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)==-1){ | |
144 int r=0; | |
145 printf("audio_setup: driver doesn't support SNDCTL_DSP_GETOSPACE :-(\n"); | |
146 if(ioctl(audio_fd, SNDCTL_DSP_GETBLKSIZE, &r)==-1){ | |
147 printf("audio_setup: %d bytes/frag (config.h)\n",ao_outburst); | |
148 } else { | |
149 ao_outburst=r; | |
150 printf("audio_setup: %d bytes/frag (GETBLKSIZE)\n",ao_outburst); | |
151 } | |
152 } else { | |
153 printf("audio_setup: frags: %3d/%d (%d bytes/frag) free: %6d\n", | |
154 zz.fragments, zz.fragstotal, zz.fragsize, zz.bytes); | |
155 if(ao_buffersize==-1) ao_buffersize=zz.bytes; | |
156 ao_outburst=zz.fragsize; | |
157 } | |
158 | |
159 if(ao_buffersize==-1){ | |
160 // Measuring buffer size: | |
161 void* data; | |
162 ao_buffersize=0; | |
163 #ifdef HAVE_AUDIO_SELECT | |
164 data=malloc(ao_outburst); memset(data,0,ao_outburst); | |
165 while(ao_buffersize<0x40000){ | |
166 fd_set rfds; | |
167 struct timeval tv; | |
168 FD_ZERO(&rfds); FD_SET(audio_fd,&rfds); | |
169 tv.tv_sec=0; tv.tv_usec = 0; | |
170 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) break; | |
171 write(audio_fd,data,ao_outburst); | |
172 ao_buffersize+=ao_outburst; | |
173 } | |
174 free(data); | |
175 if(ao_buffersize==0){ | |
176 printf("\n *** Your audio driver DOES NOT support select() ***\n"); | |
177 printf("Recompile mplayer with #undef HAVE_AUDIO_SELECT in config.h !\n\n"); | |
178 return 0; | |
179 } | |
180 #endif | |
181 } | |
182 | |
183 return 1; | |
184 } | |
185 | |
186 // close audio device | |
187 static void uninit(){ | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
188 #ifdef SNDCTL_DSP_RESET |
954 | 189 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
|
190 #endif |
954 | 191 close(audio_fd); |
192 } | |
193 | |
194 // stop playing and empty buffers (for seeking/pause) | |
195 static void reset(){ | |
196 uninit(); | |
197 audio_fd=open(dsp, O_WRONLY); | |
198 if(audio_fd<0){ | |
199 printf("\nFatal error: *** CANNOT RE-OPEN / RESET AUDIO DEVICE ***\n"); | |
200 return; | |
201 } | |
202 | |
203 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
|
204 if(ao_format != AFMT_AC3) { |
954 | 205 ioctl (audio_fd, SNDCTL_DSP_STEREO, &ao_channels); |
206 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
|
207 } |
954 | 208 } |
209 | |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
210 // 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
|
211 static void audio_pause() |
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 // 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
|
214 reset(); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
215 } |
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 // 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
|
218 static void audio_resume() |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
219 { |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
220 } |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
221 |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
222 |
954 | 223 // return: how many bytes can be played without blocking |
224 static int get_space(){ | |
225 int playsize=ao_outburst; | |
226 | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
227 #ifdef SNDCTL_DSP_GETOSPACE |
954 | 228 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)!=-1){ |
229 // calculate exact buffer space: | |
230 return zz.fragments*zz.fragsize; | |
231 } | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
232 #endif |
954 | 233 |
234 // check buffer | |
235 #ifdef HAVE_AUDIO_SELECT | |
236 { fd_set rfds; | |
237 struct timeval tv; | |
238 FD_ZERO(&rfds); | |
239 FD_SET(audio_fd, &rfds); | |
240 tv.tv_sec = 0; | |
241 tv.tv_usec = 0; | |
242 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) return 0; // not block! | |
243 } | |
244 #endif | |
245 | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
246 return ao_outburst; |
954 | 247 } |
248 | |
249 // plays 'len' bytes of 'data' | |
250 // it should round it down to outburst*n | |
251 // return: number of bytes played | |
252 static int play(void* data,int len,int flags){ | |
253 len/=ao_outburst; | |
254 len=write(audio_fd,data,len*ao_outburst); | |
255 return len; | |
256 } | |
257 | |
258 static int audio_delay_method=2; | |
259 | |
260 // return: how many unplayed bytes are in the buffer | |
261 static int get_delay(){ | |
262 if(audio_delay_method==2){ | |
263 // | |
264 int r=0; | |
265 if(ioctl(audio_fd, SNDCTL_DSP_GETODELAY, &r)!=-1) | |
266 return r; | |
267 audio_delay_method=1; // fallback if not supported | |
268 } | |
269 if(audio_delay_method==1){ | |
270 // SNDCTL_DSP_GETOSPACE | |
271 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)!=-1) | |
272 return ao_buffersize-zz.bytes; | |
273 audio_delay_method=0; // fallback if not supported | |
274 } | |
275 return ao_buffersize; | |
276 } | |
277 |