Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potential Audio Fix for Windows #1068

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 74 additions & 12 deletions src/main/screenShare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,81 @@
* Vesktop, a desktop app aiming to give you a snappier Discord Experience
* Copyright (c) 2023 Vendicated and Vencord contributors
*/

import { desktopCapturer, session, Streams } from "electron";
import type { StreamPick } from "renderer/components/ScreenSharePicker";
import { IpcEvents } from "shared/IpcEvents";

import { handle } from "./utils/ipcWrappers";
import * as path from "path";
import { execSync } from 'child_process';

interface AudioRoutingConfig {
virtualDeviceId?: string;
excludedProcesses?: string[];
}

class AudioRouter {
private config: AudioRoutingConfig = {
excludedProcesses: ['vesktop.exe', 'electron.exe']
};

private getVirtualAudioDevices(): string[] {
try {
switch (process.platform) {
case 'win32':
// PowerShell command to list virtual audio devices
const devices = execSync('powershell "Get-AudioDevice -List | Where-Object {$_.Type -eq \'Playback\'}"')
.toString()
.split('\n')
.filter(line => line.includes('Virtual Cable'));
return devices;

case 'darwin':
// macOS virtual audio device detection (placeholder)
return [];

case 'linux':
// Linux virtual audio device detection (placeholder)
return [];

default:
return [];
}
} catch (error) {
console.error('Audio device detection failed', error);
return [];
}
}

configureAudioRouting(source: any, choice: StreamPick): Streams | null {
if (process.platform !== 'win32' || !choice.audio) return null;

const virtualDevices = this.getVirtualAudioDevices();
if (virtualDevices.length === 0) return null;

try {
return {
video: source,
audio: 'loopback',
audioConstraints: {
mandatory: {
sourceId: virtualDevices[0],
excludedProcesses: this.config.excludedProcesses
}
}
};
} catch (error) {
console.error('Audio routing configuration failed', error);
return null;
}
}
}

const isWayland =
process.platform === "linux" && (process.env.XDG_SESSION_TYPE === "wayland" || !!process.env.WAYLAND_DISPLAY);

export function registerScreenShareHandler() {
const audioRouter = new AudioRouter();

handle(IpcEvents.CAPTURER_GET_LARGE_THUMBNAIL, async (_, id: string) => {
const sources = await desktopCapturer.getSources({
types: ["window", "screen"],
Expand All @@ -37,9 +101,9 @@ export function registerScreenShareHandler() {
}
})
.catch(err => console.error("Error during screenshare picker", err));

if (!sources) return callback({});

const data = sources.map(({ id, name, thumbnail }) => ({
id,
name,
Expand All @@ -56,7 +120,6 @@ export function registerScreenShareHandler() {
.catch(() => null);
if (stream === null) return callback({});
}

callback(video ? { video: sources[0] } : {});
return;
}
Expand All @@ -74,11 +137,10 @@ export function registerScreenShareHandler() {
const source = sources.find(s => s.id === choice.id);
if (!source) return callback({});

const streams: Streams = {
video: source
};
if (choice.audio && process.platform === "win32") streams.audio = "loopback";

callback(streams);
// Configure audio routing for Windows
const audioConfig = audioRouter.configureAudioRouting(source, choice);

// Fallback to default video stream if audio routing fails
callback(audioConfig || { video: source });
});
}
}