--- /home/edward/src/mplayer/main/libao2/ao_jack.c 2004-08-13 01:33:14.000000000 +0100 +++ libao2/ao_jack.c 2004-10-18 17:18:56.506134212 +0100 @@ -10,6 +10,7 @@ */ #include +#include #include "audio_out.h" #include "audio_out_internal.h" @@ -20,6 +21,8 @@ //#include "bio2jack.h" static int driver = 0; +long latency = 0; +long approx_bytes_in_jackd = 0; //bio2jack stuff: #define ERR_SUCCESS 0 @@ -31,9 +34,10 @@ 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_Write(int deviceID, unsigned 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 */ +int JACK_SetAllVolume(int deviceID, unsigned int volume); int JACK_SetVolumeForChannel(int deviceID, unsigned int channel, unsigned int volume); void JACK_GetVolumeForChannel(int deviceID, unsigned int channel, unsigned int *volume); // @@ -98,9 +102,20 @@ unsigned long rate; unsigned int bits_per_sample; + unsigned long jack_port_flags=JackPortIsPhysical; + unsigned int jack_port_name_count=0; + const char *jack_port_name=NULL; + mp_msg(MSGT_AO, MSGL_INFO, "AO: [Jack] Initialising library.\n"); JACK_Init(); + 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); + } + switch (format) { case AFMT_U8: case AFMT_S8: @@ -117,7 +132,9 @@ rate = rate_hz; - err = JACK_Open(&driver, bits_per_sample, &rate, channels); + err = JACK_OpenEx(&driver, bits_per_sample, &rate, channels, channels, + &jack_port_name, jack_port_name_count, jack_port_flags); + /* 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) */ @@ -125,7 +142,8 @@ mp_msg(MSGT_AO, MSGL_INFO, "AO: [Jack] Sample rate mismatch, trying to resample.\n"); - err = JACK_Open(&driver, bits_per_sample, &rate, channels); + err = JACK_OpenEx(&driver, bits_per_sample, &rate, channels, channels, + &jack_port_name, jack_port_name_count, jack_port_flags); } /* any other error */ @@ -135,11 +153,23 @@ return 0; } + 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); + ao_data.format = format; ao_data.channels = channels; ao_data.samplerate = rate; ao_data.bps = ( rate * channels * m ); + // Rather rough way to find out the rough number of bytes buffered + approx_bytes_in_jackd = JACK_GetJackBufferedBytes(driver) * 2; + 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); @@ -181,6 +211,9 @@ static void reset() { JACK_Reset(driver); + latency = JACK_GetJackLatency(driver); + // Rather rough way to find out the rough number of bytes buffered + approx_bytes_in_jackd = JACK_GetJackBufferedBytes(driver); } @@ -192,6 +225,8 @@ static float get_delay() { - return (float )JACK_GetJackLatency(driver); + float ret=0; + ret = (JACK_GetBytesStored(driver) + approx_bytes_in_jackd + latency) / (float)ao_data.bps; + return ret; }