janus-plugin-sfu/client/tiny.js

148 строки
5.2 KiB
JavaScript
Исходник Обычный вид История

2017-10-24 01:18:40 +03:00
var USER_ID = Math.floor(Math.random() * (1000000001));
2017-10-21 03:44:15 +03:00
var ROOM_ID = 42;
2017-10-07 03:10:12 +03:00
Minijanus.verbose = true;
2017-10-27 23:43:53 +03:00
2017-10-21 03:44:15 +03:00
const PEER_CONNECTION_CONFIG = {
iceServers: [
{ urls: "stun:stun.l.google.com:19302" },
{ urls: "stun:global.stun.twilio.com:3478?transport=udp" }
]
};
2017-10-07 03:10:12 +03:00
// global helper for interactive use
var c = {
session: null,
publisher: null,
subscribers: {}
};
2017-10-21 03:44:15 +03:00
function init() {
var ws = new WebSocket("ws://localhost:8188", "janus-protocol");
ws.addEventListener("open", () => {
var session = c.session = new Minijanus.JanusSession(ws.send.bind(ws));
2017-10-21 03:44:15 +03:00
ws.addEventListener("message", ev => handleMessage(session, ev));
session.create().then(() => attachPublisher(session)).then(x => {
c.publisher = x;
});
2017-10-21 03:44:15 +03:00
});
}
2017-10-07 03:10:12 +03:00
2017-10-21 03:44:15 +03:00
function handleMessage(session, ev) {
var data = JSON.parse(ev.data);
session.receive(data);
if (data.janus === "event") {
if (data.plugindata && data.plugindata.data) {
var contents = data.plugindata.data;
switch (contents.event) {
case "join":
addUser(session, contents.user_id);
2017-10-21 03:44:15 +03:00
break;
case "leave":
removeUser(session, contents.user_id);
2017-10-21 03:44:15 +03:00
break;
case undefined:
// a non-plugin event
break;
default:
console.error("Unknown event received: ", data.plugindata.data);
break;
}
}
2017-10-07 03:10:12 +03:00
}
}
2017-10-21 11:01:42 +03:00
function negotiateIce(conn, handle) {
return new Promise((resolve, reject) => {
conn.addEventListener("icecandidate", ev => {
handle.sendTrickle(ev.candidate || null).then(() => {
if (!ev.candidate) { // this was the last candidate on our end and now they received it
resolve();
}
});
});
});
};
function addUser(session, userId) {
console.info("Adding user " + userId + ".");
attachSubscriber(session, userId).then(x => {
c.subscribers[userId] = x;
});
}
function removeUser(session, userId) {
console.info("Removing user " + userId + ".");
var subscriber = c.subscribers[userId];
if (subscriber != null) {
subscriber.handle.detach();
subscriber.conn.close();
delete c.subscribers[userId];
}
}
2017-10-21 03:44:15 +03:00
function attachPublisher(session) {
console.info("Attaching publisher for session: ", session);
var conn = new RTCPeerConnection(PEER_CONNECTION_CONFIG);
2017-10-21 11:01:42 +03:00
var handle = new Minijanus.JanusPluginHandle(session);
return handle.attach("janus.plugin.sfu").then(() => {
2017-10-21 11:01:42 +03:00
var iceReady = negotiateIce(conn, handle);
var channel = conn.createDataChannel("reliable", { ordered: true });
2017-10-24 01:18:40 +03:00
channel.addEventListener("message", ev => console.info("Message received on channel: ", ev));
2017-10-21 03:44:15 +03:00
var mediaReady = navigator.mediaDevices.getUserMedia({ audio: true });
var offerReady = mediaReady
.then(media => {
conn.addStream(media);
return conn.createOffer({ audio: true });
}, () => conn.createOffer());
var localReady = offerReady.then(conn.setLocalDescription.bind(conn));
var remoteReady = offerReady
.then(handle.sendJsep.bind(handle))
.then(answer => conn.setRemoteDescription(answer.jsep));
var connectionReady = Promise.all([iceReady, localReady, remoteReady]);
return connectionReady
2017-10-24 01:18:40 +03:00
.then(() => handle.sendMessage({ kind: "join", room_id: ROOM_ID, user_id: USER_ID, notify: true }))
.then(reply => {
var response = reply.plugindata.data.response;
response.user_ids.forEach(otherId => {
addUser(session, otherId);
});
return { handle: handle, conn: conn, channel: channel };
});
2017-09-28 13:13:09 +03:00
});
}
2017-10-07 03:10:12 +03:00
2017-10-21 03:44:15 +03:00
function attachSubscriber(session, otherId) {
console.info("Attaching subscriber to " + otherId + " for session: ", session);
var conn = new RTCPeerConnection(PEER_CONNECTION_CONFIG);
conn.addEventListener("track", function(ev) {
var receiverEl = document.createElement("audio");
document.body.appendChild(receiverEl);
receiverEl.srcObject = ev.streams[0];
receiverEl.play();
});
2017-10-23 01:01:11 +03:00
var handle = new Minijanus.JanusPluginHandle(session);
return handle.attach("janus.plugin.sfu")
.then(() => {
var iceReady = negotiateIce(conn, handle);
var offerReady = conn.createOffer({ offerToReceiveAudio: true });
var localReady = offerReady.then(conn.setLocalDescription.bind(conn));
var remoteReady = offerReady
.then(handle.sendJsep.bind(handle))
.then(answer => conn.setRemoteDescription(answer.jsep));
var connectionReady = Promise.all([iceReady, localReady, remoteReady]);
return connectionReady.then(() => {
return handle.sendMessage({ kind: "join", room_id: ROOM_ID, user_id: USER_ID, subscription_specs: [
{ content_kind: "all", publisher_id: otherId }
]});
2017-10-21 03:44:15 +03:00
}).then(reply => {
return { handle: handle, conn: conn };
});
});
2017-10-07 03:10:12 +03:00
}
2017-10-21 03:44:15 +03:00
init();