static mrcp_engine_channel_t* mrcp_server_engine_channel_create(
								mrcp_server_session_t *session,
								mrcp_channel_t *channel, 
								const apt_str_t *resource_name)
{
	mrcp_engine_t *engine = apr_hash_get(
									session->profile->engine_table,
									resource_name->buf,
									resource_name->length);
	if(!engine) {
		apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Find MRCP Engine "APT_NAMESID_FMT" [%s]",
			MRCP_SESSION_NAMESID(session),
			resource_name->buf);
		return NULL;
	}

	channel->state_machine = engine->create_state_machine(
						channel,
						mrcp_session_version_get(session),
						channel->pool);
	if(channel->state_machine) {
		channel->state_machine->on_dispatch = state_machine_on_message_dispatch;
		channel->state_machine->on_deactivate = state_machine_on_deactivate;
	}

	return mrcp_engine_channel_virtual_create(engine,mrcp_session_version_get(session),session->base.pool);
}
static apt_bool_t mrcp_client_resource_discover(mrcp_client_session_t *session)
{
	mrcp_session_descriptor_t *descriptor = NULL;
	
	apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Discover Resources "APT_PTR_FMT, MRCP_SESSION_PTR(&session->base));
	session->answer = NULL;
	mrcp_client_session_state_set(session,SESSION_STATE_DISCOVERING);

	if(mrcp_session_version_get(session) == MRCP_VERSION_1) {
		mrcp_resource_t *resource;
		mrcp_resource_id i;

		for(i=0; i<MRCP_RESOURCE_TYPE_COUNT; i++) {
			resource = mrcp_resource_get(session->profile->resource_factory,i);
			if(!resource) continue;
		
			descriptor = mrcp_session_descriptor_create(session->base.pool);
			apt_string_copy(&descriptor->resource_name,&resource->name,session->base.pool);
			if(mrcp_session_discover_request(&session->base,descriptor) == TRUE) {
				mrcp_client_session_subrequest_add(session);
			}
		}
	}
	else {
		if(mrcp_session_discover_request(&session->base,descriptor) == TRUE) {
			mrcp_client_session_subrequest_add(session);
		}
	}

	if(session->subrequest_count == 0) {
		session->status = MRCP_SIG_STATUS_CODE_FAILURE;
		mrcp_app_sig_response_raise(session,TRUE);
	}
	return TRUE;
}
static apt_bool_t mrcp_server_session_offer_process(mrcp_server_session_t *session, mrcp_session_descriptor_t *descriptor)
{
	if(!session->context) {
		/* initial offer received, generate session id and add to session's table */
		if(!session->base.id.length) {
			apt_unique_id_generate(&session->base.id,MRCP_SESSION_ID_HEX_STRING_LENGTH,session->base.pool);
		}
		mrcp_server_session_add(session);

		session->context = mpf_engine_context_create(session->profile->media_engine,session,5,session->base.pool);
	}
	apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Receive Offer "APT_SID_FMT" [c:%d a:%d v:%d]",
		MRCP_SESSION_SID(&session->base),
		descriptor->control_media_arr->nelts,
		descriptor->audio_media_arr->nelts,
		descriptor->video_media_arr->nelts);

	/* store received offer */
	session->offer = descriptor;
	session->answer = mrcp_session_answer_create(descriptor,session->base.pool);

	mrcp_server_session_state_set(session,SESSION_STATE_GENERATING_ANSWER);

	/* first, reset/destroy existing associations and topology */
	if(mpf_engine_topology_message_add(
				session->profile->media_engine,
				MPF_RESET_ASSOCIATIONS,session->context,
				&session->mpf_task_msg) == TRUE){
		mrcp_server_session_subrequest_add(session);
	}

	if(mrcp_session_version_get(session) == MRCP_VERSION_1) {
		if(mrcp_server_resource_offer_process(session,descriptor) == TRUE) {
			mrcp_server_av_media_offer_process(session,descriptor);
		}
		else {
			session->answer->resource_state = FALSE;
		}
	}
	else {
		mrcp_server_control_media_offer_process(session,descriptor);
		mrcp_server_av_media_offer_process(session,descriptor);
	}

	/* apply topology based on assigned associations */
	if(mpf_engine_topology_message_add(
				session->profile->media_engine,
				MPF_APPLY_TOPOLOGY,session->context,
				&session->mpf_task_msg) == TRUE) {
		mrcp_server_session_subrequest_add(session);
	}
	mpf_engine_message_send(session->profile->media_engine,&session->mpf_task_msg);

	if(!session->subrequest_count) {
		/* send answer to client */
		mrcp_server_session_answer_send(session);
	}
	return TRUE;
}
static mrcp_channel_t* mrcp_server_channel_create(mrcp_server_session_t *session, const apt_str_t *resource_name, apr_size_t id, apr_array_header_t *cmid_arr)
{
	mrcp_channel_t *channel;
	apr_pool_t *pool = session->base.pool;

	channel = apr_palloc(pool,sizeof(mrcp_channel_t));
	channel->pool = pool;
	channel->session = &session->base;
	channel->resource = NULL;
	channel->control_channel = NULL;
	channel->state_machine = NULL;
	channel->engine_channel = NULL;
	channel->id = id;
	channel->cmid_arr = cmid_arr;
	channel->waiting_for_channel = FALSE;
	channel->waiting_for_termination = FALSE;

	if(resource_name && resource_name->buf) {
		mrcp_resource_t *resource;
		mrcp_engine_channel_t *engine_channel;
		resource = mrcp_resource_find(session->profile->resource_factory,resource_name);
		if(resource) {
			channel->resource = resource;
			if(mrcp_session_version_get(session) == MRCP_VERSION_2) {
				channel->control_channel = mrcp_server_control_channel_create(
									session->profile->connection_agent,
									channel,
									pool);
			}
			engine_channel = mrcp_server_engine_channel_create(session,channel,resource_name);
			if(engine_channel) {
				engine_channel->id = session->base.id;
				engine_channel->event_obj = channel;
				engine_channel->event_vtable = &engine_channel_vtable;
				channel->engine_channel = engine_channel;
			}
			else {
				apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Create Engine Channel "APT_NAMESID_FMT" [%s]",
					MRCP_SESSION_NAMESID(session),
					resource_name->buf);
				session->answer->status = MRCP_SESSION_STATUS_UNACCEPTABLE_RESOURCE;
			}
		}
		else {
			apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"No Such Resource "APT_NAMESID_FMT" [%s]",
				MRCP_SESSION_NAMESID(session),
				resource_name->buf);
			session->answer->status = MRCP_SESSION_STATUS_NO_SUCH_RESOURCE;
		}
	}
	else {
		apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Invalid Resource Identifier "APT_NAMESID_FMT,
			MRCP_SESSION_NAMESID(session));
		session->answer->status = MRCP_SESSION_STATUS_NO_SUCH_RESOURCE;
	}

	return channel;
}
static mrcp_engine_channel_t* mrcp_server_engine_channel_create(mrcp_server_session_t *session, const apt_str_t *resource_name)
{
	mrcp_engine_t *engine = apr_hash_get(
									session->profile->engine_table,
									resource_name->buf,
									resource_name->length);
	if(!engine) {
		apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Find MRCP Engine [%s]",resource_name->buf);
		return NULL;
	}

	return mrcp_engine_channel_virtual_create(engine,mrcp_session_version_get(session),session->base.pool);
}
static apt_bool_t mrcp_server_engine_channels_update(mrcp_server_session_t *session)
{
	mrcp_channel_t *channel;
	mrcp_session_descriptor_t *descriptor = session->offer;
	if(!descriptor) {
		return FALSE;
	}
	
	mrcp_server_session_state_set(session,SESSION_STATE_INITIALIZING);

	if(mrcp_session_version_get(session) == MRCP_VERSION_1) {
		if(session->offer) {
			channel = mrcp_server_channel_find(session,&descriptor->resource_name);
			if(channel && channel->engine_channel) {
				/* open engine channel */
				if(mrcp_engine_channel_virtual_open(channel->engine_channel) == TRUE) {
					mrcp_server_session_subrequest_add(session);
				}
			}
		}
	}
	else {
		int i;
		mrcp_control_descriptor_t *control_descriptor;
		for(i=0; i<session->channels->nelts; i++) {
			channel = APR_ARRAY_IDX(session->channels,i,mrcp_channel_t*);
			if(!channel || !channel->engine_channel) continue;

			control_descriptor = mrcp_session_control_media_get(descriptor,i);
			if(!control_descriptor) continue;

			if(control_descriptor->port) {
				/* open engine channel */
				if(mrcp_engine_channel_virtual_open(channel->engine_channel) == TRUE) {
					mrcp_server_session_subrequest_add(session);
				}
			}
			else {
				/* close engine channel */
				if(mrcp_engine_channel_virtual_close(channel->engine_channel) == TRUE) {
					mrcp_server_session_subrequest_add(session);
				}
			}
		}
	}

	if(!session->subrequest_count) {
		mrcp_server_session_answer_send(session);
	}
	return TRUE;
}
apt_bool_t mrcp_client_session_discover_response_process(mrcp_client_session_t *session, mrcp_session_descriptor_t *descriptor)
{
	apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Resources Discovered %s", session->base.name);
	if(!session->active_request) {
		return FALSE;
	}

	if(!descriptor) {
		/* raise app response */
		session->status = MRCP_SIG_STATUS_CODE_FAILURE;
		return mrcp_app_sig_response_raise(session,TRUE);
	}

	if(mrcp_session_version_get(session) == MRCP_VERSION_1) {
		if(descriptor->resource_state == TRUE) {
			mrcp_control_descriptor_t *control_media;
			if(!session->answer) {
				session->answer = descriptor;
			}
			control_media = mrcp_control_descriptor_create(session->base.pool);
			control_media->id = mrcp_session_control_media_add(session->answer,control_media);
			control_media->resource_name = descriptor->resource_name;
		}
	}
	else {
		session->answer = descriptor;
	}

	if(mrcp_client_session_subrequest_remove(session) == TRUE) {
		mrcp_app_message_t *response;
		response = mrcp_client_app_response_create(session->active_request,MRCP_SIG_STATUS_CODE_SUCCESS,session->base.pool);
		response->descriptor = session->answer;
		session->answer = NULL;
		apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Raise App Resource Discovery Response %s", session->base.name);
		session->application->handler(response);

		session->active_request = apt_list_pop_front(session->request_queue);
		if(session->active_request) {
			mrcp_app_request_dispatch(session,session->active_request);
		}
	}
	return TRUE;
}
static apt_bool_t mrcp_client_channel_add(mrcp_client_session_t *session, mrcp_channel_t *channel)
{
	mpf_rtp_termination_descriptor_t *rtp_descriptor = NULL;
	rtp_termination_slot_t *slot;
	apr_pool_t *pool = session->base.pool;
	mrcp_profile_t *profile = session->profile;
	if(mrcp_client_channel_find(session,channel,NULL) == TRUE) {
		/* update */
		return mrcp_client_channel_modify(session,channel,TRUE);
	}

	if(!session->offer) {
		session->offer = mrcp_session_descriptor_create(pool);
	}
	
	mrcp_client_session_state_set(session,SESSION_STATE_GENERATING_OFFER);

	if(mrcp_session_version_get(session) == MRCP_VERSION_1) {
		session->offer->resource_name = channel->resource->name;
		session->offer->resource_state = TRUE;
	}
	else {
		mrcp_control_descriptor_t *control_media;
		if(!channel->control_channel) {
			channel->control_channel = mrcp_client_control_channel_create(profile->connection_agent,channel,pool);
			mrcp_client_control_channel_log_obj_set(channel->control_channel,session->base.log_obj);
		}
		control_media = mrcp_control_offer_create(pool);
		control_media->id = mrcp_session_control_media_add(session->offer,control_media);
		mrcp_cmid_add(control_media->cmid_arr,session->offer->control_media_arr->nelts);
		control_media->resource_name = channel->resource->name;
		if(mrcp_client_control_channel_add(channel->control_channel,control_media) == TRUE) {
			channel->waiting_for_channel = TRUE;
			mrcp_client_session_subrequest_add(session);
		}
	}

	apt_obj_log(APT_LOG_MARK,APT_PRIO_NOTICE,session->base.log_obj,"Add Control Channel "APT_NAMESIDRES_FMT,
					MRCP_SESSION_NAMESID(session),
					channel->resource->name.buf);
	/* add control channel */
	APR_ARRAY_PUSH(session->channels,mrcp_channel_t*) = channel;

	/* add rtp termination slot */
	slot = apr_array_push(session->terminations);
	slot->waiting = FALSE;
	slot->termination = NULL;
	slot->descriptor = NULL;
	slot->channel = channel;
	slot->id = 0;

	if(channel->termination) {
		/* media termination mode */
		mpf_termination_t *termination;
		mpf_audio_stream_t *audio_stream;

		if(!session->context) {
			/* create media context first */
			session->context = mpf_engine_context_create(
				profile->media_engine,
				session->base.name,
				session,5,pool);
		}
		apt_obj_log(APT_LOG_MARK,APT_PRIO_DEBUG,session->base.log_obj,"Add Media Termination "APT_NAMESIDRES_FMT,
			MRCP_SESSION_NAMESID(session),
			mpf_termination_name_get(channel->termination));
		if(mpf_engine_termination_message_add(
				profile->media_engine,
				MPF_ADD_TERMINATION,session->context,channel->termination,NULL,
				&session->mpf_task_msg) == TRUE) {
			channel->waiting_for_termination = TRUE;
			mrcp_client_session_subrequest_add(session);
		}

		/* initialize rtp descriptor */
		rtp_descriptor = apr_palloc(pool,sizeof(mpf_rtp_termination_descriptor_t));
		mpf_rtp_termination_descriptor_init(rtp_descriptor);
		rtp_descriptor->audio.settings = profile->rtp_settings;
		audio_stream = mpf_termination_audio_stream_get(channel->termination);
		if(audio_stream) {
			mpf_rtp_media_descriptor_t *media;
			media = apr_palloc(pool,sizeof(mpf_rtp_media_descriptor_t));
			mpf_rtp_media_descriptor_init(media);
			media->state = MPF_MEDIA_ENABLED;
			media->direction = mpf_stream_reverse_direction_get(audio_stream->direction);
			rtp_descriptor->audio.local = media;
			if(audio_stream->capabilities) {
				rtp_descriptor->audio.capabilities = mpf_stream_capabilities_clone(audio_stream->capabilities,pool);
				rtp_descriptor->audio.capabilities->direction = media->direction;
			}
		}

		/* create rtp termination */
		termination = mpf_termination_create(profile->rtp_termination_factory,session,pool);
		slot->termination = termination;
		apt_obj_log(APT_LOG_MARK,APT_PRIO_DEBUG,session->base.log_obj,"Add Media Termination "APT_NAMESIDRES_FMT, 
			MRCP_SESSION_NAMESID(session),
			mpf_termination_name_get(termination));

		/* send add termination request (add to media context) */
		if(mpf_engine_termination_message_add(
				profile->media_engine,
				MPF_ADD_TERMINATION,session->context,termination,rtp_descriptor,
				&session->mpf_task_msg) == TRUE) {
			slot->waiting = TRUE;
			mrcp_client_session_subrequest_add(session);
		}
		mpf_engine_message_send(profile->media_engine,&session->mpf_task_msg);
	}
	else {
		/* bypass media mode */
		if(channel->rtp_termination_slot) {
			rtp_descriptor = channel->rtp_termination_slot->descriptor;
			if(rtp_descriptor) {
				if(rtp_descriptor->audio.local) {
					session->offer->ip = rtp_descriptor->audio.local->ip;
					session->offer->ext_ip = rtp_descriptor->audio.local->ext_ip;
					rtp_descriptor->audio.local->id = mrcp_session_audio_media_add(session->offer,rtp_descriptor->audio.local);
					rtp_descriptor->audio.local->mid = session->offer->audio_media_arr->nelts;
					slot->id = session->offer->audio_media_arr->nelts - 1;
				}
			}
		}
	}

	slot->descriptor = rtp_descriptor;
	channel->rtp_termination_slot = slot;

	if(!session->subrequest_count) {
		/* send offer to server */
		mrcp_client_session_offer_send(session);
	}
	return TRUE;
}
apt_bool_t mrcp_client_session_answer_process(mrcp_client_session_t *session, mrcp_session_descriptor_t *descriptor)
{
	if(!session->offer) {
		return FALSE;
	}
	if(!descriptor) {
		apt_obj_log(APT_LOG_MARK,APT_PRIO_INFO,session->base.log_obj,"Receive Answer "APT_NAMESID_FMT" [null descriptor]",
			MRCP_SESSION_NAMESID(session));
		session->status = MRCP_SIG_STATUS_CODE_FAILURE;
		/* raise app response */
		return mrcp_app_sig_response_raise(session,TRUE);
	}

	apt_obj_log(APT_LOG_MARK,APT_PRIO_INFO,session->base.log_obj,"Receive Answer "APT_NAMESID_FMT" [c:%d a:%d v:%d] Status %d",
		MRCP_SESSION_NAMESID(session),
		descriptor->control_media_arr->nelts,
		descriptor->audio_media_arr->nelts,
		descriptor->video_media_arr->nelts,
		descriptor->response_code);

	if(descriptor->response_code >=200 && descriptor->response_code < 300) {
		mrcp_client_session_state_set(session,SESSION_STATE_PROCESSING_ANSWER);
		if(session->context) {
			/* first, reset/destroy existing associations and topology */
			if(mpf_engine_topology_message_add(
						session->profile->media_engine,
						MPF_RESET_ASSOCIATIONS,session->context,
						&session->mpf_task_msg) == TRUE){
				mrcp_client_session_subrequest_add(session);
			}
		}

		if(mrcp_session_version_get(session) == MRCP_VERSION_1) {
			if(mrcp_client_resource_answer_process(session,descriptor) != TRUE) {
				session->status = MRCP_SIG_STATUS_CODE_FAILURE;
			}
		}
		else {
			mrcp_client_control_media_answer_process(session,descriptor);
			mrcp_client_av_media_answer_process(session,descriptor);
		}

		if(session->context) {
			/* apply topology based on assigned associations */
			if(mpf_engine_topology_message_add(
						session->profile->media_engine,
						MPF_APPLY_TOPOLOGY,session->context,
						&session->mpf_task_msg) == TRUE) {
				mrcp_client_session_subrequest_add(session);
			}

			mpf_engine_message_send(session->profile->media_engine,&session->mpf_task_msg);
		}
	}
	else {
		session->status = MRCP_SIG_STATUS_CODE_TERMINATE;
	}

	/* store received answer */
	session->answer = descriptor;

	if(!session->subrequest_count) {
		/* raise app response */
		mrcp_app_sig_response_raise(session,TRUE);
	}

	return TRUE;
}