gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
c89
只是想知道有没有更好的方法来使用我在下面提供的代码来做到这一点。
我正在从一些参数构建一个 sdp(会话描述协议)字符串。
但是,我可能需要扩展 sdp 以包含其他参数,即视频编解码器。
但是,我真的不希望有另一个 if else 来构建我在下面完成的完整字符串。
我只是想知道这是否足够可扩展?有什么我可以使用的技术比我所做的更好。
我刚刚复制了构建 sdp 的函数:
void create_sdp_string(char *sdp_string, char reinvite)
{
char session_id[MAX_STRING_LEN];
char session_version[MAX_STRING_LEN];
const char *local_ip_addr = "10.10.10.244";
apr_time_t time_usec = 0;
char session_identifier[MAX_STRING_LEN];
char media_transport[MAX_STRING_LEN];
char connection_info[MAX_STRING_LEN];
const char *audio_port = "49152";
/* Required sdp attributes */
#define V_PROTOCOL_VERSION "0"
#define USERNAME "JOEBLOGGS"
#define NETTYPE "IN"
#define ADDR_TYPE "IP4"
#define S_SESSION_NAME "SIP_CALL"
#define T_TIME_DESCRIPTION "0 0"
#define M_MEDIA_NAME_TRANSPORT_ADDR "RTP/AVP 0 8 101"
#define A_PCMU "rtpmap:0 PCMU/8000"
#define A_PCMA "rtpmap:8 PCMA/8000"
#define A_TELEPHONE "rtpmap:101 telephone-event/8000"
/* Get the time in micro seconds to create an unique session id */
time_usec = apr_time_usec(apr_time_now());
apr_snprintf(session_id, MAX_STRING_LEN, "%lu", (unsigned long)time_usec);
/* Get the time in micro seconds to create an unique session version */
time_usec = apr_time_usec(apr_time_now());
apr_snprintf(session_version, MAX_STRING_LEN, "%lu", (unsigned long)time_usec);
/* Build session identifier */
apr_snprintf(session_identifier,
MAX_STRING_LEN,
"o="USERNAME" "NETTYPE" %s %s "ADDR_TYPE" %s\n",
session_id,
session_version,
local_ip_addr);
/* Build media transport */
apr_snprintf(media_transport,
MAX_STRING_LEN,
"m=audio %s "M_MEDIA_NAME_TRANSPORT_ADDR"\n",
audio_port);
/* Build connection information */
apr_snprintf(connection_info,
MAX_STRING_LEN,
"c="NETTYPE" "ADDR_TYPE" %s\n",
local_ip_addr);
if(reinvite == FALSE) {
/* Build complete sdp string for cases that are not reinvite */
apr_snprintf(sdp_string,
MAX_SDP_STRING_LEN,
"v="V_PROTOCOL_VERSION"\n"
"%s"
"s="S_SESSION_NAME"\n"
"%s"
"t="T_TIME_DESCRIPTION"\n"
"%s"
"a="A_PCMU"\n"
"a="A_PCMA"\n"
"a="A_TELEPHONE"\n",
session_identifier,
connection_info,
media_transport);
}
else if(reinvite == TRUE) {
/* Build complete sdp string for cases that are for reinvite 'inactive' */
#define INACTIVE "inactive"
apr_snprintf(sdp_string,
MAX_SDP_STRING_LEN,
"v="V_PROTOCOL_VERSION"\n"
"%s"
"s="S_SESSION_NAME"\n"
"%s"
"t="T_TIME_DESCRIPTION"\n"
"%s"
"a="A_PCMU"\n"
"a="A_PCMA"\n"
"a="A_TELEPHONE"\n"
"a="INACTIVE"\n",
session_identifier,
connection_info,
media_transport);
}
}