fs_encode.c File Reference

#include <switch.h>
#include <switch_version.h>

Include dependency graph for fs_encode.c:

Go to the source code of this file.

Defines

#define _XOPEN_SOURCE   600

Functions

int main (int argc, char *argv[])


Define Documentation

#define _XOPEN_SOURCE   600

Definition at line 33 of file fs_encode.c.


Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 51 of file fs_encode.c.

References buf, switch_directories::mod_dir, pool, SCF_MINIMAL, switch_core_init(), SWITCH_FALSE, SWITCH_GLOBAL_dirs, switch_loadable_module_init(), switch_loadable_module_load_module(), switch_split, SWITCH_STATUS_SUCCESS, SWITCH_TRUE, and zstr.

Referenced by IVRMenu::IVRMenu().

00052 {
00053         int r = 1;
00054         switch_bool_t verbose = SWITCH_FALSE;
00055         const char *err = NULL;
00056         int i;
00057         char *extra_modules[100] = { 0 };
00058         int extra_modules_count = 0;
00059         int cmd_fail = 0;
00060         const char *fmtp = "";
00061         int ptime = 20;
00062         const char *input, *output, *format = NULL;
00063         int channels = 1;
00064         int rate = 8000;
00065         switch_file_handle_t fh_input = { 0 }, fh_output = { 0 };
00066         switch_codec_t codec = { 0 };
00067         char buf[2048];
00068         switch_size_t len = sizeof(buf)/2;
00069         switch_memory_pool_t *pool;
00070         int bitrate = 0;
00071         int blocksize;
00072         
00073         for (i = 1; i < argc; i++) {
00074                 if (argv[i][0] == '-') {
00075                         switch(argv[i][1]) {
00076                                 case 'l':
00077                                         i++;
00078                                         /* Load extra modules */
00079                                         if (strchr(argv[i], ','))  {
00080                                                 extra_modules_count = switch_split(argv[i], ',', extra_modules);        
00081                                         } else {
00082                                                 extra_modules_count = 1;
00083                                                 extra_modules[0] = argv[i];
00084                                         }
00085                                         break;
00086                                 case 'f':
00087                                         fmtp = argv[++i];
00088                                         break;
00089                                 case 'p':
00090                                         ptime = atoi(argv[++i]);
00091                                         break;
00092                                 case 'r':
00093                                         rate = atoi(argv[++i]);
00094                                         break;
00095                                 case 'b':
00096                                         bitrate = atoi(argv[++i]);
00097                                         break;
00098                                 case 'v':
00099                                         verbose = SWITCH_TRUE;
00100                                         break;
00101                                 default:
00102                                         printf("Command line option not recognized: %s\n", argv[i]);
00103                                         cmd_fail = 1;
00104                         }
00105                 } else {
00106                         break;
00107                 }
00108         }
00109         
00110         if (argc - i < 2 || cmd_fail) {
00111                 goto usage;
00112         }
00113         
00114         input = argv[i++];
00115         output = argv[i++];
00116         if (zstr(input) || zstr(output) || !(format = strchr(output, '.'))) {
00117                 goto usage;
00118         }
00119         
00120         format++;
00121         
00122         if (switch_core_init(SCF_MINIMAL, verbose, &err) != SWITCH_STATUS_SUCCESS) {
00123                 fprintf(stderr, "Cannot init core [%s]\n", err);
00124                 goto end;
00125         }
00126         
00127         switch_loadable_module_init(SWITCH_FALSE);
00128         
00129         for (i = 0; i < extra_modules_count; i++) {
00130                 if (switch_loadable_module_load_module((char *) SWITCH_GLOBAL_dirs.mod_dir, (char *) extra_modules[i], SWITCH_TRUE, &err) != SWITCH_STATUS_SUCCESS) {
00131                         fprintf(stderr, "Cannot init %s [%s]\n", extra_modules[i], err);
00132                         goto end;
00133                 }
00134         }
00135         
00136         if (switch_loadable_module_load_module((char *) SWITCH_GLOBAL_dirs.mod_dir, (char *) "mod_sndfile", SWITCH_TRUE, &err) != SWITCH_STATUS_SUCCESS) {
00137                 fprintf(stderr, "Cannot init mod_sndfile [%s]\n", err);
00138                 goto end;
00139         }
00140         
00141         if (switch_loadable_module_load_module((char *) SWITCH_GLOBAL_dirs.mod_dir, (char *) "mod_native_file", SWITCH_TRUE, &err) != SWITCH_STATUS_SUCCESS) {
00142                 fprintf(stderr, "Cannot init mod_native_file [%s]\n", err);
00143                 goto end;
00144         }
00145         
00146         switch_core_new_memory_pool(&pool);
00147         if (verbose) {
00148                 fprintf(stderr, "Opening file %s\n", input);
00149         }
00150         if (switch_core_file_open(&fh_input, input, channels, rate, SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL) != SWITCH_STATUS_SUCCESS) {
00151                 fprintf(stderr, "Couldn't open %s\n", input);
00152                 goto end;
00153         }
00154         
00155 
00156         if (verbose) {
00157                 fprintf(stderr, "Opening file %s\n", output);
00158         }
00159         if (switch_core_file_open(&fh_output, output, channels, rate, SWITCH_FILE_FLAG_WRITE | SWITCH_FILE_NATIVE, NULL) != SWITCH_STATUS_SUCCESS) {
00160                 fprintf(stderr, "Couldn't open %s\n", output);
00161                 goto end;       
00162         }
00163 
00164         if (switch_test_flag(&fh_input, SWITCH_FILE_NATIVE)) {
00165                 fprintf(stderr, "Input as native file is not implemented\n");
00166                 goto end;
00167         }
00168         
00169         if (switch_core_codec_init_with_bitrate(&codec, format, fmtp, rate, ptime, channels, bitrate, SWITCH_CODEC_FLAG_ENCODE, NULL, pool) != SWITCH_STATUS_SUCCESS) {
00170                 fprintf(stderr, "Couldn't initialize codec for %s@%dh@%di\n", format, rate, ptime);
00171                 goto end;
00172         }
00173 
00174         blocksize = len = (rate*ptime)/1000;
00175         switch_assert(sizeof(buf) >= len * 2);
00176 
00177         if (verbose) {
00178                 fprintf(stderr, "Frame size is %d\n", blocksize);       
00179         }
00180         
00181         while (switch_core_file_read(&fh_input, buf, &len) == SWITCH_STATUS_SUCCESS) {
00182                 char encode_buf[2048];
00183                 uint32_t encoded_len = sizeof(buf);
00184                 uint32_t encoded_rate = rate;
00185                 unsigned int flags = 0;
00186                 
00187                 if (switch_core_codec_encode(&codec, NULL, buf, len*2, rate, encode_buf, &encoded_len, &encoded_rate, &flags) != SWITCH_STATUS_SUCCESS) {
00188                         fprintf(stderr, "Codec encoder error\n");
00189                         goto end;
00190                 }
00191                 
00192                 len = encoded_len;
00193 
00194                 if (switch_core_file_write(&fh_output, encode_buf, &len) != SWITCH_STATUS_SUCCESS) {
00195                         fprintf(stderr, "Write error\n");
00196                         goto end;
00197                 }
00198                 
00199                 if (len != encoded_len) {
00200                         printf("Short write: wrote %"SWITCH_SIZE_T_FMT"/%d bytes\n", len, encoded_len);
00201                 }
00202                 
00203                 len = blocksize;
00204         }
00205         
00206         r = 0;
00207 
00208 end:
00209         
00210         switch_core_codec_destroy(&codec);
00211         
00212         if (fh_input.file_interface) {
00213                 switch_core_file_close(&fh_input);              
00214         }
00215 
00216         if (fh_output.file_interface) {
00217                 switch_core_file_close(&fh_output);             
00218         }
00219 
00220         if (pool) {
00221                 switch_core_destroy_memory_pool(&pool);
00222         }
00223         switch_core_destroy();
00224         return r;
00225 usage:
00226         printf("Usage: %s [options] input output\n\n", argv[0]);
00227         printf("The output must end in the format, e.g., myfile.SPEEX\n");
00228         printf("\t\t -l module[,module]\t Load additional modules (comma-separated)\n");
00229         printf("\t\t -f format\t\t fmtp to pass to the codec\n");
00230         printf("\t\t -p ptime\t\t ptime to use while encoding\n");
00231         printf("\t\t -r rate\t\t sampling rate\n");
00232         printf("\t\t -b bitrate\t\t codec bitrate (if supported)\n");
00233         printf("\t\t -v\t\t\t verbose\n");
00234         return 1;
00235 }


Generated on Wed May 16 04:00:07 2012 for FreeSWITCH API Documentation by  doxygen 1.4.7