Files
discord-decomp/modules/video_backgrounds/applyBackgroundOption.tsx
T
2026-11-01 12:12:55 +02:00

239 lines
8.8 KiB
TypeScript

// Metro module 8843 (f8844).
import UserStore from "../../stores/UserStore";
import VideoBackgroundStore from "./VideoBackgroundStore";
import { BLUR_BACKGROUND_OPTION, BACKGROUND_REPLACEMENT_SIZE } from "./VideoBackgroundConstants";
import { NOOP } from "../../Constants";
import Module38 from "../../_bundle/Module38";
import { applyMediaFilterSettings, errorApplyingMediaFilterSettings, startApplyMediaFilterSettings } from "./VideoBackgroundActionCreators";
import { FilterSettingsGraph, FilterSettingsKey, FilterTargetType } from "../../_bundle/Module4286";
import getDefaultBackgroundData_native from "./getDefaultBackgroundData.native";
import { isAnimatedIconHash, isVideoAssetHash, getVideoFilterAssetURL } from "../../utils/AvatarUtils";
import { trackBackgroundOptionUpdated } from "./VideoBackgroundUtils";
import { getLastUsedVideoBackgroundOption } from "./LastUsedVideoBackgroundOption";
import isVideoBackgroundSupported from "./isVideoBackgroundSupported";
function $mk115352($env) {
return function getFilterBlob() {
return $env[9].apply(this, arguments);
};
}
let applyBackgroundMediaFilterSettings, applyBackgroundOption, applyBackgroundOptionLive;
let env5766 = {};
env5766[9] = function _getFilterBlob() {
const r8 = async (c26292_0) => {
let c29983_0 = await fetch(c26292_0);
let c29983_1 = await c29983_0.blob();
const Uint8ClampedArray2 = Uint8ClampedArray;
const r70 = await c29983_1.arrayBuffer();
const r79 = Object.create(Uint8ClampedArray2.prototype);
return new Uint8ClampedArray2(r70);
};
env5766[9] = r8;
return r8.apply(this, arguments);
};
applyBackgroundMediaFilterSettings = function applyBackgroundMediaFilterSettings(a0, a1, a2, a3, a4) {
const r9 = {};
r9[a0] = {
graph: a2,
target: a1,
image: a3,
blob: a4
};
applyMediaFilterSettings(r9);
};
applyBackgroundOption = async function applyBackgroundOption(c26293_0, c26293_1, c26293_2) {
let r71, r81;
let isVideo, c26293_4, c26293_5, c26293_6;
let c26293_7 = 0;
const r10 = env5766;
L39: {
L26: {
L38: {
try {
let env29984;
c26293_5 = r10;
const r23 = env29984 = {
$parent: r10
};
c26293_4 = r23;
env29984[0] = c26293_0;
env29984[1] = c26293_1;
env29984[2] = void 0;
env29984[4] = void 0;
env29984[5] = void 0;
env29984[3] = false;
if (c26293_2 == null) {
applyBackgroundMediaFilterSettings(c26293_0, c26293_1, FilterSettingsGraph.NONE);
break L39;
}
if (c26293_2 === BLUR_BACKGROUND_OPTION) {
applyBackgroundMediaFilterSettings(c26293_0, c26293_1, FilterSettingsGraph.BACKGROUND_BLUR);
break L38;
}
if (typeof c26293_2 !== "string" && typeof c26293_2 !== "number") {
const asset = c26293_2.asset;
r71 = isAnimatedIconHash(asset) || isVideoAssetHash(asset);
env29984[3] = r71;
r81 = getVideoFilterAssetURL({
userId: c26293_2.user_id,
assetId: c26293_2.id,
assetHash: asset,
size: BACKGROUND_REPLACEMENT_SIZE.width
});
env29984[2] = r81;
} else {
const r64 = getDefaultBackgroundData_native()[c26293_2];
isVideo = r64.isVideo;
if (isVideo == null) {
isVideo = false;
}
env29984[3] = isVideo;
const source = r64.source;
env29984[2] = source;
r71 = isVideo;
r81 = source;
}
if (r81 != null) {
c26293_7 = 1;
const r88 = void 0;
if (!r71) {
env29984[4] = await (function getFilterImage(src) {
return new Promise((c31981_0, c31981_1) => {
const image = new Image();
let c31981_2 = image;
image.crossOrigin = "anonymous";
image.onload = () => {
const element = document.createElement("canvas");
element.width = BACKGROUND_REPLACEMENT_SIZE.width;
element.height = BACKGROUND_REPLACEMENT_SIZE.height;
const context = element.getContext("2d");
Module38(context != null, "Canvas context is missing");
const r33 = c31981_2.height / c31981_2.width;
const height = BACKGROUND_REPLACEMENT_SIZE.height;
const r36 = BACKGROUND_REPLACEMENT_SIZE.height / r33;
context.drawImage(c31981_2, (element.width - r36) / 2, (element.height - height) / 2, r36, height);
const imageData = context.getImageData(0, 0, element.width, element.height);
c31981_0({
data: imageData.data,
width: imageData.width,
height: imageData.height,
pixelFormat: "rgba"
});
};
image.onerror = (a0) => c31981_1(a0);
image.src = src;
});
})(r81);
const r112 = env29984[3];
const r113 = void 0;
if (r112) {
const r114 = env29984[2];
env29984[5] = await $mk115352(env5766)(r114);
(0, applyBackgroundMediaFilterSettings)(env29984[0], env29984[1], FilterSettingsGraph.BACKGROUND_REPLACEMENT, env29984[4], env29984[5]);
c26293_7 = 0;
break L26;
}
env29984[5] = r113;
(0, applyBackgroundMediaFilterSettings)(env29984[0], env29984[1], FilterSettingsGraph.BACKGROUND_REPLACEMENT, env29984[4], env29984[5]);
c26293_7 = 0;
break L26;
}
env29984[4] = r88;
const r90 = env29984[3];
const r20 = void 0;
if (r90) {
const r164 = env29984[2];
env29984[5] = await $mk115352(env5766)(r164);
(0, applyBackgroundMediaFilterSettings)(env29984[0], env29984[1], FilterSettingsGraph.BACKGROUND_REPLACEMENT, env29984[4], env29984[5]);
c26293_7 = 0;
break L26;
}
env29984[5] = r20;
(0, applyBackgroundMediaFilterSettings)(env29984[0], env29984[1], FilterSettingsGraph.BACKGROUND_REPLACEMENT, env29984[4], env29984[5]);
c26293_7 = 0;
}
} catch ($e) {
const r42 = $e;
let r8 = r42;
c26293_6 = r42;
if (c26293_7 !== 0) {
while (true) {
try {
c26293_7 = 0;
errorApplyingMediaFilterSettings();
break L26;
} catch (r224) {
r8 = r224;
c26293_6 = r224;
if (c26293_7 === 0) {
break;
}
}
}
}
throw r8;
}
return void 0;
}
return void 0;
}
return void 0;
}
};
applyBackgroundOptionLive = async function applyBackgroundOptionLive(c26294_0, c26294_1) {
let c29985_1, c29985_2;
c29985_1 = void 0;
c29985_2 = void 0;
let c29985_0 = c26294_0;
const track = c26294_1.track;
if (track !== void 0) {
c29985_1 = track;
c29985_2 = c26294_1.location;
} else {
c29985_1 = true;
c29985_2 = c26294_1.location;
}
await void 0;
await (0, applyBackgroundOption)(FilterSettingsKey.CAMERA_BACKGROUND_LIVE, {
type: FilterTargetType.INPUT_DEVICE
}, c29985_0);
if (c29985_1) {
trackBackgroundOptionUpdated(c29985_0, c29985_2, "Enabled");
}
};
export { applyBackgroundOptionLive };
export async function applyBackgroundOptionPreview(c26295_0, c26295_1, c26295_2) {
let c29986_2, c29986_3;
c29986_2 = void 0;
c29986_3 = void 0;
let c29986_0 = c26295_0;
let c29986_1 = c26295_1;
const track = c26295_2.track;
if (track !== void 0) {
c29986_2 = track;
c29986_3 = c26295_2.location;
} else {
c29986_2 = true;
c29986_3 = c26295_2.location;
}
await void 0;
startApplyMediaFilterSettings();
await (0, applyBackgroundOption)(FilterSettingsKey.CAMERA_BACKGROUND_PREVIEW, {
type: FilterTargetType.STREAM,
streamId: c29986_1
}, c29986_0);
if (c29986_2) {
trackBackgroundOptionUpdated(c29986_0, c29986_3, "Preview");
}
}
export function applyInitialVideoBackgroundOption() {
const currentUser = UserStore.getCurrentUser();
if (currentUser != null) {
const lastUsedVideoBackgroundOption = getLastUsedVideoBackgroundOption(currentUser);
if (isVideoBackgroundSupported() && !VideoBackgroundStore.hasBeenApplied && lastUsedVideoBackgroundOption != null) {
applyBackgroundOptionLive(lastUsedVideoBackgroundOption, {
track: false
}).catch(NOOP);
}
}
}