Skip to content

Commit d1435f7

Browse files
authored
Add support for EXT-X-DATERANGE tag at media playlist level (#168)
1 parent a03fe43 commit d1435f7

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

parse.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,27 @@ function parseMasterPlaylist(lines: Line[], params: Record<string, any>): Master
501501
return playlist;
502502
}
503503

504+
function parseDateRange(attributes) {
505+
const attrs: Record<string, any> = {};
506+
for (const key of Object.keys(attributes)) {
507+
if (key.startsWith('SCTE35-') || key.startsWith('X-')) {
508+
attrs[key] = attributes[key];
509+
}
510+
}
511+
const dateRange = new DateRange({
512+
id: attributes['ID'],
513+
classId: attributes['CLASS'],
514+
start: attributes['START-DATE'],
515+
cue: attributes['CUE'],
516+
end: attributes['END-DATE'],
517+
duration: attributes['DURATION'],
518+
plannedDuration: attributes['PLANNED-DURATION'],
519+
endOnNext: attributes['END-ON-NEXT'],
520+
attributes: attrs
521+
});
522+
return dateRange;
523+
}
524+
504525
function parseSegment(lines: Line[], uri: string, start: number, end: number, mediaSequenceNumber: number, discontinuitySequence: number, params: Record<string, any>): Segment {
505526
const segment = new Segment({uri, mediaSequenceNumber, discontinuitySequence});
506527
let mapHint = false;
@@ -558,23 +579,7 @@ function parseSegment(lines: Line[], uri: string, start: number, end: number, me
558579
} else if (name === 'EXT-X-PROGRAM-DATE-TIME') {
559580
segment.programDateTime = value;
560581
} else if (name === 'EXT-X-DATERANGE') {
561-
const attrs: Record<string, any> = {};
562-
for (const key of Object.keys(attributes)) {
563-
if (key.startsWith('SCTE35-') || key.startsWith('X-')) {
564-
attrs[key] = attributes[key];
565-
}
566-
}
567-
segment.dateRange = new DateRange({
568-
id: attributes['ID'],
569-
classId: attributes['CLASS'],
570-
start: attributes['START-DATE'],
571-
cue: attributes['CUE'],
572-
end: attributes['END-DATE'],
573-
duration: attributes['DURATION'],
574-
plannedDuration: attributes['PLANNED-DURATION'],
575-
endOnNext: attributes['END-ON-NEXT'],
576-
attributes: attrs
577-
});
582+
segment.dateRange = parseDateRange(attributes);
578583
} else if (name === 'EXT-X-CUE-OUT') {
579584
segment.markers.push(new SpliceInfo({
580585
type: 'OUT',
@@ -783,6 +788,9 @@ function parseMediaPlaylist(lines: Line[], params: Record<string, any>): MediaPl
783788
} else if (name === 'EXT-X-DEFINE') {
784789
playlist.defines ||= [];
785790
playlist.defines.push(attributes);
791+
} else if (name === 'EXT-X-DATERANGE') {
792+
const dateRange = parseDateRange(attributes);
793+
playlist.dateRanges.push(dateRange);
786794
} else if (typeof line === 'string') {
787795
// uri
788796
if (segmentStart === -1) {

stringify.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,9 @@ function buildMediaPlaylist(lines: LineArray, playlist: MediaPlaylist, postProce
301301
if (playlist.skip > 0) {
302302
lines.push(`#EXT-X-SKIP:SKIPPED-SEGMENTS=${playlist.skip}`);
303303
}
304+
for (const dateRange of playlist.dateRanges) {
305+
lines.push(buildDateRange(dateRange));
306+
}
304307
for (const [i, segment] of playlist.segments.entries()) {
305308
const base = lines.length;
306309
let markerType = '';

types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ class MediaPlaylist extends Playlist {
340340
endlist: boolean;
341341
playlistType?: 'EVENT' | 'VOD';
342342
isIFrame?: boolean;
343+
dateRanges: DateRange[];
343344
segments: Segment[];
344345
prefetchSegments: PrefetchSegment[];
345346
lowLatencyCompatibility?: LowLatencyCompatibility;
@@ -357,6 +358,7 @@ class MediaPlaylist extends Playlist {
357358
endlist = false,
358359
playlistType,
359360
isIFrame,
361+
dateRanges = [],
360362
segments = [],
361363
prefetchSegments = [],
362364
lowLatencyCompatibility,
@@ -371,6 +373,7 @@ class MediaPlaylist extends Playlist {
371373
this.endlist = endlist;
372374
this.playlistType = playlistType;
373375
this.isIFrame = isIFrame;
376+
this.dateRanges = dateRanges;
374377
this.segments = segments;
375378
this.prefetchSegments = prefetchSegments;
376379
this.lowLatencyCompatibility = lowLatencyCompatibility;
@@ -394,7 +397,7 @@ class Segment extends Data {
394397
key?: Key;
395398
map: MediaInitializationSection;
396399
programDateTime?: Date;
397-
dateRange: DateRange;
400+
dateRange?: DateRange;
398401
markers: SpliceInfo[];
399402
parts: PartialSegment[];
400403
gap?: boolean;

0 commit comments

Comments
 (0)