Skip to content
This repository has been archived by the owner on Nov 23, 2022. It is now read-only.

Commit

Permalink
fix: add protocol messages and trace logging
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Mar 19, 2022
1 parent fb5e423 commit 95847d1
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 17 deletions.
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -125,7 +125,6 @@
"dependencies": {
"@libp2p/interfaces": "^1.3.15",
"@libp2p/logger": "^1.1.2",
"@libp2p/webrtc-star-protocol": "^1.0.1",
"delay": "^5.0.0",
"err-code": "^3.0.1",
"iso-random-stream": "^2.0.2",
Expand Down
3 changes: 1 addition & 2 deletions src/handshake.ts
@@ -1,7 +1,6 @@
import { EventEmitter } from '@libp2p/interfaces'
import errCode from 'err-code'
import type { WebRTCPeerEvents, WRTC } from './index.js'
import type { Signal, OfferSignal, AnswerSignal, CandidateSignal, RenegotiateSignal, GoodbyeSignal } from '@libp2p/webrtc-star-protocol'
import type { WebRTCPeerEvents, WRTC, Signal, OfferSignal, AnswerSignal, CandidateSignal, RenegotiateSignal, GoodbyeSignal } from './index.js'
import type { Logger } from '@libp2p/logger'

export interface WebRTCHandshakeOptions {
Expand Down
31 changes: 29 additions & 2 deletions src/index.ts
@@ -1,5 +1,3 @@
import type { Signal } from '@libp2p/webrtc-star-protocol'

export interface WRTC {
RTCPeerConnection: typeof RTCPeerConnection
RTCSessionDescription: typeof RTCSessionDescription
Expand Down Expand Up @@ -33,3 +31,32 @@ export interface WebRTCInitiatorInit extends WebRTCPeerInit {
dataChannelInit?: RTCDataChannelInit
offerOptions?: RTCOfferOptions
}

export interface OfferSignal {
type: 'offer'
sdp: string
}

export interface AnswerSignal {
type: 'answer'
sdp: string
}

export interface CandidateSignal {
type: 'candidate'
candidate: {
candidate: string
sdpMLineIndex?: number
sdpMid?: string
}
}

export interface RenegotiateSignal {
type: 'renegotiate'
}

export interface GoodbyeSignal {
type: 'goodbye'
}

export type Signal = OfferSignal | AnswerSignal | CandidateSignal | RenegotiateSignal | GoodbyeSignal
30 changes: 20 additions & 10 deletions src/initiator.ts
Expand Up @@ -5,9 +5,11 @@ import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { pEvent } from 'p-event'
import delay from 'delay'
import { CustomEvent } from '@libp2p/interfaces'
import { logger } from '@libp2p/logger'
import type { WebRTCHandshakeOptions } from './handshake.js'
import type { AnswerSignal, Signal } from '@libp2p/webrtc-star-protocol'
import type { WebRTCInitiatorInit } from './index.js'
import type { WebRTCInitiatorInit, AnswerSignal, Signal } from './index.js'

const log = logger('libp2p:webrtc-peer:initator')

const ICECOMPLETE_TIMEOUT = 1000

Expand Down Expand Up @@ -63,15 +65,19 @@ class WebRTCInitiatorHandshake extends WebRTCHandshake {
return
}

this.dispatchEvent(new CustomEvent('signal', {
detail: {
type: 'candidate',
candidate: {
candidate: event.candidate.candidate,
sdpMLineIndex: event.candidate.sdpMLineIndex,
sdpMid: event.candidate.sdpMid
}
const signal = {
type: 'candidate',
candidate: {
candidate: event.candidate.candidate,
sdpMLineIndex: event.candidate.sdpMLineIndex,
sdpMid: event.candidate.sdpMid
}
}

log.trace('create candidate', signal)

this.dispatchEvent(new CustomEvent('signal', {
detail: signal
}))
this.dispatchEvent(new CustomEvent('ice-candidate'))
})
Expand All @@ -93,12 +99,16 @@ class WebRTCInitiatorHandshake extends WebRTCHandshake {
await pEvent(this, 'ice-candidate')
await delay(ICECOMPLETE_TIMEOUT)

log.trace('renegotiate', this.peerConnection.localDescription)

this.dispatchEvent(new CustomEvent('signal', {
detail: this.peerConnection.localDescription
}))
}

async handleAnswer (signal: AnswerSignal) {
log.trace('handle answer', signal)

await this.peerConnection.setRemoteDescription(new this.wrtc.RTCSessionDescription(signal))
this.status = 'idle'
}
Expand Down
10 changes: 8 additions & 2 deletions src/receiver.ts
@@ -1,9 +1,11 @@
import { WebRTCPeer } from './peer.js'
import { WebRTCHandshake } from './handshake.js'
import { CustomEvent } from '@libp2p/interfaces'
import { logger } from '@libp2p/logger'
import type { WebRTCHandshakeOptions } from './handshake.js'
import type { OfferSignal, Signal, CandidateSignal } from '@libp2p/webrtc-star-protocol'
import type { WebRTCReceiverInit } from './index.js'
import type { WebRTCReceiverInit, OfferSignal, Signal, CandidateSignal } from './index.js'

const log = logger('libp2p:webrtc-peer:receiver')

export class WebRTCReceiver extends WebRTCPeer {
private readonly handshake: WebRTCReceiverHandshake
Expand Down Expand Up @@ -53,6 +55,8 @@ class WebRTCReceiverHandshake extends WebRTCHandshake {
}

async handleRenegotiate () {
log.trace('renegotiate')

this.dispatchEvent(new CustomEvent<Signal>('signal', {
detail: {
type: 'renegotiate'
Expand All @@ -73,6 +77,8 @@ class WebRTCReceiverHandshake extends WebRTCHandshake {

await this.peerConnection.setLocalDescription(answer)

log.trace('handle offer', this.peerConnection.localDescription)

this.dispatchEvent(new CustomEvent('signal', {
detail: this.peerConnection.localDescription
}))
Expand Down

0 comments on commit 95847d1

Please sign in to comment.