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/polypaudio.h>
38 #include <polyp/mainloop.h>
39 #include <polyp/mainloop-signal.h>
41 #if PA_API_VERSION != 9
42 #error Invalid Polypaudio API version
45 static pa_context
*context
= NULL
;
46 static pa_stream
*stream
= NULL
;
47 static pa_mainloop_api
*mainloop_api
= NULL
;
49 static char *stream_name
= NULL
, *client_name
= NULL
, *device
= NULL
;
51 static int verbose
= 0;
52 static pa_volume_t volume
= PA_VOLUME_NORM
;
54 static SNDFILE
* sndfile
= NULL
;
56 static pa_sample_spec sample_spec
= { 0, 0, 0 };
57 static pa_channel_map channel_map
;
58 static int channel_map_set
= 0;
60 static sf_count_t (*readf_function
)(SNDFILE
*_sndfile
, void *ptr
, sf_count_t frames
) = NULL
;
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
) {
108 data
= malloc(length
);
110 if (readf_function
) {
111 size_t k
= pa_frame_size(&sample_spec
);
113 if ((bytes
= readf_function(sndfile
, data
, length
/k
)) > 0)
117 bytes
= sf_read_raw(sndfile
, data
, length
);
120 pa_stream_write(s
, data
, bytes
, free
, 0, PA_SEEK_RELATIVE
);
124 if (bytes
< length
) {
127 pa_operation_unref(pa_stream_drain(s
, stream_drain_complete
, NULL
));
131 /* This routine is called whenever the stream state changes */
132 static void stream_state_callback(pa_stream
*s
, void *userdata
) {
135 switch (pa_stream_get_state(s
)) {
136 case PA_STREAM_CREATING
:
137 case PA_STREAM_TERMINATED
:
140 case PA_STREAM_READY
:
142 fprintf(stderr
, "Stream successfully created\n");
145 case PA_STREAM_FAILED
:
147 fprintf(stderr
, "Stream errror: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s
))));
152 /* This is called whenever the context status changes */
153 static void context_state_callback(pa_context
*c
, void *userdata
) {
156 switch (pa_context_get_state(c
)) {
157 case PA_CONTEXT_CONNECTING
:
158 case PA_CONTEXT_AUTHORIZING
:
159 case PA_CONTEXT_SETTING_NAME
:
162 case PA_CONTEXT_READY
: {
165 assert(c
&& !stream
);
168 fprintf(stderr
, "Connection established.\n");
170 stream
= pa_stream_new(c
, stream_name
, &sample_spec
, channel_map_set
? &channel_map
: NULL
);
173 pa_stream_set_state_callback(stream
, stream_state_callback
, NULL
);
174 pa_stream_set_write_callback(stream
, stream_write_callback
, NULL
);
175 pa_stream_connect_playback(stream
, device
, NULL
, 0, pa_cvolume_set(&cv
, sample_spec
.channels
, volume
), NULL
);
180 case PA_CONTEXT_TERMINATED
:
184 case PA_CONTEXT_FAILED
:
186 fprintf(stderr
, "Connection failure: %s\n", pa_strerror(pa_context_errno(c
)));
191 /* UNIX signal to quit recieved */
192 static void exit_signal_callback(pa_mainloop_api
*m
, pa_signal_event
*e
, int sig
, void *userdata
) {
194 fprintf(stderr
, "Got SIGINT, exiting.\n");
199 static void help(const char *argv0
) {
201 printf("%s [options] [FILE]\n\n"
202 " -h, --help Show this help\n"
203 " --version Show version\n\n"
204 " -v, --verbose Enable verbose operations\n\n"
205 " -s, --server=SERVER The name of the server to connect to\n"
206 " -d, --device=DEVICE The name of the sink/source to connect to\n"
207 " -n, --client-name=NAME How to call this client on the server\n"
208 " --stream-name=NAME How to call this stream on the server\n"
209 " --volume=VOLUME Specify the initial (linear) volume in range 0...65536\n"
210 " --channel-map=CHANNELMAP Set the channel map to the use\n",
221 int main(int argc
, char *argv
[]) {
222 pa_mainloop
* m
= NULL
;
224 char *bn
, *server
= NULL
;
225 const char *filename
;
228 static const struct option long_options
[] = {
229 {"device", 1, NULL
, 'd'},
230 {"server", 1, NULL
, 's'},
231 {"client-name", 1, NULL
, 'n'},
232 {"stream-name", 1, NULL
, ARG_STREAM_NAME
},
233 {"version", 0, NULL
, ARG_VERSION
},
234 {"help", 0, NULL
, 'h'},
235 {"verbose", 0, NULL
, 'v'},
236 {"volume", 1, NULL
, ARG_VOLUME
},
237 {"channel-map", 1, NULL
, ARG_CHANNELMAP
},
241 if (!(bn
= strrchr(argv
[0], '/')))
246 while ((c
= getopt_long(argc
, argv
, "d:s:n:hv", long_options
, NULL
)) != -1) {
255 printf("paplay "PACKAGE_VERSION
"\nCompiled with libpolyp %s\nLinked with libpolyp %s\n", pa_get_headers_version(), pa_get_library_version());
261 device
= strdup(optarg
);
266 server
= strdup(optarg
);
271 client_name
= strdup(optarg
);
274 case ARG_STREAM_NAME
:
276 stream_name
= strdup(optarg
);
284 int v
= atoi(optarg
);
285 volume
= v
< 0 ? 0 : v
;
290 if (!pa_channel_map_parse(&channel_map
, optarg
)) {
291 fprintf(stderr
, "Invalid channel map\n");
303 filename
= optind
< argc
? argv
[optind
] : "STDIN";
305 memset(&sfinfo
, 0, sizeof(sfinfo
));
308 sndfile
= sf_open(filename
, SFM_READ
, &sfinfo
);
310 sndfile
= sf_open_fd(STDIN_FILENO
, SFM_READ
, &sfinfo
, 0);
313 fprintf(stderr
, "Failed to open file '%s'\n", filename
);
317 sample_spec
.rate
= sfinfo
.samplerate
;
318 sample_spec
.channels
= sfinfo
.channels
;
320 switch (sfinfo
.format
& 0xFF) {
321 case SF_FORMAT_PCM_16
:
322 case SF_FORMAT_PCM_U8
:
323 sample_spec
.format
= PA_SAMPLE_S16NE
;
324 readf_function
= (sf_count_t (*)(SNDFILE
*_sndfile
, void *ptr
, sf_count_t frames
)) sf_readf_short
;
328 sample_spec
.format
= PA_SAMPLE_ULAW
;
329 readf_function
= NULL
;
333 sample_spec
.format
= PA_SAMPLE_ALAW
;
334 readf_function
= NULL
;
337 case SF_FORMAT_FLOAT
:
339 sample_spec
.format
= PA_SAMPLE_FLOAT32NE
;
340 readf_function
= (sf_count_t (*)(SNDFILE
*_sndfile
, void *ptr
, sf_count_t frames
)) sf_readf_float
;
344 assert(pa_sample_spec_valid(&sample_spec
));
346 if (channel_map_set
&& channel_map
.channels
!= sample_spec
.channels
) {
347 fprintf(stderr
, "Channel map doesn't match file.\n");
352 client_name
= strdup(bn
);
355 const char *n
= sf_get_string(sndfile
, SF_STR_TITLE
);
356 stream_name
= strdup(n
? n
: filename
);
360 char t
[PA_SAMPLE_SPEC_SNPRINT_MAX
];
361 pa_sample_spec_snprint(t
, sizeof(t
), &sample_spec
);
362 fprintf(stderr
, "Using sample spec '%s'\n", t
);
365 /* Set up a new main loop */
366 if (!(m
= pa_mainloop_new())) {
367 fprintf(stderr
, "pa_mainloop_new() failed.\n");
371 mainloop_api
= pa_mainloop_get_api(m
);
373 r
= pa_signal_init(mainloop_api
);
375 pa_signal_new(SIGINT
, exit_signal_callback
, NULL
);
377 signal(SIGPIPE
, SIG_IGN
);
380 /* Create a new connection context */
381 if (!(context
= pa_context_new(mainloop_api
, client_name
))) {
382 fprintf(stderr
, "pa_context_new() failed.\n");
386 pa_context_set_state_callback(context
, context_state_callback
, NULL
);
388 /* Connect the context */
389 pa_context_connect(context
, server
, 0, NULL
);
391 /* Run the main loop */
392 if (pa_mainloop_run(m
, &ret
) < 0) {
393 fprintf(stderr
, "pa_mainloop_run() failed.\n");
399 pa_stream_unref(stream
);
402 pa_context_unref(context
);