Mercurial > mplayer.hg
annotate libao2/ao_sdl.c @ 2910:56428bdf583e
removed bad and not proven statemets...
author | arpi |
---|---|
date | Wed, 14 Nov 2001 22:45:53 +0000 |
parents | e7d98f8f9459 |
children | 981a9e5118ce |
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 |
1238 | 103 #include <SDL.h> |
966 | 104 |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
105 #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
|
106 /* 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
|
107 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
|
108 { |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
109 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
|
110 char *env = malloc(len); |
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 if (env != NULL) { |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
113 strcpy(env, name); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
114 strcat(env, "="); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
115 strcat(env, val); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
116 putenv(env); |
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 } |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
119 #endif |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
120 |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
121 |
966 | 122 // to set/get/query special features/parameters |
123 static int control(int cmd,int arg){ | |
124 return -1; | |
125 } | |
126 | |
972 | 127 // SDL Callback function |
128 void outputaudio(void *unused, Uint8 *stream, int len) { | |
129 //SDL_MixAudio(stream, read_buffer(buffers, len), len, SDL_MIX_MAXVOLUME); | |
1066 | 130 //if(!full_buffers) printf("SDL: Buffer underrun!\n"); |
131 | |
972 | 132 read_buffer(stream, len); |
1066 | 133 //printf("SDL: Full Buffers: %i\n", full_buffers); |
966 | 134 } |
135 | |
136 // open & setup audio device | |
137 // return: 1=success 0=fail | |
138 static int init(int rate,int channels,int format,int flags){ | |
139 | |
972 | 140 /* SDL Audio Specifications */ |
141 SDL_AudioSpec aspec; | |
142 | |
143 int i; | |
144 /* Allocate ring-buffer memory */ | |
983 | 145 for(i=0;i<NUM_BUFS;i++) buffer[i]=(unsigned char *) malloc(BUFFSIZE); |
972 | 146 |
1070 | 147 printf("SDL: Samplerate: %iHz Channels: %s Format %s\n", rate, (channels > 1) ? "Stereo" : "Mono", audio_out_format_name(format)); |
983 | 148 |
1189 | 149 if(ao_subdevice) { |
150 setenv("SDL_AUDIODRIVER", ao_subdevice, 1); | |
151 printf("SDL: using %s audio driver\n", ao_subdevice); | |
152 } | |
966 | 153 |
1066 | 154 /* The desired audio format (see SDL_AudioSpec) */ |
155 switch(format) { | |
156 case AFMT_U8: | |
157 aspec.format = AUDIO_U8; | |
158 break; | |
159 case AFMT_S16_LE: | |
160 aspec.format = AUDIO_S16LSB; | |
161 break; | |
162 case AFMT_S16_BE: | |
163 aspec.format = AUDIO_S16MSB; | |
164 break; | |
165 case AFMT_S8: | |
166 aspec.format = AUDIO_S8; | |
167 break; | |
168 case AFMT_U16_LE: | |
169 aspec.format = AUDIO_U16LSB; | |
170 break; | |
171 case AFMT_U16_BE: | |
172 aspec.format = AUDIO_U16MSB; | |
173 break; | |
174 default: | |
175 printf("SDL: Unsupported audio format: 0x%x.\n", format); | |
176 return 0; | |
177 } | |
178 | |
966 | 179 /* The desired audio frequency in samples-per-second. */ |
180 aspec.freq = rate; | |
181 | |
182 /* Number of channels (mono/stereo) */ | |
183 aspec.channels = channels; | |
184 | |
185 /* 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 | 186 aspec.samples = SAMPLESIZE; |
966 | 187 |
188 /* 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: | |
189 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 | 190 aspec.callback = outputaudio; |
966 | 191 |
192 /* This pointer is passed as the first parameter to the callback function. */ | |
193 aspec.userdata = NULL; | |
194 | |
972 | 195 /* initialize the SDL Audio system */ |
196 if (SDL_Init (SDL_INIT_AUDIO/*|SDL_INIT_NOPARACHUTE*/)) { | |
197 printf("SDL: Initializing of SDL Audio failed: %s.\n", SDL_GetError()); | |
198 return 0; | |
199 } | |
200 | |
966 | 201 /* Open the audio device and start playing sound! */ |
202 if(SDL_OpenAudio(&aspec, NULL) < 0) { | |
203 printf("SDL: Unable to open audio: %s\n", SDL_GetError()); | |
204 return(0); | |
205 } | |
206 | |
983 | 207 if(verbose) printf("SDL: buf size = %d\n",aspec.size); |
975 | 208 if(ao_buffersize==-1) ao_buffersize=aspec.size; |
974 | 209 |
966 | 210 /* unsilence audio, if callback is ready */ |
211 SDL_PauseAudio(0); | |
212 | |
213 return 1; | |
214 } | |
215 | |
216 // close audio device | |
217 static void uninit(){ | |
983 | 218 if(verbose) printf("SDL: Audio Subsystem shutting down!\n"); |
966 | 219 SDL_CloseAudio(); |
972 | 220 SDL_QuitSubSystem(SDL_INIT_AUDIO); |
966 | 221 } |
222 | |
223 // stop playing and empty buffers (for seeking/pause) | |
224 static void reset(){ | |
1066 | 225 |
1091 | 226 //printf("SDL: reset called!\n"); |
1066 | 227 |
972 | 228 /* Reset ring-buffer state */ |
229 buf_read=0; | |
230 buf_write=0; | |
231 buf_read_pos=0; | |
232 buf_write_pos=0; | |
233 | |
234 full_buffers=0; | |
235 buffered_bytes=0; | |
966 | 236 |
237 } | |
238 | |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
239 // 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
|
240 static void audio_pause() |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
241 { |
1070 | 242 |
1091 | 243 //printf("SDL: audio_pause called!\n"); |
244 SDL_LockAudio(); | |
1070 | 245 |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
246 } |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
247 |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
248 // 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
|
249 static void audio_resume() |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
250 { |
1091 | 251 //printf("SDL: audio_resume called!\n"); |
252 SDL_UnlockAudio(); | |
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 |
966 | 256 // return: how many bytes can be played without blocking |
257 static int get_space(){ | |
972 | 258 return (NUM_BUFS-full_buffers)*BUFFSIZE - buf_write_pos; |
966 | 259 } |
260 | |
261 // plays 'len' bytes of 'data' | |
262 // it should round it down to outburst*n | |
263 // return: number of bytes played | |
264 static int play(void* data,int len,int flags){ | |
972 | 265 |
985 | 266 #if 0 |
972 | 267 int ret; |
966 | 268 |
972 | 269 /* Audio locking prohibits call of outputaudio */ |
966 | 270 SDL_LockAudio(); |
972 | 271 // copy audio stream into ring-buffer |
272 ret = write_buffer(data, len); | |
966 | 273 SDL_UnlockAudio(); |
274 | |
972 | 275 return ret; |
276 #else | |
277 return write_buffer(data, len); | |
278 #endif | |
966 | 279 } |
280 | |
281 // return: how many unplayed bytes are in the buffer | |
282 static int get_delay(){ | |
974 | 283 return buffered_bytes + ao_buffersize; |
966 | 284 } |
285 | |
286 | |
287 | |
288 | |
289 | |
290 |