-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathobs_handler.ts
More file actions
565 lines (470 loc) · 18.3 KB
/
Copy pathobs_handler.ts
File metadata and controls
565 lines (470 loc) · 18.3 KB
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
import * as osn from '../osn';
import { logInfo, logWarning } from '../util/logger';
import { UserPoolHandler } from './user_pool_handler';
import { CacheUploader } from '../util/cache-uploader'
import { EOBSOutputType, EOBSOutputSignal, EOBSSettingsCategories } from '../util/obs_enums'
import { sleep } from './general';
import { loadTestEnv } from './env';
import { v4 as uuidv4 } from 'uuid';
const WaitQueue = require('wait-queue');
const retryContext = require('./retry_context.ts');
loadTestEnv();
// Interfaces
export interface IPerformanceState {
CPU: number;
numberDroppedFrames: number;
percentageDroppedFrames: number;
streamingBandwidth: number;
streamingDataOutput: number;
recordingBandwidth: number;
recordingDataOutput: number;
frameRate: number;
averageTimeToRenderFrame: number;
memoryUsage: number;
diskSpaceAvailable: string;
}
export interface IOBSOutputSignalInfo {
type: EOBSOutputType;
signal: EOBSOutputSignal;
code: osn.EOutputCode;
error: string;
service: string;
}
export interface IConfigProgress {
event: TConfigEvent;
description: string;
percentage?: number;
continent?: string;
}
export interface IVec2 {
x: number;
y: number;
}
interface IRetryFailure {
message?: string;
}
export interface ICrop {
top: number;
bottom: number;
left: number;
right: number;
}
// Types
export type TOBSHotkey = {
ObjectName: string;
ObjectType: osn.EHotkeyObjectType;
HotkeyName: string;
HotkeyDesc: string;
HotkeyId: number;
};
export type TConfigEvent = 'starting_step' | 'progress' | 'stopping_step' | 'error' | 'done';
// OBSHandler class
export class OBSHandler {
private path = require('path');
// Variables for obs initialization
private workingDirectory: string = this.path.normalize(osn.wd);
private language: string = 'en-US';
private obsPath: string = this.path.join(this.path.normalize(__dirname), '..', 'osnData/slobs-client');
private pipeName: string = 'osn-tests-pipe-'.concat(uuidv4());
private version: string = '0.00.00-preview.0';
private crashServer: string = '';
// Other variables/objects
private userPoolHandler: UserPoolHandler;
private cacheUploader: CacheUploader;
private hasUserFromPool: boolean = false;
private osnTestName: string;
signals = new WaitQueue();
private progress = new WaitQueue();
inputTypes: string[];
filterTypes: string[];
transitionTypes: string[];
os: string;
ci: boolean;
userStreamKey: string;
defaultVideoContext: osn.IVideo;
constructor(testName: string, needDefaultVideoContext: boolean = true) {
this.os = process.platform;
this.osnTestName = testName;
this.cacheUploader = new CacheUploader(testName, this.obsPath);
this.ci = process.env.CI === 'true';
this.startup();
if (needDefaultVideoContext) {
this.createDefaultVideoContext();
}
this.inputTypes = osn.InputFactory.types();
const index = this.inputTypes.indexOf('syphon-input', 0);
if (index > -1) {
this.inputTypes.splice(index, 1);
}
this.filterTypes = osn.FilterFactory.types();
this.transitionTypes = osn.TransitionFactory.types();
}
startup() {
let initResult: number;
logInfo(this.osnTestName, 'Initializing OBS');
try {
const exitCode = osn.NodeObs.IPC.host(this.pipeName);
if (exitCode !== osn.EVideoCodes.Success) {
if (exitCode === osn.EIPCError.OTHER_ERROR) {
throw Error('OBS IPC host failed: missing executable or some other error.');
}
throw Error(`OBS IPC host failed with code ${exitCode}. See osn.EIPCError for more details.`);
}
osn.NodeObs.SetWorkingDirectory(this.workingDirectory);
initResult = osn.NodeObs.OBS_API_initAPI(this.language, this.obsPath, this.version, this.crashServer);
} catch (e) {
throw Error('Exception when initializing OBS process: ' + e);
}
if (initResult !== osn.EVideoCodes.Success) {
throw Error('OBS process initialization failed with code ' + initResult);
}
logInfo(this.osnTestName, 'OBS started successfully');
}
shutdown() {
if(this.defaultVideoContext) {
this.destroyDefaultVideoContext();
}
logInfo(this.osnTestName, 'Shutting down OBS');
try {
osn.NodeObs.OBS_service_removeCallback();
osn.NodeObs.IPC.disconnect();
} catch (e) {
throw Error('Exception when shutting down OBS process: ' + e);
}
logInfo(this.osnTestName, 'OBS shutdown successfully');
}
instantiateUserPool(testName: string) {
this.userPoolHandler = new UserPoolHandler(testName);
}
async reserveUser() {
this.userStreamKey = "";
try {
logInfo(this.osnTestName, 'Getting stream key from user pool');
this.userStreamKey = await this.userPoolHandler.getStreamKey();
this.hasUserFromPool = true;
} catch (e) {
logWarning(this.osnTestName, e);
logWarning(this.osnTestName, 'Using predefined stream key');
this.userStreamKey = process.env.SLOBS_BE_STREAMKEY;
this.hasUserFromPool = false;
}
logInfo(this.osnTestName, 'Saving stream key');
this.setStreamKey(this.userStreamKey);
let savedStreamKey = this.getStreamKey();
if (savedStreamKey === this.userStreamKey) {
logInfo(this.osnTestName, 'Stream key saved successfully');
} else {
throw Error('Failed to save stream key');
}
}
async releaseUser() {
if (this.hasUserFromPool) {
await this.userPoolHandler.releaseUser();
this.hasUserFromPool = false;
}
}
async finalizeRetryableTest(context: Mocha.Context): Promise<boolean> {
const currentTest = context.currentTest;
const hasFailed = currentTest?.state === 'failed';
await this.prepareRetryUserIfNeeded(currentTest);
return hasFailed;
}
private clearOutputSignals() {
this.signals = new WaitQueue();
}
private getRetryFailureMessage(retryFailure?: IRetryFailure | null): string {
if (!retryFailure || typeof retryFailure.message !== 'string') {
return '';
}
return retryFailure.message;
}
private getTestLabel(test?: Mocha.Test): string {
if (!test) {
return 'unknown test';
}
if (typeof test.fullTitle === 'function') {
const fullTitle = test.fullTitle();
if (fullTitle.length > 0) {
return fullTitle;
}
}
return test.title || 'unknown test';
}
private hasRetriesRemaining(test?: Mocha.Test): boolean {
const runnable = test as any;
if (!runnable || typeof runnable.currentRetry !== 'function' || typeof runnable.retries !== 'function') {
return false;
}
return runnable.currentRetry() < runnable.retries();
}
private getPendingRetryFailure(test?: Mocha.Test): IRetryFailure | null {
if (!test || !this.hasRetriesRemaining(test)) {
return null;
}
const retryFailure = retryContext.getRetryFailure(test);
return retryFailure || null;
}
private parseOutputErrorCode(message: string): osn.EOutputCode | null {
const codeMatch = message.match(/Error code:\s*(-?\d+)/i);
if (!codeMatch) {
return null;
}
const parsedCode = Number(codeMatch[1]);
return Number.isFinite(parsedCode)
? parsedCode as osn.EOutputCode
: null;
}
private shouldRotateUserForRetry(retryFailure: IRetryFailure): string | null {
if (!this.hasUserFromPool || !this.userPoolHandler) {
return null;
}
const message = this.getRetryFailureMessage(retryFailure);
if (!message) {
return null;
}
// Only rotate the pooled account when the failure suggests a bad remote
// session or throttled credentials; most flaky cases can reuse the user
// once OBS state has been cleaned up.
const outputErrorCode = this.parseOutputErrorCode(message);
if (outputErrorCode === osn.EOutputCode.ConnectFailed
|| outputErrorCode === osn.EOutputCode.Disconnected) {
return `received streaming output error ${outputErrorCode}`;
}
const normalizedMessage = message.toLowerCase();
const retryableTimeouts = [
'streaming starting signal timeout',
'streaming activate signal timeout',
'streaming start signal timeout',
];
if (retryableTimeouts.some(timeoutMessage => normalizedMessage.includes(timeoutMessage))) {
return message;
}
return null;
}
private async cleanupOutputsForRetry() {
// The next attempt reuses the same OBS process, so clear both the active
// outputs and any queued signals that would otherwise bleed into the retry.
this.clearOutputSignals();
try {
osn.NodeObs.OBS_service_stopReplayBuffer(false);
} catch (e) {}
try {
osn.NodeObs.OBS_service_stopRecording();
} catch (e) {}
try {
osn.NodeObs.OBS_service_stopStreaming(false);
} catch (e) {}
await sleep(1000);
this.clearOutputSignals();
}
private restoreKnownGoodStreamKey() {
if (!this.userStreamKey) {
return;
}
logInfo(this.osnTestName, 'Restoring stream key before retry');
this.setStreamKey(this.userStreamKey);
if (this.getStreamKey() !== this.userStreamKey) {
throw new Error('Failed to restore stream key before retry');
}
logInfo(this.osnTestName, 'Stream key restored before retry');
}
async prepareRetryUserIfNeeded(test?: Mocha.Test): Promise<void> {
// Tests call this from afterEach so we can recover before Mocha starts the
// next attempt. The retry_context store tells us why the previous attempt failed.
const retryFailure = this.getPendingRetryFailure(test);
if (!retryFailure) {
return;
}
const testLabel = this.getTestLabel(test);
const failureMessage = this.getRetryFailureMessage(retryFailure);
const replacementReason = this.shouldRotateUserForRetry(retryFailure);
try {
logWarning(this.osnTestName, `Preparing retry for "${testLabel}" after: ${failureMessage}`);
await this.cleanupOutputsForRetry();
if (replacementReason) {
logWarning(this.osnTestName, `Replacing user pool account before retrying "${testLabel}": ${replacementReason}`);
this.userPoolHandler.markCurrentUserUnhealthy(`retrying "${testLabel}" after ${replacementReason}`);
await this.releaseUser();
await this.reserveUser();
logInfo(this.osnTestName, `Prepared fresh stream credentials for retrying "${testLabel}"`);
} else {
this.restoreKnownGoodStreamKey();
logInfo(this.osnTestName, `Prepared clean OBS state for retrying "${testLabel}"`);
}
this.clearOutputSignals();
} catch (e) {
logWarning(this.osnTestName, `Unable to prepare retry for "${testLabel}": ${e}`);
} finally {
retryContext.clearRetryFailure(test);
}
}
async uploadTestCache() {
try {
await this.cacheUploader.uploadCache();
} catch (e) {
logWarning(this.osnTestName, e);
}
};
setStreamKey(value: string) {
const service = osn.ServiceFactory.legacySettings;
service.update({ key: value });
osn.ServiceFactory.legacySettings = service;
this.setSetting(EOBSSettingsCategories.Stream, 'key', value);
}
getStreamKey(): string {
const service = osn.ServiceFactory.legacySettings;
return service.settings.key;
}
setSetting(category: string, parameter: string, value: any) {
let oldValue: any;
// Getting settings container
const settings = osn.NodeObs.OBS_settings_getSettings(category).data;
settings.forEach(subCategory => {
subCategory.parameters.forEach(param => {
if (param.name === parameter) {
oldValue = param.currentValue;
param.currentValue = value;
}
});
});
// Saving updated settings container
if (value !== oldValue) {
osn.NodeObs.OBS_settings_saveSettings(category, settings);
}
}
getSetting(category: string, parameter: string): any {
let value: any;
// Getting settings container
const settings = osn.NodeObs.OBS_settings_getSettings(category).data;
// Getting parameter value
settings.forEach(subCategory => {
subCategory.parameters.forEach(param => {
if (param.name === parameter) {
value = param.currentValue;
}
});
});
return value;
}
setSettingsContainer(category: string, settings: any) {
osn.NodeObs.OBS_settings_saveSettings(category, settings);
}
getSettingsContainer(category: string): any {
return osn.NodeObs.OBS_settings_getSettings(category).data;
}
connectOutputSignals() {
osn.NodeObs.OBS_service_connectOutputSignals((signalInfo: IOBSOutputSignalInfo) => {
this.signals.push(signalInfo);
});
}
private isTerminalErrorSignal(signalInfo: IOBSOutputSignalInfo, output: string): boolean {
return signalInfo.type === output
&& ((signalInfo.signal === EOBSOutputSignal.Stop && signalInfo.code !== osn.EOutputCode.Success)
|| signalInfo.signal === EOBSOutputSignal.WriteError);
}
private formatSignalInfo(signalInfo: IOBSOutputSignalInfo): string {
return `${signalInfo.type}/${signalInfo.signal} (code=${signalInfo.code}, error=${signalInfo.error || 'none'})`;
}
async getNextSignalInfo(output: string, signal: string): Promise<IOBSOutputSignalInfo> {
return await this.getNextSignalInfoOf(output, [signal]);
}
async getNextSignalInfoOf(output: string, signals: string[]): Promise<IOBSOutputSignalInfo> {
const signalDescription = signals.join('/');
const timeoutMessage = output.replace(/^\w/, c => c.toUpperCase()) + ' ' + signalDescription + ' signal timeout';
const deadline = Date.now() + 30000;
while (Date.now() < deadline) {
const remainingMs = deadline - Date.now();
const signalInfo = await Promise.race([
this.signals.shift(),
new Promise<IOBSOutputSignalInfo>((resolve, reject) => {
setTimeout(() => reject(new Error(timeoutMessage)), remainingMs);
}),
]);
if (signalInfo.type === output && signals.indexOf(signalInfo.signal) >= 0) {
return signalInfo;
}
if (this.isTerminalErrorSignal(signalInfo, output)) {
return signalInfo;
}
logWarning(
this.osnTestName,
`Skipping unexpected output signal while waiting for ${output}/${signalDescription}: ${this.formatSignalInfo(signalInfo)}`,
);
}
throw new Error(timeoutMessage);
}
startAutoconfig() {
osn.NodeObs.InitializeAutoConfig((progressInfo: IConfigProgress) => {
if (progressInfo.event === 'stopping_step' || progressInfo.event === 'done' || progressInfo.event === 'error') {
this.progress.push(progressInfo);
}
},
{
service_name: 'Twitch',
});
}
getNextProgressInfo(autoconfigStep: string): Promise<IConfigProgress> {
return new Promise((resolve, reject) => {
this.progress.shift().then(
function (progressInfo) {
resolve(progressInfo)
}
);
setTimeout(() => reject(new Error(autoconfigStep + ' step timeout')), 50000);
});
}
createDefaultVideoContext() {
logInfo(this.osnTestName, 'createDefaultVideoContext called');
this.defaultVideoContext = osn.VideoFactory.create();
const defaultVideoInfo: osn.IVideoInfo = {
fpsNum: 60,
fpsDen: 1,
baseWidth: 1280,
baseHeight: 720,
outputWidth: 1280,
outputHeight: 720,
outputFormat: osn.EVideoFormat.NV12,
colorspace: osn.EColorSpace.CS709,
range: osn.ERangeType.Partial,
scaleType: osn.EScaleType.Bilinear,
fpsType: osn.EFPSType.Fractional
};
this.defaultVideoContext.video = defaultVideoInfo;
}
destroyDefaultVideoContext() {
this.defaultVideoContext.destroy();
this.defaultVideoContext = null;
}
skipSource(inputType: string) {
if (process.platform === 'darwin') {
if (inputType === 'browser_source' ||
inputType === 'window_capture' ||
inputType === 'monitor_capture' ||
inputType === 'display_capture' ||
inputType === 'screen_capture' ||
inputType === 'coreaudio_input_capture' ||
inputType === 'coreaudio_output_capture') {
return true;
}
}
return false
}
setSourceMessageListener() {
osn.NodeObs.RegisterSourceCallback((message: unknown) => {
console.log('Source callback received' + JSON.stringify(message));
});
osn.NodeObs.RegisterSourceMessageCallback((message: unknown) => {
console.log('Source message callback received' + JSON.stringify(message));
});
}
isDarwin()
{
// Wrapped this in a function- just in case we want to add more conditions later or disable only within the build agent.
return this.os === 'darwin';
}
// is the build server environment
isCI()
{
return this.ci;
}
}