-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
95 lines (85 loc) · 2.34 KB
/
index.js
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
var exec = require("child_process").execFile;
var parseurl = require("url");
var pipe = false;
var map = false;
var DEFAULT_PATH = "/omx";
var BINARY = "omxplayer";
function omx(mapper) {
map = mapper;
return omx.express;
}
omx.express = function (req, res, next) {
if (req.path.indexOf(DEFAULT_PATH) === 0) {
//replace + and decode
path = decodeURIComponent(req.path.replace(/\+/g, " "));
//remove leading and trailing /
path = path.replace(/^\/|\/$/g, "");
//split and remove leading path
var parts = path.split("/");
parts.shift();
var command = parts.shift();
console.log("executing", command, parts);
if (omx[command]) {
if (command === "start") {
omx.start(
parts.join("/") + "?" + parseurl.parse(req.url).query
);
} else {
omx[command].apply(this, parts);
}
//prevent anything else from being served from this subpath
res.end("executed " + command);
return;
}
}
next();
};
omx.start = function (fn, onFinish) {
if (!pipe) {
pipe = "omxcontrol";
exec("mkfifo", [pipe]);
}
if (map) {
map(fn, cb);
} else {
cb(fn);
}
function cb(fn) {
console.log(fn);
exec(
BINARY,
["-o", "hdmi", '"' + fn + '"', "<", pipe],
function (error, stdout, stderr) {
console.log(stdout);
if (error) {
console.log(error);
}
if (onFinish) {
onFinish();
}
}
);
exec("echo", [".", ">", pipe]);
}
};
omx.sendKey = function (key) {
if (!pipe) return;
exec("echo", ["-n", key, ">", pipe]);
};
omx.mapKey = function (command, key, then) {
omx[command] = function () {
omx.sendKey(key);
if (then) {
then();
}
};
};
omx.mapKey("pause", "p");
omx.mapKey("quit", "q", function () {
exec("rm", [pipe]);
pipe = false;
});
omx.mapKey("play", ".");
omx.mapKey("forward", "$'\\x1b\\x5b\\x43'");
omx.mapKey("backward", "$'\\x1b\\x5b\\x44'");
module.exports = omx;