]>
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 | * | |
6 | * Permission is hereby granted, free of charge, to any person | |
7 | * obtaining a copy of this software and associated documentation | |
8 | * files (the "Software"), to deal in the Software without | |
9 | * restriction, including without limitation the rights to use, | |
10 | * copy, modify, merge, publish, distribute, sublicense, and/or | |
11 | * sell copies of the Software, and to permit persons to whom the | |
12 | * Software is furnished to do so, subject to the following conditions: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be | |
15 | * included in all copies or substantial portions of the Software. | |
16 | * | |
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |
19 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
21 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
22 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
23 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
24 | * OTHER DEALINGS IN THE SOFTWARE. | |
25 | */ | |
26 | ||
27 | #include <stdio.h> | |
28 | #include <unistd.h> | |
29 | #include <fcntl.h> | |
30 | #include <memory.h> | |
2e27c885 | 31 | #include <stdlib.h> |
4cd3f5c3 | 32 | #include <errno.h> |
0129db08 JL |
33 | #include "common.h" |
34 | #include "audio.h" | |
35 | ||
28a5afc9 | 36 | static int fd = -1; |
0129db08 JL |
37 | |
38 | char *pipename = NULL; | |
39 | ||
40 | static void start(int sample_rate) { | |
4cd3f5c3 | 41 | debug(1,"Pipename to start is \"%s\"",pipename); |
5f009106 MB |
42 | if (strcasecmp(pipename,"STDOUT")==0) |
43 | fd = STDOUT_FILENO; | |
44 | else | |
45 | fd = open(pipename, O_WRONLY); | |
0129db08 JL |
46 | } |
47 | ||
48 | static void play(short buf[], int samples) { | |
2e27c885 | 49 | int ignore = write(fd, buf, samples*4); |
0129db08 JL |
50 | } |
51 | ||
52 | static void stop(void) { | |
5f009106 | 53 | if (fd!=STDOUT_FILENO) |
0129db08 JL |
54 | close(fd); |
55 | } | |
56 | ||
e4804a57 | 57 | static int init(int argc, char **argv, config_t *cfgp) { |
0129db08 | 58 | |
4cd3f5c3 MB |
59 | if (cfgp!=NULL) { |
60 | /* Get the Output Pipename. */ | |
61 | const char *str; | |
62 | if(config_lookup_string(cfgp, "pipe.name", &str)) { | |
63 | pipename = (char*)str; | |
64 | } | |
65 | } | |
66 | ||
67 | ||
68 | if ((pipename==NULL) && (argc != 1)) | |
69 | die("bad or missing argument(s) to pipe"); | |
70 | ||
71 | if (argc==1) | |
72 | pipename = strdup(argv[0]); | |
5f009106 MB |
73 | |
74 | ||
4cd3f5c3 | 75 | // here, create the pipe |
5f009106 MB |
76 | if (strcasecmp(pipename,"STDOUT")!=0) |
77 | if (mkfifo(pipename, 0644) && errno != EEXIST) | |
78 | die("Could not create metadata FIFO %s", pipename); | |
4cd3f5c3 MB |
79 | |
80 | ||
81 | debug(1,"Pipename is \"%s\"",pipename); | |
0129db08 JL |
82 | |
83 | // test open pipe so we error on startup if it's going to fail | |
84 | start(44100); | |
85 | stop(); | |
86 | ||
87 | return 0; | |
88 | } | |
89 | ||
90 | static void deinit(void) { | |
5f009106 | 91 | if ((fd > 0) && (fd!=STDOUT_FILENO)) |
0129db08 | 92 | close(fd); |
0129db08 JL |
93 | } |
94 | ||
95 | static void help(void) { | |
5f009106 | 96 | printf(" pipe takes 1 argument: the name of the FIFO to write to, which can be \"stdout\".\n"); |
0129db08 JL |
97 | } |
98 | ||
99 | audio_output audio_pipe = { | |
100 | .name = "pipe", | |
101 | .help = &help, | |
102 | .init = &init, | |
103 | .deinit = &deinit, | |
104 | .start = &start, | |
105 | .stop = &stop, | |
28a5afc9 MB |
106 | .flush = NULL, |
107 | .delay = NULL, | |
0129db08 | 108 | .play = &play, |
a2790fc1 MB |
109 | .volume = NULL, |
110 | .parameters = NULL | |
0129db08 | 111 | }; |