-
Notifications
You must be signed in to change notification settings - Fork 3
/
janus_plugin.ts
257 lines (252 loc) · 6.89 KB
/
janus_plugin.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
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
import _ from "lodash";
import Janus, {
AnswerParams,
Controllers,
DataParams,
DetachOptions,
JSEP,
OfferParams,
PluginHandle,
PluginMessage,
WebRTCInfo,
} from "./interfaces/janus";
import { JanusJs } from "./janus_js";
import { JanusSession } from "./janus_session";
export class JanusPlugin implements PluginHandle {
protected statsReportHookTimer: any;
protected controllers: Controllers;
protected instance: Janus;
protected handle: PluginHandle;
protected session: JanusSession;
protected mediaRecorder: MediaRecorder;
plugin: string;
id: string;
token?: string;
detached: boolean;
webrtcStuff: WebRTCInfo;
constructor(
instance: Janus,
session: JanusSession,
handle: PluginHandle,
controllers: Controllers
) {
this.instance = instance;
this.session = session;
this.controllers = controllers;
this.handle = handle;
this.statsReportHookTimer = this.handleStatsHook(
this.handle,
controllers,
null
);
this.handleRecordingSetup(controllers);
}
recordingTimeSlice?: number;
protected handleRecordingSetup(controllers: Controllers) {
let data: any;
this.onMessage.subscribe(({ message }) => {
const result = message?.result;
if (result?.event === "accepted" || result?.event === "progress") {
if (!data) {
const remoteStream = new MediaStream();
if (!this.webrtcStuff.remoteStream || !this.webrtcStuff.myStream) {
return;
}
console.info("recording initiated");
data = JanusJs.createRecording({
mediaStreams: [
this.webrtcStuff.myStream,
this.webrtcStuff.remoteStream,
],
timeSlice: this.recordingTimeSlice,
});
}
if (data) {
this.mediaRecorder = data.mediaRecorder;
data.controller.subscribe((dat: any) => {
controllers.onRecordingDataController.next(dat);
});
}
}
if (result?.event === "hangup") {
if (this.mediaRecorder?.state !== "inactive") this.mediaRecorder?.stop();
}
});
}
protected handleStatsHook(
plugin: PluginHandle,
controllers: Controllers,
mediaStreamTrack: MediaStreamTrack = null
) {
return setInterval(async () => {
if (!plugin.webrtcStuff.pc) {
return;
}
const results: any[] = [];
const reports = await plugin.webrtcStuff.pc.getStats(mediaStreamTrack);
reports.forEach((report) => {
if (report.jitter) {
const info = {
jitter: report.jitter,
packetsLost: report.packetsLost,
roundTripTime: report.roundTripTime,
type: report.type,
};
results.push(info);
}
});
controllers.onStatReportsController.next(results);
}, 5000);
}
get recorder(): MediaRecorder {
return this.mediaRecorder;
}
get onRecordingData() {
return this.controllers.onRecordingDataController.asObservable();
}
get onStatReports() {
return this.controllers.onStatReportsController.asObservable();
}
get onMessage() {
return this.controllers.onMessageController.asObservable();
}
get onLocalTrack() {
return this.controllers.onLocalTrackController.asObservable();
}
get onData() {
return this.controllers.onDataController.asObservable();
}
get onError() {
return this.controllers.onErrorController.asObservable();
}
get onRemoteTrack() {
return this.controllers.onRemoteTrackController.asObservable();
}
get onMediaState() {
return this.controllers.onMediaStateController.asObservable();
}
get onSlowLink() {
return this.controllers.onSlowLinkController.asObservable();
}
get onWebRTCState() {
return this.controllers.onWebRTCStateController.asObservable();
}
get onIceState() {
return this.controllers.onIceStateController.asObservable();
}
get onDataOpen() {
return this.controllers.onDataOpenController.asObservable();
}
get onDetached() {
return this.controllers.onDetachedController.asObservable();
}
get onCleanup() {
return this.controllers.onCleanupController.asObservable();
}
getId(): string {
throw new Error("Method not implemented.");
}
getPlugin(): string {
throw new Error("Method not implemented.");
}
async send(message: Omit<PluginMessage, "success" | "error">): Promise<any> {
return new Promise((resolve, reject) => {
this.handle.send({
...message,
success(data) {
resolve(data);
},
error(error) {
reject(error);
},
});
});
}
createOffer(
params: Omit<OfferParams, "success" | "error">
): Promise<RTCSessionDescription> {
return new Promise<RTCSessionDescription>((resolve, reject) => {
this.handle.createOffer({
...params,
success(offer: RTCSessionDescription) {
resolve(offer);
},
error(error) {
reject(error);
},
});
});
}
createAnswer(
params: Omit<AnswerParams, "success" | "error">
): Promise<RTCSessionDescription> {
return new Promise<RTCSessionDescription>((resolve, reject) => {
this.handle.createAnswer({
...params,
success(offer: RTCSessionDescription) {
resolve(offer);
},
error(error: any) {
reject(error);
},
});
});
}
data(params: DataParams): Promise<void> {
return new Promise((resolve, reject) => {
this.handle.data({
...params,
success() {
resolve();
},
error(error: any) {
reject(error);
},
});
});
}
handleRemoteJsep(params: { jsep: JSEP }): void {
throw new Error("Method not implemented.");
}
dtmf(params: any): void {
throw new Error("Method not implemented.");
}
isAudioMuted(): boolean {
throw new Error("Method not implemented.");
}
muteAudio(): void {
throw new Error("Method not implemented.");
}
unmuteAudio(): void {
throw new Error("Method not implemented.");
}
isVideoMuted(): boolean {
throw new Error("Method not implemented.");
}
muteVideo(): void {
throw new Error("Method not implemented.");
}
unmuteVideo(): void {
throw new Error("Method not implemented.");
}
getBitrate(): string {
throw new Error("Method not implemented.");
}
hangup(sendRequest?: boolean): void {
throw new Error("Method not implemented.");
}
detach(params?: DetachOptions): void {
throw new Error("Method not implemented.");
}
stopCollectingStats() {
clearInterval(this.statsReportHookTimer);
}
}
export abstract class JanusPlugins {
static VIDEO_ROOM = "janus.plugin.videoroom";
static VIDEO_CALL = "janus.plugin.videocall";
static AUDIO_BRIDGE = "janus.plugin.audiobridge";
static SIP = "janus.plugin.sip";
static STREAMING = "janus.plugin.streaming";
static ECHO_TEST = "janus.plugin.echotest";
}