summaryrefslogtreecommitdiff
path: root/src/tests/lo-latency-test.c
blob: 8f3b04d4d8f9f323d07959ebe2ab924a38a3e274 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/***
  This file is part of PulseAudio.

  Copyright 2013 Collabora Ltd.
  Author: Arun Raghavan <arun.raghavan@collabora.co.uk>

  PulseAudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published
  by the Free Software Foundation; either version 2.1 of the License,
  or (at your option) any later version.

  PulseAudio is distributed in the hope that it will be useful, but
  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.

  You should have received a copy of the GNU Lesser General Public License
  along with PulseAudio; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include <check.h>

#include <pulse/pulseaudio.h>
#include <pulse/mainloop.h>

/* for pa_make_realtime */
#include <pulsecore/core-util.h>

#define SAMPLE_HZ 44100
#define CHANNELS 2
#define N_OUT (SAMPLE_HZ * 1)

#define TONE_HZ (SAMPLE_HZ / 100)
#define PLAYBACK_LATENCY 25 /* ms */
#define CAPTURE_LATENCY 5 /* ms */

static pa_context *context = NULL;
static pa_stream *pstream, *rstream;
static pa_mainloop_api *mainloop_api = NULL;
static const char *context_name = NULL;

static float out[N_OUT][CHANNELS];
static int ppos = 0;

static int n_underflow = 0;
static int n_overflow = 0;

static struct timeval tv_out, tv_in;

static const pa_sample_spec sample_spec = {
    .format = PA_SAMPLE_FLOAT32,
    .rate = SAMPLE_HZ,
    .channels = CHANNELS,
};
static int ss, fs;

static void nop_free_cb(void *p) {}

static void underflow_cb(struct pa_stream *s, void *userdata) {
    fprintf(stderr, "Underflow\n");
    n_underflow++;
}

static void overflow_cb(struct pa_stream *s, void *userdata) {
    fprintf(stderr, "Overlow\n");
    n_overflow++;
}

static void write_cb(pa_stream *s, size_t nbytes, void *userdata) {
    int r, nsamp = nbytes / fs;

    if (ppos + nsamp > N_OUT) {
        r = pa_stream_write(s, &out[ppos][0], (N_OUT - ppos) * fs, nop_free_cb, 0, PA_SEEK_RELATIVE);
        nbytes -= (N_OUT - ppos) * fs;
        ppos = 0;
    }

    if (ppos == 0)
        pa_gettimeofday(&tv_out);

    r = pa_stream_write(s, &out[ppos][0], nbytes, nop_free_cb, 0, PA_SEEK_RELATIVE);
    fail_unless(r == 0);

    ppos = (ppos + nbytes / fs) % N_OUT;
}

static inline float rms(const float *s, int n) {
    float sq = 0;
    int i;

    for (i = 0; i < n; i++)
        sq += s[i] * s[i];

    return sqrtf(sq / n);
}

#define WINDOW (2 * CHANNELS)

static void read_cb(pa_stream *s, size_t nbytes, void *userdata) {
    static float last = 0.0f;
    const float *in;
    float cur;
    int r;
    unsigned int i = 0;
    size_t l;

    r = pa_stream_peek(s, (const void **)&in, &l);
    fail_unless(r == 0);

    if (l == 0)
        return;

#if 0
    {
        static int fd = -1;

        if (fd == -1) {
            fd = open("loopback.raw", O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR);
            fail_if(fd < 0);
        }

        r = write(fd, in, l);
    }
#endif

    do {
#if 0
        {
            int j;
            fprintf(stderr, "%g (", rms(in, WINDOW));
            for (j = 0; j < WINDOW; j++)
                fprintf(stderr, "%g ", in[j]);
            fprintf(stderr, ")\n");
        }
#endif
        if (i + (ss * WINDOW) < l)
            cur = rms(in, WINDOW);
        else
            cur = rms(in, (l - i)/ss);

        /* We leave the definition of 0 generous since the window might
         * straddle the 0->1 transition, raising the average power. We keep the
         * definition of 1 tight in this case and detect the transition in the
         * next round. */
        if (cur - last > 0.4f) {
            pa_gettimeofday(&tv_in);
            fprintf(stderr, "Latency %llu\n", (unsigned long long) pa_timeval_diff(&tv_in, &tv_out));
        }

        last = cur;
        in += WINDOW;
        i += ss * WINDOW;
    } while (i + (ss * WINDOW) <= l);

    pa_stream_drop(s);
}

/*
 * We run a simple volume calibration so that we know we can detect the signal
 * being played back. We start with the playback stream at 100% volume, and
 * capture at 0.
 *
 * First, we then play a sine wave and increase the capture volume till the
 * signal is clearly received.
 *
 * Next, we play back silence and make sure that the level is low enough to
 * distinguish from when playback is happening.
 *
 * Finally, we hand off to the real read/write callbacks to run the actual
 * test.
 */

enum {
    CALIBRATION_ONE,
    CALIBRATION_ZERO,
    CALIBRATION_DONE,
};

static int cal_state = CALIBRATION_ONE;

static void calibrate_write_cb(pa_stream *s, size_t nbytes, void *userdata) {
    int i, r, nsamp = nbytes / fs;
    float tmp[nsamp][2];
    static int count = 0;

    /* Write out a sine tone */
    for (i = 0; i < nsamp; i++)
        tmp[i][0] = tmp[i][1] = cal_state == CALIBRATION_ONE ? sinf(count++ * TONE_HZ * 2 * M_PI / SAMPLE_HZ) : 0.0f;

    r = pa_stream_write(s, &tmp, nbytes, nop_free_cb, 0, PA_SEEK_RELATIVE);
    fail_unless(r == 0);

    if (cal_state == CALIBRATION_DONE)
        pa_stream_set_write_callback(s, write_cb, NULL);
}

static void calibrate_read_cb(pa_stream *s, size_t nbytes, void *userdata) {
    static double v = 0;
    static int skip = 0, confirm;

    pa_cvolume vol;
    pa_operation *o;
    int r, nsamp;
    float *in;
    size_t l;

    r = pa_stream_peek(s, (const void **)&in, &l);
    fail_unless(r == 0);

    nsamp = l / fs;

    /* For each state or volume step change, throw out a few samples so we know
     * we're seeing the changed samples. */
    if (skip++ < 100)
        goto out;
    else
        skip = 0;

    switch (cal_state) {
        case CALIBRATION_ONE:
            /* Try to detect the sine wave. RMS is 0.5, */
            if (rms(in, nsamp) < 0.40f) {
                confirm = 0;
                v += 0.02f;

                if (v > 1.0) {
                    fprintf(stderr, "Capture signal too weak at 100%% volume (%g). Giving up.\n", rms(in, nsamp));
                    fail();
                }

                pa_cvolume_set(&vol, CHANNELS, v * PA_VOLUME_NORM);
                o = pa_context_set_source_output_volume(context, pa_stream_get_index(s), &vol, NULL, NULL);
                fail_if(o == NULL);
                pa_operation_unref(o);
            } else {
                /* Make sure the signal strength is steadily above our threshold */
                if (++confirm > 5) {
#if 0
                    fprintf(stderr, "Capture volume = %g (%g)\n", v, rms(in, nsamp));
#endif
                    cal_state = CALIBRATION_ZERO;
                }
            }

            break;

        case CALIBRATION_ZERO:
            /* Now make sure silence doesn't trigger a false positive because
             * of noise. */
            if (rms(in, nsamp) > 0.1f) {
                fprintf(stderr, "Too much noise on capture (%g). Giving up.\n", rms(in, nsamp));
                fail();
            }

            cal_state = CALIBRATION_DONE;
            pa_stream_set_read_callback(s, read_cb, NULL);

            break;

        default:
            break;
    }

out:
    pa_stream_drop(s);
}

/* This routine is called whenever the stream state changes */
static void stream_state_callback(pa_stream *s, void *userdata) {
    switch (pa_stream_get_state(s)) {
        case PA_STREAM_UNCONNECTED:
        case PA_STREAM_CREATING:
        case PA_STREAM_TERMINATED:
            break;

        case PA_STREAM_READY: {
            pa_cvolume vol;
            pa_operation *o;

            /* Set volumes for calibration */
            if (!userdata) {
                pa_cvolume_set(&vol, CHANNELS, PA_VOLUME_NORM);
                o = pa_context_set_sink_input_volume(context, pa_stream_get_index(s), &vol, NULL, NULL);
            } else {
                pa_cvolume_set(&vol, CHANNELS, pa_sw_volume_from_linear(0.0));
                o = pa_context_set_source_output_volume(context, pa_stream_get_index(s), &vol, NULL, NULL);
            }

            if (!o) {
                fprintf(stderr, "Could not set stream volume: %s\n", pa_strerror(pa_context_errno(context)));
                fail();
            } else
                pa_operation_unref(o);

            break;
        }

        case PA_STREAM_FAILED:
        default:
            fprintf(stderr, "Stream error: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
            fail();
    }
}

/* This is called whenever the context status changes */
static void context_state_callback(pa_context *c, void *userdata) {
    fail_unless(c != NULL);

    switch (pa_context_get_state(c)) {
        case PA_CONTEXT_CONNECTING:
        case PA_CONTEXT_AUTHORIZING:
        case PA_CONTEXT_SETTING_NAME:
            break;

        case PA_CONTEXT_READY: {
            pa_buffer_attr buffer_attr;

            pa_make_realtime(4);

            /* Create playback stream */
            buffer_attr.maxlength = -1;
            buffer_attr.tlength = SAMPLE_HZ * fs * PLAYBACK_LATENCY / 1000;
            buffer_attr.prebuf = 0; /* Setting prebuf to 0 guarantees us the stream will run synchronously, no matter what */
            buffer_attr.minreq = -1;
            buffer_attr.fragsize = -1;

            pstream = pa_stream_new(c, "loopback: play", &sample_spec, NULL);
            fail_unless(pstream != NULL);
            pa_stream_set_state_callback(pstream, stream_state_callback, (void *) 0);
            pa_stream_set_write_callback(pstream, calibrate_write_cb, NULL);
            pa_stream_set_underflow_callback(pstream, underflow_cb, userdata);

            pa_stream_connect_playback(pstream, getenv("TEST_SINK"), &buffer_attr,
                    PA_STREAM_ADJUST_LATENCY | PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL);

            /* Create capture stream */
            buffer_attr.maxlength = -1;
            buffer_attr.tlength = (uint32_t) -1;
            buffer_attr.prebuf = 0;
            buffer_attr.minreq = (uint32_t) -1;
            buffer_attr.fragsize = SAMPLE_HZ * fs * CAPTURE_LATENCY / 1000;

            rstream = pa_stream_new(c, "loopback: rec", &sample_spec, NULL);
            fail_unless(rstream != NULL);
            pa_stream_set_state_callback(rstream, stream_state_callback, (void *) 1);
            pa_stream_set_read_callback(rstream, calibrate_read_cb, NULL);
            pa_stream_set_overflow_callback(rstream, overflow_cb, userdata);

            pa_stream_connect_record(rstream, getenv("TEST_SOURCE"), &buffer_attr,
                    PA_STREAM_ADJUST_LATENCY | PA_STREAM_AUTO_TIMING_UPDATE);

            break;
        }

        case PA_CONTEXT_TERMINATED:
            mainloop_api->quit(mainloop_api, 0);
            break;

        case PA_CONTEXT_FAILED:
        default:
            fprintf(stderr, "Context error: %s\n", pa_strerror(pa_context_errno(c)));
            fail();
    }
}

START_TEST (loopback_test) {
    pa_mainloop* m = NULL;
    int i, ret = 0, pulse_hz = SAMPLE_HZ / 1000;

    /* Generate a square pulse */
    for (i = 0; i < N_OUT; i++)
        if (i < pulse_hz)
            out[i][0] = out[i][1] = 1.0f;
        else
            out[i][0] = out[i][1] = 0.0f;

    ss = pa_sample_size(&sample_spec);
    fs = pa_frame_size(&sample_spec);

    pstream = NULL;

    /* Set up a new main loop */
    m = pa_mainloop_new();
    fail_unless(m != NULL);

    mainloop_api = pa_mainloop_get_api(m);

    context = pa_context_new(mainloop_api, context_name);
    fail_unless(context != NULL);

    pa_context_set_state_callback(context, context_state_callback, NULL);

    /* Connect the context */
    if (pa_context_connect(context, NULL, 0, NULL) < 0) {
        fprintf(stderr, "pa_context_connect() failed.\n");
        goto quit;
    }

    if (pa_mainloop_run(m, &ret) < 0)
        fprintf(stderr, "pa_mainloop_run() failed.\n");

quit:
    pa_context_unref(context);

    if (pstream)
        pa_stream_unref(pstream);

    pa_mainloop_free(m);

    fail_unless(ret == 0);
}
END_TEST

int main(int argc, char *argv[]) {
    int failed = 0;
    Suite *s;
    TCase *tc;
    SRunner *sr;

    context_name = argv[0];

    s = suite_create("Loopback");
    tc = tcase_create("loopback");
    tcase_add_test(tc, loopback_test);
    tcase_set_timeout(tc, 5 * 60);
    suite_add_tcase(s, tc);

    sr = srunner_create(s);
    srunner_set_fork_status(sr, CK_NOFORK);
    srunner_run_all(sr, CK_NORMAL);
    failed = srunner_ntests_failed(sr);
    srunner_free(sr);

    return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}