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