-
Notifications
You must be signed in to change notification settings - Fork 12
/
omxcontrol.js
executable file
·112 lines (102 loc) · 3.13 KB
/
omxcontrol.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var exec = require('child_process').exec;
var events = require('events');
var util = require('util');
var pipe = false;
var map = false;
var emitter = new events.EventEmitter();
var omx = function (mapper) {
map = mapper;
//events.EventEmitter.call(this);
};
//util.inherits(omx, events.EventEmitter);
omx.stop = function(cb) {
if (!pipe) {
if (cb) return cb();
}
console.info('killing omxplayer..');
exec('rm -f '+pipe, function (error, stdout, stderr) {
if (error !== null) console.error('rm exec error: ' + error);
pipe = false;
exec('killall omxplayer.bin', function () {
emitter.emit('stop');
if (cb) return cb();
});
});
};
omx.start = function(fn) {
if (!pipe) {
pipe = '/tmp/omxcontrol';
exec('rm -f ' + pipe, function (error, stdout, stderr) {
if (error !== null) {
console.error('rm exec error: ' + error);
} else {
exec('mkfifo ' + pipe, function (error, stdout, stderr) {
if (error !== null) {
console.error('mkfifo exec error: ' + error);
} else {
if (map) {
map(fn, cb);
} else {
cb(fn);
}
}
});
}
});
} else {
console.info("Pipe already exists! Restarting...");
omx.stop(function () {
return omx.start(fn);
});
}
function cb(fn) {
console.info(fn);
var insert = '';
if (fn.subtitle != null) {
insert = ' --align center --no-ghost-box --subtitles "' + fn.subtitle + '"';
}
exec('omxplayer -o hdmi --blank' + insert + ' "' + fn.input + '" < ' + pipe, function (error, stdout, stderr) {
if (error !== null) {
console.error('omxplayer exec error: ' + error);
emitter.emit('stop', error);
} else {
emitter.emit('complete');
}
});
omx.sendKey('.') // play
emitter.emit('start', fn);
}
};
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();
emitter.emit(command);
};
};
omx.mapKey('volume_up', '+');
omx.mapKey('volume_down', '-');
omx.mapKey('pause','p');
omx.mapKey('resume','p');
omx.mapKey('quit','q',function() {
omx.stop();
});
omx.mapKey('play','.');
omx.mapKey('forward',"\x5b\x43");
omx.mapKey('backward',"\x5b\x44");
omx.mapKey('next_subtitle', 'm');
omx.mapKey('previous_subtitle', 'n');
omx.mapKey('next_chapter', 'o');
omx.mapKey('previous_chapter', 'i');
omx.mapKey('next_audio', 'k');
omx.mapKey('previous_audio', 'j');
omx.mapKey('increase_speed', '1');
omx.mapKey('decrease_speed', '2');
module.exports = {
player: omx,
emitter: emitter
};