resource_sounds.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012 - 2013, Digium, Inc.
  5. *
  6. * David M. Lee, II <dlee@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. *
  20. * \brief /api-docs/sounds.{format} implementation- Sound resources
  21. *
  22. * \author David M. Lee, II <dlee@digium.com>
  23. */
  24. #include "asterisk.h"
  25. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  26. #include "resource_sounds.h"
  27. #include "asterisk/media_index.h"
  28. #include "asterisk/sounds_index.h"
  29. #include "asterisk/format.h"
  30. #include "asterisk/format_cap.h"
  31. #include "asterisk/json.h"
  32. /*! \brief arguments that are necessary for adding format/lang pairs */
  33. struct lang_format_info {
  34. struct ast_json *format_list; /*!< The embedded array to which format/lang pairs should be added */
  35. const char *filename; /*!< Name of the file for which to add format/lang pairs */
  36. const char *format_filter; /*!< Format filter provided in the request */
  37. };
  38. /*! \brief Add format/lang pairs to the array embedded in the sound object */
  39. static int add_format_information_cb(void *obj, void *arg, void *data, int flags)
  40. {
  41. char *language = obj;
  42. struct lang_format_info *args = arg;
  43. int idx;
  44. RAII_VAR(struct ast_format_cap *, cap, NULL, ao2_cleanup);
  45. struct ast_media_index *sounds_index = data;
  46. if (!sounds_index) {
  47. return CMP_STOP;
  48. }
  49. cap = ast_media_get_format_cap(sounds_index, args->filename, language);
  50. if (!cap) {
  51. return CMP_STOP;
  52. }
  53. for (idx = 0; idx < ast_format_cap_count(cap); idx++) {
  54. struct ast_format *format = ast_format_cap_get_format(cap, idx);
  55. struct ast_json *lang_format_pair;
  56. if (!ast_strlen_zero(args->format_filter)
  57. && strcmp(args->format_filter, ast_format_get_name(format))) {
  58. ao2_ref(format, -1);
  59. continue;
  60. }
  61. lang_format_pair = ast_json_pack("{s: s, s: s}",
  62. "language", language,
  63. "format", ast_format_get_name(format));
  64. if (!lang_format_pair) {
  65. ao2_ref(format, -1);
  66. return CMP_STOP;
  67. }
  68. ast_json_array_append(args->format_list, lang_format_pair);
  69. ao2_ref(format, -1);
  70. }
  71. return 0;
  72. }
  73. /*! \brief Filter out all languages not matching the specified language */
  74. static int filter_langs_cb(void *obj, void *arg, int flags)
  75. {
  76. char *lang_filter = arg;
  77. char *lang = obj;
  78. if (strcmp(lang, lang_filter)) {
  79. return CMP_MATCH;
  80. }
  81. return 0;
  82. }
  83. /*! \brief Generate a Sound structure as documented in sounds.json for the specified filename */
  84. static struct ast_json *create_sound_blob(const char *filename,
  85. struct ast_ari_sounds_list_args *args, struct ast_media_index *sounds_index)
  86. {
  87. RAII_VAR(struct ast_json *, sound, NULL, ast_json_unref);
  88. RAII_VAR(struct ao2_container *, languages, NULL, ao2_cleanup);
  89. const char *description;
  90. struct ast_json *format_lang_list;
  91. struct lang_format_info info;
  92. if (!sounds_index) {
  93. return NULL;
  94. }
  95. description = ast_media_get_description(sounds_index, filename, "en");
  96. if (ast_strlen_zero(description)) {
  97. sound = ast_json_pack("{s: s, s: []}",
  98. "id", filename,
  99. "formats");
  100. } else {
  101. sound = ast_json_pack("{s: s, s: s, s: []}",
  102. "id", filename,
  103. "text", description,
  104. "formats");
  105. }
  106. if (!sound) {
  107. return NULL;
  108. }
  109. format_lang_list = ast_json_object_get(sound, "formats");
  110. if (!format_lang_list) {
  111. return NULL;
  112. }
  113. languages = ast_media_get_variants(sounds_index, filename);
  114. if (!languages || !ao2_container_count(languages)) {
  115. return NULL;
  116. }
  117. /* filter requested languages */
  118. if (args && !ast_strlen_zero(args->lang)) {
  119. char *lang_filter = ast_strdupa(args->lang);
  120. ao2_callback(languages, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, filter_langs_cb, lang_filter);
  121. if (!languages || !ao2_container_count(languages)) {
  122. return NULL;
  123. }
  124. }
  125. info.filename = filename;
  126. info.format_list = format_lang_list;
  127. info.format_filter = NULL;
  128. if (args) {
  129. info.format_filter = args->format;
  130. }
  131. ao2_callback_data(languages, OBJ_NODATA, add_format_information_cb, &info, sounds_index);
  132. /* no format/lang pairs for this sound so nothing to return */
  133. if (!ast_json_array_size(format_lang_list)) {
  134. return NULL;
  135. }
  136. return ast_json_ref(sound);
  137. }
  138. struct sounds_cb_data {
  139. struct ast_ari_sounds_list_args *args;
  140. struct ast_media_index *index;
  141. };
  142. /*! \brief Generate a Sound structure and append it to the output blob */
  143. static int append_sound_cb(void *obj, void *arg, void *data, int flags)
  144. {
  145. struct ast_json *sounds_array = arg;
  146. char *filename = obj;
  147. struct sounds_cb_data *cb_data = data;
  148. struct ast_json *sound_blob = create_sound_blob(filename, cb_data->args, cb_data->index);
  149. if (!sound_blob) {
  150. return 0;
  151. }
  152. ast_json_array_append(sounds_array, sound_blob);
  153. return 0;
  154. }
  155. void ast_ari_sounds_list(struct ast_variable *headers,
  156. struct ast_ari_sounds_list_args *args,
  157. struct ast_ari_response *response)
  158. {
  159. RAII_VAR(struct ao2_container *, sound_files, NULL, ao2_cleanup);
  160. struct ast_json *sounds_blob;
  161. RAII_VAR(struct ast_media_index *, sounds_index, ast_sounds_get_index(), ao2_cleanup);
  162. struct sounds_cb_data cb_data = {
  163. .args = args,
  164. .index = sounds_index,
  165. };
  166. if (!sounds_index) {
  167. ast_ari_response_error(response, 500, "Internal Error", "Sounds index not available");
  168. return;
  169. }
  170. sound_files = ast_media_get_media(sounds_index);
  171. if (!sound_files) {
  172. ast_ari_response_error(response, 500, "Internal Error", "Allocation Error");
  173. return;
  174. }
  175. sounds_blob = ast_json_array_create();
  176. if (!sounds_blob) {
  177. ast_ari_response_error(response, 500, "Internal Error", "Allocation Error");
  178. return;
  179. }
  180. ao2_callback_data(sound_files, OBJ_NODATA, append_sound_cb, sounds_blob, &cb_data);
  181. if (!ast_json_array_size(sounds_blob)) {
  182. ast_ari_response_error(response, 404, "Not Found", "No sounds found that matched the query");
  183. ast_json_unref(sounds_blob);
  184. return;
  185. }
  186. ast_ari_response_ok(response, sounds_blob);
  187. }
  188. void ast_ari_sounds_get(struct ast_variable *headers,
  189. struct ast_ari_sounds_get_args *args,
  190. struct ast_ari_response *response)
  191. {
  192. struct ast_json *sound_blob;
  193. struct ast_media_index *sounds_index = ast_sounds_get_index_for_file(args->sound_id);
  194. sound_blob = create_sound_blob(args->sound_id, NULL, sounds_index);
  195. ao2_cleanup(sounds_index);
  196. if (!sound_blob) {
  197. ast_ari_response_error(response, 404, "Not Found", "Sound not found");
  198. return;
  199. }
  200. ast_ari_response_ok(response, sound_blob);
  201. }