Exemple #1
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;
}
Exemple #2
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;
}