Mercurial > mplayer.hg
annotate libao2/ao_sdl.c @ 1125:3ec8f4779e81
-frames 0 fixed
author | arpi_esp |
---|---|
date | Wed, 13 Jun 2001 21:57:36 +0000 |
parents | 06fd776f6b59 |
children | 7c6bcb281966 |
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 /* audio driver to be used by SDLlib */ | |
42 char *sdl_adriver; | |
43 | |
972 | 44 // Samplesize used by the SDLlib AudioSpec struct |
993
dfa641c44d4a
Increased sample size to solve sound problems one some systems.
atmosfear
parents:
985
diff
changeset
|
45 #define SAMPLESIZE 1024 |
972 | 46 |
47 // General purpose Ring-buffering routines | |
48 | |
974 | 49 #define BUFFSIZE 4096 |
1066 | 50 #define NUM_BUFS 16 |
972 | 51 |
52 static unsigned char *buffer[NUM_BUFS]; | |
53 | |
54 static unsigned int buf_read=0; | |
55 static unsigned int buf_write=0; | |
56 static unsigned int buf_read_pos=0; | |
57 static unsigned int buf_write_pos=0; | |
58 | |
59 static int full_buffers=0; | |
60 static int buffered_bytes=0; | |
61 | |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
62 |
972 | 63 static int write_buffer(unsigned char* data,int len){ |
64 int len2=0; | |
65 int x; | |
66 while(len>0){ | |
67 if(full_buffers==NUM_BUFS) break; | |
68 x=BUFFSIZE-buf_write_pos; | |
69 if(x>len) x=len; | |
70 memcpy(buffer[buf_write]+buf_write_pos,data+len2,x); | |
71 len2+=x; len-=x; | |
72 buffered_bytes+=x; buf_write_pos+=x; | |
73 if(buf_write_pos>=BUFFSIZE){ | |
74 // block is full, find next! | |
75 buf_write=(buf_write+1)%NUM_BUFS; | |
76 ++full_buffers; | |
77 buf_write_pos=0; | |
78 } | |
79 } | |
80 return len2; | |
81 } | |
82 | |
83 static int read_buffer(unsigned char* data,int len){ | |
84 int len2=0; | |
85 int x; | |
86 while(len>0){ | |
87 if(full_buffers==0) break; // no more data buffered! | |
88 x=BUFFSIZE-buf_read_pos; | |
89 if(x>len) x=len; | |
90 memcpy(data+len2,buffer[buf_read]+buf_read_pos,x); | |
91 len2+=x; len-=x; | |
92 buffered_bytes-=x; buf_read_pos+=x; | |
93 if(buf_read_pos>=BUFFSIZE){ | |
94 // block is empty, find next! | |
95 buf_read=(buf_read+1)%NUM_BUFS; | |
96 --full_buffers; | |
97 buf_read_pos=0; | |
98 } | |
99 } | |
100 return len2; | |
101 } | |
102 | |
103 // end ring buffer stuff | |
966 | 104 |
105 #ifdef __FreeBSD__ | |
106 #include <SDL11/SDL.h> | |
107 #else | |
108 #include <SDL/SDL.h> | |
109 #endif | |
110 | |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
111 #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
|
112 /* 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
|
113 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
|
114 { |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
115 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
|
116 char *env = malloc(len); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
117 |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
118 if (env != NULL) { |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
119 strcpy(env, name); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
120 strcat(env, "="); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
121 strcat(env, val); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
122 putenv(env); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
123 } |
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 #endif |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
126 |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
127 |
966 | 128 // to set/get/query special features/parameters |
129 static int control(int cmd,int arg){ | |
130 return -1; | |
131 } | |
132 | |
972 | 133 // SDL Callback function |
134 void outputaudio(void *unused, Uint8 *stream, int len) { | |
135 //SDL_MixAudio(stream, read_buffer(buffers, len), len, SDL_MIX_MAXVOLUME); | |
1066 | 136 //if(!full_buffers) printf("SDL: Buffer underrun!\n"); |
137 | |
972 | 138 read_buffer(stream, len); |
1066 | 139 //printf("SDL: Full Buffers: %i\n", full_buffers); |
966 | 140 } |
141 | |
142 // open & setup audio device | |
143 // return: 1=success 0=fail | |
144 static int init(int rate,int channels,int format,int flags){ | |
145 | |
972 | 146 /* SDL Audio Specifications */ |
147 SDL_AudioSpec aspec; | |
148 | |
149 int i; | |
150 /* Allocate ring-buffer memory */ | |
983 | 151 for(i=0;i<NUM_BUFS;i++) buffer[i]=(unsigned char *) malloc(BUFFSIZE); |
972 | 152 |
1070 | 153 printf("SDL: Samplerate: %iHz Channels: %s Format %s\n", rate, (channels > 1) ? "Stereo" : "Mono", audio_out_format_name(format)); |
983 | 154 |
155 if(sdl_adriver) { | |
156 setenv("SDL_AUDIODRIVER", sdl_adriver, 1); | |
157 printf("SDL: using %s audio driver\n", sdl_adriver); | |
158 } | |
159 | |
966 | 160 |
1066 | 161 /* The desired audio format (see SDL_AudioSpec) */ |
162 switch(format) { | |
163 case AFMT_U8: | |
164 aspec.format = AUDIO_U8; | |
165 break; | |
166 case AFMT_S16_LE: | |
167 aspec.format = AUDIO_S16LSB; | |
168 break; | |
169 case AFMT_S16_BE: | |
170 aspec.format = AUDIO_S16MSB; | |
171 break; | |
172 case AFMT_S8: | |
173 aspec.format = AUDIO_S8; | |
174 break; | |
175 case AFMT_U16_LE: | |
176 aspec.format = AUDIO_U16LSB; | |
177 break; | |
178 case AFMT_U16_BE: | |
179 aspec.format = AUDIO_U16MSB; | |
180 break; | |
181 default: | |
182 printf("SDL: Unsupported audio format: 0x%x.\n", format); | |
183 return 0; | |
184 } | |
185 | |
966 | 186 /* The desired audio frequency in samples-per-second. */ |
187 aspec.freq = rate; | |
188 | |
189 /* Number of channels (mono/stereo) */ | |
190 aspec.channels = channels; | |
191 | |
192 /* 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 | 193 aspec.samples = SAMPLESIZE; |
966 | 194 |
195 /* 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: | |
196 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 | 197 aspec.callback = outputaudio; |
966 | 198 |
199 /* This pointer is passed as the first parameter to the callback function. */ | |
200 aspec.userdata = NULL; | |
201 | |
972 | 202 /* initialize the SDL Audio system */ |
203 if (SDL_Init (SDL_INIT_AUDIO/*|SDL_INIT_NOPARACHUTE*/)) { | |
204 printf("SDL: Initializing of SDL Audio failed: %s.\n", SDL_GetError()); | |
205 return 0; | |
206 } | |
207 | |
966 | 208 /* Open the audio device and start playing sound! */ |
209 if(SDL_OpenAudio(&aspec, NULL) < 0) { | |
210 printf("SDL: Unable to open audio: %s\n", SDL_GetError()); | |
211 return(0); | |
212 } | |
213 | |
983 | 214 if(verbose) printf("SDL: buf size = %d\n",aspec.size); |
975 | 215 if(ao_buffersize==-1) ao_buffersize=aspec.size; |
974 | 216 |
966 | 217 /* unsilence audio, if callback is ready */ |
218 SDL_PauseAudio(0); | |
219 | |
220 return 1; | |
221 } | |
222 | |
223 // close audio device | |
224 static void uninit(){ | |
983 | 225 if(verbose) printf("SDL: Audio Subsystem shutting down!\n"); |
966 | 226 SDL_CloseAudio(); |
972 | 227 SDL_QuitSubSystem(SDL_INIT_AUDIO); |
966 | 228 } |
229 | |
230 // stop playing and empty buffers (for seeking/pause) | |
231 static void reset(){ | |
1066 | 232 |
1091 | 233 //printf("SDL: reset called!\n"); |
1066 | 234 |
972 | 235 /* Reset ring-buffer state */ |
236 buf_read=0; | |
237 buf_write=0; | |
238 buf_read_pos=0; | |
239 buf_write_pos=0; | |
240 | |
241 full_buffers=0; | |
242 buffered_bytes=0; | |
966 | 243 |
244 } | |
245 | |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
246 // 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
|
247 static void audio_pause() |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
248 { |
1070 | 249 |
1091 | 250 //printf("SDL: audio_pause called!\n"); |
251 SDL_LockAudio(); | |
1070 | 252 |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
253 } |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
254 |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
255 // 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
|
256 static void audio_resume() |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
257 { |
1091 | 258 //printf("SDL: audio_resume called!\n"); |
259 SDL_UnlockAudio(); | |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
260 } |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
261 |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
262 |
966 | 263 // return: how many bytes can be played without blocking |
264 static int get_space(){ | |
972 | 265 return (NUM_BUFS-full_buffers)*BUFFSIZE - buf_write_pos; |
966 | 266 } |
267 | |
268 // plays 'len' bytes of 'data' | |
269 // it should round it down to outburst*n | |
270 // return: number of bytes played | |
271 static int play(void* data,int len,int flags){ | |
972 | 272 |
985 | 273 #if 0 |
972 | 274 int ret; |
966 | 275 |
972 | 276 /* Audio locking prohibits call of outputaudio */ |
966 | 277 SDL_LockAudio(); |
972 | 278 // copy audio stream into ring-buffer |
279 ret = write_buffer(data, len); | |
966 | 280 SDL_UnlockAudio(); |
281 | |
972 | 282 return ret; |
283 #else | |
284 return write_buffer(data, len); | |
285 #endif | |
966 | 286 } |
287 | |
288 // return: how many unplayed bytes are in the buffer | |
289 static int get_delay(){ | |
974 | 290 return buffered_bytes + ao_buffersize; |
966 | 291 } |
292 | |
293 | |
294 | |
295 | |
296 | |
297 |