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