/** Create ASR session */ ASR_CLIENT_DECLARE(asr_session_t*) asr_session_create(asr_engine_t *engine, const char *profile) { mpf_termination_t *termination; mrcp_channel_t *channel; mrcp_session_t *session; const mrcp_app_message_t *app_message; apr_pool_t *pool; asr_session_t *asr_session; mpf_stream_capabilities_t *capabilities; /* create session */ session = mrcp_application_session_create(engine->mrcp_app,profile,NULL); if(!session) { return NULL; } pool = mrcp_application_session_pool_get(session); asr_session = apr_palloc(pool,sizeof(asr_session_t)); mrcp_application_session_object_set(session,asr_session); /* create source stream capabilities */ capabilities = mpf_source_stream_capabilities_create(pool); /* add codec capabilities (Linear PCM) */ mpf_codec_capabilities_add( &capabilities->codecs, MPF_SAMPLE_RATE_8000, "LPCM"); termination = mrcp_application_audio_termination_create( session, /* session, termination belongs to */ &audio_stream_vtable, /* virtual methods table of audio stream */ capabilities, /* capabilities of audio stream */ asr_session); /* object to associate */ channel = mrcp_application_channel_create( session, /* session, channel belongs to */ MRCP_RECOGNIZER_RESOURCE, /* MRCP resource identifier */ termination, /* media termination, used to terminate audio stream */ NULL, /* RTP descriptor, used to create RTP termination (NULL by default) */ asr_session); /* object to associate */ if(!channel) { mrcp_application_session_destroy(session); return NULL; } asr_session->engine = engine; asr_session->mrcp_session = session; asr_session->mrcp_channel = channel; asr_session->recog_complete = NULL; asr_session->input_mode = INPUT_MODE_NONE; asr_session->streaming = FALSE; asr_session->audio_in = NULL; asr_session->media_buffer = NULL; asr_session->mutex = NULL; asr_session->wait_object = NULL; asr_session->app_message = NULL; /* Create cond wait object and mutex */ apr_thread_mutex_create(&asr_session->mutex,APR_THREAD_MUTEX_DEFAULT,pool); apr_thread_cond_create(&asr_session->wait_object,pool); /* Create media buffer */ asr_session->media_buffer = mpf_frame_buffer_create(160,20,pool); /* Send add channel request and wait for the response */ apr_thread_mutex_lock(asr_session->mutex); app_message = NULL; if(mrcp_application_channel_add(asr_session->mrcp_session,asr_session->mrcp_channel) == TRUE) { apr_thread_cond_wait(asr_session->wait_object,asr_session->mutex); app_message = asr_session->app_message; asr_session->app_message = NULL; } apr_thread_mutex_unlock(asr_session->mutex); if(sig_response_check(app_message) == FALSE) { asr_session_destroy_ex(asr_session,TRUE); return NULL; } return asr_session; }
/** \brief Set up the speech structure within the engine */ static int uni_recog_create_internal(struct ast_speech *speech, ast_format_compat *format) { uni_speech_t *uni_speech; mrcp_session_t *session; apr_pool_t *pool; const mpf_codec_descriptor_t *descriptor; /* Create session instance */ session = mrcp_application_session_create(uni_engine.application,uni_engine.profile,speech); if(!session) { ast_log(LOG_ERROR, "Failed to create session\n"); return -1; } pool = mrcp_application_session_pool_get(session); uni_speech = apr_palloc(pool,sizeof(uni_speech_t)); uni_speech->session = session; uni_speech->channel = NULL; uni_speech->wait_object = NULL; uni_speech->mutex = NULL; uni_speech->media_buffer = NULL; uni_speech->active_grammars = apr_hash_make(pool); uni_speech->is_sm_request = FALSE; uni_speech->is_inprogress = FALSE; uni_speech->sm_request = 0; uni_speech->sm_response = MRCP_SIG_STATUS_CODE_SUCCESS; uni_speech->mrcp_request = NULL; uni_speech->mrcp_response = NULL; uni_speech->mrcp_event = NULL; uni_speech->speech_base = speech; speech->data = uni_speech; /* Create cond wait object and mutex */ apr_thread_mutex_create(&uni_speech->mutex,APR_THREAD_MUTEX_DEFAULT,pool); apr_thread_cond_create(&uni_speech->wait_object,pool); /* Create recognition channel instance */ if(uni_recog_channel_create(uni_speech,format) != TRUE) { ast_log(LOG_ERROR, "Failed to create channel\n"); uni_recog_cleanup(uni_speech); return -1; } /* Send add channel request and wait for response */ if(uni_recog_sm_request_send(uni_speech,MRCP_SIG_COMMAND_CHANNEL_ADD) != TRUE) { ast_log(LOG_WARNING, "Failed to send add channel request\n"); uni_recog_cleanup(uni_speech); return -1; } /* Check received response */ if(uni_speech->sm_response != MRCP_SIG_STATUS_CODE_SUCCESS) { ast_log(LOG_WARNING, "Failed to add channel\n"); uni_recog_sm_request_send(uni_speech,MRCP_SIG_COMMAND_SESSION_TERMINATE); uni_recog_cleanup(uni_speech); return -1; } descriptor = mrcp_application_source_descriptor_get(uni_speech->channel); if(descriptor) { mpf_frame_buffer_t *media_buffer; apr_size_t frame_size = mpf_codec_linear_frame_size_calculate(descriptor->sampling_rate,descriptor->channel_count); /* Create media buffer */ ast_log(LOG_DEBUG, "Create media buffer frame_size:%"APR_SIZE_T_FMT"\n",frame_size); media_buffer = mpf_frame_buffer_create(frame_size,20,pool); uni_speech->media_buffer = media_buffer; } if(!uni_speech->media_buffer) { ast_log(LOG_WARNING, "Failed to create media buffer\n"); uni_recog_sm_request_send(uni_speech,MRCP_SIG_COMMAND_SESSION_TERMINATE); uni_recog_cleanup(uni_speech); return -1; } ast_log(LOG_NOTICE, "Created speech instance '%s'\n",uni_speech_id_get(uni_speech)); /* Set properties for session */ uni_recog_properties_set(uni_speech); /* Preload grammars */ uni_recog_grammars_preload(uni_speech); return 0; }