2 * pipe output driver. This file is part of Shairport.
3 * Copyright (c) James Laird 2013
6 * Modifications for audio synchronisation
7 * and related work, copyright (c) Mike Brady 2014
10 * Permission is hereby granted, free of charge, to any person
11 * obtaining a copy of this software and associated documentation
12 * files (the "Software"), to deal in the Software without
13 * restriction, including without limitation the rights to use,
14 * copy, modify, merge, publish, distribute, sublicense, and/or
15 * sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
39 #include <sys/types.h>
44 char *pipename
= NULL
;
45 char *default_pipe_name
= "/tmp/shairport-sync-audio";
47 static void start(__attribute__((unused
)) int sample_rate
,
48 __attribute__((unused
)) int sample_format
) {
50 // this will leave fd as -1 if a reader hasn't been attached to the pipe
51 // we check that it's not a "real" error though. From the "man 2 open" page:
52 // "ENXIO O_NONBLOCK | O_WRONLY is set, the named file is a FIFO, and no process has the FIFO
55 fd
= try_to_open_pipe_for_writing(pipename
);
56 // we check that it's not a "real" error. From the "man 2 open" page:
57 // "ENXIO O_NONBLOCK | O_WRONLY is set, the named file is a FIFO, and no process has the FIFO
58 // open for reading." Which is okay.
59 if ((fd
== -1) && (errno
!= ENXIO
)) {
60 char errorstring
[1024];
61 strerror_r(errno
, (char *)errorstring
, sizeof(errorstring
));
62 debug(1, "audio_pipe start -- error %d (\"%s\") opening pipe: \"%s\".", errno
,
63 (char *)errorstring
, pipename
);
64 warn("can not open audio pipe -- error %d (\"%s\") opening pipe: \"%s\".", errno
,
65 (char *)errorstring
, pipename
);
69 static int play(void *buf
, int samples
, __attribute__((unused
)) int sample_type
,
70 __attribute__((unused
)) uint32_t timestamp
,
71 __attribute__((unused
)) uint64_t playtime
) {
72 // if the file is not open, try to open it.
73 char errorstring
[1024];
75 fd
= try_to_open_pipe_for_writing(pipename
);
77 // if it's got a reader, write to it.
79 // int rc = non_blocking_write(fd, buf, samples * 4);
80 int rc
= write(fd
, buf
, samples
* 4);
81 if ((rc
< 0) && (errno
!= EPIPE
)) {
82 strerror_r(errno
, (char *)errorstring
, 1024);
83 debug(1, "audio_pip play: error %d writing to the pipe named \"%s\": \"%s\".", errno
,
84 pipename
, errorstring
);
90 static void stop(void) {
91 // Don't close the pipe just because a play session has stopped.
94 static int init(int argc
, char **argv
) {
95 // debug(1, "pipe init");
100 // set up default values first
102 config
.audio_backend_buffer_desired_length
= 1.0;
103 config
.audio_backend_latency_offset
= 0;
105 // do the "general" audio options. Note, these options are in the "general" stanza!
106 parse_general_audio_options();
108 if (config
.cfg
!= NULL
) {
109 /* Get the Output Pipename. */
111 if (config_lookup_string(config
.cfg
, "pipe.name", &str
)) {
112 pipename
= (char *)str
;
117 die("too many command-line arguments to pipe");
120 pipename
= argv
[0]; // command line argument has priority
122 if ((pipename
) && (strcasecmp(pipename
, "STDOUT") == 0))
123 die("Can't use \"pipe\" backend for STDOUT. Use the \"stdout\" backend instead.");
125 if (pipename
== NULL
)
126 pipename
= default_pipe_name
; // if none specified
128 // here, create the pipe
129 mode_t oldumask
= umask(000);
130 if (mkfifo(pipename
, 0666) && errno
!= EEXIST
)
131 die("Could not create audio pipe \"%s\"", pipename
);
134 debug(1, "audio pipe name is \"%s\"", pipename
);
139 static void deinit(void) {
144 static void help(void) {
145 printf(" Provide the pipe's pathname. The default is \"%s\".\n", default_pipe_name
);
148 audio_output audio_pipe
= {.name
= "pipe",