-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathindex.js
175 lines (118 loc) · 3.94 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
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
'use strict';
// ----- Requires ----- //
let spawn = require('child_process').spawn;
let EventEmitter = require('events');
// ----- Setup ----- //
// The permitted audio outputs, local means via the 3.5mm jack.
let ALLOWED_OUTPUTS = ['hdmi', 'local', 'both', 'alsa'];
// ----- Functions ----- //
// Creates an array of arguments to pass to omxplayer.
function buildArgs (source, givenOutput, loop, initialVolume, showOsd) {
let output = '';
if (givenOutput) {
if (ALLOWED_OUTPUTS.indexOf(givenOutput) === -1) {
throw new Error(`Output ${givenOutput} not allowed.`);
}
output = givenOutput;
} else {
output = 'local';
}
let osd = false;
if (showOsd) {
osd = showOsd;
}
let args = [source, '-o', output, '--blank', osd ? '' : '--no-osd'];
// Handle the loop argument, if provided
if (loop) {
args.push('--loop');
}
// Handle the initial volume argument, if provided
if (Number.isInteger(initialVolume)) {
args.push('--vol', initialVolume);
}
return args;
}
// ----- Omx Class ----- //
function Omx (source, output, loop, initialVolume, showOsd) {
// ----- Local Vars ----- //
let omxplayer = new EventEmitter();
let player = null;
let open = false;
// ----- Local Functions ----- //
// Marks player as closed.
function updateStatus () {
open = false;
omxplayer.emit('close');
}
// Emits an error event, with a given message.
function emitError (message) {
open = false;
omxplayer.emit('error', message);
}
// Spawns the omxplayer process.
function spawnPlayer (src, out, loop, initialVolume, showOsd) {
let args = buildArgs(src, out, loop, initialVolume, showOsd);
console.log('args for omxplayer:', args);
let omxProcess = spawn('omxplayer', args);
open = true;
omxProcess.stdin.setEncoding('utf-8');
omxProcess.on('close', updateStatus);
omxProcess.on('error', () => {
emitError('Problem running omxplayer, is it installed?.');
});
return omxProcess;
}
// Simulates keypress to provide control.
function writeStdin (value) {
if (open) {
player.stdin.write(value);
} else {
throw new Error('Player is closed.');
}
}
// ----- Setup ----- //
if (source) {
player = spawnPlayer(source, output, loop, initialVolume, showOsd);
}
// ----- Methods ----- //
// Restarts omxplayer with a new source.
omxplayer.newSource = (src, out, loop, initialVolume, showOsd) => {
if (open) {
player.on('close', () => { player = spawnPlayer(src, out, loop, initialVolume, showOsd); });
player.removeListener('close', updateStatus);
writeStdin('q');
} else {
player = spawnPlayer(src, out, loop, initialVolume, showOsd);
}
};
omxplayer.play = () => { writeStdin('p'); };
omxplayer.pause = () => { writeStdin('p'); };
omxplayer.volUp = () => { writeStdin('+'); };
omxplayer.volDown = () => { writeStdin('-'); };
omxplayer.fastFwd = () => { writeStdin('>'); };
omxplayer.rewind = () => { writeStdin('<'); };
omxplayer.fwd30 =() => { writeStdin('\u001b[C'); };
omxplayer.back30 = () => { writeStdin('\u001b[D'); };
omxplayer.fwd600 = () => { writeStdin('\u001b[A'); };
omxplayer.back600 = () => { writeStdin('\u001b[B'); };
omxplayer.quit = () => { writeStdin('q'); };
omxplayer.subtitles = () => { writeStdin('s'); };
omxplayer.info = () => { writeStdin('z'); };
omxplayer.incSpeed = () => { writeStdin('1'); };
omxplayer.decSpeed = () => { writeStdin('2'); };
omxplayer.prevChapter = () => { writeStdin('i'); };
omxplayer.nextChapter = () => { writeStdin('o'); };
omxplayer.prevAudio = () => { writeStdin('j'); };
omxplayer.nextAudio = () => { writeStdin('k'); };
omxplayer.prevSubtitle = () => { writeStdin('n'); };
omxplayer.nextSubtitle = () => { writeStdin('m'); };
omxplayer.decSubDelay = () => { writeStdin('d'); };
omxplayer.incSubDelay = () => { writeStdin('f'); };
Object.defineProperty(omxplayer, 'running', {
get: () => { return open; }
});
// ----- Return Object ----- //
return omxplayer;
}
// ----- Module Exports ----- //
module.exports = Omx;