-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
113 lines (99 loc) · 2.38 KB
/
main.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
#include "scheduler.h"
#include "player.h"
#include "meta_handler.h"
#include "utils.h"
#include <unistd.h> /* For getopt() */
#include <signal.h> /* For sig_atomic_t and signal handling */
#include <stdlib.h> /* For strtol() */
#include <stdio.h> /* For perror() */
static struct player player = {0};
static const char * usage_str =
"Usage: %s [-s audio_sink_bin] [-d debug_level] [-m debug_mask] [-p port] <config_file>\n";
static void
signal_handler(int sig, siginfo_t * info, void *extra)
{
player_loop_quit (&player);
}
int
main(int argc, char **argv)
{
struct scheduler sched = {0};
struct sigaction sa = {0};
struct meta_handler mh = {0};
int ret = 0, opt, tmp;
int dbg_lvl = INFO;
int dbg_mask = PLR|SCHED|META;
uint16_t port = 9670;
char *sink = NULL;
while ((opt = getopt(argc, argv, "s:d:m:p:")) != -1) {
switch (opt) {
case 's':
sink = optarg;
break;
case 'd':
tmp = strtol(optarg, NULL, 10);
if (errno != 0)
perror("Failed to parse debug level");
else
dbg_lvl = tmp;
break;
case 'm':
tmp = strtol(optarg, NULL, 16);
if (errno != 0)
perror("Failed to parse debug mask");
else
dbg_mask = tmp;
break;
case 'p':
tmp = strtol(optarg, NULL, 10);
if (errno != 0)
perror("Failed to parse port number");
else
port = tmp;
break;
default:
printf(usage_str, argv[0]);
return(0);
}
}
if (optind >= argc) {
printf(usage_str, argv[0]);
return(0);
}
utils_set_log_level(dbg_lvl);
utils_set_debug_mask(dbg_mask);
ret = sched_init(&sched, argv[optind]);
if (ret < 0) {
utils_err(NONE, "Unable to initialize scheduler\n");
ret = -1;
goto cleanup;
}
ret = meta_handler_init(&mh, port, NULL);
if (ret < 0) {
utils_err(NONE, "Unable to initialize metadata request hanlder\n");
ret = -2;
goto cleanup;
}
ret = player_init(&player, &sched, &mh, sink);
if (ret < 0) {
utils_err(NONE, "Unable to initialize player\n");
ret = -3;
goto cleanup;
}
/* Install signal handler */
/* Install a signal handler for graceful exit */
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = signal_handler;
sigaction(SIGQUIT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
player_loop(&player);
utils_info(PLR, "Graceful exit...\n");
cleanup:
player_cleanup(&player);
sched_cleanup(&sched);
meta_handler_destroy(&mh);
return ret;
}