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

feat: add modal for alignment and latency config #34

Merged
merged 7 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
80 changes: 53 additions & 27 deletions src/api/ateliereLive/ingest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
ResourcesCompactIngestResponse,
ResourcesIngestResponse,
ResourcesIngestStreamResponse,
ResourcesSourceResponse,
ResourcesThumbnailResponse
} from '../../../types/ateliere-live';
Expand All @@ -16,46 +17,50 @@ export async function getUuidFromIngestName(
ingestName: string,
useCache = true
) {
const cache = INGEST_UUID_CACHE.get(ingestName);
if (cache && useCache) {
return cache;
}
const ingests = await getIngests();
const ingest = ingests.find((ingest) => ingest.name === ingestName);
if (ingestName !== undefined) {
const cache = INGEST_UUID_CACHE.get(ingestName);
if (cache && useCache) {
return cache;
}
const ingests = await getIngests();
const ingest = ingests.find((ingest) => ingest.name === ingestName);

if (ingest && ingest.uuid) {
if (!ingest) {
console.warn(`Could not find ingest ${ingestName}`);
throw 'get_uuid';
}
INGEST_UUID_CACHE.set(ingestName, ingest.uuid);
return ingest.uuid;
}
console.warn(`Could not find UUID for ${ingestName}`);
throw 'get_uuid';
}

export async function getSourceIdFromSourceName(
ingestUuid: string,
sourceName: string,
useCache = true
) {
let ingestCache = SOURCE_ID_CACHE.get(ingestUuid);
if (!ingestCache) {
ingestCache = new Map();
SOURCE_ID_CACHE.set(ingestUuid, ingestCache);
}
const cache = ingestCache?.get(sourceName);
if (cache && useCache) {
return cache;
}
const ingest = await getIngest(ingestUuid);
const source = ingest.sources?.find((source) => source.name === sourceName);
if (ingestUuid !== undefined && sourceName !== undefined) {
let ingestCache = SOURCE_ID_CACHE.get(ingestUuid);
if (!ingestCache) {
ingestCache = new Map();
SOURCE_ID_CACHE.set(ingestUuid, ingestCache);
}
const cache = ingestCache?.get(sourceName);
if (cache && useCache) {
return cache;
}
const ingest = await getIngest(ingestUuid);
const source = ingest.sources?.find((source) => source.name === sourceName);

if (source && source.source_id !== undefined) {
ingestCache.set(sourceName, source.source_id);
return source.source_id;
if (source && source.source_id !== undefined) {
ingestCache.set(sourceName, source.source_id);
return source.source_id;
}
console.warn(
`Could not find id for source ${sourceName} in ingest ${ingestUuid}`
);
throw `Could not find id for source ${sourceName} in ingest ${ingestUuid}`;
}
console.warn(
`Could not find id for source ${sourceName} in ingest ${ingestUuid}`
);
throw `Could not find id for source ${sourceName} in ingest ${ingestUuid}`;
}

export async function getIngests(): Promise<ResourcesCompactIngestResponse[]> {
Expand Down Expand Up @@ -205,3 +210,24 @@ export async function getIngestSources(
const errorText = await response.text();
throw new Error(errorText);
}

export async function getIngestStreams(
ingestUuid: string
): Promise<ResourcesIngestStreamResponse[]> {
const response = await fetch(
new URL(
LIVE_BASE_API_PATH + `/ingests/${ingestUuid}/streams?expand=true`,
process.env.LIVE_URL
),
{
method: 'GET',
headers: {
authorization: getAuthorizationHeader()
}
}
);
if (response.ok) {
return response.json();
}
throw await response.text();
}
29 changes: 26 additions & 3 deletions src/api/ateliereLive/pipelines/streams/streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export async function createStream(
});

const sourceId = await getSourceIdFromSourceName(
ingestUuid,
ingestUuid || '',
source.ingest_source_name,
false
);
Expand Down Expand Up @@ -108,8 +108,8 @@ export async function createStream(
);

const stream: PipelineStreamSettings = {
ingest_id: ingestUuid,
source_id: sourceId,
ingest_id: ingestUuid || '',
source_id: sourceId || 0,
pipeline_id: pipeline.pipeline_id!,
input_slot: input_slot,
alignment_ms: pipeline.alignment_ms,
Expand Down Expand Up @@ -357,3 +357,26 @@ export async function deleteStream(streamUuid: string) {
}
throw await response.json();
}

export async function updateStream(streamUuid: string, alignment_ms: number) {
const response = await fetch(
new URL(
LIVE_BASE_API_PATH + `/streams/${streamUuid}`,
process.env.LIVE_URL
),
{
method: 'PATCH',
headers: {
authorization: getAuthorizationHeader()
},
body: JSON.stringify({ alignment_ms: alignment_ms }),
next: {
revalidate: 0
}
}
);
if (response.ok) {
return true;
}
throw await response.json();
}
158 changes: 158 additions & 0 deletions src/api/manager/productions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,161 @@ export async function deleteProduction(id: string): Promise<void> {
function deleteMonitoring(db: Db, productionId: string) {
db.collection('monitoring').deleteMany({ productionId: productionId });
}

export async function getProductionPipelineSourceAlignment(
productionId: string,
pipelineId: string,
sourceId: number
) {
const production = await getProduction(productionId);

if (!production) {
console.error('Production not found');
return null;
}

const pipeline = production.production_settings.pipelines.find(
(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
: pipeline?.alignment_ms;

return alignment;
}

export async function setProductionPipelineSourceAlignment(
productionId: string,
pipelineId: string,
sourceId: number,
alignment_ms: number
) {
const db = await getDatabase();

try {
const result = await db.collection('productions').updateOne(
{
_id: new ObjectId(productionId),
'production_settings.pipelines.pipeline_id': pipelineId,
'production_settings.pipelines.sources.source_id': sourceId
},
{
$set: {
'production_settings.pipelines.$[p].sources.$[s].settings.alignment_ms':
alignment_ms
}
},
{
arrayFilters: [
{ 'p.pipeline_id': pipelineId },
{ 's.source_id': sourceId }
]
}
);

if (result.matchedCount === 0) {
console.error('No matching pipeline or source found to update');
return null;
}

return true;
} catch (error) {
console.error('Database error:', error);
throw new Error('Error updating pipeline source alignment');
}
}

export async function getProductionSourceLatency(
productionId: string,
pipelineId: string,
sourceId: number
) {
const production = await getProduction(productionId);

if (!production) {
console.error('Production not found');
return null;
}

const pipeline = production.production_settings.pipelines.find(
(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
: pipeline?.max_network_latency_ms;

return latency;
}

export async function setProductionPipelineSourceLatency(
productionId: string,
pipelineId: string,
sourceId: number,
max_network_latency_ms: number
) {
const db = await getDatabase();

try {
const result = await db.collection('productions').updateOne(
{
_id: new ObjectId(productionId),
'production_settings.pipelines.pipeline_id': pipelineId,
'production_settings.pipelines.sources.source_id': sourceId
},
{
$set: {
'production_settings.pipelines.$[p].sources.$[s].settings.max_network_latency_ms':
max_network_latency_ms
}
},
{
arrayFilters: [
{ 'p.pipeline_id': pipelineId },
{ 's.source_id': sourceId }
]
}
);

if (result.matchedCount === 0) {
console.error('No matching pipeline or source found to update');
return null;
}

return true;
} catch (error) {
console.error('Database error:', error);
throw new Error('Error updating pipeline source latency');
}
}
Loading
Loading