-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
270 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,14 @@ | ||
import type { ApiRequest } from './api.js'; | ||
|
||
export interface SetupInstanceOptions { | ||
/** | ||
* sets LIVE_COMPOSITOR_AHEAD_OF_TIME_PROCESSING_ENABLE environment variable. | ||
*/ | ||
aheadOfTimeProcessing: boolean; | ||
} | ||
|
||
export interface CompositorManager { | ||
setupInstance(): Promise<void>; | ||
setupInstance(opts: SetupInstanceOptions): Promise<void>; | ||
sendRequest(request: ApiRequest): Promise<object>; | ||
registerEventListener(cb: (event: unknown) => void): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export { ApiClient, ApiRequest } from './api.js'; | ||
export { LiveCompositor } from './compositor.js'; | ||
export { CompositorManager } from './compositorManager.js'; | ||
export { OfflineCompositor } from './offline_compositor.js'; | ||
export { CompositorManager, SetupInstanceOptions } from './compositorManager.js'; | ||
export { RegisterInputRequest, RegisterInput } from './api/input.js'; | ||
export { RegisterOutputRequest, RegisterOutput } from './api/output.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import type { Renderers } from 'live-compositor'; | ||
import { _liveCompositorInternals } from 'live-compositor'; | ||
import { ApiClient } from './api.js'; | ||
import type { CompositorManager } from './compositorManager.js'; | ||
import type { RegisterOutput } from './api/output.js'; | ||
import { intoRegisterOutput } from './api/output.js'; | ||
import type { RegisterInput } from './api/input.js'; | ||
import { intoRegisterInput } from './api/input.js'; | ||
import { onCompositorEvent } from './event.js'; | ||
import { intoRegisterImage, intoRegisterWebRenderer } from './api/renderer.js'; | ||
import OfflineOutput from './offline_output.js'; | ||
|
||
/** | ||
* Offline rendering only supports one output, so we can just pick any value to use | ||
* as an output ID. | ||
*/ | ||
const OFFLINE_OUTPUT_ID = 'offline_output'; | ||
|
||
export type OfflineRenderOptions = { | ||
output: RegisterOutput; | ||
durationMs: number; | ||
}; | ||
|
||
export class OfflineCompositor { | ||
private manager: CompositorManager; | ||
private api: ApiClient; | ||
private store: _liveCompositorInternals.InstanceContextStore; | ||
private outputs: Record<string, OfflineOutput> = {}; | ||
private render_started: boolean = false; | ||
|
||
public constructor(manager: CompositorManager) { | ||
this.manager = manager; | ||
this.api = new ApiClient(this.manager); | ||
this.store = new _liveCompositorInternals.InstanceContextStore(); | ||
} | ||
|
||
public async init(): Promise<void> { | ||
this.manager.registerEventListener((event: unknown) => onCompositorEvent(this.store, event)); | ||
await this.manager.setupInstance({ aheadOfTimeProcessing: true }); | ||
} | ||
|
||
public async render(request: RegisterOutput): Promise<object> { | ||
this.render_started = true; | ||
const output = new OfflineOutput(OFFLINE_OUTPUT_ID, request, this.api, this.store); | ||
|
||
const apiRequest = intoRegisterOutput(request, output.scene()); | ||
const result = await this.api.registerOutput(OFFLINE_OUTPUT_ID, apiRequest); | ||
this.outputs[OFFLINE_OUTPUT_ID] = output; | ||
await output.ready(); | ||
// loop next_timestamp() | ||
// - update props/store | ||
// - check if finished rendering | ||
// - | ||
return result; | ||
} | ||
|
||
public async registerInput(inputId: string, request: RegisterInput): Promise<object> { | ||
return this.store.runBlocking(async updateStore => { | ||
const result = await this.api.registerInput(inputId, intoRegisterInput(request)); | ||
updateStore({ type: 'add_input', input: { inputId } }); | ||
return result; | ||
}); | ||
} | ||
|
||
public async registerShader( | ||
shaderId: string, | ||
request: Renderers.RegisterShader | ||
): Promise<object> { | ||
return this.api.registerShader(shaderId, request); | ||
} | ||
|
||
public async registerImage(imageId: string, request: Renderers.RegisterImage): Promise<object> { | ||
return this.api.registerImage(imageId, intoRegisterImage(request)); | ||
} | ||
|
||
public async registerWebRenderer( | ||
instanceId: string, | ||
request: Renderers.RegisterWebRenderer | ||
): Promise<object> { | ||
return this.api.registerWebRenderer(instanceId, intoRegisterWebRenderer(request)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import type { Outputs } from 'live-compositor'; | ||
import { _liveCompositorInternals, View } from 'live-compositor'; | ||
import type React from 'react'; | ||
import { createElement, useSyncExternalStore } from 'react'; | ||
import type { ApiClient, Api } from './api.js'; | ||
import Renderer from './renderer.js'; | ||
import type { RegisterOutput } from './api/output.js'; | ||
import { intoAudioInputsConfiguration } from './api/output.js'; | ||
import { throttle } from './utils.js'; | ||
|
||
type OutputContext = _liveCompositorInternals.OutputContext; | ||
type InstanceContextStore = _liveCompositorInternals.InstanceContextStore; | ||
|
||
class OfflineOutput { | ||
api: ApiClient; | ||
outputId: string; | ||
outputCtx: OutputContext; | ||
outputShutdownStateStore: OutputShutdownStateStore; | ||
|
||
shouldUpdateWhenReady: boolean = false; | ||
throttledUpdate: () => void; | ||
videoRenderer?: Renderer; | ||
initialAudioConfig?: Outputs.AudioInputsConfiguration; | ||
|
||
constructor( | ||
outputId: string, | ||
registerRequest: RegisterOutput, | ||
api: ApiClient, | ||
store: InstanceContextStore | ||
) { | ||
this.api = api; | ||
this.outputId = outputId; | ||
this.outputShutdownStateStore = new OutputShutdownStateStore(); | ||
this.shouldUpdateWhenReady = false; | ||
this.throttledUpdate = () => { | ||
this.shouldUpdateWhenReady = true; | ||
}; | ||
|
||
const supportsAudio = 'audio' in registerRequest && !!registerRequest.audio; | ||
if (supportsAudio) { | ||
this.initialAudioConfig = registerRequest.audio!.initial ?? { inputs: [] }; | ||
} | ||
|
||
const onUpdate = () => this.throttledUpdate(); | ||
this.outputCtx = new _liveCompositorInternals.OutputContext(onUpdate, { | ||
supportsAudio, | ||
offlineMode: true, | ||
}); | ||
|
||
if (registerRequest.video) { | ||
const rootElement = createElement(OutputRootComponent, { | ||
instanceStore: store, | ||
outputCtx: this.outputCtx, | ||
outputRoot: registerRequest.video.root, | ||
outputShutdownStateStore: this.outputShutdownStateStore, | ||
}); | ||
|
||
this.videoRenderer = new Renderer({ | ||
rootElement, | ||
onUpdate, | ||
idPrefix: `${outputId}-`, | ||
}); | ||
} | ||
} | ||
|
||
public scene(): { video?: Api.Video; audio?: Api.Audio } { | ||
const audio = this.outputCtx.audioContext.getAudioConfig() ?? this.initialAudioConfig; | ||
return { | ||
video: this.videoRenderer && { root: this.videoRenderer.scene() }, | ||
audio: audio && intoAudioInputsConfiguration(audio), | ||
}; | ||
} | ||
|
||
public close(): void { | ||
this.throttledUpdate = () => {}; | ||
// close will switch a scene to just a <View />, so we need replace `throttledUpdate` | ||
// callback before it is called | ||
this.outputShutdownStateStore.close(); | ||
} | ||
|
||
public async ready() { | ||
this.throttledUpdate = throttle(async () => { | ||
await this.api.updateScene(this.outputId, this.scene()); | ||
}, 30); | ||
if (this.shouldUpdateWhenReady) { | ||
this.throttledUpdate(); | ||
} | ||
} | ||
} | ||
|
||
// External store to share shutdown information between React tree | ||
// and external code that is managing it. | ||
class OutputShutdownStateStore { | ||
private shutdown: boolean = false; | ||
private onChangeCallbacks: Set<() => void> = new Set(); | ||
|
||
public close() { | ||
this.shutdown = true; | ||
this.onChangeCallbacks.forEach(cb => cb()); | ||
} | ||
|
||
// callback for useSyncExternalStore | ||
public getSnapshot = (): boolean => { | ||
return this.shutdown; | ||
}; | ||
|
||
// callback for useSyncExternalStore | ||
public subscribe = (onStoreChange: () => void): (() => void) => { | ||
this.onChangeCallbacks.add(onStoreChange); | ||
return () => { | ||
this.onChangeCallbacks.delete(onStoreChange); | ||
}; | ||
}; | ||
} | ||
|
||
function OutputRootComponent({ | ||
outputRoot, | ||
instanceStore, | ||
outputCtx, | ||
outputShutdownStateStore, | ||
}: { | ||
outputRoot: React.ReactElement; | ||
instanceStore: InstanceContextStore; | ||
outputCtx: OutputContext; | ||
outputShutdownStateStore: OutputShutdownStateStore; | ||
}) { | ||
const shouldShutdown = useSyncExternalStore( | ||
outputShutdownStateStore.subscribe, | ||
outputShutdownStateStore.getSnapshot | ||
); | ||
|
||
if (shouldShutdown) { | ||
// replace root with view to stop all the dynamic code | ||
return createElement(View, {}); | ||
} | ||
|
||
const reactCtx = { | ||
instanceStore, | ||
outputCtx, | ||
}; | ||
return createElement( | ||
_liveCompositorInternals.LiveCompositorContext.Provider, | ||
{ value: reactCtx }, | ||
outputRoot | ||
); | ||
} | ||
|
||
export default OfflineOutput; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters