-
Notifications
You must be signed in to change notification settings - Fork 10
/
plugin.ts
127 lines (108 loc) · 3.36 KB
/
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
import Promise from 'bluebird';
import JanusError from './misc/error';
import TransactionManager from './tx/transaction-manager';
import Transaction from './tx/transaction';
import JanusPluginMessage from './misc/plugin-message';
import Session from './session';
import JanusMessage from './misc/message';
import { MediaDevices, WebRTC } from '../plugin/base/shims/definitions';
class Plugin extends TransactionManager {
private static types = {};
private session: Session | null;
private readonly name: string;
private readonly id: string;
constructor(session: Session, name: string, id: string) {
super();
this.session = session;
this.name = name;
this.id = id;
session.on('destroy', () => this.onDetached());
}
static create(session: Session, name: string, id: string, mediaDevices: MediaDevices, webRTC: WebRTC): Plugin {
//@ts-ignore
let aClass = Plugin.types[name];
if (aClass) return new aClass(session, name, id, mediaDevices, webRTC);
return new Plugin(session, name, id);
}
//@ts-ignore
static register(name: string, aClass) {
//@ts-ignore
this.types[name] = aClass;
}
getSession(): Session | null {
return this.session;
}
getId(): string {
return this.id;
}
getName(): string {
return this.name;
}
getResponseAlias(): string {
throw new Error('Plugin.getResponseAlias must be implemented.');
}
send(msg: any): Promise<any> {
msg['handle_id'] = this.id;
if (this.session) {
return this.session.send(msg);
} else {
return Promise.reject(new Error('No active session.'));
}
}
detach(): Promise<any> {
if (this.session) {
return new Promise((resolve, reject) => {
this.once('detach', resolve);
this.sendWithTransaction({ janus: 'detach' }).catch(reject);
});
}
return Promise.resolve();
}
onDetached(): Promise<any> {
if (this.session) {
this.session = null;
this.emit('detach');
}
return Promise.resolve();
}
cleanup(): Promise<any> {
return this.onDetached();
}
processOutcomeMessage(msg: any): Promise<any> {
return Promise.resolve(msg);
}
processIncomeMessage(msg: JanusMessage): Promise<any> {
return Promise.try(() => {
msg = new JanusPluginMessage(msg.getPlainMessage(), this);
if ('detached' === msg.get('janus')) {
return this.onDetached();
}
return this.defaultProcessIncomeMessage(msg);
})
.then(() => this.emit('message', msg))
.catch(error => this.emit('error', error));
}
sendWithTransaction(options: any): Promise<any> {
let transactionId = Transaction.generateRandomId();
//@ts-ignore
let transaction = new Transaction(transactionId, msg => {
let errorMessage = msg.getError();
if (!errorMessage) {
return Promise.resolve(msg);
}
let error = new JanusError(msg);
return Promise.reject(error);
});
let message = Object.assign({ janus: 'message', transaction: transactionId }, options);
this.addTransaction(transaction);
let sendPromise = this.sendSync(message, this);
return new Promise((resolve, reject) => {
transaction.getPromise().catch(e => reject(e));
sendPromise.then(r => resolve(r)).catch(e => reject(e));
});
}
toString() {
return `[Plugin] ${JSON.stringify({ id: this.id, name: this.name })}`;
}
}
export default Plugin;