Skip to content

Commit

Permalink
refactor dtlsTransport
Browse files Browse the repository at this point in the history
  • Loading branch information
shinyoshiaki committed Dec 22, 2024
1 parent dd5413a commit 164d9e8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 33 deletions.
52 changes: 28 additions & 24 deletions packages/webrtc/src/peerConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class RTCPeerConnection extends EventTarget {
onconnectionstatechange?: Callback;

private readonly router = new RtpRouter();
private readonly certificates: RTCCertificate[] = [];
private certificate?: RTCCertificate;
sctpRemotePort?: number;
private seenMid = new Set<string>();
private currentLocalDescription?: SessionDescription;
Expand Down Expand Up @@ -150,6 +150,21 @@ export class RTCPeerConnection extends EventTarget {
constructor(config: Partial<PeerConfig> = {}) {
super();

this.setConfiguration(config);

this.iceConnectionStateChange.subscribe((state) => {
switch (state) {
case "disconnected":
this.setConnectionState("disconnected");
break;
case "closed":
this.close();
break;
}
});
}

private setConfiguration(config: Partial<PeerConfig>) {
deepMerge(this.config, config);

if (this.config.icePortRange) {
Expand Down Expand Up @@ -194,23 +209,15 @@ export class RTCPeerConnection extends EventTarget {

if (this.config.dtls) {
const { keys } = this.config.dtls;

if (keys) {
this.certificates.push(
new RTCCertificate(keys.keyPem, keys.certPem, keys.signatureHash),
this.certificate = new RTCCertificate(
keys.keyPem,
keys.certPem,
keys.signatureHash,
);
}
}

this.iceConnectionStateChange.subscribe((state) => {
switch (state) {
case "disconnected":
this.setConnectionState("disconnected");
break;
case "closed":
this.close();
break;
}
});
}

get localDescription() {
Expand Down Expand Up @@ -253,6 +260,7 @@ export class RTCPeerConnection extends EventTarget {

async createOffer() {
await this.ensureCerts();

const description = this.buildOfferSdp();
return description.toJSON();
}
Expand Down Expand Up @@ -570,7 +578,7 @@ export class RTCPeerConnection extends EventTarget {
this.config,
iceTransport,
this.router,
this.certificates,
this.certificate,
srtpProfiles,
);

Expand Down Expand Up @@ -1315,22 +1323,18 @@ export class RTCPeerConnection extends EventTarget {
}

private async ensureCerts() {
const ensureCert = async (dtlsTransport: RTCDtlsTransport) => {
if (this.certificates.length === 0) {
const localCertificate = await dtlsTransport.setupCertificate();
this.certificates.push(localCertificate);
} else {
dtlsTransport.localCertificate = this.certificates[0];
}
};
if (!this.certificate) {
this.certificate = await RTCDtlsTransport.SetupCertificate();
}

for (const dtlsTransport of this.dtlsTransports) {
await ensureCert(dtlsTransport);
dtlsTransport.localCertificate = this.certificate;
}
}

async createAnswer() {
await this.ensureCerts();

const description = this.buildAnswer();
return description.toJSON();
}
Expand Down
10 changes: 5 additions & 5 deletions packages/webrtc/src/transport/dtls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ export class RTCDtlsTransport {

readonly onStateChange = new Event<[DtlsState]>();

localCertificate?: RTCCertificate;
localCertificatePromise?: Promise<RTCCertificate>;
static localCertificate?: RTCCertificate;
static localCertificatePromise?: Promise<RTCCertificate>;
private remoteParameters?: RTCDtlsParameters;

constructor(
readonly config: PeerConfig,
readonly iceTransport: RTCIceTransport,
readonly router: RtpRouter,
readonly certificates: RTCCertificate[],
public localCertificate?: RTCCertificate,
private readonly srtpProfiles: Profile[] = [],
) {
this.localCertificate = this.certificates[0];
this.localCertificate ??= RTCDtlsTransport.localCertificate;
}

get localParameters() {
Expand All @@ -72,7 +72,7 @@ export class RTCDtlsTransport {
);
}

async setupCertificate() {
static async SetupCertificate() {
if (this.localCertificate) {
return this.localCertificate;
}
Expand Down
6 changes: 2 additions & 4 deletions packages/webrtc/tests/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,19 @@ export async function dtlsTransportPair() {
transport1.connection.iceControlling = true;
transport2.connection.iceControlling = false;

await RTCDtlsTransport.SetupCertificate();

const session1 = new RTCDtlsTransport(
defaultPeerConfig,
transport1,
new RtpRouter(),
[],
);
await session1.setupCertificate();

const session2 = new RTCDtlsTransport(
defaultPeerConfig,
transport2,
new RtpRouter(),
[],
);
await session2.setupCertificate();

session1.setRemoteParams(session2.localParameters);
session2.setRemoteParams(session1.localParameters);
Expand Down

0 comments on commit 164d9e8

Please sign in to comment.