1.绑定到一个操作系统随机分配的udp端口,绑定成功后获取到这个端口
int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(fd < 0) {
JANUS_LOG(LOG_ERR, "Error creating RTCP socket for new RTP forwarder... %d (%s)\n",
errno, strerror(errno));
return 0;
}
struct sockaddr_in address;
socklen_t len = sizeof(address);
memset(&address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_port = htons(0); /* The RTCP port we received is the remote one */
address.sin_addr.s_addr = INADDR_ANY;
if(bind(fd, (struct sockaddr *)&address, sizeof(struct sockaddr)) < 0 ||
getsockname(fd, (struct sockaddr *)&address, &len) < 0) {
JANUS_LOG(LOG_ERR, "Error binding RTCP socket for new RTP forwarder... %d (%s)\n",
errno, strerror(errno));
close(fd);
return 0;
}
local_rtcp_port = ntohs(address.sin_port);
JANUS_LOG(LOG_VERB, "Bound local %s RTCP port: %"SCNu16"\n",
is_video ? "video" : "audio", local_rtcp_port);