-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm3u8handler.cpp
138 lines (113 loc) · 4.38 KB
/
m3u8handler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "m3u8handler.h"
#include "logger.h"
M3u8Handler::M3u8Handler(std::string uri) : webUri(uri) {
}
bool M3u8Handler::startsWith(const std::string& str, const std::string& prefix) {
return str.size() >= prefix.size() && 0 == str.compare(0, prefix.size(), prefix);
}
std::vector<std::string> M3u8Handler::split(const std::string &s, char delim) {
std::vector<std::string> result;
std::stringstream ss (s);
std::string item;
while (getline (ss, item, delim)) {
result.push_back (item);
}
return result;
}
m3u_stream M3u8Handler::parseM3u() {
DEBUG("Load file " + webUri);
auto uri = uri::parse_uri(webUri);
httplib::Client cli(uri.scheme + "://" + uri.authority.host + (uri.authority.port > 0 ? std::to_string(uri.authority.port) : ""));
auto res = cli.Get(uri.path);
m3u_stream result;
result.width = result.height = 0;
if (res == nullptr) {
return result;
}
if (res->status != 200) {
return result;
}
std::string m3u = res->body;
std::istringstream stream(m3u);
std::string line;
int maxW = 0, maxH = 0;
std::string audioGroup;
std::string m3uMax;
std::string starterStream = "#EXT-X-STREAM-INF:";
std::string starterMedia = "#EXT-X-MEDIA:";
std::string starterIgnore = "#EXTINF:";
while (std::getline(stream, line)) {
if (startsWith(line, starterStream)) {
auto splitted = split(line.substr(starterStream.length()), ',');
for (const auto& t : splitted) {
if (startsWith(t, "RESOLUTION=")) {
int w, h;
sscanf(t.c_str() + 11, "%dx%d", &w, &h);
if (w > maxW) {
maxW = w;
maxH = h;
std::getline(stream, m3uMax);
if (!startsWith(m3uMax, "http://") && !startsWith(m3uMax, "https://")) {
// this is a relative URL -> construct absolute URL
auto last = webUri.find_last_of('/');
m3uMax = webUri.substr(0, last+1).append(m3uMax);
}
}
} else if (startsWith(t, "AUDIO=")) {
audioGroup = t.substr(7, t.length()-8);
}
}
} else if (startsWith(line, starterMedia)) {
auto splitted = split(line.substr(starterMedia.length()), ',');
bool addThis = true;
media m;
for (const auto& t : splitted) {
if (startsWith(t, "TYPE=")) {
m.type = t.substr(5);
if (m.type != "AUDIO") {
// no audio -> skip
addThis = false;
break;
}
} else if (startsWith(t, "LANGUAGE=")) {
m.language = t.substr(10, t.length()-11);
} else if (startsWith(t, "NAME=")) {
m.name = t.substr(6, t.length()-7);
} else if (startsWith(t, "GROUP-ID=")) {
m.groupId = t.substr(10, t.length()-11);
} else if (startsWith(t, "URI=")) {
m.uri = t.substr(5, t.length()-6);
}
}
if (addThis) {
if (!startsWith(m.uri, "http://") && !startsWith(m.uri, "https://")) {
// this is a relative URL -> construct absolute URL
auto last = webUri.find_last_of('/');
m.uri = webUri.substr(0, last+1).append(m.uri);
}
result.audio.push_back(m);
}
} else if (startsWith(line, starterIgnore)) {
// this is already a usable m3u8. Don't process anymore but return a useful result
result.width = 1920;
result.height = 1080;
result.url = webUri;
return result;
}
}
// remove all unwanted audio streams with wrong group-id
if (!audioGroup.empty() && !result.audio.empty()) {
auto it = result.audio.begin();
while(it != result.audio.end()) {
if(it->groupId != audioGroup) {
it = result.audio.erase(it);
} else {
++it;
}
}
}
result.width = maxW;
result.height = maxH;
result.url = m3uMax;
return result;
}