UNPKG

@observertc/client-monitor-js

Version:

ObserveRTC Client Integration Javascript Library

1 lines 1.47 kB
import{ClientMetaTypes as e}from"../schema/ClientMetaTypes";const a="utils";export function watchMediaDevices(t,n){let i;if(void 0!==navigator)i=navigator;else{if(void 0===window||void 0===window.navigator)return n.error(`[${a}]:`,"Cannot integrate navigator.mediaDevices, because navigator is not available");i=window.navigator}const r=i.mediaDevices;if(!r)return n.error(`[${a}]:`,"Cannot integrate navigator.mediaDevices, because navigator.mediaDevices is not available");if(void 0===r.getUserMedia||"function"!=typeof r.getUserMedia)return n.error(`[${a}]:`,"Cannot integrate navigator.mediaDevices.getUserMedia, because getUserMedia is not a function");const o=r,s=o.getUserMedia.bind(o);o.getUserMedia=async a=>{try{return await s(a)}catch(a){throw t.addIssue({type:e.USER_MEDIA_ERROR,payload:{error:`${a}`}}),a}};try{const a=o.getSupportedConstraints();t.addMetaData({type:e.MEDIA_DEVICES_SUPPORTED_CONSTRAINTS,payload:{...a}})}catch(e){n.warn(`[${a}]:`,"Cannot get supported constraints",e)}const d=new Set,c=async()=>{try{const a=await o.enumerateDevices();for(const n of a){const a=`${n.groupId}-${n.deviceId}-${n.kind}-${n.label}`;d.has(a)||(t.addMetaData({type:e.MEDIA_DEVICE,payload:n.toJSON()}),d.add(a))}}catch(e){n.error(`[${a}]:`,"Cannot enumerate media devices",e)}};t.once("close",(()=>{o.getUserMedia=s,o.removeEventListener("devicechange",c)})),o.addEventListener("devicechange",c),c().catch((e=>{n.warn(`[${a}]:`,"Cannot enumerate media devices",e)}))}