Implement basic SyncPlay settings

This commit is contained in:
Ionut Andrei Oanca 2020-10-14 20:40:46 +01:00 committed by Bill Thornton
parent 4a5051317b
commit 94c405f08e
20 changed files with 618 additions and 17 deletions

View file

@ -4,6 +4,7 @@
*/
import { Events } from 'jellyfin-apiclient';
import Settings from '../Settings';
import TimeSyncServer from './TimeSyncServer';
/**
@ -13,6 +14,9 @@ class TimeSyncCore {
constructor() {
this.manager = null;
this.timeSyncServer = null;
this.timeSyncDeviceId = Settings.get('timeSyncDevice') || 'server';
this.extraTimeOffset = Settings.getFloat('extraTimeOffset', 0.0);
}
/**
@ -31,6 +35,10 @@ class TimeSyncCore {
Events.trigger(this, 'time-sync-server-update', [timeOffset, ping]);
});
Events.on(Settings, 'extraTimeOffset', (event, value, oldValue) => {
this.extraTimeOffset = Settings.getFloat('extraTimeOffset', 0.0);
});
}
/**
@ -40,6 +48,32 @@ class TimeSyncCore {
this.timeSyncServer.forceUpdate();
}
/**
* Gets the list of available devices for time sync.
* @returns {Array} The list of devices.
*/
getDevices() {
const devices = [{
type: 'server',
id: 'server',
name: 'Server',
timeOffset: this.timeSyncServer.getTimeOffset(),
ping: this.timeSyncServer.getPing(),
peerTimeOffset: 0,
peerPing: 0
}];
return devices;
}
/**
* Gets the identifier of the selected device for time sync. Default value is 'server'.
* @returns {string} The identifier.
*/
getActiveDevice() {
return this.timeSyncDeviceId;
}
/**
* Gets the display name of the selected device for time sync.
* @returns {string} The display name.
@ -54,7 +88,8 @@ class TimeSyncCore {
* @returns {Date} Local time.
*/
remoteDateToLocal(remote) {
return this.timeSyncServer.remoteDateToLocal(remote);
const date = this.timeSyncServer.remoteDateToLocal(remote);
return this.offsetDate(date, -this.extraTimeOffset);
}
/**
@ -63,15 +98,35 @@ class TimeSyncCore {
* @returns {Date} Server time.
*/
localDateToRemote(local) {
return this.timeSyncServer.localDateToRemote(local);
const date = this.timeSyncServer.localDateToRemote(local);
return this.offsetDate(date, this.extraTimeOffset);
}
/**
* Gets time offset that should be used for time syncing, in milliseconds.
* Gets time offset that should be used for time syncing, in milliseconds. Takes into account server and active device selected for syncing.
* @returns {number} The time offset.
*/
getTimeOffset() {
return this.timeSyncServer.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);
}
}