3096
|
1 #include <stdio.h>
|
|
2 #include <stdlib.h>
|
|
3
|
|
4 #include "../config.h"
|
|
5
|
|
6 #include "audio_out.h"
|
|
7 #include "audio_out_internal.h"
|
|
8
|
|
9 #include "audio_plugin.h"
|
|
10
|
|
11 static ao_info_t info =
|
|
12 {
|
|
13 "Plugin audio output",
|
|
14 "plugin",
|
|
15 "Anders",
|
|
16 ""
|
|
17 };
|
|
18
|
|
19 LIBAO_EXTERN(plugin)
|
|
20
|
3107
|
21 #define plugin(i) (ao_plugin_local_data.plugins[i])
|
|
22 #define driver() (ao_plugin_local_data.driver)
|
3096
|
23
|
3107
|
24 #define NPL 2 //Number of PLugins
|
|
25
|
|
26 extern ao_plugin_functions_t audio_plugin_delay;
|
|
27
|
|
28 // local data
|
3096
|
29 typedef struct ao_plugin_local_data_s
|
|
30 {
|
3107
|
31 char* cfg_plugins; // List of plugins read from cfg-file
|
|
32 ao_functions_t* driver; // Output driver set in mplayer.c
|
|
33 ao_plugin_functions_t** plugins; // List of used plugins
|
|
34 ao_plugin_functions_t* available_plugins[NPL]; // List of abailabel plugins
|
3096
|
35 } ao_plugin_local_data_t;
|
|
36
|
3107
|
37 ao_plugin_local_data_t ao_plugin_local_data={
|
|
38 NULL,
|
|
39 NULL,
|
|
40 NULL,
|
|
41 {
|
|
42 &audio_plugin_delay,
|
|
43 NULL
|
|
44 }
|
|
45 };
|
3096
|
46
|
3107
|
47 // gloabal data
|
3096
|
48 ao_plugin_data_t ao_plugin_data;
|
3107
|
49
|
3096
|
50
|
|
51 // to set/get/query special features/parameters
|
|
52 static int control(int cmd,int arg){
|
3107
|
53 switch(cmd){
|
|
54 case AOCONTROL_SET_PLUGIN_DRIVER:
|
|
55 ao_plugin_local_data.driver=(ao_functions_t*)arg;
|
|
56 return CONTROL_OK;
|
|
57 case AOCONTROL_SET_PLUGIN_LIST:
|
|
58 ao_plugin_local_data.cfg_plugins=(char*)arg;
|
|
59 return CONTROL_OK;
|
|
60 default:
|
|
61 return driver()->control(cmd,arg);
|
|
62 }
|
|
63 return CONTROL_UNKNOWN;
|
|
64 }
|
|
65
|
|
66 // Recursive function for adding plugins
|
|
67 // return 1 for success and 0 for error
|
|
68 int add_plugin(int i,char* cfg){
|
|
69 int cnt=0;
|
|
70 // Find end of plugin name
|
|
71 while((cfg[cnt]!=',')&&(cfg[cnt]!='\0')&&(cnt<100)) cnt++;
|
|
72 if(cnt >= 100)
|
|
73 return 0;
|
|
74
|
|
75 // Is this the last itteration or just another plugin
|
|
76 if(cfg[cnt]=='\0'){
|
|
77 ao_plugin_local_data.plugins=malloc((i+1)*sizeof(ao_plugin_functions_t*));
|
|
78 if(ao_plugin_local_data.plugins){
|
|
79 ao_plugin_local_data.plugins[i+1]=NULL;
|
|
80 // Find the plugin matching the cfg string name
|
|
81 cnt=0;
|
|
82 while(ao_plugin_local_data.available_plugins[cnt] && cnt<20){
|
|
83 if(0==strcmp(ao_plugin_local_data.available_plugins[cnt]->info->short_name,cfg)){
|
|
84 ao_plugin_local_data.plugins[i]=ao_plugin_local_data.available_plugins[cnt];
|
|
85 return 1;
|
|
86 }
|
|
87 cnt++;
|
|
88 }
|
|
89 printf("[plugin]: Invalid plugin: %s \n",cfg);
|
|
90 return 0;
|
|
91 }
|
|
92 else
|
|
93 return 0;
|
|
94 } else {
|
|
95 cfg[cnt]='\0';
|
|
96 if(add_plugin(i+1,&cfg[cnt+1])){
|
|
97 cnt=0;
|
|
98 // Find the plugin matching the cfg string name
|
|
99 while(ao_plugin_local_data.available_plugins[cnt] && cnt < 20){
|
|
100 if(0==strcmp(ao_plugin_local_data.available_plugins[cnt]->info->short_name,cfg)){
|
|
101 ao_plugin_local_data.plugins[i]=ao_plugin_local_data.available_plugins[cnt];
|
|
102 return 1;
|
|
103 }
|
|
104 cnt++;
|
|
105 }
|
|
106 printf("[plugin]: Invalid plugin: %s \n",cfg);
|
|
107 return 0;
|
|
108 }
|
|
109 else
|
|
110 return 0;
|
|
111 }
|
|
112 return 0; // Will never happen...
|
3096
|
113 }
|
|
114
|
|
115 // open & setup audio device and plugins
|
|
116 // return: 1=success 0=fail
|
|
117 static int init(int rate,int channels,int format,int flags){
|
|
118 int ok=1;
|
|
119
|
3107
|
120 /* Create list of plugins from cfg option */
|
3096
|
121 int i=0;
|
3107
|
122 if(ao_plugin_local_data.cfg_plugins){
|
|
123 if(!add_plugin(i,ao_plugin_local_data.cfg_plugins))
|
|
124 return 0;
|
|
125 }
|
3096
|
126
|
|
127 /* Set input parameters and itterate through plugins each plugin
|
|
128 changes the parameters according to its output */
|
|
129 ao_plugin_data.rate=rate;
|
|
130 ao_plugin_data.channels=channels;
|
|
131 ao_plugin_data.format=format;
|
|
132 ao_plugin_data.sz_mult=1;
|
|
133 ao_plugin_data.sz_fix=0;
|
|
134 ao_plugin_data.delay_mult=1;
|
|
135 ao_plugin_data.delay_fix=0;
|
|
136 i=0;
|
|
137 while(plugin(i)&&ok)
|
|
138 ok=plugin(i++)->init();
|
|
139
|
|
140 if(!ok) return 0;
|
|
141
|
3107
|
142 // This should never happen but check anyway
|
|
143 if(NULL==ao_plugin_local_data.driver)
|
|
144 return 0;
|
|
145
|
3096
|
146 ok = driver()->init(ao_plugin_data.rate,
|
3107
|
147 ao_plugin_data.channels,
|
|
148 ao_plugin_data.format,
|
|
149 flags);
|
3096
|
150 if(!ok) return 0;
|
|
151
|
|
152 /* Now that the driver is initialized we can calculate and set the
|
|
153 input and output buffers for each plugin */
|
|
154 ao_plugin_data.len=driver()->get_space();
|
3107
|
155 while((i>0) && ok)
|
|
156 ok=plugin(--i)->control(AOCONTROL_PLUGIN_SET_LEN,ao_plugin_data.len);
|
|
157
|
|
158 if(!ok) return 0;
|
3096
|
159
|
|
160 return 1;
|
|
161 }
|
|
162
|
|
163 // close audio device
|
|
164 static void uninit(){
|
|
165 int i=0;
|
|
166 driver()->uninit();
|
|
167 while(plugin(i))
|
|
168 plugin(i++)->uninit();
|
3107
|
169 if(ao_plugin_local_data.plugins)
|
|
170 free(ao_plugin_local_data.plugins);
|
3096
|
171 }
|
|
172
|
|
173 // stop playing and empty buffers (for seeking/pause)
|
|
174 static void reset(){
|
|
175 int i=0;
|
|
176 driver()->reset();
|
|
177 while(plugin(i))
|
|
178 plugin(i++)->reset();
|
|
179 }
|
|
180
|
|
181 // stop playing, keep buffers (for pause)
|
|
182 static void audio_pause(){
|
|
183 driver()->pause();
|
|
184 }
|
|
185
|
|
186 // resume playing, after audio_pause()
|
|
187 static void audio_resume(){
|
|
188 driver()->resume();
|
|
189 }
|
|
190
|
|
191 // return: how many bytes can be played without blocking
|
|
192 static int get_space(){
|
|
193 double sz=(double)(driver()->get_space());
|
|
194 sz*=ao_plugin_data.sz_mult;
|
|
195 sz+=ao_plugin_data.sz_fix;
|
|
196 return (int)(sz);
|
|
197 }
|
|
198
|
|
199 // plays 'len' bytes of 'data'
|
|
200 // return: number of bytes played
|
|
201 static int play(void* data,int len,int flags){
|
|
202 int i=0;
|
|
203 /* Due to constant buffer sizes in plugins limit length */
|
|
204 int tmp = get_space();
|
|
205 int ret_len =(tmp<len)?tmp:len;
|
|
206 /* Filter data */
|
|
207 ao_plugin_data.len=ret_len;
|
|
208 ao_plugin_data.data=data;
|
|
209 while(plugin(i))
|
|
210 plugin(i++)->play();
|
|
211 /* Send data to output */
|
|
212 len=driver()->play(ao_plugin_data.data,ao_plugin_data.len,flags);
|
|
213
|
|
214 if(len!=ao_plugin_data.len)
|
|
215 printf("Buffer over flow in sound plugin ");
|
|
216
|
|
217 return ret_len;
|
|
218 }
|
|
219
|
|
220 // return: delay in seconds between first and last sample in buffer
|
|
221 static float get_delay(){
|
|
222 float delay=driver()->get_delay();
|
|
223 delay*=ao_plugin_data.delay_mult;
|
|
224 delay+=ao_plugin_data.delay_fix;
|
|
225 return delay;
|
|
226 }
|
|
227
|
|
228
|