5G-MAG Reference Tools - MBMS Middleware
Classes | Public Member Functions | Private Member Functions | Private Attributes | List of all members
MBMS_RT::HlsPrimaryPlaylist Class Reference

#include <HlsPrimaryPlaylist.h>

Collaboration diagram for MBMS_RT::HlsPrimaryPlaylist:
Collaboration graph

Classes

struct  Stream
 

Public Member Functions

 HlsPrimaryPlaylist (const std::string &content, const std::string &base_path)
 
 HlsPrimaryPlaylist ()=default
 
 ~HlsPrimaryPlaylist ()=default
 
const std::vector< Stream > & streams () const
 
void add_stream (Stream stream)
 
std::string to_string () const
 

Private Member Functions

std::vector< std::pair< std::string, std::string > > parse_parameters (const std::string &line) const
 

Private Attributes

int _version = -1
 
std::vector< Stream_streams = {}
 

Detailed Description

Definition at line 24 of file HlsPrimaryPlaylist.h.

Constructor & Destructor Documentation

◆ HlsPrimaryPlaylist() [1/2]

MBMS_RT::HlsPrimaryPlaylist::HlsPrimaryPlaylist ( const std::string &  content,
const std::string &  base_path 
)

Definition at line 25 of file HlsPrimaryPlaylist.cpp.

26 {
27  spdlog::debug("Parsing HLS primary playlist: {}", content);
28 
29  std::istringstream iss(content);
30  int idx = 0;
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++ )
36  {
37 
38  boost::algorithm::trim(line);
39 
40  if (idx==0) {
41  if ( line != "#EXTM3U") {
42  throw("HLS playlist parsing failed: first line is not #EXTM3U");
43  } else {
44  continue;
45  }
46  }
47 
48  size_t cpos = line.rfind(':');
49 
50  if (line.rfind("#EXT-X-VERSION", 0) == 0) {
51  if (_version != -1) {
52  throw("HLS playlist parsing failed: duplicate #EXT-X-VERSION");
53  }
54  if (cpos != std::string::npos) {
55  _version = atoi(line.substr(cpos+1).c_str());
56  }
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);
60  auto params = parse_parameters(info);
61  for (const auto& param : params) {
62  if (param.first == "BANDWIDTH") {
63  bandwidth = std::atoi(param.second.c_str());
64  }
65  else if (param.first == "FRAME-RATE") {
66  frame_rate = std::atof(param.second.c_str());
67  }
68  else if (param.first == "RESOLUTION") {
69  resolution = param.second;
70  }
71  else if (param.first == "CODECS") {
72  codecs = param.second;
73  }
74  }
75  }
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;
82  }
83  }
84 }
std::vector< std::pair< std::string, std::string > > parse_parameters(const std::string &line) const
std::vector< Stream > _streams

◆ HlsPrimaryPlaylist() [2/2]

MBMS_RT::HlsPrimaryPlaylist::HlsPrimaryPlaylist ( )
default

◆ ~HlsPrimaryPlaylist()

MBMS_RT::HlsPrimaryPlaylist::~HlsPrimaryPlaylist ( )
default

Member Function Documentation

◆ add_stream()

void MBMS_RT::HlsPrimaryPlaylist::add_stream ( Stream  stream)
inline

Definition at line 38 of file HlsPrimaryPlaylist.h.

38 { _streams.push_back(std::move(stream)); };

◆ parse_parameters()

auto MBMS_RT::HlsPrimaryPlaylist::parse_parameters ( const std::string &  line) const
private

Definition at line 86 of file HlsPrimaryPlaylist.cpp.

87 {
88  std::vector<std::pair<std::string, std::string>> result;
89  bool quoted = false;
90  bool in_key = true;
91  std::stringstream key;
92  std::stringstream value;
93  for(const char& c : line) {
94  if (c == '"') {
95  quoted = !quoted;
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();
100  in_key = true;
101  } else if (c == '=') {
102  in_key = false;
103  } else {
104  if (in_key) {
105  key << c;
106  } else {
107  value << c;
108  }
109  }
110  }
111  result.emplace_back(std::make_pair(key.str(), value.str()));
112  return result;
113 }

◆ streams()

const std::vector<Stream>& MBMS_RT::HlsPrimaryPlaylist::streams ( ) const
inline

Definition at line 37 of file HlsPrimaryPlaylist.h.

37 { return _streams; };

◆ to_string()

auto MBMS_RT::HlsPrimaryPlaylist::to_string ( ) const

Definition at line 115 of file HlsPrimaryPlaylist.cpp.

116 {
117  std::stringstream pl;
118 
119  pl << "#EXTM3U" << std::endl;
120  pl << "#EXT-X-VERSION:3" << std::endl;
121 
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;
128  }
129  return std::move(pl.str());
130 }

Member Data Documentation

◆ _streams

std::vector<Stream> MBMS_RT::HlsPrimaryPlaylist::_streams = {}
private

Definition at line 44 of file HlsPrimaryPlaylist.h.

◆ _version

int MBMS_RT::HlsPrimaryPlaylist::_version = -1
private

Definition at line 43 of file HlsPrimaryPlaylist.h.


The documentation for this class was generated from the following files: