-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathremote.js
157 lines (115 loc) · 3.93 KB
/
remote.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
"use strict";
const net = require('net');
const {EventEmitter} = require('events');
const defer = require('nyks/promise/defer');
const isWin = process.platform === "win32";
class Remote extends EventEmitter {
constructor(port = 8088, host = "127.0.0.1") {
super();
this.playlist = [];
this._port = port;
this._host = host;
}
info() { return this._send("info"); }
stop() { return this._send("stop"); }
pause() { return this._send("pause"); }
fullscreen(state) { return this._send("fullscreen " + state || ""); }
playlist_info() { return this._send("playlist"); }
toggle_play() { return this._send("play"); }
next() { return this._send("next"); }
prev() { return this._send("prev"); }
repeat(state) { return this._send("repeat " + state || ""); }
loop(state) { return this._send("loop " + state || ""); }
clear_playlist() { return this._send("clear"); }
status() { return this._send("status"); }
stats() { return this._send("stats"); }
get_time() { return this._send("get_time"); }
is_playing() { return this._send("is_playing"); }
get_title() { return this._send("get_title"); }
snapshot() { return this._send("snapshot"); }
vratio(ratio) { return this._send("vratio " + ratio || ""); }
screenRecorder() { return this._send("add screen://"); }
shutdown() { return this._send(isWin ? "quit" : "shutdown"); }
/**
* @description set/get volume
* @param {number} vol
* @returns volume
*/
volume(vol) {
if(vol)
return this._send("volume " + vol);
return this._send("volume");
}
async getLength() {
//first call to get_length always return 0 ??
let length = await this._send("get_length\r\nget_length");
return Number(length.split("\n")[1]);
}
async playonce(file) {
var meta_delay = 1000; //time to wait for metadata to be ready
if(Array.isArray(file))
file = file[0];
if(this.firstTimeout)
clearTimeout(this.firstTimeout);
if(this.secondTimeout)
clearTimeout(this.secondTimeout);
await this._send("add " + file);
var dateStart = Date.now();
return new Promise((resolve, reject) => {
this.firstTimeout = setTimeout(async () => {
let length = await this.getLength().catch(reject);
var videoRemainingTime = length * 1000 - (Date.now() - dateStart);
this.secondTimeout = setTimeout(() => {
this.play(this.playlist).then(resolve);
}, videoRemainingTime - 500);
}, meta_delay);
});
}
enqueue(files) {
if(typeof files == "string")
files = [files];
if(!files.length)
return;
var verbs = files.map(file => `enqueue ${file}`).join("\r\n");
return this._send(verbs);
}
async play(files = []) {
if(typeof files == "string")
files = [files];
if(!files.length)
return this.stop();
this.playlist = [...files];//clone it
await this._send("clear\r\nadd " + files.shift());
await this.enqueue(files);
}
_send(str) {
// it's best to re-connect everytime rather than keeping the socket opened
let defered = defer();
var dst = { port : this._port, host : this._host};
var sock = net.connect(dst, function() {
sock.setNoDelay();
var body = "";
sock.on("data", function(buf) {
body += buf;
});
sock.once("end", function() {
//trim vlc verbosity
body = body.replace(/status change:.*\r?\n/g, '');
body = body.replace(/^[\s\S]*?>/, '');
body = body.replace(/> Bye-bye!.*/, '');
defered.resolve(body.trim());
});
try {
sock.write(str + "\r\n", function() {
sock.end();
});
} catch(e) {
/* istanbul ignore next */
defered.reject(e);
}
});
sock.on("error", defered.reject);
return defered;
}
}
module.exports = Remote;