media_index.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. ao2_cleanup(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(AST_FORMAT_CAP_FLAG_DEFAULT);
  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. 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. RAII_VAR(struct media_variant *, variant, find_variant(index, filename, variant_str), ao2_cleanup);
  267. if (!variant) {
  268. variant = alloc_variant(index, filename, variant_str);
  269. if (!variant) {
  270. return -1;
  271. }
  272. }
  273. ast_format_cap_append(variant->formats, file_format, 0);
  274. return 0;
  275. }
  276. /*! \brief Process a media file into the index */
  277. static int process_media_file(struct ast_media_index *index, const char *variant, const char *subdir, const char *filename_stripped, const char *ext)
  278. {
  279. struct ast_format *file_format;
  280. const char *file_identifier = filename_stripped;
  281. RAII_VAR(struct ast_str *, file_id_str, NULL, ast_free);
  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. file_id_str = ast_str_create(64);
  290. if (!file_id_str) {
  291. return -1;
  292. }
  293. ast_str_set(&file_id_str, 0, "%s/%s", subdir, filename_stripped);
  294. file_identifier = ast_str_buffer(file_id_str);
  295. }
  296. if (update_file_format_info(index, file_identifier, variant, file_format)) {
  297. return -1;
  298. }
  299. return 0;
  300. }
  301. /*!
  302. * \brief Process a media description text file
  303. *
  304. * This currently processes core-sounds-*.txt and extra-sounds-*.txt, but will
  305. * process others if present.
  306. */
  307. static int process_description_file(struct ast_media_index *index,
  308. const char *subdir,
  309. const char *variant_str,
  310. const char *filename)
  311. {
  312. RAII_VAR(struct ast_str *, description_file_path, ast_str_create(64), ast_free);
  313. RAII_VAR(struct ast_str *, cumulative_description, ast_str_create(64), ast_free);
  314. char *file_id_persist = NULL;
  315. int res = 0;
  316. FILE *f = NULL;
  317. #if defined(LOW_MEMORY)
  318. char buf[256];
  319. #else
  320. char buf[2048];
  321. #endif
  322. if (!description_file_path || !cumulative_description) {
  323. return -1;
  324. }
  325. if (ast_strlen_zero(subdir)) {
  326. ast_str_set(&description_file_path, 0, "%s/%s/%s", index->base_dir, variant_str, filename);
  327. } else {
  328. ast_str_set(&description_file_path, 0, "%s/%s/%s/%s", index->base_dir, variant_str, subdir, filename);
  329. }
  330. f = fopen(ast_str_buffer(description_file_path), "r");
  331. if (!f) {
  332. ast_log(LOG_WARNING, "Could not open media description file '%s': %s\n", ast_str_buffer(description_file_path), strerror(errno));
  333. return -1;
  334. }
  335. while (!feof(f)) {
  336. char *file_identifier, *description;
  337. if (!fgets(buf, sizeof(buf), f)) {
  338. if (ferror(f)) {
  339. ast_log(LOG_ERROR, "Error reading from file %s: %s\n", ast_str_buffer(description_file_path), strerror(errno));
  340. }
  341. continue;
  342. }
  343. /* Skip lines that are too long */
  344. if (strlen(buf) == sizeof(buf) - 1 && buf[sizeof(buf) - 1] != '\n') {
  345. ast_log(LOG_WARNING, "Line too long, skipping. It begins with: %.32s...\n", buf);
  346. while (fgets(buf, sizeof(buf), f)) {
  347. if (strlen(buf) != sizeof(buf) - 1 || buf[sizeof(buf) - 1] == '\n') {
  348. break;
  349. }
  350. }
  351. if (ferror(f)) {
  352. ast_log(LOG_ERROR, "Error reading from file %s: %s\n", ast_str_buffer(description_file_path), strerror(errno));
  353. }
  354. continue;
  355. }
  356. if (buf[0] == ';') {
  357. /* ignore comments */
  358. continue;
  359. }
  360. ast_trim_blanks(buf);
  361. description = buf;
  362. file_identifier = strsep(&description, ":");
  363. if (!description) {
  364. /* no ':' means this is a continuation */
  365. if (file_id_persist) {
  366. ast_str_append(&cumulative_description, 0, "\n%s", file_identifier);
  367. }
  368. continue;
  369. } else {
  370. /* if there's text in cumulative_description, archive it and start anew */
  371. if (file_id_persist && !ast_strlen_zero(ast_str_buffer(cumulative_description))) {
  372. RAII_VAR(struct media_variant *, variant, NULL, ao2_cleanup);
  373. variant = find_variant(index, file_id_persist, variant_str);
  374. if (!variant) {
  375. variant = alloc_variant(index, file_id_persist, variant_str);
  376. if (!variant) {
  377. res = -1;
  378. break;
  379. }
  380. }
  381. ast_string_field_set(variant, description, ast_str_buffer(cumulative_description));
  382. ast_str_reset(cumulative_description);
  383. }
  384. ast_free(file_id_persist);
  385. file_id_persist = ast_strdup(file_identifier);
  386. description = ast_skip_blanks(description);
  387. ast_str_set(&cumulative_description, 0, "%s", description);
  388. }
  389. }
  390. /* handle the last one */
  391. if (file_id_persist && !ast_strlen_zero(ast_str_buffer(cumulative_description))) {
  392. RAII_VAR(struct media_variant *, variant, NULL, ao2_cleanup);
  393. variant = find_variant(index, file_id_persist, variant_str);
  394. if (!variant) {
  395. variant = alloc_variant(index, file_id_persist, variant_str);
  396. }
  397. if (variant) {
  398. ast_string_field_set(variant, description, ast_str_buffer(cumulative_description));
  399. } else {
  400. res = -1;
  401. }
  402. }
  403. ast_free(file_id_persist);
  404. fclose(f);
  405. return res;
  406. }
  407. /*! \brief process an individual file listing */
  408. static int process_file(struct ast_media_index *index, const char *variant_str, const char *subdir, const char *filename)
  409. {
  410. RAII_VAR(char *, filename_stripped, ast_strdup(filename), ast_free);
  411. char *ext;
  412. if (!filename_stripped) {
  413. return -1;
  414. }
  415. ext = strrchr(filename_stripped, '.');
  416. if (!ext) {
  417. /* file has no extension */
  418. return 0;
  419. }
  420. *ext++ = '\0';
  421. if (!strcmp(ext, "txt")) {
  422. if (process_description_file(index, subdir, variant_str, filename)) {
  423. return -1;
  424. }
  425. } else {
  426. if (process_media_file(index, variant_str, subdir, filename_stripped, ext)) {
  427. return -1;
  428. }
  429. }
  430. return 0;
  431. }
  432. /*! \brief internal function for updating the index, recursive */
  433. static int media_index_update(struct ast_media_index *index,
  434. const char *variant,
  435. const char *subdir)
  436. {
  437. struct dirent* dent;
  438. DIR* srcdir;
  439. RAII_VAR(struct ast_str *, index_dir, ast_str_create(64), ast_free);
  440. RAII_VAR(struct ast_str *, statfile, ast_str_create(64), ast_free);
  441. int res = 0;
  442. if (!index_dir) {
  443. return 0;
  444. }
  445. ast_str_set(&index_dir, 0, "%s", index->base_dir);
  446. if (!ast_strlen_zero(variant)) {
  447. ast_str_append(&index_dir, 0, "/%s", variant);
  448. }
  449. if (!ast_strlen_zero(subdir)) {
  450. ast_str_append(&index_dir, 0, "/%s", subdir);
  451. }
  452. srcdir = opendir(ast_str_buffer(index_dir));
  453. if (srcdir == NULL) {
  454. ast_log(LOG_ERROR, "Failed to open %s: %s\n", ast_str_buffer(index_dir), strerror(errno));
  455. return -1;
  456. }
  457. while((dent = readdir(srcdir)) != NULL) {
  458. struct stat st;
  459. if(!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) {
  460. continue;
  461. }
  462. ast_str_reset(statfile);
  463. ast_str_set(&statfile, 0, "%s/%s", ast_str_buffer(index_dir), dent->d_name);
  464. if (stat(ast_str_buffer(statfile), &st) < 0) {
  465. ast_log(LOG_WARNING, "Failed to stat %s: %s\n", ast_str_buffer(statfile), strerror(errno));
  466. continue;
  467. }
  468. if (S_ISDIR(st.st_mode)) {
  469. if (ast_strlen_zero(subdir)) {
  470. res = media_index_update(index, variant, dent->d_name);
  471. } else {
  472. RAII_VAR(struct ast_str *, new_subdir, ast_str_create(64), ast_free);
  473. ast_str_set(&new_subdir, 0, "%s/%s", subdir, dent->d_name);
  474. res = media_index_update(index, variant, ast_str_buffer(new_subdir));
  475. }
  476. if (res) {
  477. break;
  478. }
  479. continue;
  480. }
  481. if (!S_ISREG(st.st_mode)) {
  482. continue;
  483. }
  484. if (process_file(index, variant, subdir, dent->d_name)) {
  485. res = -1;
  486. break;
  487. }
  488. }
  489. closedir(srcdir);
  490. return res;
  491. }
  492. int ast_media_index_update(struct ast_media_index *index,
  493. const char *variant)
  494. {
  495. return media_index_update(index, variant, NULL);
  496. }