Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Might Help with Audio Latency Issues #117

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions src/apulse-stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ deh_stream_first_readwrite_callback(pa_mainloop_api *api, pa_defer_event *de, vo
pa_stream_unref(s);
}

static int xruns=0;
static
void
data_available_for_stream(pa_mainloop_api *a, pa_io_event *ioe, int fd, pa_io_event_flags_t events,
Expand Down Expand Up @@ -167,17 +168,20 @@ data_available_for_stream(pa_mainloop_api *a, pa_io_event *ioe, int fd, pa_io_ev
}

if (events & PA_IO_EVENT_OUTPUT) {
int r;

if (paused) {
// client stream is corked. Pass silence to ALSA
size_t bytecnt = MIN(buf_size, frame_count * frame_size);
memset(buf, 0, bytecnt);
snd_pcm_writei(s->ph, buf, bytecnt / frame_size);
r= snd_pcm_writei(s->ph, buf, bytecnt / frame_size);
} else {
size_t writable_size = pa_stream_writable_size(s);

// Ask client for data, but only if we are ready for at least |minreq| bytes.
if (s->write_cb && writable_size >= s->buffer_attr.minreq)
s->write_cb(s, s->buffer_attr.minreq, s->write_cb_userdata);
if (s->write_cb && writable_size >= s->buffer_attr.minreq) {
s->write_cb(s, writable_size, s->write_cb_userdata);
}

size_t bytecnt = MIN(buf_size, frame_count * frame_size);
bytecnt = ringbuffer_read(s->rb, buf, bytecnt);
Expand All @@ -188,9 +192,29 @@ data_available_for_stream(pa_mainloop_api *a, pa_io_event *ioe, int fd, pa_io_ev
// application is not ready yet, play silence
bytecnt = MIN(buf_size, frame_count * frame_size);
memset(buf, 0, bytecnt);
trace_warning("application not ready\n");
}
snd_pcm_writei(s->ph, buf, bytecnt / frame_size);
r = snd_pcm_writei(s->ph, buf, bytecnt / frame_size);
}

if (r < 0) {
switch (r) {
case -EAGAIN: // non-blocking I/O, no need to do anything
break;
case -EBADFD: // FD in wrong state
trace_warning("ALSA badfd: rv=%d\n", r);
snd_pcm_prepare(s->ph);
break;
case -EPIPE: // XRUN
xruns++;
trace_warning("ALSA xrun: rv=%d, xruns=%d\n", r, xruns);
snd_pcm_recover(s->ph, r, 1);
break;
default:
trace_warning("ALSA error: rv=%d\n", r);
snd_pcm_recover(s->ph, r, 1);
}
}
}

if (events & PA_IO_EVENT_INPUT) {
Expand Down
15 changes: 15 additions & 0 deletions src/apulse-threaded-mainloop.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,30 @@ pa_threaded_mainloop_signal(pa_threaded_mainloop *m, int wait_for_accept)
pthread_cond_signal(&m->cond);
}

#define AUDIO_THREAD_RT_PRIORITY 60

APULSE_EXPORT
int
pa_threaded_mainloop_start(pa_threaded_mainloop *m)
{
struct sched_param rtparam;
int err;
trace_info_f("F %s m=%p\n", __func__, m);

if (m->running)
return 1;
pthread_create(&m->t, NULL, mainloop_thread, m);

memset(&rtparam, 0, sizeof(rtparam));
rtparam.sched_priority = AUDIO_THREAD_RT_PRIORITY;

if ((err = pthread_setschedparam(m->t, SCHED_FIFO, &rtparam)) != 0) {
trace_warning(
"cannot use real-time scheduling (FIFO at priority %d) "
"for audio thread (%d: %s)",
rtparam.sched_priority, err, strerror(err));
}

m->running = 1;
return 0;
}
Expand Down