@tencentcloud/trtc-cloud-wx 1.0.7 → 1.0.9-beta.0
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/.babelrc +6 -0
- package/.eslintrc.json +129 -0
- package/build/chokidar.js +19 -0
- package/build/clear.js +24 -0
- package/build/copy.js +24 -0
- package/build/copy_to_roomkit.js +19 -0
- package/dist/package.json +14 -0
- package/{trtc-cloud-wx.js → dist/trtc-cloud-wx.js} +25 -40
- package/docs/API/TRTCCloud.html +4353 -0
- package/docs/API/index.html +98 -0
- package/docs/API/scripts/add-toc.js +57 -0
- package/docs/API/scripts/collapse.js +20 -0
- package/docs/API/scripts/highlight/highlight.min.js +1282 -0
- package/docs/API/scripts/highlight/highlightjs-line-numbers.min.js +1 -0
- package/docs/API/scripts/linenumber.js +25 -0
- package/docs/API/scripts/nav.js +12 -0
- package/docs/API/scripts/polyfill.js +4 -0
- package/docs/API/scripts/prettify/Apache-License-2.0.txt +202 -0
- package/docs/API/scripts/prettify/lang-css.js +2 -0
- package/docs/API/scripts/prettify/prettify.js +28 -0
- package/docs/API/scripts/search.js +83 -0
- package/docs/API/styles/font.css +81 -0
- package/docs/API/styles/fonts/JTURjIg1_i6t8kCHKm45_dJE3g3D_vx3rCubqg.woff2 +0 -0
- package/docs/API/styles/fonts/JTURjIg1_i6t8kCHKm45_dJE3gTD_vx3rCubqg.woff2 +0 -0
- package/docs/API/styles/fonts/JTURjIg1_i6t8kCHKm45_dJE3gbD_vx3rCubqg.woff2 +0 -0
- package/docs/API/styles/fonts/JTURjIg1_i6t8kCHKm45_dJE3gfD_vx3rCubqg.woff2 +0 -0
- package/docs/API/styles/fonts/JTURjIg1_i6t8kCHKm45_dJE3gnD_vx3rCs.woff2 +0 -0
- package/docs/API/styles/fonts/JTUSjIg1_i6t8kCHKm459W1hyyTh89ZNpQ.woff2 +0 -0
- package/docs/API/styles/fonts/JTUSjIg1_i6t8kCHKm459WRhyyTh89ZNpQ.woff2 +0 -0
- package/docs/API/styles/fonts/JTUSjIg1_i6t8kCHKm459WZhyyTh89ZNpQ.woff2 +0 -0
- package/docs/API/styles/fonts/JTUSjIg1_i6t8kCHKm459WdhyyTh89ZNpQ.woff2 +0 -0
- package/docs/API/styles/fonts/JTUSjIg1_i6t8kCHKm459WlhyyTh89Y.woff2 +0 -0
- package/docs/API/styles/highlight/highlight.min.css +26 -0
- package/docs/API/styles/highlight/rainbow.min.css +1 -0
- package/docs/API/styles/jsdoc.css +684 -0
- package/docs/API/styles/prettify.css +79 -0
- package/docs/API/styles/toc.css +44 -0
- package/docs/API/tutorial-00-guideline.html +81 -0
- package/docs/doc-src/home.md +7 -0
- package/docs/doc-src/tutorials/00-guideline.md +1 -0
- package/docs/doc-src/tutorials/tutorials.json +5 -0
- package/jsdoc.json +43 -0
- package/package.json +40 -3
- package/rollup.config.js +19 -0
- package/sdk_publish.bash +5 -0
- package/src/TaskMachine.ts +371 -0
- package/src/index.ts +1184 -0
- package/src/interface/index.ts +45 -0
- package/src/interface/types.ts +95 -0
- package/src/log/logger.ts +80 -0
- package/src/types.d.ts +1 -0
- package/src/utils/common.ts +50 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/translate.ts +139 -0
- package/tsconfig.json +15 -0
- package/typedoc.json +21 -0
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
export enum Handletype {
|
|
2
|
+
setPusher = 'setPusher',
|
|
3
|
+
setPlayer = 'setPlayer'
|
|
4
|
+
}
|
|
5
|
+
// 数字越高,set优先级越高
|
|
6
|
+
export const TaskMap = {
|
|
7
|
+
enterRoom: {priority: 0, type: Handletype.setPusher},
|
|
8
|
+
switchRole: {priority: 1, type: Handletype.setPusher},
|
|
9
|
+
startLocalPreview: {priority: 0, type: Handletype.setPusher},
|
|
10
|
+
stopLocalPreview: {priority: 0, type: Handletype.setPusher},
|
|
11
|
+
muteLocalVideo: {priority: 1, type: Handletype.setPusher},
|
|
12
|
+
setVideoEncoderParam: {priority: 0, type: Handletype.setPusher},
|
|
13
|
+
setLocalRenderParams: {priority: 0, type: Handletype.setPusher},
|
|
14
|
+
startLocalAudio: {priority: 0, type: Handletype.setPusher},
|
|
15
|
+
stopLocalAudio: {priority: 0, type: Handletype.setPusher},
|
|
16
|
+
muteLocalAudio: {priority: 1, type: Handletype.setPusher},
|
|
17
|
+
switchCamera: {priority: 0, type: Handletype.setPusher},
|
|
18
|
+
setBeautyStyle: {priority: 0, type: Handletype.setPusher},
|
|
19
|
+
exitRoom: {priority: 9, type: Handletype.setPusher},
|
|
20
|
+
startRemoteView: {priority: 0, type: Handletype.setPlayer},
|
|
21
|
+
updateRemoteView: {priority: 0, type: Handletype.setPlayer},
|
|
22
|
+
stopAllRemoteView: {priority: 1, type: Handletype.setPlayer},
|
|
23
|
+
muteRemoteVideoStream: {priority: 1, type: Handletype.setPlayer},
|
|
24
|
+
muteAllRemoteVideoStreams: {priority: 1, type: Handletype.setPlayer},
|
|
25
|
+
setRemoteRenderParams: {priority: 0, type: Handletype.setPlayer},
|
|
26
|
+
muteRemoteAudio: {priority: 1, type: Handletype.setPlayer},
|
|
27
|
+
muteAllRemoteAudio: {priority: 1, type: Handletype.setPlayer},
|
|
28
|
+
}
|
|
29
|
+
interface TaskOptions {
|
|
30
|
+
ctx: any;
|
|
31
|
+
priority?: number;
|
|
32
|
+
action: Function;
|
|
33
|
+
args: any;
|
|
34
|
+
resolve: Function;
|
|
35
|
+
reject: Function;
|
|
36
|
+
name: string;
|
|
37
|
+
}
|
|
38
|
+
class Task {
|
|
39
|
+
public ctx: any;
|
|
40
|
+
|
|
41
|
+
public priority: number;
|
|
42
|
+
|
|
43
|
+
public action: Function
|
|
44
|
+
|
|
45
|
+
public sequenceNumber: number
|
|
46
|
+
|
|
47
|
+
public args: any
|
|
48
|
+
|
|
49
|
+
public resolve: Function
|
|
50
|
+
|
|
51
|
+
public reject: Function
|
|
52
|
+
|
|
53
|
+
public name: string
|
|
54
|
+
|
|
55
|
+
public single: boolean
|
|
56
|
+
|
|
57
|
+
public type: Handletype
|
|
58
|
+
|
|
59
|
+
public result: any
|
|
60
|
+
|
|
61
|
+
public err: any
|
|
62
|
+
|
|
63
|
+
static nextSequenceNumber = 0;
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
constructor(optinos: TaskOptions) {
|
|
67
|
+
const {
|
|
68
|
+
priority = 0, action, args, resolve, reject, ctx, name
|
|
69
|
+
} = optinos
|
|
70
|
+
this.ctx = ctx
|
|
71
|
+
this.priority = priority
|
|
72
|
+
this.action = action
|
|
73
|
+
this.args = args
|
|
74
|
+
this.resolve = resolve
|
|
75
|
+
this.reject = reject
|
|
76
|
+
this.name = name
|
|
77
|
+
this.single = this.whetherSingle(name)
|
|
78
|
+
this.type = TaskMap[name]?.type || Handletype.setPusher
|
|
79
|
+
this.sequenceNumber = Task.nextSequenceNumber++
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
whetherSingle(funName: string): boolean {
|
|
83
|
+
switch (funName) {
|
|
84
|
+
case 'enterRoom':
|
|
85
|
+
return true
|
|
86
|
+
default:
|
|
87
|
+
return false
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export enum TaskQueueState {
|
|
93
|
+
IDLE = 'idle',
|
|
94
|
+
PENDING = 'pending',
|
|
95
|
+
PROCESS = 'process',
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export enum TaskQueueEvent {
|
|
99
|
+
SET = 'SET',
|
|
100
|
+
SETPROCESS = 'SETPROCESS',
|
|
101
|
+
SETDONE = 'SETDONE',
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export class TaskMachine {
|
|
105
|
+
public state: TaskQueueState;
|
|
106
|
+
|
|
107
|
+
private isProcessing = false; // 表示 pusherTaskQueue 是否正在处理
|
|
108
|
+
|
|
109
|
+
private pusherDomReady = false
|
|
110
|
+
|
|
111
|
+
private pusherTaskQueue = [] as Task[]; // 存储所有 pusher 任务
|
|
112
|
+
|
|
113
|
+
private playerTasks: Record<
|
|
114
|
+
string, {taskQueue: Task[]; isProcessing: boolean; domReady: boolean}
|
|
115
|
+
> = {}; // 存储每个 player 的任务队列
|
|
116
|
+
|
|
117
|
+
constructor() {
|
|
118
|
+
this.state = TaskQueueState.IDLE
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
reset(): void {
|
|
122
|
+
this.state = TaskQueueState.IDLE
|
|
123
|
+
this.resetPusherTasks()
|
|
124
|
+
for (const key in this.playerTasks) {
|
|
125
|
+
if (this.playerTasks[key]) {
|
|
126
|
+
this.resetPlayerTasks(key)
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
this.playerTasks = {}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
resetPusherTasks(): void {
|
|
133
|
+
this.isProcessing = false
|
|
134
|
+
this.pusherTaskQueue.forEach((task) => {
|
|
135
|
+
task.resolve()
|
|
136
|
+
})
|
|
137
|
+
this.pusherTaskQueue.length = 0
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
resetPlayerTasks(view: string): void {
|
|
141
|
+
if (this.playerTasks[view]) {
|
|
142
|
+
this.playerTasks[view].taskQueue.forEach(task => {
|
|
143
|
+
task.resolve()
|
|
144
|
+
})
|
|
145
|
+
this.playerTasks[view].taskQueue.length = 0
|
|
146
|
+
this.playerTasks[view].isProcessing = false
|
|
147
|
+
this.playerTasks[view].domReady = false
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
setPusherDomReady(isReady: boolean): boolean {
|
|
152
|
+
this.pusherDomReady = isReady
|
|
153
|
+
if (!isReady) {
|
|
154
|
+
this.resetPusherTasks()
|
|
155
|
+
}
|
|
156
|
+
return this.pusherDomReady
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
setPlayerDomReady(view: string, isReady: boolean): void {
|
|
160
|
+
if (!this.playerTasks[view]) {
|
|
161
|
+
this.playerTasks[view] = {taskQueue: [], isProcessing: false, domReady: false}
|
|
162
|
+
}
|
|
163
|
+
this.playerTasks[view].domReady = isReady
|
|
164
|
+
if (isReady) {
|
|
165
|
+
this.processPlayerTasks(view)
|
|
166
|
+
} else {
|
|
167
|
+
this.resetPlayerTasks(view)
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
send(event: TaskQueueEvent, view?: string): void {
|
|
172
|
+
switch (this.state) {
|
|
173
|
+
case TaskQueueState.IDLE:
|
|
174
|
+
if (event === TaskQueueEvent.SET) {
|
|
175
|
+
this.state = TaskQueueState.PENDING
|
|
176
|
+
if (view) {
|
|
177
|
+
this.processPlayerTasks(view)
|
|
178
|
+
} else {
|
|
179
|
+
this.processPusherTasks()
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
break
|
|
183
|
+
case TaskQueueState.PENDING:
|
|
184
|
+
if (event === TaskQueueEvent.SETPROCESS) {
|
|
185
|
+
if (view) {
|
|
186
|
+
this.processPlayerTasks(view)
|
|
187
|
+
} else {
|
|
188
|
+
this.processPusherTasks()
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
if (event === TaskQueueEvent.SETDONE) {
|
|
192
|
+
this.state = TaskQueueState.IDLE
|
|
193
|
+
}
|
|
194
|
+
break
|
|
195
|
+
case TaskQueueState.PROCESS:
|
|
196
|
+
if (event === TaskQueueEvent.SETDONE) {
|
|
197
|
+
this.state = TaskQueueState.IDLE
|
|
198
|
+
}
|
|
199
|
+
break
|
|
200
|
+
default:
|
|
201
|
+
break
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
addTask(task: Task): void {
|
|
206
|
+
if (task.type === Handletype.setPusher) {
|
|
207
|
+
this.addPusherTask(task)
|
|
208
|
+
} else {
|
|
209
|
+
this.addPlayerTask(task.args.view, task)
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private addPusherTask(task: Task): void {
|
|
214
|
+
this.addTaskToQueue(task, this.pusherTaskQueue)
|
|
215
|
+
if (this.state === TaskQueueState.PROCESS && !this.isProcessing) {
|
|
216
|
+
this.processPusherTasks()
|
|
217
|
+
} else if (this.state === TaskQueueState.IDLE) {
|
|
218
|
+
this.send(TaskQueueEvent.SET)
|
|
219
|
+
} else if (this.state === TaskQueueState.PENDING) {
|
|
220
|
+
this.send(TaskQueueEvent.SETPROCESS)
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
private addPlayerTask(view: string, task: Task): void {
|
|
225
|
+
if (!this.playerTasks[view]) {
|
|
226
|
+
this.playerTasks[view] = {taskQueue: [], isProcessing: false, domReady: false}
|
|
227
|
+
}
|
|
228
|
+
this.addTaskToQueue(task, this.playerTasks[view].taskQueue)
|
|
229
|
+
if (this.state === TaskQueueState.PROCESS && !this.playerTasks[view].isProcessing) {
|
|
230
|
+
this.processPlayerTasks(view)
|
|
231
|
+
} else if (this.state === TaskQueueState.IDLE) {
|
|
232
|
+
this.send(TaskQueueEvent.SET, view)
|
|
233
|
+
} else if (this.state === TaskQueueState.PENDING) {
|
|
234
|
+
this.send(TaskQueueEvent.SETPROCESS, view)
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
private addTaskToQueue(task: Task, queue: Task[]): void {
|
|
239
|
+
queue.push(task)
|
|
240
|
+
queue.sort((a, b) => {
|
|
241
|
+
if (a.priority !== b.priority) {
|
|
242
|
+
return a.priority - b.priority
|
|
243
|
+
}
|
|
244
|
+
return a.sequenceNumber - b.sequenceNumber
|
|
245
|
+
})
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* 处理任务队列
|
|
250
|
+
* @param taskQueue
|
|
251
|
+
* @param isProcessing
|
|
252
|
+
* @param domReady
|
|
253
|
+
* @param setProcessing
|
|
254
|
+
* @returns
|
|
255
|
+
*/
|
|
256
|
+
private async processTasks(
|
|
257
|
+
taskQueue: Task[],
|
|
258
|
+
isProcessing: boolean,
|
|
259
|
+
domReady: boolean,
|
|
260
|
+
setProcessing: (value: boolean) => void,
|
|
261
|
+
view?: string
|
|
262
|
+
): Promise<void> {
|
|
263
|
+
if (isProcessing || !domReady || taskQueue.length === 0) return
|
|
264
|
+
setProcessing(true)
|
|
265
|
+
this.state = TaskQueueState.PROCESS
|
|
266
|
+
|
|
267
|
+
const currentTasks = [...taskQueue]
|
|
268
|
+
taskQueue.length = 0
|
|
269
|
+
const singleTasks = currentTasks.filter(task => task.single)
|
|
270
|
+
const exceptSingleTask = currentTasks.filter(task => !task.single)
|
|
271
|
+
|
|
272
|
+
for (const task of singleTasks) {
|
|
273
|
+
try {
|
|
274
|
+
task.result = await task.action.call(task.ctx, task.args, task.name, TaskMap[task.name].type)
|
|
275
|
+
} catch (err) {
|
|
276
|
+
task.err = err
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const params = exceptSingleTask.reduce(
|
|
281
|
+
(acc, cur) => {
|
|
282
|
+
const params = cur.args.params
|
|
283
|
+
return {...acc, ...(typeof params === 'function' ? params() : params)}
|
|
284
|
+
}, {}
|
|
285
|
+
)
|
|
286
|
+
const task = exceptSingleTask[0]
|
|
287
|
+
const sequenceTask = currentTasks.sort((a, b) => a.sequenceNumber - b.sequenceNumber)
|
|
288
|
+
try {
|
|
289
|
+
let r = null
|
|
290
|
+
if (task) {
|
|
291
|
+
view
|
|
292
|
+
? r = await task.action.call(
|
|
293
|
+
task.ctx,
|
|
294
|
+
{params, streamId: task.args.streamId, view},
|
|
295
|
+
task.name,
|
|
296
|
+
Handletype.setPlayer
|
|
297
|
+
)
|
|
298
|
+
: r = await task.action.call(task.ctx, {params}, task.name, Handletype.setPusher)
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
for (const task of sequenceTask) {
|
|
302
|
+
if (task.err) {
|
|
303
|
+
task.reject(task.err)
|
|
304
|
+
} else {
|
|
305
|
+
task.resolve(task.result || r)
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
} catch (err) {
|
|
309
|
+
for (const task of sequenceTask) {
|
|
310
|
+
task.reject(err)
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
setProcessing(false)
|
|
315
|
+
this.send(TaskQueueEvent.SETDONE, view)
|
|
316
|
+
|
|
317
|
+
if (taskQueue.length > 0) {
|
|
318
|
+
Promise.resolve().then(() => {
|
|
319
|
+
this.processTasks(taskQueue, isProcessing, domReady, setProcessing, view)
|
|
320
|
+
}).catch(console.error)
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
private async processPusherTasks(): Promise<void> {
|
|
325
|
+
await this.processTasks(
|
|
326
|
+
this.pusherTaskQueue,
|
|
327
|
+
this.isProcessing,
|
|
328
|
+
this.pusherDomReady,
|
|
329
|
+
(value) => { this.isProcessing = value }
|
|
330
|
+
)
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
private async processPlayerTasks(view: string): Promise<void> {
|
|
334
|
+
if (!this.playerTasks[view]) return
|
|
335
|
+
await this.processTasks(
|
|
336
|
+
this.playerTasks[view].taskQueue,
|
|
337
|
+
this.playerTasks[view].isProcessing,
|
|
338
|
+
this.playerTasks[view].domReady,
|
|
339
|
+
(value) => { this.playerTasks[view].isProcessing = value },
|
|
340
|
+
view
|
|
341
|
+
)
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
export const taskMachine = new TaskMachine()
|
|
345
|
+
interface TaskParams {
|
|
346
|
+
params: any;
|
|
347
|
+
funName: string;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
export const setHandle = (target: any, key: string, descriptor: PropertyDescriptor): PropertyDescriptor => {
|
|
351
|
+
const originalMethod = descriptor.value
|
|
352
|
+
descriptor.value = function (...args: any): Promise<any> {
|
|
353
|
+
const [params, funName] = args
|
|
354
|
+
return new Promise((resolve, reject) => {
|
|
355
|
+
try {
|
|
356
|
+
taskMachine.addTask(new Task({
|
|
357
|
+
ctx: this,
|
|
358
|
+
priority: TaskMap[funName]?.priority || 0,
|
|
359
|
+
action: originalMethod,
|
|
360
|
+
args: params,
|
|
361
|
+
name: funName,
|
|
362
|
+
resolve,
|
|
363
|
+
reject
|
|
364
|
+
}))
|
|
365
|
+
} catch (error) {
|
|
366
|
+
reject(error)
|
|
367
|
+
}
|
|
368
|
+
})
|
|
369
|
+
}
|
|
370
|
+
return descriptor
|
|
371
|
+
}
|