stremiok 2.2.9 → 2.3.1

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.
@@ -38,7 +38,7 @@
38
38
  function settleMs() { return getAudioCfg().wait * 1000; }
39
39
  var _reseek = null; // { at, mark: S.lastSeekAt when it was asked }
40
40
 
41
- function resetAudio() { _audioIdx = null; _audioPending = false; _reseek = null; }
41
+ function resetAudio() { _audioIdx = null; _audioPending = false; _reseek = null; _held = null; }
42
42
  // Playback is starting. The track is picked only once it's running (play-time ticks):
43
43
  // selected before play(), the TV records the choice but keeps decoding the default
44
44
  // track, and then ignores re-selecting it.
@@ -47,6 +47,7 @@
47
47
  // TV's player. The menu still offers the others.)
48
48
  function audioOnStart() { _audioPending = !(S.ctx && S.ctx.yt); _audioSteadySince = 0; }
49
49
  function audioOnTick() {
50
+ if (_held && !busyWhy()) { selectAudio(_held.index, _held.message, _held.force); } // (a switch held: made now it plays)
50
51
  if (_audioPending) { maybeApplyAudio(); }
51
52
  if (_reseek && Date.now() >= _reseek.at) {
52
53
  var r = _reseek; _reseek = null;
@@ -205,13 +206,21 @@
205
206
  // it switch for real (the manual workaround, in one press).
206
207
  function selectAudio(index, message, force) {
207
208
  if (P.useAV) {
209
+ var why = busyWhy();
210
+ if (why) { hold(index, message, force, why); return; }
208
211
  if (force) {
209
212
  var tracks = getAudioTracks();
210
213
  for (var k = 0; k < tracks.length; k++) {
211
214
  if (tracks[k].index !== index) { setAudioTrack(tracks[k].index); break; }
212
215
  }
213
216
  }
214
- setAudioTrack(index);
217
+ var asked = Date.now();
218
+ U.log('player', 'Audio switch to track ' + index + (force ? ' (again)' : '') + '. The TV lists: ' + rawAudio() + '. Playing: ' + playingRaw());
219
+ if (!setAudioTrack(index)) { hold(index, message, false, 'refused'); return; }
220
+ _held = null;
221
+ setTimeout(function () { // (what the TV says it plays once it's had time: a switch it took without following shows here)
222
+ if (S.active && _audioIdx === index && Date.now() - asked < 10000) { U.log('player', 'Audio switch to track ' + index + ': the TV now plays ' + playingRaw()); }
223
+ }, SWITCH_CHECK_MS);
215
224
  // A seek makes the player reload audio from the selected track instead of carrying
216
225
  // on with what it had buffered (movies/episodes; live can't seek): now, or once
217
226
  // SETTLE_MS have passed since the start / resume or a jump (see above).
@@ -241,9 +250,57 @@
241
250
  doAVSeek(back, 0);
242
251
  }
243
252
 
253
+ // true: the TV took it (asked as a number, else as text); false: it refused both.
244
254
  function setAudioTrack(index) {
245
- try { avplay.setSelectTrack('AUDIO', index); }
246
- catch (e) { try { avplay.setSelectTrack('AUDIO', '' + index); } catch (e2) {} }
255
+ try { avplay.setSelectTrack('AUDIO', index); return true; }
256
+ catch (e) {
257
+ try { avplay.setSelectTrack('AUDIO', '' + index); return true; }
258
+ catch (e2) { U.log('player', 'Audio track ' + index + ' refused by the TV: ' + trackError(e2) + ' (as a number: ' + trackError(e) + ')'); return false; }
259
+ }
260
+ }
261
+ // The TV refuses a switch while paused or while a jump is still going on (it says it plays
262
+ // meanwhile), PLAYER_ERROR_INVALID_OPERATION (2.3.0.2, issue 17: the menu ticked the track,
263
+ // the audio stayed). Such a switch is held (_held) and made at the first play-time report
264
+ // once it plays on, steadily; one it still refuses, held again, up to HOLD_TRIES times.
265
+ var _held = null; // { index, message, force, tries }
266
+ var SEEK_BUSY_MS = 1500, HOLD_TRIES = 4;
267
+ function busyWhy() {
268
+ if (P.isPaused()) { return 'paused'; }
269
+ if (S.buffering || S.pendingSeek != null) { return 'buffering'; }
270
+ if (Date.now() - (S.lastSeekAt || 0) < SEEK_BUSY_MS) { return 'jumping'; }
271
+ return '';
272
+ }
273
+ function hold(index, message, force, why) {
274
+ var tries = _held && _held.index === index ? _held.tries + 1 : 0;
275
+ if (tries >= HOLD_TRIES) {
276
+ _held = null;
277
+ U.log('player', 'Audio switch to track ' + index + ': still refused after ' + HOLD_TRIES + ' tries, left');
278
+ U.toast('The TV didn’t switch the audio: choose it again', 4000);
279
+ return;
280
+ }
281
+ var first = !_held || _held.index !== index;
282
+ _held = { index: index, message: message, force: force, tries: tries };
283
+ _audioIdx = index; // (the menu ticks it meanwhile)
284
+ if (first) { U.log('player', 'Audio switch to track ' + index + ' held (' + why + '): made once it plays on'); }
285
+ if (first && why === 'paused') { U.toast((message || 'Audio track changed') + ': when you carry on', 3000); }
286
+ }
287
+ // ----- Diagnostics: what the TV said of its audio tracks around a switch -----
288
+ var SWITCH_CHECK_MS = 2500;
289
+ function trackError(e) { return ('' + ((e && (e.name || e.code)) || '') + ' ' + ((e && e.message) || '')).replace(/\s+/g, ' ').trim().slice(0, 120) || 'no reason given'; }
290
+ function rawOf(t) { return t.index + ' ' + ('' + (t.extra_info == null ? '' : t.extra_info)).replace(/\s+/g, ' ').slice(0, 160); }
291
+ // its audio tracks as it lists them (getTotalTrackInfo): index, then what it says of each
292
+ function rawAudio() {
293
+ try {
294
+ var l = (avplay.getTotalTrackInfo() || []).filter(function (t) { return t && ('' + t.type).toUpperCase() === 'AUDIO'; }).map(rawOf);
295
+ return l.length ? l.join(' | ') : 'none';
296
+ } catch (e) { return '(unreadable: ' + trackError(e) + ')'; }
297
+ }
298
+ // the audio track it says is playing (getCurrentStreamInfo)
299
+ function playingRaw() {
300
+ try {
301
+ var now = (avplay.getCurrentStreamInfo() || []).filter(function (t) { return t && ('' + t.type).toUpperCase() === 'AUDIO'; });
302
+ return now.length ? rawOf(now[0]) : 'none reported';
303
+ } catch (e) { return '(unreadable: ' + trackError(e) + ')'; }
247
304
  }
248
305
 
249
306
  // Tracks sorted by language (preferred languages first) and named in full, so the
@@ -225,7 +225,7 @@
225
225
  function renderScrub() {
226
226
  var mark = U.byId('osd-scrub'), range = U.byId('osd-scrub-range'), tip = U.byId('osd-scrub-time');
227
227
  if (!mark || !range || !tip) { return; }
228
- if (!_scrub || !(S.dur > 0)) { mark.className = 'hidden'; range.className = 'hidden'; return; }
228
+ if (!_scrub || !(S.dur > 0)) { mark.className = 'hidden'; range.className = 'hidden'; scrubPicture(null); return; }
229
229
  var pT = _scrub.target / S.dur * 100, pC = Math.min(100, S.cur / S.dur * 100);
230
230
  mark.className = pT < 8 ? 'edge-l' : (pT > 92 ? 'edge-r' : '');
231
231
  mark.style.left = pT + '%';
@@ -236,6 +236,34 @@
236
236
  var ci = chapterAt(_scrub.target), ch = ci >= 0 ? chapters()[ci].title : ''; // (the chapter it lands in)
237
237
  if (ch.length > 40) { ch = ch.slice(0, 39) + '…'; }
238
238
  tip.textContent = U.fmtTime(_scrub.target) + ' ' + (d < 0 ? '−' : '+') + U.fmtTime(Math.abs(d)) + (ch ? ' · ' + ch : '');
239
+ scrubPicture(_scrub.target);
240
+ }
241
+ // A Jellyfin file's seek picture above the marker (its trickplay: a tile of 10 × 10, the one
242
+ // under the marker shown by moving the tile behind a 320-wide window).
243
+ var _picTile = '', _picAhead = [];
244
+ function scrubPicture(sec) {
245
+ var pic = U.byId('osd-scrub-pic'), jf = S.ctx && S.ctx.stream && S.ctx.stream._jf;
246
+ var at = (sec != null && jf && jf.tp && typeof Jellyfin !== 'undefined' && Jellyfin.trickplayAt) ? Jellyfin.trickplayAt(jf.tp, sec) : null;
247
+ if (!pic) { return; }
248
+ if (!at) { pic.className = 'hidden'; return; }
249
+ var scale = 320 / at.w;
250
+ pic.className = '';
251
+ pic.style.width = '320px';
252
+ pic.style.height = Math.round(at.h * scale) + 'px';
253
+ if (_picTile !== at.url) { _picTile = at.url; pic.style.backgroundImage = 'url("' + at.url + '")'; }
254
+ // (the tiles either side read ahead: moving into one shows it at once, not black first)
255
+ var span = jf.tp.cols * jf.tp.rows * jf.tp.every / 1000;
256
+ [sec - span, sec + span].forEach(function (t) {
257
+ var near = t >= 0 ? Jellyfin.trickplayAt(jf.tp, t) : null;
258
+ if (!near || typeof Image === 'undefined' || _picAhead.some(function (im) { return im.__url === near.url; })) { return; }
259
+ var im = new Image();
260
+ im.__url = near.url;
261
+ im.src = near.url;
262
+ _picAhead.push(im);
263
+ if (_picAhead.length > 4) { _picAhead.shift(); } // (kept: the last few, so they stay decoded)
264
+ });
265
+ pic.style.backgroundSize = Math.round(at.cols * at.w * scale) + 'px ' + Math.round(at.rows * at.h * scale) + 'px';
266
+ pic.style.backgroundPosition = '-' + Math.round(at.x * scale) + 'px -' + Math.round(at.y * scale) + 'px';
239
267
  }
240
268
 
241
269
  // ---------- in-player menu (stack of levels) ----------
package/js/player-core.js CHANGED
@@ -214,8 +214,13 @@ var PlayerParts = {};
214
214
  function hideOSD() { U.byId('player-osd').className = 'hidden'; liftSubs(0); }
215
215
  function osdHidden() { return U.byId('player-osd').className.indexOf('hidden') !== -1; }
216
216
 
217
- // ---------- chapters (a YouTube video's, from the service: S.ytInfo.chapters) ----------
218
- function chapters() { return (!S.isLive && S.ytInfo && S.ytInfo.chapters) || []; }
217
+ // ---------- chapters (a YouTube video's, from the service: S.ytInfo.chapters; a Jellyfin
218
+ // file's named ones: its source's _jf.chapters) ----------
219
+ function chapters() {
220
+ if (S.isLive) { return []; }
221
+ var jf = S.ctx && S.ctx.stream && S.ctx.stream._jf;
222
+ return (S.ytInfo && S.ytInfo.chapters) || (jf && Array.isArray(jf.chapters) ? jf.chapters : []);
223
+ }
219
224
  // The chapter playing at `sec`: its index, or -1 (no chapters, or before the first).
220
225
  function chapterAt(sec) {
221
226
  var list = chapters(), at = -1;
@@ -439,6 +444,14 @@ var PlayerParts = {};
439
444
  if (S.isLive) { vodCheck(ctx); } // (one whose manifest says it's a film: not live after all)
440
445
  formatStart(ctx);
441
446
  S.ctx.resumeSec = (ctx.trailer || ctx.fromStart) ? 0 : (ctx.resumeSec || getResume(ctx.type, ctx.id)); // (Play from start: 0:00)
447
+ // (a Jellyfin source: where Jellyfin last had it, when that's later than StremioK's place)
448
+ if (!ctx.trailer && !ctx.fromStart && !ctx.switching && ctx.stream && ctx.stream._jf && typeof Jellyfin !== 'undefined' && Jellyfin.resumeFor) {
449
+ var jfAt = Jellyfin.resumeFor(ctx.stream._jf, P.progressFor ? P.progressFor(ctx.type, ctx.id) : null, S.ctx.resumeSec);
450
+ if (jfAt !== S.ctx.resumeSec) {
451
+ S.ctx.resumeSec = jfAt;
452
+ if (jfAt > 5) { U.toast('Carrying on from where ' + (Jellyfin.sourceKind ? Jellyfin.sourceKind(ctx.stream._jf) : 'Jellyfin') + ' had it: ' + U.fmtTime(jfAt), 3500); }
453
+ }
454
+ }
442
455
 
443
456
  // reset subtitle + audio + quality + on-screen controls state
444
457
  resetSubs();
@@ -529,7 +542,9 @@ var PlayerParts = {};
529
542
  if (!ctx || ctx.trailer || S.isLive || !(S.dur > 0) || _segmentsFor === ctx) { return; }
530
543
  _segmentsFor = ctx;
531
544
  var dur = S.dur, asked = [];
532
- Stremio.getIntroSegments(ctx.type, ctx.id, Math.round(dur * 1000), asked, ctx.titleIds).then(function (seg) {
545
+ // (a Jellyfin source: its server's own media segments first, the services fill the rest)
546
+ var own = (ctx.stream && ctx.stream._jf && typeof Jellyfin !== 'undefined' && Jellyfin.segments) ? Jellyfin.segments(ctx.stream._jf, asked) : Promise.resolve(null);
547
+ own.then(function (start) { return Stremio.getIntroSegments(ctx.type, ctx.id, Math.round(dur * 1000), asked, ctx.titleIds, start); }).then(function (seg) {
533
548
  if (!S.active || S.ctx !== ctx) { return; }
534
549
  var notes = [];
535
550
  _segments = checkedSegments(seg, S.dur || dur, notes);
@@ -1020,8 +1035,15 @@ var PlayerParts = {};
1020
1035
  if (S.cur >= end - PREFETCH_LEAD) { _prefetchedNext = true; try { S.ctx.prefetchNext(); } catch (e) {} }
1021
1036
  }
1022
1037
 
1038
+ // A Jellyfin server's file: what you play told to it (Jellyfin.onPlayer: progress, pause,
1039
+ // stop, watched; never during private viewing).
1040
+ function jfTell(ev) {
1041
+ if (!S.ctx || !S.ctx.stream || !S.ctx.stream._jf || typeof Jellyfin === 'undefined' || !Jellyfin.onPlayer) { return; }
1042
+ try { Jellyfin.onPlayer(ev, S.ctx, S.cur, S.dur, ev === 'pause' || (ev === 'progress' && isPaused())); } catch (e) {}
1043
+ }
1023
1044
  function onProgress() {
1024
1045
  maybePrefetchNext();
1046
+ jfTell('progress');
1025
1047
  // The bar is redrawn on every time update only while it's on screen (showOSD
1026
1048
  // brings it up to date when it appears); hidden, it would just be wasted layout work.
1027
1049
  if (!osdHidden() || scrubbing()) { renderOSD(); }
@@ -1122,6 +1144,7 @@ var PlayerParts = {};
1122
1144
  // way (stopped past 85%: a finished scrobble), so not as a separate "watched" too.
1123
1145
  function markFinished(quiet) {
1124
1146
  setWatched(S.ctx.type, S.ctx.id, true, quiet); // (and to Trakt, when connected)
1147
+ jfTell('watched');
1125
1148
  removeProgress(S.ctx.type, S.ctx.id);
1126
1149
  if (S.ctx.nextEpisode && !(S.ctx.nextEpisode.airs > Date.now())) { // (not one that hasn't aired)
1127
1150
  var ne = S.ctx.nextEpisode;
@@ -1140,7 +1163,7 @@ var PlayerParts = {};
1140
1163
  return S.videoEl.paused;
1141
1164
  }
1142
1165
  function play_() { if (useAV) { try { avplay.play(); } catch (e) {} } else { S.videoEl.play(); } screenSaver(false); }
1143
- function pause_() { _pausedOnce = true; if (useAV) { try { avplay.pause(); } catch (e) {} } else { S.videoEl.pause(); } screenSaver(true); }
1166
+ function pause_() { _pausedOnce = true; jfTell('pause'); if (useAV) { try { avplay.pause(); } catch (e) {} } else { S.videoEl.pause(); } screenSaver(true); }
1144
1167
 
1145
1168
  // The TV's screensaver stays off while a video plays and is allowed again when it's
1146
1169
  // paused or stopped. No-op where the Samsung appcommon API isn't available.
@@ -1549,6 +1572,7 @@ var PlayerParts = {};
1549
1572
  if (U.crumb) { U.crumb('player closed'); }
1550
1573
  if (P.sleepClear) { P.sleepClear(); } // (the sleep timer: this playback's)
1551
1574
  saveProgress();
1575
+ jfTell('stop');
1552
1576
  // Record partial progress to Trakt (unfinished VOD) so it shows in Continue Watching. Past
1553
1577
  // 85% (Trakt connected): finished, Trakt's way (it marks it watched), and here as at the credits.
1554
1578
  if (!S.isLive && !_creditsHandled && S.ctx && !S.ctx.trailer && !S.ctx.private && !isNotice() && S.cur > 60 && typeof Trakt !== 'undefined' && Trakt.onProgress) {