Skip to content

Commit

Permalink
fix pgssub merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmcbroom committed Aug 13, 2024
2 parents 26ba1ed + 841c1a4 commit 1f1d575
Showing 1 changed file with 88 additions and 13 deletions.
101 changes: 88 additions & 13 deletions frontend/src/store/player-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import JASSUB from 'jassub';
import jassubWorker from 'jassub/dist/jassub-worker.js?url';
import jassubWasmUrl from 'jassub/dist/jassub-worker.wasm?url';
import { PgsRenderer } from 'libpgs';
import pgssubWorker from 'libpgs/dist/libpgs.worker.js?url';
import { computed, nextTick, shallowRef, watch } from 'vue';
import { SubtitleDeliveryMethod } from '@jellyfin/sdk/lib/generated-client/models/subtitle-delivery-method';
import { useFullscreen } from '@vueuse/core';
Expand Down Expand Up @@ -43,6 +45,7 @@ class PlayerElementStore extends CommonStore<PlayerElementState> {
*/
private readonly _fullscreenVideoRoute = '/playback/video';
private _jassub: JASSUB | undefined;
private _pgssub: PgsRenderer | undefined;
protected _storeKey = 'playerElement';

public readonly isStretched = computed({
Expand Down Expand Up @@ -101,6 +104,22 @@ class PlayerElementStore extends CommonStore<PlayerElementState> {
}
};

private readonly _setPgsTrack = (trackSrc: string): void => {
if (
!this._pgssub
&& mediaElementRef.value
&& mediaElementRef.value instanceof HTMLVideoElement
) {
this._pgssub = new PgsRenderer({
video: mediaElementRef.value,
subUrl: trackSrc,
workerUrl: pgssubWorker
});
} else if (this._pgssub) {
this._pgssub.loadFromUrl(trackSrc);
}
};

private readonly _freeSsaTrack = (): void => {
if (this._jassub) {
try {
Expand All @@ -111,6 +130,16 @@ class PlayerElementStore extends CommonStore<PlayerElementState> {
}
};

private readonly _freePgsTrack = (): void => {
if (this._pgssub) {
try {
this._pgssub.dispose();
} catch {}

this._pgssub = undefined;
}
};

private readonly _isSupportedFont = (mimeType: string | undefined | null): boolean => {
return (
!isNil(mimeType)
Expand All @@ -121,19 +150,6 @@ class PlayerElementStore extends CommonStore<PlayerElementState> {
);
};

private get usingExternalVttSubtitles(): boolean {
return !isNil(playbackManager.currentSubtitleTrack)
&& playbackManager.currentSubtitleTrack.DeliveryMethod === SubtitleDeliveryMethod.External
&& playbackManager.currentSubtitleTrack.Codec !== 'ass'
&& playbackManager.currentSubtitleTrack.Codec !== 'ssa';
}

private get usingExternalSsaSubtitles(): boolean {
return !isNil(playbackManager.currentSubtitleTrack)
&& playbackManager.currentSubtitleTrack.DeliveryMethod === SubtitleDeliveryMethod.External
&& (playbackManager.currentSubtitleTrack.Codec === 'ssa' || playbackManager.currentSubtitleTrack.Codec === 'ass');
}

/**
* Logic for applying custom subtitle track.
*
Expand All @@ -151,6 +167,58 @@ class PlayerElementStore extends CommonStore<PlayerElementState> {
&& useFullscreen().isSupported.value;
}

private get usingExternalVttSubtitles(): boolean {
return !isNil(playbackManager.currentSubtitleTrack)
&& playbackManager.currentSubtitleTrack.DeliveryMethod === SubtitleDeliveryMethod.External
&& (
playbackManager.currentSubtitleTrack.Codec === 'vtt'
|| playbackManager.currentSubtitleTrack.Codec === 'srt'
|| playbackManager.currentSubtitleTrack.Codec === 'subrip'
);
}

private get usingExternalSsaSubtitles(): boolean {
return !isNil(playbackManager.currentSubtitleTrack)
&& playbackManager.currentSubtitleTrack.DeliveryMethod === SubtitleDeliveryMethod.External
&& (
playbackManager.currentSubtitleTrack.Codec === 'ssa'
|| playbackManager.currentSubtitleTrack.Codec === 'ass'
);
}

private get usingExternalPgsSubtitles(): boolean {
return !isNil(playbackManager.currentSubtitleTrack)
&& playbackManager.currentSubtitleTrack.DeliveryMethod === SubtitleDeliveryMethod.External
&& (playbackManager.currentSubtitleTrack.Codec === 'pgs');
}

/**
* Applies PGS subtitles to the media element.
*/
private readonly _applyPgsSubtitles = (): void => {
if (
!this._pgssub
&& mediaElementRef.value
&& mediaElementRef.value instanceof HTMLVideoElement
) {
/**
* Finding (if it exists) the pgs track associated to the newly picked subtitle
*/
const pgs = playbackManager.currentItemPgsParsedSubtitleTracks.find(
sub => sub.srcIndex === playbackManager.currentSubtitleStreamIndex
);

/**
* If PGS found, applying it
*/
if (pgs?.src && pgs.srcIndex !== -1) {
this.currentExternalSubtitleTrack = pgs;

this._setPgsTrack(pgs.src);
}
}
};

/**
* Applies VTT (WebVTT) subtitles to the media element.
*
Expand Down Expand Up @@ -181,6 +249,7 @@ class PlayerElementStore extends CommonStore<PlayerElementState> {
*/
if (this.useCustomSubtitleTrack) {
const data = await parseVttFile(vtt.src);

this.currentExternalSubtitleTrack.parsed = data;
} else {
mediaElementRef.value.textTracks[vtt.srcIndex].mode = 'showing';
Expand All @@ -198,6 +267,7 @@ class PlayerElementStore extends CommonStore<PlayerElementState> {
if (!mediaElementRef.value) {
return;
}

const serverAddress = remote.sdk.api?.basePath;

/**
Expand Down Expand Up @@ -270,7 +340,9 @@ class PlayerElementStore extends CommonStore<PlayerElementState> {
textTrack.mode = 'disabled';
}
}

this._freeSsaTrack();
this._freePgsTrack();
this.currentExternalSubtitleTrack = undefined;

await nextTick();
Expand All @@ -282,6 +354,8 @@ class PlayerElementStore extends CommonStore<PlayerElementState> {
void this._applyVttSubtitles();
} else if (this.usingExternalSsaSubtitles) {
void this._applySsaSubtitles();
} else if (this.usingExternalPgsSubtitles) {
this._applyPgsSubtitles();
}
};

Expand Down Expand Up @@ -313,6 +387,7 @@ class PlayerElementStore extends CommonStore<PlayerElementState> {
watch(videoContainerRef, () => {
if (!videoContainerRef.value) {
this._freeSsaTrack();
this._freePgsTrack();
}
}, { flush: 'sync' });

Expand Down

0 comments on commit 1f1d575

Please sign in to comment.