Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor / sync ott changes #664

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/common/types/playlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type DRM = {
};
fairplay?: {
processSpcUrl: string;
certificateUrl: string;
};
};

Expand Down
8 changes: 5 additions & 3 deletions packages/hooks-react/src/useWatchHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import { VideoProgressMinMax } from '@jwp/ott-common/src/constants';

import { useWatchHistoryListener } from './useWatchHistoryListener';

export const useWatchHistory = (player: jwplayer.JWPlayer | undefined, item: PlaylistItem, seriesItem?: PlaylistItem) => {
export const useWatchHistory = (item: PlaylistItem, seriesItem?: PlaylistItem) => {
// config
const { features } = useConfigStore((s) => s.config);
const continueWatchingList = features?.continueWatchingList;
const watchHistoryEnabled = !!continueWatchingList;

// watch history listener
useWatchHistoryListener(player, item, seriesItem);
const { saveWatchProgress, handleTimeUpdate } = useWatchHistoryListener(item, seriesItem);

// watch History
const watchHistoryItem = useWatchHistoryStore((state) => (!!item && watchHistoryEnabled ? state.getItem(item) : undefined));

// calculate the `startTime` of the current item based on the watch progress
return useMemo(() => {
const startTime = useMemo(() => {
const videoProgress = watchHistoryItem?.progress;

if (videoProgress && videoProgress > VideoProgressMinMax.Min && videoProgress < VideoProgressMinMax.Max) {
Expand All @@ -29,4 +29,6 @@ export const useWatchHistory = (player: jwplayer.JWPlayer | undefined, item: Pla
// start at the beginning of the video (only for VOD content)
return 0;
}, [item.duration, watchHistoryItem?.progress]);

return { startTime, saveWatchProgress, handleTimeUpdate, watchHistoryEnabled };
};
46 changes: 17 additions & 29 deletions packages/hooks-react/src/useWatchHistoryListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const PROGRESSIVE_SAVE_INTERVAL = 300_000; // 5 minutes
* item. When this needs to be saved, the queue is used to look up the last progress and item and save it when
* necessary. The queue is then cleared to prevent duplicate saves and API calls.
*/
export const useWatchHistoryListener = (player: jwplayer.JWPlayer | undefined, item: PlaylistItem, seriesItem?: PlaylistItem) => {
export const useWatchHistoryListener = (item: PlaylistItem, seriesItem?: PlaylistItem) => {
const queuedWatchProgress = useRef<QueuedProgress | null>(null);
const watchHistoryController = getModule(WatchHistoryController);

Expand All @@ -40,7 +40,7 @@ export const useWatchHistoryListener = (player: jwplayer.JWPlayer | undefined, i
const watchHistoryEnabled = !!continueWatchingList;

// maybe store the watch progress when we have a queued watch progress
const maybeSaveWatchProgress = useCallback(() => {
const saveWatchProgress = useCallback(() => {
if (!watchHistoryEnabled || !queuedWatchProgress.current) return;

const { item, seriesItem, progress } = queuedWatchProgress.current;
Expand All @@ -53,11 +53,11 @@ export const useWatchHistoryListener = (player: jwplayer.JWPlayer | undefined, i
}, [watchHistoryEnabled, watchHistoryController]);

// update the queued watch progress on each time update event
const handleTimeUpdate = useEventCallback((event: jwplayer.TimeParam) => {
const handleTimeUpdate = useEventCallback(({ position, duration }: { position: number; duration: number }) => {
// live streams have a negative duration, we ignore these for now
if (event.duration < 0) return;
if (duration < 0) return;

const progress = Number((event.position / event.duration).toFixed(5));
const progress = Number((position / duration).toFixed(5));

if (!isNaN(progress) && isFinite(progress)) {
queuedWatchProgress.current = {
Expand All @@ -69,45 +69,33 @@ export const useWatchHistoryListener = (player: jwplayer.JWPlayer | undefined, i

// save the progress when we haven't done so in the last X minutes
if (Date.now() - queuedWatchProgress.current.timestamp > PROGRESSIVE_SAVE_INTERVAL) {
maybeSaveWatchProgress();
saveWatchProgress();
}
}
});

// listen for certain player events
useEffect(() => {
if (!player || !watchHistoryEnabled) return;

player.on('time', handleTimeUpdate);
player.on('pause', maybeSaveWatchProgress);
player.on('complete', maybeSaveWatchProgress);
player.on('remove', maybeSaveWatchProgress);

return () => {
player.off('time', handleTimeUpdate);
player.off('pause', maybeSaveWatchProgress);
player.off('complete', maybeSaveWatchProgress);
player.off('remove', maybeSaveWatchProgress);
};
}, [player, watchHistoryEnabled, maybeSaveWatchProgress, handleTimeUpdate]);

useEffect(() => {
return () => {
// store watch progress on unmount and when the media item changes
maybeSaveWatchProgress();
saveWatchProgress();
};
}, [item?.mediaid, maybeSaveWatchProgress]);
}, [item?.mediaid, saveWatchProgress]);

// add event listeners for unload and visibility change to ensure the latest watch progress is saved
useLayoutEffect(() => {
const visibilityListener = () => document.visibilityState === 'hidden' && maybeSaveWatchProgress();
const visibilityListener = () => document.visibilityState === 'hidden' && saveWatchProgress();

window.addEventListener('pagehide', maybeSaveWatchProgress);
window.addEventListener('pagehide', saveWatchProgress);
window.addEventListener('visibilitychange', visibilityListener);

return () => {
window.removeEventListener('pagehide', maybeSaveWatchProgress);
window.removeEventListener('pagehide', saveWatchProgress);
window.removeEventListener('visibilitychange', visibilityListener);
};
}, [maybeSaveWatchProgress]);
}, [saveWatchProgress]);

return {
saveWatchProgress,
handleTimeUpdate,
};
};
21 changes: 21 additions & 0 deletions packages/ui-react/src/components/Player/Player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ type Props = {
onReady?: (player?: JWPlayer) => void;
onPlay?: () => void;
onPause?: () => void;
onTime?: (params: { position: number; duration: number }) => void;
onSeek?: (params: { offset: number; position: number; duration: number }) => void;
onSeeked?: () => void;
onComplete?: () => void;
onUserActive?: () => void;
onUserInActive?: () => void;
Expand All @@ -33,6 +36,7 @@ type Props = {
onBackClick?: () => void;
onPlaylistItem?: () => void;
onPlaylistItemCallback?: (item: PlaylistItem) => Promise<undefined | PlaylistItem>;
onAdImpression?: () => void;
};

const Player: React.FC<Props> = ({
Expand All @@ -41,6 +45,9 @@ const Player: React.FC<Props> = ({
onReady,
onPlay,
onPause,
onTime,
onSeek,
onSeeked,
onComplete,
onUserActive,
onUserInActive,
Expand All @@ -50,6 +57,7 @@ const Player: React.FC<Props> = ({
onPlaylistItem,
onPlaylistItemCallback,
onNext,
onAdImpression,
onBackClick,
feedId,
startTime = 0,
Expand All @@ -71,7 +79,11 @@ const Player: React.FC<Props> = ({
const handleBeforePlay = useEventCallback(onBeforePlay);
const handlePlay = useEventCallback(onPlay);
const handlePause = useEventCallback(onPause);
const handleTime = useEventCallback(onTime);
const handleComplete = useEventCallback(onComplete);
const handleSeek = useEventCallback(onSeek);
const handleSeeked = useEventCallback(onSeeked);
const handleAdImpression = useEventCallback(onAdImpression);
const handleUserActive = useEventCallback(onUserActive);
const handleUserInactive = useEventCallback(onUserInActive);
const handleFirstFrame = useEventCallback(() => {
Expand Down Expand Up @@ -100,6 +112,10 @@ const Player: React.FC<Props> = ({
playerRef.current?.on('complete', handleComplete);
playerRef.current?.on('play', handlePlay);
playerRef.current?.on('pause', handlePause);
playerRef.current?.on('time', handleTime);
playerRef.current?.on('seek', handleSeek);
playerRef.current?.on('seeked', handleSeeked);
playerRef.current?.on('adImpression', handleAdImpression);
playerRef.current?.on('userActive', handleUserActive);
playerRef.current?.on('userInactive', handleUserInactive);
playerRef.current?.on('firstFrame', handleFirstFrame);
Expand All @@ -114,6 +130,10 @@ const Player: React.FC<Props> = ({
handleComplete,
handlePlay,
handlePause,
handleTime,
handleSeek,
handleSeeked,
handleAdImpression,
handleUserActive,
handleUserInactive,
handleFirstFrame,
Expand Down Expand Up @@ -243,6 +263,7 @@ const Player: React.FC<Props> = ({
return;
}
playerRef.current.remove();
playerRef.current = undefined;
}
};
}, [detachEvents, backClickRef]);
Expand Down
4 changes: 4 additions & 0 deletions packages/ui-react/src/components/Shelf/Shelf.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
font-size: 24px;
white-space: nowrap;
text-overflow: ellipsis;

@include responsive.mobile-only() {
font-size: 20px;
}
}

.slider {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useState } from 'react';
import React, { useState } from 'react';
import type { PlaylistItem } from '@jwp/ott-common/types/playlist';
import { useWatchHistory } from '@jwp/ott-hooks-react/src/useWatchHistory';
import { usePlaylistItemCallback } from '@jwp/ott-hooks-react/src/usePlaylistItemCallback';
Expand Down Expand Up @@ -51,22 +51,43 @@ const PlayerContainer: React.FC<Props> = ({
// state
const [playerInstance, setPlayerInstance] = useState<JWPlayer>();

// watch history
const startTime = useWatchHistory(playerInstance, item, seriesItem);
const watchHistory = useWatchHistory(item, seriesItem);

// player events
const handleReady = useCallback((player?: JWPlayer) => {
const handleReady = (player?: JWPlayer) => {
setPlayerInstance(player);
}, []);
};

const handleFirstFrame = useCallback(() => {
const handleFirstFrame = () => {
// when playing a livestream, the first moment we can seek to the beginning of the DVR range is after the
// firstFrame event.
// @todo this doesn't seem to work 100% out of the times. Confirm with player team if this is the best approach.
if (liveFromBeginning) {
playerInstance?.seek(0);
}
}, [liveFromBeginning, playerInstance]);
};

const handleTimeEvent = (params: { position: number; duration: number }) => {
watchHistory.handleTimeUpdate(params);
};

const handlePlay = () => {
watchHistory.saveWatchProgress();
onPlay?.();
};

const handlePause = () => {
watchHistory.saveWatchProgress();
onPause?.();
};

const handleComplete = () => {
onComplete?.();
};

const handleRemove = () => {
watchHistory.saveWatchProgress();
};

const handlePlaylistItemCallback = usePlaylistItemCallback(liveStartDateTime, liveEndDateTime);

Expand All @@ -92,15 +113,17 @@ const PlayerContainer: React.FC<Props> = ({
adsData={adsData}
onReady={handleReady}
onFirstFrame={handleFirstFrame}
onPlay={onPlay}
onPause={onPause}
onComplete={onComplete}
onPlay={handlePlay}
onPause={handlePause}
onTime={handleTimeEvent}
onComplete={handleComplete}
onUserActive={onUserActive}
onUserInActive={onUserInActive}
onRemove={handleRemove}
onNext={onNext}
onBackClick={onBackClick}
onPlaylistItemCallback={handlePlaylistItemCallback}
startTime={startTime}
startTime={watchHistory.startTime}
autostart={autostart}
/>
);
Expand Down
Loading