/** Run demo synthesizer scenario */ static apt_bool_t synth_application_run(demo_application_t *demo_application, const char *profile) { mrcp_channel_t *channel; /* create session */ mrcp_session_t *session = mrcp_application_session_create(demo_application->application,profile,NULL); if(!session) { return FALSE; } /* create channel and associate all the required data */ channel = synth_application_channel_create(session); if(!channel) { mrcp_application_session_destroy(session); return FALSE; } /* add channel to session (send asynchronous request) */ if(mrcp_application_channel_add(session,channel) != TRUE) { /* session and channel are still not referenced and both are allocated from session pool and will be freed with session destroy call */ mrcp_application_session_destroy(session); return FALSE; } return TRUE; }
/** Destroy ASR session */ static apt_bool_t asr_session_destroy_ex(asr_session_t *asr_session, apt_bool_t terminate) { if(terminate == TRUE) { apr_thread_mutex_lock(asr_session->mutex); if(mrcp_application_session_terminate(asr_session->mrcp_session) == TRUE) { apr_thread_cond_wait(asr_session->wait_object,asr_session->mutex); /* the response must be checked to be the valid one */ } apr_thread_mutex_unlock(asr_session->mutex); } if(asr_session->audio_in) { fclose(asr_session->audio_in); asr_session->audio_in = NULL; } if(asr_session->mutex) { apr_thread_mutex_destroy(asr_session->mutex); asr_session->mutex = NULL; } if(asr_session->wait_object) { apr_thread_cond_destroy(asr_session->wait_object); asr_session->wait_object = NULL; } if(asr_session->media_buffer) { mpf_frame_buffer_destroy(asr_session->media_buffer); asr_session->media_buffer = NULL; } return mrcp_application_session_destroy(asr_session->mrcp_session); }
/** Handle the responses sent to session terminate requests */ static apt_bool_t synth_application_on_session_terminate(mrcp_application_t *application, mrcp_session_t *session, mrcp_sig_status_code_e status) { /* received response to session termination request, now it's safe to destroy no more referenced session */ mrcp_application_session_destroy(session); return TRUE; }
static apt_bool_t OnSessionTerminate(mrcp_application_t* application, mrcp_session_t* session, mrcp_sig_status_code_e status) { (void) application; printf("Session terminted with code %d\n", status); mrcp_application_session_destroy(session); return TRUE; }
static apt_bool_t OnTerminateEvent(mrcp_application_t* application, mrcp_session_t* session, mrcp_channel_t* channel) { (void) application; (void) channel; puts("Session terminted unexpectedly"); mrcp_application_session_destroy(session); return TRUE; }
bool UmcSession::DestroyMrcpSession() { if(!m_pMrcpSession) return false; mrcp_application_session_destroy(m_pMrcpSession); m_pMrcpSession = NULL; return true; }
/** Run demo resource discover scenario */ static apt_bool_t discover_application_run(demo_application_t *demo_application, const char *profile) { /* create session */ mrcp_session_t *session = mrcp_application_session_create(demo_application->application,profile,NULL); if(!session) { return FALSE; } /* send resource discover request */ if(mrcp_application_resource_discover(session) != TRUE) { mrcp_application_session_destroy(session); return FALSE; } return TRUE; }
/* Handle the UniMRCP responses sent to session terminate requests. */ static apt_bool_t speech_on_session_terminate(mrcp_application_t *application, mrcp_session_t *session, mrcp_sig_status_code_e status) { speech_channel_t *schannel = get_speech_channel(session); if (!schannel) { ast_log(LOG_ERROR, "speech_on_session_terminate: unknown channel error!\n"); return FALSE; } ast_log(LOG_DEBUG, "(%s) speech_on_session_terminate\n", schannel->name); ast_log(LOG_DEBUG, "(%s) Destroying MRCP session\n", schannel->name); if (!mrcp_application_session_destroy(session)) ast_log(LOG_WARNING, "(%s) Unable to destroy application session\n", schannel->name); speech_channel_set_state(schannel, SPEECH_CHANNEL_CLOSED); return TRUE; }
/*! \brief Cleanup already allocated data */ static void uni_recog_cleanup(uni_speech_t *uni_speech) { if(uni_speech->speech_base) { uni_speech->speech_base->data = NULL; } if(uni_speech->mutex) { apr_thread_mutex_destroy(uni_speech->mutex); uni_speech->mutex = NULL; } if(uni_speech->wait_object) { apr_thread_cond_destroy(uni_speech->wait_object); uni_speech->wait_object = NULL; } if(uni_speech->media_buffer) { mpf_frame_buffer_destroy(uni_speech->media_buffer); uni_speech->media_buffer = NULL; } mrcp_application_session_destroy(uni_speech->session); }
/*! \brief Cleanup already allocated data */ static void uni_recog_cleanup(uni_speech_t *uni_speech) { if(uni_speech->speech_base) { uni_speech->speech_base->data = NULL; } if(uni_speech->mutex) { apr_thread_mutex_destroy(uni_speech->mutex); uni_speech->mutex = NULL; } if(uni_speech->wait_object) { apr_thread_cond_destroy(uni_speech->wait_object); uni_speech->wait_object = NULL; } if(uni_speech->media_buffer) { mpf_frame_buffer_destroy(uni_speech->media_buffer); uni_speech->media_buffer = NULL; } if(mrcp_application_session_destroy(uni_speech->session) != TRUE) { ast_log(LOG_WARNING, "(%s) Failed to destroy application session\n",uni_speech->name); } }
/* Handle the UniMRCP responses sent to session terminate requests. */ static apt_bool_t speech_on_session_terminate(mrcp_application_t *application, mrcp_session_t *session, mrcp_sig_status_code_e status) { speech_channel_t *schannel; if (session != NULL) schannel = (speech_channel_t *)mrcp_application_session_object_get(session); else schannel = NULL; ast_log(LOG_DEBUG, "(%s) speech_on_session_terminate\n", schannel->name); if (schannel != NULL) { ast_log(LOG_DEBUG, "(%s) Destroying MRCP session\n", schannel->name); if (!mrcp_application_session_destroy(session)) ast_log(LOG_WARNING, "(%s) Unable to destroy application session\n", schannel->name); speech_channel_set_state(schannel, SPEECH_CHANNEL_CLOSED); } else ast_log(LOG_ERROR, "(unknown) channel error!\n"); return TRUE; }
/** 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; }
/* Open the speech channel. */ int speech_channel_open(speech_channel_t *schannel, ast_mrcp_profile_t *profile) { int status = 0; mpf_termination_t *termination = NULL; mrcp_resource_type_e resource_type; if ((schannel == NULL) || (profile == NULL)) return -1; if (schannel->mutex != NULL) apr_thread_mutex_lock(schannel->mutex); /* Make sure we can open channel. */ if (schannel->state != SPEECH_CHANNEL_CLOSED) { if (schannel->mutex != NULL) apr_thread_mutex_unlock(schannel->mutex); return -1; } schannel->profile = profile; /* Create MRCP session. */ if ((schannel->unimrcp_session = mrcp_application_session_create(schannel->application->app, profile->name, schannel)) == NULL) { /* Profile doesn't exist? */ ast_log(LOG_ERROR, "(%s) Unable to create session with %s\n", schannel->name, profile->name); if (schannel->mutex != NULL) apr_thread_mutex_unlock(schannel->mutex); return 2; } /* Set session name for logging purposes. */ mrcp_application_session_name_set(schannel->unimrcp_session, schannel->name); /* Create audio termination and add to channel. */ if ((termination = speech_channel_create_mpf_termination(schannel)) == NULL) { ast_log(LOG_ERROR, "(%s) Unable to create termination with %s\n", schannel->name, profile->name); if (!mrcp_application_session_destroy(schannel->unimrcp_session)) ast_log(LOG_WARNING, "(%s) Unable to destroy application session for %s\n", schannel->name, profile->name); if (schannel->mutex != NULL) apr_thread_mutex_unlock(schannel->mutex); return -1; } if (schannel->type == SPEECH_CHANNEL_SYNTHESIZER) resource_type = MRCP_SYNTHESIZER_RESOURCE; else resource_type = MRCP_RECOGNIZER_RESOURCE; if ((schannel->unimrcp_channel = mrcp_application_channel_create(schannel->unimrcp_session, resource_type, termination, NULL, schannel)) == NULL) { ast_log(LOG_ERROR, "(%s) Unable to create channel with %s\n", schannel->name, profile->name); if (!mrcp_application_session_destroy(schannel->unimrcp_session)) ast_log(LOG_WARNING, "(%s) Unable to destroy application session for %s\n", schannel->name, profile->name); if (schannel->mutex != NULL) apr_thread_mutex_unlock(schannel->mutex); return -1; } /* Add channel to session. This establishes the connection to the MRCP server. */ if (mrcp_application_channel_add(schannel->unimrcp_session, schannel->unimrcp_channel) != TRUE) { ast_log(LOG_ERROR, "(%s) Unable to add channel to session with %s\n", schannel->name, profile->name); if (!mrcp_application_session_destroy(schannel->unimrcp_session)) ast_log(LOG_WARNING, "(%s) Unable to destroy application session for %s\n", schannel->name, profile->name); if (schannel->mutex != NULL) apr_thread_mutex_unlock(schannel->mutex); return -1; } /* Wait for channel to be ready. */ while ((schannel->mutex != NULL) && (schannel->cond != NULL) && (schannel->state == SPEECH_CHANNEL_CLOSED)) apr_thread_cond_timedwait(schannel->cond, schannel->mutex, SPEECH_CHANNEL_TIMEOUT_USEC); if (schannel->state == SPEECH_CHANNEL_READY) { ast_log(LOG_DEBUG, "(%s) channel is ready\n", schannel->name); } else if (schannel->state == SPEECH_CHANNEL_CLOSED) { ast_log(LOG_ERROR, "(%s) Timed out waiting for channel to be ready\n", schannel->name); /* Can't retry. */ status = -1; } else if (schannel->state == SPEECH_CHANNEL_ERROR) { /* Wait for session to be cleaned up. */ if (schannel->cond != NULL) apr_thread_cond_timedwait(schannel->cond, schannel->mutex, SPEECH_CHANNEL_TIMEOUT_USEC); if (schannel->state != SPEECH_CHANNEL_CLOSED) { /* Major issue. Can't retry. */ status = -1; } else { /* Failed to open profile, retry is allowed. */ status = 2; } } if (schannel->type == SPEECH_CHANNEL_RECOGNIZER) { recognizer_data_t *r = (recognizer_data_t *)apr_palloc(schannel->pool, sizeof(recognizer_data_t)); if (r != NULL) { schannel->data = r; memset(r, 0, sizeof(recognizer_data_t)); if ((r->grammars = apr_hash_make(schannel->pool)) == NULL) { ast_log(LOG_ERROR, "Unable to allocate hash for grammars\n"); status = -1; } } else { ast_log(LOG_ERROR, "Unable to allocate recognizer data structure\n"); status = -1; } } if (schannel->mutex != NULL) apr_thread_mutex_unlock(schannel->mutex); return status; }