00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifdef HAVE_CONFIG_H
00023 #include <config.h>
00024 #endif
00025
00026 #include <stdio.h>
00027 #include <unistd.h>
00028 #include <string.h>
00029 #include <errno.h>
00030 #include <fcntl.h>
00031
00032 #include <pulse/simple.h>
00033 #include <pulse/error.h>
00034 #include <pulsecore/gccmacro.h>
00035
00036 #define BUFSIZE 1024
00037
00038 int main(PA_GCC_UNUSED int argc, char*argv[]) {
00039
00040
00041 static const pa_sample_spec ss = {
00042 .format = PA_SAMPLE_S16LE,
00043 .rate = 44100,
00044 .channels = 2
00045 };
00046
00047 pa_simple *s = NULL;
00048 int ret = 1;
00049 int error;
00050
00051
00052 if (argc > 1) {
00053 int fd;
00054
00055 if ((fd = open(argv[1], O_RDONLY)) < 0) {
00056 fprintf(stderr, __FILE__": open() failed: %s\n", strerror(errno));
00057 goto finish;
00058 }
00059
00060 if (dup2(fd, STDIN_FILENO) < 0) {
00061 fprintf(stderr, __FILE__": dup2() failed: %s\n", strerror(errno));
00062 goto finish;
00063 }
00064
00065 close(fd);
00066 }
00067
00068
00069 if (!(s = pa_simple_new(NULL, argv[0], PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &error))) {
00070 fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
00071 goto finish;
00072 }
00073
00074 for (;;) {
00075 uint8_t buf[BUFSIZE];
00076 ssize_t r;
00077
00078 #if 0
00079 pa_usec_t latency;
00080
00081 if ((latency = pa_simple_get_latency(s, &error)) == (pa_usec_t) -1) {
00082 fprintf(stderr, __FILE__": pa_simple_get_latency() failed: %s\n", pa_strerror(error));
00083 goto finish;
00084 }
00085
00086 fprintf(stderr, "%0.0f usec \r", (float)latency);
00087 #endif
00088
00089
00090 if ((r = read(STDIN_FILENO, buf, sizeof(buf))) <= 0) {
00091 if (r == 0)
00092 break;
00093
00094 fprintf(stderr, __FILE__": read() failed: %s\n", strerror(errno));
00095 goto finish;
00096 }
00097
00098
00099 if (pa_simple_write(s, buf, r, &error) < 0) {
00100 fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error));
00101 goto finish;
00102 }
00103 }
00104
00105
00106 if (pa_simple_drain(s, &error) < 0) {
00107 fprintf(stderr, __FILE__": pa_simple_drain() failed: %s\n", pa_strerror(error));
00108 goto finish;
00109 }
00110
00111 ret = 0;
00112
00113 finish:
00114
00115 if (s)
00116 pa_simple_free(s);
00117
00118 return ret;
00119 }