forked from tvheadend/tvheadend
/
epggrab.c
366 lines (325 loc) · 9.23 KB
/
epggrab.c
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* Electronic Program Guide - Common functions
* Copyright (C) 2012 Adam Sutton
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <wordexp.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/socket.h>
#include "htsmsg.h"
#include "settings.h"
#include "tvheadend.h"
#include "queue.h"
#include "epg.h"
#include "epggrab.h"
#include "epggrab/private.h"
#include "channels.h"
#include "spawn.h"
#include "htsmsg_xml.h"
#include "file.h"
#include "service.h"
/* Thread protection */
int epggrab_confver;
pthread_mutex_t epggrab_mutex;
pthread_cond_t epggrab_cond;
/* Config */
uint32_t epggrab_interval;
epggrab_module_int_t* epggrab_module;
epggrab_module_list_t epggrab_modules;
uint32_t epggrab_channel_rename;
uint32_t epggrab_channel_renumber;
uint32_t epggrab_channel_reicon;
/* **************************************************************************
* Internal Grab Thread
* *************************************************************************/
/*
* Grab from module
*/
static void _epggrab_module_grab ( epggrab_module_int_t *mod )
{
time_t tm1, tm2;
htsmsg_t *data;
/* Grab */
time(&tm1);
data = mod->trans(mod, mod->grab(mod));
time(&tm2);
/* Process */
if ( data ) {
tvhlog(LOG_INFO, mod->id, "grab took %d seconds", tm2 - tm1);
epggrab_module_parse(mod, data);
} else {
tvhlog(LOG_WARNING, mod->id, "grab returned no data");
}
}
/*
* Thread (for internal grabbing)
*/
static void* _epggrab_internal_thread ( void* p )
{
int err, confver = -1; // force first run
struct timespec ts;
epggrab_module_int_t *mod;
/* Setup timeout */
ts.tv_nsec = 0;
time(&ts.tv_sec);
while ( 1 ) {
/* Check for config change */
pthread_mutex_lock(&epggrab_mutex);
while ( confver == epggrab_confver ) {
if (epggrab_module) {
err = pthread_cond_timedwait(&epggrab_cond, &epggrab_mutex, &ts);
} else {
err = pthread_cond_wait(&epggrab_cond, &epggrab_mutex);
}
if ( err == ETIMEDOUT ) break;
}
confver = epggrab_confver;
mod = epggrab_module;
ts.tv_sec += epggrab_interval;
pthread_mutex_unlock(&epggrab_mutex);
/* Run grabber */
if (mod) _epggrab_module_grab(mod);
}
return NULL;
}
/* **************************************************************************
* Configuration
* *************************************************************************/
static void _epggrab_load ( void )
{
epggrab_module_t *mod;
htsmsg_field_t *f;
htsmsg_t *m, *a;
uint32_t enabled = 1;
const char *str;
int old = 0;
/* Load settings */
if (!(m = hts_settings_load("epggrab/config"))) {
if ((m = hts_settings_load("xmltv/config")))
old = 1;
}
if (old) tvhlog(LOG_INFO, "epggrab", "migrating old configuration");
/* Process */
if (m) {
htsmsg_get_u32(m, "channel_rename", &epggrab_channel_rename);
htsmsg_get_u32(m, "channel_renumber", &epggrab_channel_renumber);
htsmsg_get_u32(m, "channel_reicon", &epggrab_channel_reicon);
if (!htsmsg_get_u32(m, old ? "grab-interval" : "interval",
&epggrab_interval)) {
if (old) epggrab_interval *= 3600;
}
htsmsg_get_u32(m, "grab-enabled", &enabled);
if (enabled) {
if ( (str = htsmsg_get_str(m, old ? "current-grabber" : "module")) ) {
mod = epggrab_module_find_by_id(str);
if (mod && mod->type == EPGGRAB_INT) {
epggrab_module = (epggrab_module_int_t*)mod;
}
}
if ( (a = htsmsg_get_map(m, "mod_enabled")) ) {
LIST_FOREACH(mod, &epggrab_modules, link) {
if (htsmsg_get_u32_or_default(a, mod->id, 0)) {
epggrab_enable_module(mod, 1);
}
}
}
if ( (a = htsmsg_get_list(m, "mod_priority")) ) {
int prio = 1;
LIST_FOREACH(mod, &epggrab_modules, link)
mod->priority = 0;
HTSMSG_FOREACH(f, a) {
mod = epggrab_module_find_by_id(f->hmf_str);
if (mod) mod->priority = prio++;
}
}
}
htsmsg_destroy(m);
/* Finish up migration */
if (old) {
/* Enable OTA modules */
LIST_FOREACH(mod, &epggrab_modules, link)
if (mod->type == EPGGRAB_OTA)
epggrab_enable_module(mod, 1);
/* Migrate XMLTV channels */
htsmsg_t *xc, *ch;
htsmsg_t *xchs = hts_settings_load("xmltv/channels");
htsmsg_t *chs = hts_settings_load("channels");
if (xchs) {
HTSMSG_FOREACH(f, chs) {
if ((ch = htsmsg_get_map_by_field(f))) {
if ((str = htsmsg_get_str(ch, "xmltv-channel"))) {
if ((xc = htsmsg_get_map(xchs, str))) {
htsmsg_add_u32(xc, "channel", atoi(f->hmf_name));
}
}
}
}
HTSMSG_FOREACH(f, xchs) {
if ((xc = htsmsg_get_map_by_field(f))) {
hts_settings_save(xc, "epggrab/xmltv/channels/%s", f->hmf_name);
}
}
}
/* Save epggrab config */
epggrab_save();
}
/* Defaults */
} else {
epggrab_interval = 12 * 3600; // hours
epggrab_module = NULL; // disabled
LIST_FOREACH(mod, &epggrab_modules, link) // enable all OTA by default
if (mod->type == EPGGRAB_OTA)
epggrab_enable_module(mod, 1);
}
/* Load module config (channels) */
eit_load();
xmltv_load();
pyepg_load();
opentv_load();
}
void epggrab_save ( void )
{
epggrab_module_t *mod;
htsmsg_t *m, *a;
/* Register */
epggrab_confver++;
pthread_cond_signal(&epggrab_cond);
/* Save */
m = htsmsg_create_map();
htsmsg_add_u32(m, "channel_rename", epggrab_channel_rename);
htsmsg_add_u32(m, "channel_renumber", epggrab_channel_renumber);
htsmsg_add_u32(m, "channel_reicon", epggrab_channel_reicon);
htsmsg_add_u32(m, "interval", epggrab_interval);
if ( epggrab_module )
htsmsg_add_str(m, "module", epggrab_module->id);
a = NULL;
LIST_FOREACH(mod, &epggrab_modules, link) {
if (mod->enabled) {
if (!a) a = htsmsg_create_map();
htsmsg_add_u32(a, mod->id, 1);
}
}
if (a) htsmsg_add_msg(m, "mod_enabled", a);
hts_settings_save(m, "epggrab/config");
htsmsg_destroy(m);
}
int epggrab_set_interval ( uint32_t interval )
{
int save = 0;
if ( epggrab_interval != interval ) {
save = 1;
epggrab_interval = interval;
}
return save;
}
int epggrab_set_module ( epggrab_module_t *m )
{
int save = 0;
epggrab_module_int_t *mod;
if (m && m->type != EPGGRAB_INT) return 0;
mod = (epggrab_module_int_t*)m;
if ( epggrab_module != mod ) {
epggrab_enable_module((epggrab_module_t*)epggrab_module, 0);
epggrab_module = (epggrab_module_int_t*)mod;
epggrab_enable_module((epggrab_module_t*)epggrab_module, 1);
save = 1;
}
return save;
}
int epggrab_set_module_by_id ( const char *id )
{
return epggrab_set_module(epggrab_module_find_by_id(id));
}
int epggrab_set_channel_rename ( uint32_t e )
{
int save = 0;
if ( e != epggrab_channel_rename ) {
epggrab_channel_rename = e;
save = 1;
}
return save;
}
int epggrab_set_channel_renumber ( uint32_t e )
{
int save = 0;
if ( e != epggrab_channel_renumber ) {
epggrab_channel_renumber = e;
save = 1;
}
return save;
}
int epggrab_set_channel_reicon ( uint32_t e )
{
int save = 0;
if ( e != epggrab_channel_reicon ) {
epggrab_channel_reicon = e;
save = 1;
}
return save;
}
int epggrab_enable_module ( epggrab_module_t *mod, uint8_t e )
{
int save = 0;
if (!mod) return 0;
if (mod->enable) {
save = mod->enable(mod, e);
} else if ( e != mod->enabled ) {
mod->enabled = e;
save = 1;
}
return save;
}
int epggrab_enable_module_by_id ( const char *id, uint8_t e )
{
return epggrab_enable_module(epggrab_module_find_by_id(id), e);
}
/* **************************************************************************
* Initialisation
* *************************************************************************/
/*
* TODO: implement this
*/
void epggrab_resched ( void )
{
}
/*
* Initialise
*/
void epggrab_init ( void )
{
/* Lists */
extern TAILQ_HEAD(, epggrab_ota_mux) ota_mux_all;
TAILQ_INIT(&ota_mux_all);
/* Initialise modules */
eit_init();
xmltv_init();
pyepg_init();
opentv_init();
/* Load config */
_epggrab_load();
epggrab_ota_load();
/* Start internal grab thread */
pthread_t tid;
pthread_attr_t tattr;
pthread_attr_init(&tattr);
pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
pthread_create(&tid, &tattr, _epggrab_internal_thread, NULL);
}