-
Notifications
You must be signed in to change notification settings - Fork 5
/
player.js
154 lines (118 loc) · 3.32 KB
/
player.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
"use strict";
const isWin = (process.platform == 'win32');
const Remote = require('./remote');
const debug = require('debug')('vlc');
const map = require('mout/object/map');
const values = require('mout/object/values');
const mixIn = require('mout/object/mixIn');
const sleep = require('nyks/async/sleep');
const defer = require('nyks/promise/defer');
const vlc = require('vlc-player');
const heartbeat_interval = 1000;
const splitter = /input debug: `(.*)' successfully opened/;
var config = {
'ignore-config' : null,
'no-plugins-cache' : null,
'no-media-library' : null,
'config' : 'blank',
'no-video-title-show' : null,
'verbose' : 3,
'intf' : 'dummy',
'rc-host' : '127.0.0.1:8088',
'extraintf' : 'rc',
'loop' : null,
};
if(isWin) {
mixIn(config, {
'no-crashdump' : null,
'rc-quiet' : null,
'dummy-quiet' : null,
});
}
class Player extends Remote {
constructor(options = {}, vlcfactory = vlc) {
var player_options = mixIn({}, config, (options || {}).args);
let [host, port] = player_options['rc-host'].split(':');
super(port, host);
this.options = player_options;
this.vlcfactory = vlcfactory;
}
async start() {
if(this.vlc)
return;
var cmdargs = values(map(this.options, function(v, k) {
return '--' + k + '' + (v === null ? '' : '=' + v);
}));
this.vlc = await this.vlcfactory(cmdargs, {stdio : ['ignore', 'ignore', 'pipe']});
var dataBuff = '';
this.vlc.stderr.on('data', (data) => {
dataBuff = dataBuff + data;
if(splitter.test(dataBuff)) {
var matches = splitter.exec(dataBuff);
dataBuff = '';
this.emit('play', matches[1]);
debug(`playing ${matches[1]}`);
}
});
if(this.options.intf == "skins2")
await this._waitVlcSkin();
try {
debug("waiting player server");
await this._waitVlcServerStart();
debug("Player ready");
} catch(err) {
this.close();
throw err;
}
debug(`Spawned vlc child pid: ${this.vlc.pid}`);
this.vlc.on('close', this.emit.bind(this, 'close'));
this.vlc.on('error', this.emit.bind(this, 'error'));
var heartbeat = setInterval(() => {
this.info().catch(() => {
this.close();
clearInterval(heartbeat);
});
}, heartbeat_interval);
}
close() {
if(!this.vlc)
return;
try {
this.vlc.kill();
debug(`killed vlc child pid: ${this.vlc.pid}`);
} catch(err) {
debug(err);
}
this.vlc = null;
}
_waitVlcSkin() {
var waitSkin = defer();
var skinBuff = '';
var self = this;
debug("waiting skin load ");
this.vlc.stderr.on('data', function listener(data) {
skinBuff = skinBuff + data;
if((skinBuff).indexOf("using skin file") != -1) {
debug("Skin ready");
self.vlc.stderr.removeListener('data', listener);
waitSkin.resolve();
}
});
return waitSkin;
}
async _waitVlcServerStart() {
var attempt = 10;
var shouldWait = true;
while(shouldWait && (attempt--) > 0) {
try {
await this.info();
shouldWait = false;
} catch(err) {
await sleep(200);
}
}
if(attempt <= 0)
throw 'Server not ready';
}
}
module.exports = Player;