Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
shinyoshiaki committed Oct 13, 2024
1 parent 6870d64 commit 6260b57
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 28 deletions.
7 changes: 2 additions & 5 deletions packages/ice/src/candidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,8 @@ export function candidateFoundation(
}

// priorityを決める
export function candidatePriority(
candidateComponent: number,
candidateType: string,
localPref = 65535,
) {
export function candidatePriority(candidateType: string, localPref = 65535) {
const candidateComponent: number = 1;
// See RFC 5245 - 4.1.2.1. Recommended Formula
let typePref = 0;
if (candidateType === "host") {
Expand Down
48 changes: 27 additions & 21 deletions packages/ice/src/ice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class Connection {
candidateFoundation("host", "udp", candidateAddress[0]),
1,
"udp",
candidatePriority(1, "host"),
candidatePriority("host"),
candidateAddress[0],
candidateAddress[1],
"host",
Expand Down Expand Up @@ -226,7 +226,7 @@ export class Connection {
candidateFoundation("relay", "udp", candidateAddress[0]),
1,
"udp",
candidatePriority(1, "relay"),
candidatePriority("relay"),
candidateAddress[0],
candidateAddress[1],
"relay",
Expand Down Expand Up @@ -955,7 +955,7 @@ export class Connection {
const request = new Message(methods.BINDING, classes.REQUEST);
request
.setAttribute("USERNAME", txUsername)
.setAttribute("PRIORITY", candidatePriority(pair.component, "prflx"));
.setAttribute("PRIORITY", candidatePriority("prflx"));
if (this.iceControlling) {
request.setAttribute("ICE-CONTROLLING", this._tieBreaker);
if (nominate) {
Expand Down Expand Up @@ -1015,8 +1015,9 @@ export class CandidatePair {
}

get localCandidate() {
if (!this.protocol.localCandidate)
if (!this.protocol.localCandidate) {
throw new Error("localCandidate not exist");
}
return this.protocol.localCandidate;
}

Expand Down Expand Up @@ -1083,28 +1084,33 @@ export function validateRemoteCandidate(candidate: Candidate) {
}

export function sortCandidatePairs(
pairs: CandidatePair[],
pairs: {
localCandidate: Pick<Candidate, "priority">;
remoteCandidate: Pick<Candidate, "priority">;
}[],
iceControlling: boolean,
) {
pairs.sort(
(a, b) =>
candidatePairPriority(
a.localCandidate,
a.remoteCandidate,
iceControlling,
) -
candidatePairPriority(
b.localCandidate,
b.remoteCandidate,
iceControlling,
),
);
return pairs
.sort(
(a, b) =>
candidatePairPriority(
a.localCandidate,
a.remoteCandidate,
iceControlling,
) -
candidatePairPriority(
b.localCandidate,
b.remoteCandidate,
iceControlling,
),
)
.reverse();
}

// 5.7.2. Computing Pair Priority and Ordering Pairs
export function candidatePairPriority(
local: Candidate,
remote: Candidate,
local: Pick<Candidate, "priority">,
remote: Pick<Candidate, "priority">,
iceControlling: boolean,
) {
const G = (iceControlling && local.priority) || remote.priority;
Expand Down Expand Up @@ -1134,7 +1140,7 @@ export async function serverReflexiveCandidate(
candidateFoundation("srflx", "udp", localCandidate.host),
localCandidate.component,
localCandidate.transport,
candidatePriority(localCandidate.component, "srflx"),
candidatePriority("srflx"),
response.getAttributeValue("XOR-MAPPED-ADDRESS")[0],
response.getAttributeValue("XOR-MAPPED-ADDRESS")[1],
"srflx",
Expand Down
45 changes: 43 additions & 2 deletions packages/ice/tests/ice/ice.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { setTimeout } from "timers/promises";

import { Candidate } from "../../src/candidate";
import { CandidatePair, CandidatePairState, Connection } from "../../src/ice";
import { Candidate, candidatePriority } from "../../src/candidate";
import {
CandidatePair,
CandidatePairState,
Connection,
sortCandidatePairs,
} from "../../src/ice";
import { classes, methods } from "../../src/stun/const";
import { Message } from "../../src/stun/message";
import type { Address, Protocol } from "../../src/types/model";
Expand Down Expand Up @@ -613,3 +618,39 @@ describe("ice", () => {
await b.close();
});
});

describe("sortCandidatePairs", () => {
it("controlling", () => {
const host = {
type: "host",
localCandidate: { priority: candidatePriority("host") },
remoteCandidate: { priority: candidatePriority("host") },
};

const relay = {
type: "relay",
localCandidate: { priority: candidatePriority("relay") },
remoteCandidate: { priority: candidatePriority("relay") },
};

const res = sortCandidatePairs([host, relay], true);
expect(res).toEqual([host, relay]);
});

it("controlled", () => {
const host = {
type: "host",
localCandidate: { priority: candidatePriority("host") },
remoteCandidate: { priority: candidatePriority("host") },
};

const relay = {
type: "relay",
localCandidate: { priority: candidatePriority("relay") },
remoteCandidate: { priority: candidatePriority("relay") },
};

const res = sortCandidatePairs([host, relay], true);
expect(res).toEqual([host, relay]);
});
});

0 comments on commit 6260b57

Please sign in to comment.