trtc-sdk-v5 5.8.3-beta.4 → 5.8.3-ktv.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. package/index.d.ts +3 -2
  2. package/package.json +1 -1
  3. package/plugins/cdn-streaming/cdn-streaming.esm.d.ts +6 -3
  4. package/plugins/cdn-streaming/cdn-streaming.esm.js +1 -1
  5. package/plugins/cdn-streaming/cdn-streaming.iife.js +1 -1
  6. package/plugins/cdn-streaming/package.json +1 -1
  7. package/plugins/chorus/chorus.esm.d.ts +13 -0
  8. package/plugins/chorus/chorus.esm.js +22 -0
  9. package/plugins/chorus/chorus.iife.js +22 -0
  10. package/plugins/chorus/package.json +30 -0
  11. package/plugins/cross-room/cross-room.esm.js +1 -1
  12. package/plugins/cross-room/cross-room.iife.js +1 -1
  13. package/plugins/cross-room/package.json +1 -1
  14. package/plugins/debug/debug.esm.js +16 -16
  15. package/plugins/debug/debug.iife.js +18 -18
  16. package/plugins/debug/package.json +1 -1
  17. package/plugins/device-detector/device-detector.esm.js +12 -12
  18. package/plugins/device-detector/device-detector.iife.js +12 -12
  19. package/plugins/device-detector/package.json +1 -1
  20. package/plugins/video-effect/basic-beauty/basic-beauty.esm.js +2 -2
  21. package/plugins/video-effect/basic-beauty/basic-beauty.iife.js +2 -2
  22. package/plugins/video-effect/basic-beauty/package.json +1 -1
  23. package/plugins/video-effect/beauty/package.json +1 -1
  24. package/plugins/video-effect/virtual-background/package.json +1 -1
  25. package/plugins/video-effect/virtual-background/virtual-background.esm.js +18 -18
  26. package/plugins/video-effect/virtual-background/virtual-background.iife.js +18 -18
  27. package/plugins/video-effect/watermark/package.json +1 -1
  28. package/plugins/video-effect/watermark/watermark.esm.js +1 -1
  29. package/plugins/video-effect/watermark/watermark.iife.js +1 -1
  30. package/trtc.esm.js +28 -28
  31. package/trtc.js +1 -1
package/index.d.ts CHANGED
@@ -5,10 +5,11 @@ import { Watermark, WatermarkOptions } from './plugins/video-effect/watermark';
5
5
  import { Beauty, BeautyOptions, UpdateBeautyOptions } from './plugins/video-effect/beauty';
6
6
  import { BasicBeauty, BasicBeautyOptions } from './plugins/video-effect/basic-beauty';
7
7
  import { CrossRoom, StartCrossRoomOption, UpdateCrossRoomOption, StopCrossRoomOption } from './plugins/cross-room';
8
- import { Debug, DebugOptions } from './plugins/debug';
8
+ import { Debug, DebugOptions } from './plugins/debug'
9
+ import { Chorus } from './plugins/chorus';
9
10
 
10
11
  export { CDNStreamingOptions, DeviceDetectorOptions, VirtualBackgroundOptions, UpdateVirtualBackgroundOptions, WatermarkOptions, BeautyOptions, UpdateBeautyOptions, BasicBeautyOptions, StartCrossRoomOption, UpdateCrossRoomOption, StopCrossRoomOption, DebugOptions };
11
- type TRTCPlugin = typeof CrossRoom | typeof CDNStreaming | typeof DeviceDetector | typeof VirtualBackground | typeof Watermark | typeof Beauty | typeof BasicBeauty | typeof Debug;
12
+ type TRTCPlugin = typeof CrossRoom | typeof CDNStreaming | typeof DeviceDetector | typeof VirtualBackground | typeof Watermark | typeof Beauty | typeof BasicBeauty | typeof Debug | typeof Chorus;
12
13
 
13
14
  export declare type PluginStartOptionsMap = {
14
15
  'AudioMixer': AudioMixerOptions;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "trtc-sdk-v5",
3
- "version": "5.8.3-beta.4",
3
+ "version": "5.8.3-ktv.1",
4
4
  "description": "Tencent Cloud RTC SDK for Web",
5
5
  "main": "trtc.js",
6
6
  "types": "index.d.ts",
@@ -10,6 +10,7 @@ export declare interface Target {
10
10
  appId?: number;
11
11
  bizId?: number;
12
12
  url?: string;
13
+ robotUser?: User;
13
14
  }
14
15
 
15
16
  export declare interface Encoding {
@@ -53,9 +54,10 @@ export declare enum TRTCStreamType {
53
54
  }
54
55
 
55
56
  export declare enum PublishMode {
56
- PublishMainStreamToCDN = 'PublishMainStreamToCDN',
57
- PublishSubStreamToCDN = 'PublishSubStreamToCDN',
58
- PublishMixStreamToCDN = 'PublishMixStreamToCDN'
57
+ PublishMainStreamToCDN = 'publish-main-stream-to-cdn',
58
+ PublishSubStreamToCDN = 'publish-sub-stream-to-cdn',
59
+ PublishMixStreamToCDN = 'publish-mix-stream-to-cdn',
60
+ PublishMixStreamToRoom = 'publish-mix-stream-to-room'
59
61
  }
60
62
 
61
63
  declare class CDNStreaming {
@@ -64,6 +66,7 @@ declare class CDNStreaming {
64
66
  PublishMainStreamToCDN: PublishMode.PublishMainStreamToCDN;
65
67
  PublishSubStreamToCDN: PublishMode.PublishSubStreamToCDN;
66
68
  PublishMixStreamToCDN: PublishMode.PublishMixStreamToCDN;
69
+ PublishMixStreamToRoom: PublishMode.PublishMixStreamToRoom;
67
70
  };
68
71
  };
69
72
 
@@ -1 +1 @@
1
- var ue=Object.create;var q=Object.defineProperty,me=Object.defineProperties,le=Object.getOwnPropertyDescriptor,ce=Object.getOwnPropertyDescriptors,pe=Object.getOwnPropertyNames,z=Object.getOwnPropertySymbols,he=Object.getPrototypeOf,j=Object.prototype.hasOwnProperty,ge=Object.prototype.propertyIsEnumerable;var K=(l,e,i)=>e in l?q(l,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):l[e]=i,f=(l,e)=>{for(var i in e||(e={}))j.call(e,i)&&K(l,i,e[i]);if(z)for(var i of z(e))ge.call(e,i)&&K(l,i,e[i]);return l},Y=(l,e)=>me(l,ce(e));var Se=(l,e)=>()=>(e||l((e={exports:{}}).exports,e),e.exports);var be=(l,e,i,d)=>{if(e&&typeof e=="object"||typeof e=="function")for(let u of pe(e))!j.call(l,u)&&u!==i&&q(l,u,{get:()=>e[u],enumerable:!(d=le(e,u))||d.enumerable});return l};var Z=(l,e,i)=>(i=l!=null?ue(he(l)):{},be(e||!l||!l.__esModule?q(i,"default",{value:l,enumerable:!0}):i,l));var S=(l,e,i)=>(K(l,typeof e!="symbol"?e+"":e,i),i);var J=Se((Q,V)=>{(function(l){"use strict";function e(t,p){var r=(t&65535)+(p&65535),g=(t>>16)+(p>>16)+(r>>16);return g<<16|r&65535}function i(t,p){return t<<p|t>>>32-p}function d(t,p,r,g,C,D){return e(i(e(e(p,t),e(g,D)),C),r)}function u(t,p,r,g,C,D,I){return d(p&r|~p&g,t,p,C,D,I)}function c(t,p,r,g,C,D,I){return d(p&g|r&~g,t,p,C,D,I)}function m(t,p,r,g,C,D,I){return d(p^r^g,t,p,C,D,I)}function h(t,p,r,g,C,D,I){return d(r^(p|~g),t,p,C,D,I)}function b(t,p){t[p>>5]|=128<<p%32,t[(p+64>>>9<<4)+14]=p;var r,g,C,D,I,a=1732584193,o=-271733879,s=-1732584194,n=271733878;for(r=0;r<t.length;r+=16)g=a,C=o,D=s,I=n,a=u(a,o,s,n,t[r],7,-680876936),n=u(n,a,o,s,t[r+1],12,-389564586),s=u(s,n,a,o,t[r+2],17,606105819),o=u(o,s,n,a,t[r+3],22,-1044525330),a=u(a,o,s,n,t[r+4],7,-176418897),n=u(n,a,o,s,t[r+5],12,1200080426),s=u(s,n,a,o,t[r+6],17,-1473231341),o=u(o,s,n,a,t[r+7],22,-45705983),a=u(a,o,s,n,t[r+8],7,1770035416),n=u(n,a,o,s,t[r+9],12,-1958414417),s=u(s,n,a,o,t[r+10],17,-42063),o=u(o,s,n,a,t[r+11],22,-1990404162),a=u(a,o,s,n,t[r+12],7,1804603682),n=u(n,a,o,s,t[r+13],12,-40341101),s=u(s,n,a,o,t[r+14],17,-1502002290),o=u(o,s,n,a,t[r+15],22,1236535329),a=c(a,o,s,n,t[r+1],5,-165796510),n=c(n,a,o,s,t[r+6],9,-1069501632),s=c(s,n,a,o,t[r+11],14,643717713),o=c(o,s,n,a,t[r],20,-373897302),a=c(a,o,s,n,t[r+5],5,-701558691),n=c(n,a,o,s,t[r+10],9,38016083),s=c(s,n,a,o,t[r+15],14,-660478335),o=c(o,s,n,a,t[r+4],20,-405537848),a=c(a,o,s,n,t[r+9],5,568446438),n=c(n,a,o,s,t[r+14],9,-1019803690),s=c(s,n,a,o,t[r+3],14,-187363961),o=c(o,s,n,a,t[r+8],20,1163531501),a=c(a,o,s,n,t[r+13],5,-1444681467),n=c(n,a,o,s,t[r+2],9,-51403784),s=c(s,n,a,o,t[r+7],14,1735328473),o=c(o,s,n,a,t[r+12],20,-1926607734),a=m(a,o,s,n,t[r+5],4,-378558),n=m(n,a,o,s,t[r+8],11,-2022574463),s=m(s,n,a,o,t[r+11],16,1839030562),o=m(o,s,n,a,t[r+14],23,-35309556),a=m(a,o,s,n,t[r+1],4,-1530992060),n=m(n,a,o,s,t[r+4],11,1272893353),s=m(s,n,a,o,t[r+7],16,-155497632),o=m(o,s,n,a,t[r+10],23,-1094730640),a=m(a,o,s,n,t[r+13],4,681279174),n=m(n,a,o,s,t[r],11,-358537222),s=m(s,n,a,o,t[r+3],16,-722521979),o=m(o,s,n,a,t[r+6],23,76029189),a=m(a,o,s,n,t[r+9],4,-640364487),n=m(n,a,o,s,t[r+12],11,-421815835),s=m(s,n,a,o,t[r+15],16,530742520),o=m(o,s,n,a,t[r+2],23,-995338651),a=h(a,o,s,n,t[r],6,-198630844),n=h(n,a,o,s,t[r+7],10,1126891415),s=h(s,n,a,o,t[r+14],15,-1416354905),o=h(o,s,n,a,t[r+5],21,-57434055),a=h(a,o,s,n,t[r+12],6,1700485571),n=h(n,a,o,s,t[r+3],10,-1894986606),s=h(s,n,a,o,t[r+10],15,-1051523),o=h(o,s,n,a,t[r+1],21,-2054922799),a=h(a,o,s,n,t[r+8],6,1873313359),n=h(n,a,o,s,t[r+15],10,-30611744),s=h(s,n,a,o,t[r+6],15,-1560198380),o=h(o,s,n,a,t[r+13],21,1309151649),a=h(a,o,s,n,t[r+4],6,-145523070),n=h(n,a,o,s,t[r+11],10,-1120210379),s=h(s,n,a,o,t[r+2],15,718787259),o=h(o,s,n,a,t[r+9],21,-343485551),a=e(a,g),o=e(o,C),s=e(s,D),n=e(n,I);return[a,o,s,n]}function _(t){var p,r="",g=t.length*32;for(p=0;p<g;p+=8)r+=String.fromCharCode(t[p>>5]>>>p%32&255);return r}function T(t){var p,r=[];for(r[(t.length>>2)-1]=void 0,p=0;p<r.length;p+=1)r[p]=0;var g=t.length*8;for(p=0;p<g;p+=8)r[p>>5]|=(t.charCodeAt(p/8)&255)<<p%32;return r}function E(t){return _(b(T(t),t.length*8))}function k(t,p){var r,g=T(t),C=[],D=[],I;for(C[15]=D[15]=void 0,g.length>16&&(g=b(g,t.length*8)),r=0;r<16;r+=1)C[r]=g[r]^909522486,D[r]=g[r]^1549556828;return I=b(C.concat(T(p)),512+p.length*8),_(b(D.concat(I),512+128))}function y(t){var p="0123456789abcdef",r="",g,C;for(C=0;C<t.length;C+=1)g=t.charCodeAt(C),r+=p.charAt(g>>>4&15)+p.charAt(g&15);return r}function N(t){return unescape(encodeURIComponent(t))}function M(t){return E(N(t))}function F(t){return y(M(t))}function G(t,p){return k(N(t),N(p))}function A(t,p){return y(G(t,p))}function B(t,p,r){return p?r?G(p,t):A(p,t):r?M(t):F(t)}typeof define=="function"&&define.amd?define(function(){return B}):typeof V=="object"&&V.exports?V.exports=B:l.md5=B})(Q)});var ee=Z(J(),1);var U=(d=>(d.PublishMainStreamToCDN="publish-main-stream-to-cdn",d.PublishSubStreamToCDN="publish-sub-stream-to-cdn",d.PublishMixStreamToCDN="publish-mix-stream-to-cdn",d))(U||{}),W=(i=>(i.Main="main",i.Sub="sub",i))(W||{});var H={IT_AUDIO_VIDEO:0,IT_PICTURE:2,IT_CANVAS:3,IT_PURE_AUDIO:4,IT_PURE_VIDEO:5};var R=class{constructor(e,i){S(this,"_core");S(this,"_room");S(this,"_log");S(this,"_params");S(this,"_publishGivenCDNData",null);this._core=e,this._room=e.room,this._log=i}get isPublishingGivenCDN(){return!!this._params}async startPublishGivenCDN(e){if(this._log.info(`[CDNStreaming] startPublishGivenCDN() current: ${JSON.stringify(this._params)}, params: ${JSON.stringify(e)}`),this.isPublishingGivenCDN){let{appId:i,bizId:d,url:u}=this._params||{};if(i===e.appId&&d===e.bizId&&u===e.url)return;await this.stopPublishGivenCDN()}this._params=e,this._publishGivenCDNData={pushRequestTime:Date.now(),pushAppId:e.appId,pushBizId:e.bizId,pushCdnUrl:e.url,pushStreamType:this.convertStreamType(e==null?void 0:e.publishMode),pushStreamId:e.streamId};try{let i=await this._room.sendStartPublishCDN(this._publishGivenCDNData,!1),{code:d,message:u}=i.data;if(d===0)this._log.info("[CDNStreaming] server success: start given cdn.");else throw this.resetGivenCDN(),this._log.error(`[CDNStreaming] server failed: start given cdn errCode: ${d} errMsg: ${u} options: ${JSON.stringify(e)}`),new Error(`[CDNStreaming] server failed: start given cdn errCode: ${d} errMsg: ${u}`)}catch(i){throw this.resetGivenCDN(),i}}async stopPublishGivenCDN(){if(this._log.info("[CDNStreaming] stopPublishGivenCDN"),!this.isPublishingGivenCDN||!this._publishGivenCDNData){this.resetGivenCDN();return}let{pushAppId:e,pushBizId:i,pushCdnUrl:d,pushStreamType:u,pushStreamId:c}=this._publishGivenCDNData,m={pushRequestTime:Date.now(),pushAppId:e,pushBizId:i,pushCdnUrl:d,pushStreamType:u,pushStreamId:c},h=await this._room.sendStopPublishCDN(m,!1),{code:b,message:_}=h.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(m)}`),new Error(`[CDNStreaming] server failed: stop given cdn errCode: ${b} errMsg: ${_}`)}resetGivenCDN(){this._publishGivenCDNData=null,this._params=void 0}convertStreamType(e){return e==="publish-sub-stream-to-cdn"?"aux":"main"}};var L=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,d;return((e=this._config)==null?void 0:e.target.appId)&&((i=this._config)==null?void 0:i.target.bizId)&&((d=this._config)==null?void 0:d.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),d=this.getOutputParam(e),u=this.getOutputSessionId({config:e,roomId:this._room.roomId,userId:this._room.userId});this.isMixing&&this._data&&u!==this._data.outputSessionId&&(this._log.info("[CDNStreaming] streamId changed, auto stop mixing before start"),await this.doStopMixTranscode()),await this.doStartMixTranscode({outputSessionId:u,inputParam:i,outputParam:d});let{appId:c,bizId:m,url:h,streamId:b=""}=e.target;c&&m&&h&&(this._givenCDNManager||(this._givenCDNManager=new R(this._core,this._log)),await this._givenCDNManager.startPublishGivenCDN({publishMode:e.target.publishMode,appId:c,bizId:m,url:h,streamId:b}))}catch(i){throw this.reset(),i}}async doStartMixTranscode({outputSessionId:e,inputParam:i,outputParam:d}){let u={roomId:String(this._room.roomId),mcuRequestTime:Date.now(),outputSessionId:e,inputParam:i,outputParam:d};this._log.info(`[CDNStreaming] doStartMixTranscode: ${JSON.stringify(u)}`);let c=await this._room.sendStartMixTranscode(u),{code:m}=c.data,{message:h}=c.data;if(m===0)this._log.info("[CDNStreaming] server success: start mix"),this._data=u;else throw m===-102083&&(h=`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: ${m} errMsg: ${h}`),new Error(`[CDNStreaming] server failed: start mix errCode: ${m} errMsg: ${h}`)}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:d,message:u}=i.data;if(d===0)this._log.info("[CDNStreaming] server success: stop mix"),this.reset();else throw this._log.error(`[CDNStreaming] server failed: stop mix errCode: ${d} errMsg: ${u}`),new Error(`[CDNStreaming] server failed: stop mix errCode: ${d} errMsg: ${u}`)}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:d}){return this._core.utils.isString(e.target.streamId)&&e.target.streamId.length>0?e.target.streamId:(0,ee.default)(`${d}_${i}_main`)}getStringRoomId(e,i){return e?String(e):i}getInputParam(e){let{mix:i={}}=e,{audioMixUserList:d=[],videoLayoutList:u=[]}=i,c=u.map(m=>({userId:m.fixedVideoUser.userId,roomId:this.getStringRoomId(m.fixedVideoUser.roomId,m.fixedVideoUser.strRoomId)||this._core.room.roomId,width:m.width||0,height:m.height||0,locationX:m.locationX||0,locationY:m.locationY||0,zOrder:m.zOrder||1,streamType:m.fixedVideoStreamType==="sub"?1:0,inputType:H.IT_PURE_VIDEO,renderMode:m.fillMode||0}));return d.forEach(m=>{let h=c.findIndex(b=>b.userId===m.userId&&b.roomId===this.getStringRoomId(m.roomId,m.strRoomId));h!==-1?c[h].inputType=H.IT_AUDIO_VIDEO:c.push({userId:m.userId,roomId:m.roomId||m.strRoomId||this._core.room.roomId,inputType:H.IT_PURE_AUDIO})}),c}getOutputParam(e){let i=e.target.streamId||"",{encoding:d={},mix:u={}}=e;return{streamId:i,streamType:i.length>0?1:0,width:this._core.utils.isUndefined(d.videoWidth)?640:d.videoWidth,height:this._core.utils.isUndefined(d.videoHeight)?480:d.videoHeight,videoBps:d.videoBitrate||0,fps:d.videoFramerate||15,gop:d.videoGOP||2,audioSampleRate:d.audioSampleRate||48e3,audioBps:d.audioBitrate||64,audioChannels:d.audioChannels||1,backgroundColor:u.backgroundColor||0,backgroundImg:u.backgroundImage||"",extraInfo:"",videoCodec:2,audioCodec:0}}};var $=Z(J(),1);var x=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 u;if(e.room!==this._room||(this._log.info(`[CDNStreaming] handlePublished: mediaType ${e.mediaType}, roomID ${(u=e==null?void 0:e.room)==null?void 0:u.roomId}`),e.mediaType===1))return;let i=e.mediaType===4?"publish-main-stream-to-cdn":"publish-sub-stream-to-cdn",d=this._paramsForTencentCDN.get(i)||null;d!=null&&d.target&&d.isStarted&&await this.startPublishTencentCDN(d.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 d=this._paramsForTencentCDN.get(e),u=f(f({},d),i);this._paramsForTencentCDN.set(e,u)}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||"",d=this.generatePublishCDNStreamId(i,e.publishMode),u=this.generatePublishCDNSessionId(e.publishMode),c=e.publishMode==="publish-sub-stream-to-cdn"?1:0,m={requestTime:Date.now(),sessionId:u,streamId:d,streamType:c};await this.doStartPublishTencentCDN(m,e.publishMode);let{appId:h,bizId:b,url:_}=e;if(h&&b&&_){let T=this._paramsForTencentCDN.get(e.publishMode)||this._initParamsForTencentCDN;T!=null&&T.givenCDNManager||(T.givenCDNManager=new R(this._core,this._log),this._paramsForTencentCDN.set(e.publishMode,T)),await T.givenCDNManager.startPublishGivenCDN({publishMode:e.publishMode,appId:h,bizId:b,url:_,streamId:d})}}async doStartPublishTencentCDN(e,i){this._log.info(`[CDNStreaming] doStartPublishTencentCDN: ${JSON.stringify(e)}`);let d=6,u=500,c=0;for(;;){let m=await this._room.sendStartPublishCDN(e,!0),{code:h}=m.data,{message:b}=m.data;if(h===0){this._log.info("[CDNStreaming] server success: start tencent cdn"),this.changeDataStatus(i,{isPublished:!0});break}else if(h===-10006&&c<d)this._log.warn(`[CDNStreaming] doStartPublishTencentCDN: retry...${c+1}/6, reason: ${b}`),c+=1,await new Promise(_=>setTimeout(_,u));else throw this.changeDataStatus(i,{isPublished:!1}),h===-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: ${h} errMsg: ${b}`),new Error(`[CDNStreaming] server failed: start tencent cdn errCode: ${h} 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:d}=i;d!=null&&d.bizId&&d.appId&&d.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,$.default)(`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(e)}`)};this._log.info(`[CDNStreaming] doStopPublishTencentCDN: ${JSON.stringify(i)}`);let d=await this._room.sendStopPublishCDN(i,!0),{code:u}=d.data,{message:c}=d.data;if(u===0)this._log.info("[CDNStreaming] server success: stop tencent cdn"),this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN),this.reset(e);else throw u===-102069&&(this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN),c=`can not stop in auto relayed-push mode ${c}`),this._log.error(`[CDNStreaming] server failed: stop tencent cdn errCode: ${u} errMsg: ${c}`),new Error(`[CDNStreaming] server failed: stop tencent cdn errCode: ${u} errMsg: ${c}`)}reset(e){this.uninstallEvents(),this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN)}generatePublishCDNStreamId(e,i){if(e===""){let d=`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(i)}`;return/^[A-Za-z\d_-]*$/.test(d)||(d=(0,$.default)(d)),`${this._room.sdkAppId}_${d}`}return e}convertPublishModeToStreamType(e){return e==="publish-main-stream-to-cdn"?"main":"aux"}generatePublishCDNSessionId(e){return(0,$.default)(`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(e)}`)}};var te=l=>typeof l=="string";var P;function De(l,e,i,d){if(!/^[A-Za-z\d_-]*$/.test(l)){let{RtcError:c,ErrorCode:m,ErrorCodeDictionary:h}=P;throw new c({code:m.INVALID_PARAMETER,extraCode:h.INVALID_STREAM_ID,messageParams:{key:"streamId"}})}}function Te(l,e,i,d){let{RtcError:u,ErrorCode:c,ErrorCodeDictionary:m}=P;if(te(l))throw new u({code:c.INVALID_PARAMETER,extraCode:m.INVALID_ROOM_ID_INTEGER_STRING,fnName:i,messageParams:{key:"roomId"}});if(l===void 0)return;if(!(/^[1-9]\d*$/.test(String(l))&&l<4294967295))throw new u({code:c.INVALID_PARAMETER,extraCode:m.INVALID_ROOM_ID_INTEGER,fnName:i,messageParams:{key:"roomId"}})}function _e(l,e,i,d){let{RtcError:u,ErrorCode:c,ErrorCodeDictionary:m}=P;if(!/^[A-Za-z\d\s!#$%&()+\-:;<=.>?@[\]^_{}|~,]{1,64}$/.test(l))throw new u({code:c.INVALID_PARAMETER,extraCode:m.INVALID_ROOM_ID_STRING,fnName:i,messageParams:{key:"strRoomId"}})}var O={type:"number",notLessThanZero:!0},re={type:"object",required:!0,properties:{userId:{required:!0,type:"string"},roomId:{type:["string","number"],validate:Te},strRoomId:{type:"string",validate:_e}}},ie={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:De},appId:{type:"number",allowEmpty:!1},bizId:{type:"number",allowEmpty:!1},url:{type:"string",allowEmpty:!1}}},ae={required:!1,type:"object",properties:{videoWidth:O,videoHeight:O,videoBitrate:Y(f({},O),{allowEmpty:!1}),videoFramerate:{type:"number",validate(l,e,i,d){if(l<=0||l>30){let{RtcError:u,ErrorCode:c}=P;throw new u({code:c.INVALID_PARAMETER,message:"Invalid parameter mixConfig -> videoFramerate, the value must be between (0, 30]."})}}},videoGOP:{type:"number",validate(l,e,i,d){if(l<1||l>8){let{RtcError:u,ErrorCode:c}=P;throw new u({code:c.INVALID_PARAMETER,message:"Invalid parameter mixConfig -> videoGOP, the value must be between [1, 8]."})}}},audioSampleRate:O,audioBitrate:{type:"number",validate(l,e,i,d){if(l<32||l>192){let{RtcError:u,ErrorCode:c}=P;throw new u({code:c.INVALID_PARAMETER,message:"Invalid parameter mixConfig -> audioBitrate, the value must be between [32, 192]."})}}},audioChannels:{type:"number",values:[1,2]}}},oe={required:!1,type:"object",properties:{backgroundColor:{type:"number"},backgroundImage:{type:"string"},audioMixUserList:{type:"array",arrayItem:f({},re)},videoLayoutList:{type:"array",required:!0,arrayItem:{type:"object",properties:{fixedVideoUser:f({},re),fixedVideoStreamType:{type:"string",required:!0,values:["main","sub"]},fillMode:{type:"number",values:[0,1,2,4]},zOrder:{type:"number",required:!0,validate(l,e,i,d){if(l<1||l>15){let{RtcError:u,ErrorCode:c}=P;throw new u({code:c.INVALID_PARAMETER,message:"Invalid parameter mix -> videoLayoutList -> zOrder, the value must be between [1, 15]."})}}},width:O,height:O,locationX:O,locationY:O}}}}};function se(l){return P=l.errorModule,{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:f({},ie),encoding:f({},ae),mix:f({},oe)},validate(e,i,d,u){var b,_;let{publishMode:c}=e.target,{encoding:m,mix:h}=e;if(c==="publish-mix-stream-to-cdn"){let{RtcError:T,ErrorCode:E,ErrorCodeDictionary:k}=l.errorModule;if(!m)throw new T({code:E.INVALID_PARAMETER,extraCode:k.INVALID_PARAMETER_REQUIRED,fnName:d,messageParams:{key:"encoding"}});if(!h)throw new T({code:E.INVALID_PARAMETER,extraCode:k.INVALID_PARAMETER_REQUIRED,fnName:d,messageParams:{key:"mix"}});if(h&&h.videoLayoutList){let y=0,N=0,M=[];if(h.videoLayoutList.forEach((A,B)=>{M.push(A.fixedVideoUser.userId),A.width+A.locationX>y&&(y=A.width+A.locationX),A.height+A.locationY>N&&(N=A.height+A.locationY)}),M.indexOf(l.room.userId)<0)throw new T({code:E.INVALID_PARAMETER,message:"Invalid parameter mix -> videoLayoutList, the value must be include self."});let F=(b=m==null?void 0:m.videoWidth)!=null?b:640,G=(_=m==null?void 0:m.videoHeight)!=null?_:480;if(F<y||G<N)throw new T({code:E.INVALID_PARAMETER,message:"Invalid parameter encoding, the width and height of the mixed video must encompass all the mixed-in video streams."})}}}}}function ne(l){return P=l.errorModule,{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:f({},ie),encoding:f({},ae),mix:f({},oe)}}}function de(l){return P=l.errorModule,{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:{required:!0,properties:{publishMode:{required:!0,values:["publish-main-stream-to-cdn","publish-mix-stream-to-cdn","publish-sub-stream-to-cdn"]}}}}}}var v=0,w=class w{constructor(e){this.core=e;S(this,"_mixTranscodeManager");S(this,"_publishCDNManager");S(this,"_core");S(this,"_modeOptions");S(this,"seq");S(this,"_log");v=v+1,this.seq=v,this._log=e.log.createChild({id:`${this.getAlias()}${v}`}),this._log.info(`[CDNStreaming] created id=${this.getAlias()}${v}`),this._core=e,this._modeOptions=new Map,this._mixTranscodeManager=new L(e,this._log),this._publishCDNManager=new x(e,this._log)}getName(){return w.Name}getAlias(){return"cdn"}getValidateRule(e){switch(e){case"start":return se(this._core);case"update":return ne(this._core);case"stop":return de(this._core)}}getGroup(e){return e.target.publishMode}async start(e){return this._modeOptions.set(e.target.publishMode,e),await this.doStart(e)}async update(e){let i=this._modeOptions.get(e.target.publishMode);return this._core.utils.deepMerge(i,e),await this.doStart(i)}async stop(e){switch(e.target.publishMode){case"publish-mix-stream-to-cdn":{await this._mixTranscodeManager.stopMixTranscode();break}case"publish-main-stream-to-cdn":case"publish-sub-stream-to-cdn":{await this._publishCDNManager.stopPublishTencentCDN(e.target.publishMode);break}}this._modeOptions.delete(e.target.publishMode)}async doStart(e){switch(this._log.info(`[CDNStreaming] doStart: ${JSON.stringify(e)}`),e.target.publishMode){case"publish-mix-stream-to-cdn":{await this._mixTranscodeManager.startMixTranscode(e);break}case"publish-main-stream-to-cdn":case"publish-sub-stream-to-cdn":{await this._publishCDNManager.startPublishTencentCDN(e.target);break}}}};S(w,"TYPE",{PublishMode:{PublishMainStreamToCDN:"publish-main-stream-to-cdn",PublishSubStreamToCDN:"publish-sub-stream-to-cdn",PublishMixStreamToCDN:"publish-mix-stream-to-cdn"}}),S(w,"Name","CDNStreaming");var X=w;var at=X;export{X as CDNStreaming,U as PublishMode,W as TRTCStreamType,at as default};
1
+ var le=Object.create;var z=Object.defineProperty,ce=Object.defineProperties,he=Object.getOwnPropertyDescriptor,pe=Object.getOwnPropertyDescriptors,ge=Object.getOwnPropertyNames,Q=Object.getOwnPropertySymbols,be=Object.getPrototypeOf,ee=Object.prototype.hasOwnProperty,Se=Object.prototype.propertyIsEnumerable;var X=(c,e,r)=>e in c?z(c,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):c[e]=r,C=(c,e)=>{for(var r in e||(e={}))ee.call(e,r)&&X(c,r,e[r]);if(Q)for(var r of Q(e))Se.call(e,r)&&X(c,r,e[r]);return c},H=(c,e)=>ce(c,pe(e));var Te=(c,e)=>()=>(e||c((e={exports:{}}).exports,e),e.exports);var Ce=(c,e,r,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of ge(e))!ee.call(c,o)&&o!==r&&z(c,o,{get:()=>e[o],enumerable:!(i=he(e,o))||i.enumerable});return c};var j=(c,e,r)=>(r=c!=null?le(be(c)):{},Ce(e||!c||!c.__esModule?z(r,"default",{value:c,enumerable:!0}):r,c));var g=(c,e,r)=>(X(c,typeof e!="symbol"?e+"":e,r),r);var F=Te((re,$)=>{(function(c){"use strict";function e(t,h){var a=(t&65535)+(h&65535),b=(t>>16)+(h>>16)+(a>>16);return b<<16|a&65535}function r(t,h){return t<<h|t>>>32-h}function i(t,h,a,b,T,_){return e(r(e(e(h,t),e(b,_)),T),a)}function o(t,h,a,b,T,_,I){return i(h&a|~h&b,t,h,T,_,I)}function l(t,h,a,b,T,_,I){return i(h&b|a&~b,t,h,T,_,I)}function s(t,h,a,b,T,_,I){return i(h^a^b,t,h,T,_,I)}function p(t,h,a,b,T,_,I){return i(a^(h|~b),t,h,T,_,I)}function S(t,h){t[h>>5]|=128<<h%32,t[(h+64>>>9<<4)+14]=h;var a,b,T,_,I,n=1732584193,m=-271733879,d=-1732584194,u=271733878;for(a=0;a<t.length;a+=16)b=n,T=m,_=d,I=u,n=o(n,m,d,u,t[a],7,-680876936),u=o(u,n,m,d,t[a+1],12,-389564586),d=o(d,u,n,m,t[a+2],17,606105819),m=o(m,d,u,n,t[a+3],22,-1044525330),n=o(n,m,d,u,t[a+4],7,-176418897),u=o(u,n,m,d,t[a+5],12,1200080426),d=o(d,u,n,m,t[a+6],17,-1473231341),m=o(m,d,u,n,t[a+7],22,-45705983),n=o(n,m,d,u,t[a+8],7,1770035416),u=o(u,n,m,d,t[a+9],12,-1958414417),d=o(d,u,n,m,t[a+10],17,-42063),m=o(m,d,u,n,t[a+11],22,-1990404162),n=o(n,m,d,u,t[a+12],7,1804603682),u=o(u,n,m,d,t[a+13],12,-40341101),d=o(d,u,n,m,t[a+14],17,-1502002290),m=o(m,d,u,n,t[a+15],22,1236535329),n=l(n,m,d,u,t[a+1],5,-165796510),u=l(u,n,m,d,t[a+6],9,-1069501632),d=l(d,u,n,m,t[a+11],14,643717713),m=l(m,d,u,n,t[a],20,-373897302),n=l(n,m,d,u,t[a+5],5,-701558691),u=l(u,n,m,d,t[a+10],9,38016083),d=l(d,u,n,m,t[a+15],14,-660478335),m=l(m,d,u,n,t[a+4],20,-405537848),n=l(n,m,d,u,t[a+9],5,568446438),u=l(u,n,m,d,t[a+14],9,-1019803690),d=l(d,u,n,m,t[a+3],14,-187363961),m=l(m,d,u,n,t[a+8],20,1163531501),n=l(n,m,d,u,t[a+13],5,-1444681467),u=l(u,n,m,d,t[a+2],9,-51403784),d=l(d,u,n,m,t[a+7],14,1735328473),m=l(m,d,u,n,t[a+12],20,-1926607734),n=s(n,m,d,u,t[a+5],4,-378558),u=s(u,n,m,d,t[a+8],11,-2022574463),d=s(d,u,n,m,t[a+11],16,1839030562),m=s(m,d,u,n,t[a+14],23,-35309556),n=s(n,m,d,u,t[a+1],4,-1530992060),u=s(u,n,m,d,t[a+4],11,1272893353),d=s(d,u,n,m,t[a+7],16,-155497632),m=s(m,d,u,n,t[a+10],23,-1094730640),n=s(n,m,d,u,t[a+13],4,681279174),u=s(u,n,m,d,t[a],11,-358537222),d=s(d,u,n,m,t[a+3],16,-722521979),m=s(m,d,u,n,t[a+6],23,76029189),n=s(n,m,d,u,t[a+9],4,-640364487),u=s(u,n,m,d,t[a+12],11,-421815835),d=s(d,u,n,m,t[a+15],16,530742520),m=s(m,d,u,n,t[a+2],23,-995338651),n=p(n,m,d,u,t[a],6,-198630844),u=p(u,n,m,d,t[a+7],10,1126891415),d=p(d,u,n,m,t[a+14],15,-1416354905),m=p(m,d,u,n,t[a+5],21,-57434055),n=p(n,m,d,u,t[a+12],6,1700485571),u=p(u,n,m,d,t[a+3],10,-1894986606),d=p(d,u,n,m,t[a+10],15,-1051523),m=p(m,d,u,n,t[a+1],21,-2054922799),n=p(n,m,d,u,t[a+8],6,1873313359),u=p(u,n,m,d,t[a+15],10,-30611744),d=p(d,u,n,m,t[a+6],15,-1560198380),m=p(m,d,u,n,t[a+13],21,1309151649),n=p(n,m,d,u,t[a+4],6,-145523070),u=p(u,n,m,d,t[a+11],10,-1120210379),d=p(d,u,n,m,t[a+2],15,718787259),m=p(m,d,u,n,t[a+9],21,-343485551),n=e(n,b),m=e(m,T),d=e(d,_),u=e(u,I);return[n,m,d,u]}function P(t){var h,a="",b=t.length*32;for(h=0;h<b;h+=8)a+=String.fromCharCode(t[h>>5]>>>h%32&255);return a}function D(t){var h,a=[];for(a[(t.length>>2)-1]=void 0,h=0;h<a.length;h+=1)a[h]=0;var b=t.length*8;for(h=0;h<b;h+=8)a[h>>5]|=(t.charCodeAt(h/8)&255)<<h%32;return a}function E(t){return P(S(D(t),t.length*8))}function G(t,h){var a,b=D(t),T=[],_=[],I;for(T[15]=_[15]=void 0,b.length>16&&(b=S(b,t.length*8)),a=0;a<16;a+=1)T[a]=b[a]^909522486,_[a]=b[a]^1549556828;return I=S(T.concat(D(h)),512+h.length*8),P(S(_.concat(I),512+128))}function y(t){var h="0123456789abcdef",a="",b,T;for(T=0;T<t.length;T+=1)b=t.charCodeAt(T),a+=h.charAt(b>>>4&15)+h.charAt(b&15);return a}function O(t){return unescape(encodeURIComponent(t))}function U(t){return E(O(t))}function J(t){return y(U(t))}function B(t,h){return G(O(t),O(h))}function f(t,h){return y(B(t,h))}function V(t,h,a){return h?a?B(h,t):f(h,t):a?U(t):J(t)}typeof define=="function"&&define.amd?define(function(){return V}):typeof $=="object"&&$.exports?$.exports=V:c.md5=V})(re)});var te=j(F(),1);var M=(o=>(o.PublishMainStreamToCDN="publish-main-stream-to-cdn",o.PublishSubStreamToCDN="publish-sub-stream-to-cdn",o.PublishMixStreamToCDN="publish-mix-stream-to-cdn",o.PublishMixStreamToRoom="publish-mix-stream-to-room",o))(M||{}),K=(r=>(r.Main="main",r.Sub="sub",r))(K||{});var q={IT_AUDIO_VIDEO:0,IT_PICTURE:2,IT_CANVAS:3,IT_PURE_AUDIO:4,IT_PURE_VIDEO:5};var N=class{constructor(e,r){g(this,"_core");g(this,"_room");g(this,"_log");g(this,"_params");g(this,"_publishGivenCDNData",null);this._core=e,this._room=e.room,this._log=r}get isPublishingGivenCDN(){return!!this._params}async startPublishGivenCDN(e){if(this._log.info(`[CDNStreaming] startPublishGivenCDN() current: ${JSON.stringify(this._params)}, params: ${JSON.stringify(e)}`),this.isPublishingGivenCDN){let{appId:r,bizId:i,url:o}=this._params||{};if(r===e.appId&&i===e.bizId&&o===e.url)return;await this.stopPublishGivenCDN()}this._params=e,this._publishGivenCDNData={pushRequestTime:Date.now(),pushAppId:e.appId,pushBizId:e.bizId,pushCdnUrl:e.url,pushStreamType:this.convertStreamType(e==null?void 0:e.publishMode),pushStreamId:e.streamId};try{let r=await this._room.sendStartPublishCDN(this._publishGivenCDNData,!1),{code:i,message:o}=r.data;if(i===0)this._log.info("[CDNStreaming] server success: start given cdn.");else throw this.resetGivenCDN(),this._log.error(`[CDNStreaming] server failed: start given cdn errCode: ${i} errMsg: ${o} options: ${JSON.stringify(e)}`),new Error(`[CDNStreaming] server failed: start given cdn errCode: ${i} errMsg: ${o}`)}catch(r){throw this.resetGivenCDN(),r}}async stopPublishGivenCDN(){if(this._log.info("[CDNStreaming] stopPublishGivenCDN"),!this.isPublishingGivenCDN||!this._publishGivenCDNData){this.resetGivenCDN();return}let{pushAppId:e,pushBizId:r,pushCdnUrl:i,pushStreamType:o,pushStreamId:l}=this._publishGivenCDNData,s={pushRequestTime:Date.now(),pushAppId:e,pushBizId:r,pushCdnUrl:i,pushStreamType:o,pushStreamId:l},p=await this._room.sendStopPublishCDN(s,!1),{code:S,message:P}=p.data;if(S===0)this._log.info("[CDNStreaming] server success: stop given cdn."),this.resetGivenCDN();else throw this._log.error(`[CDNStreaming] server failed: stop given cdn errCode: ${S} errMsg: ${P} data: ${JSON.stringify(s)}`),new Error(`[CDNStreaming] server failed: stop given cdn errCode: ${S} errMsg: ${P}`)}resetGivenCDN(){this._publishGivenCDNData=null,this._params=void 0}convertStreamType(e){return e==="publish-sub-stream-to-cdn"?"aux":"main"}};var x=class{constructor(e,r){g(this,"_core");g(this,"_room");g(this,"_log");g(this,"_config",null);g(this,"_data",null);g(this,"_givenCDNManager");this._core=e,this._room=e.room,this._log=r,this.reset()}get isMixing(){return!!this._data}get isStarted(){return!!this._config}get hasCustomCDN(){var e,r,i;return((e=this._config)==null?void 0:e.target.appId)&&((r=this._config)==null?void 0:r.target.bizId)&&((i=this._config)==null?void 0:i.target.url)}async startMixTranscode(e){if(this._log.info(`startMixTranscode: ${JSON.stringify(e)}`),this._config=e,this.installEvents(),!!this._core.room.isJoined)try{let r=this.getInputParam(e),i=this.getOutputParam(e),o=this.getOutputSessionId({config:e,roomId:this._room.roomId,userId:this._room.userId});this.isMixing&&this._data&&o!==this._data.outputSessionId&&(this._log.info("[CDNStreaming] streamId changed, auto stop mixing before start"),await this.doStopMixTranscode()),await this.doStartMixTranscode({outputSessionId:o,inputParam:r,outputParam:i});let{appId:l,bizId:s,url:p,streamId:S=""}=e.target;l&&s&&p&&(this._givenCDNManager||(this._givenCDNManager=new N(this._core,this._log)),await this._givenCDNManager.startPublishGivenCDN({publishMode:e.target.publishMode,appId:l,bizId:s,url:p,streamId:S}))}catch(r){throw this.reset(),r}}async doStartMixTranscode({outputSessionId:e,inputParam:r,outputParam:i}){let o={roomId:String(this._room.roomId),mcuRequestTime:Date.now(),outputSessionId:e,inputParam:r,outputParam:i};this._log.info(`[CDNStreaming] doStartMixTranscode: ${JSON.stringify(o)}`);let l=await this._room.sendStartMixTranscode(o),{code:s}=l.data,{message:p}=l.data;if(s===0)this._log.info("[CDNStreaming] server success: start mix"),this._data=o;else throw s===-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: ${s} errMsg: ${p}`),new Error(`[CDNStreaming] server failed: start mix errCode: ${s} 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 r=await this._room.sendStopMixTranscode(e),{code:i,message:o}=r.data;if(i===0)this._log.info("[CDNStreaming] server success: stop mix"),this.reset();else throw this._log.error(`[CDNStreaming] server failed: stop mix errCode: ${i} errMsg: ${o}`),new Error(`[CDNStreaming] server failed: stop mix errCode: ${i} errMsg: ${o}`)}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:r,roomId:i}){return this._core.utils.isString(e.target.streamId)&&e.target.streamId.length>0?e.target.streamId:(0,te.default)(`${i}_${r}_main`)}getStringRoomId(e,r){return e?String(e):r}getInputParam(e){let{mix:r={}}=e,{audioMixUserList:i=[],videoLayoutList:o=[]}=r,l=o.map(s=>({userId:s.fixedVideoUser.userId,roomId:this.getStringRoomId(s.fixedVideoUser.roomId,s.fixedVideoUser.strRoomId)||this._core.room.roomId,width:s.width||0,height:s.height||0,locationX:s.locationX||0,locationY:s.locationY||0,zOrder:s.zOrder||1,streamType:s.fixedVideoStreamType==="sub"?1:0,inputType:q.IT_PURE_VIDEO,renderMode:s.fillMode||0}));return i.forEach(s=>{let p=l.findIndex(S=>S.userId===s.userId&&S.roomId===this.getStringRoomId(s.roomId,s.strRoomId));p!==-1?l[p].inputType=q.IT_AUDIO_VIDEO:l.push({userId:s.userId,roomId:s.roomId||s.strRoomId||this._core.room.roomId,inputType:q.IT_PURE_AUDIO})}),l}getOutputParam(e){let r=e.target.streamId||"",{encoding:i={},mix:o={}}=e;return{streamId:r,streamType:r.length>0?1:0,width:this._core.utils.isUndefined(i.videoWidth)?640:i.videoWidth,height:this._core.utils.isUndefined(i.videoHeight)?480:i.videoHeight,videoBps:i.videoBitrate||0,fps:i.videoFramerate||15,gop:i.videoGOP||2,audioSampleRate:i.audioSampleRate||48e3,audioBps:i.audioBitrate||64,audioChannels:i.audioChannels||1,backgroundColor:o.backgroundColor||0,backgroundImg:o.backgroundImage||"",extraInfo:"",videoCodec:2,audioCodec:0}}};var W=j(F(),1);var L=class{constructor(e,r){g(this,"_room");g(this,"_core");g(this,"_log");g(this,"_paramsForTencentCDN");g(this,"_initParamsForTencentCDN",{isPublished:!1,isStarted:!1});this._core=e,this._room=e.room,this._log=r,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 o;if(e.room!==this._room||(this._log.info(`[CDNStreaming] handlePublished: mediaType ${e.mediaType}, roomID ${(o=e==null?void 0:e.room)==null?void 0:o.roomId}`),e.mediaType===1))return;let r=e.mediaType===4?"publish-main-stream-to-cdn":"publish-sub-stream-to-cdn",i=this._paramsForTencentCDN.get(r)||null;i!=null&&i.target&&i.isStarted&&await this.startPublishTencentCDN(i.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,r){let i=this._paramsForTencentCDN.get(e),o=C(C({},i),r);this._paramsForTencentCDN.set(e,o)}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 r=e.streamId||"",i=this.generatePublishCDNStreamId(r,e.publishMode),o=this.generatePublishCDNSessionId(e.publishMode),l=e.publishMode==="publish-sub-stream-to-cdn"?1:0,s={requestTime:Date.now(),sessionId:o,streamId:i,streamType:l};await this.doStartPublishTencentCDN(s,e.publishMode);let{appId:p,bizId:S,url:P}=e;if(p&&S&&P){let D=this._paramsForTencentCDN.get(e.publishMode)||this._initParamsForTencentCDN;D!=null&&D.givenCDNManager||(D.givenCDNManager=new N(this._core,this._log),this._paramsForTencentCDN.set(e.publishMode,D)),await D.givenCDNManager.startPublishGivenCDN({publishMode:e.publishMode,appId:p,bizId:S,url:P,streamId:i})}}async doStartPublishTencentCDN(e,r){this._log.info(`[CDNStreaming] doStartPublishTencentCDN: ${JSON.stringify(e)}`);let i=6,o=500,l=0;for(;;){let s=await this._room.sendStartPublishCDN(e,!0),{code:p}=s.data,{message:S}=s.data;if(p===0){this._log.info("[CDNStreaming] server success: start tencent cdn"),this.changeDataStatus(r,{isPublished:!0});break}else if(p===-10006&&l<i)this._log.warn(`[CDNStreaming] doStartPublishTencentCDN: retry...${l+1}/6, reason: ${S}`),l+=1,await new Promise(P=>setTimeout(P,o));else throw this.changeDataStatus(r,{isPublished:!1}),p===-102083&&(S=`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: ${S}`),new Error(`[CDNStreaming] server failed: start tencent cdn errCode: ${p} errMsg: ${S}`)}}async stopPublishTencentCDN(e){this._log.info(`[CDNStreaming] doStartPublishTencentCDN: ${JSON.stringify(e)}`);let r=this._paramsForTencentCDN.get(e)||this._initParamsForTencentCDN;if(r.isPublished){let{target:i}=r;i!=null&&i.bizId&&i.appId&&i.url&&(r!=null&&r.givenCDNManager)&&await(r==null?void 0:r.givenCDNManager.stopPublishGivenCDN()),await this.doStopPublishTencentCDN(e)}this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN)}async doStopPublishTencentCDN(e){let r={requestTime:Date.now(),sessionId:(0,W.default)(`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(e)}`)};this._log.info(`[CDNStreaming] doStopPublishTencentCDN: ${JSON.stringify(r)}`);let i=await this._room.sendStopPublishCDN(r,!0),{code:o}=i.data,{message:l}=i.data;if(o===0)this._log.info("[CDNStreaming] server success: stop tencent cdn"),this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN),this.reset(e);else throw o===-102069&&(this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN),l=`can not stop in auto relayed-push mode ${l}`),this._log.error(`[CDNStreaming] server failed: stop tencent cdn errCode: ${o} errMsg: ${l}`),new Error(`[CDNStreaming] server failed: stop tencent cdn errCode: ${o} errMsg: ${l}`)}reset(e){this.uninstallEvents(),this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN)}generatePublishCDNStreamId(e,r){if(e===""){let i=`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(r)}`;return/^[A-Za-z\d_-]*$/.test(i)||(i=(0,W.default)(i)),`${this._room.sdkAppId}_${i}`}return e}convertPublishModeToStreamType(e){return e==="publish-main-stream-to-cdn"?"main":"aux"}generatePublishCDNSessionId(e){return(0,W.default)(`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(e)}`)}};var ae=j(F(),1);var v=class{constructor(e,r){g(this,"_core");g(this,"_room");g(this,"_log");g(this,"_seq");g(this,"_taskId",null);g(this,"_startData",null);g(this,"_updateData",null);g(this,"_stopData",null);this._core=e,this._room=e.room,this._log=r,this._seq=0}async startPushStreamToRoom(e){var l,s;this._seq+=1,this._startData=C({roomid:String(this._room.roomId),roomType:this._room.useStringRoomId?1:0,sessionId:(0,ae.default)(`${this._room.roomId}_${this._room.userId}_start`),agentParam:{cdnRobotUserid:`mcu_robot_${this._room.roomId}_${this._room.userId}`}},this.getCommonParams(e)),this._log.info(`startPushStreamToRoom: ${JSON.stringify(this._startData)}`);let r=await this._room.sendStartPushStreamToRoom(this._startData),{code:i,message:o}=r.data;i===0?(this._taskId=(s=(l=r.data)==null?void 0:l.data)==null?void 0:s.taskId,this._taskId||this.reportServerError("startPushStreamToRoom",i,`can't resolve task id: ${JSON.stringify(r.data)}`),this._log.info("[CDNStreaming] server success: taskId",this._taskId)):this.reportServerError("startPushStreamToRoom",i,o)}async updatePushStreamToRoom(e){this._seq+=1,this._updateData=H(C({taskid:this._taskId},this.getCommonParams(e)),{enableNtpSync:!0}),this._log.info(`updatePushStreamToRoom: ${JSON.stringify(this._updateData)}`);let r=await this._room.sendUpdatePushStreamToRoom(this._updateData),{code:i,message:o}=r.data;i===0?this._log.info("server success: updatePushStreamToRoom"):this.reportServerError("updatePushStreamToRoom",i,o)}async stopPushStreamToRoom(){this._seq+=1,this._stopData={sdkappid:this._room.sdkAppId,taskid:this._taskId},this._log.info(`stopPushStreamToRoom: ${JSON.stringify(this._stopData)}`);let e=await this._room.sendStopPushStreamToRoom(this._stopData),{code:r,message:i}=e.data;r===0?this._log.info("server success: start mix"):this.reportServerError("stopPushStreamToRoom",r,i)}reportServerError(e,r,i){let o=`server failed: ${e} errCode: ${r} errMsg: ${i}`;throw this._log.error(o),new Error(o)}getPushRtcRoomParams(e){let{robotUser:r}=e.target;return r?[{roomid:String((r==null?void 0:r.roomId)||(r==null?void 0:r.strRoomId))||this._room.roomId,roomType:r!=null&&r.roomId?0:1,pushRobotUserid:r==null?void 0:r.userId}]:[]}getCommonParams(e){return{sdkappid:this._room.sdkAppId,transcode:!0,audioParam:this.getAudioParam(e),videoParam:this.getVideoParam(e),pushRtcRoomParams:this.getPushRtcRoomParams(e),sequenceNumber:this._seq}}getAudioParam(e){let{mix:r={},encoding:i={}}=e,o={audioSamplerate:i.audioSampleRate||48e3,audioBitrateKbps:i.audioBitrate||64,audioChannels:i.audioChannels||1},{audioMixUserList:l}=r;return{audioEncodeParam:o,mixAudioUsers:(l==null?void 0:l.map(s=>({roomid:String(s.roomId||s.strRoomId)||this._room.roomId,userid:s.userId,roomType:s.roomId?0:1})))||[]}}getVideoParam(e){let{mix:r={},encoding:i={}}=e,o={videoCodec:2,videoWidth:i.videoWidth||640,videoHeight:i.videoHeight||480,videoFramerate:i.videoFramerate||15,videoGop:i.videoGOP||2,videoBitrateKbps:i.videoBitrate||800},{videoLayoutList:l}=r;return{videoEncodeParam:o,layoutParams:(l==null?void 0:l.map(s=>({userMediaStream:{userInfo:{roomid:String(s.fixedVideoUser.roomId||s.fixedVideoUser.strRoomId)||this._room.roomId,userid:s.fixedVideoUser.userId,roomType:s.fixedVideoUser.roomId?0:1},streamType:s.fixedVideoStreamType==="sub"?1:0},imageWidth:s.width||0,imageHeight:s.height||0,locationX:s.locationX||0,locationY:s.locationY||0,imageZorder:s.zOrder||1,renderMode:s.fillMode||0})))||[],backgroundColor:r.backgroundColor||0,backgroundImageUrl:r.backgroundImage||""}}};var ie=c=>typeof c=="string";var R;function De(c,e,r,i){if(!/^[A-Za-z\d_-]*$/.test(c)){let{RtcError:l,ErrorCode:s,ErrorCodeDictionary:p}=R;throw new l({code:s.INVALID_PARAMETER,extraCode:p.INVALID_STREAM_ID,messageParams:{key:"streamId"}})}}function Pe(c,e,r,i){let{RtcError:o,ErrorCode:l,ErrorCodeDictionary:s}=R;if(ie(c))throw new o({code:l.INVALID_PARAMETER,extraCode:s.INVALID_ROOM_ID_INTEGER_STRING,fnName:r,messageParams:{key:"roomId"}});if(c===void 0)return;if(!(/^[1-9]\d*$/.test(String(c))&&c<4294967295))throw new o({code:l.INVALID_PARAMETER,extraCode:s.INVALID_ROOM_ID_INTEGER,fnName:r,messageParams:{key:"roomId"}})}function Ie(c,e,r,i){let{RtcError:o,ErrorCode:l,ErrorCodeDictionary:s}=R;if(!/^[A-Za-z\d\s!#$%&()+\-:;<=.>?@[\]^_{}|~,]{1,64}$/.test(c))throw new o({code:l.INVALID_PARAMETER,extraCode:s.INVALID_ROOM_ID_STRING,fnName:r,messageParams:{key:"strRoomId"}})}var A={type:"number",notLessThanZero:!0},Y={type:"object",properties:{userId:{required:!0,type:"string"},roomId:{type:["string","number"],validate:Pe},strRoomId:{type:"string",validate:Ie}}},oe={required:!0,properties:{publishMode:{required:!0,values:["publish-main-stream-to-cdn","publish-mix-stream-to-cdn","publish-sub-stream-to-cdn","publish-mix-stream-to-room"]},streamId:{required:!1,type:"string",validate:De},appId:{type:"number",allowEmpty:!1},bizId:{type:"number",allowEmpty:!1},url:{type:"string",allowEmpty:!1},robotUser:C({},Y)},validate(c,e,r,i){let{publishMode:o,robotUser:l}=c;if(o==="publish-mix-stream-to-room"&&!l){let{RtcError:s,ErrorCode:p}=R;throw new s({code:p.INVALID_PARAMETER,message:"Invalid parameter target, the value of publishMode is PublishMixStreamToRoom, robotUser is required."})}}},se={required:!1,type:"object",properties:{videoWidth:A,videoHeight:A,videoBitrate:H(C({},A),{allowEmpty:!1}),videoFramerate:{type:"number",validate(c,e,r,i){if(c<=0||c>30){let{RtcError:o,ErrorCode:l}=R;throw new o({code:l.INVALID_PARAMETER,message:"Invalid parameter mixConfig -> videoFramerate, the value must be between (0, 30]."})}}},videoGOP:{type:"number",validate(c,e,r,i){if(c<1||c>8){let{RtcError:o,ErrorCode:l}=R;throw new o({code:l.INVALID_PARAMETER,message:"Invalid parameter mixConfig -> videoGOP, the value must be between [1, 8]."})}}},audioSampleRate:A,audioBitrate:{type:"number",validate(c,e,r,i){if(c<32||c>192){let{RtcError:o,ErrorCode:l}=R;throw new o({code:l.INVALID_PARAMETER,message:"Invalid parameter mixConfig -> audioBitrate, the value must be between [32, 192]."})}}},audioChannels:{type:"number",values:[1,2]}}},ne={required:!1,type:"object",properties:{backgroundColor:{type:"number"},backgroundImage:{type:"string"},audioMixUserList:{type:"array",arrayItem:C({},Y)},videoLayoutList:{type:"array",required:!0,arrayItem:{type:"object",properties:{fixedVideoUser:C({},Y),fixedVideoStreamType:{type:"string",required:!0,values:["main","sub"]},fillMode:{type:"number",values:[0,1,2,4]},zOrder:{type:"number",required:!0,validate(c,e,r,i){if(c<1||c>15){let{RtcError:o,ErrorCode:l}=R;throw new o({code:l.INVALID_PARAMETER,message:"Invalid parameter mix -> videoLayoutList -> zOrder, the value must be between [1, 15]."})}}},width:A,height:A,locationX:A,locationY:A}}}}};function me(c){return R=c.errorModule,{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:C({},oe),encoding:C({},se),mix:C({},ne)},validate(e,r,i,o){var S,P;let{publishMode:l}=e.target,{encoding:s,mix:p}=e;if(l==="publish-mix-stream-to-cdn"){let{RtcError:D,ErrorCode:E,ErrorCodeDictionary:G}=c.errorModule;if(!s)throw new D({code:E.INVALID_PARAMETER,extraCode:G.INVALID_PARAMETER_REQUIRED,fnName:i,messageParams:{key:"encoding"}});if(!p)throw new D({code:E.INVALID_PARAMETER,extraCode:G.INVALID_PARAMETER_REQUIRED,fnName:i,messageParams:{key:"mix"}});if(p&&p.videoLayoutList){let y=0,O=0,U=[];if(p.videoLayoutList.forEach((f,V)=>{U.push(f.fixedVideoUser.userId),f.width+f.locationX>y&&(y=f.width+f.locationX),f.height+f.locationY>O&&(O=f.height+f.locationY)}),U.indexOf(c.room.userId)<0)throw new D({code:E.INVALID_PARAMETER,message:"Invalid parameter mix -> videoLayoutList, the value must be include self."});let J=(S=s==null?void 0:s.videoWidth)!=null?S:640,B=(P=s==null?void 0:s.videoHeight)!=null?P:480;if(J<y||B<O)throw new D({code:E.INVALID_PARAMETER,message:"Invalid parameter encoding, the width and height of the mixed video must encompass all the mixed-in video streams."})}}}}}function de(c){return R=c.errorModule,{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:C({},oe),encoding:C({},se),mix:C({},ne)}}}function ue(c){return R=c.errorModule,{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:{required:!0,properties:{publishMode:{required:!0,values:["publish-main-stream-to-cdn","publish-mix-stream-to-cdn","publish-sub-stream-to-cdn","publish-mix-stream-to-room"]}}}}}}var k=0,w=class w{constructor(e){this.core=e;g(this,"_mixTranscodeManager");g(this,"_publishCDNManager");g(this,"_pushStreamToRoomManager");g(this,"_core");g(this,"_modeOptions");g(this,"seq");g(this,"_log");k=k+1,this.seq=k,this._log=e.log.createChild({id:`${this.getAlias()}${k}`}),this._log.info(`[CDNStreaming] created id=${this.getAlias()}${k}`),this._core=e,this._modeOptions=new Map,this._mixTranscodeManager=new x(e,this._log),this._publishCDNManager=new L(e,this._log),this._pushStreamToRoomManager=new v(e,this._log)}getName(){return w.Name}getAlias(){return"cdn"}getValidateRule(e){switch(e){case"start":return me(this._core);case"update":return de(this._core);case"stop":return ue(this._core)}}getGroup(e){return e.target.publishMode}async start(e){return this._modeOptions.set(e.target.publishMode,e),e.target.publishMode==="publish-mix-stream-to-room"?this._pushStreamToRoomManager.startPushStreamToRoom(e):await this.doStart(e)}async update(e){let r=this._modeOptions.get(e.target.publishMode);return this._core.utils.deepMerge(r,e),e.target.publishMode==="publish-mix-stream-to-room"?this._pushStreamToRoomManager.updatePushStreamToRoom(r):await this.doStart(r)}async stop(e){switch(e.target.publishMode){case"publish-mix-stream-to-cdn":{await this._mixTranscodeManager.stopMixTranscode();break}case"publish-main-stream-to-cdn":case"publish-sub-stream-to-cdn":{await this._publishCDNManager.stopPublishTencentCDN(e.target.publishMode);break}case"publish-mix-stream-to-room":{this._pushStreamToRoomManager.stopPushStreamToRoom();break}}this._modeOptions.delete(e.target.publishMode)}async doStart(e){switch(this._log.info(`[CDNStreaming] doStart: ${JSON.stringify(e)}`),e.target.publishMode){case"publish-mix-stream-to-cdn":{await this._mixTranscodeManager.startMixTranscode(e);break}case"publish-main-stream-to-cdn":case"publish-sub-stream-to-cdn":{await this._publishCDNManager.startPublishTencentCDN(e.target);break}}}};g(w,"TYPE",{PublishMode:{PublishMainStreamToCDN:"publish-main-stream-to-cdn",PublishSubStreamToCDN:"publish-sub-stream-to-cdn",PublishMixStreamToCDN:"publish-mix-stream-to-cdn",PublishMixStreamToRoom:"publish-mix-stream-to-room"}}),g(w,"Name","CDNStreaming");var Z=w;var lr=Z;export{Z as CDNStreaming,M as PublishMode,K as TRTCStreamType,lr as default};
@@ -1 +1 @@
1
- "use strict";var CDNStreaming=(()=>{var me=Object.create;var L=Object.defineProperty,le=Object.defineProperties,ce=Object.getOwnPropertyDescriptor,pe=Object.getOwnPropertyDescriptors,he=Object.getOwnPropertyNames,z=Object.getOwnPropertySymbols,ge=Object.getPrototypeOf,j=Object.prototype.hasOwnProperty,Se=Object.prototype.propertyIsEnumerable;var W=(l,e,i)=>e in l?L(l,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):l[e]=i,f=(l,e)=>{for(var i in e||(e={}))j.call(e,i)&&W(l,i,e[i]);if(z)for(var i of z(e))Se.call(e,i)&&W(l,i,e[i]);return l},Y=(l,e)=>le(l,pe(e));var be=(l,e)=>()=>(e||l((e={exports:{}}).exports,e),e.exports),Ce=(l,e)=>{for(var i in e)L(l,i,{get:e[i],enumerable:!0})},Z=(l,e,i,d)=>{if(e&&typeof e=="object"||typeof e=="function")for(let u of he(e))!j.call(l,u)&&u!==i&&L(l,u,{get:()=>e[u],enumerable:!(d=ce(e,u))||d.enumerable});return l};var Q=(l,e,i)=>(i=l!=null?me(ge(l)):{},Z(e||!l||!l.__esModule?L(i,"default",{value:l,enumerable:!0}):i,l)),De=l=>Z(L({},"__esModule",{value:!0}),l);var S=(l,e,i)=>(W(l,typeof e!="symbol"?e+"":e,i),i);var X=be((ee,H)=>{(function(l){"use strict";function e(t,p){var r=(t&65535)+(p&65535),g=(t>>16)+(p>>16)+(r>>16);return g<<16|r&65535}function i(t,p){return t<<p|t>>>32-p}function d(t,p,r,g,C,D){return e(i(e(e(p,t),e(g,D)),C),r)}function u(t,p,r,g,C,D,I){return d(p&r|~p&g,t,p,C,D,I)}function c(t,p,r,g,C,D,I){return d(p&g|r&~g,t,p,C,D,I)}function m(t,p,r,g,C,D,I){return d(p^r^g,t,p,C,D,I)}function h(t,p,r,g,C,D,I){return d(r^(p|~g),t,p,C,D,I)}function b(t,p){t[p>>5]|=128<<p%32,t[(p+64>>>9<<4)+14]=p;var r,g,C,D,I,a=1732584193,o=-271733879,s=-1732584194,n=271733878;for(r=0;r<t.length;r+=16)g=a,C=o,D=s,I=n,a=u(a,o,s,n,t[r],7,-680876936),n=u(n,a,o,s,t[r+1],12,-389564586),s=u(s,n,a,o,t[r+2],17,606105819),o=u(o,s,n,a,t[r+3],22,-1044525330),a=u(a,o,s,n,t[r+4],7,-176418897),n=u(n,a,o,s,t[r+5],12,1200080426),s=u(s,n,a,o,t[r+6],17,-1473231341),o=u(o,s,n,a,t[r+7],22,-45705983),a=u(a,o,s,n,t[r+8],7,1770035416),n=u(n,a,o,s,t[r+9],12,-1958414417),s=u(s,n,a,o,t[r+10],17,-42063),o=u(o,s,n,a,t[r+11],22,-1990404162),a=u(a,o,s,n,t[r+12],7,1804603682),n=u(n,a,o,s,t[r+13],12,-40341101),s=u(s,n,a,o,t[r+14],17,-1502002290),o=u(o,s,n,a,t[r+15],22,1236535329),a=c(a,o,s,n,t[r+1],5,-165796510),n=c(n,a,o,s,t[r+6],9,-1069501632),s=c(s,n,a,o,t[r+11],14,643717713),o=c(o,s,n,a,t[r],20,-373897302),a=c(a,o,s,n,t[r+5],5,-701558691),n=c(n,a,o,s,t[r+10],9,38016083),s=c(s,n,a,o,t[r+15],14,-660478335),o=c(o,s,n,a,t[r+4],20,-405537848),a=c(a,o,s,n,t[r+9],5,568446438),n=c(n,a,o,s,t[r+14],9,-1019803690),s=c(s,n,a,o,t[r+3],14,-187363961),o=c(o,s,n,a,t[r+8],20,1163531501),a=c(a,o,s,n,t[r+13],5,-1444681467),n=c(n,a,o,s,t[r+2],9,-51403784),s=c(s,n,a,o,t[r+7],14,1735328473),o=c(o,s,n,a,t[r+12],20,-1926607734),a=m(a,o,s,n,t[r+5],4,-378558),n=m(n,a,o,s,t[r+8],11,-2022574463),s=m(s,n,a,o,t[r+11],16,1839030562),o=m(o,s,n,a,t[r+14],23,-35309556),a=m(a,o,s,n,t[r+1],4,-1530992060),n=m(n,a,o,s,t[r+4],11,1272893353),s=m(s,n,a,o,t[r+7],16,-155497632),o=m(o,s,n,a,t[r+10],23,-1094730640),a=m(a,o,s,n,t[r+13],4,681279174),n=m(n,a,o,s,t[r],11,-358537222),s=m(s,n,a,o,t[r+3],16,-722521979),o=m(o,s,n,a,t[r+6],23,76029189),a=m(a,o,s,n,t[r+9],4,-640364487),n=m(n,a,o,s,t[r+12],11,-421815835),s=m(s,n,a,o,t[r+15],16,530742520),o=m(o,s,n,a,t[r+2],23,-995338651),a=h(a,o,s,n,t[r],6,-198630844),n=h(n,a,o,s,t[r+7],10,1126891415),s=h(s,n,a,o,t[r+14],15,-1416354905),o=h(o,s,n,a,t[r+5],21,-57434055),a=h(a,o,s,n,t[r+12],6,1700485571),n=h(n,a,o,s,t[r+3],10,-1894986606),s=h(s,n,a,o,t[r+10],15,-1051523),o=h(o,s,n,a,t[r+1],21,-2054922799),a=h(a,o,s,n,t[r+8],6,1873313359),n=h(n,a,o,s,t[r+15],10,-30611744),s=h(s,n,a,o,t[r+6],15,-1560198380),o=h(o,s,n,a,t[r+13],21,1309151649),a=h(a,o,s,n,t[r+4],6,-145523070),n=h(n,a,o,s,t[r+11],10,-1120210379),s=h(s,n,a,o,t[r+2],15,718787259),o=h(o,s,n,a,t[r+9],21,-343485551),a=e(a,g),o=e(o,C),s=e(s,D),n=e(n,I);return[a,o,s,n]}function _(t){var p,r="",g=t.length*32;for(p=0;p<g;p+=8)r+=String.fromCharCode(t[p>>5]>>>p%32&255);return r}function T(t){var p,r=[];for(r[(t.length>>2)-1]=void 0,p=0;p<r.length;p+=1)r[p]=0;var g=t.length*8;for(p=0;p<g;p+=8)r[p>>5]|=(t.charCodeAt(p/8)&255)<<p%32;return r}function y(t){return _(b(T(t),t.length*8))}function G(t,p){var r,g=T(t),C=[],D=[],I;for(C[15]=D[15]=void 0,g.length>16&&(g=b(g,t.length*8)),r=0;r<16;r+=1)C[r]=g[r]^909522486,D[r]=g[r]^1549556828;return I=b(C.concat(T(p)),512+p.length*8),_(b(D.concat(I),512+128))}function U(t){var p="0123456789abcdef",r="",g,C;for(C=0;C<t.length;C+=1)g=t.charCodeAt(C),r+=p.charAt(g>>>4&15)+p.charAt(g&15);return r}function N(t){return unescape(encodeURIComponent(t))}function M(t){return y(N(t))}function J(t){return U(M(t))}function B(t,p){return G(N(t),N(p))}function A(t,p){return U(B(t,p))}function V(t,p,r){return p?r?B(p,t):A(p,t):r?M(t):J(t)}typeof define=="function"&&define.amd?define(function(){return V}):typeof H=="object"&&H.exports?H.exports=V:l.md5=V})(ee)});var Pe={};Ce(Pe,{CDNStreaming:()=>q,PublishMode:()=>R,TRTCStreamType:()=>$,default:()=>Ae});var te=Q(X(),1);var R=(d=>(d.PublishMainStreamToCDN="publish-main-stream-to-cdn",d.PublishSubStreamToCDN="publish-sub-stream-to-cdn",d.PublishMixStreamToCDN="publish-mix-stream-to-cdn",d))(R||{}),$=(i=>(i.Main="main",i.Sub="sub",i))($||{});var F={IT_AUDIO_VIDEO:0,IT_PICTURE:2,IT_CANVAS:3,IT_PURE_AUDIO:4,IT_PURE_VIDEO:5};var E=class{constructor(e,i){S(this,"_core");S(this,"_room");S(this,"_log");S(this,"_params");S(this,"_publishGivenCDNData",null);this._core=e,this._room=e.room,this._log=i}get isPublishingGivenCDN(){return!!this._params}async startPublishGivenCDN(e){if(this._log.info(`[CDNStreaming] startPublishGivenCDN() current: ${JSON.stringify(this._params)}, params: ${JSON.stringify(e)}`),this.isPublishingGivenCDN){let{appId:i,bizId:d,url:u}=this._params||{};if(i===e.appId&&d===e.bizId&&u===e.url)return;await this.stopPublishGivenCDN()}this._params=e,this._publishGivenCDNData={pushRequestTime:Date.now(),pushAppId:e.appId,pushBizId:e.bizId,pushCdnUrl:e.url,pushStreamType:this.convertStreamType(e==null?void 0:e.publishMode),pushStreamId:e.streamId};try{let i=await this._room.sendStartPublishCDN(this._publishGivenCDNData,!1),{code:d,message:u}=i.data;if(d===0)this._log.info("[CDNStreaming] server success: start given cdn.");else throw this.resetGivenCDN(),this._log.error(`[CDNStreaming] server failed: start given cdn errCode: ${d} errMsg: ${u} options: ${JSON.stringify(e)}`),new Error(`[CDNStreaming] server failed: start given cdn errCode: ${d} errMsg: ${u}`)}catch(i){throw this.resetGivenCDN(),i}}async stopPublishGivenCDN(){if(this._log.info("[CDNStreaming] stopPublishGivenCDN"),!this.isPublishingGivenCDN||!this._publishGivenCDNData){this.resetGivenCDN();return}let{pushAppId:e,pushBizId:i,pushCdnUrl:d,pushStreamType:u,pushStreamId:c}=this._publishGivenCDNData,m={pushRequestTime:Date.now(),pushAppId:e,pushBizId:i,pushCdnUrl:d,pushStreamType:u,pushStreamId:c},h=await this._room.sendStopPublishCDN(m,!1),{code:b,message:_}=h.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(m)}`),new Error(`[CDNStreaming] server failed: stop given cdn errCode: ${b} errMsg: ${_}`)}resetGivenCDN(){this._publishGivenCDNData=null,this._params=void 0}convertStreamType(e){return e==="publish-sub-stream-to-cdn"?"aux":"main"}};var x=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,d;return((e=this._config)==null?void 0:e.target.appId)&&((i=this._config)==null?void 0:i.target.bizId)&&((d=this._config)==null?void 0:d.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),d=this.getOutputParam(e),u=this.getOutputSessionId({config:e,roomId:this._room.roomId,userId:this._room.userId});this.isMixing&&this._data&&u!==this._data.outputSessionId&&(this._log.info("[CDNStreaming] streamId changed, auto stop mixing before start"),await this.doStopMixTranscode()),await this.doStartMixTranscode({outputSessionId:u,inputParam:i,outputParam:d});let{appId:c,bizId:m,url:h,streamId:b=""}=e.target;c&&m&&h&&(this._givenCDNManager||(this._givenCDNManager=new E(this._core,this._log)),await this._givenCDNManager.startPublishGivenCDN({publishMode:e.target.publishMode,appId:c,bizId:m,url:h,streamId:b}))}catch(i){throw this.reset(),i}}async doStartMixTranscode({outputSessionId:e,inputParam:i,outputParam:d}){let u={roomId:String(this._room.roomId),mcuRequestTime:Date.now(),outputSessionId:e,inputParam:i,outputParam:d};this._log.info(`[CDNStreaming] doStartMixTranscode: ${JSON.stringify(u)}`);let c=await this._room.sendStartMixTranscode(u),{code:m}=c.data,{message:h}=c.data;if(m===0)this._log.info("[CDNStreaming] server success: start mix"),this._data=u;else throw m===-102083&&(h=`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: ${m} errMsg: ${h}`),new Error(`[CDNStreaming] server failed: start mix errCode: ${m} errMsg: ${h}`)}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:d,message:u}=i.data;if(d===0)this._log.info("[CDNStreaming] server success: stop mix"),this.reset();else throw this._log.error(`[CDNStreaming] server failed: stop mix errCode: ${d} errMsg: ${u}`),new Error(`[CDNStreaming] server failed: stop mix errCode: ${d} errMsg: ${u}`)}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:d}){return this._core.utils.isString(e.target.streamId)&&e.target.streamId.length>0?e.target.streamId:(0,te.default)(`${d}_${i}_main`)}getStringRoomId(e,i){return e?String(e):i}getInputParam(e){let{mix:i={}}=e,{audioMixUserList:d=[],videoLayoutList:u=[]}=i,c=u.map(m=>({userId:m.fixedVideoUser.userId,roomId:this.getStringRoomId(m.fixedVideoUser.roomId,m.fixedVideoUser.strRoomId)||this._core.room.roomId,width:m.width||0,height:m.height||0,locationX:m.locationX||0,locationY:m.locationY||0,zOrder:m.zOrder||1,streamType:m.fixedVideoStreamType==="sub"?1:0,inputType:F.IT_PURE_VIDEO,renderMode:m.fillMode||0}));return d.forEach(m=>{let h=c.findIndex(b=>b.userId===m.userId&&b.roomId===this.getStringRoomId(m.roomId,m.strRoomId));h!==-1?c[h].inputType=F.IT_AUDIO_VIDEO:c.push({userId:m.userId,roomId:m.roomId||m.strRoomId||this._core.room.roomId,inputType:F.IT_PURE_AUDIO})}),c}getOutputParam(e){let i=e.target.streamId||"",{encoding:d={},mix:u={}}=e;return{streamId:i,streamType:i.length>0?1:0,width:this._core.utils.isUndefined(d.videoWidth)?640:d.videoWidth,height:this._core.utils.isUndefined(d.videoHeight)?480:d.videoHeight,videoBps:d.videoBitrate||0,fps:d.videoFramerate||15,gop:d.videoGOP||2,audioSampleRate:d.audioSampleRate||48e3,audioBps:d.audioBitrate||64,audioChannels:d.audioChannels||1,backgroundColor:u.backgroundColor||0,backgroundImg:u.backgroundImage||"",extraInfo:"",videoCodec:2,audioCodec:0}}};var K=Q(X(),1);var v=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 u;if(e.room!==this._room||(this._log.info(`[CDNStreaming] handlePublished: mediaType ${e.mediaType}, roomID ${(u=e==null?void 0:e.room)==null?void 0:u.roomId}`),e.mediaType===1))return;let i=e.mediaType===4?"publish-main-stream-to-cdn":"publish-sub-stream-to-cdn",d=this._paramsForTencentCDN.get(i)||null;d!=null&&d.target&&d.isStarted&&await this.startPublishTencentCDN(d.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 d=this._paramsForTencentCDN.get(e),u=f(f({},d),i);this._paramsForTencentCDN.set(e,u)}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||"",d=this.generatePublishCDNStreamId(i,e.publishMode),u=this.generatePublishCDNSessionId(e.publishMode),c=e.publishMode==="publish-sub-stream-to-cdn"?1:0,m={requestTime:Date.now(),sessionId:u,streamId:d,streamType:c};await this.doStartPublishTencentCDN(m,e.publishMode);let{appId:h,bizId:b,url:_}=e;if(h&&b&&_){let T=this._paramsForTencentCDN.get(e.publishMode)||this._initParamsForTencentCDN;T!=null&&T.givenCDNManager||(T.givenCDNManager=new E(this._core,this._log),this._paramsForTencentCDN.set(e.publishMode,T)),await T.givenCDNManager.startPublishGivenCDN({publishMode:e.publishMode,appId:h,bizId:b,url:_,streamId:d})}}async doStartPublishTencentCDN(e,i){this._log.info(`[CDNStreaming] doStartPublishTencentCDN: ${JSON.stringify(e)}`);let d=6,u=500,c=0;for(;;){let m=await this._room.sendStartPublishCDN(e,!0),{code:h}=m.data,{message:b}=m.data;if(h===0){this._log.info("[CDNStreaming] server success: start tencent cdn"),this.changeDataStatus(i,{isPublished:!0});break}else if(h===-10006&&c<d)this._log.warn(`[CDNStreaming] doStartPublishTencentCDN: retry...${c+1}/6, reason: ${b}`),c+=1,await new Promise(_=>setTimeout(_,u));else throw this.changeDataStatus(i,{isPublished:!1}),h===-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: ${h} errMsg: ${b}`),new Error(`[CDNStreaming] server failed: start tencent cdn errCode: ${h} 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:d}=i;d!=null&&d.bizId&&d.appId&&d.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,K.default)(`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(e)}`)};this._log.info(`[CDNStreaming] doStopPublishTencentCDN: ${JSON.stringify(i)}`);let d=await this._room.sendStopPublishCDN(i,!0),{code:u}=d.data,{message:c}=d.data;if(u===0)this._log.info("[CDNStreaming] server success: stop tencent cdn"),this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN),this.reset(e);else throw u===-102069&&(this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN),c=`can not stop in auto relayed-push mode ${c}`),this._log.error(`[CDNStreaming] server failed: stop tencent cdn errCode: ${u} errMsg: ${c}`),new Error(`[CDNStreaming] server failed: stop tencent cdn errCode: ${u} errMsg: ${c}`)}reset(e){this.uninstallEvents(),this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN)}generatePublishCDNStreamId(e,i){if(e===""){let d=`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(i)}`;return/^[A-Za-z\d_-]*$/.test(d)||(d=(0,K.default)(d)),`${this._room.sdkAppId}_${d}`}return e}convertPublishModeToStreamType(e){return e==="publish-main-stream-to-cdn"?"main":"aux"}generatePublishCDNSessionId(e){return(0,K.default)(`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(e)}`)}};var re=l=>typeof l=="string";var P;function _e(l,e,i,d){if(!/^[A-Za-z\d_-]*$/.test(l)){let{RtcError:c,ErrorCode:m,ErrorCodeDictionary:h}=P;throw new c({code:m.INVALID_PARAMETER,extraCode:h.INVALID_STREAM_ID,messageParams:{key:"streamId"}})}}function Ie(l,e,i,d){let{RtcError:u,ErrorCode:c,ErrorCodeDictionary:m}=P;if(re(l))throw new u({code:c.INVALID_PARAMETER,extraCode:m.INVALID_ROOM_ID_INTEGER_STRING,fnName:i,messageParams:{key:"roomId"}});if(l===void 0)return;if(!(/^[1-9]\d*$/.test(String(l))&&l<4294967295))throw new u({code:c.INVALID_PARAMETER,extraCode:m.INVALID_ROOM_ID_INTEGER,fnName:i,messageParams:{key:"roomId"}})}function fe(l,e,i,d){let{RtcError:u,ErrorCode:c,ErrorCodeDictionary:m}=P;if(!/^[A-Za-z\d\s!#$%&()+\-:;<=.>?@[\]^_{}|~,]{1,64}$/.test(l))throw new u({code:c.INVALID_PARAMETER,extraCode:m.INVALID_ROOM_ID_STRING,fnName:i,messageParams:{key:"strRoomId"}})}var O={type:"number",notLessThanZero:!0},ie={type:"object",required:!0,properties:{userId:{required:!0,type:"string"},roomId:{type:["string","number"],validate:Ie},strRoomId:{type:"string",validate:fe}}},ae={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:_e},appId:{type:"number",allowEmpty:!1},bizId:{type:"number",allowEmpty:!1},url:{type:"string",allowEmpty:!1}}},oe={required:!1,type:"object",properties:{videoWidth:O,videoHeight:O,videoBitrate:Y(f({},O),{allowEmpty:!1}),videoFramerate:{type:"number",validate(l,e,i,d){if(l<=0||l>30){let{RtcError:u,ErrorCode:c}=P;throw new u({code:c.INVALID_PARAMETER,message:"Invalid parameter mixConfig -> videoFramerate, the value must be between (0, 30]."})}}},videoGOP:{type:"number",validate(l,e,i,d){if(l<1||l>8){let{RtcError:u,ErrorCode:c}=P;throw new u({code:c.INVALID_PARAMETER,message:"Invalid parameter mixConfig -> videoGOP, the value must be between [1, 8]."})}}},audioSampleRate:O,audioBitrate:{type:"number",validate(l,e,i,d){if(l<32||l>192){let{RtcError:u,ErrorCode:c}=P;throw new u({code:c.INVALID_PARAMETER,message:"Invalid parameter mixConfig -> audioBitrate, the value must be between [32, 192]."})}}},audioChannels:{type:"number",values:[1,2]}}},se={required:!1,type:"object",properties:{backgroundColor:{type:"number"},backgroundImage:{type:"string"},audioMixUserList:{type:"array",arrayItem:f({},ie)},videoLayoutList:{type:"array",required:!0,arrayItem:{type:"object",properties:{fixedVideoUser:f({},ie),fixedVideoStreamType:{type:"string",required:!0,values:["main","sub"]},fillMode:{type:"number",values:[0,1,2,4]},zOrder:{type:"number",required:!0,validate(l,e,i,d){if(l<1||l>15){let{RtcError:u,ErrorCode:c}=P;throw new u({code:c.INVALID_PARAMETER,message:"Invalid parameter mix -> videoLayoutList -> zOrder, the value must be between [1, 15]."})}}},width:O,height:O,locationX:O,locationY:O}}}}};function ne(l){return P=l.errorModule,{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:f({},ae),encoding:f({},oe),mix:f({},se)},validate(e,i,d,u){var b,_;let{publishMode:c}=e.target,{encoding:m,mix:h}=e;if(c==="publish-mix-stream-to-cdn"){let{RtcError:T,ErrorCode:y,ErrorCodeDictionary:G}=l.errorModule;if(!m)throw new T({code:y.INVALID_PARAMETER,extraCode:G.INVALID_PARAMETER_REQUIRED,fnName:d,messageParams:{key:"encoding"}});if(!h)throw new T({code:y.INVALID_PARAMETER,extraCode:G.INVALID_PARAMETER_REQUIRED,fnName:d,messageParams:{key:"mix"}});if(h&&h.videoLayoutList){let U=0,N=0,M=[];if(h.videoLayoutList.forEach((A,V)=>{M.push(A.fixedVideoUser.userId),A.width+A.locationX>U&&(U=A.width+A.locationX),A.height+A.locationY>N&&(N=A.height+A.locationY)}),M.indexOf(l.room.userId)<0)throw new T({code:y.INVALID_PARAMETER,message:"Invalid parameter mix -> videoLayoutList, the value must be include self."});let J=(b=m==null?void 0:m.videoWidth)!=null?b:640,B=(_=m==null?void 0:m.videoHeight)!=null?_:480;if(J<U||B<N)throw new T({code:y.INVALID_PARAMETER,message:"Invalid parameter encoding, the width and height of the mixed video must encompass all the mixed-in video streams."})}}}}}function de(l){return P=l.errorModule,{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:f({},ae),encoding:f({},oe),mix:f({},se)}}}function ue(l){return P=l.errorModule,{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:{required:!0,properties:{publishMode:{required:!0,values:["publish-main-stream-to-cdn","publish-mix-stream-to-cdn","publish-sub-stream-to-cdn"]}}}}}}var w=0,k=class k{constructor(e){this.core=e;S(this,"_mixTranscodeManager");S(this,"_publishCDNManager");S(this,"_core");S(this,"_modeOptions");S(this,"seq");S(this,"_log");w=w+1,this.seq=w,this._log=e.log.createChild({id:`${this.getAlias()}${w}`}),this._log.info(`[CDNStreaming] created id=${this.getAlias()}${w}`),this._core=e,this._modeOptions=new Map,this._mixTranscodeManager=new x(e,this._log),this._publishCDNManager=new v(e,this._log)}getName(){return k.Name}getAlias(){return"cdn"}getValidateRule(e){switch(e){case"start":return ne(this._core);case"update":return de(this._core);case"stop":return ue(this._core)}}getGroup(e){return e.target.publishMode}async start(e){return this._modeOptions.set(e.target.publishMode,e),await this.doStart(e)}async update(e){let i=this._modeOptions.get(e.target.publishMode);return this._core.utils.deepMerge(i,e),await this.doStart(i)}async stop(e){switch(e.target.publishMode){case"publish-mix-stream-to-cdn":{await this._mixTranscodeManager.stopMixTranscode();break}case"publish-main-stream-to-cdn":case"publish-sub-stream-to-cdn":{await this._publishCDNManager.stopPublishTencentCDN(e.target.publishMode);break}}this._modeOptions.delete(e.target.publishMode)}async doStart(e){switch(this._log.info(`[CDNStreaming] doStart: ${JSON.stringify(e)}`),e.target.publishMode){case"publish-mix-stream-to-cdn":{await this._mixTranscodeManager.startMixTranscode(e);break}case"publish-main-stream-to-cdn":case"publish-sub-stream-to-cdn":{await this._publishCDNManager.startPublishTencentCDN(e.target);break}}}};S(k,"TYPE",{PublishMode:{PublishMainStreamToCDN:"publish-main-stream-to-cdn",PublishSubStreamToCDN:"publish-sub-stream-to-cdn",PublishMixStreamToCDN:"publish-mix-stream-to-cdn"}}),S(k,"Name","CDNStreaming");var q=k;var Ae=q;return De(Pe);})().default;
1
+ "use strict";var CDNStreaming=(()=>{var ce=Object.create;var x=Object.defineProperty,he=Object.defineProperties,pe=Object.getOwnPropertyDescriptor,ge=Object.getOwnPropertyDescriptors,be=Object.getOwnPropertyNames,Q=Object.getOwnPropertySymbols,Se=Object.getPrototypeOf,ee=Object.prototype.hasOwnProperty,Te=Object.prototype.propertyIsEnumerable;var j=(c,e,r)=>e in c?x(c,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):c[e]=r,C=(c,e)=>{for(var r in e||(e={}))ee.call(e,r)&&j(c,r,e[r]);if(Q)for(var r of Q(e))Te.call(e,r)&&j(c,r,e[r]);return c},F=(c,e)=>he(c,ge(e));var Ce=(c,e)=>()=>(e||c((e={exports:{}}).exports,e),e.exports),_e=(c,e)=>{for(var r in e)x(c,r,{get:e[r],enumerable:!0})},re=(c,e,r,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of be(e))!ee.call(c,o)&&o!==r&&x(c,o,{get:()=>e[o],enumerable:!(i=pe(e,o))||i.enumerable});return c};var Y=(c,e,r)=>(r=c!=null?ce(Se(c)):{},re(e||!c||!c.__esModule?x(r,"default",{value:c,enumerable:!0}):r,c)),De=c=>re(x({},"__esModule",{value:!0}),c);var g=(c,e,r)=>(j(c,typeof e!="symbol"?e+"":e,r),r);var q=Ce((te,K)=>{(function(c){"use strict";function e(t,h){var a=(t&65535)+(h&65535),b=(t>>16)+(h>>16)+(a>>16);return b<<16|a&65535}function r(t,h){return t<<h|t>>>32-h}function i(t,h,a,b,T,_){return e(r(e(e(h,t),e(b,_)),T),a)}function o(t,h,a,b,T,_,I){return i(h&a|~h&b,t,h,T,_,I)}function l(t,h,a,b,T,_,I){return i(h&b|a&~b,t,h,T,_,I)}function s(t,h,a,b,T,_,I){return i(h^a^b,t,h,T,_,I)}function p(t,h,a,b,T,_,I){return i(a^(h|~b),t,h,T,_,I)}function S(t,h){t[h>>5]|=128<<h%32,t[(h+64>>>9<<4)+14]=h;var a,b,T,_,I,n=1732584193,m=-271733879,d=-1732584194,u=271733878;for(a=0;a<t.length;a+=16)b=n,T=m,_=d,I=u,n=o(n,m,d,u,t[a],7,-680876936),u=o(u,n,m,d,t[a+1],12,-389564586),d=o(d,u,n,m,t[a+2],17,606105819),m=o(m,d,u,n,t[a+3],22,-1044525330),n=o(n,m,d,u,t[a+4],7,-176418897),u=o(u,n,m,d,t[a+5],12,1200080426),d=o(d,u,n,m,t[a+6],17,-1473231341),m=o(m,d,u,n,t[a+7],22,-45705983),n=o(n,m,d,u,t[a+8],7,1770035416),u=o(u,n,m,d,t[a+9],12,-1958414417),d=o(d,u,n,m,t[a+10],17,-42063),m=o(m,d,u,n,t[a+11],22,-1990404162),n=o(n,m,d,u,t[a+12],7,1804603682),u=o(u,n,m,d,t[a+13],12,-40341101),d=o(d,u,n,m,t[a+14],17,-1502002290),m=o(m,d,u,n,t[a+15],22,1236535329),n=l(n,m,d,u,t[a+1],5,-165796510),u=l(u,n,m,d,t[a+6],9,-1069501632),d=l(d,u,n,m,t[a+11],14,643717713),m=l(m,d,u,n,t[a],20,-373897302),n=l(n,m,d,u,t[a+5],5,-701558691),u=l(u,n,m,d,t[a+10],9,38016083),d=l(d,u,n,m,t[a+15],14,-660478335),m=l(m,d,u,n,t[a+4],20,-405537848),n=l(n,m,d,u,t[a+9],5,568446438),u=l(u,n,m,d,t[a+14],9,-1019803690),d=l(d,u,n,m,t[a+3],14,-187363961),m=l(m,d,u,n,t[a+8],20,1163531501),n=l(n,m,d,u,t[a+13],5,-1444681467),u=l(u,n,m,d,t[a+2],9,-51403784),d=l(d,u,n,m,t[a+7],14,1735328473),m=l(m,d,u,n,t[a+12],20,-1926607734),n=s(n,m,d,u,t[a+5],4,-378558),u=s(u,n,m,d,t[a+8],11,-2022574463),d=s(d,u,n,m,t[a+11],16,1839030562),m=s(m,d,u,n,t[a+14],23,-35309556),n=s(n,m,d,u,t[a+1],4,-1530992060),u=s(u,n,m,d,t[a+4],11,1272893353),d=s(d,u,n,m,t[a+7],16,-155497632),m=s(m,d,u,n,t[a+10],23,-1094730640),n=s(n,m,d,u,t[a+13],4,681279174),u=s(u,n,m,d,t[a],11,-358537222),d=s(d,u,n,m,t[a+3],16,-722521979),m=s(m,d,u,n,t[a+6],23,76029189),n=s(n,m,d,u,t[a+9],4,-640364487),u=s(u,n,m,d,t[a+12],11,-421815835),d=s(d,u,n,m,t[a+15],16,530742520),m=s(m,d,u,n,t[a+2],23,-995338651),n=p(n,m,d,u,t[a],6,-198630844),u=p(u,n,m,d,t[a+7],10,1126891415),d=p(d,u,n,m,t[a+14],15,-1416354905),m=p(m,d,u,n,t[a+5],21,-57434055),n=p(n,m,d,u,t[a+12],6,1700485571),u=p(u,n,m,d,t[a+3],10,-1894986606),d=p(d,u,n,m,t[a+10],15,-1051523),m=p(m,d,u,n,t[a+1],21,-2054922799),n=p(n,m,d,u,t[a+8],6,1873313359),u=p(u,n,m,d,t[a+15],10,-30611744),d=p(d,u,n,m,t[a+6],15,-1560198380),m=p(m,d,u,n,t[a+13],21,1309151649),n=p(n,m,d,u,t[a+4],6,-145523070),u=p(u,n,m,d,t[a+11],10,-1120210379),d=p(d,u,n,m,t[a+2],15,718787259),m=p(m,d,u,n,t[a+9],21,-343485551),n=e(n,b),m=e(m,T),d=e(d,_),u=e(u,I);return[n,m,d,u]}function P(t){var h,a="",b=t.length*32;for(h=0;h<b;h+=8)a+=String.fromCharCode(t[h>>5]>>>h%32&255);return a}function D(t){var h,a=[];for(a[(t.length>>2)-1]=void 0,h=0;h<a.length;h+=1)a[h]=0;var b=t.length*8;for(h=0;h<b;h+=8)a[h>>5]|=(t.charCodeAt(h/8)&255)<<h%32;return a}function y(t){return P(S(D(t),t.length*8))}function V(t,h){var a,b=D(t),T=[],_=[],I;for(T[15]=_[15]=void 0,b.length>16&&(b=S(b,t.length*8)),a=0;a<16;a+=1)T[a]=b[a]^909522486,_[a]=b[a]^1549556828;return I=S(T.concat(D(h)),512+h.length*8),P(S(_.concat(I),512+128))}function M(t){var h="0123456789abcdef",a="",b,T;for(T=0;T<t.length;T+=1)b=t.charCodeAt(T),a+=h.charAt(b>>>4&15)+h.charAt(b&15);return a}function O(t){return unescape(encodeURIComponent(t))}function U(t){return y(O(t))}function z(t){return M(U(t))}function H(t,h){return V(O(t),O(h))}function f(t,h){return M(H(t,h))}function $(t,h,a){return h?a?H(h,t):f(h,t):a?U(t):z(t)}typeof define=="function"&&define.amd?define(function(){return $}):typeof K=="object"&&K.exports?K.exports=$:c.md5=$})(te)});var Oe={};_e(Oe,{CDNStreaming:()=>X,PublishMode:()=>N,TRTCStreamType:()=>L,default:()=>Ae});var ae=Y(q(),1);var N=(o=>(o.PublishMainStreamToCDN="publish-main-stream-to-cdn",o.PublishSubStreamToCDN="publish-sub-stream-to-cdn",o.PublishMixStreamToCDN="publish-mix-stream-to-cdn",o.PublishMixStreamToRoom="publish-mix-stream-to-room",o))(N||{}),L=(r=>(r.Main="main",r.Sub="sub",r))(L||{});var W={IT_AUDIO_VIDEO:0,IT_PICTURE:2,IT_CANVAS:3,IT_PURE_AUDIO:4,IT_PURE_VIDEO:5};var E=class{constructor(e,r){g(this,"_core");g(this,"_room");g(this,"_log");g(this,"_params");g(this,"_publishGivenCDNData",null);this._core=e,this._room=e.room,this._log=r}get isPublishingGivenCDN(){return!!this._params}async startPublishGivenCDN(e){if(this._log.info(`[CDNStreaming] startPublishGivenCDN() current: ${JSON.stringify(this._params)}, params: ${JSON.stringify(e)}`),this.isPublishingGivenCDN){let{appId:r,bizId:i,url:o}=this._params||{};if(r===e.appId&&i===e.bizId&&o===e.url)return;await this.stopPublishGivenCDN()}this._params=e,this._publishGivenCDNData={pushRequestTime:Date.now(),pushAppId:e.appId,pushBizId:e.bizId,pushCdnUrl:e.url,pushStreamType:this.convertStreamType(e==null?void 0:e.publishMode),pushStreamId:e.streamId};try{let r=await this._room.sendStartPublishCDN(this._publishGivenCDNData,!1),{code:i,message:o}=r.data;if(i===0)this._log.info("[CDNStreaming] server success: start given cdn.");else throw this.resetGivenCDN(),this._log.error(`[CDNStreaming] server failed: start given cdn errCode: ${i} errMsg: ${o} options: ${JSON.stringify(e)}`),new Error(`[CDNStreaming] server failed: start given cdn errCode: ${i} errMsg: ${o}`)}catch(r){throw this.resetGivenCDN(),r}}async stopPublishGivenCDN(){if(this._log.info("[CDNStreaming] stopPublishGivenCDN"),!this.isPublishingGivenCDN||!this._publishGivenCDNData){this.resetGivenCDN();return}let{pushAppId:e,pushBizId:r,pushCdnUrl:i,pushStreamType:o,pushStreamId:l}=this._publishGivenCDNData,s={pushRequestTime:Date.now(),pushAppId:e,pushBizId:r,pushCdnUrl:i,pushStreamType:o,pushStreamId:l},p=await this._room.sendStopPublishCDN(s,!1),{code:S,message:P}=p.data;if(S===0)this._log.info("[CDNStreaming] server success: stop given cdn."),this.resetGivenCDN();else throw this._log.error(`[CDNStreaming] server failed: stop given cdn errCode: ${S} errMsg: ${P} data: ${JSON.stringify(s)}`),new Error(`[CDNStreaming] server failed: stop given cdn errCode: ${S} errMsg: ${P}`)}resetGivenCDN(){this._publishGivenCDNData=null,this._params=void 0}convertStreamType(e){return e==="publish-sub-stream-to-cdn"?"aux":"main"}};var v=class{constructor(e,r){g(this,"_core");g(this,"_room");g(this,"_log");g(this,"_config",null);g(this,"_data",null);g(this,"_givenCDNManager");this._core=e,this._room=e.room,this._log=r,this.reset()}get isMixing(){return!!this._data}get isStarted(){return!!this._config}get hasCustomCDN(){var e,r,i;return((e=this._config)==null?void 0:e.target.appId)&&((r=this._config)==null?void 0:r.target.bizId)&&((i=this._config)==null?void 0:i.target.url)}async startMixTranscode(e){if(this._log.info(`startMixTranscode: ${JSON.stringify(e)}`),this._config=e,this.installEvents(),!!this._core.room.isJoined)try{let r=this.getInputParam(e),i=this.getOutputParam(e),o=this.getOutputSessionId({config:e,roomId:this._room.roomId,userId:this._room.userId});this.isMixing&&this._data&&o!==this._data.outputSessionId&&(this._log.info("[CDNStreaming] streamId changed, auto stop mixing before start"),await this.doStopMixTranscode()),await this.doStartMixTranscode({outputSessionId:o,inputParam:r,outputParam:i});let{appId:l,bizId:s,url:p,streamId:S=""}=e.target;l&&s&&p&&(this._givenCDNManager||(this._givenCDNManager=new E(this._core,this._log)),await this._givenCDNManager.startPublishGivenCDN({publishMode:e.target.publishMode,appId:l,bizId:s,url:p,streamId:S}))}catch(r){throw this.reset(),r}}async doStartMixTranscode({outputSessionId:e,inputParam:r,outputParam:i}){let o={roomId:String(this._room.roomId),mcuRequestTime:Date.now(),outputSessionId:e,inputParam:r,outputParam:i};this._log.info(`[CDNStreaming] doStartMixTranscode: ${JSON.stringify(o)}`);let l=await this._room.sendStartMixTranscode(o),{code:s}=l.data,{message:p}=l.data;if(s===0)this._log.info("[CDNStreaming] server success: start mix"),this._data=o;else throw s===-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: ${s} errMsg: ${p}`),new Error(`[CDNStreaming] server failed: start mix errCode: ${s} 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 r=await this._room.sendStopMixTranscode(e),{code:i,message:o}=r.data;if(i===0)this._log.info("[CDNStreaming] server success: stop mix"),this.reset();else throw this._log.error(`[CDNStreaming] server failed: stop mix errCode: ${i} errMsg: ${o}`),new Error(`[CDNStreaming] server failed: stop mix errCode: ${i} errMsg: ${o}`)}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:r,roomId:i}){return this._core.utils.isString(e.target.streamId)&&e.target.streamId.length>0?e.target.streamId:(0,ae.default)(`${i}_${r}_main`)}getStringRoomId(e,r){return e?String(e):r}getInputParam(e){let{mix:r={}}=e,{audioMixUserList:i=[],videoLayoutList:o=[]}=r,l=o.map(s=>({userId:s.fixedVideoUser.userId,roomId:this.getStringRoomId(s.fixedVideoUser.roomId,s.fixedVideoUser.strRoomId)||this._core.room.roomId,width:s.width||0,height:s.height||0,locationX:s.locationX||0,locationY:s.locationY||0,zOrder:s.zOrder||1,streamType:s.fixedVideoStreamType==="sub"?1:0,inputType:W.IT_PURE_VIDEO,renderMode:s.fillMode||0}));return i.forEach(s=>{let p=l.findIndex(S=>S.userId===s.userId&&S.roomId===this.getStringRoomId(s.roomId,s.strRoomId));p!==-1?l[p].inputType=W.IT_AUDIO_VIDEO:l.push({userId:s.userId,roomId:s.roomId||s.strRoomId||this._core.room.roomId,inputType:W.IT_PURE_AUDIO})}),l}getOutputParam(e){let r=e.target.streamId||"",{encoding:i={},mix:o={}}=e;return{streamId:r,streamType:r.length>0?1:0,width:this._core.utils.isUndefined(i.videoWidth)?640:i.videoWidth,height:this._core.utils.isUndefined(i.videoHeight)?480:i.videoHeight,videoBps:i.videoBitrate||0,fps:i.videoFramerate||15,gop:i.videoGOP||2,audioSampleRate:i.audioSampleRate||48e3,audioBps:i.audioBitrate||64,audioChannels:i.audioChannels||1,backgroundColor:o.backgroundColor||0,backgroundImg:o.backgroundImage||"",extraInfo:"",videoCodec:2,audioCodec:0}}};var J=Y(q(),1);var k=class{constructor(e,r){g(this,"_room");g(this,"_core");g(this,"_log");g(this,"_paramsForTencentCDN");g(this,"_initParamsForTencentCDN",{isPublished:!1,isStarted:!1});this._core=e,this._room=e.room,this._log=r,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 o;if(e.room!==this._room||(this._log.info(`[CDNStreaming] handlePublished: mediaType ${e.mediaType}, roomID ${(o=e==null?void 0:e.room)==null?void 0:o.roomId}`),e.mediaType===1))return;let r=e.mediaType===4?"publish-main-stream-to-cdn":"publish-sub-stream-to-cdn",i=this._paramsForTencentCDN.get(r)||null;i!=null&&i.target&&i.isStarted&&await this.startPublishTencentCDN(i.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,r){let i=this._paramsForTencentCDN.get(e),o=C(C({},i),r);this._paramsForTencentCDN.set(e,o)}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 r=e.streamId||"",i=this.generatePublishCDNStreamId(r,e.publishMode),o=this.generatePublishCDNSessionId(e.publishMode),l=e.publishMode==="publish-sub-stream-to-cdn"?1:0,s={requestTime:Date.now(),sessionId:o,streamId:i,streamType:l};await this.doStartPublishTencentCDN(s,e.publishMode);let{appId:p,bizId:S,url:P}=e;if(p&&S&&P){let D=this._paramsForTencentCDN.get(e.publishMode)||this._initParamsForTencentCDN;D!=null&&D.givenCDNManager||(D.givenCDNManager=new E(this._core,this._log),this._paramsForTencentCDN.set(e.publishMode,D)),await D.givenCDNManager.startPublishGivenCDN({publishMode:e.publishMode,appId:p,bizId:S,url:P,streamId:i})}}async doStartPublishTencentCDN(e,r){this._log.info(`[CDNStreaming] doStartPublishTencentCDN: ${JSON.stringify(e)}`);let i=6,o=500,l=0;for(;;){let s=await this._room.sendStartPublishCDN(e,!0),{code:p}=s.data,{message:S}=s.data;if(p===0){this._log.info("[CDNStreaming] server success: start tencent cdn"),this.changeDataStatus(r,{isPublished:!0});break}else if(p===-10006&&l<i)this._log.warn(`[CDNStreaming] doStartPublishTencentCDN: retry...${l+1}/6, reason: ${S}`),l+=1,await new Promise(P=>setTimeout(P,o));else throw this.changeDataStatus(r,{isPublished:!1}),p===-102083&&(S=`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: ${S}`),new Error(`[CDNStreaming] server failed: start tencent cdn errCode: ${p} errMsg: ${S}`)}}async stopPublishTencentCDN(e){this._log.info(`[CDNStreaming] doStartPublishTencentCDN: ${JSON.stringify(e)}`);let r=this._paramsForTencentCDN.get(e)||this._initParamsForTencentCDN;if(r.isPublished){let{target:i}=r;i!=null&&i.bizId&&i.appId&&i.url&&(r!=null&&r.givenCDNManager)&&await(r==null?void 0:r.givenCDNManager.stopPublishGivenCDN()),await this.doStopPublishTencentCDN(e)}this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN)}async doStopPublishTencentCDN(e){let r={requestTime:Date.now(),sessionId:(0,J.default)(`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(e)}`)};this._log.info(`[CDNStreaming] doStopPublishTencentCDN: ${JSON.stringify(r)}`);let i=await this._room.sendStopPublishCDN(r,!0),{code:o}=i.data,{message:l}=i.data;if(o===0)this._log.info("[CDNStreaming] server success: stop tencent cdn"),this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN),this.reset(e);else throw o===-102069&&(this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN),l=`can not stop in auto relayed-push mode ${l}`),this._log.error(`[CDNStreaming] server failed: stop tencent cdn errCode: ${o} errMsg: ${l}`),new Error(`[CDNStreaming] server failed: stop tencent cdn errCode: ${o} errMsg: ${l}`)}reset(e){this.uninstallEvents(),this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN)}generatePublishCDNStreamId(e,r){if(e===""){let i=`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(r)}`;return/^[A-Za-z\d_-]*$/.test(i)||(i=(0,J.default)(i)),`${this._room.sdkAppId}_${i}`}return e}convertPublishModeToStreamType(e){return e==="publish-main-stream-to-cdn"?"main":"aux"}generatePublishCDNSessionId(e){return(0,J.default)(`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(e)}`)}};var ie=Y(q(),1);var w=class{constructor(e,r){g(this,"_core");g(this,"_room");g(this,"_log");g(this,"_seq");g(this,"_taskId",null);g(this,"_startData",null);g(this,"_updateData",null);g(this,"_stopData",null);this._core=e,this._room=e.room,this._log=r,this._seq=0}async startPushStreamToRoom(e){var l,s;this._seq+=1,this._startData=C({roomid:String(this._room.roomId),roomType:this._room.useStringRoomId?1:0,sessionId:(0,ie.default)(`${this._room.roomId}_${this._room.userId}_start`),agentParam:{cdnRobotUserid:`mcu_robot_${this._room.roomId}_${this._room.userId}`}},this.getCommonParams(e)),this._log.info(`startPushStreamToRoom: ${JSON.stringify(this._startData)}`);let r=await this._room.sendStartPushStreamToRoom(this._startData),{code:i,message:o}=r.data;i===0?(this._taskId=(s=(l=r.data)==null?void 0:l.data)==null?void 0:s.taskId,this._taskId||this.reportServerError("startPushStreamToRoom",i,`can't resolve task id: ${JSON.stringify(r.data)}`),this._log.info("[CDNStreaming] server success: taskId",this._taskId)):this.reportServerError("startPushStreamToRoom",i,o)}async updatePushStreamToRoom(e){this._seq+=1,this._updateData=F(C({taskid:this._taskId},this.getCommonParams(e)),{enableNtpSync:!0}),this._log.info(`updatePushStreamToRoom: ${JSON.stringify(this._updateData)}`);let r=await this._room.sendUpdatePushStreamToRoom(this._updateData),{code:i,message:o}=r.data;i===0?this._log.info("server success: updatePushStreamToRoom"):this.reportServerError("updatePushStreamToRoom",i,o)}async stopPushStreamToRoom(){this._seq+=1,this._stopData={sdkappid:this._room.sdkAppId,taskid:this._taskId},this._log.info(`stopPushStreamToRoom: ${JSON.stringify(this._stopData)}`);let e=await this._room.sendStopPushStreamToRoom(this._stopData),{code:r,message:i}=e.data;r===0?this._log.info("server success: start mix"):this.reportServerError("stopPushStreamToRoom",r,i)}reportServerError(e,r,i){let o=`server failed: ${e} errCode: ${r} errMsg: ${i}`;throw this._log.error(o),new Error(o)}getPushRtcRoomParams(e){let{robotUser:r}=e.target;return r?[{roomid:String((r==null?void 0:r.roomId)||(r==null?void 0:r.strRoomId))||this._room.roomId,roomType:r!=null&&r.roomId?0:1,pushRobotUserid:r==null?void 0:r.userId}]:[]}getCommonParams(e){return{sdkappid:this._room.sdkAppId,transcode:!0,audioParam:this.getAudioParam(e),videoParam:this.getVideoParam(e),pushRtcRoomParams:this.getPushRtcRoomParams(e),sequenceNumber:this._seq}}getAudioParam(e){let{mix:r={},encoding:i={}}=e,o={audioSamplerate:i.audioSampleRate||48e3,audioBitrateKbps:i.audioBitrate||64,audioChannels:i.audioChannels||1},{audioMixUserList:l}=r;return{audioEncodeParam:o,mixAudioUsers:(l==null?void 0:l.map(s=>({roomid:String(s.roomId||s.strRoomId)||this._room.roomId,userid:s.userId,roomType:s.roomId?0:1})))||[]}}getVideoParam(e){let{mix:r={},encoding:i={}}=e,o={videoCodec:2,videoWidth:i.videoWidth||640,videoHeight:i.videoHeight||480,videoFramerate:i.videoFramerate||15,videoGop:i.videoGOP||2,videoBitrateKbps:i.videoBitrate||800},{videoLayoutList:l}=r;return{videoEncodeParam:o,layoutParams:(l==null?void 0:l.map(s=>({userMediaStream:{userInfo:{roomid:String(s.fixedVideoUser.roomId||s.fixedVideoUser.strRoomId)||this._room.roomId,userid:s.fixedVideoUser.userId,roomType:s.fixedVideoUser.roomId?0:1},streamType:s.fixedVideoStreamType==="sub"?1:0},imageWidth:s.width||0,imageHeight:s.height||0,locationX:s.locationX||0,locationY:s.locationY||0,imageZorder:s.zOrder||1,renderMode:s.fillMode||0})))||[],backgroundColor:r.backgroundColor||0,backgroundImageUrl:r.backgroundImage||""}}};var oe=c=>typeof c=="string";var R;function Ie(c,e,r,i){if(!/^[A-Za-z\d_-]*$/.test(c)){let{RtcError:l,ErrorCode:s,ErrorCodeDictionary:p}=R;throw new l({code:s.INVALID_PARAMETER,extraCode:p.INVALID_STREAM_ID,messageParams:{key:"streamId"}})}}function fe(c,e,r,i){let{RtcError:o,ErrorCode:l,ErrorCodeDictionary:s}=R;if(oe(c))throw new o({code:l.INVALID_PARAMETER,extraCode:s.INVALID_ROOM_ID_INTEGER_STRING,fnName:r,messageParams:{key:"roomId"}});if(c===void 0)return;if(!(/^[1-9]\d*$/.test(String(c))&&c<4294967295))throw new o({code:l.INVALID_PARAMETER,extraCode:s.INVALID_ROOM_ID_INTEGER,fnName:r,messageParams:{key:"roomId"}})}function Re(c,e,r,i){let{RtcError:o,ErrorCode:l,ErrorCodeDictionary:s}=R;if(!/^[A-Za-z\d\s!#$%&()+\-:;<=.>?@[\]^_{}|~,]{1,64}$/.test(c))throw new o({code:l.INVALID_PARAMETER,extraCode:s.INVALID_ROOM_ID_STRING,fnName:r,messageParams:{key:"strRoomId"}})}var A={type:"number",notLessThanZero:!0},Z={type:"object",properties:{userId:{required:!0,type:"string"},roomId:{type:["string","number"],validate:fe},strRoomId:{type:"string",validate:Re}}},se={required:!0,properties:{publishMode:{required:!0,values:["publish-main-stream-to-cdn","publish-mix-stream-to-cdn","publish-sub-stream-to-cdn","publish-mix-stream-to-room"]},streamId:{required:!1,type:"string",validate:Ie},appId:{type:"number",allowEmpty:!1},bizId:{type:"number",allowEmpty:!1},url:{type:"string",allowEmpty:!1},robotUser:C({},Z)},validate(c,e,r,i){let{publishMode:o,robotUser:l}=c;if(o==="publish-mix-stream-to-room"&&!l){let{RtcError:s,ErrorCode:p}=R;throw new s({code:p.INVALID_PARAMETER,message:"Invalid parameter target, the value of publishMode is PublishMixStreamToRoom, robotUser is required."})}}},ne={required:!1,type:"object",properties:{videoWidth:A,videoHeight:A,videoBitrate:F(C({},A),{allowEmpty:!1}),videoFramerate:{type:"number",validate(c,e,r,i){if(c<=0||c>30){let{RtcError:o,ErrorCode:l}=R;throw new o({code:l.INVALID_PARAMETER,message:"Invalid parameter mixConfig -> videoFramerate, the value must be between (0, 30]."})}}},videoGOP:{type:"number",validate(c,e,r,i){if(c<1||c>8){let{RtcError:o,ErrorCode:l}=R;throw new o({code:l.INVALID_PARAMETER,message:"Invalid parameter mixConfig -> videoGOP, the value must be between [1, 8]."})}}},audioSampleRate:A,audioBitrate:{type:"number",validate(c,e,r,i){if(c<32||c>192){let{RtcError:o,ErrorCode:l}=R;throw new o({code:l.INVALID_PARAMETER,message:"Invalid parameter mixConfig -> audioBitrate, the value must be between [32, 192]."})}}},audioChannels:{type:"number",values:[1,2]}}},me={required:!1,type:"object",properties:{backgroundColor:{type:"number"},backgroundImage:{type:"string"},audioMixUserList:{type:"array",arrayItem:C({},Z)},videoLayoutList:{type:"array",required:!0,arrayItem:{type:"object",properties:{fixedVideoUser:C({},Z),fixedVideoStreamType:{type:"string",required:!0,values:["main","sub"]},fillMode:{type:"number",values:[0,1,2,4]},zOrder:{type:"number",required:!0,validate(c,e,r,i){if(c<1||c>15){let{RtcError:o,ErrorCode:l}=R;throw new o({code:l.INVALID_PARAMETER,message:"Invalid parameter mix -> videoLayoutList -> zOrder, the value must be between [1, 15]."})}}},width:A,height:A,locationX:A,locationY:A}}}}};function de(c){return R=c.errorModule,{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:C({},se),encoding:C({},ne),mix:C({},me)},validate(e,r,i,o){var S,P;let{publishMode:l}=e.target,{encoding:s,mix:p}=e;if(l==="publish-mix-stream-to-cdn"){let{RtcError:D,ErrorCode:y,ErrorCodeDictionary:V}=c.errorModule;if(!s)throw new D({code:y.INVALID_PARAMETER,extraCode:V.INVALID_PARAMETER_REQUIRED,fnName:i,messageParams:{key:"encoding"}});if(!p)throw new D({code:y.INVALID_PARAMETER,extraCode:V.INVALID_PARAMETER_REQUIRED,fnName:i,messageParams:{key:"mix"}});if(p&&p.videoLayoutList){let M=0,O=0,U=[];if(p.videoLayoutList.forEach((f,$)=>{U.push(f.fixedVideoUser.userId),f.width+f.locationX>M&&(M=f.width+f.locationX),f.height+f.locationY>O&&(O=f.height+f.locationY)}),U.indexOf(c.room.userId)<0)throw new D({code:y.INVALID_PARAMETER,message:"Invalid parameter mix -> videoLayoutList, the value must be include self."});let z=(S=s==null?void 0:s.videoWidth)!=null?S:640,H=(P=s==null?void 0:s.videoHeight)!=null?P:480;if(z<M||H<O)throw new D({code:y.INVALID_PARAMETER,message:"Invalid parameter encoding, the width and height of the mixed video must encompass all the mixed-in video streams."})}}}}}function ue(c){return R=c.errorModule,{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:C({},se),encoding:C({},ne),mix:C({},me)}}}function le(c){return R=c.errorModule,{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:{required:!0,properties:{publishMode:{required:!0,values:["publish-main-stream-to-cdn","publish-mix-stream-to-cdn","publish-sub-stream-to-cdn","publish-mix-stream-to-room"]}}}}}}var G=0,B=class B{constructor(e){this.core=e;g(this,"_mixTranscodeManager");g(this,"_publishCDNManager");g(this,"_pushStreamToRoomManager");g(this,"_core");g(this,"_modeOptions");g(this,"seq");g(this,"_log");G=G+1,this.seq=G,this._log=e.log.createChild({id:`${this.getAlias()}${G}`}),this._log.info(`[CDNStreaming] created id=${this.getAlias()}${G}`),this._core=e,this._modeOptions=new Map,this._mixTranscodeManager=new v(e,this._log),this._publishCDNManager=new k(e,this._log),this._pushStreamToRoomManager=new w(e,this._log)}getName(){return B.Name}getAlias(){return"cdn"}getValidateRule(e){switch(e){case"start":return de(this._core);case"update":return ue(this._core);case"stop":return le(this._core)}}getGroup(e){return e.target.publishMode}async start(e){return this._modeOptions.set(e.target.publishMode,e),e.target.publishMode==="publish-mix-stream-to-room"?this._pushStreamToRoomManager.startPushStreamToRoom(e):await this.doStart(e)}async update(e){let r=this._modeOptions.get(e.target.publishMode);return this._core.utils.deepMerge(r,e),e.target.publishMode==="publish-mix-stream-to-room"?this._pushStreamToRoomManager.updatePushStreamToRoom(r):await this.doStart(r)}async stop(e){switch(e.target.publishMode){case"publish-mix-stream-to-cdn":{await this._mixTranscodeManager.stopMixTranscode();break}case"publish-main-stream-to-cdn":case"publish-sub-stream-to-cdn":{await this._publishCDNManager.stopPublishTencentCDN(e.target.publishMode);break}case"publish-mix-stream-to-room":{this._pushStreamToRoomManager.stopPushStreamToRoom();break}}this._modeOptions.delete(e.target.publishMode)}async doStart(e){switch(this._log.info(`[CDNStreaming] doStart: ${JSON.stringify(e)}`),e.target.publishMode){case"publish-mix-stream-to-cdn":{await this._mixTranscodeManager.startMixTranscode(e);break}case"publish-main-stream-to-cdn":case"publish-sub-stream-to-cdn":{await this._publishCDNManager.startPublishTencentCDN(e.target);break}}}};g(B,"TYPE",{PublishMode:{PublishMainStreamToCDN:"publish-main-stream-to-cdn",PublishSubStreamToCDN:"publish-sub-stream-to-cdn",PublishMixStreamToCDN:"publish-mix-stream-to-cdn",PublishMixStreamToRoom:"publish-mix-stream-to-room"}}),g(B,"Name","CDNStreaming");var X=B;var Ae=X;return De(Oe);})().default;
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rtc-plugin/cdn-streaming",
3
- "version": "5.8.3-beta.4",
3
+ "version": "5.8.3-ktv.1",
4
4
  "description": "TRTC Web SDK 5.x CDN streaming plugin",
5
5
  "main": "./cdn-streaming.esm.js",
6
6
  "author": "longyuqi <longyuqi@tencent.com>",
@@ -0,0 +1,13 @@
1
+ export interface StartChorusOption {
2
+ useOriginalSong?: boolean;
3
+ }
4
+
5
+ export interface UpdateChorusOption {
6
+ useOriginalSong?: boolean;
7
+ bgmAction?: 'pause' | 'resume'
8
+ }
9
+ export declare class Chorus {
10
+ start(option: any): Promise<void>;
11
+ update(option: any): Promise<void>;
12
+ stop(option?: any): Promise<void>;
13
+ }