4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
41 #include <pulse/pulseaudio.h>
43 #if PA_API_VERSION < 9
44 #error Invalid PulseAudio 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 };
59 static pa_channel_map channel_map
;
60 static int channel_map_set
= 0;
62 static sf_count_t (*readf_function
)(SNDFILE
*_sndfile
, void *ptr
, sf_count_t frames
) = NULL
;
64 /* A shortcut for terminating the application */
65 static void quit(int ret
) {
67 mainloop_api
->quit(mainloop_api
, ret
);
70 /* Connection draining complete */
71 static void context_drain_complete(pa_context
*c
, void *userdata
) {
72 pa_context_disconnect(c
);
75 /* Stream draining complete */
76 static void stream_drain_complete(pa_stream
*s
, int success
, void *userdata
) {
80 fprintf(stderr
, "Failed to drain stream: %s\n", pa_strerror(pa_context_errno(context
)));
85 fprintf(stderr
, "Playback stream drained.\n");
87 pa_stream_disconnect(stream
);
88 pa_stream_unref(stream
);
91 if (!(o
= pa_context_drain(context
, context_drain_complete
, NULL
)))
92 pa_context_disconnect(context
);
94 pa_operation_unref(o
);
97 fprintf(stderr
, "Draining connection to server.\n");
101 /* This is called whenever new data may be written to the stream */
102 static void stream_write_callback(pa_stream
*s
, size_t length
, void *userdata
) {
110 data
= pa_xmalloc(length
);
112 if (readf_function
) {
113 size_t k
= pa_frame_size(&sample_spec
);
115 if ((bytes
= readf_function(sndfile
, data
, length
/k
)) > 0)
119 bytes
= sf_read_raw(sndfile
, data
, length
);
122 pa_stream_write(s
, data
, bytes
, pa_xfree
, 0, PA_SEEK_RELATIVE
);
126 if (bytes
< length
) {
129 pa_operation_unref(pa_stream_drain(s
, stream_drain_complete
, NULL
));
133 /* This routine is called whenever the stream state changes */
134 static void stream_state_callback(pa_stream
*s
, void *userdata
) {
137 switch (pa_stream_get_state(s
)) {
138 case PA_STREAM_CREATING
:
139 case PA_STREAM_TERMINATED
:
142 case PA_STREAM_READY
:
144 fprintf(stderr
, "Stream successfully created\n");
147 case PA_STREAM_FAILED
:
149 fprintf(stderr
, "Stream errror: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s
))));
154 /* This is called whenever the context status changes */
155 static void context_state_callback(pa_context
*c
, void *userdata
) {
158 switch (pa_context_get_state(c
)) {
159 case PA_CONTEXT_CONNECTING
:
160 case PA_CONTEXT_AUTHORIZING
:
161 case PA_CONTEXT_SETTING_NAME
:
164 case PA_CONTEXT_READY
: {
167 assert(c
&& !stream
);
170 fprintf(stderr
, "Connection established.\n");
172 stream
= pa_stream_new(c
, stream_name
, &sample_spec
, channel_map_set
? &channel_map
: NULL
);
175 pa_stream_set_state_callback(stream
, stream_state_callback
, NULL
);
176 pa_stream_set_write_callback(stream
, stream_write_callback
, NULL
);
177 pa_stream_connect_playback(stream
, device
, NULL
, 0, pa_cvolume_set(&cv
, sample_spec
.channels
, volume
), NULL
);
182 case PA_CONTEXT_TERMINATED
:
186 case PA_CONTEXT_FAILED
:
188 fprintf(stderr
, "Connection failure: %s\n", pa_strerror(pa_context_errno(c
)));
193 /* UNIX signal to quit recieved */
194 static void exit_signal_callback(pa_mainloop_api
*m
, pa_signal_event
*e
, int sig
, void *userdata
) {
196 fprintf(stderr
, "Got SIGINT, exiting.\n");
201 static void help(const char *argv0
) {
203 printf("%s [options] [FILE]\n\n"
204 " -h, --help Show this help\n"
205 " --version Show version\n\n"
206 " -v, --verbose Enable verbose operations\n\n"
207 " -s, --server=SERVER The name of the server to connect to\n"
208 " -d, --device=DEVICE The name of the sink/source to connect to\n"
209 " -n, --client-name=NAME How to call this client on the server\n"
210 " --stream-name=NAME How to call this stream on the server\n"
211 " --volume=VOLUME Specify the initial (linear) volume in range 0...65536\n"
212 " --channel-map=CHANNELMAP Set the channel map to the use\n",
223 int main(int argc
, char *argv
[]) {
224 pa_mainloop
* m
= NULL
;
226 char *bn
, *server
= NULL
;
227 const char *filename
;
230 static const struct option long_options
[] = {
231 {"device", 1, NULL
, 'd'},
232 {"server", 1, NULL
, 's'},
233 {"client-name", 1, NULL
, 'n'},
234 {"stream-name", 1, NULL
, ARG_STREAM_NAME
},
235 {"version", 0, NULL
, ARG_VERSION
},
236 {"help", 0, NULL
, 'h'},
237 {"verbose", 0, NULL
, 'v'},
238 {"volume", 1, NULL
, ARG_VOLUME
},
239 {"channel-map", 1, NULL
, ARG_CHANNELMAP
},
243 setlocale(LC_ALL
, "");
245 if (!(bn
= strrchr(argv
[0], '/')))
250 while ((c
= getopt_long(argc
, argv
, "d:s:n:hv", long_options
, NULL
)) != -1) {
259 printf("paplay "PACKAGE_VERSION
"\nCompiled with libpulse %s\nLinked with libpulse %s\n", pa_get_headers_version(), pa_get_library_version());
265 device
= pa_xstrdup(optarg
);
270 server
= pa_xstrdup(optarg
);
274 pa_xfree(client_name
);
275 client_name
= pa_xstrdup(optarg
);
278 case ARG_STREAM_NAME
:
279 pa_xfree(stream_name
);
280 stream_name
= pa_xstrdup(optarg
);
288 int v
= atoi(optarg
);
289 volume
= v
< 0 ? 0 : v
;
294 if (!pa_channel_map_parse(&channel_map
, optarg
)) {
295 fprintf(stderr
, "Invalid channel map\n");
307 filename
= optind
< argc
? argv
[optind
] : "STDIN";
309 memset(&sfinfo
, 0, sizeof(sfinfo
));
312 sndfile
= sf_open(filename
, SFM_READ
, &sfinfo
);
314 sndfile
= sf_open_fd(STDIN_FILENO
, SFM_READ
, &sfinfo
, 0);
317 fprintf(stderr
, "Failed to open file '%s'\n", filename
);
321 sample_spec
.rate
= sfinfo
.samplerate
;
322 sample_spec
.channels
= sfinfo
.channels
;
324 readf_function
= NULL
;
326 switch (sfinfo
.format
& 0xFF) {
327 case SF_FORMAT_PCM_16
:
328 case SF_FORMAT_PCM_U8
:
329 case SF_FORMAT_PCM_S8
:
330 sample_spec
.format
= PA_SAMPLE_S16NE
;
331 readf_function
= (sf_count_t (*)(SNDFILE
*_sndfile
, void *ptr
, sf_count_t frames
)) sf_readf_short
;
335 sample_spec
.format
= PA_SAMPLE_ULAW
;
339 sample_spec
.format
= PA_SAMPLE_ALAW
;
342 case SF_FORMAT_FLOAT
:
343 case SF_FORMAT_DOUBLE
:
345 sample_spec
.format
= PA_SAMPLE_FLOAT32NE
;
346 readf_function
= (sf_count_t (*)(SNDFILE
*_sndfile
, void *ptr
, sf_count_t frames
)) sf_readf_float
;
350 assert(pa_sample_spec_valid(&sample_spec
));
352 if (channel_map_set
&& channel_map
.channels
!= sample_spec
.channels
) {
353 fprintf(stderr
, "Channel map doesn't match file.\n");
358 client_name
= pa_locale_to_utf8(bn
);
360 client_name
= pa_utf8_filter(bn
);
366 n
= sf_get_string(sndfile
, SF_STR_TITLE
);
371 stream_name
= pa_locale_to_utf8(n
);
373 stream_name
= pa_utf8_filter(n
);
377 char t
[PA_SAMPLE_SPEC_SNPRINT_MAX
];
378 pa_sample_spec_snprint(t
, sizeof(t
), &sample_spec
);
379 fprintf(stderr
, "Using sample spec '%s'\n", t
);
382 /* Set up a new main loop */
383 if (!(m
= pa_mainloop_new())) {
384 fprintf(stderr
, "pa_mainloop_new() failed.\n");
388 mainloop_api
= pa_mainloop_get_api(m
);
390 r
= pa_signal_init(mainloop_api
);
392 pa_signal_new(SIGINT
, exit_signal_callback
, NULL
);
394 signal(SIGPIPE
, SIG_IGN
);
397 /* Create a new connection context */
398 if (!(context
= pa_context_new(mainloop_api
, client_name
))) {
399 fprintf(stderr
, "pa_context_new() failed.\n");
403 pa_context_set_state_callback(context
, context_state_callback
, NULL
);
405 /* Connect the context */
406 pa_context_connect(context
, server
, 0, NULL
);
408 /* Run the main loop */
409 if (pa_mainloop_run(m
, &ret
) < 0) {
410 fprintf(stderr
, "pa_mainloop_run() failed.\n");
416 pa_stream_unref(stream
);
419 pa_context_unref(context
);
428 pa_xfree(client_name
);
429 pa_xfree(stream_name
);