mrcp_message_t* VerifierSession::CreateStartSessionRequest(mrcp_channel_t* pMrcpChannel) { mrcp_message_t* pMrcpMessage = CreateMrcpMessage(pMrcpChannel,VERIFIER_START_SESSION); if(!pMrcpMessage) return NULL; mrcp_verifier_header_t* pVerifierHeader; /* get/allocate verifier header */ pVerifierHeader = (mrcp_verifier_header_t*) mrcp_resource_header_prepare(pMrcpMessage); if(pVerifierHeader) { const VerifierScenario* pScenario = GetScenario(); const char* pRepositoryURI = pScenario->GetRepositoryURI(); if(pRepositoryURI) { apt_string_set(&pVerifierHeader->repository_uri,pRepositoryURI); mrcp_resource_header_property_add(pMrcpMessage,VERIFIER_HEADER_REPOSITORY_URI); } const char* pVoiceprintIdentifier = pScenario->GetVoiceprintIdentifier(); if(pVoiceprintIdentifier) { apt_string_set(&pVerifierHeader->voiceprint_identifier,pVoiceprintIdentifier); mrcp_resource_header_property_add(pMrcpMessage,VERIFIER_HEADER_VOICEPRINT_IDENTIFIER); } const char* pVerificationMode = pScenario->GetVerificationMode(); if(pVerificationMode) { apt_string_set(&pVerifierHeader->verification_mode,pVerificationMode); mrcp_resource_header_property_add(pMrcpMessage,VERIFIER_HEADER_VERIFICATION_MODE); } } return pMrcpMessage; }
/** Create sample RTP remote descriptor */ static mpf_rtp_stream_descriptor_t* mpf_rtp_remote_descriptor_create(mpf_suite_session_t *session) { mpf_codec_list_t *codec_list; mpf_codec_descriptor_t *codec_descriptor; mpf_rtp_stream_descriptor_t *descriptor = apr_palloc(session->pool,sizeof(mpf_rtp_stream_descriptor_t)); mpf_rtp_stream_descriptor_init(descriptor); descriptor->remote = apr_palloc(session->pool,sizeof(mpf_rtp_media_descriptor_t)); mpf_rtp_media_descriptor_init(descriptor->remote); descriptor->remote->direction = STREAM_DIRECTION_DUPLEX; apt_string_set(&descriptor->remote->ip,"127.0.0.1"); descriptor->remote->port = 5002; codec_list = &descriptor->remote->codec_list; mpf_codec_list_init(codec_list,2,session->pool); codec_descriptor = mpf_codec_list_add(codec_list); if(codec_descriptor) { codec_descriptor->payload_type = 0; } codec_descriptor = mpf_codec_list_add(codec_list); if(codec_descriptor) { codec_descriptor->payload_type = 96; apt_string_set(&codec_descriptor->name,"PCMU"); codec_descriptor->sampling_rate = 16000; codec_descriptor->channel_count = 1; } return descriptor; }
static apt_str_t* multipart_content_generate(apt_test_suite_t *suite) { apt_multipart_content_t *multipart = apt_multipart_content_create(1500,NULL,suite->pool); apt_str_t content_type; apt_str_t content; apt_str_t *body; apt_string_set(&content_type,"text/plain"); apt_string_set(&content,"This is the content of the first part"); apt_multipart_content_add2(multipart,&content_type,NULL,&content); apt_string_set(&content_type,"application/ssml+xml"); apt_string_set(&content, "<?xml version=\"1.0\"?>\r\n" "<speak version=\"1.0\"\r\n" "<p> <s>You have 4 new messages.</s> </p>\r\n" "</speak>"); apt_multipart_content_add2(multipart,&content_type,NULL,&content); body = apt_multipart_content_finalize(multipart); if(body) { apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Genereted Multipart Content [%lu bytes]\n%s", body->length, body->buf); } return body; }
/** Create RTP tx remote descriptor */ static mpf_rtp_stream_descriptor_t* mpf_rtp_tx_remote_descriptor_create(const mpf_suite_agent_t *agent, const mpf_suite_session_t *session) { mpf_codec_list_t *codec_list; mpf_codec_descriptor_t *codec_descriptor; mpf_rtp_media_descriptor_t *media_descriptor; mpf_rtp_stream_descriptor_t *stream_descriptor; media_descriptor = mpf_rtp_media_descriptor_alloc(session->pool); media_descriptor->state = MPF_MEDIA_ENABLED; media_descriptor->direction = STREAM_DIRECTION_RECEIVE; apt_string_set(&media_descriptor->ip,"127.0.0.1"); media_descriptor->port = 5000; codec_list = &media_descriptor->codec_list; mpf_codec_list_init(codec_list,1,session->pool); codec_descriptor = mpf_codec_list_add(codec_list); if(codec_descriptor) { codec_descriptor->payload_type = 0; apt_string_set(&codec_descriptor->name,"PCMU"); codec_descriptor->sampling_rate = 8000; codec_descriptor->channel_count = 1; } stream_descriptor = apr_palloc(session->pool,sizeof(mpf_rtp_stream_descriptor_t)); mpf_rtp_stream_descriptor_init(stream_descriptor); stream_descriptor->remote = media_descriptor; stream_descriptor->settings = agent->rtp_settings; return stream_descriptor; }
/** Load RTP factory */ static apt_bool_t unimrcp_client_rtp_factory_load(unimrcp_client_loader_t *loader, const apr_xml_elem *root, const char *id) { const apr_xml_elem *elem; char *rtp_ip = NULL; char *rtp_ext_ip = NULL; mpf_termination_factory_t *rtp_factory; mpf_rtp_config_t *rtp_config; rtp_config = mpf_rtp_config_alloc(loader->pool); rtp_config->rtp_port_min = DEFAULT_RTP_PORT_MIN; rtp_config->rtp_port_max = DEFAULT_RTP_PORT_MAX; apt_log(APT_LOG_MARK,APT_PRIO_DEBUG,"Loading RTP Factory <%s>",id); for(elem = root->first_child; elem; elem = elem->next) { apt_log(APT_LOG_MARK,APT_PRIO_DEBUG,"Loading Element <%s>",elem->name); if(strcasecmp(elem->name,"rtp-ip") == 0) { rtp_ip = unimrcp_client_ip_address_get(loader,elem,loader->ip); } else if(strcasecmp(elem->name,"rtp-ext-ip") == 0) { rtp_ext_ip = unimrcp_client_ip_address_get(loader,elem,loader->ext_ip); } else if(strcasecmp(elem->name,"rtp-port-min") == 0) { if(is_cdata_valid(elem) == TRUE) { rtp_config->rtp_port_min = (apr_port_t)atol(cdata_text_get(elem)); } } else if(strcasecmp(elem->name,"rtp-port-max") == 0) { if(is_cdata_valid(elem) == TRUE) { rtp_config->rtp_port_max = (apr_port_t)atol(cdata_text_get(elem)); } } else { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Unknown Element <%s>",elem->name); } } if(rtp_ip) { apt_string_set(&rtp_config->ip,rtp_ip); } else { apt_string_set(&rtp_config->ip,loader->ip); } if(rtp_ext_ip) { apt_string_set(&rtp_config->ext_ip,rtp_ext_ip); } else if(loader->ext_ip){ apt_string_set(&rtp_config->ext_ip,loader->ext_ip); } rtp_factory = mpf_rtp_termination_factory_create(rtp_config,loader->pool); return mrcp_client_rtp_factory_register(loader->client,rtp_factory,id); }
/** Generate MRCP control media by SDP media */ static apt_bool_t mrcp_control_media_generate(mrcp_control_descriptor_t *control_media, const sdp_media_t *sdp_media, const apt_str_t *ip, apr_pool_t *pool) { mrcp_attrib_e id; apt_str_t name; apt_str_t value; sdp_attribute_t *attrib = NULL; apt_string_set(&name,sdp_media->m_proto_name); control_media->proto = mrcp_proto_find(&name); if(control_media->proto != MRCP_PROTO_TCP) { apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Not supported SDP Proto [%s], expected [%s]",sdp_media->m_proto_name,mrcp_proto_get(MRCP_PROTO_TCP)->buf); return FALSE; } for(attrib = sdp_media->m_attributes; attrib; attrib=attrib->a_next) { apt_string_set(&name,attrib->a_name); id = mrcp_attrib_id_find(&name); switch(id) { case MRCP_ATTRIB_SETUP: apt_string_set(&value,attrib->a_value); control_media->setup_type = mrcp_setup_type_find(&value); break; case MRCP_ATTRIB_CONNECTION: apt_string_set(&value,attrib->a_value); control_media->connection_type = mrcp_connection_type_find(&value); break; case MRCP_ATTRIB_RESOURCE: apt_string_assign(&control_media->resource_name,attrib->a_value,pool); break; case MRCP_ATTRIB_CHANNEL: apt_string_set(&value,attrib->a_value); apt_id_resource_parse(&value,'@',&control_media->session_id,&control_media->resource_name,pool); break; case MRCP_ATTRIB_CMID: mrcp_cmid_add(control_media->cmid_arr,atoi(attrib->a_value)); break; default: break; } } if(sdp_media->m_connections) { apt_string_assign(&control_media->ip,sdp_media->m_connections->c_address,pool); } else { control_media->ip = *ip; } control_media->port = (apr_port_t)sdp_media->m_port; return TRUE; }
/* MRCP connection established, start communication */ static apt_bool_t OnChannelAdd(mrcp_application_t* application, mrcp_session_t* session, mrcp_channel_t* channel, mrcp_sig_status_code_e status) { mrcp_message_t* msg; mrcp_generic_header_t* hdr; (void) application; if (status != MRCP_SIG_STATUS_CODE_SUCCESS) return sess_failed("Failed to add channel"); /* Start processing here */ msg = mrcp_application_message_create(session, channel, SYNTHESIZER_SPEAK); hdr = mrcp_generic_header_get(msg); apt_string_set(&hdr->content_type, "text/plain"); mrcp_generic_header_property_add(msg, GENERIC_HEADER_CONTENT_TYPE); apt_string_set(&msg->body, text); return mrcp_application_message_send(session, channel, msg); }
static apt_bool_t mrcp_unirtsp_on_session_answer(mrcp_session_t *mrcp_session, mrcp_session_descriptor_t *descriptor) { mrcp_unirtsp_session_t *session = mrcp_session->obj; mrcp_unirtsp_agent_t *agent = mrcp_session->signaling_agent->obj; rtsp_message_t *response = NULL; const rtsp_message_t *request = rtsp_server_session_request_get(session->rtsp_session); if(!request) { return FALSE; } if(request->start_line.common.request_line.method_id == RTSP_METHOD_SETUP) { if(agent->config->origin) { apt_string_set(&descriptor->origin,agent->config->origin); } response = rtsp_response_generate_by_mrcp_descriptor( request, descriptor, agent->config->resource_map, mrcp_session->pool); } else if(request->start_line.common.request_line.method_id == RTSP_METHOD_TEARDOWN) { response = rtsp_response_create(request,RTSP_STATUS_CODE_OK,RTSP_REASON_PHRASE_OK,mrcp_session->pool); } if(!response) { return FALSE; } rtsp_server_session_respond(agent->rtsp_server,session->rtsp_session,response); return TRUE; }
/** Create demo RTP termination descriptor */ mpf_rtp_termination_descriptor_t* demo_rtp_descriptor_create(apr_pool_t *pool) { mpf_codec_descriptor_t *codec_descriptor; mpf_rtp_media_descriptor_t *media; /* create rtp descriptor */ mpf_rtp_termination_descriptor_t *rtp_descriptor = apr_palloc(pool,sizeof(mpf_rtp_termination_descriptor_t)); mpf_rtp_termination_descriptor_init(rtp_descriptor); /* create rtp local media */ media = apr_palloc(pool,sizeof(mpf_rtp_media_descriptor_t)); mpf_rtp_media_descriptor_init(media); apt_string_assign(&media->ip,"127.0.0.1",pool); media->port = 6000; media->state = MPF_MEDIA_ENABLED; media->direction = STREAM_DIRECTION_RECEIVE; /* initialize codec list */ mpf_codec_list_init(&media->codec_list,2,pool); /* set codec descriptor */ codec_descriptor = mpf_codec_list_add(&media->codec_list); if(codec_descriptor) { codec_descriptor->payload_type = 0; } /* set another codec descriptor */ codec_descriptor = mpf_codec_list_add(&media->codec_list); if(codec_descriptor) { codec_descriptor->payload_type = 96; apt_string_set(&codec_descriptor->name,"PCMU"); codec_descriptor->sampling_rate = 16000; codec_descriptor->channel_count = 1; } rtp_descriptor->audio.local = media; return rtp_descriptor; }
static apt_bool_t mrcp_sofia_on_session_answer(mrcp_session_t *session, mrcp_session_descriptor_t *descriptor) { mrcp_sofia_session_t *sofia_session = session->obj; mrcp_sofia_agent_t *sofia_agent = session->signaling_agent->obj; const char *local_sdp_str = NULL; char sdp_str[2048]; if(!sofia_agent || !sofia_session || !sofia_session->nh) { return FALSE; } if(sofia_agent->config->origin) { apt_string_set(&descriptor->origin,sofia_agent->config->origin); } if(sdp_string_generate_by_mrcp_descriptor(sdp_str,sizeof(sdp_str),descriptor,FALSE) > 0) { local_sdp_str = sdp_str; apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Local SDP\n%s", local_sdp_str); } nua_respond(sofia_session->nh, SIP_200_OK, SIPTAG_CONTACT_STR(sofia_agent->sip_contact_str), TAG_IF(local_sdp_str,SOATAG_USER_SDP_STR(local_sdp_str)), NUTAG_AUTOANSWER(0), TAG_END()); return TRUE; }
/** Deactivate state machine */ static apt_bool_t recog_state_deactivate(mrcp_state_machine_t *base) { mrcp_recog_state_machine_t *state_machine = (mrcp_recog_state_machine_t*)base; mrcp_message_t *message; mrcp_message_t *source; if(state_machine->state != RECOGNIZER_STATE_RECOGNIZING) { /* no in-progress RECOGNIZE request to deactivate */ return FALSE; } source = state_machine->recog; if(!source) { return FALSE; } /* create internal STOP request */ message = mrcp_request_create( source->resource, source->start_line.version, RECOGNIZER_STOP, source->pool); message->channel_id = source->channel_id; message->start_line.request_id = source->start_line.request_id + 1; apt_string_set(&message->start_line.method_name,"DEACTIVATE"); /* informative only */ message->header = source->header; apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Create and Process STOP Request "APT_SIDRES_FMT" [%"MRCP_REQUEST_ID_FMT"]", MRCP_MESSAGE_SIDRES(message), message->start_line.request_id); return recog_request_dispatch(state_machine,message); }
static apt_bool_t mrcp_sofia_session_offer(mrcp_session_t *session, mrcp_session_descriptor_t *descriptor) { char sdp_str[2048]; char *local_sdp_str = NULL; mrcp_sofia_session_t *sofia_session = session->obj; if(!sofia_session || !sofia_session->nh) { return FALSE; } if(session->signaling_agent) { mrcp_sofia_agent_t *sofia_agent = session->signaling_agent->obj; if(sofia_agent && sofia_agent->config->origin) { apt_string_set(&descriptor->origin,sofia_agent->config->origin); } } if(sdp_string_generate_by_mrcp_descriptor(sdp_str,sizeof(sdp_str),descriptor,TRUE) > 0) { local_sdp_str = sdp_str; apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Local SDP\n%s", local_sdp_str); } nua_invite(sofia_session->nh, TAG_IF(local_sdp_str,SOATAG_USER_SDP_STR(local_sdp_str)), TAG_END()); return TRUE; }
/** Generate RTP media descriptor by SDP media */ static apt_bool_t mpf_rtp_media_generate(mpf_rtp_media_descriptor_t *rtp_media, const sdp_media_t *sdp_media, const apt_str_t *ip, apr_pool_t *pool) { mpf_rtp_attrib_e id; apt_str_t name; sdp_attribute_t *attrib = NULL; sdp_rtpmap_t *map; mpf_codec_descriptor_t *codec; for(attrib = sdp_media->m_attributes; attrib; attrib=attrib->a_next) { apt_string_set(&name,attrib->a_name); id = mpf_rtp_attrib_id_find(&name); switch(id) { case RTP_ATTRIB_PTIME: rtp_media->ptime = (apr_uint16_t)atoi(attrib->a_value); break; default: break; } } mpf_codec_list_init(&rtp_media->codec_list,5,pool); for(map = sdp_media->m_rtpmaps; map; map = map->rm_next) { codec = mpf_codec_list_add(&rtp_media->codec_list); if(codec) { codec->payload_type = (apr_byte_t)map->rm_pt; apt_string_assign(&codec->name,map->rm_encoding,pool); codec->sampling_rate = (apr_uint16_t)map->rm_rate; codec->channel_count = 1; } } switch(sdp_media->m_mode) { case sdp_inactive: rtp_media->direction = STREAM_DIRECTION_NONE; break; case sdp_sendonly: rtp_media->direction = STREAM_DIRECTION_SEND; break; case sdp_recvonly: rtp_media->direction = STREAM_DIRECTION_RECEIVE; break; case sdp_sendrecv: rtp_media->direction = STREAM_DIRECTION_DUPLEX; break; } if(sdp_media->m_connections) { apt_string_assign(&rtp_media->ip,sdp_media->m_connections->c_address,pool); } else { rtp_media->ip = *ip; } if(sdp_media->m_port) { rtp_media->port = (apr_port_t)sdp_media->m_port; rtp_media->state = MPF_MEDIA_ENABLED; } else { rtp_media->state = MPF_MEDIA_DISABLED; } return TRUE; }
/** Deactivate state machine */ static apt_bool_t synth_state_deactivate(mrcp_state_machine_t *base) { mrcp_synth_state_machine_t *state_machine = (mrcp_synth_state_machine_t*)base; mrcp_message_t *message; mrcp_message_t *source; if(!state_machine->speaker) { /* no in-progress SPEAK request to deactivate */ return FALSE; } source = state_machine->speaker; /* create internal STOP request */ message = mrcp_request_create( source->resource, source->start_line.version, SYNTHESIZER_STOP, source->pool); message->channel_id = source->channel_id; message->start_line.request_id = source->start_line.request_id + 1; apt_string_set(&message->start_line.method_name,"DEACTIVATE"); /* informative only */ message->header = source->header; apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Create and Process STOP Request [%"MRCP_REQUEST_ID_FMT"]", message->start_line.request_id); return synth_request_dispatch(state_machine,message); }
static apt_bool_t mrcp_server_agent_connection_accept(mrcp_connection_agent_t *agent) { char *local_ip = NULL; char *remote_ip = NULL; apr_socket_t *sock; apr_pool_t *pool; mrcp_connection_t *connection; if(!agent->null_connection) { pool = apt_pool_create(); if(apr_socket_accept(&sock,agent->listen_sock,pool) != APR_SUCCESS) { return FALSE; } apt_log(APT_LOG_MARK,APT_PRIO_NOTICE,"Rejected TCP/MRCPv2 Connection"); apr_socket_close(sock); apr_pool_destroy(pool); return FALSE; } pool = agent->null_connection->pool; if(apr_socket_accept(&sock,agent->listen_sock,pool) != APR_SUCCESS) { return FALSE; } connection = mrcp_connection_create(); connection->sock = sock; if(apr_socket_addr_get(&connection->r_sockaddr,APR_REMOTE,sock) != APR_SUCCESS || apr_socket_addr_get(&connection->l_sockaddr,APR_LOCAL,sock) != APR_SUCCESS) { apr_socket_close(sock); mrcp_connection_destroy(connection); return FALSE; } apr_sockaddr_ip_get(&local_ip,connection->l_sockaddr); apr_sockaddr_ip_get(&remote_ip,connection->r_sockaddr); apt_string_set(&connection->remote_ip,remote_ip); connection->id = apr_psprintf(connection->pool,"%s:%hu <-> %s:%hu", local_ip,connection->l_sockaddr->port, remote_ip,connection->r_sockaddr->port); memset(&connection->sock_pfd,0,sizeof(apr_pollfd_t)); connection->sock_pfd.desc_type = APR_POLL_SOCKET; connection->sock_pfd.reqevents = APR_POLLIN; connection->sock_pfd.desc.s = connection->sock; connection->sock_pfd.client_data = connection; if(apt_pollset_add(agent->pollset, &connection->sock_pfd) != TRUE) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Add to Pollset"); apr_socket_close(sock); mrcp_connection_destroy(connection); return FALSE; } apt_log(APT_LOG_MARK,APT_PRIO_NOTICE,"Accepted TCP/MRCPv2 Connection %s",connection->id); connection->agent = agent; connection->it = apt_list_push_back(agent->connection_list,connection,connection->pool); connection->parser = mrcp_parser_create(agent->resource_factory,connection->pool); connection->generator = mrcp_generator_create(agent->resource_factory,connection->pool); return TRUE; }
mrcp_message_t* RecogSession::CreateRecognizeRequest(mrcp_channel_t* pMrcpChannel) { mrcp_message_t* pMrcpMessage = CreateMrcpMessage(pMrcpChannel,RECOGNIZER_RECOGNIZE); if(!pMrcpMessage) return NULL; const RecogScenario* pScenario = GetScenario(); mrcp_generic_header_t* pGenericHeader; mrcp_recog_header_t* pRecogHeader; /* get/allocate generic header */ pGenericHeader = (mrcp_generic_header_t*) mrcp_generic_header_prepare(pMrcpMessage); if(pGenericHeader) { /* set generic header fields */ if(pScenario->IsDefineGrammarEnabled()) { apt_string_assign(&pGenericHeader->content_type,"text/uri-list",pMrcpMessage->pool); /* set message body */ const char* pContent = apr_pstrcat(pMrcpMessage->pool,"session:",m_ContentId,NULL); apt_string_set(&pMrcpMessage->body,pContent); } else { apt_string_assign(&pGenericHeader->content_type,pScenario->GetContentType(),pMrcpMessage->pool); /* set content-id */ apt_string_assign(&pGenericHeader->content_id,m_ContentId,pMrcpMessage->pool); mrcp_generic_header_property_add(pMrcpMessage,GENERIC_HEADER_CONTENT_ID); /* set message body */ if(pScenario->GetContent()) apt_string_assign_n(&pMrcpMessage->body,pScenario->GetContent(),pScenario->GetContentLength(),pMrcpMessage->pool); } mrcp_generic_header_property_add(pMrcpMessage,GENERIC_HEADER_CONTENT_TYPE); } /* get/allocate recognizer header */ pRecogHeader = (mrcp_recog_header_t*) mrcp_resource_header_prepare(pMrcpMessage); if(pRecogHeader) { /* set recognizer header fields */ if(pMrcpMessage->start_line.version == MRCP_VERSION_2) { pRecogHeader->cancel_if_queue = FALSE; mrcp_resource_header_property_add(pMrcpMessage,RECOGNIZER_HEADER_CANCEL_IF_QUEUE); } pRecogHeader->no_input_timeout = 5000; mrcp_resource_header_property_add(pMrcpMessage,RECOGNIZER_HEADER_NO_INPUT_TIMEOUT); pRecogHeader->recognition_timeout = 10000; mrcp_resource_header_property_add(pMrcpMessage,RECOGNIZER_HEADER_RECOGNITION_TIMEOUT); pRecogHeader->start_input_timers = TRUE; mrcp_resource_header_property_add(pMrcpMessage,RECOGNIZER_HEADER_START_INPUT_TIMERS); pRecogHeader->confidence_threshold = 0.87f; mrcp_resource_header_property_add(pMrcpMessage,RECOGNIZER_HEADER_CONFIDENCE_THRESHOLD); pRecogHeader->save_waveform = TRUE; mrcp_resource_header_property_add(pMrcpMessage,RECOGNIZER_HEADER_SAVE_WAVEFORM); } return pMrcpMessage; }
/** Create demo synthesizer channel derived from engine channel base */ static mrcp_engine_channel_t* mrcp_swift_engine_channel_create(mrcp_resource_engine_t *engine, apr_pool_t *pool) { swift_engine *synth_engine = engine->obj; mrcp_swift_channel_t *synth_channel; mrcp_engine_channel_t *channel; swift_params *params; swift_port *port; mpf_codec_descriptor_t *codec_descriptor; codec_descriptor = apr_palloc(pool,sizeof(mpf_codec_descriptor_t)); mpf_codec_descriptor_init(codec_descriptor); codec_descriptor->channel_count = 1; codec_descriptor->payload_type = 96; apt_string_set(&codec_descriptor->name,"L16"); codec_descriptor->sampling_rate = 8000; params = swift_params_new(NULL); swift_params_set_string(params, "audio/encoding", "pcm16"); swift_params_set_int(params, "audio/sampling-rate", codec_descriptor->sampling_rate); /* open swift port */ apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Open Swift Port"); if((port = swift_port_open(synth_engine,params)) == NULL) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Open Swift Port"); return NULL; } /* create swift synth channel */ synth_channel = apr_palloc(pool,sizeof(mrcp_swift_channel_t)); synth_channel->speak_request = NULL; synth_channel->stop_response = NULL; synth_channel->paused = FALSE; synth_channel->channel = NULL; synth_channel->port = port; synth_channel->tts_stream = 0; /* create engine channel base */ channel = mrcp_engine_source_channel_create( engine, /* resource engine */ &channel_vtable, /* virtual methods table of engine channel */ &audio_stream_vtable, /* virtual methods table of audio stream */ synth_channel, /* object to associate */ codec_descriptor, /* codec descriptor might be NULL by default */ pool); /* pool to allocate memory from */ if(!channel) { swift_port_close(port); synth_channel->port = NULL; return NULL; } synth_channel->audio_buffer = mpf_buffer_create(pool); /* set swift_write_audio as a callback, with the output file as its param */ swift_port_set_callback(port, &mrcp_swift_write_audio, SWIFT_EVENT_AUDIO | SWIFT_EVENT_END, synth_channel); synth_channel->channel = channel; return channel; }
/* Set RTP config struct with param, val pair. */ static int process_rtp_config(mrcp_client_t *client, mpf_rtp_config_t *rtp_config, mpf_rtp_settings_t *rtp_settings, const char *param, const char *val, apr_pool_t *pool) { int mine = 1; if ((client == NULL) || (rtp_config == NULL) || (rtp_settings == NULL) || (param == NULL) || (val == NULL) || (pool == NULL)) return mine; if (strcasecmp(param, "rtp-ip") == 0) apt_string_set(&rtp_config->ip, ip_addr_get(val, pool)); else if (strcasecmp(param, "rtp-ext-ip") == 0) apt_string_set(&rtp_config->ext_ip, ip_addr_get(val, pool)); else if (strcasecmp(param, "rtp-port-min") == 0) rtp_config->rtp_port_min = (apr_port_t)atol(val); else if (strcasecmp(param, "rtp-port-max") == 0) rtp_config->rtp_port_max = (apr_port_t)atol(val); else if (strcasecmp(param, "playout-delay") == 0) rtp_settings->jb_config.initial_playout_delay = atol(val); else if (strcasecmp(param, "min-playout-delay") == 0) rtp_settings->jb_config.min_playout_delay = atol(val); else if (strcasecmp(param, "max-playout-delay") == 0) rtp_settings->jb_config.max_playout_delay = atol(val); else if (strcasecmp(param, "codecs") == 0) { /* Make sure that /etc/mrcp.conf contains the desired codec first in the codecs parameter. */ const mpf_codec_manager_t *codec_manager = mrcp_client_codec_manager_get(client); if (codec_manager != NULL) { if (!mpf_codec_manager_codec_list_load(codec_manager, &rtp_settings->codec_list, val, pool)) ast_log(LOG_WARNING, "Unable to load codecs\n"); } } else if (strcasecmp(param, "ptime") == 0) rtp_settings->ptime = (apr_uint16_t)atol(val); else if (strcasecmp(param, "rtcp") == 0) rtp_settings->rtcp = atoi(val); else if (strcasecmp(param, "rtcp-bye") == 0) rtp_settings->rtcp_bye_policy = atoi(val); else if (strcasecmp(param, "rtcp-tx-interval") == 0) rtp_settings->rtcp_tx_interval = (apr_uint16_t)atoi(val); else if (strcasecmp(param, "rtcp-rx-resolution") == 0) rtp_settings->rtcp_rx_resolution = (apr_uint16_t)atol(val); else mine = 0; return mine; }
/** Create sample RTP local descriptor */ static mpf_rtp_stream_descriptor_t* mpf_rtp_local_descriptor_create(mpf_suite_session_t *session) { mpf_rtp_stream_descriptor_t *descriptor = apr_palloc(session->pool,sizeof(mpf_rtp_stream_descriptor_t)); mpf_rtp_stream_descriptor_init(descriptor); descriptor->local = apr_palloc(session->pool,sizeof(mpf_rtp_media_descriptor_t)); mpf_rtp_media_descriptor_init(descriptor->local); descriptor->local->direction = STREAM_DIRECTION_NONE; apt_string_set(&descriptor->local->ip,"127.0.0.1"); descriptor->local->port = 5000; return descriptor; }
/** \brief Load properties from config */ static mrcp_message_header_t* uni_engine_properties_load(struct ast_config *cfg, const char *category, mrcp_version_e version, apr_pool_t *pool) { struct ast_variable *var; mrcp_message_header_t *properties = NULL; #if defined(TRANSPARENT_HEADER_FIELDS_SUPPORT) apt_header_field_t *header_field; properties = mrcp_message_header_create( mrcp_generic_header_vtable_get(version), mrcp_recog_header_vtable_get(version), pool); for(var = ast_variable_browse(cfg, category); var; var = var->next) { ast_log(LOG_DEBUG, "%s.%s=%s\n", category, var->name, var->value); header_field = apt_header_field_create_c(var->name,var->value,pool); if(header_field) { if(mrcp_header_field_add(properties,header_field,pool) == FALSE) { ast_log(LOG_WARNING, "Unknown MRCP header %s.%s=%s\n", category, var->name, var->value); } } } #else apt_pair_t pair; properties = apr_palloc(pool,sizeof(mrcp_message_header_t)); mrcp_message_header_init(properties); properties->generic_header_accessor.vtable = mrcp_generic_header_vtable_get(version); properties->resource_header_accessor.vtable = mrcp_recog_header_vtable_get(version); mrcp_header_allocate(&properties->generic_header_accessor,pool); mrcp_header_allocate(&properties->resource_header_accessor,pool); for(var = ast_variable_browse(cfg, category); var; var = var->next) { ast_log(LOG_DEBUG, "%s.%s=%s\n", category, var->name, var->value); apt_string_set(&pair.name,var->name); apt_string_set(&pair.value,var->value); if(mrcp_header_parse(&properties->resource_header_accessor,&pair,pool) != TRUE) { if(mrcp_header_parse(&properties->generic_header_accessor,&pair,pool) != TRUE) { ast_log(LOG_WARNING, "Unknown MRCP header %s.%s=%s\n", category, var->name, var->value); } } } #endif return properties; }
/** Load RTP termination factory */ static mpf_termination_factory_t* unimrcp_server_rtp_factory_load(mrcp_server_t *server, const apr_xml_elem *root, apr_pool_t *pool) { const apr_xml_elem *elem; char *rtp_ip = DEFAULT_IP_ADDRESS; mpf_rtp_config_t *rtp_config = mpf_rtp_config_create(pool); rtp_config->rtp_port_min = DEFAULT_RTP_PORT_MIN; rtp_config->rtp_port_max = DEFAULT_RTP_PORT_MAX; apt_log(APT_LOG_MARK,APT_PRIO_DEBUG,"Loading RTP Termination Factory"); for(elem = root->first_child; elem; elem = elem->next) { if(strcasecmp(elem->name,"param") == 0) { const apr_xml_attr *attr_name; const apr_xml_attr *attr_value; if(param_name_value_get(elem,&attr_name,&attr_value) == TRUE) { apt_log(APT_LOG_MARK,APT_PRIO_DEBUG,"Loading Param %s:%s",attr_name->value,attr_value->value); if(strcasecmp(attr_name->value,"rtp-ip") == 0) { rtp_ip = ip_addr_get(attr_value->value,pool); } else if(strcasecmp(attr_name->value,"rtp-port-min") == 0) { rtp_config->rtp_port_min = (apr_port_t)atol(attr_value->value); } else if(strcasecmp(attr_name->value,"rtp-port-max") == 0) { rtp_config->rtp_port_max = (apr_port_t)atol(attr_value->value); } else if(strcasecmp(attr_name->value,"playout-delay") == 0) { rtp_config->jb_config.initial_playout_delay = atol(attr_value->value); } else if(strcasecmp(attr_name->value,"min-playout-delay") == 0) { rtp_config->jb_config.min_playout_delay = atol(attr_value->value); } else if(strcasecmp(attr_name->value,"max-playout-delay") == 0) { rtp_config->jb_config.max_playout_delay = atol(attr_value->value); } else if(strcasecmp(attr_name->value,"codecs") == 0) { const mpf_codec_manager_t *codec_manager = mrcp_server_codec_manager_get(server); if(codec_manager) { mpf_codec_manager_codec_list_load(codec_manager,&rtp_config->codec_list,attr_value->value,pool); } } else if(strcasecmp(attr_name->value,"ptime") == 0) { rtp_config->ptime = (apr_uint16_t)atol(attr_value->value); } else if(strcasecmp(attr_name->value,"own-preference") == 0) { rtp_config->own_preferrence = atoi(attr_value->value); } else { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Unknown Attribute <%s>",attr_name->value); } } } } apt_string_set(&rtp_config->ip,rtp_ip); return mpf_rtp_termination_factory_create(rtp_config,pool); }
static apt_bool_t mrcp_unirtsp_session_offer(mrcp_session_t *mrcp_session, mrcp_session_descriptor_t *descriptor) { mrcp_unirtsp_session_t *session = mrcp_session->obj; mrcp_unirtsp_agent_t *agent = mrcp_session->signaling_agent->obj; rtsp_message_t *request; if(agent->config->origin) { apt_string_set(&descriptor->origin,agent->config->origin); } request = rtsp_request_generate_by_mrcp_descriptor(descriptor,session->rtsp_settings->resource_map,mrcp_session->pool); return rtsp_client_session_request(agent->rtsp_client,session->rtsp_session,request); }
static apt_bool_t mrcp_unirtsp_on_announce_response(mrcp_unirtsp_agent_t *agent, mrcp_unirtsp_session_t *session, rtsp_message_t *message, const char *resource_name) { mrcp_message_t *mrcp_message = NULL; if(!session || !resource_name) { return FALSE; } if(rtsp_header_property_check(&message->header,RTSP_HEADER_FIELD_CONTENT_TYPE) == TRUE && message->header.content_type == RTSP_CONTENT_TYPE_MRCP && rtsp_header_property_check(&message->header,RTSP_HEADER_FIELD_CONTENT_LENGTH) == TRUE && message->header.content_length > 0) { apt_text_stream_t text_stream; mrcp_parser_t *parser; apt_str_t resource_name_str; text_stream.text = message->body; apt_text_stream_reset(&text_stream); apt_string_set(&resource_name_str,resource_name); parser = mrcp_parser_create(agent->sig_agent->resource_factory,session->mrcp_session->pool); mrcp_parser_resource_set(parser,&resource_name_str); if(mrcp_parser_run(parser,&text_stream,&mrcp_message) == APT_MESSAGE_STATUS_COMPLETE) { mrcp_message->channel_id.session_id = message->header.session_id; } else { /* error case */ apt_log(RTSP_LOG_MARK,APT_PRIO_WARNING,"Failed to Parse MRCPv1 Message"); } } else { /* error case */ apt_log(RTSP_LOG_MARK,APT_PRIO_WARNING,"Failed to Determine MRCPv1 Message Content"); } if(!mrcp_message) { if(!session->mrcp_message) { return FALSE; } mrcp_message = mrcp_response_create(session->mrcp_message,session->mrcp_session->pool); mrcp_message->start_line.status_code = MRCP_STATUS_CODE_METHOD_FAILED; } if(session->mrcp_message && mrcp_message->start_line.request_id == session->mrcp_message->start_line.request_id) { session->mrcp_message = NULL; } mrcp_session_control_response(session->mrcp_session,mrcp_message); return TRUE; }
/** Create sample file reader descriptor */ static mpf_audio_file_descriptor_t* mpf_file_reader_descriptor_create(mpf_suite_session_t *session) { mpf_codec_descriptor_t *codec_descriptor; mpf_audio_file_descriptor_t *descriptor = apr_palloc(session->pool,sizeof(mpf_audio_file_descriptor_t)); descriptor->mask = FILE_READER; descriptor->read_handle = fopen("demo.pcm","rb"); descriptor->write_handle = NULL; codec_descriptor = &descriptor->codec_descriptor; codec_descriptor->payload_type = 11; apt_string_set(&codec_descriptor->name,"L16"); codec_descriptor->sampling_rate = 8000; codec_descriptor->channel_count = 1; return descriptor; }
/** Create RTP tx local descriptor */ static mpf_rtp_stream_descriptor_t* mpf_rtp_tx_local_descriptor_create(const mpf_suite_agent_t *agent, const mpf_suite_session_t *session) { mpf_rtp_media_descriptor_t *media_descriptor; mpf_rtp_stream_descriptor_t *stream_descriptor; media_descriptor = mpf_rtp_media_descriptor_alloc(session->pool); media_descriptor->state = MPF_MEDIA_ENABLED; media_descriptor->direction = STREAM_DIRECTION_SEND; apt_string_set(&media_descriptor->ip,"127.0.0.1"); media_descriptor->port = 5002; stream_descriptor = apr_palloc(session->pool,sizeof(mpf_rtp_stream_descriptor_t)); mpf_rtp_stream_descriptor_init(stream_descriptor); stream_descriptor->local = media_descriptor; stream_descriptor->settings = agent->rtp_settings; return stream_descriptor; }
/** Load resource */ static apt_bool_t unimrcp_client_resource_load(mrcp_resource_loader_t *resource_loader, const apr_xml_elem *root, apr_pool_t *pool) { apt_str_t resource_class; const apr_xml_attr *id_attr; const apr_xml_attr *enable_attr; apt_string_reset(&resource_class); if(header_attribs_get(root,&id_attr,&enable_attr) == FALSE) { return FALSE; } if(is_attr_enabled(enable_attr) == FALSE) { return TRUE; } apt_string_set(&resource_class,id_attr->value); return mrcp_resource_load(resource_loader,&resource_class); }
/** Set Record-URI header field */ static apt_bool_t recorder_channel_uri_set(recorder_channel_t *recorder_channel, mrcp_message_t *message) { char *record_uri; /* get/allocate recorder header */ mrcp_recorder_header_t *recorder_header = mrcp_resource_header_prepare(message); if(!recorder_header) { return FALSE; } record_uri = apr_psprintf( message->pool, "<file://mediaserver/data/%s>;size=%"APR_SIZE_T_FMT";duration=%"APR_SIZE_T_FMT, recorder_channel->file_name, recorder_channel->cur_size, recorder_channel->cur_time); apt_string_set(&recorder_header->record_uri,record_uri); mrcp_resource_header_property_add(message,RECORDER_HEADER_RECORD_URI); return TRUE; }
static apt_bool_t mrcp_unirtsp_session_announce(mrcp_unirtsp_agent_t *agent, mrcp_unirtsp_session_t *session, rtsp_message_t *message) { const char *resource_name = mrcp_name_get_by_rtsp_name( agent->config->resource_map, message->start_line.common.request_line.resource_name); apt_bool_t status = TRUE; if(session && resource_name && rtsp_header_property_check(&message->header.property_set,RTSP_HEADER_FIELD_CONTENT_TYPE) == TRUE && message->header.content_type == RTSP_CONTENT_TYPE_MRCP && rtsp_header_property_check(&message->header.property_set,RTSP_HEADER_FIELD_CONTENT_LENGTH) == TRUE && message->header.content_length > 0) { apt_text_stream_t text_stream; mrcp_parser_t *parser; apt_str_t resource_name_str; text_stream.text = message->body; text_stream.pos = text_stream.text.buf; apt_string_set(&resource_name_str,resource_name); parser = mrcp_parser_create(agent->sig_agent->resource_factory,session->mrcp_session->pool); mrcp_parser_resource_name_set(parser,&resource_name_str); if(mrcp_parser_run(parser,&text_stream) == MRCP_STREAM_MESSAGE_COMPLETE) { mrcp_message_t *mrcp_message = mrcp_parser_message_get(parser); mrcp_message->channel_id.session_id = message->header.session_id; status = mrcp_session_control_request(session->mrcp_session,mrcp_message); } else { /* error response */ apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Parse MRCPv1 Message"); status = FALSE; } } else { /* error response */ status = FALSE; } return status; }
static apt_bool_t mrcp_sofia_on_session_answer(mrcp_session_t *session, mrcp_session_descriptor_t *descriptor) { mrcp_sofia_session_t *sofia_session = session->obj; mrcp_sofia_agent_t *sofia_agent = session->signaling_agent->obj; const char *local_sdp_str = NULL; char sdp_str[2048]; if(!sofia_agent || !sofia_session || !sofia_session->nh) { return FALSE; } if(descriptor->status != MRCP_SESSION_STATUS_OK) { int status = sip_status_get(descriptor->status); nua_respond(sofia_session->nh, status, sip_status_phrase(status), TAG_IF(sofia_agent->sip_contact_str,SIPTAG_CONTACT_STR(sofia_agent->sip_contact_str)), TAG_END()); return TRUE; } if(sofia_agent->config->origin) { apt_string_set(&descriptor->origin,sofia_agent->config->origin); } if(sdp_string_generate_by_mrcp_descriptor(sdp_str,sizeof(sdp_str),descriptor,FALSE) > 0) { local_sdp_str = sdp_str; apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Local SDP "APT_NAMESID_FMT"\n%s", session->name, MRCP_SESSION_SID(session), local_sdp_str); } nua_respond(sofia_session->nh, SIP_200_OK, TAG_IF(sofia_agent->sip_contact_str,SIPTAG_CONTACT_STR(sofia_agent->sip_contact_str)), TAG_IF(local_sdp_str,SOATAG_USER_SDP_STR(local_sdp_str)), SOATAG_AUDIO_AUX("telephone-event"), NUTAG_AUTOANSWER(0), TAG_END()); return TRUE; }
static apt_bool_t mrcp_sofia_session_offer(mrcp_session_t *session, mrcp_session_descriptor_t *descriptor) { char sdp_str[2048]; const char *local_sdp_str = NULL; apt_bool_t res = FALSE; mrcp_sofia_session_t *sofia_session = session->obj; if(!sofia_session) { return FALSE; } if(session->signaling_agent) { mrcp_sofia_agent_t *sofia_agent = mrcp_sofia_agent_get(session); if(sofia_agent) { if(sofia_agent->config->origin) { apt_string_set(&descriptor->origin,sofia_agent->config->origin); } } } if(sdp_string_generate_by_mrcp_descriptor(sdp_str,sizeof(sdp_str),descriptor,TRUE) > 0) { local_sdp_str = sdp_str; sofia_session->descriptor = descriptor; apt_obj_log(APT_LOG_MARK,APT_PRIO_INFO,session->log_obj,"Local SDP "APT_NAMESID_FMT"\n%s", session->name, MRCP_SESSION_SID(session), local_sdp_str); } apr_thread_mutex_lock(sofia_session->mutex); if(sofia_session->nh) { res = TRUE; nua_invite(sofia_session->nh, TAG_IF(local_sdp_str,SOATAG_USER_SDP_STR(local_sdp_str)), TAG_END()); } apr_thread_mutex_unlock(sofia_session->mutex); return res; }