RetroArch/audio/pulse.c

314 lines
8.0 KiB
C
Raw Normal View History

2012-04-21 21:13:50 +00:00
/* RetroArch - A frontend for libretro.
2014-01-01 00:50:59 +00:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2011-01-29 00:15:09 +00:00
*
2012-04-21 21:13:50 +00:00
* RetroArch is free software: you can redistribute it and/or modify it under the terms
2011-01-29 00:15:09 +00:00
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
2012-04-21 21:13:50 +00:00
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
2011-01-29 00:15:09 +00:00
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
2012-04-21 21:31:57 +00:00
* You should have received a copy of the GNU General Public License along with RetroArch.
2011-01-29 00:15:09 +00:00
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "driver.h"
#include "general.h"
#include <pulse/pulseaudio.h>
2011-12-24 12:46:12 +00:00
#include "../boolean.h"
2011-01-29 00:15:09 +00:00
#include <string.h>
#include <stdio.h>
typedef struct
{
pa_threaded_mainloop *mainloop;
2011-01-29 00:15:09 +00:00
pa_context *context;
pa_stream *stream;
2012-02-20 17:39:50 +00:00
size_t buffer_size;
bool nonblock;
bool success;
2011-01-29 00:15:09 +00:00
} pa_t;
static void pulse_free(void *data)
2011-01-29 00:15:09 +00:00
{
2011-12-24 12:46:12 +00:00
pa_t *pa = (pa_t*)data;
2011-01-29 00:15:09 +00:00
if (pa)
{
if (pa->mainloop)
pa_threaded_mainloop_stop(pa->mainloop);
2011-01-29 00:15:09 +00:00
if (pa->stream)
{
pa_stream_disconnect(pa->stream);
pa_stream_unref(pa->stream);
}
if (pa->context)
{
pa_context_disconnect(pa->context);
pa_context_unref(pa->context);
}
if (pa->mainloop)
pa_threaded_mainloop_free(pa->mainloop);
2011-01-29 00:15:09 +00:00
free(pa);
}
}
static void stream_success_cb(pa_stream *s, int success, void *data)
{
(void)s;
pa_t *pa = (pa_t*)data;
pa->success = success;
pa_threaded_mainloop_signal(pa->mainloop, 0);
}
static void context_state_cb(pa_context *c, void *data)
{
2011-12-24 12:46:12 +00:00
pa_t *pa = (pa_t*)data;
switch (pa_context_get_state(c))
{
case PA_CONTEXT_READY:
case PA_CONTEXT_TERMINATED:
case PA_CONTEXT_FAILED:
pa_threaded_mainloop_signal(pa->mainloop, 0);
break;
default:
break;
}
}
static void stream_state_cb(pa_stream *s, void *data)
{
2011-12-24 12:46:12 +00:00
pa_t *pa = (pa_t*)data;
2012-04-18 18:19:58 +00:00
switch (pa_stream_get_state(s))
{
case PA_STREAM_READY:
case PA_STREAM_FAILED:
case PA_STREAM_TERMINATED:
pa_threaded_mainloop_signal(pa->mainloop, 0);
break;
default:
break;
}
}
static void stream_request_cb(pa_stream *s, size_t length, void *data)
{
(void)length;
(void)s;
2011-12-24 12:46:12 +00:00
pa_t *pa = (pa_t*)data;
pa_threaded_mainloop_signal(pa->mainloop, 0);
}
static void stream_latency_update_cb(pa_stream *s, void *data)
{
(void)s;
2011-12-24 12:46:12 +00:00
pa_t *pa = (pa_t*)data;
pa_threaded_mainloop_signal(pa->mainloop, 0);
}
2013-05-26 22:56:13 +00:00
static void underrun_update_cb(pa_stream *s, void *data)
{
(void)s;
2013-06-02 21:10:03 +00:00
pa_t *pa = (pa_t*)data;
RARCH_LOG("[PulseAudio]: Underrun (Buffer: %u, Writable size: %u).\n",
(unsigned)pa->buffer_size,
(unsigned)pa_stream_writable_size(pa->stream));
}
static void buffer_attr_cb(pa_stream *s, void *data)
{
pa_t *pa = (pa_t*)data;
const pa_buffer_attr *server_attr = pa_stream_get_buffer_attr(s);
if (server_attr)
pa->buffer_size = server_attr->tlength;
2013-06-02 21:57:43 +00:00
RARCH_LOG("[PulseAudio]: Got new buffer size %u.\n", (unsigned)pa->buffer_size);
2013-05-26 22:56:13 +00:00
}
static void *pulse_init(const char *device, unsigned rate, unsigned latency)
2011-01-29 00:15:09 +00:00
{
2013-06-05 08:47:19 +00:00
const pa_buffer_attr *server_attr = NULL;
2011-12-24 12:46:12 +00:00
pa_sample_spec spec;
memset(&spec, 0, sizeof(spec));
pa_buffer_attr buffer_attr = {0};
pa_t *pa = (pa_t*)calloc(1, sizeof(*pa));
2011-01-29 00:15:09 +00:00
if (!pa)
goto error;
pa->mainloop = pa_threaded_mainloop_new();
2011-01-29 00:15:09 +00:00
if (!pa->mainloop)
goto error;
2012-09-07 19:56:26 +00:00
pa->context = pa_context_new(pa_threaded_mainloop_get_api(pa->mainloop), "RetroArch");
2011-01-29 00:15:09 +00:00
if (!pa->context)
goto error;
pa_context_set_state_callback(pa->context, context_state_cb, pa);
2011-01-29 00:15:09 +00:00
if (pa_context_connect(pa->context, device, PA_CONTEXT_NOFLAGS, NULL) < 0)
goto error;
pa_threaded_mainloop_lock(pa->mainloop);
if (pa_threaded_mainloop_start(pa->mainloop) < 0)
goto error;
pa_threaded_mainloop_wait(pa->mainloop);
if (pa_context_get_state(pa->context) != PA_CONTEXT_READY)
goto unlock_error;
2011-01-29 00:15:09 +00:00
2011-12-24 12:46:12 +00:00
spec.format = is_little_endian() ? PA_SAMPLE_FLOAT32LE : PA_SAMPLE_FLOAT32BE;
spec.channels = 2;
spec.rate = rate;
2011-01-29 00:15:09 +00:00
pa->stream = pa_stream_new(pa->context, "audio", &spec, NULL);
if (!pa->stream)
goto unlock_error;
pa_stream_set_state_callback(pa->stream, stream_state_cb, pa);
pa_stream_set_write_callback(pa->stream, stream_request_cb, pa);
pa_stream_set_latency_update_callback(pa->stream, stream_latency_update_cb, pa);
2013-05-26 22:56:13 +00:00
pa_stream_set_underflow_callback(pa->stream, underrun_update_cb, pa);
pa_stream_set_buffer_attr_callback(pa->stream, buffer_attr_cb, pa);
2011-01-29 00:15:09 +00:00
2011-12-24 12:46:12 +00:00
buffer_attr.maxlength = -1;
buffer_attr.tlength = pa_usec_to_bytes(latency * PA_USEC_PER_MSEC, &spec);
buffer_attr.prebuf = -1;
buffer_attr.minreq = -1;
buffer_attr.fragsize = -1;
2011-01-29 00:15:09 +00:00
if (pa_stream_connect_playback(pa->stream, NULL, &buffer_attr, PA_STREAM_ADJUST_LATENCY, NULL, NULL) < 0)
goto error;
pa_threaded_mainloop_wait(pa->mainloop);
if (pa_stream_get_state(pa->stream) != PA_STREAM_READY)
goto unlock_error;
2013-06-05 08:47:19 +00:00
server_attr = pa_stream_get_buffer_attr(pa->stream);
2013-05-26 22:56:13 +00:00
if (server_attr)
{
pa->buffer_size = server_attr->tlength;
RARCH_LOG("[PulseAudio]: Requested %u bytes buffer, got %u.\n",
(unsigned)buffer_attr.tlength,
(unsigned)pa->buffer_size);
}
else
pa->buffer_size = buffer_attr.tlength;
pa_threaded_mainloop_unlock(pa->mainloop);
2011-01-29 00:15:09 +00:00
return pa;
unlock_error:
pa_threaded_mainloop_unlock(pa->mainloop);
2011-01-29 00:15:09 +00:00
error:
pulse_free(pa);
2011-01-29 00:15:09 +00:00
return NULL;
}
static ssize_t pulse_write(void *data, const void *buf_, size_t size)
2011-01-29 00:15:09 +00:00
{
2011-12-24 12:46:12 +00:00
pa_t *pa = (pa_t*)data;
const uint8_t *buf = (const uint8_t*)buf_;
2011-01-29 00:15:09 +00:00
size_t written = 0;
2012-02-20 17:39:50 +00:00
pa_threaded_mainloop_lock(pa->mainloop);
while (size)
2011-01-29 00:15:09 +00:00
{
size_t writable = pa_stream_writable_size(pa->stream);
writable = min(size, writable);
2011-01-29 00:15:09 +00:00
if (writable)
{
pa_stream_write(pa->stream, buf, writable, NULL, 0, PA_SEEK_RELATIVE);
buf += writable;
size -= writable;
written += writable;
}
else if (!pa->nonblock)
pa_threaded_mainloop_wait(pa->mainloop);
else
break;
}
2011-01-29 00:15:09 +00:00
pa_threaded_mainloop_unlock(pa->mainloop);
return written;
2011-01-29 00:15:09 +00:00
}
static bool pulse_stop(void *data)
2011-01-29 00:15:09 +00:00
{
RARCH_LOG("[PulseAudio]: Pausing.\n");
pa_t *pa = (pa_t*)data;
pa->success = true; // In case of spurious wakeup. Not critical.
pa_threaded_mainloop_lock(pa->mainloop);
pa_stream_cork(pa->stream, true, stream_success_cb, pa);
pa_threaded_mainloop_wait(pa->mainloop);
bool ret = pa->success;
pa_threaded_mainloop_unlock(pa->mainloop);
return ret;
2011-01-29 00:15:09 +00:00
}
static bool pulse_start(void *data)
2011-01-29 00:15:09 +00:00
{
RARCH_LOG("[PulseAudio]: Unpausing.\n");
pa_t *pa = (pa_t*)data;
pa->success = true; // In case of spurious wakeup. Not critical.
pa_threaded_mainloop_lock(pa->mainloop);
pa_stream_cork(pa->stream, false, stream_success_cb, pa);
pa_threaded_mainloop_wait(pa->mainloop);
bool ret = pa->success;
pa_threaded_mainloop_unlock(pa->mainloop);
return ret;
2011-01-29 00:15:09 +00:00
}
static void pulse_set_nonblock_state(void *data, bool state)
2011-01-29 00:15:09 +00:00
{
2011-12-24 12:46:12 +00:00
pa_t *pa = (pa_t*)data;
2011-01-29 00:15:09 +00:00
pa->nonblock = state;
}
static bool pulse_use_float(void *data)
2011-01-29 00:15:09 +00:00
{
(void)data;
return true;
}
2012-02-20 17:39:50 +00:00
static size_t pulse_write_avail(void *data)
{
pa_t *pa = (pa_t*)data;
pa_threaded_mainloop_lock(pa->mainloop);
size_t length = pa_stream_writable_size(pa->stream);
g_extern.audio_data.driver_buffer_size = pa->buffer_size; // Can change spuriously.
2012-02-20 17:39:50 +00:00
pa_threaded_mainloop_unlock(pa->mainloop);
return length;
}
static size_t pulse_buffer_size(void *data)
{
pa_t *pa = (pa_t*)data;
return pa->buffer_size;
}
2011-01-29 00:15:09 +00:00
const audio_driver_t audio_pulse = {
2011-12-24 12:46:12 +00:00
pulse_init,
pulse_write,
pulse_stop,
pulse_start,
pulse_set_nonblock_state,
pulse_free,
pulse_use_float,
2012-02-20 17:39:50 +00:00
"pulse",
pulse_write_avail,
pulse_buffer_size,
2011-01-29 00:15:09 +00:00
};