/** Load components */ static apt_bool_t unimrcp_server_components_load(unimrcp_server_loader_t *loader, const apr_xml_elem *root) { const apr_xml_elem *elem; const apr_xml_attr *id_attr; const apr_xml_attr *enable_attr; const char *id; /* Create codec manager first (probably it should be loaded from config either) */ mpf_codec_manager_t *codec_manager = mpf_engine_codec_manager_create(loader->pool); if(codec_manager) { mrcp_server_codec_manager_register(loader->server,codec_manager); } apt_log(APT_LOG_MARK,APT_PRIO_DEBUG,"Loading Components"); for(elem = root->first_child; elem; elem = elem->next) { if(strcasecmp(elem->name,"resource-factory") == 0) { unimrcp_server_resource_factory_load(loader,elem); continue; } if(strcasecmp(elem->name,"plugin-factory") == 0) { unimrcp_server_plugin_factory_load(loader,elem); continue; } /* get common "id" and "enable" attributes */ if(header_attribs_get(elem,&id_attr,&enable_attr) == FALSE) { /* invalid id */ continue; } if(is_attr_enabled(enable_attr) == FALSE) { /* disabled element, just skip it */ continue; } id = apr_pstrdup(loader->pool,id_attr->value); if(strcasecmp(elem->name,"sip-uas") == 0) { unimrcp_server_sip_uas_load(loader,elem,id); } else if(strcasecmp(elem->name,"rtsp-uas") == 0) { unimrcp_server_rtsp_uas_load(loader,elem,id); } else if(strcasecmp(elem->name,"mrcpv2-uas") == 0) { unimrcp_server_mrcpv2_uas_load(loader,elem,id); } else if(strcasecmp(elem->name,"media-engine") == 0) { unimrcp_server_media_engine_load(loader,elem,id); } else if(strcasecmp(elem->name,"rtp-factory") == 0) { unimrcp_server_rtp_factory_load(loader,elem,id); } else if(strcasecmp(elem->name,"plugin-factory") == 0) { unimrcp_server_plugin_factory_load(loader,elem); } else { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Unknown Element <%s>",elem->name); } } return TRUE; }
/** Start UniMRCP server */ MRCP_DECLARE(mrcp_server_t*) unimrcp_server_start(apt_dir_layout_t *dir_layout) { apr_pool_t *pool; apr_xml_doc *doc; mrcp_resource_factory_t *resource_factory; mpf_codec_manager_t *codec_manager; mrcp_server_t *server; if(!dir_layout) { return NULL; } apt_log(APT_PRIO_NOTICE,"UniMRCP Server ["UNI_VERSION_STRING"]"); apt_log(APT_PRIO_INFO,"APR ["APR_VERSION_STRING"]"); server = mrcp_server_create(dir_layout); if(!server) { return NULL; } pool = mrcp_server_memory_pool_get(server); resource_factory = mrcp_default_factory_create(pool); if(resource_factory) { mrcp_server_resource_factory_register(server,resource_factory); } codec_manager = mpf_engine_codec_manager_create(pool); if(codec_manager) { mrcp_server_codec_manager_register(server,codec_manager); } doc = unimrcp_server_config_parse(dir_layout->conf_dir_path,pool); if(doc) { unimrcp_server_config_load(server,dir_layout->plugin_dir_path,doc,pool); } mrcp_server_start(server); return server; }
/* Create an MRCP client. * * Some code and ideas borrowed from unimrcp-client.c * Please check $unimrcp_dir$/platforms/libunimrcp-client/src/unimrcp_client.c * when upgrading the UniMRCP library to ensure nothing new needs to be set up. */ mrcp_client_t *mod_unimrcp_client_create(apr_pool_t *mod_pool) { mrcp_client_t *client = NULL; apt_dir_layout_t *dir_layout = NULL; apr_pool_t *pool = NULL; mrcp_resource_factory_t *resource_factory = NULL; mpf_codec_manager_t *codec_manager = NULL; apr_size_t max_connection_count = 0; apt_bool_t offer_new_connection = FALSE; mrcp_connection_agent_t *shared_connection_agent = NULL; mpf_engine_t *shared_media_engine = NULL; if (!globals.profiles) { ast_log(LOG_ERROR, "Profiles hash is NULL\n"); return NULL; } /* Create the client. */ if ((dir_layout = apt_default_dir_layout_create("../", mod_pool)) == NULL) { ast_log(LOG_ERROR, "Unable to create directory layout\n"); return NULL; } if ((client = mrcp_client_create(dir_layout)) == NULL) { ast_log(LOG_ERROR, "Unable to create MRCP client stack\n"); return NULL; } if ((pool = mrcp_client_memory_pool_get(client)) == NULL) { ast_log(LOG_ERROR, "MRCP client pool is NULL\n"); return NULL; } mrcp_resource_loader_t *resource_loader = mrcp_resource_loader_create(FALSE, pool); if (resource_loader == NULL) { ast_log(LOG_ERROR, "Unable to create MRCP resource loader.\n"); return NULL; } apt_str_t resource_class_synth; apt_str_t resource_class_recog; apt_string_set(&resource_class_synth, "speechsynth"); apt_string_set(&resource_class_recog, "speechrecog"); mrcp_resource_load(resource_loader, &resource_class_synth); mrcp_resource_load(resource_loader, &resource_class_recog); resource_factory = mrcp_resource_factory_get(resource_loader); if (!mrcp_client_resource_factory_register(client, resource_factory)) ast_log(LOG_WARNING, "Unable to register MRCP client resource factory\n"); if ((codec_manager = mpf_engine_codec_manager_create(pool)) != NULL) { if (!mrcp_client_codec_manager_register(client, codec_manager)) ast_log(LOG_WARNING, "Unable to register MRCP client codec manager\n"); } /* Set up MRCPv2 connection agent that will be shared with all profiles. */ if ((globals.unimrcp_max_connection_count != NULL) && (strlen(globals.unimrcp_max_connection_count) > 0)) max_connection_count = atoi(globals.unimrcp_max_connection_count); if (max_connection_count <= 0) max_connection_count = DEFAULT_UNIMRCP_MAX_CONNECTION_COUNT; if (globals.unimrcp_offer_new_connection != NULL) { if (strcasecmp(globals.unimrcp_offer_new_connection, "true") == 0 || atoi(globals.unimrcp_offer_new_connection) == 1) offer_new_connection = TRUE; } else { offer_new_connection = DEFAULT_UNIMRCP_OFFER_NEW_CONNECTION; } if ((shared_connection_agent = mrcp_client_connection_agent_create("MRCPv2ConnectionAgent", max_connection_count, offer_new_connection, pool)) != NULL) { if (shared_connection_agent != NULL) { if (globals.unimrcp_rx_buffer_size != NULL) { apr_size_t rx_buffer_size = (apr_size_t)atol(globals.unimrcp_rx_buffer_size); if (rx_buffer_size > 0) { mrcp_client_connection_rx_size_set(shared_connection_agent, rx_buffer_size); } } if (globals.unimrcp_tx_buffer_size != NULL) { apr_size_t tx_buffer_size = (apr_size_t)atol(globals.unimrcp_tx_buffer_size); if (tx_buffer_size > 0) { mrcp_client_connection_tx_size_set(shared_connection_agent, tx_buffer_size); } } if (globals.unimrcp_request_timeout != NULL) { apr_size_t request_timeout = (apr_size_t)atol(globals.unimrcp_request_timeout); if (request_timeout > 0) { mrcp_client_connection_timeout_set(shared_connection_agent, request_timeout); } } } if (!mrcp_client_connection_agent_register(client, shared_connection_agent)) ast_log(LOG_WARNING, "Unable to register MRCP client connection agent\n"); } /* Set up the media engine that will be shared with all profiles. */ if ((shared_media_engine = mpf_engine_create("MediaEngine", pool)) != NULL) { unsigned long realtime_rate = 1; if (!mpf_engine_scheduler_rate_set(shared_media_engine, realtime_rate)) ast_log(LOG_WARNING, "Unable to set scheduler rate for MRCP client media engine\n"); if (!mrcp_client_media_engine_register(client, shared_media_engine)) ast_log(LOG_WARNING, "Unable to register MRCP client media engine\n"); } if (globals.profiles) { if(load_profiles(client, shared_connection_agent, shared_media_engine, pool) !=0) return NULL; } return client; }
/** Run MPF test suite */ static apt_bool_t mpf_test_run(apt_test_suite_t *suite, int argc, const char * const *argv) { mpf_suite_agent_t *agent; mpf_codec_manager_t *codec_manager; mpf_rtp_config_t *rtp_config; mpf_rtp_settings_t *rtp_settings; mpf_engine_t *engine; apt_task_t *task; apt_task_vtable_t *vtable; apt_task_msg_pool_t *msg_pool; agent = apr_palloc(suite->pool,sizeof(mpf_suite_agent_t)); agent->dir_layout = apt_default_dir_layout_create(NULL,suite->pool); engine = mpf_engine_create("MPF-Engine",suite->pool); if(!engine) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Create MPF Engine"); return FALSE; } codec_manager = mpf_engine_codec_manager_create(suite->pool); if(!codec_manager) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Create Codec Manager"); return FALSE; } mpf_engine_codec_manager_register(engine,codec_manager); agent->engine = engine; rtp_config = mpf_rtp_config_alloc(suite->pool); apt_string_set(&rtp_config->ip,"127.0.0.1"); rtp_config->rtp_port_min = 5000; rtp_config->rtp_port_max = 6000; agent->rtp_config = rtp_config; rtp_settings = mpf_rtp_settings_alloc(suite->pool); rtp_settings->ptime = 20; rtp_settings->jb_config.adaptive = 1; rtp_settings->jb_config.time_skew_detection = 1; rtp_settings->jb_config.min_playout_delay = 0; rtp_settings->jb_config.initial_playout_delay = 50; rtp_settings->jb_config.max_playout_delay = 800; mpf_codec_manager_codec_list_load(codec_manager,&rtp_settings->codec_list,"PCMU",suite->pool); agent->rtp_settings = rtp_settings; agent->rtp_termination_factory = mpf_rtp_termination_factory_create(rtp_config,suite->pool); agent->file_termination_factory = mpf_file_termination_factory_create(suite->pool); agent->rx_session = NULL; agent->tx_session = NULL; msg_pool = apt_task_msg_pool_create_dynamic(sizeof(mpf_message_t),suite->pool); apt_log(APT_LOG_MARK,APT_PRIO_NOTICE,"Create Consumer Task"); agent->consumer_task = apt_consumer_task_create(agent,msg_pool,suite->pool); if(!agent->consumer_task) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Create Consumer Task"); return FALSE; } task = apt_consumer_task_base_get(agent->consumer_task); apt_task_name_set(task,"MPF-Tester"); vtable = apt_task_vtable_get(task); if(vtable) { vtable->process_msg = mpf_suite_task_msg_process; vtable->on_start_complete = mpf_suite_on_start_complete; vtable->on_terminate_complete = mpf_suite_on_terminate_complete; } apt_task_add(task,mpf_task_get(engine)); apr_thread_mutex_create(&agent->wait_object_mutex,APR_THREAD_MUTEX_UNNESTED,suite->pool); apr_thread_cond_create(&agent->wait_object,suite->pool); if(apt_task_start(task) == FALSE) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Start Task"); apt_task_destroy(task); return FALSE; } apr_thread_mutex_lock(agent->wait_object_mutex); apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Wait for Task to Complete"); apr_thread_cond_wait(agent->wait_object,agent->wait_object_mutex); apr_thread_mutex_unlock(agent->wait_object_mutex); apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Terminate Task [wait till complete]"); apt_task_terminate(task,TRUE); apt_log(APT_LOG_MARK,APT_PRIO_NOTICE,"Destroy Task"); apt_task_destroy(task); apr_thread_cond_destroy(agent->wait_object); apr_thread_mutex_destroy(agent->wait_object_mutex); return TRUE; }
/** Run MPF test suite */ static apt_bool_t mpf_test_run(apt_test_suite_t *suite, int argc, const char * const *argv) { mpf_suite_engine_t *suite_engine; mpf_codec_manager_t *codec_manager; mpf_rtp_config_t *config; mpf_engine_t *engine; apt_task_t *task; apt_task_vtable_t *vtable; apt_task_msg_pool_t *msg_pool; suite_engine = apr_palloc(suite->pool,sizeof(mpf_suite_engine_t)); engine = mpf_engine_create(suite->pool); if(!engine) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Create MPF Engine"); return FALSE; } codec_manager = mpf_engine_codec_manager_create(suite->pool); if(codec_manager) { mpf_engine_codec_manager_register(engine,codec_manager); } suite_engine->engine = engine; config = mpf_rtp_config_alloc(suite->pool); apt_string_set(&config->ip,"127.0.0.1"); config->rtp_port_min = 5000; config->rtp_port_min = 6000; suite_engine->rtp_termination_factory = mpf_rtp_termination_factory_create(config,suite->pool); suite_engine->file_termination_factory = mpf_file_termination_factory_create(suite->pool); msg_pool = apt_task_msg_pool_create_dynamic(sizeof(mpf_message_t),suite->pool); apt_log(APT_LOG_MARK,APT_PRIO_NOTICE,"Create Consumer Task"); suite_engine->consumer_task = apt_consumer_task_create(suite_engine,msg_pool,suite->pool); if(!suite_engine->consumer_task) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Create Consumer Task"); return FALSE; } task = apt_consumer_task_base_get(suite_engine->consumer_task); vtable = apt_task_vtable_get(task); if(vtable) { vtable->process_msg = mpf_suite_task_msg_process; vtable->on_start_complete = mpf_suite_on_start_complete; vtable->on_terminate_complete = mpf_suite_on_terminate_complete; } apt_task_add(task,mpf_task_get(engine)); apr_thread_mutex_create(&suite_engine->wait_object_mutex,APR_THREAD_MUTEX_UNNESTED,suite->pool); apr_thread_cond_create(&suite_engine->wait_object,suite->pool); apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Start Task"); if(apt_task_start(task) == FALSE) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Start Task"); apt_task_destroy(task); return FALSE; } apr_thread_mutex_lock(suite_engine->wait_object_mutex); apr_thread_cond_wait(suite_engine->wait_object,suite_engine->wait_object_mutex); apr_thread_mutex_unlock(suite_engine->wait_object_mutex); apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Terminate Task [wait till complete]"); apt_task_terminate(task,TRUE); apt_log(APT_LOG_MARK,APT_PRIO_NOTICE,"Destroy Task"); apt_task_destroy(task); apr_thread_cond_destroy(suite_engine->wait_object); apr_thread_mutex_destroy(suite_engine->wait_object_mutex); return TRUE; }