Coverage Report

Created: 2026-09-28 06:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mpv/stream/stream_dvb.c
Line
Count
Source
1
/*
2
3
   dvbstream
4
   (C) Dave Chapman <dave@dchapman.com> 2001, 2002.
5
   (C) Rozhuk Ivan <rozhuk.im@gmail.com> 2016 - 2017
6
7
   Original authors: Nico, probably Arpi
8
9
   Some code based on dvbstream, 0.4.3-pre3 (CVS checkout),
10
   http://sourceforge.net/projects/dvbtools/
11
12
   Modified for use with MPlayer, for details see the changelog at
13
   http://svn.mplayerhq.hu/mplayer/trunk/
14
   $Id$
15
16
   Copyright notice:
17
18
   This file is part of mpv.
19
20
   mpv is free software; you can redistribute it and/or modify
21
   it under the terms of the GNU General Public License as published by
22
   the Free Software Foundation; either version 2 of the License, or
23
   (at your option) any later version.
24
25
   mpv is distributed in the hope that it will be useful,
26
   but WITHOUT ANY WARRANTY; without even the implied warranty of
27
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
   GNU General Public License for more details.
29
30
   You should have received a copy of the GNU General Public License along
31
   with mpv.  If not, see <http://www.gnu.org/licenses/>.
32
33
*/
34
35
#include "config.h"
36
#include <stdio.h>
37
#include <stdlib.h>
38
#include <string.h>
39
#include <sys/ioctl.h>
40
#include <sys/time.h>
41
#include <poll.h>
42
#include <unistd.h>
43
#include <fcntl.h>
44
#include <errno.h>
45
46
#include "osdep/io.h"
47
#include "misc/ctype.h"
48
#include "osdep/timer.h"
49
50
#include "stream.h"
51
#include "common/tags.h"
52
#include "options/m_config.h"
53
#include "options/m_option.h"
54
#include "options/options.h"
55
#include "options/path.h"
56
#include "osdep/io.h"
57
#include "osdep/poll_wrapper.h"
58
#include "osdep/threads.h"
59
60
#include "dvbin.h"
61
#include "dvb_tune.h"
62
63
#if !HAVE_GPL
64
#error GPL only
65
#endif
66
67
0
#define CHANNEL_LINE_LEN 256
68
69
#define OPT_BASE_STRUCT dvb_opts_t
70
71
static dvb_state_t *global_dvb_state = NULL;
72
static mp_static_mutex global_dvb_state_lock = MP_STATIC_MUTEX_INITIALIZER;
73
74
const struct m_sub_options stream_dvb_conf = {
75
    .opts = (const m_option_t[]) {
76
        {"prog", OPT_STRING(cfg_prog), .flags = UPDATE_DVB_PROG},
77
        {"card", OPT_INT(cfg_devno), M_RANGE(0, MAX_ADAPTERS-1)},
78
        {"timeout", OPT_FLOAT(cfg_timeout), M_RANGE(0, FLT_MAX)},
79
        {"file", OPT_STRING(cfg_file), .flags = M_OPT_FILE},
80
        {"full-transponder", OPT_BOOL(cfg_full_transponder)},
81
        {"channel-switch-offset", OPT_INT(cfg_channel_switch_offset),
82
            .flags = UPDATE_DVB_PROG},
83
        {0}
84
    },
85
    .size = sizeof(dvb_opts_t),
86
    .defaults = &(const dvb_opts_t){
87
        .cfg_prog = NULL,
88
        .cfg_timeout = 30,
89
    },
90
};
91
92
void dvbin_close(stream_t *stream);
93
94
static fe_code_rate_t parse_fec(const char *cr)
95
0
{
96
0
    if (!strcmp(cr, "FEC_1_2")) {
97
0
        return FEC_1_2;
98
0
    } else if (!strcmp(cr, "FEC_2_3")) {
99
0
        return FEC_2_3;
100
0
    } else if (!strcmp(cr, "FEC_3_4")) {
101
0
        return FEC_3_4;
102
0
    } else if (!strcmp(cr, "FEC_4_5")) {
103
0
        return FEC_4_5;
104
0
    } else if (!strcmp(cr, "FEC_5_6")) {
105
0
        return FEC_5_6;
106
0
    } else if (!strcmp(cr, "FEC_6_7")) {
107
0
        return FEC_6_7;
108
0
    } else if (!strcmp(cr, "FEC_7_8")) {
109
0
        return FEC_7_8;
110
0
    } else if (!strcmp(cr, "FEC_8_9")) {
111
0
        return FEC_8_9;
112
0
    } else if (!strcmp(cr, "FEC_NONE")) {
113
0
        return FEC_NONE;
114
0
    }
115
0
    return FEC_NONE;
116
0
}
117
118
static fe_modulation_t parse_vdr_modulation(const char** modstring)
119
0
{
120
0
    static const struct { const char *s; fe_modulation_t v; } table[] = {
121
0
        { "16",  QAM_16 },
122
0
        { "32",  QAM_32 },
123
0
        { "64",  QAM_64 },
124
0
        { "128", QAM_128 },
125
0
        { "256", QAM_256 },
126
0
        { "998", QAM_AUTO },
127
0
        { "2",   QPSK },
128
0
        { "5",   PSK_8 },
129
0
        { "6",   APSK_16 },
130
0
        { "7",   APSK_32 },
131
0
        { "10",  VSB_8 },
132
0
        { "11",  VSB_16 },
133
0
        { "12",  DQPSK },
134
0
    };
135
0
    for (int i = 0; i < MP_ARRAY_SIZE(table); i++) {
136
0
        if (!strncmp(*modstring, table[i].s, strlen(table[i].s))) {
137
0
            *modstring += strlen(table[i].s);
138
0
            return table[i].v;
139
0
        }
140
0
    }
141
0
    return QAM_AUTO;
142
0
}
143
144
static void parse_vdr_par_string(const char *vdr_par_str, dvb_channel_t *ptr)
145
0
{
146
    //FIXME: There is more information in this parameter string, especially related
147
    // to non-DVB-S reception.
148
0
    if (!vdr_par_str[0])
149
0
        return;
150
0
    const char *vdr_par = &vdr_par_str[0];
151
0
    while (vdr_par && *vdr_par) {
152
0
        switch (mp_toupper(*vdr_par)) {
153
0
        case 'H':
154
0
            ptr->pol = 'H';
155
0
            vdr_par++;
156
0
            break;
157
0
        case 'V':
158
0
            ptr->pol = 'V';
159
0
            vdr_par++;
160
0
            break;
161
0
        case 'S':
162
0
            vdr_par++;
163
0
            ptr->is_dvb_x2 = *vdr_par == '1';
164
0
            vdr_par++;
165
0
            break;
166
0
        case 'P':
167
0
            vdr_par++;
168
0
            char *endptr = NULL;
169
0
            errno = 0;
170
0
            int n = strtol(vdr_par, &endptr, 10);
171
0
            if (!errno && endptr != vdr_par) {
172
0
                ptr->stream_id = n;
173
0
                vdr_par = endptr;
174
0
            }
175
0
            break;
176
0
        case 'I':
177
0
            vdr_par++;
178
0
            ptr->inv = (*vdr_par == '1') ? INVERSION_ON : INVERSION_OFF;
179
0
            vdr_par++;
180
0
            break;
181
0
        case 'M':
182
0
            vdr_par++;
183
0
            ptr->mod = parse_vdr_modulation(&vdr_par);
184
0
            break;
185
0
        default:
186
0
            vdr_par++;
187
0
        }
188
0
    }
189
0
}
190
191
static char *dvb_strtok_r(char *s, const char *sep, char **p)
192
0
{
193
0
    if (!s && !(s = *p))
194
0
        return NULL;
195
196
    /* Skip leading separators. */
197
0
    s += strspn(s, sep);
198
199
    /* s points at first non-separator, or end of string. */
200
0
    if (!*s)
201
0
        return *p = 0;
202
203
    /* Move *p to next separator. */
204
0
    *p = s + strcspn(s, sep);
205
0
    if (**p) {
206
0
        *(*p)++ = 0;
207
0
    } else {
208
0
        *p = 0;
209
0
    }
210
0
    return s;
211
0
}
212
213
static bool parse_pid_string(struct mp_log *log, char *pid_string,
214
                             dvb_channel_t *ptr)
215
0
{
216
0
    if (!pid_string[0])
217
0
        return false;
218
0
    int pcnt = 0;
219
    /* These tokens also catch vdr-style PID lists.
220
     * They can contain 123=deu@3,124=eng+jap@4;125
221
     * 3 and 4 are codes for codec type, =langLeft+langRight is allowed,
222
     * and ; may separate a dolby channel.
223
     * With the numChars-test and the full token-list, all is handled
224
     * gracefully.
225
     */
226
0
    const char *tokens = "+,;";
227
0
    char *pidPart;
228
0
    char *savePtr = NULL;
229
0
    pidPart = dvb_strtok_r(pid_string, tokens, &savePtr);
230
0
    while (pidPart != NULL) {
231
0
        if (ptr->pids_cnt >= DMX_FILTER_SIZE - 1) {
232
0
            mp_verbose(log, "Maximum number of PIDs for one channel "
233
0
                            "reached, ignoring further ones!\n");
234
0
            break;
235
0
        }
236
0
        int numChars = 0;
237
0
        int pid = 0;
238
0
        pcnt += sscanf(pidPart, "%d%n", &pid, &numChars);
239
0
        if (numChars > 0) {
240
0
            ptr->pids[ptr->pids_cnt] = pid;
241
0
            ptr->pids_cnt++;
242
0
        }
243
0
        pidPart = dvb_strtok_r(NULL, tokens, &savePtr);
244
0
    }
245
0
    return pcnt > 0;
246
0
}
247
248
static dvb_channels_list_t *dvb_get_channels(struct mp_log *log,
249
                                           dvb_channels_list_t *list_add,
250
                                           int cfg_full_transponder,
251
                                           char *filename,
252
                                           unsigned int frontend,
253
                                           int delsys, unsigned int delsys_mask)
254
0
{
255
0
    dvb_channels_list_t *list = list_add;
256
257
0
    if (!filename)
258
0
        return list;
259
260
0
    const char *cbl_conf =
261
0
        "%d:%255[^:]:%d:%255[^:]:%255[^:]:%255[^:]:%255[^:]\n";
262
0
    const char *sat_conf = "%d:%c:%d:%d:%255[^:]:%255[^:]\n";
263
0
    const char *ter_conf =
264
0
        "%d:%255[^:]:%255[^:]:%255[^:]:%255[^:]:%255[^:]:%255[^:]:%255[^:]:%255[^:]:%255[^:]:%255[^:]\n";
265
0
    const char *atsc_conf = "%d:%255[^:]:%255[^:]:%255[^:]\n";
266
0
    const char *vdr_conf =
267
0
        "%d:%255[^:]:%255[^:]:%d:%255[^:]:%255[^:]:%255[^:]:%*255[^:]:%d:%*d:%*d:%*d\n%n";
268
269
0
    mp_verbose(log, "Reading config file %s for type %s\n",
270
0
               filename, get_dvb_delsys(delsys));
271
0
    FILE *f = fopen(filename, "r");
272
0
    if (!f) {
273
0
        mp_fatal(log, "Can't open file %s\n", filename);
274
0
        return list;
275
0
    }
276
277
0
    if (!list)
278
0
        list = talloc_zero(NULL, dvb_channels_list_t);
279
280
0
    while (!feof(f)) {
281
0
        char line[CHANNEL_LINE_LEN];
282
0
        if (!fgets(line, CHANNEL_LINE_LEN, f))
283
0
            continue;
284
285
0
        if (line[0] == '#' || strlen(line) == 0)
286
0
            continue;
287
288
0
        dvb_channel_t chn = {0};
289
0
        dvb_channel_t *ptr = &chn;
290
291
0
        char tmp_lcr[256], tmp_hier[256], inv[256], bw[256], cr[256], mod[256],
292
0
            transm[256], gi[256], vpid_str[256], apid_str[256], tpid_str[256],
293
0
            vdr_par_str[256], vdr_loc_str[256];
294
295
0
        vpid_str[0] = apid_str[0] = tpid_str[0] = 0;
296
0
        vdr_loc_str[0] = vdr_par_str[0] = 0;
297
0
        tmp_lcr[0] = tmp_hier[0] = inv[0] = bw[0] = 0;
298
0
        cr[0] = mod[0] = transm[0] = gi[0] = 0;
299
300
0
        char *colon = strchr(line, ':');
301
0
        if (!colon)
302
0
            continue;
303
0
        int k = colon - line;
304
0
        if (!k)
305
0
            continue;
306
        // In some modern VDR-style configs, channel name also has bouquet after ;.
307
        // Parse that off, we ignore it.
308
0
        char *bouquet_sep = strchr(line, ';');
309
0
        {
310
0
            int namelen = k;
311
0
            if (bouquet_sep && bouquet_sep < colon)
312
0
                namelen = bouquet_sep - line;
313
0
            ptr->name = talloc_strndup(list, line, namelen);
314
0
        }
315
316
0
        k++;
317
318
0
        ptr->pids_cnt = 0;
319
0
        ptr->freq = 0;
320
0
        ptr->service_id = -1;
321
0
        ptr->is_dvb_x2 = false;
322
0
        ptr->frontend = frontend;
323
0
        ptr->delsys = delsys;
324
0
        ptr->diseqc = 0;
325
0
        ptr->stream_id = NO_STREAM_ID_FILTER;
326
0
        ptr->inv = INVERSION_AUTO;
327
0
        ptr->bw = BANDWIDTH_AUTO;
328
0
        ptr->cr = FEC_AUTO;
329
0
        ptr->cr_lp = FEC_AUTO;
330
0
        ptr->mod = QAM_AUTO;
331
0
        ptr->hier = HIERARCHY_AUTO;
332
0
        ptr->gi = GUARD_INTERVAL_AUTO;
333
0
        ptr->trans = TRANSMISSION_MODE_AUTO;
334
335
        // Check if VDR-type channels.conf-line - then full line is consumed by the scan.
336
0
        int fields, num_chars = 0;
337
0
        fields = sscanf(&line[k], vdr_conf,
338
0
                        &ptr->freq, vdr_par_str, vdr_loc_str, &ptr->srate,
339
0
                        vpid_str, apid_str, tpid_str, &ptr->service_id,
340
0
                        &num_chars);
341
342
0
        bool is_vdr_conf = (num_chars == strlen(&line[k]));
343
344
        // Special case: DVB-T style ZAP config also has 13 columns.
345
        // Most columns should have non-numeric content, but some channel list generators insert 0
346
        // if a value is not used. However, INVERSION_* should always set after frequency.
347
0
        if (is_vdr_conf && !strncmp(vdr_par_str, "INVERSION_", 10)) {
348
0
            is_vdr_conf = false;
349
0
        }
350
351
0
        if (is_vdr_conf) {
352
            // Modulation parsed here, not via old xine-parsing path.
353
0
            mod[0] = '\0';
354
            // It's a VDR-style config line.
355
0
            parse_vdr_par_string(vdr_par_str, ptr);
356
            // Frequency in VDR-style config files is in MHz for DVB-S,
357
            // and may be in MHz, kHz or Hz for DVB-C and DVB-T.
358
            // General rule to get useful units is to multiply by 1000 until value is larger than 1000000.
359
0
            while (ptr->freq < 1000000U) {
360
0
                ptr->freq *= 1000U;
361
0
            }
362
            // Symbol rate in VDR-style config files is divided by 1000.
363
0
            ptr->srate *= 1000U;
364
0
            switch (delsys) {
365
0
            case SYS_DVBT:
366
0
            case SYS_DVBT2:
367
                /* Fix delsys value. */
368
0
                if (ptr->is_dvb_x2) {
369
0
                    ptr->delsys = delsys = SYS_DVBT2;
370
0
                } else {
371
0
                    ptr->delsys = delsys = SYS_DVBT;
372
0
                }
373
0
                if (!DELSYS_IS_SET(delsys_mask, delsys))
374
0
                    continue; /* Skip channel. */
375
0
                mp_verbose(log, "VDR, %s, NUM: %d, NUM_FIELDS: %d, NAME: %s, "
376
0
                           "FREQ: %d, SRATE: %d, T2: %s\n",
377
0
                           get_dvb_delsys(delsys),
378
0
                           list->NUM_CHANNELS, fields,
379
0
                           ptr->name, ptr->freq, ptr->srate,
380
0
                           (delsys == SYS_DVBT2) ? "yes" : "no");
381
0
                break;
382
0
            case SYS_DVBC_ANNEX_A:
383
0
            case SYS_DVBC_ANNEX_C:
384
0
            case SYS_ATSC:
385
0
            case SYS_DVBC_ANNEX_B:
386
0
            case SYS_ISDBT:
387
0
                mp_verbose(log, "VDR, %s, NUM: %d, NUM_FIELDS: %d, NAME: %s, "
388
0
                           "FREQ: %d, SRATE: %d\n",
389
0
                           get_dvb_delsys(delsys),
390
0
                           list->NUM_CHANNELS, fields,
391
0
                           ptr->name, ptr->freq, ptr->srate);
392
0
                break;
393
0
            case SYS_DVBS:
394
0
            case SYS_DVBS2:
395
                /* Fix delsys value. */
396
0
                if (ptr->is_dvb_x2) {
397
0
                    ptr->delsys = delsys = SYS_DVBS2;
398
0
                } else {
399
0
                    ptr->delsys = delsys = SYS_DVBS;
400
0
                }
401
0
                if (!DELSYS_IS_SET(delsys_mask, delsys))
402
0
                    continue; /* Skip channel. */
403
404
0
                if (vdr_loc_str[0]) {
405
                    // In older vdr config format, this field contained the DISEQc information.
406
                    // If it is numeric, assume that's it.
407
0
                    int diseqc_info = 0;
408
0
                    int valid_digits = 0;
409
0
                    if (sscanf(vdr_loc_str, "%d%n", &diseqc_info,
410
0
                               &valid_digits) == 1)
411
0
                    {
412
0
                        if (valid_digits == strlen(vdr_loc_str)) {
413
0
                            ptr->diseqc = (unsigned int)diseqc_info;
414
0
                            if (ptr->diseqc > 4)
415
0
                                continue;
416
0
                            if (ptr->diseqc > 0)
417
0
                                ptr->diseqc--;
418
0
                        }
419
0
                    }
420
0
                }
421
422
0
                mp_verbose(log, "VDR, %s, NUM: %d, NUM_FIELDS: %d, NAME: %s, "
423
0
                           "FREQ: %d, SRATE: %d, POL: %c, DISEQC: %d, S2: %s, "
424
0
                           "StreamID: %d, SID: %d\n",
425
0
                           get_dvb_delsys(delsys),
426
0
                           list->NUM_CHANNELS,
427
0
                           fields, ptr->name, ptr->freq, ptr->srate, ptr->pol,
428
0
                           ptr->diseqc, (delsys == SYS_DVBS2) ? "yes" : "no",
429
0
                           ptr->stream_id, ptr->service_id);
430
0
                break;
431
0
            default:
432
0
                break;
433
0
            }
434
0
        } else {
435
            // ZAP style channel config file.
436
0
            switch (delsys) {
437
0
            case SYS_DVBT:
438
0
            case SYS_DVBT2:
439
0
            case SYS_ISDBT:
440
0
                fields = sscanf(&line[k], ter_conf,
441
0
                                &ptr->freq, inv, bw, cr, tmp_lcr, mod,
442
0
                                transm, gi, tmp_hier, vpid_str, apid_str);
443
0
                mp_verbose(log, "%s, NUM: %d, NUM_FIELDS: %d, NAME: %s, FREQ: %d\n",
444
0
                           get_dvb_delsys(delsys), list->NUM_CHANNELS,
445
0
                           fields, ptr->name, ptr->freq);
446
0
                break;
447
0
            case SYS_DVBC_ANNEX_A:
448
0
            case SYS_DVBC_ANNEX_C:
449
0
                fields = sscanf(&line[k], cbl_conf,
450
0
                                &ptr->freq, inv, &ptr->srate,
451
0
                                cr, mod, vpid_str, apid_str);
452
0
                mp_verbose(log, "%s, NUM: %d, NUM_FIELDS: %d, NAME: %s, FREQ: %d, "
453
0
                           "SRATE: %d\n",
454
0
                           get_dvb_delsys(delsys),
455
0
                           list->NUM_CHANNELS, fields, ptr->name,
456
0
                           ptr->freq, ptr->srate);
457
0
                break;
458
0
            case SYS_ATSC:
459
0
            case SYS_DVBC_ANNEX_B:
460
0
                fields = sscanf(&line[k], atsc_conf,
461
0
                                &ptr->freq, mod, vpid_str, apid_str);
462
0
                mp_verbose(log, "%s, NUM: %d, NUM_FIELDS: %d, NAME: %s, FREQ: %d\n",
463
0
                           get_dvb_delsys(delsys), list->NUM_CHANNELS,
464
0
                           fields, ptr->name, ptr->freq);
465
0
                break;
466
0
            case SYS_DVBS:
467
0
            case SYS_DVBS2:
468
0
                fields = sscanf(&line[k], sat_conf,
469
0
                                &ptr->freq, &ptr->pol, &ptr->diseqc, &ptr->srate,
470
0
                                vpid_str,
471
0
                                apid_str);
472
0
                ptr->pol = mp_toupper(ptr->pol);
473
0
                ptr->freq *= 1000U;
474
0
                ptr->srate *= 1000U;
475
0
                if (ptr->diseqc > 4)
476
0
                    continue;
477
0
                if (ptr->diseqc > 0)
478
0
                    ptr->diseqc--;
479
0
                mp_verbose(log, "%s, NUM: %d, NUM_FIELDS: %d, NAME: %s, FREQ: %d, "
480
0
                           "SRATE: %d, POL: %c, DISEQC: %d\n",
481
0
                           get_dvb_delsys(delsys),
482
0
                           list->NUM_CHANNELS, fields, ptr->name, ptr->freq,
483
0
                           ptr->srate, ptr->pol, ptr->diseqc);
484
0
                break;
485
0
            default:
486
0
                break;
487
0
            }
488
0
        }
489
490
0
        if (parse_pid_string(log, vpid_str, ptr))
491
0
            fields++;
492
0
        if (parse_pid_string(log, apid_str, ptr))
493
0
            fields++;
494
                 /* If we do not know the service_id, PMT can not be extracted.
495
                    Teletext decoding will fail without PMT. */
496
0
        if (ptr->service_id != -1) {
497
0
            if (parse_pid_string(log, tpid_str, ptr))
498
0
                fields++;
499
0
        }
500
501
502
0
        if (fields < 2 || ptr->pids_cnt == 0 || ptr->freq == 0 ||
503
0
            strlen(ptr->name) == 0)
504
0
            continue;
505
506
        /* Add some PIDs which are mandatory in DVB,
507
         * and contain human-readable helpful data. */
508
509
        /* This is the STD, the service description table.
510
         * It contains service names and such, ffmpeg decodes it. */
511
0
        ptr->pids[ptr->pids_cnt] = 0x0011;
512
0
        ptr->pids_cnt++;
513
514
        /* This is the EIT, which contains EPG data.
515
         * ffmpeg can not decode it (yet), but e.g. VLC
516
         * shows what was recorded. */
517
0
        ptr->pids[ptr->pids_cnt] = 0x0012;
518
0
        ptr->pids_cnt++;
519
520
0
        if (ptr->service_id != -1) {
521
            /* We have the PMT-PID in addition.
522
               This will be found later, when we tune to the channel.
523
               Push back here to create the additional demux. */
524
0
            ptr->pids[ptr->pids_cnt] = -1;       // Placeholder.
525
0
            ptr->pids_cnt++;
526
0
        }
527
528
0
        bool has8192 = false, has0 = false;
529
0
        for (int cnt = 0; cnt < ptr->pids_cnt; cnt++) {
530
0
            if (ptr->pids[cnt] == 8192)
531
0
                has8192 = true;
532
0
            if (ptr->pids[cnt] == 0)
533
0
                has0 = true;
534
0
        }
535
536
        /* 8192 is the pseudo-PID for full TP dump,
537
           enforce that if requested. */
538
0
        if (!has8192 && cfg_full_transponder)
539
0
            has8192 = 1;
540
0
        if (has8192) {
541
0
            ptr->pids[0] = 8192;
542
0
            ptr->pids_cnt = 1;
543
0
        } else if (!has0) {
544
0
            ptr->pids[ptr->pids_cnt] = 0;               //PID 0 is the PAT
545
0
            ptr->pids_cnt++;
546
0
        }
547
548
0
        mp_verbose(log, " PIDS: ");
549
0
        for (int cnt = 0; cnt < ptr->pids_cnt; cnt++)
550
0
            mp_verbose(log, " %d ", ptr->pids[cnt]);
551
0
        mp_verbose(log, "\n");
552
553
0
        switch (delsys) {
554
0
        case SYS_DVBT:
555
0
        case SYS_DVBT2:
556
0
        case SYS_ISDBT:
557
0
        case SYS_DVBC_ANNEX_A:
558
0
        case SYS_DVBC_ANNEX_C:
559
0
            if (!strcmp(inv, "INVERSION_ON")) {
560
0
                ptr->inv = INVERSION_ON;
561
0
            } else if (!strcmp(inv, "INVERSION_OFF")) {
562
0
                ptr->inv = INVERSION_OFF;
563
0
            }
564
565
0
            ptr->cr = parse_fec(cr);
566
0
        }
567
568
0
        switch (delsys) {
569
0
        case SYS_DVBT:
570
0
        case SYS_DVBT2:
571
0
        case SYS_ISDBT:
572
0
        case SYS_DVBC_ANNEX_A:
573
0
        case SYS_DVBC_ANNEX_C:
574
0
        case SYS_ATSC:
575
0
        case SYS_DVBC_ANNEX_B:
576
0
            if (!strcmp(mod, "QAM_128")) {
577
0
                ptr->mod = QAM_128;
578
0
            } else if (!strcmp(mod, "QAM_256")) {
579
0
                ptr->mod = QAM_256;
580
0
            } else if (!strcmp(mod, "QAM_64")) {
581
0
                ptr->mod = QAM_64;
582
0
            } else if (!strcmp(mod, "QAM_32")) {
583
0
                ptr->mod = QAM_32;
584
0
            } else if (!strcmp(mod, "QAM_16")) {
585
0
                ptr->mod = QAM_16;
586
0
            } else if (!strcmp(mod, "VSB_8") || !strcmp(mod, "8VSB")) {
587
0
                ptr->mod = VSB_8;
588
0
            } else if (!strcmp(mod, "VSB_16") || !strcmp(mod, "16VSB")) {
589
0
                ptr->mod = VSB_16;
590
0
            }
591
0
        }
592
593
        /* Modulation defines real delsys for ATSC:
594
           Terrestrial (VSB) is SYS_ATSC, Cable (QAM) is SYS_DVBC_ANNEX_B. */
595
0
        if (delsys == SYS_ATSC || delsys == SYS_DVBC_ANNEX_B) {
596
0
            if (ptr->mod == VSB_8 || ptr->mod == VSB_16) {
597
0
                delsys = SYS_ATSC;
598
0
            } else {
599
0
                delsys = SYS_DVBC_ANNEX_B;
600
0
            }
601
0
            if (!DELSYS_IS_SET(delsys_mask, delsys))
602
0
                continue; /* Skip channel. */
603
0
            mp_verbose(log, "Switched to delivery system for ATSC: %s (guessed from modulation)\n",
604
0
                       get_dvb_delsys(delsys));
605
0
        }
606
607
0
        switch (delsys) {
608
0
        case SYS_DVBT:
609
0
        case SYS_DVBT2:
610
0
        case SYS_ISDBT:
611
0
            if (!strcmp(bw, "BANDWIDTH_5_MHZ")) {
612
0
                ptr->bw = BANDWIDTH_5_MHZ;
613
0
            } else if (!strcmp(bw, "BANDWIDTH_6_MHZ")) {
614
0
                ptr->bw = BANDWIDTH_6_MHZ;
615
0
            } else if (!strcmp(bw, "BANDWIDTH_7_MHZ")) {
616
0
                ptr->bw = BANDWIDTH_7_MHZ;
617
0
            } else if (!strcmp(bw, "BANDWIDTH_8_MHZ")) {
618
0
                ptr->bw = BANDWIDTH_8_MHZ;
619
0
            } else if (!strcmp(bw, "BANDWIDTH_10_MHZ")) {
620
0
                ptr->bw = BANDWIDTH_10_MHZ;
621
0
            }
622
623
624
0
            if (!strcmp(transm, "TRANSMISSION_MODE_2K")) {
625
0
                ptr->trans = TRANSMISSION_MODE_2K;
626
0
            } else if (!strcmp(transm, "TRANSMISSION_MODE_8K")) {
627
0
                ptr->trans = TRANSMISSION_MODE_8K;
628
0
            } else if (!strcmp(transm, "TRANSMISSION_MODE_AUTO")) {
629
0
                ptr->trans = TRANSMISSION_MODE_AUTO;
630
0
            }
631
632
0
            if (!strcmp(gi, "GUARD_INTERVAL_1_32")) {
633
0
                ptr->gi = GUARD_INTERVAL_1_32;
634
0
            } else if (!strcmp(gi, "GUARD_INTERVAL_1_16")) {
635
0
                ptr->gi = GUARD_INTERVAL_1_16;
636
0
            } else if (!strcmp(gi, "GUARD_INTERVAL_1_8")) {
637
0
                ptr->gi = GUARD_INTERVAL_1_8;
638
0
            } else if (!strcmp(gi, "GUARD_INTERVAL_1_4")) {
639
0
                ptr->gi = GUARD_INTERVAL_1_4;
640
0
            }
641
642
0
            ptr->cr_lp = parse_fec(tmp_lcr);
643
644
0
            if (!strcmp(tmp_hier, "HIERARCHY_1")) {
645
0
                ptr->hier = HIERARCHY_1;
646
0
            } else if (!strcmp(tmp_hier, "HIERARCHY_2")) {
647
0
                ptr->hier = HIERARCHY_2;
648
0
            } else if (!strcmp(tmp_hier, "HIERARCHY_4")) {
649
0
                ptr->hier = HIERARCHY_4;
650
0
            } else if (!strcmp(tmp_hier, "HIERARCHY_NONE")) {
651
0
                ptr->hier = HIERARCHY_NONE;
652
0
            }
653
0
        }
654
655
0
        MP_TARRAY_APPEND(list, list->channels, list->NUM_CHANNELS, *ptr);
656
0
    }
657
658
0
    fclose(f);
659
0
    if (list->NUM_CHANNELS == 0)
660
0
        TA_FREEP(&list);
661
662
0
    return list;
663
0
}
664
665
static int dvb_streaming_read(stream_t *stream, void *buffer, int size)
666
0
{
667
0
    dvb_priv_t *priv = stream->priv;
668
0
    dvb_state_t *state = priv->state;
669
0
    int pos = 0;
670
0
    int tries = state->retry;
671
0
    const int fd = state->dvr_fd;
672
673
0
    MP_TRACE(stream, "dvb_streaming_read(%d)\n", size);
674
675
0
    struct pollfd pfds[1];
676
0
    pfds[0].fd = fd;
677
0
    pfds[0].events = POLLIN | POLLPRI;
678
679
0
    while (pos < size) {
680
0
        int rk = read(fd, (char *)buffer + pos, (size - pos));
681
0
        if (rk <= 0) {
682
0
            if (pos || tries == 0)
683
0
                break;
684
0
            tries--;
685
0
            if (mp_poll(pfds, 1, MP_TIME_S_TO_NS(2)) <= 0) {
686
0
                MP_ERR(stream, "dvb_streaming_read: failed with "
687
0
                        "errno %d when reading %d bytes\n", errno, size - pos);
688
0
                errno = 0;
689
0
                break;
690
0
            }
691
0
            continue;
692
0
        }
693
0
        pos += rk;
694
0
        MP_TRACE(stream, "got %d bytes\n", pos);
695
0
    }
696
697
0
    if (!pos)
698
0
        MP_ERR(stream, "dvb_streaming_read: returning 0 bytes\n");
699
700
    // Check if config parameters have been updated.
701
0
    dvb_update_config(stream);
702
703
0
    return pos;
704
0
}
705
706
int dvb_set_channel(stream_t *stream, unsigned int adapter, unsigned int n)
707
0
{
708
0
    dvb_priv_t *priv = stream->priv;
709
0
    dvb_state_t *state = priv->state;
710
711
0
    mp_assert(adapter < state->adapters_count);
712
0
    int devno = state->adapters[adapter].devno;
713
0
    dvb_channels_list_t *new_list = state->adapters[adapter].list;
714
0
    mp_assert(n < new_list->NUM_CHANNELS);
715
0
    dvb_channel_t *channel = &(new_list->channels[n]);
716
717
0
    if (state->is_on) {  //the fds are already open and we have to stop the demuxers
718
        /* Remove all demuxes. */
719
0
        dvb_fix_demuxes(priv, 0);
720
721
0
        state->retry = 0;
722
        // empty both the stream's and driver's buffer
723
0
        char buf[4096];
724
0
        while (dvb_streaming_read(stream, buf, sizeof(buf)) > 0) {}
725
726
0
        if (state->cur_adapter != adapter ||
727
0
            state->cur_frontend != channel->frontend) {
728
0
            dvbin_close(stream);
729
0
            if (!dvb_open_devices(priv, devno, channel->frontend, channel->pids_cnt)) {
730
0
                MP_ERR(stream, "dvb_set_channel: couldn't open devices of adapter "
731
0
                    "%d\n", devno);
732
0
                return 0;
733
0
            }
734
0
        } else {
735
            // close all demux_fds with pos > pids required for the new channel
736
            // or open other demux_fds if we have too few
737
0
            if (!dvb_fix_demuxes(priv, channel->pids_cnt))
738
0
                return 0;
739
0
        }
740
0
    } else {
741
0
        if (!dvb_open_devices(priv, devno, channel->frontend, channel->pids_cnt)) {
742
0
            MP_ERR(stream, "dvb_set_channel: couldn't open devices of adapter "
743
0
                    "%d\n", devno);
744
0
            return 0;
745
0
        }
746
0
    }
747
748
0
    state->retry = 5;
749
0
    new_list->current = n;
750
0
    MP_VERBOSE(stream, "dvb_set_channel: new channel name=\"%s\", adapter: %d, "
751
0
               "channel: %d\n", channel->name, devno, n);
752
753
0
    if (channel->freq != state->last_freq) {
754
0
        if (!dvb_tune(priv, channel->delsys, channel->freq,
755
0
                      channel->pol, channel->srate, channel->diseqc,
756
0
                      channel->stream_id, channel->inv,
757
0
                      channel->mod, channel->gi,
758
0
                      channel->trans, channel->bw, channel->cr, channel->cr_lp,
759
0
                      channel->hier, priv->opts->cfg_timeout))
760
0
            return 0;
761
0
    }
762
763
0
    state->is_on = true;
764
0
    state->last_freq = channel->freq;
765
0
    state->cur_adapter = adapter;
766
0
    state->cur_frontend = channel->frontend;
767
768
0
    if (channel->service_id != -1) {
769
        /* We need the PMT-PID in addition.
770
           If it has not yet beem resolved, do it now. */
771
0
        for (int i = 0; i < channel->pids_cnt; i++) {
772
0
            if (channel->pids[i] == -1) {
773
0
                MP_VERBOSE(stream, "dvb_set_channel: PMT-PID for service %d "
774
0
                           "not resolved yet, parsing PAT...\n",
775
0
                           channel->service_id);
776
0
                int pmt_pid = dvb_get_pmt_pid(priv, adapter, channel->service_id);
777
0
                MP_VERBOSE(stream, "found PMT-PID: %d\n", pmt_pid);
778
0
                channel->pids[i] = pmt_pid;
779
0
                break;
780
0
            }
781
0
        }
782
0
    }
783
784
    // sets demux filters and restart the stream
785
0
    for (int i = 0; i < channel->pids_cnt; i++) {
786
0
        if (channel->pids[i] == -1) {
787
            // In case PMT was not resolved, skip it here.
788
0
            MP_ERR(stream, "dvb_set_channel: PMT-PID not found, "
789
0
                           "teletext decoding may fail.\n");
790
0
            continue;
791
0
        }
792
0
        if (!dvb_set_ts_filt(priv, state->demux_fds[i], channel->pids[i],
793
0
                             DMX_PES_OTHER))
794
0
            return 0;
795
0
    }
796
797
0
    return 1;
798
0
}
799
800
static int dvbin_stream_control(struct stream *s, int cmd, void *arg)
801
0
{
802
0
    dvb_priv_t *priv = s->priv;
803
0
    dvb_state_t *state = priv->state;
804
805
0
    if (state->cur_adapter >= state->adapters_count)
806
0
        return STREAM_ERROR;
807
0
    dvb_channels_list_t *list = state->adapters[state->cur_adapter].list;
808
809
0
    switch (cmd) {
810
0
    case STREAM_CTRL_GET_METADATA: {
811
0
        struct mp_tags *metadata = talloc_zero(NULL, struct mp_tags);
812
0
        char *progname = list->channels[list->current].name;
813
0
        mp_tags_set_str(metadata, "title", progname);
814
0
        *(struct mp_tags **)arg = metadata;
815
0
        return STREAM_OK;
816
0
    }
817
0
    }
818
0
    return STREAM_UNSUPPORTED;
819
0
}
820
821
void dvbin_close(stream_t *stream)
822
0
{
823
0
    dvb_priv_t *priv = stream->priv;
824
0
    dvb_state_t *state = priv->state;
825
826
0
    if (state->switching_channel && state->is_on) {
827
        // Prevent state destruction, reset channel-switch.
828
0
        state->switching_channel = false;
829
0
        mp_mutex_lock(&global_dvb_state_lock);
830
0
        global_dvb_state->stream_used = false;
831
0
        mp_mutex_unlock(&global_dvb_state_lock);
832
0
        return;
833
0
    }
834
835
0
    for (int i = state->demux_fds_cnt - 1; i >= 0; i--) {
836
0
        state->demux_fds_cnt--;
837
0
        close(state->demux_fds[i]);
838
0
    }
839
0
    close(state->dvr_fd);
840
0
    close(state->fe_fd);
841
0
    state->fe_fd = state->dvr_fd = -1;
842
843
0
    state->is_on = false;
844
0
    state->cur_adapter = -1;
845
0
    state->cur_frontend = -1;
846
847
0
    mp_mutex_lock(&global_dvb_state_lock);
848
0
    TA_FREEP(&global_dvb_state);
849
0
    mp_mutex_unlock(&global_dvb_state_lock);
850
0
}
851
852
static int dvb_streaming_start(stream_t *stream, char *progname)
853
0
{
854
0
    dvb_priv_t *priv = stream->priv;
855
0
    dvb_state_t *state = priv->state;
856
857
0
    if (!progname)
858
0
        return 0;
859
860
0
    dvb_channels_list_t *list = state->adapters[state->cur_adapter].list;
861
0
    dvb_channel_t *channel = NULL;
862
0
    int i;
863
0
    for (i = 0; i < list->NUM_CHANNELS; i ++) {
864
0
        if (!strcmp(list->channels[i].name, progname)) {
865
0
            channel = &(list->channels[i]);
866
0
            break;
867
0
        }
868
0
    }
869
870
0
    if (!channel) {
871
0
        MP_ERR(stream, "no such channel \"%s\"\n", progname);
872
0
        return 0;
873
0
    }
874
0
    list->current = i;
875
876
    // When switching channels, cfg_channel_switch_offset
877
    // keeps the offset to the initially chosen channel.
878
0
    list->current = (list->NUM_CHANNELS + list->current +
879
0
                     priv->opts->cfg_channel_switch_offset) % list->NUM_CHANNELS;
880
0
    channel = &(list->channels[list->current]);
881
0
    MP_INFO(stream, "Tuning to channel \"%s\"...\n", channel->name);
882
0
    MP_VERBOSE(stream, "Program number %d: name=\"%s\", freq=%u\n", i,
883
0
               channel->name, channel->freq);
884
885
0
    if (!dvb_set_channel(stream, state->cur_adapter, list->current)) {
886
0
        dvbin_close(stream);
887
0
        return 0;
888
0
    }
889
890
0
    return 1;
891
0
}
892
893
void dvb_update_config(stream_t *stream)
894
0
{
895
0
    dvb_priv_t *priv = stream->priv;
896
0
    dvb_state_t *state = priv->state;
897
898
    // Throttle the check to at maximum once every 0.1 s.
899
0
    int now = (int)(mp_time_sec()*10);
900
0
    if (now == priv->opts_check_time)
901
0
        return;
902
0
    priv->opts_check_time = now;
903
904
0
    if (!m_config_cache_update(priv->opts_cache))
905
0
        return;
906
907
    // Re-parse stream path, if we have cfg parameters now,
908
    // these should be preferred.
909
0
    if (!dvb_parse_path(stream)) {
910
0
        MP_ERR(stream, "error parsing DVB config, not tuning.");
911
0
        return;
912
0
    }
913
914
0
    int r = dvb_streaming_start(stream, priv->prog);
915
0
    if (r) {
916
        // Stream will be pulled down after channel switch,
917
        // persist state.
918
0
        state->switching_channel = true;
919
0
    }
920
0
}
921
922
static int dvb_open(stream_t *stream)
923
915
{
924
915
    dvb_priv_t *priv = NULL;
925
926
915
    mp_mutex_lock(&global_dvb_state_lock);
927
915
    if (global_dvb_state && global_dvb_state->stream_used) {
928
0
        MP_ERR(stream, "DVB stream already in use, only one DVB stream can exist at a time!\n");
929
0
        mp_mutex_unlock(&global_dvb_state_lock);
930
0
        goto err_out;
931
0
    }
932
933
    // Need to re-get config in any case, not part of global state.
934
915
    stream->priv = talloc_zero(stream, dvb_priv_t);
935
915
    priv = stream->priv;
936
915
    priv->opts_cache = m_config_cache_alloc(stream, stream->global, &stream_dvb_conf);
937
915
    priv->opts = priv->opts_cache->opts;
938
939
915
    dvb_state_t *state = dvb_get_state(stream);
940
941
915
    priv->state = state;
942
915
    priv->log = stream->log;
943
915
    if (!state) {
944
915
        MP_ERR(stream, "DVB configuration is empty\n");
945
915
        mp_mutex_unlock(&global_dvb_state_lock);
946
915
        goto err_out;
947
915
    }
948
949
0
    if (!dvb_parse_path(stream)) {
950
0
        mp_mutex_unlock(&global_dvb_state_lock);
951
0
        goto err_out;
952
0
    }
953
954
0
    state->stream_used = true;
955
0
    mp_mutex_unlock(&global_dvb_state_lock);
956
957
0
    if (!state->is_on) {
958
        // State could be already initialized, for example, we just did a channel switch.
959
        // The following setup only has to be done once.
960
961
0
        state->cur_frontend = -1;
962
963
0
        if (!dvb_streaming_start(stream, priv->prog))
964
0
            goto err_out;
965
0
    }
966
967
0
    stream->fill_buffer = dvb_streaming_read;
968
0
    stream->close = dvbin_close;
969
0
    stream->control = dvbin_stream_control;
970
0
    stream->streaming = true;
971
0
    stream->demuxer = "lavf";
972
0
    stream->lavf_type = "mpegts";
973
974
0
    return STREAM_OK;
975
976
915
err_out:
977
915
    talloc_free(priv);
978
915
    stream->priv = NULL;
979
915
    return STREAM_ERROR;
980
0
}
981
982
int dvb_parse_path(stream_t *stream)
983
0
{
984
0
    dvb_priv_t *priv = stream->priv;
985
0
    dvb_state_t *state = priv->state;
986
987
    // Parse stream path. Common rule: cfg wins over stream path,
988
    // since cfg may be changed at runtime.
989
0
    bstr prog, devno;
990
0
    if (!bstr_split_tok(bstr0(stream->path), "@", &devno, &prog)) {
991
0
        prog = devno;
992
0
        devno.len = 0;
993
0
    }
994
995
0
    if (priv->opts->cfg_devno != 0) {
996
0
        priv->devno = priv->opts->cfg_devno;
997
0
    } else if (devno.len) {
998
0
        bstr r;
999
0
        priv->devno = bstrtoll(devno, &r, 0);
1000
0
        if (r.len || priv->devno < 0 || priv->devno >= MAX_ADAPTERS) {
1001
0
            MP_ERR(stream, "invalid devno: '%.*s'\n", BSTR_P(devno));
1002
0
            return 0;
1003
0
        }
1004
0
    } else {
1005
        // Default to the default of cfg_devno.
1006
0
        priv->devno = priv->opts->cfg_devno;
1007
0
    }
1008
1009
    // Current adapter is derived from devno.
1010
0
    state->cur_adapter = -1;
1011
0
    for (int i = 0; i < state->adapters_count; i++) {
1012
0
        if (state->adapters[i].devno == priv->devno) {
1013
0
            state->cur_adapter = i;
1014
0
            break;
1015
0
        }
1016
0
    }
1017
1018
0
    if (state->cur_adapter == -1) {
1019
0
        MP_ERR(stream, "No configuration found for adapter %d!\n",
1020
0
               priv->devno);
1021
0
        return 0;
1022
0
    }
1023
1024
0
    char *new_prog = NULL;
1025
0
    if (priv->opts->cfg_prog != NULL && strlen(priv->opts->cfg_prog) > 0) {
1026
0
        new_prog = talloc_strdup(priv, priv->opts->cfg_prog);
1027
0
    } else if (prog.len) {
1028
0
        new_prog = bstrto0(priv, prog);
1029
0
    } else {
1030
        // We use the first program from the channel list.
1031
0
        dvb_channels_list_t  *list = state->adapters[state->cur_adapter].list;
1032
0
        if (!list) {
1033
0
            MP_ERR(stream, "No channel list available for adapter %d!\n", priv->devno);
1034
0
            return 0;
1035
0
        }
1036
0
        new_prog = talloc_strdup(priv, list->channels[0].name);
1037
0
    }
1038
0
    talloc_free(priv->prog);
1039
0
    priv->prog = new_prog;
1040
1041
0
    MP_VERBOSE(stream, "dvb_config: prog=\"%s\", devno=%d\n",
1042
0
               priv->prog, priv->devno);
1043
0
    return 1;
1044
0
}
1045
1046
dvb_state_t *dvb_get_state(stream_t *stream)
1047
915
{
1048
915
    dvb_priv_t *priv = stream->priv;
1049
915
    if (global_dvb_state)
1050
0
        return global_dvb_state;
1051
1052
915
    struct mp_log *log = stream->log;
1053
915
    struct mpv_global *global = stream->global;
1054
1055
915
    dvb_state_t *state = talloc_zero(NULL, dvb_state_t);
1056
915
    state->switching_channel = false;
1057
915
    state->is_on = false;
1058
915
    state->stream_used = true;
1059
915
    state->fe_fd = state->dvr_fd = -1;
1060
1061
15.5k
    for (unsigned int i = 0; i < MAX_ADAPTERS; i++) {
1062
14.6k
        dvb_channels_list_t *list = NULL;
1063
14.6k
        unsigned int delsys_mask[MAX_FRONTENDS];
1064
131k
        for (unsigned int f = 0; f < MAX_FRONTENDS; f++) {
1065
117k
            char filename[100];
1066
117k
            snprintf(filename, sizeof(filename), "/dev/dvb/adapter%u/frontend%u", i, f);
1067
117k
            int fd = open(filename, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
1068
117k
            if (fd < 0)
1069
117k
                continue;
1070
1071
0
            delsys_mask[f] = dvb_get_tuner_delsys_mask(fd, log);
1072
0
            delsys_mask[f] &= DELSYS_SUPP_MASK; /* Filter unsupported delivery systems. */
1073
0
            close(fd);
1074
0
            if (delsys_mask[f] == 0) {
1075
0
                mp_verbose(log, "Frontend device %s has no supported delivery systems.\n",
1076
0
                       filename);
1077
0
                continue; /* Skip tuner. */
1078
0
            }
1079
1080
            /* Create channel list for adapter. */
1081
0
            for (unsigned int delsys = 0; delsys < SYS_DVB__COUNT__; delsys++) {
1082
0
                if (!DELSYS_IS_SET(delsys_mask[f], delsys))
1083
0
                    continue; /* Skip unsupported. */
1084
1085
0
                mp_verbose(log, "Searching channel list for delivery system %s\n", get_dvb_delsys(delsys));
1086
0
                const char *conf_file_name;
1087
0
                switch (delsys) {
1088
0
                case SYS_DVBC_ANNEX_A:
1089
0
                case SYS_DVBC_ANNEX_C:
1090
0
                    conf_file_name = "channels.conf.cbl";
1091
0
                    break;
1092
0
                case SYS_ATSC:
1093
0
                    conf_file_name = "channels.conf.atsc";
1094
0
                    break;
1095
0
                case SYS_DVBT:
1096
0
                    if (DELSYS_IS_SET(delsys_mask[f], SYS_DVBT2)) {
1097
                        // only if ter1 is present, interpret as DVB-T, else will be loaded as DVB-T2
1098
0
                        conf_file_name = "channels.conf.ter1";
1099
0
                    } else {
1100
0
                        conf_file_name = "channels.conf.ter";
1101
0
                    }
1102
0
                    break;
1103
0
                case SYS_DVBT2:
1104
0
                    conf_file_name = "channels.conf.ter";
1105
0
                    break;
1106
0
                case SYS_ISDBT:
1107
0
                    conf_file_name = "channels.conf.isdbt";
1108
0
                    break;
1109
0
                case SYS_DVBS:
1110
0
                    if (DELSYS_IS_SET(delsys_mask[f], SYS_DVBS2)) {
1111
                        // only if sat1 is present, interpret as DVB-S, else will be loaded as DVB-S2
1112
0
                        conf_file_name = "channels.conf.sat1";
1113
0
                    } else {
1114
0
                        conf_file_name = "channels.conf.sat";
1115
0
                    }
1116
0
                    break;
1117
0
                case SYS_DVBS2:
1118
0
                    conf_file_name = "channels.conf.sat";
1119
0
                    break;
1120
0
                default:
1121
0
                    continue;
1122
0
                }
1123
1124
0
                void *talloc_ctx = talloc_new(NULL);
1125
0
                char *conf_file;
1126
0
                if (priv->opts->cfg_file && priv->opts->cfg_file[0]) {
1127
0
                    conf_file = mp_get_user_path(talloc_ctx, global, priv->opts->cfg_file);
1128
0
                } else {
1129
0
                    conf_file = mp_find_config_file(talloc_ctx, global, conf_file_name);
1130
0
                    if (conf_file) {
1131
0
                        mp_verbose(log, "Ignoring other channels.conf files.\n");
1132
0
                    } else {
1133
0
                        conf_file = mp_find_config_file(talloc_ctx, global,
1134
0
                                        "channels.conf");
1135
0
                    }
1136
0
                }
1137
1138
0
                list = dvb_get_channels(log, list, priv->opts->cfg_full_transponder,
1139
0
                                        conf_file, f, delsys, delsys_mask[f]);
1140
0
                talloc_free(talloc_ctx);
1141
0
            }
1142
0
        }
1143
        /* Add adapter with non zero channel list. */
1144
14.6k
        if (!list)
1145
14.6k
            continue;
1146
1147
0
        dvb_adapter_config_t tmp = {
1148
0
            .devno = i,
1149
0
            .list = talloc_steal(state, list),
1150
0
        };
1151
0
        memcpy(&tmp.delsys_mask, delsys_mask, sizeof(delsys_mask));
1152
1153
0
        MP_TARRAY_APPEND(state, state->adapters, state->adapters_count, tmp);
1154
1155
0
        mp_verbose(log, "Added adapter with channels to state list, now %d.\n",
1156
0
                   state->adapters_count);
1157
0
    }
1158
1159
915
    if (state->adapters_count == 0)
1160
915
        TA_FREEP(&state);
1161
1162
915
    global_dvb_state = state;
1163
915
    return state;
1164
915
}
1165
1166
const stream_info_t stream_info_dvb = {
1167
    .name = "dvbin",
1168
    .open = dvb_open,
1169
    .protocols = (const char *const[]){ "dvb", NULL },
1170
    .stream_origin = STREAM_ORIGIN_UNSAFE,
1171
};