forked from subogero/omxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.c
343 lines (317 loc) · 7.54 KB
/
player.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
/* (c) SZABO Gergely <szg@subogero.com>, license GNU GPL v2 */
#include "omxd.h"
#include <signal.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
static void player_quit(int signum);
static void watchdog(int signum);
static void init(void);
struct player {
pid_t pid;
int wpipe;
int t_lastcmd;
int dt;
enum pstate state;
char file[LINE_LENGTH];
char logfile[LINE_LENGTH];
};
#define NUM_PLAYERS 3
static struct player p[NUM_PLAYERS];
static struct player *find_free(void);
static struct player *find_pid(pid_t pid);
static void player_cleanup(struct player *this);
static char vol_sz[11] = "0";
static void init_opts(void);
static void log_opts(char *prefix);
/* opts.argc is the number of args, excluding the closing NULL */
static struct { int argc; char **argv; } opts =
{ -1, NULL, };
#define DT_WATCHDOG 5
static void init(void)
{
if (opts.argv == NULL) {
init_opts();
signal(SIGALRM, watchdog);
alarm(DT_WATCHDOG);
}
}
static void watchdog(int signum)
{
int i;
int loop = 0;
for (i = 0; i < opts.argc - 2; ++i) {
if (strncmp(opts.argv[i], "--loop", 7) == 0) {
loop = 1;
break;
}
}
for (i = 0; i < NUM_PLAYERS; ++i) {
int t_play = player_dt(p + i);
int t_len = player_length(p[i].logfile);
if (t_play == -1 || t_len <= 0 || t_play <= t_len || loop)
continue;
LOG(0, "watchdog: t_play = %d, t_len = %d\n", t_play, t_len);
char cmd[50] = { 0, };
strcpy(cmd, "/usr/bin/omxwd ");
scatd(cmd, p[i].pid);
system(cmd);
}
signal(SIGALRM, watchdog);
alarm(DT_WATCHDOG);
}
struct player *player_new(char *file, char *out, enum pstate state)
{
init();
if (file == NULL || *file == 0)
return NULL;
struct player *this = find_free();
if (this == NULL)
return NULL;
/* Timekeeping */
this->t_lastcmd = time(NULL);
this->dt = 0;
/* IPC, args */
int ctrlpipe[2];
pipe(ctrlpipe);
opts.argv[opts.argc - 2] = out;
opts.argv[opts.argc - 1] = file;
opts.argv[opts.argc - 0] = NULL;
strcpy(this->logfile, OMX_FILE);
this->pid = fork();
if (this->pid < 0) { /* Fork error */
this->pid = 0;
close(ctrlpipe[0]);
close(ctrlpipe[1]);
this->state = P_DEAD;
return NULL;
} else if (this->pid > 0) { /* Parent: set SIGCHLD handler */
close(ctrlpipe[0]);
this->wpipe = ctrlpipe[1];
if (state == P_PAUSED)
write(this->wpipe, "p", 1);
this->state = state;
signal(SIGCHLD, player_quit);
signal(SIGPIPE, SIG_IGN);
strcpy(this->file, file);
scatd(this->logfile, this->pid);
LOG(0, "player_new: PID=%d %s\n", this->pid, file);
return this;
} else { /* Child: exec omxplayer */
scatd(this->logfile, getpid());
dup2(creat(this->logfile, 0644), 2);
close(ctrlpipe[1]);
/* Redirect stdin 0 to read end of control pipe */
if (ctrlpipe[0] != 0) {
close(0);
dup(ctrlpipe[0]);
close(ctrlpipe[0]);
}
execve(opts.argv[0], opts.argv, NULL);
logfd = open(LOG_FILE, O_WRONLY|O_APPEND);
log_opts(strerror(errno));
_exit(20);
}
}
void player_cmd(struct player *this, char *cmd)
{
if (this == NULL || this->state == P_DEAD)
return;
if (strchr(OMX_CMDS, *cmd) == NULL)
return;
/* Timekeeping */
int t = time(NULL);
if (this->state == P_PLAYING)
this->dt += t - this->t_lastcmd;
this->t_lastcmd = t;
this->dt += *cmd == 'F' ? 600
: *cmd == 'R' ? -600
: *cmd == 'f' ? 30
: *cmd == 'r' ? -30
: 0;
if (this->dt < 0)
this->dt = 0;
if (*cmd == 'p')
this->state = this->state == P_PLAYING ? P_PAUSED : P_PLAYING;
/* Replace FRfr with arrow-key escape sequences */
if (*cmd == 'F')
cmd = "\033[A";
else if (*cmd == 'R')
cmd = "\033[B";
else if (*cmd == 'f')
cmd = "\033[C";
else if (*cmd == 'r')
cmd = "\033[D";
else if (*cmd == 'M')
cmd = "n";
else if (*cmd == 'K')
cmd = "j";
writestr(this->wpipe, cmd);
LOG(0, "player_cmd: Send %s to omxplayer PID/fd %d/%d\n",
cmd, this->pid, this->wpipe);
}
void player_off(struct player *this)
{
if (this == NULL || this->state == P_DEAD)
return;
int pid = this->pid;
LOG(0, "player_off: PID %d\n", pid);
player_cleanup(this);
char cmd[50] = { 0, };
strcpy(cmd, "/usr/bin/omxwd ");
scatd(cmd, pid);
system(cmd);
}
void player_killall(void)
{
LOG(0, "player_killall\n");
int i;
for (i = 0; i < NUM_PLAYERS; ++i)
player_cleanup(p + i);
system("/usr/bin/omxwd");
}
const char *player_file(struct player *this)
{
return this == NULL || this->state == P_DEAD
? NULL
: (const char*)this->file;
}
const char *player_logfile(struct player *this)
{
return this == NULL || this->state == P_DEAD
? NULL
: (const char*)this->logfile;
}
int player_dt(struct player *this)
{
if (this == NULL || this->state == P_DEAD)
return -1;
int t = time(NULL);
if (this->state == P_PLAYING)
this->dt += t - this->t_lastcmd;
this->t_lastcmd = t;
return this->dt;
}
enum pstate player_state(struct player *this)
{
return this == NULL ? P_DEAD : this->state;
}
void player_add_opt(char *opt)
{
init();
if (opt == NULL || *opt == 0) {
init_opts();
return;
}
opts.argc++;
int size = opts.argc + 1;
opts.argv = realloc(opts.argv, size * sizeof(char*));
int optsize = strlen(opt) + 1; /* Add 1 char for closing zero */
int pos_new_opt = size - 4; /* Before closing NULL, file and audio */
opts.argv[pos_new_opt] = malloc(optsize * sizeof(char));
strcpy(opts.argv[pos_new_opt], opt);
}
void player_set_vol(int vol_mB)
{
init();
vol_sz[0] = 0;
scatd(vol_sz, vol_mB);
if (opts.argv != NULL)
opts.argv[3] = vol_sz;
}
static void init_opts(void)
{
#define ARGC_FIXED 4
#define ARGC_DEFAULT (ARGC_FIXED + 2)
/* Free the entire argv array if necessary */
if (opts.argv != NULL) {
int i;
/* Only the added options are dynamically allocated */
for (i = ARGC_FIXED; i < opts.argc - 2; ++i) {
if (opts.argv[i] != NULL)
free(opts.argv[i]);
}
free(opts.argv);
opts.argv = NULL;
opts.argc = -1;
}
opts.argc = ARGC_DEFAULT;
int size = opts.argc + 1;
opts.argv = malloc(size * sizeof(char*));
opts.argv[0] = "/usr/bin/omxplayer";
opts.argv[1] = "-I";
opts.argv[2] = "--vol";
opts.argv[3] = vol_sz;
opts.argv[4] = NULL; /* Audio output option */
opts.argv[5] = NULL; /* File */
opts.argv[6] = NULL; /* Closing NULL pointer */
}
static void log_opts(char *prefix)
{
int i;
char msg[LINE_LENGTH];
strcpy(msg, prefix);
for (i = 0; i < opts.argc; ++i) {
strcat(msg, " ");
strcat(msg, opts.argv[i]);
}
LOG(0, msg);
}
static void player_quit(int signum)
{
while (1) {
int status;
pid_t pid = waitpid(0, &status, WNOHANG);
status = WEXITSTATUS(status);
/* pid 0: no processes to reap */
if (pid == 0)
return;
/* pid -1 error: retry if waitpid interrupted by new signal */
if (pid == -1) {
int err = errno;
LOG(0, "player_quit: %s\n", strerror(err));
if (err == EINTR)
continue;
return;
}
LOG(0, "player_quit: PID=%d with %d\n", pid, status);
/* Clean up player object exited by itself */
struct player *this = find_pid(pid);
if (this == NULL)
return;
if (this->state != P_DEAD) {
player_cleanup(this);
quit_callback(this);
}
}
}
static void player_cleanup(struct player *this)
{
if (this == NULL || this->state == P_DEAD)
return;
close(this->wpipe);
this->pid = 0;
this->file[0] = 0;
this->state = P_DEAD;
unlink(this->logfile);
this->logfile[0] = 0;
}
static struct player *find_free(void)
{
int i;
for (i = 0; i < NUM_PLAYERS; ++i)
if (p[i].state == P_DEAD)
return p + i;
return NULL;
}
static struct player *find_pid(pid_t pid)
{
int i;
for (i = 0; i < NUM_PLAYERS; ++i)
if (p[i].pid == pid)
return p + i;
return NULL;
}