Skip to content
Snippets Groups Projects
rtp.c 105 KiB
Newer Older
  • Learn to ignore specific revisions
  • 		if (!bridged->nat || (bridged->nat && (ast_test_flag(bridged, FLAG_NAT_ACTIVE) == FLAG_NAT_ACTIVE))) {
    			ast_log(LOG_DEBUG, "RTP Transmission error of packet to %s:%d: %s\n", ast_inet_ntoa(bridged->them.sin_addr), ntohs(bridged->them.sin_port), strerror(errno));
    		} else if (((ast_test_flag(bridged, FLAG_NAT_ACTIVE) == FLAG_NAT_INACTIVE) || rtpdebug) && !ast_test_flag(bridged, FLAG_NAT_INACTIVE_NOWARN)) {
    			if (option_debug || rtpdebug)
    				ast_log(LOG_DEBUG, "RTP NAT: Can't write RTP to private address %s:%d, waiting for other end to send audio...\n", ast_inet_ntoa(bridged->them.sin_addr), ntohs(bridged->them.sin_port));
    			ast_set_flag(bridged, FLAG_NAT_INACTIVE_NOWARN);
    
    		return -1;
    	} else if (rtp_debug_test_addr(&bridged->them))
    			ast_verbose("Sent RTP P2P packet to %s:%d (type %-2.2d, len %-6.6u)\n", ast_inet_ntoa(bridged->them.sin_addr), ntohs(bridged->them.sin_port), bridged_payload, len - hdrlen);
    
    struct ast_frame *ast_rtp_read(struct ast_rtp *rtp)
    {
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int res;
    	struct sockaddr_in sin;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	unsigned int seqno;
    
    	int version;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int payloadtype;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int hdrlen = 12;
    
    	unsigned int ssrc;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	unsigned int timestamp;
    	unsigned int *rtpheader;
    
    	struct rtpPayloadType rtpPT;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	
    	len = sizeof(sin);
    	
    
    	/* Cache where the header will go */
    
    	res = recvfrom(rtp->s, rtp->rawdata + AST_FRIENDLY_OFFSET, sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET,
    
    Mark Spencer's avatar
    Mark Spencer committed
    					0, (struct sockaddr *)&sin, &len);
    
    
    	rtpheader = (unsigned int *)(rtp->rawdata + AST_FRIENDLY_OFFSET);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (res < 0) {
    
    		if (errno != EAGAIN)
    
    			ast_log(LOG_WARNING, "RTP Read error: %s\n", strerror(errno));
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (errno == EBADF)
    			CRASH;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (res < hdrlen) {
    		ast_log(LOG_WARNING, "RTP Read too short\n");
    
    	/* Get fields */
    	seqno = ntohl(rtpheader[0]);
    
    	/* Check RTP version */
    	version = (seqno & 0xC0000000) >> 30;
    	if (!version) {
    		if ((stun_handle_packet(rtp->s, &sin, rtp->rawdata + AST_FRIENDLY_OFFSET, res) == STUN_ACCEPT) &&
    			(!rtp->them.sin_port && !rtp->them.sin_addr.s_addr)) {
    			memcpy(&rtp->them, &sin, sizeof(rtp->them));
    		}
    		return &ast_null_frame;
    	}
    
    
    	/* If we don't have the other side's address, then ignore this */
    
    	if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
    
    	/* Send to whoever send to us if NAT is turned on */
    
    	if (rtp->nat) {
    
    		if ((rtp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
    		    (rtp->them.sin_port != sin.sin_port)) {
    
    			rtp->them = sin;
    
    			if (rtp->rtcp) {
    
    				memcpy(&rtp->rtcp->them, &sin, sizeof(rtp->rtcp->them));
    				rtp->rtcp->them.sin_port = htons(ntohs(rtp->them.sin_port)+1);
    			}
    
    			ast_set_flag(rtp, FLAG_NAT_ACTIVE);
    			if (option_debug || rtpdebug)
    
    				ast_log(LOG_DEBUG, "RTP NAT: Got audio from other end. Now sending to address %s:%d\n", ast_inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port));
    
    	/* If we are bridged to another RTP stream, send direct */
    
    	if (ast_rtp_get_bridged(rtp) && !bridge_p2p_rtp_write(rtp, rtpheader, res, hdrlen))
    
    		return &ast_null_frame;
    
    	if (version != 2)
    		return &ast_null_frame;
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    	payloadtype = (seqno & 0x7f0000) >> 16;
    
    	padding = seqno & (1 << 29);
    
    	ext = seqno & (1 << 28);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	seqno &= 0xffff;
    	timestamp = ntohl(rtpheader[1]);
    
    	ssrc = ntohl(rtpheader[2]);
    	
    	if (!mark && rtp->rxssrc && rtp->rxssrc != ssrc) {
    
    		if (option_debug || rtpdebug)
    			ast_log(LOG_DEBUG, "Forcing Marker bit, because SSRC has changed\n");
    
    		mark = 1;
    	}
    
    	rtp->rxssrc = ssrc;
    
    	
    	if (padding) {
    		/* Remove padding bytes */
    		res -= rtp->rawdata[AST_FRIENDLY_OFFSET + res - 1];
    	}
    	
    
    	if (ext) {
    		/* RTP Extension present */
    		hdrlen += 4;
    
    		hdrlen += (ntohl(rtpheader[3]) & 0xffff) << 2;
    
    	if (res < hdrlen) {
    		ast_log(LOG_WARNING, "RTP Read too short (%d, expecting %d)\n", res, hdrlen);
    
    	rtp->rxcount++; /* Only count reasonably valid packets, this'll make the rtcp stats more accurate */
    
    	tseqno = rtp->lastrxseqno +1;
    
    
    	if (rtp->rxcount==1) {
    
    		/* This is the first RTP packet successfully received from source */
    		rtp->seedrxseqno = seqno;
    	}
    
    
    	if (rtp->rtcp && rtp->rtcp->schedid < 1) {
    
    		/* Schedule transmission of Receiver Report */
    		rtp->rtcp->schedid = ast_sched_add(rtp->sched, ast_rtcp_calc_interval(rtp), ast_rtcp_write, rtp);
    	}
    
    
    	if (tseqno > RTP_SEQ_MOD) { /* if tseqno is greater than RTP_SEQ_MOD it would indicate that the sender cycled */
    
    		rtp->cycles += RTP_SEQ_MOD;
    		ast_verbose("SEQNO cycled: %u\t%d\n", rtp->cycles, seqno);
    	}
    
    	rtp->lastrxseqno = seqno;
    	
    
    	if (rtp->themssrc==0)
    
    		rtp->themssrc = ntohl(rtpheader[2]); /* Record their SSRC to put in future RR */
    	
    
    	if (rtp_debug_test_addr(&sin))
    
    		ast_verbose("Got  RTP packet from    %s:%d (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
    
    			ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), payloadtype, seqno, timestamp,res - hdrlen);
    
    	rtpPT = ast_rtp_lookup_pt(rtp, payloadtype);
    
    	if (!rtpPT.isAstFormat) {
    
    		struct ast_frame *f = NULL;
    
    
    		/* This is special in-band data that's not one of our codecs */
    		if (rtpPT.code == AST_RTP_DTMF) {
    
    			/* It's special -- rfc2833 process it */
    
    			if (rtp_debug_test_addr(&sin)) {
    
    				unsigned char *data;
    				unsigned int event;
    				unsigned int event_end;
    				unsigned int duration;
    				data = rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen;
    				event = ntohl(*((unsigned int *)(data)));
    				event >>= 24;
    				event_end = ntohl(*((unsigned int *)(data)));
    				event_end <<= 8;
    				event_end >>= 24;
    				duration = ntohl(*((unsigned int *)(data)));
    				duration &= 0xFFFF;
    
    				ast_verbose("Got  RTP RFC2833 from   %s:%d (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u, mark %d, event %08x, end %d, duration %-5.5d) \n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), payloadtype, seqno, timestamp, res - hdrlen, (mark?1:0), event, ((event_end & 0x80)?1:0), duration);
    
    			if (rtp->lasteventseqn <= seqno || rtp->resp == 0 || (rtp->lasteventseqn >= 65530 && seqno <= 6)) {
    				f = process_rfc2833(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen, seqno);
    				rtp->lasteventseqn = seqno;
    
    		} else if (rtpPT.code == AST_RTP_CISCO_DTMF) {
    			/* It's really special -- process it the Cisco way */
    			if (rtp->lasteventseqn <= seqno || rtp->resp == 0 || (rtp->lasteventseqn >= 65530 && seqno <= 6)) {
    				f = process_cisco_dtmf(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
    				rtp->lasteventseqn = seqno;
    
    		} else if (rtpPT.code == AST_RTP_CN) {
    			/* Comfort Noise */
    			f = process_rfc3389(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
    		} else {
    
    			ast_log(LOG_NOTICE, "Unknown RTP codec %d received from '%s'\n", payloadtype, ast_inet_ntoa(rtp->them.sin_addr));
    
    		return f ? f : &ast_null_frame;
    
    	rtp->lastrxformat = rtp->f.subclass = rtpPT.code;
    	rtp->f.frametype = (rtp->f.subclass < AST_FORMAT_MAX_AUDIO) ? AST_FRAME_VOICE : AST_FRAME_VIDEO;
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    	if (!rtp->lastrxts)
    		rtp->lastrxts = timestamp;
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (rtp->dtmfcount) {
    #if 0
    		printf("dtmfcount was %d\n", rtp->dtmfcount);
    #endif		
    		rtp->dtmfcount -= (timestamp - rtp->lastrxts);
    		if (rtp->dtmfcount < 0)
    			rtp->dtmfcount = 0;
    #if 0
    		if (dtmftimeout != rtp->dtmfcount)
    			printf("dtmfcount is %d\n", rtp->dtmfcount);
    #endif
    	}
    	rtp->lastrxts = timestamp;
    
    	/* Send any pending DTMF */
    	if (rtp->resp && !rtp->dtmfcount) {
    
    		if (option_debug)
    			ast_log(LOG_DEBUG, "Sending pending DTMF\n");
    
    		return send_dtmf(rtp);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    	rtp->f.mallocd = 0;
    
    	rtp->f.datalen = res - hdrlen;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	rtp->f.data = rtp->rawdata + hdrlen + AST_FRIENDLY_OFFSET;
    	rtp->f.offset = hdrlen + AST_FRIENDLY_OFFSET;
    
    	if (rtp->f.subclass < AST_FORMAT_MAX_AUDIO) {
    
    		rtp->f.samples = ast_codec_get_samples(&rtp->f);
    		if (rtp->f.subclass == AST_FORMAT_SLINEAR) 
    
    		calc_rxstamp(&rtp->f.delivery, rtp, timestamp, mark);
    
    		/* Add timing data to let ast_generic_bridge() put the frame into a jitterbuf */
    		rtp->f.has_timing_info = 1;
    		rtp->f.ts = timestamp / 8;
    		rtp->f.len = rtp->f.samples / 8;
    		rtp->f.seqno = seqno;
    
    	} else {
    		/* Video -- samples is # of samples vs. 90000 */
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (!rtp->lastividtimestamp)
    			rtp->lastividtimestamp = timestamp;
    
    		rtp->f.samples = timestamp - rtp->lastividtimestamp;
    		rtp->lastividtimestamp = timestamp;
    
    		rtp->f.delivery.tv_sec = 0;
    		rtp->f.delivery.tv_usec = 0;
    
    		if (mark)
    			rtp->f.subclass |= 0x1;
    		
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    	rtp->f.src = "RTP";
    
    	return &rtp->f;
    
    /* The following array defines the MIME Media type (and subtype) for each
       of our codecs, or RTP-specific data type. */
    
    Mark Spencer's avatar
    Mark Spencer committed
    static struct {
    
    	struct rtpPayloadType payloadType;
    	char* type;
    	char* subtype;
    
    } mimeTypes[] = {
    
    	{{1, AST_FORMAT_G723_1}, "audio", "G723"},
    	{{1, AST_FORMAT_GSM}, "audio", "GSM"},
    	{{1, AST_FORMAT_ULAW}, "audio", "PCMU"},
    	{{1, AST_FORMAT_ALAW}, "audio", "PCMA"},
    	{{1, AST_FORMAT_G726}, "audio", "G726-32"},
    	{{1, AST_FORMAT_ADPCM}, "audio", "DVI4"},
    	{{1, AST_FORMAT_SLINEAR}, "audio", "L16"},
    	{{1, AST_FORMAT_LPC10}, "audio", "LPC"},
    	{{1, AST_FORMAT_G729A}, "audio", "G729"},
    	{{1, AST_FORMAT_SPEEX}, "audio", "speex"},
    	{{1, AST_FORMAT_ILBC}, "audio", "iLBC"},
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    	{{1, AST_FORMAT_G726_AAL2}, "audio", "AAL2-G726-32"},
    
    	{{0, AST_RTP_DTMF}, "audio", "telephone-event"},
    	{{0, AST_RTP_CISCO_DTMF}, "audio", "cisco-telephone-event"},
    	{{0, AST_RTP_CN}, "audio", "CN"},
    	{{1, AST_FORMAT_JPEG}, "video", "JPEG"},
    	{{1, AST_FORMAT_PNG}, "video", "PNG"},
    	{{1, AST_FORMAT_H261}, "video", "H261"},
    	{{1, AST_FORMAT_H263}, "video", "H263"},
    	{{1, AST_FORMAT_H263_PLUS}, "video", "h263-1998"},
    
    	{{1, AST_FORMAT_H264}, "video", "H264"},
    
    /* Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
       also, our own choices for dynamic payload types.  This is our master
       table for transmission */
    
    static struct rtpPayloadType static_RTP_PT[MAX_RTP_PT] = {
    
    #ifdef USE_DEPRECATED_G726
    
    	[2] = {1, AST_FORMAT_G726}, /* Technically this is G.721, but if Cisco can do it, so can we... */
    
    	[3] = {1, AST_FORMAT_GSM},
    	[4] = {1, AST_FORMAT_G723_1},
    	[5] = {1, AST_FORMAT_ADPCM}, /* 8 kHz */
    	[6] = {1, AST_FORMAT_ADPCM}, /* 16 kHz */
    	[7] = {1, AST_FORMAT_LPC10},
    	[8] = {1, AST_FORMAT_ALAW},
    	[10] = {1, AST_FORMAT_SLINEAR}, /* 2 channels */
    	[11] = {1, AST_FORMAT_SLINEAR}, /* 1 channel */
    	[13] = {0, AST_RTP_CN},
    	[16] = {1, AST_FORMAT_ADPCM}, /* 11.025 kHz */
    	[17] = {1, AST_FORMAT_ADPCM}, /* 22.050 kHz */
    	[18] = {1, AST_FORMAT_G729A},
    	[19] = {0, AST_RTP_CN},		/* Also used for CN */
    	[26] = {1, AST_FORMAT_JPEG},
    	[31] = {1, AST_FORMAT_H261},
    	[34] = {1, AST_FORMAT_H263},
    	[103] = {1, AST_FORMAT_H263_PLUS},
    	[97] = {1, AST_FORMAT_ILBC},
    
    	[99] = {1, AST_FORMAT_H264},
    
    	[101] = {0, AST_RTP_DTMF},
    	[110] = {1, AST_FORMAT_SPEEX},
    	[111] = {1, AST_FORMAT_G726},
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    	[112] = {1, AST_FORMAT_G726_AAL2},
    
    	[121] = {0, AST_RTP_CISCO_DTMF}, /* Must be type 121 */
    
    void ast_rtp_pt_clear(struct ast_rtp* rtp) 
    {
    
    	for (i = 0; i < MAX_RTP_PT; ++i) {
    		rtp->current_RTP_PT[i].isAstFormat = 0;
    		rtp->current_RTP_PT[i].code = 0;
    	}
    
    	rtp->rtp_lookup_code_cache_isAstFormat = 0;
    	rtp->rtp_lookup_code_cache_code = 0;
    	rtp->rtp_lookup_code_cache_result = 0;
    
    void ast_rtp_pt_default(struct ast_rtp* rtp) 
    {
    
    	int i;
    
    	/* Initialize to default payload types */
    	for (i = 0; i < MAX_RTP_PT; ++i) {
    		rtp->current_RTP_PT[i].isAstFormat = static_RTP_PT[i].isAstFormat;
    		rtp->current_RTP_PT[i].code = static_RTP_PT[i].code;
    	}
    
    	rtp->rtp_lookup_code_cache_isAstFormat = 0;
    	rtp->rtp_lookup_code_cache_code = 0;
    	rtp->rtp_lookup_code_cache_result = 0;
    
    void ast_rtp_pt_copy(struct ast_rtp *dest, const struct ast_rtp *src)
    
    	for (i=0; i < MAX_RTP_PT; ++i) {
    		dest->current_RTP_PT[i].isAstFormat = 
    			src->current_RTP_PT[i].isAstFormat;
    		dest->current_RTP_PT[i].code = 
    			src->current_RTP_PT[i].code; 
    	}
    	dest->rtp_lookup_code_cache_isAstFormat = 0;
    	dest->rtp_lookup_code_cache_code = 0;
    	dest->rtp_lookup_code_cache_result = 0;
    }
    
    
    /*! \brief Get channel driver interface structure */
    
    static struct ast_rtp_protocol *get_proto(struct ast_channel *chan)
    {
    
    	struct ast_rtp_protocol *cur = NULL;
    
    	AST_LIST_LOCK(&protos);
    	AST_LIST_TRAVERSE(&protos, cur, list) {
    
    		if (cur->type == chan->tech->type)
    
    int ast_rtp_early_bridge(struct ast_channel *dest, struct ast_channel *src)
    
    	struct ast_rtp *destp = NULL, *srcp = NULL;		/* Audio RTP Channels */
    	struct ast_rtp *vdestp = NULL, *vsrcp = NULL;		/* Video RTP channels */
    	struct ast_rtp_protocol *destpr = NULL, *srcpr = NULL;
    	enum ast_rtp_get_result audio_dest_res = AST_RTP_GET_FAILED, video_dest_res = AST_RTP_GET_FAILED;
    	enum ast_rtp_get_result audio_src_res = AST_RTP_GET_FAILED, video_src_res = AST_RTP_GET_FAILED;
    
    	/* Lock channels */
    	ast_channel_lock(dest);
    	if (src) {
    		while(ast_channel_trylock(src)) {
    			ast_channel_unlock(dest);
    			usleep(1);
    			ast_channel_lock(dest);
    		}
    	}
    
    	/* Find channel driver interfaces */
    	destpr = get_proto(dest);
    	if (src)
    		srcpr = get_proto(src);
    	if (!destpr) {
    		if (option_debug)
    			ast_log(LOG_DEBUG, "Channel '%s' has no RTP, not doing anything\n", dest->name);
    		ast_channel_unlock(dest);
    		if (src)
    			ast_channel_unlock(src);
    		return 0;
    	}
    	if (!srcpr) {
    		if (option_debug)
    
    Mark Spencer's avatar
    Mark Spencer committed
    			ast_log(LOG_DEBUG, "Channel '%s' has no RTP, not doing anything\n", src ? src->name : "<unspecified>");
    
    		ast_channel_unlock(dest);
    		if (src)
    			ast_channel_unlock(src);
    		return 0;
    	}
    
    	/* Get audio and video interface (if native bridge is possible) */
    
    	audio_dest_res = destpr->get_rtp_info(dest, &destp);
    	video_dest_res = destpr->get_vrtp_info ? destpr->get_vrtp_info(dest, &vdestp) : AST_RTP_GET_FAILED;
    
    		audio_src_res = srcpr->get_rtp_info(src, &srcp);
    		video_src_res = srcpr->get_vrtp_info ? srcpr->get_vrtp_info(src, &vsrcp) : AST_RTP_GET_FAILED;
    
    	}
    
    	/* Check if bridge is still possible (In SIP canreinvite=no stops this, like NAT) */
    
    	if (audio_dest_res != AST_RTP_TRY_NATIVE) {
    
    		/* Somebody doesn't want to play... */
    		ast_channel_unlock(dest);
    		if (src)
    			ast_channel_unlock(src);
    		return 0;
    	}
    
    	if (audio_src_res == AST_RTP_TRY_NATIVE && srcpr->get_codec)
    
    		srccodec = srcpr->get_codec(src);
    	else
    		srccodec = 0;
    	/* Consider empty media as non-existant */
    
    	if (audio_src_res == AST_RTP_TRY_NATIVE && !srcp->them.sin_addr.s_addr)
    
    	if (destpr->set_rtp_peer(dest, srcp, vsrcp, srccodec, srcp ? ast_test_flag(srcp, FLAG_NAT_ACTIVE) : 0))
    
    		ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", dest->name, src ? src->name : "<unspecified>");
    
    	ast_channel_unlock(dest);
    	if (src)
    		ast_channel_unlock(src);
    	if (option_debug)
    
    		ast_log(LOG_DEBUG, "Setting early bridge SDP of '%s' with that of '%s'\n", dest->name, src ? src->name : "<unspecified>");
    
    	return 1;
    }
    
    int ast_rtp_make_compatible(struct ast_channel *dest, struct ast_channel *src, int media)
    
    	struct ast_rtp *destp = NULL, *srcp = NULL;		/* Audio RTP Channels */
    	struct ast_rtp *vdestp = NULL, *vsrcp = NULL;		/* Video RTP channels */
    	struct ast_rtp_protocol *destpr = NULL, *srcpr = NULL;
    	enum ast_rtp_get_result audio_dest_res = AST_RTP_GET_FAILED, video_dest_res = AST_RTP_GET_FAILED;
    	enum ast_rtp_get_result audio_src_res = AST_RTP_GET_FAILED, video_src_res = AST_RTP_GET_FAILED; 
    
    	ast_channel_lock(dest);
    	while(ast_channel_trylock(src)) {
    		ast_channel_unlock(dest);
    
    		if (option_debug)
    			ast_log(LOG_DEBUG, "Channel '%s' has no RTP, not doing anything\n", dest->name);
    
    		ast_channel_unlock(dest);
    		ast_channel_unlock(src);
    
    		if (option_debug)
    			ast_log(LOG_DEBUG, "Channel '%s' has no RTP, not doing anything\n", src->name);
    
    		ast_channel_unlock(dest);
    		ast_channel_unlock(src);
    
    		return 0;
    	}
    
    	/* Get audio and video interface (if native bridge is possible) */
    
    	audio_dest_res = destpr->get_rtp_info(dest, &destp);
    	video_dest_res = destpr->get_vrtp_info ? destpr->get_vrtp_info(dest, &vdestp) : AST_RTP_GET_FAILED;
    	audio_src_res = srcpr->get_rtp_info(src, &srcp);
    	video_src_res = srcpr->get_vrtp_info ? srcpr->get_vrtp_info(src, &vsrcp) : AST_RTP_GET_FAILED;
    
    
    	/* Check if bridge is still possible (In SIP canreinvite=no stops this, like NAT) */
    
    	if (audio_dest_res != AST_RTP_TRY_NATIVE || audio_src_res != AST_RTP_TRY_NATIVE) {
    
    		/* Somebody doesn't want to play... */
    
    		ast_channel_unlock(dest);
    		ast_channel_unlock(src);
    
    		return 0;
    	}
    	ast_rtp_pt_copy(destp, srcp);
    	if (vdestp && vsrcp)
    		ast_rtp_pt_copy(vdestp, vsrcp);
    
    	if (srcpr->get_codec)
    		srccodec = srcpr->get_codec(src);
    	else
    		srccodec = 0;
    	if (media) {
    
    		if (destpr->set_rtp_peer(dest, srcp, vsrcp, srccodec, ast_test_flag(srcp, FLAG_NAT_ACTIVE)))
    
    			ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", dest->name, src->name);
    
    	ast_channel_unlock(dest);
    	ast_channel_unlock(src);
    
    	if (option_debug)
    		ast_log(LOG_DEBUG, "Seeded SDP of '%s' with that of '%s'\n", dest->name, src->name);
    
    /*! \brief  Make a note of a RTP payload type that was seen in a SDP "m=" line.
    
     * By default, use the well-known value for this type (although it may 
     * still be set to a different value by a subsequent "a=rtpmap:" line)
     */
    
    void ast_rtp_set_m_type(struct ast_rtp* rtp, int pt) 
    {
    
    	if (pt < 0 || pt > MAX_RTP_PT) 
    		return; /* bogus payload type */
    
    		rtp->current_RTP_PT[pt] = static_RTP_PT[pt];
    
    /*! \brief Make a note of a RTP payload type (with MIME type) that was seen in
    
     * an SDP "a=rtpmap:" line.
     */
    void ast_rtp_set_rtpmap_type(struct ast_rtp *rtp, int pt,
    			     char *mimeType, char *mimeSubtype,
    			     enum ast_rtp_options options)
    
    	if (pt < 0 || pt > MAX_RTP_PT) 
    
    		return; /* bogus payload type */
    
    	for (i = 0; i < sizeof(mimeTypes)/sizeof(mimeTypes[0]); ++i) {
    
    		if (strcasecmp(mimeSubtype, mimeTypes[i].subtype) == 0 &&
    
    		    strcasecmp(mimeType, mimeTypes[i].type) == 0) {
    
    			rtp->current_RTP_PT[pt] = mimeTypes[i].payloadType;
    
    			if ((mimeTypes[i].payloadType.code == AST_FORMAT_G726) &&
    			    mimeTypes[i].payloadType.isAstFormat &&
    			    (options & AST_RTP_OPT_G726_NONSTANDARD))
    				rtp->current_RTP_PT[pt].code = AST_FORMAT_G726_AAL2;
    
    /*! \brief Return the union of all of the codecs that were set by rtp_set...() calls 
     * They're returned as two distinct sets: AST_FORMATs, and AST_RTPs */
    
    void ast_rtp_get_current_formats(struct ast_rtp* rtp,
    
    			     int* astFormats, int* nonAstFormats) {
    
    	int pt;
    
    	*astFormats = *nonAstFormats = 0;
    	for (pt = 0; pt < MAX_RTP_PT; ++pt) {
    		if (rtp->current_RTP_PT[pt].isAstFormat) {
    			*astFormats |= rtp->current_RTP_PT[pt].code;
    		} else {
    			*nonAstFormats |= rtp->current_RTP_PT[pt].code;
    		}
    	}
    
    struct rtpPayloadType ast_rtp_lookup_pt(struct ast_rtp* rtp, int pt) 
    {
    
    	struct rtpPayloadType result;
    
    	result.isAstFormat = result.code = 0;
    	if (pt < 0 || pt > MAX_RTP_PT) 
    		return result; /* bogus payload type */
    
    
    	/* Start with negotiated codecs */
    
    
    	/* If it doesn't exist, check our static RTP type list, just in case */
    	if (!result.code) 
    		result = static_RTP_PT[pt];
    	return result;
    
    /*! \brief Looks up an RTP code out of our *static* outbound list */
    
    int ast_rtp_lookup_code(struct ast_rtp* rtp, const int isAstFormat, const int code) {
    
    	if (isAstFormat == rtp->rtp_lookup_code_cache_isAstFormat &&
    		code == rtp->rtp_lookup_code_cache_code) {
    
    		/* Use our cached mapping, to avoid the overhead of the loop below */
    		return rtp->rtp_lookup_code_cache_result;
    	}
    
    	/* Check the dynamic list first */
    
    	for (pt = 0; pt < MAX_RTP_PT; ++pt) {
    
    		if (rtp->current_RTP_PT[pt].code == code && rtp->current_RTP_PT[pt].isAstFormat == isAstFormat) {
    
    			rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
    			rtp->rtp_lookup_code_cache_code = code;
    			rtp->rtp_lookup_code_cache_result = pt;
    			return pt;
    		}
    	}
    
    
    	/* Then the static list */
    
    	for (pt = 0; pt < MAX_RTP_PT; ++pt) {
    		if (static_RTP_PT[pt].code == code && static_RTP_PT[pt].isAstFormat == isAstFormat) {
    			rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
      			rtp->rtp_lookup_code_cache_code = code;
    			rtp->rtp_lookup_code_cache_result = pt;
    			return pt;
    		}
    	}
    	return -1;
    
    Mark Spencer's avatar
    Mark Spencer committed
    }
    
    const char *ast_rtp_lookup_mime_subtype(const int isAstFormat, const int code,
    				  enum ast_rtp_options options)
    
    	for (i = 0; i < sizeof(mimeTypes)/sizeof(mimeTypes[0]); ++i) {
    		if ((mimeTypes[i].payloadType.code == code) && (mimeTypes[i].payloadType.isAstFormat == isAstFormat)) {
    			if (isAstFormat &&
    			    (code == AST_FORMAT_G726_AAL2) &&
    			    (options & AST_RTP_OPT_G726_NONSTANDARD))
    				return "AAL2-G726-32";
    			else
    				return mimeTypes[i].subtype;
    		}
    
    char *ast_rtp_lookup_mime_multiple(char *buf, size_t size, const int capability,
    				   const int isAstFormat, enum ast_rtp_options options)
    
    	unsigned len;
    	char *end = buf;
    	char *start = buf;
    
    	snprintf(end, size, "0x%x (", capability);
    
    	len = strlen(end);
    	end += len;
    	size -= len;
    	start = end;
    
    
    	for (format = 1; format < AST_RTP_MAX; format <<= 1) {
    		if (capability & format) {
    
    			const char *name = ast_rtp_lookup_mime_subtype(isAstFormat, format, options);
    
    
    			snprintf(end, size, "%s|", name);
    			len = strlen(end);
    			end += len;
    			size -= len;
    
    	if (start == end)
    		snprintf(start, size, "nothing)"); 
    	else if (size > 1)
    		*(end -1) = ')';
    	
    
    Mark Spencer's avatar
    Mark Spencer committed
    static int rtp_socket(void)
    {
    	int s;
    	long flags;
    	s = socket(AF_INET, SOCK_DGRAM, 0);
    	if (s > -1) {
    		flags = fcntl(s, F_GETFL);
    		fcntl(s, F_SETFL, flags | O_NONBLOCK);
    
    		if (nochecksums)
    			setsockopt(s, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
    
    /*!
     * \brief Initialize a new RTCP session.
     * 
     * \returns The newly initialized RTCP session.
     */
    
    static struct ast_rtcp *ast_rtcp_new(void)
    {
    	struct ast_rtcp *rtcp;
    
    
    	if (!(rtcp = ast_calloc(1, sizeof(*rtcp))))
    
    Mark Spencer's avatar
    Mark Spencer committed
    	rtcp->s = rtp_socket();
    
    	rtcp->us.sin_family = AF_INET;
    
    		ast_log(LOG_WARNING, "Unable to allocate RTCP socket: %s\n", strerror(errno));
    
    struct ast_rtp *ast_rtp_new_with_bindaddr(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode, struct in_addr addr)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    	struct ast_rtp *rtp;
    	int x;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int first;
    
    	int startplace;
    
    	
    	if (!(rtp = ast_calloc(1, sizeof(*rtp))))
    
    Mark Spencer's avatar
    Mark Spencer committed
    		return NULL;
    	rtp->them.sin_family = AF_INET;
    	rtp->us.sin_family = AF_INET;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	rtp->s = rtp_socket();
    
    	rtp->ssrc = ast_random();
    	rtp->seqno = ast_random() & 0xffff;
    
    	ast_set_flag(rtp, FLAG_HAS_DTMF);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (rtp->s < 0) {
    		free(rtp);
    
    		ast_log(LOG_ERROR, "Unable to allocate socket: %s\n", strerror(errno));
    
    Mark Spencer's avatar
    Mark Spencer committed
    		return NULL;
    	}
    
    	if (sched && rtcpenable) {
    		rtp->sched = sched;
    		rtp->rtcp = ast_rtcp_new();
    	}
    
    	
    	/* Select a random port number in the range of possible RTP */
    
    	x = (ast_random() % (rtpend-rtpstart)) + rtpstart;
    
    	x = x & ~1;
    
    	/* Save it for future references. */
    
    	startplace = x;
    
    	/* Iterate tring to bind that port and incrementing it otherwise untill a port was found or no ports are available. */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	for (;;) {
    		/* Must be an even port number by RTP spec */
    		rtp->us.sin_port = htons(x);
    
    		/* If there's rtcp, initialize it as well. */
    
    		if (rtp->rtcp)
    			rtp->rtcp->us.sin_port = htons(x + 1);
    
    		/* Try to bind it/them. */
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (!(first = bind(rtp->s, (struct sockaddr *)&rtp->us, sizeof(rtp->us))) &&
    
    			(!rtp->rtcp || !bind(rtp->rtcp->s, (struct sockaddr *)&rtp->rtcp->us, sizeof(rtp->rtcp->us))))
    
    Mark Spencer's avatar
    Mark Spencer committed
    			break;
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (!first) {
    			/* Primary bind succeeded! Gotta recreate it */
    			close(rtp->s);
    			rtp->s = rtp_socket();
    		}
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (errno != EADDRINUSE) {
    
    			/* We got an error that wasn't expected, abort! */
    
    			ast_log(LOG_ERROR, "Unexpected bind error: %s\n", strerror(errno));
    
    Mark Spencer's avatar
    Mark Spencer committed
    			close(rtp->s);
    
    			if (rtp->rtcp) {
    				close(rtp->rtcp->s);
    				free(rtp->rtcp);
    			}
    
    Mark Spencer's avatar
    Mark Spencer committed
    			free(rtp);
    			return NULL;
    		}
    
    		/* The port was used, increment it (by two). */
    
    		x += 2;
    
    		/* Did we go over the limit ? */
    
    		if (x > rtpend)
    
    			/* then, start from the begingig. */
    
    			x = (rtpstart + 1) & ~1;
    
    		/* Check if we reached the place were we started. */
    
    		if (x == startplace) {
    
    			/* If so, there's no ports available. */
    
    			ast_log(LOG_ERROR, "No RTP ports remaining. Can't setup media stream for this call.\n");
    
    			close(rtp->s);
    
    			if (rtp->rtcp) {
    				close(rtp->rtcp->s);
    				free(rtp->rtcp);
    			}
    
    			free(rtp);
    			return NULL;
    		}
    
    		rtp->ioid = ast_io_add(rtp->io, rtp->s, rtpread, AST_IO_IN, rtp);
    
    	ast_rtp_pt_default(rtp);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return rtp;
    }
    
    
    struct ast_rtp *ast_rtp_new(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode)
    {
    	struct in_addr ia;
    
    	memset(&ia, 0, sizeof(ia));
    	return ast_rtp_new_with_bindaddr(sched, io, rtcpenable, callbackmode, ia);
    }
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    int ast_rtp_settos(struct ast_rtp *rtp, int tos)
    {
    	int res;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if ((res = setsockopt(rtp->s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)))) 
    
    Mark Spencer's avatar
    Mark Spencer committed
    		ast_log(LOG_WARNING, "Unable to set TOS to %d\n", tos);
    	return res;
    }
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    void ast_rtp_set_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
    {
    	rtp->them.sin_port = them->sin_port;
    	rtp->them.sin_addr = them->sin_addr;
    
    	if (rtp->rtcp) {
    		rtp->rtcp->them.sin_port = htons(ntohs(them->sin_port) + 1);
    		rtp->rtcp->them.sin_addr = them->sin_addr;
    	}
    
    int ast_rtp_get_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	if ((them->sin_family != AF_INET) ||
    		(them->sin_port != rtp->them.sin_port) ||
    		(them->sin_addr.s_addr != rtp->them.sin_addr.s_addr)) {
    		them->sin_family = AF_INET;
    		them->sin_port = rtp->them.sin_port;
    		them->sin_addr = rtp->them.sin_addr;
    		return 1;
    	}
    	return 0;
    
    Mark Spencer's avatar
    Mark Spencer committed
    void ast_rtp_get_us(struct ast_rtp *rtp, struct sockaddr_in *us)
    {
    
    struct ast_rtp *ast_rtp_get_bridged(struct ast_rtp *rtp)
    {
    	return rtp->bridged;
    }
    
    
    void ast_rtp_stop(struct ast_rtp *rtp)
    {
    
    	if (rtp->rtcp && rtp->rtcp->schedid > 0) {
    
    		ast_sched_del(rtp->sched, rtp->rtcp->schedid);
    		rtp->rtcp->schedid = -1;
    	}
    
    
    	memset(&rtp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
    	memset(&rtp->them.sin_port, 0, sizeof(rtp->them.sin_port));
    
    		memset(&rtp->rtcp->them.sin_addr, 0, sizeof(rtp->rtcp->them.sin_addr));
    		memset(&rtp->rtcp->them.sin_port, 0, sizeof(rtp->rtcp->them.sin_port));
    
    Mark Spencer's avatar
    Mark Spencer committed
    void ast_rtp_reset(struct ast_rtp *rtp)
    {
    	memset(&rtp->rxcore, 0, sizeof(rtp->rxcore));
    	memset(&rtp->txcore, 0, sizeof(rtp->txcore));
    	memset(&rtp->dtmfmute, 0, sizeof(rtp->dtmfmute));
    	rtp->lastts = 0;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	rtp->lastrxts = 0;
    	rtp->lastividtimestamp = 0;
    	rtp->lastovidtimestamp = 0;
    	rtp->lasteventseqn = 0;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	rtp->lasttxformat = 0;
    	rtp->lastrxformat = 0;
    	rtp->dtmfcount = 0;
    	rtp->dtmfduration = 0;
    	rtp->seqno = 0;
    	rtp->rxseqno = 0;
    }
    
    
    char *ast_rtp_get_quality(struct ast_rtp *rtp)
    {
    	/*
    	*ssrc          our ssrc
    	*themssrc      their ssrc
    	*lp            lost packets
    	*rxjitter      our calculated jitter(rx)
    	*rxcount       no. received packets
    	*txjitter      reported jitter of the other end
    	*txcount       transmitted packets
    	*rlp           remote lost packets
    	*/
    	
    	snprintf(rtp->rtcp->quality, sizeof(rtp->rtcp->quality), "ssrc=%u;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;txjitter=%f;txcount=%u;rlp=%u;rtt=%f", rtp->ssrc, rtp->themssrc, rtp->rtcp->expected_prior - rtp->rtcp->received_prior, rtp->rxjitter, rtp->rxcount, (double)rtp->rtcp->reported_jitter/65536., rtp->txcount, rtp->rtcp->reported_lost, rtp->rtcp->rtt);
    	
    	return rtp->rtcp->quality;
    }
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    void ast_rtp_destroy(struct ast_rtp *rtp)
    {
    
    	if (rtcp_debug_test_addr(&rtp->them) || rtcpstats) {
    
    		/*Print some info on the call here */
    		ast_verbose("  RTP-stats\n");
    		ast_verbose("* Our Receiver:\n");
    		ast_verbose("  SSRC:		 %u\n", rtp->themssrc);
    		ast_verbose("  Received packets: %u\n", rtp->rxcount);
    		ast_verbose("  Lost packets:	 %u\n", rtp->rtcp->expected_prior - rtp->rtcp->received_prior);
    		ast_verbose("  Jitter:		 %.4f\n", rtp->rxjitter);
    		ast_verbose("  Transit:		 %.4f\n", rtp->rxtransit);
    		ast_verbose("  RR-count:	 %u\n", rtp->rtcp->rr_count);
    		ast_verbose("* Our Sender:\n");
    		ast_verbose("  SSRC:		 %u\n", rtp->ssrc);
    		ast_verbose("  Sent packets:	 %u\n", rtp->txcount);
    		ast_verbose("  Lost packets:	 %u\n", rtp->rtcp->reported_lost);
    		ast_verbose("  Jitter:		 %u\n", rtp->rtcp->reported_jitter);
    		ast_verbose("  SR-count:	 %u\n", rtp->rtcp->sr_count);
    		ast_verbose("  RTT:		 %f\n", rtp->rtcp->rtt);
    	}
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (rtp->smoother)
    		ast_smoother_free(rtp->smoother);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (rtp->ioid)
    		ast_io_remove(rtp->io, rtp->ioid);
    	if (rtp->s > -1)
    		close(rtp->s);
    
    		if (rtp->rtcp->schedid > 0)
    			ast_sched_del(rtp->sched, rtp->rtcp->schedid);
    
    		close(rtp->rtcp->s);
    		free(rtp->rtcp);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	free(rtp);
    }
    
    
    static unsigned int calc_txstamp(struct ast_rtp *rtp, struct timeval *delivery)
    
    	struct timeval t;
    	long ms;
    	if (ast_tvzero(rtp->txcore)) {
    		rtp->txcore = ast_tvnow();
    
    		/* Round to 20ms for nice, pretty timestamps */
    
    Mark Spencer's avatar
    Mark Spencer committed
    		rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000;
    
    	/* Use previous txcore if available */
    	t = (delivery && !ast_tvzero(*delivery)) ? *delivery : ast_tvnow();
    	ms = ast_tvdiff_ms(t, rtp->txcore);
    
    	if (ms < 0)
    		ms = 0;
    
    	/* Use what we just got for next time */
    	rtp->txcore = t;
    	return (unsigned int) ms;
    
    Mark Spencer's avatar
    Mark Spencer committed
    int ast_rtp_senddigit(struct ast_rtp *rtp, char digit)
    {
    	unsigned int *rtpheader;
    	int hdrlen = 12;
    	int res;
    	int x;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	char data[256];
    
    	if ((digit <= '9') && (digit >= '0'))
    		digit -= '0';
    	else if (digit == '*')
    		digit = 10;
    	else if (digit == '#')
    		digit = 11;
    	else if ((digit >= 'A') && (digit <= 'D')) 
    		digit = digit - 'A' + 12;
    	else if ((digit >= 'a') && (digit <= 'd')) 
    		digit = digit - 'a' + 12;
    	else {
    		ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
    		return -1;
    	}
    
    	payload = ast_rtp_lookup_code(rtp, 0, AST_RTP_DTMF);
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    	/* If we have no peer, return immediately */	
    	if (!rtp->them.sin_addr.s_addr)
    		return 0;
    
    
    	rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
    
    Mark Spencer's avatar
    Mark Spencer committed
    	
    	/* Get a pointer to the header */
    	rtpheader = (unsigned int *)data;
    
    	rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno));
    
    	rtpheader[1] = htonl(rtp->lastdigitts);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	rtpheader[2] = htonl(rtp->ssrc);