media_index.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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(variant); /*!< The variant this media is available in */
  41. AST_STRING_FIELD(description); /*!< The description of the media */
  42. );
  43. struct ast_format_cap *formats; /*!< The formats this media is available in for this variant */
  44. };
  45. static void media_variant_destroy(void *obj)
  46. {
  47. struct media_variant *variant = obj;
  48. ast_string_field_free_memory(variant);
  49. variant->formats = ast_format_cap_destroy(variant->formats);
  50. }
  51. static struct media_variant *media_variant_alloc(const char *variant_str)
  52. {
  53. RAII_VAR(struct media_variant *, variant, ao2_alloc(sizeof(*variant), media_variant_destroy), ao2_cleanup);
  54. if (!variant || ast_string_field_init(variant, 8)) {
  55. return NULL;
  56. }
  57. variant->formats = ast_format_cap_alloc(0);
  58. if (!variant->formats) {
  59. return NULL;
  60. }
  61. ast_string_field_set(variant, variant, variant_str);
  62. ao2_ref(variant, 1);
  63. return variant;
  64. }
  65. static int media_variant_hash(const void *obj, const int flags)
  66. {
  67. const char *variant = (flags & OBJ_KEY) ? obj : ((struct media_variant*) obj)->variant;
  68. return ast_str_case_hash(variant);
  69. }
  70. static int media_variant_cmp(void *obj, void *arg, int flags)
  71. {
  72. struct media_variant *opt1 = obj, *opt2 = arg;
  73. const char *variant = (flags & OBJ_KEY) ? arg : opt2->variant;
  74. return strcasecmp(opt1->variant, variant) ? 0 : CMP_MATCH | CMP_STOP;
  75. }
  76. /*! \brief Structure to hold information about a media file */
  77. struct media_info {
  78. AST_DECLARE_STRING_FIELDS(
  79. AST_STRING_FIELD(name); /*!< The file name of the media */
  80. );
  81. struct ao2_container *variants; /*!< The variants for which this media is available */
  82. };
  83. static void media_info_destroy(void *obj)
  84. {
  85. struct media_info *info = obj;
  86. ast_string_field_free_memory(info);
  87. ao2_cleanup(info->variants);
  88. info->variants = NULL;
  89. }
  90. static struct media_info *media_info_alloc(const char *name)
  91. {
  92. RAII_VAR(struct media_info *, info, ao2_alloc(sizeof(*info), media_info_destroy), ao2_cleanup);
  93. if (!info || ast_string_field_init(info, 128)) {
  94. return NULL;
  95. }
  96. info->variants = ao2_container_alloc(VARIANT_BUCKETS, media_variant_hash, media_variant_cmp);
  97. if (!info->variants) {
  98. return NULL;
  99. }
  100. ast_string_field_set(info, name, name);
  101. ao2_ref(info, 1);
  102. return info;
  103. }
  104. static int media_info_hash(const void *obj, const int flags)
  105. {
  106. const char *name = (flags & OBJ_KEY) ? obj : ((struct media_info*) obj)->name;
  107. return ast_str_case_hash(name);
  108. }
  109. static int media_info_cmp(void *obj, void *arg, int flags)
  110. {
  111. struct media_info *opt1 = obj, *opt2 = arg;
  112. const char *name = (flags & OBJ_KEY) ? arg : opt2->name;
  113. return strcasecmp(opt1->name, name) ? 0 : CMP_MATCH | CMP_STOP;
  114. }
  115. struct ast_media_index {
  116. AST_DECLARE_STRING_FIELDS(
  117. AST_STRING_FIELD(base_dir); /*!< Base directory for indexing */
  118. );
  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. };
  122. static void media_index_dtor(void *obj)
  123. {
  124. struct ast_media_index *index = obj;
  125. ao2_cleanup(index->index);
  126. index->index = NULL;
  127. ao2_cleanup(index->media_list_cache);
  128. index->media_list_cache = NULL;
  129. ast_string_field_free_memory(index);
  130. }
  131. struct ast_media_index *ast_media_index_create(const char *base_dir)
  132. {
  133. RAII_VAR(struct ast_media_index *, index, ao2_alloc(sizeof(*index), media_index_dtor), ao2_cleanup);
  134. if (!index || ast_string_field_init(index, 64)) {
  135. return NULL;
  136. }
  137. ast_string_field_set(index, base_dir, base_dir);
  138. index->index = ao2_container_alloc(INDEX_BUCKETS, media_info_hash, media_info_cmp);
  139. if (!index->index) {
  140. return NULL;
  141. }
  142. ao2_ref(index, +1);
  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. RAII_VAR(struct media_info *, info, NULL, ao2_cleanup);
  158. RAII_VAR(struct media_variant *, variant, NULL, ao2_cleanup);
  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. }
  169. variant = ao2_find(info->variants, variant_str, OBJ_KEY);
  170. if (!variant) {
  171. /* This is the first time the index has seen this variant for
  172. * this filename, allocate and link */
  173. variant = media_variant_alloc(variant_str);
  174. if (!variant) {
  175. return NULL;
  176. }
  177. ao2_link(info->variants, variant);
  178. }
  179. ao2_ref(variant, +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. RAII_VAR(struct media_variant *, variant, NULL, ao2_cleanup);
  197. if (ast_strlen_zero(filename) || ast_strlen_zero(variant_str)) {
  198. return NULL;
  199. }
  200. variant = find_variant(index, filename, variant_str);
  201. if (!variant) {
  202. return NULL;
  203. }
  204. return ast_format_cap_dup(variant->formats);
  205. }
  206. /*! \brief Add the variant to the list of variants requested */
  207. static int add_variant_cb(void *obj, void *arg, int flags)
  208. {
  209. struct media_variant *variant = obj;
  210. struct ao2_container *variants= arg;
  211. ast_str_container_add(variants, variant->variant);
  212. return 0;
  213. }
  214. struct ao2_container *ast_media_get_variants(struct ast_media_index *index, const char *filename)
  215. {
  216. RAII_VAR(struct media_info *, info, NULL, ao2_cleanup);
  217. RAII_VAR(struct ao2_container *, variants, NULL, ao2_cleanup);
  218. if (!filename) {
  219. return NULL;
  220. }
  221. variants = ast_str_container_alloc(VARIANT_BUCKETS);
  222. if (!variants) {
  223. return NULL;
  224. }
  225. info = ao2_find(index->index, filename, OBJ_KEY);
  226. if (!info) {
  227. return NULL;
  228. }
  229. ao2_callback(info->variants, OBJ_NODATA, add_variant_cb, variants);
  230. ao2_ref(variants, +1);
  231. return variants;
  232. }
  233. /*! \brief Add the media_info's filename to the container of filenames requested */
  234. static int add_media_cb(void *obj, void *arg, int flags)
  235. {
  236. struct media_info *info = obj;
  237. struct ao2_container *media = arg;
  238. ast_str_container_add(media, info->name);
  239. return 0;
  240. }
  241. struct ao2_container *ast_media_get_media(struct ast_media_index *index)
  242. {
  243. RAII_VAR(struct ao2_container *, media, NULL, ao2_cleanup);
  244. if (!index->media_list_cache) {
  245. media = ast_str_container_alloc(INDEX_BUCKETS);
  246. if (!media) {
  247. return NULL;
  248. }
  249. ao2_callback(index->index, OBJ_NODATA, add_media_cb, media);
  250. /* Ref to the cache */
  251. ao2_ref(media, +1);
  252. index->media_list_cache = media;
  253. }
  254. /* Ref to the caller */
  255. ao2_ref(index->media_list_cache, +1);
  256. return index->media_list_cache;
  257. }
  258. /*! \brief Update an index with new format/variant information */
  259. static int update_file_format_info(struct ast_media_index *index, const char *filename, const char *variant_str, const struct ast_format *file_format)
  260. {
  261. RAII_VAR(struct media_variant *, variant, find_variant(index, filename, variant_str), ao2_cleanup);
  262. if (!variant) {
  263. variant = alloc_variant(index, filename, variant_str);
  264. if (!variant) {
  265. return -1;
  266. }
  267. }
  268. ast_format_cap_add(variant->formats, file_format);
  269. return 0;
  270. }
  271. /*! \brief Process a media file into the index */
  272. static int process_media_file(struct ast_media_index *index, const char *variant, const char *subdir, const char *filename_stripped, const char *ext)
  273. {
  274. const struct ast_format *file_format;
  275. const char *file_identifier = filename_stripped;
  276. RAII_VAR(struct ast_str *, file_id_str, NULL, ast_free);
  277. file_format = ast_get_format_for_file_ext(ext);
  278. if (!file_format) {
  279. /* extension not registered */
  280. return 0;
  281. }
  282. /* handle updating the file information */
  283. if (subdir) {
  284. file_id_str = ast_str_create(64);
  285. if (!file_id_str) {
  286. return -1;
  287. }
  288. ast_str_set(&file_id_str, 0, "%s/%s", subdir, filename_stripped);
  289. file_identifier = ast_str_buffer(file_id_str);
  290. }
  291. if (update_file_format_info(index, file_identifier, variant, file_format)) {
  292. return -1;
  293. }
  294. return 0;
  295. }
  296. /*!
  297. * \brief Process a media description text file
  298. *
  299. * This currently processes core-sounds-*.txt and extra-sounds-*.txt, but will
  300. * process others if present.
  301. */
  302. static int process_description_file(struct ast_media_index *index,
  303. const char *subdir,
  304. const char *variant_str,
  305. const char *filename)
  306. {
  307. RAII_VAR(struct ast_str *, description_file_path, ast_str_create(64), ast_free);
  308. RAII_VAR(struct ast_str *, cumulative_description, ast_str_create(64), ast_free);
  309. char *file_id_persist = NULL;
  310. int res = 0;
  311. FILE *f = NULL;
  312. #if defined(LOW_MEMORY)
  313. char buf[256];
  314. #else
  315. char buf[2048];
  316. #endif
  317. if (!description_file_path || !cumulative_description) {
  318. return -1;
  319. }
  320. if (ast_strlen_zero(subdir)) {
  321. ast_str_set(&description_file_path, 0, "%s/%s/%s", index->base_dir, variant_str, filename);
  322. } else {
  323. ast_str_set(&description_file_path, 0, "%s/%s/%s/%s", index->base_dir, variant_str, subdir, filename);
  324. }
  325. f = fopen(ast_str_buffer(description_file_path), "r");
  326. if (!f) {
  327. ast_log(LOG_WARNING, "Could not open media description file '%s': %s\n", ast_str_buffer(description_file_path), strerror(errno));
  328. return -1;
  329. }
  330. while (!feof(f)) {
  331. char *file_identifier, *description;
  332. if (!fgets(buf, sizeof(buf), f)) {
  333. if (ferror(f)) {
  334. ast_log(LOG_ERROR, "Error reading from file %s: %s\n", ast_str_buffer(description_file_path), strerror(errno));
  335. }
  336. continue;
  337. }
  338. /* Skip lines that are too long */
  339. if (strlen(buf) == sizeof(buf) - 1 && buf[sizeof(buf) - 1] != '\n') {
  340. ast_log(LOG_WARNING, "Line too long, skipping. It begins with: %.32s...\n", buf);
  341. while (fgets(buf, sizeof(buf), f)) {
  342. if (strlen(buf) != sizeof(buf) - 1 || buf[sizeof(buf) - 1] == '\n') {
  343. break;
  344. }
  345. }
  346. if (ferror(f)) {
  347. ast_log(LOG_ERROR, "Error reading from file %s: %s\n", ast_str_buffer(description_file_path), strerror(errno));
  348. }
  349. continue;
  350. }
  351. if (buf[0] == ';') {
  352. /* ignore comments */
  353. continue;
  354. }
  355. ast_trim_blanks(buf);
  356. description = buf;
  357. file_identifier = strsep(&description, ":");
  358. if (!description) {
  359. /* no ':' means this is a continuation */
  360. if (file_id_persist) {
  361. ast_str_append(&cumulative_description, 0, "\n%s", file_identifier);
  362. }
  363. continue;
  364. } else {
  365. /* if there's text in cumulative_description, archive it and start anew */
  366. if (file_id_persist && !ast_strlen_zero(ast_str_buffer(cumulative_description))) {
  367. RAII_VAR(struct media_variant *, variant, NULL, ao2_cleanup);
  368. variant = find_variant(index, file_id_persist, variant_str);
  369. if (!variant) {
  370. variant = alloc_variant(index, file_id_persist, variant_str);
  371. if (!variant) {
  372. res = -1;
  373. break;
  374. }
  375. }
  376. ast_string_field_set(variant, description, ast_str_buffer(cumulative_description));
  377. ast_str_reset(cumulative_description);
  378. }
  379. ast_free(file_id_persist);
  380. file_id_persist = ast_strdup(file_identifier);
  381. description = ast_skip_blanks(description);
  382. ast_str_set(&cumulative_description, 0, "%s", description);
  383. }
  384. }
  385. /* handle the last one */
  386. if (file_id_persist && !ast_strlen_zero(ast_str_buffer(cumulative_description))) {
  387. RAII_VAR(struct media_variant *, variant, NULL, ao2_cleanup);
  388. variant = find_variant(index, file_id_persist, variant_str);
  389. if (!variant) {
  390. variant = alloc_variant(index, file_id_persist, variant_str);
  391. }
  392. if (variant) {
  393. ast_string_field_set(variant, description, ast_str_buffer(cumulative_description));
  394. } else {
  395. res = -1;
  396. }
  397. }
  398. ast_free(file_id_persist);
  399. fclose(f);
  400. return res;
  401. }
  402. /*! \brief process an individual file listing */
  403. static int process_file(struct ast_media_index *index, const char *variant_str, const char *subdir, const char *filename)
  404. {
  405. RAII_VAR(char *, filename_stripped, ast_strdup(filename), ast_free);
  406. char *ext;
  407. if (!filename_stripped) {
  408. return -1;
  409. }
  410. ext = strrchr(filename_stripped, '.');
  411. if (!ext) {
  412. /* file has no extension */
  413. return 0;
  414. }
  415. *ext++ = '\0';
  416. if (!strcmp(ext, "txt")) {
  417. if (process_description_file(index, subdir, variant_str, filename)) {
  418. return -1;
  419. }
  420. } else {
  421. if (process_media_file(index, variant_str, subdir, filename_stripped, ext)) {
  422. return -1;
  423. }
  424. }
  425. return 0;
  426. }
  427. /*! \brief internal function for updating the index, recursive */
  428. static int media_index_update(struct ast_media_index *index,
  429. const char *variant,
  430. const char *subdir)
  431. {
  432. struct dirent* dent;
  433. DIR* srcdir;
  434. RAII_VAR(struct ast_str *, index_dir, ast_str_create(64), ast_free);
  435. RAII_VAR(struct ast_str *, statfile, ast_str_create(64), ast_free);
  436. int res = 0;
  437. if (!index_dir) {
  438. return 0;
  439. }
  440. ast_str_set(&index_dir, 0, "%s", index->base_dir);
  441. if (!ast_strlen_zero(variant)) {
  442. ast_str_append(&index_dir, 0, "/%s", variant);
  443. }
  444. if (!ast_strlen_zero(subdir)) {
  445. ast_str_append(&index_dir, 0, "/%s", subdir);
  446. }
  447. srcdir = opendir(ast_str_buffer(index_dir));
  448. if (srcdir == NULL) {
  449. ast_log(LOG_ERROR, "Failed to open %s: %s\n", ast_str_buffer(index_dir), strerror(errno));
  450. return -1;
  451. }
  452. while((dent = readdir(srcdir)) != NULL) {
  453. struct stat st;
  454. if(!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) {
  455. continue;
  456. }
  457. ast_str_reset(statfile);
  458. ast_str_set(&statfile, 0, "%s/%s", ast_str_buffer(index_dir), dent->d_name);
  459. if (stat(ast_str_buffer(statfile), &st) < 0) {
  460. ast_log(LOG_WARNING, "Failed to stat %s: %s\n", ast_str_buffer(statfile), strerror(errno));
  461. continue;
  462. }
  463. if (S_ISDIR(st.st_mode)) {
  464. if (ast_strlen_zero(subdir)) {
  465. res = media_index_update(index, variant, dent->d_name);
  466. } else {
  467. RAII_VAR(struct ast_str *, new_subdir, ast_str_create(64), ast_free);
  468. ast_str_set(&new_subdir, 0, "%s/%s", subdir, dent->d_name);
  469. res = media_index_update(index, variant, ast_str_buffer(new_subdir));
  470. }
  471. if (res) {
  472. break;
  473. }
  474. continue;
  475. }
  476. if (!S_ISREG(st.st_mode)) {
  477. continue;
  478. }
  479. if (process_file(index, variant, subdir, dent->d_name)) {
  480. res = -1;
  481. break;
  482. }
  483. }
  484. closedir(srcdir);
  485. return res;
  486. }
  487. int ast_media_index_update(struct ast_media_index *index,
  488. const char *variant)
  489. {
  490. return media_index_update(index, variant, NULL);
  491. }