Skip to content

Commit

Permalink
Try explore url if direct download failed
Browse files Browse the repository at this point in the history
  • Loading branch information
ed-asriyan committed Aug 26, 2024
1 parent 161d903 commit 4801c3e
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 29 deletions.
68 changes: 68 additions & 0 deletions src/components/video-player/explore-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Source, SourceType } from '../../normalize-source';
import { proxies } from '../../settings';

export const exploreUrl = async function(source: Source): Promise<Source> {
if (source.type !== SourceType.direct || source.src instanceof Blob) {
return source;
}

const attepmts: ((src: Source) => Generator<Source>)[] = [
tryAsIs,
tryFromQueryParams,
tryProxy,
];

for (const attempt of attepmts) {
for (const result of attempt(source)) {
try {
const a = await fetch(result.src as string, { mode: 'cors', method: 'HEAD' });
return result;
} catch {
}
}
}

return source;
};

const tryAsIs = function* (source: Source): Generator<Source> {
yield source;
};

const tryFromQueryParams = function* (source: Source): Generator<Source> {
if (typeof source.src !== 'string') return source;
let url: URL;
try {
url = new URL(source.src);
} catch {
return source
}
for (const [_, value] of url.searchParams) {
try {
new URL(value);
} catch {
continue;
}
const result = new Source({ type: SourceType.direct, src: value });
yield result;
yield* tryFromQueryParams(result);
}
};

const tryProxy = function* (source: Source): Generator<Source> {
if (typeof source.src !== 'string') return source;

const paths = new URL(source.src).pathname.split('.');
const extenssion = paths[paths.length - 1];
const isHls = extenssion === 'm3u8';
if ((isHls && !proxies.hlsUrl) || (!isHls && !proxies.regularUrl)) {
yield source;
}
let src: string;
if (isHls) {
src = `${proxies.hlsUrl}/${btoa(source.src)}.m3u8`;
} else {
src = `${proxies.regularUrl}?url=${source.src}`;
}
yield new Source({ ...source, src });
};
31 changes: 3 additions & 28 deletions src/components/video-player/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import { derived, type Readable } from 'svelte/store';
import { _ } from 'svelte-i18n';
import { Source, SourceType } from '../../normalize-source';
import { exploreUrl } from './explore-url';
import VideoPlayerVidstack from './video-player-vidstack.svelte';
import VideoPlayerVime from './video-player-vime.svelte';
import VideoPlayerMagnet from './player-magnet.svelte';
import Loader from '../loader.svelte';
import Inplayer from './inplayer.svelte';
import { proxies } from '../../settings';
import { blob } from '../../stores/blob';
import type { Room } from '../../stores/room';
import { MessageType } from '../../stores/room/bound-messages';
Expand Down Expand Up @@ -43,32 +43,6 @@
}
});
const normalizeSource = async function(source: Source): Promise<Source> {
if (source.type !== SourceType.direct || (!proxies.hlsUrl && !proxies.regularUrl) || source.src instanceof Blob) {
return source;
}
const paths = new URL(source.src).pathname.split('.');
const extenssion = paths[paths.length - 1];
const isHls = extenssion === 'm3u8';
if ((isHls && !proxies.hlsUrl) || (!isHls && !proxies.regularUrl)) {
return source;
}
try {
await fetch(source.src, { mode: 'cors', method: 'HEAD' });
return source;
} catch {
let src = source.src;
if (isHls) {
src = `${proxies.hlsUrl}/${btoa(source.src)}.m3u8`;
} else {
src = `${proxies.regularUrl}?url=${source.src}`;
}
return new Source({ ...source, src });
}
};
let watchMinuteAnalyticsTimeInterval: number;;
onMount(() => {
watchMinuteAnalyticsTimeInterval = setInterval(() => {
Expand Down Expand Up @@ -129,7 +103,7 @@
</VideoPlayerVidstack>
{:else}
{#if $source}
{#await normalizeSource($source)}
{#await exploreUrl($source)}
<Loader/>
{:then normalizedSource}
{#if $playerType === 'vidstack'}
Expand All @@ -151,6 +125,7 @@
{:else if $playerType === 'magnet'}
<VideoPlayerMagnet
source={normalizedSource}
room={room}
bind:paused={$paused}
bind:currentTime={$currentTime}
bind:muted={muted}
Expand Down
4 changes: 3 additions & 1 deletion src/components/video-player/player-magnet.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import { sleep } from '../../utils';
import VideoPlayerVidstack from './video-player-vidstack.svelte';
import VideoSelectorBtn from '../video-selector-btn.svelte';
import type { Room } from '../../stores/room';
export let room: Room;
export let source: Source;
export let currentTime: number;
export let paused: boolean;
Expand All @@ -31,7 +33,7 @@
<div class="uk-margin-top uk-text-center">
{ $_('player.isHostActive') }
<br class="uk-margin"/>
<VideoSelectorBtn forceLocal={true} />
<VideoSelectorBtn room={room} forceLocal={true} />
</div>
<div class="chat">
<slot name="chat" />
Expand Down

0 comments on commit 4801c3e

Please sign in to comment.