mirror of
https://github.com/asterisk/asterisk.git
synced 2025-09-21 12:54:56 +00:00
(closes issue #10724) Reported by: eliel Patches: chan_skinny.c.patch uploaded by eliel (license 64) chan_oss.c.patch uploaded by eliel (license 64) chan_mgcp.c.patch2 uploaded by eliel (license 64) pbx_config.c.patch uploaded by seanbright (license 71) iax2-provision.c.patch uploaded by eliel (license 64) chan_gtalk.c.patch uploaded by eliel (license 64) pbx_ael.c.patch uploaded by seanbright (license 71) file.c.patch uploaded by seanbright (license 71) image.c.patch uploaded by seanbright (license 71) cli.c.patch uploaded by moy (license 222) astobj2.c.patch uploaded by moy (license 222) asterisk.c.patch uploaded by moy (license 222) res_limit.c.patch uploaded by seanbright (license 71) res_convert.c.patch uploaded by seanbright (license 71) res_crypto.c.patch uploaded by seanbright (license 71) app_osplookup.c.patch uploaded by seanbright (license 71) app_rpt.c.patch uploaded by seanbright (license 71) app_mixmonitor.c.patch uploaded by seanbright (license 71) channel.c.patch uploaded by seanbright (license 71) translate.c.patch uploaded by seanbright (license 71) udptl.c.patch uploaded by seanbright (license 71) threadstorage.c.patch uploaded by seanbright (license 71) db.c.patch uploaded by seanbright (license 71) cdr.c.patch uploaded by moy (license 222) pbd_dundi.c.patch uploaded by moy (license 222) app_osplookup-rev83558.patch uploaded by moy (license 222) res_clioriginate.c.patch uploaded by moy (license 222) git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@85460 65c4cc65-6c06-0410-ace0-fbb531ad65f3
166 lines
4.3 KiB
C
166 lines
4.3 KiB
C
/*
|
|
* Asterisk -- An open source telephony toolkit.
|
|
*
|
|
* Copyright (C) 2005, 2006, Digium, Inc.
|
|
*
|
|
* redice li <redice_li@yahoo.com>
|
|
* Russell Bryant <russell@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 file format conversion CLI command using Asterisk formats and translators
|
|
*
|
|
* \author redice li <redice_li@yahoo.com>
|
|
* \author Russell Bryant <russell@digium.com>
|
|
*
|
|
*/
|
|
|
|
#include "asterisk.h"
|
|
|
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "asterisk/channel.h"
|
|
#include "asterisk/logger.h"
|
|
#include "asterisk/module.h"
|
|
#include "asterisk/cli.h"
|
|
#include "asterisk/file.h"
|
|
|
|
/*! \brief Split the filename to basename and extension */
|
|
static int split_ext(char *filename, char **name, char **ext)
|
|
{
|
|
*name = *ext = filename;
|
|
|
|
if ((*ext = strrchr(filename, '.'))) {
|
|
**ext = '\0';
|
|
(*ext)++;
|
|
}
|
|
|
|
if (ast_strlen_zero(*name) || ast_strlen_zero(*ext))
|
|
return -1;
|
|
|
|
return 0;
|
|
}
|
|
|
|
/*!
|
|
* \brief Convert a file from one format to another
|
|
* \param fd file descriptor
|
|
* \param argc no arguements
|
|
* \param argv list of arguements
|
|
* \retval CLI_SUCCESS on success.
|
|
* \retval CLI_SHOWUSAGE or CLI_FAILURE on failure.
|
|
*/
|
|
static char *handle_cli_file_convert(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
|
|
{
|
|
char *ret = CLI_FAILURE;
|
|
struct ast_filestream *fs_in = NULL, *fs_out = NULL;
|
|
struct ast_frame *f;
|
|
struct timeval start;
|
|
int cost;
|
|
char *file_in = NULL, *file_out = NULL;
|
|
char *name_in, *ext_in, *name_out, *ext_out;
|
|
|
|
switch (cmd) {
|
|
case CLI_INIT:
|
|
e->command = "file convert";
|
|
e->usage =
|
|
"Usage: file convert <file_in> <file_out>\n"
|
|
" Convert from file_in to file_out. If an absolute path\n"
|
|
" is not given, the default Asterisk sounds directory\n"
|
|
" will be used.\n\n"
|
|
" Example:\n"
|
|
" file convert tt-weasels.gsm tt-weasels.ulaw\n";
|
|
return NULL;
|
|
case CLI_GENERATE:
|
|
return NULL;
|
|
}
|
|
|
|
/* ugly, can be removed when CLI entries have ast_module pointers */
|
|
ast_module_ref(ast_module_info->self);
|
|
|
|
if (a->argc != 4 || ast_strlen_zero(a->argv[2]) || ast_strlen_zero(a->argv[3])) {
|
|
ret = CLI_SHOWUSAGE;
|
|
goto fail_out;
|
|
}
|
|
|
|
file_in = ast_strdupa(a->argv[2]);
|
|
file_out = ast_strdupa(a->argv[3]);
|
|
|
|
if (split_ext(file_in, &name_in, &ext_in)) {
|
|
ast_cli(a->fd, "'%s' is an invalid filename!\n", a->argv[2]);
|
|
goto fail_out;
|
|
}
|
|
if (!(fs_in = ast_readfile(name_in, ext_in, NULL, O_RDONLY, 0, 0))) {
|
|
ast_cli(a->fd, "Unable to open input file: %s\n", a->argv[2]);
|
|
goto fail_out;
|
|
}
|
|
|
|
if (split_ext(file_out, &name_out, &ext_out)) {
|
|
ast_cli(a->fd, "'%s' is an invalid filename!\n", a->argv[3]);
|
|
goto fail_out;
|
|
}
|
|
if (!(fs_out = ast_writefile(name_out, ext_out, NULL, O_CREAT|O_TRUNC|O_WRONLY, 0, AST_FILE_MODE))) {
|
|
ast_cli(a->fd, "Unable to open output file: %s\n", a->argv[3]);
|
|
goto fail_out;
|
|
}
|
|
|
|
start = ast_tvnow();
|
|
|
|
while ((f = ast_readframe(fs_in))) {
|
|
if (ast_writestream(fs_out, f)) {
|
|
ast_cli(a->fd, "Failed to convert %s.%s to %s.%s!\n", name_in, ext_in, name_out, ext_out);
|
|
goto fail_out;
|
|
}
|
|
}
|
|
|
|
cost = ast_tvdiff_ms(ast_tvnow(), start);
|
|
ast_cli(a->fd, "Converted %s.%s to %s.%s in %dms\n", name_in, ext_in, name_out, ext_out, cost);
|
|
ret = CLI_SUCCESS;
|
|
|
|
fail_out:
|
|
if (fs_out) {
|
|
ast_closestream(fs_out);
|
|
if (ret != CLI_SUCCESS)
|
|
ast_filedelete(name_out, ext_out);
|
|
}
|
|
|
|
if (fs_in)
|
|
ast_closestream(fs_in);
|
|
|
|
ast_module_unref(ast_module_info->self);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static struct ast_cli_entry cli_convert[] = {
|
|
NEW_CLI(handle_cli_file_convert, "Convert audio file")
|
|
};
|
|
|
|
static int unload_module(void)
|
|
{
|
|
ast_cli_unregister_multiple(cli_convert, sizeof(cli_convert) / sizeof(struct ast_cli_entry));
|
|
return 0;
|
|
}
|
|
|
|
static int load_module(void)
|
|
{
|
|
ast_cli_register_multiple(cli_convert, sizeof(cli_convert) / sizeof(struct ast_cli_entry));
|
|
return 0;
|
|
}
|
|
|
|
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "File format conversion CLI command");
|