Skip to content

Commit

Permalink
feat: source settings config
Browse files Browse the repository at this point in the history
  • Loading branch information
Saelmala committed Oct 31, 2024
1 parent a05f835 commit 0cfb727
Show file tree
Hide file tree
Showing 16 changed files with 599 additions and 189 deletions.
11 changes: 9 additions & 2 deletions src/api/ateliereLive/pipelines/streams/streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,20 @@ export async function createStream(
`Allocated port ${availablePort} on '${source.ingest_name}' for ${source.ingest_source_name}`
);

const pipelineSource = pipeline.sources?.find(
(source) => source.source_id === sourceId
);

const stream: PipelineStreamSettings = {
ingest_id: ingestUuid || '',
source_id: sourceId || 0,
pipeline_id: pipeline.pipeline_id!,
input_slot: input_slot,
alignment_ms: pipeline.alignment_ms,
max_network_latency_ms: pipeline.max_network_latency_ms,
alignment_ms:
pipelineSource?.settings.alignment_ms || pipeline.alignment_ms,
max_network_latency_ms:
pipelineSource?.settings.max_network_latency_ms ||
pipeline.max_network_latency_ms,
width: pipeline.width,
height: pipeline.height,
frame_rate_d: pipeline.frame_rate_d,
Expand Down
51 changes: 30 additions & 21 deletions src/api/manager/productions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Db, ObjectId, UpdateResult } from 'mongodb';
import { getDatabase } from '../mongoClient/dbClient';
import { Production, ProductionWithId } from '../../interfaces/production';
import { Log } from '../logger';
import { SourceReference } from '../../interfaces/Source';

export async function getProductions(): Promise<Production[]> {
const db = await getDatabase();
Expand Down Expand Up @@ -118,20 +117,10 @@ export async function getProductionPipelineSourceAlignment(
(p) => p.pipeline_id === pipelineId
);

if (!pipeline) {
console.error('Pipeline not found');
return null;
}

const source = pipeline?.sources?.find(
(source) => String(source.source_id) === String(sourceId)
);

if (!source) {
console.error('Source not found');
return null;
}

const alignment =
source?.settings?.alignment_ms !== undefined
? source.settings.alignment_ms
Expand Down Expand Up @@ -197,20 +186,10 @@ export async function getProductionSourceLatency(
(p) => p.pipeline_id === pipelineId
);

if (!pipeline) {
console.error('Pipeline not found');
return null;
}

const source = pipeline?.sources?.find(
(source) => String(source.source_id) === String(sourceId)
);

if (!source) {
console.error('Source not found');
return null;
}

const latency =
source?.settings?.max_network_latency_ms !== undefined
? source.settings.max_network_latency_ms
Expand Down Expand Up @@ -259,3 +238,33 @@ export async function setProductionPipelineSourceLatency(
throw new Error('Error updating pipeline source latency');
}
}

export async function replaceProductionSourceStreamIds(
productionId: string,
sourceId: string | ObjectId,
newStreamUuids: string[]
) {
const db = await getDatabase();
const productionObjectId = new ObjectId(productionId);

const sourceIdForQuery =
typeof sourceId === 'string' ? sourceId : sourceId.toString();

const updateResult = await db.collection('productions').updateOne(
{
_id: productionObjectId,
'sources._id': sourceIdForQuery
},
{
$set: {
'sources.$.stream_uuids': newStreamUuids
}
}
);

if (updateResult.matchedCount === 0) {
throw new Error('Production or source not found');
}

return updateResult;
}
22 changes: 8 additions & 14 deletions src/api/manager/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -860,34 +860,28 @@ export async function startProduction(
const ingestUuid = await getUuidFromIngestName(
source.ingest_name
);
if (!ingestUuid) {
throw new Error(
`Ingest UUID not found for ingest name: ${source.ingest_name}`
);
}
const sourceId = await getSourceIdFromSourceName(
ingestUuid,
ingestUuid || '',
source.ingest_source_name
);

const currentSettings = pipeline.sources?.find(
(s) => s.source_id === sourceId
)?.settings;

return {
source_id: sourceId || 0,
settings: {
alignment_ms:
pipeline.sources?.find((s) => s.source_id === sourceId)
?.settings.alignment_ms || pipeline.alignment_ms,
currentSettings?.alignment_ms ?? pipeline.alignment_ms,
max_network_latency_ms:
pipeline.sources?.find((s) => s.source_id === sourceId)
?.settings.max_network_latency_ms ||
currentSettings?.max_network_latency_ms ??
pipeline.max_network_latency_ms
}
};
})
);
return {
...pipeline,
sources: newSources
};
return { ...pipeline, sources: newSources };
})
)
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { NextRequest, NextResponse } from 'next/server';
import { isAuthenticated } from '../../../../../../../api/manager/auth';
import { Log } from '../../../../../../../api/logger';
import {
getSourceIdFromSourceName,
getUuidFromIngestName
Expand Down
36 changes: 36 additions & 0 deletions src/app/api/manager/productions/[id]/sources/[source_id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { NextRequest, NextResponse } from 'next/server';
import { isAuthenticated } from '../../../../../../../api/manager/auth';
import { replaceProductionSourceStreamIds } from '../../../../../../../api/manager/productions';
import { Log } from '../../../../../../../api/logger';

type Params = {
id: string;
source_id: string;
};

export async function PUT(
request: NextRequest,
{ params }: { params: Params }
): Promise<NextResponse> {
if (!(await isAuthenticated())) {
return new NextResponse(`Not Authorized!`, {
status: 403
});
}

try {
const body = (await request.json()) as { stream_uuids: string[] };
const prod = await replaceProductionSourceStreamIds(
params.id,
params.source_id,
body.stream_uuids
);
return new NextResponse(JSON.stringify(prod), { status: 200 });
} catch (error) {
Log().warn('Could not update production source stream ids', error);

return new NextResponse(`Error searching DB! Error: ${error}`, {
status: 500
});
}
}
Loading

0 comments on commit 0cfb727

Please sign in to comment.