"use strict";(self.webpackChunklernplattform_frontend=self.webpackChunklernplattform_frontend||[]).push([[97193],{53236:function(e,t,i){var o=this&&this.__awaiter||function(e,t,i,o){return new(i||(i=Promise))((function(n,s){function r(e){try{d(o.next(e))}catch(e){s(e)}}function a(e){try{d(o.throw(e))}catch(e){s(e)}}function d(e){var t;e.done?n(e.value):(t=e.value,t instanceof i?t:new i((function(e){e(t)}))).then(r,a)}d((o=o.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0});const n=i(956244),s=i(405032),r=i(44185),a=i(318988),d=i(618807),c=i(361729),u=i(638474),h=i(193099),v=i(378715),l=i(47086),g=i(298736),f=i(360478),p=i(420903),m=i(528349),I=i(396801),D=i(997094),C=i(466880);class S{constructor(e,t,i=new n.default,o){this.logger=e,this.browserBehavior=i,this.eventController=o,this.deviceInfoCache=null,this.activeDevices={audio:null,video:null},this.chosenVideoTransformDevice=null,this.audioOutputDeviceId=void 0,this.deviceChangeObservers=new Set,this.mediaStreamBrokerObservers=new Set,this.deviceLabelTrigger=()=>navigator.mediaDevices.getUserMedia({audio:!0,video:!0}),this.audioInputDestinationNode=null,this.audioInputSourceNode=null,this.videoInputQualitySettings=null,this.useWebAudio=!1,this.useMediaConstraintsFallback=!0,this.audioInputTaskQueue=new a.default,this.videoInputTaskQueue=new a.default,this.muted=!1,this.mediaStreamMuteObserver=(e,t)=>{for(const i of this.deviceChangeObservers)r.default.nextTick((()=>{this.deviceChangeObservers.has(i)&&i.audioInputMuteStateChanged&&i.audioInputMuteStateChanged(e,t)}))},this.alreadyHandlingDeviceChange=!1;const{enableWebAudio:d=!1,useMediaConstraintsFallback:c=!0}=t||{};this.useWebAudio=d,this.useMediaConstraintsFallback=c,this.videoInputQualitySettings=new D.default(S.defaultVideoWidth,S.defaultVideoHeight,S.defaultVideoFrameRate);const u=this.browserBehavior.requiresResolutionAlignment(this.videoInputQualitySettings.videoWidth,this.videoInputQualitySettings.videoHeight);this.videoInputQualitySettings.videoWidth=u[0],this.videoInputQualitySettings.videoHeight=u[1],this.logger.info(`DefaultDeviceController video dimension ${this.videoInputQualitySettings.videoWidth} x ${this.videoInputQualitySettings.videoHeight}`);try{this.mediaDeviceWrapper=(new s.default).create();const e=navigator.mediaDevices.getSupportedConstraints();this.logger.info(`Supported Constraints in this browser ${JSON.stringify(e)}`)}catch(t){e.error(t.message)}}isWatchingForDeviceChanges(){return!!this.onDeviceChangeCallback}ensureWatchingDeviceChanges(){var e;this.isWatchingForDeviceChanges()||(this.logger.info("Starting devicechange listener."),this.onDeviceChangeCallback=()=>{this.logger.info("Device change event callback is triggered"),this.handleDeviceChange()},null===(e=this.mediaDeviceWrapper)||void 0===e||e.addEventListener("devicechange",this.onDeviceChangeCallback))}stopWatchingDeviceChanges(){var e;this.isWatchingForDeviceChanges()&&(this.logger.info("Stopping devicechange listener."),null===(e=this.mediaDeviceWrapper)||void 0===e||e.removeEventListener("devicechange",this.onDeviceChangeCallback),this.onDeviceChangeCallback=void 0)}shouldObserveDeviceChanges(){return!!this.deviceChangeObservers.size||(this.activeDevices.audio&&null!==this.activeDevices.audio.constraints||this.activeDevices.video&&null!==this.activeDevices.video.constraints||!!this.audioOutputDeviceId)}watchForDeviceChangesIfNecessary(){this.shouldObserveDeviceChanges()?this.ensureWatchingDeviceChanges():this.stopWatchingDeviceChanges()}destroy(){return o(this,void 0,void 0,(function*(){this.stopWatchingDeviceChanges(),yield this.stopAudioInput(),yield this.stopVideoInput()}))}listAudioInputDevices(e=!1){return o(this,void 0,void 0,(function*(){const t=yield this.listDevicesOfKind("audioinput",e);return this.trace("listAudioInputDevices",e,t),t}))}listVideoInputDevices(e=!1){return o(this,void 0,void 0,(function*(){const t=yield this.listDevicesOfKind("videoinput",e);return this.trace("listVideoInputDevices",e,t),t}))}listAudioOutputDevices(e=!1){return o(this,void 0,void 0,(function*(){const t=yield this.listDevicesOfKind("audiooutput",e);return this.trace("listAudioOutputDevices",e,t),t}))}pushAudioMeetingStateForPermissions(e){var t;null===(t=this.eventController)||void 0===t||t.publishEvent(void 0===e?"audioInputUnselected":"audioInputSelected")}pushVideoMeetingStateForPermissions(e){var t;null===(t=this.eventController)||void 0===t||t.publishEvent(void 0===e?"videoInputUnselected":"videoInputSelected")}startAudioInput(e){return o(this,void 0,void 0,(function*(){return yield this.audioInputTaskQueue.add((()=>this.startAudioInputTask(e)))}))}startAudioInputTask(e){var t;return o(this,void 0,void 0,(function*(){if(void 0!==e)try{return h.isAudioTransformDevice(e)?(this.logger.info(`Choosing transform input device ${e}`),yield this.chooseAudioTransformInputDevice(e)):(this.logger.info(`Choosing intrinsic audio input device ${e}`),this.removeTransform(),yield this.chooseInputIntrinsicDevice("audio",e)),this.trace("startAudioInputDevice",e,"success"),this.useWebAudio?(this.attachAudioInputStreamToAudioContext(this.activeDevices.audio.stream),this.pushAudioMeetingStateForPermissions(this.getMediaStreamDestinationNode().stream),yield null===(t=this.transform)||void 0===t?void 0:t.device.mute(this.muted),this.getMediaStreamDestinationNode().stream):(this.publishAudioInputDidChangeEvent(this.activeDevices.audio.stream),this.activeDevices.audio.stream)}catch(e){throw e}else this.logger.error("Audio input device cannot be undefined")}))}stopAudioInput(){return o(this,void 0,void 0,(function*(){return this.audioInputTaskQueue.add((()=>this.stopAudioInputTask()))}))}stopAudioInputTask(){return o(this,void 0,void 0,(function*(){try{if(this.useWebAudio)return void this.releaseAudioTransformStream();this.stopTracksAndRemoveCallbacks("audio")}finally{this.watchForDeviceChangesIfNecessary(),this.publishAudioInputDidChangeEvent(void 0)}}))}chooseAudioTransformInputDevice(e){var t;return o(this,void 0,void 0,(function*(){if((null===(t=this.transform)||void 0===t?void 0:t.device)===e)return;if(!this.useWebAudio)throw new Error("Cannot apply transform device without enabling Web Audio.");const i=S.getAudioContext();if(i instanceof OfflineAudioContext);else switch(i.state){case"running":break;case"closed":throw new Error("Cannot choose a transform device with a closed audio context.");case"suspended":yield i.resume()}let o;try{o=yield e.createAudioNode(i)}catch(e){throw this.logger.error(`Unable to create transform device node: ${e}.`),e}const n=yield e.intrinsicDevice();yield this.chooseInputIntrinsicDevice("audio",n),this.logger.debug(`Got inner stream: ${n}.`),this.setTransform(e,o)}))}chooseVideoTransformInputDevice(e){var t;return o(this,void 0,void 0,(function*(){if(this.eventController&&e instanceof c.default&&e.passEventControllerToProcessors(this.eventController),e===this.chosenVideoTransformDevice)return void this.logger.info("Reselecting same VideoTransformDevice");const i=this.chosenVideoTransformDevice;i&&this.logger.info("Switched from previous VideoTransformDevice");const o=!!i,n=yield e.intrinsicDevice();if(!this.isMediaStreamReusableByDeviceId(null===(t=this.activeDevices.video)||void 0===t?void 0:t.stream,n))return this.logger.info("video transform device needs new intrinsic device"),o&&i.onOutputStreamDisconnect(),this.chosenVideoTransformDevice=e,this.activeDevices.video=null,yield this.chooseInputIntrinsicDevice("video",n),this.logger.info("apply processors to transform"),void(yield this.chosenVideoTransformDevice.transformStream(this.activeDevices.video.stream));this.chosenVideoTransformDevice=e,this.logger.info("video transform device uses previous stream"),this.logger.info("apply processors to transform"),yield e.transformStream(this.activeDevices.video.stream)}))}startVideoInput(e){return o(this,void 0,void 0,(function*(){return yield this.videoInputTaskQueue.add((()=>this.startVideoInputTask(e)))}))}startVideoInputTask(e){return o(this,void 0,void 0,(function*(){if(e)try{return C.isVideoTransformDevice(e)?(this.logger.info(`Choosing video transform device ${e}`),yield this.chooseVideoTransformInputDevice(e),this.publishVideoInputDidChangeEvent(this.chosenVideoTransformDevice.outputMediaStream),this.chosenVideoTransformDevice.outputMediaStream):(this.chosenVideoInputIsTransformDevice()&&(this.chosenVideoTransformDevice.onOutputStreamDisconnect(),this.chosenVideoTransformDevice=null),yield this.chooseInputIntrinsicDevice("video",e),this.trace("startVideoInputDevice",e),this.publishVideoInputDidChangeEvent(this.activeDevices.video.stream),this.activeDevices.video.stream)}catch(e){throw e}else this.logger.error("Invalid video input device")}))}stopVideoInput(){return o(this,void 0,void 0,(function*(){return this.videoInputTaskQueue.add((()=>this.stopVideoInputTask()))}))}stopVideoInputTask(){return o(this,void 0,void 0,(function*(){try{if(this.chosenVideoInputIsTransformDevice())return void this.releaseVideoTransformStream();this.stopTracksAndRemoveCallbacks("video")}finally{this.watchForDeviceChangesIfNecessary(),this.publishVideoInputDidChangeEvent(void 0)}}))}chooseAudioOutput(e){return o(this,void 0,void 0,(function*(){this.audioOutputDeviceId=e,this.watchForDeviceChangesIfNecessary();const t=this.deviceInfoFromDeviceId("audiooutput",this.audioOutputDeviceId);this.publishAudioOutputDidChangeEvent(t),this.trace("chooseAudioOutput",e,null)}))}addDeviceChangeObserver(e){this.logger.info("adding device change observer"),this.deviceChangeObservers.add(e),this.watchForDeviceChangesIfNecessary(),this.trace("addDeviceChangeObserver")}removeDeviceChangeObserver(e){this.logger.info("removing device change observer"),this.deviceChangeObservers.delete(e),this.watchForDeviceChangesIfNecessary(),this.trace("removeDeviceChangeObserver")}createAnalyserNodeForAudioInput(){var e,t;if(!this.activeDevices.audio)return null;const i=null===(t=null===(e=this.transform)||void 0===e?void 0:e.nodes)||void 0===t?void 0:t.end;if(i){const e=i.context.createAnalyser();return e.removeOriginalInputs=()=>{try{i.disconnect(e)}catch(e){}},i.connect(e),e}return this.createAnalyserNodeForRawAudioInput()}createAnalyserNodeForRawAudioInput(){return this.activeDevices.audio?this.createAnalyserNodeForStream(this.activeDevices.audio.stream):null}createAnalyserNodeForStream(e){const t=S.getAudioContext(),i=t.createAnalyser(),o=t.createMediaStreamSource(e);return o.connect(i),this.trace("createAnalyserNodeForAudioInput"),i.removeOriginalInputs=()=>{try{o.disconnect(i)}catch(e){}},i}startVideoPreviewForVideoInput(e){if(!this.activeDevices.video)return this.logger.warn("cannot bind video preview since video input device has not been chosen"),void this.trace("startVideoPreviewForVideoInput",e.id);u.default.connectVideoStreamToVideoElement(this.chosenVideoTransformDevice?this.chosenVideoTransformDevice.outputMediaStream:this.activeDevices.video.stream,e,!0),this.trace("startVideoPreviewForVideoInput",e.id)}stopVideoPreviewForVideoInput(e){u.default.disconnectVideoStreamFromVideoElement(e,!1),this.trace("stopVideoPreviewForVideoInput",e.id)}setDeviceLabelTrigger(e){if(this.deviceInfoCache)for(const e of this.deviceInfoCache)if(!e.label){this.deviceInfoCache=null;break}this.deviceLabelTrigger=e,this.trace("setDeviceLabelTrigger")}mixIntoAudioInput(e){let t=null;return this.useWebAudio?(t=S.getAudioContext().createMediaStreamSource(e),t.connect(this.getMediaStreamOutputNode())):this.logger.warn("WebAudio is not enabled, mixIntoAudioInput will not work"),this.trace("mixIntoAudioInput",e.id),t}chooseVideoInputQuality(e,t,i){const o=this.browserBehavior.requiresResolutionAlignment(e,t);this.videoInputQualitySettings=new D.default(o[0],o[1],i)}getVideoInputQualitySettings(){return this.videoInputQualitySettings}acquireAudioInputStream(){return o(this,void 0,void 0,(function*(){return this.activeDevices.audio||(this.logger.info("No audio device chosen, creating empty audio device"),yield this.startAudioInput(null)),this.useWebAudio?this.getMediaStreamDestinationNode().stream:this.activeDevices.audio.stream}))}acquireVideoInputStream(){return o(this,void 0,void 0,(function*(){if(!this.activeDevices.video)throw new Error("No video device chosen");return this.chosenVideoInputIsTransformDevice()?this.chosenVideoTransformDevice.outputMediaStream:this.activeDevices.video.stream}))}acquireDisplayInputStream(e){return o(this,void 0,void 0,(function*(){throw new Error("unsupported")}))}releaseAudioTransformStream(){this.logger.info("Stopping audio track for Web Audio graph"),this.stopTracksAndRemoveCallbacks("audio"),this.logger.info("Removing audio transform, if there is one."),this.removeTransform(),this.audioInputSourceNode&&(this.audioInputSourceNode.disconnect(),this.audioInputSourceNode=void 0),this.audioInputDestinationNode&&(this.audioInputDestinationNode.disconnect(),this.audioInputDestinationNode=void 0)}releaseVideoTransformStream(){this.logger.info("Stopping video track for transform"),this.stopTracksAndRemoveCallbacks("video"),this.logger.info("Disconnecting video transform"),this.chosenVideoTransformDevice.onOutputStreamDisconnect(),this.chosenVideoTransformDevice=null}stopTracksAndRemoveCallbacks(e){const t=this.activeDevices[e];if(!t)return;const i=t.endedCallback,o=t.trackMuteCallback,n=t.trackUnmuteCallback;for(const s of t.stream.getTracks())s.stop(),i&&s.removeEventListener("ended",i),o&&s.removeEventListener("mute",o),n&&s.removeEventListener("unmute",n),delete t.endedCallback,delete t.trackMuteCallback,delete t.trackUnmuteCallback,delete this.activeDevices[e]}chosenVideoInputIsTransformDevice(){return!!this.chosenVideoTransformDevice}muteLocalAudioInputStream(){this.toggleLocalAudioInputStream(!1)}unmuteLocalAudioInputStream(){this.toggleLocalAudioInputStream(!0)}toggleLocalAudioInputStream(e){var t;let i=this.activeDevices.audio;if(this.useWebAudio&&(i=this.getMediaStreamDestinationNode()),i){for(const t of i.stream.getTracks())t.enabled!==e&&(t.enabled=e);this.muted!==!e&&(this.muted=!e,null===(t=this.transform)||void 0===t||t.device.mute(this.muted))}}static getIntrinsicDeviceId(e){if(!e)return;if("string"==typeof e)return e;if(e.id)return e.id;const t=e.deviceId;if(!t)return;if("string"==typeof t||Array.isArray(t))return t;const i=t;return"string"==typeof i.exact||Array.isArray(i.exact)?i.exact:void 0}static createEmptyAudioDevice(){return S.synthesizeAudioDevice(0)}static synthesizeAudioDevice(e){const t=S.getAudioContext(),i=t.createMediaStreamDestination();if(e){const o=t.createGain();o.gain.value=.1,o.connect(i);const n=t.createOscillator();n.frequency.value=e,n.connect(o),n.start()}else{const e=t.createBufferSource();try{e.buffer=t.createBuffer(1,5*t.sampleRate,t.sampleRate)}catch(i){if(!i||"NotSupportedError"!==i.name)throw i;e.buffer=t.createBuffer(1,5*S.defaultSampleRate,S.defaultSampleRate)}e.buffer.getChannelData(0)[0]=3e-4,e.loop=!0,e.connect(i),e.start()}return i.stream}listDevicesOfKind(e,t){return o(this,void 0,void 0,(function*(){return!t&&null!==this.deviceInfoCache&&this.isWatchingForDeviceChanges()||(yield this.updateDeviceInfoCacheFromBrowser()),this.listCachedDevicesOfKind(e)}))}updateDeviceInfoCacheFromBrowser(){var e,t;return o(this,void 0,void 0,(function*(){if("undefined"==typeof MediaDeviceInfo)return void(this.deviceInfoCache=[]);let i=yield navigator.mediaDevices.enumerateDevices(),o=!0;for(const e of i)if(!e.label){o=!1;break}if(!o)try{this.logger.info("attempting to trigger media device labels since they are hidden");const e=yield this.deviceLabelTrigger();i=yield navigator.mediaDevices.enumerateDevices();for(const t of e.getTracks())t.stop()}catch(i){this.logger.info("unable to get media device labels"),null===(e=this.eventController)||void 0===e||e.publishEvent("audioInputFailed",{audioInputErrorMessage:this.getErrorMessage(i)}),null===(t=this.eventController)||void 0===t||t.publishEvent("videoInputFailed",{videoInputErrorMessage:this.getErrorMessage(i)})}this.logger.debug(`Update device info cache with devices: ${JSON.stringify(i)}`),this.deviceInfoCache=i}))}listCachedDevicesOfKind(e){const t=[];if(this.deviceInfoCache)for(const i of this.deviceInfoCache)i.kind===e&&t.push(i);return t}handleDeviceChange(){return o(this,void 0,void 0,(function*(){if(null===this.deviceInfoCache)return;if(this.alreadyHandlingDeviceChange)return void r.default.nextTick((()=>{this.handleDeviceChange()}));this.alreadyHandlingDeviceChange=!0;const e=this.listCachedDevicesOfKind("audioinput"),t=this.listCachedDevicesOfKind("videoinput"),i=this.listCachedDevicesOfKind("audiooutput");yield this.updateDeviceInfoCacheFromBrowser();const o=this.listCachedDevicesOfKind("audioinput"),n=this.listCachedDevicesOfKind("videoinput"),s=this.listCachedDevicesOfKind("audiooutput");this.forEachObserver((r=>{this.areDeviceListsEqual(e,o)||d.Maybe.of(r.audioInputsChanged).map((e=>e.bind(r)(o))),this.areDeviceListsEqual(t,n)||d.Maybe.of(r.videoInputsChanged).map((e=>e.bind(r)(n))),this.areDeviceListsEqual(i,s)||d.Maybe.of(r.audioOutputsChanged).map((e=>e.bind(r)(s)))})),this.alreadyHandlingDeviceChange=!1}))}handleDeviceStreamEnded(e,t){return o(this,void 0,void 0,(function*(){try{"audio"===e?(this.logger.warn("Audio input device which was active is no longer available, resetting to null device"),yield this.startAudioInput(null)):(this.logger.warn("Video input device which was active is no longer available, stopping video"),yield this.stopVideoInput())}catch(e){this.logger.error("Failed to choose null device after stream ended.")}"audio"===e?this.forEachObserver((e=>{d.Maybe.of(e.audioInputStreamEnded).map((i=>i.bind(e)(t)))})):this.forEachObserver((e=>{d.Maybe.of(e.videoInputStreamEnded).map((i=>i.bind(e)(t)))}))}))}forEachObserver(e){for(const t of this.deviceChangeObservers)r.default.nextTick((()=>{this.deviceChangeObservers.has(t)&&e(t)}))}forEachMediaStreamBrokerObserver(e){for(const t of this.mediaStreamBrokerObservers)e(t)}areDeviceListsEqual(e,t){return JSON.stringify(e.map((e=>JSON.stringify(e))).sort())===JSON.stringify(t.map((e=>JSON.stringify(e))).sort())}intrinsicDeviceAsMediaStream(e){return e&&e.id?e:null}hasSameMediaStreamId(e,t,i){var o,n,s,r;let a;return"audio"===e?(a=null==i?void 0:i.audio.streamId,!!a&&a===(null===(n=null===(o=t.constraints)||void 0===o?void 0:o.audio)||void 0===n?void 0:n.streamId)):(a=null==i?void 0:i.video.streamId,!!a&&a===(null===(r=null===(s=null==t?void 0:t.constraints)||void 0===s?void 0:s.video)||void 0===r?void 0:r.streamId))}hasSameGroupId(e,t,i){if(""===e)return!0;const o=S.getIntrinsicDeviceId(i);return this.logger.debug(`Checking deviceIds ${o} of type ${typeof o} with groupId ${e}`),"string"==typeof o&&e===this.getGroupIdFromDeviceId(t,o)}getGroupIdFromDeviceId(e,t){if(null!==this.deviceInfoCache){const i=this.listCachedDevicesOfKind(`${e}input`).find((e=>e.deviceId===t));if(i&&i.groupId)return this.logger.debug(`GroupId of deviceId ${t} found in cache is ${i.groupId}`),i.groupId}return this.logger.debug(`GroupId of deviceId ${t} found in cache is empty`),""}handleGetUserMediaError(e,t){if(!e)throw new l.default(e);switch(e.name){case"NotReadableError":case"TrackStartError":throw new f.default(e);case"NotFoundError":case"DevicesNotFoundError":throw new g.default(e);case"NotAllowedError":case"PermissionDeniedError":case"SecurityError":throw t&&t<S.permissionDeniedOriginDetectionThresholdMs?new m.default(e,"Permission denied by browser"):new m.default(e,"Permission denied by user");case"OverconstrainedError":case"ConstraintNotSatisfiedError":throw new p.default(e);case"TypeError":throw new I.default(e);default:throw new l.default(e)}}matchesDeviceSelection(e,t,i,o){return!!(i&&i.stream.active&&(this.hasSameMediaStreamId(e,i,o)||null!==i.groupId&&this.hasSameGroupId(i.groupId,e,t)))&&(this.logger.debug(`Compare current device constraint ${JSON.stringify(i.constraints)} to proposed constraints ${JSON.stringify(o)}`),i.matchesConstraints(o))}chooseInputIntrinsicDevice(e,t){var i,n;return o(this,void 0,void 0,(function*(){const o=this.getMediaStreamConstraints(e,t);if(this.matchesDeviceSelection(e,t,this.activeDevices[e],o))return void this.logger.info(`reusing existing ${e} input device`);this.activeDevices[e]&&this.activeDevices[e].stream&&this.stopTracksAndRemoveCallbacks(e);const s=Date.now(),r=new v.default;try{this.logger.info(`requesting new ${e} device with constraint ${JSON.stringify(o)}`);const i=this.intrinsicDeviceAsMediaStream(t);"audio"===e&&null===t?(r.stream=S.createEmptyAudioDevice(),r.constraints=null):i?(this.logger.info(`using media stream ${i.id} for ${e} device`),r.stream=i,r.constraints=o):(r.stream=yield navigator.mediaDevices.getUserMedia(o),r.constraints=o),yield this.handleNewInputDevice(e,r)}catch(a){const d=this.getErrorMessage(a);"audio"===e?null===(i=this.eventController)||void 0===i||i.publishEvent("audioInputFailed",{audioInputErrorMessage:d}):null===(n=this.eventController)||void 0===n||n.publishEvent("videoInputFailed",{videoInputErrorMessage:d}),this.logger.error(`failed to get ${e} device for constraints ${JSON.stringify(o)}: ${d}`);let c=!0;if(a&&"constraint"in a&&(this.logger.error(`Over-constrained by constraint: ${a.constraint}`),this.useMediaConstraintsFallback)){const i=this.getMediaStreamConstraints(e,t,!0),n=JSON.stringify(i);if(n!==JSON.stringify(o)){this.logger.info(`retry requesting new ${e} device with minimal constraint ${n}`);try{r.stream=yield navigator.mediaDevices.getUserMedia(i),r.constraints=i,yield this.handleNewInputDevice(e,r),c=!1}catch(t){this.logger.error(`failed to get ${e} device for constraints ${n}: ${this.getErrorMessage(t)}`)}}}if(c){if("audio"===e){this.logger.info(`choosing null ${e} device instead`);try{r.stream=S.createEmptyAudioDevice(),r.constraints=null,yield this.handleNewInputDevice(e,r)}catch(t){this.logger.error(`failed to choose null ${e} device. ${t.name}: ${t.message}`)}}this.handleGetUserMediaError(a,Date.now()-s)}}finally{this.watchForDeviceChangesIfNecessary()}}))}getErrorMessage(e){return e?e.name&&e.message?`${e.name}: ${e.message}`:e.name?e.name:e.message?e.message:"UnknownError":"UnknownError"}handleNewInputDevice(e,t){var i;return o(this,void 0,void 0,(function*(){this.logger.info(`got ${e} device for constraints ${JSON.stringify(t.constraints)}`);const o=null===(i=this.getMediaTrackSettings(t.stream))||void 0===i?void 0:i.deviceId;t.groupId=o?this.getGroupIdFromDeviceId(e,o):"",this.activeDevices[e]=t,this.logger.debug(`Set activeDevice to ${JSON.stringify(t)}`),this.watchForDeviceChangesIfNecessary();const n=t.stream.getTracks()[0];if(n&&(t.endedCallback=()=>{this.activeDevices[e]&&this.activeDevices[e].stream===t.stream&&(this.handleDeviceStreamEnded(e,o),delete t.endedCallback)},n.addEventListener("ended",t.endedCallback,{once:!0})),"audio"===e){const e=t.stream.getAudioTracks()[0];if(e){const i=e.getSettings().deviceId||t.stream;t.trackMuteCallback=()=>{this.mediaStreamMuteObserver(i,!0)},t.trackUnmuteCallback=()=>{this.mediaStreamMuteObserver(i,!1)},e.addEventListener("mute",t.trackMuteCallback,{once:!1}),e.addEventListener("unmute",t.trackUnmuteCallback,{once:!1}),this.logger.debug("Notifying mute state after selection"),e.muted?t.trackMuteCallback():t.trackUnmuteCallback()}}}))}calculateMediaStreamConstraints(e,t,i,o){if(o&&1===this.listCachedDevicesOfKind(`${e}input`).length)return!0;const n={};return this.browserBehavior.requiresNoExactMediaStreamConstraints()?n.deviceId=t:n.deviceId={exact:t},i&&(n.groupId=i),o?n:"video"===e?(n.width={ideal:this.videoInputQualitySettings.videoWidth},n.height={ideal:this.videoInputQualitySettings.videoHeight},n.frameRate={ideal:this.videoInputQualitySettings.videoFrameRate},n):(this.supportSampleRateConstraint()&&(n.sampleRate={ideal:S.defaultSampleRate}),this.supportSampleSizeConstraint()&&(n.sampleSize={ideal:S.defaultSampleSize}),this.supportChannelCountConstraint()&&(n.channelCount={ideal:S.defaultChannelCount}),Object.assign({echoCancellation:!0,googEchoCancellation:!0,googEchoCancellation2:!0,googAutoGainControl:!0,googAutoGainControl2:!0,googNoiseSuppression:!0,googNoiseSuppression2:!0,googHighpassFilter:!0},n))}getMediaStreamConstraintsFromTrackConstraints(e,t){return"audio"===e?{audio:t}:{video:t}}getMediaStreamConstraints(e,t,i=!1){let o={};if(!t)return null;const n=this.intrinsicDeviceAsMediaStream(t);if(n)return o.streamId=n.id,this.getMediaStreamConstraintsFromTrackConstraints(e,o);if("string"==typeof t){let n="";return this.browserBehavior.requiresGroupIdMediaStreamConstraints()&&(null!==this.deviceInfoCache?n=this.getGroupIdFromDeviceId(e,t):this.logger.error("Device cache is not populated. Please make sure to call list devices first")),o=this.calculateMediaStreamConstraints(e,t,n,i),this.getMediaStreamConstraintsFromTrackConstraints(e,o)}return function(e){return"object"==typeof e&&"deviceId"in e&&"groupId"in e&&"kind"in e&&"label"in e}(t)?(o=this.calculateMediaStreamConstraints(e,t.deviceId,t.groupId,i),this.getMediaStreamConstraintsFromTrackConstraints(e,o)):(o=t,this.getMediaStreamConstraintsFromTrackConstraints(e,o))}deviceInfoFromDeviceId(e,t){if(null===this.deviceInfoCache)return null;for(const i of this.deviceInfoCache)if(i.kind===e&&i.deviceId===t)return i;return null}hasAppliedTransform(){return!!this.transform}isMediaStreamReusableByDeviceId(e,t){if(!e||!e.active||!t)return!1;if(t.id)return e.id===t.id;const i=this.getMediaTrackSettings(e);if(!i.deviceId)return!1;const o=S.getIntrinsicDeviceId(t);return"string"==typeof o&&i.deviceId===o}getMediaTrackSettings(e){var t;return null===(t=e.getTracks()[0])||void 0===t?void 0:t.getSettings()}reconnectAudioInputs(){if(!this.audioInputSourceNode)return;this.audioInputSourceNode.disconnect();const e=this.getMediaStreamOutputNode();this.audioInputSourceNode.connect(e)}setTransform(e,t){var i,o;null===(o=null===(i=this.transform)||void 0===i?void 0:i.nodes)||void 0===o||o.end.disconnect(),this.transform={nodes:t,device:e};const n=null==t?void 0:t.end,s=this.getMediaStreamDestinationNode();this.logger.debug(`Connecting transform node ${n} to destination ${s}.`),null==n||n.connect(s),this.reconnectAudioInputs()}removeTransform(){var e;const t=this.transform;if(t)return null===(e=this.transform.nodes)||void 0===e||e.end.disconnect(),this.transform=void 0,this.reconnectAudioInputs(),t}attachAudioInputStreamToAudioContext(e){var t;null===(t=this.audioInputSourceNode)||void 0===t||t.disconnect(),this.audioInputSourceNode=S.getAudioContext().createMediaStreamSource(e);const i=this.getMediaStreamOutputNode();this.audioInputSourceNode.connect(i)}getMediaStreamDestinationNode(){return this.audioInputDestinationNode||(this.audioInputDestinationNode=S.getAudioContext().createMediaStreamDestination()),this.audioInputDestinationNode}getMediaStreamOutputNode(){var e,t;return(null===(t=null===(e=this.transform)||void 0===e?void 0:e.nodes)||void 0===t?void 0:t.start)||this.getMediaStreamDestinationNode()}static setDefaultLatencyHint(e){S.defaultLatencyHint=e}static getAudioContext(){if(!S.audioContext){const e={};navigator.mediaDevices.getSupportedConstraints().sampleRate&&(e.sampleRate=S.defaultSampleRate),(new n.default).requiresPlaybackLatencyHintForAudioContext()&&(e.latencyHint="playback"),S.defaultLatencyHint&&(e.latencyHint=S.defaultLatencyHint),S.audioContext=new(window.AudioContext||window.webkitAudioContext)(e)}return S.audioContext}static closeAudioContext(){if(S.audioContext)try{S.audioContext.close()}catch(e){}S.audioContext=null}addMediaStreamBrokerObserver(e){this.mediaStreamBrokerObservers.add(e)}removeMediaStreamBrokerObserver(e){this.mediaStreamBrokerObservers.delete(e)}publishVideoInputDidChangeEvent(e){this.forEachMediaStreamBrokerObserver((t=>{t.videoInputDidChange&&t.videoInputDidChange(e)})),this.pushVideoMeetingStateForPermissions(e)}publishAudioInputDidChangeEvent(e){this.forEachMediaStreamBrokerObserver((t=>{t.audioInputDidChange&&t.audioInputDidChange(e)})),this.pushAudioMeetingStateForPermissions(e)}publishAudioOutputDidChangeEvent(e){this.forEachMediaStreamBrokerObserver((t=>{t.audioOutputDidChange&&t.audioOutputDidChange(e)}))}supportSampleRateConstraint(){return this.useWebAudio&&!!navigator.mediaDevices.getSupportedConstraints().sampleRate}supportSampleSizeConstraint(){return this.useWebAudio&&!!navigator.mediaDevices.getSupportedConstraints().sampleSize}supportChannelCountConstraint(){return this.useWebAudio&&!!navigator.mediaDevices.getSupportedConstraints().channelCount}trace(e,t,i){let o=`API/DefaultDeviceController/${e}`;void 0!==t&&(o+=` ${JSON.stringify(t)}`),void 0!==i&&(o+=` -> ${JSON.stringify(i)}`),this.logger.info(o)}}t.default=S,S.permissionDeniedOriginDetectionThresholdMs=500,S.defaultVideoWidth=960,S.defaultVideoHeight=540,S.defaultVideoFrameRate=15,S.defaultSampleRate=48e3,S.defaultSampleSize=16,S.defaultChannelCount=1,S.audioContext=null}}]);