2006-06-19 11:27:00 +00:00
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2006-06-19 11:27:00 +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,
|
2006-06-19 11:27:00 +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
|
2006-06-19 11:27:00 +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
|
|
|
|
2006-06-19 11:27:00 +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
|
2006-06-19 11:27:00 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <getopt.h>
|
|
|
|
|
#include <math.h>
|
2006-06-20 13:49:30 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulse/pulseaudio.h>
|
|
|
|
|
#include <pulse/mainloop.h>
|
2006-06-19 11:27:00 +00:00
|
|
|
|
2006-09-04 22:38:41 +00:00
|
|
|
#include <pulsecore/thread.h>
|
|
|
|
|
|
2006-06-19 11:27:00 +00:00
|
|
|
static pa_context *context = NULL;
|
|
|
|
|
static pa_stream *stream = NULL;
|
|
|
|
|
static pa_mainloop_api *mainloop_api = NULL;
|
2009-02-19 03:59:56 +01:00
|
|
|
static pa_bool_t playback = TRUE;
|
2006-06-19 11:27:00 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
static void stream_write_cb(pa_stream *p, size_t nbytes, void *userdata) {
|
2006-06-19 11:27:00 +00:00
|
|
|
/* Just some silence */
|
2009-02-19 03:59:56 +01:00
|
|
|
pa_assert_se(pa_stream_write(p, pa_xmalloc0(nbytes), nbytes, pa_xfree, 0, PA_SEEK_RELATIVE) == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void stream_read_cb(pa_stream *p, size_t nbytes, void *userdata) {
|
|
|
|
|
/* We don't care, just drop the data */
|
|
|
|
|
|
|
|
|
|
while (pa_stream_readable_size(p) > 0) {
|
|
|
|
|
const void *d;
|
|
|
|
|
size_t b;
|
|
|
|
|
|
|
|
|
|
pa_assert_se(pa_stream_peek(p, &d, &b) == 0);
|
|
|
|
|
pa_assert_se(pa_stream_drop(p) == 0);
|
|
|
|
|
}
|
2006-06-19 11:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This is called whenever the context status changes */
|
|
|
|
|
static void context_state_callback(pa_context *c, void *userdata) {
|
|
|
|
|
assert(c);
|
|
|
|
|
|
|
|
|
|
switch (pa_context_get_state(c)) {
|
|
|
|
|
case PA_CONTEXT_CONNECTING:
|
|
|
|
|
case PA_CONTEXT_AUTHORIZING:
|
|
|
|
|
case PA_CONTEXT_SETTING_NAME:
|
|
|
|
|
break;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 11:27:00 +00:00
|
|
|
case PA_CONTEXT_READY: {
|
|
|
|
|
|
|
|
|
|
static const pa_sample_spec ss = {
|
|
|
|
|
.format = PA_SAMPLE_S16LE,
|
|
|
|
|
.rate = 44100,
|
2008-05-15 23:34:41 +00:00
|
|
|
.channels = 2
|
2006-06-19 11:27:00 +00:00
|
|
|
};
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 11:27:00 +00:00
|
|
|
fprintf(stderr, "Connection established.\n");
|
|
|
|
|
|
|
|
|
|
stream = pa_stream_new(c, "interpol-test", &ss, NULL);
|
|
|
|
|
assert(stream);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2009-02-19 03:59:56 +01:00
|
|
|
if (playback) {
|
|
|
|
|
pa_assert_se(pa_stream_connect_playback(stream, NULL, NULL, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL) == 0);
|
|
|
|
|
pa_stream_set_write_callback(stream, stream_write_cb, NULL);
|
|
|
|
|
} else {
|
|
|
|
|
pa_assert_se(pa_stream_connect_record(stream, NULL, NULL, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE) == 0);
|
|
|
|
|
pa_stream_set_read_callback(stream, stream_read_cb, NULL);
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 11:27:00 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 11:27:00 +00:00
|
|
|
case PA_CONTEXT_TERMINATED:
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PA_CONTEXT_FAILED:
|
|
|
|
|
default:
|
|
|
|
|
fprintf(stderr, "Context error: %s\n", pa_strerror(pa_context_errno(c)));
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
pa_threaded_mainloop* m = NULL;
|
|
|
|
|
int k, r;
|
|
|
|
|
struct timeval start, last_info = { 0, 0 };
|
|
|
|
|
pa_usec_t old_t = 0, old_rtc = 0;
|
|
|
|
|
|
2009-02-19 03:59:56 +01:00
|
|
|
playback = argc <= 1 || !pa_streq(argv[1], "-r");
|
|
|
|
|
|
2006-06-19 11:27:00 +00:00
|
|
|
/* Set up a new main loop */
|
|
|
|
|
m = pa_threaded_mainloop_new();
|
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
|
|
mainloop_api = pa_threaded_mainloop_get_api(m);
|
|
|
|
|
|
|
|
|
|
context = pa_context_new(mainloop_api, argv[0]);
|
|
|
|
|
assert(context);
|
|
|
|
|
|
|
|
|
|
pa_context_set_state_callback(context, context_state_callback, NULL);
|
|
|
|
|
|
|
|
|
|
r = pa_context_connect(context, NULL, 0, NULL);
|
|
|
|
|
assert(r >= 0);
|
|
|
|
|
|
|
|
|
|
pa_gettimeofday(&start);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2009-02-17 20:36:38 +02:00
|
|
|
r = pa_threaded_mainloop_start(m);
|
|
|
|
|
assert(r >= 0);
|
2006-06-19 11:27:00 +00:00
|
|
|
|
|
|
|
|
for (k = 0; k < 5000; k++) {
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_bool_t success = FALSE, changed = FALSE;
|
2006-06-19 11:27:00 +00:00
|
|
|
pa_usec_t t, rtc;
|
|
|
|
|
struct timeval now, tv;
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_bool_t playing = FALSE;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 11:27:00 +00:00
|
|
|
pa_threaded_mainloop_lock(m);
|
|
|
|
|
|
|
|
|
|
if (stream) {
|
|
|
|
|
const pa_timing_info *info;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 11:27:00 +00:00
|
|
|
if (pa_stream_get_time(stream, &t) >= 0)
|
2008-05-15 23:34:41 +00:00
|
|
|
success = TRUE;
|
2006-06-19 11:27:00 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
if ((info = pa_stream_get_timing_info(stream))) {
|
|
|
|
|
if (memcmp(&last_info, &info->timestamp, sizeof(struct timeval))) {
|
|
|
|
|
changed = TRUE;
|
2006-06-19 11:27:00 +00:00
|
|
|
last_info = info->timestamp;
|
|
|
|
|
}
|
2008-05-15 23:34:41 +00:00
|
|
|
if (info->playing)
|
|
|
|
|
playing = TRUE;
|
|
|
|
|
}
|
2006-06-19 11:27:00 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 11:27:00 +00:00
|
|
|
pa_threaded_mainloop_unlock(m);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_gettimeofday(&now);
|
2006-06-19 11:27:00 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
if (success) {
|
2006-06-19 11:27:00 +00:00
|
|
|
rtc = pa_timeval_diff(&now, &start);
|
2009-02-19 03:59:56 +01:00
|
|
|
printf("%i\t%llu\t%llu\t%llu\t%llu\t%u\t%u\n", k,
|
|
|
|
|
(unsigned long long) rtc,
|
|
|
|
|
(unsigned long long) t,
|
|
|
|
|
(unsigned long long) (rtc-old_rtc),
|
|
|
|
|
(unsigned long long) (t-old_t),
|
|
|
|
|
changed,
|
|
|
|
|
playing);
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
fflush(stdout);
|
2006-06-19 11:27:00 +00:00
|
|
|
old_t = t;
|
|
|
|
|
old_rtc = rtc;
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-19 12:20:10 +00:00
|
|
|
/* Spin loop, ugly but normal usleep() is just too badly grained */
|
2006-06-19 11:27:00 +00:00
|
|
|
|
|
|
|
|
tv = now;
|
|
|
|
|
while (pa_timeval_diff(pa_gettimeofday(&now), &tv) < 1000)
|
2006-09-04 22:38:41 +00:00
|
|
|
pa_thread_yield();
|
2006-06-19 11:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m)
|
|
|
|
|
pa_threaded_mainloop_stop(m);
|
|
|
|
|
|
|
|
|
|
if (stream) {
|
|
|
|
|
pa_stream_disconnect(stream);
|
|
|
|
|
pa_stream_unref(stream);
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 11:27:00 +00:00
|
|
|
if (context) {
|
|
|
|
|
pa_context_disconnect(context);
|
|
|
|
|
pa_context_unref(context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m)
|
|
|
|
|
pa_threaded_mainloop_free(m);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 11:27:00 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|