Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Functions
resource_sounds.c File Reference

/api-docs/sounds.{format} implementation- Sound resources More...

#include "asterisk.h"
#include "resource_sounds.h"
#include "asterisk/media_index.h"
#include "asterisk/sounds_index.h"
#include "asterisk/format.h"
#include "asterisk/format_cap.h"
#include "asterisk/json.h"
Include dependency graph for resource_sounds.c:

Go to the source code of this file.

Data Structures

struct  lang_format_info
 arguments that are necessary for adding format/lang pairs More...
 
struct  sounds_cb_data
 

Functions

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. More...
 
static int append_sound_cb (void *obj, void *arg, void *data, int flags)
 Generate a Sound structure and append it to the output blob. More...
 
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. More...
 
void ast_ari_sounds_list (struct ast_variable *headers, struct ast_ari_sounds_list_args *args, struct ast_ari_response *response)
 List all sounds. More...
 
static struct ast_jsoncreate_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. More...
 
static int filter_langs_cb (void *obj, void *arg, int flags)
 Filter out all languages not matching the specified language. More...
 

Detailed Description

/api-docs/sounds.{format} implementation- Sound resources

Author
David M. Lee, II dlee@.nosp@m.digi.nosp@m.um.co.nosp@m.m

Definition in file resource_sounds.c.

Function Documentation

◆ add_format_information_cb()

static int add_format_information_cb ( void *  obj,
void *  arg,
void *  data,
int  flags 
)
static

Add format/lang pairs to the array embedded in the sound object.

Definition at line 43 of file resource_sounds.c.

References ao2_cleanup, ao2_ref, args, ast_format_cap_count(), ast_format_cap_get_format(), ast_format_get_name(), ast_json_array_append(), ast_json_pack(), ast_media_get_format_cap(), ast_strlen_zero, CMP_STOP, lang_format_info::filename, format, lang_format_info::format_filter, lang_format_info::format_list, language, NULL, and RAII_VAR.

Referenced by create_sound_blob().

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 }
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:591
arguments that are necessary for adding format/lang pairs
Definition of a media format.
Definition: format.c:43
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
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
const char * args
#define NULL
Definition: resample.c:96
struct ast_json * format_list
#define ast_strlen_zero(foo)
Definition: strings.h:52
const char * format_filter
#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:911
#define ao2_ref(o, delta)
Definition: astobj2.h:464
static char language[MAX_LANGUAGE]
Definition: chan_alsa.c:117
int ast_json_array_append(struct ast_json *array, struct ast_json *value)
Append to an array.
Definition: json.c:368
Format capabilities structure, holds formats + preference order + etc.
Definition: format_cap.c:54
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
Abstract JSON element (object, array, string, int, ...).
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
static snd_pcm_format_t format
Definition: chan_alsa.c:102
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
const char * filename

◆ append_sound_cb()

static int append_sound_cb ( void *  obj,
void *  arg,
void *  data,
int  flags 
)
static

Generate a Sound structure and append it to the output blob.

Definition at line 166 of file resource_sounds.c.

References sounds_cb_data::args, ast_json_array_append(), create_sound_blob(), lang_format_info::filename, and sounds_cb_data::index.

Referenced by ast_ari_sounds_list().

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 }
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.
struct ast_media_index * index
int ast_json_array_append(struct ast_json *array, struct ast_json *value)
Append to an array.
Definition: json.c:368
struct ast_ari_sounds_list_args * args
Abstract JSON element (object, array, string, int, ...).

◆ ast_ari_sounds_get()

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.

Parameters
headersHTTP headers
argsSwagger parameters
[out]responseHTTP response

Definition at line 220 of file resource_sounds.c.

References ao2_cleanup, ast_ari_response_error(), ast_ari_response_ok(), ast_sounds_get_index_for_file(), create_sound_blob(), NULL, and ast_ari_sounds_get_args::sound_id.

Referenced by ast_ari_sounds_get_cb().

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 }
#define NULL
Definition: resample.c:96
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_response_ok(struct ast_ari_response *response, struct ast_json *message)
Fill in an OK (200) ast_ari_response.
Definition: res_ari.c:276
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
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
Abstract JSON element (object, array, string, int, ...).
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

◆ ast_ari_sounds_list()

void ast_ari_sounds_list ( struct ast_variable headers,
struct ast_ari_sounds_list_args args,
struct ast_ari_response response 
)

List all sounds.

Parameters
headersHTTP headers
argsSwagger parameters
[out]responseHTTP response

Definition at line 180 of file resource_sounds.c.

References ao2_callback_data, ao2_cleanup, append_sound_cb(), args, sounds_cb_data::args, ast_ari_response_error(), ast_ari_response_ok(), ast_json_array_create(), ast_json_array_size(), ast_json_unref(), ast_media_get_media(), ast_sounds_get_index(), NULL, OBJ_NODATA, and RAII_VAR.

Referenced by ast_ari_sounds_list_cb().

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 }
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
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
const char * args
#define NULL
Definition: resample.c:96
struct ast_media_index * ast_sounds_get_index(void)
Get the sounds index.
Definition: sounds.c:308
#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:911
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
struct ast_json * ast_json_array_create(void)
Create a empty JSON array.
Definition: json.c:352
#define ao2_callback_data(container, flags, cb_fn, arg, data)
Definition: astobj2.h:1743
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
struct ast_ari_sounds_list_args * args
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
size_t ast_json_array_size(const struct ast_json *array)
Get the size of a JSON array.
Definition: json.c:356
Abstract JSON element (object, array, string, int, ...).
static int append_sound_cb(void *obj, void *arg, void *data, int flags)
Generate a Sound structure and append it to the output blob.
Generic container type.

◆ create_sound_blob()

static struct ast_json* create_sound_blob ( const char *  filename,
struct ast_ari_sounds_list_args args,
struct ast_media_index sounds_index 
)
static

Generate a Sound structure as documented in sounds.json for the specified filename.

Definition at line 97 of file resource_sounds.c.

References add_format_information_cb(), ao2_callback, ao2_callback_data, ao2_cleanup, ao2_container_count(), ast_json_array_size(), ast_json_object_get(), ast_json_pack(), ast_json_ref(), ast_json_unref(), ast_media_get_description(), ast_media_get_variants(), ast_strdupa, ast_strlen_zero, lang_format_info::filename, filter_langs_cb(), ast_ari_sounds_list_args::format, lang_format_info::format_filter, lang_format_info::format_list, ast_ari_sounds_list_args::lang, NULL, OBJ_MULTIPLE, OBJ_NODATA, OBJ_UNLINK, and RAII_VAR.

Referenced by append_sound_cb(), and ast_ari_sounds_get().

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 }
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
Definition: json.c:67
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:591
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
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
arguments that are necessary for adding format/lang pairs
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:1716
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
#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:911
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
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
static int filter_langs_cb(void *obj, void *arg, int flags)
Filter out all languages not matching the specified language.
def info(msg)
#define ao2_callback_data(container, flags, cb_fn, arg, data)
Definition: astobj2.h:1743
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:397
size_t ast_json_array_size(const struct ast_json *array)
Get the size of a JSON array.
Definition: json.c:356
Abstract JSON element (object, array, string, int, ...).
Generic container type.
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.
const char * filename

◆ filter_langs_cb()

static int filter_langs_cb ( void *  obj,
void *  arg,
int  flags 
)
static

Filter out all languages not matching the specified language.

Definition at line 86 of file resource_sounds.c.

References CMP_MATCH.

Referenced by create_sound_blob().

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 }