-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathinput.ts
118 lines (108 loc) · 3.34 KB
/
input.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
import type { Api } from '../api';
import type {
RegisterMp4Input,
RegisterRtpInput,
Inputs,
RegisterWhipInput,
} from '@swmansion/smelter';
import { _smelterInternals } from '@swmansion/smelter';
/**
* It represents HTTP request that can be sent to
* to compositor, but also additional variants that are specific to WASM like camera
*/
export type RegisterInputRequest =
| Api.RegisterInput
| { type: 'mp4_blob'; blob: any }
| { type: 'camera' }
| { type: 'screen_capture' }
| { type: 'stream'; stream: any };
export type InputRef = _smelterInternals.InputRef;
export const inputRefIntoRawId = _smelterInternals.inputRefIntoRawId;
export const parseInputRef = _smelterInternals.parseInputRef;
export type RegisterInput =
| ({ type: 'rtp_stream' } & RegisterRtpInput)
| ({ type: 'mp4' } & RegisterMp4Input)
| ({ type: 'whip' } & RegisterWhipInput)
| { type: 'camera' }
| { type: 'screen_capture' }
| { type: 'stream'; stream: any };
/**
* Converts object passed by user (or modified by platform specific interface) into
* HTTP request
*/
export function intoRegisterInput(input: RegisterInput): RegisterInputRequest {
if (input.type === 'mp4') {
return intoMp4RegisterInput(input);
} else if (input.type === 'rtp_stream') {
return intoRtpRegisterInput(input);
} else if (input.type === 'whip') {
return intoWhipRegisterInput(input);
} else if (input.type === 'camera') {
return { type: 'camera' };
} else if (input.type === 'screen_capture') {
return { type: 'screen_capture' };
} else if (input.type === 'stream') {
return { type: 'stream', stream: input.stream };
} else {
throw new Error(`Unknown input type ${(input as any).type}`);
}
}
function intoMp4RegisterInput(input: Inputs.RegisterMp4Input): RegisterInputRequest {
if (input.blob) {
return {
type: 'mp4_blob',
blob: input.blob,
};
}
return {
type: 'mp4',
url: input.url,
path: input.serverPath,
loop: input.loop,
required: input.required,
offset_ms: input.offsetMs,
video_decoder: input.videoDecoder,
};
}
function intoRtpRegisterInput(input: Inputs.RegisterRtpInput): RegisterInputRequest {
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 intoWhipRegisterInput(input: Inputs.RegisterWhipInput): RegisterInputRequest {
return {
type: 'whip',
video: input.video,
audio: input.audio && intoInputWhipAudioOptions(input.audio),
required: input.required,
offset_ms: input.offsetMs,
};
}
function intoInputWhipAudioOptions(input: Inputs.InputWhipAudioOptions): Api.InputWhipAudioOptions {
return {
decoder: 'opus',
forward_error_correction: input.forwardErrorCorrection,
};
}
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}`);
}
}