trtc-sdk-v5 5.1.1-beta.1 → 5.1.2

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.
package/README-zh_CN.md CHANGED
@@ -1,10 +1,10 @@
1
1
  <p align="center">
2
2
  <a href="https://cloud.tencent.com/document/product/647">
3
- <img width="200" src="https://web.sdk.qcloud.com/trtc/webrtc/assets/trtc-logo.png">
3
+ <img width="200" src="https://web.sdk.qcloud.com/trtc/webrtc/assets/trtc.io-logo.png">
4
4
  </a>
5
5
  </p>
6
6
 
7
- <h1 align="center">TRTC Web SDK</h1>
7
+ <h1 align="center" style="margin-top: -40px">TRTC Web SDK</h1>
8
8
 
9
9
  <div align="center">
10
10
 
@@ -14,7 +14,8 @@
14
14
 
15
15
  </div>
16
16
 
17
- 简体中文 | [English](./README.md)
17
+ <div align="center"> 简体中文 | <a href="https://github.com/LiteAVSDK/TRTC_Web/blob/main/v5/SDK/README.md" target="_blank"> English </a> </div>
18
+
18
19
  ## 简介
19
20
 
20
21
  [TRTC Web SDK](https://web.sdk.qcloud.com/trtc/webrtc/v5/doc/zh-cn/index.html) 是腾讯云实时音视频通讯解决方案的 Web 端 SDK,它是通过 HTML 网页加载的 JavaScript 库。Web 开发者可以使用 TRTC Web SDK 提供的 API,在您的业务网站上实现实时音视频通话、直播等功能。
package/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  <p align="center">
2
- <a href="https://intl.cloud.tencent.com/products/trtc">
3
- <img width="200" src="https://web.sdk.qcloud.com/trtc/webrtc/assets/trtc-logo.png">
2
+ <a href="https://trtc.io/">
3
+ <img width="200" src="https://web.sdk.qcloud.com/trtc/webrtc/assets/trtc.io-logo.png">
4
4
  </a>
5
5
  </p>
6
6
 
7
- <h1 align="center">TRTC Web SDK</h1>
7
+ <h1 align="center" style="margin-top: -40px">TRTC Web SDK</h1>
8
8
 
9
9
  <div align="center">
10
10
 
@@ -14,13 +14,13 @@ An object-oriented WebRTC SDK library
14
14
 
15
15
  </div>
16
16
 
17
- English | [简体中文](https://github.com/LiteAVSDK/TRTC_Web/blob/main/v5/SDK/README-zh_CN.md)
17
+ <div align="center"> English | <a href="https://github.com/LiteAVSDK/TRTC_Web/blob/main/v5/SDK/README-zh_CN.md" target="_blank"> 简体中文</a> </div>
18
18
 
19
19
  ## Introduction
20
20
 
21
21
  TRTC Web SDK is an object-oriented WebRTC SDK of Tencent Cloud's real-time communication solution. Web developers can use TRTC Web SDK to establish an audio/video calls or live streaming services on your website.
22
22
 
23
- - [Online Demo](https://trtc.tencentcloud.com/demo/homepage/#/home)
23
+ - [Online Demo](https://trtc.io/demo/homepage/#/home)
24
24
 
25
25
  ## Environment Supports
26
26
 
package/index.d.ts CHANGED
@@ -263,6 +263,42 @@ declare const TRTCType: {
263
263
  */
264
264
  readonly QOS_PREFERENCE_CLEAR: "clear";
265
265
  };
266
+ declare interface AudioMixerOptions {
267
+ id: string;
268
+ url: string;
269
+ loop?: boolean;
270
+ volume?: number;
271
+ }
272
+ declare interface UpdateAudioMixerOptions {
273
+ id: string;
274
+ loop?: boolean;
275
+ volume?: number;
276
+ seekFrom?: number;
277
+ operation?: 'pause' | 'resume' | 'stop';
278
+ }
279
+ declare interface StopAudioMixerOptions {
280
+ id: string;
281
+ }
282
+ declare interface AIDenoiserOptions {
283
+ assetsPath: string;
284
+ sdkAppId: number;
285
+ userId: string;
286
+ userSig: string;
287
+ }
288
+ declare type PluginStartOptionsMap = {
289
+ 'AudioMixer': AudioMixerOptions;
290
+ 'AIDenoiser': AIDenoiserOptions;
291
+ 'CDNStreaming': CDNStreamingOptions;
292
+ };
293
+ declare type PluginUpdateOptionsMap = {
294
+ 'AudioMixer': UpdateAudioMixerOptions;
295
+ 'CDNStreaming': CDNStreamingOptions;
296
+ };
297
+ declare type PluginStopOptionsMap = {
298
+ 'AudioMixer': StopAudioMixerOptions;
299
+ 'AIDenoiser': undefined;
300
+ 'CDNStreaming': CDNStreamingOptions;
301
+ };
266
302
 
267
303
  /**
268
304
  * **TRTC事件列表**<br>
@@ -892,41 +928,46 @@ declare interface TRTCEventTypes {
892
928
  * @typedef {object|string} VideoProfile - 本地视频流配置
893
929
  *
894
930
  * 视频配置参数,可以用字符串预设值或者自定义分辨率等参数
895
- * | 视频 Profile | 分辨率(宽 x 高)| 帧率(fps)| 码率(kbps)| 备注 |
896
- * | :--- | :--- | :--- | :--- | :--- |
897
- * | 120p | 160 x 120 | 15 | 200 ||
898
- * | 180p | 320 x 180 | 15 | 350 ||
899
- * | 240p | 320 x 240 | 15 | 400 ||
900
- * | 360p | 640 x 360 | 15 | 800 ||
901
- * | 480p | 640 x 480 | 15 | 900 ||
902
- * | 720p | 1280 x 720 | 15 | 1500 ||
903
- * | 1080p | 1920 x 1080 | 15 | 2000 ||
904
- * | 1440p | 2560 x 1440 | 30 | 4860 ||
905
- * | 4K | 3840 x 2160 | 30 | 9000 ||
931
+ * | 视频 Profile | 分辨率(宽 x 高)| 帧率(fps)| 码率(kbps)| 备注 |
932
+ * | :--- | :--- | :--- | :--- | :--- |
933
+ * | 120p | 160 x 120 | 15 | 200 ||
934
+ * | 120p_2 | 160 x 120 | 15 | 100 | v5.1.1+ 支持 |
935
+ * | 180p | 320 x 180 | 15 | 350 ||
936
+ * | 180p_2 | 320 x 180 | 15 | 150 | v5.1.1+ 支持 |
937
+ * | 240p | 320 x 240 | 15 | 400 ||
938
+ * | 240p_2 | 320 x 240 | 15 | 200 | v5.1.1+ 支持 |
939
+ * | 360p | 640 x 360 | 15 | 800 ||
940
+ * | 360p_2 | 640 x 360 | 15 | 400 | v5.1.1+ 支持 |
941
+ * | 480p | 640 x 480 | 15 | 900 ||
942
+ * | 480p_2 | 640 x 480 | 15 | 500 | v5.1.1+ 支持 |
943
+ * | 720p | 1280 x 720 | 15 | 1500 ||
944
+ * | 1080p | 1920 x 1080 | 15 | 2000 ||
945
+ * | 1440p | 2560 x 1440 | 30 | 4860 ||
946
+ * | 4K | 3840 x 2160 | 30 | 9000 ||
906
947
  * @property {number} width - 视频宽度
907
948
  * @property {number} height - 视频高度
908
949
  * @property {number} frameRate - 视频帧率
909
950
  * @property {number} bitrate - 视频码率
910
- * @example
911
- * const config = {
912
- * option: {
913
- * profile: '480p',
914
- * },
915
- * }
916
- * await trtc.startLocalVideo(config);
917
- * @example
918
- * const config = {
919
- * option: {
920
- * profile: {
921
- * width: 640,
922
- * height: 480,
923
- * frameRate: 15,
924
- * bitrate: 900,
925
- * }
926
- * }
927
- * }
928
- * await trtc.startLocalVideo(config);
929
- */
951
+ * @example
952
+ * const config = {
953
+ * option: {
954
+ * profile: '480p_2',
955
+ * },
956
+ * }
957
+ * await trtc.startLocalVideo(config);
958
+ * @example
959
+ * const config = {
960
+ * option: {
961
+ * profile: {
962
+ * width: 640,
963
+ * height: 480,
964
+ * frameRate: 15,
965
+ * bitrate: 500,
966
+ * }
967
+ * }
968
+ * }
969
+ * await trtc.startLocalVideo(config);
970
+ */
930
971
  /**
931
972
  * 开启本地摄像头采集,在您指定的 HTMLElement 标签下播放摄像头画面,并将摄像头画面发布到当前所在房间中。
932
973
  * - 调用时机:进房前后均可调用,不可重复调用。
@@ -1206,33 +1247,47 @@ declare interface TRTCEventTypes {
1206
1247
  /**
1207
1248
  * 开启插件
1208
1249
  *
1209
- * 目前支持的插件:
1210
- *
1211
1250
  * | pluginName | 插件名称 | 参考教程 | 参数类型 |
1212
1251
  * | --- | --- | --- | --- |
1213
- * | 'AudioMixer' | 背景音乐插件 | {@tutorial 22-advanced-audio-mixer} | [AudioMixerOptions](./global.html#AudioMixerOptions) |
1214
- * | 'AIDenoiser' | 降噪插件 | {@tutorial 35-advanced-ai-denoiser} | [AIDenoiserOptions](./global.html#AIDenoiserOptions) |
1215
- * | 'CDNStreaming' | CDN混流插件 | {@tutorial 26-advanced-publish-cdn-stream} | [CDNStreamingOptions](./global.html#CDNStreamingOptions) |
1252
+ * | 'AudioMixer' | 背景音乐插件 | {@tutorial 22-advanced-audio-mixer} | [AudioMixerOptions](https://web.sdk.qcloud.com/trtc/webrtc/v5/doc/zh-cn/global.html#AudioMixerOptions) |
1253
+ * | 'AIDenoiser' | 降噪插件 | {@tutorial 35-advanced-ai-denoiser} | [AIDenoiserOptions](https://web.sdk.qcloud.com/trtc/webrtc/v5/doc/zh-cn/global.html#AIDenoiserOptions) |
1254
+ * | 'CDNStreaming' | CDN混流插件 | {@tutorial 26-advanced-publish-cdn-stream} | [CDNStreamingOptions](https://web.sdk.qcloud.com/trtc/webrtc/v5/doc/zh-cn/global.html#CDNStreamingOptions) |
1216
1255
  *
1217
1256
  * @param {PluginName} plugin
1218
1257
  * @param {AudioMixerOptions|AIDenoiserOptions|CDNStreamingOptions} options
1219
- * @returns {Promise<any>}
1258
+ * @returns {Promise<void>}
1220
1259
  */
1221
- startPlugin(plugin: 'AudioMixer' | 'AIDenoiser' | 'CDNStreaming', options: any): Promise<void>;
1260
+ startPlugin<T extends keyof PluginStartOptionsMap, O extends PluginStartOptionsMap[T]>(plugin: O extends undefined ? never : T, options: O): Promise<any>;
1261
+ startPlugin<T extends keyof PluginStartOptionsMap, O extends PluginStartOptionsMap[T]>(plugin: O extends undefined ? T : never): Promise<any>;
1222
1262
  /**
1223
1263
  * 更新插件
1264
+ *
1265
+ * | pluginName | 插件名称 | 参考教程 | 参数类型 |
1266
+ * | --- | --- | --- | --- |
1267
+ * | 'AudioMixer' | 背景音乐插件 | {@tutorial 22-advanced-audio-mixer} | [UpdateAudioMixerOptions](https://web.sdk.qcloud.com/trtc/webrtc/v5/doc/zh-cn/global.html#UpdateAudioMixerOptions) |
1268
+ * | 'CDNStreaming' | CDN混流插件 | {@tutorial 26-advanced-publish-cdn-stream} | [CDNStreamingOptions](https://web.sdk.qcloud.com/trtc/webrtc/v5/doc/zh-cn/global.html#CDNStreamingOptions) |
1269
+ *
1224
1270
  * @param {PluginName} plugin
1225
1271
  * @param {UpdateAudioMixerOptions|CDNStreamingOptions} options
1226
- * @returns {Promise<any>}
1272
+ * @returns {Promise<void>}
1227
1273
  */
1228
- updatePlugin(plugin: 'AudioMixer' | 'CDNStreaming', options: any): Promise<void>;
1274
+ updatePlugin<T extends keyof PluginUpdateOptionsMap, O extends PluginUpdateOptionsMap[T]>(plugin: O extends undefined ? never : T, options: O): Promise<any>;
1275
+ updatePlugin<T extends keyof PluginUpdateOptionsMap, O extends PluginUpdateOptionsMap[T]>(plugin: O extends undefined ? T : never): Promise<any>;
1229
1276
  /**
1230
1277
  * 停止插件
1278
+ *
1279
+ * | pluginName | 插件名称 | 参考教程 | 参数类型 |
1280
+ * | --- | --- | --- | --- |
1281
+ * | 'AudioMixer' | 背景音乐插件 | {@tutorial 22-advanced-audio-mixer} | [StopAudioMixerOptions](https://web.sdk.qcloud.com/trtc/webrtc/v5/doc/en/global.html#StopAudioMixerOptions) |
1282
+ * | 'AIDenoiser' | 降噪插件 | {@tutorial 35-advanced-ai-denoiser} | |
1283
+ * | 'CDNStreaming' | CDN混流插件 | {@tutorial 26-advanced-publish-cdn-stream} | [CDNStreamingOptions](https://web.sdk.qcloud.com/trtc/webrtc/v5/doc/en/global.html#CDNStreamingOptions) |
1284
+ *
1231
1285
  * @param {PluginName} plugin
1232
- * @param {StopAudioMixerOptions|CDNStreamingOptions} options
1233
- * @returns {Promise<any>}
1286
+ * @param {StopAudioMixerOptions|StopAIDenoiserOptions|CDNStreamingOptions} options
1287
+ * @returns {Promise<void>}
1234
1288
  */
1235
- stopPlugin(plugin: 'AudioMixer' | 'AIDenoiser' | 'CDNStreaming', options: any): Promise<void>;
1289
+ stopPlugin<T extends keyof PluginStopOptionsMap, O extends PluginStopOptionsMap[T]>(plugin: O extends undefined ? never : T, options: O): Promise<any>;
1290
+ stopPlugin<T extends keyof PluginStopOptionsMap, O extends PluginStopOptionsMap[T]>(plugin: O extends undefined ? T : never): Promise<any>;
1236
1291
  /**
1237
1292
  * 开启或关闭音量大小回调<br>
1238
1293
  *
package/package.json CHANGED
@@ -1,14 +1,10 @@
1
1
  {
2
2
  "name": "trtc-sdk-v5",
3
- "version": "5.1.1-beta.1",
3
+ "version": "5.1.2",
4
4
  "description": "Tencent Cloud RTC SDK for Web",
5
5
  "main": "trtc.js",
6
- "module": "trtc.esm.js",
7
6
  "types": "index.d.ts",
8
7
  "dependencies": {
9
- "afsm": "2.4.1",
10
- "eventemitter3": "^4.0.7",
11
- "sdp-transform": "^2.8.0",
12
8
  "webrtc-adapter": "^8.2.3"
13
9
  },
14
10
  "keywords": [
@@ -1,10 +1,10 @@
1
- declare interface CDNStreamingOptions {
1
+ export declare interface CDNStreamingOptions {
2
2
  target: Target;
3
3
  encoding?: Encoding;
4
4
  mix?: Mix;
5
5
  }
6
6
 
7
- declare interface Target {
7
+ export declare interface Target {
8
8
  publishMode: PublishMode;
9
9
  streamId?: string;
10
10
  appId?: number;
@@ -12,7 +12,7 @@ declare interface Target {
12
12
  url?: string;
13
13
  }
14
14
 
15
- declare interface Encoding {
15
+ export declare interface Encoding {
16
16
  videoWidth?: number;
17
17
  videoHeight?: number;
18
18
  videoBitrate?: number;
@@ -23,20 +23,20 @@ declare interface Encoding {
23
23
  audioChannels?: 1 | 2;
24
24
  }
25
25
 
26
- declare interface Mix {
26
+ export declare interface Mix {
27
27
  backgroundColor?: number;
28
28
  backgroundImage?: string;
29
29
  audioMixUserList?: User[];
30
30
  videoLayoutList?: VideoLayout[];
31
31
  }
32
32
 
33
- declare interface User {
33
+ export declare interface User {
34
34
  userId: string;
35
35
  roomId: number;
36
36
  strRoomId: string;
37
37
  }
38
38
 
39
- declare interface VideoLayout {
39
+ export declare interface VideoLayout {
40
40
  fixedVideoUser: User;
41
41
  fixedVideoStreamType?: TRTCStreamType;
42
42
  fillMode?: 0 | 1 | 2;
@@ -47,18 +47,18 @@ declare interface VideoLayout {
47
47
  zOrder?: number;
48
48
  }
49
49
 
50
- declare enum TRTCStreamType {
50
+ export declare enum TRTCStreamType {
51
51
  Main = 'main',
52
52
  Sub = 'sub'
53
53
  }
54
54
 
55
- declare enum PublishMode {
55
+ export declare enum PublishMode {
56
56
  PublishMainStreamToCDN = 'PublishMainStreamToCDN',
57
57
  PublishSubStreamToCDN = 'PublishSubStreamToCDN',
58
58
  PublishMixStreamToCDN = 'PublishMixStreamToCDN'
59
59
  }
60
60
 
61
- declare class CDNStreaming {
61
+ export declare class CDNStreaming {
62
62
  static TYPE: {
63
63
  PublishMode: {
64
64
  PublishMainStreamToCDN: PublishMode.PublishMainStreamToCDN;
@@ -72,6 +72,5 @@ declare class CDNStreaming {
72
72
  stop(options: CDNStreamingOptions): Promise<void>;
73
73
  }
74
74
 
75
- export { PublishMode, TRTCStreamType };
76
75
  export { CDNStreaming };
77
76
  export default CDNStreaming;
@@ -1 +1 @@
1
- var ne=Object.create;var H=Object.defineProperty,ue=Object.defineProperties,de=Object.getOwnPropertyDescriptor,me=Object.getOwnPropertyDescriptors,le=Object.getOwnPropertyNames,q=Object.getOwnPropertySymbols,he=Object.getPrototypeOf,X=Object.prototype.hasOwnProperty,ce=Object.prototype.propertyIsEnumerable;var $=(l,e,i)=>e in l?H(l,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):l[e]=i,I=(l,e)=>{for(var i in e||(e={}))X.call(e,i)&&$(l,i,e[i]);if(q)for(var i of q(e))ce.call(e,i)&&$(l,i,e[i]);return l},W=(l,e)=>ue(l,me(e));var pe=(l,e)=>()=>(e||l((e={exports:{}}).exports,e),e.exports);var ge=(l,e,i,u)=>{if(e&&typeof e=="object"||typeof e=="function")for(let d of le(e))!X.call(l,d)&&d!==i&&H(l,d,{get:()=>e[d],enumerable:!(u=de(e,d))||u.enumerable});return l};var z=(l,e,i)=>(i=l!=null?ne(he(l)):{},ge(e||!l||!l.__esModule?H(i,"default",{value:l,enumerable:!0}):i,l));var b=(l,e,i)=>($(l,typeof e!="symbol"?e+"":e,i),i);var V=pe((j,x)=>{(function(l){"use strict";function e(t,h){var r=(t&65535)+(h&65535),g=(t>>16)+(h>>16)+(r>>16);return g<<16|r&65535}function i(t,h){return t<<h|t>>>32-h}function u(t,h,r,g,T,C){return e(i(e(e(h,t),e(g,C)),T),r)}function d(t,h,r,g,T,C,f){return u(h&r|~h&g,t,h,T,C,f)}function c(t,h,r,g,T,C,f){return u(h&g|r&~g,t,h,T,C,f)}function m(t,h,r,g,T,C,f){return u(h^r^g,t,h,T,C,f)}function p(t,h,r,g,T,C,f){return u(r^(h|~g),t,h,T,C,f)}function S(t,h){t[h>>5]|=128<<h%32,t[(h+64>>>9<<4)+14]=h;var r,g,T,C,f,o=1732584193,a=-271733879,s=-1732584194,n=271733878;for(r=0;r<t.length;r+=16)g=o,T=a,C=s,f=n,o=d(o,a,s,n,t[r],7,-680876936),n=d(n,o,a,s,t[r+1],12,-389564586),s=d(s,n,o,a,t[r+2],17,606105819),a=d(a,s,n,o,t[r+3],22,-1044525330),o=d(o,a,s,n,t[r+4],7,-176418897),n=d(n,o,a,s,t[r+5],12,1200080426),s=d(s,n,o,a,t[r+6],17,-1473231341),a=d(a,s,n,o,t[r+7],22,-45705983),o=d(o,a,s,n,t[r+8],7,1770035416),n=d(n,o,a,s,t[r+9],12,-1958414417),s=d(s,n,o,a,t[r+10],17,-42063),a=d(a,s,n,o,t[r+11],22,-1990404162),o=d(o,a,s,n,t[r+12],7,1804603682),n=d(n,o,a,s,t[r+13],12,-40341101),s=d(s,n,o,a,t[r+14],17,-1502002290),a=d(a,s,n,o,t[r+15],22,1236535329),o=c(o,a,s,n,t[r+1],5,-165796510),n=c(n,o,a,s,t[r+6],9,-1069501632),s=c(s,n,o,a,t[r+11],14,643717713),a=c(a,s,n,o,t[r],20,-373897302),o=c(o,a,s,n,t[r+5],5,-701558691),n=c(n,o,a,s,t[r+10],9,38016083),s=c(s,n,o,a,t[r+15],14,-660478335),a=c(a,s,n,o,t[r+4],20,-405537848),o=c(o,a,s,n,t[r+9],5,568446438),n=c(n,o,a,s,t[r+14],9,-1019803690),s=c(s,n,o,a,t[r+3],14,-187363961),a=c(a,s,n,o,t[r+8],20,1163531501),o=c(o,a,s,n,t[r+13],5,-1444681467),n=c(n,o,a,s,t[r+2],9,-51403784),s=c(s,n,o,a,t[r+7],14,1735328473),a=c(a,s,n,o,t[r+12],20,-1926607734),o=m(o,a,s,n,t[r+5],4,-378558),n=m(n,o,a,s,t[r+8],11,-2022574463),s=m(s,n,o,a,t[r+11],16,1839030562),a=m(a,s,n,o,t[r+14],23,-35309556),o=m(o,a,s,n,t[r+1],4,-1530992060),n=m(n,o,a,s,t[r+4],11,1272893353),s=m(s,n,o,a,t[r+7],16,-155497632),a=m(a,s,n,o,t[r+10],23,-1094730640),o=m(o,a,s,n,t[r+13],4,681279174),n=m(n,o,a,s,t[r],11,-358537222),s=m(s,n,o,a,t[r+3],16,-722521979),a=m(a,s,n,o,t[r+6],23,76029189),o=m(o,a,s,n,t[r+9],4,-640364487),n=m(n,o,a,s,t[r+12],11,-421815835),s=m(s,n,o,a,t[r+15],16,530742520),a=m(a,s,n,o,t[r+2],23,-995338651),o=p(o,a,s,n,t[r],6,-198630844),n=p(n,o,a,s,t[r+7],10,1126891415),s=p(s,n,o,a,t[r+14],15,-1416354905),a=p(a,s,n,o,t[r+5],21,-57434055),o=p(o,a,s,n,t[r+12],6,1700485571),n=p(n,o,a,s,t[r+3],10,-1894986606),s=p(s,n,o,a,t[r+10],15,-1051523),a=p(a,s,n,o,t[r+1],21,-2054922799),o=p(o,a,s,n,t[r+8],6,1873313359),n=p(n,o,a,s,t[r+15],10,-30611744),s=p(s,n,o,a,t[r+6],15,-1560198380),a=p(a,s,n,o,t[r+13],21,1309151649),o=p(o,a,s,n,t[r+4],6,-145523070),n=p(n,o,a,s,t[r+11],10,-1120210379),s=p(s,n,o,a,t[r+2],15,718787259),a=p(a,s,n,o,t[r+9],21,-343485551),o=e(o,g),a=e(a,T),s=e(s,C),n=e(n,f);return[o,a,s,n]}function _(t){var h,r="",g=t.length*32;for(h=0;h<g;h+=8)r+=String.fromCharCode(t[h>>5]>>>h%32&255);return r}function D(t){var h,r=[];for(r[(t.length>>2)-1]=void 0,h=0;h<r.length;h+=1)r[h]=0;var g=t.length*8;for(h=0;h<g;h+=8)r[h>>5]|=(t.charCodeAt(h/8)&255)<<h%32;return r}function M(t){return _(S(D(t),t.length*8))}function U(t,h){var r,g=D(t),T=[],C=[],f;for(T[15]=C[15]=void 0,g.length>16&&(g=S(g,t.length*8)),r=0;r<16;r+=1)T[r]=g[r]^909522486,C[r]=g[r]^1549556828;return f=S(T.concat(D(h)),512+h.length*8),_(S(C.concat(f),512+128))}function E(t){var h="0123456789abcdef",r="",g,T;for(T=0;T<t.length;T+=1)g=t.charCodeAt(T),r+=h.charAt(g>>>4&15)+h.charAt(g&15);return r}function R(t){return unescape(encodeURIComponent(t))}function P(t){return M(R(t))}function F(t){return E(P(t))}function J(t,h){return U(R(t),R(h))}function se(t,h){return E(J(t,h))}function B(t,h,r){return h?r?J(h,t):se(h,t):r?P(t):F(t)}typeof define=="function"&&define.amd?define(function(){return B}):typeof x=="object"&&x.exports?x.exports=B:l.md5=B})(j)});var Y=z(V(),1);var A=(u=>(u.PublishMainStreamToCDN="publish-main-stream-to-cdn",u.PublishSubStreamToCDN="publish-sub-stream-to-cdn",u.PublishMixStreamToCDN="publish-mix-stream-to-cdn",u))(A||{}),K=(i=>(i.Main="main",i.Sub="sub",i))(K||{});var v={IT_AUDIO_VIDEO:0,IT_PICTURE:2,IT_CANVAS:3,IT_PURE_AUDIO:4,IT_PURE_VIDEO:5};var N=class{constructor(e){b(this,"_room");b(this,"_log");b(this,"_paramsForGivenCDN");b(this,"_publishGivenCDNData",null);b(this,"_isPublishingGivenCDN",!1);this._room=e.room,this._log=e.log,this._paramsForGivenCDN=new Map([["publish-main-stream-to-cdn",null],["publish-sub-stream-to-cdn",null],["publish-mix-stream-to-cdn",null]])}async startPublishGivenCDN(e){this._log.info(`startPublishGivenCDN() ${JSON.stringify(e)}`),this._paramsForGivenCDN.set(e.publishMode,e);let i={pushRequestTime:Date.now(),pushAppId:e.appId,pushBizId:e.bizId,pushCdnUrl:e.url,pushStreamType:this.convertStreamType(e==null?void 0:e.publishMode),pushStreamId:e.streamId};this._publishGivenCDNData=i;try{this._isPublishingGivenCDN&&await this.stopPublishGivenCDN();let u=await this._room.sendStartPublishCDN(i,!1),{code:d,message:c}=u.data;if(d===0)this._isPublishingGivenCDN=!0;else throw this.resetGivenCDN(),this._log.error(`startPublishGivenCDN failed, errCode: ${d}, errMsg: ${c}`),new Error(`startPublishGivenCDN failed, errCode: ${d} errMsg: ${c}`)}catch(u){throw this.resetGivenCDN(),u}}async stopPublishGivenCDN(){if(this._log.info(`stopPublishGivenCDN: ${JSON.stringify(this._publishGivenCDNData)}`),!this._isPublishingGivenCDN||!this._publishGivenCDNData){this.resetGivenCDN();return}let{pushAppId:e,pushBizId:i,pushCdnUrl:u,pushStreamType:d,pushStreamId:c}=this._publishGivenCDNData,m={pushRequestTime:Date.now(),pushAppId:e,pushBizId:i,pushCdnUrl:u,pushStreamType:d,pushStreamId:c},p=await this._room.sendStopPublishCDN(m,!1),{code:S,message:_}=p.data;if(S===0)this.resetGivenCDN();else throw this._log.error(`stopPublishGivenCDN failed, errCode: ${S} errMsg: ${_}`),new Error(`stopPublishGivenCDN failed, errCode: ${S} errMsg: ${_}`)}resetGivenCDN(){this._publishGivenCDNData=null,this._isPublishingGivenCDN=!1}convertStreamType(e){return e==="publish-main-stream-to-cdn"?"main":"aux"}};var y=class{constructor(e,i){b(this,"_core");b(this,"_room");b(this,"cdnLog");b(this,"_config",null);b(this,"_data",null);b(this,"_givenCDNManager");this._core=e,this._room=e.room,this.cdnLog=i,this.reset()}get isMixing(){return!!this._data}get isStarted(){return!!this._config}get hasCustomCDN(){var e,i,u;return((e=this._config)==null?void 0:e.target.appId)&&((i=this._config)==null?void 0:i.target.bizId)&&((u=this._config)==null?void 0:u.target.url)}async startMixTranscode(e){if(this.cdnLog.info(`startMixTranscode: ${e}`),this._config=e,this.installEvents(),!!this._core.room.isJoined)try{let i=this.getInputParam(e),u=this.getOutputParam(e),d=this.getOutputSessionId({config:e,roomId:this._room.roomId,userId:this._room.userId});this.isMixing&&this._data&&d!==this._data.outputSessionId&&(this.cdnLog.info("startMixTranscode() streamId changed, stop mixing before start"),await this.doStopMixTranscode()),await this.doStartMixTranscode({outputSessionId:d,inputParam:i,outputParam:u});let{appId:c,bizId:m,url:p,streamId:S=""}=this._config.target;c&&m&&p&&(this._givenCDNManager||(this._givenCDNManager=new N(this._core)),await this._givenCDNManager.startPublishGivenCDN({publishMode:e.target.publishMode,appId:c,bizId:m,url:p,streamId:S}))}catch(i){throw this.reset(),i}}async doStartMixTranscode({outputSessionId:e,inputParam:i,outputParam:u}){let d={roomId:String(this._room.roomId),mcuRequestTime:Date.now(),outputSessionId:e,inputParam:i,outputParam:u};this.cdnLog.info(`doStartMixTranscode: ${JSON.stringify(d)}`);let c=await this._room.sendStartMixTranscode(d),{code:m}=c.data,{message:p}=c.data;if(m===0)this._data=d;else throw m===-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.cdnLog.error(`startMixTranscode failed, errCode: ${m} errMsg: ${p}`),new Error(`startMixTranscode failed, errCode: ${m} errMsg: ${p}`)}async stopMixTranscode(){this.cdnLog.info(`stopMixTranscode: ${this.isMixing}`);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.cdnLog.info(`doStopMixTranscode: ${JSON.stringify(e)}`);let i=await this._room.sendStopMixTranscode(e),{code:u,message:d}=i.data;if(u===0)this.reset();else throw this.cdnLog.error(`stopMixTranscode failed, errCode: ${u} errMsg: ${d}`),new Error(`stopMixTranscode failed, errCode: ${u} errMsg: ${d}`)}reset(){this._config=null,this._data=null,this.uninstallEvents()}installEvents(){this._core.room.on("joined",this.handleRoomJoined,this),this._core.room.on("left",this.handleRoomLeft,this)}uninstallEvents(){this._core.room.off("joined",this.handleRoomJoined,this),this._core.room.off("left",this.handleRoomLeft,this)}async handleRoomJoined(){this.cdnLog.info(`handleJoined() ${JSON.stringify(this._config)}`),this.isStarted&&this._config&&await this.startMixTranscode(this._config)}async handleRoomLeft(){this.cdnLog.info(`handleJoined() ${JSON.stringify(this._config)}`),this._data=null}getOutputSessionId({config:e,userId:i,roomId:u}){return this._core.utils.isString(e.target.streamId)&&e.target.streamId.length>0?e.target.streamId:(0,Y.default)(`${u}_${i}_main`)}getStringRoomId(e,i){return e?String(e):i}getInputParam(e){let{mix:i={}}=e,{audioMixUserList:u=[],videoLayoutList:d=[]}=i,c=d.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:v.IT_PURE_VIDEO,renderMode:m.fillMode||0}));return u.forEach(m=>{let p=c.findIndex(S=>S.userId===m.userId&&S.room===this.getStringRoomId(m.roomId,m.strRoomId));p!==-1?c[p].inputType=v.IT_AUDIO_VIDEO:c.push({userId:m.userId,roomId:m.roomId||m.strRoomId,inputType:v.IT_PURE_AUDIO})}),c}getOutputParam(e){let i=e.target.streamId||"",{encoding:u={},mix:d={}}=e;return{streamId:i,streamType:i.length>0?1:0,width:this._core.utils.isUndefined(u.videoWidth)?640:u.videoWidth,height:this._core.utils.isUndefined(u.videoHeight)?480:u.videoHeight,videoBps:u.videoBitrate||0,fps:u.videoFramerate||15,gop:u.videoGOP||2,audioSampleRate:u.audioSampleRate||48e3,audioBps:u.audioBitrate||64,audioChannels:u.audioChannels||1,backgroundColor:d.backgroundColor||0,backgroundImg:d.backgroundImage||"",extraInfo:"",videoCodec:2,audioCodec:0}}};var w=z(V(),1);var L=class{constructor(e,i){b(this,"_core");b(this,"_room");b(this,"cdnLog");b(this,"_paramsForTencentCDN");b(this,"_initParamsForTencentCDN",{isPublished:!1,isStarted:!1});this._core=e,this._room=e.room,this.cdnLog=i,this._paramsForTencentCDN=new Map([["publish-main-stream-to-cdn",this._initParamsForTencentCDN],["publish-sub-stream-to-cdn",this._initParamsForTencentCDN]])}installEvents(){this._core.innerEmitter.on("104",this.handlePublished,this),this._core.room.on("left",this.handleRoomLeft,this)}uninstallEvents(){this._core.innerEmitter.off("104",this.handlePublished,this),this._core.room.off("left",this.handleRoomLeft,this)}async handlePublished({track:e}){var d;this.cdnLog.info(`handlePublished: mediaType ${e.mediaType}, roomID ${(d=e==null?void 0:e.room)==null?void 0:d.roomId}`);let i=e.mediaType===4?"publish-main-stream-to-cdn":"publish-sub-stream-to-cdn",u=this._paramsForTencentCDN.get(i)||null;u!=null&&u.target&&u.isStarted&&await this.startPublishTencentCDN(u.target)}async handleRoomLeft(){this.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.cdnLog.info("Main stream has not already published, will auto reStart after published."),!1):e==="publish-sub-stream-to-cdn"&&!this._room.isAuxStreamPublished?(this.cdnLog.info("Sub has not already published, will auto reStart after published."),!1):!0}changeDataStatus(e,i){let u=this._paramsForTencentCDN.get(e),d=I(I({},u),i);this._paramsForTencentCDN.set(e,d)}async startPublishTencentCDN(e){if(this.cdnLog.info(`startPublishTencentCDN ${JSON.stringify(e)}`),this.installEvents(),this.changeDataStatus(e.publishMode,{target:e,isStarted:!0}),!this.isStreamPublished(e.publishMode))return;let i=e.streamId||"",u=this.generatePublishCDNStreamId(i,e.publishMode),d=this.generatePublishCDNSessionId(e.publishMode),c=e.publishMode==="publish-sub-stream-to-cdn"?1:0,m={requestTime:Date.now(),sessionId:d,streamId:u,streamType:c};await this.doStartPublishTencentCDN(m,e.publishMode);let{appId:p,bizId:S,url:_}=e;if(p&&S&&_){let D=this._paramsForTencentCDN.get(e.publishMode)||this._initParamsForTencentCDN;D!=null&&D.givenCDNManager||(D.givenCDNManager=new N(this._core),this._paramsForTencentCDN.set(e.publishMode,D)),await D.givenCDNManager.startPublishGivenCDN({publishMode:e.publishMode,appId:p,bizId:S,url:_,streamId:u})}}async doStartPublishTencentCDN(e,i){this.cdnLog.info(`doStartPublishTencentCDN: ${JSON.stringify(e)}`);try{let c=0;for(;;){let m=await this._room.sendStartPublishCDN(e,!0),{code:p}=m.data,{message:S}=m.data;if(p===0){this.cdnLog.info(`doStartPublishTencentCDN: success ${JSON.stringify(m)}`),this.changeDataStatus(i,{isPublished:!0});break}else if(p===-10006&&c<6)this.cdnLog.warn(`doStartPublishTencentCDN: retry...${c+1}/6, reason: ${S}`),c+=1,await new Promise(_=>setTimeout(_,500));else throw this.changeDataStatus(i,{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`),new Error(`startPublishTencentCDN failed, errCode: ${p} errMsg: ${S}`)}}catch(u){throw this.cdnLog.error(`startPublishTencentCDN failed, error: ${u}`),this.changeDataStatus(i,{isPublished:!1}),u}}async stopPublishTencentCDN(e){let i=this._paramsForTencentCDN.get(e)||this._initParamsForTencentCDN;if(i.isPublished){let{target:u}=i;u!=null&&u.bizId&&u.appId&&u.url&&(i!=null&&i.givenCDNManager)&&await(i==null?void 0:i.givenCDNManager.stopPublishGivenCDN()),await this.doStopPublishTencentCDN(e)}this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN)}async doStopPublishTencentCDN(e){let i={requestTime:Date.now(),sessionId:(0,w.default)(`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(e)}`)};this.cdnLog.info(`stopPublishTencentCDN: ${JSON.stringify(i)}`);let u=await this._room.sendStopPublishCDN(i,!0),{code:d,message:c}=u.data;if(d===0)this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN),this.reset(e);else if(d===-102069)this.cdnLog.error("stopPublishTencentCDN failed, can not stopPublishTencentCDN in auto relayed-push mode"),this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN);else throw this.cdnLog.error(`stopPublishTencentCDN failed, errCode: ${d} errMsg: ${c}`),new Error(`stopPublishTencentCDN failed, errCode: ${d} errMsg: ${c}`)}reset(e){this.uninstallEvents(),this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN)}generatePublishCDNStreamId(e,i){if(e===""){let u=`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(i)}`;return/^[A-Za-z\d_-]*$/.test(u)||(u=(0,w.default)(u)),`${this._room.sdkAppId}_${u}`}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 Z=l=>typeof l=="string";function Se(l,e,i,u){if(!/^[A-Za-z\d_-]*$/.test(l))throw new Error("Invalid parameter streamId.")}function Te(l,e,i,u){if(Z(l))throw new Error("roomId must be an integer but go a string, use 'parseInt' to convert it or use 'strRoomId' instead.");if(l===void 0)return;if(!(/^[1-9]\d*$/.test(String(l))&&l<4294967295))throw new Error("roomId must be an integer between [1, 4294967294].")}function Ce(l,e,i,u){if(!/^[A-Za-z\d\s!#$%&()+\-:;<=.>?@[\]^_{}|~,]{1,64}$/.test(l))throw new Error("strRoomId must be a valid string.")}var O={type:"number",notLessThanZero:!0},Q={type:"object",required:!0,properties:{userId:{required:!0,type:"string"},roomId:{type:["string","number"],validate:Te},strRoomId:{type:"string",validate:Ce}}},ee={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:Se},appId:{type:"number",allowEmpty:!1},bizId:{type:"number",allowEmpty:!1},url:{type:"string",allowEmpty:!1}}},te={required:!1,type:"object",properties:{videoWidth:O,videoHeight:O,videoBitrate:W(I({},O),{allowEmpty:!1}),videoFramerate:{type:"number",validate(l,e,i,u){if(l<=0||l>30)throw new Error("Invalid parameter mixConfig -> videoFramerate, the value must be between (0, 30].")}},videoGOP:{type:"number",validate(l,e,i,u){if(l<1||l>8)throw new Error("Invalid parameter mixConfig -> videoGOP, the value must be between [1, 8].")}},audioSampleRate:O,audioBitrate:{type:"number",validate(l,e,i,u){if(l<32||l>192)throw new Error("Invalid parameter mixConfig -> audioBitrate, the value must be between [32, 192].")}},audioChannels:{type:"number",values:[1,2]}}},re={required:!1,type:"object",properties:{backgroundColor:{type:"number"},backgroundImage:{type:"string"},audioMixUserList:{type:"array",arrayItem:I({},Q)},videoLayoutList:{type:"array",required:!0,arrayItem:{type:"object",properties:{fixedVideoUser:I({},Q),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,u){if(l<1||l>15)throw new Error("Invalid parameter mix -> videoLayoutList -> zOrder, the value must be between [1, 15].")}},width:O,height:O,locationX:O,locationY:O}}}}};function ie(l){return{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:I({},ee),encoding:I({},te),mix:I({},re)},validate(e,i,u,d){var S,_;let{publishMode:c}=e.target,{encoding:m,mix:p}=e;if(c==="publish-mix-stream-to-cdn"){if(!m||!p)throw new Error("Invalid parameter mix & encoding, the value must be not empty.");if(p&&p.videoLayoutList){let D=0,M=0,U=[];if(p.videoLayoutList.forEach((P,F)=>{U.push(P.fixedVideoUser.userId),P.width+P.locationX>D&&(D=P.width+P.locationX),P.height+P.locationY>M&&(M=P.height+P.locationY)}),U.indexOf(l.room.userId)<0)throw new Error("Invalid parameter mix -> videoLayoutList, the value must be include self.");let E=(S=m==null?void 0:m.videoWidth)!=null?S:640,R=(_=m==null?void 0:m.videoHeight)!=null?_:480;if(E<D||R<M)throw new Error("Invalid parameter encoding, the width and height of the mixed video must encompass all the mixed-in video streams.")}}}}}function oe(l){return{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:I({},ee),encoding:I({},te),mix:I({},re)}}}function ae(l){return{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 G=0,k=class{constructor(e){this.core=e;b(this,"_mixTranscodeManager");b(this,"_publishCDNManager");b(this,"_core");b(this,"cdnLog");b(this,"_modeOptions");b(this,"seq");G=G+1,this.seq=G,this.cdnLog=e.log.createChild({id:`${this.getAlias()}${G}`}),this._core=e,this.cdnLog.info("CDNStreaming created"),this._modeOptions=new Map,this._mixTranscodeManager=new y(e,this.cdnLog),this._publishCDNManager=new L(e,this.cdnLog)}getName(){return"CDNStreaming"}getAlias(){return"cdn"}getValidateRule(e){switch(e){case"start":return ie(this._core);case"update":return oe(this._core);case"stop":return ae(this._core)}}getGroup(e){return e.target.publishMode}async start(e){return this.cdnLog.info(`Plugin CDNStreaming start: ${JSON.stringify(e)}`),this._modeOptions.set(e.target.publishMode,e),await this.doStart(e)}async update(e){this.cdnLog.info(`Plugin CDNStreaming update: ${JSON.stringify(e)}`);let i=this._modeOptions.get(e.target.publishMode);return this._core.utils.deepMerge(i,e),await this.doStart(i)}async stop(e){switch(this.cdnLog.info(`Plugin CDNStreaming stop: ${JSON.stringify(e)}`),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.cdnLog.info(`Plugin 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}}}};b(k,"TYPE",{PublishMode:{PublishMainStreamToCDN:"publish-main-stream-to-cdn",PublishSubStreamToCDN:"publish-sub-stream-to-cdn",PublishMixStreamToCDN:"publish-mix-stream-to-cdn"}});var tt=k;export{k as CDNStreaming,A as PublishMode,K as TRTCStreamType,tt as default};
1
+ var ne=Object.create;var H=Object.defineProperty,ue=Object.defineProperties,de=Object.getOwnPropertyDescriptor,me=Object.getOwnPropertyDescriptors,le=Object.getOwnPropertyNames,q=Object.getOwnPropertySymbols,he=Object.getPrototypeOf,X=Object.prototype.hasOwnProperty,ce=Object.prototype.propertyIsEnumerable;var $=(l,e,i)=>e in l?H(l,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):l[e]=i,I=(l,e)=>{for(var i in e||(e={}))X.call(e,i)&&$(l,i,e[i]);if(q)for(var i of q(e))ce.call(e,i)&&$(l,i,e[i]);return l},W=(l,e)=>ue(l,me(e));var pe=(l,e)=>()=>(e||l((e={exports:{}}).exports,e),e.exports);var ge=(l,e,i,u)=>{if(e&&typeof e=="object"||typeof e=="function")for(let d of le(e))!X.call(l,d)&&d!==i&&H(l,d,{get:()=>e[d],enumerable:!(u=de(e,d))||u.enumerable});return l};var z=(l,e,i)=>(i=l!=null?ne(he(l)):{},ge(e||!l||!l.__esModule?H(i,"default",{value:l,enumerable:!0}):i,l));var b=(l,e,i)=>($(l,typeof e!="symbol"?e+"":e,i),i);var V=pe((j,x)=>{(function(l){"use strict";function e(t,h){var r=(t&65535)+(h&65535),g=(t>>16)+(h>>16)+(r>>16);return g<<16|r&65535}function i(t,h){return t<<h|t>>>32-h}function u(t,h,r,g,T,C){return e(i(e(e(h,t),e(g,C)),T),r)}function d(t,h,r,g,T,C,f){return u(h&r|~h&g,t,h,T,C,f)}function c(t,h,r,g,T,C,f){return u(h&g|r&~g,t,h,T,C,f)}function m(t,h,r,g,T,C,f){return u(h^r^g,t,h,T,C,f)}function p(t,h,r,g,T,C,f){return u(r^(h|~g),t,h,T,C,f)}function S(t,h){t[h>>5]|=128<<h%32,t[(h+64>>>9<<4)+14]=h;var r,g,T,C,f,o=1732584193,a=-271733879,s=-1732584194,n=271733878;for(r=0;r<t.length;r+=16)g=o,T=a,C=s,f=n,o=d(o,a,s,n,t[r],7,-680876936),n=d(n,o,a,s,t[r+1],12,-389564586),s=d(s,n,o,a,t[r+2],17,606105819),a=d(a,s,n,o,t[r+3],22,-1044525330),o=d(o,a,s,n,t[r+4],7,-176418897),n=d(n,o,a,s,t[r+5],12,1200080426),s=d(s,n,o,a,t[r+6],17,-1473231341),a=d(a,s,n,o,t[r+7],22,-45705983),o=d(o,a,s,n,t[r+8],7,1770035416),n=d(n,o,a,s,t[r+9],12,-1958414417),s=d(s,n,o,a,t[r+10],17,-42063),a=d(a,s,n,o,t[r+11],22,-1990404162),o=d(o,a,s,n,t[r+12],7,1804603682),n=d(n,o,a,s,t[r+13],12,-40341101),s=d(s,n,o,a,t[r+14],17,-1502002290),a=d(a,s,n,o,t[r+15],22,1236535329),o=c(o,a,s,n,t[r+1],5,-165796510),n=c(n,o,a,s,t[r+6],9,-1069501632),s=c(s,n,o,a,t[r+11],14,643717713),a=c(a,s,n,o,t[r],20,-373897302),o=c(o,a,s,n,t[r+5],5,-701558691),n=c(n,o,a,s,t[r+10],9,38016083),s=c(s,n,o,a,t[r+15],14,-660478335),a=c(a,s,n,o,t[r+4],20,-405537848),o=c(o,a,s,n,t[r+9],5,568446438),n=c(n,o,a,s,t[r+14],9,-1019803690),s=c(s,n,o,a,t[r+3],14,-187363961),a=c(a,s,n,o,t[r+8],20,1163531501),o=c(o,a,s,n,t[r+13],5,-1444681467),n=c(n,o,a,s,t[r+2],9,-51403784),s=c(s,n,o,a,t[r+7],14,1735328473),a=c(a,s,n,o,t[r+12],20,-1926607734),o=m(o,a,s,n,t[r+5],4,-378558),n=m(n,o,a,s,t[r+8],11,-2022574463),s=m(s,n,o,a,t[r+11],16,1839030562),a=m(a,s,n,o,t[r+14],23,-35309556),o=m(o,a,s,n,t[r+1],4,-1530992060),n=m(n,o,a,s,t[r+4],11,1272893353),s=m(s,n,o,a,t[r+7],16,-155497632),a=m(a,s,n,o,t[r+10],23,-1094730640),o=m(o,a,s,n,t[r+13],4,681279174),n=m(n,o,a,s,t[r],11,-358537222),s=m(s,n,o,a,t[r+3],16,-722521979),a=m(a,s,n,o,t[r+6],23,76029189),o=m(o,a,s,n,t[r+9],4,-640364487),n=m(n,o,a,s,t[r+12],11,-421815835),s=m(s,n,o,a,t[r+15],16,530742520),a=m(a,s,n,o,t[r+2],23,-995338651),o=p(o,a,s,n,t[r],6,-198630844),n=p(n,o,a,s,t[r+7],10,1126891415),s=p(s,n,o,a,t[r+14],15,-1416354905),a=p(a,s,n,o,t[r+5],21,-57434055),o=p(o,a,s,n,t[r+12],6,1700485571),n=p(n,o,a,s,t[r+3],10,-1894986606),s=p(s,n,o,a,t[r+10],15,-1051523),a=p(a,s,n,o,t[r+1],21,-2054922799),o=p(o,a,s,n,t[r+8],6,1873313359),n=p(n,o,a,s,t[r+15],10,-30611744),s=p(s,n,o,a,t[r+6],15,-1560198380),a=p(a,s,n,o,t[r+13],21,1309151649),o=p(o,a,s,n,t[r+4],6,-145523070),n=p(n,o,a,s,t[r+11],10,-1120210379),s=p(s,n,o,a,t[r+2],15,718787259),a=p(a,s,n,o,t[r+9],21,-343485551),o=e(o,g),a=e(a,T),s=e(s,C),n=e(n,f);return[o,a,s,n]}function _(t){var h,r="",g=t.length*32;for(h=0;h<g;h+=8)r+=String.fromCharCode(t[h>>5]>>>h%32&255);return r}function D(t){var h,r=[];for(r[(t.length>>2)-1]=void 0,h=0;h<r.length;h+=1)r[h]=0;var g=t.length*8;for(h=0;h<g;h+=8)r[h>>5]|=(t.charCodeAt(h/8)&255)<<h%32;return r}function M(t){return _(S(D(t),t.length*8))}function U(t,h){var r,g=D(t),T=[],C=[],f;for(T[15]=C[15]=void 0,g.length>16&&(g=S(g,t.length*8)),r=0;r<16;r+=1)T[r]=g[r]^909522486,C[r]=g[r]^1549556828;return f=S(T.concat(D(h)),512+h.length*8),_(S(C.concat(f),512+128))}function E(t){var h="0123456789abcdef",r="",g,T;for(T=0;T<t.length;T+=1)g=t.charCodeAt(T),r+=h.charAt(g>>>4&15)+h.charAt(g&15);return r}function R(t){return unescape(encodeURIComponent(t))}function P(t){return M(R(t))}function F(t){return E(P(t))}function J(t,h){return U(R(t),R(h))}function se(t,h){return E(J(t,h))}function B(t,h,r){return h?r?J(h,t):se(h,t):r?P(t):F(t)}typeof define=="function"&&define.amd?define(function(){return B}):typeof x=="object"&&x.exports?x.exports=B:l.md5=B})(j)});var Y=z(V(),1);var A=(u=>(u.PublishMainStreamToCDN="publish-main-stream-to-cdn",u.PublishSubStreamToCDN="publish-sub-stream-to-cdn",u.PublishMixStreamToCDN="publish-mix-stream-to-cdn",u))(A||{}),K=(i=>(i.Main="main",i.Sub="sub",i))(K||{});var v={IT_AUDIO_VIDEO:0,IT_PICTURE:2,IT_CANVAS:3,IT_PURE_AUDIO:4,IT_PURE_VIDEO:5};var N=class{constructor(e){b(this,"_room");b(this,"_log");b(this,"_paramsForGivenCDN");b(this,"_publishGivenCDNData",null);b(this,"_isPublishingGivenCDN",!1);this._room=e.room,this._log=e.log,this._paramsForGivenCDN=new Map([["publish-main-stream-to-cdn",null],["publish-sub-stream-to-cdn",null],["publish-mix-stream-to-cdn",null]])}async startPublishGivenCDN(e){this._log.info(`startPublishGivenCDN() ${JSON.stringify(e)}`),this._paramsForGivenCDN.set(e.publishMode,e);let i={pushRequestTime:Date.now(),pushAppId:e.appId,pushBizId:e.bizId,pushCdnUrl:e.url,pushStreamType:this.convertStreamType(e==null?void 0:e.publishMode),pushStreamId:e.streamId};this._publishGivenCDNData=i;try{this._isPublishingGivenCDN&&await this.stopPublishGivenCDN();let u=await this._room.sendStartPublishCDN(i,!1),{code:d,message:c}=u.data;if(d===0)this._isPublishingGivenCDN=!0;else throw this.resetGivenCDN(),this._log.error(`startPublishGivenCDN failed, errCode: ${d}, errMsg: ${c}`),new Error(`startPublishGivenCDN failed, errCode: ${d} errMsg: ${c}`)}catch(u){throw this.resetGivenCDN(),u}}async stopPublishGivenCDN(){if(this._log.info(`stopPublishGivenCDN: ${JSON.stringify(this._publishGivenCDNData)}`),!this._isPublishingGivenCDN||!this._publishGivenCDNData){this.resetGivenCDN();return}let{pushAppId:e,pushBizId:i,pushCdnUrl:u,pushStreamType:d,pushStreamId:c}=this._publishGivenCDNData,m={pushRequestTime:Date.now(),pushAppId:e,pushBizId:i,pushCdnUrl:u,pushStreamType:d,pushStreamId:c},p=await this._room.sendStopPublishCDN(m,!1),{code:S,message:_}=p.data;if(S===0)this.resetGivenCDN();else throw this._log.error(`stopPublishGivenCDN failed, errCode: ${S} errMsg: ${_}`),new Error(`stopPublishGivenCDN failed, errCode: ${S} errMsg: ${_}`)}resetGivenCDN(){this._publishGivenCDNData=null,this._isPublishingGivenCDN=!1}convertStreamType(e){return e==="publish-main-stream-to-cdn"?"main":"aux"}};var L=class{constructor(e,i){b(this,"_core");b(this,"_room");b(this,"cdnLog");b(this,"_config",null);b(this,"_data",null);b(this,"_givenCDNManager");this._core=e,this._room=e.room,this.cdnLog=i,this.reset()}get isMixing(){return!!this._data}get isStarted(){return!!this._config}get hasCustomCDN(){var e,i,u;return((e=this._config)==null?void 0:e.target.appId)&&((i=this._config)==null?void 0:i.target.bizId)&&((u=this._config)==null?void 0:u.target.url)}async startMixTranscode(e){if(this.cdnLog.info(`startMixTranscode: ${e}`),this._config=e,this.installEvents(),!!this._core.room.isJoined)try{let i=this.getInputParam(e),u=this.getOutputParam(e),d=this.getOutputSessionId({config:e,roomId:this._room.roomId,userId:this._room.userId});this.isMixing&&this._data&&d!==this._data.outputSessionId&&(this.cdnLog.info("startMixTranscode() streamId changed, stop mixing before start"),await this.doStopMixTranscode()),await this.doStartMixTranscode({outputSessionId:d,inputParam:i,outputParam:u});let{appId:c,bizId:m,url:p,streamId:S=""}=this._config.target;c&&m&&p&&(this._givenCDNManager||(this._givenCDNManager=new N(this._core)),await this._givenCDNManager.startPublishGivenCDN({publishMode:e.target.publishMode,appId:c,bizId:m,url:p,streamId:S}))}catch(i){throw this.reset(),i}}async doStartMixTranscode({outputSessionId:e,inputParam:i,outputParam:u}){let d={roomId:String(this._room.roomId),mcuRequestTime:Date.now(),outputSessionId:e,inputParam:i,outputParam:u};this.cdnLog.info(`doStartMixTranscode: ${JSON.stringify(d)}`);let c=await this._room.sendStartMixTranscode(d),{code:m}=c.data,{message:p}=c.data;if(m===0)this._data=d;else throw m===-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.cdnLog.error(`startMixTranscode failed, errCode: ${m} errMsg: ${p}`),new Error(`startMixTranscode failed, errCode: ${m} errMsg: ${p}`)}async stopMixTranscode(){this.cdnLog.info(`stopMixTranscode: ${this.isMixing}`);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.cdnLog.info(`doStopMixTranscode: ${JSON.stringify(e)}`);let i=await this._room.sendStopMixTranscode(e),{code:u,message:d}=i.data;if(u===0)this.reset();else throw this.cdnLog.error(`stopMixTranscode failed, errCode: ${u} errMsg: ${d}`),new Error(`stopMixTranscode failed, errCode: ${u} errMsg: ${d}`)}reset(){this._config=null,this._data=null,this.uninstallEvents()}installEvents(){this._core.room.on("joined",this.handleRoomJoined,this),this._core.room.on("left",this.handleRoomLeft,this)}uninstallEvents(){this._core.room.off("joined",this.handleRoomJoined,this),this._core.room.off("left",this.handleRoomLeft,this)}async handleRoomJoined(){this.cdnLog.info(`handleJoined() ${JSON.stringify(this._config)}`),this.isStarted&&this._config&&await this.startMixTranscode(this._config)}async handleRoomLeft(){this.cdnLog.info(`handleJoined() ${JSON.stringify(this._config)}`),this._data=null}getOutputSessionId({config:e,userId:i,roomId:u}){return this._core.utils.isString(e.target.streamId)&&e.target.streamId.length>0?e.target.streamId:(0,Y.default)(`${u}_${i}_main`)}getStringRoomId(e,i){return e?String(e):i}getInputParam(e){let{mix:i={}}=e,{audioMixUserList:u=[],videoLayoutList:d=[]}=i,c=d.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:v.IT_PURE_VIDEO,renderMode:m.fillMode||0}));return u.forEach(m=>{let p=c.findIndex(S=>S.userId===m.userId&&S.room===this.getStringRoomId(m.roomId,m.strRoomId));p!==-1?c[p].inputType=v.IT_AUDIO_VIDEO:c.push({userId:m.userId,roomId:m.roomId||m.strRoomId,inputType:v.IT_PURE_AUDIO})}),c}getOutputParam(e){let i=e.target.streamId||"",{encoding:u={},mix:d={}}=e;return{streamId:i,streamType:i.length>0?1:0,width:this._core.utils.isUndefined(u.videoWidth)?640:u.videoWidth,height:this._core.utils.isUndefined(u.videoHeight)?480:u.videoHeight,videoBps:u.videoBitrate||0,fps:u.videoFramerate||15,gop:u.videoGOP||2,audioSampleRate:u.audioSampleRate||48e3,audioBps:u.audioBitrate||64,audioChannels:u.audioChannels||1,backgroundColor:d.backgroundColor||0,backgroundImg:d.backgroundImage||"",extraInfo:"",videoCodec:2,audioCodec:0}}};var w=z(V(),1);var y=class{constructor(e,i){b(this,"_core");b(this,"_room");b(this,"cdnLog");b(this,"_paramsForTencentCDN");b(this,"_initParamsForTencentCDN",{isPublished:!1,isStarted:!1});this._core=e,this._room=e.room,this.cdnLog=i,this._paramsForTencentCDN=new Map([["publish-main-stream-to-cdn",this._initParamsForTencentCDN],["publish-sub-stream-to-cdn",this._initParamsForTencentCDN]])}installEvents(){this._core.innerEmitter.on("104",this.handlePublished,this),this._core.room.on("left",this.handleRoomLeft,this)}uninstallEvents(){this._core.innerEmitter.off("104",this.handlePublished,this),this._core.room.off("left",this.handleRoomLeft,this)}async handlePublished({track:e}){var d;this.cdnLog.info(`handlePublished: mediaType ${e.mediaType}, roomID ${(d=e==null?void 0:e.room)==null?void 0:d.roomId}`);let i=e.mediaType===4?"publish-main-stream-to-cdn":"publish-sub-stream-to-cdn",u=this._paramsForTencentCDN.get(i)||null;u!=null&&u.target&&u.isStarted&&await this.startPublishTencentCDN(u.target)}async handleRoomLeft(){this.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.cdnLog.info("Main stream has not already published, will auto reStart after published."),!1):e==="publish-sub-stream-to-cdn"&&!this._room.isAuxStreamPublished?(this.cdnLog.info("Sub has not already published, will auto reStart after published."),!1):!0}changeDataStatus(e,i){let u=this._paramsForTencentCDN.get(e),d=I(I({},u),i);this._paramsForTencentCDN.set(e,d)}async startPublishTencentCDN(e){if(this.cdnLog.info(`startPublishTencentCDN ${JSON.stringify(e)}`),this.installEvents(),this.changeDataStatus(e.publishMode,{target:e,isStarted:!0}),!this.isStreamPublished(e.publishMode))return;let i=e.streamId||"",u=this.generatePublishCDNStreamId(i,e.publishMode),d=this.generatePublishCDNSessionId(e.publishMode),c=e.publishMode==="publish-sub-stream-to-cdn"?1:0,m={requestTime:Date.now(),sessionId:d,streamId:u,streamType:c};await this.doStartPublishTencentCDN(m,e.publishMode);let{appId:p,bizId:S,url:_}=e;if(p&&S&&_){let D=this._paramsForTencentCDN.get(e.publishMode)||this._initParamsForTencentCDN;D!=null&&D.givenCDNManager||(D.givenCDNManager=new N(this._core),this._paramsForTencentCDN.set(e.publishMode,D)),await D.givenCDNManager.startPublishGivenCDN({publishMode:e.publishMode,appId:p,bizId:S,url:_,streamId:u})}}async doStartPublishTencentCDN(e,i){this.cdnLog.info(`doStartPublishTencentCDN: ${JSON.stringify(e)}`);try{let c=0;for(;;){let m=await this._room.sendStartPublishCDN(e,!0),{code:p}=m.data,{message:S}=m.data;if(p===0){this.cdnLog.info(`doStartPublishTencentCDN: success ${JSON.stringify(m)}`),this.changeDataStatus(i,{isPublished:!0});break}else if(p===-10006&&c<6)this.cdnLog.warn(`doStartPublishTencentCDN: retry...${c+1}/6, reason: ${S}`),c+=1,await new Promise(_=>setTimeout(_,500));else throw this.changeDataStatus(i,{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`),new Error(`startPublishTencentCDN failed, errCode: ${p} errMsg: ${S}`)}}catch(u){throw this.cdnLog.error(`startPublishTencentCDN failed, error: ${u}`),this.changeDataStatus(i,{isPublished:!1}),u}}async stopPublishTencentCDN(e){let i=this._paramsForTencentCDN.get(e)||this._initParamsForTencentCDN;if(i.isPublished){let{target:u}=i;u!=null&&u.bizId&&u.appId&&u.url&&(i!=null&&i.givenCDNManager)&&await(i==null?void 0:i.givenCDNManager.stopPublishGivenCDN()),await this.doStopPublishTencentCDN(e)}this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN)}async doStopPublishTencentCDN(e){let i={requestTime:Date.now(),sessionId:(0,w.default)(`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(e)}`)};this.cdnLog.info(`stopPublishTencentCDN: ${JSON.stringify(i)}`);let u=await this._room.sendStopPublishCDN(i,!0),{code:d,message:c}=u.data;if(d===0)this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN),this.reset(e);else if(d===-102069)this.cdnLog.error("stopPublishTencentCDN failed, can not stopPublishTencentCDN in auto relayed-push mode"),this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN);else throw this.cdnLog.error(`stopPublishTencentCDN failed, errCode: ${d} errMsg: ${c}`),new Error(`stopPublishTencentCDN failed, errCode: ${d} errMsg: ${c}`)}reset(e){this.uninstallEvents(),this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN)}generatePublishCDNStreamId(e,i){if(e===""){let u=`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(i)}`;return/^[A-Za-z\d_-]*$/.test(u)||(u=(0,w.default)(u)),`${this._room.sdkAppId}_${u}`}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 Z=l=>typeof l=="string";function Se(l,e,i,u){if(!/^[A-Za-z\d_-]*$/.test(l))throw new Error("Invalid parameter streamId.")}function Te(l,e,i,u){if(Z(l))throw new Error("roomId must be an integer but go a string, use 'parseInt' to convert it or use 'strRoomId' instead.");if(l===void 0)return;if(!(/^[1-9]\d*$/.test(String(l))&&l<4294967295))throw new Error("roomId must be an integer between [1, 4294967294].")}function Ce(l,e,i,u){if(!/^[A-Za-z\d\s!#$%&()+\-:;<=.>?@[\]^_{}|~,]{1,64}$/.test(l))throw new Error("strRoomId must be a valid string.")}var O={type:"number",notLessThanZero:!0},Q={type:"object",required:!0,properties:{userId:{required:!0,type:"string"},roomId:{type:["string","number"],validate:Te},strRoomId:{type:"string",validate:Ce}}},ee={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:Se},appId:{type:"number",allowEmpty:!1},bizId:{type:"number",allowEmpty:!1},url:{type:"string",allowEmpty:!1}}},te={required:!1,type:"object",properties:{videoWidth:O,videoHeight:O,videoBitrate:W(I({},O),{allowEmpty:!1}),videoFramerate:{type:"number",validate(l,e,i,u){if(l<=0||l>30)throw new Error("Invalid parameter mixConfig -> videoFramerate, the value must be between (0, 30].")}},videoGOP:{type:"number",validate(l,e,i,u){if(l<1||l>8)throw new Error("Invalid parameter mixConfig -> videoGOP, the value must be between [1, 8].")}},audioSampleRate:O,audioBitrate:{type:"number",validate(l,e,i,u){if(l<32||l>192)throw new Error("Invalid parameter mixConfig -> audioBitrate, the value must be between [32, 192].")}},audioChannels:{type:"number",values:[1,2]}}},re={required:!1,type:"object",properties:{backgroundColor:{type:"number"},backgroundImage:{type:"string"},audioMixUserList:{type:"array",arrayItem:I({},Q)},videoLayoutList:{type:"array",required:!0,arrayItem:{type:"object",properties:{fixedVideoUser:I({},Q),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,u){if(l<1||l>15)throw new Error("Invalid parameter mix -> videoLayoutList -> zOrder, the value must be between [1, 15].")}},width:O,height:O,locationX:O,locationY:O}}}}};function ie(l){return{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:I({},ee),encoding:I({},te),mix:I({},re)},validate(e,i,u,d){var S,_;let{publishMode:c}=e.target,{encoding:m,mix:p}=e;if(c==="publish-mix-stream-to-cdn"){if(!m||!p)throw new Error("Invalid parameter mix & encoding, the value must be not empty.");if(p&&p.videoLayoutList){let D=0,M=0,U=[];if(p.videoLayoutList.forEach((P,F)=>{U.push(P.fixedVideoUser.userId),P.width+P.locationX>D&&(D=P.width+P.locationX),P.height+P.locationY>M&&(M=P.height+P.locationY)}),U.indexOf(l.room.userId)<0)throw new Error("Invalid parameter mix -> videoLayoutList, the value must be include self.");let E=(S=m==null?void 0:m.videoWidth)!=null?S:640,R=(_=m==null?void 0:m.videoHeight)!=null?_:480;if(E<D||R<M)throw new Error("Invalid parameter encoding, the width and height of the mixed video must encompass all the mixed-in video streams.")}}}}}function oe(l){return{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:I({},ee),encoding:I({},te),mix:I({},re)}}}function ae(l){return{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 G=0,k=class{constructor(e){this.core=e;b(this,"_mixTranscodeManager");b(this,"_publishCDNManager");b(this,"_core");b(this,"cdnLog");b(this,"_modeOptions");b(this,"seq");G=G+1,this.seq=G,this.cdnLog=e.log.createChild({id:`${this.getAlias()}${G}`}),this._core=e,this.cdnLog.info("CDNStreaming created"),this._modeOptions=new Map,this._mixTranscodeManager=new L(e,this.cdnLog),this._publishCDNManager=new y(e,this.cdnLog)}getName(){return"CDNStreaming"}getAlias(){return"cdn"}getValidateRule(e){switch(e){case"start":return ie(this._core);case"update":return oe(this._core);case"stop":return ae(this._core)}}getGroup(e){return e.target.publishMode}async start(e){return this.cdnLog.info(`Plugin CDNStreaming start: ${JSON.stringify(e)}`),this._modeOptions.set(e.target.publishMode,e),await this.doStart(e)}async update(e){this.cdnLog.info(`Plugin CDNStreaming update: ${JSON.stringify(e)}`);let i=this._modeOptions.get(e.target.publishMode);return this._core.utils.deepMerge(i,e),await this.doStart(i)}async stop(e){switch(this.cdnLog.info(`Plugin CDNStreaming stop: ${JSON.stringify(e)}`),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.cdnLog.info(`Plugin 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}}}};b(k,"TYPE",{PublishMode:{PublishMainStreamToCDN:"publish-main-stream-to-cdn",PublishSubStreamToCDN:"publish-sub-stream-to-cdn",PublishMixStreamToCDN:"publish-mix-stream-to-cdn"}});var tt=k;export{k as CDNStreaming,A as PublishMode,K as TRTCStreamType,tt as default};
@@ -1 +1 @@
1
- "use strict";var CDNStreaming=(()=>{var ue=Object.create;var y=Object.defineProperty,de=Object.defineProperties,me=Object.getOwnPropertyDescriptor,le=Object.getOwnPropertyDescriptors,he=Object.getOwnPropertyNames,q=Object.getOwnPropertySymbols,ce=Object.getPrototypeOf,X=Object.prototype.hasOwnProperty,pe=Object.prototype.propertyIsEnumerable;var V=(d,e,i)=>e in d?y(d,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):d[e]=i,I=(d,e)=>{for(var i in e||(e={}))X.call(e,i)&&V(d,i,e[i]);if(q)for(var i of q(e))pe.call(e,i)&&V(d,i,e[i]);return d},W=(d,e)=>de(d,le(e));var ge=(d,e)=>()=>(e||d((e={exports:{}}).exports,e),e.exports),be=(d,e)=>{for(var i in e)y(d,i,{get:e[i],enumerable:!0})},z=(d,e,i,u)=>{if(e&&typeof e=="object"||typeof e=="function")for(let m of he(e))!X.call(d,m)&&m!==i&&y(d,m,{get:()=>e[m],enumerable:!(u=me(e,m))||u.enumerable});return d};var j=(d,e,i)=>(i=d!=null?ue(ce(d)):{},z(e||!d||!d.__esModule?y(i,"default",{value:d,enumerable:!0}):i,d)),Se=d=>z(y({},"__esModule",{value:!0}),d);var b=(d,e,i)=>(V(d,typeof e!="symbol"?e+"":e,i),i);var K=ge((Y,w)=>{(function(d){"use strict";function e(t,h){var r=(t&65535)+(h&65535),g=(t>>16)+(h>>16)+(r>>16);return g<<16|r&65535}function i(t,h){return t<<h|t>>>32-h}function u(t,h,r,g,T,C){return e(i(e(e(h,t),e(g,C)),T),r)}function m(t,h,r,g,T,C,f){return u(h&r|~h&g,t,h,T,C,f)}function c(t,h,r,g,T,C,f){return u(h&g|r&~g,t,h,T,C,f)}function l(t,h,r,g,T,C,f){return u(h^r^g,t,h,T,C,f)}function p(t,h,r,g,T,C,f){return u(r^(h|~g),t,h,T,C,f)}function S(t,h){t[h>>5]|=128<<h%32,t[(h+64>>>9<<4)+14]=h;var r,g,T,C,f,o=1732584193,a=-271733879,s=-1732584194,n=271733878;for(r=0;r<t.length;r+=16)g=o,T=a,C=s,f=n,o=m(o,a,s,n,t[r],7,-680876936),n=m(n,o,a,s,t[r+1],12,-389564586),s=m(s,n,o,a,t[r+2],17,606105819),a=m(a,s,n,o,t[r+3],22,-1044525330),o=m(o,a,s,n,t[r+4],7,-176418897),n=m(n,o,a,s,t[r+5],12,1200080426),s=m(s,n,o,a,t[r+6],17,-1473231341),a=m(a,s,n,o,t[r+7],22,-45705983),o=m(o,a,s,n,t[r+8],7,1770035416),n=m(n,o,a,s,t[r+9],12,-1958414417),s=m(s,n,o,a,t[r+10],17,-42063),a=m(a,s,n,o,t[r+11],22,-1990404162),o=m(o,a,s,n,t[r+12],7,1804603682),n=m(n,o,a,s,t[r+13],12,-40341101),s=m(s,n,o,a,t[r+14],17,-1502002290),a=m(a,s,n,o,t[r+15],22,1236535329),o=c(o,a,s,n,t[r+1],5,-165796510),n=c(n,o,a,s,t[r+6],9,-1069501632),s=c(s,n,o,a,t[r+11],14,643717713),a=c(a,s,n,o,t[r],20,-373897302),o=c(o,a,s,n,t[r+5],5,-701558691),n=c(n,o,a,s,t[r+10],9,38016083),s=c(s,n,o,a,t[r+15],14,-660478335),a=c(a,s,n,o,t[r+4],20,-405537848),o=c(o,a,s,n,t[r+9],5,568446438),n=c(n,o,a,s,t[r+14],9,-1019803690),s=c(s,n,o,a,t[r+3],14,-187363961),a=c(a,s,n,o,t[r+8],20,1163531501),o=c(o,a,s,n,t[r+13],5,-1444681467),n=c(n,o,a,s,t[r+2],9,-51403784),s=c(s,n,o,a,t[r+7],14,1735328473),a=c(a,s,n,o,t[r+12],20,-1926607734),o=l(o,a,s,n,t[r+5],4,-378558),n=l(n,o,a,s,t[r+8],11,-2022574463),s=l(s,n,o,a,t[r+11],16,1839030562),a=l(a,s,n,o,t[r+14],23,-35309556),o=l(o,a,s,n,t[r+1],4,-1530992060),n=l(n,o,a,s,t[r+4],11,1272893353),s=l(s,n,o,a,t[r+7],16,-155497632),a=l(a,s,n,o,t[r+10],23,-1094730640),o=l(o,a,s,n,t[r+13],4,681279174),n=l(n,o,a,s,t[r],11,-358537222),s=l(s,n,o,a,t[r+3],16,-722521979),a=l(a,s,n,o,t[r+6],23,76029189),o=l(o,a,s,n,t[r+9],4,-640364487),n=l(n,o,a,s,t[r+12],11,-421815835),s=l(s,n,o,a,t[r+15],16,530742520),a=l(a,s,n,o,t[r+2],23,-995338651),o=p(o,a,s,n,t[r],6,-198630844),n=p(n,o,a,s,t[r+7],10,1126891415),s=p(s,n,o,a,t[r+14],15,-1416354905),a=p(a,s,n,o,t[r+5],21,-57434055),o=p(o,a,s,n,t[r+12],6,1700485571),n=p(n,o,a,s,t[r+3],10,-1894986606),s=p(s,n,o,a,t[r+10],15,-1051523),a=p(a,s,n,o,t[r+1],21,-2054922799),o=p(o,a,s,n,t[r+8],6,1873313359),n=p(n,o,a,s,t[r+15],10,-30611744),s=p(s,n,o,a,t[r+6],15,-1560198380),a=p(a,s,n,o,t[r+13],21,1309151649),o=p(o,a,s,n,t[r+4],6,-145523070),n=p(n,o,a,s,t[r+11],10,-1120210379),s=p(s,n,o,a,t[r+2],15,718787259),a=p(a,s,n,o,t[r+9],21,-343485551),o=e(o,g),a=e(a,T),s=e(s,C),n=e(n,f);return[o,a,s,n]}function _(t){var h,r="",g=t.length*32;for(h=0;h<g;h+=8)r+=String.fromCharCode(t[h>>5]>>>h%32&255);return r}function D(t){var h,r=[];for(r[(t.length>>2)-1]=void 0,h=0;h<r.length;h+=1)r[h]=0;var g=t.length*8;for(h=0;h<g;h+=8)r[h>>5]|=(t.charCodeAt(h/8)&255)<<h%32;return r}function M(t){return _(S(D(t),t.length*8))}function x(t,h){var r,g=D(t),T=[],C=[],f;for(T[15]=C[15]=void 0,g.length>16&&(g=S(g,t.length*8)),r=0;r<16;r+=1)T[r]=g[r]^909522486,C[r]=g[r]^1549556828;return f=S(T.concat(D(h)),512+h.length*8),_(S(C.concat(f),512+128))}function v(t){var h="0123456789abcdef",r="",g,T;for(T=0;T<t.length;T+=1)g=t.charCodeAt(T),r+=h.charAt(g>>>4&15)+h.charAt(g&15);return r}function R(t){return unescape(encodeURIComponent(t))}function P(t){return M(R(t))}function F(t){return v(P(t))}function J(t,h){return x(R(t),R(h))}function ne(t,h){return v(J(t,h))}function H(t,h,r){return h?r?J(h,t):ne(h,t):r?P(t):F(t)}typeof define=="function"&&define.amd?define(function(){return H}):typeof w=="object"&&w.exports?w.exports=H:d.md5=H})(Y)});var Ie={};be(Ie,{CDNStreaming:()=>E,PublishMode:()=>N,TRTCStreamType:()=>G,default:()=>fe});var Z=j(K(),1);var N=(u=>(u.PublishMainStreamToCDN="publish-main-stream-to-cdn",u.PublishSubStreamToCDN="publish-sub-stream-to-cdn",u.PublishMixStreamToCDN="publish-mix-stream-to-cdn",u))(N||{}),G=(i=>(i.Main="main",i.Sub="sub",i))(G||{});var k={IT_AUDIO_VIDEO:0,IT_PICTURE:2,IT_CANVAS:3,IT_PURE_AUDIO:4,IT_PURE_VIDEO:5};var A=class{constructor(e){b(this,"_room");b(this,"_log");b(this,"_paramsForGivenCDN");b(this,"_publishGivenCDNData",null);b(this,"_isPublishingGivenCDN",!1);this._room=e.room,this._log=e.log,this._paramsForGivenCDN=new Map([["publish-main-stream-to-cdn",null],["publish-sub-stream-to-cdn",null],["publish-mix-stream-to-cdn",null]])}async startPublishGivenCDN(e){this._log.info(`startPublishGivenCDN() ${JSON.stringify(e)}`),this._paramsForGivenCDN.set(e.publishMode,e);let i={pushRequestTime:Date.now(),pushAppId:e.appId,pushBizId:e.bizId,pushCdnUrl:e.url,pushStreamType:this.convertStreamType(e==null?void 0:e.publishMode),pushStreamId:e.streamId};this._publishGivenCDNData=i;try{this._isPublishingGivenCDN&&await this.stopPublishGivenCDN();let u=await this._room.sendStartPublishCDN(i,!1),{code:m,message:c}=u.data;if(m===0)this._isPublishingGivenCDN=!0;else throw this.resetGivenCDN(),this._log.error(`startPublishGivenCDN failed, errCode: ${m}, errMsg: ${c}`),new Error(`startPublishGivenCDN failed, errCode: ${m} errMsg: ${c}`)}catch(u){throw this.resetGivenCDN(),u}}async stopPublishGivenCDN(){if(this._log.info(`stopPublishGivenCDN: ${JSON.stringify(this._publishGivenCDNData)}`),!this._isPublishingGivenCDN||!this._publishGivenCDNData){this.resetGivenCDN();return}let{pushAppId:e,pushBizId:i,pushCdnUrl:u,pushStreamType:m,pushStreamId:c}=this._publishGivenCDNData,l={pushRequestTime:Date.now(),pushAppId:e,pushBizId:i,pushCdnUrl:u,pushStreamType:m,pushStreamId:c},p=await this._room.sendStopPublishCDN(l,!1),{code:S,message:_}=p.data;if(S===0)this.resetGivenCDN();else throw this._log.error(`stopPublishGivenCDN failed, errCode: ${S} errMsg: ${_}`),new Error(`stopPublishGivenCDN failed, errCode: ${S} errMsg: ${_}`)}resetGivenCDN(){this._publishGivenCDNData=null,this._isPublishingGivenCDN=!1}convertStreamType(e){return e==="publish-main-stream-to-cdn"?"main":"aux"}};var L=class{constructor(e,i){b(this,"_core");b(this,"_room");b(this,"cdnLog");b(this,"_config",null);b(this,"_data",null);b(this,"_givenCDNManager");this._core=e,this._room=e.room,this.cdnLog=i,this.reset()}get isMixing(){return!!this._data}get isStarted(){return!!this._config}get hasCustomCDN(){var e,i,u;return((e=this._config)==null?void 0:e.target.appId)&&((i=this._config)==null?void 0:i.target.bizId)&&((u=this._config)==null?void 0:u.target.url)}async startMixTranscode(e){if(this.cdnLog.info(`startMixTranscode: ${e}`),this._config=e,this.installEvents(),!!this._core.room.isJoined)try{let i=this.getInputParam(e),u=this.getOutputParam(e),m=this.getOutputSessionId({config:e,roomId:this._room.roomId,userId:this._room.userId});this.isMixing&&this._data&&m!==this._data.outputSessionId&&(this.cdnLog.info("startMixTranscode() streamId changed, stop mixing before start"),await this.doStopMixTranscode()),await this.doStartMixTranscode({outputSessionId:m,inputParam:i,outputParam:u});let{appId:c,bizId:l,url:p,streamId:S=""}=this._config.target;c&&l&&p&&(this._givenCDNManager||(this._givenCDNManager=new A(this._core)),await this._givenCDNManager.startPublishGivenCDN({publishMode:e.target.publishMode,appId:c,bizId:l,url:p,streamId:S}))}catch(i){throw this.reset(),i}}async doStartMixTranscode({outputSessionId:e,inputParam:i,outputParam:u}){let m={roomId:String(this._room.roomId),mcuRequestTime:Date.now(),outputSessionId:e,inputParam:i,outputParam:u};this.cdnLog.info(`doStartMixTranscode: ${JSON.stringify(m)}`);let c=await this._room.sendStartMixTranscode(m),{code:l}=c.data,{message:p}=c.data;if(l===0)this._data=m;else throw l===-102083&&(p=`Please enable relayed-push in ${this._core.constants.CLOUD_CONSOLE_URL} and try later, refer to ${this._core.constants.DOC_URL}tutorial-26-advanced-publish-cdn-stream.html`),this.cdnLog.error(`startMixTranscode failed, errCode: ${l} errMsg: ${p}`),new Error(`startMixTranscode failed, errCode: ${l} errMsg: ${p}`)}async stopMixTranscode(){this.cdnLog.info(`stopMixTranscode: ${this.isMixing}`);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.cdnLog.info(`doStopMixTranscode: ${JSON.stringify(e)}`);let i=await this._room.sendStopMixTranscode(e),{code:u,message:m}=i.data;if(u===0)this.reset();else throw this.cdnLog.error(`stopMixTranscode failed, errCode: ${u} errMsg: ${m}`),new Error(`stopMixTranscode failed, errCode: ${u} errMsg: ${m}`)}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.cdnLog.info(`handleJoined() ${JSON.stringify(this._config)}`),this.isStarted&&this._config&&await this.startMixTranscode(this._config)}async handleRoomLeft(){this.cdnLog.info(`handleJoined() ${JSON.stringify(this._config)}`),this._data=null}getOutputSessionId({config:e,userId:i,roomId:u}){return this._core.utils.isString(e.target.streamId)&&e.target.streamId.length>0?e.target.streamId:(0,Z.default)(`${u}_${i}_main`)}getStringRoomId(e,i){return e?String(e):i}getInputParam(e){let{mix:i={}}=e,{audioMixUserList:u=[],videoLayoutList:m=[]}=i,c=m.map(l=>({userId:l.fixedVideoUser.userId,roomId:this.getStringRoomId(l.fixedVideoUser.roomId,l.fixedVideoUser.strRoomId)||this._core.room.roomId,width:l.width||0,height:l.height||0,locationX:l.locationX||0,locationY:l.locationY||0,zOrder:l.zOrder||1,streamType:l.fixedVideoStreamType==="sub"?1:0,inputType:k.IT_PURE_VIDEO,renderMode:l.fillMode||0}));return u.forEach(l=>{let p=c.findIndex(S=>S.userId===l.userId&&S.room===this.getStringRoomId(l.roomId,l.strRoomId));p!==-1?c[p].inputType=k.IT_AUDIO_VIDEO:c.push({userId:l.userId,roomId:l.roomId||l.strRoomId,inputType:k.IT_PURE_AUDIO})}),c}getOutputParam(e){let i=e.target.streamId||"",{encoding:u={},mix:m={}}=e;return{streamId:i,streamType:i.length>0?1:0,width:this._core.utils.isUndefined(u.videoWidth)?640:u.videoWidth,height:this._core.utils.isUndefined(u.videoHeight)?480:u.videoHeight,videoBps:u.videoBitrate||0,fps:u.videoFramerate||15,gop:u.videoGOP||2,audioSampleRate:u.audioSampleRate||48e3,audioBps:u.audioBitrate||64,audioChannels:u.audioChannels||1,backgroundColor:m.backgroundColor||0,backgroundImg:m.backgroundImage||"",extraInfo:"",videoCodec:2,audioCodec:0}}};var B=j(K(),1);var U=class{constructor(e,i){b(this,"_core");b(this,"_room");b(this,"cdnLog");b(this,"_paramsForTencentCDN");b(this,"_initParamsForTencentCDN",{isPublished:!1,isStarted:!1});this._core=e,this._room=e.room,this.cdnLog=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 m;this.cdnLog.info(`handlePublished: mediaType ${e.mediaType}, roomID ${(m=e==null?void 0:e.room)==null?void 0:m.roomId}`);let i=e.mediaType===4?"publish-main-stream-to-cdn":"publish-sub-stream-to-cdn",u=this._paramsForTencentCDN.get(i)||null;u!=null&&u.target&&u.isStarted&&await this.startPublishTencentCDN(u.target)}async handleRoomLeft(){this.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.cdnLog.info("Main stream has not already published, will auto reStart after published."),!1):e==="publish-sub-stream-to-cdn"&&!this._room.isAuxStreamPublished?(this.cdnLog.info("Sub has not already published, will auto reStart after published."),!1):!0}changeDataStatus(e,i){let u=this._paramsForTencentCDN.get(e),m=I(I({},u),i);this._paramsForTencentCDN.set(e,m)}async startPublishTencentCDN(e){if(this.cdnLog.info(`startPublishTencentCDN ${JSON.stringify(e)}`),this.installEvents(),this.changeDataStatus(e.publishMode,{target:e,isStarted:!0}),!this.isStreamPublished(e.publishMode))return;let i=e.streamId||"",u=this.generatePublishCDNStreamId(i,e.publishMode),m=this.generatePublishCDNSessionId(e.publishMode),c=e.publishMode==="publish-sub-stream-to-cdn"?1:0,l={requestTime:Date.now(),sessionId:m,streamId:u,streamType:c};await this.doStartPublishTencentCDN(l,e.publishMode);let{appId:p,bizId:S,url:_}=e;if(p&&S&&_){let D=this._paramsForTencentCDN.get(e.publishMode)||this._initParamsForTencentCDN;D!=null&&D.givenCDNManager||(D.givenCDNManager=new A(this._core),this._paramsForTencentCDN.set(e.publishMode,D)),await D.givenCDNManager.startPublishGivenCDN({publishMode:e.publishMode,appId:p,bizId:S,url:_,streamId:u})}}async doStartPublishTencentCDN(e,i){this.cdnLog.info(`doStartPublishTencentCDN: ${JSON.stringify(e)}`);try{let c=0;for(;;){let l=await this._room.sendStartPublishCDN(e,!0),{code:p}=l.data,{message:S}=l.data;if(p===0){this.cdnLog.info(`doStartPublishTencentCDN: success ${JSON.stringify(l)}`),this.changeDataStatus(i,{isPublished:!0});break}else if(p===-10006&&c<6)this.cdnLog.warn(`doStartPublishTencentCDN: retry...${c+1}/6, reason: ${S}`),c+=1,await new Promise(_=>setTimeout(_,500));else throw this.changeDataStatus(i,{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`),new Error(`startPublishTencentCDN failed, errCode: ${p} errMsg: ${S}`)}}catch(u){throw this.cdnLog.error(`startPublishTencentCDN failed, error: ${u}`),this.changeDataStatus(i,{isPublished:!1}),u}}async stopPublishTencentCDN(e){let i=this._paramsForTencentCDN.get(e)||this._initParamsForTencentCDN;if(i.isPublished){let{target:u}=i;u!=null&&u.bizId&&u.appId&&u.url&&(i!=null&&i.givenCDNManager)&&await(i==null?void 0:i.givenCDNManager.stopPublishGivenCDN()),await this.doStopPublishTencentCDN(e)}this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN)}async doStopPublishTencentCDN(e){let i={requestTime:Date.now(),sessionId:(0,B.default)(`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(e)}`)};this.cdnLog.info(`stopPublishTencentCDN: ${JSON.stringify(i)}`);let u=await this._room.sendStopPublishCDN(i,!0),{code:m,message:c}=u.data;if(m===0)this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN),this.reset(e);else if(m===-102069)this.cdnLog.error("stopPublishTencentCDN failed, can not stopPublishTencentCDN in auto relayed-push mode"),this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN);else throw this.cdnLog.error(`stopPublishTencentCDN failed, errCode: ${m} errMsg: ${c}`),new Error(`stopPublishTencentCDN failed, errCode: ${m} errMsg: ${c}`)}reset(e){this.uninstallEvents(),this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN)}generatePublishCDNStreamId(e,i){if(e===""){let u=`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(i)}`;return/^[A-Za-z\d_-]*$/.test(u)||(u=(0,B.default)(u)),`${this._room.sdkAppId}_${u}`}return e}convertPublishModeToStreamType(e){return e==="publish-main-stream-to-cdn"?"main":"aux"}generatePublishCDNSessionId(e){return(0,B.default)(`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(e)}`)}};var Q=d=>typeof d=="string";function Ce(d,e,i,u){if(!/^[A-Za-z\d_-]*$/.test(d))throw new Error("Invalid parameter streamId.")}function De(d,e,i,u){if(Q(d))throw new Error("roomId must be an integer but go a string, use 'parseInt' to convert it or use 'strRoomId' instead.");if(d===void 0)return;if(!(/^[1-9]\d*$/.test(String(d))&&d<4294967295))throw new Error("roomId must be an integer between [1, 4294967294].")}function _e(d,e,i,u){if(!/^[A-Za-z\d\s!#$%&()+\-:;<=.>?@[\]^_{}|~,]{1,64}$/.test(d))throw new Error("strRoomId must be a valid string.")}var O={type:"number",notLessThanZero:!0},ee={type:"object",required:!0,properties:{userId:{required:!0,type:"string"},roomId:{type:["string","number"],validate:De},strRoomId:{type:"string",validate:_e}}},te={required:!0,properties:{publishMode:{required:!0,values:["publish-main-stream-to-cdn","publish-mix-stream-to-cdn","publish-sub-stream-to-cdn"]},streamId:{required:!1,type:"string",validate:Ce},appId:{type:"number",allowEmpty:!1},bizId:{type:"number",allowEmpty:!1},url:{type:"string",allowEmpty:!1}}},re={required:!1,type:"object",properties:{videoWidth:O,videoHeight:O,videoBitrate:W(I({},O),{allowEmpty:!1}),videoFramerate:{type:"number",validate(d,e,i,u){if(d<=0||d>30)throw new Error("Invalid parameter mixConfig -> videoFramerate, the value must be between (0, 30].")}},videoGOP:{type:"number",validate(d,e,i,u){if(d<1||d>8)throw new Error("Invalid parameter mixConfig -> videoGOP, the value must be between [1, 8].")}},audioSampleRate:O,audioBitrate:{type:"number",validate(d,e,i,u){if(d<32||d>192)throw new Error("Invalid parameter mixConfig -> audioBitrate, the value must be between [32, 192].")}},audioChannels:{type:"number",values:[1,2]}}},ie={required:!1,type:"object",properties:{backgroundColor:{type:"number"},backgroundImage:{type:"string"},audioMixUserList:{type:"array",arrayItem:I({},ee)},videoLayoutList:{type:"array",required:!0,arrayItem:{type:"object",properties:{fixedVideoUser:I({},ee),fixedVideoStreamType:{type:"string",required:!0,values:["main","sub"]},fillMode:{type:"number",values:[0,1,2,4]},zOrder:{type:"number",required:!0,validate(d,e,i,u){if(d<1||d>15)throw new Error("Invalid parameter mix -> videoLayoutList -> zOrder, the value must be between [1, 15].")}},width:O,height:O,locationX:O,locationY:O}}}}};function oe(d){return{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:I({},te),encoding:I({},re),mix:I({},ie)},validate(e,i,u,m){var S,_;let{publishMode:c}=e.target,{encoding:l,mix:p}=e;if(c==="publish-mix-stream-to-cdn"){if(!l||!p)throw new Error("Invalid parameter mix & encoding, the value must be not empty.");if(p&&p.videoLayoutList){let D=0,M=0,x=[];if(p.videoLayoutList.forEach((P,F)=>{x.push(P.fixedVideoUser.userId),P.width+P.locationX>D&&(D=P.width+P.locationX),P.height+P.locationY>M&&(M=P.height+P.locationY)}),x.indexOf(d.room.userId)<0)throw new Error("Invalid parameter mix -> videoLayoutList, the value must be include self.");let v=(S=l==null?void 0:l.videoWidth)!=null?S:640,R=(_=l==null?void 0:l.videoHeight)!=null?_:480;if(v<D||R<M)throw new Error("Invalid parameter encoding, the width and height of the mixed video must encompass all the mixed-in video streams.")}}}}}function ae(d){return{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:I({},te),encoding:I({},re),mix:I({},ie)}}}function se(d){return{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 $=0,E=class{constructor(e){this.core=e;b(this,"_mixTranscodeManager");b(this,"_publishCDNManager");b(this,"_core");b(this,"cdnLog");b(this,"_modeOptions");b(this,"seq");$=$+1,this.seq=$,this.cdnLog=e.log.createChild({id:`${this.getAlias()}${$}`}),this._core=e,this.cdnLog.info("CDNStreaming created"),this._modeOptions=new Map,this._mixTranscodeManager=new L(e,this.cdnLog),this._publishCDNManager=new U(e,this.cdnLog)}getName(){return"CDNStreaming"}getAlias(){return"cdn"}getValidateRule(e){switch(e){case"start":return oe(this._core);case"update":return ae(this._core);case"stop":return se(this._core)}}getGroup(e){return e.target.publishMode}async start(e){return this.cdnLog.info(`Plugin CDNStreaming start: ${JSON.stringify(e)}`),this._modeOptions.set(e.target.publishMode,e),await this.doStart(e)}async update(e){this.cdnLog.info(`Plugin CDNStreaming update: ${JSON.stringify(e)}`);let i=this._modeOptions.get(e.target.publishMode);return this._core.utils.deepMerge(i,e),await this.doStart(i)}async stop(e){switch(this.cdnLog.info(`Plugin CDNStreaming stop: ${JSON.stringify(e)}`),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.cdnLog.info(`Plugin 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}}}};b(E,"TYPE",{PublishMode:{PublishMainStreamToCDN:"publish-main-stream-to-cdn",PublishSubStreamToCDN:"publish-sub-stream-to-cdn",PublishMixStreamToCDN:"publish-mix-stream-to-cdn"}});var fe=E;return Se(Ie);})().default;
1
+ "use strict";var CDNStreaming=(()=>{var ue=Object.create;var L=Object.defineProperty,de=Object.defineProperties,me=Object.getOwnPropertyDescriptor,le=Object.getOwnPropertyDescriptors,he=Object.getOwnPropertyNames,q=Object.getOwnPropertySymbols,ce=Object.getPrototypeOf,X=Object.prototype.hasOwnProperty,pe=Object.prototype.propertyIsEnumerable;var V=(d,e,i)=>e in d?L(d,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):d[e]=i,I=(d,e)=>{for(var i in e||(e={}))X.call(e,i)&&V(d,i,e[i]);if(q)for(var i of q(e))pe.call(e,i)&&V(d,i,e[i]);return d},W=(d,e)=>de(d,le(e));var ge=(d,e)=>()=>(e||d((e={exports:{}}).exports,e),e.exports),be=(d,e)=>{for(var i in e)L(d,i,{get:e[i],enumerable:!0})},z=(d,e,i,u)=>{if(e&&typeof e=="object"||typeof e=="function")for(let m of he(e))!X.call(d,m)&&m!==i&&L(d,m,{get:()=>e[m],enumerable:!(u=me(e,m))||u.enumerable});return d};var j=(d,e,i)=>(i=d!=null?ue(ce(d)):{},z(e||!d||!d.__esModule?L(i,"default",{value:d,enumerable:!0}):i,d)),Se=d=>z(L({},"__esModule",{value:!0}),d);var b=(d,e,i)=>(V(d,typeof e!="symbol"?e+"":e,i),i);var K=ge((Y,w)=>{(function(d){"use strict";function e(t,h){var r=(t&65535)+(h&65535),g=(t>>16)+(h>>16)+(r>>16);return g<<16|r&65535}function i(t,h){return t<<h|t>>>32-h}function u(t,h,r,g,T,C){return e(i(e(e(h,t),e(g,C)),T),r)}function m(t,h,r,g,T,C,f){return u(h&r|~h&g,t,h,T,C,f)}function c(t,h,r,g,T,C,f){return u(h&g|r&~g,t,h,T,C,f)}function l(t,h,r,g,T,C,f){return u(h^r^g,t,h,T,C,f)}function p(t,h,r,g,T,C,f){return u(r^(h|~g),t,h,T,C,f)}function S(t,h){t[h>>5]|=128<<h%32,t[(h+64>>>9<<4)+14]=h;var r,g,T,C,f,o=1732584193,a=-271733879,s=-1732584194,n=271733878;for(r=0;r<t.length;r+=16)g=o,T=a,C=s,f=n,o=m(o,a,s,n,t[r],7,-680876936),n=m(n,o,a,s,t[r+1],12,-389564586),s=m(s,n,o,a,t[r+2],17,606105819),a=m(a,s,n,o,t[r+3],22,-1044525330),o=m(o,a,s,n,t[r+4],7,-176418897),n=m(n,o,a,s,t[r+5],12,1200080426),s=m(s,n,o,a,t[r+6],17,-1473231341),a=m(a,s,n,o,t[r+7],22,-45705983),o=m(o,a,s,n,t[r+8],7,1770035416),n=m(n,o,a,s,t[r+9],12,-1958414417),s=m(s,n,o,a,t[r+10],17,-42063),a=m(a,s,n,o,t[r+11],22,-1990404162),o=m(o,a,s,n,t[r+12],7,1804603682),n=m(n,o,a,s,t[r+13],12,-40341101),s=m(s,n,o,a,t[r+14],17,-1502002290),a=m(a,s,n,o,t[r+15],22,1236535329),o=c(o,a,s,n,t[r+1],5,-165796510),n=c(n,o,a,s,t[r+6],9,-1069501632),s=c(s,n,o,a,t[r+11],14,643717713),a=c(a,s,n,o,t[r],20,-373897302),o=c(o,a,s,n,t[r+5],5,-701558691),n=c(n,o,a,s,t[r+10],9,38016083),s=c(s,n,o,a,t[r+15],14,-660478335),a=c(a,s,n,o,t[r+4],20,-405537848),o=c(o,a,s,n,t[r+9],5,568446438),n=c(n,o,a,s,t[r+14],9,-1019803690),s=c(s,n,o,a,t[r+3],14,-187363961),a=c(a,s,n,o,t[r+8],20,1163531501),o=c(o,a,s,n,t[r+13],5,-1444681467),n=c(n,o,a,s,t[r+2],9,-51403784),s=c(s,n,o,a,t[r+7],14,1735328473),a=c(a,s,n,o,t[r+12],20,-1926607734),o=l(o,a,s,n,t[r+5],4,-378558),n=l(n,o,a,s,t[r+8],11,-2022574463),s=l(s,n,o,a,t[r+11],16,1839030562),a=l(a,s,n,o,t[r+14],23,-35309556),o=l(o,a,s,n,t[r+1],4,-1530992060),n=l(n,o,a,s,t[r+4],11,1272893353),s=l(s,n,o,a,t[r+7],16,-155497632),a=l(a,s,n,o,t[r+10],23,-1094730640),o=l(o,a,s,n,t[r+13],4,681279174),n=l(n,o,a,s,t[r],11,-358537222),s=l(s,n,o,a,t[r+3],16,-722521979),a=l(a,s,n,o,t[r+6],23,76029189),o=l(o,a,s,n,t[r+9],4,-640364487),n=l(n,o,a,s,t[r+12],11,-421815835),s=l(s,n,o,a,t[r+15],16,530742520),a=l(a,s,n,o,t[r+2],23,-995338651),o=p(o,a,s,n,t[r],6,-198630844),n=p(n,o,a,s,t[r+7],10,1126891415),s=p(s,n,o,a,t[r+14],15,-1416354905),a=p(a,s,n,o,t[r+5],21,-57434055),o=p(o,a,s,n,t[r+12],6,1700485571),n=p(n,o,a,s,t[r+3],10,-1894986606),s=p(s,n,o,a,t[r+10],15,-1051523),a=p(a,s,n,o,t[r+1],21,-2054922799),o=p(o,a,s,n,t[r+8],6,1873313359),n=p(n,o,a,s,t[r+15],10,-30611744),s=p(s,n,o,a,t[r+6],15,-1560198380),a=p(a,s,n,o,t[r+13],21,1309151649),o=p(o,a,s,n,t[r+4],6,-145523070),n=p(n,o,a,s,t[r+11],10,-1120210379),s=p(s,n,o,a,t[r+2],15,718787259),a=p(a,s,n,o,t[r+9],21,-343485551),o=e(o,g),a=e(a,T),s=e(s,C),n=e(n,f);return[o,a,s,n]}function _(t){var h,r="",g=t.length*32;for(h=0;h<g;h+=8)r+=String.fromCharCode(t[h>>5]>>>h%32&255);return r}function D(t){var h,r=[];for(r[(t.length>>2)-1]=void 0,h=0;h<r.length;h+=1)r[h]=0;var g=t.length*8;for(h=0;h<g;h+=8)r[h>>5]|=(t.charCodeAt(h/8)&255)<<h%32;return r}function M(t){return _(S(D(t),t.length*8))}function x(t,h){var r,g=D(t),T=[],C=[],f;for(T[15]=C[15]=void 0,g.length>16&&(g=S(g,t.length*8)),r=0;r<16;r+=1)T[r]=g[r]^909522486,C[r]=g[r]^1549556828;return f=S(T.concat(D(h)),512+h.length*8),_(S(C.concat(f),512+128))}function v(t){var h="0123456789abcdef",r="",g,T;for(T=0;T<t.length;T+=1)g=t.charCodeAt(T),r+=h.charAt(g>>>4&15)+h.charAt(g&15);return r}function R(t){return unescape(encodeURIComponent(t))}function P(t){return M(R(t))}function F(t){return v(P(t))}function J(t,h){return x(R(t),R(h))}function ne(t,h){return v(J(t,h))}function H(t,h,r){return h?r?J(h,t):ne(h,t):r?P(t):F(t)}typeof define=="function"&&define.amd?define(function(){return H}):typeof w=="object"&&w.exports?w.exports=H:d.md5=H})(Y)});var Ie={};be(Ie,{CDNStreaming:()=>E,PublishMode:()=>N,TRTCStreamType:()=>G,default:()=>fe});var Z=j(K(),1);var N=(u=>(u.PublishMainStreamToCDN="publish-main-stream-to-cdn",u.PublishSubStreamToCDN="publish-sub-stream-to-cdn",u.PublishMixStreamToCDN="publish-mix-stream-to-cdn",u))(N||{}),G=(i=>(i.Main="main",i.Sub="sub",i))(G||{});var k={IT_AUDIO_VIDEO:0,IT_PICTURE:2,IT_CANVAS:3,IT_PURE_AUDIO:4,IT_PURE_VIDEO:5};var A=class{constructor(e){b(this,"_room");b(this,"_log");b(this,"_paramsForGivenCDN");b(this,"_publishGivenCDNData",null);b(this,"_isPublishingGivenCDN",!1);this._room=e.room,this._log=e.log,this._paramsForGivenCDN=new Map([["publish-main-stream-to-cdn",null],["publish-sub-stream-to-cdn",null],["publish-mix-stream-to-cdn",null]])}async startPublishGivenCDN(e){this._log.info(`startPublishGivenCDN() ${JSON.stringify(e)}`),this._paramsForGivenCDN.set(e.publishMode,e);let i={pushRequestTime:Date.now(),pushAppId:e.appId,pushBizId:e.bizId,pushCdnUrl:e.url,pushStreamType:this.convertStreamType(e==null?void 0:e.publishMode),pushStreamId:e.streamId};this._publishGivenCDNData=i;try{this._isPublishingGivenCDN&&await this.stopPublishGivenCDN();let u=await this._room.sendStartPublishCDN(i,!1),{code:m,message:c}=u.data;if(m===0)this._isPublishingGivenCDN=!0;else throw this.resetGivenCDN(),this._log.error(`startPublishGivenCDN failed, errCode: ${m}, errMsg: ${c}`),new Error(`startPublishGivenCDN failed, errCode: ${m} errMsg: ${c}`)}catch(u){throw this.resetGivenCDN(),u}}async stopPublishGivenCDN(){if(this._log.info(`stopPublishGivenCDN: ${JSON.stringify(this._publishGivenCDNData)}`),!this._isPublishingGivenCDN||!this._publishGivenCDNData){this.resetGivenCDN();return}let{pushAppId:e,pushBizId:i,pushCdnUrl:u,pushStreamType:m,pushStreamId:c}=this._publishGivenCDNData,l={pushRequestTime:Date.now(),pushAppId:e,pushBizId:i,pushCdnUrl:u,pushStreamType:m,pushStreamId:c},p=await this._room.sendStopPublishCDN(l,!1),{code:S,message:_}=p.data;if(S===0)this.resetGivenCDN();else throw this._log.error(`stopPublishGivenCDN failed, errCode: ${S} errMsg: ${_}`),new Error(`stopPublishGivenCDN failed, errCode: ${S} errMsg: ${_}`)}resetGivenCDN(){this._publishGivenCDNData=null,this._isPublishingGivenCDN=!1}convertStreamType(e){return e==="publish-main-stream-to-cdn"?"main":"aux"}};var y=class{constructor(e,i){b(this,"_core");b(this,"_room");b(this,"cdnLog");b(this,"_config",null);b(this,"_data",null);b(this,"_givenCDNManager");this._core=e,this._room=e.room,this.cdnLog=i,this.reset()}get isMixing(){return!!this._data}get isStarted(){return!!this._config}get hasCustomCDN(){var e,i,u;return((e=this._config)==null?void 0:e.target.appId)&&((i=this._config)==null?void 0:i.target.bizId)&&((u=this._config)==null?void 0:u.target.url)}async startMixTranscode(e){if(this.cdnLog.info(`startMixTranscode: ${e}`),this._config=e,this.installEvents(),!!this._core.room.isJoined)try{let i=this.getInputParam(e),u=this.getOutputParam(e),m=this.getOutputSessionId({config:e,roomId:this._room.roomId,userId:this._room.userId});this.isMixing&&this._data&&m!==this._data.outputSessionId&&(this.cdnLog.info("startMixTranscode() streamId changed, stop mixing before start"),await this.doStopMixTranscode()),await this.doStartMixTranscode({outputSessionId:m,inputParam:i,outputParam:u});let{appId:c,bizId:l,url:p,streamId:S=""}=this._config.target;c&&l&&p&&(this._givenCDNManager||(this._givenCDNManager=new A(this._core)),await this._givenCDNManager.startPublishGivenCDN({publishMode:e.target.publishMode,appId:c,bizId:l,url:p,streamId:S}))}catch(i){throw this.reset(),i}}async doStartMixTranscode({outputSessionId:e,inputParam:i,outputParam:u}){let m={roomId:String(this._room.roomId),mcuRequestTime:Date.now(),outputSessionId:e,inputParam:i,outputParam:u};this.cdnLog.info(`doStartMixTranscode: ${JSON.stringify(m)}`);let c=await this._room.sendStartMixTranscode(m),{code:l}=c.data,{message:p}=c.data;if(l===0)this._data=m;else throw l===-102083&&(p=`Please enable relayed-push in ${this._core.constants.CLOUD_CONSOLE_URL} and try later, refer to ${this._core.constants.DOC_URL}tutorial-26-advanced-publish-cdn-stream.html`),this.cdnLog.error(`startMixTranscode failed, errCode: ${l} errMsg: ${p}`),new Error(`startMixTranscode failed, errCode: ${l} errMsg: ${p}`)}async stopMixTranscode(){this.cdnLog.info(`stopMixTranscode: ${this.isMixing}`);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.cdnLog.info(`doStopMixTranscode: ${JSON.stringify(e)}`);let i=await this._room.sendStopMixTranscode(e),{code:u,message:m}=i.data;if(u===0)this.reset();else throw this.cdnLog.error(`stopMixTranscode failed, errCode: ${u} errMsg: ${m}`),new Error(`stopMixTranscode failed, errCode: ${u} errMsg: ${m}`)}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.cdnLog.info(`handleJoined() ${JSON.stringify(this._config)}`),this.isStarted&&this._config&&await this.startMixTranscode(this._config)}async handleRoomLeft(){this.cdnLog.info(`handleJoined() ${JSON.stringify(this._config)}`),this._data=null}getOutputSessionId({config:e,userId:i,roomId:u}){return this._core.utils.isString(e.target.streamId)&&e.target.streamId.length>0?e.target.streamId:(0,Z.default)(`${u}_${i}_main`)}getStringRoomId(e,i){return e?String(e):i}getInputParam(e){let{mix:i={}}=e,{audioMixUserList:u=[],videoLayoutList:m=[]}=i,c=m.map(l=>({userId:l.fixedVideoUser.userId,roomId:this.getStringRoomId(l.fixedVideoUser.roomId,l.fixedVideoUser.strRoomId)||this._core.room.roomId,width:l.width||0,height:l.height||0,locationX:l.locationX||0,locationY:l.locationY||0,zOrder:l.zOrder||1,streamType:l.fixedVideoStreamType==="sub"?1:0,inputType:k.IT_PURE_VIDEO,renderMode:l.fillMode||0}));return u.forEach(l=>{let p=c.findIndex(S=>S.userId===l.userId&&S.room===this.getStringRoomId(l.roomId,l.strRoomId));p!==-1?c[p].inputType=k.IT_AUDIO_VIDEO:c.push({userId:l.userId,roomId:l.roomId||l.strRoomId,inputType:k.IT_PURE_AUDIO})}),c}getOutputParam(e){let i=e.target.streamId||"",{encoding:u={},mix:m={}}=e;return{streamId:i,streamType:i.length>0?1:0,width:this._core.utils.isUndefined(u.videoWidth)?640:u.videoWidth,height:this._core.utils.isUndefined(u.videoHeight)?480:u.videoHeight,videoBps:u.videoBitrate||0,fps:u.videoFramerate||15,gop:u.videoGOP||2,audioSampleRate:u.audioSampleRate||48e3,audioBps:u.audioBitrate||64,audioChannels:u.audioChannels||1,backgroundColor:m.backgroundColor||0,backgroundImg:m.backgroundImage||"",extraInfo:"",videoCodec:2,audioCodec:0}}};var B=j(K(),1);var U=class{constructor(e,i){b(this,"_core");b(this,"_room");b(this,"cdnLog");b(this,"_paramsForTencentCDN");b(this,"_initParamsForTencentCDN",{isPublished:!1,isStarted:!1});this._core=e,this._room=e.room,this.cdnLog=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 m;this.cdnLog.info(`handlePublished: mediaType ${e.mediaType}, roomID ${(m=e==null?void 0:e.room)==null?void 0:m.roomId}`);let i=e.mediaType===4?"publish-main-stream-to-cdn":"publish-sub-stream-to-cdn",u=this._paramsForTencentCDN.get(i)||null;u!=null&&u.target&&u.isStarted&&await this.startPublishTencentCDN(u.target)}async handleRoomLeft(){this.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.cdnLog.info("Main stream has not already published, will auto reStart after published."),!1):e==="publish-sub-stream-to-cdn"&&!this._room.isAuxStreamPublished?(this.cdnLog.info("Sub has not already published, will auto reStart after published."),!1):!0}changeDataStatus(e,i){let u=this._paramsForTencentCDN.get(e),m=I(I({},u),i);this._paramsForTencentCDN.set(e,m)}async startPublishTencentCDN(e){if(this.cdnLog.info(`startPublishTencentCDN ${JSON.stringify(e)}`),this.installEvents(),this.changeDataStatus(e.publishMode,{target:e,isStarted:!0}),!this.isStreamPublished(e.publishMode))return;let i=e.streamId||"",u=this.generatePublishCDNStreamId(i,e.publishMode),m=this.generatePublishCDNSessionId(e.publishMode),c=e.publishMode==="publish-sub-stream-to-cdn"?1:0,l={requestTime:Date.now(),sessionId:m,streamId:u,streamType:c};await this.doStartPublishTencentCDN(l,e.publishMode);let{appId:p,bizId:S,url:_}=e;if(p&&S&&_){let D=this._paramsForTencentCDN.get(e.publishMode)||this._initParamsForTencentCDN;D!=null&&D.givenCDNManager||(D.givenCDNManager=new A(this._core),this._paramsForTencentCDN.set(e.publishMode,D)),await D.givenCDNManager.startPublishGivenCDN({publishMode:e.publishMode,appId:p,bizId:S,url:_,streamId:u})}}async doStartPublishTencentCDN(e,i){this.cdnLog.info(`doStartPublishTencentCDN: ${JSON.stringify(e)}`);try{let c=0;for(;;){let l=await this._room.sendStartPublishCDN(e,!0),{code:p}=l.data,{message:S}=l.data;if(p===0){this.cdnLog.info(`doStartPublishTencentCDN: success ${JSON.stringify(l)}`),this.changeDataStatus(i,{isPublished:!0});break}else if(p===-10006&&c<6)this.cdnLog.warn(`doStartPublishTencentCDN: retry...${c+1}/6, reason: ${S}`),c+=1,await new Promise(_=>setTimeout(_,500));else throw this.changeDataStatus(i,{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`),new Error(`startPublishTencentCDN failed, errCode: ${p} errMsg: ${S}`)}}catch(u){throw this.cdnLog.error(`startPublishTencentCDN failed, error: ${u}`),this.changeDataStatus(i,{isPublished:!1}),u}}async stopPublishTencentCDN(e){let i=this._paramsForTencentCDN.get(e)||this._initParamsForTencentCDN;if(i.isPublished){let{target:u}=i;u!=null&&u.bizId&&u.appId&&u.url&&(i!=null&&i.givenCDNManager)&&await(i==null?void 0:i.givenCDNManager.stopPublishGivenCDN()),await this.doStopPublishTencentCDN(e)}this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN)}async doStopPublishTencentCDN(e){let i={requestTime:Date.now(),sessionId:(0,B.default)(`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(e)}`)};this.cdnLog.info(`stopPublishTencentCDN: ${JSON.stringify(i)}`);let u=await this._room.sendStopPublishCDN(i,!0),{code:m,message:c}=u.data;if(m===0)this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN),this.reset(e);else if(m===-102069)this.cdnLog.error("stopPublishTencentCDN failed, can not stopPublishTencentCDN in auto relayed-push mode"),this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN);else throw this.cdnLog.error(`stopPublishTencentCDN failed, errCode: ${m} errMsg: ${c}`),new Error(`stopPublishTencentCDN failed, errCode: ${m} errMsg: ${c}`)}reset(e){this.uninstallEvents(),this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN)}generatePublishCDNStreamId(e,i){if(e===""){let u=`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(i)}`;return/^[A-Za-z\d_-]*$/.test(u)||(u=(0,B.default)(u)),`${this._room.sdkAppId}_${u}`}return e}convertPublishModeToStreamType(e){return e==="publish-main-stream-to-cdn"?"main":"aux"}generatePublishCDNSessionId(e){return(0,B.default)(`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(e)}`)}};var Q=d=>typeof d=="string";function Ce(d,e,i,u){if(!/^[A-Za-z\d_-]*$/.test(d))throw new Error("Invalid parameter streamId.")}function De(d,e,i,u){if(Q(d))throw new Error("roomId must be an integer but go a string, use 'parseInt' to convert it or use 'strRoomId' instead.");if(d===void 0)return;if(!(/^[1-9]\d*$/.test(String(d))&&d<4294967295))throw new Error("roomId must be an integer between [1, 4294967294].")}function _e(d,e,i,u){if(!/^[A-Za-z\d\s!#$%&()+\-:;<=.>?@[\]^_{}|~,]{1,64}$/.test(d))throw new Error("strRoomId must be a valid string.")}var O={type:"number",notLessThanZero:!0},ee={type:"object",required:!0,properties:{userId:{required:!0,type:"string"},roomId:{type:["string","number"],validate:De},strRoomId:{type:"string",validate:_e}}},te={required:!0,properties:{publishMode:{required:!0,values:["publish-main-stream-to-cdn","publish-mix-stream-to-cdn","publish-sub-stream-to-cdn"]},streamId:{required:!1,type:"string",validate:Ce},appId:{type:"number",allowEmpty:!1},bizId:{type:"number",allowEmpty:!1},url:{type:"string",allowEmpty:!1}}},re={required:!1,type:"object",properties:{videoWidth:O,videoHeight:O,videoBitrate:W(I({},O),{allowEmpty:!1}),videoFramerate:{type:"number",validate(d,e,i,u){if(d<=0||d>30)throw new Error("Invalid parameter mixConfig -> videoFramerate, the value must be between (0, 30].")}},videoGOP:{type:"number",validate(d,e,i,u){if(d<1||d>8)throw new Error("Invalid parameter mixConfig -> videoGOP, the value must be between [1, 8].")}},audioSampleRate:O,audioBitrate:{type:"number",validate(d,e,i,u){if(d<32||d>192)throw new Error("Invalid parameter mixConfig -> audioBitrate, the value must be between [32, 192].")}},audioChannels:{type:"number",values:[1,2]}}},ie={required:!1,type:"object",properties:{backgroundColor:{type:"number"},backgroundImage:{type:"string"},audioMixUserList:{type:"array",arrayItem:I({},ee)},videoLayoutList:{type:"array",required:!0,arrayItem:{type:"object",properties:{fixedVideoUser:I({},ee),fixedVideoStreamType:{type:"string",required:!0,values:["main","sub"]},fillMode:{type:"number",values:[0,1,2,4]},zOrder:{type:"number",required:!0,validate(d,e,i,u){if(d<1||d>15)throw new Error("Invalid parameter mix -> videoLayoutList -> zOrder, the value must be between [1, 15].")}},width:O,height:O,locationX:O,locationY:O}}}}};function oe(d){return{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:I({},te),encoding:I({},re),mix:I({},ie)},validate(e,i,u,m){var S,_;let{publishMode:c}=e.target,{encoding:l,mix:p}=e;if(c==="publish-mix-stream-to-cdn"){if(!l||!p)throw new Error("Invalid parameter mix & encoding, the value must be not empty.");if(p&&p.videoLayoutList){let D=0,M=0,x=[];if(p.videoLayoutList.forEach((P,F)=>{x.push(P.fixedVideoUser.userId),P.width+P.locationX>D&&(D=P.width+P.locationX),P.height+P.locationY>M&&(M=P.height+P.locationY)}),x.indexOf(d.room.userId)<0)throw new Error("Invalid parameter mix -> videoLayoutList, the value must be include self.");let v=(S=l==null?void 0:l.videoWidth)!=null?S:640,R=(_=l==null?void 0:l.videoHeight)!=null?_:480;if(v<D||R<M)throw new Error("Invalid parameter encoding, the width and height of the mixed video must encompass all the mixed-in video streams.")}}}}}function ae(d){return{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:I({},te),encoding:I({},re),mix:I({},ie)}}}function se(d){return{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 $=0,E=class{constructor(e){this.core=e;b(this,"_mixTranscodeManager");b(this,"_publishCDNManager");b(this,"_core");b(this,"cdnLog");b(this,"_modeOptions");b(this,"seq");$=$+1,this.seq=$,this.cdnLog=e.log.createChild({id:`${this.getAlias()}${$}`}),this._core=e,this.cdnLog.info("CDNStreaming created"),this._modeOptions=new Map,this._mixTranscodeManager=new y(e,this.cdnLog),this._publishCDNManager=new U(e,this.cdnLog)}getName(){return"CDNStreaming"}getAlias(){return"cdn"}getValidateRule(e){switch(e){case"start":return oe(this._core);case"update":return ae(this._core);case"stop":return se(this._core)}}getGroup(e){return e.target.publishMode}async start(e){return this.cdnLog.info(`Plugin CDNStreaming start: ${JSON.stringify(e)}`),this._modeOptions.set(e.target.publishMode,e),await this.doStart(e)}async update(e){this.cdnLog.info(`Plugin CDNStreaming update: ${JSON.stringify(e)}`);let i=this._modeOptions.get(e.target.publishMode);return this._core.utils.deepMerge(i,e),await this.doStart(i)}async stop(e){switch(this.cdnLog.info(`Plugin CDNStreaming stop: ${JSON.stringify(e)}`),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.cdnLog.info(`Plugin 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}}}};b(E,"TYPE",{PublishMode:{PublishMainStreamToCDN:"publish-main-stream-to-cdn",PublishSubStreamToCDN:"publish-sub-stream-to-cdn",PublishMixStreamToCDN:"publish-mix-stream-to-cdn"}});var fe=E;return Se(Ie);})().default;
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rtc-plugin/cdn-streaming",
3
- "version": "5.1.0",
3
+ "version": "5.1.2",
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>",