-
Notifications
You must be signed in to change notification settings - Fork 13
/
bundle.js
257 lines (226 loc) · 9.27 KB
/
bundle.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
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Minijanus = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
/**
* Represents a handle to a single Janus plugin on a Janus session. Each WebRTC connection to the Janus server will be
* associated with a single handle. Once attached to the server, this handle will be given a unique ID which should be
* used to associate it with future signalling messages.
*
* See https://janus.conf.meetecho.com/docs/rest.html#handles.
**/
function JanusPluginHandle(session) {
this.session = session;
this.id = undefined;
}
/** Attaches this handle to the Janus server and sets its ID. **/
JanusPluginHandle.prototype.attach = function(plugin) {
var payload = { plugin: plugin, "force-bundle": true, "force-rtcp-mux": true };
return this.session.send("attach", payload).then(resp => {
this.id = resp.data.id;
return resp;
});
};
/** Detaches this handle. **/
JanusPluginHandle.prototype.detach = function() {
return this.send("detach");
};
/** Registers a callback to be fired upon the reception of any incoming Janus signals for this plugin handle with the
* `janus` attribute equal to `ev`.
**/
JanusPluginHandle.prototype.on = function(ev, callback) {
return this.session.on(ev, signal => {
if (signal.sender == this.id) {
callback(signal);
}
});
};
/**
* Sends a signal associated with this handle. Signals should be JSON-serializable objects. Returns a promise that will
* be resolved or rejected when a response to this signal is received, or when no response is received within the
* session timeout.
**/
JanusPluginHandle.prototype.send = function(type, signal) {
return this.session.send(type, Object.assign({ handle_id: this.id }, signal));
};
/** Sends a plugin-specific message associated with this handle. **/
JanusPluginHandle.prototype.sendMessage = function(body) {
return this.send("message", { body: body });
};
/** Sends a JSEP offer or answer associated with this handle. **/
JanusPluginHandle.prototype.sendJsep = function(jsep) {
return this.send("message", { body: {}, jsep: jsep });
};
/** Sends an ICE trickle candidate associated with this handle. **/
JanusPluginHandle.prototype.sendTrickle = function(candidate) {
return this.send("trickle", { candidate: candidate });
};
/**
* Represents a Janus session -- a Janus context from within which you can open multiple handles and connections. Once
* created, this session will be given a unique ID which should be used to associate it with future signalling messages.
*
* See https://janus.conf.meetecho.com/docs/rest.html#sessions.
**/
function JanusSession(output, options) {
this.output = output;
this.id = undefined;
this.nextTxId = 0;
this.txns = {};
this.eventHandlers = {};
this.options = Object.assign({
verbose: false,
timeoutMs: 10000,
keepaliveMs: 30000
}, options);
}
/** Creates this session on the Janus server and sets its ID. **/
JanusSession.prototype.create = function() {
return this.send("create").then(resp => {
this.id = resp.data.id;
return resp;
});
};
/**
* Destroys this session. Note that upon destruction, Janus will also close the signalling transport (if applicable) and
* any open WebRTC connections.
**/
JanusSession.prototype.destroy = function() {
return this.send("destroy").then((resp) => {
this.dispose();
return resp;
});
};
/**
* Disposes of this session in a way such that no further incoming signalling messages will be processed.
* Outstanding transactions will be rejected.
**/
JanusSession.prototype.dispose = function() {
this._killKeepalive();
this.eventHandlers = {};
for (var txId in this.txns) {
if (this.txns.hasOwnProperty(txId)) {
var txn = this.txns[txId];
clearTimeout(txn.timeout);
txn.reject(new Error("Janus session was disposed."));
delete this.txns[txId];
}
}
};
/**
* Whether this signal represents an error, and the associated promise (if any) should be rejected.
* Users should override this to handle any custom plugin-specific error conventions.
**/
JanusSession.prototype.isError = function(signal) {
return signal.janus === "error";
};
/** Registers a callback to be fired upon the reception of any incoming Janus signals for this session with the
* `janus` attribute equal to `ev`.
**/
JanusSession.prototype.on = function(ev, callback) {
var handlers = this.eventHandlers[ev];
if (handlers == null) {
handlers = this.eventHandlers[ev] = [];
}
handlers.push(callback);
};
/**
* Callback for receiving JSON signalling messages pertinent to this session. If the signals are responses to previously
* sent signals, the promises for the outgoing signals will be resolved or rejected appropriately with this signal as an
* argument.
*
* External callers should call this function every time a new signal arrives on the transport; for example, in a
* WebSocket's `message` event, or when a new datum shows up in an HTTP long-polling response.
**/
JanusSession.prototype.receive = function(signal) {
if (this.options.verbose) {
this._logIncoming(signal);
}
if (signal.session_id != this.id) {
console.warn("Incorrect session ID received in Janus signalling message: was " + signal.session_id + ", expected " + this.id + ".");
}
var responseType = signal.janus;
var handlers = this.eventHandlers[responseType];
if (handlers != null) {
for (var i = 0; i < handlers.length; i++) {
handlers[i](signal);
}
}
if (signal.transaction != null) {
var txn = this.txns[signal.transaction];
if (txn == null) {
// this is a response to a transaction that wasn't caused via JanusSession.send, or a plugin replied twice to a
// single request, or the session was disposed, or something else that isn't under our purview; that's fine
return;
}
if (responseType === "ack" && txn.type == "message") {
// this is an ack of an asynchronously-processed plugin request, we should wait to resolve the promise until the
// actual response comes in
return;
}
clearTimeout(txn.timeout);
delete this.txns[signal.transaction];
(this.isError(signal) ? txn.reject : txn.resolve)(signal);
}
};
/**
* Sends a signal associated with this session, beginning a new transaction. Returns a promise that will be resolved or
* rejected when a response is received in the same transaction, or when no response is received within the session
* timeout.
**/
JanusSession.prototype.send = function(type, signal) {
signal = Object.assign({ transaction: (this.nextTxId++).toString() }, signal);
return new Promise((resolve, reject) => {
var timeout = null;
if (this.options.timeoutMs) {
timeout = setTimeout(() => {
delete this.txns[signal.transaction];
reject(new Error("Signalling transaction with txid " + signal.transaction + " timed out."));
}, this.options.timeoutMs);
}
this.txns[signal.transaction] = { resolve: resolve, reject: reject, timeout: timeout, type: type };
this._transmit(type, signal);
});
};
JanusSession.prototype._transmit = function(type, signal) {
signal = Object.assign({ janus: type }, signal);
if (this.id != null) { // this.id is undefined in the special case when we're sending the session create message
signal = Object.assign({ session_id: this.id }, signal);
}
if (this.options.verbose) {
this._logOutgoing(signal);
}
this.output(JSON.stringify(signal));
this._resetKeepalive();
};
JanusSession.prototype._logOutgoing = function(signal) {
var kind = signal.janus;
if (kind === "message" && signal.jsep) {
kind = signal.jsep.type;
}
var message = "> Outgoing Janus " + (kind || "signal") + " (#" + signal.transaction + "): ";
console.debug("%c" + message, "color: #040", signal);
};
JanusSession.prototype._logIncoming = function(signal) {
var kind = signal.janus;
var message = signal.transaction ?
"< Incoming Janus " + (kind || "signal") + " (#" + signal.transaction + "): " :
"< Incoming Janus " + (kind || "signal") + ": ";
console.debug("%c" + message, "color: #004", signal);
};
JanusSession.prototype._sendKeepalive = function() {
return this.send("keepalive");
};
JanusSession.prototype._killKeepalive = function() {
clearTimeout(this.keepaliveTimeout);
};
JanusSession.prototype._resetKeepalive = function() {
this._killKeepalive();
if (this.options.keepaliveMs) {
this.keepaliveTimeout = setTimeout(() => {
this._sendKeepalive().catch(e => console.error("Error received from keepalive: ", e));
}, this.options.keepaliveMs);
}
};
module.exports = {
JanusPluginHandle,
JanusSession
};
},{}]},{},[1])(1)
});