-
Notifications
You must be signed in to change notification settings - Fork 703
/
Copy pathreplay.ts
114 lines (93 loc) · 2.92 KB
/
replay.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
import { Module, EApiPermissions, apiMethod, apiEvent, IApiContext } from './module';
import { StreamingService, EReplayBufferState } from 'services/streaming';
import { SettingsService } from 'services/settings';
import { Inject } from 'services/core/injector';
import { Subject } from 'rxjs';
import { FileReturnWrapper } from 'util/guest-api-handler';
import uuid from 'uuid/v4';
interface IReplayBufferState {
status: EReplayBufferState;
statusTime: string;
}
interface IReplayBufferFileInfo {
id: string;
filePath: string;
}
export class ReplayModule extends Module {
moduleName = 'Replay';
permissions = [EApiPermissions.Streaming];
@Inject() streamingService: StreamingService;
@Inject() settingsService: SettingsService;
/**
* Maps file id to filepath
*/
availableFiles: Dictionary<string> = {};
constructor() {
super();
this.streamingService.replayBufferStatusChange.subscribe(() => {
this.stateChanged.next(this.serializeState());
});
this.streamingService.replayBufferFileWrite.subscribe(filePath => {
const id = uuid();
this.availableFiles[id] = filePath;
this.fileSaved.next({ id, filePath });
});
}
@apiEvent()
stateChanged = new Subject<IReplayBufferState>();
@apiEvent()
fileSaved = new Subject<IReplayBufferFileInfo>();
@apiMethod()
getState(): IReplayBufferState {
return this.serializeState();
}
@apiMethod()
startBuffer() {
this.streamingService.startReplayBuffer();
}
@apiMethod()
stopBuffer() {
this.streamingService.stopReplayBuffer();
}
@apiMethod()
getEnabled() {
return this.settingsService.views.values.Output.RecRB;
}
@apiMethod()
setEnabled(ctx: IApiContext, enabled: boolean) {
if (this.getState().status === EReplayBufferState.Offline) {
this.settingsService.setSettingsPatch({ Output: { RecRB: enabled } });
} else {
throw new Error('Replay buffer must be stopped before its settings can be changed!');
}
}
@apiMethod()
getDuration() {
return this.settingsService.views.values.Output.RecRBTime;
}
@apiMethod()
setDuration(ctx: IApiContext, duration: number) {
if (this.getState().status === EReplayBufferState.Offline) {
this.settingsService.setSettingsPatch({ Output: { RecRBTime: duration } });
} else {
throw new Error('Replay buffer must be stopped before its settings can be changed!');
}
}
@apiMethod()
save() {
this.streamingService.saveReplay();
}
@apiMethod()
getFileContents(ctx: IApiContext, fileId: string) {
if (!this.availableFiles[fileId]) {
throw new Error(`The file with id ${fileId} does not exist!`);
}
return new FileReturnWrapper(this.availableFiles[fileId]);
}
private serializeState(): IReplayBufferState {
return {
status: this.streamingService.state.status.horizontal.replayBuffer,
statusTime: this.streamingService.state.status.horizontal.replayBufferTime,
};
}
}