-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathr_dash2ts.cpp
More file actions
164 lines (128 loc) · 4.71 KB
/
Copy pathr_dash2ts.cpp
File metadata and controls
164 lines (128 loc) · 4.71 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <unistd.h>
#include <iomanip>
#include "streamplayer.h"
#include "addonhandler.h"
#include "logger.h"
extern bool saveonly;
extern bool use_TCP;
extern std::string headers;
std::string &Trim(std::string &, const char *const);
std::string urlEncode(const std::string& str) {
std::ostringstream encodedStream;
encodedStream << std::hex << std::uppercase << std::setfill('0');
for (char c : str) {
if (std::isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
encodedStream << c;
} else {
encodedStream << '%' << std::setw(2) << static_cast<unsigned int>(static_cast<unsigned char>(c));
}
}
return encodedStream.str();
}
void usage() {
printf("Usage: r_dash2ts -u url_to_manifest.mpd\n");
printf(" -a user_agent\n");
printf(" -c cookies\n");
printf(" -r referer\n");
printf(" -k path_to_kodi\n");
printf(" -p probeFfmpeg only\n");
printf(" -s start position in seconds\n");
printf(" [-l <level>] \n");
}
int main(int argc, char *argv[]) {
std::string url;
std::string path_to_kodi;
std::string user_agent;
std::string cookies;
std::string referer;
int api_version = 0;
int loglevel = 1;
bool probeOnly = false;
int startPosition;
use_TCP = false;
int c;
while ((c = getopt(argc, argv, "u:a:c:r:k:l:ps:")) != -1) {
switch (c) {
case 'u': // URL to Manifest
url = std::string(optarg);
continue;
case 'a': // user agent
user_agent = std::string(optarg);
continue;
case 'c': // cookies
cookies = std::string(optarg);
continue;
case 'r': // referer
referer = std::string(optarg);
continue;
case 'k': // path to kodi
path_to_kodi = std::string(optarg);
continue;
case 'l': // loglevel
loglevel = atoi(optarg);
continue;
case 'p': // probe only
probeOnly = true;
continue;
case 's': // start position
startPosition = atoi(optarg);
continue;
default:
usage();
exit(EXIT_FAILURE);
}
break;
}
if (path_to_kodi.empty()) {
// set default for VDR*ELEC
path_to_kodi = "/storage/.kodi";
}
if (url.empty() || user_agent.empty() || referer.empty()) {
usage();
exit(EXIT_FAILURE);
}
switch (loglevel) {
case 0: logger.set_level(spdlog::level::critical); break;
case 1: logger.set_level(spdlog::level::err); break;
case 2: logger.set_level(spdlog::level::info); break;
case 3: logger.set_level(spdlog::level::debug); break;
case 4: logger.set_level(spdlog::level::trace); break;
default: logger.set_level(spdlog::level::err); break;
}
auto handler = new AddonHandler(path_to_kodi); // Init AddonHandler
// Load the inputstream.adaptive Library and get the API Version
api_version = handler->LoadAddon();
INFO("Found inputstream.adaptive Library, version {}", api_version);
std::string httpHeader = "Referer=" + referer + "&User-Agent=" + user_agent;
if (!cookies.empty()) {
httpHeader += "&cookie=" + urlEncode(cookies);
}
// kodi 22+
handler->AddProp("inputstream.adaptive.common_headers", httpHeader.c_str());
// kodi 20+
// handler->AddProp("inputstream.adaptive.stream_params", "paramname=value¶mname2=value2");
handler->AddProp("inputstream.adaptive.manifest_headers", httpHeader.c_str());
// kodi 21+
handler->AddProp("inputstream.adaptive.stream_headers", httpHeader.c_str());
// all
handler->AddProp("inputstream.adaptive.config", "{\"internal_cookies\":true}");
handler->SetResolution(1920, 1080);
bool ret = handler->OpenURL(const_cast<char *>(url.c_str()));
if (ret) {
INFO("Open URL {}");
if (probeOnly) {
// print result and exit
printf("dash2ts result:%d:%d:%d:%ld:%ld:%d,\n", handler->GetTotalTime(0, true), handler->GetTime(), handler->GetCapabilities(), handler->PositionStream(), handler->LengthStream(), handler->IsRealTimeStream() ? 1 : 0);
exit(EXIT_SUCCESS);
}
} else {
ERROR("Unable to open URL {}", url);
exit(EXIT_FAILURE);
}
// start streaming
auto player = new StreamPlayer(startPosition);
player->StreamPlay(handler);
delete handler;
delete player;
exit(EXIT_SUCCESS);
}