[asterisk-commits] file: branch 1.4 r48506 - /branches/1.4/main/rtp.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Fri Dec 15 12:55:29 MST 2006


Author: file
Date: Fri Dec 15 13:55:28 2006
New Revision: 48506

URL: http://svn.digium.com/view/asterisk?view=rev&rev=48506
Log:
Turn payload_lock into bridge_lock and make it encompass all RTP structure contents that may relate to bridge information, including who we are bridged to.

Modified:
    branches/1.4/main/rtp.c

Modified: branches/1.4/main/rtp.c
URL: http://svn.digium.com/view/asterisk/branches/1.4/main/rtp.c?view=diff&rev=48506&r1=48505&r2=48506
==============================================================================
--- branches/1.4/main/rtp.c (original)
+++ branches/1.4/main/rtp.c Fri Dec 15 13:55:28 2006
@@ -160,13 +160,11 @@
 	struct io_context *io;
 	void *data;
 	ast_rtp_callback callback;
-
-	ast_mutex_t payload_lock;
+	ast_mutex_t bridge_lock;
 	struct rtpPayloadType current_RTP_PT[MAX_RTP_PT];
 	int rtp_lookup_code_cache_isAstFormat; /*!< a cache for the result of rtp_lookup_code(): */
 	int rtp_lookup_code_cache_code;
 	int rtp_lookup_code_cache_result;
-
 	struct ast_rtcp *rtcp;
 	struct ast_codec_pref pref;
 	struct ast_rtp *bridged;        /*!< Who we are Packet bridged to */
@@ -1014,9 +1012,8 @@
 }
 
 /*! \brief Perform a Packet2Packet RTP write */
-static int bridge_p2p_rtp_write(struct ast_rtp *rtp, unsigned int *rtpheader, int len, int hdrlen)
-{
-	struct ast_rtp *bridged = ast_rtp_get_bridged(rtp);
+static int bridge_p2p_rtp_write(struct ast_rtp *rtp, struct ast_rtp *bridged, unsigned int *rtpheader, int len, int hdrlen)
+{
 	int res = 0, payload = 0, bridged_payload = 0, version, padding, mark, ext;
 	struct rtpPayloadType rtpPT;
 	unsigned int seqno;
@@ -1083,6 +1080,7 @@
 	unsigned int timestamp;
 	unsigned int *rtpheader;
 	struct rtpPayloadType rtpPT;
+	struct ast_rtp *bridged = NULL;
 	
 	/* If time is up, kill it */
 	if (rtp->send_digit)
@@ -1144,7 +1142,7 @@
 	}
 
 	/* If we are bridged to another RTP stream, send direct */
-	if (ast_rtp_get_bridged(rtp) && !bridge_p2p_rtp_write(rtp, rtpheader, res, hdrlen))
+	if ((bridged = ast_rtp_get_bridged(rtp)) && !bridge_p2p_rtp_write(rtp, bridged, rtpheader, res, hdrlen))
 		return &ast_null_frame;
 
 	if (version != 2)
@@ -1362,7 +1360,7 @@
 	if (!rtp)
 		return;
 
-	ast_mutex_lock(&rtp->payload_lock);
+	ast_mutex_lock(&rtp->bridge_lock);
 
 	for (i = 0; i < MAX_RTP_PT; ++i) {
 		rtp->current_RTP_PT[i].isAstFormat = 0;
@@ -1373,14 +1371,14 @@
 	rtp->rtp_lookup_code_cache_code = 0;
 	rtp->rtp_lookup_code_cache_result = 0;
 
-	ast_mutex_unlock(&rtp->payload_lock);
+	ast_mutex_unlock(&rtp->bridge_lock);
 }
 
 void ast_rtp_pt_default(struct ast_rtp* rtp) 
 {
 	int i;
 
-	ast_mutex_lock(&rtp->payload_lock);
+	ast_mutex_lock(&rtp->bridge_lock);
 
 	/* Initialize to default payload types */
 	for (i = 0; i < MAX_RTP_PT; ++i) {
@@ -1392,15 +1390,15 @@
 	rtp->rtp_lookup_code_cache_code = 0;
 	rtp->rtp_lookup_code_cache_result = 0;
 
-	ast_mutex_unlock(&rtp->payload_lock);
+	ast_mutex_unlock(&rtp->bridge_lock);
 }
 
 void ast_rtp_pt_copy(struct ast_rtp *dest, struct ast_rtp *src)
 {
 	unsigned int i;
 
-	ast_mutex_lock(&dest->payload_lock);
-	ast_mutex_lock(&src->payload_lock);
+	ast_mutex_lock(&dest->bridge_lock);
+	ast_mutex_lock(&src->bridge_lock);
 
 	for (i=0; i < MAX_RTP_PT; ++i) {
 		dest->current_RTP_PT[i].isAstFormat = 
@@ -1412,8 +1410,8 @@
 	dest->rtp_lookup_code_cache_code = 0;
 	dest->rtp_lookup_code_cache_result = 0;
 
-	ast_mutex_unlock(&src->payload_lock);
-	ast_mutex_unlock(&dest->payload_lock);
+	ast_mutex_unlock(&src->bridge_lock);
+	ast_mutex_unlock(&dest->bridge_lock);
 }
 
 /*! \brief Get channel driver interface structure */
@@ -1582,9 +1580,9 @@
 	if (pt < 0 || pt > MAX_RTP_PT || static_RTP_PT[pt].code == 0) 
 		return; /* bogus payload type */
 
-	ast_mutex_lock(&rtp->payload_lock);
+	ast_mutex_lock(&rtp->bridge_lock);
 	rtp->current_RTP_PT[pt] = static_RTP_PT[pt];
-	ast_mutex_unlock(&rtp->payload_lock);
+	ast_mutex_unlock(&rtp->bridge_lock);
 } 
 
 /*! \brief Make a note of a RTP payload type (with MIME type) that was seen in
@@ -1599,7 +1597,7 @@
 	if (pt < 0 || pt > MAX_RTP_PT) 
 		return; /* bogus payload type */
 	
-	ast_mutex_lock(&rtp->payload_lock);
+	ast_mutex_lock(&rtp->bridge_lock);
 
 	for (i = 0; i < sizeof(mimeTypes)/sizeof(mimeTypes[0]); ++i) {
 		if (strcasecmp(mimeSubtype, mimeTypes[i].subtype) == 0 &&
@@ -1613,7 +1611,7 @@
 		}
 	}
 
-	ast_mutex_unlock(&rtp->payload_lock);
+	ast_mutex_unlock(&rtp->bridge_lock);
 
 	return;
 } 
@@ -1625,7 +1623,7 @@
 {
 	int pt;
 	
-	ast_mutex_lock(&rtp->payload_lock);
+	ast_mutex_lock(&rtp->bridge_lock);
 	
 	*astFormats = *nonAstFormats = 0;
 	for (pt = 0; pt < MAX_RTP_PT; ++pt) {
@@ -1636,7 +1634,7 @@
 		}
 	}
 	
-	ast_mutex_unlock(&rtp->payload_lock);
+	ast_mutex_unlock(&rtp->bridge_lock);
 	
 	return;
 }
@@ -1651,9 +1649,9 @@
 		return result; /* bogus payload type */
 
 	/* Start with negotiated codecs */
-	ast_mutex_lock(&rtp->payload_lock);
+	ast_mutex_lock(&rtp->bridge_lock);
 	result = rtp->current_RTP_PT[pt];
-	ast_mutex_unlock(&rtp->payload_lock);
+	ast_mutex_unlock(&rtp->bridge_lock);
 
 	/* If it doesn't exist, check our static RTP type list, just in case */
 	if (!result.code) 
@@ -1667,13 +1665,13 @@
 {
 	int pt = 0;
 
-	ast_mutex_lock(&rtp->payload_lock);
+	ast_mutex_lock(&rtp->bridge_lock);
 
 	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 */
 		pt = rtp->rtp_lookup_code_cache_result;
-		ast_mutex_unlock(&rtp->payload_lock);
+		ast_mutex_unlock(&rtp->bridge_lock);
 		return pt;
 	}
 
@@ -1683,7 +1681,7 @@
 			rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
 			rtp->rtp_lookup_code_cache_code = code;
 			rtp->rtp_lookup_code_cache_result = pt;
-			ast_mutex_unlock(&rtp->payload_lock);
+			ast_mutex_unlock(&rtp->bridge_lock);
 			return pt;
 		}
 	}
@@ -1694,12 +1692,12 @@
 			rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
   			rtp->rtp_lookup_code_cache_code = code;
 			rtp->rtp_lookup_code_cache_result = pt;
-			ast_mutex_unlock(&rtp->payload_lock);
+			ast_mutex_unlock(&rtp->bridge_lock);
 			return pt;
 		}
 	}
 
-	ast_mutex_unlock(&rtp->payload_lock);
+	ast_mutex_unlock(&rtp->bridge_lock);
 
 	return -1;
 }
@@ -1810,7 +1808,7 @@
 	if (!(rtp = ast_calloc(1, sizeof(*rtp))))
 		return NULL;
 
-	ast_mutex_init(&rtp->payload_lock);
+	ast_mutex_init(&rtp->bridge_lock);
 
 	rtp->them.sin_family = AF_INET;
 	rtp->us.sin_family = AF_INET;
@@ -1940,7 +1938,13 @@
 
 struct ast_rtp *ast_rtp_get_bridged(struct ast_rtp *rtp)
 {
-	return rtp->bridged;
+	struct ast_rtp *bridged = NULL;
+
+	ast_mutex_lock(&rtp->bridge_lock);
+	bridged = rtp->bridged;
+	ast_mutex_unlock(&rtp->bridge_lock);
+
+	return bridged;
 }
 
 void ast_rtp_stop(struct ast_rtp *rtp)
@@ -2033,7 +2037,7 @@
 		rtp->rtcp=NULL;
 	}
 
-	ast_mutex_destroy(&rtp->payload_lock);
+	ast_mutex_destroy(&rtp->bridge_lock);
 
 	free(rtp);
 }
@@ -2869,7 +2873,7 @@
 	struct sockaddr_in sin;
 	socklen_t len;
 	unsigned int *header;
-	struct ast_rtp *rtp = cbdata;
+	struct ast_rtp *rtp = cbdata, *bridged = NULL;
 
 	if (!rtp)
 		return 1;
@@ -2892,8 +2896,8 @@
 	}
 
 	/* Write directly out to other RTP stream if bridged */
-	if (ast_rtp_get_bridged(rtp))
-		bridge_p2p_rtp_write(rtp, header, res, hdrlen);
+	if ((bridged = ast_rtp_get_bridged(rtp)))
+		bridge_p2p_rtp_write(rtp, bridged, header, res, hdrlen);
 
 	return 1;
 }
@@ -2938,6 +2942,16 @@
 		rtp->ioid = ast_io_add(rtp->io, rtp->s, rtpread, AST_IO_IN, rtp);
 
 	return 0;
+}
+
+/*! \brief Helper function that sets what an RTP structure is bridged to */
+static void p2p_set_bridge(struct ast_rtp *rtp0, struct ast_rtp *rtp1)
+{
+	ast_mutex_lock(&rtp0->bridge_lock);
+	rtp0->bridged = rtp1;
+	ast_mutex_unlock(&rtp0->bridge_lock);
+
+	return;
 }
 
 /*! \brief Bridge loop for partial native bridge (packet2packet) */
@@ -2952,9 +2966,9 @@
 
 	/* Okay, setup each RTP structure to do P2P forwarding */
 	ast_clear_flag(p0, FLAG_P2P_SENT_MARK);
-	p0->bridged = p1;
+	p2p_set_bridge(p0, p1);
 	ast_clear_flag(p1, FLAG_P2P_SENT_MARK);
-	p1->bridged = p0;
+	p2p_set_bridge(p1, p0);
 
 	/* Activate callback modes if possible */
 	p0_callback = p2p_callback_enable(c0, p0, &p0_fds[0], &p0_iod[0]);
@@ -3013,14 +3027,14 @@
 						p0_callback = p2p_callback_disable(c0, p0, &p0_fds[0], &p0_iod[0]);
 					if (p1_callback)
 						p1_callback = p2p_callback_disable(c1, p1, &p1_fds[0], &p1_iod[0]);
-					p0->bridged = NULL;
-					p1->bridged = NULL;
+					p2p_set_bridge(p0, NULL);
+					p2p_set_bridge(p1, NULL);
 				} else if (fr->subclass == AST_CONTROL_UNHOLD) {
 					/* If we are off hold, then go back to callback mode and P2P bridging */
 					ast_clear_flag(p0, FLAG_P2P_SENT_MARK);
-					p0->bridged = p1;
+					p2p_set_bridge(p0, p1);
 					ast_clear_flag(p1, FLAG_P2P_SENT_MARK);
-					p1->bridged = p0;
+					p2p_set_bridge(p1, p0);
 					p0_callback = p2p_callback_enable(c0, p0, &p0_fds[0], &p0_iod[0]);
 					p1_callback = p2p_callback_enable(c1, p1, &p1_fds[0], &p1_iod[0]);
 				}
@@ -3055,8 +3069,8 @@
 		p1_callback = p2p_callback_disable(c1, p1, &p1_fds[0], &p1_iod[0]);
 
 	/* Break out of the direct bridge */
-	p0->bridged = NULL;
-	p1->bridged = NULL;
+	p2p_set_bridge(p0, NULL);
+	p2p_set_bridge(p1, NULL);
 
 	return res;
 }



More information about the asterisk-commits mailing list