Browse Source

Merge pull request #947 from movie-web/fix/#905

Extension: Prepare stream for all domains in a hls playlist
pull/951/head
chaos 1 year ago committed by GitHub
parent
commit
cbb699b767
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 1
      pnpm-lock.yaml
  2. 5
      src/backend/extension/messaging.ts
  3. 4
      src/backend/extension/streams.ts
  4. 27
      src/components/player/display/base.ts
  5. 2
      src/components/player/utils/convertRunoutputToSource.ts
  6. 3
      src/stores/player/utils/qualities.ts

1
pnpm-lock.yaml

@ -7359,6 +7359,7 @@ packages: @@ -7359,6 +7359,7 @@ packages:
/workbox-google-analytics@7.0.0:
resolution: {integrity: sha512-MEYM1JTn/qiC3DbpvP2BVhyIH+dV/5BjHk756u9VbwuAhu0QHyKscTnisQuz21lfRpOwiS9z4XdqeVAKol0bzg==}
deprecated: It is not compatible with newer versions of GA starting with v4, as long as you are using GAv3 it should be ok, but the package is not longer being maintained
dependencies:
workbox-background-sync: 7.0.0
workbox-core: 7.0.0

5
src/backend/extension/messaging.ts

@ -6,6 +6,11 @@ import { @@ -6,6 +6,11 @@ import {
import { isAllowedExtensionVersion } from "@/backend/extension/compatibility";
import { ExtensionMakeRequestResponse } from "@/backend/extension/plasmo";
export const RULE_IDS = {
PREPARE_STREAM: 1,
SET_DOMAINS_HLS: 2,
};
// for some reason, about 500 ms is needed after
// page load before the extension starts responding properly
const isExtensionReady = new Promise<void>((resolve) => {

4
src/backend/extension/streams.ts

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
import { Stream } from "@movie-web/providers";
import { setDomainRule } from "@/backend/extension/messaging";
import { RULE_IDS, setDomainRule } from "@/backend/extension/messaging";
function extractDomain(url: string): string | null {
try {
@ -36,7 +36,7 @@ function buildHeadersFromStream(stream: Stream): Record<string, string> { @@ -36,7 +36,7 @@ function buildHeadersFromStream(stream: Stream): Record<string, string> {
export async function prepareStream(stream: Stream) {
await setDomainRule({
ruleId: 1,
ruleId: RULE_IDS.PREPARE_STREAM,
targetDomains: extractDomainsFromStream(stream),
requestHeaders: buildHeadersFromStream(stream),
});

27
src/components/player/display/base.ts

@ -1,6 +1,11 @@ @@ -1,6 +1,11 @@
import fscreen from "fscreen";
import Hls, { Level } from "hls.js";
import {
RULE_IDS,
isExtensionActiveCached,
setDomainRule,
} from "@/backend/extension/messaging";
import {
DisplayInterface,
DisplayInterfaceEvents,
@ -31,8 +36,8 @@ const levelConversionMap: Record<number, SourceQuality> = { @@ -31,8 +36,8 @@ const levelConversionMap: Record<number, SourceQuality> = {
480: "480",
};
function hlsLevelToQuality(level: Level): SourceQuality | null {
return levelConversionMap[level.height] ?? null;
function hlsLevelToQuality(level?: Level): SourceQuality | null {
return levelConversionMap[level?.height ?? 0] ?? null;
}
function qualityToHlsLevel(quality: SourceQuality): number | null {
@ -144,6 +149,24 @@ export function makeVideoElementDisplayInterface(): DisplayInterface { @@ -144,6 +149,24 @@ export function makeVideoElementDisplayInterface(): DisplayInterface {
if (!hls) return;
reportLevels();
setupQualityForHls();
if (isExtensionActiveCached()) {
hls.on(Hls.Events.LEVEL_LOADED, async (_, data) => {
const chunkUrlsDomains = data.details.fragments.map(
(v) => new URL(v.url).hostname,
);
const chunkUrls = [...new Set(chunkUrlsDomains)];
await setDomainRule({
ruleId: RULE_IDS.SET_DOMAINS_HLS,
targetDomains: chunkUrls,
requestHeaders: {
...src.preferredHeaders,
...src.headers,
},
});
});
}
});
hls.on(Hls.Events.LEVEL_SWITCHED, () => {
if (!hls) return;

2
src/components/player/utils/convertRunoutputToSource.ts

@ -28,6 +28,7 @@ export function convertRunoutputToSource(out: { @@ -28,6 +28,7 @@ export function convertRunoutputToSource(out: {
return {
type: "hls",
url: out.stream.playlist,
headers: out.stream.headers,
preferredHeaders: out.stream.preferredHeaders,
};
}
@ -50,6 +51,7 @@ export function convertRunoutputToSource(out: { @@ -50,6 +51,7 @@ export function convertRunoutputToSource(out: {
return {
type: "file",
qualities,
headers: out.stream.headers,
preferredHeaders: out.stream.preferredHeaders,
};
}

3
src/stores/player/utils/qualities.ts

@ -14,6 +14,7 @@ export type SourceFileStream = { @@ -14,6 +14,7 @@ export type SourceFileStream = {
export type LoadableSource = {
type: StreamType;
url: string;
headers?: Stream["headers"];
preferredHeaders?: Stream["preferredHeaders"];
};
@ -21,11 +22,13 @@ export type SourceSliceSource = @@ -21,11 +22,13 @@ export type SourceSliceSource =
| {
type: "file";
qualities: Partial<Record<SourceQuality, SourceFileStream>>;
headers?: Stream["headers"];
preferredHeaders?: Stream["preferredHeaders"];
}
| {
type: "hls";
url: string;
headers?: Stream["headers"];
preferredHeaders?: Stream["preferredHeaders"];
};

Loading…
Cancel
Save