[asterisk-commits] mogorman: branch mogorman/asterisk-5162 r41767 - in /team/mogorman/asterisk-5...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Fri Sep 1 15:28:27 MST 2006


Author: mogorman
Date: Fri Sep  1 17:28:26 2006
New Revision: 41767

URL: http://svn.digium.com/view/asterisk?rev=41767&view=rev
Log:
rtp packetization from bug 5162 patch trunk-41648

Modified:
    team/mogorman/asterisk-5162/channels/chan_sip.c
    team/mogorman/asterisk-5162/channels/chan_skinny.c
    team/mogorman/asterisk-5162/include/asterisk/frame.h
    team/mogorman/asterisk-5162/include/asterisk/rtp.h
    team/mogorman/asterisk-5162/main/frame.c
    team/mogorman/asterisk-5162/main/rtp.c

Modified: team/mogorman/asterisk-5162/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/team/mogorman/asterisk-5162/channels/chan_sip.c?rev=41767&r1=41766&r2=41767&view=diff
==============================================================================
--- team/mogorman/asterisk-5162/channels/chan_sip.c (original)
+++ team/mogorman/asterisk-5162/channels/chan_sip.c Fri Sep  1 17:28:26 2006
@@ -541,6 +541,8 @@
 static int regobjs = 0;			/*!< Registry objects */
 
 static struct ast_flags global_flags[2] = {{0}};	/*!< global SIP_ flags */
+
+static int global_autoframing = 0;
 
 /*! \brief Protect the SIP dialog list (of sip_pvt's) */
 AST_MUTEX_DEFINE_STATIC(iflock);
@@ -961,6 +963,7 @@
 	struct ast_variable *chanvars;		/*!< Channel variables to set for inbound call */
 	struct sip_pvt *next;			/*!< Next dialog in chain */
 	struct sip_invite_param *options;	/*!< Options for INVITE */
+	int autoframing;
 } *iflist = NULL;
 
 #define FLAG_RESPONSE (1 << 0)
@@ -1010,6 +1013,7 @@
 	struct ast_ha *ha;		/*!< ACL setting */
 	struct ast_variable *chanvars;	/*!< Variables to set for channel created by user */
 	int maxcallbitrate;		/*!< Maximum Bitrate for a video call */
+	int autoframing;
 };
 
 /*! \brief Structure for SIP peer data, we place calls to peers if registered  or fixed IP address (host) */
@@ -1072,6 +1076,7 @@
 	struct ast_variable *chanvars;	/*!<  Variables to set for channel created by user */
 	struct sip_pvt *mwipvt;		/*!<  Subscription for MWI */
 	int lastmsg;
+	int autoframing;
 };
 
 
@@ -2531,6 +2536,11 @@
 		if (option_debug)
 			ast_log(LOG_DEBUG, "Setting NAT on UDPTL to %s\n", natflags ? "On" : "Off");
 		ast_udptl_setnat(r->udptl, natflags);
+	}
+	/* Set Frame packetization */
+	if (r->rtp) {
+		ast_rtp_codec_setpref(r->rtp, &r->prefs);
+		r->autoframing = peer->autoframing;
 	}
 	ast_string_field_set(r, peername, peer->username);
 	ast_string_field_set(r, authname, peer->username);
@@ -4688,6 +4698,8 @@
 	 */
 	/* XXX This needs to be done per media stream, since it's media stream specific */
 	iterator = req->sdp_start;
+	int found_rtpmap_codecs[32];
+	int last_rtpmap_codec=0;
 	while ((a = get_sdp_iterate(&iterator, req, "a"))[0] != '\0') {
 		char* mimeSubtype = ast_strdupa(a); /* ensures we have enough space */
 		if (option_debug > 1) {
@@ -4738,17 +4750,49 @@
 		}  else if (!strcasecmp(a, "sendrecv")) {
 			sendonly = 0;
 			continue;
-		} else if (sscanf(a, "rtpmap: %u %[^/]/", &codec, mimeSubtype) != 2) 
+		} else if (strlen(a) > 5 && !strncasecmp(a, "ptime", 5)) {
+			char *tmp = strrchr(a, ':');
+			int framing = 0;
+			if (tmp) {
+			    tmp++;
+			    framing = strtol(tmp, NULL, 10);
+			    if (framing == LONG_MIN || framing == LONG_MAX) {
+				    framing = 0;
+				    ast_log(LOG_DEBUG, "Can't read framing from SDP: %s\n", a);
+			    }
+			}
+			if (framing && last_rtpmap_codec) {
+				if (p->autoframing || global_autoframing) {
+					struct ast_codec_pref *pref = ast_rtp_codec_getpref(p->rtp);
+					int codec_n;
+					int format = 0;
+					for (codec_n = 0; codec_n < last_rtpmap_codec; codec_n++) {
+						format = ast_rtp_codec_getformat(found_rtpmap_codecs[codec_n]);
+						if (!format)	/* non-codec or not found */
+							continue;
+						if (option_debug)
+							ast_log(LOG_DEBUG, "Setting framing for %d to %d\n", format, framing);
+						ast_codec_pref_setsize(pref, format, framing);
+					}
+					ast_rtp_codec_setpref(p->rtp, pref);
+				}
+			}
+			memset(&found_rtpmap_codecs, 0, sizeof(found_rtpmap_codecs));
+			last_rtpmap_codec = 0;
 			continue;
-		/* We have a rtpmap to handle */
-		if (debug)
-			ast_verbose("Found description format %s for ID %d\n", mimeSubtype, codec);
-
-		/* Note: should really look at the 'freq' and '#chans' params too */
-		ast_rtp_set_rtpmap_type(newaudiortp, codec, "audio", mimeSubtype,
+		} else if (sscanf(a, "rtpmap: %u %[^/]/", &codec, mimeSubtype) == 2) {
+			/* We have a rtpmap to handle */
+			if (debug)
+				ast_verbose("Found description format %s for ID %d\n", mimeSubtype, codec);
+			found_rtpmap_codecs[last_rtpmap_codec] = codec;
+			last_rtpmap_codec++;
+
+			/* Note: should really look at the 'freq' and '#chans' params too */
+			ast_rtp_set_rtpmap_type(newaudiortp, codec, "audio", mimeSubtype,
 					ast_test_flag(&p->flags[0], SIP_G726_NONSTANDARD) ? AST_RTP_OPT_G726_NONSTANDARD : 0);
-		if (p->vrtp)
-			ast_rtp_set_rtpmap_type(newvideortp, codec, "video", mimeSubtype, 0);
+			if (p->vrtp)
+				ast_rtp_set_rtpmap_type(newvideortp, codec, "video", mimeSubtype, 0);
+		}
 	}
 	
 	if (udptlportno != -1) {
@@ -5586,12 +5630,18 @@
 			     int debug)
 {
 	int rtp_code;
+	struct ast_format_list fmt;
+
 
 	if (debug)
 		ast_verbose("Adding codec 0x%x (%s) to SDP\n", codec, ast_getformatname(codec));
 	if ((rtp_code = ast_rtp_lookup_code(p->rtp, 1, codec)) == -1)
 		return;
 
+	if (p->rtp) {
+		struct ast_codec_pref *pref = ast_rtp_codec_getpref(p->rtp);
+		fmt = ast_codec_pref_getsize(pref, codec);
+	}
 	ast_build_string(m_buf, m_size, " %d", rtp_code);
 	ast_build_string(a_buf, a_size, "a=rtpmap:%d %s/%d\r\n", rtp_code,
 			 ast_rtp_lookup_mime_subtype(1, codec,
@@ -5601,9 +5651,12 @@
 		/* Indicate that we don't support VAD (G.729 annex B) */
 		ast_build_string(a_buf, a_size, "a=fmtp:%d annexb=no\r\n", rtp_code);
 	} else if (codec == AST_FORMAT_ILBC) {
-		/* Add information about us using only 20 ms packetization */
-		ast_build_string(a_buf, a_size, "a=fmtp:%d mode=20\r\n", rtp_code);
-	
+		/* Add information about us using only 20/30 ms packetization */
+		ast_build_string(a_buf, a_size, "a=fmtp:%d mode=%d\r\n", rtp_code, fmt.cur_ms);
+	}
+
+	if (codec != AST_FORMAT_ILBC) {
+		ast_build_string(a_buf, a_size, "a=ptime:%d\r\n", fmt.cur_ms);
 	}
 }
 
@@ -6079,6 +6132,11 @@
 	}
 	respprep(&resp, p, msg, req);
 	if (p->rtp) {
+		if (!p->autoframing && !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
+			if (option_debug)
+				ast_log(LOG_DEBUG, "Setting framing from config on incoming call\n");
+			ast_rtp_codec_setpref(p->rtp, &p->prefs);
+		}
 		try_suggested_sip_codec(p);	
 		add_sdp(&resp, p);
 	} else {
@@ -7930,6 +7988,11 @@
 			ASTOBJ_UNREF(peer, sip_destroy_peer);
 	}
 	if (peer) {
+		/* Set Frame packetization */
+		if (p->rtp) {
+			ast_rtp_codec_setpref(p->rtp, &peer->prefs);
+			p->autoframing = peer->autoframing;
+		}
 		if (!ast_test_flag(&peer->flags[1], SIP_PAGE2_DYNAMIC)) {
 			ast_log(LOG_ERROR, "Peer '%s' is trying to register, but not configured as host=dynamic\n", peer->name);
 		} else {
@@ -8663,6 +8726,11 @@
 			}
 		}
 		p->prefs = user->prefs;
+		/* Set Frame packetization */
+		if (p->rtp) {
+			ast_rtp_codec_setpref(p->rtp, &p->prefs);
+			p->autoframing = user->autoframing;
+		}
 		/* replace callerid if rpid found, and not restricted */
 		if (!ast_strlen_zero(rpid_num) && ast_test_flag(&p->flags[0], SIP_TRUSTRPID)) {
 			char *tmp;
@@ -8771,6 +8839,11 @@
 			peer = find_peer(NULL, &p->recv, 1);
 
 		if (peer) {
+			/* Set Frame packetization */
+			if (p->rtp) {
+				ast_rtp_codec_setpref(p->rtp, &peer->prefs);
+				p->autoframing = peer->autoframing;
+			}
 			if (debug)
 				ast_verbose("Found peer '%s'\n", peer->name);
 
@@ -9528,6 +9601,7 @@
 		if (!codec)
 			break;
 		ast_cli(fd, "%s", ast_getformatname(codec));
+		ast_cli(fd, ":%d", pref->framing[x]);
 		if (x < 31 && ast_codec_pref_index(pref, x + 1))
 			ast_cli(fd, ",");
 	}
@@ -15136,6 +15210,8 @@
 			ast_parse_allow_disallow(&user->prefs, &user->capability, v->value, 1);
 		} else if (!strcasecmp(v->name, "disallow")) {
 			ast_parse_allow_disallow(&user->prefs, &user->capability, v->value, 0);
+		} else if (!strcasecmp(v->name, "autoframing")) {
+			user->autoframing = ast_true(v->value);
 		} else if (!strcasecmp(v->name, "callingpres")) {
 			user->callingpres = ast_parse_caller_presentation(v->value);
 			if (user->callingpres == -1)
@@ -15407,6 +15483,8 @@
 			ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, 1);
 		} else if (!strcasecmp(v->name, "disallow")) {
 			ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, 0);
+		} else if (!strcasecmp(v->name, "autoframing")) {
+			peer->autoframing = ast_true(v->value);
 		} else if (!strcasecmp(v->name, "rtptimeout")) {
 			if ((sscanf(v->value, "%d", &peer->rtptimeout) != 1) || (peer->rtptimeout < 0)) {
 				ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d.  Using default.\n", v->value, v->lineno);
@@ -15768,6 +15846,8 @@
 			ast_parse_allow_disallow(&default_prefs, &global_capability, v->value, 1);
 		} else if (!strcasecmp(v->name, "disallow")) {
 			ast_parse_allow_disallow(&default_prefs, &global_capability, v->value, 0);
+		} else if (!strcasecmp(v->name, "autoframing")) {
+			global_autoframing = ast_true(v->value);
 		} else if (!strcasecmp(v->name, "allowexternaldomains")) {
 			allow_external_domains = ast_true(v->value);
 		} else if (!strcasecmp(v->name, "autodomain")) {

Modified: team/mogorman/asterisk-5162/channels/chan_skinny.c
URL: http://svn.digium.com/view/asterisk/team/mogorman/asterisk-5162/channels/chan_skinny.c?rev=41767&r1=41766&r2=41767&view=diff
==============================================================================
--- team/mogorman/asterisk-5162/channels/chan_skinny.c (original)
+++ team/mogorman/asterisk-5162/channels/chan_skinny.c Fri Sep  1 17:28:26 2006
@@ -81,7 +81,8 @@
 static const char config[] = "skinny.conf";
 
 /* Just about everybody seems to support ulaw, so make it a nice default */
-static int capability = AST_FORMAT_ULAW;
+static int default_capability = AST_FORMAT_ULAW;
+static struct ast_codec_pref default_prefs;
 
 #define DEFAULT_SKINNY_PORT	2000
 #define DEFAULT_SKINNY_BACKLOG	2
@@ -945,6 +946,7 @@
 	int hookstate;
 	int nat;
 
+	struct ast_codec_pref prefs;
 	struct skinny_subchannel *sub;
 	struct skinny_line *next;
 	struct skinny_device *parent;
@@ -977,11 +979,13 @@
 	int registered;
 	int lastlineinstance;
 	int lastcallreference;
+	int capability;
 	struct sockaddr_in addr;
 	struct in_addr ourip;
 	struct skinny_line *lines;
 	struct skinny_speeddial *speeddials;
 	struct skinny_addon *addons;
+	struct ast_codec_pref prefs;
 	struct ast_ha *ha;
 	struct skinnysession *session;
 	struct skinny_device *next;
@@ -1019,7 +1023,7 @@
 static const struct ast_channel_tech skinny_tech = {
 	.type = "Skinny",
 	.description = tdesc,
-	.capabilities = AST_FORMAT_ULAW,
+	.capabilities = ((AST_FORMAT_MAX_AUDIO << 1) - 1),
 	.properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
 	.requester = skinny_request,
 	.call = skinny_call,
@@ -1404,7 +1408,7 @@
 
 	req->data.openreceivechannel.conferenceId = 0;
 	req->data.openreceivechannel.partyId = htolel(sub->callid);
-	req->data.openreceivechannel.packets = htolel(20);
+	req->data.openreceivechannel.packets = htolel(l->prefs.framing[0]);
 	req->data.openreceivechannel.capability = htolel(convert_cap(l->capability));
 	req->data.openreceivechannel.echo = 0;
 	req->data.openreceivechannel.bitrate = 0;
@@ -1892,6 +1896,8 @@
 	} else {
 		ast_copy_string(d->name, cat, sizeof(d->name));
 		d->lastlineinstance = 1;
+		d->capability = default_capability;
+		d->prefs = default_prefs;
 		while(v) {
 			if (!strcasecmp(v->name, "host")) {
 				if (ast_get_ip(&d->addr, v->value)) {
@@ -1906,6 +1912,10 @@
 				d->ha = ast_append_ha(v->name, v->value, d->ha);
 			} else if (!strcasecmp(v->name, "context")) {
 				ast_copy_string(context, v->value, sizeof(context));
+			} else if (!strcasecmp(v->name, "allow")) {
+				ast_parse_allow_disallow(&d->prefs, &d->capability, v->value, 1);
+			} else if (!strcasecmp(v->name, "disallow")) {
+				ast_parse_allow_disallow(&d->prefs, &d->capability, v->value, 0);
 			} else if (!strcasecmp(v->name, "version")) {
 				ast_copy_string(d->version_id, v->value, sizeof(d->version_id));
 			} else if (!strcasecmp(v->name, "nat")) {
@@ -2002,7 +2012,8 @@
 							ast_verbose(VERBOSE_PREFIX_3 "Setting mailbox '%s' on %s@%s\n", mailbox, d->name, l->name);
 					}
 					l->msgstate = -1;
-					l->capability = capability;
+					l->capability = d->capability;
+					l->prefs = d->prefs;
 					l->parent = d;
 					if (!strcasecmp(v->name, "trunk")) {
 						l->type = TYPE_TRUNK;
@@ -2129,6 +2140,10 @@
 	if (sub->vrtp) {
 		ast_rtp_setnat(sub->vrtp, l->nat);
 	}
+	/* Set Frame packetization */
+	if (sub->rtp)
+		ast_rtp_codec_setpref(sub->rtp, &l->prefs);
+
 	/* Create the RTP connection */
 	transmit_connect(d->session, sub);
  	ast_mutex_unlock(&sub->lock);
@@ -2640,7 +2655,7 @@
 		tmp->tech_pvt = sub;
 		tmp->nativeformats = l->capability;
 		if (!tmp->nativeformats)
-			tmp->nativeformats = capability;
+			tmp->nativeformats = default_capability;
 		fmt = ast_best_codec(tmp->nativeformats);
 		if (skinnydebug)
 			ast_verbose("skinny_new: tmp->nativeformats=%d fmt=%d\n", tmp->nativeformats, fmt);
@@ -3435,7 +3450,7 @@
 	req->data.startmedia.passThruPartyId = htolel(sub->callid);
 	req->data.startmedia.remoteIp = htolel(d->ourip.s_addr);
 	req->data.startmedia.remotePort = htolel(ntohs(us.sin_port));
-	req->data.startmedia.packetSize = htolel(20);
+	req->data.startmedia.packetSize = htolel(l->prefs.framing[0]);
 	req->data.startmedia.payloadType = htolel(convert_cap(l->capability));
 	req->data.startmedia.qualifier.precedence = htolel(127);
 	req->data.startmedia.qualifier.vad = 0;
@@ -4137,7 +4152,7 @@
 	char *dest = data;
 
 	oldformat = format;
-	format &= capability;
+	format &= default_capability;
 	if (!format) {
 		ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", format);
 		return NULL;
@@ -4168,7 +4183,6 @@
 	int on = 1;
 	struct ast_config *cfg;
 	struct ast_variable *v;
-	int format;
 	char *cat;
 	struct skinny_device *d;
 	int oldport = ntohs(bindaddr.sin_port);
@@ -4210,19 +4224,9 @@
 		} else if (!strcasecmp(v->name, "dateformat")) {
 			ast_copy_string(date_format, v->value, sizeof(date_format));
 		} else if (!strcasecmp(v->name, "allow")) {
-			format = ast_getformatbyname(v->value);
-			if (format < 1) {
-				ast_log(LOG_WARNING, "Cannot allow unknown format '%s'\n", v->value);
-			} else {
-				capability |= format;
-			}
+                        ast_parse_allow_disallow(&default_prefs, &default_capability, v->value, 1);
 		} else if (!strcasecmp(v->name, "disallow")) {
-			format = ast_getformatbyname(v->value);
-			if (format < 1) {
-				ast_log(LOG_WARNING, "Cannot disallow unknown format '%s'\n", v->value);
-			} else {
-				capability &= ~format;
-			}
+                        ast_parse_allow_disallow(&default_prefs, &default_capability, v->value, 0);
 		} else if (!strcasecmp(v->name, "bindport") || !strcasecmp(v->name, "port")) {
 			if (sscanf(v->value, "%d", &ourport) == 1) {
 				bindaddr.sin_port = htons(ourport);

Modified: team/mogorman/asterisk-5162/include/asterisk/frame.h
URL: http://svn.digium.com/view/asterisk/team/mogorman/asterisk-5162/include/asterisk/frame.h?rev=41767&r1=41766&r2=41767&view=diff
==============================================================================
--- team/mogorman/asterisk-5162/include/asterisk/frame.h (original)
+++ team/mogorman/asterisk-5162/include/asterisk/frame.h Fri Sep  1 17:28:26 2006
@@ -36,6 +36,7 @@
 
 struct ast_codec_pref {
 	char order[32];
+	char framing[32];
 };
 
 /*! \page Def_Frame AST Multimedia and signalling frames
@@ -279,6 +280,7 @@
 };
 
 #define AST_SMOOTHER_FLAG_G729		(1 << 0)
+#define AST_SMOOTHER_FLAG_BE		(1 << 1)
 
 /* Option identifiers and flags */
 #define AST_OPTION_FLAG_REQUEST		0
@@ -344,6 +346,23 @@
 		u_int8_t data[0];
 };
 
+
+/*! \brief Definition of supported media formats (codecs) */
+struct ast_format_list {
+	int visible;    /*!< Can we see this entry */
+	int bits;       /*!< bitmask value */
+	char *name;     /*!< short name */
+	char *desc;     /*!< Description */
+	int fr_len;	/*!< Single frame length in bytes */
+	int min_ms;	/*!< Min value */
+	int max_ms;	/*!< Max value */
+	int inc_ms;	/*!< Increment */
+	int def_ms;	/*!< Default value */
+	unsigned int flags;	/*!< Smoother flags */
+	int cur_ms;	/*!< Current value */
+};
+
+
 /*! \brief  Requests a frame to be allocated 
  * 
  * \param source 
@@ -450,6 +469,7 @@
 struct ast_smoother *ast_smoother_new(int bytes);
 void ast_smoother_set_flags(struct ast_smoother *smoother, int flags);
 int ast_smoother_get_flags(struct ast_smoother *smoother);
+int ast_smoother_test_flag(struct ast_smoother *s, int flag);
 void ast_smoother_free(struct ast_smoother *s);
 void ast_smoother_reset(struct ast_smoother *s, int bytes);
 int __ast_smoother_feed(struct ast_smoother *s, struct ast_frame *f, int swap);
@@ -496,6 +516,14 @@
    the format list is selected, otherwise 0 is returned. */
 int ast_codec_choose(struct ast_codec_pref *pref, int formats, int find_best);
 
+/*! \brief Set packet size for codec
+*/
+int ast_codec_pref_setsize(struct ast_codec_pref *pref, int format, int framems);
+
+/*! \brief Get packet size for codec
+*/
+struct ast_format_list ast_codec_pref_getsize(struct ast_codec_pref *pref, int format);
+
 /*! \brief Parse an "allow" or "deny" line in a channel or device configuration 
         and update the capabilities mask and pref if provided.
 	Video codecs are not added to codec preference lists, since we can not transcode

Modified: team/mogorman/asterisk-5162/include/asterisk/rtp.h
URL: http://svn.digium.com/view/asterisk/team/mogorman/asterisk-5162/include/asterisk/rtp.h?rev=41767&r1=41766&r2=41767&view=diff
==============================================================================
--- team/mogorman/asterisk-5162/include/asterisk/rtp.h (original)
+++ team/mogorman/asterisk-5162/include/asterisk/rtp.h Fri Sep  1 17:28:26 2006
@@ -210,6 +210,12 @@
 
 int ast_rtp_reload(void);
 
+int ast_rtp_codec_setpref(struct ast_rtp *rtp, struct ast_codec_pref *prefs);
+
+struct ast_codec_pref *ast_rtp_codec_getpref(struct ast_rtp *rtp);
+
+int ast_rtp_codec_getformat(int pt);
+
 #if defined(__cplusplus) || defined(c_plusplus)
 }
 #endif

Modified: team/mogorman/asterisk-5162/main/frame.c
URL: http://svn.digium.com/view/asterisk/team/mogorman/asterisk-5162/main/frame.c?rev=41767&r1=41766&r2=41767&view=diff
==============================================================================
--- team/mogorman/asterisk-5162/main/frame.c (original)
+++ team/mogorman/asterisk-5162/main/frame.c Fri Sep  1 17:28:26 2006
@@ -101,24 +101,19 @@
 };
 
 /*! \brief Definition of supported media formats (codecs) */
-static struct ast_format_list {
-	int visible;	/*!< Can we see this entry */
-	int bits;	/*!< bitmask value */
-	char *name;	/*!< short name */
-	char *desc;	/*!< Description */
-} AST_FORMAT_LIST[] = {					/*!< Bit number: comment  - Bit numbers are hard coded in show_codec() */
-	{ 1, AST_FORMAT_G723_1 , "g723" , "G.723.1"},	/*!<  1 */
-	{ 1, AST_FORMAT_GSM, "gsm" , "GSM"},		/*!<  2: codec_gsm.c */
-	{ 1, AST_FORMAT_ULAW, "ulaw", "G.711 u-law" },	/*!<  3: codec_ulaw.c */
-	{ 1, AST_FORMAT_ALAW, "alaw", "G.711 A-law" },	/*!<  4: codec_alaw.c */
-	{ 1, AST_FORMAT_G726, "g726", "G.726 RFC3551" },/*!<  5: codec_g726.c */
-	{ 1, AST_FORMAT_ADPCM, "adpcm" , "ADPCM"},	/*!<  6: codec_adpcm.c */
-	{ 1, AST_FORMAT_SLINEAR, "slin",  "16 bit Signed Linear PCM"},	/*!< 7 */
-	{ 1, AST_FORMAT_LPC10, "lpc10", "LPC10" },	/*!<  8: codec_lpc10.c */
-	{ 1, AST_FORMAT_G729A, "g729", "G.729A" },	/*!<  9: Binary commercial distribution */
-	{ 1, AST_FORMAT_SPEEX, "speex", "SpeeX" },	/*!< 10: codec_speex.c */
-	{ 1, AST_FORMAT_ILBC, "ilbc", "iLBC"},		/*!< 11: codec_ilbc.c */
-	{ 1, AST_FORMAT_G726_AAL2, "g726aal2", "G.726 AAL2" },	/*!<  12: codec_g726.c */
+static struct ast_format_list AST_FORMAT_LIST[] = {					/*!< Bit number: comment  - Bit numbers are hard coded in show_codec() */
+	{ 1, AST_FORMAT_G723_1 , "g723" , "G.723.1", 24, 30, 300, 30, 30 },	/*!<  1 */
+	{ 1, AST_FORMAT_GSM, "gsm" , "GSM", 33, 20, 60, 20, 20 },		/*!<  2: codec_gsm.c */
+	{ 1, AST_FORMAT_ULAW, "ulaw", "G.711 u-law", 80, 10, 30, 10, 20 },	/*!<  3: codec_ulaw.c */
+	{ 1, AST_FORMAT_ALAW, "alaw", "G.711 A-law", 80, 10, 30, 10, 20 },	/*!<  4: codec_alaw.c */
+	{ 1, AST_FORMAT_G726, "g726", "G.726 RFC3551", 40, 10, 50, 10, 20 },/*!<  5: codec_g726.c */
+	{ 1, AST_FORMAT_ADPCM, "adpcm" , "ADPCM", 40, 10, 30, 10, 20 },	/*!<  6: codec_adpcm.c */
+	{ 1, AST_FORMAT_SLINEAR, "slin",  "16 bit Signed Linear PCM", 160, 10, 70, 10, 20, AST_SMOOTHER_FLAG_BE },	/*!< 7 */
+	{ 1, AST_FORMAT_LPC10, "lpc10", "LPC10", 7, 20, 20, 20, 20 },	/*!<  8: codec_lpc10.c */ 
+	{ 1, AST_FORMAT_G729A, "g729", "G.729A", 10, 10, 230, 10, 20, AST_SMOOTHER_FLAG_G729 },	/*!<  9: Binary commercial distribution */
+	{ 1, AST_FORMAT_SPEEX, "speex", "SpeeX", 10, 10, 60, 10, 20 },	/*!< 10: codec_speex.c */
+	{ 1, AST_FORMAT_ILBC, "ilbc", "iLBC", 50, 30, 30, 30, 30 },		/*!< 11: codec_ilbc.c */ /* inc=30ms - workaround */
+	{ 1, AST_FORMAT_G726_AAL2, "g726aal2", "G.726 AAL2", 40, 10, 50, 10, 20 },	/*!<  12: codec_g726.c */
 	{ 0, 0, "nothing", "undefined" },
 	{ 0, 0, "nothing", "undefined" },
 	{ 0, 0, "nothing", "undefined" },
@@ -162,6 +157,11 @@
 void ast_smoother_set_flags(struct ast_smoother *s, int flags)
 {
 	s->flags = flags;
+}
+
+int ast_smoother_test_flag(struct ast_smoother *s, int flag)
+{
+	return (s->flags & flag);
 }
 
 int __ast_smoother_feed(struct ast_smoother *s, struct ast_frame *f, int swap)
@@ -1053,6 +1053,7 @@
 	struct ast_codec_pref oldorder;
 	int x, y = 0;
 	int slot;
+	int size;
 
 	if(!pref->order[0])
 		return;
@@ -1062,10 +1063,13 @@
 
 	for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
 		slot = oldorder.order[x];
+		size = oldorder.framing[x];
 		if(! slot)
 			break;
-		if(AST_FORMAT_LIST[slot-1].bits != format)
-			pref->order[y++] = slot;
+		if(AST_FORMAT_LIST[slot-1].bits != format) {
+			pref->order[y] = slot;
+			pref->framing[y++] = size;
+		}
 	}
 	
 }
@@ -1097,6 +1101,84 @@
 }
 
 
+/*! \brief Set packet size for codec */
+int ast_codec_pref_setsize(struct ast_codec_pref *pref, int format, int framems)
+{
+        int x, index = -1;
+
+        for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
+                if(AST_FORMAT_LIST[x].bits == format) {
+                        index = x;
+                        break;
+                }
+        }
+
+	if(index < 0)
+		return -1;
+
+	/* size validation */
+	if(!framems)
+		framems = AST_FORMAT_LIST[index].def_ms;
+
+	if(AST_FORMAT_LIST[index].inc_ms && framems % AST_FORMAT_LIST[index].inc_ms) /* avoid division by zero */
+		framems -= framems % AST_FORMAT_LIST[index].inc_ms;
+
+	if(framems < AST_FORMAT_LIST[index].min_ms)
+		framems = AST_FORMAT_LIST[index].min_ms;
+
+	if(framems > AST_FORMAT_LIST[index].max_ms)
+		framems = AST_FORMAT_LIST[index].max_ms;
+
+
+        for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
+                if(pref->order[x] == (index + 1)) {
+                        pref->framing[x] = framems;
+                        break;
+                }
+        }
+
+        return x;
+}
+
+/*! \brief Get packet size for codec */
+struct ast_format_list ast_codec_pref_getsize(struct ast_codec_pref *pref, int format)
+{
+        int x, index = -1, framems = 0;
+	struct ast_format_list fmt;
+
+        for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
+                if(AST_FORMAT_LIST[x].bits == format) {
+                        fmt = AST_FORMAT_LIST[x];
+			index = x;
+                        break;
+                }
+        }
+
+        for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
+                if(pref->order[x] == (index + 1)) {
+                        framems = pref->framing[x];
+                        break;
+                }
+        }
+
+	/* size validation */
+	if(!framems)
+		framems = AST_FORMAT_LIST[index].def_ms;
+
+	if(AST_FORMAT_LIST[index].inc_ms && framems % AST_FORMAT_LIST[index].inc_ms) /* avoid division by zero */
+		framems -= framems % AST_FORMAT_LIST[index].inc_ms;
+
+	if(framems < AST_FORMAT_LIST[index].min_ms)
+		framems = AST_FORMAT_LIST[index].min_ms;
+
+	if(framems > AST_FORMAT_LIST[index].max_ms)
+		framems = AST_FORMAT_LIST[index].max_ms;
+
+	fmt.cur_ms = framems;
+
+	return fmt;
+}
+
 /*! \brief Pick a codec */
 int ast_codec_choose(struct ast_codec_pref *pref, int formats, int find_best)
 {
@@ -1126,9 +1208,22 @@
 	char *parse;
 	char *this;
 	int format;
+	char *psize;
+	int framems;
 
 	parse = ast_strdupa(list);
 	while ((this = strsep(&parse, ","))) {
+		framems = 0;
+                if ((psize = strrchr(this, ':'))) {
+                        *psize = '\0';
+                        psize++;
+			if (option_debug)
+				ast_log(LOG_DEBUG,"Packetization for codec: %s is %s\n", this, psize);
+                        framems = strtol(psize,NULL,10);
+                        if (framems == LONG_MIN || framems == LONG_MAX) {
+                                framems = 0;
+                        }
+                }
 		if (!(format = ast_getformatbyname(this))) {
 			ast_log(LOG_WARNING, "Cannot %s unknown format '%s'\n", allowing ? "allow" : "disallow", this);
 			continue;
@@ -1146,8 +1241,10 @@
 		 */
 		if (pref && (format & AST_FORMAT_AUDIO_MASK)) {
 			if (strcasecmp(this, "all")) {
-				if (allowing)
+				if (allowing) {
 					ast_codec_pref_append(pref, format);
+					ast_codec_pref_setsize(pref, format, framems);
+				}
 				else
 					ast_codec_pref_remove(pref, format);
 			} else if (!allowing) {

Modified: team/mogorman/asterisk-5162/main/rtp.c
URL: http://svn.digium.com/view/asterisk/team/mogorman/asterisk-5162/main/rtp.c?rev=41767&r1=41766&r2=41767&view=diff
==============================================================================
--- team/mogorman/asterisk-5162/main/rtp.c (original)
+++ team/mogorman/asterisk-5162/main/rtp.c Fri Sep  1 17:28:26 2006
@@ -160,6 +160,7 @@
 	int rtp_lookup_code_cache_result;
 	struct ast_rtcp *rtcp;
 	struct ast_rtp *bridged;        /*!< Who we are Packet bridged to */
+	struct ast_codec_pref pref;
 };
 
 /* Forward declarations */
@@ -2473,6 +2474,35 @@
 	return 0;
 }
 
+int ast_rtp_codec_setpref(struct ast_rtp *rtp, struct ast_codec_pref *prefs)
+{
+	int x;
+	for (x = 0; x < 32; x++) {   /* Ugly way */
+		rtp->pref.order[x] = prefs->order[x];
+		rtp->pref.framing[x] = prefs->framing[x];
+	}
+	if (rtp->smoother)
+		ast_smoother_free(rtp->smoother);
+	rtp->smoother = NULL;
+	return 0;
+}
+
+struct ast_codec_pref *ast_rtp_codec_getpref(struct ast_rtp *rtp)
+{
+	return &rtp->pref;
+}
+
+int ast_rtp_codec_getformat(int pt)
+{
+	if (pt < 0 || pt > MAX_RTP_PT)
+		return 0; /* bogus payload type */
+
+	if (static_RTP_PT[pt].isAstFormat)
+		return static_RTP_PT[pt].code;
+	else
+		return 0;
+}
+
 int ast_rtp_write(struct ast_rtp *rtp, struct ast_frame *_f)
 {
 	struct ast_frame *f;
@@ -2515,99 +2545,29 @@
 		rtp->smoother = NULL;
 	}
 
-
-	switch(subclass) {
-	case AST_FORMAT_SLINEAR:
-		if (!rtp->smoother) {
-			rtp->smoother = ast_smoother_new(320);
-		}
-		if (!rtp->smoother) {
-			ast_log(LOG_WARNING, "Unable to create smoother :(\n");
-			return -1;
-		}
-		ast_smoother_feed_be(rtp->smoother, _f);
-		
+	if (!rtp->smoother) {
+		struct ast_format_list fmt = ast_codec_pref_getsize(&rtp->pref, subclass);
+		if (fmt.inc_ms) { /* if codec parameters is set / avoid division by zero */
+			if (!(rtp->smoother = ast_smoother_new((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms))) {
+				ast_log(LOG_WARNING, "Unable to create smoother: format: %d ms: %d len: %d\n", subclass, fmt.cur_ms, ((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms));
+				return -1;
+			}
+			if (fmt.flags)
+				ast_smoother_set_flags(rtp->smoother, fmt.flags);
+			if (option_debug)
+				ast_log(LOG_DEBUG, "Created smoother: format: %d ms: %d len: %d\n", subclass, fmt.cur_ms, ((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms));
+		}
+	}
+	if (rtp->smoother) {
+		if (ast_smoother_test_flag(rtp->smoother, AST_SMOOTHER_FLAG_BE)) {
+			ast_smoother_feed_be(rtp->smoother, _f);
+		} else {
+			ast_smoother_feed(rtp->smoother, _f);
+		}
+
 		while((f = ast_smoother_read(rtp->smoother)))
 			ast_rtp_raw_write(rtp, f, codec);
-		break;
-	case AST_FORMAT_ULAW:
-	case AST_FORMAT_ALAW:
-		if (!rtp->smoother) {
-			rtp->smoother = ast_smoother_new(160);
-		}
-		if (!rtp->smoother) {
-			ast_log(LOG_WARNING, "Unable to create smoother :(\n");
-			return -1;
-		}
-		ast_smoother_feed(rtp->smoother, _f);
-		
-		while((f = ast_smoother_read(rtp->smoother)))
-			ast_rtp_raw_write(rtp, f, codec);
-		break;
-	case AST_FORMAT_ADPCM:
-	case AST_FORMAT_G726:
-	case AST_FORMAT_G726_AAL2:
-		if (!rtp->smoother) {
-			rtp->smoother = ast_smoother_new(80);
-		}
-		if (!rtp->smoother) {
-			ast_log(LOG_WARNING, "Unable to create smoother :(\n");
-			return -1;
-		}
-		ast_smoother_feed(rtp->smoother, _f);
-		
-		while((f = ast_smoother_read(rtp->smoother)))
-			ast_rtp_raw_write(rtp, f, codec);
-		break;
-	case AST_FORMAT_G729A:
-		if (!rtp->smoother) {
-			rtp->smoother = ast_smoother_new(20);
-			if (rtp->smoother)
-				ast_smoother_set_flags(rtp->smoother, AST_SMOOTHER_FLAG_G729);
-		}
-		if (!rtp->smoother) {
-			ast_log(LOG_WARNING, "Unable to create g729 smoother :(\n");
-			return -1;
-		}
-		ast_smoother_feed(rtp->smoother, _f);
-		
-		while((f = ast_smoother_read(rtp->smoother)))
-			ast_rtp_raw_write(rtp, f, codec);
-		break;
-	case AST_FORMAT_GSM:
-		if (!rtp->smoother) {
-			rtp->smoother = ast_smoother_new(33);
-		}
-		if (!rtp->smoother) {
-			ast_log(LOG_WARNING, "Unable to create GSM smoother :(\n");
-			return -1;
-		}
-		ast_smoother_feed(rtp->smoother, _f);
-		while((f = ast_smoother_read(rtp->smoother)))
-			ast_rtp_raw_write(rtp, f, codec);
-		break;
-	case AST_FORMAT_ILBC:
-		if (!rtp->smoother) {
-			rtp->smoother = ast_smoother_new(50);
-		}
-		if (!rtp->smoother) {
-			ast_log(LOG_WARNING, "Unable to create ILBC smoother :(\n");
-			return -1;
-		}
-		ast_smoother_feed(rtp->smoother, _f);
-		while((f = ast_smoother_read(rtp->smoother)))
-			ast_rtp_raw_write(rtp, f, codec);
-		break;
-	default:	
-		ast_log(LOG_WARNING, "Not sure about sending format %s packets\n", ast_getformatname(subclass));
-		/* fall through to... */
-	case AST_FORMAT_H261:
-	case AST_FORMAT_H263:
-	case AST_FORMAT_H263_PLUS:
-	case AST_FORMAT_H264:
-	case AST_FORMAT_G723_1:
-	case AST_FORMAT_LPC10:
-	case AST_FORMAT_SPEEX:
+	} else {
 	        /* Don't buffer outgoing frames; send them one-per-packet: */
 		if (_f->offset < hdrlen) {
 			f = ast_frdup(_f);



More information about the asterisk-commits mailing list