-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Add media segment skipping #6157
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
809dba5
Add media segment action settings
thornbill b934500
Add media segment manager
thornbill 9aeb64e
Add tests for segment utils
thornbill 7c4962d
Skip skipping if skip is short
thornbill 9942fb7
Apply suggestions from code review
thornbill aef9482
Prevent skipping when seeking back to segment
thornbill File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
src/apps/stable/features/playback/constants/mediaSegmentAction.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,7 @@ | ||
/** | ||
* Actions that are triggered for media segments. | ||
*/ | ||
export enum MediaSegmentAction { | ||
None = 'None', | ||
Skip = 'Skip' | ||
} |
113 changes: 113 additions & 0 deletions
113
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,113 @@ | ||
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 { TICKS_PER_MILLISECOND, TICKS_PER_SECOND } from 'constants/time'; | ||
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 { getMediaSegmentAction } from './mediaSegmentSettings'; | ||
import { findCurrentSegment } from './mediaSegments'; | ||
import { PlaybackSubscriber } from './playbackSubscriber'; | ||
import { MediaSegmentAction } from '../constants/mediaSegmentAction'; | ||
|
||
class MediaSegmentManager extends PlaybackSubscriber { | ||
private hasSegments = false; | ||
private lastIndex = 0; | ||
private mediaSegmentTypeActions: Record<Partial<MediaSegmentType>, MediaSegmentAction> | undefined; | ||
private mediaSegments: MediaSegmentDto[] = []; | ||
|
||
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) { | ||
// Do not skip if duration < 1s to avoid slow stream changes | ||
if (mediaSegment.StartTicks && mediaSegment.EndTicks - mediaSegment.StartTicks < TICKS_PER_SECOND) { | ||
console.info('[MediaSegmentManager] ignoring skipping segment with duration <1s', mediaSegment); | ||
return; | ||
} | ||
|
||
console.debug('[MediaSegmentManager] skipping to %s ms', mediaSegment.EndTicks / TICKS_PER_MILLISECOND); | ||
this.playbackManager.seek(mediaSegment.EndTicks, this.player); | ||
} else { | ||
console.debug('[MediaSegmentManager] skipping to next item in queue'); | ||
this.playbackManager.nextTrack(this.player); | ||
} | ||
} | ||
} | ||
|
||
onPlayerPlaybackStart(_e: Event, state: PlayerState) { | ||
this.lastIndex = 0; | ||
this.hasSegments = !!state.MediaSource?.HasSegments; | ||
|
||
const itemId = state.MediaSource?.Id; | ||
const serverId = state.NowPlayingItem?.ServerId || ServerConnections.currentApiClient()?.serverId(); | ||
|
||
if (!this.hasSegments || !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 api = toApi(ServerConnections.getApiClient(serverId)); | ||
void this.fetchMediaSegments( | ||
api, | ||
itemId, | ||
Object.keys(this.mediaSegmentTypeActions).map(t => t as keyof typeof MediaSegmentType)); | ||
} | ||
|
||
onPlayerTimeUpdate() { | ||
if (this.hasSegments && this.mediaSegments.length) { | ||
const time = this.playbackManager.currentTime(this.player) * TICKS_PER_MILLISECOND; | ||
const currentSegmentDetails = findCurrentSegment(this.mediaSegments, time, this.lastIndex); | ||
if (currentSegmentDetails) { | ||
console.debug( | ||
'[MediaSegmentManager] found %s segment at %s ms', | ||
currentSegmentDetails.segment.Type, | ||
time / TICKS_PER_MILLISECOND, | ||
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'; | ||
|
||
export 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; | ||
} |
68 changes: 68 additions & 0 deletions
68
src/apps/stable/features/playback/utils/mediaSegments.test.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,68 @@ | ||
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 { describe, expect, it } from 'vitest'; | ||
|
||
import { findCurrentSegment } from './mediaSegments'; | ||
|
||
const TEST_SEGMENTS: MediaSegmentDto[] = [ | ||
{ | ||
Id: 'intro', | ||
Type: MediaSegmentType.Intro, | ||
StartTicks: 0, | ||
EndTicks: 10 | ||
}, | ||
{ | ||
Id: 'preview', | ||
Type: MediaSegmentType.Preview, | ||
StartTicks: 20, | ||
EndTicks: 30 | ||
}, | ||
{ | ||
Id: 'recap', | ||
Type: MediaSegmentType.Recap, | ||
StartTicks: 30, | ||
EndTicks: 40 | ||
}, | ||
{ | ||
Id: 'commercial', | ||
Type: MediaSegmentType.Commercial, | ||
StartTicks: 40, | ||
EndTicks: 50 | ||
}, | ||
{ | ||
Id: 'outro', | ||
Type: MediaSegmentType.Outro, | ||
StartTicks: 50, | ||
EndTicks: 60 | ||
} | ||
]; | ||
|
||
describe('findCurrentSegment()', () => { | ||
it('Should return the current segment', () => { | ||
let segmentDetails = findCurrentSegment(TEST_SEGMENTS, 23); | ||
expect(segmentDetails).toBeDefined(); | ||
expect(segmentDetails?.index).toBe(1); | ||
expect(segmentDetails?.segment?.Id).toBe('preview'); | ||
|
||
segmentDetails = findCurrentSegment(TEST_SEGMENTS, 5, 1); | ||
expect(segmentDetails).toBeDefined(); | ||
expect(segmentDetails?.index).toBe(0); | ||
expect(segmentDetails?.segment?.Id).toBe('intro'); | ||
|
||
segmentDetails = findCurrentSegment(TEST_SEGMENTS, 42, 3); | ||
expect(segmentDetails).toBeDefined(); | ||
expect(segmentDetails?.index).toBe(3); | ||
expect(segmentDetails?.segment?.Id).toBe('commercial'); | ||
}); | ||
|
||
it('Should return undefined if not in a segment', () => { | ||
let segmentDetails = findCurrentSegment(TEST_SEGMENTS, 16); | ||
expect(segmentDetails).toBeUndefined(); | ||
|
||
segmentDetails = findCurrentSegment(TEST_SEGMENTS, 10, 1); | ||
expect(segmentDetails).toBeUndefined(); | ||
|
||
segmentDetails = findCurrentSegment(TEST_SEGMENTS, 100); | ||
expect(segmentDetails).toBeUndefined(); | ||
}); | ||
}); |
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,41 @@ | ||
import type { MediaSegmentDto } from '@jellyfin/sdk/lib/generated-client/models/media-segment-dto'; | ||
|
||
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) | ||
); | ||
|
||
export 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, segment = segments[index] | ||
) { | ||
if (isBeforeSegment(segment, time, direction)) return; | ||
if (isInSegment(segment, time)) return { index, segment }; | ||
} | ||
}; |
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
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,8 @@ | ||
/** The number of ticks per millisecond */ | ||
export const TICKS_PER_MILLISECOND = 10_000; | ||
|
||
/** The number of ticks per second */ | ||
export const TICKS_PER_SECOND = 1_000 * TICKS_PER_MILLISECOND; | ||
|
||
/** The number of ticks per minute */ | ||
export const TICKS_PER_MINUTE = 60 * TICKS_PER_SECOND; |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unexpected 'fixme' comment: 'FIXME: Replace with SDK...'. no-warning-comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is dependent on the media segment api being available in the stable sdk release.