2004-06-25 18:11:15 +00:00
|
|
|
/*
|
|
|
|
* ao_jack - JACK audio output driver for MPlayer
|
|
|
|
*
|
|
|
|
* Kamil Strzelecki < esack at browarek.net >
|
|
|
|
*
|
|
|
|
* This driver is distribuited under terms of GPL
|
|
|
|
*
|
|
|
|
* It uses bio2jack (http://bio2jack.sf.net/).
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2004-11-07 11:03:50 +00:00
|
|
|
#include <jack/jack.h>
|
2004-06-25 18:11:15 +00:00
|
|
|
|
2005-01-12 22:00:02 +00:00
|
|
|
#include "config.h"
|
2004-06-25 18:11:15 +00:00
|
|
|
#include "audio_out.h"
|
|
|
|
#include "audio_out_internal.h"
|
2004-12-27 17:30:15 +00:00
|
|
|
#include "libaf/af_format.h"
|
2004-12-07 02:24:15 +00:00
|
|
|
#include "mp_msg.h"
|
2004-06-25 18:11:15 +00:00
|
|
|
|
|
|
|
//#include "bio2jack.h"
|
|
|
|
|
|
|
|
static int driver = 0;
|
2004-11-07 11:03:50 +00:00
|
|
|
long latency = 0;
|
|
|
|
long approx_bytes_in_jackd = 0;
|
2004-06-25 18:11:15 +00:00
|
|
|
|
|
|
|
//bio2jack stuff:
|
|
|
|
#define ERR_SUCCESS 0
|
|
|
|
#define ERR_OPENING_JACK 1
|
|
|
|
#define ERR_RATE_MISMATCH 2
|
|
|
|
#define ERR_BYTES_PER_FRAME_INVALID 3
|
|
|
|
enum status_enum { PLAYING, PAUSED, STOPPED, CLOSED, RESET };
|
|
|
|
void JACK_Init(void);
|
|
|
|
int JACK_Open(int* deviceID, unsigned int bits_per_sample, unsigned long *rate, int channels);
|
|
|
|
int JACK_Close(int deviceID); /* return 0 for success */
|
|
|
|
void JACK_Reset(int deviceID); /* free all buffered data and reset several values in the device */
|
|
|
|
long JACK_Write(int deviceID, char *data, unsigned long bytes); /* returns the number of bytes written */
|
|
|
|
long JACK_GetJackLatency(int deviceID); /* return the latency in milliseconds of jack */
|
|
|
|
int JACK_SetState(int deviceID, enum status_enum state); /* playing, paused, stopped */
|
2004-11-07 11:03:50 +00:00
|
|
|
int JACK_SetAllVolume(int deviceID, unsigned int volume);
|
2004-08-13 00:33:14 +00:00
|
|
|
int JACK_SetVolumeForChannel(int deviceID, unsigned int channel, unsigned int volume);
|
|
|
|
void JACK_GetVolumeForChannel(int deviceID, unsigned int channel, unsigned int *volume);
|
2004-06-25 18:11:15 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
static ao_info_t info =
|
|
|
|
{
|
|
|
|
"JACK audio output",
|
|
|
|
"jack",
|
|
|
|
"Kamil Strzelecki <esack@browarek.net>",
|
|
|
|
""
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
LIBAO_EXTERN(jack)
|
|
|
|
|
|
|
|
|
|
|
|
static int control(int cmd, void *arg)
|
|
|
|
{
|
|
|
|
switch(cmd) {
|
|
|
|
case AOCONTROL_GET_VOLUME:
|
|
|
|
{
|
|
|
|
ao_control_vol_t *vol = (ao_control_vol_t *)arg;
|
2004-08-13 00:33:14 +00:00
|
|
|
unsigned int l, r;
|
2004-06-25 18:11:15 +00:00
|
|
|
|
2004-08-13 00:33:14 +00:00
|
|
|
JACK_GetVolumeForChannel(driver, 0, &l);
|
|
|
|
JACK_GetVolumeForChannel(driver, 1, &r);
|
2004-06-27 13:18:06 +00:00
|
|
|
vol->left = (float )l;
|
|
|
|
vol->right = (float )r;
|
2004-06-25 18:11:15 +00:00
|
|
|
|
|
|
|
return CONTROL_OK;
|
|
|
|
}
|
|
|
|
case AOCONTROL_SET_VOLUME:
|
|
|
|
{
|
|
|
|
ao_control_vol_t *vol = (ao_control_vol_t *)arg;
|
2004-08-13 00:33:14 +00:00
|
|
|
unsigned int l = (int )vol->left,
|
2004-06-25 18:11:15 +00:00
|
|
|
r = (int )vol->right,
|
2004-11-07 11:03:50 +00:00
|
|
|
avg_vol = (l + r) / 2,
|
2004-06-25 18:11:15 +00:00
|
|
|
err = 0;
|
|
|
|
|
2004-11-07 11:03:50 +00:00
|
|
|
switch (ao_data.channels) {
|
|
|
|
case 6:
|
|
|
|
if((err = JACK_SetVolumeForChannel(driver, 5, avg_vol))) {
|
2004-06-25 18:11:15 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_ERR,
|
2004-11-07 11:03:50 +00:00
|
|
|
"AO: [Jack] Setting lfe volume failed, error %d\n",err);
|
|
|
|
return CONTROL_ERROR;
|
|
|
|
}
|
|
|
|
case 5:
|
|
|
|
if((err = JACK_SetVolumeForChannel(driver, 4, avg_vol))) {
|
|
|
|
mp_msg(MSGT_AO, MSGL_ERR,
|
|
|
|
"AO: [Jack] Setting center volume failed, error %d\n",err);
|
|
|
|
return CONTROL_ERROR;
|
|
|
|
}
|
|
|
|
case 4:
|
|
|
|
if((err = JACK_SetVolumeForChannel(driver, 3, r))) {
|
|
|
|
mp_msg(MSGT_AO, MSGL_ERR,
|
|
|
|
"AO: [Jack] Setting rear right volume failed, error %d\n",err);
|
2004-06-25 18:11:15 +00:00
|
|
|
return CONTROL_ERROR;
|
|
|
|
}
|
2004-11-07 11:03:50 +00:00
|
|
|
case 3:
|
|
|
|
if((err = JACK_SetVolumeForChannel(driver, 2, l))) {
|
|
|
|
mp_msg(MSGT_AO, MSGL_ERR,
|
|
|
|
"AO: [Jack] Setting rear left volume failed, error %d\n",err);
|
|
|
|
return CONTROL_ERROR;
|
|
|
|
}
|
|
|
|
case 2:
|
2004-08-13 00:33:14 +00:00
|
|
|
if((err = JACK_SetVolumeForChannel(driver, 1, r))) {
|
|
|
|
mp_msg(MSGT_AO, MSGL_ERR,
|
|
|
|
"AO: [Jack] Setting right volume failed, error %d\n",err);
|
|
|
|
return CONTROL_ERROR;
|
|
|
|
}
|
2004-11-07 11:03:50 +00:00
|
|
|
case 1:
|
|
|
|
if((err = JACK_SetVolumeForChannel(driver, 0, l))) {
|
|
|
|
mp_msg(MSGT_AO, MSGL_ERR,
|
|
|
|
"AO: [Jack] Setting left volume failed, error %d\n",err);
|
|
|
|
return CONTROL_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2004-08-13 00:33:14 +00:00
|
|
|
|
2004-06-25 18:11:15 +00:00
|
|
|
return CONTROL_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return(CONTROL_UNKNOWN);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int init(int rate_hz, int channels, int format, int flags)
|
|
|
|
{
|
|
|
|
int err, m, frag_spec;
|
|
|
|
unsigned long rate;
|
|
|
|
unsigned int bits_per_sample;
|
|
|
|
|
2004-11-07 11:03:50 +00:00
|
|
|
unsigned long jack_port_flags=JackPortIsPhysical;
|
|
|
|
unsigned int jack_port_name_count=0;
|
|
|
|
const char *jack_port_name=NULL;
|
|
|
|
|
2004-06-25 18:11:15 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_INFO, "AO: [Jack] Initialising library.\n");
|
|
|
|
JACK_Init();
|
|
|
|
|
2004-11-07 11:03:50 +00:00
|
|
|
if (ao_subdevice) {
|
|
|
|
jack_port_flags = 0;
|
|
|
|
jack_port_name_count = 1;
|
|
|
|
jack_port_name = ao_subdevice;
|
|
|
|
mp_msg(MSGT_AO, MSGL_INFO, "AO: [Jack] Trying to use jack device:%s.\n", ao_subdevice);
|
|
|
|
}
|
|
|
|
|
2004-06-25 18:11:15 +00:00
|
|
|
switch (format) {
|
2004-12-27 17:30:15 +00:00
|
|
|
case AF_FORMAT_U8:
|
|
|
|
case AF_FORMAT_S8:
|
|
|
|
format = AF_FORMAT_U8;
|
2004-06-25 18:11:15 +00:00
|
|
|
bits_per_sample = 8;
|
|
|
|
m = 1;
|
|
|
|
break;
|
|
|
|
default:
|
2005-03-25 15:11:13 +00:00
|
|
|
format = AF_FORMAT_S16_NE;
|
2004-06-25 18:11:15 +00:00
|
|
|
bits_per_sample = 16;
|
|
|
|
m = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rate = rate_hz;
|
|
|
|
|
2004-11-07 11:03:50 +00:00
|
|
|
err = JACK_OpenEx(&driver, bits_per_sample, &rate, channels, channels,
|
|
|
|
&jack_port_name, jack_port_name_count, jack_port_flags);
|
2004-06-25 18:11:15 +00:00
|
|
|
|
|
|
|
/* if sample rates doesn't match try to open device with jack's rate and
|
|
|
|
* let mplayer convert it (rate now contains that which jackd use) */
|
|
|
|
if(err == ERR_RATE_MISMATCH) {
|
|
|
|
mp_msg(MSGT_AO, MSGL_INFO,
|
|
|
|
"AO: [Jack] Sample rate mismatch, trying to resample.\n");
|
|
|
|
|
2004-11-07 11:03:50 +00:00
|
|
|
err = JACK_OpenEx(&driver, bits_per_sample, &rate, channels, channels,
|
|
|
|
&jack_port_name, jack_port_name_count, jack_port_flags);
|
2004-06-25 18:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* any other error */
|
|
|
|
if(err != ERR_SUCCESS) {
|
|
|
|
mp_msg(MSGT_AO, MSGL_ERR,
|
|
|
|
"AO: [Jack] JACK_Open() failed, error %d\n", err);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-07 11:03:50 +00:00
|
|
|
err = JACK_SetAllVolume(driver, 100);
|
|
|
|
if(err != ERR_SUCCESS) {
|
|
|
|
// This is not fatal, but would be peculiar...
|
|
|
|
mp_msg(MSGT_AO, MSGL_ERR,
|
|
|
|
"AO: [Jack] JACK_SetAllVolume() failed, error %d\n", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
latency = JACK_GetJackLatency(driver);
|
|
|
|
|
2004-06-25 18:11:15 +00:00
|
|
|
ao_data.format = format;
|
|
|
|
ao_data.channels = channels;
|
|
|
|
ao_data.samplerate = rate;
|
|
|
|
ao_data.bps = ( rate * channels * m );
|
|
|
|
|
2004-11-07 11:03:50 +00:00
|
|
|
// Rather rough way to find out the rough number of bytes buffered
|
|
|
|
approx_bytes_in_jackd = JACK_GetJackBufferedBytes(driver) * 2;
|
|
|
|
|
2004-06-25 18:11:15 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_INFO,
|
|
|
|
"AO: [Jack] OK. I'm ready to go (%d Hz/%d channels/%d bit)\n",
|
|
|
|
ao_data.samplerate, ao_data.channels, bits_per_sample);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void uninit(int immed)
|
|
|
|
{
|
|
|
|
int errval = 0;
|
|
|
|
|
|
|
|
if((errval = JACK_Close(driver)))
|
|
|
|
mp_msg(MSGT_AO, MSGL_ERR,
|
|
|
|
"AO: [Jack] error closing device, error %d\n", errval);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int play(void* data,int len,int flags)
|
|
|
|
{
|
|
|
|
return JACK_Write(driver, data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void audio_pause()
|
|
|
|
{
|
|
|
|
JACK_SetState(driver, PAUSED);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void audio_resume()
|
|
|
|
{
|
|
|
|
JACK_SetState(driver, PLAYING);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void reset()
|
|
|
|
{
|
|
|
|
JACK_Reset(driver);
|
2004-11-07 11:03:50 +00:00
|
|
|
latency = JACK_GetJackLatency(driver);
|
|
|
|
// Rather rough way to find out the rough number of bytes buffered
|
|
|
|
approx_bytes_in_jackd = JACK_GetJackBufferedBytes(driver) * 2;
|
2004-06-25 18:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int get_space()
|
|
|
|
{
|
|
|
|
return JACK_GetBytesFreeSpace(driver);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static float get_delay()
|
|
|
|
{
|
2004-11-07 11:03:50 +00:00
|
|
|
float ret = 0;
|
|
|
|
ret = (JACK_GetBytesStored(driver) + approx_bytes_in_jackd + latency) / (float)ao_data.bps;
|
|
|
|
return ret;
|
2004-06-25 18:11:15 +00:00
|
|
|
}
|
|
|
|
|