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_server_message_handler(void *obj, mrcp_message_t *message, mrcp_stream_result_e result) { mrcp_connection_t *connection = obj; mrcp_connection_agent_t *agent = connection->agent; if(result == MRCP_STREAM_MESSAGE_COMPLETE) { /* message is completely parsed */ mrcp_control_channel_t *channel = mrcp_connection_channel_associate(agent,connection,message); if(channel) { mrcp_connection_message_receive(agent->vtable,channel,message); } else { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Find Channel <%s@%s> in Connection %s", message->channel_id.session_id.buf, message->channel_id.resource_name.buf, connection->id); } } else if(result == MRCP_STREAM_MESSAGE_INVALID) { /* error case */ mrcp_message_t *response; apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Parse MRCPv2 Stream"); response = mrcp_response_create(message,message->pool); response->start_line.status_code = MRCP_STATUS_CODE_UNRECOGNIZED_MESSAGE; if(mrcp_server_agent_messsage_send(agent,connection,response) == FALSE) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Send MRCPv2 Response"); } } return TRUE; }
static apt_bool_t mrcp_server_message_handler(mrcp_connection_t *connection, mrcp_message_t *message, apt_message_status_e status) { mrcp_connection_agent_t *agent = connection->agent; if(status == APT_MESSAGE_STATUS_COMPLETE) { /* message is completely parsed */ mrcp_control_channel_t *channel = mrcp_connection_channel_associate(agent,connection,message); if(channel) { 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", MRCP_MESSAGE_SIDRES(message), connection->id); } } else if(status == APT_MESSAGE_STATUS_INVALID) { /* error case */ apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Parse MRCPv2 Data"); if(message && message->resource) { mrcp_message_t *response; response = mrcp_response_create(message,message->pool); response->start_line.status_code = MRCP_STATUS_CODE_UNRECOGNIZED_MESSAGE; if(mrcp_server_agent_messsage_send(agent,connection,response) == FALSE) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Send MRCPv2 Response"); } } } return TRUE; }
static apt_bool_t mrcp_client_agent_request_cancel(mrcp_connection_agent_t *agent, mrcp_control_channel_t *channel, mrcp_message_t *message) { mrcp_message_t *response; apt_obj_log(APT_LOG_MARK,APT_PRIO_WARNING,channel->log_obj,"Cancel MRCP Request <%s@%s> [%d]", MRCP_MESSAGE_SIDRES(message), message->start_line.request_id); response = mrcp_response_create(message,message->pool); response->start_line.status_code = MRCP_STATUS_CODE_METHOD_FAILED; return mrcp_connection_message_receive(agent->vtable,channel,response); }
static apt_bool_t mrcp_client_agent_messsage_send(mrcp_connection_agent_t *agent, mrcp_control_channel_t *channel, mrcp_message_t *message) { apt_bool_t status = FALSE; mrcp_connection_t *connection = channel->connection; apt_text_stream_t *stream; mrcp_stream_result_e result; if(!connection || !connection->sock) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"No MRCPv2 Connection"); return FALSE; } stream = &connection->tx_stream; mrcp_generator_message_set(connection->generator,message); do { apt_text_stream_init(&connection->tx_stream,connection->tx_buffer,sizeof(connection->tx_buffer)-1); result = mrcp_generator_run(connection->generator,stream); if(result == MRCP_STREAM_MESSAGE_COMPLETE || result == MRCP_STREAM_MESSAGE_TRUNCATED) { stream->text.length = stream->pos - stream->text.buf; *stream->pos = '\0'; apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Send MRCPv2 Stream %s [%lu bytes]\n%s", connection->id, stream->text.length, stream->text.buf); if(apr_socket_send(connection->sock,stream->text.buf,&stream->text.length) == APR_SUCCESS) { status = TRUE; } else { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Send MRCPv2 Stream"); } } else { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Generate MRCPv2 Stream"); } } while(result == MRCP_STREAM_MESSAGE_TRUNCATED); if(status == FALSE) { mrcp_message_t *response = mrcp_response_create(message,message->pool); response->start_line.method_id = message->start_line.method_id; response->start_line.method_name = message->start_line.method_name; response->start_line.status_code = MRCP_STATUS_CODE_METHOD_FAILED; mrcp_connection_message_receive(agent->vtable,channel,response); } return TRUE; }
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 apt_bool_t mrcp_client_agent_messsage_send(mrcp_connection_agent_t *agent, mrcp_control_channel_t *channel, mrcp_message_t *message) { apt_bool_t status = FALSE; mrcp_connection_t *connection = channel->connection; if(connection && connection->sock) { char buffer[MRCP_MESSAGE_MAX_SIZE]; apt_text_stream_t text_stream; text_stream.text.buf = buffer; text_stream.text.length = sizeof(buffer)-1; text_stream.pos = text_stream.text.buf; if(mrcp_message_generate(agent->resource_factory,message,&text_stream) == TRUE) { *text_stream.pos = '\0'; apt_log(APT_PRIO_INFO,"Send MRCPv2 Message size=%lu\n%s", text_stream.text.length,text_stream.text.buf); if(apr_socket_send(connection->sock,text_stream.text.buf,&text_stream.text.length) == APR_SUCCESS) { status = TRUE; } else { apt_log(APT_PRIO_WARNING,"Failed to Send MRCPv2 Message"); } } else { apt_log(APT_PRIO_WARNING,"Failed to Generate MRCPv2 Message"); } } else { apt_log(APT_PRIO_WARNING,"No MRCPv2 Connection"); } if(status == FALSE) { mrcp_message_t *response = mrcp_response_create(message,message->pool); response->start_line.method_id = message->start_line.method_id; response->start_line.method_name = message->start_line.method_name; response->start_line.status_code = MRCP_STATUS_CODE_METHOD_FAILED; mrcp_connection_message_receive(agent->vtable,channel,response); } return TRUE; }
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; }
static apt_bool_t mrcp_server_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) { return mrcp_server_agent_connection_close(agent,connection); } 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); do { message = mrcp_message_create(connection->pool); if(mrcp_message_parse(agent->resource_factory,message,&text_stream) == TRUE) { mrcp_control_channel_t *channel = mrcp_connection_channel_associate(agent,connection,message); 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 { mrcp_message_t *response; 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; } } response = mrcp_response_create(message,message->pool); response->start_line.status_code = MRCP_STATUS_CODE_UNRECOGNIZED_MESSAGE; if(mrcp_server_agent_messsage_send(agent,connection,response) == FALSE) { apt_log(APT_PRIO_WARNING,"Failed to Send MRCPv2 Response"); } } 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; }