@easemob-community/callkit-core 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Easemob
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,162 @@
1
+ # @easemob-community/callkit-core
2
+
3
+ 框架无关的通话信令核心库,仅依赖环信 IM SDK 作为信令通道,RTC 层通过抽象接口由上层自行接入。
4
+
5
+ ## 特性
6
+
7
+ - **零 UI 依赖**:纯信令核心,不绑定任何前端框架
8
+ - **零 RTC 依赖**:通过 `RtcAdapter` 接口或事件回调接入 Agora 等 RTC SDK
9
+ - **单聊状态机**:确定性有限状态机管理 `INVITING → ALERTING → IN_CALL → IDLE`
10
+ - **群聊会话模型**:分布式参与者集合,支持邀请、加入、离开、追加邀请
11
+ - **注册式信令路由**:`SignalRouter` + `SingleCallSignalHandler` / `GroupCallSignalHandler`
12
+ - **事件驱动**:所有状态变更和 RTC 指令通过事件总线通知上层
13
+
14
+ ## 安装
15
+
16
+ ```bash
17
+ pnpm add @easemob-community/callkit-core easemob-websdk
18
+ ```
19
+
20
+ ## 快速开始
21
+
22
+ ```typescript
23
+ import { CallKitCore, CALL_TYPE, CALL_STATUS } from '@easemob-community/callkit-core'
24
+ import type { CallKitEvent } from '@easemob-community/callkit-core'
25
+
26
+ const core = new CallKitCore({
27
+ imClient: conn, // 环信 IM SDK Connection 实例
28
+ inviteTimeout: 30000, // 邀请超时时间(毫秒),默认 30000
29
+ onEvent: (event: CallKitEvent) => {
30
+ console.log('[callkit-core]', event.type, event.payload)
31
+ }
32
+ })
33
+
34
+ // 发起单聊视频通话
35
+ await core.inviteCall({
36
+ calleeUserId: 'user123',
37
+ callType: CALL_TYPE.VIDEO_1V1,
38
+ callerInfo: {
39
+ nickname: '张三',
40
+ avatarURL: 'https://example.com/avatar.png'
41
+ }
42
+ })
43
+ ```
44
+
45
+ ## 核心概念
46
+
47
+ ### CallKitCore
48
+
49
+ 核心控制器,暴露所有通话 API:
50
+
51
+ | 方法 | 说明 |
52
+ |---|---|
53
+ | `inviteCall(params)` | 发起单聊通话 |
54
+ | `answerCall(params)` | 接听 / 拒绝 / 忙线拒绝 |
55
+ | `hangup(params?)` | 挂断 / 取消通话 |
56
+ | `inviteGroupCall(params)` | 发起群聊通话 |
57
+ | `inviteMoreParticipants(ids)` | 群聊中追加邀请成员 |
58
+ | `toggleAudio()` | 切换本地音频开关 |
59
+ | `toggleVideo()` | 切换本地视频开关 |
60
+ | `reportRtcEvent(report)` | 向上层上报 RTC 事件 |
61
+ | `destroy()` | 销毁实例并移除 IM 监听 |
62
+
63
+ ### 单聊状态机
64
+
65
+ ```
66
+ IDLE ──inviteCall──► INVITING ───────────────► IN_CALL
67
+ ▲ │ │ │
68
+ │ │ └─answerCall(refuse)─┘
69
+ │ │
70
+ │ └──incomingCall / alert
71
+
72
+ └───────────────────────────────────────hangup
73
+ ```
74
+
75
+ 完整状态:`IDLE` | `INVITING` | `ALERTING` | `CONFIRM_RING` | `RECEIVED_CONFIRM_RING` | `ANSWER_CALL` | `CONFIRM_CALLEE` | `IN_CALL`
76
+
77
+ ### 群聊会话
78
+
79
+ 群聊没有全局状态机,只有:
80
+
81
+ - `session`:会话元数据(callId、groupId、channel、callerUserId)
82
+ - `participants`:参与者列表,每人状态为 `invited → accepted → joinedRtc → left`
83
+
84
+ ### RTC 接入方式
85
+
86
+ #### 方式一:RtcAdapter(推荐)
87
+
88
+ 实现 `RtcAdapter` 接口并传入 `CallKitCoreConfig.rtcAdapter`,Core 会自动处理 `shouldJoinRtc` / `shouldLeaveRtc` / `shouldPublishTracks` / `localAudioChanged` / `localVideoChanged` 等指令事件。
89
+
90
+ ```typescript
91
+ import type { RtcAdapter, JoinRtcParams } from '@easemob-community/callkit-core'
92
+
93
+ class AgoraRtcAdapter implements RtcAdapter {
94
+ private client = AgoraRTC.createClient({ mode: 'rtc', codec: 'vp8' })
95
+
96
+ async joinChannel(params: JoinRtcParams): Promise<void> {
97
+ await this.client.join(params.appId, params.channel, params.token, params.uid)
98
+ }
99
+
100
+ async leaveChannel(): Promise<void> {
101
+ await this.client.leave()
102
+ }
103
+
104
+ async publishLocalTracks(types: ('audio' | 'video')[]): Promise<void> { /* ... */ }
105
+ async unpublishLocalTracks(types: ('audio' | 'video')[]): Promise<void> { /* ... */ }
106
+ async subscribeRemoteUser(userId: string, mediaType: 'audio' | 'video'): Promise<void> { /* ... */ }
107
+ async unsubscribeRemoteUser(userId: string, mediaType: 'audio' | 'video'): Promise<void> { /* ... */ }
108
+ async setAudioEnabled(enabled: boolean): Promise<void> { /* ... */ }
109
+ async setVideoEnabled(enabled: boolean): Promise<void> { /* ... */ }
110
+ }
111
+
112
+ const core = new CallKitCore({
113
+ imClient: conn,
114
+ rtcAdapter: new AgoraRtcAdapter()
115
+ })
116
+ ```
117
+
118
+ #### 方式二:事件回调
119
+
120
+ 监听 `onEvent` / `onRtcEvent` 手动调用 RTC SDK:
121
+
122
+ ```typescript
123
+ const core = new CallKitCore({
124
+ imClient: conn,
125
+ onEvent: async (event) => {
126
+ switch (event.type) {
127
+ case 'shouldJoinRtc': {
128
+ const { channel, token, uid, appId } = event.payload
129
+ await agoraClient.join(appId, channel, token, uid)
130
+ break
131
+ }
132
+ case 'shouldLeaveRtc': {
133
+ await agoraClient.leave()
134
+ break
135
+ }
136
+ // ...
137
+ }
138
+ }
139
+ })
140
+ ```
141
+
142
+ 完整接口定义与示例参见 [docs/integration.md](./docs/integration.md)。
143
+
144
+ ## 文档导航
145
+
146
+ - [架构说明](./docs/architecture.md) — 模块设计、状态机、信令路由
147
+ - [信令交互](./docs/signaling.md) — 消息格式、交互流程、超时处理
148
+ - [RTC 接入](./docs/integration.md) — RtcAdapter 实现、事件回调、平台差异
149
+ - [事件参考](./docs/events.md) — 完整事件类型、payload 结构、使用示例
150
+ - [API 参考](./docs/api/) — TypeDoc 自动生成的完整 API 文档
151
+
152
+ ## 平台支持
153
+
154
+ | 平台 | 状态 |
155
+ |------|------|
156
+ | Web (Vue3 / React / Angular) | 已支持 |
157
+ | UniApp | 计划中 |
158
+ | 小程序 | 计划中 |
159
+
160
+ ## License
161
+
162
+ MIT