Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TS SDK #673

Merged
merged 4 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions .github/workflows/ts_sdk_check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: TS SDK check

on:
push:
branches: [master]
paths:
- "ts/**"
pull_request:
types: [opened, synchronize]
paths:
- "ts/**"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/master' }}

jobs:
check:
runs-on: ubuntu-latest

steps:
- name: Checkout repo
uses: actions/checkout@v2

- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: "20"

- name: Cache dependencies
uses: actions/cache@v2
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
${{ runner.os }}-

- name: Install project dependencies
working-directory: ./ts
run: npm install

- name: Build
working-directory: ./ts
run: npm run build

- name: Lint code
working-directory: ./ts
run: npm run lint

- name: Typecheck code
working-directory: ./ts
run: npm run typecheck
31 changes: 31 additions & 0 deletions ts/.eslintrc.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:import/recommended",
"plugin:import/typescript",
"prettier"
],
"plugins": [
"prettier"
],
"parser": "@typescript-eslint/parser",
"rules": {
"prettier/prettier": ["error"],
"@typescript-eslint/no-explicit-any": [0, {}],
"no-constant-condition": [0],
"@typescript-eslint/no-unused-vars": [
"error",
{
"args": "all",
"argsIgnorePattern": "^_",
"caughtErrors": "all",
"caughtErrorsIgnorePattern": "^_",
"destructuredArrayIgnorePattern": "^_",
"varsIgnorePattern": "^_",
"ignoreRestSiblings": true
}
]
}
}
2 changes: 2 additions & 0 deletions ts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
9 changes: 9 additions & 0 deletions ts/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"printWidth": 100,
"tabWidth": 2,
"singleQuote": true,
"bracketSameLine": true,
"trailingComma": "es5",
"arrowParens": "avoid",
"endOfLine": "auto"
}
1 change: 1 addition & 0 deletions ts/@live-compositor/core/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
5 changes: 5 additions & 0 deletions ts/@live-compositor/core/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": [
"../../.eslintrc.base.json"
]
}
24 changes: 24 additions & 0 deletions ts/@live-compositor/core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@live-compositor/core",
"version": "0.1.0-rc.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"lint": "eslint .",
"typecheck": "tsc --noEmit",
"watch": "tsc --watch",
"build": "tsc",
"clean": "rimraf dist"
},
"author": "",
"license": "MIT",
"devDependencies": {
"@types/react-reconciler": "0.28.8"
},
"dependencies": {
"react-reconciler": "0.29.2"
},
"peerDependencies": {
"live-compositor": "0.1.0-rc.0"
}
}
117 changes: 117 additions & 0 deletions ts/@live-compositor/core/src/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { Api } from 'live-compositor';
import { CompositorManager } from './compositorManager';

export { Api };

export type ApiRequest = {
method: 'GET' | 'POST';
route: string;
body?: object;
};

export class ApiClient {
private serverManager: CompositorManager;

constructor(serverManager: CompositorManager) {
this.serverManager = serverManager;
}

public async updateScene(outputId: string, request: Api.UpdateOutputRequest): Promise<object> {
return this.serverManager.sendRequest({
method: 'POST',
route: `/api/output/${encodeURIComponent(outputId)}/update`,
body: request,
});
}

public async registerOutput(outptuId: string, request: Api.RegisterOutput): Promise<object> {
return this.serverManager.sendRequest({
method: 'POST',
route: `/api/output/${encodeURIComponent(outptuId)}/register`,
body: request,
});
}

public async unregisterOutput(outptuId: string): Promise<object> {
return this.serverManager.sendRequest({
method: 'POST',
route: `/api/output/${encodeURIComponent(outptuId)}/unregister`,
body: {},
});
}

public async registerInput(inputId: string, request: Api.RegisterInput): Promise<object> {
return this.serverManager.sendRequest({
method: 'POST',
route: `/api/input/${encodeURIComponent(inputId)}/register`,
body: request,
});
}

public async unregisterInput(inputId: string): Promise<object> {
return this.serverManager.sendRequest({
method: 'POST',
route: `/api/input/${encodeURIComponent(inputId)}/unregister`,
body: {},
});
}

public async registerShader(shaderId: string, request: Api.ShaderSpec): Promise<object> {
return this.serverManager.sendRequest({
method: 'POST',
route: `/api/shader/${encodeURIComponent(shaderId)}/register`,
body: request,
});
}

public async unregisterShader(shaderId: string): Promise<object> {
return this.serverManager.sendRequest({
method: 'POST',
route: `/api/shader/${encodeURIComponent(shaderId)}/unregister`,
body: {},
});
}

public async registerImage(imageId: string, request: Api.ImageSpec): Promise<object> {
return this.serverManager.sendRequest({
method: 'POST',
route: `/api/image/${encodeURIComponent(imageId)}/register`,
body: request,
});
}

public async unregisterImage(imageId: string): Promise<object> {
return this.serverManager.sendRequest({
method: 'POST',
route: `/api/image/${encodeURIComponent(imageId)}/unregister`,
body: {},
});
}

public async registerWebRenderer(
instanceId: string,
request: Api.WebRendererSpec
): Promise<object> {
return this.serverManager.sendRequest({
method: 'POST',
route: `/api/web-renderer/${encodeURIComponent(instanceId)}/register`,
body: request,
});
}

public async unregisterWebRenderer(instanceId: string): Promise<object> {
return this.serverManager.sendRequest({
method: 'POST',
route: `/api/web-renderer/${encodeURIComponent(instanceId)}/unregister`,
body: {},
});
}

public async start(): Promise<void> {
await this.serverManager.sendRequest({
method: 'POST',
route: `/api/start`,
body: {},
});
}
}
51 changes: 51 additions & 0 deletions ts/@live-compositor/core/src/api/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Api } from '../api';
import { RegisterInput, Inputs } from 'live-compositor';

export function intoRegisterInput(input: RegisterInput): Api.RegisterInput {
if (input.type === 'mp4') {
return intoMp4RegisterInput(input);
} else if (input.type === 'rtp_stream') {
return intoRtpRegisterInput(input);
} else {
throw new Error(`Unknown input type ${(input as any).type}`);
}
}

function intoMp4RegisterInput(input: Inputs.RegisterMp4Input): Api.RegisterInput {
return {
type: 'mp4',
url: input.url,
path: input.path,
required: input.required,
offset_ms: input.offsetMs,
};
}

function intoRtpRegisterInput(input: Inputs.RegisterRtpInput): Api.RegisterInput {
return {
type: 'rtp_stream',
port: input.port,
transport_protocol: input.transportProtocol,
video: input.video,
audio: input.audio && intoInputAudio(input.audio),
required: input.required,
offset_ms: input.offsetMs,
};
}

function intoInputAudio(audio: Inputs.InputRtpAudioOptions): Api.InputRtpAudioOptions {
if (audio.decoder === 'opus') {
return {
decoder: 'opus',
forward_error_correction: audio.forwardErrorCorrection,
};
} else if (audio.decoder === 'aac') {
return {
decoder: 'aac',
audio_specific_config: audio.audioSpecificConfig,
rtp_mode: audio.rtpMode,
};
} else {
throw new Error(`Unknown audio decoder type: ${(audio as any).decoder}`);
}
}
87 changes: 87 additions & 0 deletions ts/@live-compositor/core/src/api/output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { RegisterOutput, Api, Outputs } from 'live-compositor';

export function intoRegisterOutput(
output: RegisterOutput,
initialVideo?: Api.Video
): Api.RegisterOutput {
if (output.type === 'rtp_stream') {
return intoRegisterRtpOutput(output, initialVideo);
} else {
throw new Error(`Unknown input type ${(output as any).type}`);
}
}
Comment on lines +3 to +12
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add mp4 output


function intoRegisterRtpOutput(
output: Outputs.RegisterRtpOutput,
initialVideo?: Api.Video
): Api.RegisterOutput {
return {
type: 'rtp_stream',
port: output.port,
ip: output.ip,
transport_protocol: output.transportProtocol,
video: output.video && initialVideo && intoOutputRtpVideoOptions(output.video, initialVideo),
audio: output.audio && intoOutputRtpAudioOptions(output.audio),
};
}

function intoOutputRtpVideoOptions(
video: Outputs.OutputRtpVideoOptions,
initial: Api.Video
): Api.OutputRtpVideoOptions {
return {
resolution: video.resolution,
send_eos_when: video.sendEosWhen && intoOutputEosCondition(video.sendEosWhen),
encoder: intoVideoEncoderOptions(video.encoder),
initial,
};
}

function intoVideoEncoderOptions(encoder: Outputs.VideoEncoderOptions): Api.VideoEncoderOptions {
return {
type: 'ffmpeg_h264',
preset: encoder.preset,
ffmpeg_options: encoder.ffmpegOptions,
};
}

function intoOutputRtpAudioOptions(
audio: Outputs.OutputRtpAudioOptions
): Api.OutputRtpAudioOptions {
return {
send_eos_when: audio.sendEosWhen && intoOutputEosCondition(audio.sendEosWhen),
encoder: intoAudioEncoderOptions(audio.encoder),
initial: intoAudioInputsConfiguration(audio.initial),
};
}

function intoAudioEncoderOptions(encoder: Outputs.AudioEncoderOptions): Api.AudioEncoderOptions {
return {
type: 'opus',
preset: encoder.preset,
channels: encoder.channels,
};
}

export function intoAudioInputsConfiguration(audio: Outputs.AudioInputsConfiguration): Api.Audio {
return {
inputs: audio.inputs.map(input => ({
input_id: input.inputId,
volume: input.volume,
})),
};
}

function intoOutputEosCondition(condition: Outputs.OutputEndCondition): Api.OutputEndCondition {
if ('anyOf' in condition) {
return { any_of: condition.anyOf };
} else if ('allOf' in condition) {
return { all_of: condition.allOf };
} else if ('allInputs' in condition) {
return { all_inputs: condition.allInputs };
} else if ('anyInput' in condition) {
return { any_input: condition.anyInput };
} else {
throw new Error('Invalid "send_eos_when" value.');
}
}
Loading
Loading