var ue=Object.create;var V=Object.defineProperty,de=Object.defineProperties,me=Object.getOwnPropertyDescriptor,le=Object.getOwnPropertyDescriptors,ce=Object.getOwnPropertyNames,X=Object.getOwnPropertySymbols,he=Object.getPrototypeOf,W=Object.prototype.hasOwnProperty,pe=Object.prototype.propertyIsEnumerable;var H=(m,e,i)=>e in m?V(m,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):m[e]=i,I=(m,e)=>{for(var i in e||(e={}))W.call(e,i)&&H(m,i,e[i]);if(X)for(var i of X(e))pe.call(e,i)&&H(m,i,e[i]);return m},z=(m,e)=>de(m,le(e));var ge=(m,e)=>()=>(e||m((e={exports:{}}).exports,e),e.exports);var Se=(m,e,i,u)=>{if(e&&typeof e=="object"||typeof e=="function")for(let d of ce(e))!W.call(m,d)&&d!==i&&V(m,d,{get:()=>e[d],enumerable:!(u=me(e,d))||u.enumerable});return m};var j=(m,e,i)=>(i=m!=null?ue(he(m)):{},Se(e||!m||!m.__esModule?V(i,"default",{value:m,enumerable:!0}):i,m));var S=(m,e,i)=>(H(m,typeof e!="symbol"?e+"":e,i),i);var K=ge((Y,w)=>{(function(m){"use strict";function e(t,c){var r=(t&65535)+(c&65535),g=(t>>16)+(c>>16)+(r>>16);return g<<16|r&65535}function i(t,c){return t<>>32-c}function u(t,c,r,g,C,T){return e(i(e(e(c,t),e(g,T)),C),r)}function d(t,c,r,g,C,T,f){return u(c&r|~c&g,t,c,C,T,f)}function h(t,c,r,g,C,T,f){return u(c&g|r&~g,t,c,C,T,f)}function l(t,c,r,g,C,T,f){return u(c^r^g,t,c,C,T,f)}function p(t,c,r,g,C,T,f){return u(r^(c|~g),t,c,C,T,f)}function b(t,c){t[c>>5]|=128<>>9<<4)+14]=c;var r,g,C,T,f,o=1732584193,a=-271733879,s=-1732584194,n=271733878;for(r=0;r>5]>>>c%32&255);return r}function D(t){var c,r=[];for(r[(t.length>>2)-1]=void 0,c=0;c>5]|=(t.charCodeAt(c/8)&255)<16&&(g=b(g,t.length*8)),r=0;r<16;r+=1)C[r]=g[r]^909522486,T[r]=g[r]^1549556828;return f=b(C.concat(D(c)),512+c.length*8),_(b(T.concat(f),512+128))}function v(t){var c="0123456789abcdef",r="",g,C;for(C=0;C>>4&15)+c.charAt(g&15);return r}function y(t){return unescape(encodeURIComponent(t))}function O(t){return R(y(t))}function q(t){return v(O(t))}function J(t,c){return x(y(t),y(c))}function ne(t,c){return v(J(t,c))}function $(t,c,r){return c?r?J(c,t):ne(c,t):r?O(t):q(t)}typeof define=="function"&&define.amd?define(function(){return $}):typeof w=="object"&&w.exports?w.exports=$:m.md5=$})(Y)});var Z=j(K(),1);var M=(u=>(u.PublishMainStreamToCDN="publish-main-stream-to-cdn",u.PublishSubStreamToCDN="publish-sub-stream-to-cdn",u.PublishMixStreamToCDN="publish-mix-stream-to-cdn",u))(M||{}),F=(i=>(i.Main="main",i.Sub="sub",i))(F||{});var k={IT_AUDIO_VIDEO:0,IT_PICTURE:2,IT_CANVAS:3,IT_PURE_AUDIO:4,IT_PURE_VIDEO:5};var A=class{constructor(e,i){S(this,"_core");S(this,"_room");S(this,"_log");S(this,"_paramsForGivenCDN");S(this,"_publishGivenCDNData",null);S(this,"_isPublishingGivenCDN",!1);this._core=e,this._room=e.room,this._log=i,this._paramsForGivenCDN=new Map([["publish-main-stream-to-cdn",null],["publish-sub-stream-to-cdn",null],["publish-mix-stream-to-cdn",null]])}async startPublishGivenCDN(e){this._log.info(`[CDNStreaming] startPublishGivenCDN() ${JSON.stringify(e)}`),this._paramsForGivenCDN.set(e.publishMode,e);let i={pushRequestTime:Date.now(),pushAppId:e.appId,pushBizId:e.bizId,pushCdnUrl:e.url,pushStreamType:this.convertStreamType(e==null?void 0:e.publishMode),pushStreamId:e.streamId};this._publishGivenCDNData=i;try{this._isPublishingGivenCDN&&await this.stopPublishGivenCDN();let u=await this._room.sendStartPublishCDN(i,!1),{code:d,message:h}=u.data;if(d===0)this._log.info("[CDNStreaming] server success: start given cdn."),this._isPublishingGivenCDN=!0;else throw this.resetGivenCDN(),this._log.error(`[CDNStreaming] server failed: start given cdn errCode: ${d} errMsg: ${h} options: ${JSON.stringify(e)}`),new Error(`[CDNStreaming] server failed: start given cdn errCode: ${d} errMsg: ${h}`)}catch(u){throw this.resetGivenCDN(),u}}async stopPublishGivenCDN(){if(this._log.info("[CDNStreaming] stopPublishGivenCDN"),!this._isPublishingGivenCDN||!this._publishGivenCDNData){this.resetGivenCDN();return}let{pushAppId:e,pushBizId:i,pushCdnUrl:u,pushStreamType:d,pushStreamId:h}=this._publishGivenCDNData,l={pushRequestTime:Date.now(),pushAppId:e,pushBizId:i,pushCdnUrl:u,pushStreamType:d,pushStreamId:h},p=await this._room.sendStopPublishCDN(l,!1),{code:b,message:_}=p.data;if(b===0)this._log.info("[CDNStreaming] server success: stop given cdn."),this.resetGivenCDN();else throw this._log.error(`[CDNStreaming] server failed: stop given cdn errCode: ${b} errMsg: ${_} data: ${JSON.stringify(l)}`),new Error(`[CDNStreaming] server failed: stop given cdn errCode: ${b} errMsg: ${_}`)}resetGivenCDN(){this._publishGivenCDNData=null,this._isPublishingGivenCDN=!1}convertStreamType(e){return e==="publish-main-stream-to-cdn"?"main":"aux"}};var U=class{constructor(e,i){S(this,"_core");S(this,"_room");S(this,"_log");S(this,"_config",null);S(this,"_data",null);S(this,"_givenCDNManager");this._core=e,this._room=e.room,this._log=i,this.reset()}get isMixing(){return!!this._data}get isStarted(){return!!this._config}get hasCustomCDN(){var e,i,u;return((e=this._config)==null?void 0:e.target.appId)&&((i=this._config)==null?void 0:i.target.bizId)&&((u=this._config)==null?void 0:u.target.url)}async startMixTranscode(e){if(this._log.info(`startMixTranscode: ${JSON.stringify(e)}`),this._config=e,this.installEvents(),!!this._core.room.isJoined)try{let i=this.getInputParam(e),u=this.getOutputParam(e),d=this.getOutputSessionId({config:e,roomId:this._room.roomId,userId:this._room.userId});this.isMixing&&this._data&&d!==this._data.outputSessionId&&(this._log.info("[CDNStreaming] streamId changed, auto stop mixing before start"),await this.doStopMixTranscode()),await this.doStartMixTranscode({outputSessionId:d,inputParam:i,outputParam:u});let{appId:h,bizId:l,url:p,streamId:b=""}=this._config.target;h&&l&&p&&(this._givenCDNManager||(this._givenCDNManager=new A(this._core,this._log)),await this._givenCDNManager.startPublishGivenCDN({publishMode:e.target.publishMode,appId:h,bizId:l,url:p,streamId:b}))}catch(i){throw this.reset(),i}}async doStartMixTranscode({outputSessionId:e,inputParam:i,outputParam:u}){let d={roomId:String(this._room.roomId),mcuRequestTime:Date.now(),outputSessionId:e,inputParam:i,outputParam:u};this._log.info(`[CDNStreaming] doStartMixTranscode: ${JSON.stringify(d)}`);let h=await this._room.sendStartMixTranscode(d),{code:l}=h.data,{message:p}=h.data;if(l===0)this._log.info("[CDNStreaming] server success: start mix"),this._data=d;else throw l===-102083&&(p=`Please enable relayed-push in ${this._core.constants.CLOUD_CONSOLE_URL} and try later, refer to ${this._core.constants.DOC_URL}tutorial-26-advanced-publish-cdn-stream.html`),this._log.error(`[CDNStreaming] server failed: start mix errCode: ${l} errMsg: ${p}`),new Error(`[CDNStreaming] server failed: start mix errCode: ${l} errMsg: ${p}`)}async stopMixTranscode(){this._log.info("[CDNStreaming] stopMixTranscode");try{this.isStarted&&this.isMixing&&(await this.doStopMixTranscode(),this._config&&this.hasCustomCDN&&this._givenCDNManager&&await this._givenCDNManager.stopPublishGivenCDN())}catch(e){throw e}this.reset()}async doStopMixTranscode(){let e={mcuRequestTime:Date.now(),outputSessionId:this._data.outputSessionId,streamType:this._data.outputParam.streamType};this._log.info(`[CDNStreaming] doStopMixTranscode: ${JSON.stringify(e)}`);let i=await this._room.sendStopMixTranscode(e),{code:u,message:d}=i.data;if(u===0)this._log.info("[CDNStreaming] server success: stop mix"),this.reset();else throw this._log.error(`[CDNStreaming] server failed: start mix errCode: ${u} errMsg: ${d}`),new Error(`[CDNStreaming] server failed: start mix errCode: ${u} errMsg: ${d}`)}reset(){this._config=null,this._data=null,this.uninstallEvents()}installEvents(){this._core.room.on("joined",this.handleRoomJoined,this),this._core.room.on("left",this.handleRoomLeft,this)}uninstallEvents(){this._core.room.off("joined",this.handleRoomJoined,this),this._core.room.off("left",this.handleRoomLeft,this)}async handleRoomJoined(){this._log.info(`[CDNStreaming] handleJoined: ${JSON.stringify(this._config)}`),this.isStarted&&this._config&&await this.startMixTranscode(this._config)}async handleRoomLeft(){this._log.info(`[CDNStreaming] handleRoomLeft: ${JSON.stringify(this._config)}`),this._data=null}getOutputSessionId({config:e,userId:i,roomId:u}){return this._core.utils.isString(e.target.streamId)&&e.target.streamId.length>0?e.target.streamId:(0,Z.default)(`${u}_${i}_main`)}getStringRoomId(e,i){return e?String(e):i}getInputParam(e){let{mix:i={}}=e,{audioMixUserList:u=[],videoLayoutList:d=[]}=i,h=d.map(l=>({userId:l.fixedVideoUser.userId,roomId:this.getStringRoomId(l.fixedVideoUser.roomId,l.fixedVideoUser.strRoomId)||this._core.room.roomId,width:l.width||0,height:l.height||0,locationX:l.locationX||0,locationY:l.locationY||0,zOrder:l.zOrder||1,streamType:l.fixedVideoStreamType==="sub"?1:0,inputType:k.IT_PURE_VIDEO,renderMode:l.fillMode||0}));return u.forEach(l=>{let p=h.findIndex(b=>b.userId===l.userId&&b.room===this.getStringRoomId(l.roomId,l.strRoomId));p!==-1?h[p].inputType=k.IT_AUDIO_VIDEO:h.push({userId:l.userId,roomId:l.roomId||l.strRoomId,inputType:k.IT_PURE_AUDIO})}),h}getOutputParam(e){let i=e.target.streamId||"",{encoding:u={},mix:d={}}=e;return{streamId:i,streamType:i.length>0?1:0,width:this._core.utils.isUndefined(u.videoWidth)?640:u.videoWidth,height:this._core.utils.isUndefined(u.videoHeight)?480:u.videoHeight,videoBps:u.videoBitrate||0,fps:u.videoFramerate||15,gop:u.videoGOP||2,audioSampleRate:u.audioSampleRate||48e3,audioBps:u.audioBitrate||64,audioChannels:u.audioChannels||1,backgroundColor:d.backgroundColor||0,backgroundImg:d.backgroundImage||"",extraInfo:"",videoCodec:2,audioCodec:0}}};var G=j(K(),1);var E=class{constructor(e,i){S(this,"_room");S(this,"_core");S(this,"_log");S(this,"_paramsForTencentCDN");S(this,"_initParamsForTencentCDN",{isPublished:!1,isStarted:!1});this._core=e,this._room=e.room,this._log=i,this._paramsForTencentCDN=new Map([["publish-main-stream-to-cdn",this._initParamsForTencentCDN],["publish-sub-stream-to-cdn",this._initParamsForTencentCDN]])}installEvents(){this._core.innerEmitter.on("104",this.handlePublished,this),this._core.room.on("left",this.handleRoomLeft,this)}uninstallEvents(){this._core.innerEmitter.off("104",this.handlePublished,this),this._core.room.off("left",this.handleRoomLeft,this)}async handlePublished({track:e}){var d;this._log.info(`[CDNStreaming] handlePublished: mediaType ${e.mediaType}, roomID ${(d=e==null?void 0:e.room)==null?void 0:d.roomId}`);let i=e.mediaType===4?"publish-main-stream-to-cdn":"publish-sub-stream-to-cdn",u=this._paramsForTencentCDN.get(i)||null;u!=null&&u.target&&u.isStarted&&await this.startPublishTencentCDN(u.target)}async handleRoomLeft(){this._log.info("[CDNStreaming] handleRoomLeft"),this.changeDataStatus("publish-main-stream-to-cdn",{isPublished:!1}),this.changeDataStatus("publish-sub-stream-to-cdn",{isPublished:!1})}isStreamPublished(e){return e==="publish-main-stream-to-cdn"&&!this._room.isMainStreamPublished?(this._log.info("[CDNStreaming] Main stream has not already published, will auto reStart after published."),!1):e==="publish-sub-stream-to-cdn"&&!this._room.isAuxStreamPublished?(this._log.info("[CDNStreaming] Sub has not already published, will auto reStart after published."),!1):!0}changeDataStatus(e,i){let u=this._paramsForTencentCDN.get(e),d=I(I({},u),i);this._paramsForTencentCDN.set(e,d)}async startPublishTencentCDN(e){if(this._log.info(`[CDNStreaming] startPublishTencentCDN ${JSON.stringify(e)}`),this.installEvents(),this.changeDataStatus(e.publishMode,{target:e,isStarted:!0}),!this.isStreamPublished(e.publishMode))return;let i=e.streamId||"",u=this.generatePublishCDNStreamId(i,e.publishMode),d=this.generatePublishCDNSessionId(e.publishMode),h=e.publishMode==="publish-sub-stream-to-cdn"?1:0,l={requestTime:Date.now(),sessionId:d,streamId:u,streamType:h};await this.doStartPublishTencentCDN(l,e.publishMode);let{appId:p,bizId:b,url:_}=e;if(p&&b&&_){let D=this._paramsForTencentCDN.get(e.publishMode)||this._initParamsForTencentCDN;D!=null&&D.givenCDNManager||(D.givenCDNManager=new A(this._core,this._log),this._paramsForTencentCDN.set(e.publishMode,D)),await D.givenCDNManager.startPublishGivenCDN({publishMode:e.publishMode,appId:p,bizId:b,url:_,streamId:u})}}async doStartPublishTencentCDN(e,i){this._log.info(`[CDNStreaming] doStartPublishTencentCDN: ${JSON.stringify(e)}`);let u=6,d=500,h=0;for(;;){let l=await this._room.sendStartPublishCDN(e,!0),{code:p}=l.data,{message:b}=l.data;if(p===0){this._log.info("[CDNStreaming] server success: start tencent cdn"),this.changeDataStatus(i,{isPublished:!0});break}else if(p===-10006&&hsetTimeout(_,d));else throw this.changeDataStatus(i,{isPublished:!1}),p===-102083&&(b=`Please enable relayed-push in ${this._core.constants.CLOUD_CONSOLE_URL} and try later, refer to ${this._core.constants.DOC_URL}tutorial-26-advanced-publish-cdn-stream.html`),this._log.error(`[CDNStreaming] server failed: start tencent cdn errCode: ${p} errMsg: ${b}`),new Error(`[CDNStreaming] server failed: start tencent cdn errCode: ${p} errMsg: ${b}`)}}async stopPublishTencentCDN(e){this._log.info(`[CDNStreaming] doStartPublishTencentCDN: ${JSON.stringify(e)}`);let i=this._paramsForTencentCDN.get(e)||this._initParamsForTencentCDN;if(i.isPublished){let{target:u}=i;u!=null&&u.bizId&&u.appId&&u.url&&(i!=null&&i.givenCDNManager)&&await(i==null?void 0:i.givenCDNManager.stopPublishGivenCDN()),await this.doStopPublishTencentCDN(e)}this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN)}async doStopPublishTencentCDN(e){let i={requestTime:Date.now(),sessionId:(0,G.default)(`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(e)}`)};this._log.info(`[CDNStreaming] doStopPublishTencentCDN: ${JSON.stringify(i)}`);let u=await this._room.sendStopPublishCDN(i,!0),{code:d}=u.data,{message:h}=u.data;if(d===0)this._log.info("[CDNStreaming] server success: stop tencent cdn"),this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN),this.reset(e);else throw d===-102069&&(this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN),h=`can not stop in auto relayed-push mode ${h}`),this._log.error(`[CDNStreaming] server failed: stop tencent cdn errCode: ${d} errMsg: ${h}`),new Error(`[CDNStreaming] server failed: stop tencent cdn errCode: ${d} errMsg: ${h}`)}reset(e){this.uninstallEvents(),this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN)}generatePublishCDNStreamId(e,i){if(e===""){let u=`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(i)}`;return/^[A-Za-z\d_-]*$/.test(u)||(u=(0,G.default)(u)),`${this._room.sdkAppId}_${u}`}return e}convertPublishModeToStreamType(e){return e==="publish-main-stream-to-cdn"?"main":"aux"}generatePublishCDNSessionId(e){return(0,G.default)(`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(e)}`)}};var Q=m=>typeof m=="string";function P(m){throw new Error(`[CDNStreaming] options error: ${m}`)}function Ce(m,e,i,u){/^[A-Za-z\d_-]*$/.test(m)||P("Invalid parameter streamId.")}function Te(m,e,i,u){if(Q(m))P("roomId must be an integer but go a string, use 'parseInt' to convert it or use 'strRoomId' instead.");else{if(m===void 0)return;/^[1-9]\d*$/.test(String(m))&&m<4294967295||P("roomId must be an integer between [1, 4294967294].")}}function De(m,e,i,u){/^[A-Za-z\d\s!#$%&()+\-:;<=.>?@[\]^_{}|~,]{1,64}$/.test(m)||P("strRoomId must be a valid string.")}var N={type:"number",notLessThanZero:!0},ee={type:"object",required:!0,properties:{userId:{required:!0,type:"string"},roomId:{type:["string","number"],validate:Te},strRoomId:{type:"string",validate:De}}},te={required:!0,properties:{publishMode:{required:!0,values:["publish-main-stream-to-cdn","publish-mix-stream-to-cdn","publish-sub-stream-to-cdn"]},streamId:{required:!1,type:"string",validate:Ce},appId:{type:"number",allowEmpty:!1},bizId:{type:"number",allowEmpty:!1},url:{type:"string",allowEmpty:!1}}},re={required:!1,type:"object",properties:{videoWidth:N,videoHeight:N,videoBitrate:z(I({},N),{allowEmpty:!1}),videoFramerate:{type:"number",validate(m,e,i,u){(m<=0||m>30)&&P("Invalid parameter mixConfig -> videoFramerate, the value must be between (0, 30].")}},videoGOP:{type:"number",validate(m,e,i,u){(m<1||m>8)&&P("Invalid parameter mixConfig -> videoGOP, the value must be between [1, 8].")}},audioSampleRate:N,audioBitrate:{type:"number",validate(m,e,i,u){(m<32||m>192)&&P("Invalid parameter mixConfig -> audioBitrate, the value must be between [32, 192].")}},audioChannels:{type:"number",values:[1,2]}}},ie={required:!1,type:"object",properties:{backgroundColor:{type:"number"},backgroundImage:{type:"string"},audioMixUserList:{type:"array",arrayItem:I({},ee)},videoLayoutList:{type:"array",required:!0,arrayItem:{type:"object",properties:{fixedVideoUser:I({},ee),fixedVideoStreamType:{type:"string",required:!0,values:["main","sub"]},fillMode:{type:"number",values:[0,1,2,4]},zOrder:{type:"number",required:!0,validate(m,e,i,u){(m<1||m>15)&&P("Invalid parameter mix -> videoLayoutList -> zOrder, the value must be between [1, 15].")}},width:N,height:N,locationX:N,locationY:N}}}}};function oe(m){return{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:I({},te),encoding:I({},re),mix:I({},ie)},validate(e,i,u,d){var b,_;let{publishMode:h}=e.target,{encoding:l,mix:p}=e;if(h==="publish-mix-stream-to-cdn"&&((!l||!p)&&P("Invalid parameter mix & encoding, the value must be not empty."),p&&p.videoLayoutList)){let D=0,R=0,x=[];p.videoLayoutList.forEach((O,q)=>{x.push(O.fixedVideoUser.userId),O.width+O.locationX>D&&(D=O.width+O.locationX),O.height+O.locationY>R&&(R=O.height+O.locationY)}),x.indexOf(m.room.userId)<0&&P("Invalid parameter mix -> videoLayoutList, the value must be include self.");let v=(b=l==null?void 0:l.videoWidth)!=null?b:640,y=(_=l==null?void 0:l.videoHeight)!=null?_:480;(v