mpvVideoPlayer.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /* eslint-disable indent */
  2. function getMediaStreamAudioTracks(mediaSource) {
  3. return mediaSource.MediaStreams.filter(function (s) {
  4. return s.Type === 'Audio';
  5. });
  6. }
  7. class mpvVideoPlayer {
  8. constructor({ events, loading, appRouter, globalize, appHost, appSettings }) {
  9. this.events = events;
  10. this.loading = loading;
  11. this.appRouter = appRouter;
  12. this.globalize = globalize;
  13. this.appHost = appHost;
  14. this.appSettings = appSettings;
  15. /**
  16. * @type {string}
  17. */
  18. this.name = 'MPV Video Player';
  19. /**
  20. * @type {string}
  21. */
  22. this.type = 'mediaplayer';
  23. /**
  24. * @type {string}
  25. */
  26. this.id = 'mpvvideoplayer';
  27. this.syncPlayWrapAs = 'htmlvideoplayer';
  28. this.priority = -1;
  29. this.useFullSubtitleUrls = true;
  30. /**
  31. * @type {boolean}
  32. */
  33. this.isFetching = false;
  34. /**
  35. * @type {HTMLDivElement | null | undefined}
  36. */
  37. this._videoDialog = undefined;
  38. /**
  39. * @type {number | undefined}
  40. */
  41. this._subtitleTrackIndexToSetOnPlaying = undefined;
  42. /**
  43. * @type {number | null}
  44. */
  45. this._audioTrackIndexToSetOnPlaying = undefined;
  46. /**
  47. * @type {boolean | undefined}
  48. */
  49. this._showTrackOffset = undefined;
  50. /**
  51. * @type {number | undefined}
  52. */
  53. this._currentTrackOffset = undefined;
  54. /**
  55. * @type {string[] | undefined}
  56. */
  57. this._supportedFeatures = undefined;
  58. /**
  59. * @type {string | undefined}
  60. */
  61. this._currentSrc = undefined;
  62. /**
  63. * @type {boolean | undefined}
  64. */
  65. this._started = undefined;
  66. /**
  67. * @type {boolean | undefined}
  68. */
  69. this._timeUpdated = undefined;
  70. /**
  71. * @type {number | null | undefined}
  72. */
  73. this._currentTime = undefined;
  74. /**
  75. * @private (used in other files)
  76. * @type {any | undefined}
  77. */
  78. this._currentPlayOptions = undefined;
  79. /**
  80. * @type {any | undefined}
  81. */
  82. this._lastProfile = undefined;
  83. /**
  84. * @type {number | undefined}
  85. */
  86. this._duration = undefined;
  87. /**
  88. * @type {boolean}
  89. */
  90. this._paused = false;
  91. /**
  92. * @type {int}
  93. */
  94. this._volume = 100;
  95. /**
  96. * @type {boolean}
  97. */
  98. this._muted = false;
  99. /**
  100. * @type {float}
  101. */
  102. this._playRate = 1;
  103. /**
  104. * @type {boolean}
  105. */
  106. this._hasConnection = false;
  107. /**
  108. * @private
  109. */
  110. this.onEnded = () => {
  111. this.onEndedInternal();
  112. };
  113. /**
  114. * @private
  115. */
  116. this.onTimeUpdate = (time) => {
  117. if (time && !this._timeUpdated) {
  118. this._timeUpdated = true;
  119. }
  120. this._currentTime = time;
  121. this.events.trigger(this, 'timeupdate');
  122. window.api.taskbar.setProgress(time * 100 / this._duration);
  123. };
  124. /**
  125. * @private
  126. */
  127. this.onNavigatedToOsd = () => {
  128. const dlg = this._videoDialog;
  129. if (dlg) {
  130. dlg.style.zIndex = 'unset';
  131. }
  132. };
  133. /**
  134. * @private
  135. */
  136. this.onPlaying = () => {
  137. if (!this._started) {
  138. this._started = true;
  139. this.loading.hide();
  140. const volume = this.getSavedVolume() * 100;
  141. if (volume != this._volume) {
  142. this.setVolume(volume, false);
  143. }
  144. this.setPlaybackRate(1);
  145. this.setMute(false);
  146. if (this._currentPlayOptions.fullscreen) {
  147. this.appRouter.showVideoOsd().then(this.onNavigatedToOsd);
  148. } else {
  149. this.appRouter.setTransparency('backdrop');
  150. this._videoDialog.dlg.style.zIndex = 'unset';
  151. }
  152. // Need to override default style.
  153. this._videoDialog.style.setProperty('background', 'transparent', 'important');
  154. window.api.taskbar.setControlsVisible(true);
  155. }
  156. if (this._paused) {
  157. this._paused = false;
  158. this.events.trigger(this, 'unpause');
  159. window.api.taskbar.setPaused(false);
  160. }
  161. this.events.trigger(this, 'playing');
  162. };
  163. /**
  164. * @private
  165. */
  166. this.onPause = () => {
  167. this._paused = true;
  168. // For Syncplay ready notification
  169. this.events.trigger(this, 'pause');
  170. window.api.taskbar.setPaused(true);
  171. };
  172. this.onWaiting = () => {
  173. this.events.trigger(this, 'waiting');
  174. };
  175. /**
  176. * @private
  177. * @param e {Event} The event received from the `<video>` element
  178. */
  179. this.onError = (error) => {
  180. console.error(`media element error: ${error}`);
  181. this.events.trigger(this, 'error', [
  182. {
  183. type: 'mediadecodeerror'
  184. }
  185. ]);
  186. };
  187. this.onDuration = (duration) => {
  188. this._duration = duration;
  189. };
  190. this.onPauseClicked = () => {
  191. this.paused() ? this.unpause() : this.pause();
  192. };
  193. }
  194. currentSrc() {
  195. return this._currentSrc;
  196. }
  197. async play(options) {
  198. this._started = false;
  199. this._timeUpdated = false;
  200. this._currentTime = null;
  201. this.resetSubtitleOffset();
  202. this.loading.show();
  203. window.api.power.setScreensaverEnabled(false);
  204. const elem = await this.createMediaElement(options);
  205. return await this.setCurrentSrc(elem, options);
  206. }
  207. getSavedVolume() {
  208. return this.appSettings.get('volume') || 1;
  209. }
  210. /**
  211. * @private
  212. */
  213. getSubtitleParam() {
  214. const options = this._currentPlayOptions;
  215. if (this._subtitleTrackIndexToSetOnPlaying != null && this._subtitleTrackIndexToSetOnPlaying >= 0) {
  216. const initialSubtitleStream = options.mediaSource.MediaStreams[this._subtitleTrackIndexToSetOnPlaying];
  217. if (!initialSubtitleStream || initialSubtitleStream.DeliveryMethod === 'Encode') {
  218. this._subtitleTrackIndexToSetOnPlaying = -1;
  219. } else if (initialSubtitleStream.DeliveryMethod === 'External') {
  220. return '#,' + initialSubtitleStream.DeliveryUrl;
  221. }
  222. }
  223. if (this._subtitleTrackIndexToSetOnPlaying == -1 || this._subtitleTrackIndexToSetOnPlaying == null) {
  224. return '';
  225. }
  226. return '#' + this._subtitleTrackIndexToSetOnPlaying;
  227. }
  228. tryGetFramerate(options) {
  229. if (options.mediaSource && options.mediaSource.MediaStreams) {
  230. for (let stream of options.mediaSource.MediaStreams) {
  231. if (stream.Type == "Video") {
  232. return stream.RealFrameRate || stream.AverageFrameRate || null;
  233. }
  234. }
  235. }
  236. }
  237. /**
  238. * @private
  239. */
  240. setCurrentSrc(elem, options) {
  241. return new Promise((resolve) => {
  242. const val = options.url;
  243. this._currentSrc = val;
  244. console.debug(`playing url: ${val}`);
  245. // Convert to seconds
  246. const ms = (options.playerStartPositionTicks || 0) / 10000;
  247. this._currentPlayOptions = options;
  248. this._subtitleTrackIndexToSetOnPlaying = options.mediaSource.DefaultSubtitleStreamIndex == null ? -1 : options.mediaSource.DefaultSubtitleStreamIndex;
  249. this._audioTrackIndexToSetOnPlaying = options.playMethod === 'Transcode' ? null : options.mediaSource.DefaultAudioStreamIndex;
  250. const streamdata = {type: 'video', headers: {'User-Agent': 'JellyfinMediaPlayer'}, media: {}};
  251. const fps = this.tryGetFramerate(options);
  252. if (fps) {
  253. streamdata.frameRate = fps;
  254. }
  255. const player = window.api.player;
  256. player.load(val,
  257. { startMilliseconds: ms, autoplay: true },
  258. streamdata,
  259. (this._audioTrackIndexToSetOnPlaying != null)
  260. ? '#' + this._audioTrackIndexToSetOnPlaying : '#1',
  261. this.getSubtitleParam(),
  262. resolve);
  263. });
  264. }
  265. setSubtitleStreamIndex(index) {
  266. this._subtitleTrackIndexToSetOnPlaying = index;
  267. window.api.player.setSubtitleStream(this.getSubtitleParam());
  268. }
  269. resetSubtitleOffset() {
  270. this._currentTrackOffset = 0;
  271. this._showTrackOffset = false;
  272. window.api.player.setSubtitleDelay(0);
  273. }
  274. enableShowingSubtitleOffset() {
  275. this._showTrackOffset = true;
  276. }
  277. disableShowingSubtitleOffset() {
  278. this._showTrackOffset = false;
  279. }
  280. isShowingSubtitleOffsetEnabled() {
  281. return this._showTrackOffset;
  282. }
  283. setSubtitleOffset(offset) {
  284. const offsetValue = parseFloat(offset);
  285. this._currentTrackOffset = offsetValue;
  286. window.api.player.setSubtitleDelay(Math.round(offsetValue * 1000));
  287. }
  288. getSubtitleOffset() {
  289. return this._currentTrackOffset;
  290. }
  291. /**
  292. * @private
  293. */
  294. isAudioStreamSupported() {
  295. return true;
  296. }
  297. /**
  298. * @private
  299. */
  300. getSupportedAudioStreams() {
  301. const profile = this._lastProfile;
  302. return getMediaStreamAudioTracks(this._currentPlayOptions.mediaSource).filter((stream) => {
  303. return this.isAudioStreamSupported(stream, profile);
  304. });
  305. }
  306. setAudioStreamIndex(index) {
  307. this._audioTrackIndexToSetOnPlaying = index;
  308. const streams = this.getSupportedAudioStreams();
  309. if (streams.length < 2) {
  310. // If there's only one supported stream then trust that the player will handle it on it's own
  311. return;
  312. }
  313. window.api.player.setAudioStream(index != -1 ? '#' + index : '');
  314. }
  315. onEndedInternal() {
  316. const stopInfo = {
  317. src: this._currentSrc
  318. };
  319. this.events.trigger(this, 'stopped', [stopInfo]);
  320. window.api.taskbar.setControlsVisible(false);
  321. this._currentTime = null;
  322. this._currentSrc = null;
  323. this._currentPlayOptions = null;
  324. }
  325. stop(destroyPlayer) {
  326. window.api.player.stop();
  327. window.api.power.setScreensaverEnabled(true);
  328. this.onEndedInternal();
  329. if (destroyPlayer) {
  330. this.destroy();
  331. }
  332. return Promise.resolve();
  333. }
  334. destroy() {
  335. window.api.player.stop();
  336. window.api.power.setScreensaverEnabled(true);
  337. this.appRouter.setTransparency('none');
  338. document.body.classList.remove('hide-scroll');
  339. const player = window.api.player;
  340. this._hasConnection = false;
  341. player.playing.disconnect(this.onPlaying);
  342. player.positionUpdate.disconnect(this.onTimeUpdate);
  343. player.finished.disconnect(this.onEnded);
  344. this._duration = undefined;
  345. player.updateDuration.disconnect(this.onDuration);
  346. player.error.disconnect(this.onError);
  347. player.paused.disconnect(this.onPause);
  348. window.api.taskbar.pauseClicked.disconnect(this.onPauseClicked);
  349. const dlg = this._videoDialog;
  350. if (dlg) {
  351. this._videoDialog = null;
  352. dlg.parentNode.removeChild(dlg);
  353. }
  354. // Only supporting QtWebEngine here
  355. if (document.webkitIsFullScreen && document.webkitExitFullscreen) {
  356. document.webkitExitFullscreen();
  357. }
  358. }
  359. /**
  360. * @private
  361. */
  362. createMediaElement(options) {
  363. const dlg = document.querySelector('.videoPlayerContainer');
  364. if (!dlg) {
  365. this.loading.show();
  366. const dlg = document.createElement('div');
  367. dlg.classList.add('videoPlayerContainer');
  368. dlg.style.position = 'fixed';
  369. dlg.style.top = 0;
  370. dlg.style.bottom = 0;
  371. dlg.style.left = 0;
  372. dlg.style.right = 0;
  373. dlg.style.display = 'flex';
  374. dlg.style.alignItems = 'center';
  375. if (options.fullscreen) {
  376. dlg.style.zIndex = 1000;
  377. }
  378. const html = '';
  379. dlg.innerHTML = html;
  380. document.body.insertBefore(dlg, document.body.firstChild);
  381. this._videoDialog = dlg;
  382. const player = window.api.player;
  383. if (!this._hasConnection) {
  384. this._hasConnection = true;
  385. player.playing.connect(this.onPlaying);
  386. player.positionUpdate.connect(this.onTimeUpdate);
  387. player.finished.connect(this.onEnded);
  388. player.updateDuration.connect(this.onDuration);
  389. player.error.connect(this.onError);
  390. player.paused.connect(this.onPause);
  391. window.api.taskbar.pauseClicked.connect(this.onPauseClicked);
  392. }
  393. if (options.fullscreen) {
  394. // At this point, we must hide the scrollbar placeholder, so it's not being displayed while the item is being loaded
  395. document.body.classList.add('hide-scroll');
  396. }
  397. return Promise.resolve();
  398. } else {
  399. // we need to hide scrollbar when starting playback from page with animated background
  400. if (options.fullscreen) {
  401. document.body.classList.add('hide-scroll');
  402. }
  403. return Promise.resolve();
  404. }
  405. }
  406. /**
  407. * @private
  408. */
  409. canPlayMediaType(mediaType) {
  410. return (mediaType || '').toLowerCase() === 'video';
  411. }
  412. /**
  413. * @private
  414. */
  415. supportsPlayMethod() {
  416. return true;
  417. }
  418. /**
  419. * @private
  420. */
  421. getDeviceProfile(item, options) {
  422. if (this.appHost.getDeviceProfile) {
  423. return this.appHost.getDeviceProfile(item, options);
  424. }
  425. return Promise.resolve({});
  426. }
  427. /**
  428. * @private
  429. */
  430. static getSupportedFeatures() {
  431. return ['PlaybackRate'];
  432. }
  433. supports(feature) {
  434. if (!this._supportedFeatures) {
  435. this._supportedFeatures = mpvVideoPlayer.getSupportedFeatures();
  436. }
  437. return this._supportedFeatures.includes(feature);
  438. }
  439. // Save this for when playback stops, because querying the time at that point might return 0
  440. currentTime(val) {
  441. if (val != null) {
  442. window.api.player.seekTo(val);
  443. return;
  444. }
  445. return this._currentTime;
  446. }
  447. currentTimeAsync() {
  448. return new Promise((resolve) => {
  449. window.api.player.getPosition(resolve);
  450. });
  451. }
  452. duration() {
  453. if (this._duration) {
  454. return this._duration;
  455. }
  456. return null;
  457. }
  458. canSetAudioStreamIndex() {
  459. return true;
  460. }
  461. static onPictureInPictureError(err) {
  462. console.error(`Picture in picture error: ${err}`);
  463. }
  464. setPictureInPictureEnabled() {}
  465. isPictureInPictureEnabled() {
  466. return false;
  467. }
  468. isAirPlayEnabled() {
  469. return false;
  470. }
  471. setAirPlayEnabled() {}
  472. setBrightness() {}
  473. getBrightness() {
  474. return 100;
  475. }
  476. seekable() {
  477. return Boolean(this._duration);
  478. }
  479. pause() {
  480. window.api.player.pause();
  481. }
  482. // This is a retry after error
  483. resume() {
  484. this._paused = false;
  485. window.api.player.play();
  486. }
  487. unpause() {
  488. window.api.player.play();
  489. }
  490. paused() {
  491. return this._paused;
  492. }
  493. setPlaybackRate(value) {
  494. this._playRate = value;
  495. window.api.player.setPlaybackRate(value * 1000);
  496. }
  497. getPlaybackRate() {
  498. return this._playRate;
  499. }
  500. getSupportedPlaybackRates() {
  501. return [{
  502. name: '0.5x',
  503. id: 0.5
  504. }, {
  505. name: '0.75x',
  506. id: 0.75
  507. }, {
  508. name: '1x',
  509. id: 1.0
  510. }, {
  511. name: '1.25x',
  512. id: 1.25
  513. }, {
  514. name: '1.5x',
  515. id: 1.5
  516. }, {
  517. name: '1.75x',
  518. id: 1.75
  519. }, {
  520. name: '2x',
  521. id: 2.0
  522. }];
  523. }
  524. saveVolume(value) {
  525. if (value) {
  526. this.appSettings.set('volume', value);
  527. }
  528. }
  529. setVolume(val, save = true) {
  530. this._volume = val;
  531. if (save) {
  532. this.saveVolume((val || 100) / 100);
  533. this.events.trigger(this, 'volumechange');
  534. }
  535. window.api.player.setVolume(val);
  536. }
  537. getVolume() {
  538. return this._volume;
  539. }
  540. volumeUp() {
  541. this.setVolume(Math.min(this.getVolume() + 2, 100));
  542. }
  543. volumeDown() {
  544. this.setVolume(Math.max(this.getVolume() - 2, 0));
  545. }
  546. setMute(mute) {
  547. this._muted = mute;
  548. window.api.player.setMuted(mute);
  549. }
  550. isMuted() {
  551. return this._muted;
  552. }
  553. setAspectRatio() {
  554. }
  555. getAspectRatio() {
  556. return this._currentAspectRatio || 'auto';
  557. }
  558. getSupportedAspectRatios() {
  559. return [{
  560. name: this.globalize.translate('Auto'),
  561. id: 'auto'
  562. }];
  563. }
  564. togglePictureInPicture() {
  565. }
  566. toggleAirPlay() {
  567. }
  568. getBufferedRanges() {
  569. return [];
  570. }
  571. getStats() {
  572. const playOptions = this._currentPlayOptions || [];
  573. const categories = [];
  574. if (!this._currentPlayOptions) {
  575. return Promise.resolve({
  576. categories: categories
  577. });
  578. }
  579. const mediaCategory = {
  580. stats: [],
  581. type: 'media'
  582. };
  583. categories.push(mediaCategory);
  584. if (playOptions.url) {
  585. // create an anchor element (note: no need to append this element to the document)
  586. let link = document.createElement('a');
  587. // set href to any path
  588. link.setAttribute('href', playOptions.url);
  589. const protocol = (link.protocol || '').replace(':', '');
  590. if (protocol) {
  591. mediaCategory.stats.push({
  592. label: this.globalize.translate('LabelProtocol'),
  593. value: protocol
  594. });
  595. }
  596. link = null;
  597. }
  598. mediaCategory.stats.push({
  599. label: this.globalize.translate('LabelStreamType'),
  600. value: 'Video'
  601. });
  602. const videoCategory = {
  603. stats: [],
  604. type: 'video'
  605. };
  606. categories.push(videoCategory);
  607. const audioCategory = {
  608. stats: [],
  609. type: 'audio'
  610. };
  611. categories.push(audioCategory);
  612. return Promise.resolve({
  613. categories: categories
  614. });
  615. }
  616. }
  617. /* eslint-enable indent */
  618. window._mpvVideoPlayer = mpvVideoPlayer;