Skip to content

Commit

Permalink
Emit destroy for inbound calls when the caller cancel the dial (#865)
Browse files Browse the repository at this point in the history
* fix service worker build on CI

* _rejectStartMethod can be undefined

* welcome _buildPeer

* debug

* udpate cf playground

* update log

* remove usage of emitter

* changeset

* cleanup

* remove redundant runRTCPeerWorkers call
  • Loading branch information
Edoardo Gallo committed Aug 30, 2023
1 parent be17e61 commit 6c435be
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 17 deletions.
7 changes: 7 additions & 0 deletions .changeset/yellow-pugs-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@sw-internal/playground-js': patch
'@signalwire/webrtc': patch
'@signalwire/js': patch
---

Expose `destroy` event for inbound calls in Call Fabric to handle cases where the caller cancel the dial before the callee answers.
3 changes: 3 additions & 0 deletions internal/playground-js/src/fabric-callee/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ window.tapPushNotification = async () => {
switch (resultType) {
case 'inboundCall':
window.__call = resultObject
window.__call.on('destroy', () => {
console.warn('Inbound Call got cancelled!!')
})
enableCallButtons()
connectStatus.innerHTML = 'Ringing...'
break
Expand Down
6 changes: 3 additions & 3 deletions internal/playground-js/src/fabric/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,13 @@ window.connect = async () => {
nodeId: steeringId,
})

window.__call = call
roomObj = call

await call.start()

console.debug('Call Obj', call)

window.__call = call
roomObj = call

const joinHandler = (params) => {
console.debug('>> room.joined', params)

Expand Down
8 changes: 8 additions & 0 deletions internal/playground-js/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ export default defineConfig({
videoManager: path.resolve(__dirname, 'src/videoManager/index.html'),
fabricHttp: path.resolve(__dirname, 'src/fabric-http/index.html'),
fabricCallee: path.resolve(__dirname, 'src/fabric-callee/index.html'),
sw: path.resolve(__dirname, 'src/fabric-callee/sw.js'),
},
output: {
entryFileNames: (assetInfo) => {
return ['sw'].includes(assetInfo.name)
? 'src/fabric-callee/[name].js' // put service worker in the correct folder
: 'assets/[name]-[hash].js' // others in `assets/` as default
},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/js/src/fabric/WSClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class WSClient {
return new Promise(async (resolve, reject) => {
try {
// @ts-expect-error
call.emitter.once('verto.display', () => resolve(call))
call.once('verto.display', () => resolve(call))
call.once('room.subscribed', () => resolve(call))

await call.join()
Expand Down
29 changes: 17 additions & 12 deletions packages/webrtc/src/BaseConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,7 @@ export class BaseConnection<EventTypes extends EventEmitter.ValidEventTypes>
this.logger.debug('_triggerNewRTCPeer Start')
try {
this.logger.debug('Build a new RTCPeer')
const rtcPeer = new RTCPeer(this, 'offer')
this.appendRTCPeer(rtcPeer)
this.logger.debug('Run workers for the new RTCPeer', rtcPeer.uuid)
this.runRTCPeerWorkers(rtcPeer.uuid)
const rtcPeer = this._buildPeer('offer')
this.logger.debug('Trigger start for the new RTCPeer!')
await rtcPeer.start()
} catch (error) {
Expand Down Expand Up @@ -655,10 +652,8 @@ export class BaseConnection<EventTypes extends EventEmitter.ValidEventTypes>
invite<T>(): Promise<T> {
return new Promise(async (resolve, reject) => {
this.direction = 'outbound'
this.peer = new RTCPeer(this, 'offer')
this.peer = this._buildPeer('offer')
try {
this.runRTCPeerWorkers(this.peer.uuid)

await this.peer.start()
resolve(this as any as T)
} catch (error) {
Expand All @@ -673,11 +668,9 @@ export class BaseConnection<EventTypes extends EventEmitter.ValidEventTypes>
return new Promise(async (resolve, reject) => {
this.direction = 'inbound'
if (!this.peer) {
this.peer = new RTCPeer(this, 'answer')
this.peer = this._buildPeer('answer')
}
try {
this.runRTCPeerWorkers(this.peer.uuid)

await this.peer.start()
resolve(this as any as T)
} catch (error) {
Expand Down Expand Up @@ -1004,6 +997,7 @@ export class BaseConnection<EventTypes extends EventEmitter.ValidEventTypes>

/** @internal */
public onVertoBye = (params: OnVertoByeParams) => {
this.logger.debug('onVertoBye', params)
const {
rtcPeerId,
byeCause = 'NORMAL_CLEARING',
Expand Down Expand Up @@ -1033,6 +1027,7 @@ export class BaseConnection<EventTypes extends EventEmitter.ValidEventTypes>

// Set state to hangup only if the rtcPeer is the current one
if (this.activeRTCPeerId === rtcPeer?.uuid) {
this.logger.debug('onVertoBye go hangup')
this.setState('hangup')
}
}
Expand Down Expand Up @@ -1064,8 +1059,18 @@ export class BaseConnection<EventTypes extends EventEmitter.ValidEventTypes>
}

private _initPeer() {
const rtcType: RTCSdpType = this.options.remoteSdp ? 'answer' : 'offer'
this.peer = new RTCPeer(this, rtcType)
// Build only for answer to be able to reject
if (this.options.remoteSdp) {
this.peer = this._buildPeer('answer')
}
}

private _buildPeer(type: RTCSdpType) {
const rtcPeer = new RTCPeer(this, type)
this.appendRTCPeer(rtcPeer)
this.runRTCPeerWorkers(rtcPeer.uuid)

return rtcPeer
}

/** @internal */
Expand Down
2 changes: 1 addition & 1 deletion packages/webrtc/src/RTCPeer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ export default class RTCPeer<EventTypes extends EventEmitter.ValidEventTypes> {

onRemoteBye({ code, message }: { code: string; message: string }) {
// It could be a negotiation/signaling error so reject the "startMethod"
this._rejectStartMethod({
this._rejectStartMethod?.({
code,
message,
})
Expand Down

0 comments on commit 6c435be

Please sign in to comment.