media: sort candidates in sipe-media.c
authorJakub Adam <jakub.adam@ktknet.cz>
Wed, 26 Mar 2014 09:56:31 +0000 (26 10:56 +0100)
committerJakub Adam <jakub.adam@ktknet.cz>
Wed, 26 Mar 2014 13:17:32 +0000 (26 14:17 +0100)
With this change we have only a single place where we do the sorting and
can avoid unnecessary list copies.

src/core/sdpmsg.c
src/core/sipe-media.c

index 84801ca..95477a2 100644 (file)
@@ -414,19 +414,6 @@ base64_unpad(const gchar *str)
        return result;
 }
 
-static gint
-candidate_sort_cb(struct sdpcandidate *c1, struct sdpcandidate *c2)
-{
-       int cmp = sipe_strcompare(c1->foundation, c2->foundation);
-       if (cmp == 0) {
-               cmp = sipe_strcompare(c1->username, c2->username);
-               if (cmp == 0)
-                       cmp = c1->component - c2->component;
-       }
-
-       return cmp;
-}
-
 static gchar *
 candidates_to_string(GSList *candidates, SipeIceVersion ice_version)
 {
@@ -434,9 +421,6 @@ candidates_to_string(GSList *candidates, SipeIceVersion ice_version)
        GSList *i;
        GSList *processed_tcp_candidates = NULL;
 
-       candidates = g_slist_copy(candidates);
-       candidates = g_slist_sort(candidates, (GCompareFunc)candidate_sort_cb);
-
        for (i = candidates; i; i = i->next) {
                struct sdpcandidate *c = i->data;
                const gchar *protocol;
@@ -555,7 +539,6 @@ candidates_to_string(GSList *candidates, SipeIceVersion ice_version)
                }
        }
 
-       g_slist_free(candidates);
        g_slist_free(processed_tcp_candidates);
 
        return g_string_free(result, FALSE);
@@ -566,9 +549,6 @@ remote_candidates_to_string(GSList *candidates, SipeIceVersion ice_version)
 {
        GString *result = g_string_new("");
 
-       candidates = g_slist_copy(candidates);
-       candidates = g_slist_sort(candidates, (GCompareFunc)candidate_sort_cb);
-
        if (candidates) {
                if (ice_version == SIPE_ICE_RFC_5245) {
                        GSList *i;
@@ -588,8 +568,6 @@ remote_candidates_to_string(GSList *candidates, SipeIceVersion ice_version)
                }
        }
 
-       g_slist_free(candidates);
-
        return g_string_free(result, FALSE);
 }
 
index 6bfd1cb..cce76c5 100644 (file)
@@ -100,6 +100,19 @@ sipe_media_call_free(struct sipe_media_call_private *call_private)
        }
 }
 
+static gint
+candidate_sort_cb(struct sdpcandidate *c1, struct sdpcandidate *c2)
+{
+       int cmp = sipe_strcompare(c1->foundation, c2->foundation);
+       if (cmp == 0) {
+               cmp = sipe_strcompare(c1->username, c2->username);
+               if (cmp == 0)
+                       cmp = c1->component - c2->component;
+       }
+
+       return cmp;
+}
+
 static GSList *
 backend_candidates_to_sdpcandidate(GList *candidates)
 {
@@ -122,7 +135,8 @@ backend_candidates_to_sdpcandidate(GList *candidates)
                c->username = sipe_backend_candidate_get_username(candidate);
                c->password = sipe_backend_candidate_get_password(candidate);
 
-               result = g_slist_append(result, c);
+               result = g_slist_insert_sorted(result, c,
+                                              (GCompareFunc)candidate_sort_cb);
        }
 
        return result;