-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(player): impl remote tracks fetching
- Loading branch information
Showing
6 changed files
with
252 additions
and
116 deletions.
There are no files selected for viewing
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,67 @@ | ||
import type { CaptionOption } from "@vidstack/react"; | ||
import { useCaptionOptions, useMediaState } from "@vidstack/react"; | ||
import { SubtitlesIcon } from "@/components/icon"; | ||
import { dataLpPassthrough } from "./buttons"; | ||
import { useMenu } from "./menus"; | ||
|
||
function sortCaption(a: CaptionOption, b: CaptionOption) { | ||
if (a.track && b.track) { | ||
// if with item.track.language, sort by item.track.language | ||
if (a.track.language && b.track.language) { | ||
return (a.track as { language: string }).language.localeCompare( | ||
(b.track as { language: string }).language, | ||
); | ||
} | ||
|
||
// if not with item.track.language, sort by item.label | ||
if (!a.track.language && !b.track.language) { | ||
return a.label.localeCompare(b.label); | ||
} | ||
|
||
// if one has language and the other doesn't, the one with language comes first | ||
if (a.track.language && !b.track.language) { | ||
return -1; | ||
} | ||
if (!a.track.language && b.track.language) { | ||
return 1; | ||
} | ||
} | ||
// if item.track is null, put it at the end | ||
if (a.track === null && b.track !== null) { | ||
return 1; | ||
} | ||
if (a.track !== null && b.track === null) { | ||
return -1; | ||
} | ||
return 0; | ||
} | ||
export function Captions() { | ||
const options = useCaptionOptions(); | ||
const tracks = useMediaState("textTracks"); | ||
const onClick = useMenu((menu) => { | ||
options | ||
.sort(sortCaption) | ||
.forEach(({ label, select, selected }, idx, options) => { | ||
menu.addItem((item) => { | ||
if (options.length === 2 && label === "Unknown") { | ||
label = "On"; | ||
} | ||
item.setTitle(label).setChecked(selected).onClick(select); | ||
}); | ||
}); | ||
return true; | ||
}); | ||
|
||
if (tracks.length === 0) return null; | ||
|
||
return ( | ||
<button | ||
className="group ring-mod-border-focus relative inline-flex h-10 w-10 cursor-pointer items-center justify-center rounded-md outline-none ring-inset hover:bg-white/20 focus-visible:ring-2 aria-disabled:hidden" | ||
{...{ [dataLpPassthrough]: true }} | ||
onClick={onClick} | ||
aria-label="Select Caption" | ||
> | ||
<SubtitlesIcon className="w-7 h-7" /> | ||
</button> | ||
); | ||
} |
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,36 @@ | ||
import type { PlayerSrc } from "@vidstack/react"; | ||
import { useMemo } from "react"; | ||
import { encodeWebpageUrl } from "@/lib/remote-player/encode"; | ||
import { toInfoKey } from "@/media-note/note-index/def"; | ||
import { isFileMediaInfo } from "../info/media-info"; | ||
import { useApp, useMediaViewStore } from "./context"; | ||
|
||
export function useSource() { | ||
const mediaInfo = useMediaViewStore((s) => s.source?.url); | ||
const { vault } = useApp(); | ||
const mediaInfoKey = mediaInfo ? toInfoKey(mediaInfo) : null; | ||
const src = useMemo(() => { | ||
if (!mediaInfo) return; | ||
if (isFileMediaInfo(mediaInfo)) { | ||
return vault.getResourcePath(mediaInfo.file); | ||
} | ||
return mediaInfo.source.href; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [mediaInfoKey]); | ||
|
||
const type = useMediaViewStore( | ||
(s): "video/mp4" | "audio/mp3" | "webpage" | undefined => { | ||
const viewType = s.source?.viewType; | ||
if (!viewType) return; | ||
if (viewType === "mx-webpage") return "webpage"; | ||
if (viewType?.endsWith("video")) return "video/mp4"; | ||
if (viewType?.endsWith("audio")) return "audio/mp3"; | ||
}, | ||
); | ||
if (!src) return; | ||
|
||
if (type === "webpage") { | ||
return { src: encodeWebpageUrl(src) } satisfies PlayerSrc; | ||
} | ||
return { src, type } satisfies PlayerSrc; | ||
} |
Oops, something went wrong.