var I=function(r,e){return I=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,i){t.__proto__=i}||function(t,i){for(var n in i)Object.prototype.hasOwnProperty.call(i,n)&&(t[n]=i[n])},I(r,e)};function x(r,e){if(typeof e!="function"&&e!==null)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");I(r,e);function t(){this.constructor=r}r.prototype=e===null?Object.create(e):(t.prototype=e.prototype,new t)}function O(r){var e=typeof Symbol=="function"&&Symbol.iterator,t=e&&r[e],i=0;if(t)return t.call(r);if(r&&typeof r.length=="number")return{next:function(){return r&&i>=r.length&&(r=void 0),{value:r&&r[i++],done:!r}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function P(r,e){var t=typeof Symbol=="function"&&r[Symbol.iterator];if(!t)return r;var i=t.call(r),n,o=[],a;try{for(;(e===void 0||e-- >0)&&!(n=i.next()).done;)o.push(n.value)}catch(s){a={error:s}}finally{try{n&&!n.done&&(t=i.return)&&t.call(i)}finally{if(a)throw a.error}}return o}function M(r,e,t){if(t||arguments.length===2)for(var i=0,n=e.length,o;i0},enumerable:!1,configurable:!0}),e.prototype._trySubscribe=function(t){return this._throwIfClosed(),r.prototype._trySubscribe.call(this,t)},e.prototype._subscribe=function(t){return this._throwIfClosed(),this._checkFinalizedStatuses(t),this._innerSubscribe(t)},e.prototype._innerSubscribe=function(t){var i=this,n=this,o=n.hasError,a=n.isStopped,s=n.observers;return o||a?$:(this.currentObservers=null,s.push(t),new D(function(){i.currentObservers=null,j(s,t)}))},e.prototype._checkFinalizedStatuses=function(t){var i=this,n=i.hasError,o=i.thrownError,a=i.isStopped;n?t.error(o):a&&t.complete()},e.prototype.asObservable=function(){var t=new H;return t.source=this,t},e.create=function(t,i){return new q(t,i)},e}(H),q=function(r){x(e,r);function e(t,i){var n=r.call(this)||this;return n.destination=t,n.source=i,n}return e.prototype.next=function(t){var i,n;(n=(i=this.destination)===null||i===void 0?void 0:i.next)===null||n===void 0||n.call(i,t)},e.prototype.error=function(t){var i,n;(n=(i=this.destination)===null||i===void 0?void 0:i.error)===null||n===void 0||n.call(i,t)},e.prototype.complete=function(){var t,i;(i=(t=this.destination)===null||t===void 0?void 0:t.complete)===null||i===void 0||i.call(t)},e.prototype._subscribe=function(t){var i,n;return(n=(i=this.source)===null||i===void 0?void 0:i.subscribe(t))!==null&&n!==void 0?n:$},e}(J),ce=function(r){x(e,r);function e(t){var i=r.call(this)||this;return i._value=t,i}return Object.defineProperty(e.prototype,"value",{get:function(){return this.getValue()},enumerable:!1,configurable:!0}),e.prototype._subscribe=function(t){var i=r.prototype._subscribe.call(this,t);return!i.closed&&t.next(this._value),i},e.prototype.getValue=function(){var t=this,i=t.hasError,n=t.thrownError,o=t._value;if(i)throw n;return this._throwIfClosed(),o},e.prototype.next=function(t){r.prototype.next.call(this,this._value=t)},e}(J);const b=new ce({frameRate:30}),Y=()=>!!navigator.mediaDevices&&!!navigator.mediaDevices.getDisplayMedia;function le(r){const e=document.createElement("video");e.id="simaq-video",e.setAttribute("autoplay","true"),e.setAttribute("mute","true"),e.style.width="600px",e.style.height="400px",e.style.position="fixed",e.style.top="0px",e.style.right="0px",e.style.zIndex="1000",e.style.display=r?"block":"none",document.body.appendChild(e)}function de(){return document.getElementById("simaq-video")}var K={exports:{}};(function(r){var e=Object.prototype.hasOwnProperty,t="~";function i(){}Object.create&&(i.prototype=Object.create(null),new i().__proto__||(t=!1));function n(c,u,l){this.fn=c,this.context=u,this.once=l||!1}function o(c,u,l,h,y){if(typeof l!="function")throw new TypeError("The listener must be a function");var p=new n(l,h||c,y),f=t?t+u:u;return c._events[f]?c._events[f].fn?c._events[f]=[c._events[f],p]:c._events[f].push(p):(c._events[f]=p,c._eventsCount++),c}function a(c,u){--c._eventsCount===0?c._events=new i:delete c._events[u]}function s(){this._events=new i,this._eventsCount=0}s.prototype.eventNames=function(){var u=[],l,h;if(this._eventsCount===0)return u;for(h in l=this._events)e.call(l,h)&&u.push(t?h.slice(1):h);return Object.getOwnPropertySymbols?u.concat(Object.getOwnPropertySymbols(l)):u},s.prototype.listeners=function(u){var l=t?t+u:u,h=this._events[l];if(!h)return[];if(h.fn)return[h.fn];for(var y=0,p=h.length,f=new Array(p);ynew Promise(i=>setTimeout(i,t)),this.autoDownload=e.autoDownload,this.platform=e.platform,this.platformConfig=e.config,this.uploadUrl=e.uploadUrl,this.disabledAudio=e.disabledAudio,this.debug=e.debug,this.systemAudio=e.systemAudio,this.setSystemAudio(),this.initParams(e),b.subscribe(t=>{this.debug&&console.log("subscribe",t),this.displayMediaStreamConstraints.video=Object.assign({},this.displayMediaStreamConstraints.video,t)}),this.debug&&console.log("arg",e)}get isElectron(){return this.platform==="electron"}get isWeb(){return this.platform==="web"}get isCanvas(){return this.platform==="canvas"}get canvasElement(){return document.querySelector(this.canvasId)}set canvasElement(e){this.canvasElement=e}initParams(e){switch(e.platform){case"web":e.config.frameRate&&(b.value.frameRate=e.config.frameRate);break;case"electron":this.chromeMediaSourceId=e.config.chromeMediaSourceId;break;case"canvas":this.canvasId=e.config.canvasId;break}}setSystemAudio(){this.systemAudio?(this.displayMediaStreamConstraints.systemAudio="include",this.displayMediaStreamConstraints.audio=!0):(this.displayMediaStreamConstraints.systemAudio="exclude",this.displayMediaStreamConstraints.audio=!1)}async startRecord(){try{if(!this.isStartRecoding){if(this.debug&&console.log("\u5F00\u59CB\u5F55\u5C4F\uFF01",Y()),!Y()){console.error("\u5F53\u524D\u6D4F\u89C8\u5668\u4E0D\u652F\u6301\u5F55\u5C4F\u6216\u4E0D\u5B58\u5728https\u73AF\u5883");return}const e=await this.getDefaultMedia();if(this.debug&&console.log("media",e),e){this.emit("startRecord"),this.isStartRecoding=!0,this.status=_.start;const t=de();t&&(t.srcObject=e,this.stream=e,await this.createMediaRecoder(),this.mediaRecorder.start(),this.stream.getVideoTracks()[0].onended=()=>{this.debug&&console.log("stop-share"),this.endRecord()})}else this.streamStop(),this.isStartRecoding=!1,this.status=_.end,this.emit("cancelRecord")}}catch(e){console.error("startRecord::",e)}}getDefaultMedia(){return new Promise(async e=>{switch(this.platform){case"web":return e(await this.getDisplayMedia());case"canvas":return e(await this.getCanvasSteam());case"electron":return e(await this.getEletronDisplayMedia());default:return e(await this.getDisplayMedia())}})}getCanvasSteam(){return new Promise(async e=>{try{const t=await this.getDeaultAudio();t&&(this.audioInput=t),this.debug&&console.log("audioInput",t),console.log("this.canvasElement",this.canvasElement);let i=30;typeof this.platformConfig.frameRate=="number"&&(i=this.platformConfig.frameRate);const n=this.canvasElement.captureStream(i);if(n){const o=n.getVideoTracks()[0];return o&&o.applyConstraints({advanced:[{width:b.value.width,height:b.value.height,frameRate:b.value.frameRate}]}),e(n)}return e(null)}catch{return e(null)}})}getEletronDisplayMedia(){return new Promise(async e=>{try{const t=await this.getEletronDeaultAudio();if(t&&(this.audioInput=t),console.log("eletron-audioInput",t),navigator.mediaDevices.getDisplayMedia){const i={mandatory:{chromeMediaSource:"desktop",chromeMediaSourceId:this.chromeMediaSourceId,minWidth:this.platformConfig.minWidth||1280,maxWidth:this.platformConfig.maxWidth||3840,minHeight:this.platformConfig.minHeight||720,maxHeight:this.platformConfig.maxHeight||2160,maxFrameRate:this.platformConfig.maxFrameRate||30,minFrameRate:this.platformConfig.minFrameRate||30}};this.debug&&console.log("videoConfig",i);const n=await navigator.mediaDevices.getUserMedia({audio:!1,video:i});return e(n)}return e(null)}catch{return e(null)}})}getDisplayMedia(){return new Promise(async e=>{try{const t=await this.getDeaultAudio();if(t&&(this.audioInput=t),this.debug&&console.log("audioInput",t),navigator.mediaDevices.getDisplayMedia){this.debug&&console.log("displayMediaStreamConstraints",this.displayMediaStreamConstraints);const i=await navigator.mediaDevices.getDisplayMedia(this.displayMediaStreamConstraints);return e(i)}return e(null)}catch{return e(null)}})}async getDeaultAudio(){return new Promise(async e=>{try{const t={echoCancellation:!0,autoGainControl:!0,noiseSuppression:!0,latency:0};if(console.log("audioConfig",t),navigator.mediaDevices.getUserMedia){const i=await navigator.mediaDevices.getUserMedia({audio:t,video:!1});return e(i)}return e(null)}catch{return e(null)}})}async getEletronDeaultAudio(){return new Promise(async e=>{try{if(navigator.mediaDevices.getUserMedia){const t=await navigator.mediaDevices.getUserMedia({video:!1,audio:{deviceId:"default"}});return e(t)}return e(null)}catch{return e(null)}})}holdRecord(){this.isStartRecoding=!1,this.status=_.hold,this.streamStop()}async endRecord(){return new Promise(async e=>{try{this.streamStop(),await this.sleep(1e3),this.isStartRecoding=!1,this.status=_.end;const t=this.recordChunks.slice();console.log("last-dump",t),this.autoDownload&&t!=null&&t.length&&this.handleAutoDownload(t),this.emit("endRecord",t),this.passiveEnd=!1,this.recordChunks=[],e(t)}catch{this.emit("endRecord",null),e([])}})}streamStop(){this.stream&&this.stream.getTracks().forEach(e=>e.stop()),this.audioInput&&this.audioInput.getTracks().forEach(e=>e.stop()),this.mediaRecorder&&this.mediaRecorder.stop()}async createMediaRecoder(){try{let e,t;this.audioInput&&([t]=this.stream.getVideoTracks(),[e]=this.audioInput.getAudioTracks(),this.stream=new MediaStream([t,e]));const i=window.Howler;if((i==null?void 0:i.ctx)&&!this.disabledAudio){const o=i.ctx.createMediaStreamDestination();i.masterGain.connect(o),console.log("streamDest",o),window.streamAudio=o,[t]=this.stream.getVideoTracks(),[e]=o.stream.getAudioTracks(),console.log("audioTrack",e),e.onended=a=>{console.log("audioTrack-end",a),this.mediaRecorder.stop()},this.stream=new MediaStream([t,e])}const n=new MediaRecorder(this.stream,{mimeType:this.codecType,audioBitsPerSecond:b.value.audioBitsPerSecond,videoBitsPerSecond:b.value.videoBitsPerSecond});this.mediaRecorder=n,window.__SIMAQ__=n,this.mediaRecorder.ondataavailable=o=>{this.recordChunks.push(o.data),this.emit("record",new Blob([o.data],{type:this.codecFileType}))},this.mediaRecorder.stop=()=>{},this.mediaRecorder.onerror=o=>{console.error(`onerror stream: ${o.error.name},`,o)},this.mediaRecorder.onstop=o=>{console.warn("onstop stream:",o)},this.mediaRecorder.onpause=o=>{console.warn("onpause stream: ",o)},this.mediaRecorder.onresume=o=>{console.warn("onresume stream: ",o)}}catch(e){console.error("error",e)}}handleAutoDownload(e){const t=new Blob(e,{type:this.codecFileType}),i=URL.createObjectURL(t),n=document.createElement("a");document.body.appendChild(n),n.style.display="none",n.href=i;const o=new Date().getTime();n.download=`${o}.webm`,n.click(),window.URL.revokeObjectURL(i)}updateCanvas(e){this.isCanvas&&(this.canvasElement=e)}}class fe extends he{constructor(e){super(e),this.init(e)}init(e){if(document.addEventListener("DOMContentLoaded",le.apply(this,[e.debugShowVideo])),e!=null&&e.resolution)switch(e.resolution){case"4k":b.next({...b.value,width:3840,height:2160,audioBitsPerSecond:128e3,videoBitsPerSecond:12e6});break;case"2k":b.next({...b.value,width:2560,height:1440,audioBitsPerSecond:128e3,videoBitsPerSecond:65e5});break;case"1080p":b.next({...b.value,width:1920,height:1080,audioBitsPerSecond:128e3,videoBitsPerSecond:25e5});break}}}export{fe as V};