resource_sounds.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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, 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. RAII_VAR(struct ast_media_index *, sounds_index, ast_sounds_get_index(), ao2_cleanup);
  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)
  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. RAII_VAR(struct ast_media_index *, sounds_index, ast_sounds_get_index(), ao2_cleanup);
  93. if (!sounds_index) {
  94. return NULL;
  95. }
  96. description = ast_media_get_description(sounds_index, filename, "en");
  97. if (ast_strlen_zero(description)) {
  98. sound = ast_json_pack("{s: s, s: []}",
  99. "id", filename,
  100. "formats");
  101. } else {
  102. sound = ast_json_pack("{s: s, s: s, s: []}",
  103. "id", filename,
  104. "text", description,
  105. "formats");
  106. }
  107. if (!sound) {
  108. return NULL;
  109. }
  110. format_lang_list = ast_json_object_get(sound, "formats");
  111. if (!format_lang_list) {
  112. return NULL;
  113. }
  114. languages = ast_media_get_variants(sounds_index, filename);
  115. if (!languages || !ao2_container_count(languages)) {
  116. return NULL;
  117. }
  118. /* filter requested languages */
  119. if (args && !ast_strlen_zero(args->lang)) {
  120. char *lang_filter = ast_strdupa(args->lang);
  121. ao2_callback(languages, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, filter_langs_cb, lang_filter);
  122. if (!languages || !ao2_container_count(languages)) {
  123. return NULL;
  124. }
  125. }
  126. info.filename = filename;
  127. info.format_list = format_lang_list;
  128. info.format_filter = NULL;
  129. if (args) {
  130. info.format_filter = args->format;
  131. }
  132. ao2_callback(languages, OBJ_NODATA, add_format_information_cb, &info);
  133. /* no format/lang pairs for this sound so nothing to return */
  134. if (!ast_json_array_size(format_lang_list)) {
  135. return NULL;
  136. }
  137. return ast_json_ref(sound);
  138. }
  139. /*! \brief Generate a Sound structure and append it to the output blob */
  140. static int append_sound_cb(void *obj, void *arg, void *data, int flags)
  141. {
  142. struct ast_json *sounds_array = arg;
  143. char *filename = obj;
  144. struct ast_ari_sounds_list_args *args = data;
  145. struct ast_json *sound_blob = create_sound_blob(filename, args);
  146. if (!sound_blob) {
  147. return 0;
  148. }
  149. ast_json_array_append(sounds_array, sound_blob);
  150. return 0;
  151. }
  152. void ast_ari_sounds_list(struct ast_variable *headers,
  153. struct ast_ari_sounds_list_args *args,
  154. struct ast_ari_response *response)
  155. {
  156. RAII_VAR(struct ao2_container *, sound_files, NULL, ao2_cleanup);
  157. struct ast_json *sounds_blob;
  158. RAII_VAR(struct ast_media_index *, sounds_index, ast_sounds_get_index(), ao2_cleanup);
  159. if (!sounds_index) {
  160. ast_ari_response_error(response, 500, "Internal Error", "Sounds index not available");
  161. return;
  162. }
  163. sound_files = ast_media_get_media(sounds_index);
  164. if (!sound_files) {
  165. ast_ari_response_error(response, 500, "Internal Error", "Allocation Error");
  166. return;
  167. }
  168. sounds_blob = ast_json_array_create();
  169. if (!sounds_blob) {
  170. ast_ari_response_error(response, 500, "Internal Error", "Allocation Error");
  171. return;
  172. }
  173. ao2_callback_data(sound_files, OBJ_NODATA, append_sound_cb, sounds_blob, args);
  174. if (!ast_json_array_size(sounds_blob)) {
  175. ast_ari_response_error(response, 404, "Not Found", "No sounds found that matched the query");
  176. return;
  177. }
  178. ast_ari_response_ok(response, sounds_blob);
  179. }
  180. void ast_ari_sounds_get(struct ast_variable *headers,
  181. struct ast_ari_sounds_get_args *args,
  182. struct ast_ari_response *response)
  183. {
  184. struct ast_json *sound_blob;
  185. sound_blob = create_sound_blob(args->sound_id, NULL);
  186. if (!sound_blob) {
  187. ast_ari_response_error(response, 404, "Not Found", "Sound not found");
  188. return;
  189. }
  190. ast_ari_response_ok(response, sound_blob);
  191. }