trtc-sdk-v5 5.3.0-beta.4 → 5.3.0-beta.6
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/index.d.ts +142 -14
- package/package.json +1 -1
- package/plugins/cdn-streaming/cdn-streaming.esm.js +1 -1
- package/plugins/cdn-streaming/cdn-streaming.iife.js +1 -1
- package/plugins/cdn-streaming/package.json +1 -1
- package/plugins/video-effect/virtual-background/package.json +1 -1
- package/trtc.esm.js +19 -19
- package/trtc.js +1 -1
package/index.d.ts
CHANGED
|
@@ -8,13 +8,14 @@
|
|
|
8
8
|
declare interface LocalVideoConfig {
|
|
9
9
|
view?: string | HTMLElement | HTMLElement[] | null;
|
|
10
10
|
publish?: boolean;
|
|
11
|
+
mute?: boolean;
|
|
11
12
|
option?: {
|
|
12
13
|
cameraId?: string;
|
|
13
14
|
useFrontCamera?: boolean;
|
|
14
15
|
profile?: keyof typeof videoProfileMap | VideoProfile;
|
|
15
16
|
fillMode?: 'contain' | 'cover' | 'fill';
|
|
16
17
|
mirror?: boolean;
|
|
17
|
-
small?: keyof typeof videoProfileMap | VideoProfile;
|
|
18
|
+
small?: keyof typeof videoProfileMap | VideoProfile | boolean;
|
|
18
19
|
qosPreference?: typeof TRTCType.QOS_PREFERENCE_SMOOTH | typeof TRTCType.QOS_PREFERENCE_CLEAR;
|
|
19
20
|
videoTrack?: MediaStreamTrack;
|
|
20
21
|
};
|
|
@@ -84,6 +85,7 @@
|
|
|
84
85
|
}
|
|
85
86
|
declare interface LocalAudioConfig {
|
|
86
87
|
publish?: boolean;
|
|
88
|
+
mute?: boolean;
|
|
87
89
|
option?: {
|
|
88
90
|
microphoneId?: string;
|
|
89
91
|
profile?: keyof typeof audioProfileMap;
|
|
@@ -725,17 +727,6 @@
|
|
|
725
727
|
* });
|
|
726
728
|
*/
|
|
727
729
|
readonly PUBLISH_STATE_CHANGED: "publish-state-changed";
|
|
728
|
-
/**
|
|
729
|
-
* @private
|
|
730
|
-
* @description 收到 SEI message<br>
|
|
731
|
-
* @default 'sei-message'
|
|
732
|
-
* @memberof module:EVENT
|
|
733
|
-
* @example
|
|
734
|
-
* trtc.on(TRTC.EVENT.SEI_MESSAGE, event => {
|
|
735
|
-
* console.log(`收到 ${event.userId} 的 sei message: ${event.data}`)
|
|
736
|
-
* })
|
|
737
|
-
*/
|
|
738
|
-
readonly SEI_MESSAGE: "sei-message";
|
|
739
730
|
/**
|
|
740
731
|
* @description 通话相关数据指标。<br>
|
|
741
732
|
*
|
|
@@ -750,6 +741,30 @@
|
|
|
750
741
|
* })
|
|
751
742
|
*/
|
|
752
743
|
readonly STATISTICS: "statistics";
|
|
744
|
+
/**
|
|
745
|
+
* @description 收到 SEI message<br>
|
|
746
|
+
* @default 'sei-message'
|
|
747
|
+
* @memberof module:EVENT
|
|
748
|
+
* @example
|
|
749
|
+
* trtc.on(TRTC.EVENT.SEI_MESSAGE, event => {
|
|
750
|
+
* console.log(`收到 ${event.userId} 的 sei message: ${event.data}, streamType: ${event.streamType}`)
|
|
751
|
+
* })
|
|
752
|
+
*/
|
|
753
|
+
readonly SEI_MESSAGE: "sei-message";
|
|
754
|
+
/**
|
|
755
|
+
* @private
|
|
756
|
+
* @description 收到新的 MediaStreamTrack 对象
|
|
757
|
+
* @default 'track'
|
|
758
|
+
* @memberof module:EVENT
|
|
759
|
+
* @example
|
|
760
|
+
* trtc.on(TRTC.EVENT.TRACK, event => {
|
|
761
|
+
* const isLocal = event.userId === ''; // userId === '' means event.track is a local track, otherwise it's a remote track
|
|
762
|
+
* const isSubStream = event.streamType === TRTC.TYPE.STREAM_TYPE_SUB; // Usually the sub stream is a screen-sharing video stream.
|
|
763
|
+
* const mediaStreamTrack = event.track;
|
|
764
|
+
* const kind = event.track.kind; // audio or video
|
|
765
|
+
* })
|
|
766
|
+
*/
|
|
767
|
+
readonly TRACK: "track";
|
|
753
768
|
};
|
|
754
769
|
declare interface TRTCEventTypes {
|
|
755
770
|
[TRTCEvent.ERROR]: [RtcError];
|
|
@@ -819,8 +834,14 @@
|
|
|
819
834
|
[TRTCEvent.SEI_MESSAGE]: [{
|
|
820
835
|
data: Uint8Array;
|
|
821
836
|
userId: string;
|
|
837
|
+
streamType: TRTCStreamType;
|
|
822
838
|
}];
|
|
823
839
|
[TRTCEvent.STATISTICS]: [statistics: TRTCStatistics];
|
|
840
|
+
[TRTCEvent.TRACK]: [{
|
|
841
|
+
userId: string;
|
|
842
|
+
streamType?: TRTCStreamType;
|
|
843
|
+
track: MediaStreamTrack;
|
|
844
|
+
}];
|
|
824
845
|
}
|
|
825
846
|
class TRTC extends EventEmitter<TRTCEventTypes> {
|
|
826
847
|
/**
|
|
@@ -971,6 +992,7 @@
|
|
|
971
992
|
*
|
|
972
993
|
* @param {object} [config] - 配置项
|
|
973
994
|
* @param {boolean} [config.publish] - 是否将本地音频发布到房间中,默认为true。若在进房前调用该接口,并且 publish = true,则在进房后 SDK 会自动发布。可监听该事件获取推流状态 {@link module:EVENT.PUBLISH_STATE_CHANGED PUBLISH_STATE_CHANGED}。
|
|
995
|
+
* @param {boolean} [config.mute] - 是否静音麦克风。参考:{@tutorial 15-basic-dynamic-add-video}。
|
|
974
996
|
* @param {object} [config.option] - 本地音频选项
|
|
975
997
|
* @param {string} [config.option.microphoneId]- 指定使用哪个麦克风
|
|
976
998
|
* @param {MediaStreamTrack} [config.option.audioTrack] - 自定义采集的 audioTrack。{@tutorial 20-advanced-customized-capture-rendering}。
|
|
@@ -1004,7 +1026,7 @@
|
|
|
1004
1026
|
* - 本方法采用增量更新方式:只更新传入的参数,不传入的参数保持不变。
|
|
1005
1027
|
* @param {object} [config]
|
|
1006
1028
|
* @param {boolean} [config.publish] - 是否将本地音频发布到房间中,默认为 true。可监听该事件获取推流状态 {@link module:EVENT.PUBLISH_STATE_CHANGED PUBLISH_STATE_CHANGED}。
|
|
1007
|
-
* @param {boolean} [config.mute] -
|
|
1029
|
+
* @param {boolean} [config.mute] - 是否静音麦克风。参考:{@tutorial 15-basic-dynamic-add-video}。
|
|
1008
1030
|
* @param {object} [config.option] - 本地音频配置
|
|
1009
1031
|
* @param {string} [config.option.microphoneId] - 指定使用哪个麦克风,用来切换麦克风。
|
|
1010
1032
|
* @param {MediaStreamTrack} [config.option.audioTrack] - 自定义采集的 audioTrack。 {@tutorial 20-advanced-customized-capture-rendering}。
|
|
@@ -1084,6 +1106,7 @@
|
|
|
1084
1106
|
* @param {object} [config]
|
|
1085
1107
|
* @param {string | HTMLElement | HTMLElement[] | null} [config.view] - 本地视频预览的 HTMLElement 实例或者 Id, 如果不传或传入 null, 则不会播放视频。
|
|
1086
1108
|
* @param {boolean} [config.publish] - 是否将本地视频发布到房间中。默认为 true,若在进房前调用该接口,SDK 会在进房成功后自动发布(若 publish=true)。可监听该事件获取推流状态 {@link module:EVENT.PUBLISH_STATE_CHANGED PUBLISH_STATE_CHANGED}。
|
|
1109
|
+
* @param {boolean} [config.mute] - 是否暂停摄像头采集,参考:{@tutorial 15-basic-dynamic-add-video}。
|
|
1087
1110
|
* @param {object} [config.option] - 本地视频配置
|
|
1088
1111
|
* @param {string} [config.option.cameraId] - 指定使用哪个摄像头,用于切换摄像头。
|
|
1089
1112
|
* @param {boolean} [config.option.useFrontCamera] - 是否使用前置摄像头
|
|
@@ -1460,6 +1483,57 @@
|
|
|
1460
1483
|
*/
|
|
1461
1484
|
getAudioTrack(userId?: string): MediaStreamTrack | null;
|
|
1462
1485
|
setCurrentSpeaker(speakerId: string): void;
|
|
1486
|
+
/**
|
|
1487
|
+
* 发送 SEI Message <br>
|
|
1488
|
+
*
|
|
1489
|
+
* 视频帧的头部有一个叫做 SEI 的头部数据块,该接口的原理就是利用这个被称为 SEI 的头部数据块,将您要发送的自定义信令嵌入其中,使其同视频帧一并发送出去。
|
|
1490
|
+
* SEI 消息可以伴随着视频帧一直传输到直播 CDN 上。
|
|
1491
|
+
*
|
|
1492
|
+
* 适用场景:视频画面的精准布局、歌词同步、直播答题等。
|
|
1493
|
+
* 调用时机:在 {@link TRTC#startLocalVideo trtc.startLocalVideo} 后调用。
|
|
1494
|
+
*
|
|
1495
|
+
*
|
|
1496
|
+
* 注意:
|
|
1497
|
+
* 1. 单次最大发送1KB(Byte),每秒最大调用次数30次,每秒最多发送8KB。
|
|
1498
|
+
* 2. 目前仅支持 Chrome 86+, Edge 86+, Opera 72+ 浏览器。
|
|
1499
|
+
* 3. 由于 SEI 跟随视频帧一起发送,视频帧有丢失的可能,因此 SEI 也可能丢失。可在使用频次限制范围内,增加发送次数,业务侧需要在接收端做消息去重。
|
|
1500
|
+
* 4. 没有推视频流时,无法发送 SEI;没有订阅视频流时,无法接收 SEI。
|
|
1501
|
+
* 5. 仅支持 H264 编码器发送 SEI。
|
|
1502
|
+
* 6. 小流暂不支持 SEI 收发。
|
|
1503
|
+
* @see {@link module:EVENT.SEI_MESSAGE TRTC.EVENT.SEI_MESSAGE}
|
|
1504
|
+
* @since v5.3.0
|
|
1505
|
+
* @param {ArrayBuffer} buffer 待发送的 SEI 数据
|
|
1506
|
+
* @param {Object=} options
|
|
1507
|
+
* @param {Number} options.seiPayloadType 设置 SEI payload type。SDK 默认使用自定义 payloadType 243,业务侧可使用该参数将 payloadType 设置为标准的 5。当业务侧使用 5 payloadType 时,需按照规范确保 `buffer` 的前16字节是业务侧自定义的 uuid。
|
|
1508
|
+
* @example
|
|
1509
|
+
* // 1. 开启 SEI
|
|
1510
|
+
* const trtc = TRTC.create({
|
|
1511
|
+
* enableSEI: true // 开启 SEI 收发功能
|
|
1512
|
+
* })
|
|
1513
|
+
*
|
|
1514
|
+
* // 2. 推流端发送 SEI
|
|
1515
|
+
* try {
|
|
1516
|
+
* await trtc.enterRoom({
|
|
1517
|
+
* userId: 'user_1',
|
|
1518
|
+
* roomId: 12345,
|
|
1519
|
+
* })
|
|
1520
|
+
* await trtc.startLocalVideo();
|
|
1521
|
+
* const unit8Array = new Uint8Array([1, 2, 3]);
|
|
1522
|
+
* trtc.sendSEIMessage(unit8Array.buffer);
|
|
1523
|
+
* // 消息已通过限制,等待后续视频帧发送。
|
|
1524
|
+
* } catch(error) {
|
|
1525
|
+
* // 发送失败,可能原因:当前浏览器不支持、参数校验未通过、未推流、没推视频流、调用次数超出限制、使用非 H264 编码器等。
|
|
1526
|
+
* console.warn(error);
|
|
1527
|
+
* }
|
|
1528
|
+
*
|
|
1529
|
+
* // 3. 拉流端接收 SEI
|
|
1530
|
+
* trtc.on(TRTC.EVENT.SEI_MESSAGE, event => {
|
|
1531
|
+
* console.warn(`收到 ${event.userId} 的 sei ${event.data}`);
|
|
1532
|
+
* })
|
|
1533
|
+
*/
|
|
1534
|
+
sendSEIMessage(buffer: ArrayBuffer, options?: {
|
|
1535
|
+
seiPayloadType: number;
|
|
1536
|
+
}): void;
|
|
1463
1537
|
static EVENT: {
|
|
1464
1538
|
readonly ERROR: "error";
|
|
1465
1539
|
readonly AUTOPLAY_FAILED: "autoplay-failed";
|
|
@@ -1474,12 +1548,66 @@
|
|
|
1474
1548
|
readonly NETWORK_QUALITY: "network-quality";
|
|
1475
1549
|
readonly CONNECTION_STATE_CHANGED: "connection-state-changed";
|
|
1476
1550
|
readonly AUDIO_PLAY_STATE_CHANGED: "audio-play-state-changed";
|
|
1551
|
+
/**
|
|
1552
|
+
* 进入一个音视频通话房间(以下简称"进房")。<br>
|
|
1553
|
+
* - 进房代表开始一个音视频通话会话,只有进房成功后才能和房间内的其他用户进行音视频通话。
|
|
1554
|
+
* - 可以通过 {@link TRTC#startLocalVideo startLocalVideo()} 和 {@link TRTC#startLocalAudio startLocalAudio()}发布本地音视频流,发布成功后,房间内其他用户会收到
|
|
1555
|
+
* {@link module:EVENT.REMOTE_AUDIO_AVAILABLE REMOTE_AUDIO_AVAILABLE} 和 {@link module:EVENT.REMOTE_VIDEO_AVAILABLE REMOTE_VIDEO_AVAILABLE} 事件通知。
|
|
1556
|
+
* - 默认情况下 SDK 会自动播放远端音频,您需要在调用 {@link TRTC#startRemoteVideo startRemoteVideo()} 来播放远端视频画面。
|
|
1557
|
+
*
|
|
1558
|
+
* @param {object} options 进房参数
|
|
1559
|
+
* @param {number} options.sdkAppId sdkAppId <br>
|
|
1560
|
+
* 在 [实时音视频控制台](https://console.cloud.tencent.com/trtc) 单击 **应用管理** > **创建应用** 创建新应用之后,即可在 **应用信息** 中获取 sdkAppId 信息。
|
|
1561
|
+
* @param {string} options.userId 用户ID <br>
|
|
1562
|
+
* 建议限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
|
|
1563
|
+
* @param {string} options.userSig userSig 签名 <br>
|
|
1564
|
+
* 计算 userSig 的方式请参考 [UserSig 相关](https://cloud.tencent.com/document/product/647/17275)。
|
|
1565
|
+
* @param {number=} options.roomId
|
|
1566
|
+
* 数字类型的房间号,取值要求为 [1, 4294967294] 的整数;<br>
|
|
1567
|
+
* <font color="red">如果需要使用字符串类型的房间号请使用 strRoomId 参数,roomId 和 strRoomId 必须填一个。若两者都填,则优先选择 roomId。</font>
|
|
1568
|
+
* @param {string=} options.strRoomId
|
|
1569
|
+
* 字符串类型的房间号,限制长度为64字节,且仅支持以下范围的字符集:
|
|
1570
|
+
* - 大小写英文字母(a-zA-Z)
|
|
1571
|
+
* - 数字(0-9)
|
|
1572
|
+
* - 空格 ! # $ % & ( ) + - : ; < = . > ? @ [ ] ^ _ { } | ~ ,
|
|
1573
|
+
*
|
|
1574
|
+
* <font color="red">注意:建议采用数字类型的 roomId,字符串类型的房间号 "123" 与 数字类型的房间号 123 不互通。</font>
|
|
1575
|
+
* @param {string} [options.scene] 应用场景,目前支持以下两种场景:
|
|
1576
|
+
* - {@link module:TYPE.SCENE_RTC TRTC.TYPE.SCENE_RTC}(默认)实时通话场景,该模式适合 1对1 的音视频通话,或者参会人数在 300 人以内的在线会议。[支持最大50人同时开麦](https://web.sdk.qcloud.com/trtc/webrtc/v5/doc/zh-cn/tutorial-04-info-uplink-limits.html)。
|
|
1577
|
+
* - {@link module:TYPE.SCENE_LIVE TRTC.TYPE.SCENE_LIVE} 互动直播场景,该模式适合十万人以内的在线直播场景,但需要您在接下来介绍的 options 参数中指定 角色(role) 这个字段
|
|
1578
|
+
* @param {string=} [options.role] 用户角色,仅在 {@link module:TYPE.SCENE_LIVE TRTC.TYPE.SCENE_LIVE} 场景下有意义,{@link module:TYPE.SCENE_RTC TRTC.TYPE.SCENE_RTC} 场景无需指定 role,目前支持两种角色:
|
|
1579
|
+
* - {@link module:TYPE.ROLE_ANCHOR TRTC.TYPE.ROLE_ANCHOR}(默认) 主播
|
|
1580
|
+
* - {@link module:TYPE.ROLE_AUDIENCE TRTC.TYPE.ROLE_AUDIENCE} 观众
|
|
1581
|
+
* <br>
|
|
1582
|
+
* 注意:观众角色没有发布本地音视频的权限,只有收看远端流的权限。如果观众想要连麦跟主播互动,
|
|
1583
|
+
* 请先通过 {@link TRTC#switchRole switchRole()} 切换角色到主播后再发布本地音视频。
|
|
1584
|
+
* @param {boolean} [options.autoReceiveAudio=true] 是否自动接收音频。当远端用户发布音频后,SDK 自动播放远端用户的音频。
|
|
1585
|
+
* @param {boolean} [options.autoReceiveVideo=true] 是否自动接收视频。当远端用户发布视频后,SDK 自动拉流并解码远端视频,您需要调用 {@link TRTC#startRemoteVideo startRemoteVideo} 播放远端视频。
|
|
1586
|
+
* @param {boolean} [options.enableAutoPlayDialog] 是否开启 SDK 自动播放失败弹窗,默认:true。
|
|
1587
|
+
* - 默认开启,当出现自动播放失败时,SDK 会弹窗引导用户点击页面,来恢复音视频播放。
|
|
1588
|
+
* - 可设置为 false 关闭,建议接入侧参考{@tutorial 21-advanced-auto-play-policy}来处理自动播放失败相关问题。
|
|
1589
|
+
* @param {string=} options.userDefineRecordId 用于设置云端录制的 userDefineRecordId(选填)。
|
|
1590
|
+
* - 【推荐取值】限制长度为64字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
|
|
1591
|
+
* - 【参考文档】[云端录制](https://cloud.tencent.com/document/product/647/16823)。
|
|
1592
|
+
* @param {string|ProxyServer} [options.proxy] 设置代理服务器。参考最佳实践:{@tutorial 34-advanced-proxy}。
|
|
1593
|
+
* @param {boolean} [options.privateMapKey] 进房钥匙,若需要权限控制请携带该参数(传空或传错会导致进房失败)。<br>[privateMapKey 权限设置](https://cloud.tencent.com/document/product/647/32240)
|
|
1594
|
+
* @throws
|
|
1595
|
+
* - {@link module:ERROR_CODE.INVALID_PARAMETER INVALID_PARAMETER}
|
|
1596
|
+
* - {@link module:ERROR_CODE.OPERATION_FAILED OPERATION_FAILED}
|
|
1597
|
+
* - {@link module:ERROR_CODE.OPERATION_ABORT OPERATION_ABORT}
|
|
1598
|
+
* - {@link module:ERROR_CODE.ENV_NOT_SUPPORTED ENV_NOT_SUPPORTED}
|
|
1599
|
+
* - {@link module:ERROR_CODE.SERVER_ERROR SERVER_ERROR}
|
|
1600
|
+
* @example
|
|
1601
|
+
* const trtc = TRTC.create();
|
|
1602
|
+
* await trtc.enterRoom({ roomId: 8888, sdkAppId, userId, userSig });
|
|
1603
|
+
*/
|
|
1477
1604
|
readonly VIDEO_PLAY_STATE_CHANGED: "video-play-state-changed";
|
|
1478
1605
|
readonly SCREEN_SHARE_STOPPED: "screen-share-stopped";
|
|
1479
1606
|
readonly DEVICE_CHANGED: "device-changed";
|
|
1480
1607
|
readonly PUBLISH_STATE_CHANGED: "publish-state-changed";
|
|
1481
|
-
readonly SEI_MESSAGE: "sei-message";
|
|
1482
1608
|
readonly STATISTICS: "statistics";
|
|
1609
|
+
readonly SEI_MESSAGE: "sei-message";
|
|
1610
|
+
readonly TRACK: "track";
|
|
1483
1611
|
};
|
|
1484
1612
|
static ERROR_CODE: {
|
|
1485
1613
|
INVALID_PARAMETER: number;
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var ue=Object.create;var V=Object.defineProperty,de=Object.defineProperties,me=Object.getOwnPropertyDescriptor,le=Object.getOwnPropertyDescriptors,ce=Object.getOwnPropertyNames,X=Object.getOwnPropertySymbols,pe=Object.getPrototypeOf,W=Object.prototype.hasOwnProperty,he=Object.prototype.propertyIsEnumerable;var H=(m,e,i)=>e in m?V(m,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):m[e]=i,I=(m,e)=>{for(var i in e||(e={}))W.call(e,i)&&H(m,i,e[i]);if(X)for(var i of X(e))he.call(e,i)&&H(m,i,e[i]);return m},z=(m,e)=>de(m,le(e));var ge=(m,e)=>()=>(e||m((e={exports:{}}).exports,e),e.exports);var Se=(m,e,i,u)=>{if(e&&typeof e=="object"||typeof e=="function")for(let d of ce(e))!W.call(m,d)&&d!==i&&V(m,d,{get:()=>e[d],enumerable:!(u=me(e,d))||u.enumerable});return m};var j=(m,e,i)=>(i=m!=null?ue(pe(m)):{},Se(e||!m||!m.__esModule?V(i,"default",{value:m,enumerable:!0}):i,m));var S=(m,e,i)=>(H(m,typeof e!="symbol"?e+"":e,i),i);var K=ge((Y,k)=>{(function(m){"use strict";function e(t,c){var r=(t&65535)+(c&65535),g=(t>>16)+(c>>16)+(r>>16);return g<<16|r&65535}function i(t,c){return t<<c|t>>>32-c}function u(t,c,r,g,C,T){return e(i(e(e(c,t),e(g,T)),C),r)}function d(t,c,r,g,C,T,f){return u(c&r|~c&g,t,c,C,T,f)}function p(t,c,r,g,C,T,f){return u(c&g|r&~g,t,c,C,T,f)}function l(t,c,r,g,C,T,f){return u(c^r^g,t,c,C,T,f)}function h(t,c,r,g,C,T,f){return u(r^(c|~g),t,c,C,T,f)}function b(t,c){t[c>>5]|=128<<c%32,t[(c+64>>>9<<4)+14]=c;var r,g,C,T,f,o=1732584193,a=-271733879,s=-1732584194,n=271733878;for(r=0;r<t.length;r+=16)g=o,C=a,T=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=p(o,a,s,n,t[r+1],5,-165796510),n=p(n,o,a,s,t[r+6],9,-1069501632),s=p(s,n,o,a,t[r+11],14,643717713),a=p(a,s,n,o,t[r],20,-373897302),o=p(o,a,s,n,t[r+5],5,-701558691),n=p(n,o,a,s,t[r+10],9,38016083),s=p(s,n,o,a,t[r+15],14,-660478335),a=p(a,s,n,o,t[r+4],20,-405537848),o=p(o,a,s,n,t[r+9],5,568446438),n=p(n,o,a,s,t[r+14],9,-1019803690),s=p(s,n,o,a,t[r+3],14,-187363961),a=p(a,s,n,o,t[r+8],20,1163531501),o=p(o,a,s,n,t[r+13],5,-1444681467),n=p(n,o,a,s,t[r+2],9,-51403784),s=p(s,n,o,a,t[r+7],14,1735328473),a=p(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=h(o,a,s,n,t[r],6,-198630844),n=h(n,o,a,s,t[r+7],10,1126891415),s=h(s,n,o,a,t[r+14],15,-1416354905),a=h(a,s,n,o,t[r+5],21,-57434055),o=h(o,a,s,n,t[r+12],6,1700485571),n=h(n,o,a,s,t[r+3],10,-1894986606),s=h(s,n,o,a,t[r+10],15,-1051523),a=h(a,s,n,o,t[r+1],21,-2054922799),o=h(o,a,s,n,t[r+8],6,1873313359),n=h(n,o,a,s,t[r+15],10,-30611744),s=h(s,n,o,a,t[r+6],15,-1560198380),a=h(a,s,n,o,t[r+13],21,1309151649),o=h(o,a,s,n,t[r+4],6,-145523070),n=h(n,o,a,s,t[r+11],10,-1120210379),s=h(s,n,o,a,t[r+2],15,718787259),a=h(a,s,n,o,t[r+9],21,-343485551),o=e(o,g),a=e(a,C),s=e(s,T),n=e(n,f);return[o,a,s,n]}function _(t){var c,r="",g=t.length*32;for(c=0;c<g;c+=8)r+=String.fromCharCode(t[c>>5]>>>c%32&255);return r}function D(t){var c,r=[];for(r[(t.length>>2)-1]=void 0,c=0;c<r.length;c+=1)r[c]=0;var g=t.length*8;for(c=0;c<g;c+=8)r[c>>5]|=(t.charCodeAt(c/8)&255)<<c%32;return r}function R(t){return _(b(D(t),t.length*8))}function x(t,c){var r,g=D(t),C=[],T=[],f;for(C[15]=T[15]=void 0,g.length>16&&(g=b(g,t.length*8)),r=0;r<16;r+=1)C[r]=g[r]^909522486,T[r]=g[r]^1549556828;return f=b(C.concat(D(c)),512+c.length*8),_(b(T.concat(f),512+128))}function v(t){var c="0123456789abcdef",r="",g,C;for(C=0;C<t.length;C+=1)g=t.charCodeAt(C),r+=c.charAt(g>>>4&15)+c.charAt(g&15);return r}function y(t){return unescape(encodeURIComponent(t))}function O(t){return R(y(t))}function q(t){return v(O(t))}function J(t,c){return x(y(t),y(c))}function ne(t,c){return v(J(t,c))}function $(t,c,r){return c?r?J(c,t):ne(c,t):r?O(t):q(t)}typeof define=="function"&&define.amd?define(function(){return $}):typeof k=="object"&&k.exports?k.exports=$:m.md5=$})(Y)});var Z=j(K(),1);var M=(u=>(u.PublishMainStreamToCDN="publish-main-stream-to-cdn",u.PublishSubStreamToCDN="publish-sub-stream-to-cdn",u.PublishMixStreamToCDN="publish-mix-stream-to-cdn",u))(M||{}),F=(i=>(i.Main="main",i.Sub="sub",i))(F||{});var w={IT_AUDIO_VIDEO:0,IT_PICTURE:2,IT_CANVAS:3,IT_PURE_AUDIO:4,IT_PURE_VIDEO:5};var A=class{constructor(e,i){S(this,"_core");S(this,"_room");S(this,"_log");S(this,"_paramsForGivenCDN");S(this,"_publishGivenCDNData",null);S(this,"_isPublishingGivenCDN",!1);this._core=e,this._room=e.room,this._log=i,this._paramsForGivenCDN=new Map([["publish-main-stream-to-cdn",null],["publish-sub-stream-to-cdn",null],["publish-mix-stream-to-cdn",null]])}async startPublishGivenCDN(e){this._log.info(`[CDNStreaming] startPublishGivenCDN() ${JSON.stringify(e)}`),this._paramsForGivenCDN.set(e.publishMode,e);let i={pushRequestTime:Date.now(),pushAppId:e.appId,pushBizId:e.bizId,pushCdnUrl:e.url,pushStreamType:this.convertStreamType(e==null?void 0:e.publishMode),pushStreamId:e.streamId};this._publishGivenCDNData=i;try{this._isPublishingGivenCDN&&await this.stopPublishGivenCDN();let u=await this._room.sendStartPublishCDN(i,!1),{code:d,message:p}=u.data;if(d===0)this._log.info("[CDNStreaming] server success: start given cdn."),this._isPublishingGivenCDN=!0;else throw this.resetGivenCDN(),this._log.error(`[CDNStreaming] server failed: start given cdn errCode: ${d} errMsg: ${p} options: ${JSON.stringify(e)}`),new Error(`[CDNStreaming] server failed: start given cdn errCode: ${d} errMsg: ${p}`)}catch(u){throw this.resetGivenCDN(),u}}async stopPublishGivenCDN(){if(this._log.info("[CDNStreaming] stopPublishGivenCDN"),!this._isPublishingGivenCDN||!this._publishGivenCDNData){this.resetGivenCDN();return}let{pushAppId:e,pushBizId:i,pushCdnUrl:u,pushStreamType:d,pushStreamId:p}=this._publishGivenCDNData,l={pushRequestTime:Date.now(),pushAppId:e,pushBizId:i,pushCdnUrl:u,pushStreamType:d,pushStreamId:p},h=await this._room.sendStopPublishCDN(l,!1),{code:b,message:_}=h.data;if(b===0)this._log.info("[CDNStreaming] server success: stop given cdn."),this.resetGivenCDN();else throw this._log.error(`[CDNStreaming] server failed: stop given cdn errCode: ${b} errMsg: ${_} data: ${JSON.stringify(l)}`),new Error(`[CDNStreaming] server failed: stop given cdn errCode: ${b} errMsg: ${_}`)}resetGivenCDN(){this._publishGivenCDNData=null,this._isPublishingGivenCDN=!1}convertStreamType(e){return e==="publish-main-stream-to-cdn"?"main":"aux"}};var U=class{constructor(e,i){S(this,"_core");S(this,"_room");S(this,"_log");S(this,"_config",null);S(this,"_data",null);S(this,"_givenCDNManager");this._core=e,this._room=e.room,this._log=i,this.reset()}get isMixing(){return!!this._data}get isStarted(){return!!this._config}get hasCustomCDN(){var e,i,u;return((e=this._config)==null?void 0:e.target.appId)&&((i=this._config)==null?void 0:i.target.bizId)&&((u=this._config)==null?void 0:u.target.url)}async startMixTranscode(e){if(this._log.info(`startMixTranscode: ${JSON.stringify(e)}`),this._config=e,this.installEvents(),!!this._core.room.isJoined)try{let i=this.getInputParam(e),u=this.getOutputParam(e),d=this.getOutputSessionId({config:e,roomId:this._room.roomId,userId:this._room.userId});this.isMixing&&this._data&&d!==this._data.outputSessionId&&(this._log.info("[CDNStreaming] streamId changed, auto stop mixing before start"),await this.doStopMixTranscode()),await this.doStartMixTranscode({outputSessionId:d,inputParam:i,outputParam:u});let{appId:p,bizId:l,url:h,streamId:b=""}=this._config.target;p&&l&&h&&(this._givenCDNManager||(this._givenCDNManager=new A(this._core,this._log)),await this._givenCDNManager.startPublishGivenCDN({publishMode:e.target.publishMode,appId:p,bizId:l,url:h,streamId:b}))}catch(i){throw this.reset(),i}}async doStartMixTranscode({outputSessionId:e,inputParam:i,outputParam:u}){let d={roomId:String(this._room.roomId),mcuRequestTime:Date.now(),outputSessionId:e,inputParam:i,outputParam:u};this._log.info(`[CDNStreaming] doStartMixTranscode: ${JSON.stringify(d)}`);let p=await this._room.sendStartMixTranscode(d),{code:l}=p.data,{message:h}=p.data;if(l===0)this._log.info("[CDNStreaming] server success: start mix"),this._data=d;else throw l===-102083&&(h=`Please enable relayed-push in ${this._core.constants.CLOUD_CONSOLE_URL} and try later, refer to ${this._core.constants.DOC_URL}tutorial-26-advanced-publish-cdn-stream.html`),this._log.error(`[CDNStreaming] server failed: start mix errCode: ${l} errMsg: ${h}`),new Error(`[CDNStreaming] server failed: start mix errCode: ${l} errMsg: ${h}`)}async stopMixTranscode(){this._log.info("[CDNStreaming] stopMixTranscode");try{this.isStarted&&this.isMixing&&(await this.doStopMixTranscode(),this._config&&this.hasCustomCDN&&this._givenCDNManager&&await this._givenCDNManager.stopPublishGivenCDN())}catch(e){throw e}this.reset()}async doStopMixTranscode(){let e={mcuRequestTime:Date.now(),outputSessionId:this._data.outputSessionId,streamType:this._data.outputParam.streamType};this._log.info(`[CDNStreaming] doStopMixTranscode: ${JSON.stringify(e)}`);let i=await this._room.sendStopMixTranscode(e),{code:u,message:d}=i.data;if(u===0)this._log.info("[CDNStreaming] server success: stop mix"),this.reset();else throw this._log.error(`[CDNStreaming] server failed: start mix errCode: ${u} errMsg: ${d}`),new Error(`[CDNStreaming] server failed: start mix errCode: ${u} errMsg: ${d}`)}reset(){this._config=null,this._data=null,this.uninstallEvents()}installEvents(){this._core.room.on("joined",this.handleRoomJoined,this),this._core.room.on("left",this.handleRoomLeft,this)}uninstallEvents(){this._core.room.off("joined",this.handleRoomJoined,this),this._core.room.off("left",this.handleRoomLeft,this)}async handleRoomJoined(){this._log.info(`[CDNStreaming] handleJoined: ${JSON.stringify(this._config)}`),this.isStarted&&this._config&&await this.startMixTranscode(this._config)}async handleRoomLeft(){this._log.info(`[CDNStreaming] handleRoomLeft: ${JSON.stringify(this._config)}`),this._data=null}getOutputSessionId({config:e,userId:i,roomId:u}){return this._core.utils.isString(e.target.streamId)&&e.target.streamId.length>0?e.target.streamId:(0,Z.default)(`${u}_${i}_main`)}getStringRoomId(e,i){return e?String(e):i}getInputParam(e){let{mix:i={}}=e,{audioMixUserList:u=[],videoLayoutList:d=[]}=i,p=d.map(l=>({userId:l.fixedVideoUser.userId,roomId:this.getStringRoomId(l.fixedVideoUser.roomId,l.fixedVideoUser.strRoomId)||this._core.room.roomId,width:l.width||0,height:l.height||0,locationX:l.locationX||0,locationY:l.locationY||0,zOrder:l.zOrder||1,streamType:l.fixedVideoStreamType==="sub"?1:0,inputType:w.IT_PURE_VIDEO,renderMode:l.fillMode||0}));return u.forEach(l=>{let h=p.findIndex(b=>b.userId===l.userId&&b.room===this.getStringRoomId(l.roomId,l.strRoomId));h!==-1?p[h].inputType=w.IT_AUDIO_VIDEO:p.push({userId:l.userId,roomId:l.roomId||l.strRoomId,inputType:w.IT_PURE_AUDIO})}),p}getOutputParam(e){let i=e.target.streamId||"",{encoding:u={},mix:d={}}=e;return{streamId:i,streamType:i.length>0?1:0,width:this._core.utils.isUndefined(u.videoWidth)?640:u.videoWidth,height:this._core.utils.isUndefined(u.videoHeight)?480:u.videoHeight,videoBps:u.videoBitrate||0,fps:u.videoFramerate||15,gop:u.videoGOP||2,audioSampleRate:u.audioSampleRate||48e3,audioBps:u.audioBitrate||64,audioChannels:u.audioChannels||1,backgroundColor:d.backgroundColor||0,backgroundImg:d.backgroundImage||"",extraInfo:"",videoCodec:2,audioCodec:0}}};var G=j(K(),1);var E=class{constructor(e,i){S(this,"_room");S(this,"_core");S(this,"_log");S(this,"_paramsForTencentCDN");S(this,"_initParamsForTencentCDN",{isPublished:!1,isStarted:!1});this._core=e,this._room=e.room,this._log=i,this._paramsForTencentCDN=new Map([["publish-main-stream-to-cdn",this._initParamsForTencentCDN],["publish-sub-stream-to-cdn",this._initParamsForTencentCDN]])}installEvents(){this._core.innerEmitter.on("104",this.handlePublished,this),this._core.room.on("left",this.handleRoomLeft,this)}uninstallEvents(){this._core.innerEmitter.off("104",this.handlePublished,this),this._core.room.off("left",this.handleRoomLeft,this)}async handlePublished({track:e}){var d;this._log.info(`[CDNStreaming] handlePublished: mediaType ${e.mediaType}, roomID ${(d=e==null?void 0:e.room)==null?void 0:d.roomId}`);let i=e.mediaType===4?"publish-main-stream-to-cdn":"publish-sub-stream-to-cdn",u=this._paramsForTencentCDN.get(i)||null;u!=null&&u.target&&u.isStarted&&await this.startPublishTencentCDN(u.target)}async handleRoomLeft(){this._log.info("[CDNStreaming] handleRoomLeft"),this.changeDataStatus("publish-main-stream-to-cdn",{isPublished:!1}),this.changeDataStatus("publish-sub-stream-to-cdn",{isPublished:!1})}isStreamPublished(e){return e==="publish-main-stream-to-cdn"&&!this._room.isMainStreamPublished?(this._log.info("[CDNStreaming] Main stream has not already published, will auto reStart after published."),!1):e==="publish-sub-stream-to-cdn"&&!this._room.isAuxStreamPublished?(this._log.info("[CDNStreaming] Sub has not already published, will auto reStart after published."),!1):!0}changeDataStatus(e,i){let u=this._paramsForTencentCDN.get(e),d=I(I({},u),i);this._paramsForTencentCDN.set(e,d)}async startPublishTencentCDN(e){if(this._log.info(`[CDNStreaming] startPublishTencentCDN ${JSON.stringify(e)}`),this.installEvents(),this.changeDataStatus(e.publishMode,{target:e,isStarted:!0}),!this.isStreamPublished(e.publishMode))return;let i=e.streamId||"",u=this.generatePublishCDNStreamId(i,e.publishMode),d=this.generatePublishCDNSessionId(e.publishMode),p=e.publishMode==="publish-sub-stream-to-cdn"?1:0,l={requestTime:Date.now(),sessionId:d,streamId:u,streamType:p};await this.doStartPublishTencentCDN(l,e.publishMode);let{appId:h,bizId:b,url:_}=e;if(h&&b&&_){let D=this._paramsForTencentCDN.get(e.publishMode)||this._initParamsForTencentCDN;D!=null&&D.givenCDNManager||(D.givenCDNManager=new A(this._core,this._log),this._paramsForTencentCDN.set(e.publishMode,D)),await D.givenCDNManager.startPublishGivenCDN({publishMode:e.publishMode,appId:h,bizId:b,url:_,streamId:u})}}async doStartPublishTencentCDN(e,i){this._log.info(`[CDNStreaming] doStartPublishTencentCDN: ${JSON.stringify(e)}`);let u=6,d=500,p=0;for(;;){let l=await this._room.sendStartPublishCDN(e,!0),{code:h}=l.data,{message:b}=l.data;if(h===0){this._log.info("[CDNStreaming] server success: start tencent cdn"),this.changeDataStatus(i,{isPublished:!0});break}else if(h===-10006&&p<u)this._log.warn(`[CDNStreaming] doStartPublishTencentCDN: retry...${p+1}/6, reason: ${b}`),p+=1,await new Promise(_=>setTimeout(_,d));else throw this.changeDataStatus(i,{isPublished:!1}),h===-102083&&(b=`Please enable relayed-push in ${this._core.constants.CLOUD_CONSOLE_URL} and try later, refer to ${this._core.constants.DOC_URL}tutorial-26-advanced-publish-cdn-stream.html`),this._log.error(`[CDNStreaming] server failed: start tencent cdn errCode: ${h} errMsg: ${b}`),new Error(`[CDNStreaming] server failed: start tencent cdn errCode: ${h} errMsg: ${b}`)}}async stopPublishTencentCDN(e){this._log.info(`[CDNStreaming] doStartPublishTencentCDN: ${JSON.stringify(e)}`);let i=this._paramsForTencentCDN.get(e)||this._initParamsForTencentCDN;if(i.isPublished){let{target:u}=i;u!=null&&u.bizId&&u.appId&&u.url&&(i!=null&&i.givenCDNManager)&&await(i==null?void 0:i.givenCDNManager.stopPublishGivenCDN()),await this.doStopPublishTencentCDN(e)}this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN)}async doStopPublishTencentCDN(e){let i={requestTime:Date.now(),sessionId:(0,G.default)(`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(e)}`)};this._log.info(`[CDNStreaming] doStopPublishTencentCDN: ${JSON.stringify(i)}`);let u=await this._room.sendStopPublishCDN(i,!0),{code:d}=u.data,{message:p}=u.data;if(d===0)this._log.info("[CDNStreaming] server success: stop tencent cdn"),this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN),this.reset(e);else throw d===-102069&&(this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN),p=`can not stop in auto relayed-push mode ${p}`),this._log.error(`[CDNStreaming] server failed: stop tencent cdn errCode: ${d} errMsg: ${p}`),new Error(`[CDNStreaming] server failed: stop tencent cdn errCode: ${d} errMsg: ${p}`)}reset(e){this.uninstallEvents(),this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN)}generatePublishCDNStreamId(e,i){if(e===""){let u=`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(i)}`;return/^[A-Za-z\d_-]*$/.test(u)||(u=(0,G.default)(u)),`${this._room.sdkAppId}_${u}`}return e}convertPublishModeToStreamType(e){return e==="publish-main-stream-to-cdn"?"main":"aux"}generatePublishCDNSessionId(e){return(0,G.default)(`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(e)}`)}};var Q=m=>typeof m=="string";function P(m){throw new Error(`[CDNStreaming] options error: ${m}`)}function Ce(m,e,i,u){/^[A-Za-z\d_-]*$/.test(m)||P("Invalid parameter streamId.")}function Te(m,e,i,u){if(Q(m))P("roomId must be an integer but go a string, use 'parseInt' to convert it or use 'strRoomId' instead.");else{if(m===void 0)return;/^[1-9]\d*$/.test(String(m))&&m<4294967295||P("roomId must be an integer between [1, 4294967294].")}}function De(m,e,i,u){/^[A-Za-z\d\s!#$%&()+\-:;<=.>?@[\]^_{}|~,]{1,64}$/.test(m)||P("strRoomId must be a valid string.")}var N={type:"number",notLessThanZero:!0},ee={type:"object",required:!0,properties:{userId:{required:!0,type:"string"},roomId:{type:["string","number"],validate:Te},strRoomId:{type:"string",validate:De}}},te={required:!0,properties:{publishMode:{required:!0,values:["publish-main-stream-to-cdn","publish-mix-stream-to-cdn","publish-sub-stream-to-cdn"]},streamId:{required:!1,type:"string",validate:Ce},appId:{type:"number",allowEmpty:!1},bizId:{type:"number",allowEmpty:!1},url:{type:"string",allowEmpty:!1}}},re={required:!1,type:"object",properties:{videoWidth:N,videoHeight:N,videoBitrate:z(I({},N),{allowEmpty:!1}),videoFramerate:{type:"number",validate(m,e,i,u){(m<=0||m>30)&&P("Invalid parameter mixConfig -> videoFramerate, the value must be between (0, 30].")}},videoGOP:{type:"number",validate(m,e,i,u){(m<1||m>8)&&P("Invalid parameter mixConfig -> videoGOP, the value must be between [1, 8].")}},audioSampleRate:N,audioBitrate:{type:"number",validate(m,e,i,u){(m<32||m>192)&&P("Invalid parameter mixConfig -> audioBitrate, the value must be between [32, 192].")}},audioChannels:{type:"number",values:[1,2]}}},ie={required:!1,type:"object",properties:{backgroundColor:{type:"number"},backgroundImage:{type:"string"},audioMixUserList:{type:"array",arrayItem:I({},ee)},videoLayoutList:{type:"array",required:!0,arrayItem:{type:"object",properties:{fixedVideoUser:I({},ee),fixedVideoStreamType:{type:"string",required:!0,values:["main","sub"]},fillMode:{type:"number",values:[0,1,2,4]},zOrder:{type:"number",required:!0,validate(m,e,i,u){(m<1||m>15)&&P("Invalid parameter mix -> videoLayoutList -> zOrder, the value must be between [1, 15].")}},width:N,height:N,locationX:N,locationY:N}}}}};function oe(m){return{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:I({},te),encoding:I({},re),mix:I({},ie)},validate(e,i,u,d){var b,_;let{publishMode:p}=e.target,{encoding:l,mix:h}=e;if(p==="publish-mix-stream-to-cdn"&&((!l||!h)&&P("Invalid parameter mix & encoding, the value must be not empty."),h&&h.videoLayoutList)){let D=0,R=0,x=[];h.videoLayoutList.forEach((O,q)=>{x.push(O.fixedVideoUser.userId),O.width+O.locationX>D&&(D=O.width+O.locationX),O.height+O.locationY>R&&(R=O.height+O.locationY)}),x.indexOf(m.room.userId)<0&&P("Invalid parameter mix -> videoLayoutList, the value must be include self.");let v=(b=l==null?void 0:l.videoWidth)!=null?b:640,y=(_=l==null?void 0:l.videoHeight)!=null?_:480;(v<D||y<R)&&P("Invalid parameter encoding, the width and height of the mixed video must encompass all the mixed-in video streams.")}}}}function ae(m){return{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:I({},te),encoding:I({},re),mix:I({},ie)}}}function se(m){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 L=0,B=class{constructor(e){this.core=e;S(this,"_mixTranscodeManager");S(this,"_publishCDNManager");S(this,"_core");S(this,"_modeOptions");S(this,"seq");S(this,"_log");L=L+1,this.seq=L,this._log=e.log.createChild({id:`${this.getAlias()}${L}`}),this._log.info(`[CDNStreaming] created id=${this.getAlias()}${L}`),this._core=e,this._modeOptions=new Map,this._mixTranscodeManager=new U(e,this._log),this._publishCDNManager=new E(e,this._log)}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._modeOptions.set(e.target.publishMode,e),await this.doStart(e)}async update(e){let i=this._modeOptions.get(e.target.publishMode);return this._core.utils.deepMerge(i,e),await this.doStart(i)}async stop(e){switch(e.target.publishMode){case"publish-mix-stream-to-cdn":{await this._mixTranscodeManager.stopMixTranscode();break}case"publish-main-stream-to-cdn":case"publish-sub-stream-to-cdn":{await this._publishCDNManager.stopPublishTencentCDN(e.target.publishMode);break}}this._modeOptions.delete(e.target.publishMode)}async doStart(e){switch(this._log.info(`[CDNStreaming] doStart: ${JSON.stringify(e)}`),e.target.publishMode){case"publish-mix-stream-to-cdn":{await this._mixTranscodeManager.startMixTranscode(e);break}case"publish-main-stream-to-cdn":case"publish-sub-stream-to-cdn":{await this._publishCDNManager.startPublishTencentCDN(e.target);break}}}};S(B,"TYPE",{PublishMode:{PublishMainStreamToCDN:"publish-main-stream-to-cdn",PublishSubStreamToCDN:"publish-sub-stream-to-cdn",PublishMixStreamToCDN:"publish-mix-stream-to-cdn"}});var rt=B;export{B as CDNStreaming,M as PublishMode,F as TRTCStreamType,rt as default};
|
|
1
|
+
var ue=Object.create;var V=Object.defineProperty,de=Object.defineProperties,me=Object.getOwnPropertyDescriptor,le=Object.getOwnPropertyDescriptors,ce=Object.getOwnPropertyNames,X=Object.getOwnPropertySymbols,he=Object.getPrototypeOf,W=Object.prototype.hasOwnProperty,pe=Object.prototype.propertyIsEnumerable;var H=(m,e,i)=>e in m?V(m,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):m[e]=i,I=(m,e)=>{for(var i in e||(e={}))W.call(e,i)&&H(m,i,e[i]);if(X)for(var i of X(e))pe.call(e,i)&&H(m,i,e[i]);return m},z=(m,e)=>de(m,le(e));var ge=(m,e)=>()=>(e||m((e={exports:{}}).exports,e),e.exports);var Se=(m,e,i,u)=>{if(e&&typeof e=="object"||typeof e=="function")for(let d of ce(e))!W.call(m,d)&&d!==i&&V(m,d,{get:()=>e[d],enumerable:!(u=me(e,d))||u.enumerable});return m};var j=(m,e,i)=>(i=m!=null?ue(he(m)):{},Se(e||!m||!m.__esModule?V(i,"default",{value:m,enumerable:!0}):i,m));var S=(m,e,i)=>(H(m,typeof e!="symbol"?e+"":e,i),i);var K=ge((Y,w)=>{(function(m){"use strict";function e(t,c){var r=(t&65535)+(c&65535),g=(t>>16)+(c>>16)+(r>>16);return g<<16|r&65535}function i(t,c){return t<<c|t>>>32-c}function u(t,c,r,g,C,T){return e(i(e(e(c,t),e(g,T)),C),r)}function d(t,c,r,g,C,T,f){return u(c&r|~c&g,t,c,C,T,f)}function h(t,c,r,g,C,T,f){return u(c&g|r&~g,t,c,C,T,f)}function l(t,c,r,g,C,T,f){return u(c^r^g,t,c,C,T,f)}function p(t,c,r,g,C,T,f){return u(r^(c|~g),t,c,C,T,f)}function b(t,c){t[c>>5]|=128<<c%32,t[(c+64>>>9<<4)+14]=c;var r,g,C,T,f,o=1732584193,a=-271733879,s=-1732584194,n=271733878;for(r=0;r<t.length;r+=16)g=o,C=a,T=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=h(o,a,s,n,t[r+1],5,-165796510),n=h(n,o,a,s,t[r+6],9,-1069501632),s=h(s,n,o,a,t[r+11],14,643717713),a=h(a,s,n,o,t[r],20,-373897302),o=h(o,a,s,n,t[r+5],5,-701558691),n=h(n,o,a,s,t[r+10],9,38016083),s=h(s,n,o,a,t[r+15],14,-660478335),a=h(a,s,n,o,t[r+4],20,-405537848),o=h(o,a,s,n,t[r+9],5,568446438),n=h(n,o,a,s,t[r+14],9,-1019803690),s=h(s,n,o,a,t[r+3],14,-187363961),a=h(a,s,n,o,t[r+8],20,1163531501),o=h(o,a,s,n,t[r+13],5,-1444681467),n=h(n,o,a,s,t[r+2],9,-51403784),s=h(s,n,o,a,t[r+7],14,1735328473),a=h(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,C),s=e(s,T),n=e(n,f);return[o,a,s,n]}function _(t){var c,r="",g=t.length*32;for(c=0;c<g;c+=8)r+=String.fromCharCode(t[c>>5]>>>c%32&255);return r}function D(t){var c,r=[];for(r[(t.length>>2)-1]=void 0,c=0;c<r.length;c+=1)r[c]=0;var g=t.length*8;for(c=0;c<g;c+=8)r[c>>5]|=(t.charCodeAt(c/8)&255)<<c%32;return r}function R(t){return _(b(D(t),t.length*8))}function x(t,c){var r,g=D(t),C=[],T=[],f;for(C[15]=T[15]=void 0,g.length>16&&(g=b(g,t.length*8)),r=0;r<16;r+=1)C[r]=g[r]^909522486,T[r]=g[r]^1549556828;return f=b(C.concat(D(c)),512+c.length*8),_(b(T.concat(f),512+128))}function v(t){var c="0123456789abcdef",r="",g,C;for(C=0;C<t.length;C+=1)g=t.charCodeAt(C),r+=c.charAt(g>>>4&15)+c.charAt(g&15);return r}function y(t){return unescape(encodeURIComponent(t))}function O(t){return R(y(t))}function q(t){return v(O(t))}function J(t,c){return x(y(t),y(c))}function ne(t,c){return v(J(t,c))}function $(t,c,r){return c?r?J(c,t):ne(c,t):r?O(t):q(t)}typeof define=="function"&&define.amd?define(function(){return $}):typeof w=="object"&&w.exports?w.exports=$:m.md5=$})(Y)});var Z=j(K(),1);var M=(u=>(u.PublishMainStreamToCDN="publish-main-stream-to-cdn",u.PublishSubStreamToCDN="publish-sub-stream-to-cdn",u.PublishMixStreamToCDN="publish-mix-stream-to-cdn",u))(M||{}),F=(i=>(i.Main="main",i.Sub="sub",i))(F||{});var k={IT_AUDIO_VIDEO:0,IT_PICTURE:2,IT_CANVAS:3,IT_PURE_AUDIO:4,IT_PURE_VIDEO:5};var A=class{constructor(e,i){S(this,"_core");S(this,"_room");S(this,"_log");S(this,"_paramsForGivenCDN");S(this,"_publishGivenCDNData",null);S(this,"_isPublishingGivenCDN",!1);this._core=e,this._room=e.room,this._log=i,this._paramsForGivenCDN=new Map([["publish-main-stream-to-cdn",null],["publish-sub-stream-to-cdn",null],["publish-mix-stream-to-cdn",null]])}async startPublishGivenCDN(e){this._log.info(`[CDNStreaming] startPublishGivenCDN() ${JSON.stringify(e)}`),this._paramsForGivenCDN.set(e.publishMode,e);let i={pushRequestTime:Date.now(),pushAppId:e.appId,pushBizId:e.bizId,pushCdnUrl:e.url,pushStreamType:this.convertStreamType(e==null?void 0:e.publishMode),pushStreamId:e.streamId};this._publishGivenCDNData=i;try{this._isPublishingGivenCDN&&await this.stopPublishGivenCDN();let u=await this._room.sendStartPublishCDN(i,!1),{code:d,message:h}=u.data;if(d===0)this._log.info("[CDNStreaming] server success: start given cdn."),this._isPublishingGivenCDN=!0;else throw this.resetGivenCDN(),this._log.error(`[CDNStreaming] server failed: start given cdn errCode: ${d} errMsg: ${h} options: ${JSON.stringify(e)}`),new Error(`[CDNStreaming] server failed: start given cdn errCode: ${d} errMsg: ${h}`)}catch(u){throw this.resetGivenCDN(),u}}async stopPublishGivenCDN(){if(this._log.info("[CDNStreaming] stopPublishGivenCDN"),!this._isPublishingGivenCDN||!this._publishGivenCDNData){this.resetGivenCDN();return}let{pushAppId:e,pushBizId:i,pushCdnUrl:u,pushStreamType:d,pushStreamId:h}=this._publishGivenCDNData,l={pushRequestTime:Date.now(),pushAppId:e,pushBizId:i,pushCdnUrl:u,pushStreamType:d,pushStreamId:h},p=await this._room.sendStopPublishCDN(l,!1),{code:b,message:_}=p.data;if(b===0)this._log.info("[CDNStreaming] server success: stop given cdn."),this.resetGivenCDN();else throw this._log.error(`[CDNStreaming] server failed: stop given cdn errCode: ${b} errMsg: ${_} data: ${JSON.stringify(l)}`),new Error(`[CDNStreaming] server failed: stop given cdn errCode: ${b} errMsg: ${_}`)}resetGivenCDN(){this._publishGivenCDNData=null,this._isPublishingGivenCDN=!1}convertStreamType(e){return e==="publish-main-stream-to-cdn"?"main":"aux"}};var U=class{constructor(e,i){S(this,"_core");S(this,"_room");S(this,"_log");S(this,"_config",null);S(this,"_data",null);S(this,"_givenCDNManager");this._core=e,this._room=e.room,this._log=i,this.reset()}get isMixing(){return!!this._data}get isStarted(){return!!this._config}get hasCustomCDN(){var e,i,u;return((e=this._config)==null?void 0:e.target.appId)&&((i=this._config)==null?void 0:i.target.bizId)&&((u=this._config)==null?void 0:u.target.url)}async startMixTranscode(e){if(this._log.info(`startMixTranscode: ${JSON.stringify(e)}`),this._config=e,this.installEvents(),!!this._core.room.isJoined)try{let i=this.getInputParam(e),u=this.getOutputParam(e),d=this.getOutputSessionId({config:e,roomId:this._room.roomId,userId:this._room.userId});this.isMixing&&this._data&&d!==this._data.outputSessionId&&(this._log.info("[CDNStreaming] streamId changed, auto stop mixing before start"),await this.doStopMixTranscode()),await this.doStartMixTranscode({outputSessionId:d,inputParam:i,outputParam:u});let{appId:h,bizId:l,url:p,streamId:b=""}=this._config.target;h&&l&&p&&(this._givenCDNManager||(this._givenCDNManager=new A(this._core,this._log)),await this._givenCDNManager.startPublishGivenCDN({publishMode:e.target.publishMode,appId:h,bizId:l,url:p,streamId:b}))}catch(i){throw this.reset(),i}}async doStartMixTranscode({outputSessionId:e,inputParam:i,outputParam:u}){let d={roomId:String(this._room.roomId),mcuRequestTime:Date.now(),outputSessionId:e,inputParam:i,outputParam:u};this._log.info(`[CDNStreaming] doStartMixTranscode: ${JSON.stringify(d)}`);let h=await this._room.sendStartMixTranscode(d),{code:l}=h.data,{message:p}=h.data;if(l===0)this._log.info("[CDNStreaming] server success: start mix"),this._data=d;else throw l===-102083&&(p=`Please enable relayed-push in ${this._core.constants.CLOUD_CONSOLE_URL} and try later, refer to ${this._core.constants.DOC_URL}tutorial-26-advanced-publish-cdn-stream.html`),this._log.error(`[CDNStreaming] server failed: start mix errCode: ${l} errMsg: ${p}`),new Error(`[CDNStreaming] server failed: start mix errCode: ${l} errMsg: ${p}`)}async stopMixTranscode(){this._log.info("[CDNStreaming] stopMixTranscode");try{this.isStarted&&this.isMixing&&(await this.doStopMixTranscode(),this._config&&this.hasCustomCDN&&this._givenCDNManager&&await this._givenCDNManager.stopPublishGivenCDN())}catch(e){throw e}this.reset()}async doStopMixTranscode(){let e={mcuRequestTime:Date.now(),outputSessionId:this._data.outputSessionId,streamType:this._data.outputParam.streamType};this._log.info(`[CDNStreaming] doStopMixTranscode: ${JSON.stringify(e)}`);let i=await this._room.sendStopMixTranscode(e),{code:u,message:d}=i.data;if(u===0)this._log.info("[CDNStreaming] server success: stop mix"),this.reset();else throw this._log.error(`[CDNStreaming] server failed: start mix errCode: ${u} errMsg: ${d}`),new Error(`[CDNStreaming] server failed: start mix errCode: ${u} errMsg: ${d}`)}reset(){this._config=null,this._data=null,this.uninstallEvents()}installEvents(){this._core.room.on("joined",this.handleRoomJoined,this),this._core.room.on("left",this.handleRoomLeft,this)}uninstallEvents(){this._core.room.off("joined",this.handleRoomJoined,this),this._core.room.off("left",this.handleRoomLeft,this)}async handleRoomJoined(){this._log.info(`[CDNStreaming] handleJoined: ${JSON.stringify(this._config)}`),this.isStarted&&this._config&&await this.startMixTranscode(this._config)}async handleRoomLeft(){this._log.info(`[CDNStreaming] handleRoomLeft: ${JSON.stringify(this._config)}`),this._data=null}getOutputSessionId({config:e,userId:i,roomId:u}){return this._core.utils.isString(e.target.streamId)&&e.target.streamId.length>0?e.target.streamId:(0,Z.default)(`${u}_${i}_main`)}getStringRoomId(e,i){return e?String(e):i}getInputParam(e){let{mix:i={}}=e,{audioMixUserList:u=[],videoLayoutList:d=[]}=i,h=d.map(l=>({userId:l.fixedVideoUser.userId,roomId:this.getStringRoomId(l.fixedVideoUser.roomId,l.fixedVideoUser.strRoomId)||this._core.room.roomId,width:l.width||0,height:l.height||0,locationX:l.locationX||0,locationY:l.locationY||0,zOrder:l.zOrder||1,streamType:l.fixedVideoStreamType==="sub"?1:0,inputType:k.IT_PURE_VIDEO,renderMode:l.fillMode||0}));return u.forEach(l=>{let p=h.findIndex(b=>b.userId===l.userId&&b.room===this.getStringRoomId(l.roomId,l.strRoomId));p!==-1?h[p].inputType=k.IT_AUDIO_VIDEO:h.push({userId:l.userId,roomId:l.roomId||l.strRoomId,inputType:k.IT_PURE_AUDIO})}),h}getOutputParam(e){let i=e.target.streamId||"",{encoding:u={},mix:d={}}=e;return{streamId:i,streamType:i.length>0?1:0,width:this._core.utils.isUndefined(u.videoWidth)?640:u.videoWidth,height:this._core.utils.isUndefined(u.videoHeight)?480:u.videoHeight,videoBps:u.videoBitrate||0,fps:u.videoFramerate||15,gop:u.videoGOP||2,audioSampleRate:u.audioSampleRate||48e3,audioBps:u.audioBitrate||64,audioChannels:u.audioChannels||1,backgroundColor:d.backgroundColor||0,backgroundImg:d.backgroundImage||"",extraInfo:"",videoCodec:2,audioCodec:0}}};var G=j(K(),1);var E=class{constructor(e,i){S(this,"_room");S(this,"_core");S(this,"_log");S(this,"_paramsForTencentCDN");S(this,"_initParamsForTencentCDN",{isPublished:!1,isStarted:!1});this._core=e,this._room=e.room,this._log=i,this._paramsForTencentCDN=new Map([["publish-main-stream-to-cdn",this._initParamsForTencentCDN],["publish-sub-stream-to-cdn",this._initParamsForTencentCDN]])}installEvents(){this._core.innerEmitter.on("104",this.handlePublished,this),this._core.room.on("left",this.handleRoomLeft,this)}uninstallEvents(){this._core.innerEmitter.off("104",this.handlePublished,this),this._core.room.off("left",this.handleRoomLeft,this)}async handlePublished({track:e}){var d;this._log.info(`[CDNStreaming] handlePublished: mediaType ${e.mediaType}, roomID ${(d=e==null?void 0:e.room)==null?void 0:d.roomId}`);let i=e.mediaType===4?"publish-main-stream-to-cdn":"publish-sub-stream-to-cdn",u=this._paramsForTencentCDN.get(i)||null;u!=null&&u.target&&u.isStarted&&await this.startPublishTencentCDN(u.target)}async handleRoomLeft(){this._log.info("[CDNStreaming] handleRoomLeft"),this.changeDataStatus("publish-main-stream-to-cdn",{isPublished:!1}),this.changeDataStatus("publish-sub-stream-to-cdn",{isPublished:!1})}isStreamPublished(e){return e==="publish-main-stream-to-cdn"&&!this._room.isMainStreamPublished?(this._log.info("[CDNStreaming] Main stream has not already published, will auto reStart after published."),!1):e==="publish-sub-stream-to-cdn"&&!this._room.isAuxStreamPublished?(this._log.info("[CDNStreaming] Sub has not already published, will auto reStart after published."),!1):!0}changeDataStatus(e,i){let u=this._paramsForTencentCDN.get(e),d=I(I({},u),i);this._paramsForTencentCDN.set(e,d)}async startPublishTencentCDN(e){if(this._log.info(`[CDNStreaming] startPublishTencentCDN ${JSON.stringify(e)}`),this.installEvents(),this.changeDataStatus(e.publishMode,{target:e,isStarted:!0}),!this.isStreamPublished(e.publishMode))return;let i=e.streamId||"",u=this.generatePublishCDNStreamId(i,e.publishMode),d=this.generatePublishCDNSessionId(e.publishMode),h=e.publishMode==="publish-sub-stream-to-cdn"?1:0,l={requestTime:Date.now(),sessionId:d,streamId:u,streamType:h};await this.doStartPublishTencentCDN(l,e.publishMode);let{appId:p,bizId:b,url:_}=e;if(p&&b&&_){let D=this._paramsForTencentCDN.get(e.publishMode)||this._initParamsForTencentCDN;D!=null&&D.givenCDNManager||(D.givenCDNManager=new A(this._core,this._log),this._paramsForTencentCDN.set(e.publishMode,D)),await D.givenCDNManager.startPublishGivenCDN({publishMode:e.publishMode,appId:p,bizId:b,url:_,streamId:u})}}async doStartPublishTencentCDN(e,i){this._log.info(`[CDNStreaming] doStartPublishTencentCDN: ${JSON.stringify(e)}`);let u=6,d=500,h=0;for(;;){let l=await this._room.sendStartPublishCDN(e,!0),{code:p}=l.data,{message:b}=l.data;if(p===0){this._log.info("[CDNStreaming] server success: start tencent cdn"),this.changeDataStatus(i,{isPublished:!0});break}else if(p===-10006&&h<u)this._log.warn(`[CDNStreaming] doStartPublishTencentCDN: retry...${h+1}/6, reason: ${b}`),h+=1,await new Promise(_=>setTimeout(_,d));else throw this.changeDataStatus(i,{isPublished:!1}),p===-102083&&(b=`Please enable relayed-push in ${this._core.constants.CLOUD_CONSOLE_URL} and try later, refer to ${this._core.constants.DOC_URL}tutorial-26-advanced-publish-cdn-stream.html`),this._log.error(`[CDNStreaming] server failed: start tencent cdn errCode: ${p} errMsg: ${b}`),new Error(`[CDNStreaming] server failed: start tencent cdn errCode: ${p} errMsg: ${b}`)}}async stopPublishTencentCDN(e){this._log.info(`[CDNStreaming] doStartPublishTencentCDN: ${JSON.stringify(e)}`);let i=this._paramsForTencentCDN.get(e)||this._initParamsForTencentCDN;if(i.isPublished){let{target:u}=i;u!=null&&u.bizId&&u.appId&&u.url&&(i!=null&&i.givenCDNManager)&&await(i==null?void 0:i.givenCDNManager.stopPublishGivenCDN()),await this.doStopPublishTencentCDN(e)}this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN)}async doStopPublishTencentCDN(e){let i={requestTime:Date.now(),sessionId:(0,G.default)(`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(e)}`)};this._log.info(`[CDNStreaming] doStopPublishTencentCDN: ${JSON.stringify(i)}`);let u=await this._room.sendStopPublishCDN(i,!0),{code:d}=u.data,{message:h}=u.data;if(d===0)this._log.info("[CDNStreaming] server success: stop tencent cdn"),this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN),this.reset(e);else throw d===-102069&&(this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN),h=`can not stop in auto relayed-push mode ${h}`),this._log.error(`[CDNStreaming] server failed: stop tencent cdn errCode: ${d} errMsg: ${h}`),new Error(`[CDNStreaming] server failed: stop tencent cdn errCode: ${d} errMsg: ${h}`)}reset(e){this.uninstallEvents(),this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN)}generatePublishCDNStreamId(e,i){if(e===""){let u=`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(i)}`;return/^[A-Za-z\d_-]*$/.test(u)||(u=(0,G.default)(u)),`${this._room.sdkAppId}_${u}`}return e}convertPublishModeToStreamType(e){return e==="publish-main-stream-to-cdn"?"main":"aux"}generatePublishCDNSessionId(e){return(0,G.default)(`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(e)}`)}};var Q=m=>typeof m=="string";function P(m){throw new Error(`[CDNStreaming] options error: ${m}`)}function Ce(m,e,i,u){/^[A-Za-z\d_-]*$/.test(m)||P("Invalid parameter streamId.")}function Te(m,e,i,u){if(Q(m))P("roomId must be an integer but go a string, use 'parseInt' to convert it or use 'strRoomId' instead.");else{if(m===void 0)return;/^[1-9]\d*$/.test(String(m))&&m<4294967295||P("roomId must be an integer between [1, 4294967294].")}}function De(m,e,i,u){/^[A-Za-z\d\s!#$%&()+\-:;<=.>?@[\]^_{}|~,]{1,64}$/.test(m)||P("strRoomId must be a valid string.")}var N={type:"number",notLessThanZero:!0},ee={type:"object",required:!0,properties:{userId:{required:!0,type:"string"},roomId:{type:["string","number"],validate:Te},strRoomId:{type:"string",validate:De}}},te={required:!0,properties:{publishMode:{required:!0,values:["publish-main-stream-to-cdn","publish-mix-stream-to-cdn","publish-sub-stream-to-cdn"]},streamId:{required:!1,type:"string",validate:Ce},appId:{type:"number",allowEmpty:!1},bizId:{type:"number",allowEmpty:!1},url:{type:"string",allowEmpty:!1}}},re={required:!1,type:"object",properties:{videoWidth:N,videoHeight:N,videoBitrate:z(I({},N),{allowEmpty:!1}),videoFramerate:{type:"number",validate(m,e,i,u){(m<=0||m>30)&&P("Invalid parameter mixConfig -> videoFramerate, the value must be between (0, 30].")}},videoGOP:{type:"number",validate(m,e,i,u){(m<1||m>8)&&P("Invalid parameter mixConfig -> videoGOP, the value must be between [1, 8].")}},audioSampleRate:N,audioBitrate:{type:"number",validate(m,e,i,u){(m<32||m>192)&&P("Invalid parameter mixConfig -> audioBitrate, the value must be between [32, 192].")}},audioChannels:{type:"number",values:[1,2]}}},ie={required:!1,type:"object",properties:{backgroundColor:{type:"number"},backgroundImage:{type:"string"},audioMixUserList:{type:"array",arrayItem:I({},ee)},videoLayoutList:{type:"array",required:!0,arrayItem:{type:"object",properties:{fixedVideoUser:I({},ee),fixedVideoStreamType:{type:"string",required:!0,values:["main","sub"]},fillMode:{type:"number",values:[0,1,2,4]},zOrder:{type:"number",required:!0,validate(m,e,i,u){(m<1||m>15)&&P("Invalid parameter mix -> videoLayoutList -> zOrder, the value must be between [1, 15].")}},width:N,height:N,locationX:N,locationY:N}}}}};function oe(m){return{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:I({},te),encoding:I({},re),mix:I({},ie)},validate(e,i,u,d){var b,_;let{publishMode:h}=e.target,{encoding:l,mix:p}=e;if(h==="publish-mix-stream-to-cdn"&&((!l||!p)&&P("Invalid parameter mix & encoding, the value must be not empty."),p&&p.videoLayoutList)){let D=0,R=0,x=[];p.videoLayoutList.forEach((O,q)=>{x.push(O.fixedVideoUser.userId),O.width+O.locationX>D&&(D=O.width+O.locationX),O.height+O.locationY>R&&(R=O.height+O.locationY)}),x.indexOf(m.room.userId)<0&&P("Invalid parameter mix -> videoLayoutList, the value must be include self.");let v=(b=l==null?void 0:l.videoWidth)!=null?b:640,y=(_=l==null?void 0:l.videoHeight)!=null?_:480;(v<D||y<R)&&P("Invalid parameter encoding, the width and height of the mixed video must encompass all the mixed-in video streams.")}}}}function ae(m){return{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:I({},te),encoding:I({},re),mix:I({},ie)}}}function se(m){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 L=0,B=class{constructor(e){this.core=e;S(this,"_mixTranscodeManager");S(this,"_publishCDNManager");S(this,"_core");S(this,"_modeOptions");S(this,"seq");S(this,"_log");L=L+1,this.seq=L,this._log=e.log.createChild({id:`${this.getAlias()}${L}`}),this._log.info(`[CDNStreaming] created id=${this.getAlias()}${L}`),this._core=e,this._modeOptions=new Map,this._mixTranscodeManager=new U(e,this._log),this._publishCDNManager=new E(e,this._log)}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._modeOptions.set(e.target.publishMode,e),await this.doStart(e)}async update(e){let i=this._modeOptions.get(e.target.publishMode);return this._core.utils.deepMerge(i,e),await this.doStart(i)}async stop(e){switch(e.target.publishMode){case"publish-mix-stream-to-cdn":{await this._mixTranscodeManager.stopMixTranscode();break}case"publish-main-stream-to-cdn":case"publish-sub-stream-to-cdn":{await this._publishCDNManager.stopPublishTencentCDN(e.target.publishMode);break}}this._modeOptions.delete(e.target.publishMode)}async doStart(e){switch(this._log.info(`[CDNStreaming] doStart: ${JSON.stringify(e)}`),e.target.publishMode){case"publish-mix-stream-to-cdn":{await this._mixTranscodeManager.startMixTranscode(e);break}case"publish-main-stream-to-cdn":case"publish-sub-stream-to-cdn":{await this._publishCDNManager.startPublishTencentCDN(e.target);break}}}};S(B,"TYPE",{PublishMode:{PublishMainStreamToCDN:"publish-main-stream-to-cdn",PublishSubStreamToCDN:"publish-sub-stream-to-cdn",PublishMixStreamToCDN:"publish-mix-stream-to-cdn"}});var rt=B;export{B as CDNStreaming,M as PublishMode,F as TRTCStreamType,rt as default};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var CDNStreaming=(()=>{var de=Object.create;var U=Object.defineProperty,me=Object.defineProperties,le=Object.getOwnPropertyDescriptor,ce=Object.getOwnPropertyDescriptors,pe=Object.getOwnPropertyNames,X=Object.getOwnPropertySymbols,he=Object.getPrototypeOf,W=Object.prototype.hasOwnProperty,ge=Object.prototype.propertyIsEnumerable;var K=(d,e,i)=>e in d?U(d,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):d[e]=i,I=(d,e)=>{for(var i in e||(e={}))W.call(e,i)&&K(d,i,e[i]);if(X)for(var i of X(e))ge.call(e,i)&&K(d,i,e[i]);return d},z=(d,e)=>me(d,ce(e));var Se=(d,e)=>()=>(e||d((e={exports:{}}).exports,e),e.exports),be=(d,e)=>{for(var i in e)U(d,i,{get:e[i],enumerable:!0})},j=(d,e,i,u)=>{if(e&&typeof e=="object"||typeof e=="function")for(let m of pe(e))!W.call(d,m)&&m!==i&&U(d,m,{get:()=>e[m],enumerable:!(u=le(e,m))||u.enumerable});return d};var Y=(d,e,i)=>(i=d!=null?de(he(d)):{},j(e||!d||!d.__esModule?U(i,"default",{value:d,enumerable:!0}):i,d)),Ce=d=>j(U({},"__esModule",{value:!0}),d);var S=(d,e,i)=>(K(d,typeof e!="symbol"?e+"":e,i),i);var F=Se((Z,G)=>{(function(d){"use strict";function e(t,c){var r=(t&65535)+(c&65535),g=(t>>16)+(c>>16)+(r>>16);return g<<16|r&65535}function i(t,c){return t<<c|t>>>32-c}function u(t,c,r,g,C,T){return e(i(e(e(c,t),e(g,T)),C),r)}function m(t,c,r,g,C,T,f){return u(c&r|~c&g,t,c,C,T,f)}function p(t,c,r,g,C,T,f){return u(c&g|r&~g,t,c,C,T,f)}function l(t,c,r,g,C,T,f){return u(c^r^g,t,c,C,T,f)}function h(t,c,r,g,C,T,f){return u(r^(c|~g),t,c,C,T,f)}function b(t,c){t[c>>5]|=128<<c%32,t[(c+64>>>9<<4)+14]=c;var r,g,C,T,f,o=1732584193,a=-271733879,s=-1732584194,n=271733878;for(r=0;r<t.length;r+=16)g=o,C=a,T=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=p(o,a,s,n,t[r+1],5,-165796510),n=p(n,o,a,s,t[r+6],9,-1069501632),s=p(s,n,o,a,t[r+11],14,643717713),a=p(a,s,n,o,t[r],20,-373897302),o=p(o,a,s,n,t[r+5],5,-701558691),n=p(n,o,a,s,t[r+10],9,38016083),s=p(s,n,o,a,t[r+15],14,-660478335),a=p(a,s,n,o,t[r+4],20,-405537848),o=p(o,a,s,n,t[r+9],5,568446438),n=p(n,o,a,s,t[r+14],9,-1019803690),s=p(s,n,o,a,t[r+3],14,-187363961),a=p(a,s,n,o,t[r+8],20,1163531501),o=p(o,a,s,n,t[r+13],5,-1444681467),n=p(n,o,a,s,t[r+2],9,-51403784),s=p(s,n,o,a,t[r+7],14,1735328473),a=p(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=h(o,a,s,n,t[r],6,-198630844),n=h(n,o,a,s,t[r+7],10,1126891415),s=h(s,n,o,a,t[r+14],15,-1416354905),a=h(a,s,n,o,t[r+5],21,-57434055),o=h(o,a,s,n,t[r+12],6,1700485571),n=h(n,o,a,s,t[r+3],10,-1894986606),s=h(s,n,o,a,t[r+10],15,-1051523),a=h(a,s,n,o,t[r+1],21,-2054922799),o=h(o,a,s,n,t[r+8],6,1873313359),n=h(n,o,a,s,t[r+15],10,-30611744),s=h(s,n,o,a,t[r+6],15,-1560198380),a=h(a,s,n,o,t[r+13],21,1309151649),o=h(o,a,s,n,t[r+4],6,-145523070),n=h(n,o,a,s,t[r+11],10,-1120210379),s=h(s,n,o,a,t[r+2],15,718787259),a=h(a,s,n,o,t[r+9],21,-343485551),o=e(o,g),a=e(a,C),s=e(s,T),n=e(n,f);return[o,a,s,n]}function _(t){var c,r="",g=t.length*32;for(c=0;c<g;c+=8)r+=String.fromCharCode(t[c>>5]>>>c%32&255);return r}function D(t){var c,r=[];for(r[(t.length>>2)-1]=void 0,c=0;c<r.length;c+=1)r[c]=0;var g=t.length*8;for(c=0;c<g;c+=8)r[c>>5]|=(t.charCodeAt(c/8)&255)<<c%32;return r}function R(t){return _(b(D(t),t.length*8))}function k(t,c){var r,g=D(t),C=[],T=[],f;for(C[15]=T[15]=void 0,g.length>16&&(g=b(g,t.length*8)),r=0;r<16;r+=1)C[r]=g[r]^909522486,T[r]=g[r]^1549556828;return f=b(C.concat(D(c)),512+c.length*8),_(b(T.concat(f),512+128))}function w(t){var c="0123456789abcdef",r="",g,C;for(C=0;C<t.length;C+=1)g=t.charCodeAt(C),r+=c.charAt(g>>>4&15)+c.charAt(g&15);return r}function y(t){return unescape(encodeURIComponent(t))}function O(t){return R(y(t))}function q(t){return w(O(t))}function J(t,c){return k(y(t),y(c))}function ue(t,c){return w(J(t,c))}function V(t,c,r){return c?r?J(c,t):ue(c,t):r?O(t):q(t)}typeof define=="function"&&define.amd?define(function(){return V}):typeof G=="object"&&G.exports?G.exports=V:d.md5=V})(Z)});var Oe={};be(Oe,{CDNStreaming:()=>v,PublishMode:()=>A,TRTCStreamType:()=>B,default:()=>Ie});var Q=Y(F(),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||{}),B=(i=>(i.Main="main",i.Sub="sub",i))(B||{});var $={IT_AUDIO_VIDEO:0,IT_PICTURE:2,IT_CANVAS:3,IT_PURE_AUDIO:4,IT_PURE_VIDEO:5};var M=class{constructor(e,i){S(this,"_core");S(this,"_room");S(this,"_log");S(this,"_paramsForGivenCDN");S(this,"_publishGivenCDNData",null);S(this,"_isPublishingGivenCDN",!1);this._core=e,this._room=e.room,this._log=i,this._paramsForGivenCDN=new Map([["publish-main-stream-to-cdn",null],["publish-sub-stream-to-cdn",null],["publish-mix-stream-to-cdn",null]])}async startPublishGivenCDN(e){this._log.info(`[CDNStreaming] startPublishGivenCDN() ${JSON.stringify(e)}`),this._paramsForGivenCDN.set(e.publishMode,e);let i={pushRequestTime:Date.now(),pushAppId:e.appId,pushBizId:e.bizId,pushCdnUrl:e.url,pushStreamType:this.convertStreamType(e==null?void 0:e.publishMode),pushStreamId:e.streamId};this._publishGivenCDNData=i;try{this._isPublishingGivenCDN&&await this.stopPublishGivenCDN();let u=await this._room.sendStartPublishCDN(i,!1),{code:m,message:p}=u.data;if(m===0)this._log.info("[CDNStreaming] server success: start given cdn."),this._isPublishingGivenCDN=!0;else throw this.resetGivenCDN(),this._log.error(`[CDNStreaming] server failed: start given cdn errCode: ${m} errMsg: ${p} options: ${JSON.stringify(e)}`),new Error(`[CDNStreaming] server failed: start given cdn errCode: ${m} errMsg: ${p}`)}catch(u){throw this.resetGivenCDN(),u}}async stopPublishGivenCDN(){if(this._log.info("[CDNStreaming] stopPublishGivenCDN"),!this._isPublishingGivenCDN||!this._publishGivenCDNData){this.resetGivenCDN();return}let{pushAppId:e,pushBizId:i,pushCdnUrl:u,pushStreamType:m,pushStreamId:p}=this._publishGivenCDNData,l={pushRequestTime:Date.now(),pushAppId:e,pushBizId:i,pushCdnUrl:u,pushStreamType:m,pushStreamId:p},h=await this._room.sendStopPublishCDN(l,!1),{code:b,message:_}=h.data;if(b===0)this._log.info("[CDNStreaming] server success: stop given cdn."),this.resetGivenCDN();else throw this._log.error(`[CDNStreaming] server failed: stop given cdn errCode: ${b} errMsg: ${_} data: ${JSON.stringify(l)}`),new Error(`[CDNStreaming] server failed: stop given cdn errCode: ${b} errMsg: ${_}`)}resetGivenCDN(){this._publishGivenCDNData=null,this._isPublishingGivenCDN=!1}convertStreamType(e){return e==="publish-main-stream-to-cdn"?"main":"aux"}};var E=class{constructor(e,i){S(this,"_core");S(this,"_room");S(this,"_log");S(this,"_config",null);S(this,"_data",null);S(this,"_givenCDNManager");this._core=e,this._room=e.room,this._log=i,this.reset()}get isMixing(){return!!this._data}get isStarted(){return!!this._config}get hasCustomCDN(){var e,i,u;return((e=this._config)==null?void 0:e.target.appId)&&((i=this._config)==null?void 0:i.target.bizId)&&((u=this._config)==null?void 0:u.target.url)}async startMixTranscode(e){if(this._log.info(`startMixTranscode: ${JSON.stringify(e)}`),this._config=e,this.installEvents(),!!this._core.room.isJoined)try{let i=this.getInputParam(e),u=this.getOutputParam(e),m=this.getOutputSessionId({config:e,roomId:this._room.roomId,userId:this._room.userId});this.isMixing&&this._data&&m!==this._data.outputSessionId&&(this._log.info("[CDNStreaming] streamId changed, auto stop mixing before start"),await this.doStopMixTranscode()),await this.doStartMixTranscode({outputSessionId:m,inputParam:i,outputParam:u});let{appId:p,bizId:l,url:h,streamId:b=""}=this._config.target;p&&l&&h&&(this._givenCDNManager||(this._givenCDNManager=new M(this._core,this._log)),await this._givenCDNManager.startPublishGivenCDN({publishMode:e.target.publishMode,appId:p,bizId:l,url:h,streamId:b}))}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._log.info(`[CDNStreaming] doStartMixTranscode: ${JSON.stringify(m)}`);let p=await this._room.sendStartMixTranscode(m),{code:l}=p.data,{message:h}=p.data;if(l===0)this._log.info("[CDNStreaming] server success: start mix"),this._data=m;else throw l===-102083&&(h=`Please enable relayed-push in ${this._core.constants.CLOUD_CONSOLE_URL} and try later, refer to ${this._core.constants.DOC_URL}tutorial-26-advanced-publish-cdn-stream.html`),this._log.error(`[CDNStreaming] server failed: start mix errCode: ${l} errMsg: ${h}`),new Error(`[CDNStreaming] server failed: start mix errCode: ${l} errMsg: ${h}`)}async stopMixTranscode(){this._log.info("[CDNStreaming] stopMixTranscode");try{this.isStarted&&this.isMixing&&(await this.doStopMixTranscode(),this._config&&this.hasCustomCDN&&this._givenCDNManager&&await this._givenCDNManager.stopPublishGivenCDN())}catch(e){throw e}this.reset()}async doStopMixTranscode(){let e={mcuRequestTime:Date.now(),outputSessionId:this._data.outputSessionId,streamType:this._data.outputParam.streamType};this._log.info(`[CDNStreaming] doStopMixTranscode: ${JSON.stringify(e)}`);let i=await this._room.sendStopMixTranscode(e),{code:u,message:m}=i.data;if(u===0)this._log.info("[CDNStreaming] server success: stop mix"),this.reset();else throw this._log.error(`[CDNStreaming] server failed: start mix errCode: ${u} errMsg: ${m}`),new Error(`[CDNStreaming] server failed: start mix 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._log.info(`[CDNStreaming] handleJoined: ${JSON.stringify(this._config)}`),this.isStarted&&this._config&&await this.startMixTranscode(this._config)}async handleRoomLeft(){this._log.info(`[CDNStreaming] handleRoomLeft: ${JSON.stringify(this._config)}`),this._data=null}getOutputSessionId({config:e,userId:i,roomId:u}){return this._core.utils.isString(e.target.streamId)&&e.target.streamId.length>0?e.target.streamId:(0,Q.default)(`${u}_${i}_main`)}getStringRoomId(e,i){return e?String(e):i}getInputParam(e){let{mix:i={}}=e,{audioMixUserList:u=[],videoLayoutList:m=[]}=i,p=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:$.IT_PURE_VIDEO,renderMode:l.fillMode||0}));return u.forEach(l=>{let h=p.findIndex(b=>b.userId===l.userId&&b.room===this.getStringRoomId(l.roomId,l.strRoomId));h!==-1?p[h].inputType=$.IT_AUDIO_VIDEO:p.push({userId:l.userId,roomId:l.roomId||l.strRoomId,inputType:$.IT_PURE_AUDIO})}),p}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 H=Y(F(),1);var L=class{constructor(e,i){S(this,"_room");S(this,"_core");S(this,"_log");S(this,"_paramsForTencentCDN");S(this,"_initParamsForTencentCDN",{isPublished:!1,isStarted:!1});this._core=e,this._room=e.room,this._log=i,this._paramsForTencentCDN=new Map([["publish-main-stream-to-cdn",this._initParamsForTencentCDN],["publish-sub-stream-to-cdn",this._initParamsForTencentCDN]])}installEvents(){this._core.innerEmitter.on("104",this.handlePublished,this),this._core.room.on("left",this.handleRoomLeft,this)}uninstallEvents(){this._core.innerEmitter.off("104",this.handlePublished,this),this._core.room.off("left",this.handleRoomLeft,this)}async handlePublished({track:e}){var m;this._log.info(`[CDNStreaming] 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._log.info("[CDNStreaming] handleRoomLeft"),this.changeDataStatus("publish-main-stream-to-cdn",{isPublished:!1}),this.changeDataStatus("publish-sub-stream-to-cdn",{isPublished:!1})}isStreamPublished(e){return e==="publish-main-stream-to-cdn"&&!this._room.isMainStreamPublished?(this._log.info("[CDNStreaming] Main stream has not already published, will auto reStart after published."),!1):e==="publish-sub-stream-to-cdn"&&!this._room.isAuxStreamPublished?(this._log.info("[CDNStreaming] Sub has not already published, will auto reStart after published."),!1):!0}changeDataStatus(e,i){let u=this._paramsForTencentCDN.get(e),m=I(I({},u),i);this._paramsForTencentCDN.set(e,m)}async startPublishTencentCDN(e){if(this._log.info(`[CDNStreaming] startPublishTencentCDN ${JSON.stringify(e)}`),this.installEvents(),this.changeDataStatus(e.publishMode,{target:e,isStarted:!0}),!this.isStreamPublished(e.publishMode))return;let i=e.streamId||"",u=this.generatePublishCDNStreamId(i,e.publishMode),m=this.generatePublishCDNSessionId(e.publishMode),p=e.publishMode==="publish-sub-stream-to-cdn"?1:0,l={requestTime:Date.now(),sessionId:m,streamId:u,streamType:p};await this.doStartPublishTencentCDN(l,e.publishMode);let{appId:h,bizId:b,url:_}=e;if(h&&b&&_){let D=this._paramsForTencentCDN.get(e.publishMode)||this._initParamsForTencentCDN;D!=null&&D.givenCDNManager||(D.givenCDNManager=new M(this._core,this._log),this._paramsForTencentCDN.set(e.publishMode,D)),await D.givenCDNManager.startPublishGivenCDN({publishMode:e.publishMode,appId:h,bizId:b,url:_,streamId:u})}}async doStartPublishTencentCDN(e,i){this._log.info(`[CDNStreaming] doStartPublishTencentCDN: ${JSON.stringify(e)}`);let u=6,m=500,p=0;for(;;){let l=await this._room.sendStartPublishCDN(e,!0),{code:h}=l.data,{message:b}=l.data;if(h===0){this._log.info("[CDNStreaming] server success: start tencent cdn"),this.changeDataStatus(i,{isPublished:!0});break}else if(h===-10006&&p<u)this._log.warn(`[CDNStreaming] doStartPublishTencentCDN: retry...${p+1}/6, reason: ${b}`),p+=1,await new Promise(_=>setTimeout(_,m));else throw this.changeDataStatus(i,{isPublished:!1}),h===-102083&&(b=`Please enable relayed-push in ${this._core.constants.CLOUD_CONSOLE_URL} and try later, refer to ${this._core.constants.DOC_URL}tutorial-26-advanced-publish-cdn-stream.html`),this._log.error(`[CDNStreaming] server failed: start tencent cdn errCode: ${h} errMsg: ${b}`),new Error(`[CDNStreaming] server failed: start tencent cdn errCode: ${h} errMsg: ${b}`)}}async stopPublishTencentCDN(e){this._log.info(`[CDNStreaming] doStartPublishTencentCDN: ${JSON.stringify(e)}`);let i=this._paramsForTencentCDN.get(e)||this._initParamsForTencentCDN;if(i.isPublished){let{target: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,H.default)(`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(e)}`)};this._log.info(`[CDNStreaming] doStopPublishTencentCDN: ${JSON.stringify(i)}`);let u=await this._room.sendStopPublishCDN(i,!0),{code:m}=u.data,{message:p}=u.data;if(m===0)this._log.info("[CDNStreaming] server success: stop tencent cdn"),this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN),this.reset(e);else throw m===-102069&&(this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN),p=`can not stop in auto relayed-push mode ${p}`),this._log.error(`[CDNStreaming] server failed: stop tencent cdn errCode: ${m} errMsg: ${p}`),new Error(`[CDNStreaming] server failed: stop tencent cdn errCode: ${m} errMsg: ${p}`)}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,H.default)(u)),`${this._room.sdkAppId}_${u}`}return e}convertPublishModeToStreamType(e){return e==="publish-main-stream-to-cdn"?"main":"aux"}generatePublishCDNSessionId(e){return(0,H.default)(`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(e)}`)}};var ee=d=>typeof d=="string";function P(d){throw new Error(`[CDNStreaming] options error: ${d}`)}function De(d,e,i,u){/^[A-Za-z\d_-]*$/.test(d)||P("Invalid parameter streamId.")}function _e(d,e,i,u){if(ee(d))P("roomId must be an integer but go a string, use 'parseInt' to convert it or use 'strRoomId' instead.");else{if(d===void 0)return;/^[1-9]\d*$/.test(String(d))&&d<4294967295||P("roomId must be an integer between [1, 4294967294].")}}function fe(d,e,i,u){/^[A-Za-z\d\s!#$%&()+\-:;<=.>?@[\]^_{}|~,]{1,64}$/.test(d)||P("strRoomId must be a valid string.")}var N={type:"number",notLessThanZero:!0},te={type:"object",required:!0,properties:{userId:{required:!0,type:"string"},roomId:{type:["string","number"],validate:_e},strRoomId:{type:"string",validate:fe}}},re={required:!0,properties:{publishMode:{required:!0,values:["publish-main-stream-to-cdn","publish-mix-stream-to-cdn","publish-sub-stream-to-cdn"]},streamId:{required:!1,type:"string",validate:De},appId:{type:"number",allowEmpty:!1},bizId:{type:"number",allowEmpty:!1},url:{type:"string",allowEmpty:!1}}},ie={required:!1,type:"object",properties:{videoWidth:N,videoHeight:N,videoBitrate:z(I({},N),{allowEmpty:!1}),videoFramerate:{type:"number",validate(d,e,i,u){(d<=0||d>30)&&P("Invalid parameter mixConfig -> videoFramerate, the value must be between (0, 30].")}},videoGOP:{type:"number",validate(d,e,i,u){(d<1||d>8)&&P("Invalid parameter mixConfig -> videoGOP, the value must be between [1, 8].")}},audioSampleRate:N,audioBitrate:{type:"number",validate(d,e,i,u){(d<32||d>192)&&P("Invalid parameter mixConfig -> audioBitrate, the value must be between [32, 192].")}},audioChannels:{type:"number",values:[1,2]}}},oe={required:!1,type:"object",properties:{backgroundColor:{type:"number"},backgroundImage:{type:"string"},audioMixUserList:{type:"array",arrayItem:I({},te)},videoLayoutList:{type:"array",required:!0,arrayItem:{type:"object",properties:{fixedVideoUser:I({},te),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){(d<1||d>15)&&P("Invalid parameter mix -> videoLayoutList -> zOrder, the value must be between [1, 15].")}},width:N,height:N,locationX:N,locationY:N}}}}};function ae(d){return{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:I({},re),encoding:I({},ie),mix:I({},oe)},validate(e,i,u,m){var b,_;let{publishMode:p}=e.target,{encoding:l,mix:h}=e;if(p==="publish-mix-stream-to-cdn"&&((!l||!h)&&P("Invalid parameter mix & encoding, the value must be not empty."),h&&h.videoLayoutList)){let D=0,R=0,k=[];h.videoLayoutList.forEach((O,q)=>{k.push(O.fixedVideoUser.userId),O.width+O.locationX>D&&(D=O.width+O.locationX),O.height+O.locationY>R&&(R=O.height+O.locationY)}),k.indexOf(d.room.userId)<0&&P("Invalid parameter mix -> videoLayoutList, the value must be include self.");let w=(b=l==null?void 0:l.videoWidth)!=null?b:640,y=(_=l==null?void 0:l.videoHeight)!=null?_:480;(w<D||y<R)&&P("Invalid parameter encoding, the width and height of the mixed video must encompass all the mixed-in video streams.")}}}}function se(d){return{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:I({},re),encoding:I({},ie),mix:I({},oe)}}}function ne(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 x=0,v=class{constructor(e){this.core=e;S(this,"_mixTranscodeManager");S(this,"_publishCDNManager");S(this,"_core");S(this,"_modeOptions");S(this,"seq");S(this,"_log");x=x+1,this.seq=x,this._log=e.log.createChild({id:`${this.getAlias()}${x}`}),this._log.info(`[CDNStreaming] created id=${this.getAlias()}${x}`),this._core=e,this._modeOptions=new Map,this._mixTranscodeManager=new E(e,this._log),this._publishCDNManager=new L(e,this._log)}getName(){return"CDNStreaming"}getAlias(){return"cdn"}getValidateRule(e){switch(e){case"start":return ae(this._core);case"update":return se(this._core);case"stop":return ne(this._core)}}getGroup(e){return e.target.publishMode}async start(e){return this._modeOptions.set(e.target.publishMode,e),await this.doStart(e)}async update(e){let i=this._modeOptions.get(e.target.publishMode);return this._core.utils.deepMerge(i,e),await this.doStart(i)}async stop(e){switch(e.target.publishMode){case"publish-mix-stream-to-cdn":{await this._mixTranscodeManager.stopMixTranscode();break}case"publish-main-stream-to-cdn":case"publish-sub-stream-to-cdn":{await this._publishCDNManager.stopPublishTencentCDN(e.target.publishMode);break}}this._modeOptions.delete(e.target.publishMode)}async doStart(e){switch(this._log.info(`[CDNStreaming] doStart: ${JSON.stringify(e)}`),e.target.publishMode){case"publish-mix-stream-to-cdn":{await this._mixTranscodeManager.startMixTranscode(e);break}case"publish-main-stream-to-cdn":case"publish-sub-stream-to-cdn":{await this._publishCDNManager.startPublishTencentCDN(e.target);break}}}};S(v,"TYPE",{PublishMode:{PublishMainStreamToCDN:"publish-main-stream-to-cdn",PublishSubStreamToCDN:"publish-sub-stream-to-cdn",PublishMixStreamToCDN:"publish-mix-stream-to-cdn"}});var Ie=v;return Ce(Oe);})().default;
|
|
1
|
+
"use strict";var CDNStreaming=(()=>{var de=Object.create;var U=Object.defineProperty,me=Object.defineProperties,le=Object.getOwnPropertyDescriptor,ce=Object.getOwnPropertyDescriptors,he=Object.getOwnPropertyNames,X=Object.getOwnPropertySymbols,pe=Object.getPrototypeOf,W=Object.prototype.hasOwnProperty,ge=Object.prototype.propertyIsEnumerable;var K=(d,e,i)=>e in d?U(d,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):d[e]=i,I=(d,e)=>{for(var i in e||(e={}))W.call(e,i)&&K(d,i,e[i]);if(X)for(var i of X(e))ge.call(e,i)&&K(d,i,e[i]);return d},z=(d,e)=>me(d,ce(e));var Se=(d,e)=>()=>(e||d((e={exports:{}}).exports,e),e.exports),be=(d,e)=>{for(var i in e)U(d,i,{get:e[i],enumerable:!0})},j=(d,e,i,u)=>{if(e&&typeof e=="object"||typeof e=="function")for(let m of he(e))!W.call(d,m)&&m!==i&&U(d,m,{get:()=>e[m],enumerable:!(u=le(e,m))||u.enumerable});return d};var Y=(d,e,i)=>(i=d!=null?de(pe(d)):{},j(e||!d||!d.__esModule?U(i,"default",{value:d,enumerable:!0}):i,d)),Ce=d=>j(U({},"__esModule",{value:!0}),d);var S=(d,e,i)=>(K(d,typeof e!="symbol"?e+"":e,i),i);var F=Se((Z,G)=>{(function(d){"use strict";function e(t,c){var r=(t&65535)+(c&65535),g=(t>>16)+(c>>16)+(r>>16);return g<<16|r&65535}function i(t,c){return t<<c|t>>>32-c}function u(t,c,r,g,C,T){return e(i(e(e(c,t),e(g,T)),C),r)}function m(t,c,r,g,C,T,f){return u(c&r|~c&g,t,c,C,T,f)}function h(t,c,r,g,C,T,f){return u(c&g|r&~g,t,c,C,T,f)}function l(t,c,r,g,C,T,f){return u(c^r^g,t,c,C,T,f)}function p(t,c,r,g,C,T,f){return u(r^(c|~g),t,c,C,T,f)}function b(t,c){t[c>>5]|=128<<c%32,t[(c+64>>>9<<4)+14]=c;var r,g,C,T,f,o=1732584193,a=-271733879,s=-1732584194,n=271733878;for(r=0;r<t.length;r+=16)g=o,C=a,T=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=h(o,a,s,n,t[r+1],5,-165796510),n=h(n,o,a,s,t[r+6],9,-1069501632),s=h(s,n,o,a,t[r+11],14,643717713),a=h(a,s,n,o,t[r],20,-373897302),o=h(o,a,s,n,t[r+5],5,-701558691),n=h(n,o,a,s,t[r+10],9,38016083),s=h(s,n,o,a,t[r+15],14,-660478335),a=h(a,s,n,o,t[r+4],20,-405537848),o=h(o,a,s,n,t[r+9],5,568446438),n=h(n,o,a,s,t[r+14],9,-1019803690),s=h(s,n,o,a,t[r+3],14,-187363961),a=h(a,s,n,o,t[r+8],20,1163531501),o=h(o,a,s,n,t[r+13],5,-1444681467),n=h(n,o,a,s,t[r+2],9,-51403784),s=h(s,n,o,a,t[r+7],14,1735328473),a=h(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,C),s=e(s,T),n=e(n,f);return[o,a,s,n]}function _(t){var c,r="",g=t.length*32;for(c=0;c<g;c+=8)r+=String.fromCharCode(t[c>>5]>>>c%32&255);return r}function D(t){var c,r=[];for(r[(t.length>>2)-1]=void 0,c=0;c<r.length;c+=1)r[c]=0;var g=t.length*8;for(c=0;c<g;c+=8)r[c>>5]|=(t.charCodeAt(c/8)&255)<<c%32;return r}function R(t){return _(b(D(t),t.length*8))}function w(t,c){var r,g=D(t),C=[],T=[],f;for(C[15]=T[15]=void 0,g.length>16&&(g=b(g,t.length*8)),r=0;r<16;r+=1)C[r]=g[r]^909522486,T[r]=g[r]^1549556828;return f=b(C.concat(D(c)),512+c.length*8),_(b(T.concat(f),512+128))}function k(t){var c="0123456789abcdef",r="",g,C;for(C=0;C<t.length;C+=1)g=t.charCodeAt(C),r+=c.charAt(g>>>4&15)+c.charAt(g&15);return r}function y(t){return unescape(encodeURIComponent(t))}function O(t){return R(y(t))}function q(t){return k(O(t))}function J(t,c){return w(y(t),y(c))}function ue(t,c){return k(J(t,c))}function V(t,c,r){return c?r?J(c,t):ue(c,t):r?O(t):q(t)}typeof define=="function"&&define.amd?define(function(){return V}):typeof G=="object"&&G.exports?G.exports=V:d.md5=V})(Z)});var Oe={};be(Oe,{CDNStreaming:()=>v,PublishMode:()=>A,TRTCStreamType:()=>B,default:()=>Ie});var Q=Y(F(),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||{}),B=(i=>(i.Main="main",i.Sub="sub",i))(B||{});var $={IT_AUDIO_VIDEO:0,IT_PICTURE:2,IT_CANVAS:3,IT_PURE_AUDIO:4,IT_PURE_VIDEO:5};var M=class{constructor(e,i){S(this,"_core");S(this,"_room");S(this,"_log");S(this,"_paramsForGivenCDN");S(this,"_publishGivenCDNData",null);S(this,"_isPublishingGivenCDN",!1);this._core=e,this._room=e.room,this._log=i,this._paramsForGivenCDN=new Map([["publish-main-stream-to-cdn",null],["publish-sub-stream-to-cdn",null],["publish-mix-stream-to-cdn",null]])}async startPublishGivenCDN(e){this._log.info(`[CDNStreaming] startPublishGivenCDN() ${JSON.stringify(e)}`),this._paramsForGivenCDN.set(e.publishMode,e);let i={pushRequestTime:Date.now(),pushAppId:e.appId,pushBizId:e.bizId,pushCdnUrl:e.url,pushStreamType:this.convertStreamType(e==null?void 0:e.publishMode),pushStreamId:e.streamId};this._publishGivenCDNData=i;try{this._isPublishingGivenCDN&&await this.stopPublishGivenCDN();let u=await this._room.sendStartPublishCDN(i,!1),{code:m,message:h}=u.data;if(m===0)this._log.info("[CDNStreaming] server success: start given cdn."),this._isPublishingGivenCDN=!0;else throw this.resetGivenCDN(),this._log.error(`[CDNStreaming] server failed: start given cdn errCode: ${m} errMsg: ${h} options: ${JSON.stringify(e)}`),new Error(`[CDNStreaming] server failed: start given cdn errCode: ${m} errMsg: ${h}`)}catch(u){throw this.resetGivenCDN(),u}}async stopPublishGivenCDN(){if(this._log.info("[CDNStreaming] stopPublishGivenCDN"),!this._isPublishingGivenCDN||!this._publishGivenCDNData){this.resetGivenCDN();return}let{pushAppId:e,pushBizId:i,pushCdnUrl:u,pushStreamType:m,pushStreamId:h}=this._publishGivenCDNData,l={pushRequestTime:Date.now(),pushAppId:e,pushBizId:i,pushCdnUrl:u,pushStreamType:m,pushStreamId:h},p=await this._room.sendStopPublishCDN(l,!1),{code:b,message:_}=p.data;if(b===0)this._log.info("[CDNStreaming] server success: stop given cdn."),this.resetGivenCDN();else throw this._log.error(`[CDNStreaming] server failed: stop given cdn errCode: ${b} errMsg: ${_} data: ${JSON.stringify(l)}`),new Error(`[CDNStreaming] server failed: stop given cdn errCode: ${b} errMsg: ${_}`)}resetGivenCDN(){this._publishGivenCDNData=null,this._isPublishingGivenCDN=!1}convertStreamType(e){return e==="publish-main-stream-to-cdn"?"main":"aux"}};var E=class{constructor(e,i){S(this,"_core");S(this,"_room");S(this,"_log");S(this,"_config",null);S(this,"_data",null);S(this,"_givenCDNManager");this._core=e,this._room=e.room,this._log=i,this.reset()}get isMixing(){return!!this._data}get isStarted(){return!!this._config}get hasCustomCDN(){var e,i,u;return((e=this._config)==null?void 0:e.target.appId)&&((i=this._config)==null?void 0:i.target.bizId)&&((u=this._config)==null?void 0:u.target.url)}async startMixTranscode(e){if(this._log.info(`startMixTranscode: ${JSON.stringify(e)}`),this._config=e,this.installEvents(),!!this._core.room.isJoined)try{let i=this.getInputParam(e),u=this.getOutputParam(e),m=this.getOutputSessionId({config:e,roomId:this._room.roomId,userId:this._room.userId});this.isMixing&&this._data&&m!==this._data.outputSessionId&&(this._log.info("[CDNStreaming] streamId changed, auto stop mixing before start"),await this.doStopMixTranscode()),await this.doStartMixTranscode({outputSessionId:m,inputParam:i,outputParam:u});let{appId:h,bizId:l,url:p,streamId:b=""}=this._config.target;h&&l&&p&&(this._givenCDNManager||(this._givenCDNManager=new M(this._core,this._log)),await this._givenCDNManager.startPublishGivenCDN({publishMode:e.target.publishMode,appId:h,bizId:l,url:p,streamId:b}))}catch(i){throw this.reset(),i}}async doStartMixTranscode({outputSessionId:e,inputParam:i,outputParam:u}){let m={roomId:String(this._room.roomId),mcuRequestTime:Date.now(),outputSessionId:e,inputParam:i,outputParam:u};this._log.info(`[CDNStreaming] doStartMixTranscode: ${JSON.stringify(m)}`);let h=await this._room.sendStartMixTranscode(m),{code:l}=h.data,{message:p}=h.data;if(l===0)this._log.info("[CDNStreaming] server success: start mix"),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._log.error(`[CDNStreaming] server failed: start mix errCode: ${l} errMsg: ${p}`),new Error(`[CDNStreaming] server failed: start mix errCode: ${l} errMsg: ${p}`)}async stopMixTranscode(){this._log.info("[CDNStreaming] stopMixTranscode");try{this.isStarted&&this.isMixing&&(await this.doStopMixTranscode(),this._config&&this.hasCustomCDN&&this._givenCDNManager&&await this._givenCDNManager.stopPublishGivenCDN())}catch(e){throw e}this.reset()}async doStopMixTranscode(){let e={mcuRequestTime:Date.now(),outputSessionId:this._data.outputSessionId,streamType:this._data.outputParam.streamType};this._log.info(`[CDNStreaming] doStopMixTranscode: ${JSON.stringify(e)}`);let i=await this._room.sendStopMixTranscode(e),{code:u,message:m}=i.data;if(u===0)this._log.info("[CDNStreaming] server success: stop mix"),this.reset();else throw this._log.error(`[CDNStreaming] server failed: start mix errCode: ${u} errMsg: ${m}`),new Error(`[CDNStreaming] server failed: start mix 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._log.info(`[CDNStreaming] handleJoined: ${JSON.stringify(this._config)}`),this.isStarted&&this._config&&await this.startMixTranscode(this._config)}async handleRoomLeft(){this._log.info(`[CDNStreaming] handleRoomLeft: ${JSON.stringify(this._config)}`),this._data=null}getOutputSessionId({config:e,userId:i,roomId:u}){return this._core.utils.isString(e.target.streamId)&&e.target.streamId.length>0?e.target.streamId:(0,Q.default)(`${u}_${i}_main`)}getStringRoomId(e,i){return e?String(e):i}getInputParam(e){let{mix:i={}}=e,{audioMixUserList:u=[],videoLayoutList:m=[]}=i,h=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:$.IT_PURE_VIDEO,renderMode:l.fillMode||0}));return u.forEach(l=>{let p=h.findIndex(b=>b.userId===l.userId&&b.room===this.getStringRoomId(l.roomId,l.strRoomId));p!==-1?h[p].inputType=$.IT_AUDIO_VIDEO:h.push({userId:l.userId,roomId:l.roomId||l.strRoomId,inputType:$.IT_PURE_AUDIO})}),h}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 H=Y(F(),1);var L=class{constructor(e,i){S(this,"_room");S(this,"_core");S(this,"_log");S(this,"_paramsForTencentCDN");S(this,"_initParamsForTencentCDN",{isPublished:!1,isStarted:!1});this._core=e,this._room=e.room,this._log=i,this._paramsForTencentCDN=new Map([["publish-main-stream-to-cdn",this._initParamsForTencentCDN],["publish-sub-stream-to-cdn",this._initParamsForTencentCDN]])}installEvents(){this._core.innerEmitter.on("104",this.handlePublished,this),this._core.room.on("left",this.handleRoomLeft,this)}uninstallEvents(){this._core.innerEmitter.off("104",this.handlePublished,this),this._core.room.off("left",this.handleRoomLeft,this)}async handlePublished({track:e}){var m;this._log.info(`[CDNStreaming] 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._log.info("[CDNStreaming] handleRoomLeft"),this.changeDataStatus("publish-main-stream-to-cdn",{isPublished:!1}),this.changeDataStatus("publish-sub-stream-to-cdn",{isPublished:!1})}isStreamPublished(e){return e==="publish-main-stream-to-cdn"&&!this._room.isMainStreamPublished?(this._log.info("[CDNStreaming] Main stream has not already published, will auto reStart after published."),!1):e==="publish-sub-stream-to-cdn"&&!this._room.isAuxStreamPublished?(this._log.info("[CDNStreaming] Sub has not already published, will auto reStart after published."),!1):!0}changeDataStatus(e,i){let u=this._paramsForTencentCDN.get(e),m=I(I({},u),i);this._paramsForTencentCDN.set(e,m)}async startPublishTencentCDN(e){if(this._log.info(`[CDNStreaming] startPublishTencentCDN ${JSON.stringify(e)}`),this.installEvents(),this.changeDataStatus(e.publishMode,{target:e,isStarted:!0}),!this.isStreamPublished(e.publishMode))return;let i=e.streamId||"",u=this.generatePublishCDNStreamId(i,e.publishMode),m=this.generatePublishCDNSessionId(e.publishMode),h=e.publishMode==="publish-sub-stream-to-cdn"?1:0,l={requestTime:Date.now(),sessionId:m,streamId:u,streamType:h};await this.doStartPublishTencentCDN(l,e.publishMode);let{appId:p,bizId:b,url:_}=e;if(p&&b&&_){let D=this._paramsForTencentCDN.get(e.publishMode)||this._initParamsForTencentCDN;D!=null&&D.givenCDNManager||(D.givenCDNManager=new M(this._core,this._log),this._paramsForTencentCDN.set(e.publishMode,D)),await D.givenCDNManager.startPublishGivenCDN({publishMode:e.publishMode,appId:p,bizId:b,url:_,streamId:u})}}async doStartPublishTencentCDN(e,i){this._log.info(`[CDNStreaming] doStartPublishTencentCDN: ${JSON.stringify(e)}`);let u=6,m=500,h=0;for(;;){let l=await this._room.sendStartPublishCDN(e,!0),{code:p}=l.data,{message:b}=l.data;if(p===0){this._log.info("[CDNStreaming] server success: start tencent cdn"),this.changeDataStatus(i,{isPublished:!0});break}else if(p===-10006&&h<u)this._log.warn(`[CDNStreaming] doStartPublishTencentCDN: retry...${h+1}/6, reason: ${b}`),h+=1,await new Promise(_=>setTimeout(_,m));else throw this.changeDataStatus(i,{isPublished:!1}),p===-102083&&(b=`Please enable relayed-push in ${this._core.constants.CLOUD_CONSOLE_URL} and try later, refer to ${this._core.constants.DOC_URL}tutorial-26-advanced-publish-cdn-stream.html`),this._log.error(`[CDNStreaming] server failed: start tencent cdn errCode: ${p} errMsg: ${b}`),new Error(`[CDNStreaming] server failed: start tencent cdn errCode: ${p} errMsg: ${b}`)}}async stopPublishTencentCDN(e){this._log.info(`[CDNStreaming] doStartPublishTencentCDN: ${JSON.stringify(e)}`);let i=this._paramsForTencentCDN.get(e)||this._initParamsForTencentCDN;if(i.isPublished){let{target:u}=i;u!=null&&u.bizId&&u.appId&&u.url&&(i!=null&&i.givenCDNManager)&&await(i==null?void 0:i.givenCDNManager.stopPublishGivenCDN()),await this.doStopPublishTencentCDN(e)}this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN)}async doStopPublishTencentCDN(e){let i={requestTime:Date.now(),sessionId:(0,H.default)(`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(e)}`)};this._log.info(`[CDNStreaming] doStopPublishTencentCDN: ${JSON.stringify(i)}`);let u=await this._room.sendStopPublishCDN(i,!0),{code:m}=u.data,{message:h}=u.data;if(m===0)this._log.info("[CDNStreaming] server success: stop tencent cdn"),this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN),this.reset(e);else throw m===-102069&&(this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN),h=`can not stop in auto relayed-push mode ${h}`),this._log.error(`[CDNStreaming] server failed: stop tencent cdn errCode: ${m} errMsg: ${h}`),new Error(`[CDNStreaming] server failed: stop tencent cdn errCode: ${m} errMsg: ${h}`)}reset(e){this.uninstallEvents(),this._paramsForTencentCDN.set(e,this._initParamsForTencentCDN)}generatePublishCDNStreamId(e,i){if(e===""){let u=`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(i)}`;return/^[A-Za-z\d_-]*$/.test(u)||(u=(0,H.default)(u)),`${this._room.sdkAppId}_${u}`}return e}convertPublishModeToStreamType(e){return e==="publish-main-stream-to-cdn"?"main":"aux"}generatePublishCDNSessionId(e){return(0,H.default)(`${this._room.roomId}_${this._room.userId}_${this.convertPublishModeToStreamType(e)}`)}};var ee=d=>typeof d=="string";function P(d){throw new Error(`[CDNStreaming] options error: ${d}`)}function De(d,e,i,u){/^[A-Za-z\d_-]*$/.test(d)||P("Invalid parameter streamId.")}function _e(d,e,i,u){if(ee(d))P("roomId must be an integer but go a string, use 'parseInt' to convert it or use 'strRoomId' instead.");else{if(d===void 0)return;/^[1-9]\d*$/.test(String(d))&&d<4294967295||P("roomId must be an integer between [1, 4294967294].")}}function fe(d,e,i,u){/^[A-Za-z\d\s!#$%&()+\-:;<=.>?@[\]^_{}|~,]{1,64}$/.test(d)||P("strRoomId must be a valid string.")}var N={type:"number",notLessThanZero:!0},te={type:"object",required:!0,properties:{userId:{required:!0,type:"string"},roomId:{type:["string","number"],validate:_e},strRoomId:{type:"string",validate:fe}}},re={required:!0,properties:{publishMode:{required:!0,values:["publish-main-stream-to-cdn","publish-mix-stream-to-cdn","publish-sub-stream-to-cdn"]},streamId:{required:!1,type:"string",validate:De},appId:{type:"number",allowEmpty:!1},bizId:{type:"number",allowEmpty:!1},url:{type:"string",allowEmpty:!1}}},ie={required:!1,type:"object",properties:{videoWidth:N,videoHeight:N,videoBitrate:z(I({},N),{allowEmpty:!1}),videoFramerate:{type:"number",validate(d,e,i,u){(d<=0||d>30)&&P("Invalid parameter mixConfig -> videoFramerate, the value must be between (0, 30].")}},videoGOP:{type:"number",validate(d,e,i,u){(d<1||d>8)&&P("Invalid parameter mixConfig -> videoGOP, the value must be between [1, 8].")}},audioSampleRate:N,audioBitrate:{type:"number",validate(d,e,i,u){(d<32||d>192)&&P("Invalid parameter mixConfig -> audioBitrate, the value must be between [32, 192].")}},audioChannels:{type:"number",values:[1,2]}}},oe={required:!1,type:"object",properties:{backgroundColor:{type:"number"},backgroundImage:{type:"string"},audioMixUserList:{type:"array",arrayItem:I({},te)},videoLayoutList:{type:"array",required:!0,arrayItem:{type:"object",properties:{fixedVideoUser:I({},te),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){(d<1||d>15)&&P("Invalid parameter mix -> videoLayoutList -> zOrder, the value must be between [1, 15].")}},width:N,height:N,locationX:N,locationY:N}}}}};function ae(d){return{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:I({},re),encoding:I({},ie),mix:I({},oe)},validate(e,i,u,m){var b,_;let{publishMode:h}=e.target,{encoding:l,mix:p}=e;if(h==="publish-mix-stream-to-cdn"&&((!l||!p)&&P("Invalid parameter mix & encoding, the value must be not empty."),p&&p.videoLayoutList)){let D=0,R=0,w=[];p.videoLayoutList.forEach((O,q)=>{w.push(O.fixedVideoUser.userId),O.width+O.locationX>D&&(D=O.width+O.locationX),O.height+O.locationY>R&&(R=O.height+O.locationY)}),w.indexOf(d.room.userId)<0&&P("Invalid parameter mix -> videoLayoutList, the value must be include self.");let k=(b=l==null?void 0:l.videoWidth)!=null?b:640,y=(_=l==null?void 0:l.videoHeight)!=null?_:480;(k<D||y<R)&&P("Invalid parameter encoding, the width and height of the mixed video must encompass all the mixed-in video streams.")}}}}function se(d){return{name:"CDNStreamingOptions",type:"object",required:!0,allowEmpty:!1,properties:{target:I({},re),encoding:I({},ie),mix:I({},oe)}}}function ne(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 x=0,v=class{constructor(e){this.core=e;S(this,"_mixTranscodeManager");S(this,"_publishCDNManager");S(this,"_core");S(this,"_modeOptions");S(this,"seq");S(this,"_log");x=x+1,this.seq=x,this._log=e.log.createChild({id:`${this.getAlias()}${x}`}),this._log.info(`[CDNStreaming] created id=${this.getAlias()}${x}`),this._core=e,this._modeOptions=new Map,this._mixTranscodeManager=new E(e,this._log),this._publishCDNManager=new L(e,this._log)}getName(){return"CDNStreaming"}getAlias(){return"cdn"}getValidateRule(e){switch(e){case"start":return ae(this._core);case"update":return se(this._core);case"stop":return ne(this._core)}}getGroup(e){return e.target.publishMode}async start(e){return this._modeOptions.set(e.target.publishMode,e),await this.doStart(e)}async update(e){let i=this._modeOptions.get(e.target.publishMode);return this._core.utils.deepMerge(i,e),await this.doStart(i)}async stop(e){switch(e.target.publishMode){case"publish-mix-stream-to-cdn":{await this._mixTranscodeManager.stopMixTranscode();break}case"publish-main-stream-to-cdn":case"publish-sub-stream-to-cdn":{await this._publishCDNManager.stopPublishTencentCDN(e.target.publishMode);break}}this._modeOptions.delete(e.target.publishMode)}async doStart(e){switch(this._log.info(`[CDNStreaming] doStart: ${JSON.stringify(e)}`),e.target.publishMode){case"publish-mix-stream-to-cdn":{await this._mixTranscodeManager.startMixTranscode(e);break}case"publish-main-stream-to-cdn":case"publish-sub-stream-to-cdn":{await this._publishCDNManager.startPublishTencentCDN(e.target);break}}}};S(v,"TYPE",{PublishMode:{PublishMainStreamToCDN:"publish-main-stream-to-cdn",PublishSubStreamToCDN:"publish-sub-stream-to-cdn",PublishMixStreamToCDN:"publish-mix-stream-to-cdn"}});var Ie=v;return Ce(Oe);})().default;
|