forked from rofafor/vdr-plugin-iptv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotocolyt.cpp
158 lines (120 loc) · 4.14 KB
/
protocolyt.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <fstream>
#include "protocolyt.h"
#include "common.h"
#include "config.h"
#include "log.h"
cIptvProtocolYT::cIptvProtocolYT() : isActiveM(false), handler("M3U") {
debug1("%s", __PRETTY_FUNCTION__);
}
cIptvProtocolYT::~cIptvProtocolYT() {
debug1("%s", __PRETTY_FUNCTION__);
// Drop open handles
cIptvProtocolYT::Close();
handler.stop();
}
int cIptvProtocolYT::Read(unsigned char *bufferAddrP, unsigned int bufferLenP) {
// debug16("%s (, %u)", __PRETTY_FUNCTION__, bufferLenP);
return handler.popPackets(bufferAddrP, bufferLenP);
}
bool cIptvProtocolYT::Open() {
debug1("%s", __PRETTY_FUNCTION__);
if (!isActiveM) {
isActiveM = true;
auto streams = m3u8Handler.parseYT(url);
if (streams.width==0 || streams.height==0) {
mark404Channel(channelId);
cString errmsg = cString::sprintf("Unable to load stream with URL %s", url.c_str());
debug1("%s", *errmsg);
errmsg = cString::sprintf(tr("Unable to load stream"));
Skins.Message(mtError, errmsg);
isActiveM = false;
return false;
}
{
LOCK_CHANNELS_READ;
const cChannel *Channel = Channels->GetByNumber(channelId);
if (Channel) {
streams.channelName = Channel->Name();
streams.vpid = Channel->Vpid();
streams.spid = Channel->Sid();
streams.tpid = Channel->Tid();
streams.nid = Channel->Nid();
int aidx = 0;
while (true) {
int apid = Channel->Apid(aidx);
if (apid==0) {
break;
}
streams.apids.push_back(apid);
aidx++;
}
}
}
handler.setChannelId(channelId);
handler.setHandlerType(handlerType);
m3u8Handler.printStream(streams);
handler.streamVideo(streams);
}
return true;
}
bool cIptvProtocolYT::Close() {
debug1("%s", __PRETTY_FUNCTION__);
isActiveM = false;
handler.stop();
return true;
}
bool
cIptvProtocolYT::SetSource(SourceParameter parameter) {
debug1("%s (%s, %d, %d)", __PRETTY_FUNCTION__, parameter.locationP, parameter.parameterP, parameter.indexP);
url = ReplaceAll(parameter.locationP, "%3A", ":");
url = ReplaceAll(url, "%7C", "|");
if (url.empty()) {
return false;
}
channelId = parameter.channelNumber;
handlerType = parameter.handlerType;
return true;
}
std::string cIptvProtocolYT::findUrl(int parameterP, const char* locationP) {
std::string m3uUrl;
struct stat stbuf;
cString configFileM = cString::sprintf("%s/%s", IptvConfig.GetM3uCfgPath(), locationP);
if ((stat(*configFileM, &stbuf)!=0) || (strstr(*configFileM, "..") != nullptr)) {
error("Non-existent or relative configuration file '%s'", *configFileM);
return "";
}
std::ifstream file(configFileM);
if (file.is_open()) {
std::string line;
while (std::getline(file, line)) {
unsigned long idx = line.find(':');
if (idx > 0) {
std::string nr = line.substr(0, idx);
if (nr==std::to_string(parameterP)) {
m3uUrl = line.substr(idx + 1);
if (m3uUrl.empty()) {
error("URL with index %d in file %s not found", parameterP, *configFileM);
file.close();
return "";
}
debug1("Found URL %s", m3uUrl.c_str());
break;
}
}
}
file.close();
}
if (m3uUrl.empty()) {
error("URL with index %d in file %s not found", parameterP, *configFileM);
return "";
}
return m3uUrl;
}
bool cIptvProtocolYT::SetPid(int pidP, int typeP, bool onP) {
debug16("%s (%d, %d, %d)", __PRETTY_FUNCTION__, pidP, typeP, onP);
return true;
}
cString cIptvProtocolYT::GetInformation() {
debug16("%s", __PRETTY_FUNCTION__);
return cString::sprintf("%s", url.c_str());
}