Skip to content

Commit

Permalink
Change 'not-used-playback' to 'ingest-{playbackId}' (#2193)
Browse files Browse the repository at this point in the history
* Change 'not-used-playback' to 'ingest-{playbackId}'

* Fix RegEx and tests
  • Loading branch information
leszko authored May 29, 2024
1 parent 3a65956 commit 16bc344
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 23 deletions.
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 @@ export function resolvePullUrlFromExistingStreams(

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 @@ async function resolvePullUrlAndRegion(
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 @@ app.put(
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 @@ app.put(
}

// 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

0 comments on commit 16bc344

Please sign in to comment.