Skip to content

Commit

Permalink
feat: maxResolution for master manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
matvp91 committed Aug 29, 2024
1 parent e478731 commit 8126829
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/stitcher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"type": "module",
"scripts": {
"build": "tsc",
"start": "node ./dist/index.js",
"dev": "tsc-watch --noClear --onSuccess \"node ./dist/index.js\"",
"start": "node ./dist/src/index.js",
"dev": "tsc-watch --noClear --onSuccess \"node ./dist/src/index.js\"",
"test": "NODE_OPTIONS=--experimental-vm-modules jest"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/stitcher/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const postSessionBodySchema = z.object({
)
.optional(),
bumperAssetId: z.string().optional(),
maxResolution: z.number().optional(),
});

export const contract = c.router({
Expand Down
7 changes: 7 additions & 0 deletions packages/stitcher/src/playlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ export async function formatMasterPlaylist(session: Session) {

const master = await fetchPlaylist<MasterPlaylist>(url);

master.variants = master.variants.filter((variant) => {
if (!variant.resolution) {
return true;
}
return variant.resolution.height <= session.maxResolution;
});

return stringify(master);
}

Expand Down
8 changes: 8 additions & 0 deletions packages/stitcher/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export async function createSession(data: {
vmapUrl?: string;
interstitials?: Interstitial[];
bumperAssetId?: string;
maxResolution?: number;
}) {
const sessionId = randomUUID();

Expand Down Expand Up @@ -42,10 +43,17 @@ export async function createSession(data: {
});
}

let maxResolution = data.maxResolution;
if (!maxResolution) {
const MAX_RESOLUTION_8K = 4320;
maxResolution = MAX_RESOLUTION_8K;
}

const session = {
id: sessionId,
assetId: data.assetId,
interstitials,
maxResolution,
} satisfies Session;

const redisKey = getRedisKey(sessionId);
Expand Down
1 change: 1 addition & 0 deletions packages/stitcher/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export type Session = {
id: string;
assetId: string;
interstitials: Interstitial[];
maxResolution: number;
};

export type Interstitial = {
Expand Down
33 changes: 33 additions & 0 deletions packages/stitcher/test/playlist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe("playlist", () => {
id: "sessionId",
assetId: "assetId",
interstitials: [],
maxResolution: 1080,
});

expect(master).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -64,6 +65,7 @@ describe("playlist", () => {
id: "sessionId",
assetId: "assetId",
interstitials: [],
maxResolution: 1080,
},
"video_1080"
);
Expand Down Expand Up @@ -135,6 +137,7 @@ describe("playlist", () => {
assetId: "ad3",
},
],
maxResolution: 1080,
},
"video_1080"
);
Expand All @@ -161,4 +164,34 @@ describe("playlist", () => {
#EXT-X-DATERANGE:ID="10",CLASS="com.apple.hls.interstitial",START-DATE="2022-02-21T17:35:10.000Z",X-ASSET-LIST="/session/sessionId/asset-list.json?timeOffset=10",X-RESUME-OFFSET=0,X-RESTRICT="SKIP,JUMP""
`);
});

test("should remove resolutions below maxResolution", async () => {
fetchMock.mock("https://s3-public.com/package/assetId/hls/master.m3u8", {
status: 200,
body: `
#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2149280,CODECS="mp4a.40.2,avc1.64001f",RESOLUTION=100x720,NAME="720"
720.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2149280,CODECS="mp4a.40.2,avc1.64001f",RESOLUTION=100x480,NAME="480"
480.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2149280,CODECS="mp4a.40.2,avc1.64001f",RESOLUTION=100x360,NAME="360"
360.m3u8
`,
});

const master = await formatMasterPlaylist({
id: "sessionId",
assetId: "assetId",
interstitials: [],
maxResolution: 480,
});

expect(master).toMatchInlineSnapshot(`
"#EXTM3U
#EXT-X-STREAM-INF:BANDWIDTH=2149280,CODECS="mp4a.40.2,avc1.64001f",RESOLUTION=100x480,PROGRAM-ID=1
480.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=2149280,CODECS="mp4a.40.2,avc1.64001f",RESOLUTION=100x360,PROGRAM-ID=1
360.m3u8"
`);
});
});

0 comments on commit 8126829

Please sign in to comment.