[telepathy-gabble/master] Move jingle resource selection to utils
Sjoerd Simons
sjoerd.simons at collabora.co.uk
Tue Dec 29 05:34:37 PST 2009
---
src/media-channel.c | 139 +-------------------------------------------------
src/util.c | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++
src/util.h | 11 ++++
3 files changed, 153 insertions(+), 137 deletions(-)
diff --git a/src/media-channel.c b/src/media-channel.c
index 0066029..ea72050 100644
--- a/src/media-channel.c
+++ b/src/media-channel.c
@@ -1409,141 +1409,6 @@ _pick_best_content_type (GabbleMediaChannel *chan, TpHandle peer,
return NULL;
}
-static const gchar *
-_pick_best_resource (GabbleMediaChannel *chan,
- TpHandle peer, gboolean want_audio, gboolean want_video,
- const char **transport_ns, JingleDialect *dialect)
-{
- /* We prefer gtalk-p2p to ice, because it can use tcp and https relays (if
- * available). */
- static const GabbleFeatureFallback transports[] = {
- { TRUE, TWICE (NS_GOOGLE_TRANSPORT_P2P) },
- { TRUE, TWICE (NS_JINGLE_TRANSPORT_ICEUDP) },
- { TRUE, TWICE (NS_JINGLE_TRANSPORT_RAWUDP) },
- { FALSE, NULL, NULL }
- };
- GabbleMediaChannelPrivate *priv = chan->priv;
- GabblePresence *presence;
- GabbleCapabilitySet *caps;
- const gchar *resource = NULL;
-
- presence = gabble_presence_cache_get (priv->conn->presence_cache, peer);
-
- if (presence == NULL)
- {
- DEBUG ("contact %d has no presence available", peer);
- return NULL;
- }
-
- *dialect = JINGLE_DIALECT_ERROR;
- *transport_ns = NULL;
-
- g_return_val_if_fail (want_audio || want_video, NULL);
-
- /* from here on, goto FINALLY to free this, instead of returning early */
- caps = gabble_capability_set_new ();
-
- /* Try newest Jingle standard */
- gabble_capability_set_add (caps, NS_JINGLE_RTP);
-
- if (want_audio)
- gabble_capability_set_add (caps, NS_JINGLE_RTP_AUDIO);
- if (want_video)
- gabble_capability_set_add (caps, NS_JINGLE_RTP_VIDEO);
-
- resource = gabble_presence_pick_resource_by_caps (presence,
- gabble_capability_set_predicate_at_least, caps);
-
- if (resource != NULL)
- {
- *dialect = JINGLE_DIALECT_V032;
- goto CHOOSE_TRANSPORT;
- }
-
- /* Else try older Jingle draft */
- gabble_capability_set_clear (caps);
-
- if (want_audio)
- gabble_capability_set_add (caps, NS_JINGLE_DESCRIPTION_AUDIO);
- if (want_video)
- gabble_capability_set_add (caps, NS_JINGLE_DESCRIPTION_VIDEO);
-
- resource = gabble_presence_pick_resource_by_caps (presence,
- gabble_capability_set_predicate_at_least, caps);
-
- if (resource != NULL)
- {
- *dialect = JINGLE_DIALECT_V015;
- goto CHOOSE_TRANSPORT;
- }
-
- /* The Google dialects can't do video alone. */
- if (!want_audio)
- {
- DEBUG ("No resource which supports video alone available");
- goto FINALLY;
- }
-
- /* Okay, let's try GTalk 0.3, possibly with video. */
- gabble_capability_set_clear (caps);
- gabble_capability_set_add (caps, NS_GOOGLE_FEAT_VOICE);
-
- if (want_video)
- gabble_capability_set_add (caps, NS_GOOGLE_FEAT_VIDEO);
-
- resource = gabble_presence_pick_resource_by_caps (presence,
- gabble_capability_set_predicate_at_least, caps);
-
- if (resource != NULL)
- {
- *dialect = JINGLE_DIALECT_GTALK3;
- goto CHOOSE_TRANSPORT;
- }
-
- if (want_video)
- {
- DEBUG ("No resource which supports audio+video available");
- goto FINALLY;
- }
-
- /* Maybe GTalk 0.4 will save us all... ? */
- gabble_capability_set_clear (caps);
- gabble_capability_set_add (caps, NS_GOOGLE_FEAT_VOICE);
- gabble_capability_set_add (caps, NS_GOOGLE_TRANSPORT_P2P);
- resource = gabble_presence_pick_resource_by_caps (presence,
- gabble_capability_set_predicate_at_least, caps);
-
- if (resource != NULL)
- {
- *dialect = JINGLE_DIALECT_GTALK4;
- goto CHOOSE_TRANSPORT;
- }
-
- /* Nope, nothing we can do. */
- goto FINALLY;
-
-CHOOSE_TRANSPORT:
-
-
- if (*dialect == JINGLE_DIALECT_GTALK4 || *dialect == JINGLE_DIALECT_GTALK3)
- {
- /* the GTalk dialects only support google p2p as transport protocol. */
- *transport_ns = NS_GOOGLE_TRANSPORT_P2P;
- }
- else
- {
- *transport_ns = gabble_presence_resource_pick_best_feature (presence,
- resource, transports, gabble_capability_set_predicate_has);
- }
-
- if (*transport_ns == NULL)
- resource = NULL;
-
-FINALLY:
- gabble_capability_set_free (caps);
- return resource;
-}
-
typedef struct {
/* number of streams requested == number of content objects */
guint len;
@@ -1742,8 +1607,8 @@ _gabble_media_channel_request_contents (GabbleMediaChannel *chan,
g_assert (priv->streams->len == 0);
- peer_resource = _pick_best_resource (chan, peer, want_audio, want_video,
- &transport_ns, &dialect);
+ peer_resource = jingle_pick_best_resource (priv->conn, peer,
+ want_audio, want_video, &transport_ns, &dialect);
if (peer_resource == NULL)
{
diff --git a/src/util.c b/src/util.c
index 351bacd..49a73ce 100644
--- a/src/util.c
+++ b/src/util.c
@@ -41,6 +41,7 @@
#include "connection.h"
#include "debug.h"
#include "namespaces.h"
+#include "presence-cache.h"
gchar *
sha1_hex (const gchar *bytes,
@@ -1022,3 +1023,142 @@ ensure_bare_contact_from_jid (GabbleConnection *conn,
contact_factory = wocky_session_get_contact_factory (conn->session);
return wocky_contact_factory_ensure_bare_contact (contact_factory, jid);
}
+
+#define TWICE(x) x, x
+
+const gchar *
+jingle_pick_best_resource (GabbleConnection *conn,
+ TpHandle peer,
+ gboolean want_audio,
+ gboolean want_video,
+ const char **transport_ns,
+ JingleDialect *dialect)
+{
+ /* We prefer gtalk-p2p to ice, because it can use tcp and https relays (if
+ * available). */
+ static const GabbleFeatureFallback transports[] = {
+ { TRUE, TWICE (NS_GOOGLE_TRANSPORT_P2P) },
+ { TRUE, TWICE (NS_JINGLE_TRANSPORT_ICEUDP) },
+ { TRUE, TWICE (NS_JINGLE_TRANSPORT_RAWUDP) },
+ { FALSE, NULL, NULL }
+ };
+ GabblePresence *presence;
+ GabbleCapabilitySet *caps;
+ const gchar *resource = NULL;
+
+ presence = gabble_presence_cache_get (conn->presence_cache, peer);
+
+ if (presence == NULL)
+ {
+ DEBUG ("contact %d has no presence available", peer);
+ return NULL;
+ }
+
+ *dialect = JINGLE_DIALECT_ERROR;
+ *transport_ns = NULL;
+
+ g_return_val_if_fail (want_audio || want_video, NULL);
+
+ /* from here on, goto FINALLY to free this, instead of returning early */
+ caps = gabble_capability_set_new ();
+
+ /* Try newest Jingle standard */
+ gabble_capability_set_add (caps, NS_JINGLE_RTP);
+
+ if (want_audio)
+ gabble_capability_set_add (caps, NS_JINGLE_RTP_AUDIO);
+ if (want_video)
+ gabble_capability_set_add (caps, NS_JINGLE_RTP_VIDEO);
+
+ resource = gabble_presence_pick_resource_by_caps (presence,
+ gabble_capability_set_predicate_at_least, caps);
+
+ if (resource != NULL)
+ {
+ *dialect = JINGLE_DIALECT_V032;
+ goto CHOOSE_TRANSPORT;
+ }
+
+ /* Else try older Jingle draft */
+ gabble_capability_set_clear (caps);
+
+ if (want_audio)
+ gabble_capability_set_add (caps, NS_JINGLE_DESCRIPTION_AUDIO);
+ if (want_video)
+ gabble_capability_set_add (caps, NS_JINGLE_DESCRIPTION_VIDEO);
+
+ resource = gabble_presence_pick_resource_by_caps (presence,
+ gabble_capability_set_predicate_at_least, caps);
+
+ if (resource != NULL)
+ {
+ *dialect = JINGLE_DIALECT_V015;
+ goto CHOOSE_TRANSPORT;
+ }
+
+ /* The Google dialects can't do video alone. */
+ if (!want_audio)
+ {
+ DEBUG ("No resource which supports video alone available");
+ goto FINALLY;
+ }
+
+ /* Okay, let's try GTalk 0.3, possibly with video. */
+ gabble_capability_set_clear (caps);
+ gabble_capability_set_add (caps, NS_GOOGLE_FEAT_VOICE);
+
+ if (want_video)
+ gabble_capability_set_add (caps, NS_GOOGLE_FEAT_VIDEO);
+
+ resource = gabble_presence_pick_resource_by_caps (presence,
+ gabble_capability_set_predicate_at_least, caps);
+
+ if (resource != NULL)
+ {
+ *dialect = JINGLE_DIALECT_GTALK3;
+ goto CHOOSE_TRANSPORT;
+ }
+
+ if (want_video)
+ {
+ DEBUG ("No resource which supports audio+video available");
+ goto FINALLY;
+ }
+
+ /* Maybe GTalk 0.4 will save us all... ? */
+ gabble_capability_set_clear (caps);
+ gabble_capability_set_add (caps, NS_GOOGLE_FEAT_VOICE);
+ gabble_capability_set_add (caps, NS_GOOGLE_TRANSPORT_P2P);
+ resource = gabble_presence_pick_resource_by_caps (presence,
+ gabble_capability_set_predicate_at_least, caps);
+
+ if (resource != NULL)
+ {
+ *dialect = JINGLE_DIALECT_GTALK4;
+ goto CHOOSE_TRANSPORT;
+ }
+
+ /* Nope, nothing we can do. */
+ goto FINALLY;
+
+CHOOSE_TRANSPORT:
+
+
+ if (*dialect == JINGLE_DIALECT_GTALK4 || *dialect == JINGLE_DIALECT_GTALK3)
+ {
+ /* the GTalk dialects only support google p2p as transport protocol. */
+ *transport_ns = NS_GOOGLE_TRANSPORT_P2P;
+ }
+ else
+ {
+ *transport_ns = gabble_presence_resource_pick_best_feature (presence,
+ resource, transports, gabble_capability_set_predicate_has);
+ }
+
+ if (*transport_ns == NULL)
+ resource = NULL;
+
+FINALLY:
+ gabble_capability_set_free (caps);
+ return resource;
+}
diff --git a/src/util.h b/src/util.h
index 2e23d61..672d197 100644
--- a/src/util.h
+++ b/src/util.h
@@ -30,6 +30,9 @@
#include <loudmouth/loudmouth.h>
#include <wocky/wocky-bare-contact.h>
+#include "jingle-factory.h"
+#include "jingle-content.h"
+
#include "types.h"
typedef GSList * NodeIter;
@@ -101,4 +104,12 @@ GPtrArray *gabble_g_ptr_array_copy (GPtrArray *source);
WockyBareContact * ensure_bare_contact_from_jid (GabbleConnection *conn,
const gchar *jid);
+const gchar * jingle_pick_best_resource (GabbleConnection *conn,
+ TpHandle peer,
+ gboolean want_audio,
+ gboolean want_video,
+ const char **transport_ns,
+ JingleDialect *dialect);
+
+
#endif /* __GABBLE_UTIL_H__ */
--
1.5.6.5
More information about the telepathy-commits
mailing list