Skip to content

Commit

Permalink
aout: separate time and play callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémi Denis-Courmont committed Nov 12, 2012
1 parent f492f85 commit c19df78
Show file tree
Hide file tree
Showing 18 changed files with 170 additions and 126 deletions.
13 changes: 10 additions & 3 deletions include/vlc_aout.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,14 @@ struct audio_output
/**< Stops the existing stream (optional, may be NULL).
* \note A stream must have been started when called.
*/
void (*play)(audio_output_t *, block_t *, mtime_t *);
int (*time_get)(audio_output_t *, mtime_t *write_pts);
/**< Estimates the date/time of the playback buffer write offset
* (optional, may be NULL). The read offset is not returned since it is
* always implicitly equal to the current time (mdate()).
* \param write_pts timestamp of the write offset [OUT]
* \note A stream must have been started when called.
*/
void (*play)(audio_output_t *, block_t *);
/**< Queues a block of samples for playback (mandatory, cannot be NULL).
* \note A stream must have been started when called.
*/
Expand Down Expand Up @@ -311,15 +318,15 @@ typedef struct
aout_fifo_t partial; /**< Audio blocks before packetization */
aout_fifo_t fifo; /**< Packetized audio blocks */
mtime_t pause_date; /**< Date when paused or VLC_TS_INVALID */
mtime_t time_report; /**< Desynchronization estimate or VLC_TS_INVALID */
unsigned samples; /**< Samples per packet */
bool starving; /**< Whether currently starving (to limit error messages) */
} aout_packet_t;

VLC_DEPRECATED void aout_PacketInit(audio_output_t *, aout_packet_t *, unsigned, const audio_sample_format_t *);
VLC_DEPRECATED void aout_PacketDestroy(audio_output_t *);

VLC_DEPRECATED void aout_PacketPlay(audio_output_t *, block_t *, mtime_t *);
VLC_DEPRECATED int aout_PacketTimeGet(audio_output_t *, mtime_t *);
VLC_DEPRECATED void aout_PacketPlay(audio_output_t *, block_t *);
VLC_DEPRECATED void aout_PacketPause(audio_output_t *, bool, mtime_t);
VLC_DEPRECATED void aout_PacketFlush(audio_output_t *, bool);

Expand Down
4 changes: 2 additions & 2 deletions modules/audio_output/adummy.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ vlc_module_end ()

#define A52_FRAME_NB 1536

static void Play( audio_output_t *aout, block_t *block, mtime_t *drift )
static void Play(audio_output_t *aout, block_t *block)
{
block_Release( block );
(void) aout;
(void) drift;
}

static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
Expand All @@ -68,6 +67,7 @@ static int Open(vlc_object_t *obj)
audio_output_t *aout = (audio_output_t *)obj;

aout->start = Start;
aout->time_get = NULL;
aout->play = Play;
aout->pause = NULL;
aout->flush = NULL;
Expand Down
72 changes: 46 additions & 26 deletions modules/audio_output/alsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ static int DeviceChanged (vlc_object_t *obj, const char *varname,
return VLC_SUCCESS;
}

static void Play (audio_output_t *, block_t *, mtime_t *);
static int TimeGet (audio_output_t *aout, mtime_t *);
static void Play (audio_output_t *, block_t *);
static void Pause (audio_output_t *, bool, mtime_t);
static void PauseDummy (audio_output_t *, bool, mtime_t);
static void Flush (audio_output_t *, bool);
Expand Down Expand Up @@ -508,6 +509,7 @@ static int Start (audio_output_t *aout, audio_sample_format_t *restrict fmt)
}
sys->format = *fmt;

aout->time_get = TimeGet;
aout->play = Play;
if (snd_pcm_hw_params_can_pause (hw))
aout->pause = Pause;
Expand Down Expand Up @@ -540,11 +542,33 @@ static int Start (audio_output_t *aout, audio_sample_format_t *restrict fmt)
return VLC_EGENERIC;
}

static int TimeGet (audio_output_t *aout, mtime_t *restrict pts)
{
aout_sys_t *sys = aout->sys;
snd_pcm_t *pcm = sys->pcm;
snd_pcm_status_t *status;
int val;

snd_pcm_status_alloca (&status);
val = snd_pcm_status (pcm, status);
if (val < 0)
{
msg_Err (aout, "cannot get status: %s", snd_strerror (val));
return -1;
}

if (snd_pcm_status_get_state (status) != SND_PCM_STATE_RUNNING)
return -1;

snd_pcm_sframes_t frames = snd_pcm_status_get_delay (status);
*pts = mdate () + (frames * CLOCK_FREQ / sys->format.i_rate);
return 0;
}

/**
* Queues one audio buffer to the hardware.
*/
static void Play (audio_output_t *aout, block_t *block,
mtime_t *restrict drift)
static void Play (audio_output_t *aout, block_t *block)
{
aout_sys_t *sys = aout->sys;

Expand All @@ -561,40 +585,36 @@ static void Play (audio_output_t *aout, block_t *block,
if (val < 0)
msg_Err (aout, "cannot get status: %s", snd_strerror (val));
else
if (snd_pcm_status_get_state (status) != SND_PCM_STATE_RUNNING)
{
snd_pcm_sframes_t frames = snd_pcm_status_get_delay (status);
mtime_t delay = frames * CLOCK_FREQ / sys->format.i_rate;
delay += mdate () - block->i_pts;

if (snd_pcm_status_get_state (status) != SND_PCM_STATE_RUNNING)
delay += mdate () - block->i_pts;
if (delay < 0)
{
if (delay < 0)
if (sys->format.i_format != VLC_CODEC_SPDIFL)
{
if (sys->format.i_format != VLC_CODEC_SPDIFL)
{
frames = (delay * sys->format.i_rate) / -CLOCK_FREQ;
msg_Dbg (aout, "prepending %ld zeroes", frames);

void *z = calloc (frames, sys->format.i_bytes_per_frame);
if (likely(z != NULL))
{
snd_pcm_writei (pcm, z, frames);
free (z);
delay = 0;
}
}
/* Lame fallback if zero padding does not work */
if (delay < 0)
frames = (delay * sys->format.i_rate) / -CLOCK_FREQ;
msg_Dbg (aout, "prepending %ld zeroes", frames);

void *z = calloc (frames, sys->format.i_bytes_per_frame);
if (likely(z != NULL))
{
msg_Dbg (aout, "deferring start (%"PRId64" us)", -delay);
msleep (-delay);
snd_pcm_writei (pcm, z, frames);
free (z);
delay = 0;
}
}
else
msg_Dbg (aout, "starting late (%"PRId64" us)", delay);
/* Lame fallback if zero padding does not work */
if (delay < 0)
{
msg_Dbg (aout, "deferring start (%"PRId64" us)", -delay);
msleep (-delay);
}
}
else
*drift = delay;
msg_Dbg (aout, "starting late (%"PRId64" us)", delay);
}

/* TODO: better overflow handling */
Expand Down
5 changes: 2 additions & 3 deletions modules/audio_output/amem.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,13 @@ struct aout_sys_t
bool mute;
};

static void Play (audio_output_t *aout, block_t *block,
mtime_t *restrict drift)
static void Play (audio_output_t *aout, block_t *block)
{
aout_sys_t *sys = aout->sys;

sys->play (sys->opaque, block->p_buffer, block->i_nb_samples,
block->i_pts);
block_Release (block);
(void) drift;
}

static void Pause (audio_output_t *aout, bool paused, mtime_t date)
Expand Down Expand Up @@ -251,6 +249,7 @@ static int Open (vlc_object_t *obj)

aout->start = Start;
aout->stop = Stop;
aout->time_get = NULL;
aout->play = Play;
aout->pause = Pause;
aout->flush = Flush;
Expand Down
1 change: 1 addition & 0 deletions modules/audio_output/audioqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ static int Start( audio_output_t *aout, audio_sample_format_t *restrict fmt )
fmt->i_physical_channels = AOUT_CHANS_STEREO;
fmt->i_rate = 44100;
aout_PacketInit(p_aout, &p_sys->packet, FRAME_SIZE, fmt);
p_aout->time_get = aout_PacketTimeGet;
p_aout->play = aout_PacketPlay;
p_aout->pause = aout_PacketPause;
p_aout->flush = aout_PacketFlush;
Expand Down
4 changes: 2 additions & 2 deletions modules/audio_output/audiotrack.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
}

aout->sys = p_sys;
aout->time_get = NULL;
aout->play = Play;
aout->pause = Pause;

Expand All @@ -287,9 +288,8 @@ static void Stop(audio_output_t* p_aout)
}

/* FIXME: lipsync */
static void Play(audio_output_t* p_aout, block_t* p_buffer, mtime_t* restrict drift)
static void Play(audio_output_t* p_aout, block_t* p_buffer)
{
VLC_UNUSED(drift);
aout_sys_t *p_sys = p_aout->sys;

size_t length = 0;
Expand Down
1 change: 1 addition & 0 deletions modules/audio_output/auhal.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ static int Start(audio_output_t *p_aout, audio_sample_format_t *restrict fmt)
p_sys->b_changed_mixing = false;
memset(p_sys->p_remainder_buffer, 0, sizeof(uint8_t) * BUFSIZE);

p_aout->time_get = aout_PacketTimeGet;
p_aout->play = aout_PacketPlay;
p_aout->pause = aout_PacketPause;
p_aout->flush = aout_PacketFlush;
Expand Down
8 changes: 4 additions & 4 deletions modules/audio_output/directx.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ struct aout_sys_t
static int Open( vlc_object_t * );
static void Close( vlc_object_t * );
static void Stop( audio_output_t * );
static void Play ( audio_output_t *, block_t *, mtime_t * );
static void Play ( audio_output_t *, block_t * );
static int VolumeSet ( audio_output_t *, float );
static int MuteSet ( audio_output_t *, bool );

Expand Down Expand Up @@ -282,6 +282,7 @@ static int Start( audio_output_t *p_aout, audio_sample_format_t *restrict fmt )
goto error;
}

p_aout->time_get = aout_PacketTimeGet;
p_aout->play = Play;
p_aout->pause = aout_PacketPause;
p_aout->flush = aout_PacketFlush;
Expand Down Expand Up @@ -509,8 +510,7 @@ static void Probe( audio_output_t * p_aout, const audio_sample_format_t *fmt )
* we know the first buffer has been put in the aout fifo and we also
* know its date.
*****************************************************************************/
static void Play( audio_output_t *p_aout, block_t *p_buffer,
mtime_t *restrict drift )
static void Play( audio_output_t *p_aout, block_t *p_buffer )
{
/* get the playing date of the first aout buffer */
p_aout->sys->notif.start_date = p_buffer->i_pts;
Expand All @@ -521,7 +521,7 @@ static void Play( audio_output_t *p_aout, block_t *p_buffer,
/* wake up the audio output thread */
SetEvent( p_aout->sys->notif.event );

aout_PacketPlay( p_aout, p_buffer, drift );
aout_PacketPlay( p_aout, p_buffer );
p_aout->play = aout_PacketPlay;
}

Expand Down
7 changes: 3 additions & 4 deletions modules/audio_output/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static const int pi_channels_maps[CHANNELS_MAX+1] =
* Local prototypes.
*****************************************************************************/
static int Open ( vlc_object_t * );
static void Play ( audio_output_t *, block_t *, mtime_t * );
static void Play ( audio_output_t *, block_t * );

/*****************************************************************************
* Module descriptor
Expand Down Expand Up @@ -154,6 +154,7 @@ static int Start( audio_output_t *p_aout, audio_sample_format_t *restrict fmt )
return VLC_EGENERIC;
}

p_aout->time_get = NULL;
p_aout->play = Play;
p_aout->pause = NULL;
p_aout->flush = NULL;
Expand Down Expand Up @@ -296,8 +297,7 @@ static void Stop( audio_output_t *p_aout )
/*****************************************************************************
* Play: pretend to play a sound
*****************************************************************************/
static void Play( audio_output_t * p_aout, block_t *p_buffer,
mtime_t *restrict drift )
static void Play( audio_output_t * p_aout, block_t *p_buffer )
{
if( fwrite( p_buffer->p_buffer, p_buffer->i_buffer, 1,
p_aout->sys->p_file ) != 1 )
Expand All @@ -312,7 +312,6 @@ static void Play( audio_output_t * p_aout, block_t *p_buffer,
}

block_Release( p_buffer );
(void) drift;
}

static int Open(vlc_object_t *obj)
Expand Down
1 change: 1 addition & 0 deletions modules/audio_output/jack.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ static int Start( audio_output_t *p_aout, audio_sample_format_t *restrict fmt )
// TODO add buffer size callback
fmt->i_rate = jack_get_sample_rate( p_sys->p_jack_client );

p_aout->time_get = aout_PacketTimeGet;
p_aout->play = aout_PacketPlay;
p_aout->pause = aout_PacketPause;
p_aout->flush = aout_PacketFlush;
Expand Down
8 changes: 4 additions & 4 deletions modules/audio_output/kai.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct aout_sys_t
*****************************************************************************/
static int Open ( vlc_object_t * );
static void Close ( vlc_object_t * );
static void Play ( audio_output_t *_p_aout, block_t *block, mtime_t * );
static void Play ( audio_output_t *_p_aout, block_t *block );

static ULONG APIENTRY KaiCallback ( PVOID, PVOID, ULONG );

Expand Down Expand Up @@ -195,6 +195,7 @@ static int Start ( audio_output_t *p_aout, audio_sample_format_t *fmt )

p_sys->format = *fmt = format;

p_aout->time_get = aout_PacketTimeGet;
p_aout->play = Play;
p_aout->pause = aout_PacketPause;
p_aout->flush = aout_PacketFlush;
Expand Down Expand Up @@ -244,14 +245,13 @@ exit_kai_done :
/*****************************************************************************
* Play: play a sound samples buffer
*****************************************************************************/
static void Play (audio_output_t *p_aout, block_t *block,
mtime_t *restrict drift)
static void Play (audio_output_t *p_aout, block_t *block)
{
aout_sys_t *p_sys = p_aout->sys;

kaiPlay( p_sys->hkai );

aout_PacketPlay( p_aout, block, drift );
aout_PacketPlay( p_aout, block );
}

/*****************************************************************************
Expand Down
Loading

0 comments on commit c19df78

Please sign in to comment.