1 /*
2  * pipe output driver. This file is part of Shairport.
3  * Copyright (c) James Laird 2013
4  * All rights reserved.
5  *
6  * Modifications for audio synchronisation
7  * and related work, copyright (c) Mike Brady 2014
8  * All rights reserved.
9  *
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:
17  *
18  * The above copyright notice and this permission notice shall be
19  * included in all copies or substantial portions of the Software.
20  *
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.
29  */
30 
31 #include "audio.h"
32 #include "common.h"
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <memory.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40 #include <unistd.h>
41 
42 static int fd = -1;
43 
44 char *pipename = NULL;
45 char *default_pipe_name = "/tmp/shairport-sync-audio";
46 
start(int sample_rate,int sample_format)47 static void start(__attribute__((unused)) int sample_rate,
48                   __attribute__((unused)) int sample_format) {
49 
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
53   // open for reading."
54 
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);
66   }
67 }
68 
play(void * buf,int samples)69 static int play(void *buf, int samples) {
70   // if the file is not open, try to open it.
71   char errorstring[1024];
72   if (fd == -1) {
73     fd = try_to_open_pipe_for_writing(pipename);
74   }
75   // if it's got a reader, write to it.
76   if (fd > 0) {
77     // int rc = non_blocking_write(fd, buf, samples * 4);
78     int rc = write(fd, buf, samples * 4);
79     if ((rc < 0) && (errno != EPIPE)) {
80       strerror_r(errno, (char *)errorstring, 1024);
81       debug(1, "audio_pip play: error %d writing to the pipe named \"%s\": \"%s\".", errno,
82             pipename, errorstring);
83     }
84   }
85   return 0;
86 }
87 
stop(void)88 static void stop(void) {
89   // Don't close the pipe just because a play session has stopped.
90 }
91 
init(int argc,char ** argv)92 static int init(int argc, char **argv) {
93   //  debug(1, "pipe init");
94   //  const char *str;
95   //  int value;
96   //  double dvalue;
97 
98   // set up default values first
99 
100   config.audio_backend_buffer_desired_length = 1.0;
101   config.audio_backend_latency_offset = 0;
102 
103   // do the "general" audio  options. Note, these options are in the "general" stanza!
104   parse_general_audio_options();
105 
106   if (config.cfg != NULL) {
107     /* Get the Output Pipename. */
108     const char *str;
109     if (config_lookup_string(config.cfg, "pipe.name", &str)) {
110       pipename = (char *)str;
111     }
112   }
113 
114   if (argc > 1)
115     die("too many command-line arguments to pipe");
116 
117   if (argc == 1)
118     pipename = argv[0]; // command line argument has priority
119 
120   if ((pipename) && (strcasecmp(pipename, "STDOUT") == 0))
121     die("Can't use \"pipe\" backend for STDOUT. Use the \"stdout\" backend instead.");
122 
123   if (pipename == NULL)
124     pipename = default_pipe_name; // if none specified
125 
126   // here, create the pipe
127   mode_t oldumask = umask(000);
128   if (mkfifo(pipename, 0666) && errno != EEXIST)
129     die("Could not create audio pipe \"%s\"", pipename);
130   umask(oldumask);
131 
132   debug(1, "audio pipe name is \"%s\"", pipename);
133 
134   return 0;
135 }
136 
deinit(void)137 static void deinit(void) {
138   if (fd > 0)
139     close(fd);
140 }
141 
help(void)142 static void help(void) { printf("    specify the pathname of the pipe to write to.\n"); }
143 
144 audio_output audio_pipe = {.name = "pipe",
145                            .help = &help,
146                            .init = &init,
147                            .deinit = &deinit,
148                            .prepare = NULL,
149                            .start = &start,
150                            .stop = &stop,
151                            .is_running = NULL,
152                            .flush = NULL,
153                            .delay = NULL,
154                            .play = &play,
155                            .volume = NULL,
156                            .parameters = NULL,
157                            .mute = NULL};
158