rutgersc / m3u8-rs

m3u8 parser for rust
MIT License
100 stars 27 forks source link

Blank line turns a master playlist into a media playlist #17

Closed thaytan closed 3 years ago

thaytan commented 3 years ago

I found a playlist that is mistakenly parsed as a media playlist instead of a master playlist. It turns out a blank line after the initial #EXTM3U tag is the trigger.

e.g.

#EXTM3U

#EXT-X-STREAM-INF:BANDWIDTH=1280000,AVERAGE-BANDWIDTH=1000000
http://example.com/low.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=2560000,AVERAGE-BANDWIDTH=2000000
http://example.com/mid.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=7680000,AVERAGE-BANDWIDTH=6000000
http://example.com/hi.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=65000,CODECS="mp4a.40.5"
http://example.com/audio-only.m3u8

will be parsed incorrectly. Removing the blank line makes it parse correctly.

thaytan commented 3 years ago

The problem seems to be related to how is_master_playlist_tag_line() handles empty lines. Parsing stops at the first blank line, so as long as the first master playlist tag appears before a blank line, the file will be correctly detected as a master playlist.

thaytan commented 3 years ago

This fix makes it work for me. I don't know if it's correct - I've never used nom before.

rutgersc commented 3 years ago

Nice find! Looks like it also fails when there's whitespace before the first line, which could also be something to fix maybe. Your fix looks good though, just merged it 👍

thaytan commented 3 years ago

https://tools.ietf.org/html/rfc8216#section-4.3.1.1 - The EXTM3U tag MUST be the first line of every Media Playlist and every Master Playlist.