From decb9c14f5a7aebbdebe7ef89708bbd58d4e1df6 Mon Sep 17 00:00:00 2001 From: Max Holland Date: Wed, 16 Aug 2023 11:56:10 +0100 Subject: [PATCH 1/2] Populate recordingURL when source playback is ready --- packages/api/src/controllers/stream.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/api/src/controllers/stream.ts b/packages/api/src/controllers/stream.ts index 416404463c..f85bfdd5bf 100644 --- a/packages/api/src/controllers/stream.ts +++ b/packages/api/src/controllers/stream.ts @@ -217,11 +217,14 @@ async function getRecordingUrls( // Recording V2 if (session.version === "v2") { const asset = await db.asset.getBySessionId(session.id); - if (!asset || ["waiting", "processing"].includes(asset.status?.phase)) { - // Recording processing in progress + if (!asset) { return; } const assetWithPlayback = await withPlaybackUrls(config, ingest, asset); + if (!assetWithPlayback.playbackUrl) { + // Recording processing in progress + return; + } return { recordingUrl: assetWithPlayback.playbackUrl, mp4Url: assetWithPlayback.downloadUrl, @@ -447,9 +450,6 @@ export async function getRecordingFields( const isUnused = !session.lastSeen && session.createdAt < readyThreshold; const recordingStatus = isReady ? "ready" : isUnused ? "none" : "waiting"; - if (!isReady && !forceUrl) { - return { recordingStatus }; - } const recordingUrls = await getRecordingUrls(config, ingest, session); if (!recordingUrls) { return { recordingStatus: "waiting" }; From e63c1aacfaac6f803fe331372b3c34241834438d Mon Sep 17 00:00:00 2001 From: Max Holland Date: Fri, 18 Aug 2023 14:10:52 +0100 Subject: [PATCH 2/2] Simplify recording status logic For recording V2 we can just base the recording status on the asset status --- packages/api/src/controllers/stream.ts | 73 +++++++++++--------------- 1 file changed, 31 insertions(+), 42 deletions(-) diff --git a/packages/api/src/controllers/stream.ts b/packages/api/src/controllers/stream.ts index f85bfdd5bf..4ca8c79b14 100644 --- a/packages/api/src/controllers/stream.ts +++ b/packages/api/src/controllers/stream.ts @@ -209,40 +209,6 @@ export function getWebRTCPlaybackUrl(ingest: string, stream: DBStream) { return pathJoin(ingest, `webrtc`, stream.playbackId); } -async function getRecordingUrls( - config: CliArgs, - ingest: string, - session: DBSession -): Promise<{ recordingUrl: string; mp4Url: string }> { - // Recording V2 - if (session.version === "v2") { - const asset = await db.asset.getBySessionId(session.id); - if (!asset) { - return; - } - const assetWithPlayback = await withPlaybackUrls(config, ingest, asset); - if (!assetWithPlayback.playbackUrl) { - // Recording processing in progress - return; - } - return { - recordingUrl: assetWithPlayback.playbackUrl, - mp4Url: assetWithPlayback.downloadUrl, - }; - } - - // Backwards-compatibility for Recording V1 - const base = pathJoin( - ingest, - `recordings`, - session.lastSessionId ?? session.id - ); - return { - recordingUrl: pathJoin(base, "index.m3u8"), - mp4Url: pathJoin(base, "source.mp4"), - }; -} - /** * Returns whether the stream is currently tagged as active but hasn't been * updated in a long time and thus should be cleaned up. @@ -445,19 +411,42 @@ export async function getRecordingFields( return {}; } + // Recording V2 + if (session.version === "v2") { + const asset = await db.asset.getBySessionId(session.id); + if (!asset) { + return { recordingStatus: "waiting" }; + } + const assetWithPlayback = await withPlaybackUrls(config, ingest, asset); + const assetPhase = assetWithPlayback.status?.phase; + return { + recordingStatus: + assetPhase == "ready" + ? "ready" + : assetPhase == "failed" + ? "none" + : "waiting", + recordingUrl: assetWithPlayback.playbackUrl, + mp4Url: assetWithPlayback.downloadUrl, + }; + } + + // Backwards-compatibility for Recording V1 const readyThreshold = Date.now() - USER_SESSION_TIMEOUT; const isReady = session.lastSeen > 0 && session.lastSeen < readyThreshold; const isUnused = !session.lastSeen && session.createdAt < readyThreshold; const recordingStatus = isReady ? "ready" : isUnused ? "none" : "waiting"; - const recordingUrls = await getRecordingUrls(config, ingest, session); - if (!recordingUrls) { - return { recordingStatus: "waiting" }; - } - if (!recordingUrls.recordingUrl) { - return { recordingStatus: "none" }; - } - return { recordingStatus, ...recordingUrls }; + const base = pathJoin( + ingest, + `recordings`, + session.lastSessionId ?? session.id + ); + return { + recordingStatus: recordingStatus, + recordingUrl: pathJoin(base, "index.m3u8"), + mp4Url: pathJoin(base, "source.mp4"), + }; } export async function withRecordingFields(