-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbase-plugin.js
278 lines (248 loc) · 7.65 KB
/
base-plugin.js
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
/*
minnie-janus - Minimal and modern JavaScript interface for the Janus WebRTC gateway
Copyright 2018 Michael Karl Franzl
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* @module
*/
import EventEmitter from '@michaelfranzl/captain-hook';
/**
* @lends BasePlugin
*/
const properties = {
/**
* The plugin name string in the C source code.
* @member {String}
* @instance
* @readonly
*/
name: null,
/**
* The session to which this plugin is attached. An instance of {@link Session}.
* @member {Session}
* @instance
* @readonly
*/
session: null,
/**
* The server-side "plugin handle"
* @member {String}
* @instance
* @readonly
*/
id: null,
/**
* Is this plugin attached on the server?
* @member {Boolean}
* @instance
* @readonly
*/
attached: false,
};
/**
* @lends BasePlugin.prototype
*/
const methods = {
/**
* Attach the server-side plugin (identified by {@link BasePlugin#name}) to the session. Meant to
* be called only by {@link Session}.
*
* The method {@link BasePlugin#onAttached} will be called.
*
* @public
* @param {Session} session - An instance of {@link Session}
* @emits BasePlugin#attached
* @returns {Promise} Rejected if synchronous reply contains `janus: 'error'` or response
* takes too long. Resolved otherwise.
*/
async attach(session) {
this.logger.debug('attach()');
this.session = session;
const msg = {
janus: 'attach',
plugin: this.name,
};
const response = await this.session.send(msg);
this.id = response.data.id;
this.attached = true;
this.onAttached();
/** @event BasePlugin#attached */
this.emit('attached');
return response;
},
/**
* @protected
* @abstract
*/
onAttached() {
this.logger.debug('onAttached() abstract method called');
},
/**
* @protected
* @abstract
*/
onDetached() {
this.logger.debug('onDetached() abstract method called');
},
/**
* Detach this plugin from the session. Meant to be called only from {@link Session}.
*
* The method {@link BasePlugin#onDetached} will be called.
*
* @public
* @returns {Promise} Response from janus-gateway.
* @emits BasePlugin#detached
*/
async detach() {
this.logger.debug('detach()');
await this.send({ janus: 'detach' });
this.attached = false;
this.onDetached();
/** @event BasePlugin#detached */
this.emit('detached');
},
/**
* @private
* @param {Object} obj - Should be JSON-serializable. Expected to have a key 'janus'
* with one of the following values: 'attach|detach|message|trickle|hangup'
*
* @returns {Promise} Rejected if synchronous reply contains `janus: 'error'` or response
* takes too long. Resolved otherwise.
*
* @see {@link https://janus.conf.meetecho.com/docs/rest.html}
*/
async send(obj) {
this.logger.debug('send()');
return this.session.send({ ...obj, handle_id: this.id });
},
/**
* Send a message to the server-side plugin.
*
* Janus will call the plugin C function `.handle_message` with the provided
* arguments.
*
* @public
* @param {Object} body - Should be JSON-serializable. Janus expects this. Will be
* provided to the `.handle_message` C function as `json_t *message`.
* @param {Object} [jsep] - Should be JSON-serializable. Will be provided to the
* `.handle_message` C function as `json_t *jsep`.
*
* @returns {Promise} Response from janus-gateway.
*/
async sendMessage(body = {}, jsep) {
const msg = {
janus: 'message',
body, // required. 3rd argument in the server-side .handle_message() function
};
if (jsep) msg.jsep = jsep; // 'jsep' is a recognized key by Janus. 4th arg in .handle_message().
this.logger.debug('sendMessage()');
return this.send(msg);
},
/**
* Alias for `sendMessage({}, jsep)`
*
* @public
* @param {Object} jsep - Should be JSON-serializable. Will be provided to the
* `.handle_message` C function as `json_t *jsep`.
*
* @returns {Promise} Response from janus-gateway.
*/
async sendJsep(jsep) {
this.logger.debug('sendJsep()');
return this.sendMessage({}, jsep);
},
/**
* Send trickle ICE candidates to the janus core, related to this plugin.
*
* @public
* @param {(Object|Array|null)} candidate Should be JSON-serializable.
*
* @returns {Promise} Response from janus-gateway.
*/
async sendTrickle(candidate) {
this.logger.debug('sendTrickle()');
return this.send({ janus: 'trickle', candidate });
},
/**
* Hangup the WebRTC peer connection, but keep the plugin attached.
*
* @public
* @returns {Promise} Response from janus-gateway.
*/
async hangup() {
this.logger.debug('hangup()');
return this.send({ janus: 'hangup' });
},
/**
* Receive an asynchronous ('pushed') message sent by the Janus core.
*
* The parent Session instance is responsible for dispatching messages here. You should have no
* need of calling this method directly.
*
* This method always contains plugin-specific logic and can be overridden.
*
* @protected
* @abstract
* @param {Object} msg - Object parsed from server-side JSON
*/
async receive(msg) {
this.logger.debug(`Abstract method 'receive' called with message ${msg}`);
},
};
/**
* @constructs BasePlugin
* @mixes EventEmitter
*
* @classdesc
*
* This is a mixin which implements the base behavior of a plugin: Attach/detach and
* send/receive messages to/from the server-side plugin.
*
* This is intentionally not implemented as a class or a function (constructor-prototype
* combination). Rather, properties, methods and an initializer/constructor are exported separately
* as plain objects for maximum flexibility regarding different JS ways of
* inheriting/subclassing/extending/mixing.
*
* See /demo/echotest-plugin.js for usage.
*
* @param {Object} [options={}]
* @param {Object} [options.logger] - The logger to use
* @param {Function} [options.logger.info=function(){}]
* @param {Function} [options.logger.warn=function(){}]
* @param {Function} [options.logger.debug=function(){}]
* @param {Function} [options.logger.error=function(){}]
* @return {BasePlugin}
*/
function init({
logger = {
info() {},
warn() {},
debug() {},
error() {},
},
} = {}) {
/**
* @member {Object}
* @property {Function} info - Called for log level 'info'
* @property {Function} warn - Called for log level 'warn'
* @property {Function} debug - Called for log level 'debug'
* @property {Function} error - Called for log level 'error'
*/
this.logger = logger;
}
Object.assign(methods, EventEmitter({ emit_prop: 'emit' }));
export default {
properties, methods, init,
};