Skip to content

Commit

Permalink
fix(srgssr-middleware): safari stalls at end of blocked segment
Browse files Browse the repository at this point in the history
Adds a `tolerance` to prevent some `Safari` browsers from getting stuck at the
`endTime` of a blocked segment.

> [!NOTE]
To maintain a certain consistency in the
experience, this tolerance is applied to all browsers.

- add a tolerance of 0.1s `handleCurrentTime`
- update the related test case
- adds a missing test case for `srgAnalytics`
  • Loading branch information
amtins committed Mar 15, 2024
1 parent 8629f66 commit c0f5105
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/middleware/srgssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,11 +486,15 @@ class SrgSsr {
return currentTime;
}

// as a workaround, add 0.1 seconds to avoid getting stuck on endTime on
// some safaris.
const endTimeWithTolerance = blockedSegment.endTime + 0.1;

// proxy for handling cuechange events at the player level
player.trigger({ type: 'srgssr/blocked-segment', data: blockedSegment });
player.currentTime(blockedSegment.endTime);
player.currentTime(endTimeWithTolerance);

return blockedSegment.endTime;
return endTimeWithTolerance;
}

/**
Expand Down Expand Up @@ -563,7 +567,7 @@ class SrgSsr {
debug: player.debug(),
playerVersion: Pillarbox.VERSION.pillarbox,
tagCommanderScriptURL:
player.options().srgOptions.tagCommanderScriptURL,
player.options().srgOptions.tagCommanderScriptURL,
});

player.options({
Expand Down
17 changes: 14 additions & 3 deletions test/middleware/srgssr.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,16 @@ describe('SrgSsr', () => {

expect(spyOnOptions).toHaveBeenLastCalledWith(expect.objectContaining({ trackers: { srgAnalytics: expect.any(Object) }}));
});

it('should not reinitialize the srgAnalytics', () => {
player.options().trackers.srgAnalytics = {};

const spyOnOptions = jest.spyOn(player, 'options');

SrgSsr.srgAnalytics(player);

expect(spyOnOptions).not.toHaveBeenLastCalledWith(expect.objectContaining({ trackers: { srgAnalytics: expect.any(Object) }}));
});
});

/**
Expand Down Expand Up @@ -1018,7 +1028,7 @@ describe('SrgSsr', () => {
expect(middleware.currentTime(currentTime)).toBe(currentTime);
});

it('should return the blocked segment end time', () => {
it('should return the blocked segment end time with a 0.1 second tolerance', () => {
const spyOnPlayerCurrentTime = jest.spyOn(player, 'currentTime');
const spyOnPlayerTrigger = jest.spyOn(player, 'trigger');
const middleware = SrgSsr.middleware(player);
Expand All @@ -1028,13 +1038,14 @@ describe('SrgSsr', () => {
endTime: 20,
text: JSON.stringify('data')
};
const endTimeWithTolerance = blockedSegmentCue.endTime + 0.1;

player.textTracks().getTrackById.mockReturnValueOnce({
activeCues: [blockedSegmentCue]
});

expect(middleware.currentTime(currentTime)).toBe(blockedSegmentCue.endTime);
expect(spyOnPlayerCurrentTime).toHaveBeenCalledWith(blockedSegmentCue.endTime);
expect(middleware.currentTime(currentTime)).toBe(endTimeWithTolerance);
expect(spyOnPlayerCurrentTime).toHaveBeenCalledWith(endTimeWithTolerance);
expect(spyOnPlayerTrigger).toHaveBeenCalledWith({ 'data': blockedSegmentCue, 'type': 'srgssr/blocked-segment' });
});
});
Expand Down

0 comments on commit c0f5105

Please sign in to comment.