-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
124 lines (107 loc) · 2.66 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
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
var exec = require('child_process').exec;
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var defaults = ['--loop -o local'];
var STATES = {
PLAYING: 0,
PAUSED: 1,
IDLE: 2
};
var keys = {
decreaseSpeed: '1',
increaseSpeed: '2',
previousAudioStream: 'j',
nextAudioStream: 'k',
previousChapter: 'i',
nextChapter: 'o',
previousSubtitleStream: 'n',
nextSubtitleStream: 'm',
toggleSubtitles: 's',
decreaseSubtitleDelay: 'd',
increaseSubtitleDelay: 'f',
pause: 'p', // toggle between pause and play
stop: 'q',
decreaseVolume: '-',
increaseVolume: '+',
seekForward: "\x5b\x43",
seekBackward: "\x5b\x44",
seekFastForward: "\x5b\x41",
seekFastBackward: "\x5B\x42"
};
var omx = function() {
if (!(this instanceof omx)) {
return new omx();
}
this.state = STATES.IDLE;
};
util.inherits(omx, EventEmitter);
// start playing.. before make sure to
// shutdown any existing instance
omx.prototype.play = function(file, opts) {
// toggle between play and pause if no file
// was passed in.
if (!file) {
return this.pause();
}
// quit any existing instance
this.stop();
if (this.state === STATES.IDLE) {
return this.init(file, opts);
}
// init asap
this.once('ended', function() {
this.init(file, opts);
}.bind(this));
};
// Init omxplayer
omx.prototype.init = function(file, opts) {
var cmdOptions = (opts || defaults).join(' ');
this.player = exec('omxplayer '+cmdOptions+' "'+file+'"');
this.emit('playing', file);
this.state = STATES.PLAYING;
this.player.on('exit', function() {
this.state = STATES.IDLE;
this.player = null;
this.emit('ended', file);
}.bind(this));
};
// send a key command to omxplayer
omx.prototype.send = function(key) {
if (!this.player || this.state === STATES.IDLE) {
return;
}
this.player.stdin.write(key);
};
// check the current state
omx.prototype.getState = function() {
return this.state;
};
omx.prototype.stop = function() {
exec('pkill omxplayer');
this.state = STATES.IDLE;
console.log(this.state, STATES.IDLE);
};
// build some nice methods for interacting
// with the player
for (var method in keys) {
(function(key) {
omx.prototype[method] = function() {
this.send(key);
};
})(keys[method]);
}
omx.prototype.pause = function() {
if (this.state === STATES.IDLE) return;
this.state = (this.state === STATES.IDLE) ? STATES.PLAYING : STATES.PAUSED;
this.send('p');
};
// build some nice methods for interacting
// with the player
for (var method in keys) {
(function(key) {
omx.prototype[method] = function() {
this.send(key);
};
})(keys[method]);
}
module.exports = omx();