00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifdef HAVE_CONFIG_H
00026 #include <config.h>
00027 #endif
00028
00029 #include <signal.h>
00030 #include <string.h>
00031 #include <errno.h>
00032 #include <unistd.h>
00033 #include <assert.h>
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036 #include <getopt.h>
00037 #include <locale.h>
00038
00039 #include <sndfile.h>
00040
00041 #include <pulse/pulseaudio.h>
00042
00043 #if PA_API_VERSION < 9
00044 #error Invalid PulseAudio API version
00045 #endif
00046
00047 static pa_context *context = NULL;
00048 static pa_stream *stream = NULL;
00049 static pa_mainloop_api *mainloop_api = NULL;
00050
00051 static char *stream_name = NULL, *client_name = NULL, *device = NULL;
00052
00053 static int verbose = 0;
00054 static pa_volume_t volume = PA_VOLUME_NORM;
00055
00056 static SNDFILE* sndfile = NULL;
00057
00058 static pa_sample_spec sample_spec = { 0, 0, 0 };
00059 static pa_channel_map channel_map;
00060 static int channel_map_set = 0;
00061
00062 static sf_count_t (*readf_function)(SNDFILE *_sndfile, void *ptr, sf_count_t frames) = NULL;
00063
00064
00065 static void quit(int ret) {
00066 assert(mainloop_api);
00067 mainloop_api->quit(mainloop_api, ret);
00068 }
00069
00070
00071 static void context_drain_complete(pa_context*c, void *userdata) {
00072 pa_context_disconnect(c);
00073 }
00074
00075
00076 static void stream_drain_complete(pa_stream*s, int success, void *userdata) {
00077 pa_operation *o;
00078
00079 if (!success) {
00080 fprintf(stderr, "Failed to drain stream: %s\n", pa_strerror(pa_context_errno(context)));
00081 quit(1);
00082 }
00083
00084 if (verbose)
00085 fprintf(stderr, "Playback stream drained.\n");
00086
00087 pa_stream_disconnect(stream);
00088 pa_stream_unref(stream);
00089 stream = NULL;
00090
00091 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
00092 pa_context_disconnect(context);
00093 else {
00094 pa_operation_unref(o);
00095
00096 if (verbose)
00097 fprintf(stderr, "Draining connection to server.\n");
00098 }
00099 }
00100
00101
00102 static void stream_write_callback(pa_stream *s, size_t length, void *userdata) {
00103 sf_count_t bytes;
00104 void *data;
00105 assert(s && length);
00106
00107 if (!sndfile)
00108 return;
00109
00110 data = pa_xmalloc(length);
00111
00112 if (readf_function) {
00113 size_t k = pa_frame_size(&sample_spec);
00114
00115 if ((bytes = readf_function(sndfile, data, length/k)) > 0)
00116 bytes *= k;
00117
00118 } else
00119 bytes = sf_read_raw(sndfile, data, length);
00120
00121 if (bytes > 0)
00122 pa_stream_write(s, data, bytes, pa_xfree, 0, PA_SEEK_RELATIVE);
00123 else
00124 pa_xfree(data);
00125
00126 if (bytes < length) {
00127 sf_close(sndfile);
00128 sndfile = NULL;
00129 pa_operation_unref(pa_stream_drain(s, stream_drain_complete, NULL));
00130 }
00131 }
00132
00133
00134 static void stream_state_callback(pa_stream *s, void *userdata) {
00135 assert(s);
00136
00137 switch (pa_stream_get_state(s)) {
00138 case PA_STREAM_CREATING:
00139 case PA_STREAM_TERMINATED:
00140 break;
00141
00142 case PA_STREAM_READY:
00143 if (verbose)
00144 fprintf(stderr, "Stream successfully created\n");
00145 break;
00146
00147 case PA_STREAM_FAILED:
00148 default:
00149 fprintf(stderr, "Stream errror: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
00150 quit(1);
00151 }
00152 }
00153
00154
00155 static void context_state_callback(pa_context *c, void *userdata) {
00156 assert(c);
00157
00158 switch (pa_context_get_state(c)) {
00159 case PA_CONTEXT_CONNECTING:
00160 case PA_CONTEXT_AUTHORIZING:
00161 case PA_CONTEXT_SETTING_NAME:
00162 break;
00163
00164 case PA_CONTEXT_READY: {
00165 pa_cvolume cv;
00166
00167 assert(c && !stream);
00168
00169 if (verbose)
00170 fprintf(stderr, "Connection established.\n");
00171
00172 stream = pa_stream_new(c, stream_name, &sample_spec, channel_map_set ? &channel_map : NULL);
00173 assert(stream);
00174
00175 pa_stream_set_state_callback(stream, stream_state_callback, NULL);
00176 pa_stream_set_write_callback(stream, stream_write_callback, NULL);
00177 pa_stream_connect_playback(stream, device, NULL, 0, pa_cvolume_set(&cv, sample_spec.channels, volume), NULL);
00178
00179 break;
00180 }
00181
00182 case PA_CONTEXT_TERMINATED:
00183 quit(0);
00184 break;
00185
00186 case PA_CONTEXT_FAILED:
00187 default:
00188 fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(c)));
00189 quit(1);
00190 }
00191 }
00192
00193
00194 static void exit_signal_callback(pa_mainloop_api*m, pa_signal_event *e, int sig, void *userdata) {
00195 if (verbose)
00196 fprintf(stderr, "Got SIGINT, exiting.\n");
00197 quit(0);
00198
00199 }
00200
00201 static void help(const char *argv0) {
00202
00203 printf("%s [options] [FILE]\n\n"
00204 " -h, --help Show this help\n"
00205 " --version Show version\n\n"
00206 " -v, --verbose Enable verbose operations\n\n"
00207 " -s, --server=SERVER The name of the server to connect to\n"
00208 " -d, --device=DEVICE The name of the sink/source to connect to\n"
00209 " -n, --client-name=NAME How to call this client on the server\n"
00210 " --stream-name=NAME How to call this stream on the server\n"
00211 " --volume=VOLUME Specify the initial (linear) volume in range 0...65536\n"
00212 " --channel-map=CHANNELMAP Set the channel map to the use\n",
00213 argv0);
00214 }
00215
00216 enum {
00217 ARG_VERSION = 256,
00218 ARG_STREAM_NAME,
00219 ARG_VOLUME,
00220 ARG_CHANNELMAP
00221 };
00222
00223 int main(int argc, char *argv[]) {
00224 pa_mainloop* m = NULL;
00225 int ret = 1, r, c;
00226 char *bn, *server = NULL;
00227 const char *filename;
00228 SF_INFO sfinfo;
00229
00230 static const struct option long_options[] = {
00231 {"device", 1, NULL, 'd'},
00232 {"server", 1, NULL, 's'},
00233 {"client-name", 1, NULL, 'n'},
00234 {"stream-name", 1, NULL, ARG_STREAM_NAME},
00235 {"version", 0, NULL, ARG_VERSION},
00236 {"help", 0, NULL, 'h'},
00237 {"verbose", 0, NULL, 'v'},
00238 {"volume", 1, NULL, ARG_VOLUME},
00239 {"channel-map", 1, NULL, ARG_CHANNELMAP},
00240 {NULL, 0, NULL, 0}
00241 };
00242
00243 setlocale(LC_ALL, "");
00244
00245 if (!(bn = strrchr(argv[0], '/')))
00246 bn = argv[0];
00247 else
00248 bn++;
00249
00250 while ((c = getopt_long(argc, argv, "d:s:n:hv", long_options, NULL)) != -1) {
00251
00252 switch (c) {
00253 case 'h' :
00254 help(bn);
00255 ret = 0;
00256 goto quit;
00257
00258 case ARG_VERSION:
00259 printf("paplay "PACKAGE_VERSION"\nCompiled with libpulse %s\nLinked with libpulse %s\n", pa_get_headers_version(), pa_get_library_version());
00260 ret = 0;
00261 goto quit;
00262
00263 case 'd':
00264 pa_xfree(device);
00265 device = pa_xstrdup(optarg);
00266 break;
00267
00268 case 's':
00269 pa_xfree(server);
00270 server = pa_xstrdup(optarg);
00271 break;
00272
00273 case 'n':
00274 pa_xfree(client_name);
00275 client_name = pa_xstrdup(optarg);
00276 break;
00277
00278 case ARG_STREAM_NAME:
00279 pa_xfree(stream_name);
00280 stream_name = pa_xstrdup(optarg);
00281 break;
00282
00283 case 'v':
00284 verbose = 1;
00285 break;
00286
00287 case ARG_VOLUME: {
00288 int v = atoi(optarg);
00289 volume = v < 0 ? 0 : v;
00290 break;
00291 }
00292
00293 case ARG_CHANNELMAP:
00294 if (!pa_channel_map_parse(&channel_map, optarg)) {
00295 fprintf(stderr, "Invalid channel map\n");
00296 goto quit;
00297 }
00298
00299 channel_map_set = 1;
00300 break;
00301
00302 default:
00303 goto quit;
00304 }
00305 }
00306
00307 filename = optind < argc ? argv[optind] : "STDIN";
00308
00309 memset(&sfinfo, 0, sizeof(sfinfo));
00310
00311 if (optind < argc)
00312 sndfile = sf_open(filename, SFM_READ, &sfinfo);
00313 else
00314 sndfile = sf_open_fd(STDIN_FILENO, SFM_READ, &sfinfo, 0);
00315
00316 if (!sndfile) {
00317 fprintf(stderr, "Failed to open file '%s'\n", filename);
00318 goto quit;
00319 }
00320
00321 sample_spec.rate = sfinfo.samplerate;
00322 sample_spec.channels = sfinfo.channels;
00323
00324 readf_function = NULL;
00325
00326 switch (sfinfo.format & 0xFF) {
00327 case SF_FORMAT_PCM_16:
00328 case SF_FORMAT_PCM_U8:
00329 case SF_FORMAT_PCM_S8:
00330 sample_spec.format = PA_SAMPLE_S16NE;
00331 readf_function = (sf_count_t (*)(SNDFILE *_sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
00332 break;
00333
00334 case SF_FORMAT_ULAW:
00335 sample_spec.format = PA_SAMPLE_ULAW;
00336 break;
00337
00338 case SF_FORMAT_ALAW:
00339 sample_spec.format = PA_SAMPLE_ALAW;
00340 break;
00341
00342 case SF_FORMAT_FLOAT:
00343 case SF_FORMAT_DOUBLE:
00344 default:
00345 sample_spec.format = PA_SAMPLE_FLOAT32NE;
00346 readf_function = (sf_count_t (*)(SNDFILE *_sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
00347 break;
00348 }
00349
00350 assert(pa_sample_spec_valid(&sample_spec));
00351
00352 if (channel_map_set && channel_map.channels != sample_spec.channels) {
00353 fprintf(stderr, "Channel map doesn't match file.\n");
00354 goto quit;
00355 }
00356
00357 if (!client_name) {
00358 client_name = pa_locale_to_utf8(bn);
00359 if (!client_name)
00360 client_name = pa_utf8_filter(bn);
00361 }
00362
00363 if (!stream_name) {
00364 const char *n;
00365
00366 n = sf_get_string(sndfile, SF_STR_TITLE);
00367
00368 if (!n)
00369 n = filename;
00370
00371 stream_name = pa_locale_to_utf8(n);
00372 if (!stream_name)
00373 stream_name = pa_utf8_filter(n);
00374 }
00375
00376 if (verbose) {
00377 char t[PA_SAMPLE_SPEC_SNPRINT_MAX];
00378 pa_sample_spec_snprint(t, sizeof(t), &sample_spec);
00379 fprintf(stderr, "Using sample spec '%s'\n", t);
00380 }
00381
00382
00383 if (!(m = pa_mainloop_new())) {
00384 fprintf(stderr, "pa_mainloop_new() failed.\n");
00385 goto quit;
00386 }
00387
00388 mainloop_api = pa_mainloop_get_api(m);
00389
00390 r = pa_signal_init(mainloop_api);
00391 assert(r == 0);
00392 pa_signal_new(SIGINT, exit_signal_callback, NULL);
00393 #ifdef SIGPIPE
00394 signal(SIGPIPE, SIG_IGN);
00395 #endif
00396
00397
00398 if (!(context = pa_context_new(mainloop_api, client_name))) {
00399 fprintf(stderr, "pa_context_new() failed.\n");
00400 goto quit;
00401 }
00402
00403 pa_context_set_state_callback(context, context_state_callback, NULL);
00404
00405
00406 pa_context_connect(context, server, 0, NULL);
00407
00408
00409 if (pa_mainloop_run(m, &ret) < 0) {
00410 fprintf(stderr, "pa_mainloop_run() failed.\n");
00411 goto quit;
00412 }
00413
00414 quit:
00415 if (stream)
00416 pa_stream_unref(stream);
00417
00418 if (context)
00419 pa_context_unref(context);
00420
00421 if (m) {
00422 pa_signal_done();
00423 pa_mainloop_free(m);
00424 }
00425
00426 pa_xfree(server);
00427 pa_xfree(device);
00428 pa_xfree(client_name);
00429 pa_xfree(stream_name);
00430
00431 if (sndfile)
00432 sf_close(sndfile);
00433
00434 return ret;
00435 }