Mercurial > mplayer.hg
annotate libao2/ao_sdl.c @ 1203:5f0d9239c5b2
sub at key=0 fixed
author | arpi_esp |
---|---|
date | Sat, 23 Jun 2001 16:59:40 +0000 |
parents | 7c6bcb281966 |
children | e7d98f8f9459 |
rev | line source |
---|---|
972 | 1 /* |
2 * ao_sdl.c - libao2 SDLlib Audio Output Driver for MPlayer | |
3 * | |
4 * This driver is under the same license as MPlayer. | |
5 * (http://mplayer.sf.net) | |
6 * | |
7 * Copyleft 2001 by Felix Bünemann (atmosfear@users.sf.net) | |
8 * | |
9 * Thanks to Arpi for nice ringbuffer-code! | |
10 * | |
11 */ | |
12 | |
966 | 13 #include <stdio.h> |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
14 #include <stdlib.h> |
966 | 15 |
16 #include "audio_out.h" | |
17 #include "audio_out_internal.h" | |
1066 | 18 #include "afmt.h" |
966 | 19 |
972 | 20 #include "../libvo/fastmemcpy.h" |
21 | |
966 | 22 static ao_info_t info = |
23 { | |
24 "SDLlib audio output", | |
25 "sdl", | |
26 "Felix Buenemann <atmosfear@users.sourceforge.net>", | |
27 "" | |
28 }; | |
29 | |
30 LIBAO_EXTERN(sdl) | |
31 | |
32 // there are some globals: | |
33 // ao_samplerate | |
34 // ao_channels | |
35 // ao_format | |
36 // ao_bps | |
37 // ao_outburst | |
38 // ao_buffersize | |
39 | |
983 | 40 extern int verbose; |
41 | |
972 | 42 // Samplesize used by the SDLlib AudioSpec struct |
993
dfa641c44d4a
Increased sample size to solve sound problems one some systems.
atmosfear
parents:
985
diff
changeset
|
43 #define SAMPLESIZE 1024 |
972 | 44 |
45 // General purpose Ring-buffering routines | |
46 | |
974 | 47 #define BUFFSIZE 4096 |
1066 | 48 #define NUM_BUFS 16 |
972 | 49 |
50 static unsigned char *buffer[NUM_BUFS]; | |
51 | |
52 static unsigned int buf_read=0; | |
53 static unsigned int buf_write=0; | |
54 static unsigned int buf_read_pos=0; | |
55 static unsigned int buf_write_pos=0; | |
56 | |
57 static int full_buffers=0; | |
58 static int buffered_bytes=0; | |
59 | |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
60 |
972 | 61 static int write_buffer(unsigned char* data,int len){ |
62 int len2=0; | |
63 int x; | |
64 while(len>0){ | |
65 if(full_buffers==NUM_BUFS) break; | |
66 x=BUFFSIZE-buf_write_pos; | |
67 if(x>len) x=len; | |
68 memcpy(buffer[buf_write]+buf_write_pos,data+len2,x); | |
69 len2+=x; len-=x; | |
70 buffered_bytes+=x; buf_write_pos+=x; | |
71 if(buf_write_pos>=BUFFSIZE){ | |
72 // block is full, find next! | |
73 buf_write=(buf_write+1)%NUM_BUFS; | |
74 ++full_buffers; | |
75 buf_write_pos=0; | |
76 } | |
77 } | |
78 return len2; | |
79 } | |
80 | |
81 static int read_buffer(unsigned char* data,int len){ | |
82 int len2=0; | |
83 int x; | |
84 while(len>0){ | |
85 if(full_buffers==0) break; // no more data buffered! | |
86 x=BUFFSIZE-buf_read_pos; | |
87 if(x>len) x=len; | |
88 memcpy(data+len2,buffer[buf_read]+buf_read_pos,x); | |
89 len2+=x; len-=x; | |
90 buffered_bytes-=x; buf_read_pos+=x; | |
91 if(buf_read_pos>=BUFFSIZE){ | |
92 // block is empty, find next! | |
93 buf_read=(buf_read+1)%NUM_BUFS; | |
94 --full_buffers; | |
95 buf_read_pos=0; | |
96 } | |
97 } | |
98 return len2; | |
99 } | |
100 | |
101 // end ring buffer stuff | |
966 | 102 |
103 #ifdef __FreeBSD__ | |
104 #include <SDL11/SDL.h> | |
105 #else | |
106 #include <SDL/SDL.h> | |
107 #endif | |
108 | |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
109 #if defined(sun) && defined(__svr4__) |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
110 /* setenv is missing on solaris */ |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
111 static void setenv(const char *name, const char *val, int _xx) |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
112 { |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
113 int len = strlen(name) + strlen(val) + 2; |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
114 char *env = malloc(len); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
115 |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
116 if (env != NULL) { |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
117 strcpy(env, name); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
118 strcat(env, "="); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
119 strcat(env, val); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
120 putenv(env); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
121 } |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
122 } |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
123 #endif |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
124 |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
125 |
966 | 126 // to set/get/query special features/parameters |
127 static int control(int cmd,int arg){ | |
128 return -1; | |
129 } | |
130 | |
972 | 131 // SDL Callback function |
132 void outputaudio(void *unused, Uint8 *stream, int len) { | |
133 //SDL_MixAudio(stream, read_buffer(buffers, len), len, SDL_MIX_MAXVOLUME); | |
1066 | 134 //if(!full_buffers) printf("SDL: Buffer underrun!\n"); |
135 | |
972 | 136 read_buffer(stream, len); |
1066 | 137 //printf("SDL: Full Buffers: %i\n", full_buffers); |
966 | 138 } |
139 | |
140 // open & setup audio device | |
141 // return: 1=success 0=fail | |
142 static int init(int rate,int channels,int format,int flags){ | |
143 | |
972 | 144 /* SDL Audio Specifications */ |
145 SDL_AudioSpec aspec; | |
146 | |
147 int i; | |
148 /* Allocate ring-buffer memory */ | |
983 | 149 for(i=0;i<NUM_BUFS;i++) buffer[i]=(unsigned char *) malloc(BUFFSIZE); |
972 | 150 |
1070 | 151 printf("SDL: Samplerate: %iHz Channels: %s Format %s\n", rate, (channels > 1) ? "Stereo" : "Mono", audio_out_format_name(format)); |
983 | 152 |
1189 | 153 if(ao_subdevice) { |
154 setenv("SDL_AUDIODRIVER", ao_subdevice, 1); | |
155 printf("SDL: using %s audio driver\n", ao_subdevice); | |
156 } | |
966 | 157 |
1066 | 158 /* The desired audio format (see SDL_AudioSpec) */ |
159 switch(format) { | |
160 case AFMT_U8: | |
161 aspec.format = AUDIO_U8; | |
162 break; | |
163 case AFMT_S16_LE: | |
164 aspec.format = AUDIO_S16LSB; | |
165 break; | |
166 case AFMT_S16_BE: | |
167 aspec.format = AUDIO_S16MSB; | |
168 break; | |
169 case AFMT_S8: | |
170 aspec.format = AUDIO_S8; | |
171 break; | |
172 case AFMT_U16_LE: | |
173 aspec.format = AUDIO_U16LSB; | |
174 break; | |
175 case AFMT_U16_BE: | |
176 aspec.format = AUDIO_U16MSB; | |
177 break; | |
178 default: | |
179 printf("SDL: Unsupported audio format: 0x%x.\n", format); | |
180 return 0; | |
181 } | |
182 | |
966 | 183 /* The desired audio frequency in samples-per-second. */ |
184 aspec.freq = rate; | |
185 | |
186 /* Number of channels (mono/stereo) */ | |
187 aspec.channels = channels; | |
188 | |
189 /* The desired size of the audio buffer in samples. This number should be a power of two, and may be adjusted by the audio driver to a value more suitable for the hardware. Good values seem to range between 512 and 8192 inclusive, depending on the application and CPU speed. Smaller values yield faster response time, but can lead to underflow if the application is doing heavy processing and cannot fill the audio buffer in time. A stereo sample consists of both right and left channels in LR ordering. Note that the number of samples is directly related to time by the following formula: ms = (samples*1000)/freq */ | |
972 | 190 aspec.samples = SAMPLESIZE; |
966 | 191 |
192 /* This should be set to a function that will be called when the audio device is ready for more data. It is passed a pointer to the audio buffer, and the length in bytes of the audio buffer. This function usually runs in a separate thread, and so you should protect data structures that it accesses by calling SDL_LockAudio and SDL_UnlockAudio in your code. The callback prototype is: | |
193 void callback(void *userdata, Uint8 *stream, int len); userdata is the pointer stored in userdata field of the SDL_AudioSpec. stream is a pointer to the audio buffer you want to fill with information and len is the length of the audio buffer in bytes. */ | |
972 | 194 aspec.callback = outputaudio; |
966 | 195 |
196 /* This pointer is passed as the first parameter to the callback function. */ | |
197 aspec.userdata = NULL; | |
198 | |
972 | 199 /* initialize the SDL Audio system */ |
200 if (SDL_Init (SDL_INIT_AUDIO/*|SDL_INIT_NOPARACHUTE*/)) { | |
201 printf("SDL: Initializing of SDL Audio failed: %s.\n", SDL_GetError()); | |
202 return 0; | |
203 } | |
204 | |
966 | 205 /* Open the audio device and start playing sound! */ |
206 if(SDL_OpenAudio(&aspec, NULL) < 0) { | |
207 printf("SDL: Unable to open audio: %s\n", SDL_GetError()); | |
208 return(0); | |
209 } | |
210 | |
983 | 211 if(verbose) printf("SDL: buf size = %d\n",aspec.size); |
975 | 212 if(ao_buffersize==-1) ao_buffersize=aspec.size; |
974 | 213 |
966 | 214 /* unsilence audio, if callback is ready */ |
215 SDL_PauseAudio(0); | |
216 | |
217 return 1; | |
218 } | |
219 | |
220 // close audio device | |
221 static void uninit(){ | |
983 | 222 if(verbose) printf("SDL: Audio Subsystem shutting down!\n"); |
966 | 223 SDL_CloseAudio(); |
972 | 224 SDL_QuitSubSystem(SDL_INIT_AUDIO); |
966 | 225 } |
226 | |
227 // stop playing and empty buffers (for seeking/pause) | |
228 static void reset(){ | |
1066 | 229 |
1091 | 230 //printf("SDL: reset called!\n"); |
1066 | 231 |
972 | 232 /* Reset ring-buffer state */ |
233 buf_read=0; | |
234 buf_write=0; | |
235 buf_read_pos=0; | |
236 buf_write_pos=0; | |
237 | |
238 full_buffers=0; | |
239 buffered_bytes=0; | |
966 | 240 |
241 } | |
242 | |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
243 // stop playing, keep buffers (for pause) |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
244 static void audio_pause() |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
245 { |
1070 | 246 |
1091 | 247 //printf("SDL: audio_pause called!\n"); |
248 SDL_LockAudio(); | |
1070 | 249 |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
250 } |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
251 |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
252 // resume playing, after audio_pause() |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
253 static void audio_resume() |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
254 { |
1091 | 255 //printf("SDL: audio_resume called!\n"); |
256 SDL_UnlockAudio(); | |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
257 } |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
258 |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
259 |
966 | 260 // return: how many bytes can be played without blocking |
261 static int get_space(){ | |
972 | 262 return (NUM_BUFS-full_buffers)*BUFFSIZE - buf_write_pos; |
966 | 263 } |
264 | |
265 // plays 'len' bytes of 'data' | |
266 // it should round it down to outburst*n | |
267 // return: number of bytes played | |
268 static int play(void* data,int len,int flags){ | |
972 | 269 |
985 | 270 #if 0 |
972 | 271 int ret; |
966 | 272 |
972 | 273 /* Audio locking prohibits call of outputaudio */ |
966 | 274 SDL_LockAudio(); |
972 | 275 // copy audio stream into ring-buffer |
276 ret = write_buffer(data, len); | |
966 | 277 SDL_UnlockAudio(); |
278 | |
972 | 279 return ret; |
280 #else | |
281 return write_buffer(data, len); | |
282 #endif | |
966 | 283 } |
284 | |
285 // return: how many unplayed bytes are in the buffer | |
286 static int get_delay(){ | |
974 | 287 return buffered_bytes + ao_buffersize; |
966 | 288 } |
289 | |
290 | |
291 | |
292 | |
293 | |
294 |