2007-10-28 19:13:50 +00:00
|
|
|
|
2004-07-16 19:56:36 +00:00
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-02-13 15:35:19 +00:00
|
|
|
Copyright 2004-2006 Lennart Poettering
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2004-11-14 14:58:54 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
2009-03-03 20:23:02 +00:00
|
|
|
by the Free Software Foundation; either version 2.1 of the License,
|
2004-07-16 19:56:36 +00:00
|
|
|
or (at your option) any later version.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2004-07-16 19:56:36 +00:00
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
General Public License for more details.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-11-14 14:58:54 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2006-06-19 21:53:48 +00:00
|
|
|
along with PulseAudio; if not, write to the Free Software
|
2004-07-16 19:56:36 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
2004-07-16 19:16:42 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-07-10 19:04:21 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
2004-07-06 00:08:44 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulse/pulseaudio.h>
|
|
|
|
|
#include <pulse/thread-mainloop.h>
|
|
|
|
|
#include <pulse/xmalloc.h>
|
2006-02-17 12:10:58 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/native-common.h>
|
|
|
|
|
#include <pulsecore/log.h>
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <pulsecore/macro.h>
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2006-02-17 12:10:58 +00:00
|
|
|
#include "simple.h"
|
|
|
|
|
|
2004-06-23 23:17:30 +00:00
|
|
|
struct pa_simple {
|
2006-05-06 20:58:28 +00:00
|
|
|
pa_threaded_mainloop *mainloop;
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_context *context;
|
|
|
|
|
pa_stream *stream;
|
2006-01-27 16:25:31 +00:00
|
|
|
pa_stream_direction_t direction;
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
const void *read_data;
|
2004-07-10 19:04:21 +00:00
|
|
|
size_t read_index, read_length;
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
int operation_success;
|
|
|
|
|
};
|
2004-08-14 20:25:32 +00:00
|
|
|
|
2009-05-27 23:18:55 +02:00
|
|
|
#define CHECK_VALIDITY_RETURN_ANY(rerror, expression, error, ret) \
|
|
|
|
|
do { \
|
|
|
|
|
if (!(expression)) { \
|
|
|
|
|
if (rerror) \
|
|
|
|
|
*(rerror) = error; \
|
|
|
|
|
return (ret); \
|
|
|
|
|
} \
|
|
|
|
|
} while(FALSE);
|
|
|
|
|
|
|
|
|
|
#define CHECK_SUCCESS_GOTO(p, rerror, expression, label) \
|
|
|
|
|
do { \
|
|
|
|
|
if (!(expression)) { \
|
|
|
|
|
if (rerror) \
|
|
|
|
|
*(rerror) = pa_context_errno((p)->context); \
|
|
|
|
|
goto label; \
|
|
|
|
|
} \
|
|
|
|
|
} while(FALSE);
|
|
|
|
|
|
|
|
|
|
#define CHECK_DEAD_GOTO(p, rerror, label) \
|
|
|
|
|
do { \
|
2009-08-05 01:03:49 +02:00
|
|
|
if (!(p)->context || !PA_CONTEXT_IS_GOOD(pa_context_get_state((p)->context)) || \
|
|
|
|
|
!(p)->stream || !PA_STREAM_IS_GOOD(pa_stream_get_state((p)->stream))) { \
|
2009-05-27 23:18:55 +02:00
|
|
|
if (((p)->context && pa_context_get_state((p)->context) == PA_CONTEXT_FAILED) || \
|
|
|
|
|
((p)->stream && pa_stream_get_state((p)->stream) == PA_STREAM_FAILED)) { \
|
|
|
|
|
if (rerror) \
|
|
|
|
|
*(rerror) = pa_context_errno((p)->context); \
|
|
|
|
|
} else \
|
|
|
|
|
if (rerror) \
|
|
|
|
|
*(rerror) = PA_ERR_BADSTATE; \
|
|
|
|
|
goto label; \
|
|
|
|
|
} \
|
|
|
|
|
} while(FALSE);
|
2004-07-09 23:26:10 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
static void context_state_cb(pa_context *c, void *userdata) {
|
|
|
|
|
pa_simple *p = userdata;
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(c);
|
|
|
|
|
pa_assert(p);
|
2004-07-07 00:22:46 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
switch (pa_context_get_state(c)) {
|
|
|
|
|
case PA_CONTEXT_READY:
|
|
|
|
|
case PA_CONTEXT_TERMINATED:
|
|
|
|
|
case PA_CONTEXT_FAILED:
|
|
|
|
|
pa_threaded_mainloop_signal(p->mainloop, 0);
|
|
|
|
|
break;
|
2004-07-09 23:26:10 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
case PA_CONTEXT_UNCONNECTED:
|
|
|
|
|
case PA_CONTEXT_CONNECTING:
|
|
|
|
|
case PA_CONTEXT_AUTHORIZING:
|
|
|
|
|
case PA_CONTEXT_SETTING_NAME:
|
|
|
|
|
break;
|
2006-04-12 22:45:57 +00:00
|
|
|
}
|
2006-05-06 20:58:28 +00:00
|
|
|
}
|
2004-07-07 00:22:46 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
static void stream_state_cb(pa_stream *s, void * userdata) {
|
|
|
|
|
pa_simple *p = userdata;
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(s);
|
|
|
|
|
pa_assert(p);
|
2004-09-19 00:03:12 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
switch (pa_stream_get_state(s)) {
|
2004-09-19 00:03:12 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
case PA_STREAM_READY:
|
|
|
|
|
case PA_STREAM_FAILED:
|
|
|
|
|
case PA_STREAM_TERMINATED:
|
|
|
|
|
pa_threaded_mainloop_signal(p->mainloop, 0);
|
2006-04-12 22:45:57 +00:00
|
|
|
break;
|
|
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
case PA_STREAM_UNCONNECTED:
|
|
|
|
|
case PA_STREAM_CREATING:
|
|
|
|
|
break;
|
2004-09-19 00:03:12 +00:00
|
|
|
}
|
2006-05-06 20:58:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
|
|
|
|
|
pa_simple *p = userdata;
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(p);
|
2006-05-06 20:58:28 +00:00
|
|
|
|
|
|
|
|
pa_threaded_mainloop_signal(p->mainloop, 0);
|
2004-07-07 00:22:46 +00:00
|
|
|
}
|
|
|
|
|
|
2006-05-16 00:06:02 +00:00
|
|
|
static void stream_latency_update_cb(pa_stream *s, void *userdata) {
|
|
|
|
|
pa_simple *p = userdata;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(p);
|
2006-05-16 00:06:02 +00:00
|
|
|
|
|
|
|
|
pa_threaded_mainloop_signal(p->mainloop, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_simple* pa_simple_new(
|
2007-10-28 19:13:50 +00:00
|
|
|
const char *server,
|
|
|
|
|
const char *name,
|
|
|
|
|
pa_stream_direction_t dir,
|
|
|
|
|
const char *dev,
|
|
|
|
|
const char *stream_name,
|
|
|
|
|
const pa_sample_spec *ss,
|
|
|
|
|
const pa_channel_map *map,
|
|
|
|
|
const pa_buffer_attr *attr,
|
|
|
|
|
int *rerror) {
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_simple *p;
|
2006-02-20 23:32:12 +00:00
|
|
|
int error = PA_ERR_INTERNAL, r;
|
|
|
|
|
|
|
|
|
|
CHECK_VALIDITY_RETURN_ANY(rerror, !server || *server, PA_ERR_INVALID, NULL);
|
|
|
|
|
CHECK_VALIDITY_RETURN_ANY(rerror, dir == PA_STREAM_PLAYBACK || dir == PA_STREAM_RECORD, PA_ERR_INVALID, NULL);
|
|
|
|
|
CHECK_VALIDITY_RETURN_ANY(rerror, !dev || *dev, PA_ERR_INVALID, NULL);
|
|
|
|
|
CHECK_VALIDITY_RETURN_ANY(rerror, ss && pa_sample_spec_valid(ss), PA_ERR_INVALID, NULL);
|
2006-05-17 18:52:34 +00:00
|
|
|
CHECK_VALIDITY_RETURN_ANY(rerror, !map || (pa_channel_map_valid(map) && map->channels == ss->channels), PA_ERR_INVALID, NULL)
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2009-08-05 01:04:08 +02:00
|
|
|
p = pa_xnew0(pa_simple, 1);
|
2004-07-10 19:04:21 +00:00
|
|
|
p->direction = dir;
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
if (!(p->mainloop = pa_threaded_mainloop_new()))
|
2004-06-23 23:17:30 +00:00
|
|
|
goto fail;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
if (!(p->context = pa_context_new(pa_threaded_mainloop_get_api(p->mainloop), name)))
|
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
|
|
pa_context_set_state_callback(p->context, context_state_cb, p);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-20 23:32:12 +00:00
|
|
|
if (pa_context_connect(p->context, server, 0, NULL) < 0) {
|
|
|
|
|
error = pa_context_errno(p->context);
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
2006-05-06 20:58:28 +00:00
|
|
|
|
|
|
|
|
pa_threaded_mainloop_lock(p->mainloop);
|
|
|
|
|
|
|
|
|
|
if (pa_threaded_mainloop_start(p->mainloop) < 0)
|
|
|
|
|
goto unlock_and_fail;
|
|
|
|
|
|
2009-08-05 01:04:36 +02:00
|
|
|
for (;;) {
|
|
|
|
|
pa_context_state_t state;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2009-08-05 01:04:36 +02:00
|
|
|
state = pa_context_get_state(p->context);
|
|
|
|
|
|
|
|
|
|
if (state == PA_CONTEXT_READY)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (!PA_CONTEXT_IS_GOOD(state)) {
|
|
|
|
|
error = pa_context_errno(p->context);
|
|
|
|
|
goto unlock_and_fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Wait until the context is ready */
|
|
|
|
|
pa_threaded_mainloop_wait(p->mainloop);
|
2004-06-23 23:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
2006-05-17 18:52:34 +00:00
|
|
|
if (!(p->stream = pa_stream_new(p->context, stream_name, ss, map))) {
|
2006-02-20 23:32:12 +00:00
|
|
|
error = pa_context_errno(p->context);
|
2006-05-06 20:58:28 +00:00
|
|
|
goto unlock_and_fail;
|
2006-02-20 23:32:12 +00:00
|
|
|
}
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
pa_stream_set_state_callback(p->stream, stream_state_cb, p);
|
|
|
|
|
pa_stream_set_read_callback(p->stream, stream_request_cb, p);
|
|
|
|
|
pa_stream_set_write_callback(p->stream, stream_request_cb, p);
|
2006-05-16 00:06:02 +00:00
|
|
|
pa_stream_set_latency_update_callback(p->stream, stream_latency_update_cb, p);
|
2006-05-06 20:58:28 +00:00
|
|
|
|
2004-08-14 20:25:32 +00:00
|
|
|
if (dir == PA_STREAM_PLAYBACK)
|
2009-05-27 23:18:55 +02:00
|
|
|
r = pa_stream_connect_playback(p->stream, dev, attr,
|
|
|
|
|
PA_STREAM_INTERPOLATE_TIMING
|
|
|
|
|
|PA_STREAM_ADJUST_LATENCY
|
|
|
|
|
|PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL);
|
2004-08-14 20:25:32 +00:00
|
|
|
else
|
2009-05-27 23:18:55 +02:00
|
|
|
r = pa_stream_connect_record(p->stream, dev, attr,
|
|
|
|
|
PA_STREAM_INTERPOLATE_TIMING
|
|
|
|
|
|PA_STREAM_ADJUST_LATENCY
|
|
|
|
|
|PA_STREAM_AUTO_TIMING_UPDATE);
|
2006-02-20 23:32:12 +00:00
|
|
|
|
|
|
|
|
if (r < 0) {
|
|
|
|
|
error = pa_context_errno(p->context);
|
2006-05-06 20:58:28 +00:00
|
|
|
goto unlock_and_fail;
|
2006-02-20 23:32:12 +00:00
|
|
|
}
|
2004-08-14 20:25:32 +00:00
|
|
|
|
2009-08-05 01:04:36 +02:00
|
|
|
for (;;) {
|
|
|
|
|
pa_stream_state_t state;
|
2006-05-06 20:58:28 +00:00
|
|
|
|
2009-08-05 01:04:36 +02:00
|
|
|
state = pa_stream_get_state(p->stream);
|
|
|
|
|
|
|
|
|
|
if (state == PA_STREAM_READY)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (!PA_STREAM_IS_GOOD(state)) {
|
|
|
|
|
error = pa_context_errno(p->context);
|
|
|
|
|
goto unlock_and_fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Wait until the stream is ready */
|
|
|
|
|
pa_threaded_mainloop_wait(p->mainloop);
|
2004-06-23 23:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
pa_threaded_mainloop_unlock(p->mainloop);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-06-23 23:17:30 +00:00
|
|
|
return p;
|
2006-05-06 20:58:28 +00:00
|
|
|
|
|
|
|
|
unlock_and_fail:
|
|
|
|
|
pa_threaded_mainloop_unlock(p->mainloop);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-06-23 23:17:30 +00:00
|
|
|
fail:
|
2006-01-11 01:17:39 +00:00
|
|
|
if (rerror)
|
|
|
|
|
*rerror = error;
|
2004-06-23 23:17:30 +00:00
|
|
|
pa_simple_free(p);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa_simple_free(pa_simple *s) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(s);
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
if (s->mainloop)
|
|
|
|
|
pa_threaded_mainloop_stop(s->mainloop);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-06-23 23:17:30 +00:00
|
|
|
if (s->stream)
|
2004-08-14 20:25:32 +00:00
|
|
|
pa_stream_unref(s->stream);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2009-08-05 01:04:50 +02:00
|
|
|
if (s->context) {
|
|
|
|
|
pa_context_disconnect(s->context);
|
2004-08-14 20:25:32 +00:00
|
|
|
pa_context_unref(s->context);
|
2009-08-05 01:04:50 +02:00
|
|
|
}
|
2004-06-23 23:17:30 +00:00
|
|
|
|
|
|
|
|
if (s->mainloop)
|
2006-05-06 20:58:28 +00:00
|
|
|
pa_threaded_mainloop_free(s->mainloop);
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2004-08-04 16:39:30 +00:00
|
|
|
pa_xfree(s);
|
2004-06-23 23:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
int pa_simple_write(pa_simple *p, const void*data, size_t length, int *rerror) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(p);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-20 23:32:12 +00:00
|
|
|
CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE, -1);
|
2009-08-05 01:05:08 +02:00
|
|
|
CHECK_VALIDITY_RETURN_ANY(rerror, data, PA_ERR_INVALID, -1);
|
|
|
|
|
CHECK_VALIDITY_RETURN_ANY(rerror, length > 0, PA_ERR_INVALID, -1);
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
pa_threaded_mainloop_lock(p->mainloop);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
|
2004-09-12 19:37:04 +00:00
|
|
|
|
2004-06-23 23:17:30 +00:00
|
|
|
while (length > 0) {
|
|
|
|
|
size_t l;
|
2006-05-06 20:58:28 +00:00
|
|
|
int r;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
while (!(l = pa_stream_writable_size(p->stream))) {
|
|
|
|
|
pa_threaded_mainloop_wait(p->mainloop);
|
|
|
|
|
CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
|
|
|
|
|
}
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
CHECK_SUCCESS_GOTO(p, rerror, l != (size_t) -1, unlock_and_fail);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-07-06 00:08:44 +00:00
|
|
|
if (l > length)
|
|
|
|
|
l = length;
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2008-08-29 23:53:55 +02:00
|
|
|
r = pa_stream_write(p->stream, data, l, NULL, 0LL, PA_SEEK_RELATIVE);
|
2006-05-06 20:58:28 +00:00
|
|
|
CHECK_SUCCESS_GOTO(p, rerror, r >= 0, unlock_and_fail);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
data = (const uint8_t*) data + l;
|
2004-06-23 23:17:30 +00:00
|
|
|
length -= l;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
pa_threaded_mainloop_unlock(p->mainloop);
|
2004-06-23 23:17:30 +00:00
|
|
|
return 0;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
unlock_and_fail:
|
|
|
|
|
pa_threaded_mainloop_unlock(p->mainloop);
|
|
|
|
|
return -1;
|
2004-06-23 23:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
int pa_simple_read(pa_simple *p, void*data, size_t length, int *rerror) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(p);
|
2004-07-10 19:04:21 +00:00
|
|
|
|
2006-02-20 23:32:12 +00:00
|
|
|
CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_RECORD, PA_ERR_BADSTATE, -1);
|
2009-08-05 01:05:08 +02:00
|
|
|
CHECK_VALIDITY_RETURN_ANY(rerror, data, PA_ERR_INVALID, -1);
|
|
|
|
|
CHECK_VALIDITY_RETURN_ANY(rerror, length > 0, PA_ERR_INVALID, -1);
|
2006-05-06 20:58:28 +00:00
|
|
|
|
|
|
|
|
pa_threaded_mainloop_lock(p->mainloop);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
|
|
|
|
|
|
2004-07-10 19:04:21 +00:00
|
|
|
while (length > 0) {
|
2006-05-06 20:58:28 +00:00
|
|
|
size_t l;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
while (!p->read_data) {
|
|
|
|
|
int r;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
r = pa_stream_peek(p->stream, &p->read_data, &p->read_length);
|
|
|
|
|
CHECK_SUCCESS_GOTO(p, rerror, r == 0, unlock_and_fail);
|
2006-02-20 04:05:16 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
if (!p->read_data) {
|
|
|
|
|
pa_threaded_mainloop_wait(p->mainloop);
|
|
|
|
|
CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
|
|
|
|
|
} else
|
2006-02-20 04:05:16 +00:00
|
|
|
p->read_index = 0;
|
2006-05-06 20:58:28 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
l = p->read_length < length ? p->read_length : length;
|
|
|
|
|
memcpy(data, (const uint8_t*) p->read_data+p->read_index, l);
|
2004-07-10 19:04:21 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
data = (uint8_t*) data + l;
|
|
|
|
|
length -= l;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
p->read_index += l;
|
|
|
|
|
p->read_length -= l;
|
2004-07-10 19:04:21 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
if (!p->read_length) {
|
|
|
|
|
int r;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
r = pa_stream_drop(p->stream);
|
|
|
|
|
p->read_data = NULL;
|
|
|
|
|
p->read_length = 0;
|
|
|
|
|
p->read_index = 0;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
CHECK_SUCCESS_GOTO(p, rerror, r == 0, unlock_and_fail);
|
2004-07-10 19:04:21 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
pa_threaded_mainloop_unlock(p->mainloop);
|
2004-07-10 19:04:21 +00:00
|
|
|
return 0;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
unlock_and_fail:
|
|
|
|
|
pa_threaded_mainloop_unlock(p->mainloop);
|
|
|
|
|
return -1;
|
2004-07-10 19:04:21 +00:00
|
|
|
}
|
2004-07-07 22:02:15 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
static void success_cb(pa_stream *s, int success, void *userdata) {
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_simple *p = userdata;
|
2006-02-20 23:32:12 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(s);
|
|
|
|
|
pa_assert(p);
|
2006-05-06 20:58:28 +00:00
|
|
|
|
|
|
|
|
p->operation_success = success;
|
|
|
|
|
pa_threaded_mainloop_signal(p->mainloop, 0);
|
2004-07-07 22:02:15 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
int pa_simple_drain(pa_simple *p, int *rerror) {
|
2006-05-06 20:58:28 +00:00
|
|
|
pa_operation *o = NULL;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(p);
|
2006-02-20 23:32:12 +00:00
|
|
|
|
|
|
|
|
CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE, -1);
|
2004-09-12 19:37:04 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
pa_threaded_mainloop_lock(p->mainloop);
|
|
|
|
|
CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
|
2004-09-12 19:37:04 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
o = pa_stream_drain(p->stream, success_cb, p);
|
|
|
|
|
CHECK_SUCCESS_GOTO(p, rerror, o, unlock_and_fail);
|
2004-09-12 19:37:04 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
p->operation_success = 0;
|
2009-08-05 01:05:35 +02:00
|
|
|
while (pa_operation_get_state(o) == PA_OPERATION_RUNNING) {
|
2006-05-06 20:58:28 +00:00
|
|
|
pa_threaded_mainloop_wait(p->mainloop);
|
|
|
|
|
CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
|
2004-09-12 19:37:04 +00:00
|
|
|
}
|
2006-05-06 20:58:28 +00:00
|
|
|
CHECK_SUCCESS_GOTO(p, rerror, p->operation_success, unlock_and_fail);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-09-12 19:37:04 +00:00
|
|
|
pa_operation_unref(o);
|
2006-05-06 20:58:28 +00:00
|
|
|
pa_threaded_mainloop_unlock(p->mainloop);
|
2004-09-12 19:37:04 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
return 0;
|
2006-02-20 23:32:12 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
unlock_and_fail:
|
2004-09-12 19:37:04 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
if (o) {
|
|
|
|
|
pa_operation_cancel(o);
|
|
|
|
|
pa_operation_unref(o);
|
2004-11-08 23:48:19 +00:00
|
|
|
}
|
2006-05-06 20:58:28 +00:00
|
|
|
|
|
|
|
|
pa_threaded_mainloop_unlock(p->mainloop);
|
|
|
|
|
return -1;
|
2004-09-12 19:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
int pa_simple_flush(pa_simple *p, int *rerror) {
|
|
|
|
|
pa_operation *o = NULL;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(p);
|
2006-05-06 20:58:28 +00:00
|
|
|
|
2006-02-20 23:32:12 +00:00
|
|
|
CHECK_VALIDITY_RETURN_ANY(rerror, p->direction == PA_STREAM_PLAYBACK, PA_ERR_BADSTATE, -1);
|
2004-09-12 19:37:04 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
pa_threaded_mainloop_lock(p->mainloop);
|
|
|
|
|
CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
|
2004-09-12 19:37:04 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
o = pa_stream_flush(p->stream, success_cb, p);
|
|
|
|
|
CHECK_SUCCESS_GOTO(p, rerror, o, unlock_and_fail);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
p->operation_success = 0;
|
2009-08-05 01:05:35 +02:00
|
|
|
while (pa_operation_get_state(o) == PA_OPERATION_RUNNING) {
|
2006-05-06 20:58:28 +00:00
|
|
|
pa_threaded_mainloop_wait(p->mainloop);
|
|
|
|
|
CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
|
2004-09-12 19:37:04 +00:00
|
|
|
}
|
2006-05-06 20:58:28 +00:00
|
|
|
CHECK_SUCCESS_GOTO(p, rerror, p->operation_success, unlock_and_fail);
|
2004-09-12 19:37:04 +00:00
|
|
|
|
|
|
|
|
pa_operation_unref(o);
|
2006-05-06 20:58:28 +00:00
|
|
|
pa_threaded_mainloop_unlock(p->mainloop);
|
|
|
|
|
|
|
|
|
|
return 0;
|
2004-09-12 19:37:04 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
unlock_and_fail:
|
|
|
|
|
|
|
|
|
|
if (o) {
|
|
|
|
|
pa_operation_cancel(o);
|
|
|
|
|
pa_operation_unref(o);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_threaded_mainloop_unlock(p->mainloop);
|
|
|
|
|
return -1;
|
2004-09-12 19:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
2006-05-20 14:59:02 +00:00
|
|
|
pa_usec_t pa_simple_get_latency(pa_simple *p, int *rerror) {
|
2006-05-06 20:58:28 +00:00
|
|
|
pa_usec_t t;
|
2006-05-16 00:06:02 +00:00
|
|
|
int negative;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(p);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
pa_threaded_mainloop_lock(p->mainloop);
|
2004-09-12 19:37:04 +00:00
|
|
|
|
2006-05-16 00:06:02 +00:00
|
|
|
for (;;) {
|
|
|
|
|
CHECK_DEAD_GOTO(p, rerror, unlock_and_fail);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-05-16 00:06:02 +00:00
|
|
|
if (pa_stream_get_latency(p->stream, &t, &negative) >= 0)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
CHECK_SUCCESS_GOTO(p, rerror, pa_context_errno(p->context) == PA_ERR_NODATA, unlock_and_fail);
|
|
|
|
|
|
|
|
|
|
/* Wait until latency data is available again */
|
|
|
|
|
pa_threaded_mainloop_wait(p->mainloop);
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
pa_threaded_mainloop_unlock(p->mainloop);
|
2004-07-07 22:02:15 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
return negative ? 0 : t;
|
2004-08-14 20:25:32 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
unlock_and_fail:
|
2004-08-14 20:25:32 +00:00
|
|
|
|
2006-05-06 20:58:28 +00:00
|
|
|
pa_threaded_mainloop_unlock(p->mainloop);
|
|
|
|
|
return (pa_usec_t) -1;
|
2004-07-07 22:02:15 +00:00
|
|
|
}
|