Compare commits
4 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 39eb7c77ad | |||
| 9d7f658fc0 | |||
| c9592a9cb0 | |||
| 76097f3294 |
2 changed files with 110 additions and 6 deletions
82
main.js
82
main.js
|
|
@ -2,9 +2,10 @@ const { app, BrowserWindow, session, desktopCapturer, ipcMain, clipboard } = req
|
||||||
const { autoUpdater } = require('electron-updater');
|
const { autoUpdater } = require('electron-updater');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const packageJson = require('./package.json');
|
const packageJson = require('./package.json');
|
||||||
|
const PERIODIC_UPDATE_CHECK_INTERVAL_MS = 1 * 60 * 1000;
|
||||||
|
|
||||||
const updateState = {
|
const updateState = {
|
||||||
status: 'idle', // idle | checking | available | downloading | downloaded | not-available | error
|
status: 'idle', // idle | checking | available | downloading | downloaded | installing | not-available | error
|
||||||
info: null,
|
info: null,
|
||||||
error: null,
|
error: null,
|
||||||
};
|
};
|
||||||
|
|
@ -24,6 +25,8 @@ function isNewerVersionAvailable(info) {
|
||||||
}
|
}
|
||||||
|
|
||||||
let updateCheckInProgress = false;
|
let updateCheckInProgress = false;
|
||||||
|
let installInProgress = false;
|
||||||
|
let periodicUpdateTimer = null;
|
||||||
|
|
||||||
function resolveBackendUrl() {
|
function resolveBackendUrl() {
|
||||||
const configuredUrl = (process.env.CHATTZ_URL || process.env.APP_BASE_URL || '').trim();
|
const configuredUrl = (process.env.CHATTZ_URL || process.env.APP_BASE_URL || '').trim();
|
||||||
|
|
@ -41,7 +44,18 @@ function resolveBackendUrl() {
|
||||||
return 'http://localhost:3000';
|
return 'http://localhost:3000';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function webContentsOrigin(webContents) {
|
||||||
|
if (!webContents || typeof webContents.getURL !== 'function') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return safeOrigin(webContents.getURL());
|
||||||
|
}
|
||||||
|
|
||||||
async function runUpdateCheck(reason = 'manual') {
|
async function runUpdateCheck(reason = 'manual') {
|
||||||
|
if (installInProgress) {
|
||||||
|
console.log(`Update check skipped (${reason}): install already in progress`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (updateCheckInProgress) {
|
if (updateCheckInProgress) {
|
||||||
console.log(`Update check skipped (${reason}): another check is already in progress`);
|
console.log(`Update check skipped (${reason}): another check is already in progress`);
|
||||||
return;
|
return;
|
||||||
|
|
@ -71,6 +85,57 @@ async function runUpdateCheck(reason = 'manual') {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function installDownloadedUpdate() {
|
||||||
|
if (installInProgress) {
|
||||||
|
console.log('Update install already in progress');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
installInProgress = true;
|
||||||
|
updateState.status = 'installing';
|
||||||
|
updateState.error = null;
|
||||||
|
broadcastUpdateState();
|
||||||
|
|
||||||
|
const wins = BrowserWindow.getAllWindows();
|
||||||
|
for (const win of wins) {
|
||||||
|
if (win.isDestroyed()) continue;
|
||||||
|
try {
|
||||||
|
win.removeAllListeners('close');
|
||||||
|
win.destroy();
|
||||||
|
} catch (err) {
|
||||||
|
console.warn('Failed to destroy window before update install', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setImmediate(() => {
|
||||||
|
try {
|
||||||
|
autoUpdater.quitAndInstall(false, true);
|
||||||
|
} catch (err) {
|
||||||
|
installInProgress = false;
|
||||||
|
updateState.status = 'error';
|
||||||
|
updateState.error = err && err.message ? err.message : String(err);
|
||||||
|
broadcastUpdateState();
|
||||||
|
console.error('quitAndInstall failed', err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
if (installInProgress) {
|
||||||
|
console.warn('Update install is still waiting for app shutdown');
|
||||||
|
}
|
||||||
|
}, 15000);
|
||||||
|
}
|
||||||
|
|
||||||
|
function startPeriodicUpdateChecks() {
|
||||||
|
if (periodicUpdateTimer || !app.isPackaged) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
periodicUpdateTimer = setInterval(() => {
|
||||||
|
void runUpdateCheck('periodic');
|
||||||
|
}, PERIODIC_UPDATE_CHECK_INTERVAL_MS);
|
||||||
|
}
|
||||||
|
|
||||||
function createWindow() {
|
function createWindow() {
|
||||||
const sess = session.fromPartition('persist:chattz');
|
const sess = session.fromPartition('persist:chattz');
|
||||||
const backendUrl = resolveBackendUrl();
|
const backendUrl = resolveBackendUrl();
|
||||||
|
|
@ -93,13 +158,13 @@ function createWindow() {
|
||||||
win.setMenuBarVisibility(true);
|
win.setMenuBarVisibility(true);
|
||||||
|
|
||||||
sess.setPermissionCheckHandler((webContents, permission) => {
|
sess.setPermissionCheckHandler((webContents, permission) => {
|
||||||
const origin = safeOrigin(webContents.getURL());
|
const origin = webContentsOrigin(webContents);
|
||||||
if (origin !== backendOrigin) return false;
|
if (origin !== backendOrigin) return false;
|
||||||
return permission === 'media' || permission === 'clipboard-write';
|
return permission === 'media' || permission === 'clipboard-write';
|
||||||
});
|
});
|
||||||
|
|
||||||
sess.setPermissionRequestHandler((webContents, permission, callback) => {
|
sess.setPermissionRequestHandler((webContents, permission, callback) => {
|
||||||
const origin = safeOrigin(webContents.getURL());
|
const origin = webContentsOrigin(webContents);
|
||||||
if (origin === backendOrigin && (permission === 'media' || permission === 'clipboard-write')) {
|
if (origin === backendOrigin && (permission === 'media' || permission === 'clipboard-write')) {
|
||||||
callback(true);
|
callback(true);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -202,6 +267,7 @@ app.whenReady().then(() => {
|
||||||
});
|
});
|
||||||
|
|
||||||
ipcMain.on('download-update', () => {
|
ipcMain.on('download-update', () => {
|
||||||
|
if (installInProgress) return;
|
||||||
updateState.status = 'downloading';
|
updateState.status = 'downloading';
|
||||||
updateState.error = null;
|
updateState.error = null;
|
||||||
broadcastUpdateState();
|
broadcastUpdateState();
|
||||||
|
|
@ -209,13 +275,14 @@ app.whenReady().then(() => {
|
||||||
});
|
});
|
||||||
|
|
||||||
ipcMain.on('quit-and-install', () => {
|
ipcMain.on('quit-and-install', () => {
|
||||||
autoUpdater.quitAndInstall();
|
installDownloadedUpdate();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Check once on startup
|
// Check once on startup
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
void runUpdateCheck('startup');
|
void runUpdateCheck('startup');
|
||||||
}, 5000);
|
}, 5000);
|
||||||
|
startPeriodicUpdateChecks();
|
||||||
|
|
||||||
app.on('activate', () => {
|
app.on('activate', () => {
|
||||||
if (BrowserWindow.getAllWindows().length === 0) {
|
if (BrowserWindow.getAllWindows().length === 0) {
|
||||||
|
|
@ -230,6 +297,13 @@ app.on('window-all-closed', () => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.on('before-quit', () => {
|
||||||
|
if (periodicUpdateTimer) {
|
||||||
|
clearInterval(periodicUpdateTimer);
|
||||||
|
periodicUpdateTimer = null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
function safeOrigin(value) {
|
function safeOrigin(value) {
|
||||||
try {
|
try {
|
||||||
return new URL(value).origin;
|
return new URL(value).origin;
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ const state = {
|
||||||
viewMode: 'chat', // 'chat' or 'video'
|
viewMode: 'chat', // 'chat' or 'video'
|
||||||
iceServers: [{ urls: "stun:stun.l.google.com:19302" }],
|
iceServers: [{ urls: "stun:stun.l.google.com:19302" }],
|
||||||
peerGainNodes: new Map(), // userId -> GainNode
|
peerGainNodes: new Map(), // userId -> GainNode
|
||||||
|
peerAudioNodes: new Map(), // userId -> { sourceNode, compressorNode, gainNode }
|
||||||
visibleVolumeSliders: new Set(), // userIds whose sliders are visible
|
visibleVolumeSliders: new Set(), // userIds whose sliders are visible
|
||||||
},
|
},
|
||||||
voicePresencePollId: null,
|
voicePresencePollId: null,
|
||||||
|
|
@ -1039,7 +1040,24 @@ function shouldInitiateOffer(peerId) {
|
||||||
return state.me.id > peerId;
|
return state.me.id > peerId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function disconnectPeerAudioNodes(peerId) {
|
||||||
|
const nodes = state.voice.peerAudioNodes.get(peerId);
|
||||||
|
if (!nodes) return;
|
||||||
|
|
||||||
|
for (const node of [nodes.sourceNode, nodes.compressorNode, nodes.gainNode]) {
|
||||||
|
try {
|
||||||
|
node?.disconnect();
|
||||||
|
} catch { }
|
||||||
|
}
|
||||||
|
|
||||||
|
state.voice.peerAudioNodes.delete(peerId);
|
||||||
|
state.voice.peerGainNodes.delete(peerId);
|
||||||
|
}
|
||||||
|
|
||||||
function stopAndClearAudioPipeline() {
|
function stopAndClearAudioPipeline() {
|
||||||
|
for (const peerId of state.voice.peerAudioNodes.keys()) {
|
||||||
|
disconnectPeerAudioNodes(peerId);
|
||||||
|
}
|
||||||
if (state.voice.deepFilterProcessor) {
|
if (state.voice.deepFilterProcessor) {
|
||||||
state.voice.deepFilterProcessor.destroy();
|
state.voice.deepFilterProcessor.destroy();
|
||||||
state.voice.deepFilterProcessor = null;
|
state.voice.deepFilterProcessor = null;
|
||||||
|
|
@ -1265,7 +1283,7 @@ async function createLocalVoiceStream() {
|
||||||
sampleRate: 48000,
|
sampleRate: 48000,
|
||||||
echoCancellation: true,
|
echoCancellation: true,
|
||||||
noiseSuppression: false,
|
noiseSuppression: false,
|
||||||
autoGainControl: false,
|
autoGainControl: true,
|
||||||
},
|
},
|
||||||
video: false,
|
video: false,
|
||||||
};
|
};
|
||||||
|
|
@ -1388,16 +1406,27 @@ function ensurePeerConnection(peerId) {
|
||||||
const ctx = state.voice.audioContext;
|
const ctx = state.voice.audioContext;
|
||||||
if (ctx.state === 'suspended') ctx.resume();
|
if (ctx.state === 'suspended') ctx.resume();
|
||||||
|
|
||||||
|
disconnectPeerAudioNodes(peerId);
|
||||||
|
|
||||||
const sourceNode = ctx.createMediaStreamSource(audio.srcObject);
|
const sourceNode = ctx.createMediaStreamSource(audio.srcObject);
|
||||||
|
const compressorNode = ctx.createDynamicsCompressor();
|
||||||
const gainNode = ctx.createGain();
|
const gainNode = ctx.createGain();
|
||||||
|
|
||||||
|
compressorNode.threshold.setValueAtTime(-24, ctx.currentTime);
|
||||||
|
compressorNode.knee.setValueAtTime(18, ctx.currentTime);
|
||||||
|
compressorNode.ratio.setValueAtTime(4, ctx.currentTime);
|
||||||
|
compressorNode.attack.setValueAtTime(0.003, ctx.currentTime);
|
||||||
|
compressorNode.release.setValueAtTime(0.25, ctx.currentTime);
|
||||||
|
|
||||||
const currentVolume = state.userVolumes.get(peerId) ?? 1.0;
|
const currentVolume = state.userVolumes.get(peerId) ?? 1.0;
|
||||||
gainNode.gain.setValueAtTime(currentVolume, ctx.currentTime);
|
gainNode.gain.setValueAtTime(currentVolume, ctx.currentTime);
|
||||||
|
|
||||||
sourceNode.connect(gainNode);
|
sourceNode.connect(compressorNode);
|
||||||
|
compressorNode.connect(gainNode);
|
||||||
gainNode.connect(ctx.destination);
|
gainNode.connect(ctx.destination);
|
||||||
|
|
||||||
state.voice.peerGainNodes.set(peerId, gainNode);
|
state.voice.peerGainNodes.set(peerId, gainNode);
|
||||||
|
state.voice.peerAudioNodes.set(peerId, { sourceNode, compressorNode, gainNode });
|
||||||
|
|
||||||
// Mute the original element as we play through Web Audio destination
|
// Mute the original element as we play through Web Audio destination
|
||||||
audio.volume = 0;
|
audio.volume = 0;
|
||||||
|
|
@ -1582,6 +1611,7 @@ async function joinVoice() {
|
||||||
pc.close();
|
pc.close();
|
||||||
state.voice.peerConnections.delete(msg.user_id);
|
state.voice.peerConnections.delete(msg.user_id);
|
||||||
}
|
}
|
||||||
|
disconnectPeerAudioNodes(msg.user_id);
|
||||||
document.getElementById(`audio-${msg.user_id}`)?.remove();
|
document.getElementById(`audio-${msg.user_id}`)?.remove();
|
||||||
} else if (msg.type === "signal") {
|
} else if (msg.type === "signal") {
|
||||||
await handleSignal(msg.from_user_id, msg.kind, msg.data);
|
await handleSignal(msg.from_user_id, msg.kind, msg.data);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue