153a5a1b3Sopenharmony_ci/***
253a5a1b3Sopenharmony_ci  This file is part of PulseAudio.
353a5a1b3Sopenharmony_ci
453a5a1b3Sopenharmony_ci  PulseAudio is free software; you can redistribute it and/or modify
553a5a1b3Sopenharmony_ci  it under the terms of the GNU Lesser General Public License as published
653a5a1b3Sopenharmony_ci  by the Free Software Foundation; either version 2.1 of the License,
753a5a1b3Sopenharmony_ci  or (at your option) any later version.
853a5a1b3Sopenharmony_ci
953a5a1b3Sopenharmony_ci  PulseAudio is distributed in the hope that it will be useful, but
1053a5a1b3Sopenharmony_ci  WITHOUT ANY WARRANTY; without even the implied warranty of
1153a5a1b3Sopenharmony_ci  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1253a5a1b3Sopenharmony_ci  General Public License for more details.
1353a5a1b3Sopenharmony_ci
1453a5a1b3Sopenharmony_ci  You should have received a copy of the GNU Lesser General Public License
1553a5a1b3Sopenharmony_ci  along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
1653a5a1b3Sopenharmony_ci***/
1753a5a1b3Sopenharmony_ci
1853a5a1b3Sopenharmony_ci#ifdef HAVE_CONFIG_H
1953a5a1b3Sopenharmony_ci#include <config.h>
2053a5a1b3Sopenharmony_ci#endif
2153a5a1b3Sopenharmony_ci
2253a5a1b3Sopenharmony_ci#include <stdio.h>
2353a5a1b3Sopenharmony_ci#include <unistd.h>
2453a5a1b3Sopenharmony_ci#include <string.h>
2553a5a1b3Sopenharmony_ci#include <errno.h>
2653a5a1b3Sopenharmony_ci
2753a5a1b3Sopenharmony_ci#include <pulse/simple.h>
2853a5a1b3Sopenharmony_ci#include <pulse/error.h>
2953a5a1b3Sopenharmony_ci
3053a5a1b3Sopenharmony_ci#define BUFSIZE 1024
3153a5a1b3Sopenharmony_ci
3253a5a1b3Sopenharmony_ci/* A simple routine calling UNIX write() in a loop */
3353a5a1b3Sopenharmony_cistatic ssize_t loop_write(int fd, const void*data, size_t size) {
3453a5a1b3Sopenharmony_ci    ssize_t ret = 0;
3553a5a1b3Sopenharmony_ci
3653a5a1b3Sopenharmony_ci    while (size > 0) {
3753a5a1b3Sopenharmony_ci        ssize_t r;
3853a5a1b3Sopenharmony_ci
3953a5a1b3Sopenharmony_ci        if ((r = write(fd, data, size)) < 0)
4053a5a1b3Sopenharmony_ci            return r;
4153a5a1b3Sopenharmony_ci
4253a5a1b3Sopenharmony_ci        if (r == 0)
4353a5a1b3Sopenharmony_ci            break;
4453a5a1b3Sopenharmony_ci
4553a5a1b3Sopenharmony_ci        ret += r;
4653a5a1b3Sopenharmony_ci        data = (const uint8_t*) data + r;
4753a5a1b3Sopenharmony_ci        size -= (size_t) r;
4853a5a1b3Sopenharmony_ci    }
4953a5a1b3Sopenharmony_ci
5053a5a1b3Sopenharmony_ci    return ret;
5153a5a1b3Sopenharmony_ci}
5253a5a1b3Sopenharmony_ci
5353a5a1b3Sopenharmony_ciint main(int argc, char*argv[]) {
5453a5a1b3Sopenharmony_ci    /* The sample type to use */
5553a5a1b3Sopenharmony_ci    static const pa_sample_spec ss = {
5653a5a1b3Sopenharmony_ci        .format = PA_SAMPLE_S16LE,
5753a5a1b3Sopenharmony_ci        .rate = 44100,
5853a5a1b3Sopenharmony_ci        .channels = 2
5953a5a1b3Sopenharmony_ci    };
6053a5a1b3Sopenharmony_ci    pa_simple *s = NULL;
6153a5a1b3Sopenharmony_ci    int ret = 1;
6253a5a1b3Sopenharmony_ci    int error;
6353a5a1b3Sopenharmony_ci
6453a5a1b3Sopenharmony_ci    /* Create the recording stream */
6553a5a1b3Sopenharmony_ci    if (!(s = pa_simple_new(NULL, argv[0], PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error))) {
6653a5a1b3Sopenharmony_ci        fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
6753a5a1b3Sopenharmony_ci        goto finish;
6853a5a1b3Sopenharmony_ci    }
6953a5a1b3Sopenharmony_ci
7053a5a1b3Sopenharmony_ci    for (;;) {
7153a5a1b3Sopenharmony_ci        uint8_t buf[BUFSIZE];
7253a5a1b3Sopenharmony_ci
7353a5a1b3Sopenharmony_ci        /* Record some data ... */
7453a5a1b3Sopenharmony_ci        if (pa_simple_read(s, buf, sizeof(buf), &error) < 0) {
7553a5a1b3Sopenharmony_ci            fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error));
7653a5a1b3Sopenharmony_ci            goto finish;
7753a5a1b3Sopenharmony_ci        }
7853a5a1b3Sopenharmony_ci
7953a5a1b3Sopenharmony_ci        /* And write it to STDOUT */
8053a5a1b3Sopenharmony_ci        if (loop_write(STDOUT_FILENO, buf, sizeof(buf)) != sizeof(buf)) {
8153a5a1b3Sopenharmony_ci            fprintf(stderr, __FILE__": write() failed: %s\n", strerror(errno));
8253a5a1b3Sopenharmony_ci            goto finish;
8353a5a1b3Sopenharmony_ci        }
8453a5a1b3Sopenharmony_ci    }
8553a5a1b3Sopenharmony_ci
8653a5a1b3Sopenharmony_ci    ret = 0;
8753a5a1b3Sopenharmony_ci
8853a5a1b3Sopenharmony_cifinish:
8953a5a1b3Sopenharmony_ci
9053a5a1b3Sopenharmony_ci    if (s)
9153a5a1b3Sopenharmony_ci        pa_simple_free(s);
9253a5a1b3Sopenharmony_ci
9353a5a1b3Sopenharmony_ci    return ret;
9453a5a1b3Sopenharmony_ci}
95