Skip to content

Commit

Permalink
Merge pull request #309 from koush/develop
Browse files Browse the repository at this point in the history
fix initializer vs constructor order
  • Loading branch information
shinyoshiaki authored Jan 15, 2023
2 parents 8ce30f8 + 9fd9bd1 commit 1aaa806
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 34 deletions.
4 changes: 3 additions & 1 deletion packages/dtls/src/context/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ import { Transport } from "../transport";
export class TransportContext {
constructor(public socket: Transport) {}

readonly send = this.socket.send;
readonly send = (buf: Buffer) => {
return this.socket.send(buf);
};
}
21 changes: 11 additions & 10 deletions packages/dtls/src/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,9 @@ export class DtlsSocket {
readonly onData = new Event<[Buffer]>();
readonly onError = new Event<[Error]>();
readonly onClose = new Event();
readonly transport: TransportContext = new TransportContext(
this.options.transport
);
cipher: CipherContext = new CipherContext(
this.sessionType,
this.options.cert,
this.options.key,
this.options.signatureHash
);
dtls: DtlsContext = new DtlsContext(this.options, this.sessionType);
readonly transport: TransportContext;
cipher: CipherContext;
dtls: DtlsContext;
srtp: SrtpContext = new SrtpContext();

connected = false;
Expand All @@ -54,6 +47,14 @@ export class DtlsSocket {
private bufferFragmentedHandshakes: FragmentedHandshake[] = [];

constructor(public options: Options, public sessionType: SessionTypes) {
this.dtls = new DtlsContext(this.options, this.sessionType);
this.cipher = new CipherContext(
this.sessionType,
this.options.cert,
this.options.key,
this.options.signatureHash
);
this.transport = new TransportContext(this.options.transport);
this.setupExtensions();
this.transport.socket.onData = this.udpOnMessage;
}
Expand Down
8 changes: 5 additions & 3 deletions packages/ice/src/stun/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ export class Transaction {
private timeoutDelay = RETRY_RTO;
private timeoutHandle?: any;
private tries = 0;
private readonly triesMax =
1 + (this.retransmissions ? this.retransmissions : RETRY_MAX);
private readonly triesMax: number;
private readonly onResponse = new Event<[Message, Address]>();

constructor(
private request: Message,
private addr: Address,
private protocol: Protocol,
private retransmissions?: number
) {}
) {
this.triesMax =
1 + (this.retransmissions ? this.retransmissions : RETRY_MAX);
}

responseReceived = (message: Message, addr: Address) => {
if (this.onResponse.length > 0) {
Expand Down
5 changes: 3 additions & 2 deletions packages/ice/src/transport.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import debug from "debug";
import { createSocket, SocketType } from "dgram";
import { createSocket, Socket, SocketType } from "dgram";

import {
findPort,
Expand All @@ -12,14 +12,15 @@ import { normalizeFamilyNodeV18 } from "./utils";
const log = debug("werift-ice:packages/ice/src/transport.ts");

export class UdpTransport implements Transport {
private socket = createSocket(this.type);
private socket: Socket;
onData: (data: Buffer, addr: Address) => void = () => {};

constructor(
private type: SocketType,
private portRange?: [number, number],
private interfaceAddresses?: InterfaceAddresses
) {
this.socket = createSocket(this.type);
this.socket.on("message", (data, info) => {
if (normalizeFamilyNodeV18(info.family) === 6) {
[info.address] = info.address.split("%"); // example fe80::1d3a:8751:4ffd:eb80%wlp82s0
Expand Down
15 changes: 10 additions & 5 deletions packages/rtp/src/processor/lipsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ export type LipsyncOutput = {
};

export class LipsyncBase implements AVProcessor<LipsyncInput> {
bufferLength = this.options.bufferLength ?? 50;
bufferLength: number;
/**ms */
baseTime?: number;
audioBuffer: (LipsyncInput & {
elapsed: number;
kind: string;
[key: string]: any;
})[][] = [...new Array(this.bufferLength)].map(() => []);
})[][];
videoBuffer: (LipsyncInput & {
elapsed: number;
kind: string;
[key: string]: any;
})[][] = [...new Array(this.bufferLength)].map(() => []);
})[][];
stopped = false;
/**ms */
private interval = this.options.interval ?? 500;
private interval: number;
private started = false;
/**ms */
lastCommited = 0;
Expand All @@ -37,7 +37,12 @@ export class LipsyncBase implements AVProcessor<LipsyncInput> {
private audioOutput: (output: LipsyncOutput) => void,
private videoOutput: (output: LipsyncOutput) => void,
private options: Partial<LipSyncOptions> = {}
) {}
) {
this.bufferLength = this.options.bufferLength ?? 50;
this.audioBuffer = [...new Array(this.bufferLength)].map(() => []);
this.videoBuffer = [...new Array(this.bufferLength)].map(() => []);
this.interval = this.options.interval ?? 500;
}

private start() {
// 2列目にカーソルが移ってから処理を始めることで1列目の処理を完了できる
Expand Down
3 changes: 2 additions & 1 deletion packages/sctp/src/sctp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class SCTP {

private hmacKey = randomBytes(16);
private localPartialReliability = true;
private localPort = this.port;
private localPort: number;
private localVerificationTag = random32();

remoteExtensions: number[] = [];
Expand Down Expand Up @@ -166,6 +166,7 @@ export class SCTP {
private ssthresh?: number; // slow start threshold

constructor(public transport: Transport, public port = 5000) {
this.localPort = this.port;
this.transport.onData = (buf) => {
this.handleData(buf);
};
Expand Down
4 changes: 3 additions & 1 deletion packages/webrtc/src/dataChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class RTCDataChannel extends EventTarget {
// todo impl
onerror?: CallbackWithValue<RTCErrorEvent>;
isCreatedByRemote = false;
id: number = this.parameters.id;
id: number;
readyState: DCState = "connecting";

bufferedAmount = 0;
Expand All @@ -33,6 +33,8 @@ export class RTCDataChannel extends EventTarget {
) {
super();

this.id = this.parameters.id;

if (parameters.negotiated) {
if (this.id == undefined || this.id < 0 || this.id > 65534) {
throw new Error(
Expand Down
9 changes: 5 additions & 4 deletions packages/webrtc/src/media/rtpSender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ const RTT_ALPHA = 0.85;

export class RTCRtpSender {
readonly type = "sender";
readonly kind =
typeof this.trackOrKind === "string"
? this.trackOrKind
: this.trackOrKind.kind;
readonly kind: Kind;
readonly ssrc = jspack.Unpack("!L", randomBytes(4))[0];
readonly rtxSsrc = jspack.Unpack("!L", randomBytes(4))[0];
streamId = uuid.v4();
Expand Down Expand Up @@ -124,6 +121,10 @@ export class RTCRtpSender {
private rtcpCancel = new AbortController();

constructor(public trackOrKind: Kind | MediaStreamTrack) {
this.kind =
typeof this.trackOrKind === "string"
? this.trackOrKind
: this.trackOrKind.kind;
if (trackOrKind instanceof MediaStreamTrack) {
if (trackOrKind.streamId) {
this.streamId = trackOrKind.streamId;
Expand Down
10 changes: 7 additions & 3 deletions packages/webrtc/src/transport/dtls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class RTCDtlsTransport {

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

localCertificate?: RTCCertificate = this.certificates[0];
localCertificate?: RTCCertificate;
private remoteParameters?: RTCDtlsParameters;

constructor(
Expand All @@ -58,7 +58,9 @@ export class RTCDtlsTransport {
readonly router: RtpRouter,
readonly certificates: RTCCertificate[],
private readonly srtpProfiles: Profile[] = []
) {}
) {
this.localCertificate = this.certificates[0];
}

get localParameters() {
return new RTCDtlsParameters(
Expand Down Expand Up @@ -340,7 +342,9 @@ class IceTransport implements Transport {
}
onData?: (buf: Buffer) => void;

readonly send = this.ice.send;
readonly send = (data: Buffer) => {
return this.ice.send(data);
};

close() {
this.ice.close();
Expand Down
9 changes: 6 additions & 3 deletions packages/webrtc/src/transport/ice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ const log = debug("werift:packages/webrtc/src/transport/ice.ts");

export class RTCIceTransport {
readonly id = v4();
connection = this.gather.connection;
connection: Connection;
state: RTCIceConnectionState = "new";

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

private waitStart?: Event<[]>;

constructor(private gather: RTCIceGatherer) {
this.connection = this.gather.connection;
this.connection.stateChanged.subscribe((state) => {
this.setState(state);
});
Expand Down Expand Up @@ -116,9 +117,11 @@ export class RTCIceGatherer {
gatheringState: IceGathererState = "new";

readonly onGatheringStateChange = new Event<[IceGathererState]>();
readonly connection = new Connection(false, this.options);
readonly connection: Connection;

constructor(private options: Partial<IceOptions> = {}) {}
constructor(private options: Partial<IceOptions> = {}) {
this.connection = new Connection(false, this.options);
}

async gather() {
if (this.gatheringState === "new") {
Expand Down
4 changes: 3 additions & 1 deletion packages/webrtc/src/transport/sctp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ class BridgeDtls implements Transport {
set onData(onData: (buf: Buffer) => void) {
this.dtls.dataReceiver = onData;
}
readonly send = this.dtls.sendData;
readonly send = (data: Buffer) => {
return this.dtls.sendData(data);
};
close() {}
}

0 comments on commit 1aaa806

Please sign in to comment.