-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.ts
641 lines (588 loc) · 19.1 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/* ---------------------------------------------------------------------------------------------
* Copyright (c) Infiniscene, Inc. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* -------------------------------------------------------------------------------------------- */
import { ApiStream, LayoutApiModel, LiveApiModel } from '@api.stream/sdk'
import config from '../../config'
import { omit, toDataNode, toSceneTree } from '../logic'
import { compositorAdapter, latestUpdateVersion } from './compositor-adapter'
import { CoreContext, log, setAppState } from './context'
import './internal-events'
import { prepareInternalEvents } from './internal-events'
import * as Layouts from './layouts/index'
import * as Sources from './sources/index'
import * as Transforms from './transforms/index'
// Register default scene components
import { render } from '../helpers/compositor'
import { EngineWebsocket } from '../helpers/engine-socket'
import {
getBaseUser,
getProject,
getProjectByLayoutId,
hydrateProject,
layerToNode,
toBaseProject,
} from './data'
import { Compositor, SDK } from './namespaces'
import { GuestOptions, LogLevel } from './types'
export * from './namespaces'
const BroadcastPhase = LiveApiModel.ProjectBroadcastPhase
const { trigger, triggerInternal } = CoreContext
const EventType = LiveApiModel.EventType
const EventSubType = LiveApiModel.EventSubType
/**
* Basic application settings.
*/
export type Settings = {
env?: 'dev' | 'stage' | 'prod'
logLevel?: LogLevel
guestToken?: string
}
/**
* @experimental Experimental settings that should be used with caution.
*
* ----
* _Note: Custom layouts and transforms should not be registered
* unless the same layouts and transforms are accessible by the
* server rendering the output stream._
*/
export type AdvancedSettings = {
sources?: Compositor.Source.SourceDeclaration[]
layouts?: Compositor.Layout.LayoutDeclaration[]
transforms?: Compositor.Transform.TransformDeclaration[]
defaultTransforms?: Compositor.Transform.DefaultTransformMap
useLatestRenderer?: boolean
rendererVersion?: string
}
let initResult: SDK.Studio
let engineSocket: EngineWebsocket
/**
* Create a singleton {@link Studio} instance to serve as the root for
* all SDK functionality.
*/
export const init = async (
settings: Settings & AdvancedSettings = {},
): Promise<SDK.Studio> => {
if (initResult) return initResult
prepareInternalEvents()
// Default env to prod
const env = settings.env || 'prod'
const logLevel: LogLevel = settings.logLevel || 'Warn'
// Update log levels
const livekitLogger = log.getLogger('livekit')
livekitLogger.setLevel(logLevel as any)
log.setLevel(logLevel as any)
log.info('Initializing Studio SDK...')
const {
layouts = [],
transforms = [],
sources = [],
defaultTransforms = {},
useLatestRenderer = false,
guestToken,
rendererVersion,
} = settings
const client = new ApiStream({
sdkVersion: CoreContext.version,
env: env,
logLevel,
})
const conf = config(env)
const compositor = Compositor.start({
dbAdapter: compositorAdapter,
transformSettings: {
defaultTransforms: {
...defaultTransforms,
...conf.defaults.transforms,
},
},
})
CoreContext.config = conf
CoreContext.clients = client
CoreContext.compositor = compositor
CoreContext.logLevel = logLevel
CoreContext.Request = await import('./requests')
CoreContext.Command = await import('./commands')
CoreContext.rendererVersion = useLatestRenderer
? 'latest-v2'
: rendererVersion || CoreContext.version
// Tie context to global scope for debugging purposes
window.__StudioKit = {
...CoreContext,
}
if (compositor) {
compositor.registerSource([...Object.values(Sources), ...sources])
compositor.registerTransform([...Object.values(Transforms), ...transforms])
compositor.registerLayout([...Object.values(Layouts), ...layouts])
}
const guestProject = await client.load(guestToken)
let initialProject: SDK.Project
if (guestProject) {
await client
.LiveApi()
.project.getProject({ ...guestProject })
.then((resp) => hydrateProject(resp.project, guestProject.role))
.then(async (project) => {
setAppState({
// As a contributor, `user` refers to the collection
// that the project belongs to. This will be referenced
// when making requests requiring `collectionId`.
user: {
id: guestProject.collectionId,
props: {},
name: null,
metadata: {},
},
// TODO: Populate
sources: [],
projects: [project],
activeProjectId: null,
})
project.isInitial = true
initialProject = await CoreContext.Command.setActiveProject({
projectId: project.id,
})
})
}
/**
* Collection events from the Event API
*/
client.LiveApi().on(EventType.EVENT_TYPE_COLLECTION, (event, type) => {
log.info('Received: Collection event', type, event)
switch (type) {
case EventSubType.EVENT_SUB_TYPE_UPDATE: {
triggerInternal('UserChanged', event.update.collection)
return
}
}
})
/**
* Destination events from the Event API
*/
client.LiveApi().on(EventType.EVENT_TYPE_DESTINATION, (event, type) => {
log.info('Received: Destination event', type, event)
switch (type) {
case EventSubType.EVENT_SUB_TYPE_CREATE: {
const { destination } = event.create
triggerInternal('DestinationAdded', destination)
return
}
case EventSubType.EVENT_SUB_TYPE_UPDATE: {
const { destination } = event.update
triggerInternal('DestinationChanged', destination)
return
}
case EventSubType.EVENT_SUB_TYPE_DELETE: {
triggerInternal('DestinationRemoved', event.delete)
return
}
}
})
/**
* Source events from the Event API
*/
client.LiveApi().on(EventType.EVENT_TYPE_SOURCE, (event, type) => {
log.info('Received: Source event', type, event)
switch (type) {
case EventSubType.EVENT_SUB_TYPE_CREATE: {
triggerInternal('SourceAdded', event.create.source)
return
}
case EventSubType.EVENT_SUB_TYPE_UPDATE: {
triggerInternal('SourceChanged', event.update.source)
return
}
case EventSubType.EVENT_SUB_TYPE_DELETE: {
triggerInternal('SourceRemoved', event.delete.sourceId)
return
}
case EventSubType.EVENT_SUB_TYPE_ADD: {
triggerInternal('ProjectSourceAdded', {
projectId: event.add.projectId,
source: event.add.source,
})
return
}
case EventSubType.EVENT_SUB_TYPE_REMOVE: {
triggerInternal('ProjectSourceRemoved', {
projectId: event.remove.projectId,
sourceId: event.remove.sourceId,
})
return
}
}
})
/**
* Project events from the Event API
*/
client.LiveApi().on(EventType.EVENT_TYPE_PROJECT, (event, type) => {
log.info('Received: Project event', type, event)
switch (type) {
case EventSubType.EVENT_SUB_TYPE_CREATE: {
const project = event.create.project
// Ignore the event if we already have the project in state
if (getProject(project.projectId)) return
triggerInternal('ProjectAdded', project)
return
}
case EventSubType.EVENT_SUB_TYPE_UPDATE: {
const { project } = event.update
const existingProject = getProject(project.projectId)
if (!existingProject) return
triggerInternal('ProjectChanged', {
project,
phase: existingProject.videoApi.phase,
})
return
}
case EventSubType.EVENT_SUB_TYPE_DELETE: {
triggerInternal('ProjectRemoved', { projectId: event.delete.projectId })
return
}
case EventSubType.EVENT_SUB_TYPE_STATE: {
// Get the project from memory since it is not included in event subtype `state`
const project = getProject(event.state?.projectId)
if (!project) return
let broadcastId = event.state.broadcastId
// Emit explicit external events for broadcast start/stop/error
if (event.state.error) {
trigger('BroadcastError', {
projectId: project.id,
broadcastId: event.state.broadcastId,
error: event.state.error,
})
}
if (event.state.phase) {
const phase = event.state.phase
if (phase === BroadcastPhase.PROJECT_BROADCAST_PHASE_RUNNING) {
trigger('BroadcastStarted', {
projectId: project.id,
broadcastId: event.state.broadcastId,
})
} else if (phase === BroadcastPhase.PROJECT_BROADCAST_PHASE_STOPPED) {
broadcastId = null
trigger('BroadcastStopped', {
projectId: project.id,
broadcastId: event.state.broadcastId,
})
}
}
triggerInternal('ProjectChanged', {
project: project.videoApi.project,
phase: event.state.phase,
broadcastId,
})
return
}
}
})
/**
* Layout events from the Event API
* Listen for remote changes to nodes within a project.
**/
client
.LayoutApi()
.on(LayoutApiModel.EventType.EVENT_TYPE_LAYER, (layer, type) => {
if (type === LayoutApiModel.EventSubType.EVENT_SUB_TYPE_CREATE) {
log.debug('Received: Node Insert', layer.create)
const { connectionId, layoutId } = layer.create.requestMetadata
if (CoreContext.connectionId === connectionId) return
const node = layerToNode(layer.create)
const project = getProjectByLayoutId(layoutId)
if (project) {
const nodes = [node, ...project?.compositor.nodes.map(toDataNode)]
const tree = toSceneTree(nodes, node.id)
project.compositor.local.insert(tree)
triggerInternal('NodeAdded', {
projectId: project.id,
nodeId: node.id,
})
}
} else if (type === LayoutApiModel.EventSubType.EVENT_SUB_TYPE_UPDATE) {
log.debug('Received: Node Update', layer.update)
const {
connectionId,
layoutId,
updateVersions = {},
} = layer.update?.requestMetadata || {}
if (CoreContext.connectionId === connectionId) return
const node = layerToNode(layer.update)
// Check whether we have a more recent change and ignore if we do
const latestUpdateId = latestUpdateVersion[node.id] || 0
if (latestUpdateId > updateVersions[node.id]) {
return log.info(
'Ignoring node update - updateID is less than latest.',
)
}
latestUpdateVersion[node.id] = updateVersions[node.id]
const project = getProjectByLayoutId(layoutId)
if (project) {
project.compositor.local.update(
layer.update.id,
node.props,
node.childIds,
)
triggerInternal('NodeChanged', {
projectId: project.id,
nodeId: node.id,
})
}
} else if (type === LayoutApiModel.EventSubType.EVENT_SUB_TYPE_DELETE) {
log.debug('Received: Node Delete', layer.delete)
const { connectionId, layoutId } = layer.delete.requestMetadata
if (CoreContext.connectionId === connectionId) return
const project = getProjectByLayoutId(layoutId)
if (project) {
project.compositor.local.remove(layer.delete.id)
triggerInternal('NodeRemoved', {
projectId: project.id,
nodeId: layer.delete.id,
})
}
} else if (type === LayoutApiModel.EventSubType.EVENT_SUB_TYPE_BATCH) {
log.debug('Received: Node Batch Update', layer.batch)
const {
connectionId,
layoutId,
updateVersions = {},
} = layer.batch.requestMetadata
if (CoreContext.connectionId === connectionId) return
const project = getProjectByLayoutId(layoutId)
layer.batch.layers.forEach((batch) => {
try {
const [type, args] = Object.entries(batch)[0]
if (type === 'create') {
const node = layerToNode(args)
if (project) {
project.compositor.local.insert(node)
triggerInternal('NodeAdded', {
projectId: project.id,
nodeId: node.id,
})
}
} else if (type === 'update') {
const node = layerToNode(args)
// Check whether we have a more recent change and ignore if we do
const latestUpdateId = latestUpdateVersion[node.id] || 0
if (latestUpdateId > updateVersions[node.id]) {
return log.info(
'Ignoring node update - updateID is less than latest.',
)
}
latestUpdateVersion[node.id] = updateVersions[node.id]
if (project) {
project.compositor.local.update(
node.id,
node.props,
node.childIds,
)
triggerInternal('NodeChanged', {
projectId: project.id,
nodeId: node.id,
})
}
} else if (type === 'delete') {
if (project) {
project.compositor.local.remove(args.id)
triggerInternal('NodeRemoved', {
projectId: project.id,
nodeId: args.id,
})
}
}
} catch (e) {
log.warn('Error handling batch item', e, { item: batch })
}
})
// Trigger a single change on the root node
if (project)
triggerInternal('NodeChanged', {
projectId: project.id,
nodeId: project?.compositor.getRoot().id,
})
}
})
const createGuestCode = (options: GuestOptions = {}, url?: string) => {
const {
displayName,
role,
maxDuration = CoreContext.config.defaults.guestTokenDuration,
projectId = CoreContext.state.activeProjectId,
} = options
const project = getProject(projectId)
const token = displayName
? {
direct: {
displayName,
},
}
: {
exchange: {
maxDuration,
},
}
return client.LiveApi().authentication.createGuestCode({
projectId,
token,
url,
collectionId: project.videoApi.project.collectionId,
maxDuration,
role: (role || LiveApiModel.Role.ROLE_GUEST) as LiveApiModel.Role,
})
}
const createGuestToken = (options: GuestOptions = {}, url?: string) => {
const {
displayName,
role,
maxDuration = CoreContext.config.defaults.guestTokenDuration,
projectId = CoreContext.state.activeProjectId,
} = options
const project = getProject(projectId)
const token = displayName
? {
direct: {
displayName,
},
}
: {
exchange: {
maxDuration,
},
}
return client.LiveApi().authentication.createGuestAccessToken({
projectId,
token,
url,
collectionId: project.videoApi.project.collectionId,
maxDuration,
role: (role || LiveApiModel.Role.ROLE_GUEST) as LiveApiModel.Role,
})
}
return {
...omit(CoreContext, [
'clients',
'config',
'connectionId',
'Request',
'state',
'trigger',
]),
createDemoToken: async () => {
console.warn('createDemoToken() is currently unavailable.')
return ''
},
createPreviewLink: async (request = {}) => {
const { maxDuration, projectId = CoreContext.state.activeProjectId } =
request
const project = getProject(projectId)
const url = project.videoApi.project.composition.studioSdk.rendererUrl
let response = await client
.LiveApi()
.authentication.createGuestAccessToken({
projectId,
token: {
direct: {
displayName: 'Preview',
},
},
url,
collectionId: project.videoApi.project.collectionId,
maxDuration:
maxDuration || CoreContext.config.defaults.previewTokenDuration,
role: LiveApiModel.Role.ROLE_VIEWER,
})
return response.url
},
createGuestLink: async (baseUrl, options = {}) => {
const response = await createGuestCode(options, baseUrl)
return response.guestCode.linkUrl
},
deleteGuestLink: async (code: string) => {
await client.LiveApi().authentication.deleteGuestCode({ code })
},
getGuestLinks: async (options = {}) => {
const { projectId = CoreContext.state.activeProjectId } = options
const project = getProject(projectId)
return await client.LiveApi().authentication.getGuestCodes({
projectId: projectId,
collectionId: project?.videoApi.project.collectionId!,
})
},
createGuestToken: async (options = {}) => {
const response = await createGuestToken(options)
return response.accessToken
},
initialProject,
getProject: (id: string) => toBaseProject(getProject(id)),
load,
render,
}
}
CoreContext.on('ActiveProjectChanged', ({ projectId }) => {
const project = toBaseProject(getProject(projectId))
if (project.role === SDK.Role.ROLE_RENDERER) {
log.info('Connecting to engine websocket...')
if (engineSocket) {
log.info('Disconnecting existing engine websocket...')
engineSocket.disconnect()
engineSocket = null
}
if (!engineSocket) {
log.info('Creating new engine websocket...')
engineSocket = setupEngineWebsocket(
async function connectSource(id) {
triggerInternal('SourceConnected', id)
},
async function disconnectSource(id) {
triggerInternal('SourceDisconnected', id)
},
)
engineSocket.connect()
}
}
})
function setupEngineWebsocket(
connectSource: (id: string) => Promise<void>,
disconnectSource: (id: string) => Promise<void>,
) {
return new EngineWebsocket(connectSource, disconnectSource)
}
/**
* Register the access token and make API request to gather user data.
*/
const load = async (
accessToken: string,
size?: {
x: number
y: number
},
options?: {
updateLayoutOnly?: boolean
}
): Promise<SDK.User> => {
let user = getBaseUser()
if (user) {
log.info('Attempted to load user again - returning existing user')
return user
}
if (!accessToken) {
log.warn('Access token required for load()')
return
}
log.info('Loading user...')
const client = CoreContext.clients
// Init API clients with this jwt
await client.load(accessToken)
// Load the projects and user data
const result = await CoreContext.Request.loadUser(size, options)
// TODO: Move to UserLoaded event handler
setAppState({
user: result.user,
sources: result.sources,
projects: result.projects,
activeProjectId: null,
})
user = getBaseUser()
trigger('UserLoaded', user)
return user
}