mirror of
https://github.com/asterisk/asterisk.git
synced 2025-09-03 11:25:35 +00:00
Index installed sounds and implement ARI sounds queries
This adds support for stasis/sounds and stasis/sounds/{ID} queries via the Asterisk RESTful Interface (ARI, formerly Stasis-HTTP). The following changes have been made to accomplish this: * A modular indexer was created for local media. * A new function to get an ast_format associated with a file extension was added. * Modifications were made to the built-in HTTP server so that URI decoding could be deferred to the URI handler when necessary. * The Stasis-HTTP sounds JSON documentation was modified to handle cases where multiple languages are installed in different formats. * Register and Unregister events for formats were added to the system topic. (closes issue ASTERISK-21584) (closes issue ASTERISK-21585) Review: https://reviewboard.asterisk.org/r/2507/ git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@392700 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
@@ -140,4 +140,7 @@ int ast_rtp_engine_init(void);
|
|||||||
* \since 12.0.0
|
* \since 12.0.0
|
||||||
*/
|
*/
|
||||||
int ast_parking_stasis_init(void);
|
int ast_parking_stasis_init(void);
|
||||||
|
|
||||||
|
/*! \brief initialize the sounds index */
|
||||||
|
int ast_sounds_index_init(void);
|
||||||
#endif /* _ASTERISK__PRIVATE_H */
|
#endif /* _ASTERISK__PRIVATE_H */
|
||||||
|
@@ -376,6 +376,17 @@ int ast_file_init(void);
|
|||||||
*/
|
*/
|
||||||
char *ast_format_str_reduce(char *fmts);
|
char *ast_format_str_reduce(char *fmts);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Get the ast_format associated with the given file extension
|
||||||
|
* \since 12
|
||||||
|
*
|
||||||
|
* \param file_ext The file extension for which to find the format
|
||||||
|
*
|
||||||
|
* \retval NULL if not found
|
||||||
|
* \retval A pointer to the ast_format associated with this file extension
|
||||||
|
*/
|
||||||
|
const struct ast_format *ast_get_format_for_file_ext(const char *file_ext);
|
||||||
|
|
||||||
#if defined(__cplusplus) || defined(c_plusplus)
|
#if defined(__cplusplus) || defined(c_plusplus)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@@ -471,4 +471,22 @@ int ast_format_is_slinear(const struct ast_format *format);
|
|||||||
* \brief Get the best slinear format id for a given sample rate
|
* \brief Get the best slinear format id for a given sample rate
|
||||||
*/
|
*/
|
||||||
enum ast_format_id ast_format_slin_by_rate(unsigned int rate);
|
enum ast_format_id ast_format_slin_by_rate(unsigned int rate);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \since 12
|
||||||
|
* \brief Get the message type used for signaling a format registration
|
||||||
|
*
|
||||||
|
* \retval Stasis message type for format registration
|
||||||
|
* \retval NULL on error
|
||||||
|
*/
|
||||||
|
struct stasis_message_type *ast_format_register_type(void);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \since 12
|
||||||
|
* \brief Get the message type used for signaling a format unregistration
|
||||||
|
*
|
||||||
|
* \retval Stasis message type for format unregistration
|
||||||
|
* \retval NULL on error
|
||||||
|
*/
|
||||||
|
struct stasis_message_type *ast_format_unregister_type(void);
|
||||||
#endif /* _AST_FORMAT_H */
|
#endif /* _AST_FORMAT_H */
|
||||||
|
@@ -101,6 +101,8 @@ struct ast_http_uri {
|
|||||||
unsigned int mallocd:1;
|
unsigned int mallocd:1;
|
||||||
/*! Data structure is malloc'd */
|
/*! Data structure is malloc'd */
|
||||||
unsigned int dmallocd:1;
|
unsigned int dmallocd:1;
|
||||||
|
/*! Don't automatically decode URI before passing it to the callback */
|
||||||
|
unsigned int no_decode_uri:1;
|
||||||
/*! Data to bind to the uri if needed */
|
/*! Data to bind to the uri if needed */
|
||||||
void *data;
|
void *data;
|
||||||
/*! Key to be used for unlinking if multiple URIs registered */
|
/*! Key to be used for unlinking if multiple URIs registered */
|
||||||
|
108
include/asterisk/media_index.h
Normal file
108
include/asterisk/media_index.h
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
/*
|
||||||
|
* Asterisk -- An open source telephony toolkit.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2013, Digium, Inc.
|
||||||
|
*
|
||||||
|
* Kinsey Moore <kmoore@digium.com>
|
||||||
|
*
|
||||||
|
* See http://www.asterisk.org for more information about
|
||||||
|
* the Asterisk project. Please do not directly contact
|
||||||
|
* any of the maintainers of this project for assistance;
|
||||||
|
* the project provides a web site, mailing lists and IRC
|
||||||
|
* channels for your use.
|
||||||
|
*
|
||||||
|
* This program is free software, distributed under the terms of
|
||||||
|
* the GNU General Public License Version 2. See the LICENSE file
|
||||||
|
* at the top of the source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*! \file
|
||||||
|
* \brief Media file format and description indexing engine.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _ASTERISK_MEDIA_INDEX_H
|
||||||
|
#define _ASTERISK_MEDIA_INDEX_H
|
||||||
|
|
||||||
|
#if defined(__cplusplus) || defined(c_plusplus)
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct ast_format_cap;
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Object representing a media index
|
||||||
|
*/
|
||||||
|
struct ast_media_index;
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Creates a new media index
|
||||||
|
*
|
||||||
|
* \param base_dir Base directory for indexing
|
||||||
|
*
|
||||||
|
* \retval NULL on error
|
||||||
|
* \retval A new AO2 refcounted media index
|
||||||
|
*/
|
||||||
|
struct ast_media_index *ast_media_index_create(
|
||||||
|
const char *base_dir);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Get the description for a media file
|
||||||
|
*
|
||||||
|
* \param index Media index in which to query information
|
||||||
|
* \param filename Name of the file for which to get the description
|
||||||
|
* \param variant Media variant for which to get the description
|
||||||
|
*
|
||||||
|
* \retval NULL if not found
|
||||||
|
* \return The description requested (must be copied to be kept)
|
||||||
|
*/
|
||||||
|
const char *ast_media_get_description(struct ast_media_index *index, const char *filename, const char *variant);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Get the ast_format_cap for a media file
|
||||||
|
*
|
||||||
|
* \param index Media index in which to query information
|
||||||
|
* \param filename Name of the file for which to get the description
|
||||||
|
* \param variant Media variant for which to get the description
|
||||||
|
*
|
||||||
|
* \retval NULL if not found
|
||||||
|
* \return a copy of the format capabilities (must be destroyed with ast_format_cap_destroy)
|
||||||
|
*/
|
||||||
|
struct ast_format_cap *ast_media_get_format_cap(struct ast_media_index *index, const char *filename, const char *variant);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Get the languages in which a media file is available
|
||||||
|
*
|
||||||
|
* \param index Media index in which to query information
|
||||||
|
* \param filename Name of the file for which to get available languages
|
||||||
|
*
|
||||||
|
* \retval NULL on error
|
||||||
|
* \return an ast_str_container filled with language strings
|
||||||
|
*/
|
||||||
|
struct ao2_container *ast_media_get_variants(struct ast_media_index *index, const char *filename);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Get the a container of all media available on the system
|
||||||
|
*
|
||||||
|
* \param index Media index in which to query information
|
||||||
|
*
|
||||||
|
* \retval NULL on error
|
||||||
|
* \return an ast_str_container filled with media file name strings
|
||||||
|
*/
|
||||||
|
struct ao2_container *ast_media_get_media(struct ast_media_index *index);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Update a media index
|
||||||
|
*
|
||||||
|
* \param index Media index in which to query information
|
||||||
|
* \param variant Media variant for which to get the description
|
||||||
|
*
|
||||||
|
* \retval non-zero on error
|
||||||
|
* \return zero on success
|
||||||
|
*/
|
||||||
|
int ast_media_index_update(struct ast_media_index *index,
|
||||||
|
const char *variant);
|
||||||
|
#if defined(__cplusplus) || defined(c_plusplus)
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* _ASTERISK_MEDIA_INDEX_H */
|
55
include/asterisk/sounds_index.h
Normal file
55
include/asterisk/sounds_index.h
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* Asterisk -- An open source telephony toolkit.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2013, Digium, Inc.
|
||||||
|
*
|
||||||
|
* Kinsey Moore <kmoore@digium.com>
|
||||||
|
*
|
||||||
|
* See http://www.asterisk.org for more information about
|
||||||
|
* the Asterisk project. Please do not directly contact
|
||||||
|
* any of the maintainers of this project for assistance;
|
||||||
|
* the project provides a web site, mailing lists and IRC
|
||||||
|
* channels for your use.
|
||||||
|
*
|
||||||
|
* This program is free software, distributed under the terms of
|
||||||
|
* the GNU General Public License Version 2. See the LICENSE file
|
||||||
|
* at the top of the source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*! \file
|
||||||
|
* \brief Sound file format and description indexer.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _ASTERISK_SOUNDS_INDEX_H
|
||||||
|
#define _ASTERISK_SOUNDS_INDEX_H
|
||||||
|
|
||||||
|
#if defined(__cplusplus) || defined(c_plusplus)
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Object representing a media index
|
||||||
|
*/
|
||||||
|
struct ast_media_index;
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Reload the sounds index
|
||||||
|
*
|
||||||
|
* \retval zero on success
|
||||||
|
* \retval non-zero on failure
|
||||||
|
*/
|
||||||
|
int ast_sounds_reindex(void);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Get the sounds index
|
||||||
|
*
|
||||||
|
* \retval sounds index (must be ao2_cleanup()'ed)
|
||||||
|
* \retval NULL on failure
|
||||||
|
*/
|
||||||
|
struct ast_media_index *ast_sounds_get_index(void);
|
||||||
|
|
||||||
|
#if defined(__cplusplus) || defined(c_plusplus)
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* _ASTERISK_SOUNDS_INDEX_H */
|
@@ -4396,6 +4396,11 @@ int main(int argc, char *argv[])
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ast_sounds_index_init()) {
|
||||||
|
printf("%s", term_quit());
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
if ((moduleresult = load_modules(0))) { /* Load modules */
|
if ((moduleresult = load_modules(0))) { /* Load modules */
|
||||||
printf("%s", term_quit());
|
printf("%s", term_quit());
|
||||||
exit(moduleresult == -2 ? 2 : 1);
|
exit(moduleresult == -2 ? 2 : 1);
|
||||||
|
70
main/file.c
70
main/file.c
@@ -51,6 +51,8 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
|||||||
#include "asterisk/module.h"
|
#include "asterisk/module.h"
|
||||||
#include "asterisk/astobj2.h"
|
#include "asterisk/astobj2.h"
|
||||||
#include "asterisk/test.h"
|
#include "asterisk/test.h"
|
||||||
|
#include "asterisk/stasis.h"
|
||||||
|
#include "asterisk/json.h"
|
||||||
|
|
||||||
/*! \brief
|
/*! \brief
|
||||||
* The following variable controls the layout of localized sound files.
|
* The following variable controls the layout of localized sound files.
|
||||||
@@ -66,6 +68,55 @@ int ast_language_is_prefix = 1;
|
|||||||
|
|
||||||
static AST_RWLIST_HEAD_STATIC(formats, ast_format_def);
|
static AST_RWLIST_HEAD_STATIC(formats, ast_format_def);
|
||||||
|
|
||||||
|
STASIS_MESSAGE_TYPE_DEFN(ast_format_register_type);
|
||||||
|
STASIS_MESSAGE_TYPE_DEFN(ast_format_unregister_type);
|
||||||
|
|
||||||
|
static struct ast_json *json_array_from_list(const char *list, const char *sep)
|
||||||
|
{
|
||||||
|
RAII_VAR(struct ast_json *, array, ast_json_array_create(), ast_json_unref);
|
||||||
|
RAII_VAR(char *, stringp, ast_strdup(list), ast_free);
|
||||||
|
char *ext;
|
||||||
|
|
||||||
|
if (!array || !stringp) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
while ((ext = strsep(&stringp, sep))) {
|
||||||
|
if (ast_json_array_append(array, ast_json_string_create(ext))) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ast_json_ref(array);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int publish_format_update(const struct ast_format_def *f, struct stasis_message_type *type)
|
||||||
|
{
|
||||||
|
RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
|
||||||
|
RAII_VAR(struct ast_json_payload *, json_payload, NULL, ao2_cleanup);
|
||||||
|
RAII_VAR(struct ast_json *, json_object, NULL, ast_json_unref);
|
||||||
|
|
||||||
|
json_object = ast_json_pack("{s: s, s: o}",
|
||||||
|
"format", f->name,
|
||||||
|
"extensions", json_array_from_list(f->exts, "|"));
|
||||||
|
if (!json_object) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
json_payload = ast_json_payload_create(json_object);
|
||||||
|
if (!json_payload) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
msg = stasis_message_create(type, json_payload);
|
||||||
|
if (!msg) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
stasis_publish(ast_system_topic(), msg);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int __ast_format_def_register(const struct ast_format_def *f, struct ast_module *mod)
|
int __ast_format_def_register(const struct ast_format_def *f, struct ast_module *mod)
|
||||||
{
|
{
|
||||||
struct ast_format_def *tmp;
|
struct ast_format_def *tmp;
|
||||||
@@ -99,6 +150,7 @@ int __ast_format_def_register(const struct ast_format_def *f, struct ast_module
|
|||||||
AST_RWLIST_INSERT_HEAD(&formats, tmp, list);
|
AST_RWLIST_INSERT_HEAD(&formats, tmp, list);
|
||||||
AST_RWLIST_UNLOCK(&formats);
|
AST_RWLIST_UNLOCK(&formats);
|
||||||
ast_verb(2, "Registered file format %s, extension(s) %s\n", f->name, f->exts);
|
ast_verb(2, "Registered file format %s, extension(s) %s\n", f->name, f->exts);
|
||||||
|
publish_format_update(f, ast_format_register_type());
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -112,6 +164,7 @@ int ast_format_def_unregister(const char *name)
|
|||||||
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&formats, tmp, list) {
|
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&formats, tmp, list) {
|
||||||
if (!strcasecmp(name, tmp->name)) {
|
if (!strcasecmp(name, tmp->name)) {
|
||||||
AST_RWLIST_REMOVE_CURRENT(list);
|
AST_RWLIST_REMOVE_CURRENT(list);
|
||||||
|
publish_format_update(tmp, ast_format_unregister_type());
|
||||||
ast_free(tmp);
|
ast_free(tmp);
|
||||||
res = 0;
|
res = 0;
|
||||||
}
|
}
|
||||||
@@ -1672,6 +1725,19 @@ static char *handle_cli_core_show_file_formats(struct ast_cli_entry *e, int cmd,
|
|||||||
#undef FORMAT2
|
#undef FORMAT2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const struct ast_format *ast_get_format_for_file_ext(const char *file_ext)
|
||||||
|
{
|
||||||
|
struct ast_format_def *f;
|
||||||
|
SCOPED_RDLOCK(lock, &formats.lock);
|
||||||
|
AST_RWLIST_TRAVERSE(&formats, f, list) {
|
||||||
|
if (exts_compare(f->exts, file_ext)) {
|
||||||
|
return &f->format;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
static struct ast_cli_entry cli_file[] = {
|
static struct ast_cli_entry cli_file[] = {
|
||||||
AST_CLI_DEFINE(handle_cli_core_show_file_formats, "Displays file formats")
|
AST_CLI_DEFINE(handle_cli_core_show_file_formats, "Displays file formats")
|
||||||
};
|
};
|
||||||
@@ -1679,10 +1745,14 @@ static struct ast_cli_entry cli_file[] = {
|
|||||||
static void file_shutdown(void)
|
static void file_shutdown(void)
|
||||||
{
|
{
|
||||||
ast_cli_unregister_multiple(cli_file, ARRAY_LEN(cli_file));
|
ast_cli_unregister_multiple(cli_file, ARRAY_LEN(cli_file));
|
||||||
|
STASIS_MESSAGE_TYPE_CLEANUP(ast_format_register_type);
|
||||||
|
STASIS_MESSAGE_TYPE_CLEANUP(ast_format_unregister_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ast_file_init(void)
|
int ast_file_init(void)
|
||||||
{
|
{
|
||||||
|
STASIS_MESSAGE_TYPE_INIT(ast_format_register_type);
|
||||||
|
STASIS_MESSAGE_TYPE_INIT(ast_format_unregister_type);
|
||||||
ast_cli_register_multiple(cli_file, ARRAY_LEN(cli_file));
|
ast_cli_register_multiple(cli_file, ARRAY_LEN(cli_file));
|
||||||
ast_register_atexit(file_shutdown);
|
ast_register_atexit(file_shutdown);
|
||||||
return 0;
|
return 0;
|
||||||
|
@@ -718,7 +718,6 @@ static int handle_uri(struct ast_tcptls_session_instance *ser, char *uri,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ast_uri_decode(uri, ast_uri_http_legacy);
|
|
||||||
|
|
||||||
AST_RWLIST_RDLOCK(&uri_redirects);
|
AST_RWLIST_RDLOCK(&uri_redirects);
|
||||||
AST_RWLIST_TRAVERSE(&uri_redirects, redirect, entry) {
|
AST_RWLIST_TRAVERSE(&uri_redirects, redirect, entry) {
|
||||||
@@ -760,6 +759,9 @@ static int handle_uri(struct ast_tcptls_session_instance *ser, char *uri,
|
|||||||
AST_RWLIST_UNLOCK(&uris);
|
AST_RWLIST_UNLOCK(&uris);
|
||||||
}
|
}
|
||||||
if (urih) {
|
if (urih) {
|
||||||
|
if (!urih->no_decode_uri) {
|
||||||
|
ast_uri_decode(uri, ast_uri_http_legacy);
|
||||||
|
}
|
||||||
res = urih->callback(ser, urih, uri, method, get_vars, headers);
|
res = urih->callback(ser, urih, uri, method, get_vars, headers);
|
||||||
} else {
|
} else {
|
||||||
ast_http_error(ser, 404, "Not Found", "The requested URL was not found on this server.");
|
ast_http_error(ser, 404, "Not Found", "The requested URL was not found on this server.");
|
||||||
|
592
main/media_index.c
Normal file
592
main/media_index.c
Normal file
@@ -0,0 +1,592 @@
|
|||||||
|
/*
|
||||||
|
* Asterisk -- An open source telephony toolkit.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2013, Digium, Inc.
|
||||||
|
*
|
||||||
|
* Kinsey Moore <markster@digium.com>
|
||||||
|
*
|
||||||
|
* See http://www.asterisk.org for more information about
|
||||||
|
* the Asterisk project. Please do not directly contact
|
||||||
|
* any of the maintainers of this project for assistance;
|
||||||
|
* the project provides a web site, mailing lists and IRC
|
||||||
|
* channels for your use.
|
||||||
|
*
|
||||||
|
* This program is free software, distributed under the terms of
|
||||||
|
* the GNU General Public License Version 2. See the LICENSE file
|
||||||
|
* at the top of the source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*! \file
|
||||||
|
* \brief Sound file format and description indexer.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "asterisk.h"
|
||||||
|
|
||||||
|
#include <dirent.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
#include "asterisk/utils.h"
|
||||||
|
#include "asterisk/lock.h"
|
||||||
|
#include "asterisk/format.h"
|
||||||
|
#include "asterisk/format_cap.h"
|
||||||
|
#include "asterisk/media_index.h"
|
||||||
|
#include "asterisk/file.h"
|
||||||
|
|
||||||
|
/*** MODULEINFO
|
||||||
|
<support_level>core</support_level>
|
||||||
|
***/
|
||||||
|
|
||||||
|
/*! \brief The number of buckets to be used for storing variant-keyed objects */
|
||||||
|
#define VARIANT_BUCKETS 7
|
||||||
|
|
||||||
|
/*! \brief The number of buckets to be used for storing media filename-keyed objects */
|
||||||
|
#define INDEX_BUCKETS 157
|
||||||
|
|
||||||
|
/*! \brief Structure to hold a list of the format variations for a media file for a specific variant */
|
||||||
|
struct media_variant {
|
||||||
|
AST_DECLARE_STRING_FIELDS(
|
||||||
|
AST_STRING_FIELD(variant); /*!< The variant this media is available in */
|
||||||
|
AST_STRING_FIELD(description); /*!< The description of the media */
|
||||||
|
);
|
||||||
|
struct ast_format_cap *formats; /*!< The formats this media is available in for this variant */
|
||||||
|
};
|
||||||
|
|
||||||
|
static void media_variant_destroy(void *obj)
|
||||||
|
{
|
||||||
|
struct media_variant *variant = obj;
|
||||||
|
|
||||||
|
ast_string_field_free_memory(variant);
|
||||||
|
variant->formats = ast_format_cap_destroy(variant->formats);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct media_variant *media_variant_alloc(const char *variant_str)
|
||||||
|
{
|
||||||
|
RAII_VAR(struct media_variant *, variant, ao2_alloc(sizeof(*variant), media_variant_destroy), ao2_cleanup);
|
||||||
|
|
||||||
|
if (!variant || ast_string_field_init(variant, 8)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
variant->formats = ast_format_cap_alloc();
|
||||||
|
if (!variant->formats) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
ast_string_field_set(variant, variant, variant_str);
|
||||||
|
|
||||||
|
ao2_ref(variant, 1);
|
||||||
|
return variant;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int media_variant_hash(const void *obj, const int flags)
|
||||||
|
{
|
||||||
|
const char *variant = (flags & OBJ_KEY) ? obj : ((struct media_variant*) obj)->variant;
|
||||||
|
return ast_str_case_hash(variant);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int media_variant_cmp(void *obj, void *arg, int flags)
|
||||||
|
{
|
||||||
|
struct media_variant *opt1 = obj, *opt2 = arg;
|
||||||
|
const char *variant = (flags & OBJ_KEY) ? arg : opt2->variant;
|
||||||
|
return strcasecmp(opt1->variant, variant) ? 0 : CMP_MATCH | CMP_STOP;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*! \brief Structure to hold information about a media file */
|
||||||
|
struct media_info {
|
||||||
|
AST_DECLARE_STRING_FIELDS(
|
||||||
|
AST_STRING_FIELD(name); /*!< The file name of the media */
|
||||||
|
);
|
||||||
|
struct ao2_container *variants; /*!< The variants for which this media is available */
|
||||||
|
};
|
||||||
|
|
||||||
|
static void media_info_destroy(void *obj)
|
||||||
|
{
|
||||||
|
struct media_info *info = obj;
|
||||||
|
|
||||||
|
ast_string_field_free_memory(info);
|
||||||
|
ao2_cleanup(info->variants);
|
||||||
|
info->variants = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct media_info *media_info_alloc(const char *name)
|
||||||
|
{
|
||||||
|
RAII_VAR(struct media_info *, info, ao2_alloc(sizeof(*info), media_info_destroy), ao2_cleanup);
|
||||||
|
|
||||||
|
if (!info || ast_string_field_init(info, 128)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
info->variants = ao2_container_alloc(VARIANT_BUCKETS, media_variant_hash, media_variant_cmp);
|
||||||
|
if (!info->variants) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
ast_string_field_set(info, name, name);
|
||||||
|
|
||||||
|
ao2_ref(info, 1);
|
||||||
|
return info;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int media_info_hash(const void *obj, const int flags)
|
||||||
|
{
|
||||||
|
const char *name = (flags & OBJ_KEY) ? obj : ((struct media_info*) obj)->name;
|
||||||
|
return ast_str_case_hash(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int media_info_cmp(void *obj, void *arg, int flags)
|
||||||
|
{
|
||||||
|
struct media_info *opt1 = obj, *opt2 = arg;
|
||||||
|
const char *name = (flags & OBJ_KEY) ? arg : opt2->name;
|
||||||
|
return strcasecmp(opt1->name, name) ? 0 : CMP_MATCH | CMP_STOP;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ast_media_index {
|
||||||
|
AST_DECLARE_STRING_FIELDS(
|
||||||
|
AST_STRING_FIELD(base_dir); /*!< Base directory for indexing */
|
||||||
|
);
|
||||||
|
struct ao2_container *index; /*!< The index of media that has requested */
|
||||||
|
struct ao2_container *media_list_cache; /*!< Cache of filenames to prevent them from being regenerated so often */
|
||||||
|
};
|
||||||
|
|
||||||
|
static void media_index_dtor(void *obj)
|
||||||
|
{
|
||||||
|
struct ast_media_index *index = obj;
|
||||||
|
ao2_cleanup(index->index);
|
||||||
|
index->index = NULL;
|
||||||
|
ao2_cleanup(index->media_list_cache);
|
||||||
|
index->media_list_cache = NULL;
|
||||||
|
ast_string_field_free_memory(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ast_media_index *ast_media_index_create(const char *base_dir)
|
||||||
|
{
|
||||||
|
RAII_VAR(struct ast_media_index *, index, ao2_alloc(sizeof(*index), media_index_dtor), ao2_cleanup);
|
||||||
|
if (!index || ast_string_field_init(index, 64)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
ast_string_field_set(index, base_dir, base_dir);
|
||||||
|
|
||||||
|
index->index = ao2_container_alloc(INDEX_BUCKETS, media_info_hash, media_info_cmp);
|
||||||
|
if (!index->index) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
ao2_ref(index, +1);
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct media_variant *find_variant(struct ast_media_index *index, const char *filename, const char *variant)
|
||||||
|
{
|
||||||
|
RAII_VAR(struct media_info *, info, NULL, ao2_cleanup);
|
||||||
|
|
||||||
|
info = ao2_find(index->index, filename, OBJ_KEY);
|
||||||
|
if (!info) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ao2_find(info->variants, variant, OBJ_KEY);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*! \brief create the appropriate media_variant and any necessary structures */
|
||||||
|
static struct media_variant *alloc_variant(struct ast_media_index *index, const char *filename, const char *variant_str)
|
||||||
|
{
|
||||||
|
RAII_VAR(struct media_info *, info, NULL, ao2_cleanup);
|
||||||
|
RAII_VAR(struct media_variant *, variant, NULL, ao2_cleanup);
|
||||||
|
|
||||||
|
info = ao2_find(index->index, filename, OBJ_KEY);
|
||||||
|
if (!info) {
|
||||||
|
/* This is the first time the index has seen this filename,
|
||||||
|
* allocate and link */
|
||||||
|
info = media_info_alloc(filename);
|
||||||
|
if (!info) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
ao2_link(index->index, info);
|
||||||
|
}
|
||||||
|
|
||||||
|
variant = ao2_find(info->variants, variant_str, OBJ_KEY);
|
||||||
|
if (!variant) {
|
||||||
|
/* This is the first time the index has seen this variant for
|
||||||
|
* this filename, allocate and link */
|
||||||
|
variant = media_variant_alloc(variant_str);
|
||||||
|
if (!variant) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
ao2_link(info->variants, variant);
|
||||||
|
}
|
||||||
|
|
||||||
|
ao2_ref(variant, +1);
|
||||||
|
return variant;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *ast_media_get_description(struct ast_media_index *index, const char *filename, const char *variant_str)
|
||||||
|
{
|
||||||
|
RAII_VAR(struct media_variant *, variant, NULL, ao2_cleanup);
|
||||||
|
if (ast_strlen_zero(filename) || ast_strlen_zero(variant_str)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
variant = find_variant(index, filename, variant_str);
|
||||||
|
if (!variant) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return variant->description;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ast_format_cap *ast_media_get_format_cap(struct ast_media_index *index, const char *filename, const char *variant_str)
|
||||||
|
{
|
||||||
|
RAII_VAR(struct media_variant *, variant, NULL, ao2_cleanup);
|
||||||
|
if (ast_strlen_zero(filename) || ast_strlen_zero(variant_str)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
variant = find_variant(index, filename, variant_str);
|
||||||
|
if (!variant) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ast_format_cap_dup(variant->formats);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*! \brief Add the variant to the list of variants requested */
|
||||||
|
static int add_variant_cb(void *obj, void *arg, int flags)
|
||||||
|
{
|
||||||
|
struct media_variant *variant = obj;
|
||||||
|
struct ao2_container *variants= arg;
|
||||||
|
ast_str_container_add(variants, variant->variant);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ao2_container *ast_media_get_variants(struct ast_media_index *index, const char *filename)
|
||||||
|
{
|
||||||
|
RAII_VAR(struct media_info *, info, NULL, ao2_cleanup);
|
||||||
|
RAII_VAR(struct ao2_container *, variants, NULL, ao2_cleanup);
|
||||||
|
if (!filename) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
variants = ast_str_container_alloc(VARIANT_BUCKETS);
|
||||||
|
if (!variants) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
info = ao2_find(index->index, filename, OBJ_KEY);
|
||||||
|
if (!info) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
ao2_callback(info->variants, OBJ_NODATA, add_variant_cb, variants);
|
||||||
|
|
||||||
|
ao2_ref(variants, +1);
|
||||||
|
return variants;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*! \brief Add the media_info's filename to the container of filenames requested */
|
||||||
|
static int add_media_cb(void *obj, void *arg, int flags)
|
||||||
|
{
|
||||||
|
struct media_info *info = obj;
|
||||||
|
struct ao2_container *media = arg;
|
||||||
|
ast_str_container_add(media, info->name);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ao2_container *ast_media_get_media(struct ast_media_index *index)
|
||||||
|
{
|
||||||
|
RAII_VAR(struct ao2_container *, media, NULL, ao2_cleanup);
|
||||||
|
|
||||||
|
if (!index->media_list_cache) {
|
||||||
|
media = ast_str_container_alloc(INDEX_BUCKETS);
|
||||||
|
if (!media) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
ao2_callback(index->index, OBJ_NODATA, add_media_cb, media);
|
||||||
|
|
||||||
|
/* Ref to the cache */
|
||||||
|
ao2_ref(media, +1);
|
||||||
|
index->media_list_cache = media;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Ref to the caller */
|
||||||
|
ao2_ref(index->media_list_cache, +1);
|
||||||
|
return index->media_list_cache;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*! \brief Update an index with new format/variant information */
|
||||||
|
static int update_file_format_info(struct ast_media_index *index, const char *filename, const char *variant_str, const struct ast_format *file_format)
|
||||||
|
{
|
||||||
|
RAII_VAR(struct media_variant *, variant, find_variant(index, filename, variant_str), ao2_cleanup);
|
||||||
|
if (!variant) {
|
||||||
|
variant = alloc_variant(index, filename, variant_str);
|
||||||
|
if (!variant) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ast_format_cap_add(variant->formats, file_format);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*! \brief Process a media file into the index */
|
||||||
|
static int process_media_file(struct ast_media_index *index, const char *variant, const char *subdir, const char *filename_stripped, const char *ext)
|
||||||
|
{
|
||||||
|
const struct ast_format *file_format;
|
||||||
|
const char *file_identifier = filename_stripped;
|
||||||
|
RAII_VAR(struct ast_str *, file_id_str, NULL, ast_free);
|
||||||
|
|
||||||
|
file_format = ast_get_format_for_file_ext(ext);
|
||||||
|
if (!file_format) {
|
||||||
|
/* extension not registered */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* handle updating the file information */
|
||||||
|
if (subdir) {
|
||||||
|
file_id_str = ast_str_create(64);
|
||||||
|
if (!file_id_str) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ast_str_set(&file_id_str, 0, "%s/%s", subdir, filename_stripped);
|
||||||
|
file_identifier = ast_str_buffer(file_id_str);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (update_file_format_info(index, file_identifier, variant, file_format)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Process a media description text file
|
||||||
|
*
|
||||||
|
* This currently processes core-sounds-*.txt and extra-sounds-*.txt, but will
|
||||||
|
* process others if present.
|
||||||
|
*/
|
||||||
|
static int process_description_file(struct ast_media_index *index,
|
||||||
|
const char *subdir,
|
||||||
|
const char *variant_str,
|
||||||
|
const char *filename)
|
||||||
|
{
|
||||||
|
RAII_VAR(struct ast_str *, description_file_path, ast_str_create(64), ast_free);
|
||||||
|
RAII_VAR(struct ast_str *, cumulative_description, ast_str_create(64), ast_free);
|
||||||
|
char *file_id_persist = NULL;
|
||||||
|
int res = 0;
|
||||||
|
FILE *f = NULL;
|
||||||
|
#if defined(LOW_MEMORY)
|
||||||
|
char buf[256];
|
||||||
|
#else
|
||||||
|
char buf[2048];
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (!description_file_path || !cumulative_description) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ast_strlen_zero(subdir)) {
|
||||||
|
ast_str_set(&description_file_path, 0, "%s/%s/%s", index->base_dir, variant_str, filename);
|
||||||
|
} else {
|
||||||
|
ast_str_set(&description_file_path, 0, "%s/%s/%s/%s", index->base_dir, variant_str, subdir, filename);
|
||||||
|
}
|
||||||
|
f = fopen(ast_str_buffer(description_file_path), "r");
|
||||||
|
if (!f) {
|
||||||
|
ast_log(LOG_WARNING, "Could not open media description file '%s'\n", ast_str_buffer(description_file_path));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!feof(f)) {
|
||||||
|
char *file_identifier, *description;
|
||||||
|
if (!fgets(buf, sizeof(buf), f)) {
|
||||||
|
if (ferror(f)) {
|
||||||
|
ast_log(LOG_ERROR, "Error reading from file %s\n", ast_str_buffer(description_file_path));
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Skip lines that are too long */
|
||||||
|
if (strlen(buf) == sizeof(buf) - 1 && buf[sizeof(buf) - 1] != '\n') {
|
||||||
|
ast_log(LOG_WARNING, "Line too long, skipping. It begins with: %.32s...\n", buf);
|
||||||
|
while (fgets(buf, sizeof(buf), f)) {
|
||||||
|
if (strlen(buf) != sizeof(buf) - 1 || buf[sizeof(buf) - 1] == '\n') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (ferror(f)) {
|
||||||
|
ast_log(LOG_ERROR, "Error reading from file %s\n", ast_str_buffer(description_file_path));
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (buf[0] == ';') {
|
||||||
|
/* ignore comments */
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
ast_trim_blanks(buf);
|
||||||
|
description = buf;
|
||||||
|
file_identifier = strsep(&description, ":");
|
||||||
|
if (!description) {
|
||||||
|
/* no ':' means this is a continuation */
|
||||||
|
if (file_id_persist) {
|
||||||
|
ast_str_append(&cumulative_description, 0, "\n%s", file_identifier);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
/* if there's text in cumulative_description, archive it and start anew */
|
||||||
|
if (file_id_persist && !ast_strlen_zero(ast_str_buffer(cumulative_description))) {
|
||||||
|
RAII_VAR(struct media_variant *, variant, NULL, ao2_cleanup);
|
||||||
|
variant = find_variant(index, file_id_persist, variant_str);
|
||||||
|
if (!variant) {
|
||||||
|
variant = alloc_variant(index, file_id_persist, variant_str);
|
||||||
|
if (!variant) {
|
||||||
|
res = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ast_string_field_set(variant, description, ast_str_buffer(cumulative_description));
|
||||||
|
|
||||||
|
ast_str_reset(cumulative_description);
|
||||||
|
ast_free(file_id_persist);
|
||||||
|
file_id_persist = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
file_id_persist = strdup(file_identifier);
|
||||||
|
description = ast_skip_blanks(description);
|
||||||
|
ast_str_set(&cumulative_description, 0, "%s", description);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* handle the last one */
|
||||||
|
if (file_id_persist && !ast_strlen_zero(ast_str_buffer(cumulative_description))) {
|
||||||
|
RAII_VAR(struct media_variant *, variant, NULL, ao2_cleanup);
|
||||||
|
variant = find_variant(index, file_id_persist, variant_str);
|
||||||
|
if (!variant) {
|
||||||
|
variant = alloc_variant(index, file_id_persist, variant_str);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (variant) {
|
||||||
|
ast_string_field_set(variant, description, ast_str_buffer(cumulative_description));
|
||||||
|
ast_free(file_id_persist);
|
||||||
|
} else {
|
||||||
|
res = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(f);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*! \brief process an individual file listing */
|
||||||
|
static int process_file(struct ast_media_index *index, const char *variant_str, const char *subdir, const char *filename)
|
||||||
|
{
|
||||||
|
RAII_VAR(char *, filename_stripped, ast_strdup(filename), ast_free);
|
||||||
|
char *ext;
|
||||||
|
|
||||||
|
if (!filename_stripped) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ext = strrchr(filename_stripped, '.');
|
||||||
|
if (!ext) {
|
||||||
|
/* file has no extension */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
*ext++ = '\0';
|
||||||
|
if (!strcmp(ext, "txt")) {
|
||||||
|
if (process_description_file(index, subdir, variant_str, filename)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (process_media_file(index, variant_str, subdir, filename_stripped, ext)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*! \brief internal function for updating the index, recursive */
|
||||||
|
static int media_index_update(struct ast_media_index *index,
|
||||||
|
const char *variant,
|
||||||
|
const char *subdir)
|
||||||
|
{
|
||||||
|
struct dirent* dent;
|
||||||
|
DIR* srcdir;
|
||||||
|
RAII_VAR(struct ast_str *, index_dir, ast_str_create(64), ast_free);
|
||||||
|
RAII_VAR(struct ast_str *, statfile, ast_str_create(64), ast_free);
|
||||||
|
int res = 0;
|
||||||
|
|
||||||
|
if (!index_dir) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
ast_str_set(&index_dir, 0, "%s", index->base_dir);
|
||||||
|
if (!ast_strlen_zero(variant)) {
|
||||||
|
ast_str_append(&index_dir, 0, "/%s", variant);
|
||||||
|
}
|
||||||
|
if (!ast_strlen_zero(subdir)) {
|
||||||
|
ast_str_append(&index_dir, 0, "/%s", subdir);
|
||||||
|
}
|
||||||
|
|
||||||
|
srcdir = opendir(ast_str_buffer(index_dir));
|
||||||
|
if (srcdir == NULL) {
|
||||||
|
ast_log(LOG_ERROR, "Failed to open %s\n", ast_str_buffer(index_dir));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while((dent = readdir(srcdir)) != NULL) {
|
||||||
|
struct stat st;
|
||||||
|
|
||||||
|
if(!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
ast_str_reset(statfile);
|
||||||
|
ast_str_set(&statfile, 0, "%s/%s", ast_str_buffer(index_dir), dent->d_name);
|
||||||
|
|
||||||
|
if (stat(ast_str_buffer(statfile), &st) < 0) {
|
||||||
|
ast_log(LOG_ERROR, "Failed to stat %s\n", ast_str_buffer(statfile));
|
||||||
|
res = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (S_ISDIR(st.st_mode)) {
|
||||||
|
if (ast_strlen_zero(subdir)) {
|
||||||
|
res = media_index_update(index, variant, dent->d_name);
|
||||||
|
} else {
|
||||||
|
RAII_VAR(struct ast_str *, new_subdir, ast_str_create(64), ast_free);
|
||||||
|
ast_str_set(&new_subdir, 0, "%s/%s", subdir, dent->d_name);
|
||||||
|
res = media_index_update(index, variant, ast_str_buffer(new_subdir));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (res) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!S_ISREG(st.st_mode)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (process_file(index, variant, subdir, dent->d_name)) {
|
||||||
|
res = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
closedir(srcdir);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ast_media_index_update(struct ast_media_index *index,
|
||||||
|
const char *variant)
|
||||||
|
{
|
||||||
|
return media_index_update(index, variant, NULL);
|
||||||
|
}
|
||||||
|
|
334
main/sounds_index.c
Normal file
334
main/sounds_index.c
Normal file
@@ -0,0 +1,334 @@
|
|||||||
|
/*
|
||||||
|
* Asterisk -- An open source telephony toolkit.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2013, Digium, Inc.
|
||||||
|
*
|
||||||
|
* Kinsey Moore <markster@digium.com>
|
||||||
|
*
|
||||||
|
* See http://www.asterisk.org for more information about
|
||||||
|
* the Asterisk project. Please do not directly contact
|
||||||
|
* any of the maintainers of this project for assistance;
|
||||||
|
* the project provides a web site, mailing lists and IRC
|
||||||
|
* channels for your use.
|
||||||
|
*
|
||||||
|
* This program is free software, distributed under the terms of
|
||||||
|
* the GNU General Public License Version 2. See the LICENSE file
|
||||||
|
* at the top of the source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*! \file
|
||||||
|
* \brief Sound file format and description index.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "asterisk.h"
|
||||||
|
|
||||||
|
#include <dirent.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
#include "asterisk/utils.h"
|
||||||
|
#include "asterisk/lock.h"
|
||||||
|
#include "asterisk/format.h"
|
||||||
|
#include "asterisk/format_cap.h"
|
||||||
|
#include "asterisk/paths.h" /* use ast_config_AST_DATA_DIR */
|
||||||
|
#include "asterisk/media_index.h"
|
||||||
|
#include "asterisk/sounds_index.h"
|
||||||
|
#include "asterisk/file.h"
|
||||||
|
#include "asterisk/cli.h"
|
||||||
|
#include "asterisk/_private.h"
|
||||||
|
#include "asterisk/stasis_message_router.h"
|
||||||
|
|
||||||
|
/*** MODULEINFO
|
||||||
|
<support_level>core</support_level>
|
||||||
|
***/
|
||||||
|
|
||||||
|
/*! \brief The number of buckets to be used for storing language-keyed objects */
|
||||||
|
#define LANGUAGE_BUCKETS 7
|
||||||
|
|
||||||
|
static struct ast_media_index *sounds_index;
|
||||||
|
|
||||||
|
static struct stasis_message_router *sounds_system_router;
|
||||||
|
|
||||||
|
/*! \brief Get the languages in which sound files are available */
|
||||||
|
static struct ao2_container *get_languages(void)
|
||||||
|
{
|
||||||
|
RAII_VAR(struct ao2_container *, lang_dirs, NULL, ao2_cleanup);
|
||||||
|
struct dirent* dent;
|
||||||
|
DIR* srcdir;
|
||||||
|
RAII_VAR(struct ast_str *, media_dir, ast_str_create(64), ast_free);
|
||||||
|
RAII_VAR(struct ast_str *, variant_dir, ast_str_create(64), ast_free);
|
||||||
|
|
||||||
|
lang_dirs = ast_str_container_alloc(LANGUAGE_BUCKETS);
|
||||||
|
if (!media_dir || !lang_dirs) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
ast_str_set(&media_dir, 0, "%s/sounds", ast_config_AST_DATA_DIR);
|
||||||
|
|
||||||
|
srcdir = opendir(ast_str_buffer(media_dir));
|
||||||
|
|
||||||
|
if (srcdir == NULL) {
|
||||||
|
ast_log(LOG_ERROR, "Failed to open %s\n", ast_str_buffer(media_dir));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
while((dent = readdir(srcdir)) != NULL) {
|
||||||
|
struct stat st;
|
||||||
|
|
||||||
|
if(!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
ast_str_reset(variant_dir);
|
||||||
|
ast_str_set(&variant_dir, 0, "%s/%s", ast_str_buffer(media_dir), dent->d_name);
|
||||||
|
|
||||||
|
if (stat(ast_str_buffer(variant_dir), &st) < 0) {
|
||||||
|
ast_log(LOG_ERROR, "Failed to stat %s\n", ast_str_buffer(variant_dir));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (S_ISDIR(st.st_mode)) {
|
||||||
|
ast_str_container_add(lang_dirs, dent->d_name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
closedir(srcdir);
|
||||||
|
ao2_ref(lang_dirs, +1);
|
||||||
|
return lang_dirs;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*! \brief Callback to process an individual language directory or subdirectory */
|
||||||
|
static int update_index_cb(void *obj, void *arg, int flags)
|
||||||
|
{
|
||||||
|
char *lang = obj;
|
||||||
|
struct ast_media_index *index = arg;
|
||||||
|
|
||||||
|
if (ast_media_index_update(index, lang)) {
|
||||||
|
return CMP_MATCH;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
AST_MUTEX_DEFINE_STATIC(reload_lock);
|
||||||
|
|
||||||
|
int ast_sounds_reindex(void)
|
||||||
|
{
|
||||||
|
RAII_VAR(struct ast_str *, sounds_dir, NULL, ast_free);
|
||||||
|
RAII_VAR(struct ao2_container *, languages, NULL, ao2_cleanup);
|
||||||
|
RAII_VAR(char *, failed_index, NULL, ao2_cleanup);
|
||||||
|
RAII_VAR(struct ast_media_index *, new_index, NULL, ao2_cleanup);
|
||||||
|
struct ast_media_index *old_index;
|
||||||
|
|
||||||
|
SCOPED_MUTEX(lock, &reload_lock);
|
||||||
|
|
||||||
|
old_index = sounds_index;
|
||||||
|
languages = get_languages();
|
||||||
|
sounds_dir = ast_str_create(64);
|
||||||
|
|
||||||
|
if (!languages || !sounds_dir) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ast_str_set(&sounds_dir, 0, "%s/sounds", ast_config_AST_DATA_DIR);
|
||||||
|
new_index = ast_media_index_create(ast_str_buffer(sounds_dir));
|
||||||
|
if (!new_index) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
failed_index = ao2_callback(languages, 0, update_index_cb, new_index);
|
||||||
|
if (failed_index) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ao2_ref(new_index, +1);
|
||||||
|
sounds_index = new_index;
|
||||||
|
ao2_cleanup(old_index);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int show_sounds_cb(void *obj, void *arg, int flags)
|
||||||
|
{
|
||||||
|
char *name = obj;
|
||||||
|
struct ast_cli_args *a = arg;
|
||||||
|
ast_cli(a->fd, "%s\n", name);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int show_sound_info_cb(void *obj, void *arg, int flags)
|
||||||
|
{
|
||||||
|
char *language = obj;
|
||||||
|
struct ast_cli_args *a = arg;
|
||||||
|
struct ast_format format;
|
||||||
|
int formats_shown = 0;
|
||||||
|
RAII_VAR(struct ast_media_index *, local_index, ast_sounds_get_index(), ao2_cleanup);
|
||||||
|
RAII_VAR(struct ast_format_cap *, cap, NULL, ast_format_cap_destroy);
|
||||||
|
const char *description = ast_media_get_description(local_index, a->argv[2], language);
|
||||||
|
|
||||||
|
ast_cli(a->fd, " Language %s:\n", language);
|
||||||
|
if (!ast_strlen_zero(description)) {
|
||||||
|
ast_cli(a->fd, " Description: %s\n", description);
|
||||||
|
}
|
||||||
|
|
||||||
|
cap = ast_media_get_format_cap(local_index, a->argv[2], language);
|
||||||
|
ast_format_cap_iter_start(cap);
|
||||||
|
while (!ast_format_cap_iter_next(cap, &format)) {
|
||||||
|
ast_cli(a->fd, " Format: %s\n", ast_getformatname(&format));
|
||||||
|
formats_shown = 1;
|
||||||
|
}
|
||||||
|
ast_format_cap_iter_end(cap);
|
||||||
|
|
||||||
|
if (!formats_shown) {
|
||||||
|
ast_cli(a->fd, " No Formats Available\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*! \brief Allow for reloading of sounds via the command line */
|
||||||
|
static char *handle_cli_sounds_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
|
||||||
|
{
|
||||||
|
switch (cmd) {
|
||||||
|
case CLI_INIT:
|
||||||
|
e->command = "sounds reload";
|
||||||
|
e->usage =
|
||||||
|
"Usage: sounds reload\n"
|
||||||
|
" Reloads the index of sound files and their descriptions.\n";
|
||||||
|
return NULL;
|
||||||
|
case CLI_GENERATE:
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (a->argc != 2) {
|
||||||
|
return CLI_SHOWUSAGE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ast_sounds_reindex()) {
|
||||||
|
ast_cli(a->fd, "Sound re-indexing failed.\n");
|
||||||
|
return CLI_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
ast_cli(a->fd, "Sound files re-indexed.\n");
|
||||||
|
return CLI_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*! \brief Allow for reloading of sounds via the command line */
|
||||||
|
static char *handle_cli_sounds_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
|
||||||
|
{
|
||||||
|
switch (cmd) {
|
||||||
|
case CLI_INIT:
|
||||||
|
e->command = "sounds show";
|
||||||
|
e->usage =
|
||||||
|
"Usage: sounds show [soundid]\n"
|
||||||
|
" Shows a listing of sound files or information about the specified sound.\n";
|
||||||
|
return NULL;
|
||||||
|
case CLI_GENERATE:
|
||||||
|
{
|
||||||
|
int length = strlen(a->word);
|
||||||
|
int which = 0;
|
||||||
|
struct ao2_iterator it_sounds;
|
||||||
|
char *match = NULL;
|
||||||
|
char *filename;
|
||||||
|
RAII_VAR(struct ao2_container *, sound_files, ast_media_get_media(sounds_index), ao2_cleanup);
|
||||||
|
if (!sound_files) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
it_sounds = ao2_iterator_init(sound_files, 0);
|
||||||
|
while ((filename = ao2_iterator_next(&it_sounds))) {
|
||||||
|
if (!strncasecmp(a->word, filename, length) && ++which > a->n) {
|
||||||
|
match = ast_strdup(filename);
|
||||||
|
ao2_ref(filename, -1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ao2_ref(filename, -1);
|
||||||
|
}
|
||||||
|
ao2_iterator_destroy(&it_sounds);
|
||||||
|
return match;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (a->argc == 2) {
|
||||||
|
RAII_VAR(struct ao2_container *, sound_files, ast_media_get_media(sounds_index), ao2_cleanup);
|
||||||
|
if (!sound_files) {
|
||||||
|
return CLI_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
ast_cli(a->fd, "Available audio files:\n");
|
||||||
|
ao2_callback(sound_files, OBJ_MULTIPLE | OBJ_NODATA, show_sounds_cb, a);
|
||||||
|
return CLI_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (a->argc == 3) {
|
||||||
|
RAII_VAR(struct ao2_container *, variants, ast_media_get_variants(sounds_index, a->argv[2]), ao2_cleanup);
|
||||||
|
if (!variants || !ao2_container_count(variants)) {
|
||||||
|
ast_cli(a->fd, "ERROR: File %s not found in index\n", a->argv[2]);
|
||||||
|
return CLI_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
ast_cli(a->fd, "Indexed Information for %s:\n", a->argv[2]);
|
||||||
|
ao2_callback(variants, OBJ_MULTIPLE | OBJ_NODATA, show_sound_info_cb, a);
|
||||||
|
return CLI_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
return CLI_SHOWUSAGE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*! \brief Struct for registering CLI commands */
|
||||||
|
static struct ast_cli_entry cli_sounds[] = {
|
||||||
|
AST_CLI_DEFINE(handle_cli_sounds_show, "Shows available sounds"),
|
||||||
|
AST_CLI_DEFINE(handle_cli_sounds_reload, "Reload sounds index"),
|
||||||
|
};
|
||||||
|
|
||||||
|
static void sounds_cleanup(void)
|
||||||
|
{
|
||||||
|
stasis_message_router_unsubscribe_and_join(sounds_system_router);
|
||||||
|
sounds_system_router = NULL;
|
||||||
|
ast_cli_unregister_multiple(cli_sounds, ARRAY_LEN(cli_sounds));
|
||||||
|
ao2_cleanup(sounds_index);
|
||||||
|
sounds_index = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void format_update_cb(void *data, struct stasis_subscription *sub,
|
||||||
|
struct stasis_topic *topic, struct stasis_message *message)
|
||||||
|
{
|
||||||
|
ast_sounds_reindex();
|
||||||
|
}
|
||||||
|
|
||||||
|
int ast_sounds_index_init(void)
|
||||||
|
{
|
||||||
|
int res = 0;
|
||||||
|
sounds_index = NULL;
|
||||||
|
if (ast_sounds_reindex()) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
res |= ast_cli_register_multiple(cli_sounds, ARRAY_LEN(cli_sounds));
|
||||||
|
|
||||||
|
sounds_system_router = stasis_message_router_create(ast_system_topic());
|
||||||
|
if (!sounds_system_router) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
res |= stasis_message_router_add(
|
||||||
|
sounds_system_router,
|
||||||
|
ast_format_register_type(),
|
||||||
|
format_update_cb,
|
||||||
|
NULL);
|
||||||
|
|
||||||
|
res |= stasis_message_router_add(
|
||||||
|
sounds_system_router,
|
||||||
|
ast_format_unregister_type(),
|
||||||
|
format_update_cb,
|
||||||
|
NULL);
|
||||||
|
|
||||||
|
if (res) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ast_register_atexit(sounds_cleanup);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ast_media_index *ast_sounds_get_index(void)
|
||||||
|
{
|
||||||
|
ao2_ref(sounds_index, +1);
|
||||||
|
return sounds_index;
|
||||||
|
}
|
@@ -506,7 +506,7 @@ void stasis_http_invoke(const char *uri,
|
|||||||
struct stasis_rest_handlers *handler;
|
struct stasis_rest_handlers *handler;
|
||||||
struct ast_variable *path_vars = NULL;
|
struct ast_variable *path_vars = NULL;
|
||||||
char *path = ast_strdupa(uri);
|
char *path = ast_strdupa(uri);
|
||||||
const char *path_segment;
|
char *path_segment;
|
||||||
stasis_rest_callback callback;
|
stasis_rest_callback callback;
|
||||||
|
|
||||||
root = handler = get_root_handler();
|
root = handler = get_root_handler();
|
||||||
@@ -515,6 +515,7 @@ void stasis_http_invoke(const char *uri,
|
|||||||
while ((path_segment = strsep(&path, "/")) && (strlen(path_segment) > 0)) {
|
while ((path_segment = strsep(&path, "/")) && (strlen(path_segment) > 0)) {
|
||||||
struct stasis_rest_handlers *found_handler = NULL;
|
struct stasis_rest_handlers *found_handler = NULL;
|
||||||
int i;
|
int i;
|
||||||
|
ast_uri_decode(path_segment, ast_uri_http_legacy);
|
||||||
ast_debug(3, "Finding handler for %s\n", path_segment);
|
ast_debug(3, "Finding handler for %s\n", path_segment);
|
||||||
for (i = 0; found_handler == NULL && i < handler->num_children; ++i) {
|
for (i = 0; found_handler == NULL && i < handler->num_children; ++i) {
|
||||||
struct stasis_rest_handlers *child = handler->children[i];
|
struct stasis_rest_handlers *child = handler->children[i];
|
||||||
@@ -863,6 +864,7 @@ static struct ast_http_uri http_uri = {
|
|||||||
.has_subtree = 1,
|
.has_subtree = 1,
|
||||||
.data = NULL,
|
.data = NULL,
|
||||||
.key = __FILE__,
|
.key = __FILE__,
|
||||||
|
.no_decode_uri = 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
static int load_module(void)
|
static int load_module(void)
|
||||||
|
@@ -28,12 +28,193 @@
|
|||||||
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
||||||
|
|
||||||
#include "resource_sounds.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"
|
||||||
|
|
||||||
|
/*! \brief arguments that are necessary for adding format/lang pairs */
|
||||||
|
struct lang_format_info {
|
||||||
|
struct ast_json *format_list; /*!< The embedded array to which format/lang pairs should be added */
|
||||||
|
const char *filename; /*!< Name of the file for which to add format/lang pairs */
|
||||||
|
const char *format_filter; /*!< Format filter provided in the request */
|
||||||
|
};
|
||||||
|
|
||||||
|
/*! \brief Add format/lang pairs to the array embedded in the sound object */
|
||||||
|
static int add_format_information_cb(void *obj, void *arg, int flags)
|
||||||
|
{
|
||||||
|
char *language = obj;
|
||||||
|
struct lang_format_info *args = arg;
|
||||||
|
struct ast_format format;
|
||||||
|
RAII_VAR(struct ast_format_cap *, cap, NULL, ast_format_cap_destroy);
|
||||||
|
RAII_VAR(struct ast_media_index *, sounds_index, ast_sounds_get_index(), ao2_cleanup);
|
||||||
|
|
||||||
|
if (!sounds_index) {
|
||||||
|
return CMP_STOP;
|
||||||
|
}
|
||||||
|
|
||||||
|
cap = ast_media_get_format_cap(sounds_index, args->filename, language);
|
||||||
|
if (!cap) {
|
||||||
|
return CMP_STOP;
|
||||||
|
}
|
||||||
|
|
||||||
|
ast_format_cap_iter_start(cap);
|
||||||
|
while (!ast_format_cap_iter_next(cap, &format)) {
|
||||||
|
struct ast_json *lang_format_pair;
|
||||||
|
const char *format_name = ast_getformatname(&format);
|
||||||
|
|
||||||
|
if (!ast_strlen_zero(args->format_filter)
|
||||||
|
&& strcmp(args->format_filter, format_name)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
lang_format_pair = ast_json_pack("{s: s, s: s}",
|
||||||
|
"language", language,
|
||||||
|
"format", format_name);
|
||||||
|
if (!lang_format_pair) {
|
||||||
|
ast_format_cap_iter_end(cap);
|
||||||
|
return CMP_STOP;
|
||||||
|
}
|
||||||
|
|
||||||
|
ast_json_array_append(args->format_list, lang_format_pair);
|
||||||
|
}
|
||||||
|
ast_format_cap_iter_end(cap);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*! \brief Filter out all languages not matching the specified language */
|
||||||
|
static int filter_langs_cb(void *obj, void *arg, int flags)
|
||||||
|
{
|
||||||
|
char *lang_filter = arg;
|
||||||
|
char *lang = obj;
|
||||||
|
if (strcmp(lang, lang_filter)) {
|
||||||
|
return CMP_MATCH;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*! \brief Generate a Sound structure as documented in sounds.json for the specified filename */
|
||||||
|
static struct ast_json *create_sound_blob(const char *filename, struct ast_get_sounds_args *args)
|
||||||
|
{
|
||||||
|
RAII_VAR(struct ast_json *, sound, NULL, ast_json_unref);
|
||||||
|
RAII_VAR(struct ao2_container *, languages, NULL, ao2_cleanup);
|
||||||
|
const char *description;
|
||||||
|
struct ast_json *format_lang_list;
|
||||||
|
struct lang_format_info info;
|
||||||
|
RAII_VAR(struct ast_media_index *, sounds_index, ast_sounds_get_index(), ao2_cleanup);
|
||||||
|
|
||||||
|
if (!sounds_index) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
description = ast_media_get_description(sounds_index, filename, "en");
|
||||||
|
if (ast_strlen_zero(description)) {
|
||||||
|
sound = ast_json_pack("{s: s, s: []}",
|
||||||
|
"id", filename,
|
||||||
|
"formats");
|
||||||
|
} else {
|
||||||
|
sound = ast_json_pack("{s: s, s: s, s: []}",
|
||||||
|
"id", filename,
|
||||||
|
"text", description,
|
||||||
|
"formats");
|
||||||
|
}
|
||||||
|
if (!sound) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
format_lang_list = ast_json_object_get(sound, "formats");
|
||||||
|
if (!format_lang_list) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
languages = ast_media_get_variants(sounds_index, filename);
|
||||||
|
if (!languages || !ao2_container_count(languages)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* filter requested languages */
|
||||||
|
if (args && !ast_strlen_zero(args->lang)) {
|
||||||
|
char *lang_filter = ast_strdupa(args->lang);
|
||||||
|
ao2_callback(languages, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, filter_langs_cb, lang_filter);
|
||||||
|
if (!languages || !ao2_container_count(languages)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
info.filename = filename;
|
||||||
|
info.format_list = format_lang_list;
|
||||||
|
info.format_filter = NULL;
|
||||||
|
if (args) {
|
||||||
|
info.format_filter = args->format;
|
||||||
|
}
|
||||||
|
ao2_callback(languages, OBJ_NODATA, add_format_information_cb, &info);
|
||||||
|
|
||||||
|
/* no format/lang pairs for this sound so nothing to return */
|
||||||
|
if (!ast_json_array_size(format_lang_list)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ast_json_ref(sound);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*! \brief Generate a Sound structure and append it to the output blob */
|
||||||
|
static int append_sound_cb(void *obj, void *arg, void *data, int flags)
|
||||||
|
{
|
||||||
|
struct ast_json *sounds_array = arg;
|
||||||
|
char *filename = obj;
|
||||||
|
struct ast_get_sounds_args *args = data;
|
||||||
|
struct ast_json *sound_blob = create_sound_blob(filename, args);
|
||||||
|
if (!sound_blob) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
ast_json_array_append(sounds_array, sound_blob);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
void stasis_http_get_sounds(struct ast_variable *headers, struct ast_get_sounds_args *args, struct stasis_http_response *response)
|
void stasis_http_get_sounds(struct ast_variable *headers, struct ast_get_sounds_args *args, struct stasis_http_response *response)
|
||||||
{
|
{
|
||||||
ast_log(LOG_ERROR, "TODO: stasis_http_get_sounds\n");
|
RAII_VAR(struct ao2_container *, sound_files, NULL, ao2_cleanup);
|
||||||
|
struct ast_json *sounds_blob;
|
||||||
|
RAII_VAR(struct ast_media_index *, sounds_index, ast_sounds_get_index(), ao2_cleanup);
|
||||||
|
|
||||||
|
if (!sounds_index) {
|
||||||
|
stasis_http_response_error(response, 500, "Internal Error", "Sounds index not available");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
sound_files = ast_media_get_media(sounds_index);
|
||||||
|
if (!sound_files) {
|
||||||
|
stasis_http_response_error(response, 500, "Internal Error", "Allocation Error");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
sounds_blob = ast_json_array_create();
|
||||||
|
if (!sounds_blob) {
|
||||||
|
stasis_http_response_error(response, 500, "Internal Error", "Allocation Error");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ao2_callback_data(sound_files, OBJ_NODATA, append_sound_cb, sounds_blob, args);
|
||||||
|
|
||||||
|
if (!ast_json_array_size(sounds_blob)) {
|
||||||
|
stasis_http_response_error(response, 404, "Not Found", "No sounds found that matched the query");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
stasis_http_response_ok(response, sounds_blob);
|
||||||
}
|
}
|
||||||
|
|
||||||
void stasis_http_get_stored_sound(struct ast_variable *headers, struct ast_get_stored_sound_args *args, struct stasis_http_response *response)
|
void stasis_http_get_stored_sound(struct ast_variable *headers, struct ast_get_stored_sound_args *args, struct stasis_http_response *response)
|
||||||
{
|
{
|
||||||
ast_log(LOG_ERROR, "TODO: stasis_http_get_stored_sound\n");
|
struct ast_json *sound_blob;
|
||||||
|
|
||||||
|
sound_blob = create_sound_blob(args->sound_id, NULL);
|
||||||
|
if (!sound_blob) {
|
||||||
|
stasis_http_response_error(response, 404, "Not Found", "Sound not found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
stasis_http_response_ok(response, sound_blob);
|
||||||
}
|
}
|
||||||
|
@@ -58,6 +58,19 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"models": {
|
"models": {
|
||||||
|
"FormatLangPair": {
|
||||||
|
"id": "FormatLangPair",
|
||||||
|
"properties": {
|
||||||
|
"language": {
|
||||||
|
"required": true,
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"format": {
|
||||||
|
"required": true,
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"Sound": {
|
"Sound": {
|
||||||
"id": "Sound",
|
"id": "Sound",
|
||||||
"properties": {
|
"properties": {
|
||||||
@@ -71,13 +84,10 @@
|
|||||||
"description": "Text description of the sound, usually the words spoken.",
|
"description": "Text description of the sound, usually the words spoken.",
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
"lang": {
|
|
||||||
"required": true,
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"formats": {
|
"formats": {
|
||||||
"required": true,
|
"required": true,
|
||||||
"type": "List[string]"
|
"description": "The formats and languages in which this sound is available.",
|
||||||
|
"type": "List[FormatLangPair]"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user