static void teletone_destroy(JSContext * cx, JSObject * obj) { struct teletone_obj *tto = JS_GetPrivate(cx, obj); switch_memory_pool_t *pool; if (tto) { if (tto->timer) { switch_core_timer_destroy(tto->timer); } teletone_destroy_session(&tto->ts); switch_buffer_destroy(&tto->audio_buffer); switch_core_codec_destroy(&tto->codec); pool = tto->pool; tto->pool = NULL; if (pool) { switch_core_destroy_memory_pool(&pool); } } }
static switch_status_t tone_stream_file_open(switch_file_handle_t *handle, const char *path) { switch_buffer_t *audio_buffer = NULL; teletone_generation_session_t ts; char *tonespec; int loops = 0; char *tmp; int fd = -1; char buf[1024] = ""; size_t len; memset(&ts, 0, sizeof(ts)); tonespec = switch_core_strdup(handle->memory_pool, path); switch_buffer_create_dynamic(&audio_buffer, 1024, 1024, 0); switch_assert(audio_buffer); if ((tmp = (char *)switch_stristr(";loops=", tonespec))) { *tmp = '\0'; tmp += 7; if (tmp) { loops = atoi(tmp); switch_buffer_set_loops(audio_buffer, loops); } } if (handle->params) { if ((tmp = switch_event_get_header(handle->params, "loops"))) { loops = atoi(tmp); switch_buffer_set_loops(audio_buffer, loops); } } if (!handle->samplerate) { handle->samplerate = 8000; } handle->channels = 1; teletone_init_session(&ts, 0, teletone_handler, audio_buffer); ts.rate = handle->samplerate; ts.channels = 1; if (!strncasecmp(tonespec, "path=", 5)) { tmp = tonespec + 5; if ((fd = open(tmp, O_RDONLY)) < 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Failed to open [%s]\n", tmp); return SWITCH_STATUS_FALSE; } while ((len = switch_fd_read_line(fd, buf, sizeof(buf)))) { teletone_run(&ts, buf); } close(fd); fd = -1; } else { teletone_run(&ts, tonespec); } teletone_destroy_session(&ts); handle->private_info = audio_buffer; return SWITCH_STATUS_SUCCESS; }