1041
|
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/audioio.h>
|
|
11 #ifdef __svr4__
|
|
12 #include <stropts.h>
|
|
13 #endif
|
|
14
|
|
15 #include "../config.h"
|
|
16
|
|
17 #include "audio_out.h"
|
|
18 #include "audio_out_internal.h"
|
|
19
|
|
20 static ao_info_t info =
|
|
21 {
|
|
22 "Sun audio output",
|
|
23 "sun",
|
|
24 "jk@tools.de",
|
|
25 ""
|
|
26 };
|
|
27
|
|
28 LIBAO_EXTERN(sun)
|
|
29
|
|
30
|
|
31 #ifndef AUDIO_PRECISION_8
|
|
32 #define AUDIO_PRECISION_8 8
|
|
33 #define AUDIO_PRECISION_16 16
|
|
34 #endif
|
|
35
|
|
36
|
|
37 // there are some globals:
|
|
38 // ao_samplerate
|
|
39 // ao_channels
|
|
40 // ao_format
|
|
41 // ao_bps
|
|
42 // ao_outburst
|
|
43 // ao_buffersize
|
|
44
|
|
45 static char *dsp="/dev/audio";
|
|
46 static int queued_bursts = 0;
|
|
47 static int audio_fd=-1;
|
|
48
|
|
49 // to set/get/query special features/parameters
|
|
50 static int control(int cmd,int arg){
|
|
51 switch(cmd){
|
|
52 case AOCONTROL_SET_DEVICE:
|
|
53 dsp=(char*)arg;
|
|
54 return CONTROL_OK;
|
|
55 case AOCONTROL_QUERY_FORMAT:
|
|
56 return CONTROL_TRUE;
|
|
57 }
|
|
58 return CONTROL_UNKNOWN;
|
|
59 }
|
|
60
|
|
61 // open & setup audio device
|
|
62 // return: 1=success 0=fail
|
|
63 static int init(int rate,int channels,int format,int flags){
|
|
64
|
|
65 audio_info_t info;
|
|
66 int byte_per_sec;
|
|
67
|
|
68 printf("ao2: %d Hz %d chans 0x%X\n",rate,channels,format);
|
|
69
|
|
70 audio_fd=open(dsp, O_WRONLY);
|
|
71 if(audio_fd<0){
|
|
72 printf("Can't open audio device %s -> nosound\n",dsp);
|
|
73 return 0;
|
|
74 }
|
|
75
|
|
76 ioctl(audio_fd, AUDIO_DRAIN, 0);
|
|
77
|
|
78 AUDIO_INITINFO(&info);
|
|
79 info.play.encoding = ao_format = format;
|
|
80 info.play.precision = (format==AUDIO_ENCODING_LINEAR? AUDIO_PRECISION_16:AUDIO_PRECISION_8);
|
|
81 info.play.channels = ao_channels = channels;
|
|
82 --ao_channels;
|
|
83 info.play.sample_rate = ao_samplerate = rate;
|
|
84 info.play.samples = 0;
|
|
85 info.play.eof = 0;
|
|
86 if(ioctl (audio_fd, AUDIO_SETINFO, &info)<0)
|
|
87 printf("audio_setup: your card doesn't support %d Hz samplerate\n",rate);
|
|
88 byte_per_sec = (channels
|
|
89 * (format==AUDIO_ENCODING_LINEAR ? 16 : 8)
|
|
90 * rate);
|
|
91 ao_outburst=byte_per_sec > 100000 ? 16384 : 8192;
|
|
92 queued_bursts = 0;
|
|
93
|
|
94 if(ao_buffersize==-1){
|
|
95 // Measuring buffer size:
|
|
96 void* data;
|
|
97 ao_buffersize=0;
|
|
98 #ifdef HAVE_AUDIO_SELECT
|
|
99 data=malloc(ao_outburst); memset(data,0,ao_outburst);
|
|
100 while(ao_buffersize<0x40000){
|
|
101 fd_set rfds;
|
|
102 struct timeval tv;
|
|
103 FD_ZERO(&rfds); FD_SET(audio_fd,&rfds);
|
|
104 tv.tv_sec=0; tv.tv_usec = 0;
|
|
105 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) break;
|
|
106 write(audio_fd,data,ao_outburst);
|
|
107 ao_buffersize+=ao_outburst;
|
|
108 }
|
|
109 free(data);
|
|
110 if(ao_buffersize==0){
|
|
111 printf("\n *** Your audio driver DOES NOT support select() ***\n");
|
|
112 printf("Recompile mplayer with #undef HAVE_AUDIO_SELECT in config.h !\n\n");
|
|
113 return 0;
|
|
114 }
|
|
115 #ifdef __svr4__
|
|
116 ioctl(audio_fd, I_FLUSH, FLUSHW);
|
|
117 #endif
|
|
118 ioctl(audio_fd, AUDIO_DRAIN, 0);
|
|
119 #endif
|
|
120 }
|
|
121
|
|
122 return 1;
|
|
123 }
|
|
124
|
|
125 // close audio device
|
|
126 static void uninit(){
|
|
127 close(audio_fd);
|
|
128 }
|
|
129
|
|
130 // stop playing and empty buffers (for seeking/pause)
|
|
131 static void reset(){
|
|
132 audio_info_t info;
|
|
133
|
|
134 #ifdef __svr4__
|
|
135 ioctl(audio_fd, I_FLUSH, FLUSHW);
|
|
136 #endif
|
|
137 uninit();
|
|
138 audio_fd=open(dsp, O_WRONLY);
|
|
139 if(audio_fd<0){
|
|
140 printf("\nFatal error: *** CANNOT RE-OPEN / RESET AUDIO DEVICE ***\n");
|
|
141 return;
|
|
142 }
|
|
143
|
|
144 ioctl(audio_fd, AUDIO_DRAIN, 0);
|
|
145
|
|
146 AUDIO_INITINFO(&info);
|
|
147 info.play.encoding = ao_format;
|
|
148 info.play.precision = (ao_format==AUDIO_ENCODING_LINEAR? AUDIO_PRECISION_16:AUDIO_PRECISION_8);
|
|
149 info.play.channels = ao_channels+1;
|
|
150 info.play.sample_rate = ao_samplerate;
|
|
151 info.play.samples = 0;
|
|
152 info.play.eof = 0;
|
|
153 ioctl (audio_fd, AUDIO_SETINFO, &info);
|
|
154 queued_bursts = 0;
|
|
155 }
|
|
156
|
|
157 // stop playing, keep buffers (for pause)
|
|
158 static void audio_pause()
|
|
159 {
|
|
160 struct audio_info info;
|
|
161 AUDIO_INITINFO(&info);
|
|
162 info.play.pause = 1;
|
|
163 ioctl(audio_fd, AUDIO_SETINFO, &info);
|
|
164 }
|
|
165
|
|
166 // resume playing, after audio_pause()
|
|
167 static void audio_resume()
|
|
168 {
|
|
169 struct audio_info info;
|
|
170 AUDIO_INITINFO(&info);
|
|
171 info.play.pause = 0;
|
|
172 ioctl(audio_fd, AUDIO_SETINFO, &info);
|
|
173 }
|
|
174
|
|
175
|
|
176 // return: how many bytes can be played without blocking
|
|
177 static int get_space(){
|
|
178 int playsize=ao_outburst;
|
|
179
|
|
180 // check buffer
|
|
181 #ifdef HAVE_AUDIO_SELECT
|
|
182 { fd_set rfds;
|
|
183 struct timeval tv;
|
|
184 FD_ZERO(&rfds);
|
|
185 FD_SET(audio_fd, &rfds);
|
|
186 tv.tv_sec = 0;
|
|
187 tv.tv_usec = 0;
|
|
188 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) return 0; // not block!
|
|
189 }
|
|
190 #endif
|
|
191
|
|
192 {
|
|
193 audio_info_t info;
|
|
194 ioctl(audio_fd, AUDIO_GETINFO, &info);
|
|
195 if(queued_bursts - info.play.eof > 2)
|
|
196 return 0;
|
|
197 }
|
|
198 return ao_outburst;
|
|
199 }
|
|
200
|
|
201 // plays 'len' bytes of 'data'
|
|
202 // it should round it down to outburst*n
|
|
203 // return: number of bytes played
|
|
204 static int play(void* data,int len,int flags){
|
|
205 len/=ao_outburst;
|
|
206 len=write(audio_fd,data,len*ao_outburst);
|
|
207 if(len>0) {
|
|
208 queued_bursts ++;
|
|
209 write(audio_fd,data,0);
|
|
210 }
|
|
211 return len;
|
|
212 }
|
|
213
|
|
214 static int audio_delay_method=2;
|
|
215
|
|
216 // return: how many unplayed bytes are in the buffer
|
|
217 static int get_delay(){
|
|
218 int q;
|
|
219 audio_info_t info;
|
|
220 ioctl(audio_fd, AUDIO_GETINFO, &info);
|
|
221 return (queued_bursts - info.play.eof) * ao_outburst;
|
|
222 }
|
|
223
|