static apt_bool_t mrcp_server_agent_channel_add(mrcp_connection_agent_t *agent, mrcp_control_channel_t *channel, mrcp_control_descriptor_t *offer) { mrcp_control_descriptor_t *answer = mrcp_control_answer_create(offer,channel->pool); apt_id_resource_generate(&offer->session_id,&offer->resource_name,'@',&channel->identifier,channel->pool); if(offer->port) { answer->port = agent->sockaddr->port; } if(offer->connection_type == MRCP_CONNECTION_TYPE_EXISTING) { if(agent->force_new_connection == TRUE) { /* force client to establish new connection */ answer->connection_type = MRCP_CONNECTION_TYPE_NEW; } else { mrcp_connection_t *connection = NULL; /* try to find any existing connection */ connection = mrcp_connection_find(agent,&offer->ip); if(!connection) { /* no existing conection found, force the new one */ answer->connection_type = MRCP_CONNECTION_TYPE_NEW; } } } if(!agent->null_connection) { apt_log(APT_LOG_MARK,APT_PRIO_DEBUG,"Create Pending Connection"); agent->null_connection = mrcp_connection_create(); agent->connection_list = apt_list_create(agent->null_connection->pool); } mrcp_connection_channel_add(agent->null_connection,channel); apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Add Control Channel <%s> to Pending Connection [%d]", channel->identifier.buf, apr_hash_count(agent->null_connection->channel_table)); /* send response */ return mrcp_control_channel_add_respond(agent->vtable,channel,answer,TRUE); }
static apt_bool_t mrcp_client_message_handler(mrcp_connection_t *connection, mrcp_message_t *message, apt_message_status_e status) { if(status == APT_MESSAGE_STATUS_COMPLETE) { /* message is completely parsed */ mrcp_control_channel_t *channel; apt_str_t identifier; apt_id_resource_generate(&message->channel_id.session_id,&message->channel_id.resource_name,'@',&identifier,message->pool); channel = mrcp_connection_channel_find(connection,&identifier); if(channel) { mrcp_connection_agent_t *agent = connection->agent; if(message->start_line.message_type == MRCP_MESSAGE_TYPE_RESPONSE) { if(!channel->active_request || channel->active_request->start_line.request_id != message->start_line.request_id) { apt_obj_log(APT_LOG_MARK,APT_PRIO_WARNING,channel->log_obj,"Unexpected MRCP Response "APT_SIDRES_FMT" [%d]", MRCP_MESSAGE_SIDRES(message), message->start_line.request_id); return FALSE; } if(channel->request_timer) { apt_timer_kill(channel->request_timer); } channel->active_request = NULL; } mrcp_connection_message_receive(agent->vtable,channel,message); } else { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Find Channel "APT_SIDRES_FMT" in Connection %s [%d]", MRCP_MESSAGE_SIDRES(message), connection->id, apr_hash_count(connection->channel_table)); } } return TRUE; }
static apt_bool_t mrcp_client_agent_channel_modify(mrcp_connection_agent_t *agent, mrcp_control_channel_t *channel, mrcp_control_descriptor_t *descriptor) { apt_bool_t status = TRUE; if(descriptor->port) { if(!channel->connection) { mrcp_connection_t *connection = NULL; apt_id_resource_generate(&descriptor->session_id,&descriptor->resource_name,'@',&channel->identifier,channel->pool); /* no connection yet */ if(descriptor->connection_type == MRCP_CONNECTION_TYPE_EXISTING) { /* try to find existing connection */ connection = mrcp_client_agent_connection_find(agent,descriptor); if(!connection) { apt_obj_log(APT_LOG_MARK,APT_PRIO_WARNING,channel->log_obj,"Found No Existing TCP/MRCPv2 Connection"); } } if(!connection) { /* create new connection */ connection = mrcp_client_agent_connection_create(agent,descriptor); if(!connection) { apt_obj_log(APT_LOG_MARK,APT_PRIO_WARNING,channel->log_obj,"Failed to Establish TCP/MRCPv2 Connection"); } } if(connection) { mrcp_connection_channel_add(connection,channel); apt_obj_log(APT_LOG_MARK,APT_PRIO_INFO,channel->log_obj,"Add Control Channel <%s> %s [%d]", channel->identifier.buf, connection->id, apr_hash_count(connection->channel_table)); if(descriptor->connection_type == MRCP_CONNECTION_TYPE_NEW) { /* set connection type to existing for the next offers / if any */ descriptor->connection_type = MRCP_CONNECTION_TYPE_EXISTING; } } else { descriptor->port = 0; status = FALSE; } } } /* send response */ return mrcp_control_channel_modify_respond(agent->vtable,channel,descriptor,status); }
static mrcp_control_channel_t* mrcp_connection_channel_associate(mrcp_connection_agent_t *agent, mrcp_connection_t *connection, const mrcp_message_t *message) { apt_str_t identifier; mrcp_control_channel_t *channel; if(!connection || !message) { return NULL; } apt_id_resource_generate(&message->channel_id.session_id,&message->channel_id.resource_name,'@',&identifier,connection->pool); channel = mrcp_connection_channel_find(connection,&identifier); if(!channel) { channel = mrcp_connection_channel_find(agent->null_connection,&identifier); if(channel) { mrcp_connection_channel_remove(agent->null_connection,channel); mrcp_connection_channel_add(connection,channel); apt_log(APT_PRIO_INFO,"Move Control Channel <%s> to Connection [%d]", channel->identifier.buf, apr_hash_count(connection->channel_table)); } } return channel; }
static apt_bool_t mrcp_client_message_handler(void *obj, mrcp_message_t *message, mrcp_stream_result_e result) { if(result == MRCP_STREAM_MESSAGE_COMPLETE) { /* message is completely parsed */ mrcp_connection_t *connection = obj; mrcp_control_channel_t *channel; apt_str_t identifier; apt_id_resource_generate(&message->channel_id.session_id,&message->channel_id.resource_name,'@',&identifier,message->pool); channel = mrcp_connection_channel_find(connection,&identifier); if(channel) { mrcp_connection_agent_t *agent = connection->agent; mrcp_connection_message_receive(agent->vtable,channel,message); } else { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Find Channel <%s@%s>", message->channel_id.session_id.buf, message->channel_id.resource_name.buf); } } return TRUE; }
static mrcp_control_channel_t* mrcp_connection_channel_associate(mrcp_connection_agent_t *agent, mrcp_connection_t *connection, const mrcp_message_t *message) { apt_str_t identifier; mrcp_control_channel_t *channel; if(!connection || !message) { return NULL; } apt_id_resource_generate(&message->channel_id.session_id,&message->channel_id.resource_name,'@',&identifier,connection->pool); channel = mrcp_connection_channel_find(connection,&identifier); if(!channel) { channel = apr_hash_get(agent->pending_channel_table,identifier.buf,identifier.length); if(channel) { apr_hash_set(agent->pending_channel_table,identifier.buf,identifier.length,NULL); mrcp_connection_channel_add(connection,channel); apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Assign Control Channel <%s> to Connection %s [%d] -> [%d]", channel->identifier.buf, connection->id, apr_hash_count(agent->pending_channel_table), apr_hash_count(connection->channel_table)); } } return channel; }
static apt_bool_t mrcp_client_agent_messsage_receive(mrcp_connection_agent_t *agent, mrcp_connection_t *connection) { char buffer[MRCP_MESSAGE_MAX_SIZE]; apt_bool_t more_messages_on_buffer = FALSE; apr_status_t status; apt_text_stream_t text_stream; mrcp_message_t *message; if(!connection || !connection->sock) { return FALSE; } text_stream.text.buf = buffer; text_stream.text.length = sizeof(buffer)-1; status = apr_socket_recv(connection->sock, text_stream.text.buf, &text_stream.text.length); if(status == APR_EOF || text_stream.text.length == 0) { apt_log(APT_PRIO_NOTICE,"TCP/MRCPv2 Connection Disconnected"); apr_pollset_remove(agent->pollset,&connection->sock_pfd); apr_socket_close(connection->sock); connection->sock = NULL; // agent->vtable->on_disconnect(agent,connection); return TRUE; } text_stream.text.buf[text_stream.text.length] = '\0'; text_stream.pos = text_stream.text.buf; apt_log(APT_PRIO_INFO,"Receive MRCPv2 Message size=%lu\n%s",text_stream.text.length,text_stream.text.buf); if(!connection->access_count) { return FALSE; } do { message = mrcp_message_create(connection->pool); if(mrcp_message_parse(agent->resource_factory,message,&text_stream) == TRUE) { mrcp_control_channel_t *channel; apt_str_t identifier; apt_id_resource_generate(&message->channel_id.session_id,&message->channel_id.resource_name,'@',&identifier,connection->pool); channel = mrcp_connection_channel_find(connection,&identifier); if(channel) { mrcp_connection_message_receive(agent->vtable,channel,message); } else { apt_log(APT_PRIO_WARNING,"Failed to Find Channel <%s@%s>", message->channel_id.session_id.buf, message->channel_id.resource_name.buf); } } else { apt_log(APT_PRIO_WARNING,"Failed to Parse MRCPv2 Message"); if(message->start_line.version == MRCP_VERSION_2) { /* assume that at least message length field is valid */ if(message->start_line.length <= text_stream.text.length) { /* skip to the end of the message */ text_stream.pos = text_stream.text.buf + message->start_line.length; } else { /* skip to the end of the buffer (support incomplete) */ text_stream.pos = text_stream.text.buf + text_stream.text.length; } } } more_messages_on_buffer = FALSE; if(text_stream.text.length > (apr_size_t)(text_stream.pos - text_stream.text.buf)) { /* there are more MRCPv2 messages to signal */ more_messages_on_buffer = TRUE; text_stream.text.length -= text_stream.pos - text_stream.text.buf; text_stream.text.buf = text_stream.pos; apt_log(APT_PRIO_DEBUG,"Saving Remaining Buffer for Next Message"); } } while(more_messages_on_buffer); return TRUE; }