📄 mgcp_test.c
字号:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <arpa/inet.h>#include "posix.h" #include "debg.h"#include "typedef.h"#include "mgcpdef.h"#include "stackcb.h"#include "protocalapi.h"#include "endpointctrl.h" #include "transacmng.h"#define ENDPOINT_TOTALL_CNT 16#define MAX_MEDIA_FORMAT_CNT 10 // 最多编码方式最多支持10种#define REAL_MEDIA_FORMAT_CNT 4 // 实际支持4种H_STACK gpStack = 0;H_ENDPOINT gpEndpoint[ENDPOINT_TOTALL_CNT] = {0};int gmain_recv_fd;int ginternal_send_fd;struct sockaddr_in gsock_main_recv;struct sockaddr_in gsock_cbk_send;char * gpc_media_format[MAX_MEDIA_FORMAT_CNT+1] = {"4", "18", "8", "0", ""}; // G723 G729 PCMA PCMUchar * gpc_media_string[MAX_MEDIA_FORMAT_CNT+1] = {"G723", "G729", "PCMA", "PCMU", ""}; typedef struct TMgcp_Stack_Param_tag{ H_ENDPOINT pEndpointHandle; MGCP_CBK_MSG *pCbkMsg; WORD *pwErrorCode;}TMgcp_Stack_Param;#define CBK_MGCP_STACK_PARAM 1#define MGCP_USER_MSG 11#define MGCP_MAX_LOCAL_NAME_LEN 128 // localname 最长为 128 字节#define MGCP_USER_MSG_MAX_LEN 4090#define MGCP_MAX_CODE_LEN 32// 用户的动作#define USER_HOOK_ON 1#define USER_HOOK_OFF 2#define USER_HOOK_FLUSH 4#define USER_SEND_CODE 8// 内部消息格式 ------> 协议栈的回调函数发给主线程的内部消息格式typedef struct internal_msg_tag{ int imsg_type; union { TMgcp_Stack_Param tmgcp_stack_param; unsigned char pch_user_msg[MGCP_USER_MSG_MAX_LEN]; }u;}TInternal_Msg, *PTInternal_Msg;WORD gwCurPortVal = 15000;WORD GetRTPPort(void){ return gwCurPortVal++;}// 将发来的消息从 7777 端口转发给 6666 端口处理// pCbkMsg pwErrorCode 是MgcpEndpointProcessCrcxCmd( ... ) 中的临时变量的地址, 所以他们指区域的数据仅在此函数内有效LONG Cbk_MGCP_Stack(H_ENDPOINT pEndpointHandle, MGCP_CBK_MSG *pCbkMsg, WORD *pwErrorCode){ TInternal_Msg tinternal_msg; WORD *pwErrorCode_temp = NULL; MGCP_CBK_MSG *pCbkMsg_temp = NULL; if ((void *)pEndpointHandle == NULL || pCbkMsg == NULL || pwErrorCode == NULL) { return FAIL; } memset(&tinternal_msg, 0, sizeof(TInternal_Msg)); pCbkMsg_temp = (MGCP_CBK_MSG *)calloc(1, sizeof(MGCP_CBK_MSG)); pwErrorCode_temp = (WORD *)calloc(1, sizeof(WORD)); if (pCbkMsg_temp == NULL || pwErrorCode_temp == NULL) { return FAIL; } memcpy(pCbkMsg_temp, pCbkMsg, sizeof(MGCP_CBK_MSG)); *pwErrorCode_temp = *pwErrorCode; tinternal_msg.imsg_type = CBK_MGCP_STACK_PARAM; tinternal_msg.u.tmgcp_stack_param.pEndpointHandle = pEndpointHandle; tinternal_msg.u.tmgcp_stack_param.pCbkMsg = pCbkMsg_temp; tinternal_msg.u.tmgcp_stack_param.pwErrorCode = pwErrorCode_temp; if (sendto(ginternal_send_fd, &tinternal_msg, sizeof(TInternal_Msg), 0, (struct sockaddr *)&gsock_main_recv, sizeof(gsock_main_recv)) <= 0) { printf("\r\n %s : L%d\r\n", __FILE__, __LINE__); printf("\r\n send tinternal_msg fail\r\n"); return FAIL; } return OK;}void Cbk_Get_Connec_Params(H_ENDPOINT pEndpointHandle, DWORD dwConnecID, CONNECTION_PARAMETERS *pConnecParams){ printf("\r\n\t Cbk_Get_Connec_Params( ... ) \r\n\t"); }LONG Cbk_Sig_Check(H_ENDPOINT hEndpointHandle, MGCP_EVENT_ID *pSigID, BOOL bEmbeddedRqnt, WORD *pwErrorCode){ printf("\r\n\t Cbk_Sig_Check( ... ) \r\n\t"); return OK;}int Construct_Sdp_Desc(CONNECTION_DESCRIPTOR * ptlocal_sdp_desc, LOCAL_CONNEC_OPTS * plocal_connec_opts){ CONNECTION_FIELD * ptconn_field_temp = NULL; CONNECTION_ADDRESS * ptconn_addr_temp = NULL; MEDIA_DESCRIPTIONS * ptmedia_desc = NULL; char * pch_port; if (ptlocal_sdp_desc == NULL) { printf("\r\n %s : L%d\r\n", __FILE__, __LINE__); return FAIL; } memset(ptlocal_sdp_desc, 0, sizeof(CONNECTION_DESCRIPTOR)); pch_port = (char *)calloc(1, 6); if (pch_port == NULL) { printf("\r\n %s : L%d\r\n", __FILE__, __LINE__); return FAIL; } ptlocal_sdp_desc->pConnectionField = (CONNECTION_FIELD *)malloc(sizeof(CONNECTION_FIELD)); if (ptlocal_sdp_desc->pConnectionField == NULL) { printf("\r\n %s : L%d\r\n", __FILE__, __LINE__); free(pch_port); return FAIL; } ptconn_field_temp = ptlocal_sdp_desc->pConnectionField; memset(ptconn_field_temp, 0, sizeof(CONNECTION_FIELD)); ptconn_field_temp->pcNetType = "IN"; ptconn_field_temp->pcAddrType = "IP4"; ptconn_addr_temp = &ptconn_field_temp->ConnectionAddress; memset(ptconn_addr_temp, 0, sizeof(CONNECTION_ADDRESS)); ptconn_addr_temp->eType = ADDRESS_IPV4; ptconn_addr_temp->pcAddress = "192.168.7.40"; ptlocal_sdp_desc->wAttributeNum = 0; ptlocal_sdp_desc->wMediaNum = 1; ptlocal_sdp_desc->pMediaDesc = (MEDIA_DESCRIPTIONS *)malloc(sizeof(MEDIA_DESCRIPTIONS)); if (ptlocal_sdp_desc->pMediaDesc == NULL) { free(pch_port); free(ptlocal_sdp_desc->pConnectionField); printf("\r\n %s : L%d\r\n", __FILE__, __LINE__); return FAIL; } ptmedia_desc = ptlocal_sdp_desc->pMediaDesc; memset(ptmedia_desc, 0, sizeof(MEDIA_DESCRIPTIONS)); ptmedia_desc->MediaField.pcMedia = "audio"; sprintf(pch_port, "%d", GetRTPPort()); ptmedia_desc->MediaField.pcPort = pch_port; ptmedia_desc->MediaField.Protocol.eType = MEDIA_PROTOCOL_RTP; ptmedia_desc->MediaField.wNum = REAL_MEDIA_FORMAT_CNT; // 测试时最多支持 REAL_MEDIA_FORMAT_CNT 种编码方式 ptmedia_desc->MediaField.ppcFormat = (char **)malloc(sizeof(char *) * (REAL_MEDIA_FORMAT_CNT + 1)); if (ptmedia_desc->MediaField.ppcFormat == NULL) { free(pch_port); free(ptlocal_sdp_desc->pConnectionField); free(ptlocal_sdp_desc->pMediaDesc); printf("\r\n %s : L%d\r\n", __FILE__, __LINE__); return FAIL; } memset(ptmedia_desc->MediaField.ppcFormat, 0, sizeof(char *) * (REAL_MEDIA_FORMAT_CNT + 1)); if (plocal_connec_opts == NULL || plocal_connec_opts->pCompressAlgo == NULL) { // 没有指定编码方式 ptmedia_desc->MediaField.ppcFormat[0] = gpc_media_format[0]; ptmedia_desc->MediaField.ppcFormat[1] = gpc_media_format[1]; ptmedia_desc->MediaField.ppcFormat[2] = gpc_media_format[2]; ptmedia_desc->MediaField.ppcFormat[2] = gpc_media_format[3]; } else { int tt = 0; int iformat_cnt = 0; char ** ppc_format_list = NULL; int icur_format = 0; printf("\r\n %s : L%d", __FILE__, __LINE__); printf("\r\n plocal_connec_opts->pCompressAlgo == 0x%08X", (unsigned int)plocal_connec_opts->pCompressAlgo); usleep(1000*20); iformat_cnt = plocal_connec_opts->pCompressAlgo->wNum; ppc_format_list = plocal_connec_opts->pCompressAlgo->ppcAlgorithmList; for (; tt<iformat_cnt; tt++) { int kk = 0; for (; kk<REAL_MEDIA_FORMAT_CNT; kk++) { if (strcmp(ppc_format_list[tt], gpc_media_string[kk]) == 0) { ptmedia_desc->MediaField.ppcFormat[icur_format++] = gpc_media_format[kk]; } } } if (icur_format == 0) // 指定了编码方式, 但是我们不支持 { ptmedia_desc->MediaField.ppcFormat[0] = gpc_media_format[0]; ptmedia_desc->MediaField.ppcFormat[1] = gpc_media_format[1]; ptmedia_desc->MediaField.ppcFormat[2] = gpc_media_format[2]; ptmedia_desc->MediaField.ppcFormat[2] = gpc_media_format[3]; } else { ptmedia_desc->MediaField.wNum = icur_format; } } ptmedia_desc->wNum = 0; return OK;}int Destroy_Sdp_Desc(CONNECTION_DESCRIPTOR * ptlocal_sdp_desc){ if (ptlocal_sdp_desc == NULL) { printf("\r\n %s : L%d\r\n", __FILE__, __LINE__); return FAIL; } if (ptlocal_sdp_desc->pConnectionField != NULL) { free(ptlocal_sdp_desc->pConnectionField); } if (ptlocal_sdp_desc->pMediaDesc != NULL) { if (ptlocal_sdp_desc->pMediaDesc->MediaField.pcPort != NULL) { free(ptlocal_sdp_desc->pMediaDesc->MediaField.pcPort); } if (ptlocal_sdp_desc->pMediaDesc->MediaField.ppcFormat != NULL) { free(ptlocal_sdp_desc->pMediaDesc->MediaField.ppcFormat); } free(ptlocal_sdp_desc->pMediaDesc); } memset(ptlocal_sdp_desc, 0, sizeof(CONNECTION_DESCRIPTOR)); return OK;}int Print_Sdp_Desc(CONNECTION_DESCRIPTOR *ptconnection_desc){ int i = 0; int cnt = 0; printf("\r\n Print_Sdp_Desc( ... )"); if (ptconnection_desc == NULL) { printf("\r\n %s : L%d\r\n", __FILE__, __LINE__); return FAIL; } if (ptconnection_desc->pConnectionField == NULL) { printf("\r\n %s : L%d\r\n", __FILE__, __LINE__); return FAIL; } else { printf("\r\n pcNetType == %s", ptconnection_desc->pConnectionField->pcNetType); printf("\r\n pcAddrType == %s", ptconnection_desc->pConnectionField->pcAddrType); printf("\r\n pcAddress == %s", ptconnection_desc->pConnectionField->ConnectionAddress.pcAddress); } cnt = ptconnection_desc->wAttributeNum; if (cnt > 0) { if (ptconnection_desc->pAttributeFields == NULL) { printf("\r\n %s : L%d\r\n", __FILE__, __LINE__); return FAIL; } for (i=0; i<cnt; i++) { printf("\r\n pcAttibuteName == %s", ptconnection_desc->pAttributeFields[i].pcAttibuteName); printf("\r\n pcAttibuteValue == %s", ptconnection_desc->pAttributeFields[i].pcAttibuteValue); } } cnt = ptconnection_desc->wMediaNum; if (cnt <= 0) { printf("\r\n %s : L%d\r\n", __FILE__, __LINE__); return FAIL; } else { if (ptconnection_desc->pMediaDesc == NULL) { printf("\r\n %s : L%d\r\n", __FILE__, __LINE__); return FAIL; } for (i=0; i<cnt; i++) { MEDIA_FIELD * pmedia_field = &ptconnection_desc->pMediaDesc[i].MediaField; printf("\r\n pcMedia == %s", pmedia_field->pcMedia); printf("\r\n pcPort == %s", pmedia_field->pcPort); printf("\r\n pcInteger == %s", pmedia_field->pcInteger); switch (pmedia_field->Protocol.eType) { case MEDIA_PROTOCOL_RTP: { printf("\r\n Protocol == RTP/AVP"); break; } case MEDIA_PROTOCOL_UDP: { printf("\r\n Protocol == UDP"); break; } case MEDIA_PROTOCOL_OTHER: { printf("\r\n Protocol == %s", pmedia_field->Protocol.pcOtherProtocol); break; } } if (pmedia_field->wNum <= 0) { printf("\r\n %s : L%d\r\n", __FILE__, __LINE__); return FAIL; } else { int j = 0; printf("\r\n pmedia_field->wNum == %d", pmedia_field->wNum); printf("\r\n pmedia_field->ppcFormat : "); for (; j<pmedia_field->wNum; j++) { printf("%s ", pmedia_field->ppcFormat[j]); } } if (ptconnection_desc->pMediaDesc[i].wNum > 0) { int j = 0; if (ptconnection_desc->pMediaDesc[i].pAttributeFields == NULL) { printf("\r\n %s : L%d\r\n", __FILE__, __LINE__); return FAIL; } for (; j<cnt; j++) { printf("\r\n pcAttibuteName == %s", ptconnection_desc->pMediaDesc[i].pAttributeFields[j].pcAttibuteName); printf("\r\n pcAttibuteValue == %s", ptconnection_desc->pMediaDesc[i].pAttributeFields[j].pcAttibuteValue); } } } } return OK;}int Print_Mgcp_Cbk_Crcx_Msg(MGCP_CBK_CRCX *ptmgcp_cbk_crcx){ int i = 0; int cnt = 0; if (ptmgcp_cbk_crcx == NULL) { printf("\r\n %s : L%d\r\n", __FILE__, __LINE__); return FAIL; } printf("\r\n Print_Mgcp_Cbk_Crcx_Msg( ... )"); printf("\r\n ptmgcp_cbk_crcx->dwTransacID == %ld", ptmgcp_cbk_crcx->dwTransacID); printf("\r\n ptmgcp_cbk_crcx->pcCallId == %s", ptmgcp_cbk_crcx->pcCallId); printf("\r\n ptmgcp_cbk_crcx->dwConnectionID == %ld", ptmgcp_cbk_crcx->dwConnectionID); printf("\r\n ptmgcp_cbk_crcx->pLocalConnecOpt == 0x%08X", (unsigned int)ptmgcp_cbk_crcx->pLocalConnecOpt); if (ptmgcp_cbk_crcx->pLocalConnecOpt != NULL) { if (ptmgcp_cbk_crcx->pLocalConnecOpt->pPacketizationPeriod != NULL) { printf("\r\n ptmgcp_cbk_crcx->pLocalConnecOpt.pPacketizationPeriod == %d-%d", ptmgcp_cbk_crcx->pLocalConnecOpt->pPacketizationPeriod->wMinValue, ptmgcp_cbk_crcx->pLocalConnecOpt->pPacketizationPeriod->wMaxValue); } if (ptmgcp_cbk_crcx->pLocalConnecOpt->pCompressAlgo != NULL) { cnt = ptmgcp_cbk_crcx->pLocalConnecOpt->pCompressAlgo->wNum; printf("\r\n ptmgcp_cbk_crcx->pLocalConnecOpt.pCompressAlgo->wNum == %d : ", cnt); for (i=0; i<cnt; i++) { printf("%s ", ptmgcp_cbk_crcx->pLocalConnecOpt->pCompressAlgo->ppcAlgorithmList[i]); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -