/** Initiate recognition based on specified grammar and input file */ ASR_CLIENT_DECLARE(apt_bool_t) asr_session_file_recognize( asr_session_t *asr_session, const char *grammar_file, const char *input_file) { const mrcp_app_message_t *app_message; mrcp_message_t *mrcp_message; app_message = NULL; mrcp_message = define_grammar_message_create(asr_session,grammar_file); if(!mrcp_message) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Create DEFINE-GRAMMAR Request"); return FALSE; } /* Send DEFINE-GRAMMAR request and wait for the response */ apr_thread_mutex_lock(asr_session->mutex); if(mrcp_application_message_send(asr_session->mrcp_session,asr_session->mrcp_channel,mrcp_message) == 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(mrcp_response_check(app_message,MRCP_REQUEST_STATE_COMPLETE) == FALSE) { return FALSE; } /* Reset prev recog result (if any) */ asr_session->recog_complete = NULL; app_message = NULL; mrcp_message = recognize_message_create(asr_session); if(!mrcp_message) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Create RECOGNIZE Request"); return FALSE; } /* Send RECOGNIZE request and wait for the response */ apr_thread_mutex_lock(asr_session->mutex); if(mrcp_application_message_send(asr_session->mrcp_session,asr_session->mrcp_channel,mrcp_message) == 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(mrcp_response_check(app_message,MRCP_REQUEST_STATE_INPROGRESS) == FALSE) { return FALSE; } /* Open input file and start streaming */ asr_session->input_mode = INPUT_MODE_FILE; if(asr_input_file_open(asr_session,input_file) == FALSE) { return FALSE; } asr_session->streaming = TRUE; return TRUE; }
/** Initiate recognition based on specified grammar and input stream */ ASR_CLIENT_DECLARE(const char*) asr_session_stream_recognize( asr_session_t *asr_session, const char *grammar_file) { const mrcp_app_message_t *app_message; mrcp_message_t *mrcp_message; app_message = NULL; mrcp_message = define_grammar_message_create(asr_session,grammar_file); if(!mrcp_message) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Create DEFINE-GRAMMAR Request"); return NULL; } /* Send DEFINE-GRAMMAR request and wait for the response */ apr_thread_mutex_lock(asr_session->mutex); if(mrcp_application_message_send(asr_session->mrcp_session,asr_session->mrcp_channel,mrcp_message) == 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(mrcp_response_check(app_message,MRCP_REQUEST_STATE_COMPLETE) == FALSE) { return NULL; } /* Reset prev recog result (if any) */ asr_session->recog_complete = NULL; app_message = NULL; mrcp_message = recognize_message_create(asr_session); if(!mrcp_message) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Create RECOGNIZE Request"); return NULL; } /* Send RECOGNIZE request and wait for the response */ apr_thread_mutex_lock(asr_session->mutex); if(mrcp_application_message_send(asr_session->mrcp_session,asr_session->mrcp_channel,mrcp_message) == 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(mrcp_response_check(app_message,MRCP_REQUEST_STATE_INPROGRESS) == FALSE) { return NULL; } /* Reset media buffer */ mpf_frame_buffer_restart(asr_session->media_buffer); /* Set input mode and start streaming */ asr_session->input_mode = INPUT_MODE_STREAM; asr_session->streaming = TRUE; /* Wait for events either START-OF-INPUT or RECOGNITION-COMPLETE */ do { apr_thread_mutex_lock(asr_session->mutex); app_message = NULL; if(apr_thread_cond_timedwait(asr_session->wait_object,asr_session->mutex, 60 * 1000000) != APR_SUCCESS) { apr_thread_mutex_unlock(asr_session->mutex); return NULL; } app_message = asr_session->app_message; asr_session->app_message = NULL; apr_thread_mutex_unlock(asr_session->mutex); mrcp_message = mrcp_event_get(app_message); if(mrcp_message && mrcp_message->start_line.method_id == RECOGNIZER_RECOGNITION_COMPLETE) { asr_session->recog_complete = mrcp_message; } } while(!asr_session->recog_complete); /* Get results */ return nlsml_input_get(asr_session->recog_complete); }