4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
37 #include <polyp/polyplib.h>
38 #include <polyp/polyplib-error.h>
39 #include <polyp/mainloop.h>
40 #include <polyp/mainloop-signal.h>
41 #include <polyp/polyplib-version.h>
43 #if PA_API_VERSION != 8
44 #error Invalid Polypaudio API version
47 static pa_context
*context
= NULL
;
48 static pa_stream
*stream
= NULL
;
49 static pa_mainloop_api
*mainloop_api
= NULL
;
51 static char *stream_name
= NULL
, *client_name
= NULL
, *device
= NULL
;
53 static int verbose
= 0;
54 static pa_volume_t volume
= PA_VOLUME_NORM
;
56 static SNDFILE
* sndfile
= NULL
;
58 static pa_sample_spec sample_spec
= { 0, 0, 0 };
60 static sf_count_t (*readf_function
)(SNDFILE
*_sndfile
, void *ptr
, sf_count_t frames
);
62 /* A shortcut for terminating the application */
63 static void quit(int ret
) {
65 mainloop_api
->quit(mainloop_api
, ret
);
68 /* Connection draining complete */
69 static void context_drain_complete(pa_context
*c
, void *userdata
) {
70 pa_context_disconnect(c
);
73 /* Stream draining complete */
74 static void stream_drain_complete(pa_stream
*s
, int success
, void *userdata
) {
78 fprintf(stderr
, "Failed to drain stream: %s\n", pa_strerror(pa_context_errno(context
)));
83 fprintf(stderr
, "Playback stream drained.\n");
85 pa_stream_disconnect(stream
);
86 pa_stream_unref(stream
);
89 if (!(o
= pa_context_drain(context
, context_drain_complete
, NULL
)))
90 pa_context_disconnect(context
);
92 pa_operation_unref(o
);
95 fprintf(stderr
, "Draining connection to server.\n");
99 /* This is called whenever new data may be written to the stream */
100 static void stream_write_callback(pa_stream
*s
, size_t length
, void *userdata
) {
109 k
= pa_frame_size(&sample_spec
);
111 data
= malloc(length
);
115 f
= readf_function(sndfile
, data
, n
);
118 pa_stream_write(s
, data
, f
*k
, free
, 0);
123 pa_operation_unref(pa_stream_drain(s
, stream_drain_complete
, NULL
));
127 /* This routine is called whenever the stream state changes */
128 static void stream_state_callback(pa_stream
*s
, void *userdata
) {
131 switch (pa_stream_get_state(s
)) {
132 case PA_STREAM_CREATING
:
133 case PA_STREAM_TERMINATED
:
136 case PA_STREAM_READY
:
138 fprintf(stderr
, "Stream successfully created\n");
141 case PA_STREAM_FAILED
:
143 fprintf(stderr
, "Stream errror: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s
))));
148 /* This is called whenever the context status changes */
149 static void context_state_callback(pa_context
*c
, void *userdata
) {
152 switch (pa_context_get_state(c
)) {
153 case PA_CONTEXT_CONNECTING
:
154 case PA_CONTEXT_AUTHORIZING
:
155 case PA_CONTEXT_SETTING_NAME
:
158 case PA_CONTEXT_READY
: {
161 assert(c
&& !stream
);
164 fprintf(stderr
, "Connection established.\n");
166 stream
= pa_stream_new(c
, stream_name
, &sample_spec
, NULL
);
169 pa_stream_set_state_callback(stream
, stream_state_callback
, NULL
);
170 pa_stream_set_write_callback(stream
, stream_write_callback
, NULL
);
171 pa_stream_connect_playback(stream
, device
, NULL
, 0, pa_cvolume_set(&cv
, PA_CHANNELS_MAX
, volume
));
176 case PA_CONTEXT_TERMINATED
:
180 case PA_CONTEXT_FAILED
:
182 fprintf(stderr
, "Connection failure: %s\n", pa_strerror(pa_context_errno(c
)));
187 /* UNIX signal to quit recieved */
188 static void exit_signal_callback(pa_mainloop_api
*m
, pa_signal_event
*e
, int sig
, void *userdata
) {
190 fprintf(stderr
, "Got SIGINT, exiting.\n");
195 static void help(const char *argv0
) {
197 printf("%s [options] [FILE]\n\n"
198 " -h, --help Show this help\n"
199 " --version Show version\n\n"
200 " -v, --verbose Enable verbose operations\n\n"
201 " -s, --server=SERVER The name of the server to connect to\n"
202 " -d, --device=DEVICE The name of the sink/source to connect to\n"
203 " -n, --client-name=NAME How to call this client on the server\n"
204 " --stream-name=NAME How to call this stream on the server\n"
205 " --volume=VOLUME Specify the initial (linear) volume in range 0...256\n",
215 int main(int argc
, char *argv
[]) {
216 pa_mainloop
* m
= NULL
;
218 char *bn
, *server
= NULL
;
219 const char *filename
;
222 static const struct option long_options
[] = {
223 {"device", 1, NULL
, 'd'},
224 {"server", 1, NULL
, 's'},
225 {"client-name", 1, NULL
, 'n'},
226 {"stream-name", 1, NULL
, ARG_STREAM_NAME
},
227 {"version", 0, NULL
, ARG_VERSION
},
228 {"help", 0, NULL
, 'h'},
229 {"verbose", 0, NULL
, 'v'},
230 {"volume", 1, NULL
, ARG_VOLUME
},
234 if (!(bn
= strrchr(argv
[0], '/')))
239 while ((c
= getopt_long(argc
, argv
, "d:s:n:hv", long_options
, NULL
)) != -1) {
248 printf("paplay "PACKAGE_VERSION
"\nCompiled with libpolyp %s\nLinked with libpolyp %s\n", pa_get_headers_version(), pa_get_library_version());
254 device
= strdup(optarg
);
259 server
= strdup(optarg
);
264 client_name
= strdup(optarg
);
267 case ARG_STREAM_NAME
:
269 stream_name
= strdup(optarg
);
277 int v
= atoi(optarg
);
278 volume
= v
< 0 ? 0 : v
;
288 filename
= optind
< argc
? argv
[optind
] : "STDIN";
292 client_name
= strdup(bn
);
295 stream_name
= strdup(filename
);
297 memset(&sfinfo
, 0, sizeof(sfinfo
));
300 sndfile
= sf_open(filename
, SFM_READ
, &sfinfo
);
302 sndfile
= sf_open_fd(STDIN_FILENO
, SFM_READ
, &sfinfo
, 0);
305 fprintf(stderr
, "Failed to open file '%s'\n", filename
);
309 sample_spec
.rate
= sfinfo
.samplerate
;
310 sample_spec
.channels
= sfinfo
.channels
;
312 switch (sfinfo
.format
& 0xFF) {
313 case SF_FORMAT_PCM_16
:
314 case SF_FORMAT_PCM_U8
:
317 sample_spec
.format
= PA_SAMPLE_S16NE
;
318 readf_function
= (sf_count_t (*)(SNDFILE
*_sndfile
, void *ptr
, sf_count_t frames
)) sf_readf_short
;
320 case SF_FORMAT_FLOAT
:
322 sample_spec
.format
= PA_SAMPLE_FLOAT32NE
;
323 readf_function
= (sf_count_t (*)(SNDFILE
*_sndfile
, void *ptr
, sf_count_t frames
)) sf_readf_float
;
328 char t
[PA_SAMPLE_SPEC_SNPRINT_MAX
];
329 pa_sample_spec_snprint(t
, sizeof(t
), &sample_spec
);
330 fprintf(stderr
, "Using sample spec '%s'\n", t
);
333 /* Set up a new main loop */
334 if (!(m
= pa_mainloop_new())) {
335 fprintf(stderr
, "pa_mainloop_new() failed.\n");
339 mainloop_api
= pa_mainloop_get_api(m
);
341 r
= pa_signal_init(mainloop_api
);
343 pa_signal_new(SIGINT
, exit_signal_callback
, NULL
);
345 signal(SIGPIPE
, SIG_IGN
);
348 /* Create a new connection context */
349 if (!(context
= pa_context_new(mainloop_api
, client_name
))) {
350 fprintf(stderr
, "pa_context_new() failed.\n");
354 pa_context_set_state_callback(context
, context_state_callback
, NULL
);
356 /* Connect the context */
357 pa_context_connect(context
, server
, 1, NULL
);
359 /* Run the main loop */
360 if (pa_mainloop_run(m
, &ret
) < 0) {
361 fprintf(stderr
, "pa_mainloop_run() failed.\n");
367 pa_stream_unref(stream
);
370 pa_context_unref(context
);