static switch_status_t do_snap(switch_core_session_t *session) { switch_channel_t *channel = switch_core_session_get_channel(session); switch_media_bug_t *bug = switch_channel_get_private(channel, "snapshot"); char *file; switch_file_handle_t fh = { 0 }; switch_codec_implementation_t read_impl = { 0 }; switch_size_t bytes_read; int16_t pdata[4096] = { 0 }; if (bug) { switch_time_exp_t tm; switch_size_t retsize; char date[80] = ""; struct cap_cb *cb = (struct cap_cb *) switch_core_media_bug_get_user_data(bug); if (!cb) { return SWITCH_STATUS_FALSE; } switch_time_exp_lt(&tm, switch_time_make(switch_epoch_time_now(NULL), 0)); switch_strftime(date, &retsize, sizeof(date), "%Y_%m_%d_%H_%M_%S", &tm); file = switch_core_session_sprintf(session, "%s%s%s_%s.wav", SWITCH_GLOBAL_dirs.sounds_dir, SWITCH_PATH_SEPARATOR, cb->base, date); switch_core_session_get_read_impl(session, &read_impl); fh.channels = 0; fh.native_rate = read_impl.actual_samples_per_second; if (switch_core_file_open(&fh, file, 0, read_impl.actual_samples_per_second, SWITCH_FILE_FLAG_WRITE | SWITCH_FILE_DATA_SHORT, NULL) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Error opening %s\n", file); switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER); switch_core_session_reset(session, SWITCH_TRUE, SWITCH_TRUE); return SWITCH_STATUS_FALSE; } switch_mutex_lock(cb->mutex); while ((bytes_read = switch_buffer_read(cb->buffer, pdata, sizeof(pdata)))) { switch_size_t samples = bytes_read / 2; if (switch_core_file_write(&fh, pdata, &samples) != SWITCH_STATUS_SUCCESS) { break; } } switch_mutex_unlock(cb->mutex); switch_core_file_close(&fh); switch_core_set_variable("file", file); switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "Wrote %s\n", file); return SWITCH_STATUS_SUCCESS; } switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "%s Bug is not attached.\n", switch_channel_get_name(channel)); return SWITCH_STATUS_FALSE; }
/** * Write to file * @param handle * @param data * @param len * @return */ static switch_status_t fileman_file_write(switch_file_handle_t *handle, void *data, size_t *len) { struct fileman_file_context *context = (struct fileman_file_context *)handle->private_info; switch_file_handle_t *fh = &context->fh; if (!switch_test_flag(handle, SWITCH_FILE_PAUSE)) { return switch_core_file_write(fh, data, len); } return SWITCH_STATUS_SUCCESS; }
switch_status_t vmivr_merge_media_files(const char** inputs, const char *output, int rate) { switch_status_t status = SWITCH_STATUS_SUCCESS; switch_file_handle_t fh_output = { 0 }; int channels = 1; int j = 0; if (switch_core_file_open(&fh_output, output, channels, rate, SWITCH_FILE_FLAG_WRITE | SWITCH_FILE_DATA_SHORT, NULL) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't open %s\n", output); goto end; } for (j = 0; inputs[j] != NULL && j < 128 && status == SWITCH_STATUS_SUCCESS; j++) { switch_file_handle_t fh_input = { 0 }; char buf[2048]; switch_size_t len = sizeof(buf) / 2; if (switch_core_file_open(&fh_input, inputs[j], channels, rate, SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't open %s\n", inputs[j]); status = SWITCH_STATUS_GENERR; break; } while (switch_core_file_read(&fh_input, buf, &len) == SWITCH_STATUS_SUCCESS) { if (switch_core_file_write(&fh_output, buf, &len) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Write error\n"); status = SWITCH_STATUS_GENERR; break; } } if (fh_input.file_interface) { switch_core_file_close(&fh_input); } } if (fh_output.file_interface) { switch_core_file_close(&fh_output); } end: return status; }
int main(int argc, char *argv[]) { int r = 1; switch_bool_t verbose = SWITCH_FALSE; const char *err = NULL; int i; char *extra_modules[100] = { 0 }; int extra_modules_count = 0; int cmd_fail = 0; const char *fmtp = ""; int ptime = 20; const char *input, *output, *format = NULL; int channels = 1; int rate = 8000; switch_file_handle_t fh_input = { 0 }, fh_output = { 0 }; switch_codec_t codec = { 0 }; switch_codec_t raw_codec = { 0 }; char buf[2048]; switch_size_t len = sizeof(buf)/2; switch_memory_pool_t *pool = NULL; int bitrate = 0; int blocksize; int in_asis = 0; int out_asis = 0; int out_flags = SWITCH_FILE_FLAG_WRITE; for (i = 1; i < argc; i++) { if (argv[i][0] == '-') { switch(argv[i][1]) { case 'c': i++; if((SWITCH_GLOBAL_dirs.conf_dir = (char *) malloc(strlen(argv[i]) + 1)) == NULL) { return 255; } strcpy(SWITCH_GLOBAL_dirs.conf_dir, argv[i]); break; case 'k': i++; if((SWITCH_GLOBAL_dirs.log_dir = (char *) malloc(strlen(argv[i]) + 1)) == NULL) { return 255; } strcpy(SWITCH_GLOBAL_dirs.log_dir, argv[i]); break; case 'm': i++; if((SWITCH_GLOBAL_dirs.mod_dir = (char *) malloc(strlen(argv[i]) + 1)) == NULL) { return 255; } strcpy(SWITCH_GLOBAL_dirs.mod_dir, argv[i]); break; case 'l': i++; /* Load extra modules */ if (strchr(argv[i], ',')) { extra_modules_count = switch_split(argv[i], ',', extra_modules); } else { extra_modules_count = 1; extra_modules[0] = argv[i]; } break; case 'f': fmtp = argv[++i]; break; case 'p': ptime = atoi(argv[++i]); break; case 'r': rate = atoi(argv[++i]); break; case 'b': bitrate = atoi(argv[++i]); break; case 'v': verbose = SWITCH_TRUE; break; default: printf("Command line option not recognized: %s\n", argv[i]); cmd_fail = 1; } } else { break; } } if (argc - i < 2 || cmd_fail) { goto usage; } input = argv[i++]; output = argv[i++]; if (zstr(input) || zstr(output) || !(format = strchr(output, '.'))) { goto usage; } format++; if (switch_core_init(SCF_MINIMAL, verbose, &err) != SWITCH_STATUS_SUCCESS) { fprintf(stderr, "Cannot init core [%s]\n", err); goto end; } switch_loadable_module_init(SWITCH_FALSE); switch_loadable_module_load_module("", "CORE_PCM_MODULE", SWITCH_TRUE, &err); switch_loadable_module_load_module("", "CORE_SPEEX_MODULE", SWITCH_TRUE, &err); switch_loadable_module_load_module("", "CORE_SOFTTIMER_MODULE", SWITCH_TRUE, &err); for (i = 0; i < extra_modules_count; i++) { if (switch_loadable_module_load_module((char *) SWITCH_GLOBAL_dirs.mod_dir, (char *) extra_modules[i], SWITCH_TRUE, &err) != SWITCH_STATUS_SUCCESS) { fprintf(stderr, "Cannot init %s [%s]\n", extra_modules[i], err); goto end; } } if (switch_loadable_module_load_module((char *) SWITCH_GLOBAL_dirs.mod_dir, (char *) "mod_spandsp", SWITCH_TRUE, &err) != SWITCH_STATUS_SUCCESS) { fprintf(stderr, "Cannot init mod_spandsp [%s]\n", err); goto end; } if (switch_loadable_module_load_module((char *) SWITCH_GLOBAL_dirs.mod_dir, (char *) "mod_sndfile", SWITCH_TRUE, &err) != SWITCH_STATUS_SUCCESS) { fprintf(stderr, "Cannot init mod_sndfile [%s]\n", err); goto end; } if (switch_loadable_module_load_module((char *) SWITCH_GLOBAL_dirs.mod_dir, (char *) "mod_native_file", SWITCH_TRUE, &err) != SWITCH_STATUS_SUCCESS) { fprintf(stderr, "Cannot init mod_native_file [%s]\n", err); goto end; } switch_core_new_memory_pool(&pool); if (verbose) { fprintf(stderr, "Opening file %s\n", input); } if (switch_core_file_open(&fh_input, input, channels, rate, SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL) != SWITCH_STATUS_SUCCESS) { fprintf(stderr, "Couldn't open %s\n", input); goto end; } if (verbose) { fprintf(stderr, "Opening file %s\n", output); } if (switch_stristr(".wav", output)) { out_asis = 0; out_flags |= SWITCH_FILE_DATA_SHORT; } else { out_asis = 1; out_flags |= SWITCH_FILE_NATIVE; } if (out_asis) { if (switch_core_codec_init_with_bitrate(&codec, format, NULL, fmtp, rate, ptime, channels, bitrate, SWITCH_CODEC_FLAG_ENCODE, NULL, pool) != SWITCH_STATUS_SUCCESS) { fprintf(stderr, "Couldn't initialize codec for %s@%dh@%di\n", format, rate, ptime); goto end; } } else { char *p; if ((p = strchr(input, '.'))) { p++; } if (!p || switch_core_codec_init_with_bitrate(&codec, p, NULL, fmtp, rate, ptime, channels, bitrate, SWITCH_CODEC_FLAG_ENCODE|SWITCH_CODEC_FLAG_DECODE, NULL, pool) != SWITCH_STATUS_SUCCESS) { fprintf(stderr, "Couldn't initialize codec for %s@%dh@%di\n", p, rate, ptime); goto end; } if (switch_core_codec_init_with_bitrate(&raw_codec, "L16", NULL, fmtp, rate, ptime, channels, bitrate, SWITCH_CODEC_FLAG_ENCODE|SWITCH_CODEC_FLAG_DECODE, NULL, pool) != SWITCH_STATUS_SUCCESS) { fprintf(stderr, "Couldn't initialize codec for %s@%dh@%di\n", "L16", rate, ptime); goto end; } } if (switch_core_file_open(&fh_output, output, channels, codec.implementation->actual_samples_per_second, out_flags, NULL) != SWITCH_STATUS_SUCCESS) { fprintf(stderr, "Couldn't open %s\n", output); goto end; } if (switch_test_flag(&fh_input, SWITCH_FILE_NATIVE)) { in_asis = 1; } if (in_asis) { blocksize = len = codec.implementation->encoded_bytes_per_packet; } else { blocksize = len = (rate*ptime)/1000; } switch_assert(sizeof(buf) >= len * 2); if (verbose) { fprintf(stderr, "Frame size is %d\n", blocksize); } while (switch_core_file_read(&fh_input, buf, &len) == SWITCH_STATUS_SUCCESS) { char encode_buf[2048]; uint32_t encoded_len = sizeof(buf); uint32_t encoded_rate = rate; unsigned int flags = 0; if (out_asis) { if (switch_core_codec_encode(&codec, NULL, buf, len*2, rate, encode_buf, &encoded_len, &encoded_rate, &flags) != SWITCH_STATUS_SUCCESS) { fprintf(stderr, "Codec encoder error\n"); goto end; } len = encoded_len; } else { if (!in_asis) { encoded_len = len; } else if (in_asis) { switch_core_codec_decode(&codec, &raw_codec, buf, len, rate, encode_buf, &encoded_len, &encoded_rate, &flags); encoded_len /= 2; len = encoded_len; } } if (switch_core_file_write(&fh_output, encode_buf, &len) != SWITCH_STATUS_SUCCESS) { fprintf(stderr, "Write error\n"); goto end; } if (len != encoded_len) { printf("Short write: wrote %"SWITCH_SIZE_T_FMT"/%d bytes\n", len, encoded_len); } len = blocksize; } r = 0; end: switch_core_codec_destroy(&codec); switch_core_codec_destroy(&raw_codec); if (fh_input.file_interface) { switch_core_file_close(&fh_input); } if (fh_output.file_interface) { switch_core_file_close(&fh_output); } if (pool) { switch_core_destroy_memory_pool(&pool); } fs_encode_cleanup(); //switch_core_destroy(); return r; usage: printf("Usage: %s [options] input output\n\n", argv[0]); printf("The output must end in the format, e.g., myfile.SPEEX\n"); printf("\t\t -c path\t\t Path to the FS configurations.\n"); printf("\t\t -k path\t\t Path to the FS log directory\n"); printf("\t\t -l module[,module]\t Load additional modules (comma-separated)\n"); printf("\t\t -m path\t\t Path to the modules.\n"); printf("\t\t -f format\t\t fmtp to pass to the codec\n"); printf("\t\t -p ptime\t\t ptime to use while encoding\n"); printf("\t\t -r rate\t\t sampling rate\n"); printf("\t\t -b bitrate\t\t codec bitrate (if supported)\n"); printf("\t\t -v\t\t\t verbose\n"); fs_encode_cleanup(); return 1; }