Sat Apr 26 2014 22:03:09

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"
Include dependency graph for res_convert.c:

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.
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 = AST_BUILDOPT_SUM, .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 <redice_li@yahoo.com>
Russell Bryant <russell@digium.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_filedelete(), ast_frfree, ast_module_ref(), ast_module_unref(), ast_readfile(), ast_readframe(), 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.

{
   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_frfree(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;
      }
      ast_frfree(f);
   }

   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 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(), and realtime_sqlite3_db::filename.

Referenced by handle_cli_file_convert().

{
   *name = *ext = filename;
   
   if ((*ext = strrchr(filename, '.'))) {
      **ext = '\0';
      (*ext)++;
   }

   if (ast_strlen_zero(*name) || ast_strlen_zero(*ext))
      return -1;

   return 0;
}
static int unload_module ( void  ) [static]

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 = AST_BUILDOPT_SUM, .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")
}

Definition at line 150 of file res_convert.c.