| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#include "asterisk.h" |
| 27 |
|
| 28 |
#include "resource_sounds.h" |
| 29 |
#include "asterisk/media_index.h" |
| 30 |
#include "asterisk/sounds_index.h" |
| 31 |
#include "asterisk/format.h" |
| 32 |
#include "asterisk/format_cap.h" |
| 33 |
#include "asterisk/json.h" |
| 34 |
|
| 35 |
|
| 36 |
struct lang_format_info { |
| 37 |
struct ast_json *format_list; |
| 38 |
const char *filename; |
| 39 |
const char *format_filter; |
| 40 |
}; |
| 41 |
|
| 42 |
|
| 43 |
static int add_format_information_cb(void *obj, void *arg, void *data, int flags) |
| 44 |
{ |
| 45 |
char *language = obj; |
| 46 |
struct lang_format_info *args = arg; |
| 47 |
int idx; |
| 48 |
RAII_VAR(struct ast_format_cap *, cap, NULL, ao2_cleanup); |
| 49 |
struct ast_media_index *sounds_index = data; |
| 50 |
|
| 51 |
if (!sounds_index) { |
| 52 |
return CMP_STOP; |
| 53 |
} |
| 54 |
|
| 55 |
cap = ast_media_get_format_cap(sounds_index, args->filename, language); |
| 56 |
if (!cap) { |
| 57 |
return CMP_STOP; |
| 58 |
} |
| 59 |
|
| 60 |
for (idx = 0; idx < ast_format_cap_count(cap); idx++) { |
| 61 |
struct ast_format *format = ast_format_cap_get_format(cap, idx); |
| 62 |
struct ast_json *lang_format_pair; |
| 63 |
|
| 64 |
if (!ast_strlen_zero(args->format_filter) |
| 65 |
&& strcmp(args->format_filter, ast_format_get_name(format))) { |
| 66 |
ao2_ref(format, -1); |
| 67 |
continue; |
| 68 |
} |
| 69 |
|
| 70 |
lang_format_pair = ast_json_pack("{s: s, s: s}", |
| 71 |
"language", language, |
| 72 |
"format", ast_format_get_name(format)); |
| 73 |
if (!lang_format_pair) { |
| 74 |
ao2_ref(format, -1); |
| 75 |
return CMP_STOP; |
| 76 |
} |
| 77 |
|
| 78 |
ast_json_array_append(args->format_list, lang_format_pair); |
| 79 |
ao2_ref(format, -1); |
| 80 |
} |
| 81 |
|
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
static int filter_langs_cb(void *obj, void *arg, int flags) |
| 87 |
{ |
| 88 |
char *lang_filter = arg; |
| 89 |
char *lang = obj; |
| 90 |
if (strcmp(lang, lang_filter)) { |
| 91 |
return CMP_MATCH; |
| 92 |
} |
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
static struct ast_json *create_sound_blob(const char *filename, |
| 98 |
struct ast_ari_sounds_list_args *args, struct ast_media_index *sounds_index) |
| 99 |
{ |
| 100 |
RAII_VAR(struct ast_json *, sound, NULL, ast_json_unref); |
| 101 |
RAII_VAR(struct ao2_container *, languages, NULL, ao2_cleanup); |
| 102 |
const char *description; |
| 103 |
struct ast_json *format_lang_list; |
| 104 |
struct lang_format_info info; |
| 105 |
|
| 106 |
if (!sounds_index) { |
| 107 |
return NULL; |
| 108 |
} |
| 109 |
|
| 110 |
description = ast_media_get_description(sounds_index, filename, "en"); |
| 111 |
if (ast_strlen_zero(description)) { |
| 112 |
sound = ast_json_pack("{s: s, s: []}", |
| 113 |
"id", filename, |
| 114 |
"formats"); |
| 115 |
} else { |
| 116 |
sound = ast_json_pack("{s: s, s: s, s: []}", |
| 117 |
"id", filename, |
| 118 |
"text", description, |
| 119 |
"formats"); |
| 120 |
} |
| 121 |
if (!sound) { |
| 122 |
return NULL; |
| 123 |
} |
| 124 |
|
| 125 |
format_lang_list = ast_json_object_get(sound, "formats"); |
| 126 |
if (!format_lang_list) { |
| 127 |
return NULL; |
| 128 |
} |
| 129 |
|
| 130 |
languages = ast_media_get_variants(sounds_index, filename); |
| 131 |
if (!languages || !ao2_container_count(languages)) { |
| 132 |
return NULL; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
if (args && !ast_strlen_zero(args->lang)) { |
| 137 |
char *lang_filter = ast_strdupa(args->lang); |
| 138 |
ao2_callback(languages, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, filter_langs_cb, lang_filter); |
| 139 |
if (!languages || !ao2_container_count(languages)) { |
| 140 |
return NULL; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
info.filename = filename; |
| 145 |
info.format_list = format_lang_list; |
| 146 |
info.format_filter = NULL; |
| 147 |
if (args) { |
| 148 |
info.format_filter = args->format; |
| 149 |
} |
| 150 |
ao2_callback_data(languages, OBJ_NODATA, add_format_information_cb, &info, sounds_index); |
| 151 |
|
| 152 |
|
| 153 |
if (!ast_json_array_size(format_lang_list)) { |
| 154 |
return NULL; |
| 155 |
} |
| 156 |
|
| 157 |
return ast_json_ref(sound); |
| 158 |
} |
| 159 |
|
| 160 |
struct sounds_cb_data { |
| 161 |
struct ast_ari_sounds_list_args *args; |
| 162 |
struct ast_media_index *index; |
| 163 |
}; |
| 164 |
|
| 165 |
|
| 166 |
static int append_sound_cb(void *obj, void *arg, void *data, int flags) |
| 167 |
{ |
| 168 |
struct ast_json *sounds_array = arg; |
| 169 |
char *filename = obj; |
| 170 |
struct sounds_cb_data *cb_data = data; |
| 171 |
struct ast_json *sound_blob = create_sound_blob(filename, cb_data->args, cb_data->index); |
| 172 |
if (!sound_blob) { |
| 173 |
return 0; |
| 174 |
} |
| 175 |
|
| 176 |
ast_json_array_append(sounds_array, sound_blob); |
| 177 |
return 0; |
| 178 |
} |
| 179 |
|
| 180 |
void ast_ari_sounds_list(struct ast_variable *headers, |
| 181 |
struct ast_ari_sounds_list_args *args, |
| 182 |
struct ast_ari_response *response) |
| 183 |
{ |
| 184 |
RAII_VAR(struct ao2_container *, sound_files, NULL, ao2_cleanup); |
| 185 |
struct ast_json *sounds_blob; |
| 186 |
RAII_VAR(struct ast_media_index *, sounds_index, ast_sounds_get_index(), ao2_cleanup); |
| 187 |
struct sounds_cb_data cb_data = { |
| 188 |
.args = args, |
| 189 |
.index = sounds_index, |
| 190 |
}; |
| 191 |
|
| 192 |
if (!sounds_index) { |
| 193 |
ast_ari_response_error(response, 500, "Internal Error", "Sounds index not available"); |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
sound_files = ast_media_get_media(sounds_index); |
| 198 |
if (!sound_files) { |
| 199 |
ast_ari_response_error(response, 500, "Internal Error", "Allocation Error"); |
| 200 |
return; |
| 201 |
} |
| 202 |
|
| 203 |
sounds_blob = ast_json_array_create(); |
| 204 |
if (!sounds_blob) { |
| 205 |
ast_ari_response_error(response, 500, "Internal Error", "Allocation Error"); |
| 206 |
return; |
| 207 |
} |
| 208 |
|
| 209 |
ao2_callback_data(sound_files, OBJ_NODATA, append_sound_cb, sounds_blob, &cb_data); |
| 210 |
|
| 211 |
if (!ast_json_array_size(sounds_blob)) { |
| 212 |
ast_ari_response_error(response, 404, "Not Found", "No sounds found that matched the query"); |
| 213 |
ast_json_unref(sounds_blob); |
| 214 |
return; |
| 215 |
} |
| 216 |
|
| 217 |
ast_ari_response_ok(response, sounds_blob); |
| 218 |
} |
| 219 |
|
| 220 |
void ast_ari_sounds_get(struct ast_variable *headers, |
| 221 |
struct ast_ari_sounds_get_args *args, |
| 222 |
struct ast_ari_response *response) |
| 223 |
{ |
| 224 |
struct ast_json *sound_blob; |
| 225 |
struct ast_media_index *sounds_index = ast_sounds_get_index_for_file(args->sound_id); |
| 226 |
|
| 227 |
sound_blob = create_sound_blob(args->sound_id, NULL, sounds_index); |
| 228 |
ao2_cleanup(sounds_index); |
| 229 |
if (!sound_blob) { |
| 230 |
ast_ari_response_error(response, 404, "Not Found", "Sound not found"); |
| 231 |
return; |
| 232 |
} |
| 233 |
|
| 234 |
ast_ari_response_ok(response, sound_blob); |
| 235 |
} |