2002-05-30 11:53:51 +00:00
|
|
|
#include "../config.h"
|
|
|
|
|
2001-11-24 05:24:06 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2002-05-30 11:53:51 +00:00
|
|
|
#include <string.h>
|
2001-11-24 05:24:06 +00:00
|
|
|
|
2004-09-25 22:18:41 +00:00
|
|
|
#include "mp_msg.h"
|
|
|
|
#include "help_mp.h"
|
2001-11-29 12:44:06 +00:00
|
|
|
#include "afmt.h"
|
2001-11-24 05:24:06 +00:00
|
|
|
#include "audio_out.h"
|
|
|
|
#include "audio_out_internal.h"
|
|
|
|
|
|
|
|
#include "audio_plugin.h"
|
|
|
|
|
|
|
|
static ao_info_t info =
|
|
|
|
{
|
|
|
|
"Plugin audio output",
|
|
|
|
"plugin",
|
|
|
|
"Anders",
|
|
|
|
""
|
|
|
|
};
|
|
|
|
|
|
|
|
LIBAO_EXTERN(plugin)
|
|
|
|
|
2001-11-25 14:29:54 +00:00
|
|
|
#define plugin(i) (ao_plugin_local_data.plugins[i])
|
|
|
|
#define driver() (ao_plugin_local_data.driver)
|
2001-11-24 05:24:06 +00:00
|
|
|
|
2001-11-25 14:29:54 +00:00
|
|
|
// local data
|
2001-11-24 05:24:06 +00:00
|
|
|
typedef struct ao_plugin_local_data_s
|
|
|
|
{
|
2002-01-23 12:28:36 +00:00
|
|
|
void* buf; // Output data buffer
|
|
|
|
int len; // Amount of data in buffer
|
2002-05-09 07:41:25 +00:00
|
|
|
int channels;
|
|
|
|
int format;
|
|
|
|
int bpm; //bit of format
|
2002-01-23 12:28:36 +00:00
|
|
|
float bps; // Bytes per second out
|
|
|
|
ao_functions_t* driver; // Output driver
|
2001-11-25 14:29:54 +00:00
|
|
|
ao_plugin_functions_t** plugins; // List of used plugins
|
2001-11-29 12:44:06 +00:00
|
|
|
ao_plugin_functions_t* available_plugins[NPL]; // List of available plugins
|
2001-11-24 05:24:06 +00:00
|
|
|
} ao_plugin_local_data_t;
|
|
|
|
|
2002-05-09 07:41:25 +00:00
|
|
|
static ao_plugin_local_data_t ao_plugin_local_data={NULL,0,0,0,0,0.0,NULL,NULL,AO_PLUGINS};
|
2001-11-24 05:24:06 +00:00
|
|
|
|
2002-01-23 12:28:36 +00:00
|
|
|
// global data
|
2002-02-28 21:17:12 +00:00
|
|
|
volatile ao_plugin_data_t ao_plugin_data; // Data used by the plugins
|
2002-02-25 13:31:26 +00:00
|
|
|
ao_plugin_cfg_t ao_plugin_cfg=CFG_DEFAULTS; // Set in cfg-mplayer.h
|
2001-11-24 05:24:06 +00:00
|
|
|
|
|
|
|
// to set/get/query special features/parameters
|
2003-03-21 16:42:50 +00:00
|
|
|
static int control(int cmd,void *arg){
|
2001-11-25 14:29:54 +00:00
|
|
|
switch(cmd){
|
|
|
|
case AOCONTROL_SET_PLUGIN_DRIVER:
|
2003-03-21 16:42:50 +00:00
|
|
|
ao_plugin_local_data.driver=arg;
|
2001-11-25 14:29:54 +00:00
|
|
|
return CONTROL_OK;
|
2002-02-25 13:31:26 +00:00
|
|
|
case AOCONTROL_GET_VOLUME:
|
|
|
|
case AOCONTROL_SET_VOLUME:
|
|
|
|
{
|
|
|
|
int r=audio_plugin_volume.control(cmd,arg);
|
|
|
|
if(CONTROL_OK != r)
|
|
|
|
return driver()->control(cmd,arg);
|
|
|
|
else
|
|
|
|
return r;
|
|
|
|
}
|
2001-11-25 14:29:54 +00:00
|
|
|
default:
|
|
|
|
return driver()->control(cmd,arg);
|
|
|
|
}
|
|
|
|
return CONTROL_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recursive function for adding plugins
|
|
|
|
// return 1 for success and 0 for error
|
|
|
|
int add_plugin(int i,char* cfg){
|
|
|
|
int cnt=0;
|
|
|
|
// Find end of plugin name
|
|
|
|
while((cfg[cnt]!=',')&&(cfg[cnt]!='\0')&&(cnt<100)) cnt++;
|
|
|
|
if(cnt >= 100)
|
|
|
|
return 0;
|
|
|
|
|
2001-12-23 19:09:24 +00:00
|
|
|
// Is this the last iteration or just another plugin
|
2001-11-25 14:29:54 +00:00
|
|
|
if(cfg[cnt]=='\0'){
|
2002-04-21 16:56:19 +00:00
|
|
|
ao_plugin_local_data.plugins=malloc((i+2)*sizeof(ao_plugin_functions_t*));
|
2001-11-25 14:29:54 +00:00
|
|
|
if(ao_plugin_local_data.plugins){
|
|
|
|
ao_plugin_local_data.plugins[i+1]=NULL;
|
|
|
|
// Find the plugin matching the cfg string name
|
|
|
|
cnt=0;
|
|
|
|
while(ao_plugin_local_data.available_plugins[cnt] && cnt<20){
|
|
|
|
if(0==strcmp(ao_plugin_local_data.available_plugins[cnt]->info->short_name,cfg)){
|
|
|
|
ao_plugin_local_data.plugins[i]=ao_plugin_local_data.available_plugins[cnt];
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
cnt++;
|
|
|
|
}
|
2004-09-25 22:18:41 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_PLUGIN_InvalidPlugin,cfg);
|
2001-11-25 14:29:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
cfg[cnt]='\0';
|
|
|
|
if(add_plugin(i+1,&cfg[cnt+1])){
|
|
|
|
cnt=0;
|
|
|
|
// Find the plugin matching the cfg string name
|
|
|
|
while(ao_plugin_local_data.available_plugins[cnt] && cnt < 20){
|
|
|
|
if(0==strcmp(ao_plugin_local_data.available_plugins[cnt]->info->short_name,cfg)){
|
|
|
|
ao_plugin_local_data.plugins[i]=ao_plugin_local_data.available_plugins[cnt];
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
cnt++;
|
|
|
|
}
|
2004-09-25 22:18:41 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_PLUGIN_InvalidPlugin,cfg);
|
2001-11-25 14:29:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 0; // Will never happen...
|
2001-11-24 05:24:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// open & setup audio device and plugins
|
|
|
|
// return: 1=success 0=fail
|
|
|
|
static int init(int rate,int channels,int format,int flags){
|
2002-06-07 00:30:32 +00:00
|
|
|
int use_plugin[NPL];
|
|
|
|
int x,npl,unused=0;
|
2001-11-24 05:24:06 +00:00
|
|
|
int ok=1;
|
2002-06-07 00:30:32 +00:00
|
|
|
char *config;
|
2001-11-24 05:24:06 +00:00
|
|
|
|
2002-01-23 12:28:36 +00:00
|
|
|
// Create list of plugins from cfg option
|
2001-11-24 05:24:06 +00:00
|
|
|
int i=0;
|
2001-11-29 12:44:06 +00:00
|
|
|
if(ao_plugin_cfg.plugin_list){
|
2003-01-18 01:06:05 +00:00
|
|
|
config = malloc(strlen(ao_plugin_cfg.plugin_list)+1);
|
2002-06-07 00:30:32 +00:00
|
|
|
if(!config) return 0;
|
|
|
|
if(!strcpy(config, ao_plugin_cfg.plugin_list) || !add_plugin(i,config)){
|
|
|
|
free(config);
|
2001-11-25 14:29:54 +00:00
|
|
|
return 0;
|
2002-06-07 00:30:32 +00:00
|
|
|
}
|
|
|
|
free(config);
|
2001-11-25 14:29:54 +00:00
|
|
|
}
|
2001-11-24 05:24:06 +00:00
|
|
|
|
|
|
|
/* Set input parameters and itterate through plugins each plugin
|
|
|
|
changes the parameters according to its output */
|
2002-05-09 07:41:25 +00:00
|
|
|
|
|
|
|
ao_plugin_local_data.format=format;
|
|
|
|
ao_plugin_local_data.channels=channels;
|
|
|
|
ao_plugin_local_data.bpm=audio_out_format_bits(format);
|
|
|
|
|
2001-11-24 05:24:06 +00:00
|
|
|
ao_plugin_data.rate=rate;
|
|
|
|
ao_plugin_data.channels=channels;
|
|
|
|
ao_plugin_data.format=format;
|
|
|
|
ao_plugin_data.sz_mult=1;
|
|
|
|
ao_plugin_data.sz_fix=0;
|
|
|
|
ao_plugin_data.delay_mult=1;
|
|
|
|
ao_plugin_data.delay_fix=0;
|
2002-06-07 00:30:32 +00:00
|
|
|
|
2002-09-22 02:33:28 +00:00
|
|
|
for(i=0;i<NPL && plugin(i);i++){
|
2002-06-07 00:30:32 +00:00
|
|
|
use_plugin[i]=plugin(i)->init();
|
|
|
|
if(!use_plugin[i]) plugin(i)->uninit();
|
|
|
|
}
|
|
|
|
npl=i;
|
2002-09-22 02:33:28 +00:00
|
|
|
for(i=0;i<npl && plugin(i);i++)
|
2002-06-07 00:30:32 +00:00
|
|
|
if(!use_plugin[i+unused]){
|
|
|
|
unused++;
|
2002-09-22 02:33:28 +00:00
|
|
|
for(x=i;x<npl && plugin(x+1);x++) plugin(x)=plugin(x+1);
|
2002-06-07 00:30:32 +00:00
|
|
|
plugin(x)=NULL;
|
|
|
|
npl--;
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
i=npl;
|
2001-11-24 05:24:06 +00:00
|
|
|
|
2002-01-23 12:28:36 +00:00
|
|
|
// Calculate bps
|
|
|
|
ao_plugin_local_data.bps=(float)(ao_plugin_data.rate *
|
|
|
|
ao_plugin_data.channels);
|
2002-05-09 07:41:25 +00:00
|
|
|
ao_plugin_local_data.bps*=audio_out_format_bits(ao_plugin_data.format)/8;
|
2002-01-23 12:28:36 +00:00
|
|
|
|
2001-11-25 14:29:54 +00:00
|
|
|
// This should never happen but check anyway
|
|
|
|
if(NULL==ao_plugin_local_data.driver)
|
|
|
|
return 0;
|
|
|
|
|
2001-11-24 05:24:06 +00:00
|
|
|
ok = driver()->init(ao_plugin_data.rate,
|
2001-11-25 14:29:54 +00:00
|
|
|
ao_plugin_data.channels,
|
|
|
|
ao_plugin_data.format,
|
|
|
|
flags);
|
2001-11-24 05:24:06 +00:00
|
|
|
if(!ok) return 0;
|
|
|
|
|
|
|
|
/* Now that the driver is initialized we can calculate and set the
|
|
|
|
input and output buffers for each plugin */
|
|
|
|
ao_plugin_data.len=driver()->get_space();
|
2001-11-25 14:29:54 +00:00
|
|
|
while((i>0) && ok)
|
2001-11-29 12:44:06 +00:00
|
|
|
ok=plugin(--i)->control(AOCONTROL_PLUGIN_SET_LEN,0);
|
2001-11-25 14:29:54 +00:00
|
|
|
|
|
|
|
if(!ok) return 0;
|
2001-11-24 05:24:06 +00:00
|
|
|
|
2002-01-27 10:41:43 +00:00
|
|
|
// Allocate output buffer
|
2002-01-23 12:28:36 +00:00
|
|
|
if(ao_plugin_local_data.buf)
|
|
|
|
free(ao_plugin_local_data.buf);
|
|
|
|
ao_plugin_local_data.buf=malloc(MAX_OUTBURST);
|
|
|
|
|
|
|
|
if(!ao_plugin_local_data.buf) return 0;
|
|
|
|
|
2001-11-24 05:24:06 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// close audio device
|
2004-04-06 17:55:36 +00:00
|
|
|
static void uninit(int immed){
|
2001-11-24 05:24:06 +00:00
|
|
|
int i=0;
|
2004-04-06 17:55:36 +00:00
|
|
|
driver()->uninit(immed);
|
2001-11-24 05:24:06 +00:00
|
|
|
while(plugin(i))
|
|
|
|
plugin(i++)->uninit();
|
2001-11-25 14:29:54 +00:00
|
|
|
if(ao_plugin_local_data.plugins)
|
|
|
|
free(ao_plugin_local_data.plugins);
|
2002-03-12 06:32:02 +00:00
|
|
|
ao_plugin_local_data.plugins=NULL;
|
2002-01-23 12:28:36 +00:00
|
|
|
if(ao_plugin_local_data.buf)
|
|
|
|
free(ao_plugin_local_data.buf);
|
2002-03-12 06:32:02 +00:00
|
|
|
ao_plugin_local_data.buf=NULL;
|
2001-11-24 05:24:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// stop playing and empty buffers (for seeking/pause)
|
|
|
|
static void reset(){
|
|
|
|
int i=0;
|
|
|
|
driver()->reset();
|
|
|
|
while(plugin(i))
|
|
|
|
plugin(i++)->reset();
|
2002-01-23 12:28:36 +00:00
|
|
|
ao_plugin_local_data.len=0;
|
2001-11-24 05:24:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// stop playing, keep buffers (for pause)
|
|
|
|
static void audio_pause(){
|
|
|
|
driver()->pause();
|
|
|
|
}
|
|
|
|
|
|
|
|
// resume playing, after audio_pause()
|
|
|
|
static void audio_resume(){
|
|
|
|
driver()->resume();
|
|
|
|
}
|
|
|
|
|
|
|
|
// return: how many bytes can be played without blocking
|
|
|
|
static int get_space(){
|
2002-05-09 07:41:25 +00:00
|
|
|
double sz;
|
|
|
|
int isz;
|
|
|
|
sz=(double)(driver()->get_space());
|
2002-01-23 12:28:36 +00:00
|
|
|
if(sz+(double)ao_plugin_local_data.len > (double)MAX_OUTBURST)
|
|
|
|
sz=(double)MAX_OUTBURST-(double)ao_plugin_local_data.len;
|
2001-11-24 05:24:06 +00:00
|
|
|
sz*=ao_plugin_data.sz_mult;
|
|
|
|
sz+=ao_plugin_data.sz_fix;
|
2002-05-09 07:41:25 +00:00
|
|
|
isz=(int)(sz);
|
|
|
|
isz-=isz%(ao_plugin_local_data.channels*ao_plugin_local_data.bpm/8);
|
|
|
|
return isz;
|
2001-11-24 05:24:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// plays 'len' bytes of 'data'
|
|
|
|
// return: number of bytes played
|
|
|
|
static int play(void* data,int len,int flags){
|
2002-01-23 12:28:36 +00:00
|
|
|
int l,i=0;
|
|
|
|
// Limit length to avoid over flow in plugins
|
2002-03-03 12:53:26 +00:00
|
|
|
int tmp = get_space();
|
2001-11-24 05:24:06 +00:00
|
|
|
int ret_len =(tmp<len)?tmp:len;
|
2002-06-10 12:10:52 +00:00
|
|
|
// keep all channels of each sample together
|
|
|
|
ret_len -= ret_len % (ao_plugin_local_data.channels*ao_plugin_local_data.bpm/8);
|
2002-01-27 10:59:03 +00:00
|
|
|
if(ret_len){
|
|
|
|
// Filter data
|
|
|
|
ao_plugin_data.len=ret_len;
|
|
|
|
ao_plugin_data.data=data;
|
2002-05-09 07:41:25 +00:00
|
|
|
|
2002-01-27 11:09:55 +00:00
|
|
|
while(plugin(i))
|
2002-01-27 10:59:03 +00:00
|
|
|
plugin(i++)->play();
|
|
|
|
// Copy data to output buffer
|
|
|
|
memcpy(ao_plugin_local_data.buf+ao_plugin_local_data.len,
|
|
|
|
ao_plugin_data.data,ao_plugin_data.len);
|
|
|
|
// Send data to output
|
|
|
|
l=driver()->play(ao_plugin_local_data.buf,
|
|
|
|
ao_plugin_data.len+ao_plugin_local_data.len,flags);
|
|
|
|
// Save away unsent data
|
|
|
|
ao_plugin_local_data.len=ao_plugin_data.len+ao_plugin_local_data.len-l;
|
|
|
|
memcpy(ao_plugin_local_data.buf,ao_plugin_local_data.buf+l,
|
|
|
|
ao_plugin_local_data.len);
|
|
|
|
}
|
2001-11-24 05:24:06 +00:00
|
|
|
return ret_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return: delay in seconds between first and last sample in buffer
|
|
|
|
static float get_delay(){
|
|
|
|
float delay=driver()->get_delay();
|
2002-01-23 12:28:36 +00:00
|
|
|
delay+=(float)ao_plugin_local_data.len/ao_plugin_local_data.bps;
|
2001-11-24 05:24:06 +00:00
|
|
|
delay*=ao_plugin_data.delay_mult;
|
|
|
|
delay+=ao_plugin_data.delay_fix;
|
|
|
|
return delay;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-03 14:05:02 +00:00
|
|
|
|
|
|
|
|