20 #include "spdlog/spdlog.h"
21 #include "boost/algorithm/string.hpp"
27 spdlog::debug(
"Parsing HLS primary playlist: {}", content);
29 std::istringstream iss(content);
31 std::string resolution =
"";
32 std::string codecs =
"";
33 unsigned long bandwidth = 0;
34 double frame_rate = 0;
35 for (std::string line; std::getline(iss, line); idx++ )
38 boost::algorithm::trim(line);
41 if ( line !=
"#EXTM3U") {
42 throw(
"HLS playlist parsing failed: first line is not #EXTM3U");
48 size_t cpos = line.rfind(
':');
50 if (line.rfind(
"#EXT-X-VERSION", 0) == 0) {
52 throw(
"HLS playlist parsing failed: duplicate #EXT-X-VERSION");
54 if (cpos != std::string::npos) {
55 _version = atoi(line.substr(cpos+1).c_str());
57 }
else if (line.rfind(
"#EXT-X-STREAM-INF", 0) == 0) {
58 if (cpos != std::string::npos) {
59 std::string info = line.substr(cpos+1);
61 for (
const auto& param : params) {
62 if (param.first ==
"BANDWIDTH") {
63 bandwidth = std::atoi(param.second.c_str());
65 else if (param.first ==
"FRAME-RATE") {
66 frame_rate = std::atof(param.second.c_str());
68 else if (param.first ==
"RESOLUTION") {
69 resolution = param.second;
71 else if (param.first ==
"CODECS") {
72 codecs = param.second;
76 }
else if (line.rfind(
'#', 0) == 0) {
77 spdlog::debug(
"HLS playlist parser ignoring unhandled line {}", line);
78 }
else if (line.size() > 0) {
79 std::string uri = base_path + line;
80 _streams.push_back({uri, resolution, codecs, bandwidth, frame_rate});
81 resolution =
""; codecs =
""; bandwidth = 0; frame_rate = 0;
88 std::vector<std::pair<std::string, std::string>> result;
91 std::stringstream key;
92 std::stringstream value;
93 for(
const char& c : line) {
96 }
else if (c ==
',' && !quoted) {
97 result.emplace_back(std::make_pair(key.str(), value.str()));
98 key.str(
""); key.clear();
99 value.str(
""); value.clear();
101 }
else if (c ==
'=') {
111 result.emplace_back(std::make_pair(key.str(), value.str()));
117 std::stringstream pl;
119 pl <<
"#EXTM3U" << std::endl;
120 pl <<
"#EXT-X-VERSION:3" << std::endl;
122 for (
const auto& s : _streams) {
123 pl <<
"#EXT-X-STREAM-INF:BANDWITH=" << s.bandwidth <<
124 ",RESOLUTION=" << s.resolution <<
125 ",FRAME-RATE=" << std::fixed << std::setprecision(3) << s.frame_rate <<
126 ",CODECS=\"" << s.codecs <<
"\"" << std::endl;
127 pl << s.uri << std::endl;
129 return std::move(pl.str());
std::vector< std::pair< std::string, std::string > > parse_parameters(const std::string &line) const
HlsPrimaryPlaylist()=default
std::vector< Stream > _streams
std::string to_string() const