Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node: Add RouterDump and DirectTransportDump types #1195

Merged
merged 6 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions node/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ const eslintConfig =
}
],
'space-in-parens' : [ 2, 'never' ],
'space-infix-ops' : [ 2, { 'int32Hint': false } ],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good. I'm adding it in other projects.

'spaced-comment' : [ 2, 'always' ],
'strict' : 2,
'valid-typeof' : 2,
Expand Down
4 changes: 3 additions & 1 deletion node/src/DirectTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export type DirectTransportOptions<DirectTransportAppData extends AppData = AppD
appData?: DirectTransportAppData;
};

export type DirectTransportDump = BaseTransportDump;

export type DirectTransportStat = BaseTransportStats &
{
type: string;
Expand Down Expand Up @@ -118,7 +120,7 @@ export class DirectTransport<DirectTransportAppData extends AppData = AppData>
/**
* Dump Transport.
*/
async dump(): Promise<any>
async dump(): Promise<DirectTransportDump>
{
logger.debug('dump()');

Expand Down
58 changes: 55 additions & 3 deletions node/src/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { RtpCapabilities, RtpCodecCapability } from './RtpParameters';
import { cryptoSuiteToFbs } from './SrtpParameters';
import { NumSctpStreams } from './SctpParameters';
import { AppData, Either } from './types';
import { generateUUIDv4 } from './utils';
import { generateUUIDv4, parseVector, parseStringStringVector, parseStringStringArrayVector } from './utils';
import * as FbsActiveSpeakerObserver from './fbs/active-speaker-observer';
import * as FbsAudioLevelObserver from './fbs/audio-level-observer';
import * as FbsRequest from './fbs/request';
Expand Down Expand Up @@ -128,6 +128,42 @@ export type PipeToRouterResult =
pipeDataProducer?: DataProducer;
};

export type RouterDump =
{
/**
* The Router id.
*/
id: string;
/**
* Id of Transports.
*/
transportIds: string[];
/**
* Id of RtpObservers.
*/
rtpObserverIds: string[];
/**
* Array of Producer id and its respective Consumer ids.
*/
mapProducerIdConsumerIds: { key: string; values: string[] }[];
/**
* Array of Consumer id and its Producer id.
*/
mapConsumerIdProducerId: {key: string; value: string}[];
/**
* Array of Producer id and its respective Observer ids.
*/
mapProducerIdObserverIds: {key: string; values: string[]}[];
/**
* Array of Producer id and its respective DataConsumer ids.
*/
mapDataProducerIdDataConsumerIds: {key: string; values: string[]}[];
/**
* Array of DataConsumer id and its DataProducer id.
*/
mapDataConsumerIdDataProducerId: {key: string; value: string}[];
};

type PipeTransportPair =
{
[key: string]: PipeTransport;
Expand Down Expand Up @@ -376,7 +412,7 @@ export class Router<RouterAppData extends AppData = AppData>
/**
* Dump Router.
*/
async dump(): Promise<any>
async dump(): Promise<RouterDump>
{
logger.debug('dump()');

Expand All @@ -393,7 +429,7 @@ export class Router<RouterAppData extends AppData = AppData>

response.body(dump);

return dump.unpack();
return parseRouterDumpResponse(dump);
}

/**
Expand Down Expand Up @@ -1566,3 +1602,19 @@ export class Router<RouterAppData extends AppData = AppData>
}
}
}

export function parseRouterDumpResponse(
binary: FbsRouter.DumpResponse
): RouterDump
{
return {
id : binary.id()!,
transportIds : parseVector(binary, 'transportIds'),
rtpObserverIds : parseVector(binary, 'rtpObserverIds'),
mapProducerIdConsumerIds : parseStringStringArrayVector(binary, 'mapProducerIdConsumerIds'),
mapConsumerIdProducerId : parseStringStringVector(binary, 'mapConsumerIdProducerId'),
mapProducerIdObserverIds : parseStringStringArrayVector(binary, 'mapProducerIdObserverIds'),
mapDataProducerIdDataConsumerIds : parseStringStringArrayVector(binary, 'mapDataProducerIdDataConsumerIds'),
mapDataConsumerIdDataProducerId : parseStringStringVector(binary, 'mapDataConsumerIdDataProducerId')
};
}
6 changes: 3 additions & 3 deletions node/src/RtpParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ export function serializeRtpParameters(

const rtcpFeedback: number[] = [];

for (const rtcp of codec.rtcpFeedback?? [])
for (const rtcp of codec.rtcpFeedback ?? [])
{
const typeOffset = builder.createString(rtcp.type);
const rtcpParametersOffset = builder.createString(rtcp.parameter);
Expand Down Expand Up @@ -575,7 +575,7 @@ export function serializeParameters(
if (typeof value === 'boolean')
{
parameterOffset = FbsParameter.createParameter(
builder, keyOffset, FbsValue.Boolean, value === true ? 1:0
builder, keyOffset, FbsValue.Boolean, value === true ? 1 : 0
);
}
else if (typeof value === 'number')
Expand Down Expand Up @@ -638,7 +638,7 @@ export function parseParameters(data: any): any
{
const parameters: any = {};

for (let i=0; i<data.parametersLength(); i++)
for (let i = 0; i < data.parametersLength(); i++)
{
const fbsParameter = data.parameters(i)!;

Expand Down
6 changes: 3 additions & 3 deletions node/src/Transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ export class Transport
async dump(): Promise<any>
{
// Should not happen.
throw new Error('method not implemented in the subclass');
throw new Error('method implemented in the subclass');
}

/**
Expand All @@ -610,7 +610,7 @@ export class Transport
async getStats(): Promise<any[]>
{
// Should not happen.
throw new Error('method not implemented in the subclass');
throw new Error('method implemented in the subclass');
}

/**
Expand All @@ -622,7 +622,7 @@ export class Transport
async connect(params: any): Promise<void>
{
// Should not happen.
throw new Error('method not implemented in the subclass');
throw new Error('method implemented in the subclass');
}

/**
Expand Down
76 changes: 60 additions & 16 deletions node/src/WebRtcServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export type WebRtcServerOptions<WebRtcServerAppData extends AppData = AppData> =
export type WebRtcServerListenInfo = TransportListenInfo;

export type WebRtcServerEvents =
{
{
workerclose: [];
// Private events.
'@close': [];
Expand All @@ -42,6 +42,34 @@ export type WebRtcServerObserverEvents =
webrtctransportunhandled: [WebRtcTransport];
};

export type WebRtcServerDump =
{
id: string;
udpSockets: IpPort[];
tcpServers: IpPort[];
webRtcTransportIds: string[];
localIceUsernameFragments: IceUserNameFragment[];
tupleHashes: TupleHash[];
};

type IpPort =
{
ip: string;
port: number;
};

type IceUserNameFragment =
{
localIceUsernameFragment: string;
webRtcTransportId: string;
};

type TupleHash =
{
tupleHash: number;
webRtcTransportId: string;
};

type WebRtcServerInternal =
{
webRtcServerId: string;
Expand Down Expand Up @@ -213,7 +241,7 @@ export class WebRtcServer<WebRtcServerAppData extends AppData = AppData>
/**
* Dump WebRtcServer.
*/
async dump(): Promise<any>
async dump(): Promise<WebRtcServerDump>
{
logger.debug('dump()');

Expand Down Expand Up @@ -248,32 +276,48 @@ export class WebRtcServer<WebRtcServerAppData extends AppData = AppData>
}
}

// TODO: This function should return WebRtcServerDump TypeScript type but we
// don't have it yet (same for many other dump() methods everywhere).
function parseIpPort(binary: FbsWebRtcServer.IpPort): IpPort
{
return {
ip : binary.ip()!,
port : binary.port()
};
}

function parseIceUserNameFragment(binary: FbsWebRtcServer.IceUserNameFragment): IceUserNameFragment
{
return {
localIceUsernameFragment : binary.localIceUsernameFragment()!,
webRtcTransportId : binary.webRtcTransportId()!
};
}

function parseTupleHash(binary: FbsWebRtcServer.TupleHash): TupleHash
{
return {
tupleHash : Number(binary.tupleHash()!),
webRtcTransportId : binary.webRtcTransportId()!
};
}

function parseWebRtcServerDump(
data: FbsWebRtcServer.DumpResponse
): any
): WebRtcServerDump
{
return {
id : data.id(),
id : data.id()!,
udpSockets : utils.parseVector(
data, 'udpSockets', (udpSocket: any) => udpSocket.unpack()
data, 'udpSockets', parseIpPort
),
tcpServers : utils.parseVector(
data, 'tcpServers', (tcpServer: any) => tcpServer.unpack()
data, 'tcpServers', parseIpPort
),
webRtcTransportIds : utils.parseVector(data, 'webRtcTransportIds'),
localIceUsernameFragments : utils.parseVector(
data, 'localIceUsernameFragments', (localIceUsernameFragment: any) => localIceUsernameFragment.unpack()
data, 'localIceUsernameFragments', parseIceUserNameFragment
),
tupleHashes : utils.parseVector(
data, 'tupleHashes', (tupleHash: any) =>
{
return {
localIceUsernameFragment : Number(tupleHash.localIceUsernameFragment()),
webRtcTransportId : tupleHash.webRtcTransportId()
};
}
data, 'tupleHashes', parseTupleHash
)
};
}
4 changes: 2 additions & 2 deletions node/src/WebRtcTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ function parseDtlsParameters(binary: FbsWebRtcTransport.DtlsParameters): DtlsPar
{
const fingerprints: DtlsFingerprint[] = [];

for (let i=0; i<binary.fingerprintsLength(); ++i)
for (let i = 0; i < binary.fingerprintsLength(); ++i)
{
const fbsFingerprint = binary.fingerprints(i)!;
const fingerPrint : DtlsFingerprint =
Expand All @@ -878,7 +878,7 @@ function parseDtlsParameters(binary: FbsWebRtcTransport.DtlsParameters): DtlsPar

return {
fingerprints : fingerprints,
role : binary.role() === null? undefined : dtlsRoleFromFbs(binary.role()!)
role : binary.role() === null ? undefined : dtlsRoleFromFbs(binary.role()!)
};
}

Expand Down
24 changes: 20 additions & 4 deletions node/src/Worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Channel } from './Channel';
import { Router, RouterOptions } from './Router';
import { WebRtcServer, WebRtcServerOptions } from './WebRtcServer';
import { AppData } from './types';
import { generateUUIDv4 } from './utils';
import { generateUUIDv4, parseVector } from './utils';
import { Event } from './fbs/notification';
import * as FbsRequest from './fbs/request';
import * as FbsWorker from './fbs/worker';
Expand Down Expand Up @@ -184,7 +184,7 @@ export type WorkerResourceUsage =
export type WorkerDump =
{
pid : number;
webrtcServerIds : string[];
webRtcServerIds : string[];
routerIds : string[];
channelMessageHandlers :
{
Expand Down Expand Up @@ -581,7 +581,7 @@ export class Worker<WorkerAppData extends AppData = AppData>
/**
* Dump Worker.
*/
async dump(): Promise<any>
async dump(): Promise<WorkerDump>
{
logger.debug('dump()');

Expand All @@ -595,7 +595,7 @@ export class Worker<WorkerAppData extends AppData = AppData>

response.body(dump);

return dump.unpack();
return parseWorkerDumpResponse(dump);
}

/**
Expand Down Expand Up @@ -807,3 +807,19 @@ export class Worker<WorkerAppData extends AppData = AppData>
this.#observer.safeEmit('close');
}
}

export function parseWorkerDumpResponse(
binary: FbsWorker.DumpResponse
): WorkerDump
{
return {
pid : binary.pid()!,
webRtcServerIds : parseVector(binary, 'webRtcServerIds'),
routerIds : parseVector(binary, 'routerIds'),
channelMessageHandlers :
{
channelRequestHandlers : parseVector(binary.channelMessageHandlers()!, 'channelRequestHandlers'),
channelNotificationHandlers : parseVector(binary.channelMessageHandlers()!, 'channelNotificationHandlers')
}
};
}
2 changes: 1 addition & 1 deletion node/src/ortc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ export function getConsumerRtpParameters(
}

// Must sanitize the list of matched codecs by removing useless RTX codecs.
for (let idx = consumerParams.codecs.length -1; idx >= 0; --idx)
for (let idx = consumerParams.codecs.length - 1; idx >= 0; --idx)
{
const codec = consumerParams.codecs[idx];

Expand Down
Loading
Loading