-
-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathLivestreamerController.js
521 lines (440 loc) · 14.5 KB
/
LivestreamerController.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
define([
"Ember",
"nwjs/nwGui",
"nwjs/nwWindow",
"mixins/ChannelMixin",
"mixins/ChannelSettingsMixin",
"utils/fs/which",
"utils/fs/stat",
"utils/semver"
], function(
Ember,
nwGui,
nwWindow,
ChannelMixin,
ChannelSettingsMixin,
which,
stat,
semver
) {
var CP = require( "child_process" ),
PATH = require( "path" );
var get = Ember.get,
set = Ember.set,
setP = Ember.setProperties,
run = Ember.run;
var alias = Ember.computed.alias;
var isWin = process.platform === "win32";
var reVersion = /^livestreamer(?:\.exe|-script\.py)? (\d+\.\d+.\d+)(.*)$/,
reUnable = /^error: Unable to open URL: /,
reNoStreams = /^error: No streams found on this URL: /,
reNoPlayer = /^error: Failed to start player: /,
reNoPlayer2 = /^error: The default player \(.+\) does not seem to be installed\./,
reReplace = /^\[(?:cli|plugin\.\w+)]\[\S+]\s+/,
rePlayer = /^Starting player: \S+/,
reSplit = /\r?\n/g;
function VersionError( version ) { this.version = version; }
VersionError.prototype = new Error();
function NotFoundError() {}
NotFoundError.prototype = new Error();
function UnableToOpenError() {}
UnableToOpenError.prototype = new Error();
function NoStreamsFoundError() {}
NoStreamsFoundError.prototype = new Error();
function NoPlayerError() {}
NoPlayerError.prototype = new Error();
function setIfNotNull( objA, objB, key ) {
var val = get( objA, key );
if ( val !== null ) {
set( objB, key, val );
}
}
return Ember.Controller.extend( ChannelMixin, ChannelSettingsMixin, {
metadata: Ember.inject.service(),
store : Ember.inject.service(),
settings: Ember.inject.service(),
config: alias( "metadata.config" ),
modalBtns: null,
active: null,
model : function() {
var store = get( this, "store" );
return store.all( "livestreamer" );
}.property(),
startStream: function( stream ) {
this.send( "openModal", {
view : "livestreamerModal",
template: "livestreamerModal"
}, this, {
modalHead: "Preparing",
modalBody: "Please wait...",
modalBtns: null
});
var store = get( this, "store" );
var channel = get( stream, "channel" );
var id = get( channel, "id" );
// is the stream already running?
if ( store.hasRecordForId( "livestreamer", id ) ) {
set( this, "active", store.recordForId( "livestreamer", id ) );
return setP( this, {
modalHead: "You're watching %@".fmt( get( channel, "display_name" ) ),
modalBody: get( channel, "status" ),
modalBtns: "running"
});
}
// create a new livestreamer object
var livestreamer = store.createRecord( "livestreamer", {
id : id,
stream : stream,
channel : channel,
quality : get( this, "settings.quality" ),
gui_openchat: get( this, "settings.gui_openchat" ),
started : new Date()
});
// modal belongs to this stream now
set( this, "active", livestreamer );
this.loadChannelSettings( id )
// override channel specific settings
.then(function( settings ) {
setIfNotNull( settings, livestreamer, "quality" );
setIfNotNull( settings, livestreamer, "gui_openchat" );
})
// validate configuration and get the exec command
.then( this.checkLivestreamer.bind( this ) )
// launch the stream
.then(function( exec ) {
setP( this, {
modalHead: "Launching stream",
modalBody: "Waiting for Livestreamer to launch the stream..."
});
return this.launchLivestreamer( exec, livestreamer );
}.bind( this ) )
// independently check whether the user is following / subscribing the channel
.then(function() {
this.checkUserSubscribesChannel( channel );
this.checkUserFollowsChannel( channel );
}.bind( this ) )
// setup stream refresh interval
.then( this.refreshStream.bind( this, livestreamer ) )
// success/failure
.then(
this.streamSuccess.bind( this, livestreamer, true ),
this.streamFailure.bind( this, livestreamer )
);
},
streamSuccess: function( livestreamer, guiActions ) {
setP( this, {
modalHead: "Watching now: %@".fmt( get( livestreamer, "channel.display_name" ) ),
modalBody: get( livestreamer, "channel.status" ),
modalBtns: "running"
});
set( livestreamer, "success", true );
if ( !guiActions ) { return; }
// automatically close modal on success
if ( get( this, "settings.gui_hidestreampopup" ) ) {
this.send( "close" );
}
// automatically open chat
if ( get( livestreamer, "gui_openchat" ) ) {
this.send( "chat", get( livestreamer, "channel" ) );
}
// hide the GUI
this.minimize( false );
},
streamFailure: function( livestreamer, err ) {
if ( !get( livestreamer, "isDeleted" ) ) {
livestreamer.destroyRecord();
}
if ( err instanceof VersionError ) {
setP( this, {
modalHead: "Error: Invalid Livestreamer version",
modalBody: "Your version v%@ doesn't match the min. requirements (v%@)"
.fmt( err.version, get( this, "versionMin" ) ),
modalBtns: "download"
});
} else if ( err instanceof NotFoundError ) {
setP( this, {
modalHead: "Error: Livestreamer was not found",
modalBody: "Please check settings and/or (re)install Livestreamer.",
modalBtns: "download"
});
} else if ( err instanceof UnableToOpenError ) {
setP( this, {
modalHead: "Error: Unable to open stream",
modalBody: "Livestreamer was unable to open the stream.",
modalBtns: null
});
} else if ( err instanceof NoStreamsFoundError ) {
setP( this, {
modalHead: "Error: No streams found",
modalBody: "Livestreamer was unable to find the stream.",
modalBtns: null
});
} else if ( err instanceof NoPlayerError ) {
setP( this, {
modalHead: "Error: Invalid player",
modalBody: "Please check your player configuration.",
modalBtns: null
});
} else {
setP( this, {
modalHead: "Error: Couldn't launch the stream",
modalBody: err
? err.message || err.toString()
: "Internal error",
modalBtns: null
});
}
},
/**
* Check the location of livestreamer and validate
* @returns {Promise}
*/
checkLivestreamer: function() {
var path = get( this, "settings.livestreamer" );
var exec = get( this, "config.livestreamer-exec" );
var fb = get( this, "config.livestreamer-fallback-paths-unix" );
// use the default command if the user did not define one
path = path ? String( path ) : exec;
// check for invalid values first
if ( path.indexOf( exec ) === -1 ) {
return Promise.reject( new NotFoundError() );
}
function execCheck( stat ) {
return isWin || ( stat.mode & 0111 ) > 0;
}
// check for the executable
return which( path, execCheck )
// check fallback paths
.catch(function() {
var promise = Promise.reject();
if ( isWin || !fb || !fb.length ) {
return promise;
}
return fb.reduce(function( promise, path ) {
var check = PATH.join( PATH.resolve( path ), exec );
return promise.catch(function() {
return stat( check, execCheck );
});
}, promise );
}.bind( this ) )
// not found
.catch(function() { throw new NotFoundError(); })
// check for correct version
.then( this.validateLivestreamer.bind( this ) );
},
/**
* Validate livestreamer
* Runs the executable with `--version` parameters and reads answer from stderr
* @param {string} exec
* @returns {Promise}
*/
validateLivestreamer: function( exec ) {
var minimum = get( this, "config.livestreamer-version-min" );
var time = get( this, "config.livestreamer-validation-timeout" );
var defer = Promise.defer();
var spawn = CP.spawn( exec, [ "--version", "--no-version-check" ] );
function failed( err ) {
spawn = null;
defer.reject( err );
}
function onData( data ) {
var match = reVersion.exec( String( data ).trim() );
if ( match ) {
// resolve before process exit
defer.resolve( match[1] );
}
// immediately kill the process
spawn.kill( "SIGKILL" );
}
function onTimeout() {
if ( spawn ) { spawn.kill( "SIGKILL" ); }
failed( new Error( "timeout" ) );
}
// reject on error / exit
spawn.on( "error", failed );
spawn.on( "exit", failed );
// only check the first chunk of data
spawn.stdout.on( "data", onData );
spawn.stderr.on( "data", onData );
// kill after a certain time
run.later( onTimeout, time );
return defer.promise.then(function( version ) {
return version === semver.getMax([ version, minimum ])
? Promise.resolve( exec )
: Promise.reject( new VersionError( version ) );
});
},
/**
* Launch the stream
* @returns {Promise}
*/
launchLivestreamer: function( exec, livestreamer ) {
// in case the shutdown button was pressed before
if ( get( livestreamer, "shutdown" ) ) {
return Promise.reject();
}
var defer = Promise.defer();
var channel = get( livestreamer, "channel.id" );
var quality = get( livestreamer, "quality" );
var streamURL = get( this, "config.twitch-stream-url" );
var qualities = get( this, "settings.content.constructor.qualities" );
// get the livestreamer parameter list
var params = get( livestreamer, "parameters" );
// append stream url and quality
params.push( streamURL.replace( "{channel}", channel ) );
params.push( ( qualities[ quality ] || qualities[ 0 ] ).quality );
// spawn the livestreamer process
var spawn = CP.spawn( exec, params, { detached: true } );
set( livestreamer, "success", false );
set( livestreamer, "spawn", spawn );
spawn.on( "error", defer.reject );
spawn.on( "exit", function() {
// clear up some memory
set( livestreamer, "spawn", null );
spawn = null;
// quality was changed
if ( quality !== get( livestreamer, "quality" ) ) {
run.next( this, function() {
this.launchLivestreamer( exec, livestreamer ).then(
this.streamSuccess.bind( this, livestreamer, false ),
this.streamFailure.bind( this, livestreamer )
);
});
// stream was shut down regularly
} else {
set( livestreamer, "shutdown", true );
// close the modal only if there was no error and if it belongs to the stream
if (
!get( livestreamer, "error" )
&& get( this, "active" ) === livestreamer
) {
this.send( "close" );
}
// restore the GUI
this.minimize( true );
// remove the livestreamer record from the store
if ( !get( livestreamer, "isDeleted" ) ) {
livestreamer.destroyRecord();
}
}
}.bind( this ) );
// we need a common error parsing function for stdout and stderr, because
// livestreamer is weird sometimes and prints error messages to stdout instead... :(
function parseError( data ) {
if ( reUnable.test( data ) ) {
return new UnableToOpenError();
} else if ( reNoStreams.test( data ) ) {
return new NoStreamsFoundError();
} else if ( reNoPlayer.test( data ) || reNoPlayer2.test( data ) ) {
return new NoPlayerError();
}
}
// reject promise on any error output
function stderrCallback( data ) {
data = data.trim();
set( livestreamer, "error", true );
defer.reject( parseError( data ) || new Error( data ) );
}
// fulfill promise as soon as livestreamer is launching the player
// also print all stdout messages
function stdoutCallback( data ) {
data = data.trim();
var error = parseError( data );
if ( error ) {
set( livestreamer, "error", true );
return defer.reject( error );
}
data = data.replace( reReplace, "" );
if (
get( this, "active" ) === livestreamer
&& !get( livestreamer, "success" )
&& !get( livestreamer, "error" )
) {
set( this, "modalBody", data );
}
if ( rePlayer.test( data ) ) {
/*
* FIXME:
* The promise should resolve at the point when livestreamer is launching the
* player. The only way of doing this is reading from stdout. The issue here
* is though, that in case the user has set an invalid player path, we don't
* know it yet, because the error message is being sent afterwards (and also
* in stdout instead of stderr - worth of a bug report?).
* The stupid solution is adding a short delay. This is again stupid, because
* we don't know how long the machine of the user takes for launching the player
* or detecting an invalid path, etc.
*/
run.later( defer, defer.resolve, 500 );
}
}
spawn.stderr.on( "data", function( data ) {
String( data ).trim().split( reSplit ).forEach( stderrCallback );
});
spawn.stdout.on( "data", function( data ) {
String( data ).trim().split( reSplit ).forEach( stdoutCallback.bind( this ) );
}.bind( this ) );
return defer.promise;
},
killAll: function() {
/** @type {Array} */
var model = get( this, "model" );
model.slice().forEach(function( stream ) {
stream.kill();
});
},
minimize: function( restore ) {
switch ( get( this, "settings.gui_minimize" ) ) {
// minimize
case 1:
nwWindow.toggleMinimize( restore );
break;
// move to tray: toggle window and taskbar visibility
case 2:
nwWindow.toggleVisibility( restore );
if ( get( this, "settings.isVisibleInTaskbar" ) ) {
nwWindow.setShowInTaskbar( restore );
}
break;
}
},
refreshStream: function( livestreamer ) {
var interval = get( this, "config.stream-reload-interval" ) || 60000;
if ( get( livestreamer, "shutdown" ) ) { return; }
var stream = get( livestreamer, "stream" );
var reload = stream.reload.bind( stream );
var promise = reload();
// try to reload the record at least 3 times
for ( var i = 1; i < 3; i++ ) {
promise = promise.catch( reload );
}
// queue another refresh
promise.then(function() {
run.later( this, this.refreshStream, livestreamer, interval );
}.bind( this ) );
},
actions: {
"download": function( callback ) {
this.send( "openBrowser", get( this, "config.livestreamer-download-url" ) );
if ( callback instanceof Function ) {
callback();
}
},
"close": function() {
this.send( "closeModal" );
run.schedule( "destroy", this, function() {
set( this, "active", null );
});
},
"shutdown": function() {
var active = get( this, "active" ),
spawn;
if ( active ) {
set( active, "shutdown", true );
spawn = get( active, "spawn" );
if ( spawn ) { spawn.kill(); }
}
this.send( "close" );
}
}
});
});