Skip to content

Commit

Permalink
Create new func for metadata call
Browse files Browse the repository at this point in the history
  • Loading branch information
mjh1 committed Sep 12, 2024
1 parent d15a9ae commit 186c789
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
32 changes: 32 additions & 0 deletions packages/api/src/controllers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ export const triggerCatalystPullStart =
process.env.NODE_ENV === "test"
? async () => {} // noop in case of tests
: async (stream: DBStream, playbackUrl: string) => {
console.log("pull start for " + playbackUrl);
const { lat, lon } = stream.pull?.location ?? {};
if (lat && lon) {
// Set the lat/lon qs to override the observed "client location" and
Expand All @@ -682,6 +683,7 @@ export const triggerCatalystPullStart =
errHeader != "" &&
!errHeader.includes("not allowed to view this stream");
if (res.ok && !isHlsErr) {
console.log("pull start finished for " + playbackUrl);
return;
}

Expand All @@ -699,6 +701,36 @@ export const triggerCatalystPullStart =
throw new Error(`failed to trigger catalyst pull`);
};

export const getCatalystStreamMetadata =
process.env.NODE_ENV === "test"
? async () => {} // noop in case of tests
: async (url: string) => {
console.log("pull start for " + url);

const deadline = Date.now() + 2 * PULL_START_TIMEOUT;
while (Date.now() < deadline) {
const res = await fetchWithTimeoutAndRedirects(url, {
method: "GET",
timeout: PULL_START_TIMEOUT,
maxRedirects: 10,
});
const body = await res.text();
if (res.ok && body.includes(`"meta":`)) {
console.log("pull start finished for " + url);
return;
}

logger.warn(
`getCatalystStreamMetadata failed for playbackUrl=${url} status=${
res.status
} error=${JSON.stringify(body)}`,
);
await sleep(250);
}

throw new Error(`failed to trigger catalyst pull`);
};

export const triggerCatalystStreamStopSessions = (
req: Request,
playback_id: string,
Expand Down
9 changes: 4 additions & 5 deletions packages/api/src/controllers/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import {
triggerCatalystPullStart,
triggerCatalystStreamStopSessions,
triggerCatalystStreamUpdated,
getCatalystStreamMetadata,
} from "./helpers";
import { toExternalSession } from "./session";
import wowzaHydrate from "./wowza-hydrate";
Expand Down Expand Up @@ -1259,11 +1260,9 @@ app.put(
: getHLSPlaybackUrl(ingest, stream);
if (!stream.isActive || streamExisted) {
triggerCatalystPullStart(stream, playbackUrl);
const jsonUrl = new URL(playbackUrl);
jsonUrl.pathname = "/json_video+" + stream.playbackId + ".js";
console.log("calling json " + jsonUrl.toString());
await triggerCatalystPullStart(stream, jsonUrl.toString());
console.log("finished calling json " + jsonUrl.toString());
const metadataUrl = new URL(playbackUrl);
metadataUrl.pathname = "/json_video+" + stream.playbackId + ".js";
await getCatalystStreamMetadata(metadataUrl.toString());
}

res.status(streamExisted ? 200 : 201);
Expand Down

0 comments on commit 186c789

Please sign in to comment.