const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) const exception = /Requested device not found/ const permission = /DOMException: Permission denied/ const constrains = isMobile ? [ { video: { facingMode: "user", width: { ideal: 1280 }, height: { ideal: 720 }, aspectRatio: 1.79, frameRate: 24 }, audio: { echoCancellation: true, noiseSuppression: true, autoGainControl: true } }, { video: true, audio: { echoCancellation: true, noiseSuppression: true, autoGainControl: true } }, { video: true, audio: true }, { video: { facingMode: "user", width: { ideal: 1280 }, height: { ideal: 720 }, aspectRatio: 1.79, frameRate: 24 } }, { video: true }, { audio: { echoCancellation: true, noiseSuppression: true, autoGainControl: true } }, { audio: true } ] : [{ video: { frameRate: 24, width: { min: 480, ideal: 720, max: 1280 }, aspectRatio: 1.79 }, audio: true }, { video: true, audio: true }, { video: { frameRate: 24, width: { min: 480, ideal: 720, max: 1280 }, aspectRatio: 1.79 } }, { audio: true }] export const getUserMedia = async (): Promise<[MediaStream, number]> => { if (window.navigator?.mediaDevices === undefined) (window.navigator as any).mediaDevices = {}; if (window.navigator.mediaDevices.getUserMedia === undefined) { window.navigator.mediaDevices.getUserMedia = function (constraints) { var getUserMedia = (window.navigator as any).webkitGetUserMedia || (window.navigator as any).mozGetUserMedia; if (!getUserMedia) return Promise.reject(new Error('getUserMedia is not implemented in this browser')); return new Promise(function (resolve, reject) { getUserMedia.call(navigator, constraints, resolve, reject); }); } } if (typeof window.navigator?.mediaDevices?.getUserMedia !== 'function') throw new Error('VIDEO UNAVAILABLE ON THIS DEVICE') let set = 0 // 0 = video and audio, 1 = video, 2 = audio let stream for (let i = 0; i < constrains.length; i++) { set = constrains[i].video && constrains[i].audio ? 0 : constrains[i].video ? 1 : 2 try { stream = await window.navigator.mediaDevices.getUserMedia(constrains[i]) return [stream, set] } catch (er: any) { if (permission.test(er.message)) throw new Error('ISSUE GETTING ACCESS TO MEDIA DEVICES. PLEASE ALLOW PERMISSION TO ACCESS CAMERA/AUDIO WHEN ASKED.') if (!exception.test(er.message) && i === constrains.length - 1) { console.error('DEVICE REGEX FAILED 1', exception.test(er.message), er.message) throw new Error('ISSUE GETTING ACCESS TO MEDIA DEVICES.') } } } if (!stream) throw new Error('ISSUE GETTING ACCESS TO MEDIA DEVICES.') return [stream, set] }