Пример #1
0
static switch_status_t tone_stream_file_read(switch_file_handle_t *handle, void *data, size_t *len)
{
	switch_buffer_t *audio_buffer = handle->private_info;
	switch_size_t bytes;

	if ((bytes = switch_buffer_read_loop(audio_buffer, data, *len * 2)) <= 0) {
		*len = 0;
		return SWITCH_STATUS_FALSE;
	}

	*len = bytes / 2;
	return SWITCH_STATUS_SUCCESS;
}
static JSBool teletone_generate(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval)
{
	struct teletone_obj *tto = JS_GetPrivate(cx, obj);
	int32 loops = 0;

	if (argc > 0) {
		char *script;
		switch_core_session_t *session;
		switch_frame_t write_frame = { 0 };
		unsigned char *fdata[1024];
		switch_frame_t *read_frame;
		switch_channel_t *channel;

		if (argc > 1) {
			if (!JS_ValueToInt32(cx, argv[1], &loops)) {
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot Convert to INT\n");
				return JS_FALSE;
			}
			loops--;
		}

		if (tto->audio_buffer) {
			switch_buffer_zero(tto->audio_buffer);
		}

		tto->ts.debug = 1;
		tto->ts.debug_stream = switch_core_get_console();

		script = JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
		teletone_run(&tto->ts, script);

		session = tto->session;
		write_frame.codec = &tto->codec;
		write_frame.data = fdata;
		write_frame.buflen = sizeof(fdata);

		channel = switch_core_session_get_channel(session);

		if (tto->timer) {
			switch_core_service_session(session);
		}

		if (loops) {
			switch_buffer_set_loops(tto->audio_buffer, loops);
		}

		for (;;) {

			if (switch_test_flag(tto, TTF_DTMF)) {
				char dtmf[128];
				char *ret;

				if (switch_channel_has_dtmf(channel)) {
					uintN aargc = 0;
					jsval aargv[4];

					switch_channel_dequeue_dtmf_string(channel, dtmf, sizeof(dtmf));
					aargv[aargc++] = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, dtmf));
					JS_CallFunction(cx, obj, tto->function, aargc, aargv, &tto->ret);
					ret = JS_GetStringBytes(JS_ValueToString(cx, tto->ret));
					if (strcmp(ret, "true") && strcmp(ret, "undefined")) {
						*rval = tto->ret;
						return JS_TRUE;
					}
				}
			}

			if (tto->timer) {
				if (switch_core_timer_next(tto->timer) != SWITCH_STATUS_SUCCESS) {
					break;
				}

			} else {
				switch_status_t status;
				status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);

				if (!SWITCH_READ_ACCEPTABLE(status)) {
					break;
				}
			}
			if ((write_frame.datalen = (uint32_t) switch_buffer_read_loop(tto->audio_buffer,
																		  fdata, write_frame.codec->implementation->decoded_bytes_per_packet)) <= 0) {
				break;
			}

			write_frame.samples = write_frame.datalen / 2;
			if (switch_core_session_write_frame(session, &write_frame, SWITCH_IO_FLAG_NONE, 0) != SWITCH_STATUS_SUCCESS) {
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Bad Write\n");
				break;
			}
		}

		if (tto->timer) {
			switch_core_thread_session_end(session);
		}
		return JS_TRUE;
	}

	return JS_FALSE;
}