UNPKG

@bddh/starling-realtime-client

Version:

2 lines (1 loc) 5.93 kB
var DEFAULT_CANVAS_ID="starling-cutout-canvas",HUMAN_WRAPPER_ID="cloud-brtc-player-wrapper",RTC_VIDEO_ID="remotevideo500",LAST_FRAME_COVER_CANVAS_CLASS="canvas-item",LAST_FRAME_COVER_INNER_CANVAS_ID="starling-last-frame-cover-canvas";function createHumanDom(options){var _options$autoChromaKe=options.autoChromaKey,autoChromaKey=void 0!==_options$autoChromaKe&&_options$autoChromaKe,_options$autoplay=options.autoplay,autoplay=void 0===_options$autoplay||_options$autoplay;if(document.getElementById(RTC_VIDEO_ID))return console.error('DOM with id "'.concat(RTC_VIDEO_ID,'" already exists.')),null;var playerWrapper=document.createElement("div");playerWrapper.className=HUMAN_WRAPPER_ID,playerWrapper.id=HUMAN_WRAPPER_ID,playerWrapper.style.position="relative";var video=document.createElement("video");if(video.id=RTC_VIDEO_ID,video.width=0,video.height=0,video.setAttribute("width","100%"),video.setAttribute("height","100%"),video.setAttribute("data-playing","true"),video.setAttribute("playsinline",""),autoplay&&video.setAttribute("autoplay",""),playerWrapper.appendChild(video),autoChromaKey){var canvas=document.createElement("canvas");canvas.id="starling-cutout-canvas",Object.assign(canvas.style,{position:"absolute",top:"0",left:"0",height:"100%",width:"100%",zIndex:"1",display:autoChromaKey?"block":"none"}),video.style.visibility="hidden",playerWrapper.appendChild(canvas)}return{playerWrapper:playerWrapper,video:video}}function ensureCutoutDomMounted(){if(document.getElementById("starling-cutout-canvas"))return!0;var wrapper=document.getElementById(HUMAN_WRAPPER_ID),video=document.getElementById(RTC_VIDEO_ID);if(!wrapper||!video)return!1;var canvas=document.createElement("canvas");return canvas.id="starling-cutout-canvas",Object.assign(canvas.style,{position:"absolute",top:"0",left:"0",height:"100%",width:"100%",zIndex:"1",display:"block"}),video.style.visibility="hidden",wrapper.appendChild(canvas),!0}function appendLastFrameCoverSibling(mountEl){if(mountEl&&!mountEl.querySelector("[".concat("data-dh-last-frame-cover",'="1"]'))){var wrap=document.createElement("div");wrap.className="".concat("canvas-item"," "),wrap.setAttribute("data-dh-last-frame-cover","1");var canvas=document.createElement("canvas");canvas.id="starling-last-frame-cover-canvas",Object.assign(canvas.style,{position:"absolute",top:"0",left:"0",width:"100%",height:"100%",objectFit:"cover",display:"block",zIndex:"0"}),wrap.appendChild(canvas),mountEl.appendChild(wrap)}}function captureLastFrameToCover(){var cover=document.getElementById("starling-last-frame-cover-canvas");if(!cover)return!1;var ctx=cover.getContext("2d");if(!ctx)return!1;var chroma=document.getElementById("starling-cutout-canvas"),video=document.getElementById(RTC_VIDEO_ID);return chroma&&chroma.width>0&&chroma.height>0?(cover.width=chroma.width,cover.height=chroma.height,ctx.drawImage(chroma,0,0),!0):!!(video&&video.videoWidth>0&&video.videoHeight>0)&&(cover.width=video.videoWidth,cover.height=video.videoHeight,ctx.drawImage(video,0,0,video.videoWidth,video.videoHeight),!0)}function setHumanPlayerWrapperVisible(visible){var el=document.getElementById(HUMAN_WRAPPER_ID);el&&(el.style.visibility=visible?"visible":"hidden",el.style.pointerEvents=visible?"":"none")}function setLastFrameCoverVisible(visible){var inner=document.getElementById("starling-last-frame-cover-canvas"),wrap=null==inner?void 0:inner.parentNode;wrap&&(wrap.style.visibility=visible?"visible":"hidden")}function removeLastFrameCoverWrapper(){var wrap=document.querySelector("[".concat("data-dh-last-frame-cover",'="1"]'));null!=wrap&&wrap.parentNode&&wrap.parentNode.removeChild(wrap)}function removeHumanDom(){var humanDom=document.getElementById(HUMAN_WRAPPER_ID);humanDom&&humanDom.parentNode&&humanDom.parentNode.removeChild(humanDom)}function muteHumanVideo(muted){var humanVideoDom=document.getElementById(RTC_VIDEO_ID);return humanVideoDom&&(humanVideoDom.muted=muted),humanVideoDom}function playHumanVideo(play){var humanVideoDom=document.getElementById(RTC_VIDEO_ID);try{if(humanVideoDom)return play?humanVideoDom.play():humanVideoDom.pause()}catch(e){console.info(e)}}function checkIsHuawei(){var ua=navigator.userAgent||navigator.vendor;return/huawei/i.test(ua)||/honor/i.test(ua)}function appendRtcDomStyle(){var style=document.createElement("style");style.appendChild(document.createTextNode('\n [id^="videoremote1-"] {\n display: none !important;\n }\n \n video#remotevideo500 {\n width: auto !important;\n height: auto !important;\n max-width: 100% !important;\n max-height: 100% !important;\n width: 100% !important;\n height: 100% !important;\n object-fit: cover;\n }\n\n .cloud-brtc-player-wrapper {\n width: inherit !important;\n background-size: cover;\n height: inherit;\n display: flex;\n justify-content: center;\n }\n\n #starling-cutout-canvas {\n object-fit: cover;\n }\n\n [data-dh-last-frame-cover="1"] {\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n pointer-events: none;\n z-index: 1;\n visibility: hidden;\n }\n\n [data-dh-last-frame-cover="1"] canvas {\n width: 100%;\n height: 100%;\n object-fit: cover;\n display: block;\n }\n ')),document.head.appendChild(style)}export{DEFAULT_CANVAS_ID,HUMAN_WRAPPER_ID,LAST_FRAME_COVER_CANVAS_CLASS,LAST_FRAME_COVER_INNER_CANVAS_ID,RTC_VIDEO_ID,appendLastFrameCoverSibling,appendRtcDomStyle,captureLastFrameToCover,checkIsHuawei,createHumanDom,ensureCutoutDomMounted,muteHumanVideo,playHumanVideo,removeHumanDom,removeLastFrameCoverWrapper,setHumanPlayerWrapperVisible,setLastFrameCoverVisible};