Mercurial > mplayer.hg
annotate libao2/ao_oss.c @ 1060:a8918dfbc180
streaming is optional: --enable-streaming
author | arpi_esp |
---|---|
date | Fri, 08 Jun 2001 23:36:13 +0000 |
parents | cab5ba9ffc6c |
children | 1e0da351feaa |
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 |
18 static ao_info_t info = | |
19 { | |
20 "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
|
21 "oss", |
954 | 22 "A'rpi", |
23 "" | |
24 }; | |
25 | |
26 LIBAO_EXTERN(oss) | |
27 | |
28 // there are some globals: | |
29 // ao_samplerate | |
30 // ao_channels | |
31 // ao_format | |
32 // ao_bps | |
33 // ao_outburst | |
34 // ao_buffersize | |
35 | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
36 static char *dsp="/dev/dsp"; |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
37 static audio_buf_info zz; |
954 | 38 static int audio_fd=-1; |
39 | |
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; | |
48 } | |
49 return CONTROL_UNKNOWN; | |
50 } | |
51 | |
52 // open & setup audio device | |
53 // return: 1=success 0=fail | |
54 static int init(int rate,int channels,int format,int flags){ | |
55 | |
56 printf("ao2: %d Hz %d chans 0x%X\n",rate,channels,format); | |
57 | |
58 audio_fd=open(dsp, O_WRONLY); | |
59 if(audio_fd<0){ | |
60 printf("Can't open audio device %s -> nosound\n",dsp); | |
61 return 0; | |
62 } | |
63 | |
64 ao_format=format; | |
65 ioctl (audio_fd, SNDCTL_DSP_SETFMT, &ao_format); | |
66 printf("audio_setup: sample format: 0x%X (requested: 0x%X)\n",ao_format,format); | |
67 | |
68 ao_channels=channels-1; | |
69 ioctl (audio_fd, SNDCTL_DSP_STEREO, &ao_channels); | |
70 | |
71 // set rate | |
72 ao_samplerate=rate; | |
73 ioctl (audio_fd, SNDCTL_DSP_SPEED, &ao_samplerate); | |
74 printf("audio_setup: using %d Hz samplerate (requested: %d)\n",ao_samplerate,rate); | |
75 | |
76 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)==-1){ | |
77 int r=0; | |
78 printf("audio_setup: driver doesn't support SNDCTL_DSP_GETOSPACE :-(\n"); | |
79 if(ioctl(audio_fd, SNDCTL_DSP_GETBLKSIZE, &r)==-1){ | |
80 printf("audio_setup: %d bytes/frag (config.h)\n",ao_outburst); | |
81 } else { | |
82 ao_outburst=r; | |
83 printf("audio_setup: %d bytes/frag (GETBLKSIZE)\n",ao_outburst); | |
84 } | |
85 } else { | |
86 printf("audio_setup: frags: %3d/%d (%d bytes/frag) free: %6d\n", | |
87 zz.fragments, zz.fragstotal, zz.fragsize, zz.bytes); | |
88 if(ao_buffersize==-1) ao_buffersize=zz.bytes; | |
89 ao_outburst=zz.fragsize; | |
90 } | |
91 | |
92 if(ao_buffersize==-1){ | |
93 // Measuring buffer size: | |
94 void* data; | |
95 ao_buffersize=0; | |
96 #ifdef HAVE_AUDIO_SELECT | |
97 data=malloc(ao_outburst); memset(data,0,ao_outburst); | |
98 while(ao_buffersize<0x40000){ | |
99 fd_set rfds; | |
100 struct timeval tv; | |
101 FD_ZERO(&rfds); FD_SET(audio_fd,&rfds); | |
102 tv.tv_sec=0; tv.tv_usec = 0; | |
103 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) break; | |
104 write(audio_fd,data,ao_outburst); | |
105 ao_buffersize+=ao_outburst; | |
106 } | |
107 free(data); | |
108 if(ao_buffersize==0){ | |
109 printf("\n *** Your audio driver DOES NOT support select() ***\n"); | |
110 printf("Recompile mplayer with #undef HAVE_AUDIO_SELECT in config.h !\n\n"); | |
111 return 0; | |
112 } | |
113 #endif | |
114 } | |
115 | |
116 return 1; | |
117 } | |
118 | |
119 // close audio device | |
120 static void uninit(){ | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
121 #ifdef SNDCTL_DSP_RESET |
954 | 122 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
|
123 #endif |
954 | 124 close(audio_fd); |
125 } | |
126 | |
127 // stop playing and empty buffers (for seeking/pause) | |
128 static void reset(){ | |
129 uninit(); | |
130 audio_fd=open(dsp, O_WRONLY); | |
131 if(audio_fd<0){ | |
132 printf("\nFatal error: *** CANNOT RE-OPEN / RESET AUDIO DEVICE ***\n"); | |
133 return; | |
134 } | |
135 | |
136 ioctl (audio_fd, SNDCTL_DSP_SETFMT, &ao_format); | |
137 ioctl (audio_fd, SNDCTL_DSP_STEREO, &ao_channels); | |
138 ioctl (audio_fd, SNDCTL_DSP_SPEED, &ao_samplerate); | |
139 | |
140 } | |
141 | |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
142 // 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
|
143 static void audio_pause() |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
144 { |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
145 // 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
|
146 reset(); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
147 } |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
148 |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
149 // 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
|
150 static void audio_resume() |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
151 { |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
152 } |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
153 |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
154 |
954 | 155 // return: how many bytes can be played without blocking |
156 static int get_space(){ | |
157 int playsize=ao_outburst; | |
158 | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
159 #ifdef SNDCTL_DSP_GETOSPACE |
954 | 160 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)!=-1){ |
161 // calculate exact buffer space: | |
162 return zz.fragments*zz.fragsize; | |
163 } | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
164 #endif |
954 | 165 |
166 // check buffer | |
167 #ifdef HAVE_AUDIO_SELECT | |
168 { fd_set rfds; | |
169 struct timeval tv; | |
170 FD_ZERO(&rfds); | |
171 FD_SET(audio_fd, &rfds); | |
172 tv.tv_sec = 0; | |
173 tv.tv_usec = 0; | |
174 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) return 0; // not block! | |
175 } | |
176 #endif | |
177 | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
178 return ao_outburst; |
954 | 179 } |
180 | |
181 // plays 'len' bytes of 'data' | |
182 // it should round it down to outburst*n | |
183 // return: number of bytes played | |
184 static int play(void* data,int len,int flags){ | |
185 len/=ao_outburst; | |
186 len=write(audio_fd,data,len*ao_outburst); | |
187 return len; | |
188 } | |
189 | |
190 static int audio_delay_method=2; | |
191 | |
192 // return: how many unplayed bytes are in the buffer | |
193 static int get_delay(){ | |
194 if(audio_delay_method==2){ | |
195 // | |
196 int r=0; | |
197 if(ioctl(audio_fd, SNDCTL_DSP_GETODELAY, &r)!=-1) | |
198 return r; | |
199 audio_delay_method=1; // fallback if not supported | |
200 } | |
201 if(audio_delay_method==1){ | |
202 // SNDCTL_DSP_GETOSPACE | |
203 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)!=-1) | |
204 return ao_buffersize-zz.bytes; | |
205 audio_delay_method=0; // fallback if not supported | |
206 } | |
207 return ao_buffersize; | |
208 } | |
209 |