Skip to content

Change 'not-used-playback' to 'ingest-{playbackId}' #2193

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

Merged
merged 3 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 16 additions & 8 deletions packages/api/src/controllers/stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -888,48 +888,56 @@ describe("controllers/stream", () => {
it("should extract host from redirected playback url", async () => {
expect(
extractUrlFrom(
"https://sto-prod-catalyst-0.lp-playback.studio:443/hls/video+not-used-playback/index.m3u8"
"https://sto-prod-catalyst-0.lp-playback.studio:443/hls/video+ingest-12345-67890/index.m3u8",
"12345-67890"
)
).toBe("https://sto-prod-catalyst-0.lp-playback.studio:443/hls/video+");
expect(
extractUrlFrom(
"https://mos2-prod-catalyst-0.lp-playback.studio:443/hls/video+not-used-playback/index.m3u8"
"https://mos2-prod-catalyst-0.lp-playback.studio:443/hls/video+ingest-12345-67890/index.m3u8",
"12345-67890"
)
).toBe(
"https://mos2-prod-catalyst-0.lp-playback.studio:443/hls/video+"
);
expect(
extractUrlFrom(
"https://fra-staging-staging-catalyst-0.livepeer.monster:443/hls/video+not-used-playback/index.m3u8"
"https://fra-staging-staging-catalyst-0.livepeer.monster:443/hls/video+ingest-12345-67890/index.m3u8",
"12345-67890"
)
).toBe(
"https://fra-staging-staging-catalyst-0.livepeer.monster:443/hls/video+"
);
expect(
extractUrlFrom(
"https://fra-staging-staging-catalyst-0.livepeer.monster:443/hls/video+other-playback/index.m3u8"
"https://fra-staging-staging-catalyst-0.livepeer.monster:443/hls/video+other-playback/index.m3u8",
"12345-67890"
)
).toBe(null);
});
it("should extract region from redirected playback url", async () => {
expect(
extractRegionFrom(
"https://sto-prod-catalyst-0.lp-playback.studio:443/hls/video+not-used-playback/index.m3u8"
"https://sto-prod-catalyst-0.lp-playback.studio:443/hls/video+ingest-12345-67890/index.m3u8",
"12345-67890"
)
).toBe("sto");
expect(
extractRegionFrom(
"https://mos2-prod-catalyst-0.lp-playback.studio:443/hls/video+not-used-playback/index.m3u8"
"https://mos2-prod-catalyst-0.lp-playback.studio:443/hls/video+ingest-12345-67890/index.m3u8",
"12345-67890"
)
).toBe("mos2");
expect(
extractRegionFrom(
"https://fra-staging-staging-catalyst-0.livepeer.monster:443/hls/video+not-used-playback/index.m3u8"
"https://fra-staging-staging-catalyst-0.livepeer.monster:443/hls/video+ingest-12345-67890/index.m3u8",
"12345-67890"
)
).toBe("fra-staging");
expect(
extractRegionFrom(
"https://fra-staging-staging-catalyst-0.livepeer.monster:443/hls/video+other-playback/index.m3u8"
"https://fra-staging-staging-catalyst-0.livepeer.monster:443/hls/video+other-playback/index.m3u8",
"12345-67890"
)
).toBe(null);
});
Expand Down
51 changes: 36 additions & 15 deletions packages/api/src/controllers/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,14 @@

async function resolvePullUrlAndRegion(
stream: NewStreamPayload,
ingest: string
ingest: string,
playbackId: string
): Promise<{ pullUrl: string; pullRegion: string }> {
if (process.env.NODE_ENV === "test") {
return { pullUrl: null, pullRegion: null };
}
const url = new URL(
pathJoin(ingest, `hls`, "not-used-playback", `index.m3u8`)
pathJoin(ingest, `hls`, `ingest-${playbackId}`, `index.m3u8`)
);
const { lat, lon } = stream.pull?.location ?? {};
if (lat && lon) {
Expand All @@ -316,23 +317,31 @@
return null;
}
return {
pullUrl: extractUrlFrom(response.url),
pullRegion: extractRegionFrom(response.url),
pullUrl: extractUrlFrom(response.url, playbackId),
pullRegion: extractRegionFrom(response.url, playbackId),
};
}

// Extracts Mist URL from redirected node URL, e.g. "https://sto-prod-catalyst-0.lp-playback.studio:443/hls/video+" from "https://sto-prod-catalyst-0.lp-playback.studio:443/hls/video+foo/index.m3u8"
export function extractUrlFrom(playbackUrl: string): string {
const hostRegex =
/(https?:\/\/.+-\w+-catalyst.+\/hls\/.+)not-used-playback\/index.m3u8/;
export function extractUrlFrom(
playbackUrl: string,
playbackId: string
): string {
const hostRegex = new RegExp(
`(https?:\/\/.+-\\w+-catalyst.+\/hls\/.+)ingest-${playbackId}\/index.m3u8`
);
const matches = playbackUrl.match(hostRegex);
return matches ? matches[1] : null;
}

// Extracts region from redirected node URL, e.g. "sto" from "https://sto-prod-catalyst-0.lp-playback.studio:443/hls/video+foo/index.m3u8"
export function extractRegionFrom(playbackUrl: string): string {
const regionRegex =
/https?:\/\/(.+)-\w+-catalyst.+not-used-playback\/index.m3u8/;
export function extractRegionFrom(
playbackUrl: string,
playbackId: string
): string {
const regionRegex = new RegExp(
`https?:\/\/(.+)-\\w+-catalyst.+ingest-${playbackId}\/index.m3u8`
);
const matches = playbackUrl.match(regionRegex);
return matches ? matches[1] : null;
}
Expand Down Expand Up @@ -1177,20 +1186,32 @@
const streamExisted = streams.length === 1;

const ingest = await getIngestBase(req);
const { pullUrl, pullRegion } =
resolvePullUrlFromExistingStreams(streams) ||
(await resolvePullUrlAndRegion(rawPayload, ingest));
let streamPullUrl: string;

let stream: DBStream;
if (!streamExisted) {
logger.info(
`pull request creating a new stream with name=${rawPayload.name}`
);
stream = await handleCreateStream(req, payload);
const { pullUrl, pullRegion } = await resolvePullUrlAndRegion(
rawPayload,
ingest,
stream.playbackId
);
streamPullUrl = pullUrl;
stream.pullRegion = pullRegion;
await db.stream.replace(stream);
} else {
const oldStream = streams[0];
const { pullUrl, pullRegion } =
resolvePullUrlFromExistingStreams(streams) ||
(await resolvePullUrlAndRegion(
rawPayload,
ingest,
oldStream.playbackId
));
streamPullUrl = pullUrl;
logger.info(
`pull reusing existing old stream with id=${oldStream.id} name=${oldStream.name}`
);
Expand Down Expand Up @@ -1222,8 +1243,8 @@
}

// If pullHost was resolved, then stick to that host for triggering Catalyst pull start
const playbackUrl = pullUrl
? pathJoin(pullUrl + stream.playbackId, `index.m3u8`)
const playbackUrl = streamPullUrl
? pathJoin(streamPullUrl + stream.playbackId, `index.m3u8`)
: getHLSPlaybackUrl(ingest, stream);
if (!stream.isActive || streamExisted) {
await triggerCatalystPullStart(stream, playbackUrl);
Expand Down
Loading