added platform checks and error handling when virtmic is not present
This commit is contained in:
parent
19c4ea2524
commit
ba83bf6ff7
2 changed files with 45 additions and 35 deletions
|
@ -1,36 +1,45 @@
|
|||
const original = navigator.mediaDevices.getDisplayMedia;
|
||||
async function getVirtmic() {
|
||||
const getAudioDevice = async (deviceName: string|undefined) => {;
|
||||
let devices = await navigator.mediaDevices.enumerateDevices();
|
||||
let audioDevice = devices.find(({
|
||||
label
|
||||
}) => label === deviceName);
|
||||
return audioDevice;
|
||||
}
|
||||
import { isLinux } from "../utils";
|
||||
if (isLinux) {
|
||||
const original = navigator.mediaDevices.getDisplayMedia;
|
||||
async function getVirtmic() {
|
||||
const getAudioDevice = async (deviceName: string|undefined) => {;
|
||||
let devices = await navigator.mediaDevices.enumerateDevices();
|
||||
let audioDevice = devices.find(({
|
||||
label
|
||||
}) => label === deviceName);
|
||||
return audioDevice;
|
||||
}
|
||||
|
||||
var id: string|undefined;
|
||||
try {
|
||||
let myDiscordAudioSink = await getAudioDevice('virtmic');
|
||||
id = myDiscordAudioSink?.deviceId;
|
||||
}
|
||||
catch (error) {
|
||||
id = 'default';
|
||||
}
|
||||
return id;
|
||||
};
|
||||
navigator.mediaDevices.getDisplayMedia = async function() {
|
||||
const id = await getVirtmic();
|
||||
const stream = await original.apply(this);
|
||||
|
||||
const audio = await navigator.mediaDevices.getUserMedia({
|
||||
audio: {
|
||||
deviceId: {
|
||||
exact: id
|
||||
},
|
||||
autoGainControl: false,
|
||||
echoCancellation: false,
|
||||
noiseSuppression: false
|
||||
} });
|
||||
audio.getAudioTracks().forEach(t => stream.addTrack(t));
|
||||
return stream;
|
||||
}
|
||||
var id: string|undefined;
|
||||
try {
|
||||
let myDiscordAudioSink = await getAudioDevice('virtmic');
|
||||
id = myDiscordAudioSink?.deviceId;
|
||||
}
|
||||
catch (error) {
|
||||
id = 'error';
|
||||
}
|
||||
return id;
|
||||
};
|
||||
navigator.mediaDevices.getDisplayMedia = async function() {
|
||||
const id = await getVirtmic();
|
||||
const stream = await original.apply(this);
|
||||
if (id !== 'error'){
|
||||
const audio = await navigator.mediaDevices.getUserMedia({
|
||||
audio: {
|
||||
deviceId: {
|
||||
exact: id
|
||||
},
|
||||
autoGainControl: false,
|
||||
echoCancellation: false,
|
||||
noiseSuppression: false
|
||||
}
|
||||
});
|
||||
audio.getAudioTracks().forEach(t => stream.addTrack(t));
|
||||
} else {
|
||||
const audio = await navigator.mediaDevices.getUserMedia({ audio: false });
|
||||
|
||||
};
|
||||
|
||||
return stream;
|
||||
};
|
||||
};
|
|
@ -17,3 +17,4 @@ const { platform } = navigator;
|
|||
|
||||
export const isWindows = platform.startsWith("Win");
|
||||
export const isMac = platform.startsWith("Mac");
|
||||
export const isLinux = platform.startsWith("Linux");
|
||||
|
|
Reference in a new issue