Example #1
0
bool UmcFramework::CreateMrcpClient()
{
	/* create MRCP client stack first */
	m_pMrcpClient = unimrcp_client_create(m_pDirLayout);
	if(!m_pMrcpClient)
		return false;

	/* create MRCP application to send/get requests to/from MRCP client stack */
	m_pMrcpApplication = mrcp_application_create(AppMessageHandler,this,m_pPool);
	if(!m_pMrcpApplication)
	{
		mrcp_client_destroy(m_pMrcpClient);
		m_pMrcpClient = NULL;
		return false;
	}

	/* register MRCP application to MRCP client */
	mrcp_client_application_register(m_pMrcpClient,m_pMrcpApplication,"UMC");
	/* start MRCP client stack processing */
	if(mrcp_client_start(m_pMrcpClient) == FALSE)
	{
		mrcp_client_destroy(m_pMrcpClient);
		m_pMrcpClient = NULL;
		m_pMrcpApplication = NULL;
		return false;
	}
	return true;
}
Example #2
0
/** Create demo framework */
demo_framework_t* demo_framework_create(apt_dir_layout_t *dir_layout)
{
	demo_framework_t *framework = NULL;
	mrcp_client_t *client = unimrcp_client_create(dir_layout);
	if(client) {
		demo_application_t *demo_application;
		apr_pool_t *pool = mrcp_client_memory_pool_get(client);
		/* create demo framework */
		framework = apr_palloc(pool,sizeof(demo_framework_t));
		framework->pool = pool;
		framework->client = client;
		framework->application_table = apr_hash_make(pool);

		/* create demo synthesizer application */
		demo_application = demo_synth_application_create(framework->pool);
		if(demo_application) {
			demo_framework_app_register(framework,demo_application,"synth");
		}

		/* create demo recognizer application */
		demo_application = demo_recog_application_create(framework->pool);
		if(demo_application) {
			demo_framework_app_register(framework,demo_application,"recog");
		}

		/* create demo bypass media application */
		demo_application = demo_bypass_application_create(framework->pool);
		if(demo_application) {
			demo_framework_app_register(framework,demo_application,"bypass");
		}

		/* create demo resource discover application */
		demo_application = demo_discover_application_create(framework->pool);
		if(demo_application) {
			demo_framework_app_register(framework,demo_application,"discover");
		}

		demo_framework_consumer_task_create(framework);

		if(framework->task) {
			apt_task_t *task = apt_consumer_task_base_get(framework->task);
			apt_task_start(task);
		}
		
		/* start client stack */
		mrcp_client_start(client);
	}

	return framework;
}
/** \brief Load module */
static int load_module(void)
{
	ast_log(LOG_NOTICE, "Load Res-Speech-UniMRCP module\n");

	if(uni_engine_load() == FALSE) {
		return AST_MODULE_LOAD_FAILURE;
	}

	if(mrcp_client_start(uni_engine.client) != TRUE) {
		ast_log(LOG_ERROR, "Failed to start MRCP client\n");
		uni_engine_unload();
		return AST_MODULE_LOAD_FAILURE;
	}

#if AST_VERSION_AT_LEAST(10,0,0)

#if AST_VERSION_AT_LEAST(13,0,0)
	ast_engine.formats = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
#elif AST_VERSION_AT_LEAST(12,0,0)
	ast_engine.formats = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
#else /* <= 11 */
	ast_engine.formats = ast_format_cap_alloc_nolock();
#endif
	if(!ast_engine.formats) {
		ast_log(LOG_ERROR, "Failed to alloc media format capabilities\n");
		uni_engine_unload();
		return AST_MODULE_LOAD_FAILURE;
	}
#if AST_VERSION_AT_LEAST(13,0,0)
	ast_format_cap_append(ast_engine.formats, ast_format_slin, 0);
#else
	struct ast_format format;
	ast_format_set(&format, AST_FORMAT_SLINEAR, 0);
	ast_format_cap_add(ast_engine.formats, &format);
#endif

#else /* <= 1.8 */
	ast_engine.formats = AST_FORMAT_SLINEAR;
#endif

	if(ast_speech_register(&ast_engine)) {
		ast_log(LOG_ERROR, "Failed to register module\n");
		mrcp_client_shutdown(uni_engine.client);
		uni_engine_unload();
		return AST_MODULE_LOAD_FAILURE;
	}

	return AST_MODULE_LOAD_SUCCESS;
}
Example #4
0
/** Create ASR engine */
ASR_CLIENT_DECLARE(asr_engine_t*) asr_engine_create(
									const char *root_dir_path,
									apt_log_priority_e log_priority,
									apt_log_output_e log_output)
{
	apr_pool_t *pool = NULL;
	apt_dir_layout_t *dir_layout;
	asr_engine_t *engine;
	mrcp_client_t *mrcp_client;
	mrcp_application_t *mrcp_app;

	/* create APR pool */
	pool = apt_pool_create();
	if(!pool) {
		return NULL;
	}

	/* create the structure of default directories layout */
	dir_layout = apt_default_dir_layout_create(root_dir_path,pool);
	/* create singleton logger */
	apt_log_instance_create(log_output,log_priority,pool);

	if((log_output & APT_LOG_OUTPUT_FILE) == APT_LOG_OUTPUT_FILE) {
		/* open the log file */
		apt_log_file_open(dir_layout->log_dir_path,"unimrcpclient",MAX_LOG_FILE_SIZE,MAX_LOG_FILE_COUNT,FALSE,pool);
	}

	engine = apr_palloc(pool,sizeof(asr_engine_t));
	engine->pool = pool;
	engine->mrcp_client = NULL;
	engine->mrcp_app = NULL;

	/* create UniMRCP client stack */
	mrcp_client = unimrcp_client_create(dir_layout);
	if(!mrcp_client) {
		apt_log_instance_destroy();
		apr_pool_destroy(pool);
		return NULL;
	}
	
	/* create an application */
	mrcp_app = mrcp_application_create(
								app_message_handler,
								engine,
								pool);
	if(!mrcp_app) {
		mrcp_client_destroy(mrcp_client);
		apt_log_instance_destroy();
		apr_pool_destroy(pool);
		return NULL;
	}

	/* register application in client stack */
	mrcp_client_application_register(mrcp_client,mrcp_app,"ASRAPP");

	/* start client stack */
	if(mrcp_client_start(mrcp_client) != TRUE) {
		mrcp_client_destroy(mrcp_client);
		apt_log_instance_destroy();
		apr_pool_destroy(pool);
		return NULL;
	}

	engine->mrcp_client = mrcp_client;
	engine->mrcp_app = mrcp_app;
	return engine;
}
Example #5
0
int main(int argc, char const* argv[])
{
	apr_pool_t* pool = NULL;
	apr_pool_t* spool = NULL;
	int i;
	struct iovec cattext[101];
	static char const SP = ' ';
	char const* outfile;
	apr_status_t status;
	apt_dir_layout_t* dirLayout = NULL;
	mrcp_client_t* client = NULL;
	mrcp_application_t* app = NULL;
	mrcp_session_t* sess = NULL;
	mpf_stream_capabilities_t* caps = NULL;
	mpf_termination_t* term = NULL;
	mrcp_channel_t* chan = NULL;
	struct stat info;

	if (argc < 2) {
		puts("Usage:");
		printf("\t%s \"This is a synthetic voice.\"", argv[0]);
		exit(1);
	}

	/* Just detect various directory layout constellations */
	if (stat(ROOT_DIR, &info))
		ROOT_DIR = ROOT_DIR2;
	if (stat(ROOT_DIR, &info))
		ROOT_DIR = ROOT_DIR3;

	/* Initialize platform first */
	if (apr_initialize() != APR_SUCCESS) FAIL("Cannot initialize APR platform");
	pool = apt_pool_create();
	if (!pool) FAIL("Not enough memory");
	for (i = 0; (i < argc - 2) && (i < 50); i += 2) {
		cattext[2 * i].iov_base = (void*) argv[i + 1];
		cattext[2 * i].iov_len = strlen(argv[i + 1]);
		cattext[2 * i + 1].iov_base = (void*) &SP;
		cattext[2 * i + 1].iov_len = 1;
	}
	cattext[2 * i].iov_base = (void*) argv[i + 1];
	cattext[2 * i].iov_len = strlen(argv[i + 1]);
	text = apr_pstrcatv(pool, cattext, 2 * i + 1, NULL);
	if (!text) FAIL("Not enough memory");
	outfile = apr_pstrcat(pool, ROOT_DIR, "/data/", PCM_OUT_FILE, NULL);
	
	printf("This is a sample C UniMRCP client synthesizer scenario.\n");
	printf("Use client configuration from %s/conf/unimrcpclient.xml\n", ROOT_DIR);
	printf("Use profile %s\n", MRCP_PROFILE);
	printf("Synthesize text: `%s'\n", text);
	printf("Write output to file: %s\n", outfile);
	printf("\n");
	printf("Press enter to start the session...\n");
	(void) getchar();

	apt_log_instance_create(APT_LOG_OUTPUT_NONE, APT_PRIO_DEBUG, pool);
	apt_log_ext_handler_set(UniSynth_logger);
	dirLayout = apt_default_dir_layout_create(ROOT_DIR, pool);

	/* Create and start the client in a root dir */
	client = unimrcp_client_create(dirLayout);
	if (!client) FAIL("Cannot create UniMRCP client");
	app = mrcp_application_create(UniSynthAppMsgHandler, NULL, mrcp_client_memory_pool_get(client));
	if (!app) FAIL("Cannot create MRCP application");
	if (!mrcp_client_application_register(client, app, "Sample C app"))
		FAIL("Cannot register MRCP application");
	if (!mrcp_client_start(client)) FAIL("Cannot start MRCP client");

	/* Create a session using MRCP profile MRCP_PROFILE */
	sess = mrcp_application_session_create(app, MRCP_PROFILE, NULL);
	if (!sess) FAIL("Cannot create session");
	spool = mrcp_application_session_pool_get(sess);
	/* Create audio termination with capabilities */
	caps = mpf_stream_capabilities_create(STREAM_DIRECTION_SEND, spool);
	if (!caps) FAIL("Error creating capabilities");
	if (!mpf_codec_capabilities_add(&caps->codecs, MPF_SAMPLE_RATE_8000, "LPCM"))
		FAIL("Error adding codec capabilities");
	term = mrcp_application_audio_termination_create(sess, &stream_vtable, caps, NULL);
	if (!term) FAIL("Cannot create audio termination");
	/* Add signaling channel (and start processing in OnAdd method */
	f = fopen(outfile, "wb");
	if (!f) FAIL("Cannot open output file");
	status = apr_thread_mutex_create(&mutex, APR_THREAD_MUTEX_DEFAULT, pool);
	if (status != APR_SUCCESS) FAIL("Cannot create mutex");
	status = apr_thread_cond_create(&cond, pool);
	if (status != APR_SUCCESS) FAIL("Cannot create condition variable");
	chan = mrcp_application_channel_create(sess, MRCP_SYNTHESIZER_RESOURCE, term, NULL, NULL);
	if (!chan) FAIL("Cannot create channel");
	if (!mrcp_application_channel_add(sess, chan))
		FAIL("Cannot add channel");

	/* Now wait until the processing finishes */
	apr_thread_mutex_lock(mutex);
	while (err < 0) apr_thread_cond_wait(cond, mutex);
	apr_thread_mutex_unlock(mutex);

cleanup:
	if (sess) mrcp_application_session_terminate(sess);
	if (f) fclose(f);
	if (client) mrcp_client_shutdown(client);
	if (app) mrcp_application_destroy(app);
	if (client) mrcp_client_destroy(client);
	apt_log_instance_destroy();
	if (pool) apr_pool_destroy(pool);
	apr_terminate();
	puts("Program finished, memory released. Press any key to exit.");
	(void) getchar();
	return err;
}
Example #6
0
AST_COMPAT_STATIC int load_module(void)
{
	int res = 0;
	apr_hash_index_t *hi;

	if (apr_initialized == 0) {
		if (apr_initialize() != APR_SUCCESS) {
			ast_log(LOG_ERROR, "Unable to initialize APR\n");
			apr_terminate();
			apr_initialized = 0;
			return AST_MODULE_LOAD_DECLINE;
		} else {
			ast_log(LOG_DEBUG, "APR initialized\n");
			apr_initialized = 1;
		}
	}

	/* Initialize globals. */
	if (globals_init() != 0) {
		ast_log(LOG_DEBUG, "Unable to initialize globals\n");
		apr_terminate();
		apr_initialized = 0;
		return AST_MODULE_LOAD_DECLINE;
	}

	/* Load the configuration file mrcp.conf. */
#if AST_VERSION_AT_LEAST(1,6,0)
	struct ast_flags config_flags = { 0 };
	struct ast_config *cfg = ast_config_load(MRCP_CONFIG, config_flags);
#else
	struct ast_config *cfg = ast_config_load(MRCP_CONFIG);
#endif
	if (!cfg) {
		ast_log(LOG_WARNING, "No such configuration file %s\n", MRCP_CONFIG);
		globals_destroy();
		apr_terminate();
		apr_initialized = 0;
		return AST_MODULE_LOAD_DECLINE;
	}

	if (load_mrcp_config(cfg) != 0) {
		ast_log(LOG_DEBUG, "Unable to load configuration\n");
		globals_destroy();
		apr_terminate();
		apr_initialized = 0;
		return AST_MODULE_LOAD_DECLINE;
	}

	/* Link UniMRCP logs to Asterisk. */
	ast_log(LOG_NOTICE, "UniMRCP log level = %s\n", globals.unimrcp_log_level);
	apt_log_priority_e log_priority = apt_log_priority_translate(globals.unimrcp_log_level);
	if (apt_log_instance_create(APT_LOG_OUTPUT_NONE, log_priority, globals.pool) == FALSE) {
		/* Already created. */
		apt_log_priority_set(log_priority);
	}
	apt_log_ext_handler_set(unimrcp_log);

	/* Create the MRCP client. */
	if ((globals.mrcp_client = mod_unimrcp_client_create(globals.pool)) == NULL) {
		ast_log(LOG_ERROR, "Failed to create MRCP client\n");
		if (!apt_log_instance_destroy())
			ast_log(LOG_WARNING, "Unable to destroy UniMRCP logger instance\n");
		globals_destroy();
		apr_terminate();
		apr_initialized = 0;
		return AST_MODULE_LOAD_DECLINE;
	}
	
	/* Load the applications. */
	load_mrcpsynth_app();
	load_mrcprecog_app();
	load_synthandrecog_app();

	/* Start the client stack. */
	if (!mrcp_client_start(globals.mrcp_client)) {
		ast_log(LOG_ERROR, "Failed to start MRCP client stack processing\n");
		if (!mrcp_client_destroy(globals.mrcp_client))
			ast_log(LOG_WARNING, "Unable to destroy MRCP client stack\n");
		else
			ast_log(LOG_DEBUG, "MRCP client stack destroyed\n");
		globals.mrcp_client = NULL;
		if (!apt_log_instance_destroy())
			ast_log(LOG_WARNING, "Unable to destroy UniMRCP logger instance\n");
		globals_destroy();
		apr_terminate();
		apr_initialized = 0;
		return AST_MODULE_LOAD_DECLINE;
	}

	/* Register the applications. */
	for (hi = apr_hash_first(NULL, globals.apps); hi; hi = apr_hash_next(hi)) {
		const void *key;
		void *val;
		const char *name;
		ast_mrcp_application_t *application;

		apr_hash_this(hi, &key, NULL, &val);

		name = (const char *) key;
		application = (ast_mrcp_application_t *) val;

#if AST_VERSION_AT_LEAST(1,6,2)
		res |= ast_register_application_xml(name, application->exec);
#else 
		res |= ast_register_application(name, application->exec, application->synopsis, application->description);
#endif
	}
	
	return res;
}