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