media_index.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Kinsey Moore <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. * \brief Sound file format and description indexer.
  20. */
  21. #include "asterisk.h"
  22. #include <dirent.h>
  23. #include <sys/stat.h>
  24. #include "asterisk/utils.h"
  25. #include "asterisk/lock.h"
  26. #include "asterisk/format.h"
  27. #include "asterisk/format_cap.h"
  28. #include "asterisk/media_index.h"
  29. #include "asterisk/file.h"
  30. /*** MODULEINFO
  31. <support_level>core</support_level>
  32. ***/
  33. /*! \brief The number of buckets to be used for storing variant-keyed objects */
  34. #define VARIANT_BUCKETS 7
  35. /*! \brief The number of buckets to be used for storing media filename-keyed objects */
  36. #define INDEX_BUCKETS 157
  37. /*! \brief Structure to hold a list of the format variations for a media file for a specific variant */
  38. struct media_variant {
  39. AST_DECLARE_STRING_FIELDS(
  40. AST_STRING_FIELD(description); /*!< The description of the media */
  41. );
  42. struct ast_format_cap *formats; /*!< The formats this media is available in for this variant */
  43. char variant[0]; /*!< The variant this media is available in */
  44. };
  45. static void media_variant_destroy(void *obj)
  46. {
  47. struct media_variant *variant = obj;
  48. ast_string_field_free_memory(variant);
  49. ao2_cleanup(variant->formats);
  50. }
  51. static struct media_variant *media_variant_alloc(const char *variant_str)
  52. {
  53. size_t str_sz = strlen(variant_str) + 1;
  54. struct media_variant *variant;
  55. variant = ao2_alloc_options(sizeof(*variant) + str_sz, media_variant_destroy,
  56. AO2_ALLOC_OPT_LOCK_NOLOCK);
  57. if (!variant) {
  58. return NULL;
  59. }
  60. memcpy(variant->variant, variant_str, str_sz);
  61. variant->formats = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  62. if (!variant->formats || ast_string_field_init(variant, 8)) {
  63. ao2_ref(variant, -1);
  64. return NULL;
  65. }
  66. return variant;
  67. }
  68. static int media_variant_hash(const void *obj, const int flags)
  69. {
  70. const char *variant = (flags & OBJ_KEY) ? obj : ((struct media_variant*) obj)->variant;
  71. return ast_str_case_hash(variant);
  72. }
  73. static int media_variant_cmp(void *obj, void *arg, int flags)
  74. {
  75. struct media_variant *opt1 = obj, *opt2 = arg;
  76. const char *variant = (flags & OBJ_KEY) ? arg : opt2->variant;
  77. return strcasecmp(opt1->variant, variant) ? 0 : CMP_MATCH | CMP_STOP;
  78. }
  79. /*! \brief Structure to hold information about a media file */
  80. struct media_info {
  81. struct ao2_container *variants; /*!< The variants for which this media is available */
  82. char name[0]; /*!< The file name of the media */
  83. };
  84. static void media_info_destroy(void *obj)
  85. {
  86. struct media_info *info = obj;
  87. ao2_cleanup(info->variants);
  88. }
  89. static struct media_info *media_info_alloc(const char *name)
  90. {
  91. size_t name_sz = strlen(name) + 1;
  92. struct media_info *info;
  93. info = ao2_alloc_options(sizeof(*info) + name_sz, media_info_destroy,
  94. AO2_ALLOC_OPT_LOCK_NOLOCK);
  95. if (!info) {
  96. return NULL;
  97. }
  98. memcpy(info->name, name, name_sz);
  99. info->variants = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
  100. VARIANT_BUCKETS, media_variant_hash, NULL, media_variant_cmp);
  101. if (!info->variants) {
  102. ao2_ref(info, -1);
  103. return NULL;
  104. }
  105. return info;
  106. }
  107. static int media_info_hash(const void *obj, const int flags)
  108. {
  109. const char *name = (flags & OBJ_KEY) ? obj : ((struct media_info*) obj)->name;
  110. return ast_str_case_hash(name);
  111. }
  112. static int media_info_cmp(void *obj, void *arg, int flags)
  113. {
  114. struct media_info *opt1 = obj, *opt2 = arg;
  115. const char *name = (flags & OBJ_KEY) ? arg : opt2->name;
  116. return strcasecmp(opt1->name, name) ? 0 : CMP_MATCH | CMP_STOP;
  117. }
  118. struct ast_media_index {
  119. struct ao2_container *index; /*!< The index of media that has requested */
  120. struct ao2_container *media_list_cache; /*!< Cache of filenames to prevent them from being regenerated so often */
  121. char base_dir[0]; /*!< Base directory for indexing */
  122. };
  123. static void media_index_dtor(void *obj)
  124. {
  125. struct ast_media_index *index = obj;
  126. ao2_cleanup(index->index);
  127. ao2_cleanup(index->media_list_cache);
  128. }
  129. struct ast_media_index *ast_media_index_create(const char *base_dir)
  130. {
  131. size_t base_dir_sz = strlen(base_dir) + 1;
  132. struct ast_media_index *index = ao2_alloc(sizeof(*index) + base_dir_sz, media_index_dtor);
  133. if (!index) {
  134. return NULL;
  135. }
  136. memcpy(index->base_dir, base_dir, base_dir_sz);
  137. index->index = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, INDEX_BUCKETS,
  138. media_info_hash, NULL, media_info_cmp);
  139. if (!index->index) {
  140. ao2_ref(index, -1);
  141. return NULL;
  142. }
  143. return index;
  144. }
  145. static struct media_variant *find_variant(struct ast_media_index *index, const char *filename, const char *variant)
  146. {
  147. RAII_VAR(struct media_info *, info, NULL, ao2_cleanup);
  148. info = ao2_find(index->index, filename, OBJ_KEY);
  149. if (!info) {
  150. return NULL;
  151. }
  152. return ao2_find(info->variants, variant, OBJ_KEY);
  153. }
  154. /*! \brief create the appropriate media_variant and any necessary structures */
  155. static struct media_variant *alloc_variant(struct ast_media_index *index, const char *filename, const char *variant_str)
  156. {
  157. struct media_info *info;
  158. struct media_variant *variant = NULL;
  159. info = ao2_find(index->index, filename, OBJ_KEY);
  160. if (!info) {
  161. /* This is the first time the index has seen this filename,
  162. * allocate and link */
  163. info = media_info_alloc(filename);
  164. if (!info) {
  165. return NULL;
  166. }
  167. ao2_link(index->index, info);
  168. } else {
  169. variant = ao2_find(info->variants, variant_str, OBJ_KEY);
  170. }
  171. if (!variant) {
  172. /* This is the first time the index has seen this variant for
  173. * this filename, allocate and link */
  174. variant = media_variant_alloc(variant_str);
  175. if (variant) {
  176. ao2_link(info->variants, variant);
  177. }
  178. }
  179. ao2_ref(info, -1);
  180. return variant;
  181. }
  182. const char *ast_media_get_description(struct ast_media_index *index, const char *filename, const char *variant_str)
  183. {
  184. RAII_VAR(struct media_variant *, variant, NULL, ao2_cleanup);
  185. if (ast_strlen_zero(filename) || ast_strlen_zero(variant_str)) {
  186. return NULL;
  187. }
  188. variant = find_variant(index, filename, variant_str);
  189. if (!variant) {
  190. return NULL;
  191. }
  192. return variant->description;
  193. }
  194. struct ast_format_cap *ast_media_get_format_cap(struct ast_media_index *index, const char *filename, const char *variant_str)
  195. {
  196. struct ast_format_cap *dupcap;
  197. RAII_VAR(struct media_variant *, variant, NULL, ao2_cleanup);
  198. if (ast_strlen_zero(filename) || ast_strlen_zero(variant_str)) {
  199. return NULL;
  200. }
  201. variant = find_variant(index, filename, variant_str);
  202. if (!variant) {
  203. return NULL;
  204. }
  205. dupcap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  206. if (dupcap) {
  207. ast_format_cap_append_from_cap(dupcap, variant->formats, AST_MEDIA_TYPE_UNKNOWN);
  208. }
  209. return dupcap;
  210. }
  211. /*! \brief Add the variant to the list of variants requested */
  212. static int add_variant_cb(void *obj, void *arg, int flags)
  213. {
  214. struct media_variant *variant = obj;
  215. struct ao2_container *variants= arg;
  216. ast_str_container_add(variants, variant->variant);
  217. return 0;
  218. }
  219. struct ao2_container *ast_media_get_variants(struct ast_media_index *index, const char *filename)
  220. {
  221. RAII_VAR(struct media_info *, info, NULL, ao2_cleanup);
  222. RAII_VAR(struct ao2_container *, variants, NULL, ao2_cleanup);
  223. if (!filename) {
  224. return NULL;
  225. }
  226. variants = ast_str_container_alloc(VARIANT_BUCKETS);
  227. if (!variants) {
  228. return NULL;
  229. }
  230. info = ao2_find(index->index, filename, OBJ_KEY);
  231. if (!info) {
  232. return NULL;
  233. }
  234. ao2_callback(info->variants, OBJ_NODATA, add_variant_cb, variants);
  235. ao2_ref(variants, +1);
  236. return variants;
  237. }
  238. /*! \brief Add the media_info's filename to the container of filenames requested */
  239. static int add_media_cb(void *obj, void *arg, int flags)
  240. {
  241. struct media_info *info = obj;
  242. struct ao2_container *media = arg;
  243. ast_str_container_add(media, info->name);
  244. return 0;
  245. }
  246. struct ao2_container *ast_media_get_media(struct ast_media_index *index)
  247. {
  248. RAII_VAR(struct ao2_container *, media, NULL, ao2_cleanup);
  249. if (!index->media_list_cache) {
  250. media = ast_str_container_alloc(INDEX_BUCKETS);
  251. if (!media) {
  252. return NULL;
  253. }
  254. ao2_callback(index->index, OBJ_NODATA, add_media_cb, media);
  255. /* Ref to the cache */
  256. ao2_ref(media, +1);
  257. index->media_list_cache = media;
  258. }
  259. /* Ref to the caller */
  260. ao2_ref(index->media_list_cache, +1);
  261. return index->media_list_cache;
  262. }
  263. /*! \brief Update an index with new format/variant information */
  264. static int update_file_format_info(struct ast_media_index *index, const char *filename, const char *variant_str, struct ast_format *file_format)
  265. {
  266. struct media_variant *variant;
  267. variant = alloc_variant(index, filename, variant_str);
  268. if (!variant) {
  269. return -1;
  270. }
  271. ast_format_cap_append(variant->formats, file_format, 0);
  272. ao2_ref(variant, -1);
  273. return 0;
  274. }
  275. /*! \brief Process a media file into the index */
  276. static int process_media_file(struct ast_media_index *index, const char *variant, const char *subdir, const char *filename_stripped, const char *ext)
  277. {
  278. struct ast_format *file_format;
  279. const char *file_identifier = filename_stripped;
  280. char *file_id_str = NULL;
  281. int res;
  282. file_format = ast_get_format_for_file_ext(ext);
  283. if (!file_format) {
  284. /* extension not registered */
  285. return 0;
  286. }
  287. /* handle updating the file information */
  288. if (subdir) {
  289. if (ast_asprintf(&file_id_str, "%s/%s", subdir, filename_stripped) == -1) {
  290. return -1;
  291. }
  292. file_identifier = file_id_str;
  293. }
  294. res = update_file_format_info(index, file_identifier, variant, file_format);
  295. ast_free(file_id_str);
  296. return res;
  297. }
  298. /*!
  299. * \brief Process a media description text file
  300. *
  301. * This currently processes core-sounds-*.txt and extra-sounds-*.txt, but will
  302. * process others if present.
  303. */
  304. static int process_description_file(struct ast_media_index *index,
  305. const char *subdir,
  306. const char *variant_str,
  307. const char *filename,
  308. const char *match_filename)
  309. {
  310. RAII_VAR(struct ast_str *, description_file_path, ast_str_create(64), ast_free);
  311. RAII_VAR(struct ast_str *, cumulative_description, ast_str_create(64), ast_free);
  312. char *file_id_persist = NULL;
  313. int res = 0;
  314. FILE *f = NULL;
  315. #if defined(LOW_MEMORY)
  316. char buf[256];
  317. #else
  318. char buf[2048];
  319. #endif
  320. if (!description_file_path || !cumulative_description) {
  321. return -1;
  322. }
  323. if (ast_strlen_zero(subdir)) {
  324. ast_str_set(&description_file_path, 0, "%s/%s/%s", index->base_dir, variant_str, filename);
  325. } else {
  326. ast_str_set(&description_file_path, 0, "%s/%s/%s/%s", index->base_dir, variant_str, subdir, filename);
  327. }
  328. f = fopen(ast_str_buffer(description_file_path), "r");
  329. if (!f) {
  330. ast_log(LOG_WARNING, "Could not open media description file '%s': %s\n", ast_str_buffer(description_file_path), strerror(errno));
  331. return -1;
  332. }
  333. while (!feof(f)) {
  334. char *file_identifier, *description;
  335. if (!fgets(buf, sizeof(buf), f)) {
  336. if (ferror(f)) {
  337. ast_log(LOG_ERROR, "Error reading from file %s: %s\n", ast_str_buffer(description_file_path), strerror(errno));
  338. }
  339. continue;
  340. }
  341. /* Skip lines that are too long */
  342. if (strlen(buf) == sizeof(buf) - 1 && buf[sizeof(buf) - 1] != '\n') {
  343. ast_log(LOG_WARNING, "Line too long, skipping. It begins with: %.32s...\n", buf);
  344. while (fgets(buf, sizeof(buf), f)) {
  345. if (strlen(buf) != sizeof(buf) - 1 || buf[sizeof(buf) - 1] == '\n') {
  346. break;
  347. }
  348. }
  349. if (ferror(f)) {
  350. ast_log(LOG_ERROR, "Error reading from file %s: %s\n", ast_str_buffer(description_file_path), strerror(errno));
  351. }
  352. continue;
  353. }
  354. if (buf[0] == ';') {
  355. /* ignore comments */
  356. continue;
  357. }
  358. ast_trim_blanks(buf);
  359. description = buf;
  360. file_identifier = strsep(&description, ":");
  361. if (!description) {
  362. /* no ':' means this is a continuation */
  363. if (file_id_persist) {
  364. ast_str_append(&cumulative_description, 0, "\n%s", file_identifier);
  365. }
  366. continue;
  367. } else {
  368. /* if there's text in cumulative_description, archive it and start anew */
  369. if (file_id_persist && !ast_strlen_zero(ast_str_buffer(cumulative_description))) {
  370. struct media_variant *variant;
  371. /*
  372. * If we were only searching for a specific sound filename
  373. * don't include others.
  374. */
  375. if (ast_strlen_zero(match_filename) || strcmp(match_filename, file_id_persist) == 0) {
  376. variant = alloc_variant(index, file_id_persist, variant_str);
  377. if (!variant) {
  378. res = -1;
  379. break;
  380. }
  381. ast_string_field_set(variant, description, ast_str_buffer(cumulative_description));
  382. ao2_ref(variant, -1);
  383. }
  384. ast_str_reset(cumulative_description);
  385. }
  386. ast_free(file_id_persist);
  387. file_id_persist = ast_strdup(file_identifier);
  388. description = ast_skip_blanks(description);
  389. ast_str_set(&cumulative_description, 0, "%s", description);
  390. }
  391. }
  392. /* handle the last one */
  393. if (file_id_persist && !ast_strlen_zero(ast_str_buffer(cumulative_description))) {
  394. struct media_variant *variant;
  395. /*
  396. * If we were only searching for a specific sound filename
  397. * don't include others.
  398. */
  399. if (ast_strlen_zero(match_filename) || strcmp(match_filename, file_id_persist) == 0) {
  400. variant = alloc_variant(index, file_id_persist, variant_str);
  401. if (variant) {
  402. ast_string_field_set(variant, description, ast_str_buffer(cumulative_description));
  403. ao2_ref(variant, -1);
  404. } else {
  405. res = -1;
  406. }
  407. }
  408. }
  409. ast_free(file_id_persist);
  410. fclose(f);
  411. return res;
  412. }
  413. struct read_dirs_data {
  414. const char *search_filename;
  415. size_t search_filename_len;
  416. const char *search_variant;
  417. struct ast_media_index *index;
  418. size_t dirname_len;
  419. };
  420. static int read_dirs_cb(const char *dir_name, const char *filename, void *obj)
  421. {
  422. struct read_dirs_data *data = obj;
  423. char *ext;
  424. size_t match_len;
  425. char *match;
  426. size_t match_base_len;
  427. char *subdirs = (char *)dir_name + data->dirname_len;
  428. /*
  429. * Example:
  430. * From the filesystem:
  431. * index's base_dir = "/var/lib/asterisk/sounds"
  432. * search_variant = "en"
  433. * search directory base = "/var/lib/asterisk/sounds/en"
  434. * dirname_len = 27
  435. * current dir_name = "/var/lib/asterisk/sounds/en/digits"
  436. * subdirs = "/digits"
  437. * filename = "1.ulaw"
  438. *
  439. * From the search criteria:
  440. * search_filename = "digits/1"
  441. * search_filename_len = 8
  442. */
  443. if (*subdirs == '/') {
  444. subdirs++;
  445. }
  446. /* subdirs = "digits" */
  447. match_len = strlen(subdirs) + strlen(filename) + 2;
  448. match = ast_alloca(match_len);
  449. snprintf(match, match_len, "%s%s%s", subdirs,
  450. ast_strlen_zero(subdirs) ? "" : "/", filename);
  451. /* match = discovered filename relative to language = "digits/1.ulaw" */
  452. ext = strrchr(match, '.');
  453. if (!ext) {
  454. return 0;
  455. }
  456. /* ext = ".ulaw" */
  457. if (data->search_filename_len > 0) {
  458. match_base_len = ext - match;
  459. /*
  460. * match_base_len = length of "digits/1" = 8 which
  461. * happens to match the length of search_filename.
  462. * However if the discovered filename was 11.ulaw
  463. * it would be length of "digits/11" = 9.
  464. * We need to use the larger during the compare to
  465. * make sure we don't match just search_filename
  466. * as a substring of the discovered filename.
  467. */
  468. if (data->search_filename_len > match_base_len) {
  469. match_base_len = data->search_filename_len;
  470. }
  471. }
  472. /* We always process txt files because they should contain description. */
  473. if (strcmp(ext, ".txt") == 0) {
  474. if (process_description_file(data->index, NULL, data->search_variant,
  475. match, data->search_filename)) {
  476. return -1;
  477. }
  478. } else if (data->search_filename_len == 0
  479. || strncmp(data->search_filename, match, match_base_len ) == 0) {
  480. *ext = '\0';
  481. ext++;
  482. process_media_file(data->index, data->search_variant, NULL, match, ext);
  483. }
  484. return 0;
  485. }
  486. int ast_media_index_update_for_file(struct ast_media_index *index,
  487. const char *variant, const char *filename)
  488. {
  489. struct timeval start;
  490. struct timeval end;
  491. int64_t elapsed;
  492. int rc;
  493. size_t dirname_len = strlen(index->base_dir) + strlen(S_OR(variant, "")) + 1;
  494. struct read_dirs_data data = {
  495. .search_filename = S_OR(filename, ""),
  496. .search_filename_len = strlen(S_OR(filename, "")),
  497. .search_variant = S_OR(variant, ""),
  498. .index = index,
  499. .dirname_len = dirname_len,
  500. };
  501. char *search_dir = ast_alloca(dirname_len + 1);
  502. sprintf(search_dir, "%s%s%s", index->base_dir, ast_strlen_zero(variant) ? "" : "/",
  503. data.search_variant);
  504. gettimeofday(&start, NULL);
  505. rc = ast_file_read_dirs(search_dir, read_dirs_cb, &data, -1);
  506. gettimeofday(&end, NULL);
  507. elapsed = ast_tvdiff_us(end, start);
  508. ast_debug(1, "Media for language '%s' indexed in %8.6f seconds\n", data.search_variant, elapsed / 1E6);
  509. return rc;
  510. }
  511. int ast_media_index_update(struct ast_media_index *index, const char *variant)
  512. {
  513. return ast_media_index_update_for_file(index, variant, NULL);
  514. }