Skip to content

Commit

Permalink
feat: new user always creates new endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
oshinongit committed Jan 8, 2024
1 parent 116f6d6 commit 829aec6
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 69 deletions.
93 changes: 32 additions & 61 deletions src/api_productions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@ import {
import { SmbProtocol } from './smb';
import { ProductionManager } from './production_manager';
import { Connection } from './connection';
import { write, parse } from 'sdp-transform';
import { write, parse, SessionDescription } from 'sdp-transform';
import dotenv from 'dotenv';
import { MediaStreamsInfoSsrc } from './media_streams_info';
import { v4 as uuidv4 } from 'uuid';
dotenv.config();

const productionManager = new ProductionManager();

function generateOffer(
function createConnection(
endpoint: SmbEndpointDescription,
productionName: string,
lineName: string,
username: string
): string {
username: string,
endpointId: string
): Connection {
if (!endpoint.audio) {
throw new Error('Missing audio when creating offer');
}
Expand All @@ -46,19 +48,19 @@ function generateOffer(
const connection = new Connection(
username,
endpointMediaStreamInfo,
endpoint
endpoint,
endpointId
);

productionManager.addConnectionToLine(
productionName,
lineName,
username,
endpoint
endpoint,
endpointId
);

const offer = connection.createOffer();
const sdp = write(offer);
return sdp;
return connection;
}

async function createEndpoint(
Expand Down Expand Up @@ -377,11 +379,17 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
}
}

if (line.connections[request.params.username]) {
throw new Error(
`Connection ${request.params.username} already exists`
);
}
const endpointId: string = uuidv4();
const endpoint = await createEndpoint(
smb,
smbServerUrl,
line.id,
request.params.username,
endpointId,
true,
false,
parseInt(opts.endpointIdleTimeout, 10)
Expand All @@ -392,16 +400,17 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
if (!endpoint.audio.ssrcs) {
throw new Error('Missing ssrcs when creating sdp offer for endpoint');
}
if (request.params.name in line.connections) {
throw new Error(`Connection ${request.params.name} already exists`);
}
const sdpOffer = generateOffer(
const connection: Connection = createConnection(
endpoint,
production.name,
line.name,
request.params.username
request.params.username,
endpointId
);

const offer: SessionDescription = connection.createOffer();
const sdpOffer: string = write(offer);

if (sdpOffer) {
reply.code(200).send({ sdp: sdpOffer });
} else {
Expand Down Expand Up @@ -433,18 +442,23 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
const production: Production = getProduction(request.params.name);
const line: Line = getLine(production.lines, request.params.linename);

const connectionEndpointDescription =
line.connections[request.params.username];
const connectionEndpointDescription: SmbEndpointDescription =
line.connections[request.params.username].sessionDescription;
const endpointId: string =
line.connections[request.params.username].endpointId;

if (!connectionEndpointDescription) {
throw new Error('Could not get connection endpoint description');
}
if (!endpointId) {
throw new Error('Could not get connection endpoint id');
}

await handleAnswerRequest(
smb,
smbServerUrl,
line.id,
request.params.username,
endpointId,
connectionEndpointDescription,
request.body
);
Expand Down Expand Up @@ -519,50 +533,7 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
}
);

let parsedDelay_ms: number = parseInt(opts.smbPollInterval_s) * 1000;
if (parsedDelay_ms < 20000) {
parsedDelay_ms = 60000;
}

if (opts.smbPoll) {
startRepeatingPoll(smb, smbServerUrl, parsedDelay_ms);
}

next();
};

async function poll(smb: SmbProtocol, smbServerUrl: string) {
const productions: Production[] = productionManager.getProductions();

for (const production of productions) {
for (const line of production.lines) {
const endpoints: DetailedConference[] = await smb.getConference(
smbServerUrl,
line.id
);
const activeConnections: Record<string, SmbEndpointDescription> = {};
for (const endpoint of endpoints) {
const activeConnection: SmbEndpointDescription =
line.connections[endpoint['id']];
if (activeConnection) {
activeConnections[endpoint['id']] = activeConnection;
}
}
line.connections = activeConnections;
console.log(line.connections);
}
}
}

function startRepeatingPoll(
smb: SmbProtocol,
smbServerUrl: string,
delay_ms: number
) {
setInterval(() => {
poll(smb, smbServerUrl);
console.log('Repeating poll running...');
}, delay_ms);
}

export default apiProductions;
7 changes: 4 additions & 3 deletions src/connection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { v4 as uuidv4 } from 'uuid';
import { EventEmitter } from 'events';
import { SessionDescription } from 'sdp-transform';

Expand All @@ -17,15 +16,17 @@ export class Connection extends EventEmitter {

protected mediaStreams?: MediaStreamsInfo;
protected endpointDescription?: SfuEndpointDescription;
protected endpointId?: string;

constructor(
resourceId: string,
mediaStreams: MediaStreamsInfo,
endpointDescription: SfuEndpointDescription
endpointDescription: SfuEndpointDescription,
endpointId: string
) {
super();
this.resourceId = resourceId;
this.connectionId = uuidv4();
this.connectionId = endpointId;
this.mediaStreams = mediaStreams;
this.endpointDescription = endpointDescription;
this.log(`Create, sfuResourceId ${resourceId}`);
Expand Down
7 changes: 6 additions & 1 deletion src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,12 @@ export const SmbEndpointDescription = Type.Object({
idleTimeout: Type.Optional(Type.Number())
});

export const Connections = Type.Record(Type.String(), SmbEndpointDescription);
export const Endpoint = Type.Object({
endpointId: Type.String(),
sessionDescription: SmbEndpointDescription
});

export const Connections = Type.Record(Type.String(), Endpoint);

export const Production = Type.Object({
name: Type.String(),
Expand Down
10 changes: 8 additions & 2 deletions src/production_manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ describe('production_manager', () => {
'productionname',
'linename',
'username',
SmbEndpointDescriptionMock
SmbEndpointDescriptionMock,
'endpoinId'
);
const productionLines =
productionManagerTest.getProduction('productionname')?.lines;
Expand All @@ -294,7 +295,12 @@ describe('production_manager', () => {
const endpointDescription = productionManagerTest.getLine(
productionLines,
'linename'
)?.connections['username'];
)?.connections['username'].sessionDescription;
const endpointId = productionManagerTest.getLine(
productionLines,
'linename'
)?.connections['username'].endpointId;
expect(endpointDescription).toStrictEqual(SmbEndpointDescriptionMock);
expect(endpointId).toStrictEqual('endpoinId');
});
});
8 changes: 6 additions & 2 deletions src/production_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,19 @@ export class ProductionManager {
productionName: string,
lineName: string,
userName: string,
endpointDescription: SmbEndpointDescription
endpointDescription: SmbEndpointDescription,
endpointId: string
): void {
const production = this.getProduction(productionName);
if (production) {
const matchedLine = production.lines.find(
(line) => line.name === lineName
);
if (matchedLine) {
matchedLine.connections[userName] = endpointDescription;
matchedLine.connections[userName] = {
sessionDescription: endpointDescription,
endpointId: endpointId
};
}
} else {
throw new Error(
Expand Down

0 comments on commit 829aec6

Please sign in to comment.