forked from mikebrady/shairport-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshairport.c
152 lines (136 loc) · 4.69 KB
/
shairport.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Shairport, an Apple Airplay receiver
* Copyright (c) James Laird 2013
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include <signal.h>
#include <unistd.h>
#include <memory.h>
#include "common.h"
#include "rtsp.h"
#include "mdns.h"
static int shutting_down = 0;
static void shutdown(void) {
if (shutting_down)
return;
shutting_down = 1;
printf("Shutting down...\n");
mdns_unregister();
rtsp_shutdown_stream();
config.output->deinit();
exit(0);
}
static void sig_ignore(int foo, siginfo_t *bar, void *baz) {
}
static void sig_shutdown(int foo, siginfo_t *bar, void *baz) {
shutdown();
}
void usage(char *progname) {
printf("Usage: %s [options...] [-- [output options...]]\n\n", progname);
printf("Available options:\n"
" -h show this help\n"
" -p port set RTSP listening port\n"
" -n name set advertised name\n"
" -o output set audio output\n"
" -s fill set how full the buffer must be before audio output starts\n"
" This value is in frames; default %d\n"
"Run %s -o <output> -h to find the available options for a specific output\n"
"\n", config.buffer_start_fill, progname);
if (config.output_name)
config.output = audio_get_output(config.output_name);
if (config.output) {
printf("Options for output %s:\n", config.output_name);
config.output->help();
} else {
audio_ls_outputs();
}
}
int parse_options(int argc, char **argv) {
char opt;
while ((opt = getopt(argc, argv, "+hp:n:o:s:")) > 0) {
switch (opt) {
default:
printf("Unknown argument -%c\n", optopt);
case 'h':
usage(argv[0]);
exit(1);
case 'v':
debuglev++;
break;
case 'p':
config.port = atoi(optarg);
break;
case 'n':
config.apname = optarg;
break;
case 'o':
config.output_name = optarg;
break;
case 's':
config.buffer_start_fill = atoi(optarg);
break;
}
}
return optind;
}
int main(int argc, char **argv) {
memset(&config, 0, sizeof(config));
// set defaults
config.buffer_start_fill = 220;
config.port = 9000;
char hostname[100];
gethostname(hostname, 100);
config.apname = malloc(20 + 100);
snprintf(config.apname, 20 + 100, "Shairport on %s", hostname);
int audio_arg = parse_options(argc, argv);
config.output = audio_get_output(config.output_name);
if (!config.output) {
audio_ls_outputs();
die("Invalid audio output specified!\n");
}
config.output->init(argc-audio_arg, argv+audio_arg);
// mask off all signals before creating threads.
// this way we control which thread gets which signals.
// for now, we don't care which thread gets the following.
sigset_t set;
sigfillset(&set);
sigdelset(&set, SIGINT);
sigdelset(&set, SIGTERM);
sigdelset(&set, SIGSTOP);
sigdelset(&set, SIGCHLD);
pthread_sigmask(SIG_BLOCK, &set, NULL);
// setting this to SIG_IGN would prevent signalling any threads.
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = &sig_ignore;
sigaction(SIGUSR1, &sa, NULL);
sa.sa_sigaction = &sig_shutdown;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGCHLD, &sa, NULL);
rtsp_listen_loop();
// should not.
shutdown();
return 1;
}