1/***
2  This file is part of PulseAudio.
3
4  PulseAudio is free software; you can redistribute it and/or modify
5  it under the terms of the GNU Lesser General Public License as published
6  by the Free Software Foundation; either version 2.1 of the License,
7  or (at your option) any later version.
8
9  PulseAudio is distributed in the hope that it will be useful, but
10  WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  General Public License for more details.
13
14  You should have received a copy of the GNU Lesser General Public License
15  along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
16***/
17
18#ifdef HAVE_CONFIG_H
19#include <config.h>
20#endif
21
22#include <stdio.h>
23#include <unistd.h>
24#include <string.h>
25#include <errno.h>
26#include <fcntl.h>
27
28#include <pulse/simple.h>
29#include <pulse/error.h>
30
31#define BUFSIZE 1024
32
33int main(int argc, char*argv[]) {
34
35    /* The Sample format to use */
36    static const pa_sample_spec ss = {
37        .format = PA_SAMPLE_S16LE,
38        .rate = 44100,
39        .channels = 2
40    };
41
42    pa_simple *s = NULL;
43    int ret = 1;
44    int error;
45
46    /* replace STDIN with the specified file if needed */
47    if (argc > 1) {
48        int fd;
49
50        if ((fd = open(argv[1], O_RDONLY)) < 0) {
51            fprintf(stderr, __FILE__": open() failed: %s\n", strerror(errno));
52            goto finish;
53        }
54
55        if (dup2(fd, STDIN_FILENO) < 0) {
56            fprintf(stderr, __FILE__": dup2() failed: %s\n", strerror(errno));
57            goto finish;
58        }
59
60        close(fd);
61    }
62
63    /* Create a new playback stream */
64    if (!(s = pa_simple_new(NULL, argv[0], PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &error))) {
65        fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
66        goto finish;
67    }
68
69    for (;;) {
70        uint8_t buf[BUFSIZE];
71        ssize_t r;
72
73#if 0
74        pa_usec_t latency;
75
76        if ((latency = pa_simple_get_latency(s, &error)) == (pa_usec_t) -1) {
77            fprintf(stderr, __FILE__": pa_simple_get_latency() failed: %s\n", pa_strerror(error));
78            goto finish;
79        }
80
81        fprintf(stderr, "%0.0f usec    \r", (float)latency);
82#endif
83
84        /* Read some data ... */
85        if ((r = read(STDIN_FILENO, buf, sizeof(buf))) <= 0) {
86            if (r == 0) /* EOF */
87                break;
88
89            fprintf(stderr, __FILE__": read() failed: %s\n", strerror(errno));
90            goto finish;
91        }
92
93        /* ... and play it */
94        if (pa_simple_write(s, buf, (size_t) r, &error) < 0) {
95            fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error));
96            goto finish;
97        }
98    }
99
100    /* Make sure that every single sample was played */
101    if (pa_simple_drain(s, &error) < 0) {
102        fprintf(stderr, __FILE__": pa_simple_drain() failed: %s\n", pa_strerror(error));
103        goto finish;
104    }
105
106    ret = 0;
107
108finish:
109
110    if (s)
111        pa_simple_free(s);
112
113    return ret;
114}
115