diff --git a/package.json b/package.json index e708160..39e8774 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/handshake.ts b/src/handshake.ts index 0c5152e..74cc305 100644 --- a/src/handshake.ts +++ b/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 { diff --git a/src/index.ts b/src/index.ts index 74cc551..3014c89 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,3 @@ -import type { Signal } from '@libp2p/webrtc-star-protocol' - export interface WRTC { RTCPeerConnection: typeof RTCPeerConnection RTCSessionDescription: typeof RTCSessionDescription @@ -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 diff --git a/src/initiator.ts b/src/initiator.ts index 5bbe3e4..06d32cc 100644 --- a/src/initiator.ts +++ b/src/initiator.ts @@ -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 @@ -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')) }) @@ -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' } diff --git a/src/receiver.ts b/src/receiver.ts index e4a3b06..22d1695 100644 --- a/src/receiver.ts +++ b/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 @@ -53,6 +55,8 @@ class WebRTCReceiverHandshake extends WebRTCHandshake { } async handleRenegotiate () { + log.trace('renegotiate') + this.dispatchEvent(new CustomEvent('signal', { detail: { type: 'renegotiate' @@ -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 }))