dvb-scan.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * Copyright (c) 2011-2014 - Mauro Carvalho Chehab
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation version 2
  7. * of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. * Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  18. */
  19. #ifndef _LIBSCAN_H
  20. #define _LIBSCAN_H
  21. #include <stdint.h>
  22. #include <linux/dvb/dmx.h>
  23. #include <libdvbv5/descriptors.h>
  24. #include <libdvbv5/dvb-sat.h>
  25. /**
  26. * @file dvb-scan.h
  27. * @ingroup frontend_scan
  28. * @brief Provides interfaces to scan programs inside MPEG-TS digital TV streams.
  29. * @copyright GNU General Public License version 2 (GPLv2)
  30. * @author Mauro Carvalho Chehab
  31. *
  32. * @par Bug Report
  33. * Please submit bug reports and patches to linux-media@vger.kernel.org
  34. */
  35. /* According with ISO/IEC 13818-1:2007 */
  36. #define MAX_TABLE_SIZE 1024 * 1024
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. struct dvb_entry;
  41. /**
  42. * @struct dvb_v5_descriptors_program
  43. * @brief Associates PMT with PAT tables
  44. * @ingroup frontend_scan
  45. *
  46. * @param pat_pgm pointer for PAT descriptor
  47. * @param pmt pointer for PMT descriptor
  48. */
  49. struct dvb_v5_descriptors_program {
  50. struct dvb_table_pat_program *pat_pgm;
  51. struct dvb_table_pmt *pmt;
  52. };
  53. /**
  54. * @struct dvb_v5_descriptors
  55. * @brief Contains the descriptors needed to scan the Service ID and other relevant info at a MPEG-TS Digital TV stream
  56. * @ingroup frontend_scan
  57. *
  58. * @param delivery_system Delivery system of the parsed MPEG-TS
  59. * @param entry struct dvb_entry pointer (see dvb-file.h)
  60. * @param pat PAT table descriptor pointer
  61. * @param vct VCT table descriptor pointer
  62. * @param program PAT/PMT array associated programs found at MPEG-TS
  63. * @param nit NIT table descriptor pointer
  64. * @param sdt SDT table descriptor pointer
  65. * @param num_program Number of program entries at program array.
  66. *
  67. * Those descriptors are filled by the scan routines when the tables are
  68. * found. Otherwise, they're NULL.
  69. */
  70. struct dvb_v5_descriptors {
  71. uint32_t delivery_system;
  72. struct dvb_entry *entry;
  73. unsigned num_entry;
  74. struct dvb_table_pat *pat;
  75. struct atsc_table_vct *vct;
  76. struct dvb_v5_descriptors_program *program;
  77. struct dvb_table_nit *nit;
  78. struct dvb_table_sdt *sdt;
  79. unsigned num_program;
  80. };
  81. /**
  82. * @struct dvb_table_filter
  83. * @brief Describes the PES filters used by DVB scan
  84. * @ingroup frontend_scan
  85. *
  86. * @param tid Table ID
  87. * @param pid Program ID
  88. * @param ts_id Table section ID (for multisession filtering). If no
  89. * specific table section is needed, -1 should be used
  90. * @param table pointer to a pointer for the table struct to be filled
  91. * @param allow_section_gaps Allow non-continuous section numbering
  92. * @param priv Internal structure used inside the DVB core. shouldn't
  93. * be touched externally.
  94. */
  95. struct dvb_table_filter {
  96. /* Input data */
  97. unsigned char tid;
  98. uint16_t pid;
  99. int ts_id;
  100. void **table;
  101. int allow_section_gaps;
  102. /*
  103. * Private temp data used by dvb_read_sections().
  104. * Should not be filled outside dvb-scan.c, as they'll be
  105. * overrided
  106. */
  107. void *priv;
  108. };
  109. /**
  110. * @brief deallocates all data associated with a table filter
  111. * @ingroup frontend_scan
  112. *
  113. * @param sect table filter pointer
  114. */
  115. void dvb_table_filter_free(struct dvb_table_filter *sect);
  116. /**
  117. * @brief read MPEG-TS tables that comes from a DTV card
  118. * @ingroup frontend_scan
  119. *
  120. * @param parms pointer to struct dvb_v5_fe_parms created when the
  121. * frontend is opened
  122. * @param dmx_fd an opened demux file descriptor
  123. * @param tid Table ID
  124. * @param pid Program ID
  125. * @param table pointer to a pointer for the table struct to be filled
  126. * @param timeout Limit, in seconds, to read a MPEG-TS table
  127. *
  128. * This function is used to read the DVB tables by specifying a table ID and
  129. * a program ID. The libdvbv5 should have a parser for the descriptors of the
  130. * table type that should be parsed.
  131. * The table will be automatically allocated on success.
  132. * The function will read on the specified demux and return when reading is
  133. * done or an error has occurred. If table is not NULL after the call, it has
  134. * to be freed with the apropriate free table function (even if an error has
  135. * occurred).
  136. *
  137. * If the application wants to abort the read operation, it can change the
  138. * value of parms->p.abort to 1.
  139. *
  140. * Returns 0 on success or a negative error code.
  141. *
  142. * Example usage:
  143. * @code
  144. * struct dvb_table_pat *pat;
  145. * int r = dvb_read_section( parms, dmx_fd, DVB_TABLE_PAT, DVB_TABLE_PAT_PID,
  146. * (void **) &pat, 5 );
  147. * if (r < 0)
  148. * dvb_logerr("error reading PAT table");
  149. * else {
  150. * // do something with pat
  151. * }
  152. * if (pat)
  153. * dvb_table_pat_free( pat );
  154. * @endcode
  155. */
  156. int dvb_read_section(struct dvb_v5_fe_parms *parms, int dmx_fd,
  157. unsigned char tid, uint16_t pid, void **table,
  158. unsigned timeout);
  159. /**
  160. * @brief read MPEG-TS tables that comes from a DTV card
  161. * with an specific table section ID
  162. * @ingroup frontend_scan
  163. *
  164. * @param parms pointer to struct dvb_v5_fe_parms created when the
  165. * frontend is opened
  166. * @param dmx_fd an opened demux file descriptor
  167. * @param tid Table ID
  168. * @param pid Program ID
  169. * @param ts_id Table section ID (for multisession filtering). If no
  170. * specific table section is needed, -1 should be used
  171. * @param table pointer to a pointer for the table struct to be filled
  172. * @param timeout limit, in seconds, to read a MPEG-TS table
  173. *
  174. * This is a variant of dvb_read_section() that also seeks for an specific
  175. * table section ID given by ts_id.
  176. */
  177. int dvb_read_section_with_id(struct dvb_v5_fe_parms *parms, int dmx_fd,
  178. unsigned char tid, uint16_t pid, int ts_id,
  179. void **table, unsigned timeout);
  180. /**
  181. * @brief read MPEG-TS tables that comes from a DTV card
  182. * @ingroup frontend_scan
  183. *
  184. * @param parms pointer to struct dvb_v5_fe_parms created when the
  185. * frontend is opened
  186. * @param dmx_fd an opened demux file descriptor
  187. * @param sect section filter pointer
  188. * @param timeout limit, in seconds, to read a MPEG-TS table
  189. *
  190. * This is a variant of dvb_read_section() that uses a struct dvb_table_filter
  191. * to specify the filter to use.
  192. */
  193. int dvb_read_sections(struct dvb_v5_fe_parms *parms, int dmx_fd,
  194. struct dvb_table_filter *sect,
  195. unsigned timeout);
  196. /**
  197. * @brief allocates a struct dvb_v5_descriptors
  198. * @ingroup frontend_scan
  199. *
  200. * @param delivery_system Delivery system to be used on the table
  201. *
  202. * At success, returns a pointer. NULL otherwise.
  203. */
  204. struct dvb_v5_descriptors *dvb_scan_alloc_handler_table(uint32_t delivery_system);
  205. /**
  206. * @brief frees a struct dvb_v5_descriptors
  207. * @ingroup frontend_scan
  208. *
  209. * @param dvb_scan_handler pointer to the struct to be freed.
  210. */
  211. void dvb_scan_free_handler_table(struct dvb_v5_descriptors *dvb_scan_handler);
  212. /**
  213. * @brief Scans a DVB stream, looking for the tables needed to
  214. * identify the programs inside a MPEG-TS
  215. * @ingroup frontend_scan
  216. *
  217. * @param parms pointer to struct dvb_v5_fe_parms created when
  218. * the frontend is opened
  219. * @param dmx_fd an opened demux file descriptor
  220. * @param delivery_system delivery system to be scanned
  221. * @param other_nit use alternate table IDs for NIT and other tables
  222. * @param timeout_multiply improves the timeout for each table reception
  223. * by using a value that will multiply the wait
  224. * time.
  225. *
  226. * Given an opened frontend and demux, this function seeks for all programs
  227. * available at the transport stream, and parses the following tables:
  228. * PAT, PMT, NIT, SDT (and VCT, if the delivery system is ATSC).
  229. *
  230. * On sucess, it returns a pointer to a struct dvb_v5_descriptors, that can
  231. * either be used to tune into a service or to be stored inside a file.
  232. */
  233. struct dvb_v5_descriptors *dvb_get_ts_tables(struct dvb_v5_fe_parms *parms, int dmx_fd,
  234. uint32_t delivery_system,
  235. unsigned other_nit,
  236. unsigned timeout_multiply);
  237. /**
  238. * @brief frees a struct dvb_v5_descriptors
  239. * @ingroup frontend_scan
  240. *
  241. * @param dvb_desc pointed to the structure to be freed.
  242. *
  243. * This function recursively frees everything that is allocated by
  244. * dvb_get_ts_tables() and stored at dvb_desc, including dvb_desc itself.
  245. */
  246. void dvb_free_ts_tables(struct dvb_v5_descriptors *dvb_desc);
  247. /**
  248. * @brief Callback for the application to show the frontend status
  249. * @ingroup frontend_scan
  250. *
  251. * @param args a pointer, opaque to libdvbv5, to be used by the
  252. * application if needed.
  253. * @param parms pointer to struct dvb_v5_fe_parms created when the
  254. * frontend is opened
  255. */
  256. typedef int (check_frontend_t)(void *args, struct dvb_v5_fe_parms *parms);
  257. /**
  258. * @brief Scans a DVB dvb_add_scaned_transponder
  259. * @ingroup frontend_scan
  260. *
  261. * @param parms pointer to struct dvb_v5_fe_parms created when the
  262. * frontend is opened
  263. * @param entry DVB file entry that corresponds to a transponder to be
  264. * tuned
  265. * @param dmx_fd an opened demux file descriptor
  266. * @param check_frontend a pointer to a function that will show the frontend
  267. * status while tuning into a transponder
  268. * @param args a pointer, opaque to libdvbv5, that will be used when
  269. * calling check_frontend. It should contain any parameters
  270. * that could be needed by check_frontend.
  271. * @param other_nit Use alternate table IDs for NIT and other tables
  272. * @param timeout_multiply Improves the timeout for each table reception, by
  273. *
  274. * This is the function that applications should use when doing a transponders
  275. * scan. It does everything needed to fill the entries with DVB programs
  276. * (virtual channels) and detect the PIDs associated with them.
  277. *
  278. * A typical usage is to after open a channel file, open a dmx_fd and open
  279. * a frontend. Then, seek for the MPEG tables on all the transponder
  280. * frequencies with:
  281. *
  282. * @code
  283. * for (entry = dvb_file->first_entry; entry != NULL; entry = entry->next) {
  284. * struct dvb_v5_descriptors *dvb_scan_handler = NULL;
  285. *
  286. * dvb_scan_handler = dvb_scan_transponder(parms, entry, dmx_fd,
  287. * &check_frontend, args,
  288. * args->other_nit,
  289. * args->timeout_multiply);
  290. * if (parms->abort) {
  291. * dvb_scan_free_handler_table(dvb_scan_handler);
  292. * break;
  293. * }
  294. * if (dvb_scan_handler) {
  295. * dvb_store_channel(&dvb_file_new, parms, dvb_scan_handler,
  296. * args->get_detected, args->get_nit);
  297. * dvb_scan_free_handler_table(dvb_scan_handler);
  298. * }
  299. * }
  300. * @endcode
  301. */
  302. struct dvb_v5_descriptors *dvb_scan_transponder(struct dvb_v5_fe_parms *parms,
  303. struct dvb_entry *entry,
  304. int dmx_fd,
  305. check_frontend_t *check_frontend,
  306. void *args,
  307. unsigned other_nit,
  308. unsigned timeout_multiply);
  309. /**
  310. * @brief Add new transponders to a dvb_file
  311. * @ingroup frontend_scan
  312. *
  313. * @param parms pointer to struct dvb_v5_fe_parms created when the
  314. * frontend is opened
  315. * @param dvb_scan_handler pointer to a struct dvb_v5_descriptors containing
  316. * scaned MPEG-TS
  317. * @param first_entry first entry of a DVB file struct
  318. * @param entry current entry on a DVB file struct
  319. *
  320. * When the NIT table is parsed, some new transponders could be described
  321. * inside. This function adds new entries to a dvb_file struct, pointing
  322. * to those new transponders. It is used inside the scan loop, as shown at
  323. * the dvb_scan_transponder(), to add new channels.
  324. *
  325. * Example:
  326. * @code
  327. * for (entry = dvb_file->first_entry; entry != NULL; entry = entry->next) {
  328. * struct dvb_v5_descriptors *dvb_scan_handler = NULL;
  329. *
  330. * dvb_scan_handler = dvb_scan_transponder(parms, entry, dmx_fd,
  331. * &check_frontend, args,
  332. * args->other_nit,
  333. * args->timeout_multiply);
  334. * if (parms->abort) {
  335. * dvb_scan_free_handler_table(dvb_scan_handler);
  336. * break;
  337. * }
  338. * if (dvb_scan_handler) {
  339. * dvb_store_channel(&dvb_file_new, parms, dvb_scan_handler,
  340. * args->get_detected, args->get_nit);
  341. * dvb_scan_free_handler_table(dvb_scan_handler);
  342. *
  343. * dvb_add_scaned_transponders(parms, dvb_scan_handler,
  344. * dvb_file->first_entry, entry);
  345. *
  346. * dvb_scan_free_handler_table(dvb_scan_handler);
  347. * }
  348. * }
  349. * @endcode
  350. */
  351. void dvb_add_scaned_transponders(struct dvb_v5_fe_parms *parms,
  352. struct dvb_v5_descriptors *dvb_scan_handler,
  353. struct dvb_entry *first_entry,
  354. struct dvb_entry *entry);
  355. #ifndef _DOXYGEN
  356. /*
  357. * Some ancillary functions used internally inside the library, used to
  358. * identify duplicated transport streams and add new found transponder entries
  359. */
  360. int dvb_estimate_freq_shift(struct dvb_v5_fe_parms *parms);
  361. int dvb_new_freq_is_needed(struct dvb_entry *entry, struct dvb_entry *last_entry,
  362. uint32_t freq, enum dvb_sat_polarization pol, int shift);
  363. struct dvb_entry *dvb_scan_add_entry(struct dvb_v5_fe_parms *parms,
  364. struct dvb_entry *first_entry,
  365. struct dvb_entry *entry,
  366. uint32_t freq, uint32_t shift,
  367. enum dvb_sat_polarization pol);
  368. int dvb_new_entry_is_needed(struct dvb_entry *entry,
  369. struct dvb_entry *last_entry,
  370. uint32_t freq, int shift,
  371. enum dvb_sat_polarization pol, uint32_t stream_id);
  372. struct dvb_entry *dvb_scan_add_entry_ex(struct dvb_v5_fe_parms *parms,
  373. struct dvb_entry *first_entry,
  374. struct dvb_entry *entry,
  375. uint32_t freq, uint32_t shift,
  376. enum dvb_sat_polarization pol,
  377. uint32_t stream_id);
  378. void dvb_update_transponders(struct dvb_v5_fe_parms *parms,
  379. struct dvb_v5_descriptors *dvb_scan_handler,
  380. struct dvb_entry *first_entry,
  381. struct dvb_entry *entry);
  382. #endif
  383. #ifdef __cplusplus
  384. }
  385. #endif
  386. #endif