/
EgressClient.ts
177 lines (159 loc) · 4.35 KB
/
EgressClient.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
import { AccessToken } from './AccessToken';
import { VideoGrant } from './grants';
import {
EgressInfo,
EncodedFileOutput,
EncodingOptions,
EncodingOptionsPreset,
ListEgressRequest,
StopEgressRequest,
StreamOutput,
UpdateLayoutRequest,
UpdateStreamRequest,
WebCompositeEgressRequest,
} from './proto/livekit_egress';
import { livekitPackage, Rpc, TwirpRpc } from './TwirpRPC';
const svc = 'Egress';
/**
* Client to access Egress APIs
*/
export default class EgressClient {
private readonly rpc: Rpc;
private readonly apiKey?: string;
private readonly secret?: string;
/**
* @param host hostname including protocol. i.e. 'https://cluster.livekit.io'
* @param apiKey API Key, can be set in env var LIVEKIT_API_KEY
* @param secret API Secret, can be set in env var LIVEKIT_API_SECRET
*/
constructor(host: string, apiKey?: string, secret?: string) {
this.rpc = new TwirpRpc(host, livekitPackage);
this.apiKey = apiKey;
this.secret = secret;
}
/**
* @param roomName room name
* @param layout egress layout
* @param output output filepath or RtmpOutput
* @param options egress options or preset
* @param audioOnly record audio only
* @param videoOnly record video only
* @param customBaseUrl custom template url
*/
async startWebCompositeEgress(
roomName: string,
layout: string,
output: EncodedFileOutput | StreamOutput,
options?: EncodingOptionsPreset | EncodingOptions,
audioOnly?: boolean,
videoOnly?: boolean,
customBaseUrl?: string,
): Promise<EgressInfo> {
let file: EncodedFileOutput | undefined;
let stream: StreamOutput | undefined;
let preset: EncodingOptionsPreset | undefined;
let advanced: EncodingOptions | undefined;
if (!audioOnly) {
audioOnly = false;
}
if (!videoOnly) {
videoOnly = false;
}
if (!customBaseUrl) {
customBaseUrl = '';
}
if ((<EncodedFileOutput>output).filepath !== undefined) {
file = <EncodedFileOutput>output;
} else {
stream = <StreamOutput>output;
}
if (options) {
if (typeof options === 'number') {
preset = <EncodingOptionsPreset>options;
} else {
advanced = <EncodingOptions>options;
}
}
const req = WebCompositeEgressRequest.toJSON({
roomName, layout, audioOnly, videoOnly, file, stream, preset, advanced, customBaseUrl,
});
const data = await this.rpc.request(
svc,
'StartWebCompositeEgress',
req,
this.authHeader({ roomRecord: true }),
);
return EgressInfo.fromJSON(data);
}
/**
* @param egressId
* @param layout
*/
async updateLayout(egressId: string, layout: string): Promise<EgressInfo> {
const data = await this.rpc.request(
svc,
'UpdateLayout',
UpdateLayoutRequest.toJSON({ egressId, layout }),
this.authHeader({ roomRecord: true }),
);
return EgressInfo.fromJSON(data);
}
/**
* @param egressId
* @param addOutputUrls
* @param removeOutputUrls
*/
async updateStream(
egressId: string,
addOutputUrls?: string[],
removeOutputUrls?: string[],
): Promise<EgressInfo> {
if (!addOutputUrls) {
addOutputUrls = [];
}
if (!removeOutputUrls) {
removeOutputUrls = [];
}
const data = await this.rpc.request(
svc,
'UpdateStream',
UpdateStreamRequest.toJSON({ egressId, addOutputUrls, removeOutputUrls }),
this.authHeader({ roomRecord: true }),
);
return EgressInfo.fromJSON(data);
}
/**
* @param roomName list egress for one room only
*/
async listEgress(roomName?: string): Promise<EgressInfo> {
if (!roomName) {
roomName = '';
}
const data = await this.rpc.request(
svc,
'ListEgress',
ListEgressRequest.toJSON({ roomName }),
this.authHeader({ roomRecord: true }),
);
return EgressInfo.fromJSON(data);
}
/**
* @param egressId
*/
async stopEgress(egressId: string): Promise<EgressInfo> {
const data = await this.rpc.request(
svc,
'StopEgress',
StopEgressRequest.toJSON({ egressId }),
this.authHeader({ roomRecord: true }),
);
return EgressInfo.fromJSON(data);
}
private authHeader(grant: VideoGrant): any {
const at = new AccessToken(this.apiKey, this.secret, { ttl: '10m' });
at.addGrant(grant);
return {
Authorization: `Bearer ${at.toJwt()}`,
};
}
}