1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-22 16:45:58 +01:00

Keep track of stream direction.

This simplifies the interface since close does the right thing
without help from the application.  Also improves error checking.
This commit is contained in:
Juliusz Chroboczek 2020-12-05 20:27:32 +01:00
parent 1c5bce2a38
commit f002a83806
2 changed files with 26 additions and 13 deletions

View file

@ -253,7 +253,7 @@ ServerConnection.prototype.connect = async function(url) {
for(let id in sc.down) { for(let id in sc.down) {
let c = sc.down[id]; let c = sc.down[id];
delete(sc.down[id]); delete(sc.down[id]);
c.close(false); c.close();
if(c.onclose) if(c.onclose)
c.onclose.call(c); c.onclose.call(c);
} }
@ -423,9 +423,9 @@ ServerConnection.prototype.newUpStream = function(id) {
if(!pc) if(!pc)
throw new Error("Couldn't create peer connection"); throw new Error("Couldn't create peer connection");
if(sc.up[id]) { if(sc.up[id]) {
sc.up[id].close(false); sc.up[id].close();
} }
let c = new Stream(this, id, pc); let c = new Stream(this, id, pc, true);
sc.up[id] = c; sc.up[id] = c;
pc.onnegotiationneeded = async e => { pc.onnegotiationneeded = async e => {
@ -545,15 +545,18 @@ ServerConnection.prototype.gotOffer = async function(id, labels, offer, renegoti
// Unless the server indicates that this is a renegotiation with // Unless the server indicates that this is a renegotiation with
// all parameters unchanged, tear down the existing connection. // all parameters unchanged, tear down the existing connection.
delete(sc.down[id]); delete(sc.down[id]);
c.close(false); c.close();
c = null; c = null;
} }
if(sc.up[id])
throw new Error('Duplicate connection id');
if(!c) { if(!c) {
let pc = new RTCPeerConnection({ let pc = new RTCPeerConnection({
iceServers: this.iceServers, iceServers: this.iceServers,
}); });
c = new Stream(this, id, pc); c = new Stream(this, id, pc, false);
sc.down[id] = c; sc.down[id] = c;
c.pc.onicecandidate = function(e) { c.pc.onicecandidate = function(e) {
@ -681,7 +684,7 @@ ServerConnection.prototype.gotClose = function(id) {
if(!c) if(!c)
throw new Error('unknown down stream'); throw new Error('unknown down stream');
delete(this.down[id]); delete(this.down[id]);
c.close(false); c.close();
if(c.onclose) if(c.onclose)
c.onclose.call(c); c.onclose.call(c);
}; };
@ -730,7 +733,7 @@ ServerConnection.prototype.gotRemoteIce = async function(id, candidate) {
* *
* @constructor * @constructor
*/ */
function Stream(sc, id, pc) { function Stream(sc, id, pc, up) {
/** /**
* The associated ServerConnection. * The associated ServerConnection.
* *
@ -745,6 +748,13 @@ function Stream(sc, id, pc) {
* @const * @const
*/ */
this.id = id; this.id = id;
/**
* Indicates whether the stream is in the client->server direction.
*
* @type {boolean}
* @const
*/
this.up = up
/** /**
* For up streams, one of "local" or "screenshare". * For up streams, one of "local" or "screenshare".
* *
@ -884,10 +894,9 @@ function Stream(sc, id, pc) {
} }
/** /**
* close closes an up stream. It should not be called for down streams. * close closes a stream.
* @param {boolean} sendclose - whether to send a close message to the server
*/ */
Stream.prototype.close = function(sendclose) { Stream.prototype.close = function() {
let c = this; let c = this;
if(c.statsHandler) { if(c.statsHandler) {
clearInterval(c.statsHandler); clearInterval(c.statsHandler);
@ -904,7 +913,7 @@ Stream.prototype.close = function(sendclose) {
} }
c.pc.close(); c.pc.close();
if(sendclose) { if(c.up && c.localDescriptionSent) {
try { try {
c.sc.send({ c.sc.send({
type: 'close', type: 'close',
@ -983,6 +992,8 @@ Stream.prototype.flushRemoteIceCandidates = async function () {
*/ */
Stream.prototype.negotiate = async function (restartIce) { Stream.prototype.negotiate = async function (restartIce) {
let c = this; let c = this;
if(!c.up)
throw new Error('not an up stream');
let options = {}; let options = {};
if(restartIce) if(restartIce)
@ -1022,6 +1033,8 @@ Stream.prototype.negotiate = async function (restartIce) {
Stream.prototype.restartIce = function () { Stream.prototype.restartIce = function () {
let c = this; let c = this;
if(!c.up)
throw new Error('not an up stream');
if('restartIce' in c.pc) { if('restartIce' in c.pc) {
try { try {

View file

@ -963,7 +963,7 @@ function delUpMedia(c) {
} catch(e) { } catch(e) {
console.warn(e); console.warn(e);
} }
c.close(true); c.close();
delete(serverConnection.up[c.id]); delete(serverConnection.up[c.id]);
setButtonsVisibility() setButtonsVisibility()
} }
@ -978,7 +978,7 @@ function delUpMediaKind(kind) {
let c = serverConnection.up[id]; let c = serverConnection.up[id];
if(kind && c.kind != kind) if(kind && c.kind != kind)
continue continue
c.close(true); c.close();
delMedia(id); delMedia(id);
delete(serverConnection.up[id]); delete(serverConnection.up[id]);
} }