1
mirror of https://github.com/mpv-player/mpv synced 2024-07-31 16:29:58 +02:00

ao_null: convert to new API, cleanup/rewrite

This commit is contained in:
Uoti Urpala 2011-05-04 17:03:55 +03:00
parent 3a5fd15fa2
commit 2fae42d00e

View File

@ -20,114 +20,110 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/time.h>
#include "talloc.h"
#include "config.h" #include "config.h"
#include "osdep/timer.h"
#include "libaf/af_format.h" #include "libaf/af_format.h"
#include "audio_out.h" #include "audio_out.h"
#include "audio_out_internal.h"
static const ao_info_t info = struct priv {
{ unsigned last_time;
"Null audio output", float buffered_bytes;
"null",
"Tobias Diedrich <ranma+mplayer@tdiedrich.de>",
""
}; };
LIBAO_EXTERN(null) static void drain(struct ao *ao)
{
struct priv *priv = ao->priv;
struct timeval last_tv; unsigned now = GetTimer();
int buffer; priv->buffered_bytes -= (now - priv->last_time) / 1e6 * ao->bps;
if (priv->buffered_bytes < 0)
static void drain(void){ priv->buffered_bytes = 0;
priv->last_time = now;
struct timeval now_tv;
int temp, temp2;
gettimeofday(&now_tv, 0);
temp = now_tv.tv_sec - last_tv.tv_sec;
temp *= ao_data.bps;
temp2 = now_tv.tv_usec - last_tv.tv_usec;
temp2 /= 1000;
temp2 *= ao_data.bps;
temp2 /= 1000;
temp += temp2;
buffer-=temp;
if (buffer<0) buffer=0;
if(temp>0) last_tv = now_tv;//mplayer is fast
} }
// to set/get/query special features/parameters static int init(struct ao *ao, char *params)
static int control(int cmd,void *arg){ {
return -1; struct priv *priv = talloc_zero(ao, struct priv);
} ao->priv = priv;
int samplesize = af_fmt2bits(ao->format) / 8;
// open & setup audio device ao->outburst = 256 * ao->channels * samplesize;
// return: 1=success 0=fail
static int init(int rate,int channels,int format,int flags){
int samplesize = af_fmt2bits(format) / 8;
ao_data.outburst = 256 * channels * samplesize;
// A "buffer" for about 0.2 seconds of audio // A "buffer" for about 0.2 seconds of audio
ao_data.buffersize = (int)(rate * 0.2 / 256 + 1) * ao_data.outburst; ao->buffersize = (int)(ao->samplerate * 0.2 / 256 + 1) * ao->outburst;
ao_data.channels=channels; ao->bps = ao->channels * ao->samplerate * samplesize;
ao_data.samplerate=rate; priv->last_time = GetTimer();
ao_data.format=format;
ao_data.bps=channels*rate*samplesize;
buffer=0;
gettimeofday(&last_tv, 0);
return 1; return 0;
} }
// close audio device // close audio device
static void uninit(int immed){ static void uninit(struct ao *ao, bool cut_audio)
{
} }
// stop playing and empty buffers (for seeking/pause) // stop playing and empty buffers (for seeking/pause)
static void reset(void){ static void reset(struct ao *ao)
buffer=0; {
struct priv *priv = ao->priv;
priv->buffered_bytes = 0;
} }
// stop playing, keep buffers (for pause) // stop playing, keep buffers (for pause)
static void audio_pause(void) static void pause(struct ao *ao)
{ {
// for now, just call reset(); // for now, just call reset();
reset(); reset(ao);
} }
// resume playing, after audio_pause() // resume playing, after audio_pause()
static void audio_resume(void) static void resume(struct ao *ao)
{ {
} }
// return: how many bytes can be played without blocking static int get_space(struct ao *ao)
static int get_space(void){ {
struct priv *priv = ao->priv;
drain(); drain(ao);
return ao_data.buffersize - buffer; return ao->buffersize - priv->buffered_bytes;
} }
// plays 'len' bytes of 'data' static int play(struct ao *ao, void *data, int len, int flags)
// it should round it down to outburst*n {
// return: number of bytes played struct priv *priv = ao->priv;
static int play(void* data,int len,int flags){
int maxbursts = (ao_data.buffersize - buffer) / ao_data.outburst; int maxbursts = (ao->buffersize - priv->buffered_bytes) / ao->outburst;
int playbursts = len / ao_data.outburst; int playbursts = len / ao->outburst;
int bursts = playbursts > maxbursts ? maxbursts : playbursts; int bursts = playbursts > maxbursts ? maxbursts : playbursts;
buffer += bursts * ao_data.outburst; priv->buffered_bytes += bursts * ao->outburst;
return bursts * ao_data.outburst; return bursts * ao->outburst;
} }
// return: delay in seconds between first and last sample in buffer static float get_delay(struct ao *ao)
static float get_delay(void){ {
struct priv *priv = ao->priv;
drain(); drain(ao);
return (float) buffer / (float) ao_data.bps; return priv->buffered_bytes / ao->bps;
} }
const struct ao_driver audio_out_null = {
.is_new = true,
.info = &(const struct ao_info) {
"Null audio output",
"null",
"Tobias Diedrich <ranma+mplayer@tdiedrich.de>",
"",
},
.init = init,
.uninit = uninit,
.reset = reset,
.get_space = get_space,
.play = play,
.get_delay = get_delay,
.pause = pause,
.resume = resume,
};