-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
168 additions
and
5 deletions.
There are no files selected for viewing
146 changes: 146 additions & 0 deletions
146
src/apps/stable/features/playback/utils/mediaSegmentManager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import type { Api } from '@jellyfin/sdk/lib/api'; | ||
import type { MediaSegmentDto } from '@jellyfin/sdk/lib/generated-client/models/media-segment-dto'; | ||
import { MediaSegmentType } from '@jellyfin/sdk/lib/generated-client/models/media-segment-type'; | ||
import { MediaSegmentsApi } from '@jellyfin/sdk/lib/generated-client/api/media-segments-api'; | ||
|
||
import type { PlaybackManager } from 'components/playback/playbackmanager'; | ||
import ServerConnections from 'components/ServerConnections'; | ||
import { currentSettings as userSettings } from 'scripts/settings/userSettings'; | ||
import type { PlayerState } from 'types/playbackStopInfo'; | ||
import type { Event } from 'utils/events'; | ||
import { toApi } from 'utils/jellyfin-apiclient/compat'; | ||
|
||
import { PlaybackSubscriber } from './playbackSubscriber'; | ||
import { getMediaSegmentAction } from './mediaSegmentSettings'; | ||
import { MediaSegmentAction } from '../constants/mediaSegmentAction'; | ||
|
||
const isBeforeSegment = (segment: MediaSegmentDto, time: number, direction: number) => { | ||
if (direction === -1) { | ||
return ( | ||
typeof segment.EndTicks !== 'undefined' | ||
&& segment.EndTicks < time | ||
); | ||
} | ||
return ( | ||
typeof segment.StartTicks !== 'undefined' | ||
&& segment.StartTicks > time | ||
); | ||
}; | ||
|
||
const isInSegment = (segment: MediaSegmentDto, time: number) => ( | ||
typeof segment.StartTicks !== 'undefined' | ||
&& segment.StartTicks < time | ||
&& (typeof segment.EndTicks === 'undefined' || segment.EndTicks > time) | ||
); | ||
|
||
const findCurrentSegment = (segments: MediaSegmentDto[], time: number, lastIndex = 0) => { | ||
const lastSegment = segments[lastIndex]; | ||
if (isInSegment(lastSegment, time)) { | ||
return { index: lastIndex, segment: lastSegment }; | ||
} | ||
|
||
let direction = 1; | ||
if (lastIndex > 0 && lastSegment.StartTicks && lastSegment.StartTicks > time) { | ||
direction = -1; | ||
} | ||
|
||
for (let index = lastIndex, segment = segments[index]; index >= 0 && index < segments.length; index += direction) { | ||
if (isBeforeSegment(segment, time, direction)) return; | ||
if (isInSegment(segment, time)) return { index, segment }; | ||
} | ||
}; | ||
|
||
class MediaSegmentManager extends PlaybackSubscriber { | ||
private hasSegments = false; | ||
private lastIndex = 0; | ||
private mediaSegmentTypeActions: Record<Partial<MediaSegmentType>, MediaSegmentAction> | undefined; | ||
private mediaSegments: MediaSegmentDto[] = []; | ||
private serverId: string | undefined; | ||
|
||
private async fetchMediaSegments(api: Api, itemId: string, includeSegmentTypes: MediaSegmentType[]) { | ||
// FIXME: Replace with SDK getMediaSegmentsApi function when available in stable | ||
const mediaSegmentsApi = new MediaSegmentsApi(api.configuration, undefined, api.axiosInstance); | ||
|
||
try { | ||
const { data: mediaSegments } = await mediaSegmentsApi.getItemSegments({ itemId, includeSegmentTypes }); | ||
this.mediaSegments = mediaSegments.Items || []; | ||
} catch (err) { | ||
console.error('[MediaSegmentManager] failed to fetch segments', err); | ||
this.mediaSegments = []; | ||
} | ||
} | ||
|
||
private performAction(mediaSegment: MediaSegmentDto) { | ||
if (!this.mediaSegmentTypeActions || !mediaSegment.Type || !this.mediaSegmentTypeActions[mediaSegment.Type]) { | ||
console.error('[MediaSegmentManager] segment type missing from action map', mediaSegment, this.mediaSegmentTypeActions); | ||
return; | ||
} | ||
|
||
const action = this.mediaSegmentTypeActions[mediaSegment.Type]; | ||
if (action === MediaSegmentAction.Skip) { | ||
// Perform skip | ||
if (mediaSegment.EndTicks) { | ||
this.playbackManager.seek(mediaSegment.EndTicks, this.player); | ||
} else { | ||
this.playbackManager.nextTrack(this.player); | ||
} | ||
} | ||
} | ||
|
||
onPlayerPlaybackStart(_e: Event, state: PlayerState) { | ||
this.lastIndex = 0; | ||
this.hasSegments = !!state.MediaSource?.HasSegments; | ||
// XXX: replace | ||
// this.hasSegments = true; | ||
// TODO: Might not need this at the class level | ||
this.serverId = state.NowPlayingItem?.ServerId || ServerConnections.currentApiClient()?.serverId(); | ||
const itemId = state.MediaSource?.Id; | ||
|
||
if (!this.hasSegments || !this.serverId || !itemId) return; | ||
|
||
// Get the user settings for media segment actions | ||
this.mediaSegmentTypeActions = Object.values(MediaSegmentType) | ||
.map(type => ({ | ||
type, | ||
action: getMediaSegmentAction(userSettings, type) | ||
})) | ||
.filter(({ action }) => !!action && action !== MediaSegmentAction.None) | ||
.reduce((acc, { type, action }) => { | ||
if (action) acc[type] = action; | ||
return acc; | ||
}, {} as Record<Partial<MediaSegmentType>, MediaSegmentAction>); | ||
|
||
if (!Object.keys(this.mediaSegmentTypeActions).length) { | ||
console.info('[MediaSegmentManager] user has no media segment actions enabled'); | ||
return; | ||
} | ||
|
||
const apiClient = ServerConnections.getApiClient(this.serverId); | ||
|
||
if (!apiClient.isMinServerVersion('10.10.0')) { | ||
console.warn('[MediaSegmentManager] server does not support media segments'); | ||
return; | ||
} | ||
|
||
const api = toApi(apiClient); | ||
void this.fetchMediaSegments( | ||
api, | ||
itemId, | ||
Object.keys(this.mediaSegmentTypeActions).map(t => t as keyof typeof MediaSegmentType)); | ||
} | ||
|
||
onPlayerTimeUpdate() { | ||
console.debug('update'); | ||
|
||
if (this.hasSegments && this.mediaSegments.length) { | ||
const time = this.playbackManager.currentTime(this.player) * 10000; | ||
const currentSegmentDetails = findCurrentSegment(this.mediaSegments, time, this.lastIndex); | ||
if (currentSegmentDetails) { | ||
this.performAction(currentSegmentDetails.segment); | ||
this.lastIndex = currentSegmentDetails.index; | ||
} | ||
} | ||
} | ||
} | ||
|
||
export const bindMediaSegmentManager = (playbackManager: PlaybackManager) => new MediaSegmentManager(playbackManager); |
14 changes: 14 additions & 0 deletions
14
src/apps/stable/features/playback/utils/mediaSegmentSettings.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { MediaSegmentType } from '@jellyfin/sdk/lib/generated-client/models/media-segment-type'; | ||
|
||
import { UserSettings } from 'scripts/settings/userSettings'; | ||
|
||
import { MediaSegmentAction } from '../constants/mediaSegmentAction'; | ||
|
||
const PREFIX = 'segmentTypeAction__'; | ||
|
||
const getId = (type: MediaSegmentType) => `${PREFIX}${type}`; | ||
|
||
export function getMediaSegmentAction(userSettings: UserSettings, type: MediaSegmentType): MediaSegmentAction | undefined { | ||
const action = userSettings.get(getId(type), false); | ||
return action ? action as MediaSegmentAction : undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters