vidtv.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ================================
  3. vidtv: Virtual Digital TV driver
  4. ================================
  5. Author: Daniel W. S. Almeida <[email protected]>, June 2020.
  6. Background
  7. ----------
  8. Vidtv is a virtual DVB driver that aims to serve as a reference for driver
  9. writers by serving as a template. It also validates the existing media DVB
  10. APIs, thus helping userspace application writers.
  11. Currently, it consists of:
  12. - A fake tuner driver, which will report a bad signal quality if the chosen
  13. frequency is too far away from a table of valid frequencies for a
  14. particular delivery system.
  15. - A fake demod driver, which will constantly poll the fake signal quality
  16. returned by the tuner, simulating a device that can lose/reacquire a lock
  17. on the signal depending on the CNR levels.
  18. - A fake bridge driver, which is the module responsible for modprobing the
  19. fake tuner and demod modules and implementing the demux logic. This module
  20. takes parameters at initialization that will dictate how the simulation
  21. behaves.
  22. - Code reponsible for encoding a valid MPEG Transport Stream, which is then
  23. passed to the bridge driver. This fake stream contains some hardcoded content.
  24. For now, we have a single, audio-only channel containing a single MPEG
  25. Elementary Stream, which in turn contains a SMPTE 302m encoded sine-wave.
  26. Note that this particular encoder was chosen because it is the easiest
  27. way to encode PCM audio data in a MPEG Transport Stream.
  28. Building vidtv
  29. --------------
  30. vidtv is a test driver and thus is **not** enabled by default when
  31. compiling the kernel.
  32. In order to enable compilation of vidtv:
  33. - Enable **DVB_TEST_DRIVERS**, then
  34. - Enable **DVB_VIDTV**
  35. When compiled as a module, expect the following .ko files:
  36. - dvb_vidtv_tuner.ko
  37. - dvb_vidtv_demod.ko
  38. - dvb_vidtv_bridge.ko
  39. Running vidtv
  40. -------------
  41. When compiled as a module, run::
  42. modprobe vidtv
  43. That's it! The bridge driver will initialize the tuner and demod drivers as
  44. part of its own initialization.
  45. By default, it will accept the following frequencies:
  46. - 474 MHz for DVB-T/T2/C;
  47. - 11,362 GHz for DVB-S/S2.
  48. For satellite systems, the driver simulates an universal extended
  49. LNBf, with frequencies at Ku-Band, ranging from 10.7 GHz to 12.75 GHz.
  50. You can optionally define some command-line arguments to vidtv.
  51. Command-line arguments to vidtv
  52. -------------------------------
  53. Below is a list of all arguments that can be supplied to vidtv:
  54. drop_tslock_prob_on_low_snr
  55. Probability of losing the TS lock if the signal quality is bad.
  56. This probability be used by the fake demodulator driver to
  57. eventually return a status of 0 when the signal quality is not
  58. good.
  59. recover_tslock_prob_on_good_snr:
  60. Probability recovering the TS lock when the signal improves. This
  61. probability be used by the fake demodulator driver to eventually
  62. return a status of 0x1f when/if the signal quality improves.
  63. mock_power_up_delay_msec
  64. Simulate a power up delay. Default: 0.
  65. mock_tune_delay_msec
  66. Simulate a tune delay. Default 0.
  67. vidtv_valid_dvb_t_freqs
  68. Valid DVB-T frequencies to simulate, in Hz.
  69. vidtv_valid_dvb_c_freqs
  70. Valid DVB-C frequencies to simulate, in Hz.
  71. vidtv_valid_dvb_s_freqs
  72. Valid DVB-S/S2 frequencies to simulate at Ku-Band, in kHz.
  73. max_frequency_shift_hz,
  74. Maximum shift in HZ allowed when tuning in a channel.
  75. si_period_msec
  76. How often to send SI packets. Default: 40ms.
  77. pcr_period_msec
  78. How often to send PCR packets. Default: 40ms.
  79. mux_rate_kbytes_sec
  80. Attempt to maintain this bit rate by inserting TS null packets, if
  81. necessary. Default: 4096.
  82. pcr_pid,
  83. PCR PID for all channels. Default: 0x200.
  84. mux_buf_sz_pkts,
  85. Size for the mux buffer in multiples of 188 bytes.
  86. vidtv internal structure
  87. ------------------------
  88. The kernel modules are split in the following way:
  89. vidtv_tuner.[ch]
  90. Implements a fake tuner DVB driver.
  91. vidtv_demod.[ch]
  92. Implements a fake demodulator DVB driver.
  93. vidtv_bridge.[ch]
  94. Implements a bridge driver.
  95. The MPEG related code is split in the following way:
  96. vidtv_ts.[ch]
  97. Code to work with MPEG TS packets, such as TS headers, adaptation
  98. fields, PCR packets and NULL packets.
  99. vidtv_psi.[ch]
  100. This is the PSI generator. PSI packets contain general information
  101. about a MPEG Transport Stream. A PSI generator is needed so
  102. userspace apps can retrieve information about the Transport Stream
  103. and eventually tune into a (dummy) channel.
  104. Because the generator is implemented in a separate file, it can be
  105. reused elsewhere in the media subsystem.
  106. Currently vidtv supports working with 5 PSI tables: PAT, PMT,
  107. SDT, NIT and EIT.
  108. The specification for PAT and PMT can be found in *ISO 13818-1:
  109. Systems*, while the specification for the SDT, NIT, EIT can be found in *ETSI
  110. EN 300 468: Specification for Service Information (SI) in DVB
  111. systems*.
  112. It isn't strictly necessary, but using a real TS file helps when
  113. debugging PSI tables. Vidtv currently tries to replicate the PSI
  114. structure found in this file: `TS1Globo.ts
  115. <https://tsduck.io/streams/brazil-isdb-tb/TS1globo.ts>`_.
  116. A good way to visualize the structure of streams is by using
  117. `DVBInspector <https://sourceforge.net/projects/dvbinspector/>`_.
  118. vidtv_pes.[ch]
  119. Implements the PES logic to convert encoder data into MPEG TS
  120. packets. These can then be fed into a TS multiplexer and eventually
  121. into userspace.
  122. vidtv_encoder.h
  123. An interface for vidtv encoders. New encoders can be added to this
  124. driver by implementing the calls in this file.
  125. vidtv_s302m.[ch]
  126. Implements a S302M encoder to make it possible to insert PCM audio
  127. data in the generated MPEG Transport Stream. The relevant
  128. specification is available online as *SMPTE 302M-2007: Television -
  129. Mapping of AES3 Data into MPEG-2 Transport Stream*.
  130. The resulting MPEG Elementary Stream is conveyed in a private
  131. stream with a S302M registration descriptor attached.
  132. This shall enable passing an audio signal into userspace so it can
  133. be decoded and played by media software. The corresponding decoder
  134. in ffmpeg is located in 'libavcodec/s302m.c' and is experimental.
  135. vidtv_channel.[ch]
  136. Implements a 'channel' abstraction.
  137. When vidtv boots, it will create some hardcoded channels:
  138. #. Their services will be concatenated to populate the SDT.
  139. #. Their programs will be concatenated to populate the PAT
  140. #. Their events will be concatenated to populate the EIT
  141. #. For each program in the PAT, a PMT section will be created
  142. #. The PMT section for a channel will be assigned its streams.
  143. #. Every stream will have its corresponding encoder polled in a
  144. loop to produce TS packets.
  145. These packets may be interleaved by the muxer and then delivered
  146. to the bridge.
  147. vidtv_mux.[ch]
  148. Implements a MPEG TS mux, loosely based on the ffmpeg
  149. implementation in "libavcodec/mpegtsenc.c"
  150. The muxer runs a loop which is responsible for:
  151. #. Keeping track of the amount of time elapsed since the last
  152. iteration.
  153. #. Polling encoders in order to fetch 'elapsed_time' worth of data.
  154. #. Inserting PSI and/or PCR packets, if needed.
  155. #. Padding the resulting stream with NULL packets if
  156. necessary in order to maintain the chosen bit rate.
  157. #. Delivering the resulting TS packets to the bridge
  158. driver so it can pass them to the demux.
  159. Testing vidtv with v4l-utils
  160. ----------------------------
  161. Using the tools in v4l-utils is a great way to test and inspect the output of
  162. vidtv. It is hosted here: `v4l-utils Documentation
  163. <https://linuxtv.org/wiki/index.php/V4l-utils>`_.
  164. From its webpage::
  165. The v4l-utils are a series of packages for handling media devices.
  166. It is hosted at http://git.linuxtv.org/v4l-utils.git, and packaged
  167. on most distributions.
  168. It provides a series of libraries and utilities to be used to
  169. control several aspect of the media boards.
  170. Start by installing v4l-utils and then modprobing vidtv::
  171. modprobe dvb_vidtv_bridge
  172. If the driver is OK, it should load and its probing code will run. This will
  173. pull in the tuner and demod drivers.
  174. Using dvb-fe-tool
  175. ~~~~~~~~~~~~~~~~~
  176. The first step to check whether the demod loaded successfully is to run::
  177. $ dvb-fe-tool
  178. Device Dummy demod for DVB-T/T2/C/S/S2 (/dev/dvb/adapter0/frontend0) capabilities:
  179. CAN_FEC_1_2
  180. CAN_FEC_2_3
  181. CAN_FEC_3_4
  182. CAN_FEC_4_5
  183. CAN_FEC_5_6
  184. CAN_FEC_6_7
  185. CAN_FEC_7_8
  186. CAN_FEC_8_9
  187. CAN_FEC_AUTO
  188. CAN_GUARD_INTERVAL_AUTO
  189. CAN_HIERARCHY_AUTO
  190. CAN_INVERSION_AUTO
  191. CAN_QAM_16
  192. CAN_QAM_32
  193. CAN_QAM_64
  194. CAN_QAM_128
  195. CAN_QAM_256
  196. CAN_QAM_AUTO
  197. CAN_QPSK
  198. CAN_TRANSMISSION_MODE_AUTO
  199. DVB API Version 5.11, Current v5 delivery system: DVBC/ANNEX_A
  200. Supported delivery systems:
  201. DVBT
  202. DVBT2
  203. [DVBC/ANNEX_A]
  204. DVBS
  205. DVBS2
  206. Frequency range for the current standard:
  207. From: 51.0 MHz
  208. To: 2.15 GHz
  209. Step: 62.5 kHz
  210. Tolerance: 29.5 MHz
  211. Symbol rate ranges for the current standard:
  212. From: 1.00 MBauds
  213. To: 45.0 MBauds
  214. This should return what is currently set up at the demod struct, i.e.::
  215. static const struct dvb_frontend_ops vidtv_demod_ops = {
  216. .delsys = {
  217. SYS_DVBT,
  218. SYS_DVBT2,
  219. SYS_DVBC_ANNEX_A,
  220. SYS_DVBS,
  221. SYS_DVBS2,
  222. },
  223. .info = {
  224. .name = "Dummy demod for DVB-T/T2/C/S/S2",
  225. .frequency_min_hz = 51 * MHz,
  226. .frequency_max_hz = 2150 * MHz,
  227. .frequency_stepsize_hz = 62500,
  228. .frequency_tolerance_hz = 29500 * kHz,
  229. .symbol_rate_min = 1000000,
  230. .symbol_rate_max = 45000000,
  231. .caps = FE_CAN_FEC_1_2 |
  232. FE_CAN_FEC_2_3 |
  233. FE_CAN_FEC_3_4 |
  234. FE_CAN_FEC_4_5 |
  235. FE_CAN_FEC_5_6 |
  236. FE_CAN_FEC_6_7 |
  237. FE_CAN_FEC_7_8 |
  238. FE_CAN_FEC_8_9 |
  239. FE_CAN_QAM_16 |
  240. FE_CAN_QAM_64 |
  241. FE_CAN_QAM_32 |
  242. FE_CAN_QAM_128 |
  243. FE_CAN_QAM_256 |
  244. FE_CAN_QAM_AUTO |
  245. FE_CAN_QPSK |
  246. FE_CAN_FEC_AUTO |
  247. FE_CAN_INVERSION_AUTO |
  248. FE_CAN_TRANSMISSION_MODE_AUTO |
  249. FE_CAN_GUARD_INTERVAL_AUTO |
  250. FE_CAN_HIERARCHY_AUTO,
  251. }
  252. ....
  253. For more information on dvb-fe-tools check its online documentation here:
  254. `dvb-fe-tool Documentation
  255. <https://www.linuxtv.org/wiki/index.php/Dvb-fe-tool>`_.
  256. Using dvb-scan
  257. ~~~~~~~~~~~~~~
  258. In order to tune into a channel and read the PSI tables, we can use dvb-scan.
  259. For this, one should provide a configuration file known as a 'scan file',
  260. here's an example::
  261. [Channel]
  262. FREQUENCY = 474000000
  263. MODULATION = QAM/AUTO
  264. SYMBOL_RATE = 6940000
  265. INNER_FEC = AUTO
  266. DELIVERY_SYSTEM = DVBC/ANNEX_A
  267. .. note::
  268. The parameters depend on the video standard you're testing.
  269. .. note::
  270. Vidtv is a fake driver and does not validate much of the information
  271. in the scan file. Just specifying 'FREQUENCY' and 'DELIVERY_SYSTEM'
  272. should be enough for DVB-T/DVB-T2. For DVB-S/DVB-C however, you
  273. should also provide 'SYMBOL_RATE'.
  274. You can browse scan tables online here: `dvb-scan-tables
  275. <https://git.linuxtv.org/dtv-scan-tables.git>`_.
  276. Assuming this channel is named 'channel.conf', you can then run::
  277. $ dvbv5-scan channel.conf
  278. dvbv5-scan ~/vidtv.conf
  279. ERROR command BANDWIDTH_HZ (5) not found during retrieve
  280. Cannot calc frequency shift. Either bandwidth/symbol-rate is unavailable (yet).
  281. Scanning frequency #1 330000000
  282. (0x00) Signal= -68.00dBm
  283. Scanning frequency #2 474000000
  284. Lock (0x1f) Signal= -34.45dBm C/N= 33.74dB UCB= 0
  285. Service Beethoven, provider LinuxTV.org: digital television
  286. For more information on dvb-scan, check its documentation online here:
  287. `dvb-scan Documentation <https://www.linuxtv.org/wiki/index.php/Dvbscan>`_.
  288. Using dvb-zap
  289. ~~~~~~~~~~~~~
  290. dvbv5-zap is a command line tool that can be used to record MPEG-TS to disk. The
  291. typical use is to tune into a channel and put it into record mode. The example
  292. below - which is taken from the documentation - illustrates that\ [1]_::
  293. $ dvbv5-zap -c dvb_channel.conf "beethoven" -o music.ts -P -t 10
  294. using demux 'dvb0.demux0'
  295. reading channels from file 'dvb_channel.conf'
  296. tuning to 474000000 Hz
  297. pass all PID's to TS
  298. dvb_set_pesfilter 8192
  299. dvb_dev_set_bufsize: buffer set to 6160384
  300. Lock (0x1f) Quality= Good Signal= -34.66dBm C/N= 33.41dB UCB= 0 postBER= 0 preBER= 1.05x10^-3 PER= 0
  301. Lock (0x1f) Quality= Good Signal= -34.57dBm C/N= 33.46dB UCB= 0 postBER= 0 preBER= 1.05x10^-3 PER= 0
  302. Record to file 'music.ts' started
  303. received 24587768 bytes (2401 Kbytes/sec)
  304. Lock (0x1f) Quality= Good Signal= -34.42dBm C/N= 33.89dB UCB= 0 postBER= 0 preBER= 2.44x10^-3 PER= 0
  305. .. [1] In this example, it records 10 seconds with all program ID's stored
  306. at the music.ts file.
  307. The channel can be watched by playing the contents of the stream with some
  308. player that recognizes the MPEG-TS format, such as ``mplayer`` or ``vlc``.
  309. By playing the contents of the stream one can visually inspect the workings of
  310. vidtv, e.g., to play a recorded TS file with::
  311. $ mplayer music.ts
  312. or, alternatively, running this command on one terminal::
  313. $ dvbv5-zap -c dvb_channel.conf "beethoven" -P -r &
  314. And, on a second terminal, playing the contents from DVR interface with::
  315. $ mplayer /dev/dvb/adapter0/dvr0
  316. For more information on dvb-zap check its online documentation here:
  317. `dvb-zap Documentation
  318. <https://www.linuxtv.org/wiki/index.php/Dvbv5-zap>`_.
  319. See also: `zap <https://www.linuxtv.org/wiki/index.php/Zap>`_.
  320. What can still be improved in vidtv
  321. -----------------------------------
  322. Add *debugfs* integration
  323. ~~~~~~~~~~~~~~~~~~~~~~~~~
  324. Although frontend drivers provide DVBv5 statistics via the .read_status
  325. call, a nice addition would be to make additional statistics available to
  326. userspace via debugfs, which is a simple-to-use, RAM-based filesystem
  327. specifically designed for debug purposes.
  328. The logic for this would be implemented on a separate file so as not to
  329. pollute the frontend driver. These statistics are driver-specific and can
  330. be useful during tests.
  331. The Siano driver is one example of a driver using
  332. debugfs to convey driver-specific statistics to userspace and it can be
  333. used as a reference.
  334. This should be further enabled and disabled via a Kconfig
  335. option for convenience.
  336. Add a way to test video
  337. ~~~~~~~~~~~~~~~~~~~~~~~
  338. Currently, vidtv can only encode PCM audio. It would be great to implement
  339. a barebones version of MPEG-2 video encoding so we can also test video. The
  340. first place to look into is *ISO 13818-2: Information technology — Generic
  341. coding of moving pictures and associated audio information — Part 2: Video*,
  342. which covers the encoding of compressed video in MPEG Transport Streams.
  343. This might optionally use the Video4Linux2 Test Pattern Generator, v4l2-tpg,
  344. which resides at::
  345. drivers/media/common/v4l2-tpg/
  346. Add white noise simulation
  347. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  348. The vidtv tuner already has code to identify whether the chosen frequency
  349. is too far away from a table of valid frequencies. For now, this means that
  350. the demodulator can eventually lose the lock on the signal, since the tuner will
  351. report a bad signal quality.
  352. A nice addition is to simulate some noise when the signal quality is bad by:
  353. - Randomly dropping some TS packets. This will trigger a continuity error if the
  354. continuity counter is updated but the packet is not passed on to the demux.
  355. - Updating the error statistics accordingly (e.g. BER, etc).
  356. - Simulating some noise in the encoded data.
  357. Functions and structs used within vidtv
  358. ---------------------------------------
  359. .. kernel-doc:: drivers/media/test-drivers/vidtv/vidtv_bridge.h
  360. .. kernel-doc:: drivers/media/test-drivers/vidtv/vidtv_channel.h
  361. .. kernel-doc:: drivers/media/test-drivers/vidtv/vidtv_demod.h
  362. .. kernel-doc:: drivers/media/test-drivers/vidtv/vidtv_encoder.h
  363. .. kernel-doc:: drivers/media/test-drivers/vidtv/vidtv_mux.h
  364. .. kernel-doc:: drivers/media/test-drivers/vidtv/vidtv_pes.h
  365. .. kernel-doc:: drivers/media/test-drivers/vidtv/vidtv_psi.h
  366. .. kernel-doc:: drivers/media/test-drivers/vidtv/vidtv_s302m.h
  367. .. kernel-doc:: drivers/media/test-drivers/vidtv/vidtv_ts.h
  368. .. kernel-doc:: drivers/media/test-drivers/vidtv/vidtv_tuner.h
  369. .. kernel-doc:: drivers/media/test-drivers/vidtv/vidtv_common.c
  370. .. kernel-doc:: drivers/media/test-drivers/vidtv/vidtv_tuner.c