-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvlc.js
122 lines (101 loc) · 2.65 KB
/
vlc.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
var spawn = require('child_process').spawn;
var connect = require('net').connect;
var existsSync = require('fs').existsSync;
var WINDOWS_VLC = [
'C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe',
'C:\\Program Files\\VideoLAN\\VLC\\vlc.exe'
];
function VLCPlayer(executable, port, readyCallback) {
if (!executable) {
executable = 'vlc';
if (process.platform == 'win32'){
WINDOWS_VLC.forEach(function (path) {
if (existsSync(path)){
executable = path;
}
});
}
}
if (!port){
port = 7564;
}
spawn(executable,
['--extraintf', 'rc', '--rc-host', 'localhost:' + port],
{ stdio: 'ignore' }
);
this.initialized = false;
var self = this;
var warmup = setInterval(function () {
self.sock = connect({ port: port }, function () {
clearInterval(warmup);
self.sock.on('data', self.handleData.bind(self));
self.sock.on('end', self.handleEnd.bind(self));
if (readyCallback) {
readyCallback();
}
});
self.sock.on('error', function (error) {
if (!self.initialized) {
self.sock.destroy();
} else {
console.error('Socket error: ' + error);
}
});
}, 500);
this.waiting = [];
}
VLCPlayer.prototype = {
handleData: function (data) {
data = (data+'').trim('\n');
if (data.trim() !== '>') {
console.log(data);
}
if (this.waiting.length > 0) {
var fn = this.waiting.shift();
setImmediate(function () {
fn(data);
});
}
},
handleEnd: function(){
console.log('socket ended');
process.exit(0);
},
play: function () {
if (!this.sock) {
return;
}
this.sock.write('play\n');
},
pause: function () {
if (!this.sock) {
return;
}
this.sock.write('pause\n');
},
getTime: function (cb) {
this.sock.write('get_time\n');
this.waiting.push(function (data) {
cb(parseInt(data));
});
},
seek: function (to) {
if (!this.sock) {
return;
}
if (to < 0) {
this.pause();
return;
} else {
this.play();
}
this.sock.write('seek ' + to + '\n');
},
load: function (link) {
if (!this.sock) {
return;
}
this.sock.write('clear\nadd ' + link + '\n');
}
};
module.exports = VLCPlayer;