Skip to content

Commit

Permalink
fix(#64): handle hls with init segments (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
birme authored Apr 23, 2024
1 parent 5206b32 commit 8ed5a2f
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 10 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"dependencies": {
"@aws-sdk/client-ssm": "^3.306.0",
"@eyevinn/m3u8": ">=0.1.2",
"@eyevinn/m3u8": ">=0.5.6",
"@fastify/aws-lambda": "^3.2.0",
"aws-lambda": "^1.0.7",
"dotenv": "^8.2.0",
Expand Down
81 changes: 81 additions & 0 deletions src/manifests/handlers/hls/media.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,87 @@ https://mock.mock.com/stream/hls/manifest_1_00001.ts
expect(response.body).toEqual(expected.body);
});

it('should handle init segments for HLS cmaf media', async () => {
// Arrange
const getMedia = () => {
return new Promise((resolve) => {
const readStream: ReadStream = createReadStream(
path.join(
__dirname,
`../../../testvectors/hls/hls1_cmaf/manifest_1.m3u8`
)
);
resolve(readStream);
});
};
nock(mockBaseURL).persist().get('/manifest_1.m3u8').reply(200, getMedia, {
'Content-Type': 'application/vnd.apple.mpegurl;charset=UTF-8',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type, Origin'
});

const queryParams = {
url: mockMediaURL,
statusCode: '[{i:2,ms:3000}]'
};
const event: ALBEvent = {
requestContext: {
elb: {
targetGroupArn: ''
}
},
path: '/stream/hls/manifest.m3u8',
httpMethod: 'GET',
headers: {
accept: 'application/vnd.apple.mpegurl;charset=UTF-8',
'accept-language': 'en-US,en;q=0.8',
'content-type': 'text/plain',
host: 'lambda-846800462-us-east-2.elb.amazonaws.com',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6)',
'x-amzn-trace-id': 'Root=1-5bdb40ca-556d8b0c50dc66f0511bf520',
'x-forwarded-for': '72.21.198.xx',
'x-forwarded-port': '443',
'x-forwarded-proto': 'https'
},
isBase64Encoded: false,
queryStringParameters: queryParams,
body: ''
};

// Act
const response = await hlsMediaHandler(event);

// Assert
const expected: ALBResult = {
statusCode: 200,
headers: {
'Access-Control-Allow-Headers': 'Content-Type, Origin',
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/vnd.apple.mpegurl'
},
body: `#EXTM3U
#EXT-X-VERSION:6
#EXT-X-PLAYLIST-TYPE:VOD
#EXT-X-MEDIA-SEQUENCE:1
#EXT-X-INDEPENDENT-SEGMENTS
#EXT-X-TARGETDURATION:4
#USP-X-TIMESTAMP-MAP:MPEGTS=900000,LOCAL=1970-01-01T00:00:00Z
#EXT-X-MAP:URI="https://mock.mock.com/stream/hls/subdir/142d4370-56c9-11ee-a816-2da19aea6ed1_20571919-video=6500000.m4s"
#EXT-X-PROGRAM-DATE-TIME:1970-01-01T00:00:00Z
#EXTINF:3.8400, no desc
https://mock.mock.com/stream/hls/subdir/142d4370-56c9-11ee-a816-2da19aea6ed1_20571919-video=6500000-1.m4s
#EXTINF:3.8400, no desc
https://mock.mock.com/stream/hls/subdir/142d4370-56c9-11ee-a816-2da19aea6ed1_20571919-video=6500000-2.m4s
#EXTINF:3.8400, no desc
https://mock.mock.com/stream/hls/subdir/142d4370-56c9-11ee-a816-2da19aea6ed1_20571919-video=6500000-3.m4s
#EXT-X-ENDLIST
`
};
expect(response.statusCode).toEqual(expected.statusCode);
expect(response.headers).toEqual(expected.headers);
expect(response.body).toEqual(expected.body);
});

//it('should return code 500 on Other Errors, eg M3U8 parser error', async () => {});
});
});
5 changes: 5 additions & 0 deletions src/manifests/utils/hlsManifestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ export default function (): HLSManifestTools {
if (!sourceSegURL.match(/^http/)) {
sourceSegURL = `${sourceBaseURL}/${item.get('uri')}`;
}
let sourceMapURL: string | undefined = item.get('map-uri');
if (sourceMapURL && !sourceMapURL.match(/^http/)) {
sourceMapURL = `${sourceBaseURL}/${sourceMapURL}`;
item.set('map-uri', sourceMapURL);
}

if (!corruption) {
item.set('uri', sourceSegURL);
Expand Down
4 changes: 2 additions & 2 deletions src/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export type TargetLevel = number;

/* eslint-disable */
export type M3UItem = {
get: (key: 'uri' | 'type') => string | any;
set: (key: 'uri', value: string) => void;
get: (key: 'uri' | 'type' | 'map-uri') => string | any;
set: (key: 'uri' | 'map-uri', value: string) => void;
};

export type M3U = {
Expand Down
6 changes: 6 additions & 0 deletions src/testvectors/hls/hls1_cmaf/manifest.m3u8
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-INDEPENDENT-SEGMENTS
#EXT-X-STREAM-INF:BANDWIDTH=4255267,AVERAGE-BANDWIDTH=4255267,CODECS="avc1.4d4032,mp4a.40.2",RESOLUTION=2560x1440,FRAME-RATE=25.000,AUDIO="audio",SUBTITLES="subs"
manifest_1.m3u8

16 changes: 16 additions & 0 deletions src/testvectors/hls/hls1_cmaf/manifest_1.m3u8
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#EXTM3U
#EXT-X-VERSION:6
#EXT-X-PLAYLIST-TYPE:VOD
#EXT-X-MEDIA-SEQUENCE:1
#EXT-X-INDEPENDENT-SEGMENTS
#EXT-X-TARGETDURATION:4
#USP-X-TIMESTAMP-MAP:MPEGTS=900000,LOCAL=1970-01-01T00:00:00Z
#EXT-X-MAP:URI="subdir/142d4370-56c9-11ee-a816-2da19aea6ed1_20571919-video=6500000.m4s"
#EXT-X-PROGRAM-DATE-TIME:1970-01-01T00:00:00Z
#EXTINF:3.84, no desc
subdir/142d4370-56c9-11ee-a816-2da19aea6ed1_20571919-video=6500000-1.m4s
#EXTINF:3.84, no desc
subdir/142d4370-56c9-11ee-a816-2da19aea6ed1_20571919-video=6500000-2.m4s
#EXTINF:3.84, no desc
subdir/142d4370-56c9-11ee-a816-2da19aea6ed1_20571919-video=6500000-3.m4s
#EXT-X-ENDLIST

0 comments on commit 8ed5a2f

Please sign in to comment.