1 /*
2  * stdout output driver. This file is part of Shairport Sync.
3  * Copyright (c) Mike Brady 2015
4  *
5  * Based on pipe output driver
6  * Copyright (c) James Laird 2013
7  * All rights reserved.
8  *
9  * Permission is hereby granted, free of charge, to any person
10  * obtaining a copy of this software and associated documentation
11  * files (the "Software"), to deal in the Software without
12  * restriction, including without limitation the rights to use,
13  * copy, modify, merge, publish, distribute, sublicense, and/or
14  * sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27  * OTHER DEALINGS IN THE SOFTWARE.
28  */
29 
30 #include "audio.h"
31 #include "common.h"
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <memory.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 
39 static int fd = -1;
40 
start(int sample_rate,int sample_format)41 static void start(__attribute__((unused)) int sample_rate,
42                   __attribute__((unused)) int sample_format) {
43   fd = STDOUT_FILENO;
44 }
45 
play(void * buf,int samples)46 static int play(void *buf, int samples) {
47   char errorstring[1024];
48   int warned = 0;
49   int rc = write(fd, buf, samples * 4);
50   if ((rc < 0) && (warned == 0)) {
51     strerror_r(errno, (char *)errorstring, 1024);
52     warn("Error %d writing to stdout: \"%s\".", errno, errorstring);
53     warned = 1;
54   }
55   return rc;
56 }
57 
stop(void)58 static void stop(void) {
59   // Do nothing when play stops
60 }
61 
init(int argc,char ** argv)62 static int init(__attribute__((unused)) int argc, __attribute__((unused)) char **argv) {
63   // set up default values first
64   config.audio_backend_buffer_desired_length = 1.0;
65   config.audio_backend_latency_offset = 0;
66 
67   // get settings from settings file
68   // do the "general" audio  options. Note, these options are in the "general" stanza!
69   parse_general_audio_options();
70   return 0;
71 }
72 
deinit(void)73 static void deinit(void) {
74   // don't close stdout
75 }
76 
77 audio_output audio_stdout = {.name = "stdout",
78                              .help = NULL,
79                              .init = &init,
80                              .deinit = &deinit,
81                              .prepare = NULL,
82                              .start = &start,
83                              .stop = &stop,
84                              .is_running = NULL,
85                              .flush = NULL,
86                              .delay = NULL,
87                              .play = &play,
88                              .volume = NULL,
89                              .parameters = NULL,
90                              .mute = NULL};
91