-
Notifications
You must be signed in to change notification settings - Fork 19
/
omxd.c
419 lines (400 loc) · 9.27 KB
/
omxd.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/* (c) SZABO Gergely <[email protected]>, license GNU GPL v2 */
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include "omxd.h"
struct player *now = NULL;
struct player *next = NULL;
static void read_config(void);
static int daemon_running(void);
static int daemonize(void);
static int files(void);
static int read_fifo(char *line);
static int parse(char *line);
static void player(char *cmd, char **files);
static void stop_playback(struct player **this);
static void stop_all(void);
static char *get_output(char *cmd);
static void status_log(void);
static volatile char spinlock;
#define SPINLOCK_TAKE while (spinlock) {} spinlock = 1;
#define SPINLOCK_RELEASE spinlock = 0;
static char **next_hdmi_filter(char **files);
static int vol_mB = 0;
static char bg = 1;
static char gap = 0;
int main(int argc, char *argv[])
{
/* Help for -h */
if (argc == 2 && !strncmp(argv[1], "-h", 3)) {
writestr(1,
#include "omxd_help.h"
);
return 0;
}
/* Version for --version */
if (argc == 2 && !strncmp(argv[1], "--version", 10)) {
writestr(1,
#include "version.h"
);
return 0;
}
/* Config and options */
read_config();
int arg;
for (arg = 1; arg < argc; ++arg) {
if (!strncmp(argv[arg], "-d", 3))
loglevel = 1;
else if (!strncmp(argv[arg], "-n", 3))
bg = 0;
else if (!strncmp(argv[arg], "-g", 3))
gap = 1;
else
return client(argc, argv);
}
I_root = geteuid() == 0;
/* Exit if instance of daemon already running */
if (daemon_running()) {
printfd(2, "Daemon as uid %d already running\n", geteuid());
return -1;
}
/* Daemonize */
logfd = 2;
int daemon_error = bg ? daemonize() : 0;
if (daemon_error > 0)
return daemon_error;
else if (daemon_error < 0)
return 0;
/* Set up FIFO */
int files_error = files();
if (files_error)
return files_error;
/* Main loop */
status_log();
while (1) {
char line[LINE_LENGTH];
if (read_fifo(line))
LOG(0, "main: %s\n", line);
parse(line);
}
return 0;
}
/* Config file parsing */
static void read_config(void)
{
int cfg = open("/etc/omxd.conf", O_RDONLY);
if (cfg < 0)
return;
char buffer[4096];
if (read(cfg, buffer, 4096) == 0)
return;
char *start = buffer;
while (1) {
char *key = strtok(start, "=");
start = NULL;
char *val = strtok(start, "\n");
if (key == NULL || val == NULL)
return;
if (!strncmp(key, "debug", 6) && !strncmp(val, "1", 2))
loglevel = 1;
else if (!strncmp(key, "fg", 3) && !strncmp(val, "1", 2))
bg = 0;
else if (!strncmp(key, "gap", 4) && !strncmp(val, "1", 2))
gap = 1;
}
}
/* Check existing instance of omxd daemon running as current user */
static int daemon_running(void)
{
char cmd[1024] = "ps -Comxd -opid,ppid,uid,command | grep ' 1 *";
scatd(cmd, geteuid());
strcat(cmd, " ' | grep -v '^ *");
scatd(cmd, getpid());
strcat(cmd, " '");
int not_found = system(cmd);
return !not_found;
}
/* Fork, umask, SID, chdir, close, logfile, FIFO */
static int daemonize(void)
{
/* Fork the real daemon */
pid_t pid = fork();
if (pid < 0)
return 1;
if (pid > 0) {
int pidfd = creat(PID_FILE, 0644);
if (pidfd < 0 || printfd(pidfd, "%d", pid) == 0)
return 7;
close(pidfd);
printfd(1, "omxd daemon started, PID %d\n", pid);
return -1;
}
/* umask and session ID */
pid_t sid = setsid();
if (sid < 0)
return 2;
/* Create log file as stdout and stderr */
close(2);
struct stat st;
if (stat(LOG_FILE, &st) == 0)
logfd = open(LOG_FILE, O_WRONLY|O_APPEND, 0644);
else
logfd = creat(LOG_FILE, 0644);
close(0);
close(1);
if (logfd < 0)
return 4;
LOG(0, "daemonize: omxd started, SID %d\n", sid);
return 0;
}
/* CD, set up files */
static int files(void)
{
/* Run in /var/run if invoked as root, or allow testing in CWD */
if (I_root && chdir("/var/run/") < 0)
return 3;
/* Create and open FIFO for command input as stdin */
umask(0);
unlink("omxctl");
LOG(1, "files: Deleted original omxctl FIFO\n");
if (mknod("omxctl", S_IFIFO | 0622, 0) < 0)
return 6;
LOG(1, "files: Created new omxctl FIFO\n");
return 0;
}
/* Read a line from FIFO /var/run/omxctl */
static int read_fifo(char *line)
{
static int cmdfd = -1;
if (cmdfd < 0) {
cmdfd = open("omxctl", O_RDONLY);
if (cmdfd < 0) {
LOG(0, "read_fifo: Can't open omxctl\n");
return -1;
} else {
LOG(1, "read_fifo: Client opened omxctl\n");
}
}
int i = 0;
while (1) {
if (!read(cmdfd, line + i, 1)) {
LOG(1, "read_fifo: Client closed omxctl\n");
close(cmdfd);
cmdfd = -1;
line[i] = 0;
return i;
} else if (line[i] == '\n') {
LOG(1, "read_fifo: omxctl end of line\n");
line[i] = 0;
return i;
} else if (i == LINE_LENGTH - 2) {
LOG(0, "read_fifo: omxctl too long line\n");
i++;
line[i] = 0;
return i;
}
i++;
}
}
/* Get command char and file/URL name from a command line */
static int parse(char *line)
{
/* Extract command and file/URL from line */
char *cmd = NULL;
char *file = line;
while (1) {
if (*file == 0) {
if (cmd == NULL)
cmd = line;
break;
} else if (*file == ' ' || *file == '\t') {
if (cmd == NULL) {
*file = 0;
cmd = line;
}
} else if (cmd != NULL) {
break;
}
file++;
}
if (cmd != NULL && *cmd != 0) {
get_output(cmd);
player(cmd, next_hdmi_filter(m_list(cmd, file)));
}
if (cmd != NULL && *cmd == 'O')
player_add_opt(file);
return cmd != NULL ? *cmd : 0;
}
/* Control the actual omxplayer */
static void player(char *cmd, char **files)
{
SPINLOCK_TAKE
if (strchr(STOP_CMDS, *cmd) != NULL) {
LOG(1, "player: stop all\n");
stop_all();
goto player_end;
}
if (strchr(OMX_CMDS, *cmd) != NULL && now != NULL ) {
if (*cmd == 'p')
LOG(1, "player: play/pause\n")
else
LOG(1, "player: send %s\n", cmd)
player_cmd(now, cmd);
if (strchr(VOL_CMDS, *cmd) != NULL) {
if (*cmd == '-')
vol_mB -= 300;
else if (*cmd == '+')
vol_mB += 300;
player_set_vol(vol_mB);
if (next != NULL)
player_cmd(next, cmd);
}
status_log();
goto player_end;
}
if (files == NULL)
goto player_end;
/* Now/next: NULL = leave player alone; "" = destroy player */
if (files[0] != NULL) {
stop_playback(&now);
if (*files[0] != 0) {
LOG(1, "player: start %s\n", files[0]);
if (next != NULL &&
strcmp(files[0], player_file(next)) == 0) {
now = next;
next = NULL;
player_cmd(now, "p");
status_log();
} else {
now = player_new(files[0],
get_output(cmd),
P_PLAYING);
status_log();
}
}
}
if (files[1] != NULL) {
stop_playback(&next);
if (*files[1] != 0) {
LOG(1, "player: prime %s\n", files[1]);
next = player_new(files[1], get_output(cmd), P_PAUSED);
}
}
player_end:
SPINLOCK_RELEASE
}
/* Stop the playback immediately */
static void stop_playback(struct player **this)
{
if (*this != NULL) {
player_off(*this);
*this = NULL;
}
if (*this == now)
status_log();
}
static void stop_all(void)
{
player_killall();
if (now != NULL)
status_log();
now = NULL;
next = NULL;
}
/* Signal handler for SIGCHLD when player exits */
void quit_callback(struct player *this)
{
SPINLOCK_TAKE
if (this == next) {
next = NULL;
goto quit_callback_end;
}
if (this != now)
goto quit_callback_end;
int now_started = 0;
if (next != NULL) {
now = next;
next = NULL;
player_cmd(now, "p");
status_log();
now_started = 1;
}
char **now_next = next_hdmi_filter(m_list("n", NULL));
if (now_next == NULL)
goto quit_callback_end;
if (now_next[0] != NULL && *now_next[0] == 0) {
LOG(0, "quit_callback: nothing to play\n");
status_log();
}
if (now_next[0] != NULL && *now_next[0] && !now_started) {
LOG(0, "quit_callback: start %s\n", now_next[0]);
now = player_new(now_next[0], get_output("n"), P_PLAYING);
status_log();
}
if (now_next[1] != NULL && *now_next[1]) {
LOG(1, "quit_callback: prime %s\n", now_next[1]);
next = player_new(now_next[1], get_output("n"), P_PAUSED);
}
quit_callback_end:
SPINLOCK_RELEASE
}
/* Disable low-gap for videos when audio over HDMI */
static char **next_hdmi_filter(char **files)
{
static char *copy[2] = { NULL, NULL };
if (files == NULL)
return files;
if (files[1] == NULL)
return files;
if (!gap && strcmp("-olocal", get_output(NULL)) == 0)
return files;
copy[0] = files[0];
copy[1] = "";
LOG(1, "next_hdmi_filter: disable next\n");
return copy;
}
/* Return omxplayer argument to set output interface (Jack/HDMI) */
static char *get_output(char *cmd)
{
enum e_outputs { OUT_JACK, OUT_HDMI };
static char *outputs[] = { "-olocal", "-ohdmi" };
static enum e_outputs output = OUT_JACK;
enum e_outputs output_now;
if (cmd == NULL)
output_now = output;
else if (*cmd == 'h')
output_now = output = OUT_HDMI;
else if (*cmd == 'H')
output_now = OUT_HDMI;
else if (*cmd == 'j')
output_now = output = OUT_JACK;
else if (*cmd == 'J')
output_now = OUT_JACK;
else
output_now = output;
return outputs[output_now];
}
static void status_log(void)
{
unlink(STAT_FILE);
int fd = creat(STAT_FILE, 0644);
/* Format: timestamp state [dt logfile file] */
enum pstate state = player_state(now);
if (state == P_DEAD)
printfd(fd, "%d Stopped\n", time(NULL));
else
printfd(
fd,
"%d %s %d %s %s\n",
time(NULL),
state == P_PAUSED ? "Paused"
: "Playing",
player_dt(now),
player_logfile(now),
player_file(now)
);
close(fd);
}