@stremio/stremio-video 0.0.20-rc.6 → 0.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stremio/stremio-video",
3
- "version": "0.0.20-rc.6",
3
+ "version": "0.0.21",
4
4
  "description": "Abstraction layer on top of different media players",
5
5
  "author": "Smart Code OOD",
6
6
  "main": "src/index.js",
@@ -16,7 +16,7 @@
16
16
  "deep-freeze": "0.0.1",
17
17
  "eventemitter3": "4.0.7",
18
18
  "hat": "0.0.3",
19
- "hls.js": "https://github.com/Stremio/hls.js/releases/download/v1.0.10-patch2/hls.js-1.0.10-patch2.tgz",
19
+ "hls.js": "https://github.com/Stremio/hls.js/releases/download/v1.2.3-patch1/hls.js-1.2.3-patch1.tgz",
20
20
  "lodash.clonedeep": "4.5.0",
21
21
  "magnet-uri": "6.2.0",
22
22
  "url": "0.11.0",
@@ -3,8 +3,8 @@ function getContentType(stream) {
3
3
  return Promise.reject(new Error('Invalid stream parameter!'));
4
4
  }
5
5
 
6
- if (stream.behaviorHints && stream.behaviorHints.headers && typeof stream.behaviorHints.headers['content-type'] === 'string') {
7
- return Promise.resolve(stream.behaviorHints.headers['content-type']);
6
+ if (stream.behaviorHints && stream.behaviorHints.proxyHeaders && stream.behaviorHints.proxyHeaders.response && typeof stream.behaviorHints.proxyHeaders.response['content-type'] === 'string') {
7
+ return Promise.resolve(stream.behaviorHints.proxyHeaders.response['content-type']);
8
8
  }
9
9
 
10
10
  return fetch(stream.url, { method: 'HEAD' })
@@ -0,0 +1,436 @@
1
+ var EventEmitter = require('eventemitter3');
2
+ var cloneDeep = require('lodash.clonedeep');
3
+ var deepFreeze = require('deep-freeze');
4
+ var ERROR = require('../error');
5
+
6
+ var SUBS_SCALE_FACTOR = 0.0066;
7
+
8
+ var stremioToMPVProps = {
9
+ 'stream': null,
10
+ 'paused': 'pause',
11
+ 'time': 'time-pos',
12
+ 'duration': 'duration',
13
+ 'buffering': 'buffering',
14
+ 'volume': 'volume',
15
+ 'muted': 'mute',
16
+ 'playbackSpeed': 'speed',
17
+ 'audioTracks': 'audioTracks',
18
+ 'selectedAudioTrackId': 'aid',
19
+ 'subtitlesTracks': 'subtitlesTracks',
20
+ 'selectedSubtitlesTrackId': 'sid',
21
+ 'subtitlesSize': 'sub-scale',
22
+ 'subtitlesTextColor': 'sub-color',
23
+ 'subtitlesBackgroundColor': 'sub-back-color',
24
+ 'subtitlesOutlineColor': 'sub-border-color',
25
+ };
26
+
27
+ function ShellVideo(options) {
28
+ options = options || {};
29
+
30
+ var ipc = options.shellTransport;
31
+
32
+ var stremioProps = {};
33
+ Object.keys(stremioToMPVProps).forEach(function(key) {
34
+ if(stremioToMPVProps[key]) {
35
+ stremioProps[stremioToMPVProps[key]] = key;
36
+ }
37
+ });
38
+
39
+ ipc.send('mpv-command', ['stop']);
40
+ ipc.send('mpv-observe-prop', 'path');
41
+
42
+ ipc.send('mpv-observe-prop', 'time-pos');
43
+ ipc.send('mpv-observe-prop', 'volume');
44
+ ipc.send('mpv-observe-prop', 'pause');
45
+ ipc.send('mpv-observe-prop', 'seeking');
46
+ ipc.send('mpv-observe-prop', 'eof-reached');
47
+
48
+ ipc.send('mpv-observe-prop', 'duration');
49
+ ipc.send('mpv-observe-prop', 'metadata');
50
+ ipc.send('mpv-observe-prop', 'video-params'); // video width/height
51
+ ipc.send('mpv-observe-prop', 'track-list');
52
+
53
+ ipc.send('mpv-observe-prop', 'paused-for-cache');
54
+ ipc.send('mpv-observe-prop', 'cache-buffering-state');
55
+
56
+ ipc.send('mpv-observe-prop', 'aid');
57
+ ipc.send('mpv-observe-prop', 'vid');
58
+ ipc.send('mpv-observe-prop', 'sid');
59
+ ipc.send('mpv-observe-prop', 'sub-scale');
60
+ ipc.send('mpv-observe-prop', 'sub-pos');
61
+ ipc.send('mpv-observe-prop', 'speed');
62
+
63
+ ipc.send('mpv-observe-prop', 'mpv-version');
64
+ ipc.send('mpv-observe-prop', 'ffmpeg-version');
65
+
66
+ var events = new EventEmitter();
67
+ var destroyed = false;
68
+ var stream = null;
69
+ // var selectedSubtitlesTrackId = null;
70
+ var observedProps = {};
71
+ var continueFrom = 0;
72
+
73
+ var avgDuration = 0;
74
+ var minClipDuration = 30;
75
+ var props = { };
76
+
77
+ function setBackground(visible) {
78
+ // This is a bit of a hack but there is no better way so far
79
+ var bg = visible ? '' : 'transparent';
80
+ for(var container = options.containerElement; container; container = container.parentElement) {
81
+ container.style.background = bg;
82
+ }
83
+ }
84
+ function logProp(args) {
85
+ // eslint-disable-next-line no-console
86
+ console.log(args.name+': '+args.data);
87
+ }
88
+ function embeddedProp(args) {
89
+ return args.data ? 'EMBEDDED_' + args.data.toString() : null;
90
+ }
91
+
92
+ var last_time = 0;
93
+ ipc.on('mpv-prop-change', function(args) {
94
+ switch (args.name) {
95
+ case 'mpv-version':
96
+ case 'ffmpeg-version': {
97
+ props[args.name] = logProp(args);
98
+ break;
99
+ }
100
+ case 'duration': {
101
+ var intDuration = args.data | 0;
102
+ // Accumulate average duration over time. if it is greater than minClipDuration
103
+ // and equal to the currently reported duration, it is returned as video length.
104
+ // If the reported duration changes over time the average duration is always
105
+ // smaller than the currently reported one so we set the video length to 0 as
106
+ // this is a live stream.
107
+ props[args.name] = args.data >= minClipDuration && (!avgDuration || intDuration === avgDuration) ? Math.round(args.data * 1000) : null;
108
+ // The average duration is calculated using right bit shifting by one of the sum of
109
+ // the previous average and the currently reported value. This method is not very precise
110
+ // as we get integer value but we avoid floating point errors. JS uses 32 bit values
111
+ // for bitwise maths so the maximum supported video duration is 1073741823 (2 ^ 30 - 1)
112
+ // which is around 34 years of playback time.
113
+ avgDuration = avgDuration ? (avgDuration + intDuration) >> 1 : intDuration;
114
+ break;
115
+ }
116
+ case 'time-pos': {
117
+ props[args.name] = Math.round(args.data*1000);
118
+ if(continueFrom) {
119
+ ipc.send('mpv-set-prop', ['time-pos', continueFrom]);
120
+ props[args.name] = Math.round(continueFrom);
121
+ continueFrom = 0;
122
+ }
123
+ break;
124
+ }
125
+ case 'sub-scale': {
126
+ props[args.name] = Math.round(args.data / SUBS_SCALE_FACTOR);
127
+ break;
128
+ }
129
+ case 'paused-for-cache':
130
+ case 'seeking':
131
+ {
132
+ if(props.buffering !== args.data) {
133
+ props.buffering = args.data;
134
+ onPropChanged('buffering');
135
+ }
136
+ break;
137
+ }
138
+ case 'aid':
139
+ case 'sid':
140
+ case 'vid': {
141
+ props[args.name] = embeddedProp(args);
142
+ break;
143
+ }
144
+ // In that case onPropChanged() is manually invoked as track-list contains all
145
+ // the tracks but we have different event for each track type
146
+ case 'track-list': {
147
+ props.audioTracks = args.data.filter(function(x) { return x.type === 'audio'; })
148
+ .map(function(x, index) {
149
+ return {
150
+ id: 'EMBEDDED_' + x.id,
151
+ lang: x.lang === undefined ? 'Track' + (index + 1) : x.lang,
152
+ label: x.title === undefined || x.lang === undefined ? '' : x.title || x.lang,
153
+ origin: 'EMBEDDED',
154
+ embedded: true,
155
+ mode: x.id === props.aid ? 'showing' : 'disabled',
156
+ };
157
+ });
158
+ onPropChanged('audioTracks');
159
+
160
+ props.subtitlesTracks = args.data
161
+ .filter(function(x) { return x.type === 'sub'; })
162
+ .map(function(x, index) {
163
+ return {
164
+ id: 'EMBEDDED_' + x.id,
165
+ lang: x.lang === undefined ? 'Track ' + (index + 1) : x.lang,
166
+ label: x.title === undefined || x.lang === undefined ? '' : x.title || x.lang,
167
+ origin: 'EMBEDDED',
168
+ embedded: true,
169
+ mode: x.id === props.sid ? 'showing' : 'disabled',
170
+ };
171
+ });
172
+ onPropChanged('subtitlesTracks');
173
+ break;
174
+ }
175
+ default: {
176
+ props[args.name] = args.data;
177
+ break;
178
+ }
179
+ }
180
+
181
+ // Cap time update to update only when a second passes
182
+ var current_time = args.name === 'time-pos' ? Math.floor(props['time-pos'] / 1000) : null;
183
+ if((!current_time || last_time !== current_time)&& stremioProps[args.name]) {
184
+ if(current_time) {
185
+ last_time = current_time;
186
+ }
187
+ onPropChanged(stremioProps[args.name]);
188
+ }
189
+ });
190
+ ipc.on('mpv-event-ended', function(args) {
191
+ if (args.error) onError(args.error);
192
+ else onEnded();
193
+ });
194
+
195
+ function getProp(propName) {
196
+ if(stremioToMPVProps[propName]) return props[stremioToMPVProps[propName]];
197
+ // eslint-disable-next-line no-console
198
+ console.log('Unsupported prop requested', propName);
199
+ return null;
200
+ }
201
+ function onError(error) {
202
+ events.emit('error', error);
203
+ if (error.critical) {
204
+ command('unload');
205
+ }
206
+ }
207
+ function onEnded() {
208
+ events.emit('ended');
209
+ }
210
+ function onPropChanged(propName) {
211
+ if (observedProps[propName]) {
212
+ events.emit('propChanged', propName, getProp(propName));
213
+ }
214
+ }
215
+ function observeProp(propName) {
216
+ events.emit('propValue', propName, getProp(propName));
217
+ observedProps[propName] = true;
218
+ }
219
+ function setProp(propName, propValue) {
220
+ switch (propName) {
221
+ case 'paused': {
222
+ if (stream !== null) {
223
+ ipc.send('mpv-set-prop', ['pause', propValue]);
224
+ }
225
+
226
+ break;
227
+ }
228
+ case 'time': {
229
+ if (stream !== null && propValue !== null && isFinite(propValue)) {
230
+ ipc.send('mpv-set-prop', ['time-pos', propValue/1000]);
231
+ }
232
+
233
+ break;
234
+ }
235
+ case 'playbackSpeed': {
236
+ if (stream !== null && propValue !== null && isFinite(propValue)) {
237
+ ipc.send('mpv-set-prop', ['speed', propValue]);
238
+ }
239
+ break;
240
+ }
241
+ case 'volume': {
242
+ if (stream !== null && propValue !== null && isFinite(propValue)) {
243
+ props.mute = false;
244
+ ipc.send('mpv-set-prop', ['mute', 'no']);
245
+ ipc.send('mpv-set-prop', ['volume', propValue]);
246
+ onPropChanged('muted');
247
+ onPropChanged('volume');
248
+ }
249
+ break;
250
+ }
251
+ case 'muted': {
252
+ if (stream !== null) {
253
+ ipc.send('mpv-set-prop', ['mute', propValue ? 'yes' : 'no']);
254
+ props.mute = propValue;
255
+ onPropChanged('muted');
256
+ }
257
+ break;
258
+ }
259
+ case 'selectedAudioTrackId': {
260
+ if (stream !== null) {
261
+ var actualId = propValue.slice('EMBEDDED_'.length);
262
+ ipc.send('mpv-set-prop', ['aid', actualId]);
263
+ }
264
+ break;
265
+ }
266
+ case 'selectedSubtitlesTrackId': {
267
+ if (stream !== null) {
268
+ if(propValue) {
269
+ var actualId = propValue.slice('EMBEDDED_'.length);
270
+ ipc.send('mpv-set-prop', ['sid', actualId]);
271
+ events.emit('subtitlesTrackLoaded', propValue);
272
+ } else {
273
+ // turn off subs
274
+ ipc.send('mpv-set-prop', ['sid', 'no']);
275
+ props.sid = null;
276
+ }
277
+ }
278
+ onPropChanged('selectedSubtitlesTrackId');
279
+ break;
280
+ }
281
+ case 'subtitlesSize': {
282
+ ipc.send('mpv-set-prop', [stremioToMPVProps[propName], propValue * SUBS_SCALE_FACTOR]);
283
+ break;
284
+ }
285
+ case 'subtitlesOffset': {
286
+ ipc.send('mpv-set-prop', [stremioToMPVProps[propName], propValue]);
287
+ break;
288
+ }
289
+ case 'subtitlesTextColor':
290
+ case 'subtitlesBackgroundColor':
291
+ case 'subtitlesOutlineColor':
292
+ {
293
+ // MPV accepts color in #AARRGGBB
294
+ var argb = propValue.replace(/^#(\w{6})(\w{2})$/, '#$2$1');
295
+ ipc.send('mpv-set-prop', [stremioToMPVProps[propName], argb]);
296
+ break;
297
+ }
298
+ default: {
299
+ // eslint-disable-next-line no-console
300
+ console.log('Unhandled setProp for', propName);
301
+ }
302
+ }
303
+ }
304
+ function command(commandName, commandArgs) {
305
+ switch (commandName) {
306
+ case 'load': {
307
+ command('unload');
308
+ if (commandArgs && commandArgs.stream && typeof commandArgs.stream.url === 'string') {
309
+ stream = commandArgs.stream;
310
+ onPropChanged('stream');
311
+ continueFrom = commandArgs.time !== null && isFinite(commandArgs.time) ? parseInt(commandArgs.time, 10) / 1000 : 0;
312
+
313
+ setBackground(false);
314
+
315
+ ipc.send('mpv-set-prop', ['no-sub-ass']);
316
+
317
+ // opengl-cb is an alias for the new name "libmpv", as shown in mpv's video/out/vo.c aliases
318
+ // opengl is an alias for the new name "gpu"
319
+ // When on Windows we use d3d for the rendering in separate window
320
+ var windowRenderer = navigator.platform === 'Win32' ? 'direct3d' : 'opengl';
321
+ var videoOutput = options.mpvSeparateWindow ? windowRenderer : 'opengl-cb';
322
+ var separateWindow = options.mpvSeparateWindow ? 'yes' : 'no';
323
+ ipc.send('mpv-set-prop', ['vo', videoOutput]);
324
+ ipc.send('mpv-set-prop', ['osc', separateWindow]);
325
+ ipc.send('mpv-set-prop', ['input-defalt-bindings', separateWindow]);
326
+ ipc.send('mpv-set-prop', ['input-vo-keyboard', separateWindow]);
327
+
328
+ ipc.send('mpv-command', ['loadfile', stream.url]);
329
+ ipc.send('mpv-set-prop', ['pause', false]);
330
+ ipc.send('mpv-set-prop', ['speed', props.speed]);
331
+ ipc.send('mpv-set-prop', ['aid', props.aid]);
332
+ ipc.send('mpv-set-prop', ['mute', 'no']);
333
+
334
+ onPropChanged('paused');
335
+ onPropChanged('time');
336
+ onPropChanged('duration');
337
+ onPropChanged('buffering');
338
+ onPropChanged('volume');
339
+ onPropChanged('muted');
340
+ onPropChanged('subtitlesTracks');
341
+ onPropChanged('selectedSubtitlesTrackId');
342
+ } else {
343
+ onError(Object.assign({}, ERROR.UNSUPPORTED_STREAM, {
344
+ critical: true,
345
+ stream: commandArgs ? commandArgs.stream : null
346
+ }));
347
+ }
348
+
349
+ break;
350
+ }
351
+ case 'unload': {
352
+ props = {
353
+ mute: false,
354
+ speed: 1,
355
+ subtitlesTracks: [],
356
+ buffering: true,
357
+ aid: null,
358
+ sid: null,
359
+ };
360
+ continueFrom = 0;
361
+ avgDuration = 0;
362
+ ipc.send('mpv-command', ['stop']);
363
+ onPropChanged('stream');
364
+ onPropChanged('paused');
365
+ onPropChanged('time');
366
+ onPropChanged('duration');
367
+ onPropChanged('buffering');
368
+ onPropChanged('volume');
369
+ onPropChanged('muted');
370
+ onPropChanged('subtitlesTracks');
371
+ onPropChanged('selectedSubtitlesTrackId');
372
+ setBackground(true);
373
+ break;
374
+ }
375
+ case 'destroy': {
376
+ command('unload');
377
+ destroyed = true;
378
+ events.removeAllListeners();
379
+ break;
380
+ }
381
+ }
382
+ }
383
+
384
+ this.on = function (eventName, listener) {
385
+ if (destroyed) {
386
+ throw new Error('Video is destroyed');
387
+ }
388
+
389
+ events.on(eventName, listener);
390
+ };
391
+ this.dispatch = function (action) {
392
+ if (destroyed) {
393
+ throw new Error('Video is destroyed');
394
+ }
395
+
396
+ if (action) {
397
+ action = deepFreeze(cloneDeep(action));
398
+ switch (action.type) {
399
+ case 'observeProp': {
400
+ observeProp(action.propName);
401
+ break;
402
+ }
403
+ case 'setProp': {
404
+ setProp(action.propName, action.propValue);
405
+ return;
406
+ }
407
+ case 'command': {
408
+ command(
409
+ action.commandName,
410
+ action.commandArgs
411
+ );
412
+ return;
413
+ }
414
+ }
415
+ }
416
+ };
417
+ }
418
+ ShellVideo.canPlayStream = function() {
419
+ return Promise.resolve(true);
420
+ };
421
+
422
+ ShellVideo.manifest = {
423
+ name: 'ShellVideo',
424
+ external: false,
425
+ props: Object.keys(stremioToMPVProps),
426
+ commands: ['load', 'unload', 'destroy'],
427
+ events: [
428
+ 'propValue',
429
+ 'propChanged',
430
+ 'ended',
431
+ 'error',
432
+ 'subtitlesTrackLoaded',
433
+ ],
434
+ };
435
+
436
+ module.exports = ShellVideo;
@@ -0,0 +1,3 @@
1
+ var ShellVideo = require('./ShellVideo');
2
+
3
+ module.exports = ShellVideo;
@@ -1,4 +1,5 @@
1
1
  var ChromecastSenderVideo = require('../ChromecastSenderVideo');
2
+ var ShellVideo = require('../ShellVideo');
2
3
  var HTMLVideo = require('../HTMLVideo');
3
4
  var TizenVideo = require('../TizenVideo');
4
5
  var IFrameVideo = require('../IFrameVideo');
@@ -22,6 +23,9 @@ function selectVideoImplementation(commandArgs, options) {
22
23
  if (typeof commandArgs.stream.playerFrameUrl === 'string') {
23
24
  return IFrameVideo;
24
25
  }
26
+ if (options.shellTransport) {
27
+ return withStreamingServer(withHTMLSubtitles(ShellVideo));
28
+ }
25
29
 
26
30
  if (typeof commandArgs.streamingServerURL === 'string') {
27
31
  if (typeof global.tizen !== 'undefined') {
@@ -17,6 +17,8 @@ function TizenVideo(options) {
17
17
  throw new Error('Container element required to be instance of HTMLElement');
18
18
  }
19
19
 
20
+ var promiseAudioTrackChange = false;
21
+
20
22
  var size = 100;
21
23
  var offset = 0;
22
24
  var textColor = 'rgb(255, 255, 255)';
@@ -151,7 +153,14 @@ function TizenVideo(options) {
151
153
  return null;
152
154
  }
153
155
 
154
- return !!(window.webapis.avplay.getState() === 'PAUSED');
156
+ var isPaused = !!(window.webapis.avplay.getState() === 'PAUSED');
157
+
158
+ if (!isPaused && promiseAudioTrackChange) {
159
+ window.webapis.avplay.setSelectTrack('AUDIO', parseInt(promiseAudioTrackChange.replace('EMBEDDED_', '')));
160
+ promiseAudioTrackChange = false;
161
+ }
162
+
163
+ return isPaused;
155
164
  }
156
165
  case 'time': {
157
166
  var currentTime = window.webapis.avplay.getCurrentTime();
@@ -301,8 +310,12 @@ function TizenVideo(options) {
301
310
  return null;
302
311
  }
303
312
 
313
+ if (promiseAudioTrackChange) {
314
+ return promiseAudioTrackChange;
315
+ }
316
+
304
317
  var currentTracks = window.webapis.avplay.getCurrentStreamInfo();
305
- var currentIndex;
318
+ var currentIndex = false;
306
319
 
307
320
  for (var i = 0; i < currentTracks.length; i++) {
308
321
  if (currentTracks[i].type === 'AUDIO') {
@@ -312,7 +325,7 @@ function TizenVideo(options) {
312
325
  }
313
326
  }
314
327
 
315
- return currentIndex ? 'EMBEDDED_' + String(currentIndex) : null;
328
+ return currentIndex !== false ? 'EMBEDDED_' + String(currentIndex) : null;
316
329
  }
317
330
  case 'playbackSpeed': {
318
331
  if (destroyed || videoSpeed === null || !isFinite(videoSpeed)) {
@@ -375,6 +388,16 @@ function TizenVideo(options) {
375
388
 
376
389
  onPropChanged('paused');
377
390
 
391
+ // the paused state is usually correct, but i have seen it not change on tizen 3
392
+ // which causes all kinds of issues in the UI: (only happens with some videos)
393
+ var lastKnownProp = getProp('paused');
394
+
395
+ setTimeout(function() {
396
+ if (getProp('paused') !== lastKnownProp) {
397
+ onPropChanged('paused');
398
+ }
399
+ }, 1000);
400
+
378
401
  break;
379
402
  }
380
403
  case 'time': {
@@ -493,8 +516,17 @@ function TizenVideo(options) {
493
516
  return track.id === propValue;
494
517
  });
495
518
 
496
- window.webapis.avplay.setSelectTrack('AUDIO', parseInt(currentAudioTrack.replace('EMBEDDED_', '')));
519
+ if (getProp('paused')) {
520
+ // issues before this logic:
521
+ // tizen 3 does not allow changing audio track when paused
522
+ // tizen 5 does, but it will only change getProp('selectedAudioTrackId') after playback starts
497
523
 
524
+ // will be changed on next play event, until then we will overwrite the result of getProp('selectedAudioTrackId')
525
+ promiseAudioTrackChange = propValue;
526
+ onPropChanged('selectedAudioTrackId');
527
+ } else {
528
+ window.webapis.avplay.setSelectTrack('AUDIO', parseInt(currentAudioTrack.replace('EMBEDDED_', '')));
529
+ }
498
530
  if (selectedAudioTrack) {
499
531
  events.emit('audioTrackLoaded', selectedAudioTrack);
500
532
  onPropChanged('selectedAudioTrackId');
package/src/error.js CHANGED
@@ -58,7 +58,7 @@ var ERROR = {
58
58
  WITH_STREAMING_SERVER: {
59
59
  CONVERT_FAILED: {
60
60
  code: 60,
61
- message: 'Unable to convert stream'
61
+ message: 'Streaming server failed to convert torrent stream'
62
62
  }
63
63
  },
64
64
  UNKNOWN_ERROR: {