Wed Jan 8 2020 09:50:18

Asterisk developer's documentation


res_convert.c File Reference

file format conversion CLI command using Asterisk formats and translators More...

#include "asterisk.h"
#include "asterisk/channel.h"
#include "asterisk/module.h"
#include "asterisk/cli.h"
#include "asterisk/file.h"

Go to the source code of this file.

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static char * handle_cli_file_convert (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 Convert a file from one format to another. More...
 
static int load_module (void)
 
static int split_ext (char *filename, char **name, char **ext)
 Split the filename to basename and extension. More...
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "File format conversion CLI command" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
 
static struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_cli_entry cli_convert []
 

Detailed Description

file format conversion CLI command using Asterisk formats and translators

Author
redice li redic.nosp@m.e_li.nosp@m.@yaho.nosp@m.o.co.nosp@m.m
Russell Bryant russe.nosp@m.ll@d.nosp@m.igium.nosp@m..com

Definition in file res_convert.c.

Function Documentation

static void __reg_module ( void  )
static

Definition at line 166 of file res_convert.c.

static void __unreg_module ( void  )
static

Definition at line 166 of file res_convert.c.

static char* handle_cli_file_convert ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Convert a file from one format to another.

Parameters
eCLI entry
cmdcommand number
alist of cli arguments
Return values
CLI_SUCCESSon success.
CLI_SHOWUSAGEor CLI_FAILURE on failure.

Definition at line 66 of file res_convert.c.

References ast_cli_args::argc, ast_cli_args::argv, ast_cli(), ast_closestream(), AST_FILE_MODE, ast_filedelete(), ast_frfree, ast_module_ref(), ast_module_unref(), ast_readfile(), ast_readframe(), ast_strdupa, ast_strlen_zero(), ast_tvdiff_ms(), ast_tvnow(), ast_writefile(), ast_writestream(), CLI_FAILURE, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, f, ast_cli_args::fd, ast_module_info::self, split_ext(), and ast_cli_entry::usage.

67 {
68  char *ret = CLI_FAILURE;
69  struct ast_filestream *fs_in = NULL, *fs_out = NULL;
70  struct ast_frame *f;
71  struct timeval start;
72  int cost;
73  char *file_in = NULL, *file_out = NULL;
74  char *name_in, *ext_in, *name_out, *ext_out;
75 
76  switch (cmd) {
77  case CLI_INIT:
78  e->command = "file convert";
79  e->usage =
80  "Usage: file convert <file_in> <file_out>\n"
81  " Convert from file_in to file_out. If an absolute path\n"
82  " is not given, the default Asterisk sounds directory\n"
83  " will be used.\n\n"
84  " Example:\n"
85  " file convert tt-weasels.gsm tt-weasels.ulaw\n";
86  return NULL;
87  case CLI_GENERATE:
88  return NULL;
89  }
90 
91  /* ugly, can be removed when CLI entries have ast_module pointers */
93 
94  if (a->argc != 4 || ast_strlen_zero(a->argv[2]) || ast_strlen_zero(a->argv[3])) {
95  ret = CLI_SHOWUSAGE;
96  goto fail_out;
97  }
98 
99  file_in = ast_strdupa(a->argv[2]);
100  file_out = ast_strdupa(a->argv[3]);
101 
102  if (split_ext(file_in, &name_in, &ext_in)) {
103  ast_cli(a->fd, "'%s' is an invalid filename!\n", a->argv[2]);
104  goto fail_out;
105  }
106  if (!(fs_in = ast_readfile(name_in, ext_in, NULL, O_RDONLY, 0, 0))) {
107  ast_cli(a->fd, "Unable to open input file: %s\n", a->argv[2]);
108  goto fail_out;
109  }
110 
111  if (split_ext(file_out, &name_out, &ext_out)) {
112  ast_cli(a->fd, "'%s' is an invalid filename!\n", a->argv[3]);
113  goto fail_out;
114  }
115  if (!(fs_out = ast_writefile(name_out, ext_out, NULL, O_CREAT|O_TRUNC|O_WRONLY, 0, AST_FILE_MODE))) {
116  ast_cli(a->fd, "Unable to open output file: %s\n", a->argv[3]);
117  goto fail_out;
118  }
119 
120  start = ast_tvnow();
121 
122  while ((f = ast_readframe(fs_in))) {
123  if (ast_writestream(fs_out, f)) {
124  ast_frfree(f);
125  ast_cli(a->fd, "Failed to convert %s.%s to %s.%s!\n", name_in, ext_in, name_out, ext_out);
126  goto fail_out;
127  }
128  ast_frfree(f);
129  }
130 
131  cost = ast_tvdiff_ms(ast_tvnow(), start);
132  ast_cli(a->fd, "Converted %s.%s to %s.%s in %dms\n", name_in, ext_in, name_out, ext_out, cost);
133  ret = CLI_SUCCESS;
134 
135 fail_out:
136  if (fs_out) {
137  ast_closestream(fs_out);
138  if (ret != CLI_SUCCESS)
139  ast_filedelete(name_out, ext_out);
140  }
141 
142  if (fs_in)
143  ast_closestream(fs_in);
144 
146 
147  return ret;
148 }
void ast_module_unref(struct ast_module *)
Definition: loader.c:1312
const int argc
Definition: cli.h:154
Definition: cli.h:146
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:142
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:90
int ast_filedelete(const char *filename, const char *fmt)
Deletes a file.
Definition: file.c:931
void ast_cli(int fd, const char *fmt,...)
Definition: cli.c:105
#define AST_FILE_MODE
Definition: asterisk.h:36
struct ast_module * self
Definition: module.h:227
const int fd
Definition: cli.h:153
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
const char *const * argv
Definition: cli.h:155
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
#define CLI_SHOWUSAGE
Definition: cli.h:44
struct ast_frame * ast_readframe(struct ast_filestream *s)
Read a frame from a filestream.
Definition: file.c:737
struct ast_filestream * ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
Starts writing a file.
Definition: file.c:1049
#define CLI_FAILURE
Definition: cli.h:45
char * command
Definition: cli.h:180
int ast_closestream(struct ast_filestream *f)
Closes a stream.
Definition: file.c:904
static struct ast_format f[]
Definition: format_g726.c:181
const char * usage
Definition: cli.h:171
#define CLI_SUCCESS
Definition: cli.h:43
This structure is allocated by file.c in one chunk, together with buf_size and desc_size bytes of mem...
Definition: mod_format.h:100
int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
Writes a frame to a stream.
Definition: file.c:150
Data structure associated with a single frame of data.
Definition: frame.h:142
struct ast_filestream * ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
Starts reading from a file.
Definition: file.c:997
#define ast_frfree(fr)
Definition: frame.h:583
static int split_ext(char *filename, char **name, char **ext)
Split the filename to basename and extension.
Definition: res_convert.c:43
struct ast_module * ast_module_ref(struct ast_module *)
Definition: loader.c:1300
static int load_module ( void  )
static

Definition at line 160 of file res_convert.c.

References ARRAY_LEN, ast_cli_register_multiple(), and AST_MODULE_LOAD_SUCCESS.

161 {
164 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
static struct ast_cli_entry cli_convert[]
Definition: res_convert.c:150
int ast_cli_register_multiple(struct ast_cli_entry *e, int len)
Register multiple commands.
Definition: cli.c:2167
static int split_ext ( char *  filename,
char **  name,
char **  ext 
)
static

Split the filename to basename and extension.

Definition at line 43 of file res_convert.c.

References ast_strlen_zero().

Referenced by handle_cli_file_convert().

44 {
45  *name = *ext = filename;
46 
47  if ((*ext = strrchr(filename, '.'))) {
48  **ext = '\0';
49  (*ext)++;
50  }
51 
53  return -1;
54 
55  return 0;
56 }
const char * ext
Definition: http.c:112
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
static const char name[]
static int unload_module ( void  )
static

Definition at line 154 of file res_convert.c.

References ARRAY_LEN, and ast_cli_unregister_multiple().

155 {
157  return 0;
158 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: cli.c:2177
static struct ast_cli_entry cli_convert[]
Definition: res_convert.c:150

Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "File format conversion CLI command" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
static

Definition at line 166 of file res_convert.c.

Definition at line 166 of file res_convert.c.

struct ast_cli_entry cli_convert[]
static
Initial value:
= {
AST_CLI_DEFINE(handle_cli_file_convert, "Convert audio file")
}
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:191
static char * handle_cli_file_convert(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Convert a file from one format to another.
Definition: res_convert.c:66

Definition at line 150 of file res_convert.c.