-- playlist/m3u tests (+ the PLS parts of the ported suite): M3U / M3U8 (EXTINF, comments, BOM, CRLF / CR, -- Windows-1252, relative / absolute / local locations), PLS, HLS master and media playlists, detection, serialize round -- trips, bad input; Python-written playlists (tools/fixtures/playlist.py) decode to their entries, and our M3U8 output -- decodes back; the api; the fuzz pass. Ported from playlist-luau tests/playlist.luau. Fixtures: -- tools/fixtures/playlist.py. local T = require("@tests/T") local M3u = require("@formats/playlist/m3u") local Pls = require("@formats/playlist/pls") local Bytes = require("@formats/core/Bytes") local F = require("@formats") local Cases = require("@tests/gen/playlist_cases") local t = T.suite("m3u") local function eq(got: any, want: any, label: string) t:eq(got, want, label) end local function has(list: { string }, pattern: string): boolean for _, w in list do if string.find(w, pattern) then return true end end return false end -- the playlist-luau API on top of the two formats (the ported tests below use it) local Playlist = {} function Playlist.detect(text: string): string if type(text) ~= "string" then return "m3u" end local r = Bytes.reader(buffer.fromstring(text)) if Pls.sniff(r) > 0 then return "pls" end return if string.find(text, "#EXT%-X%-") then "hls" else "m3u" end function Playlist.parse(text: any, baseUrl: string?): any if type(text) ~= "string" then return { kind = "m3u", entries = {}, warnings = { "expected a string" } } end local kind = Playlist.detect(text) local mod = if kind == "pls" then Pls else M3u local pl = mod.decode(Bytes.reader(buffer.fromstring(text)), { baseUrl = baseUrl }) :: any pl.kind = pl.format -- the old field return pl end function Playlist.serialize(entries: any, opts: any?): string return buffer.tostring(M3u.encode(entries, opts) :: buffer) end -- M3U8 with EXTINF, comments, blank lines, BOM, CRLF do local text = "\239\187\191#EXTM3U\r\n#PLAYLIST:My Mix\r\n\r\n# a comment\r\n#EXTINF:123,Daft Punk - One More Time\r\n" .. "music/one more time.mp3\r\n#EXTINF:-1,Radio\r\nhttp://radio.example/stream\r\n" .. "#EXTINF:61.5 tvg-id=\"x\" group-title=\"a,b\",Grouped, with comma\r\n../other/b.mp3\r\n" .. "#EXTGRP:ignored\r\n/abs/c.mp3\r\n https://cdn.example/d.mp3?x=1 \r\n" local pl = Playlist.parse(text, "https://host.example/lists/m.m3u8") eq(pl.kind, "m3u", "kind") eq(pl.title, "My Mix", "#PLAYLIST") eq(#pl.entries, 5, "entry count") local e = pl.entries eq(e[1].url, "https://host.example/lists/music/one%20more%20time.mp3", "relative resolved + encoded") eq(e[1].title, "Daft Punk - One More Time", "EXTINF title") eq(e[1].duration, 123, "EXTINF duration") eq(e[1].playable, true, "https playable") eq(e[2].url, "http://radio.example/stream", "absolute kept") eq(e[2].duration, nil, "duration -1 = unknown") eq(e[2].title, "Radio", "title 2") eq(e[3].url, "https://host.example/other/b.mp3", "dot-dot resolved") eq(e[3].duration, 61.5, "fractional duration with attributes") eq(e[3].title, "Grouped, with comma", "title after the attributes keeps its commas") eq(e[4].url, "https://host.example/abs/c.mp3", "absolute path") eq(e[4].title, nil, "EXTINF does not leak to the next entry") eq(e[5].url, "https://cdn.example/d.mp3?x=1", "trimmed absolute") eq(#pl.warnings, 0, "no warnings") end -- plain M3U (no header), CR line endings, Windows paths, relative without base, other schemes do local text = "C:\\Music\\Song A.mp3\rD:/Other/b.mp3\r\\\\server\\share\\c.mp3\rsongs\\d.mp3\rftp://x/e.mp3\rfile:///home/u/f.mp3" local pl = Playlist.parse(text) eq(#pl.entries, 6, "plain count") eq(pl.entries[1].url, "C:\\Music\\Song A.mp3", "windows path kept as is") eq(pl.entries[1].playable, false, "windows path unplayable") eq(pl.entries[1].reason, "local path", "windows path reason") eq(pl.entries[2].reason, "local path", "drive with slash") eq(pl.entries[3].reason, "local path", "UNC") eq(pl.entries[4].url, "songs\\d.mp3", "relative without base kept") eq(pl.entries[4].reason, "relative path without a base URL", "relative without base reason") eq(pl.entries[5].reason, "scheme ftp", "ftp unplayable") eq(pl.entries[6].reason, "local path", "file: is local") eq(has(pl.warnings, "relative path without a base URL"), true, "warning for relative") eq(has(pl.warnings, "4 local paths"), true, "local paths summary") -- with a URL base, Windows separators in relative entries become slashes; drive paths stay local local pl2 = Playlist.parse(text, "http://h/x/list.m3u") eq(pl2.entries[4].url, "http://h/x/songs/d.mp3", "backslash relative resolved") eq(pl2.entries[4].playable, true, "backslash relative playable") eq(pl2.entries[1].url, "C:\\Music\\Song A.mp3", "windows path untouched with a base") -- with a Windows base, relative entries are paths next to the playlist local pl3 = Playlist.parse("a.mp3\nsub\\b.mp3", "C:\\Music\\list.m3u") eq(pl3.entries[1].url, "C:\\Music\\a.mp3", "windows base") eq(pl3.entries[2].url, "C:\\Music\\sub\\b.mp3", "windows base sub") eq(pl3.entries[1].playable, false, "windows base unplayable") -- file: base local pl4 = Playlist.parse("a.mp3", "file:///C:/Music/list.m3u") eq(pl4.entries[1].url, "file:///C:/Music/a.mp3", "file base") eq(pl4.entries[1].reason, "local path", "file base local") -- a relative base is not used local pl5 = Playlist.parse("a.mp3", "lists/x.m3u") eq(has(pl5.warnings, "not absolute"), true, "relative base warning") end -- Windows-1252 text (a classic .m3u), and UTF-8 kept do local pl = Playlist.parse("#EXTM3U\n#EXTINF:10,Bj\246rk \150 J\243ga\nhttp://x/j.mp3\n") eq(pl.entries[1].title, "Björk – Jóga", "cp1252 transcoded") eq(has(pl.warnings, "Windows%-1252"), true, "cp1252 warning") local pl2 = Playlist.parse("#EXTINF:10,Björk – Jóga\nhttp://x/j.mp3") eq(pl2.entries[1].title, "Björk – Jóga", "utf-8 kept") eq(#pl2.warnings, 0, "utf-8 no warning") local pl3 = Playlist.parse("http://x/Björk.mp3") eq(pl3.entries[1].url, "http://x/Bj%C3%B6rk.mp3", "utf-8 url encoded") end -- PLS do local text = "[playlist]\r\nNumberOfEntries=3\r\nFile2=http://s.example:8000/live\r\nTitle2=(#1) Some Radio\r\nLength2=-1\r\n" .. "File1=music/a.mp3\r\nTitle1=Artist - Song\r\nLength1=200\r\n; comment\r\nfile10 = http://x/z.mp3\r\nVersion=2\r\n" local pl = Playlist.parse(text, "http://h/p/list.pls") eq(pl.kind, "pls", "pls kind") eq(#pl.entries, 3, "pls count") eq(pl.entries[1].url, "http://h/p/music/a.mp3", "pls order by index + resolved") eq(pl.entries[1].title, "Artist - Song", "pls title") eq(pl.entries[1].duration, 200, "pls length") eq(pl.entries[2].url, "http://s.example:8000/live", "pls 2") eq(pl.entries[2].duration, nil, "pls length -1") eq(pl.entries[3].url, "http://x/z.mp3", "pls key case / spaces / index 10") eq(pl.entries[3].title, nil, "pls no title") eq(#pl.warnings, 0, "pls no warnings") local bad = Playlist.parse("[Playlist]\nNumberOfEntries=2\nFile1=http://a/1.mp3\n") eq(bad.kind, "pls", "pls case-insensitive header") eq(has(bad.warnings, "NumberOfEntries=2 but 1"), true, "pls count mismatch warning") end -- HLS master playlist: variants with attributes (quoted commas), EXT-X-MEDIA audio renditions do local text = [[#EXTM3U #EXT-X-VERSION:3 #EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="aud",NAME="English",LANGUAGE="en",URI="audio/en.m3u8" #EXT-X-STREAM-INF:BANDWIDTH=128000,CODECS="mp4a.40.2,avc1.4d401e",RESOLUTION=640x360,AUDIO="aud" low/index.m3u8 #EXT-X-STREAM-INF:BANDWIDTH=320000,CODECS="mp4a.40.34" http://other.example/hi/index.m3u8 ]] local pl = Playlist.parse(text, "https://cdn.example/show/master.m3u8") eq(pl.kind, "hls", "hls kind") assert(pl.hls) eq(pl.hls.master, true, "master") eq(#pl.entries, 3, "variants + rendition") eq(pl.entries[1].url, "https://cdn.example/show/audio/en.m3u8", "rendition url") eq(pl.entries[1].title, "English", "rendition name") eq(pl.entries[2].url, "https://cdn.example/show/low/index.m3u8", "variant 1 url") eq(pl.entries[2].bandwidth, 128000, "variant 1 bandwidth") eq(pl.entries[2].codecs, "mp4a.40.2,avc1.4d401e", "variant codecs keeps its comma") eq((pl.entries[2].attributes :: any).RESOLUTION, "640x360", "variant attributes") eq(pl.entries[3].bandwidth, 320000, "variant 2 bandwidth") eq(pl.entries[3].url, "http://other.example/hi/index.m3u8", "variant 2 absolute") eq(#pl.warnings, 0, "master no warnings") end -- HLS media playlist with mp3 segments (VOD) do local text = [[#EXTM3U #EXT-X-VERSION:3 #EXT-X-TARGETDURATION:10 #EXT-X-MEDIA-SEQUENCE:7 #EXTINF:10.0, seg7.mp3 #EXTINF:9.5,Part two seg8.mp3?token=a #EXT-X-BYTERANGE:1000@0 #EXTINF:4, all.mp3 #EXT-X-BYTERANGE:500 #EXTINF:2, all.mp3 #EXT-X-ENDLIST ]] local pl = Playlist.parse(text, "https://h/radio/list.m3u8") assert(pl.hls) eq(pl.kind, "hls", "media kind") eq(pl.hls.master, false, "media not master") eq(pl.hls.targetDuration, 10, "target duration") eq(pl.hls.mediaSequence, 7, "media sequence") eq(pl.hls.endList, true, "endlist") eq(pl.hls.totalDuration, 25.5, "total duration") eq(pl.hls.segmentFormat, "mp3", "segment format") eq(#pl.entries, 4, "segments") eq(pl.entries[1].url, "https://h/radio/seg7.mp3", "segment url") eq(pl.entries[1].duration, 10, "segment duration") eq(pl.entries[1].title, nil, "empty title is nil") eq(pl.entries[2].title, "Part two", "segment title") eq(pl.entries[2].url, "https://h/radio/seg8.mp3?token=a", "segment query") eq(pl.entries[2].playable, true, "mp3 segments playable") eq((pl.entries[3].byteRange :: any).length, 1000, "byterange length") eq((pl.entries[3].byteRange :: any).offset, 0, "byterange offset") eq((pl.entries[4].byteRange :: any).offset, 1000, "byterange continues") eq(#pl.warnings, 0, "mp3 VOD no warnings") end -- HLS media playlists with unsupported segments, live, encrypted, fMP4 do local ts = Playlist.parse("#EXTM3U\n#EXT-X-TARGETDURATION:6\n#EXTINF:6,\na.ts\n#EXTINF:6,\nb.ts\n", "http://h/x.m3u8") assert(ts.hls) eq(ts.entries[1].playable, false, "ts unplayable") eq(ts.entries[1].reason, "segment format ts", "ts reason") eq(ts.hls.segmentFormat, "ts", "ts format") eq(has(ts.warnings, "HLS segments are %.ts"), true, "ts warning") eq(has(ts.warnings, "live HLS"), true, "live warning") local aac = Playlist.parse("#EXTM3U\n#EXT-X-TARGETDURATION:6\n#EXTINF:6,\na.aac\n#EXTINF:6,\nb.mp3\n#EXT-X-ENDLIST\n", "http://h/x.m3u8") assert(aac.hls) eq(aac.hls.segmentFormat, "mixed", "mixed format") eq(has(aac.warnings, "%.aac"), true, "aac warning") eq(aac.entries[2].playable, true, "mp3 among aac still playable") local m4s = Playlist.parse("#EXTM3U\n#EXT-X-TARGETDURATION:4\n#EXT-X-MAP:URI=\"init.mp4\"\n#EXTINF:4,\n1.m4s\n#EXT-X-ENDLIST\n", "http://h/x.m3u8") eq(m4s.entries[1].playable, false, "m4s unplayable") eq(has(m4s.warnings, "%.m4s") and has(m4s.warnings, "fMP4"), true, "m4s + fmp4 warning") local enc = Playlist.parse("#EXTM3U\n#EXT-X-TARGETDURATION:4\n#EXT-X-KEY:METHOD=AES-128,URI=\"k\"\n#EXTINF:4,\n1.mp3\n#EXT-X-ENDLIST\n", "http://h/x.m3u8") assert(enc.hls) eq(enc.hls.encrypted, true, "encrypted") eq(enc.entries[1].playable, false, "encrypted unplayable") eq(has(enc.warnings, "encrypted"), true, "encrypted warning") local none = Playlist.parse("#EXTM3U\n#EXT-X-TARGETDURATION:4\n#EXT-X-KEY:METHOD=NONE\n#EXTINF:4,\n1.mp3\n#EXT-X-ENDLIST\n", "http://h/x.m3u8") eq(none.entries[1].playable, true, "METHOD=NONE is clear") end -- detection do eq(Playlist.detect("[playlist]\nFile1=a"), "pls", "detect pls") eq(Playlist.detect("\239\187\191 [PLAYLIST]\r\n"), "pls", "detect pls with BOM / spaces") eq(Playlist.detect("#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=1\na.m3u8"), "hls", "detect hls master") eq(Playlist.detect("#EXTM3U\n#EXT-X-TARGETDURATION:5\n"), "hls", "detect hls media") eq(Playlist.detect("#EXTM3U\n#EXTINF:1,a\na.mp3"), "m3u", "detect m3u") eq(Playlist.detect("a.mp3"), "m3u", "detect plain") end -- serialize: M3U8, round trip do local entries = { { url = "http://x/a.mp3", title = "A - B", duration = 61.6 }, { url = "http://x/b.mp3" }, { url = "http://x/c.mp3", title = "multi\nline" }, { url = "http://x/d.mp3", duration = 5 }, { title = "no url" }, } local text = Playlist.serialize(entries :: any, { title = "Mix" }) eq(text, "#EXTM3U\n#PLAYLIST:Mix\n#EXTINF:62,A - B\nhttp://x/a.mp3\nhttp://x/b.mp3\n#EXTINF:-1,multi line\nhttp://x/c.mp3\n" .. "#EXTINF:5,\nhttp://x/d.mp3\n", "serialize text") local back = Playlist.parse(text) eq(#back.entries, 4, "round trip count") eq(back.title, "Mix", "round trip title") eq(back.entries[1].title, "A - B", "round trip title 1") eq(back.entries[1].duration, 62, "round trip duration") eq(back.entries[2].title, nil, "round trip no title") eq(back.entries[3].duration, nil, "round trip unknown duration") eq(Playlist.serialize({}), "#EXTM3U\n", "serialize empty") -- parse -> serialize -> parse keeps everything of a real playlist local pl = Playlist.parse("#EXTM3U\n#EXTINF:100,X - Y\nhttp://h/1.mp3\n#EXTINF:-1,Live\nhttp://h/live\n") local again = Playlist.parse(Playlist.serialize(pl.entries)) for i, e in pl.entries do eq(again.entries[i].url, e.url, "rt url " .. i) eq(again.entries[i].title, e.title, "rt title " .. i) eq(again.entries[i].duration, e.duration, "rt duration " .. i) end end -- bad input never throws do local inputs: { any } = { "", "\n\n\n", "#EXTM3U", "#EXTINF:abc,\n", "[playlist]\n=\nFile=\nFilex1=y", "#EXT-X-STREAM-INF:\n", "#EXT-X-STREAM-INF:BANDWIDTH=\"unterminated\n", "#EXT-X-BYTERANGE:x\n#EXT-X-TARGETDURATION:\n", "\255\254\0\0", 42, nil } for i = 1, 11 do local ok, pl = pcall(Playlist.parse, inputs[i], "http://h/") eq(ok, true, "bad input " .. i .. " threw " .. tostring(pl)) eq(type(pl.entries), "table", "bad input " .. i .. " entries") end eq(#Playlist.parse("").entries, 0, "empty text") eq(has(Playlist.parse(42 :: any).warnings, "expected a string"), true, "non-string warning") local ok = pcall(Playlist.serialize, { 1, "x", { url = 5 } } :: any) eq(ok, true, "serialize skips junk") -- random bytes local state = 7 for _ = 1, 300 do local t = table.create(80) for k = 1, 80 do state = (state * 1103515245 + 12345) % 2147483648 t[k] = string.char(({ 10, 13, 35, 44, 58, 61, 34, 92, 47, 46, 65, 120, 200 })[state % 13 + 1]) end local s = "#EXTM3U\n#EXT-X-" .. table.concat(t) local ok2, err = pcall(Playlist.parse, s, "http://h/a/") eq(ok2, true, "fuzz threw " .. tostring(err)) local ok3, err3 = pcall(Playlist.parse, "[playlist]\n" .. table.concat(t), "http://h/a/") eq(ok3, true, "fuzz pls threw " .. tostring(err3)) end end -- Python-written playlists for _, c in Cases do local pl = M3u.decode(Bytes.reader(buffer.fromstring(c.m3u))) :: any eq(#pl.entries, #c.entries, c.name .. ": entries") for i, e in c.entries do local g = pl.entries[i] eq(g.url, e.url, c.name .. ": url " .. i) eq(g.duration, e.duration, c.name .. ": duration " .. i) if e.title then eq(g.title, e.title, c.name .. ": title " .. i) end end local back = M3u.decode(Bytes.reader(M3u.encode(pl, { title = c.title }) :: buffer)) :: any eq(back.title, c.title, c.name .. ": #PLAYLIST round trip") for i, e in pl.entries do eq(back.entries[i].url, e.url, c.name .. ": round trip url " .. i) eq(back.entries[i].title, e.title, c.name .. ": round trip title " .. i) end end t:note("%d Python-written M3U8 files decode to their entries", #Cases) -- the contract and the api do eq(M3u.name, "m3u", "name") eq(M3u.category, "playlist", "category") local c = Cases[1] eq(F.detect(c.m3u), "m3u", "F.detect") local pl = F.playlist.load(c.m3u, { baseUrl = "http://x/" }) t:check(pl and pl.kind == "playlist" and pl.format == "m3u" and #pl.entries == #c.entries, "F.playlist.load") local saved = F.save(pl, nil, { format = "m3u8" }) t:check(saved and string.sub(buffer.tostring(saved), 1, 7) == "#EXTM3U", "F.save m3u8") local hls = F.playlist.load("#EXTM3U\n#EXT-X-TARGETDURATION:4\n#EXTINF:4,\n1.mp3\n#EXT-X-ENDLIST\n", { baseUrl = "http://h/x.m3u8" }) t:check(hls and hls.format == "hls" and hls.hls.endList, "F.playlist.load HLS") t:eq(M3u.sniff(Bytes.reader(buffer.fromstring("\239\187\191 #EXTM3U\n"))), 0.95, "sniff with BOM") t:eq(M3u.sniff(Bytes.reader(buffer.fromstring("a.mp3\n"))), 0, "plain lists are found by extension") local _, e = M3u.encode(5 :: any) t:check(e ~= nil, "encode a number") end local samples = {} for _, c in Cases do table.insert(samples, c.m3u) end table.insert(samples, "#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=128000,CODECS=\"mp4a.40.2\"\nlo.m3u8\n#EXT-X-MEDIA:TYPE=AUDIO,URI=\"a.m3u8\",NAME=\"x\"\n") table.insert(samples, "#EXTM3U\n#EXT-X-TARGETDURATION:6\n#EXT-X-BYTERANGE:100@0\n#EXTINF:6,\na.mp3\n#EXT-X-KEY:METHOD=AES-128\n#EXT-X-ENDLIST\n") t:fuzz("decode", function(b: buffer) return M3u.decode(Bytes.reader(b), { baseUrl = "http://h/a/" }) end, samples, { mutations = 20 }) t:done()