diff --git a/src/components/syncPlay/core/PlaybackCore.js b/src/components/syncPlay/core/PlaybackCore.js index 479377327..18a9c43e6 100644 --- a/src/components/syncPlay/core/PlaybackCore.js +++ b/src/components/syncPlay/core/PlaybackCore.js @@ -538,9 +538,6 @@ class PlaybackCore { // Diff might be caused by the player internally starting the playback. const diffMillis = (serverPositionTicks - currentPositionTicks) / Helper.TicksPerMillisecond; - // Adapt playback diff to selected device for time syncing. - const targetPlaybackDiff = diffMillis - this.timeSyncCore.getPlaybackDiff(); - // Notify update for playback sync. this.playbackDiffMillis = diffMillis; Events.trigger(this.manager, 'playback-diff', [this.playbackDiffMillis]); @@ -553,22 +550,22 @@ class PlaybackCore { const playerWrapper = this.manager.getPlayerWrapper(); if (this.syncEnabled && this.enableSyncCorrection) { - const absDiffMillis = Math.abs(targetPlaybackDiff); + const absDiffMillis = Math.abs(diffMillis); // TODO: SpeedToSync sounds bad on songs. // TODO: SpeedToSync is failing on Safari (Mojave); even if playbackRate is supported, some delay seems to exist. // TODO: both SpeedToSync and SpeedToSync seem to have a hard time keeping up on Android Chrome as well. if (playerWrapper.hasPlaybackRate() && this.useSpeedToSync && absDiffMillis >= this.minDelaySpeedToSync && absDiffMillis < this.maxDelaySpeedToSync) { // Fix negative speed when client is ahead of time more than speedToSyncTime. const MinSpeed = 0.2; - if (targetPlaybackDiff <= -speedToSyncTime * MinSpeed) { - speedToSyncTime = Math.abs(targetPlaybackDiff) / (1.0 - MinSpeed); + if (diffMillis <= -speedToSyncTime * MinSpeed) { + speedToSyncTime = Math.abs(diffMillis) / (1.0 - MinSpeed); } // SpeedToSync strategy. - const speed = 1 + targetPlaybackDiff / speedToSyncTime; + const speed = 1 + diffMillis / speedToSyncTime; if (speed <= 0) { - console.error('SyncPlay error: speed should not be negative!', speed, targetPlaybackDiff, speedToSyncTime); + console.error('SyncPlay error: speed should not be negative!', speed, diffMillis, speedToSyncTime); } playerWrapper.setPlaybackRate(speed); diff --git a/src/components/syncPlay/core/timeSync/TimeSyncCore.js b/src/components/syncPlay/core/timeSync/TimeSyncCore.js index 1b5bf3165..2413bf4d7 100644 --- a/src/components/syncPlay/core/timeSync/TimeSyncCore.js +++ b/src/components/syncPlay/core/timeSync/TimeSyncCore.js @@ -7,6 +7,16 @@ import { Events } from 'jellyfin-apiclient'; import Settings from '../Settings'; import TimeSyncServer from './TimeSyncServer'; +/** + * Utility function to offset a given date by a given amount of milliseconds. + * @param {Date} date The date. + * @param {number} offset The offset, in milliseconds. + * @returns {Date} The offset date. + */ +function offsetDate(date, offset) { + return new Date(date.getTime() + offset); +} + /** * Class that manages time syncing with several devices. */ @@ -89,7 +99,7 @@ class TimeSyncCore { */ remoteDateToLocal(remote) { const date = this.timeSyncServer.remoteDateToLocal(remote); - return this.offsetDate(date, -this.extraTimeOffset); + return offsetDate(date, -this.extraTimeOffset); } /** @@ -99,7 +109,7 @@ class TimeSyncCore { */ localDateToRemote(local) { const date = this.timeSyncServer.localDateToRemote(local); - return this.offsetDate(date, this.extraTimeOffset); + return offsetDate(date, this.extraTimeOffset); } /** @@ -109,25 +119,6 @@ class TimeSyncCore { getTimeOffset() { return this.timeSyncServer.getTimeOffset() + this.extraTimeOffset; } - - /** - * Gets the playback diff that should be used to offset local playback, in milliseconds. - * @returns {number} The time offset. - */ - getPlaybackDiff() { - // TODO: this will use playback data from WebRTC peers. - return 0; - } - - /** - * Offsets a given date by a given ammount of milliseconds. - * @param {Date} date The date. - * @param {number} offset The offset, in milliseconds. - * @returns {Date} The offset date. - */ - offsetDate(date, offset) { - return new Date(date.getTime() + offset); - } } export default TimeSyncCore;