Skip to content

Commit

Permalink
Node: Add RouterDump and DirectTransportDump types
Browse files Browse the repository at this point in the history
  • Loading branch information
jmillan committed Oct 26, 2023
1 parent be38184 commit c2d96b8
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 13 deletions.
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
57 changes: 54 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,41 @@ 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 +411,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 +428,7 @@ export class Router<RouterAppData extends AppData = AppData>

response.body(dump);

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

/**
Expand Down Expand Up @@ -1566,3 +1601,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')
};
}
14 changes: 7 additions & 7 deletions node/src/tests/test-Consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1009,27 +1009,27 @@ test('consumer.close() succeeds', async () =>
expect(onObserverClose).toHaveBeenCalledTimes(1);
expect(audioConsumer.closed).toBe(true);

let dump = await router.dump();
const routerDump = await router.dump();

expect(dump.mapProducerIdConsumerIds)
expect(routerDump.mapProducerIdConsumerIds)
.toEqual(expect.arrayContaining([
{ key: audioProducer.id, values: [ ] }
]));

expect(dump.mapConsumerIdProducerId)
expect(routerDump.mapConsumerIdProducerId)
.toEqual(expect.arrayContaining([
{ key: videoConsumer.id, value: videoProducer.id }
]));
expect(dump.mapConsumerIdProducerId)
expect(routerDump.mapConsumerIdProducerId)
.toEqual(expect.arrayContaining([
{ key: videoPipeConsumer.id, value: videoProducer.id }
]));

dump = await transport2.dump();
const transportDump = await transport2.dump();

dump.consumerIds = dump.consumerIds.sort();
transportDump.consumerIds = transportDump.consumerIds.sort();

expect(dump)
expect(routerDump)
.toMatchObject(
{
id : transport2.id,
Expand Down
48 changes: 46 additions & 2 deletions node/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@ export function parseVector<Type>(
return array;
}

/**
* Parse flatbuffers vector of StringString into the corresponding array.
*/
export function parseStringStringVector(
binary: any, methodName: string
): { key: string; value: string }[]
{
const array: { key: string; value: string }[] = [];

for (let i=0; i<binary[`${methodName}Length`](); ++i)
{
const kv = binary[methodName](i)!;

array.push({ key: kv.key(), value: kv.value() });
}

return array;
}

/**
* Parse flatbuffers vector of StringUint8 into the corresponding array.
*/
Expand All @@ -111,7 +130,7 @@ export function parseStringUint8Vector(
}

/**
* Parse flatbuffers of Uint16String into the corresponding array.
* Parse flatbuffers vector of Uint16String into the corresponding array.
*/
export function parseUint16StringVector(
binary: any, methodName: string
Expand All @@ -130,7 +149,7 @@ export function parseUint16StringVector(
}

/**
* Parse flatbuffers of Uint32String into the corresponding array.
* Parse flatbuffers vector of Uint32String into the corresponding array.
*/
export function parseUint32StringVector(
binary: any, methodName: string
Expand All @@ -147,3 +166,28 @@ export function parseUint32StringVector(

return array;
}

/**
* Parse flatbuffers vector of StringStringArray into the corresponding array.
*/
export function parseStringStringArrayVector(
binary: any, methodName: string
): { key: string; values: string[] }[]
{
const array: { key: string; values: string[] }[] = [];

for (let i=0; i<binary[`${methodName}Length`](); ++i)
{
const kv = binary[methodName](i)!;
const values: string[] = [];

for (let i2=0; i2<kv.valuesLength(); ++ i2)
{
values.push(kv.values(i2)!);
}

array.push({ key: kv.key(), values });
}

return array;
}

0 comments on commit c2d96b8

Please sign in to comment.