Asterisk - The Open Source Telephony Project GIT-master-f36a736
resource_sounds.c
Go to the documentation of this file.
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
19/*! \file
20 *
21 * \brief /api-docs/sounds.{format} implementation- Sound resources
22 *
23 * \author David M. Lee, II <dlee@digium.com>
24 */
25
26#include "asterisk.h"
27
28#include "resource_sounds.h"
31#include "asterisk/format.h"
32#include "asterisk/format_cap.h"
33#include "asterisk/json.h"
34
35/*! \brief arguments that are necessary for adding format/lang pairs */
37 struct ast_json *format_list; /*!< The embedded array to which format/lang pairs should be added */
38 const char *filename; /*!< Name of the file for which to add format/lang pairs */
39 const char *format_filter; /*!< Format filter provided in the request */
40};
41
42/*! \brief Add format/lang pairs to the array embedded in the sound object */
43static 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/*! \brief Filter out all languages not matching the specified language */
86static 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/*! \brief Generate a Sound structure as documented in sounds.json for the specified filename */
97static 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 /* filter requested languages */
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 /* no format/lang pairs for this sound so nothing to return */
153 if (!ast_json_array_size(format_lang_list)) {
154 return NULL;
155 }
156
157 return ast_json_ref(sound);
158}
159
163};
164
165/*! \brief Generate a Sound structure and append it to the output blob */
166static 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
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
220void ast_ari_sounds_get(struct ast_variable *headers,
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}
void ast_ari_response_error(struct ast_ari_response *response, int response_code, const char *response_text, const char *message_fmt,...)
Fill in an error ast_ari_response.
Definition: res_ari.c:259
void ast_ari_response_ok(struct ast_ari_response *response, struct ast_json *message)
Fill in an OK (200) ast_ari_response.
Definition: res_ari.c:276
Asterisk main include file. File version handling, generic pbx functions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
@ CMP_MATCH
Definition: astobj2.h:1027
@ CMP_STOP
Definition: astobj2.h:1028
#define ao2_callback(c, flags, cb_fn, arg)
ao2_callback() is a generic function that applies cb_fn() to all objects in a container,...
Definition: astobj2.h:1693
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define ao2_callback_data(container, flags, cb_fn, arg, data)
Definition: astobj2.h:1723
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
@ OBJ_NODATA
Definition: astobj2.h:1044
@ OBJ_MULTIPLE
Definition: astobj2.h:1049
@ OBJ_UNLINK
Definition: astobj2.h:1039
static char language[MAX_LANGUAGE]
Definition: chan_iax2.c:324
Media Format API.
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
Format Capabilities API.
struct ast_format * ast_format_cap_get_format(const struct ast_format_cap *cap, int position)
Get the format at a specific index.
Definition: format_cap.c:400
size_t ast_format_cap_count(const struct ast_format_cap *cap)
Get the number of formats present within the capabilities structure.
Definition: format_cap.c:395
Asterisk JSON abstraction layer.
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
int ast_json_array_append(struct ast_json *array, struct ast_json *value)
Append to an array.
Definition: json.c:378
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:612
struct ast_json * ast_json_array_create(void)
Create a empty JSON array.
Definition: json.c:362
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
Definition: json.c:67
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:407
size_t ast_json_array_size(const struct ast_json *array)
Get the size of a JSON array.
Definition: json.c:366
Media file format and description indexing engine.
const char * ast_media_get_description(struct ast_media_index *index, const char *filename, const char *variant)
Get the description for a media file.
Definition: media_index.c:230
struct ao2_container * ast_media_get_media(struct ast_media_index *index)
Get the a container of all media available on the system.
Definition: media_index.c:307
struct ast_format_cap * ast_media_get_format_cap(struct ast_media_index *index, const char *filename, const char *variant)
Get the ast_format_cap for a media file.
Definition: media_index.c:245
struct ao2_container * ast_media_get_variants(struct ast_media_index *index, const char *filename)
Get the languages in which a media file is available.
Definition: media_index.c:274
def info(msg)
#define NULL
Definition: resample.c:96
void ast_ari_sounds_get(struct ast_variable *headers, struct ast_ari_sounds_get_args *args, struct ast_ari_response *response)
Get a sound's details.
static int filter_langs_cb(void *obj, void *arg, int flags)
Filter out all languages not matching the specified language.
static struct ast_json * create_sound_blob(const char *filename, struct ast_ari_sounds_list_args *args, struct ast_media_index *sounds_index)
Generate a Sound structure as documented in sounds.json for the specified filename.
void ast_ari_sounds_list(struct ast_variable *headers, struct ast_ari_sounds_list_args *args, struct ast_ari_response *response)
List all sounds.
static int add_format_information_cb(void *obj, void *arg, void *data, int flags)
Add format/lang pairs to the array embedded in the sound object.
static int append_sound_cb(void *obj, void *arg, void *data, int flags)
Generate a Sound structure and append it to the output blob.
Generated file - declares stubs to be implemented in res/ari/resource_sounds.c.
Sound file format and description indexer.
struct ast_media_index * ast_sounds_get_index(void)
Get the sounds index.
Definition: sounds.c:308
struct ast_media_index * ast_sounds_get_index_for_file(const char *filename)
Get the index for a specific sound file.
Definition: sounds.c:313
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
Generic container type.
Format capabilities structure, holds formats + preference order + etc.
Definition: format_cap.c:54
Definition of a media format.
Definition: format.c:43
Abstract JSON element (object, array, string, int, ...).
Structure for variables, used for configurations and for channel variables.
arguments that are necessary for adding format/lang pairs
const char * format_filter
struct ast_json * format_list
const char * filename
struct ast_media_index * index
struct ast_ari_sounds_list_args * args
const char * args
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941