20 #include "spdlog/spdlog.h"
21 #include "boost/algorithm/string.hpp"
26 spdlog::debug(
"Parsing HLS media playlist: {}", content);
28 std::istringstream iss(content);
32 for (std::string line; std::getline(iss, line); idx++ )
34 boost::algorithm::trim(line);
37 if ( line !=
"#EXTM3U") {
38 throw(
"HLS playlist parsing failed: first line is not #EXTM3U");
44 size_t cpos = line.rfind(
':');
46 if (line.rfind(
"#EXT-X-VERSION", 0) == 0) {
48 throw(
"HLS playlist parsing failed: duplicate #EXT-X-VERSION");
50 if (cpos != std::string::npos) {
51 _version = atoi(line.substr(cpos+1).c_str());
53 }
else if (line.rfind(
"#EXT-X-TARGETDURATION", 0) == 0) {
54 if (cpos != std::string::npos) {
57 }
else if (line.rfind(
"#EXT-X-MEDIA-SEQUENCE", 0) == 0) {
58 if (cpos != std::string::npos) {
59 seq_nr = atoi(line.substr(cpos+1).c_str());
61 }
else if (line.rfind(
"#EXTINF", 0) == 0) {
62 if (cpos != std::string::npos) {
63 extinf = atof(line.substr(cpos+1).c_str());
65 }
else if (line.rfind(
'#', 0) == 0) {
66 spdlog::debug(
"HLS playlist parser ignoring unhandled line {}", line);
67 }
else if (line.size() > 0) {
68 _segments.push_back({line, seq_nr++, extinf});
78 pl <<
"#EXTM3U" << std::endl;
79 pl <<
"#EXT-X-VERSION:3" << std::endl;
81 if (_segments.size() > 0) {
82 pl <<
"#EXT-X-TARGETDURATION:" << _targetduration << std::endl;
83 pl <<
"#EXT-X-MEDIA-SEQUENCE:" << _segments[0].seq << std::endl;
85 for (
const auto& seg : _segments) {
86 pl <<
"#EXTINF:" << seg.extinf << std::endl;
87 pl <<
"/" << seg.uri << std::endl;
89 return std::move(pl.str());