Skip to content

Commit

Permalink
Added check when time is changed externally to snap to range
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenwf committed May 4, 2023
1 parent 889adf9 commit 7d80d2d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/components/av-component.ts
Expand Up @@ -363,10 +363,10 @@ export class AVComponent extends BaseComponent {
}
}

public async setCurrentTime(time: number): Promise<void> {
public async setCurrentTime(time: number, external = false): Promise<void> {
const canvas: CanvasInstance | undefined = this._getCurrentCanvas();
if (canvas) {
return canvas.setCurrentTime(time as TimelineTime);
return canvas.setCurrentTime(time as TimelineTime, external);
}
return;
}
Expand Down
15 changes: 12 additions & 3 deletions src/components/canvas-instance.ts
Expand Up @@ -666,6 +666,10 @@ export class CanvasInstance extends BaseComponent {
return null;
}

getDuration() {
return this._getDuration();
}

private _getDuration(): TimelineTime {
if (this.isVirtual() && AVComponent.newRanges) {
return this.timePlanPlayer.getDuration();
Expand Down Expand Up @@ -1610,9 +1614,9 @@ export class CanvasInstance extends BaseComponent {
}
}

public setCurrentTime(seconds: TimelineTime): Promise<void> {
public setCurrentTime(seconds: TimelineTime, external = false): Promise<void> {
Logger.log('External set current time?');
return this._setCurrentTime(seconds, false);
return this._setCurrentTime(seconds, false, external);
}

now(): TimelineTime {
Expand All @@ -1623,9 +1627,14 @@ export class CanvasInstance extends BaseComponent {
return Date.now() as TimelineTimeMs;
}

private async _setCurrentTime(seconds: TimelineTime, setRange = true): Promise<void> {
private async _setCurrentTime(seconds: TimelineTime, setRange = true, external = false): Promise<void> {
if (AVComponent.newRanges && this.isVirtual()) {
this._buffering = true;

if (external) {
seconds = this.timePlanPlayer.validateExternalTime(seconds);
}

await this.timePlanPlayer.setTime(seconds, setRange);
this._buffering = false;
this._canvasClockStartDate = toMs(minusTime(this.now(), this._canvasClockTime));
Expand Down
15 changes: 15 additions & 0 deletions src/elements/timeplan-player.ts
Expand Up @@ -208,6 +208,21 @@ export class TimePlanPlayer {
return annotationTime(addTime(time, timelineTime(this.currentStop.canvasTime.start)));
}

validateExternalTime(time: TimelineTime): TimelineTime {
// The time externally may be rounded.
// For example, a track with a duration of 1200.51 seconds may be rounded to 1200
// This means that the time may be slightly off the intention is to skip forward to the next stop.
// We need to check if the time is within 1 second of the next stop.
// If it is, we should skip to the next stop.
const currentStop = this.findStop(time);
const nextStop = this.findStop(time + 1);
if (nextStop && currentStop !== nextStop) {
Logger.log('Skipping to next stop', { nextStop, currentStop });
return nextStop?.start;
}
return time;
}

pause(): TimelineTime {
this.log('Pause', this.getTime());
this.setIsPlaying(false);
Expand Down

0 comments on commit 7d80d2d

Please sign in to comment.