soc.2008.vv: 8501cf8c: Added functions to help with the creatio...

maiku at soc.pidgin.im maiku at soc.pidgin.im
Wed Jun 4 15:42:09 EDT 2008


-----------------------------------------------------------------
Revision: 8501cf8c5571def9ebdca1644bc12e2145ab5a90
Ancestor: d5688257ef6db8fbbc8bf1fc309bdf51ffb88a64
Author: maiku at soc.pidgin.im
Date: 2008-06-03T23:35:39
Branch: im.pidgin.soc.2008.vv
URL: http://d.pidgin.im/viewmtn/revision/info/8501cf8c5571def9ebdca1644bc12e2145ab5a90

Modified files:
        libpurple/protocols/jabber/jingle.c

ChangeLog: 

Added functions to help with the creation of Jingle packets.
Also tweaked a few Jingle attribute values.

-------------- next part --------------
============================================================
--- libpurple/protocols/jabber/jingle.c	ee26fd1bf20926dc926a656c83fae0d67b840ee2
+++ libpurple/protocols/jabber/jingle.c	0ceebeacd7cbdd6e1dfbce35ea39a620cfc18bc3
@@ -287,10 +287,13 @@ jabber_jingle_get_candidates(const xmlno
 		c = fs_candidate_new(xmlnode_get_attrib(candidate, "component"), 
 							atoi(xmlnode_get_attrib(candidate, "component")),
 							strcmp(type, "host") == 0 ?
-								FS_CANDIDATE_TYPE_HOST :
-								strcmp(type, "prflx") == 0 ?
-									FS_CANDIDATE_TYPE_PRFLX :
-									FS_CANDIDATE_TYPE_RELAY,
+							FS_CANDIDATE_TYPE_HOST :
+							strcmp(type, "prflx") == 0 ?
+							FS_CANDIDATE_TYPE_PRFLX :
+							strcmp(type, "relay") == 0 ?
+							FS_CANDIDATE_TYPE_RELAY :
+							strcmp(type, "srflx") == 0 ?
+							FS_CANDIDATE_TYPE_SRFLX : 0,
 							strcmp(xmlnode_get_attrib(candidate, "protocol"),
 							  "udp") == 0 ? 
 				 				FS_NETWORK_PROTOCOL_UDP :
@@ -353,49 +356,11 @@ jabber_jingle_session_is_initiator(const
 	return sess->is_initiator;
 }
 
-static xmlnode *
-jabber_jingle_session_create_jingle_element(const JingleSession *sess,
-											const char *action)
+static void
+jabber_jingle_session_add_payload_types(const JingleSessionContent *jsc,
+					xmlnode *description,
+					const GList *codecs)
 {
-	xmlnode *jingle = xmlnode_new("jingle");
-	xmlnode_set_namespace(jingle, "urn:xmpp:tmp:jingle");
-	xmlnode_set_attrib(jingle, "action", action);
-	xmlnode_set_attrib(jingle, "initiator", 
-					   jabber_jingle_session_get_initiator(sess));
-	xmlnode_set_attrib(jingle, "responder", 
-					   jabber_jingle_session_is_initiator(sess) ?
-					   jabber_jingle_session_get_remote_jid(sess) :
-					   jabber_jingle_session_get_initiator(sess));
-	xmlnode_set_attrib(jingle, "sid", sess->id);
-	
-	return jingle;
-}
-
-static xmlnode *
-jabber_jingle_session_create_terminate(const JingleSession *sess,
-									   const char *reasoncode,
-									   const char *reasontext)
-{
-	xmlnode *jingle = 
-		jabber_jingle_session_create_jingle_element(sess, "session-terminate");
-	xmlnode_set_attrib(jingle, "resoncode", reasoncode);
-	if (reasontext) {
-		xmlnode_set_attrib(jingle, "reasontext", reasontext);
-	}
-	xmlnode_set_attrib(jingle, "sid", sess->id);
-	
-	return jingle;
-}
-
-static xmlnode *
-jabber_jingle_session_create_description(const JingleSession *sess)
-{
-    GList *codecs = purple_media_get_local_audio_codecs(sess->media);
-    xmlnode *description = xmlnode_new("description");
-
-	xmlnode_set_namespace(description, "urn:xmpp:tmp:jingle:apps:audio-rtp");
-	
-	/* get codecs */
 	for (; codecs ; codecs = codecs->next) {
 		FsCodec *codec = (FsCodec*)codecs->data;
 		char id[8], clockrate[10], channels[10];
@@ -403,49 +368,51 @@ jabber_jingle_session_create_description
 		
 		g_snprintf(id, sizeof(id), "%d", codec->id);
 		g_snprintf(clockrate, sizeof(clockrate), "%d", codec->clock_rate);
-		g_snprintf(channels, sizeof(channels), "%d", codec->channels);
+		g_snprintf(channels, sizeof(channels), "%d",
+			   codec->channels == 0 ? 1 : codec->channels);
 		
 		xmlnode_set_attrib(payload, "name", codec->encoding_name);
 		xmlnode_set_attrib(payload, "id", id);
 		xmlnode_set_attrib(payload, "clockrate", clockrate);
 		xmlnode_set_attrib(payload, "channels", channels);
-    }
-    
-    fs_codec_list_destroy(codecs);
-    return description;
+	}
 }
-    
-static guint
-jabber_jingle_get_priority(guint type, guint network)
+
+static xmlnode *
+jabber_jingle_session_add_description_vv(const JingleSessionContent *jsc,
+					 xmlnode *description)
 {
-	switch (type) {
-		case FS_CANDIDATE_TYPE_HOST:
-			return network == 0 ? 4096 : network == 1 ? 2048 : 1024;
-			break;
-		case FS_CANDIDATE_TYPE_PRFLX:
-			return 126;
-			break;
-		case FS_CANDIDATE_TYPE_RELAY:
-			return 100;
-			break;
-		default:
-			return 0; /* unknown type, should not happen */
-	}
+	xmlnode_set_attrib(description, "profile", "RTP/AVP");
+	return description;
 }
 
 static xmlnode *
-jabber_jingle_session_create_candidate_info(FsCandidate *c, FsCandidate *remote)
+jabber_jingle_session_add_description(const JingleSessionContent *jsc,
+				      xmlnode *content)
 {
+	xmlnode *description = xmlnode_new_child(content, "description");
+	const gchar *type = jabber_jingle_session_content_get_type(jsc);
+	xmlnode_set_namespace(description, type);
+
+	if (!strcmp(type, "urn:xmpp:tmp:jingle:apps:audio-rtp") ||
+			!strcmp(type, "urn:xmpp:tmp:jingle:apps:video-rtp"))
+		return jabber_jingle_session_add_description_vv(jsc, description);
+	else
+		return description;
+}
+
+static xmlnode *
+jabber_jingle_session_add_candidate_iceudp(xmlnode *transport,
+					   FsCandidate *c,
+					   FsCandidate *remote)
+{
 	char port[8];
 	char prio[8];
 	char component[8];
-	xmlnode *candidate = NULL;
+	xmlnode *candidate = xmlnode_new_child(transport, "candidate");
 	
-	candidate = xmlnode_new("candidate");
-	
 	g_snprintf(port, sizeof(port), "%d", c->port);
-	g_snprintf(prio, sizeof(prio), "%d", 
-		   jabber_jingle_get_priority(c->type, 0));
+	g_snprintf(prio, sizeof(prio), "%d", c->priority);
 	g_snprintf(component, sizeof(component), "%d", c->component_id);
 	
 	xmlnode_set_attrib(candidate, "component", component);
@@ -459,9 +426,9 @@ jabber_jingle_session_create_candidate_i
 			   c->proto == FS_NETWORK_PROTOCOL_UDP ?
 			   "udp" : "tcp");
 	if (c->username)
-		xmlnode_set_attrib(candidate, "ufrag", c->username);
+		xmlnode_set_attrib(transport, "ufrag", c->username);
 	if (c->password)
-		xmlnode_set_attrib(candidate, "pwd", c->password);
+		xmlnode_set_attrib(transport, "pwd", c->password);
 	
 	xmlnode_set_attrib(candidate, "type", 
 			   c->type == FS_CANDIDATE_TYPE_HOST ? 
@@ -469,7 +436,9 @@ jabber_jingle_session_create_candidate_i
 			   c->type == FS_CANDIDATE_TYPE_PRFLX ? 
 			   "prflx" :
 		       	   c->type == FS_CANDIDATE_TYPE_RELAY ? 
-			   "relay" : NULL);
+			   "relay" :
+			   c->type == FS_CANDIDATE_TYPE_SRFLX ?
+			   "srflx" : NULL);
 
 	/* relay */
 	if (c->type == FS_CANDIDATE_TYPE_RELAY) {
@@ -486,14 +455,117 @@ jabber_jingle_session_create_candidate_i
 	return candidate;
 }
 
+static xmlnode *
+jabber_jingle_session_add_transport(const JingleSessionContent *jsc,
+				    xmlnode *content)
+{
+	xmlnode *transport = xmlnode_new_child(content, "transport");
+	const gchar *transport_type = jabber_jingle_session_content_get_transport_type(jsc);
+	xmlnode_set_namespace(transport, transport_type);
+	return transport;
+}
+
+static xmlnode *
+jabber_jingle_session_add_content(const JingleSessionContent *jsc,
+				  xmlnode *jingle)
+{
+	xmlnode *content = xmlnode_new_child(jingle, "content");
+	xmlnode_set_attrib(content, "creator",
+			   jabber_jingle_session_content_get_creator(jsc));
+	xmlnode_set_attrib(content, "name",
+			   jabber_jingle_session_content_get_name(jsc));
+	xmlnode_set_attrib(content, "sender",
+			   jabber_jingle_session_content_get_sender(jsc));
+	return content;
+}
+
+
+static xmlnode *
+jabber_jingle_session_add_jingle(const JingleSession *sess,
+				 JabberIq *iq, const char *action)
+{
+	xmlnode *jingle = iq ? xmlnode_new_child(iq->node, "jingle") : 
+				xmlnode_new("jingle");
+	xmlnode_set_namespace(jingle, "urn:xmpp:tmp:jingle");
+	xmlnode_set_attrib(jingle, "action", action);
+	xmlnode_set_attrib(jingle, "initiator", 
+			   jabber_jingle_session_get_initiator(sess));
+	xmlnode_set_attrib(jingle, "responder", 
+			   jabber_jingle_session_is_initiator(sess) ?
+			   jabber_jingle_session_get_remote_jid(sess) :
+			   jabber_jingle_session_get_initiator(sess));
+	xmlnode_set_attrib(jingle, "sid", jabber_jingle_session_get_id(sess));
+	
+	return jingle;
+}
+
+static JabberIq *
+jabber_jingle_session_create_iq(const JingleSession *session)
+{
+	JabberIq *result = jabber_iq_new(jabber_jingle_session_get_js(session),
+					 JABBER_IQ_SET);
+	gchar *from = g_strdup_printf("%s@%s/%s", session->js->user->node,
+				      session->js->user->domain,
+				      session->js->user->resource);
+	xmlnode_set_attrib(result->node, "from", from);
+	g_free(from);
+	xmlnode_set_attrib(result->node, "to",
+			   jabber_jingle_session_get_remote_jid(session));
+	return result;
+}
+
+static xmlnode *
+jabber_jingle_session_create_terminate(const JingleSession *sess,
+				       const char *reasoncode,
+				       const char *reasontext)
+{
+	xmlnode *jingle = 
+		jabber_jingle_session_add_jingle(sess, NULL, "session-terminate");
+	xmlnode_set_attrib(jingle, "resoncode", reasoncode);
+	if (reasontext) {
+		xmlnode_set_attrib(jingle, "reasontext", reasontext);
+	}
+	xmlnode_set_attrib(jingle, "sid", sess->id);
+	
+	return jingle;
+}
+
+static xmlnode *
+jabber_jingle_session_create_description(const JingleSession *sess)
+{
+    GList *codecs = purple_media_get_local_audio_codecs(sess->media);
+    xmlnode *description = xmlnode_new("description");
+
+	xmlnode_set_namespace(description, "urn:xmpp:tmp:jingle:apps:audio-rtp");
+	
+	/* get codecs */
+	for (; codecs ; codecs = codecs->next) {
+		FsCodec *codec = (FsCodec*)codecs->data;
+		char id[8], clockrate[10], channels[10];
+		xmlnode *payload = xmlnode_new_child(description, "payload-type");
+		
+		g_snprintf(id, sizeof(id), "%d", codec->id);
+		g_snprintf(clockrate, sizeof(clockrate), "%d", codec->clock_rate);
+		g_snprintf(channels, sizeof(channels), "%d", codec->channels);
+		
+		xmlnode_set_attrib(payload, "name", codec->encoding_name);
+		xmlnode_set_attrib(payload, "id", id);
+		xmlnode_set_attrib(payload, "clockrate", clockrate);
+		xmlnode_set_attrib(payload, "channels", channels);
+    }
+    
+    fs_codec_list_destroy(codecs);
+    return description;
+}
+
 /* split into two separate methods, one to generate session-accept
 	(includes codecs) and one to generate transport-info (includes transports
 	candidates) */
 static xmlnode *
 jabber_jingle_session_create_session_accept(const JingleSession *sess)
 {
-	xmlnode *jingle = 
-		jabber_jingle_session_create_jingle_element(sess, "session-accept");
+	xmlnode *jingle =
+		jabber_jingle_session_add_jingle(sess, NULL, "session-accept");
 	xmlnode *content = NULL;
 	xmlnode *description = NULL;
 	xmlnode *transport = NULL;
@@ -510,10 +582,9 @@ jabber_jingle_session_create_session_acc
 
 	transport = xmlnode_new_child(content, "transport");
 	xmlnode_set_namespace(transport, "urn:xmpp:tmp:jingle:transports:ice-udp");
-	candidate = jabber_jingle_session_create_candidate_info(
+	candidate = jabber_jingle_session_add_candidate_iceudp(transport,
 			purple_media_get_local_candidate(sess->media),
 			purple_media_get_remote_candidate(sess->media));
-	xmlnode_insert_child(transport, candidate);
 
 	return jingle;
 }
@@ -521,8 +592,8 @@ jabber_jingle_session_create_transport_i
 static xmlnode *
 jabber_jingle_session_create_transport_info(const JingleSession *sess)
 {
-	xmlnode *jingle = 
-		jabber_jingle_session_create_jingle_element(sess, "transport-info");
+	xmlnode *jingle =
+		jabber_jingle_session_add_jingle(sess, NULL, "transport-info");
 	xmlnode *content = NULL;
 	xmlnode *transport = NULL;
 	GList *candidates = purple_media_get_local_audio_candidates(sess->media);
@@ -543,8 +614,7 @@ jabber_jingle_session_create_transport_i
 			continue;
 		}
 
-		xmlnode_insert_child(transport,
-				     jabber_jingle_session_create_candidate_info(c, NULL));
+		jabber_jingle_session_add_candidate_iceudp(transport, c, NULL);
 	}
 	fs_candidate_list_destroy(candidates);
 	
@@ -557,7 +627,7 @@ jabber_jingle_session_create_content_rep
 					     FsCandidate *remote_candidate)
 {
 	xmlnode *jingle = 
-		jabber_jingle_session_create_jingle_element(sess, "content-replace");
+		jabber_jingle_session_add_jingle(sess, NULL, "content-replace");
 	xmlnode *content = NULL;
 	xmlnode *transport = NULL;
 
@@ -577,9 +647,8 @@ jabber_jingle_session_create_content_rep
 
 	transport = xmlnode_new_child(content, "transport");
 	xmlnode_set_namespace(transport, "urn:xmpp:tmp:jingle:transports:ice-udp");
-	xmlnode_insert_child(transport,
-			     jabber_jingle_session_create_candidate_info(native_candidate,
-									 remote_candidate));
+	jabber_jingle_session_add_candidate_iceudp(transport, native_candidate,
+						   remote_candidate);
 
 	purple_debug_info("jingle", "End create content modify\n");
 	
@@ -590,7 +659,7 @@ jabber_jingle_session_create_content_acc
 jabber_jingle_session_create_content_accept(const JingleSession *sess)
 {
 	xmlnode *jingle = 
-		jabber_jingle_session_create_jingle_element(sess, "content-accept");
+		jabber_jingle_session_add_jingle(sess, NULL, "content-accept");
 	
     xmlnode *content = xmlnode_new_child(jingle, "content");
 	xmlnode *description = jabber_jingle_session_create_description(sess);
@@ -669,9 +738,9 @@ jabber_jingle_session_content_create_med
 	gchar sender[10] = "";
 
 	if (type & PURPLE_MEDIA_AUDIO) {
-		if (type & PURPLE_MEDIA_SEND_AUDIO)
+		if (type == PURPLE_MEDIA_SEND_AUDIO)
 			strcpy(sender, "initiator");
-		else if (type & PURPLE_MEDIA_RECV_AUDIO)
+		else if (type == PURPLE_MEDIA_RECV_AUDIO)
 			strcpy(sender, "responder");
 		else
 			strcpy(sender, "both");


More information about the Commits mailing list