3 * @brief Digital broadcasting input module for VLC media player
5 /*****************************************************************************
6 * Copyright © 2011 Rémi Denis-Courmont
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1
11 * of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 ****************************************************************************/
27 #include <vlc_common.h>
28 #include <vlc_access.h>
29 #include <vlc_plugin.h>
30 #include <vlc_dialog.h>
34 #define CACHING_TEXT N_("Caching value (ms)")
35 #define CACHING_LONGTEXT N_( \
36 "The cache size (delay) for digital broadcasts (in milliseconds).")
38 #define ADAPTER_TEXT N_("DVB adapter")
39 #define ADAPTER_LONGTEXT N_( \
40 "If there is more than one digital broadcasting adapter, " \
41 "the adapter number must be selected. Numbering start from zero.")
43 #define DEVICE_TEXT N_("DVB device")
44 #define DEVICE_LONGTEXT N_( \
45 "If the selected adapter has more than one tuner, " \
46 "the tuner number must be selected. Numbering start from zero.")
48 #define BUDGET_TEXT N_("Do not demultiplex")
49 #define BUDGET_LONGTEXT N_( \
50 "Only useful programs are normally demultiplexed from the transponder. " \
51 "This option will disable demultiplexing and receive all programs.")
53 #define FREQ_TEXT N_("Frequency (kHz)")
54 #define FREQ_LONGTEXT N_( \
55 "TV channels are grouped by transponder (a.k.a. multiplex) " \
56 "on a given frequency. This is required to tune the receiver.")
58 #define MODULATION_TEXT N_("Modulation / Constellation")
59 #define MODULATION_LONGTEXT N_( \
60 "The digital signal can be modulated according with different " \
61 "constellations (depending on the delivery system). " \
62 "If the demodulator cannot detect the constellation automatically, " \
63 "it needs to be configured manually.")
64 static const char *const modulation_vlc[] = { "",
65 "QAM", "16QAM", "32QAM", "64QAM", "128QAM", "256QAM",
67 "QPSK", "DQPSK", "8PSK", "16APSK", "32APSK",
69 static const char *const modulation_user[] = { N_("Undefined"),
70 "Auto QAM", "16-QAM", "32-QAM", "64-QAM", "128-QAM", "256-QAM",
72 "QPSK", "DQPSK", "8-PSK", "16-APSK", "32-APSK",
75 #define SRATE_TEXT N_("Symbol rate (bauds)")
76 #define SRATE_LONGTEXT N_( \
77 "The symbol rate must be specified manually for some systems, " \
78 "notably DVB-C, DVB-S and DVB-S2.")
80 #define INVERSION_TEXT N_("Spectrum inversion")
81 #define INVERSION_LONGTEXT N_( \
82 "If the demodulator cannot detect spectral inversion correctly, " \
83 "it needs to be configured manually.")
84 const int auto_off_on_vlc[] = { -1, 0, 1 };
85 static const char *const auto_off_on_user[] = { N_("Automatic"),
86 N_("Off"), N_("On") };
88 #define CODE_RATE_TEXT N_("FEC code rate")
89 #define CODE_RATE_HP_TEXT N_("High-priority code rate")
90 #define CODE_RATE_LP_TEXT N_("Low-priority code rate")
91 #define CODE_RATE_LONGTEXT N_( \
92 "The code rate for Forward Error Correction can be specified.")
93 static const char *const code_rate_vlc[] = { "",
94 "none", /*"1/4", "1/3",*/ "1/2", "3/5", "2/3", "3/4",
95 "4/5", "5/6", "6/7", "7/8", "8/9", "9/10",
97 static const char *const code_rate_user[] = { N_("Automatic"),
98 N_("None"), /*"1/4", "1/3",*/ "1/2", "3/5", "2/3", "3/4",
99 "4/5", "5/6", "6/7", "7/8", "8/9", "9/10",
102 #define TRANSMISSION_TEXT N_("Transmission mode")
103 const int transmission_vlc[] = { -1,
106 static const char *const transmission_user[] = { N_("Automatic"),
107 "2k", "4k", "8k", /*"16k", "32k", */
110 #define BANDWIDTH_TEXT N_("Bandwidth (MHz)")
111 const int bandwidth_vlc[] = { 0,
114 static const char *const bandwidth_user[] = { N_("Automatic"),
115 N_("8 MHz"), N_("7 MHz"), N_("6 MHz"),
118 #define GUARD_TEXT N_("Guard interval")
119 const char *const guard_vlc[] = { "",
120 /*"1/128",*/ "1/32", "1/16", /*"19/128",*/ "1/8", /*"9/256",*/ "1/4",
122 static const char *const guard_user[] = { N_("Automatic"),
123 /*"1/128",*/ "1/32", "1/16", /*"19/128",*/ "1/8", /*"9/256",*/ "1/4",
126 #define HIERARCHY_TEXT N_("Hierarchy mode")
127 const int hierarchy_vlc[] = { -1,
130 static const char *const hierarchy_user[] = { N_("Automatic"),
131 N_("None"), "1", "2", "4",
134 #define PILOT_TEXT N_("Pilot")
136 #define ROLLOFF_TEXT N_("Roll-off factor")
137 const int rolloff_vlc[] = { -1,
140 static const char *const rolloff_user[] = { N_("Automatic"),
141 N_("0.35 (same as DVB-S)"), N_("0.20"), N_("0.25"),
144 #define POLARIZATION_TEXT N_("Polarization (Voltage)")
145 #define POLARIZATION_LONGTEXT N_( \
146 "To select the polarization of the transponder, a different voltage " \
147 "is normally applied to the low noise block-downconverter (LNB).")
148 static const char *const polarization_vlc[] = { "", "V", "H", "R", "L" };
149 static const char *const polarization_user[] = { N_("Unspecified (0V)"),
150 N_("Vertical (13V)"), N_("Horizontal (18V)"),
151 N_("Circular Right Hand (13V)"), N_("Circular Left Hand (18V)") };
153 #define HIGH_VOLTAGE_TEXT N_("High LNB voltage")
154 #define HIGH_VOLTAGE_LONGTEXT N_( \
155 "If the cables between the satellilte low noise block-downconverter and " \
156 "the receiver are long, higher voltage may be required.\n" \
157 "Not all receivers support this.")
159 #define LNB_LOW_TEXT N_("Local oscillator low frequency (kHz)")
160 #define LNB_HIGH_TEXT N_("Local oscillator high frequency (kHz)")
161 #define LNB_LONGTEXT N_( \
162 "The downconverter (LNB) will substract the local oscillator frequency " \
163 "from the satellite transmission frequency. " \
164 "The intermediate frequency (IF) on the RF cable is the result.")
165 #define LNB_SWITCH_TEXT N_("Universal LNB switch frequency (kHz)")
166 #define LNB_SWITCH_LONGTEXT N_( \
167 "If the satellite transmission frequency exceeds the switch frequency, " \
168 "the oscillator high frequency will be used as reference. " \
169 "Furthermore the automatic continuous 22kHz tone will be sent.")
170 #define TONE_TEXT N_("Continuous 22kHz tone")
171 #define TONE_LONGTEXT N_( \
172 "A continuous tone at 22kHz can be sent on the cable. " \
173 "This normally selects the higher frequency band from a universal LNB.")
176 #define SATNO_TEXT N_("DiSEqC LNB number")
177 #define SATNO_LONGTEXT N_( \
178 "If the satellite receiver is connected to multiple " \
179 "low noise block-downconverters (LNB) through a DiSEqC 1.0 switch, " \
180 "the correct LNB can be selected (1 to 4). " \
181 "If there is no switch, this parameter should be 0.")
184 static int Open (vlc_object_t *);
185 static void Close (vlc_object_t *);
188 set_shortname (N_("DTV"))
189 set_description (N_("Digital Television and Radio (Linux DVB)"))
190 set_category (CAT_INPUT)
191 set_subcategory (SUBCAT_INPUT_ACCESS)
192 set_capability ("access", 0)
193 set_callbacks (Open, Close)
194 add_shortcut ("dtv", "tv", "dvb", /* "radio", "dab",*/
195 "cable", "dvb-c", "satellite", "dvb-s", "dvb-s2",
196 "terrestrial", "dvb-t", "atsc")
198 /* All options starting with dvb- can be overriden in the MRL, so they
199 * must all be "safe". Nevertheless, we do not mark as safe those that are
200 * really specific to the local system (e.g. device ID...).
201 * It wouldn't make sense to deliver those through a playlist. */
203 add_integer ("dvb-caching", DEFAULT_PTS_DELAY / 1000,
204 CACHING_TEXT, CACHING_LONGTEXT, true)
205 change_integer_range (0, 60000)
208 add_integer ("dvb-adapter", 0, ADAPTER_TEXT, ADAPTER_LONGTEXT, false)
209 change_integer_range (0, 255)
210 add_integer ("dvb-device", 0, DEVICE_TEXT, DEVICE_LONGTEXT, false)
211 change_integer_range (0, 255)
212 add_bool ("dvb-budget-mode", false, BUDGET_TEXT, BUDGET_LONGTEXT, true)
214 add_integer ("dvb-frequency", 0, FREQ_TEXT, FREQ_LONGTEXT, false)
215 change_integer_range (0, 107999999)
217 add_integer ("dvb-inversion", -1, INVERSION_TEXT, INVERSION_LONGTEXT, true)
218 change_integer_list (auto_off_on_vlc, auto_off_on_user)
221 set_section (N_("Terrestrial reception parameters"), NULL)
222 add_integer ("dvb-bandwidth", -1, BANDWIDTH_TEXT, BANDWIDTH_TEXT, true)
223 change_integer_list (bandwidth_vlc, bandwidth_user)
225 add_string ("dvb-code-rate-hp", "",
226 CODE_RATE_HP_TEXT, CODE_RATE_LONGTEXT, false)
227 change_string_list (code_rate_vlc, code_rate_user, NULL)
229 add_string ("dvb-code-rate-lp", "",
230 CODE_RATE_LP_TEXT, CODE_RATE_LONGTEXT, false)
231 change_string_list (code_rate_vlc, code_rate_user, NULL)
233 add_integer ("dvb-transmission", 0,
234 TRANSMISSION_TEXT, TRANSMISSION_TEXT, true)
235 change_integer_list (transmission_vlc, transmission_user)
237 add_string ("dvb-guard", "", GUARD_TEXT, GUARD_TEXT, true)
238 change_string_list (guard_vlc, guard_user, NULL)
240 add_integer ("dvb-hierarchy", -1, HIERARCHY_TEXT, HIERARCHY_TEXT, true)
241 change_integer_list (hierarchy_vlc, hierarchy_user)
244 set_section (N_("Cable and satellite reception parameters"), NULL)
245 add_string ("dvb-modulation", 0,
246 MODULATION_TEXT, MODULATION_LONGTEXT, false)
247 change_string_list (modulation_vlc, modulation_user, NULL)
249 add_integer ("dvb-srate", 0, SRATE_TEXT, SRATE_LONGTEXT, false)
250 change_integer_range (0, UINT64_C(0xffffffff))
252 add_string ("dvb-code-rate", "", CODE_RATE_TEXT, CODE_RATE_LONGTEXT, true)
253 change_string_list (code_rate_vlc, code_rate_user, NULL)
255 add_integer ("dvb-fec", 9, " ", " ", true)
256 change_integer_range (0, 9)
259 set_section (N_("DVB-S2 parameters"), NULL)
260 add_integer ("dvb-pilot", -1, PILOT_TEXT, PILOT_TEXT, true)
261 change_integer_list (auto_off_on_vlc, auto_off_on_user)
263 add_integer ("dvb-rolloff", -1, ROLLOFF_TEXT, ROLLOFF_TEXT, true)
264 change_integer_list (rolloff_vlc, rolloff_user)
266 set_section (N_("Satellite equipment control"), NULL)
267 add_string ("dvb-polarization", "",
268 POLARIZATION_TEXT, POLARIZATION_LONGTEXT, false)
269 change_string_list (polarization_vlc, polarization_user, NULL)
271 add_integer ("dvb-voltage", 13, " ", " ", true)
272 change_integer_range (0, 18)
276 add_bool ("dvb-high-voltage", false,
277 HIGH_VOLTAGE_TEXT, HIGH_VOLTAGE_LONGTEXT, false)
279 add_integer ("dvb-lnb-low", 0, LNB_LOW_TEXT, LNB_LONGTEXT, true)
280 change_integer_range (0, 0x7fffffff)
281 add_deprecated_alias ("dvb-lnb-lof1")
282 add_integer ("dvb-lnb-high", 0, LNB_HIGH_TEXT, LNB_LONGTEXT, true)
283 change_integer_range (0, 0x7fffffff)
284 add_deprecated_alias ("dvb-lnb-lof2")
285 add_integer ("dvb-lnb-switch", 11700000,
286 LNB_SWITCH_TEXT, LNB_SWITCH_LONGTEXT, true)
287 change_integer_range (0, 0x7fffffff)
288 add_deprecated_alias ("dvb-lnb-slof")
289 add_integer ("dvb-tone", -1, TONE_TEXT, TONE_LONGTEXT, true)
290 change_integer_list (auto_off_on_vlc, auto_off_on_user)
292 add_integer ("dvb-satno", 0, SATNO_TEXT, SATNO_LONGTEXT, true)
293 change_integer_range (0, 4)
305 int (*setup) (vlc_object_t *, dvb_device_t *, unsigned freq);
306 /* TODO: scan stuff */
309 static block_t *Read (access_t *);
310 static int Control (access_t *, int, va_list);
311 static const delsys_t *GuessSystem (const char *, dvb_device_t *);
312 static int Tune (vlc_object_t *, dvb_device_t *, const delsys_t *, unsigned);
313 static unsigned var_InheritFrequency (vlc_object_t *);
315 static int Open (vlc_object_t *obj)
317 access_t *access = (access_t *)obj;
318 access_sys_t *sys = malloc (sizeof (*sys));
319 if (unlikely(sys == NULL))
322 var_LocationParse (obj, access->psz_location, "dvb-");
323 unsigned freq = var_InheritFrequency (obj);
325 dvb_device_t *dev = dvb_open (obj, freq != 0);
337 const delsys_t *delsys = GuessSystem (access->psz_access, dev);
338 if (delsys == NULL || Tune (obj, dev, delsys, freq))
340 msg_Err (obj, "tuning to %u kHz failed", freq);
341 dialog_Fatal (obj, N_("Digital broadcasting"),
342 N_("The selected digital tuner does not support "
343 "the specified parameters.\n"
344 "Please check the preferences."));
348 dvb_add_pid (dev, 0);
350 access->pf_block = Read;
351 access->pf_control = Control;
352 if (access->psz_demux == NULL || !access->psz_demux[0])
354 free (access->psz_demux);
355 access->psz_demux = strdup ("ts");
361 access->p_sys = NULL;
365 static void Close (vlc_object_t *obj)
367 access_t *access = (access_t *)obj;
368 access_sys_t *sys = access->p_sys;
370 dvb_close (sys->dev);
374 static block_t *Read (access_t *access)
376 #define BUFSIZE (20*188)
377 block_t *block = block_Alloc (BUFSIZE);
378 if (unlikely(block == NULL))
381 access_sys_t *sys = access->p_sys;
382 ssize_t val = dvb_read (sys->dev, block->p_buffer, BUFSIZE);
387 access->info.b_eof = true;
388 block_Release (block);
392 block->i_buffer = val;
396 static int Control (access_t *access, int query, va_list args)
398 access_sys_t *sys = access->p_sys;
399 dvb_device_t *dev = sys->dev;
403 case ACCESS_CAN_SEEK:
404 case ACCESS_CAN_FASTSEEK:
405 case ACCESS_CAN_PAUSE:
406 case ACCESS_CAN_CONTROL_PACE:
408 bool *v = va_arg (args, bool *);
413 case ACCESS_GET_PTS_DELAY:
415 int64_t *v = va_arg (args, int64_t *);
416 *v = var_InheritInteger (access, "dvb-caching") * INT64_C(1000);
420 case ACCESS_GET_TITLE_INFO:
421 case ACCESS_GET_META:
424 case ACCESS_GET_CONTENT_TYPE:
426 char **pt = va_arg (args, char **);
427 *pt = strdup ("video/MP2T");
431 case ACCESS_SET_PAUSE_STATE:
432 case ACCESS_SET_TITLE:
433 case ACCESS_SET_SEEKPOINT:
436 case ACCESS_GET_SIGNAL:
437 *va_arg (args, double *) = dvb_get_snr (dev);
438 *va_arg (args, double *) = dvb_get_signal_strength (dev);
441 case ACCESS_SET_PRIVATE_ID_STATE:
443 unsigned pid = va_arg (args, unsigned);
444 bool add = va_arg (args, unsigned);
446 if (unlikely(pid > 0x1FFF))
450 if (dvb_add_pid (dev, pid))
454 dvb_remove_pid (dev, pid);
458 case ACCESS_SET_PRIVATE_ID_CA:
462 case ACCESS_GET_PRIVATE_ID_STATE:
466 msg_Warn (access, "unimplemented query %d in control", query);
471 /*** Generic tuning ***/
473 /** Determines which delivery system to use. */
474 static const delsys_t *GuessSystem (const char *scheme, dvb_device_t *dev)
476 /* NOTE: We should guess the delivery system for the "cable", "satellite"
477 * and "terrestrial" shortcuts (i.e. DVB, ISDB, ATSC...). But there is
478 * seemingly no sane way to do get the info with Linux DVB version 5.2.
479 * In particular, the frontend infos distinguish only the modulator class
480 * (QPSK, QAM, OFDM or ATSC).
482 * Furthermore, if the demodulator supports 2G, we cannot guess whether
483 * 1G or 2G is intended. For backward compatibility, 1G is assumed
484 * (this is not a limitation of Linux DVB). We will probably need something
485 * smarter when 2G (semi automatic) scanning is implemented. */
486 if (!strcasecmp (scheme, "cable"))
489 if (!strcasecmp (scheme, "satellite"))
492 if (!strcasecmp (scheme, "terrestrial"))
495 if (!strcasecmp (scheme, "atsc"))
497 if (!strcasecmp (scheme, "dvb-c"))
499 if (!strcasecmp (scheme, "dvb-s"))
501 if (!strcasecmp (scheme, "dvb-s2"))
503 if (!strcasecmp (scheme, "dvb-t"))
506 return dvb_guess_system (dev);
509 /** Set parameters and tune the device */
510 static int Tune (vlc_object_t *obj, dvb_device_t *dev, const delsys_t *delsys,
513 if (delsys->setup (obj, dev, freq)
514 || dvb_set_inversion (dev, var_InheritInteger (obj, "dvb-inversion"))
520 static unsigned var_InheritFrequency (vlc_object_t *obj)
522 unsigned freq = var_InheritInteger (obj, "dvb-frequency");
523 if (freq >= 108000000)
525 msg_Err (obj, "%u kHz frequency is too high.", freq);
527 msg_Info (obj, "Assuming %u kHz carrier frequency instead.", freq);
532 static char *var_InheritCodeRate (vlc_object_t *obj)
534 char *code_rate = var_InheritString (obj, "dvb-code-rate");
535 if (code_rate != NULL)
538 /* Backward compatibility with VLC < 1.2 (= Linux DVBv3 enum) */
539 unsigned fec = var_InheritInteger (obj, "dvb-fec");
542 static const char linux_dvb[9][5] = {
543 "none", "1/2", "2/3", "3/4", "4/5", "5/6", "6/7", "7/8" };
544 msg_Warn (obj, "\"fec=%u\" option is obsolete. "
545 "Use \"code-rate=%s\" instead.", fec, linux_dvb[fec]);
546 return strdup (linux_dvb[fec]);
551 static char *var_InheritModulation (vlc_object_t *obj)
553 char *mod = var_InheritString (obj, "dvb-modulation");
558 unsigned long l = strtol (mod, &end, 0);
559 if (*end != '\0') /* not a number = not from VLC < 1.2 */
562 /* Backward compatibility with VLC < 1.2 */
566 case -1: str = "QPSK"; break;
567 case 0: str = "QAM"; break;
568 case 8: str = "8VSB"; break;
569 case 16: str = "16QAM"; break;
570 case 32: str = "32QAM"; break;
571 case 64: str = "64QAM"; break;
572 case 128: str = "128QAM"; break;
573 case 256: str = "256QAM"; break;
577 msg_Warn (obj, "\"modulation=%ld\" option is obsolete. "
578 "Use \"modulation=%s\" instead.", l, str);
584 static int atsc_setup (vlc_object_t *obj, dvb_device_t *dev, unsigned freq)
586 char *mod = var_InheritModulation (obj);
588 int ret = dvb_set_atsc (dev, freq, mod);
593 const delsys_t atsc = { .setup = atsc_setup };
597 static int dvbc_setup (vlc_object_t *obj, dvb_device_t *dev, unsigned freq)
599 char *mod = var_InheritModulation (obj);
600 char *fec = var_InheritCodeRate (obj);
601 unsigned srate = var_InheritInteger (obj, "dvb-srate");
603 int ret = dvb_set_dvbc (dev, freq, mod, srate, fec);
609 const delsys_t dvbc = { .setup = dvbc_setup };
613 static char var_InheritPolarization (vlc_object_t *obj)
616 char *polstr = var_InheritString (obj, "dvb-polarization");
621 if (unlikely(pol >= 'a' && pol <= 'z'))
626 /* Backward compatibility with VLC for Linux < 1.2 */
627 unsigned voltage = var_InheritInteger (obj, "dvb-voltage");
630 case 13: pol = 'V'; break;
631 case 18: pol = 'H'; break;
635 msg_Warn (obj, "\"voltage=%u\" option is obsolete. "
636 "Use \"polarization=%c\" instead.", voltage, pol);
640 static int sec_setup (vlc_object_t *obj, dvb_device_t *dev, unsigned freq)
642 char pol = var_InheritPolarization (obj);
643 unsigned lowf = var_InheritInteger (obj, "dvb-lnb-low");
644 unsigned highf = var_InheritInteger (obj, "dvb-lnb-high");
645 unsigned switchf = var_InheritInteger (obj, "dvb-lnb-switch");
647 return dvb_set_sec (dev, freq, pol, lowf, highf, switchf);
650 static int dvbs_setup (vlc_object_t *obj, dvb_device_t *dev, unsigned freq)
652 char *fec = var_InheritCodeRate (obj);
653 uint32_t srate = var_InheritInteger (obj, "dvb-srate");
655 /* FIXME: adjust frequency (offset) */
656 int ret = dvb_set_dvbs (dev, freq, srate, fec);
659 ret = sec_setup (obj, dev, freq);
663 static int dvbs2_setup (vlc_object_t *obj, dvb_device_t *dev, unsigned freq)
665 char *mod = var_InheritModulation (obj);
666 char *fec = var_InheritCodeRate (obj);
667 uint32_t srate = var_InheritInteger (obj, "dvb-srate");
668 int pilot = var_InheritInteger (obj, "dvb-pilot");
669 int rolloff = var_InheritInteger (obj, "dvb-rolloff");
671 /* FIXME: adjust frequency (offset)? */
672 int ret = dvb_set_dvbs2 (dev, freq, mod, srate, fec, pilot, rolloff);
676 ret = sec_setup (obj, dev, freq);
680 const delsys_t dvbs = { .setup = dvbs_setup };
681 const delsys_t dvbs2 = { .setup = dvbs2_setup };
685 static int dvbt_setup (vlc_object_t *obj, dvb_device_t *dev, unsigned freq)
687 char *mod = var_InheritModulation (obj);
688 char *fec_hp = var_InheritString (obj, "dvb-code-rate-hp");
689 char *fec_lp = var_InheritString (obj, "dvb-code-rate-lp");
690 char *guard = var_InheritString (obj, "dvb-guard");
691 uint32_t bw = var_InheritInteger (obj, "dvb-bandwidth");
692 int tx = var_InheritInteger (obj, "dvb-transmission");
693 int h = var_InheritInteger (obj, "dvb-hierarchy");
695 int ret = dvb_set_dvbt (dev, freq, mod, fec_hp, fec_lp, bw, tx, guard, h);
703 const delsys_t dvbt = { .setup = dvbt_setup };