Asterisk - The Open Source Telephony Project  21.4.1
Functions | Variables
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)
 
struct ast_moduleAST_MODULE_SELF_SYM (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.
 
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 = "da6642af068ee5e6490c5b1d2cc1d238" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const 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 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 64 of file res_convert.c.

References ast_closestream(), ast_filedelete(), ast_readfile(), ast_readframe(), ast_strdupa, ast_tvdiff_ms(), ast_tvnow(), ast_writefile(), ast_writestream(), ast_cli_entry::command, split_ext(), and ast_cli_entry::usage.

65 {
66  char *ret = CLI_FAILURE;
67  struct ast_filestream *fs_in = NULL, *fs_out = NULL;
68  struct ast_frame *f;
69  struct timeval start;
70  int cost;
71  char *file_in = NULL, *file_out = NULL;
72  char *name_in, *ext_in, *name_out, *ext_out;
73 
74  switch (cmd) {
75  case CLI_INIT:
76  e->command = "file convert";
77  e->usage =
78  "Usage: file convert <file_in> <file_out>\n"
79  " Convert from file_in to file_out. If an absolute path\n"
80  " is not given, the default Asterisk sounds directory\n"
81  " will be used.\n\n"
82  " Example:\n"
83  " file convert tt-weasels.gsm tt-weasels.ulaw\n";
84  return NULL;
85  case CLI_GENERATE:
86  return NULL;
87  }
88 
89  if (a->argc != 4 || ast_strlen_zero(a->argv[2]) || ast_strlen_zero(a->argv[3])) {
90  ret = CLI_SHOWUSAGE;
91  goto fail_out;
92  }
93 
94  file_in = ast_strdupa(a->argv[2]);
95  file_out = ast_strdupa(a->argv[3]);
96 
97  if (split_ext(file_in, &name_in, &ext_in)) {
98  ast_cli(a->fd, "'%s' is an invalid filename!\n", a->argv[2]);
99  goto fail_out;
100  }
101  if (!(fs_in = ast_readfile(name_in, ext_in, NULL, O_RDONLY, 0, 0))) {
102  ast_cli(a->fd, "Unable to open input file: %s\n", a->argv[2]);
103  goto fail_out;
104  }
105 
106  if (split_ext(file_out, &name_out, &ext_out)) {
107  ast_cli(a->fd, "'%s' is an invalid filename!\n", a->argv[3]);
108  goto fail_out;
109  }
110  if (!(fs_out = ast_writefile(name_out, ext_out, NULL, O_CREAT|O_TRUNC|O_WRONLY, 0, AST_FILE_MODE))) {
111  ast_cli(a->fd, "Unable to open output file: %s\n", a->argv[3]);
112  goto fail_out;
113  }
114 
115  start = ast_tvnow();
116 
117  while ((f = ast_readframe(fs_in))) {
118  if (ast_writestream(fs_out, f)) {
119  ast_frfree(f);
120  ast_cli(a->fd, "Failed to convert %s.%s to %s.%s!\n", name_in, ext_in, name_out, ext_out);
121  goto fail_out;
122  }
123  ast_frfree(f);
124  }
125 
126  cost = ast_tvdiff_ms(ast_tvnow(), start);
127  ast_cli(a->fd, "Converted %s.%s to %s.%s in %dms\n", name_in, ext_in, name_out, ext_out, cost);
128  ret = CLI_SUCCESS;
129 
130 fail_out:
131  if (fs_out) {
132  ast_closestream(fs_out);
133  if (ret != CLI_SUCCESS)
134  ast_filedelete(name_out, ext_out);
135  }
136 
137  if (fs_in)
138  ast_closestream(fs_in);
139 
140  return ret;
141 }
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:159
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:107
int ast_filedelete(const char *filename, const char *fmt)
Deletes a file.
Definition: file.c:1141
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
struct ast_frame * ast_readframe(struct ast_filestream *s)
Read a frame from a filestream.
Definition: file.c:936
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:1423
char * command
Definition: cli.h:186
int ast_closestream(struct ast_filestream *f)
Closes a stream.
Definition: file.c:1111
const char * usage
Definition: cli.h:177
This structure is allocated by file.c in one chunk, together with buf_size and desc_size bytes of mem...
Definition: mod_format.h:101
int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
Writes a frame to a stream.
Definition: file.c:244
Data structure associated with a single frame of data.
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:1371
static int split_ext(char *filename, char **name, char **ext)
Split the filename to basename and extension.
Definition: res_convert.c:41

Variable Documentation

struct ast_cli_entry cli_convert[]
static
Initial value:
= {
{ .handler = handle_cli_file_convert , .summary = "Convert audio file" ,}
}
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:64

Definition at line 143 of file res_convert.c.