forked from BtbN/vlc-htsp-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiscovery.cpp
339 lines (272 loc) · 9.5 KB
/
discovery.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*****************************************************************************
* Copyright (C) 2012
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#define __STDC_CONSTANT_MACROS 1
#include <functional>
#include <unordered_map>
#include <sstream>
#include "discovery.h"
#include "helper.h"
#include "htsmessage.h"
#include "sha1.h"
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_network.h>
#include <vlc_services_discovery.h>
struct tmp_channel
{
std::string name;
uint32_t cid;
uint32_t cnum;
std::string url;
std::string cicon;
input_item_t *item;
std::list<std::string> tags;
};
struct services_discovery_sys_t : public sys_common_t
{
services_discovery_sys_t()
:thread(0)
,disconnect(false)
{}
vlc_thread_t thread;
std::unordered_map<uint32_t, tmp_channel> channelMap;
bool disconnect;
};
bool ConnectSD(services_discovery_t *sd)
{
services_discovery_sys_t *sys = sd->p_sys;
char *host = var_GetString(sd, CFG_PREFIX"host");
int port = var_GetInteger(sd, CFG_PREFIX"port");
sys->disconnect = var_GetBool(sd, CFG_PREFIX"disconnect");
if(port == 0)
port = 9982;
if(host == 0 || host[0] == 0)
sys->netfd = net_ConnectTCP(sd, "localhost", port);
else
sys->netfd = net_ConnectTCP(sd, host, port);
if(host)
free(host);
if(sys->netfd < 0)
{
msg_Err(sd, "net_ConnectTCP failed");
return false;
}
HtsMap map;
map.setData("method", "hello");
map.setData("clientname", "VLC media player");
map.setData("htspversion", HTSP_PROTO_VERSION);
HtsMessage m = ReadResult(sd, sys, map.makeMsg());
if(!m.isValid())
{
msg_Err(sd, "No valid hello response");
return false;
}
uint32_t chall_len;
void * chall;
m.getRoot()->getBin("challenge", &chall_len, &chall);
std::string serverName = m.getRoot()->getStr("servername");
std::string serverVersion = m.getRoot()->getStr("serverversion");
uint32_t protoVersion = m.getRoot()->getU32("htspversion");
msg_Info(sd, "Connected to HTSP Server %s, version %s, protocol %d", serverName.c_str(), serverVersion.c_str(), protoVersion);
if(protoVersion < HTSP_PROTO_VERSION)
{
msg_Warn(sd, "TVHeadend is running an older version of HTSP(v%d) than we are(v%d). No effort was made to keep compatible with older versions, update tvh before reporting problems!", protoVersion, HTSP_PROTO_VERSION);
}
else if(protoVersion > HTSP_PROTO_VERSION)
{
msg_Info(sd, "TVHeadend is running a more recent version of HTSP(v%d) than we are(v%d). Check if there is an update available!", protoVersion, HTSP_PROTO_VERSION);
}
char *user = var_GetString(sd, CFG_PREFIX"user");
char *pass = var_GetString(sd, CFG_PREFIX"pass");
if(user == 0 || user[0] == 0)
return true;
map = HtsMap();
map.setData("method", "authenticate");
map.setData("username", user);
if(pass != 0 && pass[0] != 0 && chall)
{
msg_Info(sd, "Authenticating as '%s' with a password", user);
HTSSHA1 *shactx = (HTSSHA1*)malloc(hts_sha1_size);
uint8_t d[20];
hts_sha1_init(shactx);
hts_sha1_update(shactx, (const uint8_t *)pass, strlen(pass));
hts_sha1_update(shactx, (const uint8_t *)chall, chall_len);
hts_sha1_final(shactx, d);
std::shared_ptr<HtsBin> bin = std::make_shared<HtsBin>();
bin->setBin(20, d);
map.setData("digest", bin);
free(shactx);
}
else
msg_Info(sd, "Authenticating as '%s' without a password", user);
if(user)
free(user);
if(pass)
free(pass);
if(chall)
free(chall);
bool res = ReadSuccess(sd, sys, map.makeMsg(), "authenticate");
if(res)
msg_Info(sd, "Successfully authenticated!");
else
msg_Err(sd, "Authentication failed!");
return res;
}
bool GetChannels(services_discovery_t *sd)
{
services_discovery_sys_t *sys = sd->p_sys;
HtsMap map;
map.setData("method", "enableAsyncMetadata");
if(!ReadSuccess(sd, sys, map.makeMsg(), "enable async metadata"))
return false;
std::list<uint32_t> channelIds;
std::unordered_map<uint32_t, tmp_channel> channels;
HtsMessage m;
while((m = ReadMessage(sd, sys)).isValid())
{
std::string method = m.getRoot()->getStr("method");
if(method.empty() || method == "initialSyncCompleted")
{
msg_Info(sd, "Finished getting initial metadata sync");
break;
}
if(method == "channelAdd")
{
if(!m.getRoot()->contains("channelId"))
continue;
uint32_t cid = m.getRoot()->getU32("channelId");
std::string cname = m.getRoot()->getStr("channelName");
if(cname.empty())
{
std::ostringstream ss;
ss << "Channel " << cid;
cname = ss.str();
}
uint32_t cnum = m.getRoot()->getU32("channelNumber");
std::string cicon = m.getRoot()->getStr("channelIcon");
std::ostringstream oss;
oss << "htsp://";
char *user = var_GetString(sd, CFG_PREFIX"user");
char *pass = var_GetString(sd, CFG_PREFIX"pass");
if(user != 0 && user[0] != 0 && pass != 0 && pass[0] != 0)
oss << user << ":" << pass << "@";
else if(user != 0 && user[0] != 0)
oss << user << "@";
char *_host = var_GetString(sd, CFG_PREFIX"host");
const char *host = _host;
if(host == 0 || host[0] == 0)
host = "localhost";
int port = var_GetInteger(sd, CFG_PREFIX"port");
if(port == 0)
port = 9982;
oss << host << ":" << port << "/" << cid;
channels[cid].name = cname;
channels[cid].cid = cid;
channels[cid].cnum = cnum;
channels[cid].cicon = cicon;
channels[cid].url = oss.str();
channelIds.push_back(cid);
if(user)
free(user);
if(pass)
free(pass);
if(_host)
free(_host);
}
else if(method == "tagAdd" || method == "tagUpdate")
{
if(!m.getRoot()->contains("tagId") || !m.getRoot()->contains("tagName"))
continue;
std::string tagName = m.getRoot()->getStr("tagName");
std::shared_ptr<HtsList> chList = m.getRoot()->getList("members");
for(uint32_t i = 0; i < chList->count(); ++i)
channels[chList->getData(i)->getU32()].tags.push_back(tagName);
}
}
channelIds.sort([&](const uint32_t &first, const uint32_t &second) {
return channels[first].cnum < channels[second].cnum;
});
while(channelIds.size() > 0)
{
tmp_channel ch = channels[channelIds.front()];
channelIds.pop_front();
ch.item = input_item_New(ch.url.c_str(), ch.name.c_str());
if(unlikely(ch.item == 0))
return false;
input_item_SetArtworkURL(ch.item, ch.cicon.c_str());
ch.item->i_type = ITEM_TYPE_STREAM;
for(std::string tag: ch.tags)
services_discovery_AddItem(sd, ch.item, tag.c_str());
services_discovery_AddItem(sd, ch.item, "All Channels");
sys->channelMap[ch.cid] = ch;
}
return true;
}
void * RunSD(void *obj)
{
services_discovery_t *sd = (services_discovery_t *)obj;
services_discovery_sys_t *sys = sd->p_sys;
if(!ConnectSD(sd))
{
msg_Err(sd, "Connecting to HTS Failed!");
return 0;
}
GetChannels(sd);
while(!sys->disconnect)
{
HtsMessage msg = ReadMessage(sd, sys);
if(!msg.isValid())
break;
std::string method = msg.getRoot()->getStr("method");
if(method.empty())
break;
msg_Dbg(sd, "Got Message with method %s", method.c_str());
}
net_Close(sys->netfd);
return 0;
}
int OpenSD(vlc_object_t *obj)
{
services_discovery_t *sd = (services_discovery_t *)obj;
services_discovery_sys_t *sys = new services_discovery_sys_t;
if(unlikely(sys == NULL))
return VLC_ENOMEM;
sd->p_sys = sys;
config_ChainParse(sd, CFG_PREFIX, cfg_options, sd->p_cfg);
if(vlc_clone(&sys->thread, RunSD, sd, VLC_THREAD_PRIORITY_LOW))
{
delete sys;
return VLC_EGENERIC;
}
return VLC_SUCCESS;
}
void CloseSD(vlc_object_t *obj)
{
services_discovery_t *sd = (services_discovery_t *)obj;
services_discovery_sys_t *sys = sd->p_sys;
if(!sys)
return;
if(sys->thread)
{
vlc_cancel(sys->thread);
vlc_join(sys->thread, 0);
sys->thread = 0;
}
delete sys;
sys = sd->p_sys = 0;
}