Skip to content

Commit 3d961d9

Browse files
authored
Add support for tag EXT-X-DEFINE (#167)
1 parent 62f9611 commit 3d961d9

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

parse.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ function getTagCategory(tagName: string): TagCategory {
7373
return 'MasterPlaylist';
7474
case 'EXT-X-INDEPENDENT-SEGMENTS':
7575
case 'EXT-X-START':
76+
case 'EXT-X-DEFINE':
7677
return 'MediaorMasterPlaylist';
7778
default:
7879
return 'Unknown';
@@ -247,6 +248,7 @@ function parseTagParam(name: string, param): TagParam {
247248
case 'EXT-X-PRELOAD-HINT':
248249
case 'EXT-X-RENDITION-REPORT':
249250
case 'EXT-X-SKIP':
251+
case 'EXT-X-DEFINE':
250252
return [null, parseAttributeList(param)];
251253
case 'EXTINF':
252254
return [parseEXTINF(param), null];
@@ -477,6 +479,11 @@ function parseMasterPlaylist(lines: Line[], params: Record<string, any>): Master
477479
utils.INVALIDPLAYLIST('EXT-X-START: TIME-OFFSET attribute is REQUIRED');
478480
}
479481
playlist.start = {offset: attributes['TIME-OFFSET'], precise: attributes['PRECISE'] || false};
482+
} else if (name === 'EXT-X-DEFINE') {
483+
if (!playlist.defines) {
484+
playlist.defines = [];
485+
}
486+
playlist.defines.push(attributes);
480487
}
481488
}
482489
if (variantIsScored) {
@@ -775,6 +782,11 @@ function parseMediaPlaylist(lines: Line[], params: Record<string, any>): MediaPl
775782
}
776783
prefetchFound = true;
777784
segmentStart = -1;
785+
} else if (name === 'EXT-X-DEFINE') {
786+
if (!playlist.defines) {
787+
playlist.defines = [];
788+
}
789+
playlist.defines.push(attributes);
778790
} else if (typeof line === 'string') {
779791
// uri
780792
if (segmentStart === -1) {

stringify.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,15 @@ function buildParts(lines: LineArray, parts: PartialSegment[]) {
490490
return hint;
491491
}
492492

493+
function buildDefines(define: Record<string, string>) {
494+
const attrs: string[] = [];
495+
for (var attr in define) {
496+
attrs.push(`${attr}="${define[attr]}"`);
497+
}
498+
return `#EXT-X-DEFINE:${attrs.join(',')}`;
499+
}
500+
501+
493502
function stringify(playlist: MasterPlaylist | MediaPlaylist, postProcess?: PostProcess): string {
494503
utils.PARAMCHECK(playlist);
495504
utils.ASSERT('Not a playlist', playlist.type === 'playlist');
@@ -504,6 +513,11 @@ function stringify(playlist: MasterPlaylist | MediaPlaylist, postProcess?: PostP
504513
if (playlist.start) {
505514
lines.push(`#EXT-X-START:TIME-OFFSET=${buildDecimalFloatingNumber(playlist.start.offset)}${playlist.start.precise ? ',PRECISE=YES' : ''}`);
506515
}
516+
if (playlist.defines) {
517+
for (const session of playlist.defines) {
518+
lines.push(buildDefines(session));
519+
}
520+
}
507521
if (playlist.isMasterPlaylist) {
508522
buildMasterPlaylist(lines, playlist, postProcess);
509523
} else {

types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,16 @@ class Playlist extends Data {
277277
independentSegments: boolean;
278278
start?: { offset: number; precise: boolean };
279279
source?: string;
280+
defines?: Record<string, string>[];
280281

281282
constructor({
282283
isMasterPlaylist, // required
283284
uri,
284285
version,
285286
independentSegments = false,
286287
start,
287-
source
288+
source,
289+
defines,
288290
}: Partial<Playlist> & { isMasterPlaylist: boolean }) {
289291
super('playlist');
290292
utils.PARAMCHECK(isMasterPlaylist);
@@ -294,6 +296,7 @@ class Playlist extends Data {
294296
this.independentSegments = independentSegments;
295297
this.start = start;
296298
this.source = source;
299+
this.defines = defines;
297300
}
298301
}
299302

0 commit comments

Comments
 (0)