| 
									
										
										
										
											2006-03-14 19:50:27 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  |  * Copyright (C) 2005, 2006, Digium, Inc. | 
					
						
							| 
									
										
										
										
											2006-03-14 19:50:27 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * 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> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */  | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-14 20:28:54 +00:00
										 |  |  | /*** MODULEINFO
 | 
					
						
							|  |  |  | 	<support_level>core</support_level> | 
					
						
							|  |  |  |  ***/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 18:54:56 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-03-14 19:50:27 +00:00
										 |  |  | #include "asterisk/channel.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; | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2007-07-20 17:11:39 +00:00
										 |  |  | 	if ((*ext = strrchr(filename, '.'))) { | 
					
						
							|  |  |  | 		**ext = '\0'; | 
					
						
							|  |  |  | 		(*ext)++; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-03-14 19:50:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (ast_strlen_zero(*name) || ast_strlen_zero(*ext)) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-07-23 14:32:04 +00:00
										 |  |  | /*! 
 | 
					
						
							|  |  |  |  * \brief Convert a file from one format to another  | 
					
						
							| 
									
										
										
										
											2007-11-12 23:44:20 +00:00
										 |  |  |  * \param e CLI entry | 
					
						
							|  |  |  |  * \param cmd command number | 
					
						
							|  |  |  |  * \param a list of cli arguments | 
					
						
							| 
									
										
										
										
											2007-10-11 19:03:06 +00:00
										 |  |  |  * \retval CLI_SUCCESS on success. | 
					
						
							|  |  |  |  * \retval CLI_SHOWUSAGE or CLI_FAILURE on failure. | 
					
						
							| 
									
										
										
										
											2007-07-23 14:32:04 +00:00
										 |  |  | */ | 
					
						
							| 
									
										
										
										
											2007-10-11 19:03:06 +00:00
										 |  |  | static char *handle_cli_file_convert(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) | 
					
						
							| 
									
										
										
										
											2006-09-20 20:18:25 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-10-11 19:03:06 +00:00
										 |  |  | 	char *ret = CLI_FAILURE; | 
					
						
							| 
									
										
										
										
											2006-09-20 20:18:25 +00:00
										 |  |  | 	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; | 
					
						
							| 
									
										
										
										
											2007-10-11 19:03:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	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; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-09-20 20:18:25 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2007-10-11 19:03:06 +00:00
										 |  |  | 	if (a->argc != 4 || ast_strlen_zero(a->argv[2]) || ast_strlen_zero(a->argv[3])) { | 
					
						
							|  |  |  | 		ret = CLI_SHOWUSAGE; | 
					
						
							| 
									
										
										
										
											2006-09-20 20:18:25 +00:00
										 |  |  | 		goto fail_out;	 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-11 19:03:06 +00:00
										 |  |  | 	file_in = ast_strdupa(a->argv[2]); | 
					
						
							|  |  |  | 	file_out = ast_strdupa(a->argv[3]); | 
					
						
							| 
									
										
										
										
											2006-09-20 20:18:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (split_ext(file_in, &name_in, &ext_in)) { | 
					
						
							| 
									
										
										
										
											2007-10-11 19:03:06 +00:00
										 |  |  | 		ast_cli(a->fd, "'%s' is an invalid filename!\n", a->argv[2]); | 
					
						
							| 
									
										
										
										
											2006-09-20 20:18:25 +00:00
										 |  |  | 		goto fail_out; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (!(fs_in = ast_readfile(name_in, ext_in, NULL, O_RDONLY, 0, 0))) { | 
					
						
							| 
									
										
										
										
											2007-10-11 19:03:06 +00:00
										 |  |  | 		ast_cli(a->fd, "Unable to open input file: %s\n", a->argv[2]); | 
					
						
							| 
									
										
										
										
											2006-09-20 20:18:25 +00:00
										 |  |  | 		goto fail_out; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	if (split_ext(file_out, &name_out, &ext_out)) { | 
					
						
							| 
									
										
										
										
											2007-10-11 19:03:06 +00:00
										 |  |  | 		ast_cli(a->fd, "'%s' is an invalid filename!\n", a->argv[3]); | 
					
						
							| 
									
										
										
										
											2006-09-20 20:18:25 +00:00
										 |  |  | 		goto fail_out; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-12-21 19:44:20 +00:00
										 |  |  | 	if (!(fs_out = ast_writefile(name_out, ext_out, NULL, O_CREAT|O_TRUNC|O_WRONLY, 0, AST_FILE_MODE))) { | 
					
						
							| 
									
										
										
										
											2007-10-11 19:03:06 +00:00
										 |  |  | 		ast_cli(a->fd, "Unable to open output file: %s\n", a->argv[3]); | 
					
						
							| 
									
										
										
										
											2006-09-20 20:18:25 +00:00
										 |  |  | 		goto fail_out; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	start = ast_tvnow(); | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	while ((f = ast_readframe(fs_in))) { | 
					
						
							|  |  |  | 		if (ast_writestream(fs_out, f)) { | 
					
						
							| 
									
										
										
										
											2009-05-26 18:20:57 +00:00
										 |  |  | 			ast_frfree(f); | 
					
						
							| 
									
										
										
										
											2007-10-11 19:03:06 +00:00
										 |  |  | 			ast_cli(a->fd, "Failed to convert %s.%s to %s.%s!\n", name_in, ext_in, name_out, ext_out); | 
					
						
							| 
									
										
										
										
											2006-09-20 20:18:25 +00:00
										 |  |  | 			goto fail_out; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-05-26 18:20:57 +00:00
										 |  |  | 		ast_frfree(f); | 
					
						
							| 
									
										
										
										
											2006-09-20 20:18:25 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cost = ast_tvdiff_ms(ast_tvnow(), start); | 
					
						
							| 
									
										
										
										
											2007-10-11 19:03:06 +00:00
										 |  |  | 	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; | 
					
						
							| 
									
										
										
										
											2006-09-20 20:18:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | fail_out: | 
					
						
							|  |  |  | 	if (fs_out) { | 
					
						
							|  |  |  | 		ast_closestream(fs_out); | 
					
						
							| 
									
										
										
										
											2007-10-11 19:03:06 +00:00
										 |  |  | 		if (ret != CLI_SUCCESS) | 
					
						
							| 
									
										
										
										
											2006-09-20 20:18:25 +00:00
										 |  |  | 			ast_filedelete(name_out, ext_out); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (fs_in)  | 
					
						
							|  |  |  | 		ast_closestream(fs_in); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-09-18 19:54:18 +00:00
										 |  |  | static struct ast_cli_entry cli_convert[] = { | 
					
						
							| 
									
										
										
										
											2007-10-22 20:05:18 +00:00
										 |  |  | 	AST_CLI_DEFINE(handle_cli_file_convert, "Convert audio file") | 
					
						
							| 
									
										
										
										
											2006-03-14 19:50:27 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int unload_module(void) | 
					
						
							| 
									
										
										
										
											2006-03-14 19:50:27 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-12-05 10:31:25 +00:00
										 |  |  | 	ast_cli_unregister_multiple(cli_convert, ARRAY_LEN(cli_convert)); | 
					
						
							| 
									
										
										
										
											2006-09-18 19:54:18 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2006-03-14 19:50:27 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int load_module(void) | 
					
						
							| 
									
										
										
										
											2006-03-14 19:50:27 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-12-05 10:31:25 +00:00
										 |  |  | 	ast_cli_register_multiple(cli_convert, ARRAY_LEN(cli_convert)); | 
					
						
							| 
									
										
										
										
											2007-10-31 19:24:29 +00:00
										 |  |  | 	return AST_MODULE_LOAD_SUCCESS; | 
					
						
							| 
									
										
										
										
											2006-03-14 19:50:27 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "File format conversion CLI command"); |