]>
Commit | Line | Data |
---|---|---|
0129db08 JL |
1 | /* |
2 | * pipe output driver. This file is part of Shairport. | |
3 | * Copyright (c) James Laird 2013 | |
4 | * All rights reserved. | |
5 | * | |
018537e8 MB |
6 | * Modifications for audio synchronisation |
7 | * and related work, copyright (c) Mike Brady 2014 | |
8 | * All rights reserved. | |
9 | * | |
0129db08 JL |
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 | ||
064bd293 MB |
31 | #include "audio.h" |
32 | #include "common.h" | |
33 | #include <errno.h> | |
0129db08 JL |
34 | #include <fcntl.h> |
35 | #include <memory.h> | |
064bd293 | 36 | #include <stdio.h> |
2e27c885 | 37 | #include <stdlib.h> |
0cc29d63 | 38 | #include <sys/stat.h> |
064bd293 MB |
39 | #include <sys/types.h> |
40 | #include <unistd.h> | |
0129db08 | 41 | |
28a5afc9 | 42 | static int fd = -1; |
0129db08 JL |
43 | |
44 | char *pipename = NULL; | |
ec0bc81b | 45 | int warned = 0; |
0129db08 | 46 | |
0cd8a2ce MB |
47 | static void start(__attribute__((unused)) int sample_rate, |
48 | __attribute__((unused)) int sample_format) { | |
fdbf64e9 MB |
49 | // this will leave fd as -1 if a reader hasn't been attached |
50 | fd = open(pipename, O_WRONLY | O_NONBLOCK); | |
e513e533 MB |
51 | if ((fd < -1) && (warned == 0)) { |
52 | warn("Error %d opening the pipe named \"%s\".", errno, pipename); | |
ec0bc81b MB |
53 | warned = 1; |
54 | } | |
0129db08 JL |
55 | } |
56 | ||
923e7f67 | 57 | static void play(void *buf, int samples) { |
fdbf64e9 | 58 | // if the file is not open, try to open it. |
ec0bc81b | 59 | char errorstring[1024]; |
fdbf64e9 | 60 | if (fd == -1) { |
064bd293 | 61 | fd = open(pipename, O_WRONLY | O_NONBLOCK); |
fdbf64e9 MB |
62 | } |
63 | // if it's got a reader, write to it. | |
ec0bc81b MB |
64 | if (fd > 0) { |
65 | int rc = non_blocking_write(fd, buf, samples * 4); | |
e513e533 MB |
66 | if ((rc < 0) && (warned == 0)) { |
67 | strerror_r(errno, (char *)errorstring, 1024); | |
68 | warn("Error %d writing to the pipe named \"%s\": \"%s\".", errno, pipename, errorstring); | |
ec0bc81b MB |
69 | warned = 1; |
70 | } | |
e513e533 MB |
71 | } else if ((fd == -1) && (warned == 0)) { |
72 | strerror_r(errno, (char *)errorstring, 1024); | |
73 | warn("Error %d opening the pipe named \"%s\": \"%s\".", errno, pipename, errorstring); | |
ec0bc81b | 74 | warned = 1; |
e513e533 | 75 | } |
fdbf64e9 | 76 | } |
0129db08 JL |
77 | |
78 | static void stop(void) { | |
064bd293 MB |
79 | // Don't close the pipe just because a play session has stopped. |
80 | // if (fd > 0) | |
81 | // close(fd); | |
0129db08 JL |
82 | } |
83 | ||
54250f75 | 84 | static int init(int argc, char **argv) { |
fdbf64e9 | 85 | debug(1, "pipe init"); |
c36a7822 MB |
86 | // const char *str; |
87 | // int value; | |
88 | // double dvalue; | |
ae84366e | 89 | |
b7864b4e MB |
90 | // set up default values first |
91 | ||
ae84366e | 92 | config.audio_backend_buffer_desired_length = 1.0; |
54250f75 | 93 | config.audio_backend_latency_offset = 0; |
e513e533 | 94 | |
b7864b4e MB |
95 | // do the "general" audio options. Note, these options are in the "general" stanza! |
96 | parse_general_audio_options(); | |
0129db08 | 97 | |
87a0475c MB |
98 | if (config.cfg != NULL) { |
99 | /* Get the Output Pipename. */ | |
100 | const char *str; | |
101 | if (config_lookup_string(config.cfg, "pipe.name", &str)) { | |
102 | pipename = (char *)str; | |
103 | } | |
e513e533 | 104 | |
fdbf64e9 MB |
105 | if ((pipename) && (strcasecmp(pipename, "STDOUT") == 0)) |
106 | die("Can't use \"pipe\" backend for STDOUT. Use the \"stdout\" backend instead."); | |
4cd3f5c3 | 107 | } |
b7864b4e | 108 | |
87a0475c MB |
109 | if ((pipename == NULL) && (argc != 1)) |
110 | die("bad or missing argument(s) to pipe"); | |
4cd3f5c3 | 111 | |
87a0475c MB |
112 | if (argc == 1) |
113 | pipename = strdup(argv[0]); | |
064bd293 | 114 | |
87a0475c | 115 | // here, create the pipe |
fdbf64e9 MB |
116 | if (mkfifo(pipename, 0644) && errno != EEXIST) |
117 | die("Could not create output pipe \"%s\"", pipename); | |
4cd3f5c3 | 118 | |
87a0475c | 119 | debug(1, "Pipename is \"%s\"", pipename); |
0129db08 | 120 | |
87a0475c | 121 | return 0; |
0129db08 JL |
122 | } |
123 | ||
124 | static void deinit(void) { | |
064bd293 | 125 | if (fd > 0) |
87a0475c | 126 | close(fd); |
0129db08 JL |
127 | } |
128 | ||
064bd293 | 129 | static void help(void) { printf(" pipe takes 1 argument: the name of the FIFO to write to.\n"); } |
0129db08 | 130 | |
87a0475c MB |
131 | audio_output audio_pipe = {.name = "pipe", |
132 | .help = &help, | |
133 | .init = &init, | |
134 | .deinit = &deinit, | |
135 | .start = &start, | |
136 | .stop = &stop, | |
137 | .flush = NULL, | |
138 | .delay = NULL, | |
139 | .play = &play, | |
140 | .volume = NULL, | |
4c70223f MB |
141 | .parameters = NULL, |
142 | .mute = NULL}; |