implement terminate and hangup (currently unused)

This commit is contained in:
Bruno Windels 2022-03-29 12:00:54 +02:00
parent 11a9177592
commit ba45178e04
2 changed files with 37 additions and 19 deletions

View file

@ -166,7 +166,7 @@ export class PeerCall implements IDisposable {
} catch (err) { } catch (err) {
await log.wrap(`Failed to create answer`, log => { await log.wrap(`Failed to create answer`, log => {
log.catch(err); log.catch(err);
this.terminate(CallParty.Local, CallErrorCode.CreateAnswer, true, log); this.terminate(CallParty.Local, CallErrorCode.CreateAnswer, log);
}); });
return; return;
} }
@ -177,7 +177,7 @@ export class PeerCall implements IDisposable {
} catch (err) { } catch (err) {
await log.wrap(`Error setting local description!`, log => { await log.wrap(`Error setting local description!`, log => {
log.catch(err); log.catch(err);
this.terminate(CallParty.Local, CallErrorCode.SetLocalDescription, true, log); this.terminate(CallParty.Local, CallErrorCode.SetLocalDescription, log);
}); });
return; return;
} }
@ -212,6 +212,7 @@ export class PeerCall implements IDisposable {
}); });
} }
/** group calls would handle reject at the group call level, not at the peer call level */
async reject() { async reject() {
} }
@ -223,10 +224,11 @@ export class PeerCall implements IDisposable {
} }
private async _hangup(errorCode: CallErrorCode, log: ILogItem): Promise<void> { private async _hangup(errorCode: CallErrorCode, log: ILogItem): Promise<void> {
if (this._state !== CallState.Ended) { if (this._state === CallState.Ended) {
this._state = CallState.Ended; return;
await this.sendHangupWithCallId(this.callId, errorCode, log);
} }
this.terminate(CallParty.Local, errorCode, log);
await this.sendHangupWithCallId(this.callId, errorCode, log);
} }
handleIncomingSignallingMessage<B extends MCallBase>(message: SignallingMessage<B>, partyId: PartyId): Promise<void> { handleIncomingSignallingMessage<B extends MCallBase>(message: SignallingMessage<B>, partyId: PartyId): Promise<void> {
@ -257,6 +259,7 @@ export class PeerCall implements IDisposable {
call_id: callId, call_id: callId,
version: 1, version: 1,
}; };
// TODO: Don't send UserHangup reason to older clients
if (reason) { if (reason) {
content["reason"] = reason; content["reason"] = reason;
} }
@ -274,7 +277,7 @@ export class PeerCall implements IDisposable {
await this.peerConnection.setLocalDescription(); await this.peerConnection.setLocalDescription();
} catch (err) { } catch (err) {
log.log(`Error setting local description!`).catch(err); log.log(`Error setting local description!`).catch(err);
this.terminate(CallParty.Local, CallErrorCode.SetLocalDescription, true, log); this.terminate(CallParty.Local, CallErrorCode.SetLocalDescription, log);
return; return;
} }
@ -385,7 +388,7 @@ export class PeerCall implements IDisposable {
} catch (e) { } catch (e) {
await log.wrap(`Call failed to set remote description`, async log => { await log.wrap(`Call failed to set remote description`, async log => {
log.catch(e); log.catch(e);
return this.terminate(CallParty.Local, CallErrorCode.SetRemoteDescription, false, log); return this.terminate(CallParty.Local, CallErrorCode.SetRemoteDescription, log);
}); });
return; return;
} }
@ -395,7 +398,7 @@ export class PeerCall implements IDisposable {
// (81 at time of writing), this is no longer a problem, so let's do it the correct way. // (81 at time of writing), this is no longer a problem, so let's do it the correct way.
if (this.peerConnection.remoteTracks.length === 0) { if (this.peerConnection.remoteTracks.length === 0) {
await log.wrap(`Call no remote stream or no tracks after setting remote description!`, async log => { await log.wrap(`Call no remote stream or no tracks after setting remote description!`, async log => {
return this.terminate(CallParty.Local, CallErrorCode.SetRemoteDescription, false, log); return this.terminate(CallParty.Local, CallErrorCode.SetRemoteDescription, log);
}); });
return; return;
} }
@ -444,7 +447,7 @@ export class PeerCall implements IDisposable {
} catch (e) { } catch (e) {
await log.wrap(`Failed to set remote description`, log => { await log.wrap(`Failed to set remote description`, log => {
log.catch(e); log.catch(e);
this.terminate(CallParty.Local, CallErrorCode.SetRemoteDescription, false, log); this.terminate(CallParty.Local, CallErrorCode.SetRemoteDescription, log);
}); });
return; return;
} }
@ -591,7 +594,7 @@ export class PeerCall implements IDisposable {
try { try {
await this.sendSignallingMessage({type: EventType.Answer, content: answerContent}, log); await this.sendSignallingMessage({type: EventType.Answer, content: answerContent}, log);
} catch (error) { } catch (error) {
this.terminate(CallParty.Local, CallErrorCode.SendAnswer, false, log); this.terminate(CallParty.Local, CallErrorCode.SendAnswer, log);
throw error; throw error;
} }
@ -656,7 +659,7 @@ export class PeerCall implements IDisposable {
// put all the candidates we failed to send back in the queue // put all the candidates we failed to send back in the queue
// TODO: terminate doesn't seem to vibe with the comment above? // TODO: terminate doesn't seem to vibe with the comment above?
this.terminate(CallParty.Local, CallErrorCode.SignallingFailed, false, log); this.terminate(CallParty.Local, CallErrorCode.SignallingFailed, log);
} }
}); });
} }
@ -761,8 +764,19 @@ export class PeerCall implements IDisposable {
})); }));
} }
private async terminate(hangupParty: CallParty, hangupReason: CallErrorCode, shouldEmit: boolean, log: ILogItem): Promise<void> { private terminate(hangupParty: CallParty, hangupReason: CallErrorCode, log: ILogItem): void {
if (this._state === CallState.Ended) {
return;
}
this.hangupParty = hangupParty;
// this.hangupReason = hangupReason;
this.setState(CallState.Ended, log);
this.stopAllMedia();
if (this.peerConnection && this.peerConnection.signalingState !== 'closed') {
this.peerConnection.close();
}
} }
private stopAllMedia(): void { private stopAllMedia(): void {
@ -791,8 +805,11 @@ export class PeerCall implements IDisposable {
this.peerConnection.dispose(); this.peerConnection.dispose();
} }
public close(): void { public close(reason: CallErrorCode | undefined, log: ILogItem): void {
this.peerConnection.close(); if (reason === undefined) {
reason = CallErrorCode.UserHangup;
}
this.terminate(CallParty.Local, reason, log);
} }
} }

View file

@ -71,11 +71,12 @@ export class Member {
/** @internal */ /** @internal */
disconnect() { disconnect() {
this.peerCall?.close(); this.logItem.wrap("disconnect", log => {
this.peerCall?.close(undefined, log);
this.peerCall?.dispose(); this.peerCall?.dispose();
this.peerCall = undefined; this.peerCall = undefined;
this.localMedia = undefined; this.localMedia = undefined;
this.logItem.log("disconnect"); });
} }
/** @internal */ /** @internal */