Mercurial > pidgin
changeset 26470:4f14455a86d2
propagate from branch 'im.pidgin.pidgin' (head f25348fdce436002d7d508f074aaa70fd9491f01)
to branch 'im.pidgin.cpw.darkrain42.xmpp.iq-handlers' (head 33f84ea59a9e7afb3a03806268589ea49622326a)
author | Paul Aurich <paul@darkrain42.org> |
---|---|
date | Fri, 03 Apr 2009 16:45:38 +0000 |
parents | 00870e5f2e90 (current diff) 4103d15e4205 (diff) |
children | 91583903b478 |
files | libpurple/protocols/jabber/caps.c libpurple/protocols/jabber/disco.c libpurple/protocols/jabber/google.c libpurple/protocols/jabber/google.h libpurple/protocols/jabber/iq.c libpurple/protocols/jabber/jabber.c libpurple/protocols/jabber/jabber.h libpurple/protocols/jabber/presence.c |
diffstat | 81 files changed, 12546 insertions(+), 61 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog Fri Apr 03 02:43:22 2009 +0000 +++ b/ChangeLog Fri Apr 03 16:45:38 2009 +0000 @@ -10,6 +10,8 @@ * Fixed NTLM authentication on big-endian systems. XMPP: + * Add voice & video support with Jingle (XEP-0166, 0167, 0176, & 0177), + and voice support with GTalk and GMail. (Mike "Maiku" Ruprecht) * Add support for in-band bytestreams for file transfers (XEP-0047). * Add support for sending attentions (equivalent to "buzz" and "nudge") using the command /buzz (XEP-0224).
--- a/ChangeLog.API Fri Apr 03 02:43:22 2009 +0000 +++ b/ChangeLog.API Fri Apr 03 16:45:38 2009 +0000 @@ -3,6 +3,7 @@ version 2.6.0 (??/??/2009): libpurple: Added: + * PurpleMedia and PurpleMediaManager API * PURPLE_BLIST_NODE * PURPLE_GROUP * PURPLE_CONTACT @@ -21,11 +22,14 @@ * purple_global_proxy_set_info * purple_log_get_activity_score * purple_network_force_online + * purple_prpl_get_media_caps + * purple_prpl_initiate_media * purple_request_field_get_group * purple_request_field_get_ui_data * purple_request_field_set_ui_data * purple_strequal * xmlnode_from_file + * xmlnode_get_parent * xmlnode_set_attrib_full Changed: @@ -60,6 +64,7 @@ * gtk_imhtml_set_return_inserts_newline * pidgin_blist_set_theme * pidgin_blist_get_theme + * pidgin_prefs_labeled_password * pidgin_sound_is_customized * pidgin_utils_init, pidgin_utils_uninit * pidgin_notify_pounce_add
--- a/configure.ac Fri Apr 03 02:43:22 2009 +0000 +++ b/configure.ac Fri Apr 03 16:45:38 2009 +0000 @@ -324,6 +324,9 @@ AC_SUBST(GLIB_CFLAGS) AC_SUBST(GLIB_LIBS) +GLIB_GENMARSHAL=`pkg-config --variable=glib_genmarshal glib-2.0` +AC_SUBST(GLIB_GENMARSHAL) + AC_ARG_WITH([extraversion], AC_HELP_STRING([--with-extraversion=STRING], [extra version number to be displayed in Help->About and --help (for packagers)]), @@ -745,6 +748,56 @@ fi dnl ####################################################################### +dnl # Check for Farsight +dnl ####################################################################### +AC_ARG_ENABLE(farsight, + [AC_HELP_STRING([--disable-farsight], [compile without farsight support])], + enable_farsight="$enableval", enable_farsight="yes") +if test "x$enable_farsight" != "xno"; then + PKG_CHECK_MODULES(FARSIGHT, [farsight2-0.10 >= 0.0.8 gstreamer-0.10 gstreamer-plugins-base-0.10 libxml-2.0], [ + AC_DEFINE(USE_FARSIGHT, 1, [Use Farsight for voice and video]) + AC_SUBST(FARSIGHT_CFLAGS) + AC_SUBST(FARSIGHT_LIBS) + ], [ + enable_farsight="no" + ]) +fi + +dnl ####################################################################### +dnl # Check for GStreamer-properties +dnl ####################################################################### +AC_ARG_ENABLE(gstprops, + [AC_HELP_STRING([--disable-gstprops], [compile without gstreamer props])], + enable_gstprops="$enableval", enable_gstprops="yes") +if test "x$enable_gstprops" != "xno"; +then + dnl gstreamer-libs-$GST_MAJORMINOR + dnl gstreamer-gconf-$GST_MAJORMINOR + PKG_CHECK_MODULES(GSTPROPS, [gstreamer-0.10 gstreamer-plugins-base-0.10 libxml-2.0], [ + GSTPROPS_LIBS="$GSTPROPS_LIBS -lgstinterfaces-0.10" + AC_DEFINE(USE_GSTPROPS, 1, [Use GStreamer property probe for finding devices]) + AC_SUBST(GSTPROPS_LIBS) + AC_SUBST(GSTPROPS_CFLAGS) + ], [ + enable_gstprops="no" + ]) +fi + +dnl ####################################################################### +dnl # Check for Voice and Video support +dnl ####################################################################### +AC_ARG_ENABLE(vv, + [AC_HELP_STRING([--disable-vv], [compile without voice and video support])], + enable_vv="$enableval", enable_vv="yes") +if test "x$enable_vv" != "xno"; then + if test "x$enable_farsight" != "xno" -a "x$enable_gstprops" != "xno"; then + AC_DEFINE(USE_VV, 1, [Use voice and video]) + else + enable_vv="no" + fi +fi + +dnl ####################################################################### dnl # Check for Meanwhile headers (for Sametime) dnl ####################################################################### AC_ARG_ENABLE(meanwhile, @@ -2478,6 +2531,7 @@ echo echo Build with GStreamer support.. : $enable_gst echo Build with D-Bus support...... : $enable_dbus +echo Build with voice and video.... : $enable_vv if test "x$enable_dbus" = "xyes" ; then eval eval echo D-Bus services directory...... : $DBUS_SERVICES_DIR fi
--- a/finch/Makefile.am Fri Apr 03 02:43:22 2009 +0000 +++ b/finch/Makefile.am Fri Apr 03 16:45:38 2009 +0000 @@ -26,6 +26,7 @@ finch.c \ gntidle.c \ gntlog.c \ + gntmedia.c \ gntnotify.c \ gntplugin.c \ gntpounce.c \ @@ -47,6 +48,7 @@ finch.h \ gntidle.h \ gntlog.h \ + gntmedia.h \ gntnotify.h \ gntplugin.h \ gntpounce.h \
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/finch/gntmedia.c Fri Apr 03 16:45:38 2009 +0000 @@ -0,0 +1,539 @@ +/** + * @file gntmedia.c GNT Media API + * @ingroup finch + */ + +/* finch + * + * Finch is the legal property of its developers, whose names are too numerous + * to list here. Please refer to the COPYRIGHT file distributed with this + * source distribution. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA + */ + +#include "finch.h" + +#include "gntconv.h" +#include "gntmedia.h" + +#include "gnt.h" +#include "gntbutton.h" +#include "gntbox.h" +#include "gntlabel.h" + +#include "cmds.h" +#include "conversation.h" +#include "debug.h" +#include "media-gst.h" +#include "mediamanager.h" + +/* An incredibly large part of the following is from gtkmedia.c */ +#ifdef USE_VV + +#undef hangup + +#define FINCH_TYPE_MEDIA (finch_media_get_type()) +#define FINCH_MEDIA(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), FINCH_TYPE_MEDIA, FinchMedia)) +#define FINCH_MEDIA_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), FINCH_TYPE_MEDIA, FinchMediaClass)) +#define FINCH_IS_MEDIA(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), FINCH_TYPE_MEDIA)) +#define FINCH_IS_MEDIA_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), FINCH_TYPE_MEDIA)) +#define FINCH_MEDIA_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), FINCH_TYPE_MEDIA, FinchMediaClass)) + +typedef struct _FinchMedia FinchMedia; +typedef struct _FinchMediaClass FinchMediaClass; +typedef struct _FinchMediaPrivate FinchMediaPrivate; +typedef enum _FinchMediaState FinchMediaState; + +struct _FinchMediaClass +{ + GntBoxClass parent_class; +}; + +struct _FinchMedia +{ + GntBox parent; + FinchMediaPrivate *priv; +}; + +struct _FinchMediaPrivate +{ + PurpleMedia *media; + + GntWidget *accept; + GntWidget *reject; + GntWidget *hangup; + GntWidget *calling; + + PurpleConversation *conv; +}; + +#define FINCH_MEDIA_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), FINCH_TYPE_MEDIA, FinchMediaPrivate)) + +static void finch_media_class_init (FinchMediaClass *klass); +static void finch_media_init (FinchMedia *media); +static void finch_media_finalize (GObject *object); +static void finch_media_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec); +static void finch_media_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec); + +static GntBoxClass *parent_class = NULL; + +enum { + MESSAGE, + LAST_SIGNAL +}; +static guint finch_media_signals[LAST_SIGNAL] = {0}; + +enum { + PROP_0, + PROP_MEDIA, +}; + +static GType +finch_media_get_type(void) +{ + static GType type = 0; + + if (type == 0) { + static const GTypeInfo info = { + sizeof(FinchMediaClass), + NULL, + NULL, + (GClassInitFunc) finch_media_class_init, + NULL, + NULL, + sizeof(FinchMedia), + 0, + (GInstanceInitFunc) finch_media_init, + NULL + }; + type = g_type_register_static(GNT_TYPE_BOX, "FinchMedia", &info, 0); + } + return type; +} + + +static void +finch_media_class_init (FinchMediaClass *klass) +{ + GObjectClass *gobject_class = (GObjectClass*)klass; + parent_class = g_type_class_peek_parent(klass); + + gobject_class->finalize = finch_media_finalize; + gobject_class->set_property = finch_media_set_property; + gobject_class->get_property = finch_media_get_property; + + g_object_class_install_property(gobject_class, PROP_MEDIA, + g_param_spec_object("media", + "PurpleMedia", + "The PurpleMedia associated with this media.", + PURPLE_TYPE_MEDIA, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)); + + finch_media_signals[MESSAGE] = g_signal_new("message", G_TYPE_FROM_CLASS(klass), + G_SIGNAL_RUN_LAST, 0, NULL, NULL, + g_cclosure_marshal_VOID__STRING, + G_TYPE_NONE, 1, G_TYPE_STRING); + + g_type_class_add_private(klass, sizeof(FinchMediaPrivate)); +} + + +static void +finch_media_init (FinchMedia *media) +{ + media->priv = FINCH_MEDIA_GET_PRIVATE(media); + + media->priv->calling = gnt_label_new(_("Calling ... ")); + media->priv->hangup = gnt_button_new(_("Hangup")); + media->priv->accept = gnt_button_new(_("Accept")); + media->priv->reject = gnt_button_new(_("Reject")); + + gnt_box_set_alignment(GNT_BOX(media), GNT_ALIGN_MID); + + gnt_box_add_widget(GNT_BOX(media), media->priv->accept); + gnt_box_add_widget(GNT_BOX(media), media->priv->reject); +} + +static void +finch_media_finalize (GObject *media) +{ + FinchMedia *gntmedia = FINCH_MEDIA(media); + purple_debug_info("gntmedia", "finch_media_finalize\n"); + if (gntmedia->priv->media) + g_object_unref(gntmedia->priv->media); +} + +static void +finch_media_emit_message(FinchMedia *gntmedia, const char *msg) +{ + g_signal_emit(gntmedia, finch_media_signals[MESSAGE], 0, msg); +} + +static void +finch_media_connected_cb(PurpleMedia *media, FinchMedia *gntmedia) +{ + GntWidget *parent; + + finch_media_emit_message(gntmedia, _("Call in progress.")); + + gnt_box_remove(GNT_BOX(gntmedia), gntmedia->priv->accept); + gnt_box_remove(GNT_BOX(gntmedia), gntmedia->priv->reject); + gnt_box_remove(GNT_BOX(gntmedia), gntmedia->priv->hangup); + gnt_box_remove(GNT_BOX(gntmedia), gntmedia->priv->calling); + + gnt_box_add_widget(GNT_BOX(gntmedia), gntmedia->priv->hangup); + + gnt_widget_destroy(gntmedia->priv->accept); + gnt_widget_destroy(gntmedia->priv->reject); + gnt_widget_destroy(gntmedia->priv->calling); + gntmedia->priv->accept = NULL; + gntmedia->priv->reject = NULL; + gntmedia->priv->calling = NULL; + + parent = GNT_WIDGET(gntmedia); + while (parent->parent) + parent = parent->parent; + gnt_box_readjust(GNT_BOX(parent)); + gnt_widget_draw(parent); +} + +static void +finch_media_wait_cb(PurpleMedia *media, FinchMedia *gntmedia) +{ + GntWidget *parent; + + gnt_box_remove(GNT_BOX(gntmedia), gntmedia->priv->accept); + gnt_box_remove(GNT_BOX(gntmedia), gntmedia->priv->reject); + gnt_box_remove(GNT_BOX(gntmedia), gntmedia->priv->hangup); + gnt_box_remove(GNT_BOX(gntmedia), gntmedia->priv->calling); + + gnt_box_add_widget(GNT_BOX(gntmedia), gntmedia->priv->calling); + gnt_box_add_widget(GNT_BOX(gntmedia), gntmedia->priv->hangup); + + parent = GNT_WIDGET(gntmedia); + while (parent->parent) + parent = parent->parent; + gnt_box_readjust(GNT_BOX(parent)); + gnt_widget_draw(parent); +} + +static void +finch_media_state_changed_cb(PurpleMedia *media, PurpleMediaState state, + gchar *sid, gchar *name, FinchMedia *gntmedia) +{ + purple_debug_info("gntmedia", "state: %d sid: %s name: %s\n", + state, sid, name); + if (sid == NULL && name == NULL) { + if (state == PURPLE_MEDIA_STATE_END) { + finch_media_emit_message(gntmedia, + _("The call has been terminated.")); + finch_conversation_set_info_widget( + gntmedia->priv->conv, NULL); + gnt_widget_destroy(GNT_WIDGET(gntmedia)); + /* + * XXX: This shouldn't have to be here + * to free the FinchMedia widget. + */ + g_object_unref(gntmedia); + } + } else if (state == PURPLE_MEDIA_STATE_CONNECTED) { + finch_media_connected_cb(media, gntmedia); + } else if (state == PURPLE_MEDIA_STATE_NEW && + sid != NULL && name != NULL && + purple_media_is_initiator(media, sid, name) == FALSE) { + PurpleConnection *pc; + PurpleBuddy *buddy; + const gchar *alias; + PurpleMediaSessionType type = + purple_media_get_session_type(media, sid); + gchar *message = NULL; + + pc = purple_media_get_connection(gntmedia->priv->media); + buddy = purple_find_buddy( + purple_connection_get_account(pc), name); + alias = buddy ? purple_buddy_get_contact_alias(buddy) : name; + + if (type & PURPLE_MEDIA_AUDIO) { + message = g_strdup_printf( + _("%s wishes to start an audio session with you."), + alias); + } else { + message = g_strdup_printf( + _("%s is trying to start an unsuppoted media session type with you."), + alias); + } + finch_media_emit_message(gntmedia, message); + g_free(message); + } +} + +static void +finch_media_stream_info_cb(PurpleMedia *media, PurpleMediaInfoType type, + gchar *sid, gchar *name, gboolean local, FinchMedia *gntmedia) +{ + if (type == PURPLE_MEDIA_INFO_REJECT) { + finch_media_emit_message(gntmedia, + _("You have rejected the call.")); + } +} + +static void +finch_media_accept_cb(PurpleMedia *media, GntWidget *widget) +{ + purple_media_stream_info(media, PURPLE_MEDIA_INFO_ACCEPT, + NULL, NULL, TRUE); +} + +static void +finch_media_hangup_cb(PurpleMedia *media, GntWidget *widget) +{ + purple_media_stream_info(media, PURPLE_MEDIA_INFO_HANGUP, + NULL, NULL, TRUE); +} + +static void +finch_media_reject_cb(PurpleMedia *media, GntWidget *widget) +{ + purple_media_stream_info(media, PURPLE_MEDIA_INFO_REJECT, + NULL, NULL, TRUE); +} + +static void +finch_media_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) +{ + FinchMedia *media; + g_return_if_fail(FINCH_IS_MEDIA(object)); + + media = FINCH_MEDIA(object); + switch (prop_id) { + case PROP_MEDIA: + { + if (media->priv->media) + g_object_unref(media->priv->media); + media->priv->media = g_value_get_object(value); + g_object_ref(media->priv->media); + g_signal_connect_swapped(G_OBJECT(media->priv->accept), "activate", + G_CALLBACK(finch_media_accept_cb), media->priv->media); + g_signal_connect_swapped(G_OBJECT(media->priv->reject), "activate", + G_CALLBACK(finch_media_reject_cb), media->priv->media); + g_signal_connect_swapped(G_OBJECT(media->priv->hangup), "activate", + G_CALLBACK(finch_media_hangup_cb), media->priv->media); + + if (purple_media_is_initiator(media->priv->media, + NULL, NULL) == TRUE) { + finch_media_wait_cb(media->priv->media, media); + } + g_signal_connect(G_OBJECT(media->priv->media), "state-changed", + G_CALLBACK(finch_media_state_changed_cb), media); + g_signal_connect(G_OBJECT(media->priv->media), "stream-info", + G_CALLBACK(finch_media_stream_info_cb), media); + break; + } + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +finch_media_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) +{ + FinchMedia *media; + g_return_if_fail(FINCH_IS_MEDIA(object)); + + media = FINCH_MEDIA(object); + + switch (prop_id) { + case PROP_MEDIA: + g_value_set_object(value, media->priv->media); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static GntWidget * +finch_media_new(PurpleMedia *media) +{ + return GNT_WIDGET(g_object_new(finch_media_get_type(), + "media", media, + "vertical", FALSE, + "homogeneous", FALSE, + NULL)); +} + +static void +gntmedia_message_cb(FinchMedia *gntmedia, const char *msg, PurpleConversation *conv) +{ + if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM) { + purple_conv_im_write(PURPLE_CONV_IM(conv), NULL, msg, PURPLE_MESSAGE_SYSTEM, time(NULL)); + } +} + +static gboolean +finch_new_media(PurpleMediaManager *manager, PurpleMedia *media, + PurpleConnection *gc, gchar *name, gpointer null) +{ + GntWidget *gntmedia; + PurpleConversation *conv; + + conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, + purple_connection_get_account(gc), name); + + gntmedia = finch_media_new(media); + g_signal_connect(G_OBJECT(gntmedia), "message", G_CALLBACK(gntmedia_message_cb), conv); + FINCH_MEDIA(gntmedia)->priv->conv = conv; + finch_conversation_set_info_widget(conv, gntmedia); + return TRUE; +} + +static PurpleCmdRet +call_cmd_cb(PurpleConversation *conv, const char *cmd, char **args, + char **eror, gpointer data) +{ + PurpleAccount *account = purple_conversation_get_account(conv); + + if (!purple_prpl_initiate_media(account, + purple_conversation_get_name(conv), + PURPLE_MEDIA_AUDIO)) + return PURPLE_CMD_STATUS_FAILED; + + return PURPLE_CMD_STATUS_OK; +} + +static GstElement * +create_default_audio_src(PurpleMedia *media, + const gchar *session_id, const gchar *participant) +{ + GstElement *bin, *src, *volume; + GstPad *pad, *ghost; + double input_volume = purple_prefs_get_int( + "/finch/media/audio/volume/input")/10.0; + + src = gst_element_factory_make("gconfaudiosrc", NULL); + if (src == NULL) + src = gst_element_factory_make("autoaudiosrc", NULL); + if (src == NULL) + src = gst_element_factory_make("alsasrc", NULL); + if (src == NULL) + src = gst_element_factory_make("osssrc", NULL); + if (src == NULL) + src = gst_element_factory_make("dshowaudiosrc", NULL); + if (src == NULL) { + purple_debug_error("gntmedia", "Unable to find a suitable " + "element for the default audio source.\n"); + return NULL; + } + + bin = gst_bin_new("finchdefaultaudiosrc"); + volume = gst_element_factory_make("volume", "purpleaudioinputvolume"); + g_object_set(volume, "volume", input_volume, NULL); + gst_bin_add_many(GST_BIN(bin), src, volume, NULL); + gst_element_link(src, volume); + pad = gst_element_get_pad(volume, "src"); + ghost = gst_ghost_pad_new("ghostsrc", pad); + gst_element_add_pad(bin, ghost); + + return bin; +} + +static GstElement * +create_default_audio_sink(PurpleMedia *media, + const gchar *session_id, const gchar *participant) +{ + GstElement *bin, *sink, *volume, *queue; + GstPad *pad, *ghost; + double output_volume = purple_prefs_get_int( + "/finch/media/audio/volume/output")/10.0; + + sink = gst_element_factory_make("gconfaudiosink", NULL); + if (sink == NULL) + sink = gst_element_factory_make("autoaudiosink",NULL); + if (sink == NULL) { + purple_debug_error("gntmedia", "Unable to find a suitable " + "element for the default audio sink.\n"); + return NULL; + } + + bin = gst_bin_new("finchdefaultaudiosink"); + volume = gst_element_factory_make("volume", "purpleaudiooutputvolume"); + g_object_set(volume, "volume", output_volume, NULL); + queue = gst_element_factory_make("queue", NULL); + gst_bin_add_many(GST_BIN(bin), sink, volume, queue, NULL); + gst_element_link(volume, sink); + gst_element_link(queue, volume); + pad = gst_element_get_pad(queue, "sink"); + ghost = gst_ghost_pad_new("ghostsink", pad); + gst_element_add_pad(bin, ghost); + + return bin; +} +#endif /* USE_VV */ + +void finch_media_manager_init(void) +{ +#ifdef USE_VV + PurpleMediaManager *manager = purple_media_manager_get(); + PurpleMediaElementInfo *default_audio_src = + g_object_new(PURPLE_TYPE_MEDIA_ELEMENT_INFO, + "id", "finchdefaultaudiosrc", + "name", "Finch Default Audio Source", + "type", PURPLE_MEDIA_ELEMENT_AUDIO + | PURPLE_MEDIA_ELEMENT_SRC + | PURPLE_MEDIA_ELEMENT_ONE_SRC + | PURPLE_MEDIA_ELEMENT_UNIQUE, + "create-cb", create_default_audio_src, NULL); + PurpleMediaElementInfo *default_audio_sink = + g_object_new(PURPLE_TYPE_MEDIA_ELEMENT_INFO, + "id", "finchdefaultaudiosink", + "name", "Finch Default Audio Sink", + "type", PURPLE_MEDIA_ELEMENT_AUDIO + | PURPLE_MEDIA_ELEMENT_SINK + | PURPLE_MEDIA_ELEMENT_ONE_SINK, + "create-cb", create_default_audio_sink, NULL); + + g_signal_connect(G_OBJECT(manager), "init-media", G_CALLBACK(finch_new_media), NULL); + purple_cmd_register("call", "", PURPLE_CMD_P_DEFAULT, + PURPLE_CMD_FLAG_IM, NULL, + call_cmd_cb, _("call: Make an audio call."), NULL); + + purple_media_manager_set_ui_caps(manager, + PURPLE_MEDIA_CAPS_AUDIO | + PURPLE_MEDIA_CAPS_AUDIO_SINGLE_DIRECTION); + + purple_debug_info("gntmedia", "Registering media element types\n"); + purple_media_manager_set_active_element(manager, default_audio_src); + purple_media_manager_set_active_element(manager, default_audio_sink); + + purple_prefs_add_none("/finch/media"); + purple_prefs_add_none("/finch/media/audio"); + purple_prefs_add_none("/finch/media/audio/volume"); + purple_prefs_add_int("/finch/media/audio/volume/input", 10); + purple_prefs_add_int("/finch/media/audio/volume/output", 10); +#endif +} + +void finch_media_manager_uninit(void) +{ +#ifdef USE_VV + PurpleMediaManager *manager = purple_media_manager_get(); + g_signal_handlers_disconnect_by_func(G_OBJECT(manager), + G_CALLBACK(finch_new_media), NULL); +#endif +} + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/finch/gntmedia.h Fri Apr 03 16:45:38 2009 +0000 @@ -0,0 +1,42 @@ +/** + * @file gntmedia.h GNT Media API + * @ingroup finch + */ + +/* finch + * + * Finch is the legal property of its developers, whose names are too numerous + * to list here. Please refer to the COPYRIGHT file distributed with this + * source distribution. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA + */ + +#ifndef GNT_MEDIA_H +#define GNT_MEDIA_H + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +G_BEGIN_DECLS + +void finch_media_manager_init(void); +void finch_media_manager_uninit(void); + +G_END_DECLS + +#endif /* GNT_MEDIA_H */ +
--- a/finch/gntui.c Fri Apr 03 02:43:22 2009 +0000 +++ b/finch/gntui.c Fri Apr 03 16:45:38 2009 +0000 @@ -31,6 +31,7 @@ #include "gntdebug.h" #include "gntft.h" #include "gntlog.h" +#include "gntmedia.h" #include "gntnotify.h" #include "gntplugin.h" #include "gntpounce.h" @@ -91,6 +92,9 @@ finch_roomlist_init(); purple_roomlist_set_ui_ops(finch_roomlist_get_ui_ops()); + /* Media */ + finch_media_manager_init(); + gnt_register_action(_("Accounts"), finch_accounts_show_all); gnt_register_action(_("Buddy List"), finch_blist_show); gnt_register_action(_("Buddy Pounces"), finch_pounces_manager_show); @@ -136,6 +140,10 @@ finch_roomlist_uninit(); purple_roomlist_set_ui_ops(NULL); +#ifdef USE_VV + finch_media_manager_uninit(); +#endif + gnt_quit(); #endif }
--- a/finch/libgnt/gntkeys.h Fri Apr 03 02:43:22 2009 +0000 +++ b/finch/libgnt/gntkeys.h Fri Apr 03 16:45:38 2009 +0000 @@ -165,5 +165,6 @@ #undef lines #undef buttons #undef newline +#undef set_clock #endif
--- a/libpurple/Makefile.am Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/Makefile.am Fri Apr 03 16:45:38 2009 +0000 @@ -1,6 +1,7 @@ EXTRA_DIST = \ dbus-analyze-functions.py \ dbus-analyze-types.py \ + marshallers.list \ purple-notifications-example \ purple-remote \ purple-send \ @@ -51,6 +52,9 @@ idle.c \ imgstore.c \ log.c \ + marshallers.c \ + media.c \ + mediamanager.c \ mime.c \ nat-pmp.c \ network.c \ @@ -109,6 +113,10 @@ idle.h \ imgstore.h \ log.h \ + marshallers.h \ + media.h \ + media-gst.h \ + mediamanager.h \ mime.h \ nat-pmp.h \ network.h \ @@ -147,6 +155,15 @@ purple_builtheaders = purple.h version.h +marshallers.h: marshallers.list + @echo "Generating marshallers.h" + $(GLIB_GENMARSHAL) --prefix=purple_smarshal $(srcdir)/marshallers.list --header > marshallers.h + +marshallers.c: marshallers.list marshallers.h + @echo "Generating marshallers.c" + echo "#include \"marshallers.h\"" > marshallers.c + $(GLIB_GENMARSHAL) --prefix=purple_smarshal $(srcdir)/marshallers.list --body >> marshallers.c + if ENABLE_DBUS CLEANFILES = \ @@ -155,6 +172,8 @@ dbus-client-binding.h \ dbus-types.c \ dbus-types.h \ + marshallers.c \ + marshallers.h \ purple-client-bindings.c \ purple-client-bindings.h \ purple.service @@ -225,6 +244,8 @@ dbus-types.c \ dbus-types.h \ dbus-bindings.c \ + marshallers.c \ + marshallers.h \ purple-client-bindings.c \ purple-client-bindings.h @@ -258,6 +279,9 @@ $(LIBXML_LIBS) \ $(NETWORKMANAGER_LIBS) \ $(INTLLIBS) \ + $(FARSIGHT_LIBS) \ + $(GSTPROPS_LIBS) \ + $(GSTREAMER_LIBS) \ -lm AM_CPPFLAGS = \ @@ -270,6 +294,9 @@ $(DEBUG_CFLAGS) \ $(DBUS_CFLAGS) \ $(LIBXML_CFLAGS) \ + $(FARSIGHT_CFLAGS) \ + $(GSTPROPS_CFLAGS) \ + $(GSTREAMER_CFLAGS) \ $(NETWORKMANAGER_CFLAGS) # INSTALL_SSL_CERTIFICATES is true when SSL_CERTIFICATES_DIR is empty.
--- a/libpurple/internal.h Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/internal.h Fri Apr 03 16:45:38 2009 +0000 @@ -222,6 +222,41 @@ # endif #endif +#include <glib.h> +#include <glib-object.h> + +#ifndef G_DEFINE_TYPE +#define G_DEFINE_TYPE(TypeName, type_name, TYPE_PARENT) \ +\ +static void type_name##_init (TypeName *self); \ +static void type_name##_class_init (TypeName##Class *klass); \ +static gpointer type_name##_parent_class = NULL; \ +static void type_name##_class_intern_init (gpointer klass) \ +{ \ + type_name##_parent_class = g_type_class_peek_parent (klass); \ + type_name##_class_init ((TypeName##Class*) klass); \ +} \ +\ +GType \ +type_name##_get_type (void) \ +{ \ + static GType g_define_type_id = 0; \ + if (G_UNLIKELY (g_define_type_id == 0)) \ + { \ + g_define_type_id = \ + g_type_register_static_simple (TYPE_PARENT, \ + g_intern_static_string (#TypeName), \ + sizeof (TypeName##Class), \ + (GClassInitFunc)type_name##_class_intern_init, \ + sizeof (TypeName), \ + (GInstanceInitFunc)type_name##_init, \ + (GTypeFlags) 0); \ + } \ + return g_define_type_id; \ +} /* closes type_name##_get_type() */ + +#endif + /* Safer ways to work with static buffers. When using non-static * buffers, either use g_strdup_* functions (preferred) or use * g_strlcpy/g_strlcpy directly. */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libpurple/marshallers.list Fri Apr 03 16:45:38 2009 +0000 @@ -0,0 +1,5 @@ +VOID:POINTER,POINTER,OBJECT +BOOLEAN:OBJECT,POINTER,STRING +VOID:STRING,STRING +VOID:ENUM,STRING,STRING +VOID:ENUM,STRING,STRING,BOOLEAN
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libpurple/media-gst.h Fri Apr 03 16:45:38 2009 +0000 @@ -0,0 +1,161 @@ +/** + * @file media-gst.h Media API + * @ingroup core + */ + +/* purple + * + * Purple is the legal property of its developers, whose names are too numerous + * to list here. Please refer to the COPYRIGHT file distributed with this + * source distribution. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef __MEDIA_GST_H_ +#define __MEDIA_GST_H_ + +#include "media.h" +#include "mediamanager.h" + +#include <gst/gst.h> + +G_BEGIN_DECLS + +#define PURPLE_TYPE_MEDIA_ELEMENT_TYPE (purple_media_element_type_get_type()) +#define PURPLE_TYPE_MEDIA_ELEMENT_INFO (purple_media_element_info_get_type()) +#define PURPLE_MEDIA_ELEMENT_INFO(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), PURPLE_TYPE_MEDIA_ELEMENT_INFO, PurpleMediaElementInfo)) +#define PURPLE_MEDIA_ELEMENT_INFO_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), PURPLE_TYPE_MEDIA_ELEMENT_INFO, PurpleMediaElementInfo)) +#define PURPLE_IS_MEDIA_ELEMENT_INFO(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), PURPLE_TYPE_MEDIA_ELEMENT_INFO)) +#define PURPLE_IS_MEDIA_ELEMENT_INFO_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), PURPLE_TYPE_MEDIA_ELEMENT_INFO)) +#define PURPLE_MEDIA_ELEMENT_INFO_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), PURPLE_TYPE_MEDIA_ELEMENT_INFO, PurpleMediaElementInfo)) + +/** @copydoc _PurpleMediaElementInfo */ +typedef struct _PurpleMediaElementInfo PurpleMediaElementInfo; +typedef struct _PurpleMediaElementInfoClass PurpleMediaElementInfoClass; +typedef GstElement *(*PurpleMediaElementCreateCallback)(PurpleMedia *media, + const gchar *session_id, const gchar *participant); + +typedef enum { + PURPLE_MEDIA_ELEMENT_NONE = 0, /** empty element */ + PURPLE_MEDIA_ELEMENT_AUDIO = 1, /** supports audio */ + PURPLE_MEDIA_ELEMENT_VIDEO = 1 << 1, /** supports video */ + PURPLE_MEDIA_ELEMENT_AUDIO_VIDEO = PURPLE_MEDIA_ELEMENT_AUDIO + | PURPLE_MEDIA_ELEMENT_VIDEO, /** supports audio and video */ + + PURPLE_MEDIA_ELEMENT_NO_SRCS = 0, /** has no src pads */ + PURPLE_MEDIA_ELEMENT_ONE_SRC = 1 << 2, /** has one src pad */ + PURPLE_MEDIA_ELEMENT_MULTI_SRC = 1 << 3, /** has multiple src pads */ + PURPLE_MEDIA_ELEMENT_REQUEST_SRC = 1 << 4, /** src pads must be requested */ + + PURPLE_MEDIA_ELEMENT_NO_SINKS = 0, /** has no sink pads */ + PURPLE_MEDIA_ELEMENT_ONE_SINK = 1 << 5, /** has one sink pad */ + PURPLE_MEDIA_ELEMENT_MULTI_SINK = 1 << 6, /** has multiple sink pads */ + PURPLE_MEDIA_ELEMENT_REQUEST_SINK = 1 << 7, /** sink pads must be requested */ + + PURPLE_MEDIA_ELEMENT_UNIQUE = 1 << 8, /** This element is unique and + only one instance of it should + be created at a time */ + + PURPLE_MEDIA_ELEMENT_SRC = 1 << 9, /** can be set as an active src */ + PURPLE_MEDIA_ELEMENT_SINK = 1 << 10, /** can be set as an active sink */ +} PurpleMediaElementType; + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Gets the element type's GType. + * + * @return The element type's GType. + */ +GType purple_media_element_type_get_type(void); + +/** + * Gets the element info's GType. + * + * @return The element info's GType. + */ +GType purple_media_element_info_get_type(void); + +/** + * Gets the source from a session + * + * @param media The media object the session is in. + * @param sess_id The session id of the session to get the source from. + * + * @return The source retrieved. + */ +GstElement *purple_media_get_src(PurpleMedia *media, const gchar *sess_id); + +/** + * Gets the tee from a given session/stream. + * + * @param media The instance to get the tee from. + * @param session_id The id of the session to get the tee from. + * @param participant Optionally, the participant of the stream to get the tee from. + * + * @return The GstTee element from the chosen session/stream. + */ +GstElement *purple_media_get_tee(PurpleMedia *media, + const gchar *session_id, const gchar *participant); + + +/** + * Gets the pipeline from the media manager. + * + * @param manager The media manager to get the pipeline from. + * + * @return The pipeline. + */ +GstElement *purple_media_manager_get_pipeline(PurpleMediaManager *manager); + +/** + * Returns a GStreamer source or sink for audio or video. + * + * @param manager The media manager to use to obtain the source/sink. + * @param type The type of source/sink to get. + */ +GstElement *purple_media_manager_get_element(PurpleMediaManager *manager, + PurpleMediaSessionType type, PurpleMedia *media, + const gchar *session_id, const gchar *participant); + +PurpleMediaElementInfo *purple_media_manager_get_element_info( + PurpleMediaManager *manager, const gchar *name); +gboolean purple_media_manager_register_element(PurpleMediaManager *manager, + PurpleMediaElementInfo *info); +gboolean purple_media_manager_unregister_element(PurpleMediaManager *manager, + const gchar *name); +gboolean purple_media_manager_set_active_element(PurpleMediaManager *manager, + PurpleMediaElementInfo *info); +PurpleMediaElementInfo *purple_media_manager_get_active_element( + PurpleMediaManager *manager, PurpleMediaElementType type); + +gchar *purple_media_element_info_get_id(PurpleMediaElementInfo *info); +gchar *purple_media_element_info_get_name(PurpleMediaElementInfo *info); +PurpleMediaElementType purple_media_element_info_get_element_type( + PurpleMediaElementInfo *info); +GstElement *purple_media_element_info_call_create( + PurpleMediaElementInfo *info, PurpleMedia *media, + const gchar *session_id, const gchar *participant); + +#ifdef __cplusplus +} +#endif + +G_END_DECLS + +#endif /* __MEDIA_GST_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libpurple/media.c Fri Apr 03 16:45:38 2009 +0000 @@ -0,0 +1,3029 @@ +/** + * @file media.c Media API + * @ingroup core + */ + +/* purple + * + * Purple is the legal property of its developers, whose names are too numerous + * to list here. Please refer to the COPYRIGHT file distributed with this + * source distribution. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <string.h> + +#include "internal.h" + +#include "connection.h" +#include "marshallers.h" +#include "media.h" +#include "media-gst.h" +#include "mediamanager.h" +#include "network.h" + +#include "debug.h" + +#ifdef USE_VV + +#include <gst/farsight/fs-conference-iface.h> + +/** @copydoc _PurpleMediaSession */ +typedef struct _PurpleMediaSession PurpleMediaSession; +/** @copydoc _PurpleMediaStream */ +typedef struct _PurpleMediaStream PurpleMediaStream; +/** @copydoc _PurpleMediaClass */ +typedef struct _PurpleMediaClass PurpleMediaClass; +/** @copydoc _PurpleMediaPrivate */ +typedef struct _PurpleMediaPrivate PurpleMediaPrivate; +/** @copydoc _PurpleMediaCandidateClass */ +typedef struct _PurpleMediaCandidateClass PurpleMediaCandidateClass; +/** @copydoc _PurpleMediaCandidatePrivate */ +typedef struct _PurpleMediaCandidatePrivate PurpleMediaCandidatePrivate; +/** @copydoc _PurpleMediaCodecClass */ +typedef struct _PurpleMediaCodecClass PurpleMediaCodecClass; +/** @copydoc _PurpleMediaCodecPrivate */ +typedef struct _PurpleMediaCodecPrivate PurpleMediaCodecPrivate; + +/** The media class */ +struct _PurpleMediaClass +{ + GObjectClass parent_class; /**< The parent class. */ +}; + +/** The media class's private data */ +struct _PurpleMedia +{ + GObject parent; /**< The parent of this object. */ + PurpleMediaPrivate *priv; /**< The private data of this object. */ +}; + +struct _PurpleMediaSession +{ + gchar *id; + PurpleMedia *media; + GstElement *src; + GstElement *tee; + FsSession *session; + + PurpleMediaSessionType type; + gboolean initiator; +}; + +struct _PurpleMediaStream +{ + PurpleMediaSession *session; + gchar *participant; + FsStream *stream; + GstElement *src; + GstElement *tee; + + GList *local_candidates; + GList *remote_candidates; + + gboolean initiator; + gboolean accepted; + gboolean candidates_prepared; + + GList *active_local_candidates; + GList *active_remote_candidates; + + guint connected_cb_id; +}; +#endif + +struct _PurpleMediaPrivate +{ +#ifdef USE_VV + PurpleMediaManager *manager; + PurpleConnection *pc; + FsConference *conference; + gboolean initiator; + gpointer prpl_data; + + GHashTable *sessions; /* PurpleMediaSession table */ + GHashTable *participants; /* FsParticipant table */ + + GList *streams; /* PurpleMediaStream table */ + + GstElement *confbin; +#else + gpointer dummy; +#endif +}; + +#ifdef USE_VV +#define PURPLE_MEDIA_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), PURPLE_TYPE_MEDIA, PurpleMediaPrivate)) +#define PURPLE_MEDIA_CANDIDATE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), PURPLE_TYPE_MEDIA_CANDIDATE, PurpleMediaCandidatePrivate)) +#define PURPLE_MEDIA_CODEC_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), PURPLE_TYPE_MEDIA_CODEC, PurpleMediaCodecPrivate)) + +static void purple_media_class_init (PurpleMediaClass *klass); +static void purple_media_init (PurpleMedia *media); +static void purple_media_dispose (GObject *object); +static void purple_media_finalize (GObject *object); +static void purple_media_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec); +static void purple_media_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec); + +static void purple_media_new_local_candidate_cb(FsStream *stream, + FsCandidate *local_candidate, PurpleMediaSession *session); +static void purple_media_candidates_prepared_cb(FsStream *stream, + PurpleMediaSession *session); +static void purple_media_candidate_pair_established_cb(FsStream *stream, + FsCandidate *native_candidate, FsCandidate *remote_candidate, + PurpleMediaSession *session); +static gboolean media_bus_call(GstBus *bus, + GstMessage *msg, PurpleMedia *media); + +static GObjectClass *parent_class = NULL; + + + +enum { + ERROR, + ACCEPTED, + CANDIDATES_PREPARED, + CODECS_CHANGED, + NEW_CANDIDATE, + STATE_CHANGED, + STREAM_INFO, + LAST_SIGNAL +}; +static guint purple_media_signals[LAST_SIGNAL] = {0}; + +enum { + PROP_0, + PROP_MANAGER, + PROP_CONNECTION, + PROP_CONFERENCE, + PROP_INITIATOR, + PROP_PRPL_DATA, +}; +#endif + + +/* + * PurpleMediaElementType + */ + +GType +purple_media_session_type_get_type() +{ + static GType type = 0; + if (type == 0) { + static const GFlagsValue values[] = { + { PURPLE_MEDIA_NONE, + "PURPLE_MEDIA_NONE", "none" }, + { PURPLE_MEDIA_RECV_AUDIO, + "PURPLE_MEDIA_RECV_AUDIO", "recv-audio" }, + { PURPLE_MEDIA_SEND_AUDIO, + "PURPLE_MEDIA_SEND_AUDIO", "send-audio" }, + { PURPLE_MEDIA_RECV_VIDEO, + "PURPLE_MEDIA_RECV_VIDEO", "recv-video" }, + { PURPLE_MEDIA_SEND_VIDEO, + "PURPLE_MEDIA_SEND_VIDEO", "send-audio" }, + { PURPLE_MEDIA_AUDIO, + "PURPLE_MEDIA_AUDIO", "audio" }, + { PURPLE_MEDIA_VIDEO, + "PURPLE_MEDIA_VIDEO", "video" }, + { 0, NULL, NULL } + }; + type = g_flags_register_static( + "PurpleMediaSessionType", values); + } + return type; +} + +GType +purple_media_get_type() +{ +#ifdef USE_VV + static GType type = 0; + + if (type == 0) { + static const GTypeInfo info = { + sizeof(PurpleMediaClass), + NULL, + NULL, + (GClassInitFunc) purple_media_class_init, + NULL, + NULL, + sizeof(PurpleMedia), + 0, + (GInstanceInitFunc) purple_media_init, + NULL + }; + type = g_type_register_static(G_TYPE_OBJECT, "PurpleMedia", &info, 0); + } + return type; +#else + return G_TYPE_NONE; +#endif +} + +GType +purple_media_state_changed_get_type() +{ + static GType type = 0; + if (type == 0) { + static const GEnumValue values[] = { + { PURPLE_MEDIA_STATE_NEW, + "PURPLE_MEDIA_STATE_NEW", "new" }, + { PURPLE_MEDIA_STATE_CONNECTED, + "PURPLE_MEDIA_STATE_CONNECTED", "connected" }, + { PURPLE_MEDIA_STATE_END, + "PURPLE_MEDIA_STATE_END", "end" }, + { 0, NULL, NULL } + }; + type = g_enum_register_static("PurpleMediaState", values); + } + return type; +} + +GType +purple_media_info_type_get_type() +{ + static GType type = 0; + if (type == 0) { + static const GEnumValue values[] = { + { PURPLE_MEDIA_INFO_HANGUP, + "PURPLE_MEDIA_INFO_HANGUP", "hangup" }, + { PURPLE_MEDIA_INFO_ACCEPT, + "PURPLE_MEDIA_INFO_ACCEPT", "accept" }, + { PURPLE_MEDIA_INFO_REJECT, + "PURPLE_MEDIA_INFO_REJECT", "reject" }, + { PURPLE_MEDIA_INFO_MUTE, + "PURPLE_MEDIA_INFO_MUTE", "mute" }, + { PURPLE_MEDIA_INFO_UNMUTE, + "PURPLE_MEDIA_INFO_UNMUTE", "unmute" }, + { PURPLE_MEDIA_INFO_HOLD, + "PURPLE_MEDIA_INFO_HOLD", "hold" }, + { PURPLE_MEDIA_INFO_UNHOLD, + "PURPLE_MEDIA_INFO_HOLD", "unhold" }, + { 0, NULL, NULL } + }; + type = g_enum_register_static("PurpleMediaInfoType", values); + } + return type; +} + +#ifdef USE_VV +static void +purple_media_class_init (PurpleMediaClass *klass) +{ + GObjectClass *gobject_class = (GObjectClass*)klass; + parent_class = g_type_class_peek_parent(klass); + + gobject_class->dispose = purple_media_dispose; + gobject_class->finalize = purple_media_finalize; + gobject_class->set_property = purple_media_set_property; + gobject_class->get_property = purple_media_get_property; + + g_object_class_install_property(gobject_class, PROP_MANAGER, + g_param_spec_object("manager", + "Purple Media Manager", + "The media manager that contains this media session.", + PURPLE_TYPE_MEDIA_MANAGER, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_CONNECTION, + g_param_spec_pointer("connection", + "PurpleConnection", + "The connection this media session is on.", + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_CONFERENCE, + g_param_spec_object("conference", + "Farsight conference", + "The FsConference associated with this media.", + FS_TYPE_CONFERENCE, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE)); + + g_object_class_install_property(gobject_class, PROP_INITIATOR, + g_param_spec_boolean("initiator", + "initiator", + "If the local user initiated the conference.", + FALSE, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_PRPL_DATA, + g_param_spec_pointer("prpl-data", + "gpointer", + "Data the prpl plugin set on the media session.", + G_PARAM_READWRITE)); + + purple_media_signals[ERROR] = g_signal_new("error", G_TYPE_FROM_CLASS(klass), + G_SIGNAL_RUN_LAST, 0, NULL, NULL, + g_cclosure_marshal_VOID__STRING, + G_TYPE_NONE, 1, G_TYPE_STRING); + purple_media_signals[ACCEPTED] = g_signal_new("accepted", G_TYPE_FROM_CLASS(klass), + G_SIGNAL_RUN_LAST, 0, NULL, NULL, + purple_smarshal_VOID__STRING_STRING, + G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_STRING); + purple_media_signals[CANDIDATES_PREPARED] = g_signal_new("candidates-prepared", G_TYPE_FROM_CLASS(klass), + G_SIGNAL_RUN_LAST, 0, NULL, NULL, + purple_smarshal_VOID__STRING_STRING, + G_TYPE_NONE, 2, G_TYPE_STRING, + G_TYPE_STRING); + purple_media_signals[CODECS_CHANGED] = g_signal_new("codecs-changed", G_TYPE_FROM_CLASS(klass), + G_SIGNAL_RUN_LAST, 0, NULL, NULL, + g_cclosure_marshal_VOID__STRING, + G_TYPE_NONE, 1, G_TYPE_STRING); + purple_media_signals[NEW_CANDIDATE] = g_signal_new("new-candidate", G_TYPE_FROM_CLASS(klass), + G_SIGNAL_RUN_LAST, 0, NULL, NULL, + purple_smarshal_VOID__POINTER_POINTER_OBJECT, + G_TYPE_NONE, 3, G_TYPE_POINTER, + G_TYPE_POINTER, PURPLE_TYPE_MEDIA_CANDIDATE); + purple_media_signals[STATE_CHANGED] = g_signal_new("state-changed", G_TYPE_FROM_CLASS(klass), + G_SIGNAL_RUN_LAST, 0, NULL, NULL, + purple_smarshal_VOID__ENUM_STRING_STRING, + G_TYPE_NONE, 3, PURPLE_MEDIA_TYPE_STATE, + G_TYPE_STRING, G_TYPE_STRING); + purple_media_signals[STREAM_INFO] = g_signal_new("stream-info", G_TYPE_FROM_CLASS(klass), + G_SIGNAL_RUN_LAST, 0, NULL, NULL, + purple_smarshal_VOID__ENUM_STRING_STRING_BOOLEAN, + G_TYPE_NONE, 4, PURPLE_MEDIA_TYPE_INFO_TYPE, + G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN); + g_type_class_add_private(klass, sizeof(PurpleMediaPrivate)); +} + + +static void +purple_media_init (PurpleMedia *media) +{ + media->priv = PURPLE_MEDIA_GET_PRIVATE(media); + memset(media->priv, 0, sizeof(*media->priv)); +} + +static void +purple_media_stream_free(PurpleMediaStream *stream) +{ + if (stream == NULL) + return; + + /* Remove the connected_cb timeout */ + if (stream->connected_cb_id != 0) + purple_timeout_remove(stream->connected_cb_id); + + g_free(stream->participant); + + if (stream->local_candidates) + fs_candidate_list_destroy(stream->local_candidates); + if (stream->remote_candidates) + fs_candidate_list_destroy(stream->remote_candidates); + + if (stream->active_local_candidates) + fs_candidate_list_destroy(stream->active_local_candidates); + if (stream->active_remote_candidates) + fs_candidate_list_destroy(stream->active_remote_candidates); + + g_free(stream); +} + +static void +purple_media_session_free(PurpleMediaSession *session) +{ + if (session == NULL) + return; + + g_free(session->id); + g_free(session); +} + +static void +purple_media_dispose(GObject *media) +{ + PurpleMediaPrivate *priv = PURPLE_MEDIA_GET_PRIVATE(media); + GList *iter = NULL; + + purple_debug_info("media","purple_media_dispose\n"); + + purple_media_manager_remove_media(priv->manager, PURPLE_MEDIA(media)); + + if (priv->confbin) { + gst_element_set_locked_state(priv->confbin, TRUE); + gst_element_set_state(GST_ELEMENT(priv->confbin), + GST_STATE_NULL); + gst_bin_remove(GST_BIN(purple_media_manager_get_pipeline( + priv->manager)), priv->confbin); + priv->confbin = NULL; + priv->conference = NULL; + } + + for (iter = priv->streams; iter; iter = g_list_next(iter)) { + PurpleMediaStream *stream = iter->data; + if (stream->stream) { + g_object_unref(stream->stream); + stream->stream = NULL; + } + } + + if (priv->sessions) { + GList *sessions = g_hash_table_get_values(priv->sessions); + for (; sessions; sessions = g_list_delete_link(sessions, sessions)) { + PurpleMediaSession *session = sessions->data; + if (session->session) { + g_object_unref(session->session); + session->session = NULL; + } + } + } + + if (priv->participants) { + GList *participants = g_hash_table_get_values(priv->participants); + for (; participants; participants = g_list_delete_link(participants, participants)) + g_object_unref(participants->data); + } + + if (priv->manager) { + GstElement *pipeline = purple_media_manager_get_pipeline( + priv->manager); + GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline)); + g_signal_handlers_disconnect_matched(G_OBJECT(bus), + G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA, + 0, 0, 0, media_bus_call, media); + gst_object_unref(bus); + + g_object_unref(priv->manager); + priv->manager = NULL; + } + + G_OBJECT_CLASS(parent_class)->dispose(media); +} + +static void +purple_media_finalize(GObject *media) +{ + PurpleMediaPrivate *priv = PURPLE_MEDIA_GET_PRIVATE(media); + purple_debug_info("media","purple_media_finalize\n"); + + for (; priv->streams; priv->streams = g_list_delete_link(priv->streams, priv->streams)) + purple_media_stream_free(priv->streams->data); + + if (priv->sessions) { + GList *sessions = g_hash_table_get_values(priv->sessions); + for (; sessions; sessions = g_list_delete_link(sessions, sessions)) { + purple_media_session_free(sessions->data); + } + g_hash_table_destroy(priv->sessions); + } + + G_OBJECT_CLASS(parent_class)->finalize(media); +} + +static void +purple_media_setup_pipeline(PurpleMedia *media) +{ + GstBus *bus; + gchar *name; + GstElement *pipeline; + + if (media->priv->conference == NULL || media->priv->manager == NULL) + return; + + pipeline = purple_media_manager_get_pipeline(media->priv->manager); + + name = g_strdup_printf("conf_%p", + media->priv->conference); + media->priv->confbin = gst_bin_new(name); + g_free(name); + + bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline)); + g_signal_connect(G_OBJECT(bus), "message", + G_CALLBACK(media_bus_call), media); + gst_object_unref(bus); + + gst_bin_add(GST_BIN(pipeline), + media->priv->confbin); + gst_bin_add(GST_BIN(media->priv->confbin), + GST_ELEMENT(media->priv->conference)); + gst_element_set_state(GST_ELEMENT(media->priv->confbin), + GST_STATE_PLAYING); +} + +static void +purple_media_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) +{ + PurpleMedia *media; + g_return_if_fail(PURPLE_IS_MEDIA(object)); + + media = PURPLE_MEDIA(object); + + switch (prop_id) { + case PROP_MANAGER: + media->priv->manager = g_value_get_object(value); + g_object_ref(media->priv->manager); + + purple_media_setup_pipeline(media); + break; + case PROP_CONNECTION: + media->priv->pc = g_value_get_pointer(value); + break; + case PROP_CONFERENCE: { + if (media->priv->conference) + gst_object_unref(media->priv->conference); + media->priv->conference = g_value_get_object(value); + gst_object_ref(media->priv->conference); + + purple_media_setup_pipeline(media); + break; + } + case PROP_INITIATOR: + media->priv->initiator = g_value_get_boolean(value); + break; + case PROP_PRPL_DATA: + media->priv->prpl_data = g_value_get_pointer(value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +purple_media_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) +{ + PurpleMedia *media; + g_return_if_fail(PURPLE_IS_MEDIA(object)); + + media = PURPLE_MEDIA(object); + + switch (prop_id) { + case PROP_MANAGER: + g_value_set_object(value, media->priv->manager); + break; + case PROP_CONNECTION: + g_value_set_pointer(value, media->priv->pc); + break; + case PROP_CONFERENCE: + g_value_set_object(value, media->priv->conference); + break; + case PROP_INITIATOR: + g_value_set_boolean(value, media->priv->initiator); + break; + case PROP_PRPL_DATA: + g_value_set_pointer(value, media->priv->prpl_data); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } + +} +#endif + +/* + * PurpleMediaCandidateType + */ + +GType +purple_media_candidate_type_get_type() +{ + static GType type = 0; + if (type == 0) { + static const GEnumValue values[] = { + { PURPLE_MEDIA_CANDIDATE_TYPE_HOST, + "PURPLE_MEDIA_CANDIDATE_TYPE_HOST", + "host" }, + { PURPLE_MEDIA_CANDIDATE_TYPE_SRFLX, + "PURPLE_MEDIA_CANDIDATE_TYPE_SRFLX", + "srflx" }, + { PURPLE_MEDIA_CANDIDATE_TYPE_PRFLX, + "PURPLE_MEDIA_CANDIDATE_TYPE_PRFLX", + "prflx" }, + { PURPLE_MEDIA_CANDIDATE_TYPE_RELAY, + "PPURPLE_MEDIA_CANDIDATE_TYPE_RELAY", + "relay" }, + { PURPLE_MEDIA_CANDIDATE_TYPE_MULTICAST, + "PURPLE_MEDIA_CANDIDATE_TYPE_MULTICAST", + "multicast" }, + { 0, NULL, NULL } + }; + type = g_enum_register_static("PurpleMediaCandidateType", + values); + } + return type; +} + +/* + * PurpleMediaNetworkProtocol + */ + +GType +purple_media_network_protocol_get_type() +{ + static GType type = 0; + if (type == 0) { + static const GEnumValue values[] = { + { PURPLE_MEDIA_NETWORK_PROTOCOL_UDP, + "PURPLE_MEDIA_NETWORK_PROTOCOL_UDP", + "udp" }, + { PURPLE_MEDIA_NETWORK_PROTOCOL_TCP, + "PURPLE_MEDIA_NETWORK_PROTOCOL_TCP", + "tcp" }, + { 0, NULL, NULL } + }; + type = g_enum_register_static("PurpleMediaNetworkProtocol", + values); + } + return type; +} + +/* + * PurpleMediaCandidate + */ + +struct _PurpleMediaCandidateClass +{ + GObjectClass parent_class; +}; + +struct _PurpleMediaCandidate +{ + GObject parent; +}; + +struct _PurpleMediaCandidatePrivate +{ + gchar *foundation; + guint component_id; + gchar *ip; + guint16 port; + gchar *base_ip; + guint16 base_port; + PurpleMediaNetworkProtocol proto; + guint32 priority; + PurpleMediaCandidateType type; + gchar *username; + gchar *password; + guint ttl; +}; + +enum { + PROP_CANDIDATE_0, + PROP_FOUNDATION, + PROP_COMPONENT_ID, + PROP_IP, + PROP_PORT, + PROP_BASE_IP, + PROP_BASE_PORT, + PROP_PROTOCOL, + PROP_PRIORITY, + PROP_TYPE, + PROP_USERNAME, + PROP_PASSWORD, + PROP_TTL, +}; + +static void +purple_media_candidate_init(PurpleMediaCandidate *info) +{ + PurpleMediaCandidatePrivate *priv = + PURPLE_MEDIA_CANDIDATE_GET_PRIVATE(info); + priv->foundation = NULL; + priv->component_id = 0; + priv->ip = NULL; + priv->port = 0; + priv->base_ip = NULL; + priv->proto = PURPLE_MEDIA_NETWORK_PROTOCOL_UDP; + priv->priority = 0; + priv->type = PURPLE_MEDIA_CANDIDATE_TYPE_HOST; + priv->username = NULL; + priv->password = NULL; + priv->ttl = 0; +} + +static void +purple_media_candidate_finalize(GObject *info) +{ + PurpleMediaCandidatePrivate *priv = + PURPLE_MEDIA_CANDIDATE_GET_PRIVATE(info); + + g_free(priv->foundation); + g_free(priv->ip); + g_free(priv->base_ip); + g_free(priv->username); + g_free(priv->password); +} + +static void +purple_media_candidate_set_property (GObject *object, guint prop_id, + const GValue *value, GParamSpec *pspec) +{ + PurpleMediaCandidatePrivate *priv; + g_return_if_fail(PURPLE_IS_MEDIA_CANDIDATE(object)); + + priv = PURPLE_MEDIA_CANDIDATE_GET_PRIVATE(object); + + switch (prop_id) { + case PROP_FOUNDATION: + g_free(priv->foundation); + priv->foundation = g_value_dup_string(value); + break; + case PROP_COMPONENT_ID: + priv->component_id = g_value_get_uint(value); + break; + case PROP_IP: + g_free(priv->ip); + priv->ip = g_value_dup_string(value); + break; + case PROP_PORT: + priv->port = g_value_get_uint(value); + break; + case PROP_BASE_IP: + g_free(priv->base_ip); + priv->base_ip = g_value_dup_string(value); + break; + case PROP_BASE_PORT: + priv->base_port = g_value_get_uint(value); + break; + case PROP_PROTOCOL: + priv->proto = g_value_get_enum(value); + break; + case PROP_PRIORITY: + priv->priority = g_value_get_uint(value); + break; + case PROP_TYPE: + priv->type = g_value_get_enum(value); + break; + case PROP_USERNAME: + g_free(priv->username); + priv->username = g_value_dup_string(value); + break; + case PROP_PASSWORD: + g_free(priv->password); + priv->password = g_value_dup_string(value); + break; + case PROP_TTL: + priv->ttl = g_value_get_uint(value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID( + object, prop_id, pspec); + break; + } +} + +static void +purple_media_candidate_get_property (GObject *object, guint prop_id, + GValue *value, GParamSpec *pspec) +{ + PurpleMediaCandidatePrivate *priv; + g_return_if_fail(PURPLE_IS_MEDIA_CANDIDATE(object)); + + priv = PURPLE_MEDIA_CANDIDATE_GET_PRIVATE(object); + + switch (prop_id) { + case PROP_FOUNDATION: + g_value_set_string(value, priv->foundation); + break; + case PROP_COMPONENT_ID: + g_value_set_uint(value, priv->component_id); + break; + case PROP_IP: + g_value_set_string(value, priv->ip); + break; + case PROP_PORT: + g_value_set_uint(value, priv->port); + break; + case PROP_BASE_IP: + g_value_set_string(value, priv->base_ip); + break; + case PROP_BASE_PORT: + g_value_set_uint(value, priv->base_port); + break; + case PROP_PROTOCOL: + g_value_set_enum(value, priv->proto); + break; + case PROP_PRIORITY: + g_value_set_uint(value, priv->priority); + break; + case PROP_TYPE: + g_value_set_enum(value, priv->type); + break; + case PROP_USERNAME: + g_value_set_string(value, priv->username); + break; + case PROP_PASSWORD: + g_value_set_string(value, priv->password); + break; + case PROP_TTL: + g_value_set_uint(value, priv->ttl); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID( + object, prop_id, pspec); + break; + } +} + +static void +purple_media_candidate_class_init(PurpleMediaCandidateClass *klass) +{ + GObjectClass *gobject_class = (GObjectClass*)klass; + + gobject_class->finalize = purple_media_candidate_finalize; + gobject_class->set_property = purple_media_candidate_set_property; + gobject_class->get_property = purple_media_candidate_get_property; + + g_object_class_install_property(gobject_class, PROP_FOUNDATION, + g_param_spec_string("foundation", + "Foundation", + "The foundation of the candidate.", + NULL, + G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_COMPONENT_ID, + g_param_spec_uint("component-id", + "Component ID", + "The component id of the candidate.", + 0, G_MAXUINT, 0, + G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_IP, + g_param_spec_string("ip", + "IP Address", + "The IP address of the candidate.", + NULL, + G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_PORT, + g_param_spec_uint("port", + "Port", + "The port of the candidate.", + 0, G_MAXUINT16, 0, + G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_BASE_IP, + g_param_spec_string("base-ip", + "Base IP", + "The internal IP address of the candidate.", + NULL, + G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_BASE_PORT, + g_param_spec_uint("base-port", + "Base Port", + "The internal port of the candidate.", + 0, G_MAXUINT16, 0, + G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_PROTOCOL, + g_param_spec_enum("protocol", + "Protocol", + "The protocol of the candidate.", + PURPLE_TYPE_MEDIA_NETWORK_PROTOCOL, + PURPLE_MEDIA_NETWORK_PROTOCOL_UDP, + G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_PRIORITY, + g_param_spec_uint("priority", + "Priority", + "The priority of the candidate.", + 0, G_MAXUINT32, 0, + G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_TYPE, + g_param_spec_enum("type", + "Type", + "The type of the candidate.", + PURPLE_TYPE_MEDIA_CANDIDATE_TYPE, + PURPLE_MEDIA_CANDIDATE_TYPE_HOST, + G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_USERNAME, + g_param_spec_string("username", + "Username", + "The username used to connect to the candidate.", + NULL, + G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_PASSWORD, + g_param_spec_string("password", + "Password", + "The password use to connect to the candidate.", + NULL, + G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_TTL, + g_param_spec_uint("ttl", + "TTL", + "The TTL of the candidate.", + 0, G_MAXUINT, 0, + G_PARAM_READWRITE)); + + g_type_class_add_private(klass, sizeof(PurpleMediaCandidatePrivate)); +} + +G_DEFINE_TYPE(PurpleMediaCandidate, + purple_media_candidate, G_TYPE_OBJECT); + +PurpleMediaCandidate * +purple_media_candidate_new(const gchar *foundation, guint component_id, + PurpleMediaCandidateType type, + PurpleMediaNetworkProtocol proto, + const gchar *ip, guint port) +{ + return g_object_new(PURPLE_TYPE_MEDIA_CANDIDATE, + "foundation", foundation, + "component-id", component_id, + "type", type, + "protocol", proto, + "ip", ip, + "port", port, NULL); +} + +static PurpleMediaCandidate * +purple_media_candidate_copy(PurpleMediaCandidate *candidate) +{ + PurpleMediaCandidatePrivate *priv; + PurpleMediaCandidate *new_candidate; + + if (candidate == NULL) + return NULL; + + priv = PURPLE_MEDIA_CANDIDATE_GET_PRIVATE(candidate); + + new_candidate = purple_media_candidate_new(priv->foundation, + priv->component_id, priv->type, priv->proto, + priv->ip, priv->port); + g_object_set(new_candidate, + "base-ip", priv->base_ip, + "base-port", priv->base_port, + "priority", priv->priority, + "username", priv->username, + "password", priv->password, + "ttl", priv->ttl, NULL); + return new_candidate; +} + +#ifdef USE_VV +static FsCandidate * +purple_media_candidate_to_fs(PurpleMediaCandidate *candidate) +{ + PurpleMediaCandidatePrivate *priv; + FsCandidate *fscandidate; + + if (candidate == NULL) + return NULL; + + priv = PURPLE_MEDIA_CANDIDATE_GET_PRIVATE(candidate); + + fscandidate = fs_candidate_new(priv->foundation, + priv->component_id, priv->type, + priv->proto, priv->ip, priv->port); + + fscandidate->base_ip = g_strdup(priv->base_ip); + fscandidate->base_port = priv->base_port; + fscandidate->priority = priv->priority; + fscandidate->username = g_strdup(priv->username); + fscandidate->password = g_strdup(priv->password); + fscandidate->ttl = priv->ttl; + return fscandidate; +} + +static PurpleMediaCandidate * +purple_media_candidate_from_fs(FsCandidate *fscandidate) +{ + PurpleMediaCandidate *candidate; + + if (fscandidate == NULL) + return NULL; + + candidate = purple_media_candidate_new(fscandidate->foundation, + fscandidate->component_id, fscandidate->type, + fscandidate->proto, fscandidate->ip, fscandidate->port); + g_object_set(candidate, + "base-ip", fscandidate->base_ip, + "base-port", fscandidate->base_port, + "priority", fscandidate->priority, + "username", fscandidate->username, + "password", fscandidate->password, + "ttl", fscandidate->ttl, NULL); + return candidate; +} + +static GList * +purple_media_candidate_list_from_fs(GList *candidates) +{ + GList *new_list = NULL; + + for (; candidates; candidates = g_list_next(candidates)) { + new_list = g_list_prepend(new_list, + purple_media_candidate_from_fs( + candidates->data)); + } + + new_list = g_list_reverse(new_list); + return new_list; +} + +static GList * +purple_media_candidate_list_to_fs(GList *candidates) +{ + GList *new_list = NULL; + + for (; candidates; candidates = g_list_next(candidates)) { + new_list = g_list_prepend(new_list, + purple_media_candidate_to_fs( + candidates->data)); + } + + new_list = g_list_reverse(new_list); + return new_list; +} +#endif + +GList * +purple_media_candidate_list_copy(GList *candidates) +{ + GList *new_list = NULL; + + for (; candidates; candidates = g_list_next(candidates)) { + new_list = g_list_prepend(new_list, + purple_media_candidate_copy(candidates->data)); + } + + new_list = g_list_reverse(new_list); + return new_list; +} + +void +purple_media_candidate_list_free(GList *candidates) +{ + for (; candidates; candidates = + g_list_delete_link(candidates, candidates)) { + g_object_unref(candidates->data); + } +} + +gchar * +purple_media_candidate_get_foundation(PurpleMediaCandidate *candidate) +{ + gchar *foundation; + g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), NULL); + g_object_get(candidate, "foundation", &foundation, NULL); + return foundation; +} + +guint +purple_media_candidate_get_component_id(PurpleMediaCandidate *candidate) +{ + guint component_id; + g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), 0); + g_object_get(candidate, "component-id", &component_id, NULL); + return component_id; +} + +gchar * +purple_media_candidate_get_ip(PurpleMediaCandidate *candidate) +{ + gchar *ip; + g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), NULL); + g_object_get(candidate, "ip", &ip, NULL); + return ip; +} + +guint16 +purple_media_candidate_get_port(PurpleMediaCandidate *candidate) +{ + guint port; + g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), 0); + g_object_get(candidate, "port", &port, NULL); + return port; +} + +gchar * +purple_media_candidate_get_base_ip(PurpleMediaCandidate *candidate) +{ + gchar *base_ip; + g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), NULL); + g_object_get(candidate, "base-ip", &base_ip, NULL); + return base_ip; +} + +guint16 +purple_media_candidate_get_base_port(PurpleMediaCandidate *candidate) +{ + guint base_port; + g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), 0); + g_object_get(candidate, "base_port", &base_port, NULL); + return base_port; +} + +PurpleMediaNetworkProtocol +purple_media_candidate_get_protocol(PurpleMediaCandidate *candidate) +{ + PurpleMediaNetworkProtocol protocol; + g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), + PURPLE_MEDIA_NETWORK_PROTOCOL_UDP); + g_object_get(candidate, "protocol", &protocol, NULL); + return protocol; +} + +guint32 +purple_media_candidate_get_priority(PurpleMediaCandidate *candidate) +{ + guint priority; + g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), 0); + g_object_get(candidate, "priority", &priority, NULL); + return priority; +} + +PurpleMediaCandidateType +purple_media_candidate_get_candidate_type(PurpleMediaCandidate *candidate) +{ + PurpleMediaCandidateType type; + g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), + PURPLE_MEDIA_CANDIDATE_TYPE_HOST); + g_object_get(candidate, "type", &type, NULL); + return type; +} + +gchar * +purple_media_candidate_get_username(PurpleMediaCandidate *candidate) +{ + gchar *username; + g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), NULL); + g_object_get(candidate, "username", &username, NULL); + return username; +} + +gchar * +purple_media_candidate_get_password(PurpleMediaCandidate *candidate) +{ + gchar *password; + g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), NULL); + g_object_get(candidate, "password", &password, NULL); + return password; +} + +guint +purple_media_candidate_get_ttl(PurpleMediaCandidate *candidate) +{ + guint ttl; + g_return_val_if_fail(PURPLE_IS_MEDIA_CANDIDATE(candidate), 0); + g_object_get(candidate, "ttl", &ttl, NULL); + return ttl; +} + +#ifdef USE_VV +static FsMediaType +purple_media_to_fs_media_type(PurpleMediaSessionType type) +{ + if (type & PURPLE_MEDIA_AUDIO) + return FS_MEDIA_TYPE_AUDIO; + else if (type & PURPLE_MEDIA_VIDEO) + return FS_MEDIA_TYPE_VIDEO; + else + return 0; +} + +static FsStreamDirection +purple_media_to_fs_stream_direction(PurpleMediaSessionType type) +{ + if ((type & PURPLE_MEDIA_AUDIO) == PURPLE_MEDIA_AUDIO || + (type & PURPLE_MEDIA_VIDEO) == PURPLE_MEDIA_VIDEO) + return FS_DIRECTION_BOTH; + else if ((type & PURPLE_MEDIA_SEND_AUDIO) || + (type & PURPLE_MEDIA_SEND_VIDEO)) + return FS_DIRECTION_SEND; + else if ((type & PURPLE_MEDIA_RECV_AUDIO) || + (type & PURPLE_MEDIA_RECV_VIDEO)) + return FS_DIRECTION_RECV; + else + return FS_DIRECTION_NONE; +} + +static PurpleMediaSessionType +purple_media_from_fs(FsMediaType type, FsStreamDirection direction) +{ + PurpleMediaSessionType result = PURPLE_MEDIA_NONE; + if (type == FS_MEDIA_TYPE_AUDIO) { + if (direction & FS_DIRECTION_SEND) + result |= PURPLE_MEDIA_SEND_AUDIO; + if (direction & FS_DIRECTION_RECV) + result |= PURPLE_MEDIA_RECV_AUDIO; + } else if (type == FS_MEDIA_TYPE_VIDEO) { + if (direction & FS_DIRECTION_SEND) + result |= PURPLE_MEDIA_SEND_VIDEO; + if (direction & FS_DIRECTION_RECV) + result |= PURPLE_MEDIA_RECV_VIDEO; + } + return result; +} +#endif + +/* + * PurpleMediaCodec + */ + +struct _PurpleMediaCodecClass +{ + GObjectClass parent_class; +}; + +struct _PurpleMediaCodec +{ + GObject parent; +}; + +struct _PurpleMediaCodecPrivate +{ + gint id; + char *encoding_name; + PurpleMediaSessionType media_type; + guint clock_rate; + guint channels; + GList *optional_params; +}; + +enum { + PROP_CODEC_0, + PROP_ID, + PROP_ENCODING_NAME, + PROP_MEDIA_TYPE, + PROP_CLOCK_RATE, + PROP_CHANNELS, + PROP_OPTIONAL_PARAMS, +}; + +static void +purple_media_codec_init(PurpleMediaCodec *info) +{ + PurpleMediaCodecPrivate *priv = + PURPLE_MEDIA_CODEC_GET_PRIVATE(info); + priv->encoding_name = NULL; + priv->optional_params = NULL; +} + +static void +purple_media_codec_finalize(GObject *info) +{ + PurpleMediaCodecPrivate *priv = + PURPLE_MEDIA_CODEC_GET_PRIVATE(info); + g_free(priv->encoding_name); + for (; priv->optional_params; priv->optional_params = + g_list_delete_link(priv->optional_params, + priv->optional_params)) { + g_free(priv->optional_params->data); + } +} + +static void +purple_media_codec_set_property (GObject *object, guint prop_id, + const GValue *value, GParamSpec *pspec) +{ + PurpleMediaCodecPrivate *priv; + g_return_if_fail(PURPLE_IS_MEDIA_CODEC(object)); + + priv = PURPLE_MEDIA_CODEC_GET_PRIVATE(object); + + switch (prop_id) { + case PROP_ID: + priv->id = g_value_get_uint(value); + break; + case PROP_ENCODING_NAME: + g_free(priv->encoding_name); + priv->encoding_name = g_value_dup_string(value); + break; + case PROP_MEDIA_TYPE: + priv->media_type = g_value_get_flags(value); + break; + case PROP_CLOCK_RATE: + priv->clock_rate = g_value_get_uint(value); + break; + case PROP_CHANNELS: + priv->channels = g_value_get_uint(value); + break; + case PROP_OPTIONAL_PARAMS: + priv->optional_params = g_value_get_pointer(value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID( + object, prop_id, pspec); + break; + } +} + +static void +purple_media_codec_get_property (GObject *object, guint prop_id, + GValue *value, GParamSpec *pspec) +{ + PurpleMediaCodecPrivate *priv; + g_return_if_fail(PURPLE_IS_MEDIA_CODEC(object)); + + priv = PURPLE_MEDIA_CODEC_GET_PRIVATE(object); + + switch (prop_id) { + case PROP_ID: + g_value_set_uint(value, priv->id); + break; + case PROP_ENCODING_NAME: + g_value_set_string(value, priv->encoding_name); + break; + case PROP_MEDIA_TYPE: + g_value_set_flags(value, priv->media_type); + break; + case PROP_CLOCK_RATE: + g_value_set_uint(value, priv->clock_rate); + break; + case PROP_CHANNELS: + g_value_set_uint(value, priv->channels); + break; + case PROP_OPTIONAL_PARAMS: + g_value_set_pointer(value, priv->optional_params); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID( + object, prop_id, pspec); + break; + } +} + +static void +purple_media_codec_class_init(PurpleMediaCodecClass *klass) +{ + GObjectClass *gobject_class = (GObjectClass*)klass; + + gobject_class->finalize = purple_media_codec_finalize; + gobject_class->set_property = purple_media_codec_set_property; + gobject_class->get_property = purple_media_codec_get_property; + + g_object_class_install_property(gobject_class, PROP_ID, + g_param_spec_uint("id", + "ID", + "The numeric identifier of the codec.", + 0, G_MAXUINT, 0, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_ENCODING_NAME, + g_param_spec_string("encoding-name", + "Encoding Name", + "The name of the codec.", + NULL, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_MEDIA_TYPE, + g_param_spec_flags("media-type", + "Media Type", + "Whether this is an audio of video codec.", + PURPLE_TYPE_MEDIA_SESSION_TYPE, + PURPLE_MEDIA_NONE, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_CLOCK_RATE, + g_param_spec_uint("clock-rate", + "Create Callback", + "The function called to create this element.", + 0, G_MAXUINT, 0, + G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_CHANNELS, + g_param_spec_uint("channels", + "Channels", + "The number of channels in this codec.", + 0, G_MAXUINT, 0, + G_PARAM_READWRITE)); + g_object_class_install_property(gobject_class, PROP_OPTIONAL_PARAMS, + g_param_spec_pointer("optional-params", + "Optional Params", + "A list of optional parameters for the codec.", + G_PARAM_READWRITE)); + + g_type_class_add_private(klass, sizeof(PurpleMediaCodecPrivate)); +} + +G_DEFINE_TYPE(PurpleMediaCodec, + purple_media_codec, G_TYPE_OBJECT); + +guint +purple_media_codec_get_id(PurpleMediaCodec *codec) +{ + guint id; + g_return_val_if_fail(PURPLE_IS_MEDIA_CODEC(codec), 0); + g_object_get(codec, "id", &id, NULL); + return id; +} + +gchar * +purple_media_codec_get_encoding_name(PurpleMediaCodec *codec) +{ + gchar *name; + g_return_val_if_fail(PURPLE_IS_MEDIA_CODEC(codec), NULL); + g_object_get(codec, "encoding-name", &name, NULL); + return name; +} + +guint +purple_media_codec_get_clock_rate(PurpleMediaCodec *codec) +{ + guint clock_rate; + g_return_val_if_fail(PURPLE_IS_MEDIA_CODEC(codec), 0); + g_object_get(codec, "clock-rate", &clock_rate, NULL); + return clock_rate; +} + +guint +purple_media_codec_get_channels(PurpleMediaCodec *codec) +{ + guint channels; + g_return_val_if_fail(PURPLE_IS_MEDIA_CODEC(codec), 0); + g_object_get(codec, "channels", &channels, NULL); + return channels; +} + +GList * +purple_media_codec_get_optional_parameters(PurpleMediaCodec *codec) +{ + GList *optional_params; + g_return_val_if_fail(PURPLE_IS_MEDIA_CODEC(codec), NULL); + g_object_get(codec, "optional-params", &optional_params, NULL); + return optional_params; +} + +void +purple_media_codec_add_optional_parameter(PurpleMediaCodec *codec, + const gchar *name, const gchar *value) +{ + PurpleMediaCodecPrivate *priv; + PurpleKeyValuePair *new_param; + + g_return_if_fail(codec != NULL); + g_return_if_fail(name != NULL && value != NULL); + + priv = PURPLE_MEDIA_CODEC_GET_PRIVATE(codec); + + new_param = g_new0(PurpleKeyValuePair, 1); + new_param->key = g_strdup(name); + new_param->value = g_strdup(value); + priv->optional_params = g_list_append( + priv->optional_params, new_param); +} + +void +purple_media_codec_remove_optional_parameter(PurpleMediaCodec *codec, + PurpleKeyValuePair *param) +{ + PurpleMediaCodecPrivate *priv; + + g_return_if_fail(codec != NULL && param != NULL); + + priv = PURPLE_MEDIA_CODEC_GET_PRIVATE(codec); + + g_free(param->key); + g_free(param->value); + g_free(param); + + priv->optional_params = + g_list_remove(priv->optional_params, param); +} + +PurpleKeyValuePair * +purple_media_codec_get_optional_parameter(PurpleMediaCodec *codec, + const gchar *name, const gchar *value) +{ + PurpleMediaCodecPrivate *priv; + GList *iter; + + g_return_val_if_fail(codec != NULL, NULL); + g_return_val_if_fail(name != NULL, NULL); + + priv = PURPLE_MEDIA_CODEC_GET_PRIVATE(codec); + + for (iter = priv->optional_params; iter; iter = g_list_next(iter)) { + PurpleKeyValuePair *param = iter->data; + if (!g_ascii_strcasecmp(param->key, name) && + (value == NULL || + !g_ascii_strcasecmp(param->value, value))) + return param; + } + + return NULL; +} + +PurpleMediaCodec * +purple_media_codec_new(int id, const char *encoding_name, + PurpleMediaSessionType media_type, guint clock_rate) +{ + PurpleMediaCodec *codec = + g_object_new(PURPLE_TYPE_MEDIA_CODEC, + "id", id, + "encoding_name", encoding_name, + "media_type", media_type, + "clock-rate", clock_rate, NULL); + return codec; +} + +static PurpleMediaCodec * +purple_media_codec_copy(PurpleMediaCodec *codec) +{ + PurpleMediaCodecPrivate *priv; + PurpleMediaCodec *new_codec; + GList *iter; + + if (codec == NULL) + return NULL; + + priv = PURPLE_MEDIA_CODEC_GET_PRIVATE(codec); + + new_codec = purple_media_codec_new(priv->id, priv->encoding_name, + priv->media_type, priv->clock_rate); + g_object_set(codec, "channels", priv->channels, NULL); + + for (iter = priv->optional_params; iter; iter = g_list_next(iter)) { + PurpleKeyValuePair *param = + (PurpleKeyValuePair*)iter->data; + purple_media_codec_add_optional_parameter(new_codec, + param->key, param->value); + } + + return new_codec; +} + +#ifdef USE_VV +static FsCodec * +purple_media_codec_to_fs(const PurpleMediaCodec *codec) +{ + PurpleMediaCodecPrivate *priv; + FsCodec *new_codec; + GList *iter; + + if (codec == NULL) + return NULL; + + priv = PURPLE_MEDIA_CODEC_GET_PRIVATE(codec); + + new_codec = fs_codec_new(priv->id, priv->encoding_name, + purple_media_to_fs_media_type(priv->media_type), + priv->clock_rate); + new_codec->channels = priv->channels; + + for (iter = priv->optional_params; iter; iter = g_list_next(iter)) { + PurpleKeyValuePair *param = (PurpleKeyValuePair*)iter->data; + fs_codec_add_optional_parameter(new_codec, + param->key, param->value); + } + + return new_codec; +} + +static PurpleMediaCodec * +purple_media_codec_from_fs(const FsCodec *codec) +{ + PurpleMediaCodec *new_codec; + GList *iter; + + if (codec == NULL) + return NULL; + + new_codec = purple_media_codec_new(codec->id, codec->encoding_name, + purple_media_from_fs(codec->media_type, + FS_DIRECTION_BOTH), codec->clock_rate); + g_object_set(new_codec, "channels", codec->channels, NULL); + + for (iter = codec->optional_params; iter; iter = g_list_next(iter)) { + FsCodecParameter *param = (FsCodecParameter*)iter->data; + purple_media_codec_add_optional_parameter(new_codec, + param->name, param->value); + } + + return new_codec; +} +#endif + +gchar * +purple_media_codec_to_string(const PurpleMediaCodec *codec) +{ +#ifdef USE_VV + FsCodec *fscodec = purple_media_codec_to_fs(codec); + gchar *str = fs_codec_to_string(fscodec); + fs_codec_destroy(fscodec); + return str; +#else + return g_strdup(""); +#endif +} + +#ifdef USE_VV +static GList * +purple_media_codec_list_from_fs(GList *codecs) +{ + GList *new_list = NULL; + + for (; codecs; codecs = g_list_next(codecs)) { + new_list = g_list_prepend(new_list, + purple_media_codec_from_fs( + codecs->data)); + } + + new_list = g_list_reverse(new_list); + return new_list; +} + +static GList * +purple_media_codec_list_to_fs(GList *codecs) +{ + GList *new_list = NULL; + + for (; codecs; codecs = g_list_next(codecs)) { + new_list = g_list_prepend(new_list, + purple_media_codec_to_fs( + codecs->data)); + } + + new_list = g_list_reverse(new_list); + return new_list; +} +#endif + +GList * +purple_media_codec_list_copy(GList *codecs) +{ + GList *new_list = NULL; + + for (; codecs; codecs = g_list_next(codecs)) { + new_list = g_list_prepend(new_list, + purple_media_codec_copy(codecs->data)); + } + + new_list = g_list_reverse(new_list); + return new_list; +} + +void +purple_media_codec_list_free(GList *codecs) +{ + for (; codecs; codecs = + g_list_delete_link(codecs, codecs)) { + g_object_unref(codecs->data); + } +} + +#ifdef USE_VV +static PurpleMediaSession* +purple_media_get_session(PurpleMedia *media, const gchar *sess_id) +{ + g_return_val_if_fail(PURPLE_IS_MEDIA(media), NULL); + return (PurpleMediaSession*) (media->priv->sessions) ? + g_hash_table_lookup(media->priv->sessions, sess_id) : NULL; +} + +static FsParticipant* +purple_media_get_participant(PurpleMedia *media, const gchar *name) +{ + g_return_val_if_fail(PURPLE_IS_MEDIA(media), NULL); + return (FsParticipant*) (media->priv->participants) ? + g_hash_table_lookup(media->priv->participants, name) : NULL; +} + +static PurpleMediaStream* +purple_media_get_stream(PurpleMedia *media, const gchar *session, const gchar *participant) +{ + GList *streams; + + g_return_val_if_fail(PURPLE_IS_MEDIA(media), NULL); + + streams = media->priv->streams; + + for (; streams; streams = g_list_next(streams)) { + PurpleMediaStream *stream = streams->data; + if (!strcmp(stream->session->id, session) && + !strcmp(stream->participant, participant)) + return stream; + } + + return NULL; +} + +static GList * +purple_media_get_streams(PurpleMedia *media, const gchar *session, + const gchar *participant) +{ + GList *streams; + GList *ret = NULL; + + g_return_val_if_fail(PURPLE_IS_MEDIA(media), NULL); + + streams = media->priv->streams; + + for (; streams; streams = g_list_next(streams)) { + PurpleMediaStream *stream = streams->data; + if ((session == NULL || + !strcmp(stream->session->id, session)) && + (participant == NULL || + !strcmp(stream->participant, participant))) + ret = g_list_append(ret, stream); + } + + return ret; +} + +static void +purple_media_add_session(PurpleMedia *media, PurpleMediaSession *session) +{ + g_return_if_fail(PURPLE_IS_MEDIA(media)); + g_return_if_fail(session != NULL); + + if (!media->priv->sessions) { + purple_debug_info("media", "Creating hash table for sessions\n"); + media->priv->sessions = g_hash_table_new(g_str_hash, g_str_equal); + } + g_hash_table_insert(media->priv->sessions, g_strdup(session->id), session); +} + +static gboolean +purple_media_remove_session(PurpleMedia *media, PurpleMediaSession *session) +{ + g_return_val_if_fail(PURPLE_IS_MEDIA(media), FALSE); + return g_hash_table_remove(media->priv->sessions, session->id); +} + +static FsParticipant * +purple_media_add_participant(PurpleMedia *media, const gchar *name) +{ + FsParticipant *participant; + GError *err = NULL; + + g_return_val_if_fail(PURPLE_IS_MEDIA(media), NULL); + + participant = purple_media_get_participant(media, name); + + if (participant) + return participant; + + participant = fs_conference_new_participant(media->priv->conference, + (gchar*)name, &err); + + if (err) { + purple_debug_error("media", "Error creating participant: %s\n", + err->message); + g_error_free(err); + return NULL; + } + + if (!media->priv->participants) { + purple_debug_info("media", "Creating hash table for participants\n"); + media->priv->participants = g_hash_table_new_full(g_str_hash, + g_str_equal, g_free, NULL); + } + + g_hash_table_insert(media->priv->participants, g_strdup(name), participant); + + return participant; +} + +static PurpleMediaStream * +purple_media_insert_stream(PurpleMediaSession *session, const gchar *name, FsStream *stream) +{ + PurpleMediaStream *media_stream; + + g_return_val_if_fail(session != NULL, NULL); + + media_stream = g_new0(PurpleMediaStream, 1); + media_stream->stream = stream; + media_stream->participant = g_strdup(name); + media_stream->session = session; + + session->media->priv->streams = + g_list_append(session->media->priv->streams, media_stream); + + return media_stream; +} + +static void +purple_media_insert_local_candidate(PurpleMediaSession *session, const gchar *name, + FsCandidate *candidate) +{ + PurpleMediaStream *stream; + + g_return_if_fail(session != NULL); + + stream = purple_media_get_stream(session->media, session->id, name); + stream->local_candidates = g_list_append(stream->local_candidates, candidate); +} +#endif + +GList * +purple_media_get_session_names(PurpleMedia *media) +{ +#ifdef USE_VV + g_return_val_if_fail(PURPLE_IS_MEDIA(media), NULL); + return media->priv->sessions != NULL ? + g_hash_table_get_keys(media->priv->sessions) : NULL; +#else + return NULL; +#endif +} + +#ifdef USE_VV +static void +purple_media_set_src(PurpleMedia *media, const gchar *sess_id, GstElement *src) +{ + PurpleMediaSession *session; + GstPad *sinkpad; + GstPad *srcpad; + + g_return_if_fail(PURPLE_IS_MEDIA(media)); + g_return_if_fail(GST_IS_ELEMENT(src)); + + session = purple_media_get_session(media, sess_id); + + if (session == NULL) { + purple_debug_warning("media", "purple_media_set_src: trying" + " to set src on non-existent session\n"); + return; + } + + if (session->src) + gst_object_unref(session->src); + session->src = src; + gst_element_set_locked_state(session->src, TRUE); + + session->tee = gst_element_factory_make("tee", NULL); + gst_bin_add(GST_BIN(session->media->priv->confbin), session->tee); + + /* This supposedly isn't necessary, but it silences some warnings */ + if (GST_ELEMENT_PARENT(session->media->priv->confbin) + == GST_ELEMENT_PARENT(session->src)) { + GstPad *pad = gst_element_get_static_pad(session->tee, "sink"); + GstPad *ghost = gst_ghost_pad_new(NULL, pad); + gst_object_unref(pad); + gst_pad_set_active(ghost, TRUE); + gst_element_add_pad(session->media->priv->confbin, ghost); + } + + gst_element_link(session->src, session->media->priv->confbin); + gst_element_set_state(session->tee, GST_STATE_PLAYING); + + g_object_get(session->session, "sink-pad", &sinkpad, NULL); + srcpad = gst_element_get_request_pad(session->tee, "src%d"); + purple_debug_info("media", "connecting pad: %s\n", + gst_pad_link(srcpad, sinkpad) == GST_PAD_LINK_OK + ? "success" : "failure"); + gst_element_set_locked_state(session->src, FALSE); + gst_object_unref(session->src); +} +#endif + +GstElement * +purple_media_get_src(PurpleMedia *media, const gchar *sess_id) +{ +#ifdef USE_VV + PurpleMediaSession *session; + g_return_val_if_fail(PURPLE_IS_MEDIA(media), NULL); + session = purple_media_get_session(media, sess_id); + return (session != NULL) ? session->src : NULL; +#else + return NULL; +#endif +} + +#ifdef USE_VV +static PurpleMediaSession * +purple_media_session_from_fs_stream(PurpleMedia *media, FsStream *stream) +{ + FsSession *fssession; + GList *values; + + g_return_val_if_fail(PURPLE_IS_MEDIA(media), NULL); + g_return_val_if_fail(FS_IS_STREAM(stream), NULL); + + g_object_get(stream, "session", &fssession, NULL); + + values = g_hash_table_get_values(media->priv->sessions); + + for (; values; values = g_list_delete_link(values, values)) { + PurpleMediaSession *session = values->data; + + if (session->session == fssession) { + g_list_free(values); + g_object_unref(fssession); + return session; + } + } + + g_object_unref(fssession); + return NULL; +} + +static gboolean +media_bus_call(GstBus *bus, GstMessage *msg, PurpleMedia *media) +{ + switch(GST_MESSAGE_TYPE(msg)) { + case GST_MESSAGE_ELEMENT: { + if (!FS_IS_CONFERENCE(GST_MESSAGE_SRC(msg)) || + !PURPLE_IS_MEDIA(media) || + media->priv->conference != + FS_CONFERENCE(GST_MESSAGE_SRC(msg))) + break; + + if (gst_structure_has_name(msg->structure, "farsight-error")) { + FsError error_no; + gst_structure_get_enum(msg->structure, "error-no", + FS_TYPE_ERROR, (gint*)&error_no); + /* + * Unknown CName is only a problem for the + * multicast transmitter which isn't used. + */ + if (error_no != FS_ERROR_UNKNOWN_CNAME) + purple_debug_error("media", "farsight-error: %i: %s\n", error_no, + gst_structure_get_string(msg->structure, "error-msg")); + } else if (gst_structure_has_name(msg->structure, + "farsight-new-local-candidate")) { + FsStream *stream = g_value_get_object(gst_structure_get_value(msg->structure, "stream")); + FsCandidate *local_candidate = g_value_get_boxed(gst_structure_get_value(msg->structure, "candidate")); + PurpleMediaSession *session = purple_media_session_from_fs_stream(media, stream); + purple_media_new_local_candidate_cb(stream, local_candidate, session); + } else if (gst_structure_has_name(msg->structure, + "farsight-local-candidates-prepared")) { + FsStream *stream = g_value_get_object(gst_structure_get_value(msg->structure, "stream")); + PurpleMediaSession *session = purple_media_session_from_fs_stream(media, stream); + purple_media_candidates_prepared_cb(stream, session); + } else if (gst_structure_has_name(msg->structure, + "farsight-new-active-candidate-pair")) { + FsStream *stream = g_value_get_object(gst_structure_get_value(msg->structure, "stream")); + FsCandidate *local_candidate = g_value_get_boxed(gst_structure_get_value(msg->structure, "local-candidate")); + FsCandidate *remote_candidate = g_value_get_boxed(gst_structure_get_value(msg->structure, "remote-candidate")); + PurpleMediaSession *session = purple_media_session_from_fs_stream(media, stream); + purple_media_candidate_pair_established_cb(stream, local_candidate, remote_candidate, session); + } else if (gst_structure_has_name(msg->structure, + "farsight-recv-codecs-changed")) { + GList *codecs = g_value_get_boxed(gst_structure_get_value(msg->structure, "codecs")); + FsCodec *codec = codecs->data; + purple_debug_info("media", "farsight-recv-codecs-changed: %s\n", codec->encoding_name); + + } else if (gst_structure_has_name(msg->structure, + "farsight-component-state-changed")) { + FsStreamState fsstate = g_value_get_enum(gst_structure_get_value(msg->structure, "state")); + guint component = g_value_get_uint(gst_structure_get_value(msg->structure, "component")); + const gchar *state; + switch (fsstate) { + case FS_STREAM_STATE_FAILED: + state = "FAILED"; + break; + case FS_STREAM_STATE_DISCONNECTED: + state = "DISCONNECTED"; + break; + case FS_STREAM_STATE_GATHERING: + state = "GATHERING"; + break; + case FS_STREAM_STATE_CONNECTING: + state = "CONNECTING"; + break; + case FS_STREAM_STATE_CONNECTED: + state = "CONNECTED"; + break; + case FS_STREAM_STATE_READY: + state = "READY"; + break; + default: + state = "UNKNOWN"; + break; + } + purple_debug_info("media", "farsight-component-state-changed: component: %u state: %s\n", component, state); + } else if (gst_structure_has_name(msg->structure, + "farsight-send-codec-changed")) { + FsCodec *codec = g_value_get_boxed(gst_structure_get_value(msg->structure, "codec")); + gchar *codec_str = fs_codec_to_string(codec); + purple_debug_info("media", "farsight-send-codec-changed: codec: %s\n", codec_str); + g_free(codec_str); + } else if (gst_structure_has_name(msg->structure, + "farsight-codecs-changed")) { + GList *sessions = g_hash_table_get_values(PURPLE_MEDIA(media)->priv->sessions); + FsSession *fssession = g_value_get_object(gst_structure_get_value(msg->structure, "session")); + for (; sessions; sessions = g_list_delete_link(sessions, sessions)) { + PurpleMediaSession *session = sessions->data; + if (session->session == fssession) { + gchar *session_id = g_strdup(session->id); + g_signal_emit(media, purple_media_signals[CODECS_CHANGED], 0, session_id); + g_free(session_id); + g_list_free(sessions); + break; + } + } + } + break; + } + default: + break; + } + + return TRUE; +} +#endif + +PurpleConnection * +purple_media_get_connection(PurpleMedia *media) +{ +#ifdef USE_VV + PurpleConnection *pc; + g_return_val_if_fail(PURPLE_IS_MEDIA(media), NULL); + g_object_get(G_OBJECT(media), "connection", &pc, NULL); + return pc; +#else + return NULL; +#endif +} + +gpointer +purple_media_get_prpl_data(PurpleMedia *media) +{ +#ifdef USE_VV + gpointer prpl_data; + g_return_val_if_fail(PURPLE_IS_MEDIA(media), NULL); + g_object_get(G_OBJECT(media), "prpl-data", &prpl_data, NULL); + return prpl_data; +#else + return NULL; +#endif +} + +void +purple_media_set_prpl_data(PurpleMedia *media, gpointer prpl_data) +{ +#ifdef USE_VV + g_return_if_fail(PURPLE_IS_MEDIA(media)); + g_object_set(G_OBJECT(media), "prpl-data", prpl_data, NULL); +#endif +} + +void +purple_media_error(PurpleMedia *media, const gchar *error, ...) +{ +#ifdef USE_VV + va_list args; + gchar *message; + + g_return_if_fail(PURPLE_IS_MEDIA(media)); + + va_start(args, error); + message = g_strdup_vprintf(error, args); + va_end(args); + + purple_debug_error("media", "%s\n", message); + g_signal_emit(media, purple_media_signals[ERROR], 0, message); + + g_free(message); +#endif +} + +void +purple_media_end(PurpleMedia *media, + const gchar *session_id, const gchar *participant) +{ +#ifdef USE_VV + g_return_if_fail(PURPLE_IS_MEDIA(media)); + if (session_id == NULL && participant == NULL) { + g_signal_emit(media, purple_media_signals[STATE_CHANGED], + 0, PURPLE_MEDIA_STATE_END, + NULL, NULL); + g_object_unref(media); + } +#endif +} + +void +purple_media_stream_info(PurpleMedia *media, PurpleMediaInfoType type, + const gchar *session_id, const gchar *participant, + gboolean local) +{ +#ifdef USE_VV + g_return_if_fail(PURPLE_IS_MEDIA(media)); + + if (type == PURPLE_MEDIA_INFO_ACCEPT) { + GList *streams; + + g_return_if_fail(PURPLE_IS_MEDIA(media)); + + streams = purple_media_get_streams(media, + session_id, participant); + + for (; streams; streams = + g_list_delete_link(streams, streams)) { + PurpleMediaStream *stream = streams->data; + g_object_set(G_OBJECT(stream->stream), "direction", + purple_media_to_fs_stream_direction( + stream->session->type), NULL); + stream->accepted = TRUE; + } + + g_signal_emit(media, purple_media_signals[ACCEPTED], + 0, NULL, NULL); + } else if (local == TRUE && (type == PURPLE_MEDIA_INFO_MUTE || + type == PURPLE_MEDIA_INFO_UNMUTE)) { + GList *sessions; + gboolean active = (type == PURPLE_MEDIA_INFO_MUTE); + + g_return_if_fail(PURPLE_IS_MEDIA(media)); + + if (session_id == NULL) + sessions = g_hash_table_get_values( + media->priv->sessions); + else + sessions = g_list_prepend(NULL, + purple_media_get_session( + media, session_id)); + + purple_debug_info("media", "Turning mute %s\n", + active ? "on" : "off"); + + for (; sessions; sessions = g_list_delete_link( + sessions, sessions)) { + PurpleMediaSession *session = sessions->data; + if (session->type & PURPLE_MEDIA_SEND_AUDIO) { + GstElement *volume = gst_bin_get_by_name( + GST_BIN(session->src), + "purpleaudioinputvolume"); + g_object_set(volume, "mute", active, NULL); + } + } + } + + g_signal_emit(media, purple_media_signals[STREAM_INFO], + 0, type, session_id, participant, local); + + if (type == PURPLE_MEDIA_INFO_HANGUP || + type == PURPLE_MEDIA_INFO_REJECT) { + purple_media_end(media, session_id, participant); + } +#endif +} + +#ifdef USE_VV +static void +purple_media_new_local_candidate_cb(FsStream *stream, + FsCandidate *local_candidate, + PurpleMediaSession *session) +{ + gchar *name; + FsParticipant *participant; + PurpleMediaCandidate *candidate; + + g_return_if_fail(FS_IS_STREAM(stream)); + g_return_if_fail(session != NULL); + + purple_debug_info("media", "got new local candidate: %s\n", local_candidate->foundation); + g_object_get(stream, "participant", &participant, NULL); + g_object_get(participant, "cname", &name, NULL); + g_object_unref(participant); + + purple_media_insert_local_candidate(session, name, fs_candidate_copy(local_candidate)); + + candidate = purple_media_candidate_from_fs(local_candidate); + g_signal_emit(session->media, purple_media_signals[NEW_CANDIDATE], + 0, session->id, name, candidate); + g_object_unref(candidate); + + g_free(name); +} + +static void +purple_media_candidates_prepared_cb(FsStream *stream, PurpleMediaSession *session) +{ + gchar *name; + FsParticipant *participant; + PurpleMediaStream *stream_data; + + g_return_if_fail(FS_IS_STREAM(stream)); + g_return_if_fail(session != NULL); + + g_object_get(stream, "participant", &participant, NULL); + g_object_get(participant, "cname", &name, NULL); + g_object_unref(participant); + + stream_data = purple_media_get_stream(session->media, session->id, name); + stream_data->candidates_prepared = TRUE; + + g_signal_emit(session->media, + purple_media_signals[CANDIDATES_PREPARED], + 0, session->id, name); + + g_free(name); +} + +/* callback called when a pair of transport candidates (local and remote) + * has been established */ +static void +purple_media_candidate_pair_established_cb(FsStream *fsstream, + FsCandidate *native_candidate, + FsCandidate *remote_candidate, + PurpleMediaSession *session) +{ + gchar *name; + FsParticipant *participant; + PurpleMediaStream *stream; + GList *iter; + + g_return_if_fail(FS_IS_STREAM(fsstream)); + g_return_if_fail(session != NULL); + + g_object_get(fsstream, "participant", &participant, NULL); + g_object_get(participant, "cname", &name, NULL); + g_object_unref(participant); + + stream = purple_media_get_stream(session->media, session->id, name); + + iter = stream->active_local_candidates; + for(; iter; iter = g_list_next(iter)) { + FsCandidate *c = iter->data; + if (native_candidate->component_id == c->component_id) { + fs_candidate_destroy(c); + stream->active_local_candidates = + g_list_delete_link(iter, iter); + stream->active_local_candidates = g_list_prepend( + stream->active_local_candidates, + fs_candidate_copy(native_candidate)); + break; + } + } + if (iter == NULL) + stream->active_local_candidates = g_list_prepend( + stream->active_local_candidates, + fs_candidate_copy(native_candidate)); + + iter = stream->active_remote_candidates; + for(; iter; iter = g_list_next(iter)) { + FsCandidate *c = iter->data; + if (native_candidate->component_id == c->component_id) { + fs_candidate_destroy(c); + stream->active_remote_candidates = + g_list_delete_link(iter, iter); + stream->active_remote_candidates = g_list_prepend( + stream->active_remote_candidates, + fs_candidate_copy(remote_candidate)); + break; + } + } + if (iter == NULL) + stream->active_remote_candidates = g_list_prepend( + stream->active_remote_candidates, + fs_candidate_copy(remote_candidate)); + + purple_debug_info("media", "candidate pair established\n"); +} + +static gboolean +purple_media_connected_cb(PurpleMediaStream *stream) +{ + g_return_val_if_fail(stream != NULL, FALSE); + + stream->connected_cb_id = 0; + + purple_media_manager_create_output_window( + stream->session->media->priv->manager, + stream->session->media, + stream->session->id, stream->participant); + + g_signal_emit(stream->session->media, + purple_media_signals[STATE_CHANGED], + 0, PURPLE_MEDIA_STATE_CONNECTED, + stream->session->id, stream->participant); + return FALSE; +} + +static void +purple_media_src_pad_added_cb(FsStream *fsstream, GstPad *srcpad, + FsCodec *codec, PurpleMediaStream *stream) +{ + PurpleMediaPrivate *priv; + GstPad *sinkpad; + + g_return_if_fail(FS_IS_STREAM(fsstream)); + g_return_if_fail(stream != NULL); + + priv = stream->session->media->priv; + + if (stream->src == NULL) { + GstElement *sink = NULL; + + if (codec->media_type == FS_MEDIA_TYPE_AUDIO) { + /* + * Should this instead be: + * audioconvert ! audioresample ! liveadder ! + * audioresample ! audioconvert ! realsink + */ + stream->src = gst_element_factory_make( + "liveadder", NULL); + sink = purple_media_manager_get_element(priv->manager, + PURPLE_MEDIA_RECV_AUDIO, + stream->session->media, + stream->session->id, + stream->participant); + } else if (codec->media_type == FS_MEDIA_TYPE_VIDEO) { + stream->src = gst_element_factory_make( + "fsfunnel", NULL); + sink = gst_element_factory_make( + "fakesink", NULL); + g_object_set(G_OBJECT(sink), "async", FALSE, NULL); + } + stream->tee = gst_element_factory_make("tee", NULL); + gst_bin_add_many(GST_BIN(priv->confbin), + stream->src, stream->tee, sink, NULL); + gst_element_sync_state_with_parent(sink); + gst_element_sync_state_with_parent(stream->tee); + gst_element_sync_state_with_parent(stream->src); + gst_element_link_many(stream->src, stream->tee, sink, NULL); + } + + sinkpad = gst_element_get_request_pad(stream->src, "sink%d"); + gst_pad_link(srcpad, sinkpad); + gst_object_unref(sinkpad); + + stream->connected_cb_id = purple_timeout_add(0, + (GSourceFunc)purple_media_connected_cb, stream); +} +#endif /* USE_VV */ + +gboolean +purple_media_add_stream(PurpleMedia *media, const gchar *sess_id, + const gchar *who, PurpleMediaSessionType type, + gboolean initiator, const gchar *transmitter, + guint num_params, GParameter *params) +{ +#ifdef USE_VV + PurpleMediaSession *session; + FsParticipant *participant = NULL; + PurpleMediaStream *stream = NULL; + FsMediaType media_type = purple_media_to_fs_media_type(type); + FsStreamDirection type_direction = + purple_media_to_fs_stream_direction(type); + gboolean is_nice = !strcmp(transmitter, "nice"); + + g_return_val_if_fail(PURPLE_IS_MEDIA(media), FALSE); + + session = purple_media_get_session(media, sess_id); + + if (!session) { + GError *err = NULL; + GList *codec_conf = NULL; + gchar *filename = NULL; + PurpleMediaSessionType session_type; + GstElement *src = NULL; + + session = g_new0(PurpleMediaSession, 1); + + session->session = fs_conference_new_session( + media->priv->conference, media_type, &err); + + if (err != NULL) { + purple_media_error(media, "Error creating session: %s\n", err->message); + g_error_free(err); + g_free(session); + return FALSE; + } + + /* XXX: SPEEX has a latency of 5 or 6 seconds for me */ +#if 0 + /* SPEEX is added through the configuration */ + codec_conf = g_list_prepend(codec_conf, fs_codec_new(FS_CODEC_ID_ANY, + "SPEEX", FS_MEDIA_TYPE_AUDIO, 8000)); + codec_conf = g_list_prepend(codec_conf, fs_codec_new(FS_CODEC_ID_ANY, + "SPEEX", FS_MEDIA_TYPE_AUDIO, 16000)); +#endif + + filename = g_build_filename(purple_user_dir(), "fs-codec.conf", NULL); + codec_conf = fs_codec_list_from_keyfile(filename, &err); + g_free(filename); + + if (err != NULL) { + if (err->code == 4) + purple_debug_info("media", "Couldn't read " + "fs-codec.conf: %s\n", + err->message); + else + purple_debug_error("media", "Error reading " + "fs-codec.conf: %s\n", + err->message); + g_error_free(err); + } + + fs_session_set_codec_preferences(session->session, codec_conf, NULL); + + /* + * Removes a 5-7 second delay before + * receiving the src-pad-added signal. + * Only works for non-multicast FsRtpSessions. + */ + if (is_nice || !strcmp(transmitter, "rawudp")) + g_object_set(G_OBJECT(session->session), + "no-rtcp-timeout", 0, NULL); + + fs_codec_list_destroy(codec_conf); + + session->id = g_strdup(sess_id); + session->media = media; + session->type = type; + session->initiator = initiator; + + purple_media_add_session(media, session); + g_signal_emit(media, purple_media_signals[STATE_CHANGED], + 0, PURPLE_MEDIA_STATE_NEW, + session->id, NULL); + + session_type = purple_media_from_fs(media_type, + FS_DIRECTION_SEND); + src = purple_media_manager_get_element( + media->priv->manager, session_type, + media, session->id, who); + if (!GST_IS_ELEMENT(src)) { + purple_debug_error("media", + "Error creating src for session %s\n", + session->id); + purple_media_end(media, session->id, NULL); + return FALSE; + } + + purple_media_set_src(media, session->id, src); + gst_element_set_state(session->src, GST_STATE_PLAYING); + + purple_media_manager_create_output_window( + media->priv->manager, + session->media, + session->id, NULL); + } + + if (!(participant = purple_media_add_participant(media, who))) { + purple_media_remove_session(media, session); + g_free(session); + return FALSE; + } else { + g_signal_emit(media, purple_media_signals[STATE_CHANGED], + 0, PURPLE_MEDIA_STATE_NEW, + NULL, who); + } + + stream = purple_media_get_stream(media, sess_id, who); + + if (!stream) { + GError *err = NULL; + FsStream *fsstream = NULL; + const gchar *stun_ip = purple_network_get_stun_ip(); + const gchar *turn_ip = purple_network_get_turn_ip(); + + if (stun_ip || turn_ip) { + guint new_num_params = + (stun_ip && is_nice) && turn_ip ? + num_params + 2 : num_params + 1; + guint next_param_index = num_params; + GParameter *param = g_new0(GParameter, new_num_params); + memcpy(param, params, sizeof(GParameter) * num_params); + + if (stun_ip) { + purple_debug_info("media", + "setting property stun-ip on new stream: %s\n", stun_ip); + + param[next_param_index].name = "stun-ip"; + g_value_init(¶m[next_param_index].value, G_TYPE_STRING); + g_value_set_string(¶m[next_param_index].value, stun_ip); + next_param_index++; + } + + if (turn_ip && is_nice) { + GValueArray *relay_info = g_value_array_new(0); + GValue value; + gint turn_port = + purple_prefs_get_int("/purple/network/turn_port"); + const gchar *username = + purple_prefs_get_string("/purple/network/turn_username"); + const gchar *password = + purple_prefs_get_string("/purple/network/turn_password"); + GstStructure *turn_setup = gst_structure_new("relay-info", + "ip", G_TYPE_STRING, turn_ip, + "port", G_TYPE_UINT, turn_port, + "username", G_TYPE_STRING, username, + "password", G_TYPE_STRING, password, + NULL); + + if (turn_setup) { + memset(&value, 0, sizeof(GValue)); + g_value_init(&value, GST_TYPE_STRUCTURE); + gst_value_set_structure(&value, turn_setup); + relay_info = g_value_array_append(relay_info, &value); + gst_structure_free(turn_setup); + + purple_debug_info("media", + "setting property relay-info on new stream\n"); + param[next_param_index].name = "relay-info"; + g_value_init(¶m[next_param_index].value, + G_TYPE_VALUE_ARRAY); + g_value_set_boxed(¶m[next_param_index].value, + relay_info); + g_value_array_free(relay_info); + } else { + purple_debug_error("media", "Error relay info"); + g_object_unref(participant); + g_hash_table_remove(media->priv->participants, who); + purple_media_remove_session(media, session); + g_free(session); + return FALSE; + } + } + + fsstream = fs_session_new_stream(session->session, + participant, type_direction & + FS_DIRECTION_RECV, transmitter, + new_num_params, param, &err); + g_free(param); + } else { + fsstream = fs_session_new_stream(session->session, + participant, type_direction & + FS_DIRECTION_RECV, transmitter, + num_params, params, &err); + } + + if (err) { + purple_debug_error("media", "Error creating stream: %s\n", + err->message); + g_error_free(err); + g_object_unref(participant); + g_hash_table_remove(media->priv->participants, who); + purple_media_remove_session(media, session); + g_free(session); + return FALSE; + } + + stream = purple_media_insert_stream(session, who, fsstream); + stream->initiator = initiator; + + /* callback for source pad added (new stream source ready) */ + g_signal_connect(G_OBJECT(fsstream), + "src-pad-added", G_CALLBACK(purple_media_src_pad_added_cb), stream); + + g_signal_emit(media, purple_media_signals[STATE_CHANGED], + 0, PURPLE_MEDIA_STATE_NEW, + session->id, who); + } else { + if (purple_media_to_fs_stream_direction(stream->session->type) + != type_direction) { + /* change direction */ + g_object_set(stream->stream, "direction", + type_direction, NULL); + } + } + + return TRUE; +#else + return FALSE; +#endif /* USE_VV */ +} + +PurpleMediaManager * +purple_media_get_manager(PurpleMedia *media) +{ + PurpleMediaManager *ret; + g_return_val_if_fail(PURPLE_IS_MEDIA(media), NULL); + g_object_get(media, "manager", &ret, NULL); + return ret; +} + +PurpleMediaSessionType +purple_media_get_session_type(PurpleMedia *media, const gchar *sess_id) +{ +#ifdef USE_VV + PurpleMediaSession *session; + g_return_val_if_fail(PURPLE_IS_MEDIA(media), PURPLE_MEDIA_NONE); + session = purple_media_get_session(media, sess_id); + return session->type; +#else + return PURPLE_MEDIA_NONE; +#endif +} +/* XXX: Should wait until codecs-ready is TRUE before using this function */ +GList * +purple_media_get_codecs(PurpleMedia *media, const gchar *sess_id) +{ +#ifdef USE_VV + GList *fscodecs; + GList *codecs; + PurpleMediaSession *session; + + g_return_val_if_fail(PURPLE_IS_MEDIA(media), NULL); + + session = purple_media_get_session(media, sess_id); + + if (session == NULL) + return NULL; + + g_object_get(G_OBJECT(session->session), + "codecs", &fscodecs, NULL); + codecs = purple_media_codec_list_from_fs(fscodecs); + fs_codec_list_destroy(fscodecs); + return codecs; +#else + return NULL; +#endif +} + +GList * +purple_media_get_local_candidates(PurpleMedia *media, const gchar *sess_id, const gchar *name) +{ +#ifdef USE_VV + PurpleMediaStream *stream; + g_return_val_if_fail(PURPLE_IS_MEDIA(media), NULL); + stream = purple_media_get_stream(media, sess_id, name); + return purple_media_candidate_list_from_fs(stream->local_candidates); +#else + return NULL; +#endif +} + +void +purple_media_add_remote_candidates(PurpleMedia *media, const gchar *sess_id, + const gchar *name, GList *remote_candidates) +{ +#ifdef USE_VV + PurpleMediaStream *stream; + GError *err = NULL; + + g_return_if_fail(PURPLE_IS_MEDIA(media)); + stream = purple_media_get_stream(media, sess_id, name); + + if (stream == NULL) { + purple_debug_error("media", + "purple_media_add_remote_candidates: " + "couldn't find stream %s %s.\n", + sess_id, name); + return; + } + + stream->remote_candidates = g_list_concat(stream->remote_candidates, + purple_media_candidate_list_to_fs(remote_candidates)); + + fs_stream_set_remote_candidates(stream->stream, + stream->remote_candidates, &err); + + if (err) { + purple_debug_error("media", "Error adding remote" + " candidates: %s\n", err->message); + g_error_free(err); + } +#endif +} + +#if 0 +/* + * These two functions aren't being used and I'd rather not lock in the API + * until they are needed. If they ever are. + */ + +GList * +purple_media_get_active_local_candidates(PurpleMedia *media, + const gchar *sess_id, const gchar *name) +{ +#ifdef USE_VV + PurpleMediaStream *stream; + g_return_val_if_fail(PURPLE_IS_MEDIA(media), NULL); + stream = purple_media_get_stream(media, sess_id, name); + return purple_media_candidate_list_from_fs( + stream->active_local_candidates); +#else + return NULL; +#endif +} + +GList * +purple_media_get_active_remote_candidates(PurpleMedia *media, + const gchar *sess_id, const gchar *name) +{ +#ifdef USE_VV + PurpleMediaStream *stream; + g_return_val_if_fail(PURPLE_IS_MEDIA(media), NULL); + stream = purple_media_get_stream(media, sess_id, name); + return purple_media_candidate_list_from_fs( + stream->active_remote_candidates); +#else + return NULL; +#endif +} +#endif + +gboolean +purple_media_set_remote_codecs(PurpleMedia *media, const gchar *sess_id, const gchar *name, GList *codecs) +{ +#ifdef USE_VV + PurpleMediaStream *stream; + FsStream *fsstream; + GList *fscodecs; + GError *err = NULL; + + g_return_val_if_fail(PURPLE_IS_MEDIA(media), FALSE); + stream = purple_media_get_stream(media, sess_id, name); + + if (stream == NULL) + return FALSE; + + fsstream = stream->stream; + fscodecs = purple_media_codec_list_to_fs(codecs); + fs_stream_set_remote_codecs(fsstream, fscodecs, &err); + fs_codec_list_destroy(fscodecs); + + if (err) { + purple_debug_error("media", "Error setting remote codecs: %s\n", + err->message); + g_error_free(err); + return FALSE; + } + return TRUE; +#else + return FALSE; +#endif +} + +gboolean +purple_media_candidates_prepared(PurpleMedia *media, + const gchar *session_id, const gchar *participant) +{ +#ifdef USE_VV + GList *streams; + gboolean prepared = TRUE; + + g_return_val_if_fail(PURPLE_IS_MEDIA(media), FALSE); + + streams = purple_media_get_streams(media, session_id, participant); + + for (; streams; streams = g_list_delete_link(streams, streams)) { + PurpleMediaStream *stream = streams->data; + if (stream->candidates_prepared == FALSE) { + g_list_free(streams); + prepared = FALSE; + break; + } + } + + return prepared; +#else + return FALSE; +#endif +} + +gboolean +purple_media_set_send_codec(PurpleMedia *media, const gchar *sess_id, PurpleMediaCodec *codec) +{ +#ifdef USE_VV + PurpleMediaSession *session; + FsCodec *fscodec; + GError *err = NULL; + + g_return_val_if_fail(PURPLE_IS_MEDIA(media), FALSE); + + session = purple_media_get_session(media, sess_id); + + if (session != NULL) + return FALSE; + + fscodec = purple_media_codec_to_fs(codec); + fs_session_set_send_codec(session->session, fscodec, &err); + fs_codec_destroy(fscodec); + + if (err) { + purple_debug_error("media", "Error setting send codec\n"); + g_error_free(err); + return FALSE; + } + return TRUE; +#else + return FALSE; +#endif +} + +gboolean +purple_media_codecs_ready(PurpleMedia *media, const gchar *sess_id) +{ +#ifdef USE_VV + gboolean ret; + + g_return_val_if_fail(PURPLE_IS_MEDIA(media), FALSE); + + if (sess_id != NULL) { + PurpleMediaSession *session; + session = purple_media_get_session(media, sess_id); + + if (session == NULL) + return FALSE; + + g_object_get(session->session, "codecs-ready", &ret, NULL); + } else { + GList *values = g_hash_table_get_values(media->priv->sessions); + for (; values; values = g_list_delete_link(values, values)) { + PurpleMediaSession *session = values->data; + g_object_get(session->session, + "codecs-ready", &ret, NULL); + if (ret == FALSE) + break; + } + if (values != NULL) + g_list_free(values); + } + return ret; +#else + return FALSE; +#endif +} + +gboolean +purple_media_is_initiator(PurpleMedia *media, + const gchar *sess_id, const gchar *participant) +{ +#ifdef USE_VV + g_return_val_if_fail(PURPLE_IS_MEDIA(media), FALSE); + + if (sess_id == NULL && participant == NULL) + return media->priv->initiator; + else if (sess_id != NULL && participant == NULL) { + PurpleMediaSession *session = + purple_media_get_session(media, sess_id); + return session != NULL ? session->initiator : FALSE; + } else if (sess_id != NULL && participant != NULL) { + PurpleMediaStream *stream = purple_media_get_stream( + media, sess_id, participant); + return stream != NULL ? stream->initiator : FALSE; + } +#endif + return FALSE; +} + +gboolean +purple_media_accepted(PurpleMedia *media, const gchar *sess_id, + const gchar *participant) +{ +#ifdef USE_VV + gboolean accepted = TRUE; + + g_return_val_if_fail(PURPLE_IS_MEDIA(media), FALSE); + + if (sess_id == NULL && participant == NULL) { + GList *streams = media->priv->streams; + + for (; streams; streams = g_list_next(streams)) { + PurpleMediaStream *stream = streams->data; + if (stream->accepted == FALSE) { + accepted = FALSE; + break; + } + } + } else if (sess_id != NULL && participant == NULL) { + GList *streams = purple_media_get_streams( + media, sess_id, NULL); + for (; streams; streams = + g_list_delete_link(streams, streams)) { + PurpleMediaStream *stream = streams->data; + if (stream->accepted == FALSE) { + g_list_free(streams); + accepted = FALSE; + break; + } + } + } else if (sess_id != NULL && participant != NULL) { + PurpleMediaStream *stream = purple_media_get_stream( + media, sess_id, participant); + if (stream == NULL || stream->accepted == FALSE) + accepted = FALSE; + } + + return accepted; +#else + return FALSE; +#endif +} + +void purple_media_set_input_volume(PurpleMedia *media, + const gchar *session_id, double level) +{ +#ifdef USE_VV + GList *sessions; + + g_return_if_fail(PURPLE_IS_MEDIA(media)); + + if (session_id == NULL) + sessions = g_hash_table_get_values(media->priv->sessions); + else + sessions = g_list_append(NULL, + purple_media_get_session(media, session_id)); + + for (; sessions; sessions = g_list_delete_link(sessions, sessions)) { + PurpleMediaSession *session = sessions->data; + + if (session->type & PURPLE_MEDIA_SEND_AUDIO) { + GstElement *volume = gst_bin_get_by_name( + GST_BIN(session->src), + "purpleaudioinputvolume"); + g_object_set(volume, "volume", level, NULL); + } + } +#endif +} + +void purple_media_set_output_volume(PurpleMedia *media, + const gchar *session_id, const gchar *participant, + double level) +{ +#ifdef USE_VV + GList *streams; + + g_return_if_fail(PURPLE_IS_MEDIA(media)); + + streams = purple_media_get_streams(media, + session_id, participant); + + for (; streams; streams = g_list_delete_link(streams, streams)) { + PurpleMediaStream *stream = streams->data; + + if (stream->session->type & PURPLE_MEDIA_RECV_AUDIO) { + GstElement *tee = stream->tee; + GstIterator *iter = gst_element_iterate_src_pads(tee); + GstPad *sinkpad; + while (gst_iterator_next(iter, (gpointer)&sinkpad) + == GST_ITERATOR_OK) { + GstPad *peer = gst_pad_get_peer(sinkpad); + GstElement *volume; + + if (peer == NULL) { + gst_object_unref(sinkpad); + continue; + } + + volume = gst_bin_get_by_name(GST_BIN( + GST_OBJECT_PARENT(peer)), + "purpleaudiooutputvolume"); + g_object_set(volume, "volume", level, NULL); + gst_object_unref(peer); + gst_object_unref(sinkpad); + } + gst_iterator_free(iter); + } + } +#endif +} + +gulong +purple_media_set_output_window(PurpleMedia *media, const gchar *session_id, + const gchar *participant, gulong window_id) +{ +#ifdef USE_VV + g_return_val_if_fail(PURPLE_IS_MEDIA(media), FALSE); + + return purple_media_manager_set_output_window(media->priv->manager, + media, session_id, participant, window_id); +#else + return 0; +#endif +} + +void +purple_media_remove_output_windows(PurpleMedia *media) +{ +#ifdef USE_VV + GList *iter = media->priv->streams; + for (; iter; iter = g_list_next(iter)) { + PurpleMediaStream *stream = iter->data; + purple_media_manager_remove_output_windows( + media->priv->manager, media, + stream->session->id, stream->participant); + } + + iter = purple_media_get_session_names(media); + for (; iter; iter = g_list_delete_link(iter, iter)) { + gchar *session_name = iter->data; + purple_media_manager_remove_output_windows( + media->priv->manager, media, + session_name, NULL); + } +#endif +} + +GstElement * +purple_media_get_tee(PurpleMedia *media, + const gchar *session_id, const gchar *participant) +{ +#ifdef USE_VV + g_return_val_if_fail(PURPLE_IS_MEDIA(media), NULL); + + if (session_id != NULL && participant == NULL) { + PurpleMediaSession *session = + purple_media_get_session(media, session_id); + return (session != NULL) ? session->tee : NULL; + } else if (session_id != NULL && participant != NULL) { + PurpleMediaStream *stream = + purple_media_get_stream(media, + session_id, participant); + return (stream != NULL) ? stream->tee : NULL; + } + g_return_val_if_reached(NULL); +#else + return NULL; +#endif +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libpurple/media.h Fri Apr 03 16:45:38 2009 +0000 @@ -0,0 +1,606 @@ +/** + * @file media.h Media API + * @ingroup core + */ + +/* purple + * + * Purple is the legal property of its developers, whose names are too numerous + * to list here. Please refer to the COPYRIGHT file distributed with this + * source distribution. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef __MEDIA_H_ +#define __MEDIA_H_ + +#include "signals.h" +#include "util.h" + +#include <glib.h> +#include <glib-object.h> + +G_BEGIN_DECLS + +#define PURPLE_TYPE_MEDIA_CANDIDATE (purple_media_candidate_get_type()) +#define PURPLE_MEDIA_CANDIDATE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), PURPLE_TYPE_MEDIA_CANDIDATE, PurpleMediaCandidate)) +#define PURPLE_MEDIA_CANDIDATE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), PURPLE_TYPE_MEDIA_CANDIDATE, PurpleMediaCandidate)) +#define PURPLE_IS_MEDIA_CANDIDATE(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), PURPLE_TYPE_MEDIA_CANDIDATE)) +#define PURPLE_IS_MEDIA_CANDIDATE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), PURPLE_TYPE_MEDIA_CANDIDATE)) +#define PURPLE_MEDIA_CANDIDATE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), PURPLE_TYPE_MEDIA_CANDIDATE, PurpleMediaCandidate)) + +#define PURPLE_TYPE_MEDIA_CODEC (purple_media_codec_get_type()) +#define PURPLE_MEDIA_CODEC(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), PURPLE_TYPE_MEDIA_CODEC, PurpleMediaCodec)) +#define PURPLE_MEDIA_CODEC_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), PURPLE_TYPE_MEDIA_CODEC, PurpleMediaCodec)) +#define PURPLE_IS_MEDIA_CODEC(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), PURPLE_TYPE_MEDIA_CODEC)) +#define PURPLE_IS_MEDIA_CODEC_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), PURPLE_TYPE_MEDIA_CODEC)) +#define PURPLE_MEDIA_CODEC_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), PURPLE_TYPE_MEDIA_CODEC, PurpleMediaCodec)) + +#define PURPLE_TYPE_MEDIA_SESSION_TYPE (purple_media_session_type_get_type()) +#define PURPLE_TYPE_MEDIA (purple_media_get_type()) +#define PURPLE_MEDIA(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), PURPLE_TYPE_MEDIA, PurpleMedia)) +#define PURPLE_MEDIA_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), PURPLE_TYPE_MEDIA, PurpleMediaClass)) +#define PURPLE_IS_MEDIA(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), PURPLE_TYPE_MEDIA)) +#define PURPLE_IS_MEDIA_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), PURPLE_TYPE_MEDIA)) +#define PURPLE_MEDIA_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), PURPLE_TYPE_MEDIA, PurpleMediaClass)) + +#define PURPLE_TYPE_MEDIA_CANDIDATE_TYPE (purple_media_candidate_type_get_type()) +#define PURPLE_TYPE_MEDIA_NETWORK_PROTOCOL (purple_media_network_protocol_get_type()) +#define PURPLE_MEDIA_TYPE_STATE (purple_media_state_changed_get_type()) +#define PURPLE_MEDIA_TYPE_INFO_TYPE (purple_media_info_type_get_type()) + +/** @copydoc _PurpleMedia */ +typedef struct _PurpleMedia PurpleMedia; +/** @copydoc _PurpleMediaCandidate */ +typedef struct _PurpleMediaCandidate PurpleMediaCandidate; +/** @copydoc _PurpleMediaCodec */ +typedef struct _PurpleMediaCodec PurpleMediaCodec; + +/** Media caps */ +typedef enum { + PURPLE_MEDIA_CAPS_NONE = 0, + PURPLE_MEDIA_CAPS_AUDIO = 1, + PURPLE_MEDIA_CAPS_AUDIO_SINGLE_DIRECTION = 1 << 1, + PURPLE_MEDIA_CAPS_VIDEO = 1 << 2, + PURPLE_MEDIA_CAPS_VIDEO_SINGLE_DIRECTION = 1 << 3, + PURPLE_MEDIA_CAPS_AUDIO_VIDEO = 1 << 4, + PURPLE_MEDIA_CAPS_MODIFY_SESSION = 1 << 5, + PURPLE_MEDIA_CAPS_CHANGE_DIRECTION = 1 << 6, +} PurpleMediaCaps; + +/** Media session types */ +typedef enum { + PURPLE_MEDIA_NONE = 0, + PURPLE_MEDIA_RECV_AUDIO = 1 << 0, + PURPLE_MEDIA_SEND_AUDIO = 1 << 1, + PURPLE_MEDIA_RECV_VIDEO = 1 << 2, + PURPLE_MEDIA_SEND_VIDEO = 1 << 3, + PURPLE_MEDIA_AUDIO = PURPLE_MEDIA_RECV_AUDIO | PURPLE_MEDIA_SEND_AUDIO, + PURPLE_MEDIA_VIDEO = PURPLE_MEDIA_RECV_VIDEO | PURPLE_MEDIA_SEND_VIDEO +} PurpleMediaSessionType; + +/** Media state-changed types */ +typedef enum { + PURPLE_MEDIA_STATE_NEW = 0, + PURPLE_MEDIA_STATE_CONNECTED, + PURPLE_MEDIA_STATE_END, +} PurpleMediaState; + +/** Media info types */ +typedef enum { + PURPLE_MEDIA_INFO_HANGUP = 0, + PURPLE_MEDIA_INFO_ACCEPT, + PURPLE_MEDIA_INFO_REJECT, + PURPLE_MEDIA_INFO_MUTE, + PURPLE_MEDIA_INFO_UNMUTE, + PURPLE_MEDIA_INFO_HOLD, + PURPLE_MEDIA_INFO_UNHOLD, +} PurpleMediaInfoType; + +typedef enum { + PURPLE_MEDIA_CANDIDATE_TYPE_HOST, + PURPLE_MEDIA_CANDIDATE_TYPE_SRFLX, + PURPLE_MEDIA_CANDIDATE_TYPE_PRFLX, + PURPLE_MEDIA_CANDIDATE_TYPE_RELAY, + PURPLE_MEDIA_CANDIDATE_TYPE_MULTICAST, +} PurpleMediaCandidateType; + +typedef enum { + PURPLE_MEDIA_COMPONENT_NONE = 0, + PURPLE_MEDIA_COMPONENT_RTP = 1, + PURPLE_MEDIA_COMPONENT_RTCP = 2, +} PurpleMediaComponentType; + +typedef enum { + PURPLE_MEDIA_NETWORK_PROTOCOL_UDP, + PURPLE_MEDIA_NETWORK_PROTOCOL_TCP, +} PurpleMediaNetworkProtocol; + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Gets the media session type's GType + * + * @return The media session type's GType. + */ +GType purple_media_session_type_get_type(void); + +/** + * Gets the media candidate type's GType + * + * @return The media candidate type's GType. + */ +GType purple_media_candidate_type_get_type(void); + +/** + * Gets the media network protocol's GType + * + * @return The media network protocol's GType. + */ +GType purple_media_network_protocol_get_type(void); + +/** + * Gets the media class's GType + * + * @return The media class's GType. + */ +GType purple_media_get_type(void); + +/** + * Gets the type of the state-changed enum + * + * @return The state-changed enum's GType + */ +GType purple_media_state_changed_get_type(void); + +/** + * Gets the type of the info type enum + * + * @return The info type enum's GType + */ +GType purple_media_info_type_get_type(void); + +/** + * Gets the type of the media candidate structure. + * + * @return The media canditate's GType + */ +GType purple_media_candidate_get_type(void); + +/** + * Creates a PurpleMediaCandidate instance. + * + * @param foundation The foundation of the candidate. + * @param component_id The component this candidate is for. + * @param type The type of candidate. + * @param proto The protocol this component is for. + * @param ip The IP address of this component. + * @param port The network port. + * + * @return The newly created PurpleMediaCandidate instance. + */ +PurpleMediaCandidate *purple_media_candidate_new( + const gchar *foundation, guint component_id, + PurpleMediaCandidateType type, + PurpleMediaNetworkProtocol proto, + const gchar *ip, guint port); + +/** + * Copies a GList of PurpleMediaCandidate and its contents. + * + * @param candidates The list of candidates to be copied. + * + * @return The copy of the GList. + */ +GList *purple_media_candidate_list_copy(GList *candidates); + +/** + * Frees a GList of PurpleMediaCandidate and its contents. + * + * @param candidates The list of candidates to be freed. + */ +void purple_media_candidate_list_free(GList *candidates); + +gchar *purple_media_candidate_get_foundation(PurpleMediaCandidate *candidate); +guint purple_media_candidate_get_component_id(PurpleMediaCandidate *candidate); +gchar *purple_media_candidate_get_ip(PurpleMediaCandidate *candidate); +guint16 purple_media_candidate_get_port(PurpleMediaCandidate *candidate); +gchar *purple_media_candidate_get_base_ip(PurpleMediaCandidate *candidate); +guint16 purple_media_candidate_get_base_port(PurpleMediaCandidate *candidate); +PurpleMediaNetworkProtocol purple_media_candidate_get_protocol( + PurpleMediaCandidate *candidate); +guint32 purple_media_candidate_get_priority(PurpleMediaCandidate *candidate); +PurpleMediaCandidateType purple_media_candidate_get_candidate_type( + PurpleMediaCandidate *candidate); +gchar *purple_media_candidate_get_username(PurpleMediaCandidate *candidate); +gchar *purple_media_candidate_get_password(PurpleMediaCandidate *candidate); +guint purple_media_candidate_get_ttl(PurpleMediaCandidate *candidate); + +/** + * Gets the type of the media codec structure. + * + * @return The media codec's GType + */ +GType purple_media_codec_get_type(void); + +/** + * Creates a new PurpleMediaCodec instance. + * + * @param id Codec identifier. + * @param encoding_name Name of the media type this encodes. + * @param media_type PurpleMediaSessionType of this codec. + * @param clock_rate The clock rate this codec encodes at, if applicable. + * + * @return The newly created PurpleMediaCodec. + */ +PurpleMediaCodec *purple_media_codec_new(int id, const char *encoding_name, + PurpleMediaSessionType media_type, guint clock_rate); + +guint purple_media_codec_get_id(PurpleMediaCodec *codec); +gchar *purple_media_codec_get_encoding_name(PurpleMediaCodec *codec); +guint purple_media_codec_get_clock_rate(PurpleMediaCodec *codec); +guint purple_media_codec_get_channels(PurpleMediaCodec *codec); +GList *purple_media_codec_get_optional_parameters(PurpleMediaCodec *codec); + +/** + * Creates a string representation of the codec. + * + * @param codec The codec to create the string of. + * + * @return The new string representation. + */ +gchar *purple_media_codec_to_string(const PurpleMediaCodec *codec); + +/** + * Adds an optional parameter to the codec. + * + * @param codec The codec to add the parameter to. + * @param name The name of the parameter to add. + * @param value The value of the parameter to add. + */ +void purple_media_codec_add_optional_parameter(PurpleMediaCodec *codec, + const gchar *name, const gchar *value); + +/** + * Removes an optional parameter from the codec. + * + * @param codec The codec to remove the parameter from. + * @param param A pointer to the parameter to remove. + */ +void purple_media_codec_remove_optional_parameter(PurpleMediaCodec *codec, + PurpleKeyValuePair *param); + +/** + * Gets an optional parameter based on the values given. + * + * @param codec The codec to find the parameter in. + * @param name The name of the parameter to search for. + * @param value The value to search for or NULL. + * + * @return The value found or NULL. + */ +PurpleKeyValuePair *purple_media_codec_get_optional_parameter( + PurpleMediaCodec *codec, const gchar *name, + const gchar *value); + +/** + * Copies a GList of PurpleMediaCodec and its contents. + * + * @param codecs The list of codecs to be copied. + * + * @return The copy of the GList. + */ +GList *purple_media_codec_list_copy(GList *codecs); + +/** + * Frees a GList of PurpleMediaCodec and its contents. + * + * @param codecs The list of codecs to be freed. + */ +void purple_media_codec_list_free(GList *codecs); + +/** + * Gets a list of session names. + * + * @param media The media session to retrieve session names from. + * + * @return GList of session names. + */ +GList *purple_media_get_session_names(PurpleMedia *media); + +/** + * Gets the PurpleConnection this media session is on. + * + * @param media The media session to retrieve the connection from. + * + * @return The connection retrieved. + */ +PurpleConnection *purple_media_get_connection(PurpleMedia *media); + +/** + * Gets the prpl data from the media session. + * + * @param media The media session to retrieve the prpl data from. + * + * @return The prpl data retrieved. + */ +gpointer purple_media_get_prpl_data(PurpleMedia *media); + +/** + * Sets the prpl data on the media session. + * + * @param media The media session to set the prpl data on. + * @param prpl_data The data to set on the media session. + */ +void purple_media_set_prpl_data(PurpleMedia *media, gpointer prpl_data); + +/** + * Signals an error in the media session. + * + * @param media The media object to set the state on. + * @param error The format of the error message to send in the signal. + * @param ... The arguments to plug into the format. + */ +void purple_media_error(PurpleMedia *media, const gchar *error, ...); + +/** + * Ends all streams that match the given parameters + * + * @param media The media object with which to end streams. + * @param session_id The session to end streams on. + * @param participant The participant to end streams with. + */ +void purple_media_end(PurpleMedia *media, const gchar *session_id, + const gchar *participant); + +/** + * Signals different information about the given stream. + * + * @param media The media instance to containing the stream to signal. + * @param type The type of info being signaled. + * @param session_id The id of the session of the stream being signaled. + * @param participant The participant of the stream being signaled. + * @param local TRUE if the info originated locally, FALSE if on the remote end. + */ +void purple_media_stream_info(PurpleMedia *media, PurpleMediaInfoType type, + const gchar *session_id, const gchar *participant, + gboolean local); + +/** + * Adds a stream to a session. + * + * It only adds a stream to one audio session or video session as + * the @c sess_id must be unique between sessions. + * + * @param media The media object to find the session in. + * @param sess_id The session id of the session to add the stream to. + * @param who The name of the remote user to add the stream for. + * @param type The type of stream to create. + * @param initiator Whether or not the local user initiated the stream. + * @param transmitter The transmitter to use for the stream. + * @param num_params The number of parameters to pass to Farsight. + * @param params The parameters to pass to Farsight. + * + * @return @c TRUE The stream was added successfully, @c FALSE otherwise. + */ +gboolean purple_media_add_stream(PurpleMedia *media, const gchar *sess_id, + const gchar *who, PurpleMediaSessionType type, + gboolean initiator, const gchar *transmitter, + guint num_params, GParameter *params); + +/** + * Gets the session type from a session + * + * @param media The media object to find the session in. + * @param sess_id The session id of the session to get the type from. + * + * @return The retreived session type. + */ +PurpleMediaSessionType purple_media_get_session_type(PurpleMedia *media, const gchar *sess_id); + +/** + * Gets the PurpleMediaManager this media session is a part of. + * + * @param media The media object to get the manager instance from. + * + * @return The PurpleMediaManager instance retrieved. + */ +struct _PurpleMediaManager *purple_media_get_manager(PurpleMedia *media); + +/** + * Gets the codecs from a session. + * + * @param media The media object to find the session in. + * @param sess_id The session id of the session to get the codecs from. + * + * @return The retreieved codecs. + */ +GList *purple_media_get_codecs(PurpleMedia *media, const gchar *sess_id); + +/** + * Adds remote candidates to the stream. + * + * @param media The media object to find the session in. + * @param sess_id The session id of the session find the stream in. + * @param name The name of the remote user to add the candidates for. + * @param remote_candidates The remote candidates to add. + */ +void purple_media_add_remote_candidates(PurpleMedia *media, + const gchar *sess_id, + const gchar *name, + GList *remote_candidates); + +/** + * Gets the local candidates from a stream. + * + * @param media The media object to find the session in. + * @param sess_id The session id of the session to find the stream in. + * @param name The name of the remote user to get the candidates from. + */ +GList *purple_media_get_local_candidates(PurpleMedia *media, + const gchar *sess_id, + const gchar *name); + +#if 0 +/* + * These two functions aren't being used and I'd rather not lock in the API + * until they are needed. If they ever are. + */ + +/** + * Gets the active local candidates for the stream. + * + * @param media The media object to find the session in. + * @param sess_id The session id of the session to find the stream in. + * @param name The name of the remote user to get the active candidate from. + * + * @return The active candidates retrieved. + */ +GList *purple_media_get_active_local_candidates(PurpleMedia *media, + const gchar *sess_id, const gchar *name); + +/** + * Gets the active remote candidates for the stream. + * + * @param media The media object to find the session in. + * @param sess_id The session id of the session to find the stream in. + * @param name The name of the remote user to get the remote candidate from. + * + * @return The remote candidates retrieved. + */ +GList *purple_media_get_active_remote_candidates(PurpleMedia *media, + const gchar *sess_id, const gchar *name); +#endif + +/** + * Sets remote candidates from the stream. + * + * @param media The media object to find the session in. + * @param sess_id The session id of the session find the stream in. + * @param name The name of the remote user to get the candidates from. + * + * @return @c TRUE The codecs were set successfully, or @c FALSE otherwise. + */ +gboolean purple_media_set_remote_codecs(PurpleMedia *media, const gchar *sess_id, + const gchar *name, GList *codecs); + +/** + * Returns whether or not the candidates for set of streams are prepared + * + * @param media The media object to find the remote user in. + * @param session_id The session id of the session to check. + * @param participant The remote user to check for. + * + * @return @c TRUE All streams for the given session_id/participant combination have candidates prepared, @c FALSE otherwise. + */ +gboolean purple_media_candidates_prepared(PurpleMedia *media, + const gchar *session_id, const gchar *participant); + +/** + * Sets the send codec for the a session. + * + * @param media The media object to find the session in. + * @param sess_id The session id of the session to set the codec for. + * @param codec The codec to set the session to stream. + * + * @return @c TRUE The codec was successfully changed, or @c FALSE otherwise. + */ +gboolean purple_media_set_send_codec(PurpleMedia *media, const gchar *sess_id, PurpleMediaCodec *codec); + +/** + * Gets whether a session's codecs are ready to be used. + * + * @param media The media object to find the session in. + * @param sess_id The session id of the session to check. + * + * @return @c TRUE The codecs are ready, or @c FALSE otherwise. + */ +gboolean purple_media_codecs_ready(PurpleMedia *media, const gchar *sess_id); + +/** + * Gets whether the local user is the conference/session/stream's initiator. + * + * @param media The media instance to find the session in. + * @param sess_id The session id of the session to check. + * @param participant The participant of the stream to check. + * + * @return TRUE if the local user is the stream's initator, else FALSE. + */ +gboolean purple_media_is_initiator(PurpleMedia *media, + const gchar *sess_id, const gchar *participant); + +/** + * Gets whether a streams selected have been accepted. + * + * @param media The media object to find the session in. + * @param sess_id The session id of the session to check. + * @param participant The participant to check. + * + * @return @c TRUE The selected streams have been accepted, or @c FALSE otherwise. + */ +gboolean purple_media_accepted(PurpleMedia *media, const gchar *sess_id, + const gchar *participant); + +/** + * Sets the input volume of all the selected sessions. + * + * @param media The media object the sessions are in. + * @param session_id The session to select (if any). + * @param level The level to set the volume to. + */ +void purple_media_set_input_volume(PurpleMedia *media, const gchar *session_id, double level); + +/** + * Sets the output volume of all the selected streams. + * + * @param media The media object the streams are in. + * @param session_id The session to limit the streams to (if any). + * @param participant The participant to limit the streams to (if any). + * @param level The level to set the volume to. + */ +void purple_media_set_output_volume(PurpleMedia *media, const gchar *session_id, + const gchar *participant, double level); + +/** + * Sets a video output window for the given session/stream. + * + * @param media The media instance to set the output window on. + * @param session_id The session to set the output window on. + * @param participant Optionally, the participant to set the output window on. + * @param window_id The window id use for embedding the video in. + * + * @return An id to reference the output window. + */ +gulong purple_media_set_output_window(PurpleMedia *media, + const gchar *session_id, const gchar *participant, + gulong window_id); + +/** + * Removes all output windows from a given media session. + * + * @param media The instance to remove all output windows from. + */ +void purple_media_remove_output_windows(PurpleMedia *media); + +#ifdef __cplusplus +} +#endif + +G_END_DECLS + +#endif /* __MEDIA_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libpurple/mediamanager.c Fri Apr 03 16:45:38 2009 +0000 @@ -0,0 +1,1106 @@ +/** + * @file mediamanager.c Media Manager API + * @ingroup core + */ + +/* purple + * + * Purple is the legal property of its developers, whose names are too numerous + * to list here. Please refer to the COPYRIGHT file distributed with this + * source distribution. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "internal.h" + +#include "connection.h" +#include "debug.h" +#include "marshallers.h" +#include "media.h" +#include "media-gst.h" +#include "mediamanager.h" + +#ifdef USE_VV + +#include <gst/farsight/fs-conference-iface.h> +#include <gst/interfaces/xoverlay.h> + +/** @copydoc _PurpleMediaManagerPrivate */ +typedef struct _PurpleMediaManagerPrivate PurpleMediaManagerPrivate; +/** @copydoc _PurpleMediaOutputWindow */ +typedef struct _PurpleMediaOutputWindow PurpleMediaOutputWindow; +/** @copydoc _PurpleMediaManagerPrivate */ +typedef struct _PurpleMediaElementInfoPrivate PurpleMediaElementInfoPrivate; + +/** The media manager class. */ +struct _PurpleMediaManagerClass +{ + GObjectClass parent_class; /**< The parent class. */ +}; + +/** The media manager's data. */ +struct _PurpleMediaManager +{ + GObject parent; /**< The parent of this manager. */ + PurpleMediaManagerPrivate *priv; /**< Private data for the manager. */ +}; + +struct _PurpleMediaOutputWindow +{ + gulong id; + PurpleMedia *media; + gchar *session_id; + gchar *participant; + gulong window_id; + GstElement *sink; +}; +#endif + +struct _PurpleMediaManagerPrivate +{ + GstElement *pipeline; + PurpleMediaCaps ui_caps; + GList *medias; + GList *elements; + GList *output_windows; + gulong next_output_window_id; + + PurpleMediaElementInfo *video_src; + PurpleMediaElementInfo *video_sink; + PurpleMediaElementInfo *audio_src; + PurpleMediaElementInfo *audio_sink; +}; + +#define PURPLE_MEDIA_MANAGER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), PURPLE_TYPE_MEDIA_MANAGER, PurpleMediaManagerPrivate)) +#define PURPLE_MEDIA_ELEMENT_INFO_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), PURPLE_TYPE_MEDIA_ELEMENT_INFO, PurpleMediaElementInfoPrivate)) + +static void purple_media_manager_class_init (PurpleMediaManagerClass *klass); +static void purple_media_manager_init (PurpleMediaManager *media); +static void purple_media_manager_finalize (GObject *object); + +static GObjectClass *parent_class = NULL; + + + +enum { + INIT_MEDIA, + LAST_SIGNAL +}; +static guint purple_media_manager_signals[LAST_SIGNAL] = {0}; + +GType +purple_media_manager_get_type() +{ + static GType type = 0; + + if (type == 0) { + static const GTypeInfo info = { + sizeof(PurpleMediaManagerClass), + NULL, + NULL, + (GClassInitFunc) purple_media_manager_class_init, + NULL, + NULL, + sizeof(PurpleMediaManager), + 0, + (GInstanceInitFunc) purple_media_manager_init, + NULL + }; + type = g_type_register_static(G_TYPE_OBJECT, "PurpleMediaManager", &info, 0); + } + return type; +} + + +static void +purple_media_manager_class_init (PurpleMediaManagerClass *klass) +{ + GObjectClass *gobject_class = (GObjectClass*)klass; + parent_class = g_type_class_peek_parent(klass); + + gobject_class->finalize = purple_media_manager_finalize; + + purple_media_manager_signals[INIT_MEDIA] = g_signal_new ("init-media", + G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, + 0, NULL, NULL, + purple_smarshal_BOOLEAN__OBJECT_POINTER_STRING, + G_TYPE_BOOLEAN, 3, PURPLE_TYPE_MEDIA, + G_TYPE_POINTER, G_TYPE_STRING); + g_type_class_add_private(klass, sizeof(PurpleMediaManagerPrivate)); +} + +static void +purple_media_manager_init (PurpleMediaManager *media) +{ + media->priv = PURPLE_MEDIA_MANAGER_GET_PRIVATE(media); + media->priv->medias = NULL; + media->priv->next_output_window_id = 1; +} + +static void +purple_media_manager_finalize (GObject *media) +{ + PurpleMediaManagerPrivate *priv = PURPLE_MEDIA_MANAGER_GET_PRIVATE(media); + for (; priv->medias; priv->medias = + g_list_delete_link(priv->medias, priv->medias)) { + g_object_unref(priv->medias->data); + } + for (; priv->elements; priv->elements = + g_list_delete_link(priv->elements, priv->elements)) { + g_object_unref(priv->elements->data); + } + parent_class->finalize(media); +} + +PurpleMediaManager * +purple_media_manager_get() +{ +#ifdef USE_VV + static PurpleMediaManager *manager = NULL; + + if (manager == NULL) + manager = PURPLE_MEDIA_MANAGER(g_object_new(purple_media_manager_get_type(), NULL)); + return manager; +#else + return NULL; +#endif +} + +#ifdef USE_VV +static gboolean +pipeline_bus_call(GstBus *bus, GstMessage *msg, PurpleMediaManager *manager) +{ + switch(GST_MESSAGE_TYPE(msg)) { + case GST_MESSAGE_EOS: + purple_debug_info("mediamanager", "End of Stream\n"); + break; + case GST_MESSAGE_ERROR: { + gchar *debug = NULL; + GError *err = NULL; + + gst_message_parse_error(msg, &err, &debug); + + purple_debug_error("mediamanager", + "gst pipeline error: %s\n", + err->message); + g_error_free(err); + + if (debug) { + purple_debug_error("mediamanager", + "Debug details: %s\n", debug); + g_free (debug); + } + break; + } + default: + break; + } + return TRUE; +} +#endif + +GstElement * +purple_media_manager_get_pipeline(PurpleMediaManager *manager) +{ +#ifdef USE_VV + g_return_val_if_fail(PURPLE_IS_MEDIA_MANAGER(manager), NULL); + + if (manager->priv->pipeline == NULL) { + GstBus *bus; + manager->priv->pipeline = gst_pipeline_new(NULL); + + bus = gst_pipeline_get_bus( + GST_PIPELINE(manager->priv->pipeline)); + gst_bus_add_signal_watch(GST_BUS(bus)); + g_signal_connect(G_OBJECT(bus), "message", + G_CALLBACK(pipeline_bus_call), manager); + gst_bus_set_sync_handler(bus, + gst_bus_sync_signal_handler, NULL); + gst_object_unref(bus); + + gst_element_set_state(manager->priv->pipeline, + GST_STATE_PLAYING); + } + + return manager->priv->pipeline; +#else + return NULL; +#endif +} + +PurpleMedia * +purple_media_manager_create_media(PurpleMediaManager *manager, + PurpleConnection *gc, + const char *conference_type, + const char *remote_user, + gboolean initiator) +{ +#ifdef USE_VV + PurpleMedia *media; + FsConference *conference = FS_CONFERENCE(gst_element_factory_make(conference_type, NULL)); + GstStateChangeReturn ret; + gboolean signal_ret; + + if (conference == NULL) { + purple_conv_present_error(remote_user, + purple_connection_get_account(gc), + _("Error creating conference.")); + purple_debug_error("media", "Conference == NULL\n"); + return NULL; + } + + media = PURPLE_MEDIA(g_object_new(purple_media_get_type(), + "manager", manager, + "connection", gc, + "conference", conference, + "initiator", initiator, + NULL)); + + ret = gst_element_set_state(GST_ELEMENT(conference), GST_STATE_PLAYING); + + if (ret == GST_STATE_CHANGE_FAILURE) { + purple_conv_present_error(remote_user, + purple_connection_get_account(gc), + _("Error creating conference.")); + purple_debug_error("media", "Failed to start conference.\n"); + g_object_unref(media); + return NULL; + } + + g_signal_emit(manager, purple_media_manager_signals[INIT_MEDIA], 0, + media, gc, remote_user, &signal_ret); + + if (signal_ret == FALSE) { + g_object_unref(media); + return NULL; + } + + manager->priv->medias = g_list_append(manager->priv->medias, media); + return media; +#else + return NULL; +#endif +} + +GList * +purple_media_manager_get_media(PurpleMediaManager *manager) +{ +#ifdef USE_VV + return manager->priv->medias; +#else + return NULL; +#endif +} + +GList * +purple_media_manager_get_media_by_connection(PurpleMediaManager *manager, + PurpleConnection *pc) +{ +#ifdef USE_VV + GList *media = NULL; + GList *iter; + + g_return_val_if_fail(PURPLE_IS_MEDIA_MANAGER(manager), NULL); + + iter = manager->priv->medias; + for (; iter; iter = g_list_next(iter)) { + if (purple_media_get_connection(iter->data) == pc) { + media = g_list_prepend(media, iter->data); + } + } + + return media; +#else + return NULL; +#endif +} + +void +purple_media_manager_remove_media(PurpleMediaManager *manager, + PurpleMedia *media) +{ +#ifdef USE_VV + GList *list = g_list_find(manager->priv->medias, media); + if (list) + manager->priv->medias = + g_list_delete_link(manager->priv->medias, list); +#endif +} + +#ifdef USE_VV +static void +request_pad_unlinked_cb(GstPad *pad, GstPad *peer, gpointer user_data) +{ + GstElement *parent = GST_ELEMENT_PARENT(pad); + GstIterator *iter; + GstPad *remaining_pad; + + gst_element_release_request_pad(GST_ELEMENT_PARENT(pad), pad); + iter = gst_element_iterate_pads(parent); + + if (gst_iterator_next(iter, (gpointer)&remaining_pad) + == GST_ITERATOR_DONE) { + gst_element_set_locked_state(parent, TRUE); + gst_element_set_state(parent, GST_STATE_NULL); + gst_bin_remove(GST_BIN(GST_ELEMENT_PARENT(parent)), parent); + } + + gst_iterator_free(iter); +} +#endif + +GstElement * +purple_media_manager_get_element(PurpleMediaManager *manager, + PurpleMediaSessionType type, PurpleMedia *media, + const gchar *session_id, const gchar *participant) +{ +#ifdef USE_VV + GstElement *ret = NULL; + PurpleMediaElementInfo *info = NULL; + PurpleMediaElementType element_type; + + if (type & PURPLE_MEDIA_SEND_AUDIO) + info = manager->priv->audio_src; + else if (type & PURPLE_MEDIA_RECV_AUDIO) + info = manager->priv->audio_sink; + else if (type & PURPLE_MEDIA_SEND_VIDEO) + info = manager->priv->video_src; + else if (type & PURPLE_MEDIA_RECV_VIDEO) + info = manager->priv->video_sink; + + if (info == NULL) + return NULL; + + element_type = purple_media_element_info_get_element_type(info); + + if (element_type & PURPLE_MEDIA_ELEMENT_UNIQUE && + element_type & PURPLE_MEDIA_ELEMENT_SRC) { + GstElement *tee; + GstPad *pad; + GstPad *ghost; + gchar *id = purple_media_element_info_get_id(info); + + ret = gst_bin_get_by_name(GST_BIN( + purple_media_manager_get_pipeline( + manager)), id); + + if (ret == NULL) { + GstElement *bin, *fakesink; + ret = purple_media_element_info_call_create(info, + media, session_id, participant); + bin = gst_bin_new(id); + tee = gst_element_factory_make("tee", "tee"); + gst_bin_add_many(GST_BIN(bin), ret, tee, NULL); + gst_element_link(ret, tee); + + /* + * This shouldn't be necessary, but it stops it from + * giving a not-linked error upon destruction + */ + fakesink = gst_element_factory_make("fakesink", NULL); + g_object_set(fakesink, "sync", FALSE, NULL); + gst_bin_add(GST_BIN(bin), fakesink); + gst_element_link(tee, fakesink); + + ret = bin; + gst_element_set_locked_state(ret, TRUE); + gst_object_ref(ret); + gst_bin_add(GST_BIN(purple_media_manager_get_pipeline( + manager)), ret); + } + g_free(id); + + tee = gst_bin_get_by_name(GST_BIN(ret), "tee"); + pad = gst_element_get_request_pad(tee, "src%d"); + gst_object_unref(tee); + ghost = gst_ghost_pad_new(NULL, pad); + gst_object_unref(pad); + g_signal_connect(GST_PAD(ghost), "unlinked", + G_CALLBACK(request_pad_unlinked_cb), NULL); + gst_pad_set_active(ghost, TRUE); + gst_element_add_pad(ret, ghost); + } else { + ret = purple_media_element_info_call_create(info, + media, session_id, participant); + } + + if (ret == NULL) + purple_debug_error("media", "Error creating source or sink\n"); + + return ret; +#else + return NULL; +#endif +} + +PurpleMediaElementInfo * +purple_media_manager_get_element_info(PurpleMediaManager *manager, + const gchar *id) +{ +#ifdef USE_VV + GList *iter; + + g_return_val_if_fail(PURPLE_IS_MEDIA_MANAGER(manager), NULL); + + iter = manager->priv->elements; + + for (; iter; iter = g_list_next(iter)) { + gchar *element_id = + purple_media_element_info_get_id(iter->data); + if (!strcmp(element_id, id)) { + g_free(element_id); + g_object_ref(iter->data); + return iter->data; + } + g_free(element_id); + } +#endif + + return NULL; +} + +gboolean +purple_media_manager_register_element(PurpleMediaManager *manager, + PurpleMediaElementInfo *info) +{ +#ifdef USE_VV + PurpleMediaElementInfo *info2; + gchar *id; + + g_return_val_if_fail(PURPLE_IS_MEDIA_MANAGER(manager), FALSE); + g_return_val_if_fail(info != NULL, FALSE); + + id = purple_media_element_info_get_id(info); + info2 = purple_media_manager_get_element_info(manager, id); + g_free(id); + + if (info2 != NULL) { + g_object_unref(info2); + return FALSE; + } + g_object_unref(info2); + + manager->priv->elements = + g_list_prepend(manager->priv->elements, info); + return TRUE; +#else + return FALSE; +#endif +} + +gboolean +purple_media_manager_unregister_element(PurpleMediaManager *manager, + const gchar *id) +{ +#ifdef USE_VV + PurpleMediaElementInfo *info; + + g_return_val_if_fail(PURPLE_IS_MEDIA_MANAGER(manager), FALSE); + + info = purple_media_manager_get_element_info(manager, id); + + if (info == NULL) { + g_object_unref(info); + return FALSE; + } + + if (manager->priv->audio_src == info) + manager->priv->audio_src = NULL; + if (manager->priv->audio_sink == info) + manager->priv->audio_sink = NULL; + if (manager->priv->video_src == info) + manager->priv->video_src = NULL; + if (manager->priv->video_sink == info) + manager->priv->video_sink = NULL; + + manager->priv->elements = g_list_remove( + manager->priv->elements, info); + g_object_unref(info); + return TRUE; +#else + return FALSE; +#endif +} + +gboolean +purple_media_manager_set_active_element(PurpleMediaManager *manager, + PurpleMediaElementInfo *info) +{ +#ifdef USE_VV + PurpleMediaElementInfo *info2; + PurpleMediaElementType type; + gboolean ret = FALSE; + gchar *id; + + g_return_val_if_fail(PURPLE_IS_MEDIA_MANAGER(manager), FALSE); + g_return_val_if_fail(info != NULL, FALSE); + + id = purple_media_element_info_get_id(info); + info2 = purple_media_manager_get_element_info(manager, id); + g_free(id); + + if (info2 == NULL) + purple_media_manager_register_element(manager, info); + g_object_unref(info2); + + type = purple_media_element_info_get_element_type(info); + + if (type & PURPLE_MEDIA_ELEMENT_SRC) { + if (type & PURPLE_MEDIA_ELEMENT_AUDIO) { + manager->priv->audio_src = info; + ret = TRUE; + } + if (type & PURPLE_MEDIA_ELEMENT_VIDEO) { + manager->priv->video_src = info; + ret = TRUE; + } + } + if (type & PURPLE_MEDIA_ELEMENT_SINK) { + if (type & PURPLE_MEDIA_ELEMENT_AUDIO) { + manager->priv->audio_sink = info; + ret = TRUE; + } + if (type & PURPLE_MEDIA_ELEMENT_VIDEO) { + manager->priv->video_sink = info; + ret = TRUE; + } + } + + return ret; +#else + return FALSE; +#endif +} + +PurpleMediaElementInfo * +purple_media_manager_get_active_element(PurpleMediaManager *manager, + PurpleMediaElementType type) +{ +#ifdef USE_VV + g_return_val_if_fail(PURPLE_IS_MEDIA_MANAGER(manager), NULL); + + if (type & PURPLE_MEDIA_ELEMENT_SRC) { + if (type & PURPLE_MEDIA_ELEMENT_AUDIO) + return manager->priv->audio_src; + else if (type & PURPLE_MEDIA_ELEMENT_VIDEO) + return manager->priv->video_src; + } else if (type & PURPLE_MEDIA_ELEMENT_SINK) { + if (type & PURPLE_MEDIA_ELEMENT_AUDIO) + return manager->priv->audio_sink; + else if (type & PURPLE_MEDIA_ELEMENT_VIDEO) + return manager->priv->video_sink; + } +#endif + + return NULL; +} + +#ifdef USE_VV +static void +window_id_cb(GstBus *bus, GstMessage *msg, PurpleMediaOutputWindow *ow) +{ + GstElement *sink; + + if (GST_MESSAGE_TYPE(msg) != GST_MESSAGE_ELEMENT || + !gst_structure_has_name(msg->structure, + "prepare-xwindow-id")) + return; + + sink = GST_ELEMENT(GST_MESSAGE_SRC(msg)); + while (sink != ow->sink) { + if (sink == NULL) + return; + sink = GST_ELEMENT_PARENT(sink); + } + + g_signal_handlers_disconnect_matched(bus, G_SIGNAL_MATCH_FUNC + | G_SIGNAL_MATCH_DATA, 0, 0, NULL, + window_id_cb, ow); + + gst_x_overlay_set_xwindow_id(GST_X_OVERLAY( + GST_MESSAGE_SRC(msg)), ow->window_id); +} +#endif + +gboolean +purple_media_manager_create_output_window(PurpleMediaManager *manager, + PurpleMedia *media, const gchar *session_id, + const gchar *participant) +{ +#ifdef USE_VV + GList *iter; + + g_return_val_if_fail(PURPLE_IS_MEDIA(media), FALSE); + + iter = manager->priv->output_windows; + for(; iter; iter = g_list_next(iter)) { + PurpleMediaOutputWindow *ow = iter->data; + + if (ow->sink == NULL && ow->media == media && + ((participant != NULL && + ow->participant != NULL && + !strcmp(participant, ow->participant)) || + (participant == ow->participant)) && + !strcmp(session_id, ow->session_id)) { + GstBus *bus; + GstElement *queue; + GstElement *tee = purple_media_get_tee(media, + session_id, participant); + + if (tee == NULL) + continue; + + queue = gst_element_factory_make( + "queue", NULL); + ow->sink = purple_media_manager_get_element( + manager, PURPLE_MEDIA_RECV_VIDEO, + ow->media, ow->session_id, + ow->participant); + + if (participant == NULL) { + /* aka this is a preview sink */ + GObjectClass *klass = + G_OBJECT_GET_CLASS(ow->sink); + if (g_object_class_find_property(klass, + "sync")) + g_object_set(G_OBJECT(ow->sink), + "sync", "FALSE", NULL); + if (g_object_class_find_property(klass, + "async")) + g_object_set(G_OBJECT(ow->sink), + "async", FALSE, NULL); + } + + gst_bin_add_many(GST_BIN(GST_ELEMENT_PARENT(tee)), + queue, ow->sink, NULL); + + bus = gst_pipeline_get_bus(GST_PIPELINE( + manager->priv->pipeline)); + g_signal_connect(bus, "sync-message::element", + G_CALLBACK(window_id_cb), ow); + gst_object_unref(bus); + + gst_element_sync_state_with_parent(ow->sink); + gst_element_link(queue, ow->sink); + gst_element_sync_state_with_parent(queue); + gst_element_link(tee, queue); + } + } + return TRUE; +#else + return FALSE; +#endif +} + +gulong +purple_media_manager_set_output_window(PurpleMediaManager *manager, + PurpleMedia *media, const gchar *session_id, + const gchar *participant, gulong window_id) +{ +#ifdef USE_VV + PurpleMediaOutputWindow *output_window; + + g_return_val_if_fail(PURPLE_IS_MEDIA_MANAGER(manager), FALSE); + g_return_val_if_fail(PURPLE_IS_MEDIA(media), FALSE); + + output_window = g_new0(PurpleMediaOutputWindow, 1); + output_window->id = manager->priv->next_output_window_id++; + output_window->media = media; + output_window->session_id = g_strdup(session_id); + output_window->participant = g_strdup(participant); + output_window->window_id = window_id; + + manager->priv->output_windows = g_list_prepend( + manager->priv->output_windows, output_window); + + if (purple_media_get_tee(media, session_id, participant) != NULL) + purple_media_manager_create_output_window(manager, + media, session_id, participant); + + return output_window->id; +#else + return 0; +#endif +} + +gboolean +purple_media_manager_remove_output_window(PurpleMediaManager *manager, + gulong output_window_id) +{ +#ifdef USE_VV + PurpleMediaOutputWindow *output_window = NULL; + GList *iter; + + g_return_val_if_fail(PURPLE_IS_MEDIA_MANAGER(manager), FALSE); + + iter = manager->priv->output_windows; + for (; iter; iter = g_list_next(iter)) { + PurpleMediaOutputWindow *ow = iter->data; + if (ow->id == output_window_id) { + manager->priv->output_windows = g_list_delete_link( + manager->priv->output_windows, iter); + output_window = ow; + break; + } + } + + if (output_window == NULL) + return FALSE; + + if (output_window->sink != NULL) { + GstPad *pad = gst_element_get_static_pad( + output_window->sink, "sink"); + GstPad *peer = gst_pad_get_peer(pad); + GstElement *queue = GST_ELEMENT_PARENT(peer); + gst_object_unref(pad); + pad = gst_element_get_static_pad(queue, "sink"); + peer = gst_pad_get_peer(pad); + gst_object_unref(pad); + gst_element_release_request_pad(GST_ELEMENT_PARENT(peer), peer); + gst_element_set_locked_state(queue, TRUE); + gst_element_set_state(queue, GST_STATE_NULL); + gst_bin_remove(GST_BIN(GST_ELEMENT_PARENT(queue)), queue); + gst_element_set_locked_state(output_window->sink, TRUE); + gst_element_set_state(output_window->sink, GST_STATE_NULL); + gst_bin_remove(GST_BIN(GST_ELEMENT_PARENT(output_window->sink)), + output_window->sink); + } + + g_free(output_window->session_id); + g_free(output_window->participant); + g_free(output_window); + + return TRUE; +#else + return FALSE; +#endif +} + +void +purple_media_manager_remove_output_windows(PurpleMediaManager *manager, + PurpleMedia *media, const gchar *session_id, + const gchar *participant) +{ +#ifdef USE_VV + GList *iter; + + g_return_if_fail(PURPLE_IS_MEDIA(media)); + + iter = manager->priv->output_windows; + + for (; iter;) { + PurpleMediaOutputWindow *ow = iter->data; + iter = g_list_next(iter); + + if (media == ow->media && + ((session_id != NULL && ow->session_id != NULL && + !strcmp(session_id, ow->session_id)) || + (session_id == ow->session_id)) && + ((participant != NULL && ow->participant != NULL && + !strcmp(participant, ow->participant)) || + (participant == ow->participant))) + purple_media_manager_remove_output_window( + manager, ow->id); + } +#endif +} + +void +purple_media_manager_set_ui_caps(PurpleMediaManager *manager, + PurpleMediaCaps caps) +{ +#ifdef USE_VV + g_return_if_fail(PURPLE_IS_MEDIA_MANAGER(manager)); + manager->priv->ui_caps = caps; +#endif +} + +PurpleMediaCaps +purple_media_manager_get_ui_caps(PurpleMediaManager *manager) +{ +#ifdef USE_VV + g_return_val_if_fail(PURPLE_IS_MEDIA_MANAGER(manager), + PURPLE_MEDIA_CAPS_NONE); + return manager->priv->ui_caps; +#else + return PURPLE_CAPS_NONE; +#endif +} + + +/* + * PurpleMediaElementType + */ + +GType +purple_media_element_type_get_type() +{ + static GType type = 0; + if (type == 0) { + static const GFlagsValue values[] = { + { PURPLE_MEDIA_ELEMENT_NONE, + "PURPLE_MEDIA_ELEMENT_NONE", "none" }, + { PURPLE_MEDIA_ELEMENT_AUDIO, + "PURPLE_MEDIA_ELEMENT_AUDIO", "audio" }, + { PURPLE_MEDIA_ELEMENT_VIDEO, + "PURPLE_MEDIA_ELEMENT_VIDEO", "video" }, + { PURPLE_MEDIA_ELEMENT_AUDIO_VIDEO, + "PURPLE_MEDIA_ELEMENT_AUDIO_VIDEO", + "audio-video" }, + { PURPLE_MEDIA_ELEMENT_NO_SRCS, + "PURPLE_MEDIA_ELEMENT_NO_SRCS", "no-srcs" }, + { PURPLE_MEDIA_ELEMENT_ONE_SRC, + "PURPLE_MEDIA_ELEMENT_ONE_SRC", "one-src" }, + { PURPLE_MEDIA_ELEMENT_MULTI_SRC, + "PURPLE_MEDIA_ELEMENT_MULTI_SRC", + "multi-src" }, + { PURPLE_MEDIA_ELEMENT_REQUEST_SRC, + "PURPLE_MEDIA_ELEMENT_REQUEST_SRC", + "request-src" }, + { PURPLE_MEDIA_ELEMENT_NO_SINKS, + "PURPLE_MEDIA_ELEMENT_NO_SINKS", "no-sinks" }, + { PURPLE_MEDIA_ELEMENT_ONE_SINK, + "PURPLE_MEDIA_ELEMENT_ONE_SINK", "one-sink" }, + { PURPLE_MEDIA_ELEMENT_MULTI_SINK, + "PURPLE_MEDIA_ELEMENT_MULTI_SINK", + "multi-sink" }, + { PURPLE_MEDIA_ELEMENT_REQUEST_SINK, + "PURPLE_MEDIA_ELEMENT_REQUEST_SINK", + "request-sink" }, + { PURPLE_MEDIA_ELEMENT_UNIQUE, + "PURPLE_MEDIA_ELEMENT_UNIQUE", "unique" }, + { PURPLE_MEDIA_ELEMENT_SRC, + "PURPLE_MEDIA_ELEMENT_SRC", "src" }, + { PURPLE_MEDIA_ELEMENT_SINK, + "PURPLE_MEDIA_ELEMENT_SINK", "sink" }, + { 0, NULL, NULL } + }; + type = g_flags_register_static( + "PurpleMediaElementType", values); + } + return type; +} + +/* + * PurpleMediaElementInfo + */ + +struct _PurpleMediaElementInfoClass +{ + GObjectClass parent_class; +}; + +struct _PurpleMediaElementInfo +{ + GObject parent; +}; + +struct _PurpleMediaElementInfoPrivate +{ + gchar *id; + gchar *name; + PurpleMediaElementType type; + PurpleMediaElementCreateCallback create; +}; + +enum { + PROP_0, + PROP_ID, + PROP_NAME, + PROP_TYPE, + PROP_CREATE_CB, +}; + +static void +purple_media_element_info_init(PurpleMediaElementInfo *info) +{ + PurpleMediaElementInfoPrivate *priv = + PURPLE_MEDIA_ELEMENT_INFO_GET_PRIVATE(info); + priv->id = NULL; + priv->name = NULL; + priv->type = PURPLE_MEDIA_ELEMENT_NONE; + priv->create = NULL; +} + +static void +purple_media_element_info_finalize(GObject *info) +{ + PurpleMediaElementInfoPrivate *priv = + PURPLE_MEDIA_ELEMENT_INFO_GET_PRIVATE(info); + g_free(priv->id); + g_free(priv->name); +} + +static void +purple_media_element_info_set_property (GObject *object, guint prop_id, + const GValue *value, GParamSpec *pspec) +{ + PurpleMediaElementInfoPrivate *priv; + g_return_if_fail(PURPLE_IS_MEDIA_ELEMENT_INFO(object)); + + priv = PURPLE_MEDIA_ELEMENT_INFO_GET_PRIVATE(object); + + switch (prop_id) { + case PROP_ID: + g_free(priv->id); + priv->id = g_value_dup_string(value); + break; + case PROP_NAME: + g_free(priv->name); + priv->name = g_value_dup_string(value); + break; + case PROP_TYPE: { + priv->type = g_value_get_flags(value); + break; + } + case PROP_CREATE_CB: + priv->create = g_value_get_pointer(value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID( + object, prop_id, pspec); + break; + } +} + +static void +purple_media_element_info_get_property (GObject *object, guint prop_id, + GValue *value, GParamSpec *pspec) +{ + PurpleMediaElementInfoPrivate *priv; + g_return_if_fail(PURPLE_IS_MEDIA_ELEMENT_INFO(object)); + + priv = PURPLE_MEDIA_ELEMENT_INFO_GET_PRIVATE(object); + + switch (prop_id) { + case PROP_ID: + g_value_set_string(value, priv->id); + break; + case PROP_NAME: + g_value_set_string(value, priv->name); + break; + case PROP_TYPE: + g_value_set_flags(value, priv->type); + break; + case PROP_CREATE_CB: + g_value_set_pointer(value, priv->create); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID( + object, prop_id, pspec); + break; + } +} + +static void +purple_media_element_info_class_init(PurpleMediaElementInfoClass *klass) +{ + GObjectClass *gobject_class = (GObjectClass*)klass; + + gobject_class->finalize = purple_media_element_info_finalize; + gobject_class->set_property = purple_media_element_info_set_property; + gobject_class->get_property = purple_media_element_info_get_property; + + g_object_class_install_property(gobject_class, PROP_ID, + g_param_spec_string("id", + "ID", + "The unique identifier of the element.", + NULL, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_NAME, + g_param_spec_string("name", + "Name", + "The friendly/display name of this element.", + NULL, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_TYPE, + g_param_spec_flags("type", + "Element Type", + "The type of element this is.", + PURPLE_TYPE_MEDIA_ELEMENT_TYPE, + PURPLE_MEDIA_ELEMENT_NONE, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_CREATE_CB, + g_param_spec_pointer("create-cb", + "Create Callback", + "The function called to create this element.", + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)); + + g_type_class_add_private(klass, sizeof(PurpleMediaElementInfoPrivate)); +} + +G_DEFINE_TYPE(PurpleMediaElementInfo, + purple_media_element_info, G_TYPE_OBJECT); + +gchar * +purple_media_element_info_get_id(PurpleMediaElementInfo *info) +{ +#ifdef USE_VV + gchar *id; + g_return_val_if_fail(PURPLE_IS_MEDIA_ELEMENT_INFO(info), NULL); + g_object_get(info, "id", &id, NULL); + return id; +#else + return NULL; +#endif +} + +gchar * +purple_media_element_info_get_name(PurpleMediaElementInfo *info) +{ +#ifdef USE_VV + gchar *name; + g_return_val_if_fail(PURPLE_IS_MEDIA_ELEMENT_INFO(info), NULL); + g_object_get(info, "name", &name, NULL); + return name; +#else + return NULL; +#endif +} + +PurpleMediaElementType +purple_media_element_info_get_element_type(PurpleMediaElementInfo *info) +{ +#ifdef USE_VV + PurpleMediaElementType type; + g_return_val_if_fail(PURPLE_IS_MEDIA_ELEMENT_INFO(info), + PURPLE_MEDIA_ELEMENT_NONE); + g_object_get(info, "type", &type, NULL); + return type; +#else + return PURPLE_MEDIA_ELEMENT_NONE; +#endif +} + +GstElement * +purple_media_element_info_call_create(PurpleMediaElementInfo *info, + PurpleMedia *media, const gchar *session_id, + const gchar *participant) +{ +#ifdef USE_VV + PurpleMediaElementCreateCallback create; + g_return_val_if_fail(PURPLE_IS_MEDIA_ELEMENT_INFO(info), NULL); + g_object_get(info, "create-cb", &create, NULL); + if (create) + return create(media, session_id, participant); +#endif + return NULL; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libpurple/mediamanager.h Fri Apr 03 16:45:38 2009 +0000 @@ -0,0 +1,199 @@ +/** + * @file mediamanager.h Media Manager API + * @ingroup core + */ + +/* purple + * + * Purple is the legal property of its developers, whose names are too numerous + * to list here. Please refer to the COPYRIGHT file distributed with this + * source distribution. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef __MEDIA_MANAGER_H_ +#define __MEDIA_MANAGER_H_ + +#include <glib.h> +#include <glib-object.h> + +#include "connection.h" +#include "media.h" + +G_BEGIN_DECLS + +#define PURPLE_TYPE_MEDIA_MANAGER (purple_media_manager_get_type()) +#define PURPLE_MEDIA_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), PURPLE_TYPE_MEDIA_MANAGER, PurpleMediaManager)) +#define PURPLE_MEDIA_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), PURPLE_TYPE_MEDIA_MANAGER, PurpleMediaManagerClass)) +#define PURPLE_IS_MEDIA_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), PURPLE_TYPE_MEDIA_MANAGER)) +#define PURPLE_IS_MEDIA_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), PURPLE_TYPE_MEDIA_MANAGER)) +#define PURPLE_MEDIA_MANAGER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), PURPLE_TYPE_MEDIA_MANAGER, PurpleMediaManagerClass)) + +/** @copydoc _PurpleMediaManager */ +typedef struct _PurpleMediaManager PurpleMediaManager; +/** @copydoc _PurpleMediaManagerClass */ +typedef struct _PurpleMediaManagerClass PurpleMediaManagerClass; + +#ifdef __cplusplus +extern "C" { +#endif + +/**************************************************************************/ +/** @cname Media Manager API */ +/**************************************************************************/ +/*@{*/ + +/** + * Gets the media manager's GType. + * + * @return The media manager's GType. + */ +GType purple_media_manager_get_type(void); + +/** + * Gets the "global" media manager object. It's created if it doesn't already exist. + * + * @return The "global" instance of the media manager object. + */ +PurpleMediaManager *purple_media_manager_get(void); + +/** + * Creates a media session. + * + * @param manager The media manager to create the session under. + * @param gc The connection to create the session on. + * @param conference_type The conference type to feed into Farsight2. + * @param remote_user The remote user to initiate the session with. + * + * @return A newly created media session. + */ +PurpleMedia *purple_media_manager_create_media(PurpleMediaManager *manager, + PurpleConnection *gc, + const char *conference_type, + const char *remote_user, + gboolean initiator); + +/** + * Gets all of the media sessions. + * + * @param manager The media manager to get all of the sessions from. + * + * @return A list of all the media sessions. + */ +GList *purple_media_manager_get_media(PurpleMediaManager *manager); + +/** + * Gets all of the media sessions for a given connection. + * + * @param manager The media manager to get the sessions from. + * @param pc The connection the sessions are on. + * + * @return A list of the media sessions on the given connection. + */ +GList *purple_media_manager_get_media_by_connection( + PurpleMediaManager *manager, PurpleConnection *pc); + +/** + * Removes a media session from the media manager. + * + * @param manager The media manager to remove the media session from. + * @param media The media session to remove. + */ +void +purple_media_manager_remove_media(PurpleMediaManager *manager, + PurpleMedia *media); + +/** + * Signals that output windows should be created for the chosen stream. + * + * This shouldn't be called outside of mediamanager.c and media.c + * + * @param manager Manager the output windows are registered with. + * @param media Media session the output windows are registered for. + * @param session_id The session the output windows are registered with. + * @param participant The participant the output windows are registered with. + * + * @return TRUE if it succeeded, FALSE if it failed. + */ +gboolean purple_media_manager_create_output_window( + PurpleMediaManager *manager, PurpleMedia *media, + const gchar *session_id, const gchar *participant); + +/** + * Registers a video output window to be created for a given stream. + * + * @param manager The manager to register the output window with. + * @param media The media instance to find the stream in. + * @param session_id The session the stream is associated with. + * @param participant The participant the stream is associated with. + * @param window_id The window ID to embed the video in. + * + * @return A unique ID to the registered output window, 0 if it failed. + */ +gulong purple_media_manager_set_output_window(PurpleMediaManager *manager, + PurpleMedia *media, const gchar *session_id, + const gchar *participant, gulong window_id); + +/** + * Remove a previously registerd output window. + * + * @param manager The manager the output window was registered with. + * @param output_window_id The ID of the output window. + * + * @return TRUE if it found the output window and was successful, else FALSE. + */ +gboolean purple_media_manager_remove_output_window( + PurpleMediaManager *manager, gulong output_window_id); + +/** + * Remove all output windows for a given conference/session/participant/stream. + * + * @param manager The manager the output windows were registered with. + * @param media The media instance the output windows were registered for. + * @param session_id The session the output windows were registered for. + * @param participant The participant the output windows were registered for. + */ +void purple_media_manager_remove_output_windows( + PurpleMediaManager *manager, PurpleMedia *media, + const gchar *session_id, const gchar *participant); + +/** + * Sets which media caps the UI supports. + * + * @param manager The manager to set the caps on. + * @param caps The caps to set. + */ +void purple_media_manager_set_ui_caps(PurpleMediaManager *manager, + PurpleMediaCaps caps); + +/** + * Gets which media caps the UI supports. + * + * @param manager The manager to get caps from. + * + * @return caps The caps retrieved. + */ +PurpleMediaCaps purple_media_manager_get_ui_caps(PurpleMediaManager *manager); + +/*}@*/ + +#ifdef __cplusplus +} +#endif + +G_END_DECLS + +#endif /* __MEDIA_MANAGER_H_ */
--- a/libpurple/network.c Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/network.c Fri Apr 03 16:45:38 2009 +0000 @@ -48,6 +48,7 @@ #include "prefs.h" #include "stun.h" #include "upnp.h" +#include "dnsquery.h" /* * Calling sizeof(struct ifreq) isn't always correct on @@ -100,6 +101,10 @@ static gboolean force_online; #endif +/* Cached IP addresses for STUN and TURN servers (set globally in prefs) */ +static gchar *stun_ip = NULL; +static gchar *turn_ip = NULL; + const unsigned char * purple_network_ip_atoi(const char *ip) { @@ -723,6 +728,14 @@ case NM_STATE_CONNECTED: /* Call res_init in case DNS servers have changed */ res_init(); + /* update STUN IP in case we it changed (theoretically we could + have gone from IPv4 to IPv6, f.ex. or we were previously + offline */ + purple_network_set_stun_server( + purple_prefs_get_string("/purple/network/stun_server")); + purple_network_set_turn_server( + purple_prefs_get_string("/purple/network/turn_server")); + if (ui_ops != NULL && ui_ops->network_connected != NULL) ui_ops->network_connected(); break; @@ -784,6 +797,88 @@ #endif +static void +purple_network_ip_lookup_cb(GSList *hosts, gpointer data, + const char *error_message) +{ + const gchar **ip = (const gchar **) data; + + if (error_message) { + purple_debug_error("network", "lookup of IP address failed: %s\n", + error_message); + g_slist_free(hosts); + return; + } + + if (hosts && g_slist_next(hosts)) { + struct sockaddr *addr = g_slist_next(hosts)->data; + char dst[INET6_ADDRSTRLEN]; + + if (addr->sa_family == AF_INET6) { + inet_ntop(addr->sa_family, &((struct sockaddr_in6 *) addr)->sin6_addr, + dst, sizeof(dst)); + } else { + inet_ntop(addr->sa_family, &((struct sockaddr_in *) addr)->sin_addr, + dst, sizeof(dst)); + } + + *ip = g_strdup(dst); + purple_debug_info("network", "set IP address: %s\n", *ip); + } + + g_slist_free(hosts); +} + +void +purple_network_set_stun_server(const gchar *stun_server) +{ + if (stun_server && stun_server[0] != '\0') { + if (purple_network_is_available()) { + purple_debug_info("network", "running DNS query for STUN server\n"); + purple_dnsquery_a(stun_server, 3478, purple_network_ip_lookup_cb, + &stun_ip); + } else { + purple_debug_info("network", + "network is unavailable, don't try to update STUN IP"); + } + } else if (stun_ip) { + g_free(stun_ip); + stun_ip = NULL; + } +} + +void +purple_network_set_turn_server(const gchar *turn_server) +{ + if (turn_server && turn_server[0] != '\0') { + if (purple_network_is_available()) { + purple_debug_info("network", "running DNS query for TURN server\n"); + purple_dnsquery_a(turn_server, + purple_prefs_get_int("/purple/network/turn_port"), + purple_network_ip_lookup_cb, &turn_ip); + } else { + purple_debug_info("network", + "network is unavailable, don't try to update TURN IP"); + } + } else if (turn_ip) { + g_free(turn_ip); + turn_ip = NULL; + } +} + + +const gchar * +purple_network_get_stun_ip(void) +{ + return stun_ip; +} + +const gchar * +purple_network_get_turn_ip(void) +{ + return turn_ip; +} + void * purple_network_get_handle(void) { @@ -816,6 +911,11 @@ #endif purple_prefs_add_none ("/purple/network"); + purple_prefs_add_string("/purple/network/stun_server", ""); + purple_prefs_add_string("/purple/network/turn_server", ""); + purple_prefs_add_int ("/purple/network/turn_port", 3478); + purple_prefs_add_string("/purple/network/turn_username", ""); + purple_prefs_add_string("/purple/network/turn_password", ""); purple_prefs_add_bool ("/purple/network/auto_ip", TRUE); purple_prefs_add_string("/purple/network/public_ip", ""); purple_prefs_add_bool ("/purple/network/map_ports", TRUE); @@ -854,6 +954,11 @@ purple_pmp_init(); purple_upnp_init(); + + purple_network_set_stun_server( + purple_prefs_get_string("/purple/network/stun_server")); + purple_network_set_turn_server( + purple_prefs_get_string("/purple/network/turn_server")); } void @@ -895,4 +1000,7 @@ #endif purple_signal_unregister(purple_network_get_handle(), "network-configuration-changed"); + + if (stun_ip) + g_free(stun_ip); }
--- a/libpurple/network.h Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/network.h Fri Apr 03 16:45:38 2009 +0000 @@ -225,6 +225,37 @@ */ void *purple_network_get_handle(void); +/** + * Update the STUN server IP given the host name + * Will result in a DNS query being executed asynchronous + * + * @param stun_server The host name of the STUN server to set + */ +void purple_network_set_stun_server(const gchar *stun_server); + +/** + * Get the IP address of the STUN server as a string representation + * + * @return the IP address + */ +const gchar *purple_network_get_stun_ip(void); + +/** + * Update the TURN server IP given the host name + * Will result in a DNS query being executed asynchronous + * + * @param stun_server The host name of the STUN server to set + */ +void purple_network_set_turn_server(const gchar *stun_server); + +/** + * Get the IP address of the STUN server as a string representation + * + * @return the IP address + */ +const gchar *purple_network_get_turn_ip(void); + + /** * Initializes the network subsystem. */
--- a/libpurple/protocols/bonjour/bonjour.c Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/protocols/bonjour/bonjour.c Fri Apr 03 16:45:38 2009 +0000 @@ -498,13 +498,13 @@ NULL, /* whiteboard_prpl_ops */ NULL, /* send_raw */ NULL, /* roomlist_room_serialize */ - - /* padding */ - NULL, - NULL, - NULL, - sizeof(PurplePluginProtocolInfo), /* struct_size */ - NULL + NULL, /* unregister_user */ + NULL, /* send_attention */ + NULL, /* get_attention_types */ + sizeof(PurplePluginProtocolInfo), /* struct_size */ + NULL, /* get_account_text_table */ + NULL, /* initiate_media */ + NULL /* can_do_media */ }; static PurplePluginInfo info =
--- a/libpurple/protocols/gg/gg.c Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/protocols/gg/gg.c Fri Apr 03 16:45:38 2009 +0000 @@ -2293,13 +2293,13 @@ NULL, /* whiteboard_prpl_ops */ NULL, /* send_raw */ NULL, /* roomlist_room_serialize */ - - /* padding */ - NULL, - NULL, - NULL, + NULL, /* unregister_user */ + NULL, /* send_attention */ + NULL, /* get_attention_types */ sizeof(PurplePluginProtocolInfo), /* struct_size */ - NULL + NULL, /* get_account_text_table */ + NULL, /* initiate_media */ + NULL /* can_do_media */ }; static PurplePluginInfo info = {
--- a/libpurple/protocols/irc/irc.c Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/protocols/irc/irc.c Fri Apr 03 16:45:38 2009 +0000 @@ -912,13 +912,13 @@ NULL, /* whiteboard_prpl_ops */ irc_send_raw, /* send_raw */ NULL, /* roomlist_room_serialize */ - - /* padding */ - NULL, - NULL, - NULL, - sizeof(PurplePluginProtocolInfo), /* struct_size */ - NULL + NULL, /* unregister_user */ + NULL, /* send_attention */ + NULL, /* get_attention_types */ + sizeof(PurplePluginProtocolInfo), /* struct_size */ + NULL, /* get_account_text_table */ + NULL, /* initiate_media */ + NULL /* can_do_media */ }; static gboolean load_plugin (PurplePlugin *plugin) {
--- a/libpurple/protocols/jabber/Makefile.am Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/protocols/jabber/Makefile.am Fri Apr 03 16:45:38 2009 +0000 @@ -23,6 +23,20 @@ iq.h \ jabber.c \ jabber.h \ + jingle/jingle.c \ + jingle/jingle.h \ + jingle/content.c \ + jingle/content.h \ + jingle/iceudp.c \ + jingle/iceudp.h \ + jingle/rawudp.c \ + jingle/rawudp.h \ + jingle/rtp.c \ + jingle/rtp.h \ + jingle/session.c \ + jingle/session.h \ + jingle/transport.c \ + jingle/transport.h \ jutil.c \ jutil.h \ message.c \
--- a/libpurple/protocols/jabber/Makefile.mingw Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/protocols/jabber/Makefile.mingw Fri Apr 03 16:45:38 2009 +0000 @@ -54,6 +54,13 @@ ibb.c \ iq.c \ jabber.c \ + jingle/jingle.c \ + jingle/content.c \ + jingle/iceudp.c \ + jingle/rawudp.c \ + jingle/rtp.c \ + jingle/session.c \ + jingle/transport.c \ jutil.c \ message.c \ oob.c \ @@ -79,6 +86,7 @@ ## LIBS = \ -lglib-2.0 \ + -lgobject-2.0 \ -lxml2 \ -lws2_32 \ -lintl \
--- a/libpurple/protocols/jabber/disco.c Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/protocols/jabber/disco.c Fri Apr 03 16:45:38 2009 +0000 @@ -28,6 +28,7 @@ #include "iq.h" #include "disco.h" #include "jabber.h" +#include "jingle/jingle.h" #include "presence.h" #include "roster.h" #include "pep.h" @@ -143,6 +144,16 @@ SUPPORT_FEATURE(feat->namespace); } } +#ifdef USE_VV + } else if (node && !strcmp(node, CAPS0115_NODE "#voice-v1")) { + SUPPORT_FEATURE("http://www.google.com/xmpp/protocol/session"); + SUPPORT_FEATURE("http://www.google.com/xmpp/protocol/voice/v1"); + SUPPORT_FEATURE(JINGLE); + SUPPORT_FEATURE(JINGLE_APP_RTP_SUPPORT_AUDIO); + SUPPORT_FEATURE(JINGLE_APP_RTP_SUPPORT_VIDEO); + SUPPORT_FEATURE(JINGLE_TRANSPORT_RAWUDP); + SUPPORT_FEATURE(JINGLE_TRANSPORT_ICEUDP); +#endif } else { const char *ext = NULL; unsigned pos; @@ -430,6 +441,11 @@ if (!strcmp(name, "Google Talk")) { purple_debug_info("jabber", "Google Talk!\n"); js->googletalk = TRUE; + + /* autodiscover stun and relays */ + jabber_google_send_jingle_info(js); + } else { + /* TODO: add external service discovery here... */ } }
--- a/libpurple/protocols/jabber/google.c Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/protocols/jabber/google.c Fri Apr 03 16:45:38 2009 +0000 @@ -20,8 +20,11 @@ #include "internal.h" #include "debug.h" +#include "mediamanager.h" #include "util.h" #include "privacy.h" +#include "dnsquery.h" +#include "network.h" #include "buddy.h" #include "google.h" @@ -29,6 +32,586 @@ #include "presence.h" #include "iq.h" +#include "jingle/jingle.h" + +#ifdef USE_VV + +typedef struct { + char *id; + char *initiator; +} GoogleSessionId; + +typedef enum { + UNINIT, + SENT_INITIATE, + RECEIVED_INITIATE, + IN_PRORESS, + TERMINATED +} GoogleSessionState; + +typedef struct { + GoogleSessionId id; + GoogleSessionState state; + PurpleMedia *media; + JabberStream *js; + char *remote_jid; +} GoogleSession; + +static gboolean +google_session_id_equal(gconstpointer a, gconstpointer b) +{ + GoogleSessionId *c = (GoogleSessionId*)a; + GoogleSessionId *d = (GoogleSessionId*)b; + + return !strcmp(c->id, d->id) && !strcmp(c->initiator, d->initiator); +} + +static void +google_session_destroy(GoogleSession *session) +{ + g_free(session->id.id); + g_free(session->id.initiator); + g_free(session->remote_jid); + g_free(session); +} + +static xmlnode * +google_session_create_xmlnode(GoogleSession *session, const char *type) +{ + xmlnode *node = xmlnode_new("session"); + xmlnode_set_namespace(node, "http://www.google.com/session"); + xmlnode_set_attrib(node, "id", session->id.id); + xmlnode_set_attrib(node, "initiator", session->id.initiator); + xmlnode_set_attrib(node, "type", type); + return node; +} + +static void +google_session_send_terminate(GoogleSession *session) +{ + xmlnode *sess; + JabberIq *iq = jabber_iq_new(session->js, JABBER_IQ_SET); + + xmlnode_set_attrib(iq->node, "to", session->remote_jid); + sess = google_session_create_xmlnode(session, "terminate"); + xmlnode_insert_child(iq->node, sess); + + jabber_iq_send(iq); + google_session_destroy(session); +} + +static void +google_session_send_candidates(PurpleMedia *media, gchar *session_id, + gchar *participant, GoogleSession *session) +{ + GList *candidates = purple_media_get_local_candidates(session->media, "google-voice", + session->remote_jid); + PurpleMediaCandidate *transport; + + for (;candidates;candidates = candidates->next) { + JabberIq *iq; + gchar *ip, *port, *pref, *username, *password; + PurpleMediaCandidateType type; + xmlnode *sess; + xmlnode *candidate; + transport = (PurpleMediaCandidate*)(candidates->data); + + if (purple_media_candidate_get_component_id(transport) + != PURPLE_MEDIA_COMPONENT_RTP) + continue; + + iq = jabber_iq_new(session->js, JABBER_IQ_SET); + sess = google_session_create_xmlnode(session, "candidates"); + xmlnode_insert_child(iq->node, sess); + xmlnode_set_attrib(iq->node, "to", session->remote_jid); + + candidate = xmlnode_new("candidate"); + + ip = purple_media_candidate_get_ip(transport); + port = g_strdup_printf("%d", + purple_media_candidate_get_port(transport)); + pref = g_strdup_printf("%f", + purple_media_candidate_get_priority(transport) + /1000.0); + username = purple_media_candidate_get_username(transport); + password = purple_media_candidate_get_password(transport); + type = purple_media_candidate_get_candidate_type(transport); + + xmlnode_set_attrib(candidate, "address", ip); + xmlnode_set_attrib(candidate, "port", port); + xmlnode_set_attrib(candidate, "name", "rtp"); + xmlnode_set_attrib(candidate, "username", username); + /* + * As of this writing, Farsight 2 in Google compatibility + * mode doesn't provide a password. The Gmail client + * requires this to be set. + */ + xmlnode_set_attrib(candidate, "password", + password != NULL ? password : ""); + xmlnode_set_attrib(candidate, "preference", pref); + xmlnode_set_attrib(candidate, "protocol", + purple_media_candidate_get_protocol(transport) + == PURPLE_MEDIA_NETWORK_PROTOCOL_UDP ? + "udp" : "tcp"); + xmlnode_set_attrib(candidate, "type", type == + PURPLE_MEDIA_CANDIDATE_TYPE_HOST ? "local" : + type == + PURPLE_MEDIA_CANDIDATE_TYPE_SRFLX ? "stun" : + type == + PURPLE_MEDIA_CANDIDATE_TYPE_RELAY ? "relay" : + NULL); + xmlnode_set_attrib(candidate, "generation", "0"); + xmlnode_set_attrib(candidate, "network", "0"); + xmlnode_insert_child(sess, candidate); + + g_free(ip); + g_free(port); + g_free(pref); + g_free(username); + g_free(password); + + jabber_iq_send(iq); + } +} + +static void +google_session_ready(GoogleSession *session) +{ + PurpleMedia *media = session->media; + if (purple_media_codecs_ready(media, NULL) && + purple_media_candidates_prepared(media, NULL, NULL)) { + gchar *me = g_strdup_printf("%s@%s/%s", + session->js->user->node, + session->js->user->domain, + session->js->user->resource); + JabberIq *iq; + xmlnode *sess, *desc, *payload; + GList *codecs, *iter; + gboolean is_initiator = !strcmp(session->id.initiator, me); + + if (!is_initiator && + !purple_media_accepted(media, NULL, NULL)) { + g_free(me); + return; + } + + iq = jabber_iq_new(session->js, JABBER_IQ_SET); + + if (is_initiator) { + xmlnode_set_attrib(iq->node, "to", session->remote_jid); + xmlnode_set_attrib(iq->node, "from", session->id.initiator); + sess = google_session_create_xmlnode(session, "initiate"); + } else { + google_session_send_candidates(session->media, + "google-voice", session->remote_jid, + session); + xmlnode_set_attrib(iq->node, "to", session->remote_jid); + xmlnode_set_attrib(iq->node, "from", me); + sess = google_session_create_xmlnode(session, "accept"); + } + xmlnode_insert_child(iq->node, sess); + desc = xmlnode_new_child(sess, "description"); + xmlnode_set_namespace(desc, "http://www.google.com/session/phone"); + + codecs = purple_media_get_codecs(media, "google-voice"); + + for (iter = codecs; iter; iter = g_list_next(iter)) { + PurpleMediaCodec *codec = (PurpleMediaCodec*)iter->data; + gchar *id = g_strdup_printf("%d", + purple_media_codec_get_id(codec)); + gchar *encoding_name = + purple_media_codec_get_encoding_name(codec); + gchar *clock_rate = g_strdup_printf("%d", + purple_media_codec_get_clock_rate(codec)); + payload = xmlnode_new_child(desc, "payload-type"); + xmlnode_set_attrib(payload, "id", id); + xmlnode_set_attrib(payload, "name", encoding_name); + xmlnode_set_attrib(payload, "clockrate", clock_rate); + g_free(clock_rate); + g_free(encoding_name); + g_free(id); + } + purple_media_codec_list_free(codecs); + + jabber_iq_send(iq); + + if (is_initiator) + google_session_send_candidates(session->media, + "google-voice", session->remote_jid, + session); + + g_signal_handlers_disconnect_by_func(G_OBJECT(session->media), + G_CALLBACK(google_session_ready), session); + } +} + +static void +google_session_state_changed_cb(PurpleMedia *media, PurpleMediaState state, + gchar *sid, gchar *name, GoogleSession *session) +{ + if (sid == NULL && name == NULL) { + if (state == PURPLE_MEDIA_STATE_END) { + google_session_destroy(session); + } + } +} + +static void +google_session_stream_info_cb(PurpleMedia *media, PurpleMediaInfoType type, + gchar *sid, gchar *name, gboolean local, + GoogleSession *session) +{ + if (type == PURPLE_MEDIA_INFO_HANGUP) { + xmlnode *sess; + JabberIq *iq = jabber_iq_new(session->js, JABBER_IQ_SET); + + xmlnode_set_attrib(iq->node, "to", session->remote_jid); + sess = google_session_create_xmlnode(session, "terminate"); + xmlnode_insert_child(iq->node, sess); + + jabber_iq_send(iq); + } else if (type == PURPLE_MEDIA_INFO_REJECT) { + xmlnode *sess; + JabberIq *iq = jabber_iq_new(session->js, JABBER_IQ_SET); + + xmlnode_set_attrib(iq->node, "to", session->remote_jid); + sess = google_session_create_xmlnode(session, "reject"); + xmlnode_insert_child(iq->node, sess); + + jabber_iq_send(iq); + } +} + +static GParameter * +jabber_google_session_get_params(JabberStream *js, guint *num) +{ + guint num_params; + GParameter *params = jingle_get_params(js, &num_params); + GParameter *new_params = g_new0(GParameter, num_params + 1); + + memcpy(new_params, params, sizeof(GParameter) * num_params); + + purple_debug_info("jabber", "setting Google jingle compatibility param\n"); + new_params[num_params].name = "compatibility-mode"; + g_value_init(&new_params[num_params].value, G_TYPE_UINT); + g_value_set_uint(&new_params[num_params].value, 1); /* NICE_COMPATIBILITY_GOOGLE */ + + g_free(params); + *num = num_params + 1; + return new_params; +} + + +gboolean +jabber_google_session_initiate(JabberStream *js, const gchar *who, PurpleMediaSessionType type) +{ + GoogleSession *session; + JabberBuddy *jb; + JabberBuddyResource *jbr; + gchar *jid; + GParameter *params; + guint num_params; + + /* construct JID to send to */ + jb = jabber_buddy_find(js, who, FALSE); + if (!jb) { + purple_debug_error("jingle-rtp", + "Could not find Jabber buddy\n"); + return FALSE; + } + jbr = jabber_buddy_find_resource(jb, NULL); + if (!jbr) { + purple_debug_error("jingle-rtp", + "Could not find buddy's resource\n"); + } + + if ((strchr(who, '/') == NULL) && jbr && (jbr->name != NULL)) { + jid = g_strdup_printf("%s/%s", who, jbr->name); + } else { + jid = g_strdup(who); + } + + session = g_new0(GoogleSession, 1); + session->id.id = jabber_get_next_id(js); + session->id.initiator = g_strdup_printf("%s@%s/%s", js->user->node, + js->user->domain, js->user->resource); + session->state = SENT_INITIATE; + session->js = js; + session->remote_jid = jid; + + session->media = purple_media_manager_create_media( + purple_media_manager_get(), js->gc, + "fsrtpconference", session->remote_jid, TRUE); + + purple_media_set_prpl_data(session->media, session); + + params = jabber_google_session_get_params(js, &num_params); + + if (purple_media_add_stream(session->media, "google-voice", + session->remote_jid, PURPLE_MEDIA_AUDIO, + TRUE, "nice", num_params, params) == FALSE) { + purple_media_error(session->media, "Error adding stream."); + purple_media_stream_info(session->media, + PURPLE_MEDIA_INFO_HANGUP, NULL, NULL, TRUE); + google_session_destroy(session); + g_free(params); + return FALSE; + } + + g_signal_connect_swapped(G_OBJECT(session->media), + "candidates-prepared", + G_CALLBACK(google_session_ready), session); + g_signal_connect_swapped(G_OBJECT(session->media), "codecs-changed", + G_CALLBACK(google_session_ready), session); + g_signal_connect(G_OBJECT(session->media), "state-changed", + G_CALLBACK(google_session_state_changed_cb), session); + g_signal_connect(G_OBJECT(session->media), "stream-info", + G_CALLBACK(google_session_stream_info_cb), session); + + g_free(params); + + return (session->media != NULL) ? TRUE : FALSE; +} + +static void +google_session_handle_initiate(JabberStream *js, GoogleSession *session, xmlnode *packet, xmlnode *sess) +{ + JabberIq *result; + GList *codecs = NULL; + xmlnode *desc_element, *codec_element; + PurpleMediaCodec *codec; + const char *id, *encoding_name, *clock_rate; + GParameter *params; + guint num_params; + + if (session->state != UNINIT) { + purple_debug_error("jabber", "Received initiate for active session.\n"); + return; + } + + session->media = purple_media_manager_create_media(purple_media_manager_get(), js->gc, + "fsrtpconference", session->remote_jid, FALSE); + + purple_media_set_prpl_data(session->media, session); + + params = jabber_google_session_get_params(js, &num_params); + + if (purple_media_add_stream(session->media, "google-voice", + session->remote_jid, PURPLE_MEDIA_AUDIO, FALSE, + "nice", num_params, params) == FALSE) { + purple_media_error(session->media, "Error adding stream."); + purple_media_stream_info(session->media, + PURPLE_MEDIA_INFO_HANGUP, NULL, NULL, TRUE); + google_session_send_terminate(session); + g_free(params); + return; + } + + g_free(params); + + desc_element = xmlnode_get_child(sess, "description"); + + for (codec_element = xmlnode_get_child(desc_element, "payload-type"); + codec_element; + codec_element = xmlnode_get_next_twin(codec_element)) { + encoding_name = xmlnode_get_attrib(codec_element, "name"); + id = xmlnode_get_attrib(codec_element, "id"); + clock_rate = xmlnode_get_attrib(codec_element, "clockrate"); + + codec = purple_media_codec_new(atoi(id), encoding_name, PURPLE_MEDIA_AUDIO, + clock_rate ? atoi(clock_rate) : 0); + codecs = g_list_append(codecs, codec); + } + + purple_media_set_remote_codecs(session->media, "google-voice", session->remote_jid, codecs); + + g_signal_connect_swapped(G_OBJECT(session->media), "accepted", + G_CALLBACK(google_session_ready), session); + g_signal_connect_swapped(G_OBJECT(session->media), + "candidates-prepared", + G_CALLBACK(google_session_ready), session); + g_signal_connect_swapped(G_OBJECT(session->media), "codecs-changed", + G_CALLBACK(google_session_ready), session); + g_signal_connect(G_OBJECT(session->media), "state-changed", + G_CALLBACK(google_session_state_changed_cb), session); + g_signal_connect(G_OBJECT(session->media), "stream-info", + G_CALLBACK(google_session_stream_info_cb), session); + + purple_media_codec_list_free(codecs); + + result = jabber_iq_new(js, JABBER_IQ_RESULT); + jabber_iq_set_id(result, xmlnode_get_attrib(packet, "id")); + xmlnode_set_attrib(result->node, "to", session->remote_jid); + jabber_iq_send(result); +} + +static void +google_session_handle_candidates(JabberStream *js, GoogleSession *session, xmlnode *packet, xmlnode *sess) +{ + JabberIq *result; + GList *list = NULL; + xmlnode *cand; + static int name = 0; + char n[4]; + + for (cand = xmlnode_get_child(sess, "candidate"); cand; cand = xmlnode_get_next_twin(cand)) { + PurpleMediaCandidate *info; + g_snprintf(n, sizeof(n), "S%d", name++); + info = purple_media_candidate_new(n, PURPLE_MEDIA_COMPONENT_RTP, + !strcmp(xmlnode_get_attrib(cand, "type"), "local") ? + PURPLE_MEDIA_CANDIDATE_TYPE_HOST : + !strcmp(xmlnode_get_attrib(cand, "type"), "stun") ? + PURPLE_MEDIA_CANDIDATE_TYPE_PRFLX : + !strcmp(xmlnode_get_attrib(cand, "type"), "relay") ? + PURPLE_MEDIA_CANDIDATE_TYPE_RELAY : + PURPLE_MEDIA_CANDIDATE_TYPE_HOST, + !strcmp(xmlnode_get_attrib(cand, "protocol"),"udp") ? + PURPLE_MEDIA_NETWORK_PROTOCOL_UDP : + PURPLE_MEDIA_NETWORK_PROTOCOL_TCP, + xmlnode_get_attrib(cand, "address"), + atoi(xmlnode_get_attrib(cand, "port"))); + g_object_set(info, "username", xmlnode_get_attrib(cand, "username"), + "password", xmlnode_get_attrib(cand, "password"), NULL); + + list = g_list_append(list, info); + } + + purple_media_add_remote_candidates(session->media, "google-voice", session->remote_jid, list); + purple_media_candidate_list_free(list); + + result = jabber_iq_new(js, JABBER_IQ_RESULT); + jabber_iq_set_id(result, xmlnode_get_attrib(packet, "id")); + xmlnode_set_attrib(result->node, "to", session->remote_jid); + jabber_iq_send(result); +} + +static void +google_session_handle_accept(JabberStream *js, GoogleSession *session, xmlnode *packet, xmlnode *sess) +{ + xmlnode *desc_element = xmlnode_get_child(sess, "description"); + xmlnode *codec_element = xmlnode_get_child(desc_element, "payload-type"); + GList *codecs = NULL; + JabberIq *result = NULL; + + for (; codec_element; codec_element = + xmlnode_get_next_twin(codec_element)) { + const gchar *encoding_name = + xmlnode_get_attrib(codec_element, "name"); + const gchar *id = xmlnode_get_attrib(codec_element, "id"); + const gchar *clock_rate = + xmlnode_get_attrib(codec_element, "clockrate"); + + PurpleMediaCodec *codec = purple_media_codec_new(atoi(id), + encoding_name, PURPLE_MEDIA_AUDIO, + clock_rate ? atoi(clock_rate) : 0); + codecs = g_list_append(codecs, codec); + } + + purple_media_set_remote_codecs(session->media, "google-voice", + session->remote_jid, codecs); + + purple_media_stream_info(session->media, PURPLE_MEDIA_INFO_ACCEPT, + NULL, NULL, FALSE); + + result = jabber_iq_new(js, JABBER_IQ_RESULT); + jabber_iq_set_id(result, xmlnode_get_attrib(packet, "id")); + xmlnode_set_attrib(result->node, "to", session->remote_jid); + jabber_iq_send(result); +} + +static void +google_session_handle_reject(JabberStream *js, GoogleSession *session, xmlnode *packet, xmlnode *sess) +{ + purple_media_end(session->media, NULL, NULL); +} + +static void +google_session_handle_terminate(JabberStream *js, GoogleSession *session, xmlnode *packet, xmlnode *sess) +{ + purple_media_end(session->media, NULL, NULL); +} + +static void +google_session_parse_iq(JabberStream *js, GoogleSession *session, xmlnode *packet) +{ + xmlnode *sess = xmlnode_get_child(packet, "session"); + const char *type = xmlnode_get_attrib(sess, "type"); + + if (!strcmp(type, "initiate")) { + google_session_handle_initiate(js, session, packet, sess); + } else if (!strcmp(type, "accept")) { + google_session_handle_accept(js, session, packet, sess); + } else if (!strcmp(type, "reject")) { + google_session_handle_reject(js, session, packet, sess); + } else if (!strcmp(type, "terminate")) { + google_session_handle_terminate(js, session, packet, sess); + } else if (!strcmp(type, "candidates")) { + google_session_handle_candidates(js, session, packet, sess); + } +} + +void +jabber_google_session_parse(JabberStream *js, xmlnode *packet) +{ + GoogleSession *session = NULL; + GoogleSessionId id; + + xmlnode *session_node; + xmlnode *desc_node; + + GList *iter = NULL; + + if (strcmp(xmlnode_get_attrib(packet, "type"), "set")) + return; + + session_node = xmlnode_get_child(packet, "session"); + if (!session_node) + return; + + id.id = (gchar*)xmlnode_get_attrib(session_node, "id"); + if (!id.id) + return; + + id.initiator = (gchar*)xmlnode_get_attrib(session_node, "initiator"); + if (!id.initiator) + return; + + iter = purple_media_manager_get_media_by_connection( + purple_media_manager_get(), js->gc); + for (; iter; iter = g_list_delete_link(iter, iter)) { + GoogleSession *gsession = + purple_media_get_prpl_data(iter->data); + if (google_session_id_equal(&(gsession->id), &id)) { + session = gsession; + break; + } + } + if (iter != NULL) { + g_list_free(iter); + } + + if (session) { + google_session_parse_iq(js, session, packet); + return; + } + + /* If the session doesn't exist, this has to be an initiate message */ + if (strcmp(xmlnode_get_attrib(session_node, "type"), "initiate")) + return; + desc_node = xmlnode_get_child(session_node, "description"); + if (!desc_node) + return; + session = g_new0(GoogleSession, 1); + session->id.id = g_strdup(id.id); + session->id.initiator = g_strdup(id.initiator); + session->state = UNINIT; + session->js = js; + session->remote_jid = g_strdup(session->id.initiator); + + google_session_parse_iq(js, session, packet); +} +#endif /* USE_VV */ + static void jabber_gmail_parse(JabberStream *js, const char *from, JabberIqType type, const char *id, @@ -530,3 +1113,105 @@ const char *attr = purple_status_get_attr_string(tune, PURPLE_TUNE_TITLE); return attr ? g_strdup_printf("♫ %s", attr) : g_strdup(""); } + +static void +jabber_google_stun_lookup_cb(GSList *hosts, gpointer data, + const char *error_message) +{ + JabberStream *js = (JabberStream *) data; + + if (error_message) { + purple_debug_error("jabber", "Google STUN lookup failed: %s\n", + error_message); + g_slist_free(hosts); + return; + } + + if (hosts && g_slist_next(hosts)) { + struct sockaddr *addr = g_slist_next(hosts)->data; + char dst[INET6_ADDRSTRLEN]; + int port; + + if (addr->sa_family == AF_INET6) { + inet_ntop(addr->sa_family, &((struct sockaddr_in6 *) addr)->sin6_addr, + dst, sizeof(dst)); + port = ntohs(((struct sockaddr_in6 *) addr)->sin6_port); + } else { + inet_ntop(addr->sa_family, &((struct sockaddr_in *) addr)->sin_addr, + dst, sizeof(dst)); + port = ntohs(((struct sockaddr_in *) addr)->sin_port); + } + + if (js) { + if (js->stun_ip) { + g_free(js->stun_ip); + } + js->stun_ip = g_strdup(dst); + purple_debug_info("jabber", "set Google STUN IP address: %s\n", dst); + js->stun_port = port; + purple_debug_info("jabber", "set Google STUN port: %d\n", port); + purple_debug_info("jabber", "set Google STUN port: %d\n", port); + /* unmark ongoing query */ + js->stun_query = NULL; + } + } + + g_slist_free(hosts); +} + +static void +jabber_google_jingle_info_cb(JabberStream *js, xmlnode *result, + gpointer nullus) +{ + if (result) { + const xmlnode *query = + xmlnode_get_child_with_namespace(result, "query", + GOOGLE_JINGLE_INFO_NAMESPACE); + + if (query) { + const xmlnode *stun = xmlnode_get_child(query, "stun"); + + purple_debug_info("jabber", "got google:jingleinfo\n"); + + if (stun) { + xmlnode *server = xmlnode_get_child(stun, "server"); + + if (server) { + const gchar *host = xmlnode_get_attrib(server, "host"); + const gchar *udp = xmlnode_get_attrib(server, "udp"); + + if (host && udp) { + int port = atoi(udp); + /* if there, would already be an ongoing query, + cancel it */ + if (js->stun_query) + purple_dnsquery_destroy(js->stun_query); + + js->stun_query = purple_dnsquery_a(host, port, + jabber_google_stun_lookup_cb, js); + } + } + } + /* should perhaps handle relays later on, or maybe wait until + Google supports a common standard... */ + } + } +} + +void +jabber_google_handle_jingle_info(JabberStream *js, xmlnode *packet) +{ + jabber_google_jingle_info_cb(js, packet, NULL); +} + +void +jabber_google_send_jingle_info(JabberStream *js) +{ + JabberIq *jingle_info = + jabber_iq_new_query(js, JABBER_IQ_GET, GOOGLE_JINGLE_INFO_NAMESPACE); + + jabber_iq_set_callback(jingle_info, jabber_google_jingle_info_cb, + NULL); + purple_debug_info("jabber", "sending google:jingleinfo query\n"); + jabber_iq_send(jingle_info); +}
--- a/libpurple/protocols/jabber/google.h Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/protocols/jabber/google.h Fri Apr 03 16:45:38 2009 +0000 @@ -25,6 +25,10 @@ * such that they don't intermingle with code for the XMPP RFCs and XEPs :) */ #include "jabber.h" +#include "media.h" + +#define GOOGLE_VOICE_CAP "http://www.google.com/xmpp/protocol/voice/v1" +#define GOOGLE_JINGLE_INFO_NAMESPACE "google:jingleinfo" void jabber_gmail_init(JabberStream *js); void jabber_gmail_poke(JabberStream *js, const char *from, JabberIqType type, @@ -46,6 +50,10 @@ char *jabber_google_format_to_html(const char *text); +gboolean jabber_google_session_initiate(JabberStream *js, const gchar *who, PurpleMediaSessionType type); +void jabber_google_session_parse(JabberStream *js, xmlnode *node); +void jabber_google_handle_jingle_info(JabberStream *js, xmlnode *packet); +void jabber_google_send_jingle_info(JabberStream *js); #endif /* _PURPLE_GOOGLE_H_ */
--- a/libpurple/protocols/jabber/iq.c Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/protocols/jabber/iq.c Fri Apr 03 16:45:38 2009 +0000 @@ -28,6 +28,7 @@ #include "disco.h" #include "google.h" #include "iq.h" +#include "jingle/jingle.h" #include "oob.h" #include "roster.h" #include "si.h" @@ -426,6 +427,10 @@ jabber_iq_register_handler("data", XEP_0231_NAMESPACE, jabber_data_parse); jabber_iq_register_handler("ping", "urn:xmpp:ping", jabber_ping_parse); jabber_iq_register_handler("time", "urn:xmpp:time", jabber_iq_time_parse); + + jabber_iq_register_handler("jingle", JINGLE, jingle_parse); + jabber_iq_register_handler("query", GOOGLE_JINGLE_INFO_NAMESPACE, + jabber_google_handle_jingle_info); } void jabber_iq_uninit(void)
--- a/libpurple/protocols/jabber/jabber.c Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/protocols/jabber/jabber.c Fri Apr 03 16:45:38 2009 +0000 @@ -59,6 +59,8 @@ #include "pep.h" #include "adhoccommands.h" +#include "jingle/jingle.h" +#include "jingle/rtp.h" #define JABBER_CONNECT_STEPS (js->gsc ? 9 : 5) @@ -719,6 +721,10 @@ js->old_length = 0; js->keepalive_timeout = -1; js->certificate_CN = g_strdup(connect_server[0] ? connect_server : js->user ? js->user->domain : NULL); + js->sessions = NULL; + js->stun_ip = NULL; + js->stun_port = 0; + js->stun_query = NULL; if(!js->user) { purple_connection_error_reason (gc, @@ -1214,6 +1220,10 @@ server = connect_server[0] ? connect_server : js->user->domain; js->certificate_CN = g_strdup(server); + js->stun_ip = NULL; + js->stun_port = 0; + js->stun_query = NULL; + jabber_stream_set_state(js, JABBER_STREAM_CONNECTING); if(purple_account_get_bool(account, "old_ssl", FALSE)) { @@ -1315,6 +1325,9 @@ { JabberStream *js = gc->proto_data; + /* Close all of the open Jingle sessions on this stream */ + jingle_terminate_sessions(js); + /* Don't perform any actions on the ssl connection * if we were forcibly disconnected because it will crash * on some SSL backends. @@ -1416,6 +1429,15 @@ g_free(js->srv_rec); js->srv_rec = NULL; + g_free(js->stun_ip); + js->stun_ip = NULL; + + /* cancel DNS query for STUN, if one is ongoing */ + if (js->stun_query) { + purple_dnsquery_destroy(js->stun_query); + js->stun_query = NULL; + } + g_free(js); gc->proto_data = NULL; @@ -2590,6 +2612,88 @@ return TRUE; } +gboolean +jabber_initiate_media(PurpleConnection *gc, const char *who, + PurpleMediaSessionType type) +{ +#ifdef USE_VV + JabberStream *js = (JabberStream *) gc->proto_data; + JabberBuddy *jb; + + if (!js) { + purple_debug_error("jabber", + "jabber_initiate_media: NULL stream\n"); + return FALSE; + } + + jb = jabber_buddy_find(js, who, FALSE); + + if (!jb) { + purple_debug_error("jabber", "Could not find buddy\n"); + return FALSE; + } + + if (type & PURPLE_MEDIA_AUDIO && + !jabber_buddy_has_capability(jb, + JINGLE_APP_RTP_SUPPORT_AUDIO) && + jabber_buddy_has_capability(jb, GOOGLE_VOICE_CAP)) + return jabber_google_session_initiate(gc->proto_data, who, type); + else + return jingle_rtp_initiate_media(gc->proto_data, who, type); +#else + return FALSE; +#endif +} + +PurpleMediaCaps jabber_get_media_caps(PurpleConnection *gc, const char *who) +{ +#ifdef USE_VV + JabberStream *js = (JabberStream *) gc->proto_data; + JabberBuddy *jb; + PurpleMediaCaps caps = PURPLE_MEDIA_CAPS_NONE; + + if (!js) { + purple_debug_info("jabber", + "jabber_can_do_media: NULL stream\n"); + return FALSE; + } + + jb = jabber_buddy_find(js, who, FALSE); + + if (!jb) { + purple_debug_error("jabber", "Could not find buddy\n"); + return FALSE; + } + + if (jabber_buddy_has_capability(jb, JINGLE_APP_RTP_SUPPORT_AUDIO)) + caps |= PURPLE_MEDIA_CAPS_AUDIO | + PURPLE_MEDIA_CAPS_AUDIO_SINGLE_DIRECTION; + if (jabber_buddy_has_capability(jb, JINGLE_APP_RTP_SUPPORT_VIDEO)) + caps |= PURPLE_MEDIA_CAPS_VIDEO | + PURPLE_MEDIA_CAPS_VIDEO_SINGLE_DIRECTION; + if (caps & PURPLE_MEDIA_CAPS_AUDIO && caps & PURPLE_MEDIA_CAPS_VIDEO) + caps |= PURPLE_MEDIA_CAPS_AUDIO_VIDEO; + if (caps != PURPLE_MEDIA_CAPS_NONE) { + if (!jabber_buddy_has_capability(jb, + JINGLE_TRANSPORT_ICEUDP) && + !jabber_buddy_has_capability(jb, + JINGLE_TRANSPORT_RAWUDP)) { + purple_debug_info("jingle-rtp", "Buddy doesn't " + "support the same transport types\n"); + caps = PURPLE_MEDIA_CAPS_NONE; + } else + caps |= PURPLE_MEDIA_CAPS_MODIFY_SESSION | + PURPLE_MEDIA_CAPS_CHANGE_DIRECTION; + } + if (jabber_buddy_has_capability(jb, GOOGLE_VOICE_CAP)) + caps |= PURPLE_MEDIA_CAPS_AUDIO; + + return caps; +#else + return PURPLE_MEDIA_CAPS_NONE; +#endif +} + void jabber_register_commands(void) { purple_cmd_register("config", "", PURPLE_CMD_P_PRPL,
--- a/libpurple/protocols/jabber/jabber.h Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/protocols/jabber/jabber.h Fri Apr 03 16:45:38 2009 +0000 @@ -54,8 +54,11 @@ #include "circbuffer.h" #include "connection.h" #include "dnssrv.h" +#include "media.h" +#include "mediamanager.h" #include "roomlist.h" #include "sslconn.h" +#include "dnsquery.h" #include "iq.h" #include "jutil.h" @@ -243,6 +246,15 @@ * for when we lookup buddy icons from a url */ GSList *url_datas; + + /* keep a hash table of JingleSessions */ + GHashTable *sessions; + + /* maybe this should only be present when USE_VV? */ + gchar *stun_ip; + int stun_port; + PurpleDnsQueryData *stun_query; + /* later add stuff to handle TURN relays... */ }; typedef gboolean (JabberFeatureEnabled)(JabberStream *js, const gchar *shortname, const gchar *namespace); @@ -311,6 +323,9 @@ gboolean jabber_offline_message(const PurpleBuddy *buddy); int jabber_prpl_send_raw(PurpleConnection *gc, const char *buf, int len); GList *jabber_actions(PurplePlugin *plugin, gpointer context); +gboolean jabber_initiate_media(PurpleConnection *gc, const char *who, + PurpleMediaSessionType type); +PurpleMediaCaps jabber_get_media_caps(PurpleConnection *gc, const char *who); void jabber_register_commands(void); void jabber_init_plugin(PurplePlugin *plugin);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libpurple/protocols/jabber/jingle/content.c Fri Apr 03 16:45:38 2009 +0000 @@ -0,0 +1,455 @@ +/** + * @file content.c + * + * purple + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA + */ + +#include "internal.h" + +#include "debug.h" +#include "content.h" +#include "jingle.h" + +#include <string.h> + +struct _JingleContentPrivate +{ + JingleSession *session; + gchar *description_type; + gchar *creator; + gchar *disposition; + gchar *name; + gchar *senders; + JingleTransport *transport; + JingleTransport *pending_transport; +}; + +#define JINGLE_CONTENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), JINGLE_TYPE_CONTENT, JingleContentPrivate)) + +static void jingle_content_class_init (JingleContentClass *klass); +static void jingle_content_init (JingleContent *content); +static void jingle_content_finalize (GObject *object); +static void jingle_content_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec); +static void jingle_content_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec); +static xmlnode *jingle_content_to_xml_internal(JingleContent *content, xmlnode *jingle, JingleActionType action); +static JingleContent *jingle_content_parse_internal(xmlnode *content); + +static GObjectClass *parent_class = NULL; + +enum { + PROP_0, + PROP_SESSION, + PROP_CREATOR, + PROP_DISPOSITION, + PROP_NAME, + PROP_SENDERS, + PROP_TRANSPORT, + PROP_PENDING_TRANSPORT, +}; + +GType +jingle_content_get_type() +{ + static GType type = 0; + + if (type == 0) { + static const GTypeInfo info = { + sizeof(JingleContentClass), + NULL, + NULL, + (GClassInitFunc) jingle_content_class_init, + NULL, + NULL, + sizeof(JingleContent), + 0, + (GInstanceInitFunc) jingle_content_init, + NULL + }; + type = g_type_register_static(G_TYPE_OBJECT, "JingleContent", &info, 0); + } + return type; +} + +static void +jingle_content_class_init (JingleContentClass *klass) +{ + GObjectClass *gobject_class = (GObjectClass*)klass; + parent_class = g_type_class_peek_parent(klass); + + gobject_class->finalize = jingle_content_finalize; + gobject_class->set_property = jingle_content_set_property; + gobject_class->get_property = jingle_content_get_property; + klass->to_xml = jingle_content_to_xml_internal; + klass->parse = jingle_content_parse_internal; + + g_object_class_install_property(gobject_class, PROP_SESSION, + g_param_spec_object("session", + "Jingle Session", + "The jingle session parent of this content.", + JINGLE_TYPE_SESSION, + G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_CREATOR, + g_param_spec_string("creator", + "Creator", + "The participant that created this content.", + NULL, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_DISPOSITION, + g_param_spec_string("disposition", + "Disposition", + "The disposition of the content.", + NULL, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_NAME, + g_param_spec_string("name", + "Name", + "The name of this content.", + NULL, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_SENDERS, + g_param_spec_string("senders", + "Senders", + "The sender of this content.", + NULL, + G_PARAM_CONSTRUCT | G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_TRANSPORT, + g_param_spec_object("transport", + "transport", + "The transport of this content.", + JINGLE_TYPE_TRANSPORT, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_PENDING_TRANSPORT, + g_param_spec_object("pending-transport", + "Pending transport", + "The pending transport contained within this content", + JINGLE_TYPE_TRANSPORT, + G_PARAM_READWRITE)); + + g_type_class_add_private(klass, sizeof(JingleContentPrivate)); +} + +static void +jingle_content_init (JingleContent *content) +{ + content->priv = JINGLE_CONTENT_GET_PRIVATE(content); + memset(content->priv, 0, sizeof(*content->priv)); +} + +static void +jingle_content_finalize (GObject *content) +{ + JingleContentPrivate *priv = JINGLE_CONTENT_GET_PRIVATE(content); + purple_debug_info("jingle","jingle_content_finalize\n"); + + g_free(priv->description_type); + g_free(priv->creator); + g_free(priv->disposition); + g_free(priv->name); + g_free(priv->senders); + g_object_unref(priv->transport); + if (priv->pending_transport) + g_object_unref(priv->pending_transport); + + parent_class->finalize(content); +} + +static void +jingle_content_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) +{ + JingleContent *content; + g_return_if_fail(JINGLE_IS_CONTENT(object)); + + content = JINGLE_CONTENT(object); + + switch (prop_id) { + case PROP_SESSION: + content->priv->session = g_value_get_object(value); + break; + case PROP_CREATOR: + g_free(content->priv->creator); + content->priv->creator = g_value_dup_string(value); + break; + case PROP_DISPOSITION: + g_free(content->priv->disposition); + content->priv->disposition = g_value_dup_string(value); + break; + case PROP_NAME: + g_free(content->priv->name); + content->priv->name = g_value_dup_string(value); + break; + case PROP_SENDERS: + g_free(content->priv->senders); + content->priv->senders = g_value_dup_string(value); + break; + case PROP_TRANSPORT: + if (content->priv->transport) + g_object_unref(content->priv->transport); + content->priv->transport = g_value_get_object(value); + break; + case PROP_PENDING_TRANSPORT: + if (content->priv->pending_transport) + g_object_unref(content->priv->pending_transport); + content->priv->pending_transport = g_value_get_object(value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +jingle_content_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) +{ + JingleContent *content; + g_return_if_fail(JINGLE_IS_CONTENT(object)); + + content = JINGLE_CONTENT(object); + + switch (prop_id) { + case PROP_SESSION: + g_value_set_object(value, content->priv->session); + break; + case PROP_CREATOR: + g_value_set_string(value, content->priv->creator); + break; + case PROP_DISPOSITION: + g_value_set_string(value, content->priv->disposition); + break; + case PROP_NAME: + g_value_set_string(value, content->priv->name); + break; + case PROP_SENDERS: + g_value_set_string(value, content->priv->senders); + break; + case PROP_TRANSPORT: + g_value_set_object(value, content->priv->transport); + break; + case PROP_PENDING_TRANSPORT: + g_value_set_object(value, content->priv->pending_transport); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +JingleContent * +jingle_content_create(const gchar *type, const gchar *creator, + const gchar *disposition, const gchar *name, + const gchar *senders, JingleTransport *transport) +{ + + + JingleContent *content = g_object_new(jingle_get_type(type), + "creator", creator, + "disposition", disposition != NULL ? disposition : "session", + "name", name, + "senders", senders != NULL ? senders : "both", + "transport", transport, + NULL); + return content; +} + +JingleSession *jingle_content_get_session(JingleContent *content) +{ + JingleSession *session; + g_object_get(content, "session", &session, NULL); + return session; +} + +const gchar * +jingle_content_get_description_type(JingleContent *content) +{ + return JINGLE_CONTENT_GET_CLASS(content)->description_type; +} + +gchar * +jingle_content_get_creator(JingleContent *content) +{ + gchar *creator; + g_object_get(content, "creator", &creator, NULL); + return creator; +} + +gchar * +jingle_content_get_disposition(JingleContent *content) +{ + gchar *disposition; + g_object_get(content, "disposition", &disposition, NULL); + return disposition; +} + +gchar * +jingle_content_get_name(JingleContent *content) +{ + gchar *name; + g_object_get(content, "name", &name, NULL); + return name; +} + +gchar * +jingle_content_get_senders(JingleContent *content) +{ + gchar *senders; + g_object_get(content, "senders", &senders, NULL); + return senders; +} + +JingleTransport * +jingle_content_get_transport(JingleContent *content) +{ + JingleTransport *transport; + g_object_get(content, "transport", &transport, NULL); + return transport; +} + +void +jingle_content_set_session(JingleContent *content, JingleSession *session) +{ + JINGLE_IS_CONTENT(content); + JINGLE_IS_SESSION(session); + g_object_set(content, "session", session, NULL); +} + +JingleTransport * +jingle_content_get_pending_transport(JingleContent *content) +{ + JingleTransport *pending_transport; + g_object_get(content, "pending_transport", &pending_transport, NULL); + return pending_transport; +} + +void +jingle_content_set_pending_transport(JingleContent *content, JingleTransport *transport) +{ + g_object_set(content, "pending-transport", transport, NULL); +} + +void +jingle_content_accept_transport(JingleContent *content) +{ + if (content->priv->transport) + g_object_unref(content->priv->transport); + content->priv->transport = content->priv->pending_transport; + content->priv->pending_transport = NULL; +} + +void +jingle_content_remove_pending_transport(JingleContent *content) +{ + if (content->priv->pending_transport) { + g_object_unref(content->priv->pending_transport); + content->priv->pending_transport = NULL; + } +} + +void +jingle_content_modify(JingleContent *content, const gchar *senders) +{ + g_object_set(content, "senders", senders, NULL); +} + +static JingleContent * +jingle_content_parse_internal(xmlnode *content) +{ + xmlnode *description = xmlnode_get_child(content, "description"); + const gchar *type = xmlnode_get_namespace(description); + const gchar *creator = xmlnode_get_attrib(content, "creator"); + const gchar *disposition = xmlnode_get_attrib(content, "disposition"); + const gchar *senders = xmlnode_get_attrib(content, "senders"); + const gchar *name = xmlnode_get_attrib(content, "name"); + JingleTransport *transport = + jingle_transport_parse(xmlnode_get_child(content, "transport")); + + if (senders == NULL) + senders = "both"; + + return jingle_content_create(type, creator, disposition, name, senders, transport); +} + +JingleContent * +jingle_content_parse(xmlnode *content) +{ + const gchar *type = xmlnode_get_namespace(xmlnode_get_child(content, "description")); + return JINGLE_CONTENT_CLASS(g_type_class_ref(jingle_get_type(type)))->parse(content); +} + +static xmlnode * +jingle_content_to_xml_internal(JingleContent *content, xmlnode *jingle, JingleActionType action) +{ + xmlnode *node = xmlnode_new_child(jingle, "content"); + gchar *creator = jingle_content_get_creator(content); + gchar *name = jingle_content_get_name(content); + gchar *senders = jingle_content_get_senders(content); + gchar *disposition = jingle_content_get_disposition(content); + + xmlnode_set_attrib(node, "creator", creator); + xmlnode_set_attrib(node, "name", name); + xmlnode_set_attrib(node, "senders", senders); + if (strcmp("session", disposition)) + xmlnode_set_attrib(node, "disposition", disposition); + + g_free(disposition); + g_free(senders); + g_free(name); + g_free(creator); + + if (action != JINGLE_CONTENT_REMOVE) { + JingleTransport *transport; + + if (action != JINGLE_TRANSPORT_ACCEPT && + action != JINGLE_TRANSPORT_INFO && + action != JINGLE_TRANSPORT_REJECT && + action != JINGLE_TRANSPORT_REPLACE) { + xmlnode *description = xmlnode_new_child(node, "description"); + + xmlnode_set_namespace(description, + jingle_content_get_description_type(content)); + } + + if (action != JINGLE_TRANSPORT_REJECT && action == JINGLE_TRANSPORT_REPLACE) + transport = jingle_content_get_pending_transport(content); + else + transport = jingle_content_get_transport(content); + + jingle_transport_to_xml(transport, node, action); + g_object_unref(transport); + } + + return node; +} + +xmlnode * +jingle_content_to_xml(JingleContent *content, xmlnode *jingle, JingleActionType action) +{ + g_return_val_if_fail(JINGLE_IS_CONTENT(content), NULL); + return JINGLE_CONTENT_GET_CLASS(content)->to_xml(content, jingle, action); +} + +void +jingle_content_handle_action(JingleContent *content, xmlnode *xmlcontent, JingleActionType action) +{ + g_return_if_fail(JINGLE_IS_CONTENT(content)); + JINGLE_CONTENT_GET_CLASS(content)->handle_action(content, xmlcontent, action); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libpurple/protocols/jabber/jingle/content.h Fri Apr 03 16:45:38 2009 +0000 @@ -0,0 +1,117 @@ +/** + * @file content.h + * + * purple + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA + */ + +#ifndef JINGLE_CONTENT_H +#define JINGLE_CONTENT_H + + +#include "jabber.h" +#include "jingle.h" +#include "session.h" +#include "transport.h" + +#include <glib.h> +#include <glib-object.h> + +G_BEGIN_DECLS + +#define JINGLE_TYPE_CONTENT (jingle_content_get_type()) +#define JINGLE_CONTENT(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), JINGLE_TYPE_CONTENT, JingleContent)) +#define JINGLE_CONTENT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), JINGLE_TYPE_CONTENT, JingleContentClass)) +#define JINGLE_IS_CONTENT(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), JINGLE_TYPE_CONTENT)) +#define JINGLE_IS_CONTENT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), JINGLE_TYPE_CONTENT)) +#define JINGLE_CONTENT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), JINGLE_TYPE_CONTENT, JingleContentClass)) + +/** @copydoc _JingleContent */ +typedef struct _JingleContent JingleContent; +/** @copydoc _JingleContentClass */ +typedef struct _JingleContentClass JingleContentClass; +/** @copydoc _JingleContentPrivate */ +typedef struct _JingleContentPrivate JingleContentPrivate; + +/** The content class */ +struct _JingleContentClass +{ + GObjectClass parent_class; /**< The parent class. */ + + xmlnode *(*to_xml) (JingleContent *content, xmlnode *jingle, JingleActionType action); + JingleContent *(*parse) (xmlnode *content); + void (*handle_action) (JingleContent *content, xmlnode *xmlcontent, JingleActionType action); + const gchar *description_type; +}; + +/** The content class's private data */ +struct _JingleContent +{ + GObject parent; /**< The parent of this object. */ + JingleContentPrivate *priv; /**< The private data of this object. */ +}; + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Gets the content class's GType + * + * @return The content class's GType. + */ +GType jingle_content_get_type(void); + +JingleContent *jingle_content_create(const gchar *type, const gchar *creator, + const gchar *disposition, const gchar *name, + const gchar *senders, JingleTransport *transport); + +JingleSession *jingle_content_get_session(JingleContent *content); +const gchar *jingle_content_get_description_type(JingleContent *content); +gchar *jingle_content_get_creator(JingleContent *content); +gchar *jingle_content_get_disposition(JingleContent *content); +gchar *jingle_content_get_name(JingleContent *content); +gchar *jingle_content_get_senders(JingleContent *content); +JingleTransport *jingle_content_get_transport(JingleContent *content); +JingleTransport *jingle_content_get_pending_transport(JingleContent *content); + +void jingle_content_set_session(JingleContent *content, JingleSession *session); +void jingle_content_set_pending_transport(JingleContent *content, JingleTransport *transport); +void jingle_content_accept_transport(JingleContent *content); +void jingle_content_remove_pending_transport(JingleContent *content); +void jingle_content_modify(JingleContent *content, const gchar *senders); + +#define jingle_content_create_content_accept(session) \ + jingle_session_to_packet(session, JINGLE_CONTENT_ACCEPT) +#define jingle_content_create_content_add(session) \ + jingle_session_to_packet(session, JINGLE_CONTENT_ADD) +#define jingle_content_create_content_modify(session) \ + jingle_session_to_packet(session, JINGLE_CONTENT_MODIFY) +#define jingle_content_create_content_remove(session) \ + jingle_session_to_packet(session, JINGLE_CONTENT_REMOVE) + +JingleContent *jingle_content_parse(xmlnode *content); +xmlnode *jingle_content_to_xml(JingleContent *content, xmlnode *jingle, JingleActionType action); +void jingle_content_handle_action(JingleContent *content, xmlnode *xmlcontent, JingleActionType action); + +#ifdef __cplusplus +} +#endif + +G_END_DECLS + +#endif /* JINGLE_CONTENT_H */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libpurple/protocols/jabber/jingle/iceudp.c Fri Apr 03 16:45:38 2009 +0000 @@ -0,0 +1,413 @@ +/** + * @file iceudp.c + * + * purple + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA + */ + +#include "internal.h" + +#include "iceudp.h" +#include "jingle.h" +#include "debug.h" + +#include <string.h> + +struct _JingleIceUdpPrivate +{ + GList *local_candidates; + GList *remote_candidates; +}; + +#define JINGLE_ICEUDP_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), JINGLE_TYPE_ICEUDP, JingleIceUdpPrivate)) + +static void jingle_iceudp_class_init (JingleIceUdpClass *klass); +static void jingle_iceudp_init (JingleIceUdp *iceudp); +static void jingle_iceudp_finalize (GObject *object); +static void jingle_iceudp_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec); +static void jingle_iceudp_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec); +static JingleTransport *jingle_iceudp_parse_internal(xmlnode *iceudp); +static xmlnode *jingle_iceudp_to_xml_internal(JingleTransport *transport, xmlnode *content, JingleActionType action); + +static JingleTransportClass *parent_class = NULL; + +enum { + PROP_0, + PROP_LOCAL_CANDIDATES, + PROP_REMOTE_CANDIDATES, +}; + +static JingleIceUdpCandidate * +jingle_iceudp_candidate_copy(JingleIceUdpCandidate *candidate) +{ + JingleIceUdpCandidate *new_candidate = g_new0(JingleIceUdpCandidate, 1); + new_candidate->component = candidate->component; + new_candidate->foundation = g_strdup(candidate->foundation); + new_candidate->generation = candidate->generation; + new_candidate->id = g_strdup(candidate->id); + new_candidate->ip = g_strdup(candidate->ip); + new_candidate->network = candidate->network; + new_candidate->port = candidate->port; + new_candidate->priority = candidate->priority; + new_candidate->protocol = g_strdup(candidate->protocol); + new_candidate->type = g_strdup(candidate->type); + + new_candidate->username = g_strdup(candidate->username); + new_candidate->password = g_strdup(candidate->password); + + new_candidate->rem_known = candidate->rem_known; + + return new_candidate; +} + +static void +jingle_iceudp_candidate_free(JingleIceUdpCandidate *candidate) +{ + g_free(candidate->foundation); + g_free(candidate->id); + g_free(candidate->ip); + g_free(candidate->protocol); + g_free(candidate->reladdr); + g_free(candidate->type); + + g_free(candidate->username); + g_free(candidate->password); +} + +GType +jingle_iceudp_candidate_get_type() +{ + static GType type = 0; + + if (type == 0) { + type = g_boxed_type_register_static("JingleIceUdpCandidate", + (GBoxedCopyFunc)jingle_iceudp_candidate_copy, + (GBoxedFreeFunc)jingle_iceudp_candidate_free); + } + return type; +} + +JingleIceUdpCandidate * +jingle_iceudp_candidate_new(guint component, const gchar *foundation, + guint generation, const gchar *id, const gchar *ip, + guint network, guint port, guint priority, + const gchar *protocol, const gchar *type, + const gchar *username, const gchar *password) +{ + JingleIceUdpCandidate *candidate = g_new0(JingleIceUdpCandidate, 1); + candidate->component = component; + candidate->foundation = g_strdup(foundation); + candidate->generation = generation; + candidate->id = g_strdup(id); + candidate->ip = g_strdup(ip); + candidate->network = network; + candidate->port = port; + candidate->priority = priority; + candidate->protocol = g_strdup(protocol); + candidate->type = g_strdup(type); + + candidate->username = g_strdup(username); + candidate->password = g_strdup(password); + + candidate->rem_known = FALSE; + return candidate; +} + +GType +jingle_iceudp_get_type() +{ + static GType type = 0; + + if (type == 0) { + static const GTypeInfo info = { + sizeof(JingleIceUdpClass), + NULL, + NULL, + (GClassInitFunc) jingle_iceudp_class_init, + NULL, + NULL, + sizeof(JingleIceUdp), + 0, + (GInstanceInitFunc) jingle_iceudp_init, + NULL + }; + type = g_type_register_static(JINGLE_TYPE_TRANSPORT, "JingleIceUdp", &info, 0); + } + return type; +} + +static void +jingle_iceudp_class_init (JingleIceUdpClass *klass) +{ + GObjectClass *gobject_class = (GObjectClass*)klass; + parent_class = g_type_class_peek_parent(klass); + + gobject_class->finalize = jingle_iceudp_finalize; + gobject_class->set_property = jingle_iceudp_set_property; + gobject_class->get_property = jingle_iceudp_get_property; + klass->parent_class.to_xml = jingle_iceudp_to_xml_internal; + klass->parent_class.parse = jingle_iceudp_parse_internal; + klass->parent_class.transport_type = JINGLE_TRANSPORT_ICEUDP; + + g_object_class_install_property(gobject_class, PROP_LOCAL_CANDIDATES, + g_param_spec_pointer("local-candidates", + "Local candidates", + "The local candidates for this transport.", + G_PARAM_READABLE)); + + g_object_class_install_property(gobject_class, PROP_REMOTE_CANDIDATES, + g_param_spec_pointer("remote-candidates", + "Remote candidates", + "The remote candidates for this transport.", + G_PARAM_READABLE)); + + g_type_class_add_private(klass, sizeof(JingleIceUdpPrivate)); +} + +static void +jingle_iceudp_init (JingleIceUdp *iceudp) +{ + iceudp->priv = JINGLE_ICEUDP_GET_PRIVATE(iceudp); + iceudp->priv->local_candidates = NULL; + iceudp->priv->remote_candidates = NULL; +} + +static void +jingle_iceudp_finalize (GObject *iceudp) +{ +/* JingleIceUdpPrivate *priv = JINGLE_ICEUDP_GET_PRIVATE(iceudp); */ + purple_debug_info("jingle","jingle_iceudp_finalize\n"); + + G_OBJECT_CLASS(parent_class)->finalize(iceudp); +} + +static void +jingle_iceudp_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) +{ + JingleIceUdp *iceudp; + g_return_if_fail(JINGLE_IS_ICEUDP(object)); + + iceudp = JINGLE_ICEUDP(object); + + switch (prop_id) { + case PROP_LOCAL_CANDIDATES: + iceudp->priv->local_candidates = + g_value_get_pointer(value); + break; + case PROP_REMOTE_CANDIDATES: + iceudp->priv->remote_candidates = + g_value_get_pointer(value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +jingle_iceudp_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) +{ + JingleIceUdp *iceudp; + g_return_if_fail(JINGLE_IS_ICEUDP(object)); + + iceudp = JINGLE_ICEUDP(object); + + switch (prop_id) { + case PROP_LOCAL_CANDIDATES: + g_value_set_pointer(value, iceudp->priv->local_candidates); + break; + case PROP_REMOTE_CANDIDATES: + g_value_set_pointer(value, iceudp->priv->remote_candidates); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +void +jingle_iceudp_add_local_candidate(JingleIceUdp *iceudp, JingleIceUdpCandidate *candidate) +{ + GList *iter = iceudp->priv->local_candidates; + + for (; iter; iter = g_list_next(iter)) { + JingleIceUdpCandidate *c = iter->data; + if (!strcmp(c->id, candidate->id)) { + guint generation = c->generation + 1; + + g_boxed_free(JINGLE_TYPE_ICEUDP_CANDIDATE, c); + iceudp->priv->local_candidates = g_list_delete_link( + iceudp->priv->local_candidates, iter); + + candidate->generation = generation; + + iceudp->priv->local_candidates = g_list_append( + iceudp->priv->local_candidates, candidate); + return; + } + } + + iceudp->priv->local_candidates = g_list_append( + iceudp->priv->local_candidates, candidate); +} + +GList * +jingle_iceudp_get_remote_candidates(JingleIceUdp *iceudp) +{ + return g_list_copy(iceudp->priv->remote_candidates); +} + +static JingleIceUdpCandidate * +jingle_iceudp_get_remote_candidate_by_id(JingleIceUdp *iceudp, + const gchar *id) +{ + GList *iter = iceudp->priv->remote_candidates; + for (; iter; iter = g_list_next(iter)) { + JingleIceUdpCandidate *candidate = iter->data; + if (!strcmp(candidate->id, id)) { + return candidate; + } + } + return NULL; +} + +static void +jingle_iceudp_add_remote_candidate(JingleIceUdp *iceudp, JingleIceUdpCandidate *candidate) +{ + JingleIceUdpPrivate *priv = JINGLE_ICEUDP_GET_PRIVATE(iceudp); + JingleIceUdpCandidate *iceudp_candidate = + jingle_iceudp_get_remote_candidate_by_id(iceudp, + candidate->id); + if (iceudp_candidate != NULL) { + priv->remote_candidates = g_list_remove( + priv->remote_candidates, iceudp_candidate); + g_boxed_free(JINGLE_TYPE_ICEUDP_CANDIDATE, iceudp_candidate); + } + priv->remote_candidates = g_list_append(priv->remote_candidates, candidate); +} + +static JingleTransport * +jingle_iceudp_parse_internal(xmlnode *iceudp) +{ + JingleTransport *transport = parent_class->parse(iceudp); + xmlnode *candidate = xmlnode_get_child(iceudp, "candidate"); + JingleIceUdpCandidate *iceudp_candidate = NULL; + + const gchar *username = xmlnode_get_attrib(iceudp, "ufrag"); + const gchar *password = xmlnode_get_attrib(iceudp, "pwd"); + + for (; candidate; candidate = xmlnode_get_next_twin(candidate)) { + const gchar *relport = + xmlnode_get_attrib(candidate, "rel-port"); + iceudp_candidate = jingle_iceudp_candidate_new( + atoi(xmlnode_get_attrib(candidate, "component")), + xmlnode_get_attrib(candidate, "foundation"), + atoi(xmlnode_get_attrib(candidate, "generation")), + xmlnode_get_attrib(candidate, "id"), + xmlnode_get_attrib(candidate, "ip"), + atoi(xmlnode_get_attrib(candidate, "network")), + atoi(xmlnode_get_attrib(candidate, "port")), + atoi(xmlnode_get_attrib(candidate, "priority")), + xmlnode_get_attrib(candidate, "protocol"), + xmlnode_get_attrib(candidate, "type"), + username, password); + iceudp_candidate->reladdr = g_strdup( + xmlnode_get_attrib(candidate, "rel-addr")); + iceudp_candidate->relport = + relport != NULL ? atoi(relport) : 0; + iceudp_candidate->rem_known = TRUE; + jingle_iceudp_add_remote_candidate(JINGLE_ICEUDP(transport), iceudp_candidate); + } + + return transport; +} + +static xmlnode * +jingle_iceudp_to_xml_internal(JingleTransport *transport, xmlnode *content, JingleActionType action) +{ + xmlnode *node = parent_class->to_xml(transport, content, action); + + if (action == JINGLE_SESSION_INITIATE || + action == JINGLE_SESSION_ACCEPT || + action == JINGLE_TRANSPORT_INFO || + action == JINGLE_CONTENT_ADD || + action == JINGLE_TRANSPORT_REPLACE) { + JingleIceUdpPrivate *priv = JINGLE_ICEUDP_GET_PRIVATE(transport); + GList *iter = priv->local_candidates; + gboolean used_candidate = FALSE; + + for (; iter; iter = g_list_next(iter)) { + JingleIceUdpCandidate *candidate = iter->data; + xmlnode *xmltransport; + gchar *component, *generation, *network, + *port, *priority; + + if (candidate->rem_known == TRUE) + continue; + + used_candidate = TRUE; + candidate->rem_known = TRUE; + + xmltransport = xmlnode_new_child(node, "candidate"); + component = g_strdup_printf("%d", candidate->component); + generation = g_strdup_printf("%d", + candidate->generation); + network = g_strdup_printf("%d", candidate->network); + port = g_strdup_printf("%d", candidate->port); + priority = g_strdup_printf("%d", candidate->priority); + + xmlnode_set_attrib(xmltransport, "component", component); + xmlnode_set_attrib(xmltransport, "foundation", candidate->foundation); + xmlnode_set_attrib(xmltransport, "generation", generation); + xmlnode_set_attrib(xmltransport, "id", candidate->id); + xmlnode_set_attrib(xmltransport, "ip", candidate->ip); + xmlnode_set_attrib(xmltransport, "network", network); + xmlnode_set_attrib(xmltransport, "port", port); + xmlnode_set_attrib(xmltransport, "priority", priority); + xmlnode_set_attrib(xmltransport, "protocol", candidate->protocol); + + if (candidate->reladdr != NULL && + (strcmp(candidate->ip, candidate->reladdr) || + (candidate->port != candidate->relport))) { + gchar *relport = g_strdup_printf("%d", + candidate->relport); + xmlnode_set_attrib(xmltransport, "rel-addr", + candidate->reladdr); + xmlnode_set_attrib(xmltransport, "rel-port", + relport); + g_free(relport); + } + + xmlnode_set_attrib(xmltransport, "type", candidate->type); + + g_free(component); + g_free(generation); + g_free(network); + g_free(port); + g_free(priority); + } + + if (used_candidate == TRUE) { + JingleIceUdpCandidate *candidate = + priv->local_candidates->data; + xmlnode_set_attrib(node, "pwd", candidate->password); + xmlnode_set_attrib(node, "ufrag", candidate->username); + } + } + + return node; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libpurple/protocols/jabber/jingle/iceudp.h Fri Apr 03 16:45:38 2009 +0000 @@ -0,0 +1,114 @@ +/** + * @file iceudp.h + * + * purple + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA + */ + +#ifndef JINGLE_ICEUDP_H +#define JINGLE_ICEUDP_H + +#include <glib.h> +#include <glib-object.h> + +#include "transport.h" + +G_BEGIN_DECLS + +#define JINGLE_TYPE_ICEUDP (jingle_iceudp_get_type()) +#define JINGLE_TYPE_ICEUDP_CANDIDATE (jingle_iceudp_candidate_get_type()) +#define JINGLE_ICEUDP(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), JINGLE_TYPE_ICEUDP, JingleIceUdp)) +#define JINGLE_ICEUDP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), JINGLE_TYPE_ICEUDP, JingleIceUdpClass)) +#define JINGLE_IS_ICEUDP(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), JINGLE_TYPE_ICEUDP)) +#define JINGLE_IS_ICEUDP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), JINGLE_TYPE_ICEUDP)) +#define JINGLE_ICEUDP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), JINGLE_TYPE_ICEUDP, JingleIceUdpClass)) + +/** @copydoc _JingleIceUdp */ +typedef struct _JingleIceUdp JingleIceUdp; +/** @copydoc _JingleIceUdpClass */ +typedef struct _JingleIceUdpClass JingleIceUdpClass; +/** @copydoc _JingleIceUdpPrivate */ +typedef struct _JingleIceUdpPrivate JingleIceUdpPrivate; +/** @copydoc _JingleIceUdpCandidate */ +typedef struct _JingleIceUdpCandidate JingleIceUdpCandidate; + +/** The iceudp class */ +struct _JingleIceUdpClass +{ + JingleTransportClass parent_class; /**< The parent class. */ + + xmlnode *(*to_xml) (JingleTransport *transport, xmlnode *content, JingleActionType action); + JingleTransport *(*parse) (xmlnode *transport); +}; + +/** The iceudp class's private data */ +struct _JingleIceUdp +{ + JingleTransport parent; /**< The parent of this object. */ + JingleIceUdpPrivate *priv; /**< The private data of this object. */ +}; + +struct _JingleIceUdpCandidate +{ + guint component; + gchar *foundation; + guint generation; + gchar *id; + gchar *ip; + guint network; + guint port; + guint priority; + gchar *protocol; + gchar *reladdr; + guint relport; + gchar *type; + + gchar *username; + gchar *password; + + gboolean rem_known; /* TRUE if the remote side knows + * about this candidate */ +}; + +#ifdef __cplusplus +extern "C" { +#endif + +GType jingle_iceudp_candidate_get_type(void); + +/** + * Gets the iceudp class's GType + * + * @return The iceudp class's GType. + */ +GType jingle_iceudp_get_type(void); + +JingleIceUdpCandidate *jingle_iceudp_candidate_new(guint component, + const gchar *foundation, guint generation, const gchar *id, + const gchar *ip, guint network, guint port, guint priority, + const gchar *protocol, const gchar *type, + const gchar *username, const gchar *password); +void jingle_iceudp_add_local_candidate(JingleIceUdp *iceudp, JingleIceUdpCandidate *candidate); +GList *jingle_iceudp_get_remote_candidates(JingleIceUdp *iceudp); + +#ifdef __cplusplus +} +#endif + +G_END_DECLS + +#endif /* JINGLE_ICEUDP_H */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libpurple/protocols/jabber/jingle/jingle.c Fri Apr 03 16:45:38 2009 +0000 @@ -0,0 +1,463 @@ +/* + * @file jingle.c + * + * purple - Jabber Protocol Plugin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA + * + */ + +#include "internal.h" +#include "network.h" + +#include "content.h" +#include "debug.h" +#include "jingle.h" +#include <string.h> +#include "session.h" +#include "iceudp.h" +#include "rawudp.h" +#include "rtp.h" + +GType +jingle_get_type(const gchar *type) +{ + if (!strcmp(type, JINGLE_TRANSPORT_RAWUDP)) + return JINGLE_TYPE_RAWUDP; + else if (!strcmp(type, JINGLE_TRANSPORT_ICEUDP)) + return JINGLE_TYPE_ICEUDP; +#if 0 + else if (!strcmp(type, JINGLE_TRANSPORT_SOCKS)) + return JINGLE_TYPE_SOCKS; + else if (!strcmp(type, JINGLE_TRANSPORT_IBB)) + return JINGLE_TYPE_IBB; +#endif +#ifdef USE_VV + else if (!strcmp(type, JINGLE_APP_RTP)) + return JINGLE_TYPE_RTP; +#endif +#if 0 + else if (!strcmp(type, JINGLE_APP_FT)) + return JINGLE_TYPE_FT; + else if (!strcmp(type, JINGLE_APP_XML)) + return JINGLE_TYPE_XML; +#endif + else + return G_TYPE_NONE; +} + +static void +jingle_handle_unknown_type(JingleSession *session, xmlnode *jingle) +{ + /* Send error */ +} + +static void +jingle_handle_content_accept(JingleSession *session, xmlnode *jingle) +{ + xmlnode *content = xmlnode_get_child(jingle, "content"); + jabber_iq_send(jingle_session_create_ack(session, jingle)); + + for (; content; content = xmlnode_get_next_twin(content)) { + const gchar *name = xmlnode_get_attrib(content, "name"); + const gchar *creator = xmlnode_get_attrib(content, "creator"); + jingle_session_accept_content(session, name, creator); + /* signal here */ + } +} + +static void +jingle_handle_content_add(JingleSession *session, xmlnode *jingle) +{ + xmlnode *content = xmlnode_get_child(jingle, "content"); + jabber_iq_send(jingle_session_create_ack(session, jingle)); + + for (; content; content = xmlnode_get_next_twin(content)) { + JingleContent *pending_content = + jingle_content_parse(content); + if (pending_content == NULL) { + purple_debug_error("jingle", + "Error parsing \"content-add\" content.\n"); + /* XXX: send error here */ + } else { + jingle_session_add_pending_content(session, + pending_content); + } + } + + /* XXX: signal here */ +} + +static void +jingle_handle_content_modify(JingleSession *session, xmlnode *jingle) +{ + xmlnode *content = xmlnode_get_child(jingle, "content"); + jabber_iq_send(jingle_session_create_ack(session, jingle)); + + for (; content; content = xmlnode_get_next_twin(content)) { + const gchar *name = xmlnode_get_attrib(content, "name"); + const gchar *creator = xmlnode_get_attrib(content, "creator"); + JingleContent *local_content = jingle_session_find_content(session, name, creator); + + if (content != NULL) { + const gchar *senders = xmlnode_get_attrib(content, "senders"); + gchar *local_senders = jingle_content_get_senders(local_content); + if (strcmp(senders, local_senders)) + jingle_content_modify(local_content, senders); + g_free(local_senders); + } else { + purple_debug_error("jingle", "content_modify: unknown content\n"); + /* XXX: send error */ + } + } +} + +static void +jingle_handle_content_reject(JingleSession *session, xmlnode *jingle) +{ + xmlnode *content = xmlnode_get_child(jingle, "content"); + jabber_iq_send(jingle_session_create_ack(session, jingle)); + + for (; content; content = xmlnode_get_next_twin(content)) { + const gchar *name = xmlnode_get_attrib(content, "name"); + const gchar *creator = xmlnode_get_attrib(content, "creator"); + jingle_session_remove_pending_content(session, name, creator); + /* signal here */ + } +} + +static void +jingle_handle_content_remove(JingleSession *session, xmlnode *jingle) +{ + xmlnode *content = xmlnode_get_child(jingle, "content"); + + jabber_iq_send(jingle_session_create_ack(session, jingle)); + + for (; content; content = xmlnode_get_next_twin(content)) { + const gchar *name = xmlnode_get_attrib(content, "name"); + const gchar *creator = xmlnode_get_attrib(content, "creator"); + jingle_session_remove_content(session, name, creator); + } +} + +static void +jingle_handle_description_info(JingleSession *session, xmlnode *jingle) +{ + xmlnode *content = xmlnode_get_child(jingle, "content"); + + jabber_iq_send(jingle_session_create_ack(session, jingle)); + + jingle_session_accept_session(session); + + for (; content; content = xmlnode_get_next_twin(content)) { + const gchar *name = xmlnode_get_attrib(content, "name"); + const gchar *creator = xmlnode_get_attrib(content, "creator"); + JingleContent *parsed_content = + jingle_session_find_content(session, name, creator); + if (parsed_content == NULL) { + purple_debug_error("jingle", "Error parsing content\n"); + /* XXX: send error */ + } else { + jingle_content_handle_action(parsed_content, content, + JINGLE_DESCRIPTION_INFO); + } + } +} + +static void +jingle_handle_security_info(JingleSession *session, xmlnode *jingle) +{ + jabber_iq_send(jingle_session_create_ack(session, jingle)); +} + +static void +jingle_handle_session_accept(JingleSession *session, xmlnode *jingle) +{ + xmlnode *content = xmlnode_get_child(jingle, "content"); + + jabber_iq_send(jingle_session_create_ack(session, jingle)); + + jingle_session_accept_session(session); + + for (; content; content = xmlnode_get_next_twin(content)) { + const gchar *name = xmlnode_get_attrib(content, "name"); + const gchar *creator = xmlnode_get_attrib(content, "creator"); + JingleContent *parsed_content = + jingle_session_find_content(session, name, creator); + if (parsed_content == NULL) { + purple_debug_error("jingle", "Error parsing content\n"); + /* XXX: send error */ + } else { + jingle_content_handle_action(parsed_content, content, + JINGLE_SESSION_ACCEPT); + } + } +} + +static void +jingle_handle_session_info(JingleSession *session, xmlnode *jingle) +{ + jabber_iq_send(jingle_session_create_ack(session, jingle)); + /* XXX: call signal */ +} + +static void +jingle_handle_session_initiate(JingleSession *session, xmlnode *jingle) +{ + xmlnode *content = xmlnode_get_child(jingle, "content"); + + for (; content; content = xmlnode_get_next_twin(content)) { + JingleContent *parsed_content = jingle_content_parse(content); + if (parsed_content == NULL) { + purple_debug_error("jingle", "Error parsing content\n"); + /* XXX: send error */ + } else { + jingle_session_add_content(session, parsed_content); + jingle_content_handle_action(parsed_content, content, + JINGLE_SESSION_INITIATE); + } + } + + jabber_iq_send(jingle_session_create_ack(session, jingle)); +} + +static void +jingle_handle_session_terminate(JingleSession *session, xmlnode *jingle) +{ + jabber_iq_send(jingle_session_create_ack(session, jingle)); + + jingle_session_handle_action(session, jingle, + JINGLE_SESSION_TERMINATE); + /* display reason? */ + g_object_unref(session); +} + +static void +jingle_handle_transport_accept(JingleSession *session, xmlnode *jingle) +{ + xmlnode *content = xmlnode_get_child(jingle, "content"); + + jabber_iq_send(jingle_session_create_ack(session, jingle)); + + for (; content; content = xmlnode_get_next_twin(content)) { + const gchar *name = xmlnode_get_attrib(content, "name"); + const gchar *creator = xmlnode_get_attrib(content, "creator"); + JingleContent *content = jingle_session_find_content(session, name, creator); + jingle_content_accept_transport(content); + } +} + +static void +jingle_handle_transport_info(JingleSession *session, xmlnode *jingle) +{ + xmlnode *content = xmlnode_get_child(jingle, "content"); + + jabber_iq_send(jingle_session_create_ack(session, jingle)); + + for (; content; content = xmlnode_get_next_twin(content)) { + const gchar *name = xmlnode_get_attrib(content, "name"); + const gchar *creator = xmlnode_get_attrib(content, "creator"); + JingleContent *parsed_content = + jingle_session_find_content(session, name, creator); + if (parsed_content == NULL) { + purple_debug_error("jingle", "Error parsing content\n"); + /* XXX: send error */ + } else { + jingle_content_handle_action(parsed_content, content, + JINGLE_TRANSPORT_INFO); + } + } +} + +static void +jingle_handle_transport_reject(JingleSession *session, xmlnode *jingle) +{ + xmlnode *content = xmlnode_get_child(jingle, "content"); + + jabber_iq_send(jingle_session_create_ack(session, jingle)); + + for (; content; content = xmlnode_get_next_twin(content)) { + const gchar *name = xmlnode_get_attrib(content, "name"); + const gchar *creator = xmlnode_get_attrib(content, "creator"); + JingleContent *content = jingle_session_find_content(session, name, creator); + jingle_content_remove_pending_transport(content); + } +} + +static void +jingle_handle_transport_replace(JingleSession *session, xmlnode *jingle) +{ + xmlnode *content = xmlnode_get_child(jingle, "content"); + + jabber_iq_send(jingle_session_create_ack(session, jingle)); + + for (; content; content = xmlnode_get_next_twin(content)) { + const gchar *name = xmlnode_get_attrib(content, "name"); + const gchar *creator = xmlnode_get_attrib(content, "creator"); + xmlnode *xmltransport = xmlnode_get_child(content, "transport"); + JingleTransport *transport = jingle_transport_parse(xmltransport); + JingleContent *content = jingle_session_find_content(session, name, creator); + + jingle_content_set_pending_transport(content, transport); + } +} + +typedef struct { + const char *name; + void (*handler)(JingleSession*, xmlnode*); +} JingleAction; + +static const JingleAction jingle_actions[] = { + {"unknown-type", jingle_handle_unknown_type}, + {"content-accept", jingle_handle_content_accept}, + {"content-add", jingle_handle_content_add}, + {"content-modify", jingle_handle_content_modify}, + {"content-reject", jingle_handle_content_reject}, + {"content-remove", jingle_handle_content_remove}, + {"description-info", jingle_handle_description_info}, + {"security-info", jingle_handle_security_info}, + {"session-accept", jingle_handle_session_accept}, + {"session-info", jingle_handle_session_info}, + {"session-initiate", jingle_handle_session_initiate}, + {"session-terminate", jingle_handle_session_terminate}, + {"transport-accept", jingle_handle_transport_accept}, + {"transport-info", jingle_handle_transport_info}, + {"transport-reject", jingle_handle_transport_reject}, + {"transport-replace", jingle_handle_transport_replace}, +}; + +const gchar * +jingle_get_action_name(JingleActionType action) +{ + return jingle_actions[action].name; +} + +JingleActionType +jingle_get_action_type(const gchar *action) +{ + static const int num_actions = + sizeof(jingle_actions)/sizeof(JingleAction); + /* Start at 1 to skip the unknown-action type */ + int i = 1; + for (; i < num_actions; ++i) { + if (!strcmp(action, jingle_actions[i].name)) + return i; + } + return JINGLE_UNKNOWN_TYPE; +} + +void +jingle_parse(JabberStream *js, xmlnode *packet) +{ + const gchar *type = xmlnode_get_attrib(packet, "type"); + xmlnode *jingle; + const gchar *action; + const gchar *sid; + JingleActionType action_type; + JingleSession *session; + + if (!type || strcmp(type, "set")) { + /* send iq error here */ + return; + } + + /* is this a Jingle package? */ + if (!(jingle = xmlnode_get_child(packet, "jingle"))) { + /* send iq error here */ + return; + } + + if (!(action = xmlnode_get_attrib(jingle, "action"))) { + /* send iq error here */ + return; + } + + action_type = jingle_get_action_type(action); + + purple_debug_info("jabber", "got Jingle package action = %s\n", + action); + + if (!(sid = xmlnode_get_attrib(jingle, "sid"))) { + /* send iq error here */ + return; + } + + if (!(session = jingle_session_find_by_sid(js, sid)) + && strcmp(action, "session-initiate")) { + purple_debug_error("jingle", "jabber_jingle_session_parse couldn't find session\n"); + /* send iq error here */ + return; + } + + if (action_type == JINGLE_SESSION_INITIATE) { + if (session) { + /* This should only happen if you start a session with yourself */ + purple_debug_error("jingle", "Jingle session with " + "id={%s} already exists\n", sid); + /* send iq error */ + return; + } else { + session = jingle_session_create(js, sid, + xmlnode_get_attrib(packet, "to"), + xmlnode_get_attrib(packet, "from"), FALSE); + } + } + + jingle_actions[action_type].handler(session, jingle); +} + +static void +jingle_terminate_sessions_gh(gpointer key, gpointer value, gpointer user_data) +{ + g_object_unref(value); +} + +void +jingle_terminate_sessions(JabberStream *js) +{ + if (js->sessions) + g_hash_table_foreach(js->sessions, + jingle_terminate_sessions_gh, NULL); +} + +GParameter * +jingle_get_params(JabberStream *js, guint *num) +{ + /* don't set a STUN server if one is set globally in prefs, in that case + this will be handled in media.c */ + gboolean has_account_stun = js->stun_ip && !purple_network_get_stun_ip(); + guint num_params = has_account_stun ? 2 : 0; + GParameter *params = NULL; + + if (num_params > 0) { + params = g_new0(GParameter, num_params); + + purple_debug_info("jabber", + "setting param stun-ip for stream using Google auto-config: %s\n", + js->stun_ip); + params[0].name = "stun-ip"; + g_value_init(¶ms[0].value, G_TYPE_STRING); + g_value_set_string(¶ms[0].value, js->stun_ip); + purple_debug_info("jabber", + "setting param stun-port for stream using Google auto-config: %d\n", + js->stun_port); + params[1].name = "stun-port"; + g_value_init(¶ms[1].value, G_TYPE_UINT); + g_value_set_uint(¶ms[1].value, js->stun_port); + } + + *num = num_params; + return params; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libpurple/protocols/jabber/jingle/jingle.h Fri Apr 03 16:45:38 2009 +0000 @@ -0,0 +1,86 @@ +/* + * @file jingle.h + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA + */ + +#ifndef JINGLE_H +#define JINGLE_H + +#include "jabber.h" + +#include <glib.h> +#include <glib-object.h> + +G_BEGIN_DECLS + +#ifdef __cplusplus +extern "C" { +#endif + +#define JINGLE "urn:xmpp:jingle:1" +#define JINGLE_ERROR "urn:xmpp:jingle:errors:0" +#define JINGLE_APP_FT "urn:xmpp:jingle:apps:file-transfer:1" +#define JINGLE_APP_RTP "urn:xmpp:jingle:apps:rtp:1" +#define JINGLE_APP_RTP_ERROR "urn:xmpp:jingle:apps:rtp:errors:1" +#define JINGLE_APP_RTP_INFO "urn:xmpp:jingle:apps:rtp:info:1" +#define JINGLE_APP_RTP_SUPPORT_AUDIO "urn:xmpp:jingle:apps:rtp:audio" +#define JINGLE_APP_RTP_SUPPORT_VIDEO "urn:xmpp:jingle:apps:rtp:video" +#define JINGLE_APP_XML "urn:xmpp:tmp:jingle:apps:xmlstream" +#define JINGLE_DTMF "urn:xmpp:jingle:dtmf:0" +#define JINGLE_TRANSPORT_S5B "urn:xmpp:jingle:transports:s5b:0" +#define JINGLE_TRANSPORT_IBB "urn:xmpp:jingle:transports:ibb:0" +#define JINGLE_TRANSPORT_ICEUDP "urn:xmpp:jingle:transports:ice-udp:1" +#define JINGLE_TRANSPORT_RAWUDP "urn:xmpp:jingle:transports:raw-udp:1" + +typedef enum { + JINGLE_UNKNOWN_TYPE, + JINGLE_CONTENT_ACCEPT, + JINGLE_CONTENT_ADD, + JINGLE_CONTENT_MODIFY, + JINGLE_CONTENT_REJECT, + JINGLE_CONTENT_REMOVE, + JINGLE_DESCRIPTION_INFO, + JINGLE_SECURITY_INFO, + JINGLE_SESSION_ACCEPT, + JINGLE_SESSION_INFO, + JINGLE_SESSION_INITIATE, + JINGLE_SESSION_TERMINATE, + JINGLE_TRANSPORT_ACCEPT, + JINGLE_TRANSPORT_INFO, + JINGLE_TRANSPORT_REJECT, + JINGLE_TRANSPORT_REPLACE, +} JingleActionType; + +const gchar *jingle_get_action_name(JingleActionType action); +JingleActionType jingle_get_action_type(const gchar *action); + +GType jingle_get_type(const gchar *type); + +void jingle_parse(JabberStream *js, xmlnode *packet); + +void jingle_terminate_sessions(JabberStream *js); + +/* create a GParam array given autoconfigured STUN (and later perhaps TURN). + if google_talk is TRUE, set compatability mode to GOOGLE_TALK */ +GParameter *jingle_get_params(JabberStream *js, guint *num_params); + +#ifdef __cplusplus +} +#endif + +G_END_DECLS + +#endif /* JINGLE_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libpurple/protocols/jabber/jingle/rawudp.c Fri Apr 03 16:45:38 2009 +0000 @@ -0,0 +1,342 @@ +/** + * @file rawudp.c + * + * purple + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA + */ + +#include "internal.h" + +#include "rawudp.h" +#include "jingle.h" +#include "debug.h" + +#include <string.h> + +struct _JingleRawUdpPrivate +{ + GList *local_candidates; + GList *remote_candidates; +}; + +#define JINGLE_RAWUDP_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), JINGLE_TYPE_RAWUDP, JingleRawUdpPrivate)) + +static void jingle_rawudp_class_init (JingleRawUdpClass *klass); +static void jingle_rawudp_init (JingleRawUdp *rawudp); +static void jingle_rawudp_finalize (GObject *object); +static void jingle_rawudp_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec); +static void jingle_rawudp_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec); +static JingleTransport *jingle_rawudp_parse_internal(xmlnode *rawudp); +static xmlnode *jingle_rawudp_to_xml_internal(JingleTransport *transport, xmlnode *content, JingleActionType action); + +static JingleTransportClass *parent_class = NULL; + +enum { + PROP_0, + PROP_LOCAL_CANDIDATES, + PROP_REMOTE_CANDIDATES, +}; + +static JingleRawUdpCandidate * +jingle_rawudp_candidate_copy(JingleRawUdpCandidate *candidate) +{ + JingleRawUdpCandidate *new_candidate = g_new0(JingleRawUdpCandidate, 1); + new_candidate->generation = candidate->generation; + new_candidate->component = candidate->component; + new_candidate->id = g_strdup(candidate->id); + new_candidate->ip = g_strdup(candidate->ip); + new_candidate->port = candidate->port; + + new_candidate->rem_known = candidate->rem_known; + return new_candidate; +} + +static void +jingle_rawudp_candidate_free(JingleRawUdpCandidate *candidate) +{ + g_free(candidate->id); + g_free(candidate->ip); +} + +GType +jingle_rawudp_candidate_get_type() +{ + static GType type = 0; + + if (type == 0) { + type = g_boxed_type_register_static("JingleRawUdpCandidate", + (GBoxedCopyFunc)jingle_rawudp_candidate_copy, + (GBoxedFreeFunc)jingle_rawudp_candidate_free); + } + return type; +} + +JingleRawUdpCandidate * +jingle_rawudp_candidate_new(const gchar *id, guint generation, guint component, const gchar *ip, guint port) +{ + JingleRawUdpCandidate *candidate = g_new0(JingleRawUdpCandidate, 1); + candidate->generation = generation; + candidate->component = component; + candidate->id = g_strdup(id); + candidate->ip = g_strdup(ip); + candidate->port = port; + + candidate->rem_known = FALSE; + return candidate; +} + +GType +jingle_rawudp_get_type() +{ + static GType type = 0; + + if (type == 0) { + static const GTypeInfo info = { + sizeof(JingleRawUdpClass), + NULL, + NULL, + (GClassInitFunc) jingle_rawudp_class_init, + NULL, + NULL, + sizeof(JingleRawUdp), + 0, + (GInstanceInitFunc) jingle_rawudp_init, + NULL + }; + type = g_type_register_static(JINGLE_TYPE_TRANSPORT, "JingleRawUdp", &info, 0); + } + return type; +} + +static void +jingle_rawudp_class_init (JingleRawUdpClass *klass) +{ + GObjectClass *gobject_class = (GObjectClass*)klass; + parent_class = g_type_class_peek_parent(klass); + + gobject_class->finalize = jingle_rawudp_finalize; + gobject_class->set_property = jingle_rawudp_set_property; + gobject_class->get_property = jingle_rawudp_get_property; + klass->parent_class.to_xml = jingle_rawudp_to_xml_internal; + klass->parent_class.parse = jingle_rawudp_parse_internal; + klass->parent_class.transport_type = JINGLE_TRANSPORT_RAWUDP; + + g_object_class_install_property(gobject_class, PROP_LOCAL_CANDIDATES, + g_param_spec_pointer("local-candidates", + "Local candidates", + "The local candidates for this transport.", + G_PARAM_READABLE)); + + g_object_class_install_property(gobject_class, PROP_REMOTE_CANDIDATES, + g_param_spec_pointer("remote-candidates", + "Remote candidates", + "The remote candidates for this transport.", + G_PARAM_READABLE)); + + g_type_class_add_private(klass, sizeof(JingleRawUdpPrivate)); +} + +static void +jingle_rawudp_init (JingleRawUdp *rawudp) +{ + rawudp->priv = JINGLE_RAWUDP_GET_PRIVATE(rawudp); + rawudp->priv->local_candidates = NULL; + rawudp->priv->remote_candidates = NULL; +} + +static void +jingle_rawudp_finalize (GObject *rawudp) +{ +/* JingleRawUdpPrivate *priv = JINGLE_RAWUDP_GET_PRIVATE(rawudp); */ + purple_debug_info("jingle","jingle_rawudp_finalize\n"); + + G_OBJECT_CLASS(parent_class)->finalize(rawudp); +} + +static void +jingle_rawudp_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) +{ + JingleRawUdp *rawudp; + g_return_if_fail(JINGLE_IS_RAWUDP(object)); + + rawudp = JINGLE_RAWUDP(object); + + switch (prop_id) { + case PROP_LOCAL_CANDIDATES: + rawudp->priv->local_candidates = + g_value_get_pointer(value); + break; + case PROP_REMOTE_CANDIDATES: + rawudp->priv->remote_candidates = + g_value_get_pointer(value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +jingle_rawudp_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) +{ + JingleRawUdp *rawudp; + g_return_if_fail(JINGLE_IS_RAWUDP(object)); + + rawudp = JINGLE_RAWUDP(object); + + switch (prop_id) { + case PROP_LOCAL_CANDIDATES: + g_value_set_pointer(value, rawudp->priv->local_candidates); + break; + case PROP_REMOTE_CANDIDATES: + g_value_set_pointer(value, rawudp->priv->remote_candidates); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +void +jingle_rawudp_add_local_candidate(JingleRawUdp *rawudp, JingleRawUdpCandidate *candidate) +{ + GList *iter = rawudp->priv->local_candidates; + + for (; iter; iter = g_list_next(iter)) { + JingleRawUdpCandidate *c = iter->data; + if (!strcmp(c->id, candidate->id)) { + guint generation = c->generation + 1; + + g_boxed_free(JINGLE_TYPE_RAWUDP_CANDIDATE, c); + rawudp->priv->local_candidates = g_list_delete_link( + rawudp->priv->local_candidates, iter); + + candidate->generation = generation; + + rawudp->priv->local_candidates = g_list_append( + rawudp->priv->local_candidates, candidate); + return; + } + } + + rawudp->priv->local_candidates = g_list_append( + rawudp->priv->local_candidates, candidate); +} + +GList * +jingle_rawudp_get_remote_candidates(JingleRawUdp *rawudp) +{ + return g_list_copy(rawudp->priv->remote_candidates); +} + +static JingleRawUdpCandidate * +jingle_rawudp_get_remote_candidate_by_id(JingleRawUdp *rawudp, gchar *id) +{ + GList *iter = rawudp->priv->remote_candidates; + for (; iter; iter = g_list_next(iter)) { + JingleRawUdpCandidate *candidate = iter->data; + if (!strcmp(candidate->id, id)) { + return candidate; + } + } + return NULL; +} + +static void +jingle_rawudp_add_remote_candidate(JingleRawUdp *rawudp, JingleRawUdpCandidate *candidate) +{ + JingleRawUdpPrivate *priv = JINGLE_RAWUDP_GET_PRIVATE(rawudp); + JingleRawUdpCandidate *rawudp_candidate = + jingle_rawudp_get_remote_candidate_by_id(rawudp, candidate->id); + if (rawudp_candidate != NULL) { + priv->remote_candidates = g_list_remove( + priv->remote_candidates, rawudp_candidate); + g_boxed_free(JINGLE_TYPE_RAWUDP_CANDIDATE, rawudp_candidate); + } + priv->remote_candidates = g_list_append(priv->remote_candidates, candidate); +} + +static JingleTransport * +jingle_rawudp_parse_internal(xmlnode *rawudp) +{ + JingleTransport *transport = parent_class->parse(rawudp); + JingleRawUdpPrivate *priv = JINGLE_RAWUDP_GET_PRIVATE(transport); + xmlnode *candidate = xmlnode_get_child(rawudp, "candidate"); + JingleRawUdpCandidate *rawudp_candidate = NULL; + + for (; candidate; candidate = xmlnode_get_next_twin(candidate)) { + rawudp_candidate = jingle_rawudp_candidate_new( + xmlnode_get_attrib(candidate, "id"), + atoi(xmlnode_get_attrib(candidate, "generation")), + atoi(xmlnode_get_attrib(candidate, "component")), + xmlnode_get_attrib(candidate, "ip"), + atoi(xmlnode_get_attrib(candidate, "port"))); + rawudp_candidate->rem_known = TRUE; + jingle_rawudp_add_remote_candidate(JINGLE_RAWUDP(transport), rawudp_candidate); + } + + if (rawudp_candidate != NULL && + g_list_length(priv->remote_candidates) == 1) { + /* manufacture rtcp candidate */ + rawudp_candidate = g_boxed_copy(JINGLE_TYPE_RAWUDP_CANDIDATE, rawudp_candidate); + rawudp_candidate->component = 2; + rawudp_candidate->port = rawudp_candidate->port + 1; + rawudp_candidate->rem_known = TRUE; + jingle_rawudp_add_remote_candidate(JINGLE_RAWUDP(transport), rawudp_candidate); + } + + return transport; +} + +static xmlnode * +jingle_rawudp_to_xml_internal(JingleTransport *transport, xmlnode *content, JingleActionType action) +{ + xmlnode *node = parent_class->to_xml(transport, content, action); + + if (action == JINGLE_SESSION_INITIATE || + action == JINGLE_TRANSPORT_INFO || + action == JINGLE_SESSION_ACCEPT) { + JingleRawUdpPrivate *priv = JINGLE_RAWUDP_GET_PRIVATE(transport); + GList *iter = priv->local_candidates; + + for (; iter; iter = g_list_next(iter)) { + JingleRawUdpCandidate *candidate = iter->data; + xmlnode *xmltransport; + gchar *generation, *component, *port; + + if (candidate->rem_known == TRUE) + continue; + candidate->rem_known = TRUE; + + xmltransport = xmlnode_new_child(node, "candidate"); + generation = g_strdup_printf("%d", candidate->generation); + component = g_strdup_printf("%d", candidate->component); + port = g_strdup_printf("%d", candidate->port); + + xmlnode_set_attrib(xmltransport, "generation", generation); + xmlnode_set_attrib(xmltransport, "component", component); + xmlnode_set_attrib(xmltransport, "id", candidate->id); + xmlnode_set_attrib(xmltransport, "ip", candidate->ip); + xmlnode_set_attrib(xmltransport, "port", port); + + g_free(port); + g_free(generation); + } + } + + return node; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libpurple/protocols/jabber/jingle/rawudp.h Fri Apr 03 16:45:38 2009 +0000 @@ -0,0 +1,101 @@ +/** + * @file rawudp.h + * + * purple + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA + */ + +#ifndef JINGLE_RAWUDP_H +#define JINGLE_RAWUDP_H + +#include <glib.h> +#include <glib-object.h> + +#include "transport.h" + +G_BEGIN_DECLS + +#define JINGLE_TYPE_RAWUDP (jingle_rawudp_get_type()) +#define JINGLE_TYPE_RAWUDP_CANDIDATE (jingle_rawudp_candidate_get_type()) +#define JINGLE_RAWUDP(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), JINGLE_TYPE_RAWUDP, JingleRawUdp)) +#define JINGLE_RAWUDP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), JINGLE_TYPE_RAWUDP, JingleRawUdpClass)) +#define JINGLE_IS_RAWUDP(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), JINGLE_TYPE_RAWUDP)) +#define JINGLE_IS_RAWUDP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), JINGLE_TYPE_RAWUDP)) +#define JINGLE_RAWUDP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), JINGLE_TYPE_RAWUDP, JingleRawUdpClass)) + +/** @copydoc _JingleRawUdp */ +typedef struct _JingleRawUdp JingleRawUdp; +/** @copydoc _JingleRawUdpClass */ +typedef struct _JingleRawUdpClass JingleRawUdpClass; +/** @copydoc _JingleRawUdpPrivate */ +typedef struct _JingleRawUdpPrivate JingleRawUdpPrivate; +/** @copydoc _JingleRawUdpCandidate */ +typedef struct _JingleRawUdpCandidate JingleRawUdpCandidate; + +/** The rawudp class */ +struct _JingleRawUdpClass +{ + JingleTransportClass parent_class; /**< The parent class. */ + + xmlnode *(*to_xml) (JingleTransport *transport, xmlnode *content, JingleActionType action); + JingleTransport *(*parse) (xmlnode *transport); +}; + +/** The rawudp class's private data */ +struct _JingleRawUdp +{ + JingleTransport parent; /**< The parent of this object. */ + JingleRawUdpPrivate *priv; /**< The private data of this object. */ +}; + +struct _JingleRawUdpCandidate +{ + guint generation; + guint component; + gchar *id; + gchar *ip; + guint port; + + gboolean rem_known; /* TRUE if the remote side knows + * about this candidate */ +}; + +#ifdef __cplusplus +extern "C" { +#endif + +GType jingle_rawudp_candidate_get_type(void); + +/** + * Gets the rawudp class's GType + * + * @return The rawudp class's GType. + */ +GType jingle_rawudp_get_type(void); + +JingleRawUdpCandidate *jingle_rawudp_candidate_new(const gchar *id, + guint generation, guint component, const gchar *ip, guint port); +void jingle_rawudp_add_local_candidate(JingleRawUdp *rawudp, JingleRawUdpCandidate *candidate); +GList *jingle_rawudp_get_remote_candidates(JingleRawUdp *rawudp); + +#ifdef __cplusplus +} +#endif + +G_END_DECLS + +#endif /* JINGLE_RAWUDP_H */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libpurple/protocols/jabber/jingle/rtp.c Fri Apr 03 16:45:38 2009 +0000 @@ -0,0 +1,920 @@ +/** + * @file rtp.c + * + * purple + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA + */ + +#include "config.h" + +#ifdef USE_VV + +#include "jabber.h" +#include "jingle.h" +#include "media.h" +#include "mediamanager.h" +#include "iceudp.h" +#include "rawudp.h" +#include "rtp.h" +#include "session.h" +#include "debug.h" + +#include <string.h> + +struct _JingleRtpPrivate +{ + gchar *media_type; + gchar *ssrc; +}; + +#define JINGLE_RTP_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), JINGLE_TYPE_RTP, JingleRtpPrivate)) + +static void jingle_rtp_class_init (JingleRtpClass *klass); +static void jingle_rtp_init (JingleRtp *rtp); +static void jingle_rtp_finalize (GObject *object); +static void jingle_rtp_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec); +static void jingle_rtp_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec); +static JingleContent *jingle_rtp_parse_internal(xmlnode *rtp); +static xmlnode *jingle_rtp_to_xml_internal(JingleContent *rtp, xmlnode *content, JingleActionType action); +static void jingle_rtp_handle_action_internal(JingleContent *content, xmlnode *jingle, JingleActionType action); + +static PurpleMedia *jingle_rtp_get_media(JingleSession *session); + +static JingleContentClass *parent_class = NULL; +#if 0 +enum { + LAST_SIGNAL +}; +static guint jingle_rtp_signals[LAST_SIGNAL] = {0}; +#endif + +enum { + PROP_0, + PROP_MEDIA_TYPE, + PROP_SSRC, +}; + +GType +jingle_rtp_get_type() +{ + static GType type = 0; + + if (type == 0) { + static const GTypeInfo info = { + sizeof(JingleRtpClass), + NULL, + NULL, + (GClassInitFunc) jingle_rtp_class_init, + NULL, + NULL, + sizeof(JingleRtp), + 0, + (GInstanceInitFunc) jingle_rtp_init, + NULL + }; + type = g_type_register_static(JINGLE_TYPE_CONTENT, "JingleRtp", &info, 0); + } + return type; +} + +static void +jingle_rtp_class_init (JingleRtpClass *klass) +{ + GObjectClass *gobject_class = (GObjectClass*)klass; + parent_class = g_type_class_peek_parent(klass); + + gobject_class->finalize = jingle_rtp_finalize; + gobject_class->set_property = jingle_rtp_set_property; + gobject_class->get_property = jingle_rtp_get_property; + klass->parent_class.to_xml = jingle_rtp_to_xml_internal; + klass->parent_class.parse = jingle_rtp_parse_internal; + klass->parent_class.description_type = JINGLE_APP_RTP; + klass->parent_class.handle_action = jingle_rtp_handle_action_internal; + + g_object_class_install_property(gobject_class, PROP_MEDIA_TYPE, + g_param_spec_string("media-type", + "Media Type", + "The media type (\"audio\" or \"video\") for this rtp session.", + NULL, + G_PARAM_READWRITE)); + g_object_class_install_property(gobject_class, PROP_SSRC, + g_param_spec_string("ssrc", + "ssrc", + "The ssrc for this rtp session.", + NULL, + G_PARAM_READWRITE)); + + g_type_class_add_private(klass, sizeof(JingleRtpPrivate)); +} + +static void +jingle_rtp_init (JingleRtp *rtp) +{ + rtp->priv = JINGLE_RTP_GET_PRIVATE(rtp); + memset(rtp->priv, 0, sizeof(*rtp->priv)); +} + +static void +jingle_rtp_finalize (GObject *rtp) +{ + JingleRtpPrivate *priv = JINGLE_RTP_GET_PRIVATE(rtp); + purple_debug_info("jingle-rtp","jingle_rtp_finalize\n"); + + g_free(priv->media_type); + g_free(priv->ssrc); + + G_OBJECT_CLASS(parent_class)->finalize(rtp); +} + +static void +jingle_rtp_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) +{ + JingleRtp *rtp; + g_return_if_fail(JINGLE_IS_RTP(object)); + + rtp = JINGLE_RTP(object); + + switch (prop_id) { + case PROP_MEDIA_TYPE: + g_free(rtp->priv->media_type); + rtp->priv->media_type = g_value_dup_string(value); + break; + case PROP_SSRC: + g_free(rtp->priv->ssrc); + rtp->priv->ssrc = g_value_dup_string(value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +jingle_rtp_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) +{ + JingleRtp *rtp; + g_return_if_fail(JINGLE_IS_RTP(object)); + + rtp = JINGLE_RTP(object); + + switch (prop_id) { + case PROP_MEDIA_TYPE: + g_value_set_string(value, rtp->priv->media_type); + break; + case PROP_SSRC: + g_value_set_string(value, rtp->priv->ssrc); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +gchar * +jingle_rtp_get_media_type(JingleContent *content) +{ + gchar *media_type; + g_object_get(content, "media-type", &media_type, NULL); + return media_type; +} + +gchar * +jingle_rtp_get_ssrc(JingleContent *content) +{ + gchar *ssrc; + g_object_get(content, "ssrc", &ssrc, NULL); + return ssrc; +} + +static PurpleMedia * +jingle_rtp_get_media(JingleSession *session) +{ + JabberStream *js = jingle_session_get_js(session); + PurpleMedia *media = NULL; + GList *iter = purple_media_manager_get_media_by_connection( + purple_media_manager_get(), js->gc); + + for (; iter; iter = g_list_delete_link(iter, iter)) { + JingleSession *media_session = + purple_media_get_prpl_data(iter->data); + if (media_session == session) { + media = iter->data; + break; + } + } + if (iter != NULL) + g_list_free(iter); + + return media; +} + +static JingleRawUdpCandidate * +jingle_rtp_candidate_to_rawudp(JingleSession *session, guint generation, + PurpleMediaCandidate *candidate) +{ + gchar *id = jabber_get_next_id(jingle_session_get_js(session)); + gchar *ip = purple_media_candidate_get_ip(candidate); + JingleRawUdpCandidate *rawudp_candidate = + jingle_rawudp_candidate_new(id, generation, + purple_media_candidate_get_component_id(candidate), + ip, purple_media_candidate_get_port(candidate)); + g_free(ip); + g_free(id); + return rawudp_candidate; +} + +static JingleIceUdpCandidate * +jingle_rtp_candidate_to_iceudp(JingleSession *session, guint generation, + PurpleMediaCandidate *candidate) +{ + gchar *id = jabber_get_next_id(jingle_session_get_js(session)); + gchar *ip = purple_media_candidate_get_ip(candidate); + gchar *username = purple_media_candidate_get_username(candidate); + gchar *password = purple_media_candidate_get_password(candidate); + PurpleMediaCandidateType type = + purple_media_candidate_get_candidate_type(candidate); + + JingleIceUdpCandidate *iceudp_candidate = jingle_iceudp_candidate_new( + purple_media_candidate_get_component_id(candidate), + purple_media_candidate_get_foundation(candidate), + generation, id, ip, 0, + purple_media_candidate_get_port(candidate), + purple_media_candidate_get_priority(candidate), "udp", + type == PURPLE_MEDIA_CANDIDATE_TYPE_HOST ? "host" : + type == PURPLE_MEDIA_CANDIDATE_TYPE_SRFLX ? "srflx" : + type == PURPLE_MEDIA_CANDIDATE_TYPE_PRFLX ? "prflx" : + type == PURPLE_MEDIA_CANDIDATE_TYPE_RELAY ? "relay" : + "", username, password); + iceudp_candidate->reladdr = + purple_media_candidate_get_base_ip(candidate); + iceudp_candidate->relport = + purple_media_candidate_get_base_port(candidate); + g_free(password); + g_free(username); + g_free(ip); + g_free(id); + return iceudp_candidate; +} + +static JingleTransport * +jingle_rtp_candidates_to_transport(JingleSession *session, GType type, guint generation, GList *candidates) +{ + if (type == JINGLE_TYPE_RAWUDP) { + JingleTransport *transport = jingle_transport_create(JINGLE_TRANSPORT_RAWUDP); + JingleRawUdpCandidate *rawudp_candidate; + for (; candidates; candidates = g_list_next(candidates)) { + PurpleMediaCandidate *candidate = candidates->data; + rawudp_candidate = jingle_rtp_candidate_to_rawudp( + session, generation, candidate); + jingle_rawudp_add_local_candidate( + JINGLE_RAWUDP(transport), + rawudp_candidate); + } + return transport; + } else if (type == JINGLE_TYPE_ICEUDP) { + JingleTransport *transport = jingle_transport_create(JINGLE_TRANSPORT_ICEUDP); + JingleIceUdpCandidate *iceudp_candidate; + for (; candidates; candidates = g_list_next(candidates)) { + PurpleMediaCandidate *candidate = candidates->data; + iceudp_candidate = jingle_rtp_candidate_to_iceudp( + session, generation, candidate); + jingle_iceudp_add_local_candidate( + JINGLE_ICEUDP(transport), + iceudp_candidate); + } + return transport; + } else { + return NULL; + } +} + +static GList * +jingle_rtp_transport_to_candidates(JingleTransport *transport) +{ + const gchar *type = jingle_transport_get_transport_type(transport); + GList *ret = NULL; + if (!strcmp(type, JINGLE_TRANSPORT_RAWUDP)) { + GList *candidates = jingle_rawudp_get_remote_candidates(JINGLE_RAWUDP(transport)); + + for (; candidates; candidates = g_list_delete_link(candidates, candidates)) { + JingleRawUdpCandidate *candidate = candidates->data; + ret = g_list_append(ret, purple_media_candidate_new( + "", candidate->component, + PURPLE_MEDIA_CANDIDATE_TYPE_SRFLX, + PURPLE_MEDIA_NETWORK_PROTOCOL_UDP, + candidate->ip, candidate->port)); + } + + return ret; + } else if (!strcmp(type, JINGLE_TRANSPORT_ICEUDP)) { + GList *candidates = jingle_iceudp_get_remote_candidates(JINGLE_ICEUDP(transport)); + + for (; candidates; candidates = g_list_delete_link(candidates, candidates)) { + JingleIceUdpCandidate *candidate = candidates->data; + PurpleMediaCandidate *new_candidate = purple_media_candidate_new( + candidate->foundation, candidate->component, + !strcmp(candidate->type, "host") ? + PURPLE_MEDIA_CANDIDATE_TYPE_HOST : + !strcmp(candidate->type, "srflx") ? + PURPLE_MEDIA_CANDIDATE_TYPE_SRFLX : + !strcmp(candidate->type, "prflx") ? + PURPLE_MEDIA_CANDIDATE_TYPE_PRFLX : + !strcmp(candidate->type, "relay") ? + PURPLE_MEDIA_CANDIDATE_TYPE_RELAY : 0, + PURPLE_MEDIA_NETWORK_PROTOCOL_UDP, + candidate->ip, candidate->port); + g_object_set(new_candidate, + "base-ip", candidate->reladdr, + "base-port", candidate->relport, + "username", candidate->username, + "password", candidate->password, + "priority", candidate->priority, NULL); + ret = g_list_append(ret, new_candidate); + } + + return ret; + } else { + return NULL; + } +} + +static void jingle_rtp_ready(JingleSession *session); + +static void +jingle_rtp_accepted_cb(PurpleMedia *media, gchar *sid, gchar *name, + JingleSession *session) +{ + purple_debug_info("jingle-rtp", "jingle_rtp_accepted_cb\n"); + jingle_rtp_ready(session); +} + +static void +jingle_rtp_candidates_prepared_cb(PurpleMedia *media, + gchar *sid, gchar *name, JingleSession *session) +{ + JingleContent *content = jingle_session_find_content( + session, sid, NULL); + JingleTransport *oldtransport, *transport; + GList *candidates; + + purple_debug_info("jingle-rtp", "jingle_rtp_candidates_prepared_cb\n"); + + if (content == NULL) { + purple_debug_error("jingle-rtp", + "jingle_rtp_candidates_prepared_cb: " + "Can't find session %s\n", sid); + return; + } + + oldtransport = jingle_content_get_transport(content); + candidates = purple_media_get_local_candidates(media, sid, name); + transport = JINGLE_TRANSPORT(jingle_rtp_candidates_to_transport( + session, JINGLE_IS_RAWUDP(oldtransport) ? + JINGLE_TYPE_RAWUDP : JINGLE_TYPE_ICEUDP, + 0, candidates)); + + g_list_free(candidates); + g_object_unref(oldtransport); + + jingle_content_set_pending_transport(content, transport); + jingle_content_accept_transport(content); + + jingle_rtp_ready(session); +} + +static void +jingle_rtp_codecs_changed_cb(PurpleMedia *media, gchar *sid, + JingleSession *session) +{ + purple_debug_info("jingle-rtp", "jingle_rtp_codecs_changed_cb: " + "session_id: %s jingle_session: %p\n", sid, session); + jingle_rtp_ready(session); +} + +static void +jingle_rtp_new_candidate_cb(PurpleMedia *media, gchar *sid, gchar *name, PurpleMediaCandidate *candidate, JingleSession *session) +{ + JingleContent *content = jingle_session_find_content( + session, sid, NULL); + JingleTransport *transport; + + purple_debug_info("jingle-rtp", "jingle_rtp_new_candidate_cb\n"); + + if (content == NULL) { + purple_debug_error("jingle-rtp", + "jingle_rtp_new_candidate_cb: " + "Can't find session %s\n", sid); + return; + } + + transport = jingle_content_get_transport(content); + + if (JINGLE_IS_ICEUDP(transport)) + jingle_iceudp_add_local_candidate(JINGLE_ICEUDP(transport), + jingle_rtp_candidate_to_iceudp( + session, 1, candidate)); + else if (JINGLE_IS_RAWUDP(transport)) + jingle_rawudp_add_local_candidate(JINGLE_RAWUDP(transport), + jingle_rtp_candidate_to_rawudp( + session, 1, candidate)); + + g_object_unref(transport); + + jabber_iq_send(jingle_session_to_packet(session, + JINGLE_TRANSPORT_INFO)); +} + +static void +jingle_rtp_initiate_ack_cb(JabberStream *js, xmlnode *packet, gpointer data) +{ + JingleSession *session = data; + + if (!strcmp(xmlnode_get_attrib(packet, "type"), "error") || + xmlnode_get_child(packet, "error")) { + purple_media_end(jingle_rtp_get_media(session), NULL, NULL); + g_object_unref(session); + return; + } +} + +static void +jingle_rtp_state_changed_cb(PurpleMedia *media, PurpleMediaState state, + gchar *sid, gchar *name, JingleSession *session) +{ + purple_debug_info("jingle-rtp", "state-changed: state %d " + "id: %s name: %s\n", state, sid, name); +} + +static void +jingle_rtp_stream_info_cb(PurpleMedia *media, PurpleMediaInfoType type, + gchar *sid, gchar *name, gboolean local, + JingleSession *session) +{ + purple_debug_info("jingle-rtp", "stream-info: type %d " + "id: %s name: %s\n", type, sid, name); + if (type == PURPLE_MEDIA_INFO_HANGUP) { + jabber_iq_send(jingle_session_terminate_packet( + session, "success")); + g_object_unref(session); + } else if (type == PURPLE_MEDIA_INFO_REJECT) { + jabber_iq_send(jingle_session_terminate_packet( + session, "decline")); + g_object_unref(session); + } +} + +static void +jingle_rtp_ready(JingleSession *session) +{ + PurpleMedia *media = jingle_rtp_get_media(session); + + if (purple_media_candidates_prepared(media, NULL, NULL) && + purple_media_codecs_ready(media, NULL) && + (jingle_session_is_initiator(session) == TRUE || + purple_media_accepted(media, NULL, NULL))) { + if (jingle_session_is_initiator(session)) { + JabberIq *iq = jingle_session_to_packet( + session, JINGLE_SESSION_INITIATE); + jabber_iq_set_callback(iq, + jingle_rtp_initiate_ack_cb, session); + jabber_iq_send(iq); + } else { + jabber_iq_send(jingle_session_to_packet(session, + JINGLE_SESSION_ACCEPT)); + } + + g_signal_handlers_disconnect_by_func(G_OBJECT(media), + G_CALLBACK(jingle_rtp_accepted_cb), session); + g_signal_handlers_disconnect_by_func(G_OBJECT(media), + G_CALLBACK(jingle_rtp_candidates_prepared_cb), + session); + g_signal_handlers_disconnect_by_func(G_OBJECT(media), + G_CALLBACK(jingle_rtp_codecs_changed_cb), + session); + g_signal_connect(G_OBJECT(media), "new-candidate", + G_CALLBACK(jingle_rtp_new_candidate_cb), + session); + } +} + +static PurpleMedia * +jingle_rtp_create_media(JingleContent *content) +{ + JingleSession *session = jingle_content_get_session(content); + JabberStream *js = jingle_session_get_js(session); + gchar *remote_jid = jingle_session_get_remote_jid(session); + + PurpleMedia *media = purple_media_manager_create_media(purple_media_manager_get(), + js->gc, "fsrtpconference", remote_jid, + jingle_session_is_initiator(session)); + g_free(remote_jid); + + if (!media) { + purple_debug_error("jingle-rtp", "Couldn't create media session\n"); + return NULL; + } + + purple_media_set_prpl_data(media, session); + + /* connect callbacks */ + if (jingle_session_is_initiator(session) == FALSE) + g_signal_connect(G_OBJECT(media), "accepted", + G_CALLBACK(jingle_rtp_accepted_cb), session); + g_signal_connect(G_OBJECT(media), "candidates-prepared", + G_CALLBACK(jingle_rtp_candidates_prepared_cb), session); + g_signal_connect(G_OBJECT(media), "codecs-changed", + G_CALLBACK(jingle_rtp_codecs_changed_cb), session); + g_signal_connect(G_OBJECT(media), "state-changed", + G_CALLBACK(jingle_rtp_state_changed_cb), session); + g_signal_connect(G_OBJECT(media), "stream-info", + G_CALLBACK(jingle_rtp_stream_info_cb), session); + + g_object_unref(session); + return media; +} + +static gboolean +jingle_rtp_init_media(JingleContent *content) +{ + JingleSession *session = jingle_content_get_session(content); + PurpleMedia *media = jingle_rtp_get_media(session); + gchar *creator; + gchar *media_type; + gchar *remote_jid; + gchar *senders; + gchar *name; + const gchar *transmitter; + gboolean is_audio; + gboolean is_creator; + PurpleMediaSessionType type; + JingleTransport *transport; + GParameter *params = NULL; + guint num_params; + + /* maybe this create ought to just be in initiate and handle initiate */ + if (media == NULL) + media = jingle_rtp_create_media(content); + + if (media == NULL) + return FALSE; + + name = jingle_content_get_name(content); + media_type = jingle_rtp_get_media_type(content); + remote_jid = jingle_session_get_remote_jid(session); + senders = jingle_content_get_senders(content); + transport = jingle_content_get_transport(content); + + if (JINGLE_IS_RAWUDP(transport)) + transmitter = "rawudp"; + else if (JINGLE_IS_ICEUDP(transport)) + transmitter = "nice"; + else + transmitter = "notransmitter"; + g_object_unref(transport); + + is_audio = !strcmp(media_type, "audio"); + + if (!strcmp(senders, "both")) + type = is_audio == TRUE ? PURPLE_MEDIA_AUDIO + : PURPLE_MEDIA_VIDEO; + else if (!strcmp(senders, "initiator") + && jingle_session_is_initiator(session)) + type = is_audio == TRUE ? PURPLE_MEDIA_SEND_AUDIO + : PURPLE_MEDIA_SEND_VIDEO; + else + type = is_audio == TRUE ? PURPLE_MEDIA_RECV_AUDIO + : PURPLE_MEDIA_RECV_VIDEO; + + params = + jingle_get_params(jingle_session_get_js(session), &num_params); + + creator = jingle_content_get_creator(content); + if (!strcmp(creator, "initiator")) + is_creator = jingle_session_is_initiator(session); + else + is_creator = !jingle_session_is_initiator(session); + g_free(creator); + + purple_media_add_stream(media, name, remote_jid, + type, is_creator, transmitter, num_params, params); + + g_free(name); + g_free(media_type); + g_free(remote_jid); + g_free(senders); + g_free(params); + g_object_unref(session); + + return TRUE; +} + +static GList * +jingle_rtp_parse_codecs(xmlnode *description) +{ + GList *codecs = NULL; + xmlnode *codec_element = NULL; + const char *encoding_name,*id, *clock_rate; + PurpleMediaCodec *codec; + const gchar *media = xmlnode_get_attrib(description, "media"); + PurpleMediaSessionType type = + !strcmp(media, "video") ? PURPLE_MEDIA_VIDEO : + !strcmp(media, "audio") ? PURPLE_MEDIA_AUDIO : 0; + + for (codec_element = xmlnode_get_child(description, "payload-type") ; + codec_element ; + codec_element = xmlnode_get_next_twin(codec_element)) { + xmlnode *param; + gchar *codec_str; + encoding_name = xmlnode_get_attrib(codec_element, "name"); + + id = xmlnode_get_attrib(codec_element, "id"); + clock_rate = xmlnode_get_attrib(codec_element, "clockrate"); + + codec = purple_media_codec_new(atoi(id), encoding_name, + type, + clock_rate ? atoi(clock_rate) : 0); + + for (param = xmlnode_get_child(codec_element, "parameter"); + param; param = xmlnode_get_next_twin(param)) { + purple_media_codec_add_optional_parameter(codec, + xmlnode_get_attrib(param, "name"), + xmlnode_get_attrib(param, "value")); + } + + codec_str = purple_media_codec_to_string(codec); + purple_debug_info("jingle-rtp", "received codec: %s\n", codec_str); + g_free(codec_str); + + codecs = g_list_append(codecs, codec); + } + return codecs; +} + +static JingleContent * +jingle_rtp_parse_internal(xmlnode *rtp) +{ + JingleContent *content = parent_class->parse(rtp); + xmlnode *description = xmlnode_get_child(rtp, "description"); + const gchar *media_type = xmlnode_get_attrib(description, "media"); + const gchar *ssrc = xmlnode_get_attrib(description, "ssrc"); + purple_debug_info("jingle-rtp", "rtp parse\n"); + g_object_set(content, "media-type", media_type, NULL); + if (ssrc != NULL) + g_object_set(content, "ssrc", ssrc, NULL); + return content; +} + +static void +jingle_rtp_add_payloads(xmlnode *description, GList *codecs) +{ + for (; codecs ; codecs = codecs->next) { + PurpleMediaCodec *codec = (PurpleMediaCodec*)codecs->data; + GList *iter = purple_media_codec_get_optional_parameters(codec); + gchar *id, *name, *clockrate, *channels; + gchar *codec_str; + xmlnode *payload = xmlnode_new_child(description, "payload-type"); + + id = g_strdup_printf("%d", + purple_media_codec_get_id(codec)); + name = purple_media_codec_get_encoding_name(codec); + clockrate = g_strdup_printf("%d", + purple_media_codec_get_clock_rate(codec)); + channels = g_strdup_printf("%d", + purple_media_codec_get_channels(codec)); + + xmlnode_set_attrib(payload, "name", name); + xmlnode_set_attrib(payload, "id", id); + xmlnode_set_attrib(payload, "clockrate", clockrate); + xmlnode_set_attrib(payload, "channels", channels); + + g_free(channels); + g_free(clockrate); + g_free(name); + g_free(id); + + for (; iter; iter = g_list_next(iter)) { + PurpleKeyValuePair *mparam = iter->data; + xmlnode *param = xmlnode_new_child(payload, "parameter"); + xmlnode_set_attrib(param, "name", mparam->key); + xmlnode_set_attrib(param, "value", mparam->value); + } + + codec_str = purple_media_codec_to_string(codec); + purple_debug_info("jingle", "adding codec: %s\n", codec_str); + g_free(codec_str); + } +} + +static xmlnode * +jingle_rtp_to_xml_internal(JingleContent *rtp, xmlnode *content, JingleActionType action) +{ + xmlnode *node = parent_class->to_xml(rtp, content, action); + xmlnode *description = xmlnode_get_child(node, "description"); + if (description != NULL) { + JingleSession *session = jingle_content_get_session(rtp); + PurpleMedia *media = jingle_rtp_get_media(session); + gchar *media_type = jingle_rtp_get_media_type(rtp); + gchar *ssrc = jingle_rtp_get_ssrc(rtp); + gchar *name = jingle_content_get_name(rtp); + GList *codecs = purple_media_get_codecs(media, name); + + xmlnode_set_attrib(description, "media", media_type); + + if (ssrc != NULL) + xmlnode_set_attrib(description, "ssrc", ssrc); + + g_free(media_type); + g_free(name); + g_object_unref(session); + + jingle_rtp_add_payloads(description, codecs); + purple_media_codec_list_free(codecs); + } + return node; +} + +static void +jingle_rtp_handle_action_internal(JingleContent *content, xmlnode *xmlcontent, JingleActionType action) +{ + switch (action) { + case JINGLE_SESSION_ACCEPT: + case JINGLE_SESSION_INITIATE: { + JingleSession *session = jingle_content_get_session(content); + JingleTransport *transport = jingle_transport_parse( + xmlnode_get_child(xmlcontent, "transport")); + xmlnode *description = xmlnode_get_child(xmlcontent, "description"); + GList *candidates = jingle_rtp_transport_to_candidates(transport); + GList *codecs = jingle_rtp_parse_codecs(description); + gchar *name = jingle_content_get_name(content); + gchar *remote_jid = + jingle_session_get_remote_jid(session); + PurpleMedia *media; + + if (action == JINGLE_SESSION_INITIATE && + jingle_rtp_init_media(content) == FALSE) { + /* XXX: send error */ + jabber_iq_send(jingle_session_terminate_packet( + session, "general-error")); + g_object_unref(session); + break; + } + + media = jingle_rtp_get_media(session); + purple_media_set_remote_codecs(media, + name, remote_jid, codecs); + purple_media_add_remote_candidates(media, + name, remote_jid, candidates); + + if (action == JINGLE_SESSION_ACCEPT) + purple_media_stream_info(media, + PURPLE_MEDIA_INFO_ACCEPT, + name, remote_jid, FALSE); + + g_free(remote_jid); + g_free(name); + g_object_unref(session); + break; + } + case JINGLE_SESSION_TERMINATE: { + JingleSession *session = jingle_content_get_session(content); + PurpleMedia *media = jingle_rtp_get_media(session); + + if (media != NULL) { + purple_media_end(media, NULL, NULL); + } + + g_object_unref(session); + break; + } + case JINGLE_TRANSPORT_INFO: { + JingleSession *session = jingle_content_get_session(content); + JingleTransport *transport = jingle_transport_parse( + xmlnode_get_child(xmlcontent, "transport")); + GList *candidates = jingle_rtp_transport_to_candidates(transport); + gchar *name = jingle_content_get_name(content); + gchar *remote_jid = + jingle_session_get_remote_jid(session); + + purple_media_add_remote_candidates( + jingle_rtp_get_media(session), + name, remote_jid, candidates); + + g_free(remote_jid); + g_free(name); + g_object_unref(session); + break; + } + default: + break; + } +} + +gboolean +jingle_rtp_initiate_media(JabberStream *js, const gchar *who, + PurpleMediaSessionType type) +{ + /* create content negotiation */ + JingleSession *session; + JingleContent *content; + JingleTransport *transport; + JabberBuddy *jb; + JabberBuddyResource *jbr; + const gchar *transport_type; + + gchar *jid = NULL, *me = NULL, *sid = NULL; + + /* construct JID to send to */ + jb = jabber_buddy_find(js, who, FALSE); + if (!jb) { + purple_debug_error("jingle-rtp", "Could not find Jabber buddy\n"); + return FALSE; + } + jbr = jabber_buddy_find_resource(jb, NULL); + if (!jbr) { + purple_debug_error("jingle-rtp", "Could not find buddy's resource\n"); + } + + if (jabber_resource_has_capability(jbr, JINGLE_TRANSPORT_ICEUDP)) { + transport_type = JINGLE_TRANSPORT_ICEUDP; + } else if (jabber_resource_has_capability(jbr, JINGLE_TRANSPORT_RAWUDP)) { + transport_type = JINGLE_TRANSPORT_RAWUDP; + } else { + purple_debug_error("jingle-rtp", "Resource doesn't support " + "the same transport types\n"); + return FALSE; + } + + if ((strchr(who, '/') == NULL) && jbr && (jbr->name != NULL)) { + jid = g_strdup_printf("%s/%s", who, jbr->name); + } else { + jid = g_strdup(who); + } + + /* set ourselves as initiator */ + me = g_strdup_printf("%s@%s/%s", js->user->node, js->user->domain, js->user->resource); + + sid = jabber_get_next_id(js); + session = jingle_session_create(js, sid, me, jid, TRUE); + g_free(sid); + + + if (type & PURPLE_MEDIA_AUDIO) { + transport = jingle_transport_create(transport_type); + content = jingle_content_create(JINGLE_APP_RTP, "initiator", + "session", "audio-session", "both", transport); + jingle_session_add_content(session, content); + JINGLE_RTP(content)->priv->media_type = g_strdup("audio"); + jingle_rtp_init_media(content); + } + if (type & PURPLE_MEDIA_VIDEO) { + transport = jingle_transport_create(transport_type); + content = jingle_content_create(JINGLE_APP_RTP, "initiator", + "session", "video-session", "both", transport); + jingle_session_add_content(session, content); + JINGLE_RTP(content)->priv->media_type = g_strdup("video"); + jingle_rtp_init_media(content); + } + + g_free(jid); + g_free(me); + + if (jingle_rtp_get_media(session) == NULL) { + return FALSE; + } + + return TRUE; +} + +void +jingle_rtp_terminate_session(JabberStream *js, const gchar *who) +{ + JingleSession *session; +/* XXX: This may cause file transfers and xml sessions to stop as well */ + session = jingle_session_find_by_jid(js, who); + + if (session) { + PurpleMedia *media = jingle_rtp_get_media(session); + if (media) { + purple_debug_info("jingle-rtp", "hanging up media\n"); + purple_media_stream_info(media, + PURPLE_MEDIA_INFO_HANGUP, + NULL, NULL, TRUE); + } + } +} + +#endif /* USE_VV */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libpurple/protocols/jabber/jingle/rtp.h Fri Apr 03 16:45:38 2009 +0000 @@ -0,0 +1,92 @@ +/** + * @file rtp.h + * + * purple + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA + */ + +#ifndef JINGLE_RTP_H +#define JINGLE_RTP_H + +#include "config.h" + +#ifdef USE_VV + +#include <glib.h> +#include <glib-object.h> + +#include "content.h" +#include "media.h" +#include "xmlnode.h" + +G_BEGIN_DECLS + +#define JINGLE_TYPE_RTP (jingle_rtp_get_type()) +#define JINGLE_RTP(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), JINGLE_TYPE_RTP, JingleRtp)) +#define JINGLE_RTP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), JINGLE_TYPE_RTP, JingleRtpClass)) +#define JINGLE_IS_RTP(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), JINGLE_TYPE_RTP)) +#define JINGLE_IS_RTP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), JINGLE_TYPE_RTP)) +#define JINGLE_RTP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), JINGLE_TYPE_RTP, JingleRtpClass)) + +/** @copydoc _JingleRtp */ +typedef struct _JingleRtp JingleRtp; +/** @copydoc _JingleRtpClass */ +typedef struct _JingleRtpClass JingleRtpClass; +/** @copydoc _JingleRtpPrivate */ +typedef struct _JingleRtpPrivate JingleRtpPrivate; + +/** The rtp class */ +struct _JingleRtpClass +{ + JingleContentClass parent_class; /**< The parent class. */ +}; + +/** The rtp class's private data */ +struct _JingleRtp +{ + JingleContent parent; /**< The parent of this object. */ + JingleRtpPrivate *priv; /**< The private data of this object. */ +}; + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Gets the rtp class's GType + * + * @return The rtp class's GType. + */ +GType jingle_rtp_get_type(void); + +gchar *jingle_rtp_get_media_type(JingleContent *content); +gchar *jingle_rtp_get_ssrc(JingleContent *content); + +gboolean jingle_rtp_initiate_media(JabberStream *js, + const gchar *who, + PurpleMediaSessionType type); +void jingle_rtp_terminate_session(JabberStream *js, const gchar *who); + +#ifdef __cplusplus +} +#endif + +G_END_DECLS + +#endif /* USE_VV */ + +#endif /* JINGLE_RTP_H */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libpurple/protocols/jabber/jingle/session.c Fri Apr 03 16:45:38 2009 +0000 @@ -0,0 +1,633 @@ +/** + * @file session.c + * + * purple + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA + */ + +#include "internal.h" + +#include "content.h" +#include "debug.h" +#include "session.h" +#include "jingle.h" + +#include <string.h> + +struct _JingleSessionPrivate +{ + gchar *sid; + JabberStream *js; + gchar *remote_jid; + gchar *local_jid; + gboolean is_initiator; + gboolean state; + GList *contents; + GList *pending_contents; +}; + +#define JINGLE_SESSION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), JINGLE_TYPE_SESSION, JingleSessionPrivate)) + +static void jingle_session_class_init (JingleSessionClass *klass); +static void jingle_session_init (JingleSession *session); +static void jingle_session_finalize (GObject *object); +static void jingle_session_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec); +static void jingle_session_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec); + +static GObjectClass *parent_class = NULL; + +enum { + PROP_0, + PROP_SID, + PROP_JS, + PROP_REMOTE_JID, + PROP_LOCAL_JID, + PROP_IS_INITIATOR, + PROP_STATE, + PROP_CONTENTS, + PROP_PENDING_CONTENTS, +}; + +GType +jingle_session_get_type() +{ + static GType type = 0; + + if (type == 0) { + static const GTypeInfo info = { + sizeof(JingleSessionClass), + NULL, + NULL, + (GClassInitFunc) jingle_session_class_init, + NULL, + NULL, + sizeof(JingleSession), + 0, + (GInstanceInitFunc) jingle_session_init, + NULL + }; + type = g_type_register_static(G_TYPE_OBJECT, "JingleSession", &info, 0); + } + return type; +} + +static void +jingle_session_class_init (JingleSessionClass *klass) +{ + GObjectClass *gobject_class = (GObjectClass*)klass; + parent_class = g_type_class_peek_parent(klass); + + gobject_class->finalize = jingle_session_finalize; + gobject_class->set_property = jingle_session_set_property; + gobject_class->get_property = jingle_session_get_property; + + g_object_class_install_property(gobject_class, PROP_SID, + g_param_spec_string("sid", + "Session ID", + "The unique session ID of the Jingle Session.", + NULL, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_JS, + g_param_spec_pointer("js", + "JabberStream", + "The Jabber stream associated with this session.", + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_REMOTE_JID, + g_param_spec_string("remote-jid", + "Remote JID", + "The JID of the remote participant.", + NULL, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_LOCAL_JID, + g_param_spec_string("local-jid", + "Local JID", + "The JID of the local participant.", + NULL, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_IS_INITIATOR, + g_param_spec_boolean("is-initiator", + "Is Initiator", + "Whether or not the local JID is the initiator of the session.", + FALSE, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, PROP_STATE, + g_param_spec_boolean("state", + "State", + "The state of the session (PENDING=FALSE, ACTIVE=TRUE).", + FALSE, + G_PARAM_READABLE)); + + g_object_class_install_property(gobject_class, PROP_CONTENTS, + g_param_spec_pointer("contents", + "Contents", + "The active contents contained within this session", + G_PARAM_READABLE)); + + g_object_class_install_property(gobject_class, PROP_PENDING_CONTENTS, + g_param_spec_pointer("pending-contents", + "Pending contents", + "The pending contents contained within this session", + G_PARAM_READABLE)); + + g_type_class_add_private(klass, sizeof(JingleSessionPrivate)); +} + +static void +jingle_session_init (JingleSession *session) +{ + session->priv = JINGLE_SESSION_GET_PRIVATE(session); + memset(session->priv, 0, sizeof(*session->priv)); +} + +static void +jingle_session_finalize (GObject *session) +{ + JingleSessionPrivate *priv = JINGLE_SESSION_GET_PRIVATE(session); + purple_debug_info("jingle","jingle_session_finalize\n"); + + g_hash_table_remove(priv->js->sessions, priv->sid); + + g_free(priv->sid); + g_free(priv->remote_jid); + g_free(priv->local_jid); + + for (; priv->contents; priv->contents = + g_list_delete_link(priv->contents, priv->contents)) { + g_object_unref(priv->contents->data); + } + for (; priv->pending_contents; priv->pending_contents = + g_list_delete_link(priv->pending_contents, priv->pending_contents)) { + g_object_unref(priv->pending_contents->data); + } + + parent_class->finalize(session); +} + +static void +jingle_session_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) +{ + JingleSession *session; + g_return_if_fail(JINGLE_IS_SESSION(object)); + + session = JINGLE_SESSION(object); + + switch (prop_id) { + case PROP_SID: + g_free(session->priv->sid); + session->priv->sid = g_value_dup_string(value); + break; + case PROP_JS: + session->priv->js = g_value_get_pointer(value); + break; + case PROP_REMOTE_JID: + g_free(session->priv->remote_jid); + session->priv->remote_jid = g_value_dup_string(value); + break; + case PROP_LOCAL_JID: + g_free(session->priv->local_jid); + session->priv->local_jid = g_value_dup_string(value); + break; + case PROP_IS_INITIATOR: + session->priv->is_initiator = g_value_get_boolean(value); + break; + case PROP_STATE: + session->priv->state = g_value_get_boolean(value); + break; + case PROP_CONTENTS: + session->priv->contents = g_value_get_pointer(value); + break; + case PROP_PENDING_CONTENTS: + session->priv->pending_contents = g_value_get_pointer(value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +jingle_session_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) +{ + JingleSession *session; + g_return_if_fail(JINGLE_IS_SESSION(object)); + + session = JINGLE_SESSION(object); + + switch (prop_id) { + case PROP_SID: + g_value_set_string(value, session->priv->sid); + break; + case PROP_JS: + g_value_set_pointer(value, session->priv->js); + break; + case PROP_REMOTE_JID: + g_value_set_string(value, session->priv->remote_jid); + break; + case PROP_LOCAL_JID: + g_value_set_string(value, session->priv->local_jid); + break; + case PROP_IS_INITIATOR: + g_value_set_boolean(value, session->priv->is_initiator); + break; + case PROP_STATE: + g_value_set_boolean(value, session->priv->state); + break; + case PROP_CONTENTS: + g_value_set_pointer(value, session->priv->contents); + break; + case PROP_PENDING_CONTENTS: + g_value_set_pointer(value, session->priv->pending_contents); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + + +JingleSession * +jingle_session_create(JabberStream *js, const gchar *sid, + const gchar *local_jid, const gchar *remote_jid, + gboolean is_initiator) +{ + JingleSession *session = g_object_new(jingle_session_get_type(), + "js", js, + "sid", sid, + "local-jid", local_jid, + "remote-jid", remote_jid, + "is_initiator", is_initiator, + NULL); + + /* insert it into the hash table */ + if (!js->sessions) { + purple_debug_info("jingle", + "Creating hash table for sessions\n"); + js->sessions = g_hash_table_new(g_str_hash, g_str_equal); + } + purple_debug_info("jingle", + "inserting session with key: %s into table\n", sid); + g_hash_table_insert(js->sessions, g_strdup(sid), session); + + return session; +} + +JabberStream * +jingle_session_get_js(JingleSession *session) +{ + JabberStream *js; + g_object_get(session, "js", &js, NULL); + return js; +} + +gchar * +jingle_session_get_sid(JingleSession *session) +{ + gchar *sid; + g_object_get(session, "sid", &sid, NULL); + return sid; +} + +gchar * +jingle_session_get_local_jid(JingleSession *session) +{ + gchar *local_jid; + g_object_get(session, "local-jid", &local_jid, NULL); + return local_jid; +} + +gchar * +jingle_session_get_remote_jid(JingleSession *session) +{ + gchar *remote_jid; + g_object_get(session, "remote-jid", &remote_jid, NULL); + return remote_jid; +} + +gboolean +jingle_session_is_initiator(JingleSession *session) +{ + gboolean is_initiator; + g_object_get(session, "is-initiator", &is_initiator, NULL); + return is_initiator; +} + +gboolean +jingle_session_get_state(JingleSession *session) +{ + gboolean state; + g_object_get(session, "state", &state, NULL); + return state; +} + +GList * +jingle_session_get_contents(JingleSession *session) +{ + GList *contents; + g_object_get(session, "contents", &contents, NULL); + return contents; +} + +GList * +jingle_session_get_pending_contents(JingleSession *session) +{ + GList *pending_contents; + g_object_get(session, "pending-contents", &pending_contents, NULL); + return pending_contents; +} + +JingleSession * +jingle_session_find_by_sid(JabberStream *js, const gchar *sid) +{ + purple_debug_info("jingle", "find_by_id %s\n", sid); + purple_debug_info("jingle", "lookup: %p\n", (js->sessions) ? + g_hash_table_lookup(js->sessions, sid) : NULL); + return (JingleSession *) (js->sessions) ? + g_hash_table_lookup(js->sessions, sid) : NULL; +} + +static gboolean find_by_jid_ghr(gpointer key, + gpointer value, gpointer user_data) +{ + JingleSession *session = (JingleSession *)value; + const gchar *jid = user_data; + gboolean use_bare = strchr(jid, '/') == NULL; + gchar *remote_jid = jingle_session_get_remote_jid(session); + gchar *cmp_jid = use_bare ? jabber_get_bare_jid(remote_jid) + : g_strdup(remote_jid); + g_free(remote_jid); + if (!strcmp(jid, cmp_jid)) { + g_free(cmp_jid); + return TRUE; + } + g_free(cmp_jid); + + return FALSE; +} + +JingleSession * +jingle_session_find_by_jid(JabberStream *js, const gchar *jid) +{ + return js->sessions != NULL ? + g_hash_table_find(js->sessions, + find_by_jid_ghr, (gpointer)jid) : NULL; +} + +static xmlnode * +jingle_add_jingle_packet(JingleSession *session, + JabberIq *iq, JingleActionType action) +{ + xmlnode *jingle = iq ? + xmlnode_new_child(iq->node, "jingle") : + xmlnode_new("jingle"); + gchar *local_jid = jingle_session_get_local_jid(session); + gchar *remote_jid = jingle_session_get_remote_jid(session); + + xmlnode_set_namespace(jingle, JINGLE); + xmlnode_set_attrib(jingle, "action", jingle_get_action_name(action)); + + if (jingle_session_is_initiator(session)) { + xmlnode_set_attrib(jingle, "initiator", + jingle_session_get_local_jid(session)); + xmlnode_set_attrib(jingle, "responder", + jingle_session_get_remote_jid(session)); + } else { + xmlnode_set_attrib(jingle, "initiator", + jingle_session_get_remote_jid(session)); + xmlnode_set_attrib(jingle, "responder", + jingle_session_get_local_jid(session)); + } + + g_free(local_jid); + g_free(remote_jid); + + xmlnode_set_attrib(jingle, "sid", jingle_session_get_sid(session)); + + return jingle; +} + +JabberIq * +jingle_session_create_ack(JingleSession *session, const xmlnode *jingle) +{ + JabberIq *result = jabber_iq_new( + jingle_session_get_js(session), + JABBER_IQ_RESULT); + xmlnode *packet = xmlnode_get_parent(jingle); + jabber_iq_set_id(result, xmlnode_get_attrib(packet, "id")); + xmlnode_set_attrib(result->node, "from", xmlnode_get_attrib(packet, "to")); + xmlnode_set_attrib(result->node, "to", xmlnode_get_attrib(packet, "from")); + return result; +} + +static JabberIq * +jingle_create_iq(JingleSession *session) +{ + JabberStream *js = jingle_session_get_js(session); + JabberIq *result = jabber_iq_new(js, JABBER_IQ_SET); + gchar *from = jingle_session_get_local_jid(session); + gchar *to = jingle_session_get_remote_jid(session); + + xmlnode_set_attrib(result->node, "from", from); + xmlnode_set_attrib(result->node, "to", to); + + g_free(from); + g_free(to); + return result; +} + +xmlnode * +jingle_session_to_xml(JingleSession *session, xmlnode *jingle, JingleActionType action) +{ + if (action != JINGLE_SESSION_INFO && action != JINGLE_SESSION_TERMINATE) { + GList *iter; + if (action == JINGLE_CONTENT_ACCEPT + || action == JINGLE_CONTENT_ADD + || action == JINGLE_CONTENT_REMOVE) + iter = jingle_session_get_pending_contents(session); + else + iter = jingle_session_get_contents(session); + + for (; iter; iter = g_list_next(iter)) { + jingle_content_to_xml(iter->data, jingle, action); + } + } + return jingle; +} + +JabberIq * +jingle_session_to_packet(JingleSession *session, JingleActionType action) +{ + JabberIq *iq = jingle_create_iq(session); + xmlnode *jingle = jingle_add_jingle_packet(session, iq, action); + jingle_session_to_xml(session, jingle, action); + return iq; +} + +void jingle_session_handle_action(JingleSession *session, xmlnode *jingle, JingleActionType action) +{ + GList *iter; + if (action == JINGLE_CONTENT_ADD || action == JINGLE_CONTENT_REMOVE) + iter = jingle_session_get_pending_contents(session); + else + iter = jingle_session_get_contents(session); + + for (; iter; iter = g_list_next(iter)) { + jingle_content_handle_action(iter->data, jingle, action); + } +} + +JingleContent * +jingle_session_find_content(JingleSession *session, const gchar *name, const gchar *creator) +{ + GList *iter = session->priv->contents; + for (; iter; iter = g_list_next(iter)) { + JingleContent *content = iter->data; + gchar *cname = jingle_content_get_name(content); + gboolean result = !strcmp(name, cname); + g_free(cname); + + if (creator != NULL) { + gchar *ccreator = jingle_content_get_creator(content); + result = (result && !strcmp(creator, ccreator)); + g_free(ccreator); + } + + if (result == TRUE) + return content; + } + return NULL; +} + +JingleContent * +jingle_session_find_pending_content(JingleSession *session, const gchar *name, const gchar *creator) +{ + GList *iter = session->priv->pending_contents; + for (; iter; iter = g_list_next(iter)) { + JingleContent *content = iter->data; + gchar *cname = jingle_content_get_name(content); + gboolean result = !strcmp(name, cname); + g_free(cname); + + if (creator != NULL) { + gchar *ccreator = jingle_content_get_creator(content); + result = (result && !strcmp(creator, ccreator)); + g_free(ccreator); + } + + if (result == TRUE) + return content; + } + return NULL; +} + +void +jingle_session_add_content(JingleSession *session, JingleContent* content) +{ + session->priv->contents = + g_list_append(session->priv->contents, content); + jingle_content_set_session(content, session); +} + +void +jingle_session_remove_content(JingleSession *session, const gchar *name, const gchar *creator) +{ + JingleContent *content = + jingle_session_find_content(session, name, creator); + + if (content) { + session->priv->contents = + g_list_remove(session->priv->contents, content); + g_object_unref(content); + } +} + +void +jingle_session_add_pending_content(JingleSession *session, JingleContent* content) +{ + session->priv->pending_contents = + g_list_append(session->priv->pending_contents, content); + jingle_content_set_session(content, session); +} + +void +jingle_session_remove_pending_content(JingleSession *session, const gchar *name, const gchar *creator) +{ + JingleContent *content = jingle_session_find_pending_content(session, name, creator); + + if (content) { + session->priv->pending_contents = + g_list_remove(session->priv->pending_contents, content); + g_object_unref(content); + } +} + +void +jingle_session_accept_content(JingleSession *session, const gchar *name, const gchar *creator) +{ + JingleContent *content = jingle_session_find_pending_content(session, name, creator); + + if (content) { + g_object_ref(content); + jingle_session_remove_pending_content(session, name, creator); + jingle_session_add_content(session, content); + } +} + +void +jingle_session_accept_session(JingleSession *session) +{ + session->priv->state = TRUE; +} + +JabberIq * +jingle_session_terminate_packet(JingleSession *session, const gchar *reason) +{ + JabberIq *iq = jingle_session_to_packet(session, + JINGLE_SESSION_TERMINATE); + xmlnode *jingle = xmlnode_get_child(iq->node, "jingle"); + + if (reason != NULL) { + xmlnode *reason_node; + reason_node = xmlnode_new_child(jingle, "reason"); + xmlnode_new_child(reason_node, reason); + } + return iq; +} + +JabberIq * +jingle_session_redirect_packet(JingleSession *session, const gchar *sid) +{ + JabberIq *iq = jingle_session_terminate_packet(session, + "alternative-session"); + xmlnode *alt_session; + + if (sid == NULL) + return iq; + + alt_session = xmlnode_get_child(iq->node, + "jingle/reason/alternative-session"); + + if (alt_session != NULL) { + xmlnode *sid_node = xmlnode_new_child(alt_session, "sid"); + xmlnode_insert_data(sid_node, sid, -1); + } + return iq; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libpurple/protocols/jabber/jingle/session.h Fri Apr 03 16:45:38 2009 +0000 @@ -0,0 +1,115 @@ +/** + * @file session.h + * + * purple + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA + */ + +#ifndef JINGLE_SESSION_H +#define JINGLE_SESSION_H + +#include "iq.h" +#include "jabber.h" + +#include <glib.h> +#include <glib-object.h> + +G_BEGIN_DECLS + +#define JINGLE_TYPE_SESSION (jingle_session_get_type()) +#define JINGLE_SESSION(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), JINGLE_TYPE_SESSION, JingleSession)) +#define JINGLE_SESSION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), JINGLE_TYPE_SESSION, JingleSessionClass)) +#define JINGLE_IS_SESSION(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), JINGLE_TYPE_SESSION)) +#define JINGLE_IS_SESSION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), JINGLE_TYPE_SESSION)) +#define JINGLE_SESSION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), JINGLE_TYPE_SESSION, JingleSessionClass)) + +/** @copydoc _JingleSession */ +typedef struct _JingleSession JingleSession; +/** @copydoc _JingleSessionClass */ +typedef struct _JingleSessionClass JingleSessionClass; +/** @copydoc _JingleSessionPrivate */ +typedef struct _JingleSessionPrivate JingleSessionPrivate; + +/** The session class */ +struct _JingleSessionClass +{ + GObjectClass parent_class; /**< The parent class. */ +}; + +/** The session class's private data */ +struct _JingleSession +{ + GObject parent; /**< The parent of this object. */ + JingleSessionPrivate *priv; /**< The private data of this object. */ +}; + +struct _JingleContent; + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Gets the session class's GType + * + * @return The session class's GType. + */ +GType jingle_session_get_type(void); + +JingleSession *jingle_session_create(JabberStream *js, const gchar *sid, + const gchar *local_jid, const gchar *remote_jid, + gboolean is_initiator); +JabberStream *jingle_session_get_js(JingleSession *session); +gchar *jingle_session_get_sid(JingleSession *session); +gchar *jingle_session_get_local_jid(JingleSession *session); +gchar *jingle_session_get_remote_jid(JingleSession *session); +gboolean jingle_session_is_initiator(JingleSession *session); +gboolean jingle_session_get_state(JingleSession *session); + +GList *jingle_session_get_contents(JingleSession *session); +GList *jingle_session_get_pending_contents(JingleSession *session); + +JingleSession *jingle_session_find_by_sid(JabberStream *js, const gchar *sid); +JingleSession *jingle_session_find_by_jid(JabberStream *js, const gchar *jid); + +JabberIq *jingle_session_create_ack(JingleSession *session, const xmlnode *jingle); +xmlnode *jingle_session_to_xml(JingleSession *session, xmlnode *parent, JingleActionType action); +JabberIq *jingle_session_to_packet(JingleSession *session, JingleActionType action); + +void jingle_session_handle_action(JingleSession *session, xmlnode *jingle, JingleActionType action); + +struct _JingleContent *jingle_session_find_content(JingleSession *session, + const gchar *name, const gchar *creator); +struct _JingleContent *jingle_session_find_pending_content(JingleSession *session, + const gchar *name, const gchar *creator); + +void jingle_session_add_content(JingleSession *session, struct _JingleContent* content); +void jingle_session_remove_content(JingleSession *session, const gchar *name, const gchar *creator); +void jingle_session_add_pending_content(JingleSession *session, struct _JingleContent* content); +void jingle_session_remove_pending_content(JingleSession *session, const gchar *name, const gchar *creator); +void jingle_session_accept_content(JingleSession *session, const gchar *name, const gchar *creator); +void jingle_session_accept_session(JingleSession *session); +JabberIq *jingle_session_terminate_packet(JingleSession *session, const gchar *reason); +JabberIq *jingle_session_redirect_packet(JingleSession *session, const gchar *sid); + +#ifdef __cplusplus +} +#endif + +G_END_DECLS + +#endif /* JINGLE_SESSION_H */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libpurple/protocols/jabber/jingle/transport.c Fri Apr 03 16:45:38 2009 +0000 @@ -0,0 +1,174 @@ +/** + * @file transport.c + * + * purple + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA + */ + +#include "internal.h" + +#include "transport.h" +#include "jingle.h" +#include "debug.h" + +#include <string.h> + +struct _JingleTransportPrivate +{ + void *dummy; +}; + +#define JINGLE_TRANSPORT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), JINGLE_TYPE_TRANSPORT, JingleTransportPrivate)) + +static void jingle_transport_class_init (JingleTransportClass *klass); +static void jingle_transport_init (JingleTransport *transport); +static void jingle_transport_finalize (GObject *object); +static void jingle_transport_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec); +static void jingle_transport_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec); +JingleTransport *jingle_transport_parse_internal(xmlnode *transport); +xmlnode *jingle_transport_to_xml_internal(JingleTransport *transport, xmlnode *content, JingleActionType action); + +static GObjectClass *parent_class = NULL; + +enum { + PROP_0, +}; + +GType +jingle_transport_get_type() +{ + static GType type = 0; + + if (type == 0) { + static const GTypeInfo info = { + sizeof(JingleTransportClass), + NULL, + NULL, + (GClassInitFunc) jingle_transport_class_init, + NULL, + NULL, + sizeof(JingleTransport), + 0, + (GInstanceInitFunc) jingle_transport_init, + NULL + }; + type = g_type_register_static(G_TYPE_OBJECT, "JingleTransport", &info, 0); + } + return type; +} + +static void +jingle_transport_class_init (JingleTransportClass *klass) +{ + GObjectClass *gobject_class = (GObjectClass*)klass; + parent_class = g_type_class_peek_parent(klass); + + gobject_class->finalize = jingle_transport_finalize; + gobject_class->set_property = jingle_transport_set_property; + gobject_class->get_property = jingle_transport_get_property; + klass->to_xml = jingle_transport_to_xml_internal; + klass->parse = jingle_transport_parse_internal; + + g_type_class_add_private(klass, sizeof(JingleTransportPrivate)); +} + +static void +jingle_transport_init (JingleTransport *transport) +{ + transport->priv = JINGLE_TRANSPORT_GET_PRIVATE(transport); + transport->priv->dummy = NULL; +} + +static void +jingle_transport_finalize (GObject *transport) +{ + /* JingleTransportPrivate *priv = JINGLE_TRANSPORT_GET_PRIVATE(transport); */ + purple_debug_info("jingle","jingle_transport_finalize\n"); + + parent_class->finalize(transport); +} + +static void +jingle_transport_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) +{ + JingleTransport *transport; + g_return_if_fail(JINGLE_IS_TRANSPORT(object)); + + transport = JINGLE_TRANSPORT(object); + + switch (prop_id) { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +jingle_transport_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) +{ + JingleTransport *transport; + g_return_if_fail(JINGLE_IS_TRANSPORT(object)); + + transport = JINGLE_TRANSPORT(object); + + switch (prop_id) { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +JingleTransport * +jingle_transport_create(const gchar *type) +{ + return g_object_new(jingle_get_type(type), NULL); +} + +const gchar * +jingle_transport_get_transport_type(JingleTransport *transport) +{ + return JINGLE_TRANSPORT_GET_CLASS(transport)->transport_type; +} + +JingleTransport * +jingle_transport_parse_internal(xmlnode *transport) +{ + const gchar *type = xmlnode_get_namespace(transport); + return jingle_transport_create(type); +} + +xmlnode * +jingle_transport_to_xml_internal(JingleTransport *transport, xmlnode *content, JingleActionType action) +{ + xmlnode *node = xmlnode_new_child(content, "transport"); + xmlnode_set_namespace(node, jingle_transport_get_transport_type(transport)); + return node; +} + +JingleTransport * +jingle_transport_parse(xmlnode *transport) +{ + const gchar *type = xmlnode_get_namespace(transport); + return JINGLE_TRANSPORT_CLASS(g_type_class_ref(jingle_get_type(type)))->parse(transport); +} + +xmlnode * +jingle_transport_to_xml(JingleTransport *transport, xmlnode *content, JingleActionType action) +{ + g_return_val_if_fail(JINGLE_IS_TRANSPORT(transport), NULL); + return JINGLE_TRANSPORT_GET_CLASS(transport)->to_xml(transport, content, action); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libpurple/protocols/jabber/jingle/transport.h Fri Apr 03 16:45:38 2009 +0000 @@ -0,0 +1,88 @@ +/** + * @file transport.h + * + * purple + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA + */ + +#ifndef JINGLE_TRANSPORT_H +#define JINGLE_TRANSPORT_H + +#include <glib.h> +#include <glib-object.h> + +#include "jingle.h" +#include "xmlnode.h" + +G_BEGIN_DECLS + +#define JINGLE_TYPE_TRANSPORT (jingle_transport_get_type()) +#define JINGLE_TRANSPORT(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), JINGLE_TYPE_TRANSPORT, JingleTransport)) +#define JINGLE_TRANSPORT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), JINGLE_TYPE_TRANSPORT, JingleTransportClass)) +#define JINGLE_IS_TRANSPORT(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), JINGLE_TYPE_TRANSPORT)) +#define JINGLE_IS_TRANSPORT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), JINGLE_TYPE_TRANSPORT)) +#define JINGLE_TRANSPORT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), JINGLE_TYPE_TRANSPORT, JingleTransportClass)) + +/** @copydoc _JingleTransport */ +typedef struct _JingleTransport JingleTransport; +/** @copydoc _JingleTransportClass */ +typedef struct _JingleTransportClass JingleTransportClass; +/** @copydoc _JingleTransportPrivate */ +typedef struct _JingleTransportPrivate JingleTransportPrivate; + +/** The transport class */ +struct _JingleTransportClass +{ + GObjectClass parent_class; /**< The parent class. */ + + const gchar *transport_type; + xmlnode *(*to_xml) (JingleTransport *transport, xmlnode *content, JingleActionType action); + JingleTransport *(*parse) (xmlnode *transport); +}; + +/** The transport class's private data */ +struct _JingleTransport +{ + GObject parent; /**< The parent of this object. */ + JingleTransportPrivate *priv; /**< The private data of this object. */ +}; + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Gets the transport class's GType + * + * @return The transport class's GType. + */ +GType jingle_transport_get_type(void); + +JingleTransport *jingle_transport_create(const gchar *type); +const gchar *jingle_transport_get_transport_type(JingleTransport *transport); +void jingle_transport_add_candidate(); + +JingleTransport *jingle_transport_parse(xmlnode *transport); +xmlnode *jingle_transport_to_xml(JingleTransport *transport, xmlnode *content, JingleActionType action); + +#ifdef __cplusplus +} +#endif + +G_END_DECLS + +#endif /* JINGLE_TRANSPORT_H */ +
--- a/libpurple/protocols/jabber/libxmpp.c Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/protocols/jabber/libxmpp.c Fri Apr 03 16:45:38 2009 +0000 @@ -117,9 +117,10 @@ jabber_unregister_account, /* unregister_user */ jabber_send_attention, /* send_attention */ jabber_attention_types, /* attention_types */ - sizeof(PurplePluginProtocolInfo), /* struct_size */ - NULL + NULL, /* get_account_text_table */ + jabber_initiate_media, /* initiate_media */ + jabber_get_media_caps, /* get_media_caps */ }; static gboolean load_plugin(PurplePlugin *plugin) @@ -297,6 +298,9 @@ jabber_add_feature("ibb", XEP_0047_NAMESPACE, NULL); jabber_pep_register_handler("avatar", AVATARNAMESPACEMETA, jabber_buddy_avatar_update_metadata); +#ifdef USE_VV + jabber_add_feature("voice-v1", "http://www.xmpp.org/extensions/xep-0167.html#ns", NULL); +#endif }
--- a/libpurple/protocols/jabber/presence.c Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/protocols/jabber/presence.c Fri Apr 03 16:45:38 2009 +0000 @@ -264,6 +264,10 @@ xmlnode_set_namespace(c, "http://jabber.org/protocol/caps"); xmlnode_set_attrib(c, "node", CAPS0115_NODE); xmlnode_set_attrib(c, "ver", VERSION); +#ifdef USE_VV + /* Make sure this is 'voice-v1', or you won't be able to talk to Google Talk */ + xmlnode_set_attrib(c, "ext", "voice-v1"); +#endif if(js != NULL) { /* add the extensions */
--- a/libpurple/protocols/msn/msn.c Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/protocols/msn/msn.c Fri Apr 03 16:45:38 2009 +0000 @@ -2607,9 +2607,10 @@ NULL, /* unregister_user */ msn_send_attention, /* send_attention */ msn_attention_types, /* attention_types */ - sizeof(PurplePluginProtocolInfo), /* struct_size */ msn_get_account_text_table, /* get_account_text_table */ + NULL, /* initiate_media */ + NULL /* can_do_media */ }; static PurplePluginInfo info =
--- a/libpurple/protocols/msnp9/msn.c Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/protocols/msnp9/msn.c Fri Apr 03 16:45:38 2009 +0000 @@ -2276,9 +2276,10 @@ NULL, /* unregister_user */ msn_send_attention, /* send_attention */ msn_attention_types, /* attention_types */ - sizeof(PurplePluginProtocolInfo), /* struct_size */ msn_get_account_text_table, /* get_account_text_table */ + NULL, /* initiate_media */ + NULL /* can_do_media */ }; static PurplePluginInfo info =
--- a/libpurple/protocols/myspace/myspace.c Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/protocols/myspace/myspace.c Fri Apr 03 16:45:38 2009 +0000 @@ -3088,9 +3088,10 @@ NULL, /* unregister_user */ msim_send_attention, /* send_attention */ msim_attention_types, /* attention_types */ - - sizeof(PurplePluginProtocolInfo), /* struct_size */ + sizeof(PurplePluginProtocolInfo), /* struct_size */ msim_get_account_text_table, /* get_account_text_table */ + NULL, /* initiate_media */ + NULL /* can_do_media */ }; /**
--- a/libpurple/protocols/novell/novell.c Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/protocols/novell/novell.c Fri Apr 03 16:45:38 2009 +0000 @@ -3524,13 +3524,13 @@ NULL, /* whiteboard_prpl_ops */ NULL, /* send_raw */ NULL, /* roomlist_room_serialize */ - - /* padding */ - NULL, - NULL, - NULL, + NULL, /* unregister_user */ + NULL, /* send_attention */ + NULL, /* get_attention_types */ sizeof(PurplePluginProtocolInfo), /* struct_size */ - NULL + NULL, /* get_account_text_table */ + NULL, /* initiate_media */ + NULL /* can_do_media */ }; static PurplePluginInfo info = {
--- a/libpurple/protocols/null/nullprpl.c Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/protocols/null/nullprpl.c Fri Apr 03 16:45:38 2009 +0000 @@ -1112,11 +1112,13 @@ NULL, /* whiteboard_prpl_ops */ NULL, /* send_raw */ NULL, /* roomlist_room_serialize */ - NULL, /* unregister_user */ + NULL, /* unregister_user */ NULL, /* send_attention */ - NULL, /* attention_types */ + NULL, /* get_attention_types */ sizeof(PurplePluginProtocolInfo), /* struct_size */ - NULL, /* get_account_text_table */ + NULL, + NULL, /* initiate_media */ + NULL /* can_do_media */ }; static void nullprpl_init(PurplePlugin *plugin)
--- a/libpurple/protocols/oscar/libaim.c Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/protocols/oscar/libaim.c Fri Apr 03 16:45:38 2009 +0000 @@ -95,9 +95,10 @@ NULL, /* unregister_user */ NULL, /* send_attention */ NULL, /* get_attention_types */ - sizeof(PurplePluginProtocolInfo), /* struct_size */ - NULL + NULL, /* get_account_text_table */ + NULL, /* initiate_media */ + NULL /* can_do_media */ }; static PurplePluginInfo info =
--- a/libpurple/protocols/oscar/libicq.c Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/protocols/oscar/libicq.c Fri Apr 03 16:45:38 2009 +0000 @@ -107,6 +107,8 @@ sizeof(PurplePluginProtocolInfo), /* struct_size */ icq_get_account_text_table, /* get_account_text_table */ + NULL, /* initiate_media */ + NULL /* can_do_media */ }; static PurplePluginInfo info =
--- a/libpurple/protocols/qq/qq.c Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/protocols/qq/qq.c Fri Apr 03 16:45:38 2009 +0000 @@ -1035,7 +1035,9 @@ NULL, /* get attention_types */ sizeof(PurplePluginProtocolInfo), /* struct_size */ - NULL + NULL, /* get_account_text_table */ + NULL, /* initiate_media */ + NULL /* can_do_media */ }; static PurplePluginInfo info = {
--- a/libpurple/protocols/sametime/sametime.c Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/protocols/sametime/sametime.c Fri Apr 03 16:45:38 2009 +0000 @@ -5209,7 +5209,8 @@ .new_xfer = mw_prpl_new_xfer, .offline_message = NULL, .whiteboard_prpl_ops = NULL, - .send_raw = NULL + .send_raw = NULL, + .struct_size = sizeof(PurplePluginProtocolInfo) };
--- a/libpurple/protocols/silc/silc.c Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/protocols/silc/silc.c Fri Apr 03 16:45:38 2009 +0000 @@ -2109,13 +2109,13 @@ &silcpurple_wb_ops, /* whiteboard_prpl_ops */ NULL, /* send_raw */ NULL, /* roomlist_room_serialize */ - - /* padding */ - NULL, - NULL, - NULL, + NULL, /* unregister_user */ + NULL, /* send_attention */ + NULL, /* get_attention_types */ sizeof(PurplePluginProtocolInfo), /* struct_size */ - NULL + NULL, /* get_account_text_table */ + NULL, /* initiate_media */ + NULL /* can_do_media */ }; static PurplePluginInfo info =
--- a/libpurple/protocols/silc10/silc.c Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/protocols/silc10/silc.c Fri Apr 03 16:45:38 2009 +0000 @@ -1836,12 +1836,13 @@ &silcpurple_wb_ops, /* whiteboard_prpl_ops */ NULL, /* send_raw */ NULL, /* roomlist_room_serialize */ - - NULL, - NULL, - NULL, + NULL, /* unregister_user */ + NULL, /* send_attention */ + NULL, /* get_attention_types */ sizeof(PurplePluginProtocolInfo), /* struct_size */ - NULL + NULL, /* get_account_text_table */ + NULL, /* initiate_media */ + NULL /* can_do_media */ }; static PurplePluginInfo info =
--- a/libpurple/protocols/simple/simple.c Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/protocols/simple/simple.c Fri Apr 03 16:45:38 2009 +0000 @@ -2097,13 +2097,13 @@ NULL, /* whiteboard_prpl_ops */ simple_send_raw, /* send_raw */ NULL, /* roomlist_room_serialize */ - - /* padding */ - NULL, - NULL, - NULL, + NULL, /* unregister_user */ + NULL, /* send_attention */ + NULL, /* get_attention_types */ sizeof(PurplePluginProtocolInfo), /* struct_size */ - NULL + NULL, /* get_account_text_table */ + NULL, /* initiate_media */ + NULL /* can_do_media */ };
--- a/libpurple/protocols/yahoo/yahoo.c Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/protocols/yahoo/yahoo.c Fri Apr 03 16:45:38 2009 +0000 @@ -5412,6 +5412,8 @@ sizeof(PurplePluginProtocolInfo), /* struct_size */ yahoo_get_account_text_table, /* get_account_text_table */ + NULL, /* initiate_media */ + NULL /* can_do_media */ }; static PurplePluginInfo info =
--- a/libpurple/protocols/zephyr/zephyr.c Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/protocols/zephyr/zephyr.c Fri Apr 03 16:45:38 2009 +0000 @@ -2953,7 +2953,9 @@ NULL, NULL, sizeof(PurplePluginProtocolInfo), /* struct_size */ - NULL + NULL, /* get_account_text_table */ + NULL, /* initate_media */ + NULL /* can_do_media */ }; static PurplePluginInfo info = {
--- a/libpurple/prpl.c Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/prpl.c Fri Apr 03 16:45:38 2009 +0000 @@ -496,6 +496,54 @@ got_attention(gc, id, who, type_code); } +gboolean +purple_prpl_initiate_media(PurpleAccount *account, + const char *who, + PurpleMediaSessionType type) +{ +#ifdef USE_VV + PurpleConnection *gc = NULL; + PurplePlugin *prpl = NULL; + PurplePluginProtocolInfo *prpl_info = NULL; + + if (account) + gc = purple_account_get_connection(account); + if (gc) + prpl = purple_connection_get_prpl(gc); + if (prpl) + prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl); + + if (prpl_info && PURPLE_PROTOCOL_PLUGIN_HAS_FUNC(prpl_info, initiate_media)) { + /* should check that the protocol supports this media type here? */ + return prpl_info->initiate_media(gc, who, type); + } else +#endif + return FALSE; +} + +PurpleMediaCaps +purple_prpl_get_media_caps(PurpleAccount *account, const char *who) +{ +#ifdef USE_VV + PurpleConnection *gc = NULL; + PurplePlugin *prpl = NULL; + PurplePluginProtocolInfo *prpl_info = NULL; + + if (account) + gc = purple_account_get_connection(account); + if (gc) + prpl = purple_connection_get_prpl(gc); + if (prpl) + prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl); + + if (prpl_info && PURPLE_PROTOCOL_PLUGIN_HAS_FUNC(prpl_info, + get_media_caps)) { + return prpl_info->get_media_caps(gc, who); + } +#endif + return PURPLE_MEDIA_CAPS_NONE; +} + /************************************************************************** * Protocol Plugin Subsystem API **************************************************************************/
--- a/libpurple/prpl.h Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/prpl.h Fri Apr 03 16:45:38 2009 +0000 @@ -65,6 +65,7 @@ #include "conversation.h" #include "ft.h" #include "imgstore.h" +#include "media.h" #include "notify.h" #include "proxy.h" #include "plugin.h" @@ -459,6 +460,27 @@ * destroyed by the caller when it's no longer needed. */ GHashTable *(*get_account_text_table)(PurpleAccount *account); + + /** + * Initiate a media session with the given contact. + * + * @param conn The connection to initiate the media session on. + * @param who The remote user to initiate the session with. + * @param type The type of media session to initiate. + * @return TRUE if the call succeeded else FALSE. (Doesn't imply the media session or stream will be successfully created) + */ + gboolean (*initiate_media)(PurpleConnection *gc, const char *who, + PurpleMediaSessionType type); + + /** + * Checks to see if the given contact supports the given type of media session. + * + * @param conn The connection the contact is on. + * @param who The remote user to check for media capability with. + * @return The media caps the contact supports. + */ + PurpleMediaCaps (*get_media_caps)(PurpleConnection *gc, + const char *who); }; #define PURPLE_PROTOCOL_PLUGIN_HAS_FUNC(prpl, member) \ @@ -755,6 +777,30 @@ */ void purple_prpl_got_attention_in_chat(PurpleConnection *gc, int id, const char *who, guint type_code); +/** + * Determines if the contact supports the given media session type. + * + * @param account The account the user is on. + * @param who The name of the contact to check capabilities for. + * + * @return The media caps the contact supports. + */ +PurpleMediaCaps purple_prpl_get_media_caps(PurpleAccount *account, + const char *who); + +/** + * Initiates a media session with the given contact. + * + * @param account The account the user is on. + * @param who The name of the contact to start a session with. + * @param type The type of media session to start. + * + * @return TRUE if the call succeeded else FALSE. (Doesn't imply the media session or stream will be successfully created) + */ +gboolean purple_prpl_initiate_media(PurpleAccount *account, + const char *who, + PurpleMediaSessionType type); + /*@}*/ /**************************************************************************/
--- a/libpurple/xmlnode.c Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/xmlnode.c Fri Apr 03 16:45:38 2009 +0000 @@ -288,6 +288,12 @@ return node->prefix; } +xmlnode *xmlnode_get_parent(const xmlnode *child) +{ + g_return_val_if_fail(child != NULL, NULL); + return child->parent; +} + void xmlnode_free(xmlnode *node) {
--- a/libpurple/xmlnode.h Fri Apr 03 02:43:22 2009 +0000 +++ b/libpurple/xmlnode.h Fri Apr 03 16:45:38 2009 +0000 @@ -268,6 +268,15 @@ const char *xmlnode_get_prefix(const xmlnode *node); /** + * Gets the parent node. + * + * @param child The child node. + * + * @return The parent or NULL. + */ +xmlnode *xmlnode_get_parent(const xmlnode *child); + +/** * Returns the node in a string of xml. * * @param node The starting node to output.
--- a/pidgin/Makefile.am Fri Apr 03 02:43:22 2009 +0000 +++ b/pidgin/Makefile.am Fri Apr 03 16:45:38 2009 +0000 @@ -103,6 +103,7 @@ gtkimhtmltoolbar.c \ gtklog.c \ gtkmain.c \ + gtkmedia.c \ gtkmenutray.c \ gtknotify.c \ gtkplugin.c \ @@ -160,6 +161,7 @@ gtkimhtml.h \ gtkimhtmltoolbar.h \ gtklog.h \ + gtkmedia.h \ gtkmenutray.h \ gtknickcolors.h \ gtknotify.h \
--- a/pidgin/Makefile.mingw Fri Apr 03 02:43:22 2009 +0000 +++ b/pidgin/Makefile.mingw Fri Apr 03 16:45:38 2009 +0000 @@ -77,6 +77,7 @@ gtkimhtmltoolbar.c \ gtklog.c \ gtkmain.c \ + gtkmedia.c \ gtkmenutray.c \ gtknotify.c \ gtkplugin.c \
--- a/pidgin/gtkblist.c Fri Apr 03 02:43:22 2009 +0000 +++ b/pidgin/gtkblist.c Fri Apr 03 16:45:38 2009 +0000 @@ -338,6 +338,30 @@ purple_buddy_get_name(b)); } +#ifdef USE_VV +static void gtk_blist_menu_audio_call_cb(GtkWidget *w, PurpleBuddy *b) +{ + purple_prpl_initiate_media(purple_buddy_get_account(b), + purple_buddy_get_name(b), PURPLE_MEDIA_AUDIO); +} + +static void gtk_blist_menu_video_call_cb(GtkWidget *w, PurpleBuddy *b) +{ + /* if the buddy supports both audio and video, start a combined call, + otherwise start a pure video session */ + if (purple_prpl_get_media_caps(purple_buddy_get_account(b), + purple_buddy_get_name(b)) & + PURPLE_MEDIA_CAPS_AUDIO_VIDEO) { + purple_prpl_initiate_media(purple_buddy_get_account(b), + purple_buddy_get_name(b), PURPLE_MEDIA_AUDIO | PURPLE_MEDIA_VIDEO); + } else { + purple_prpl_initiate_media(purple_buddy_get_account(b), + purple_buddy_get_name(b), PURPLE_MEDIA_VIDEO); + } +} + +#endif + static void gtk_blist_menu_send_file_cb(GtkWidget *w, PurpleBuddy *b) { PurpleAccount *account = purple_buddy_get_account(b); @@ -1476,6 +1500,30 @@ } pidgin_new_item_from_stock(menu, _("I_M"), PIDGIN_STOCK_TOOLBAR_MESSAGE_NEW, G_CALLBACK(gtk_blist_menu_im_cb), buddy, 0, 0, NULL); + +#ifdef USE_VV + if (prpl_info && prpl_info->get_media_caps) { + PurpleAccount *account = purple_buddy_get_account(buddy); + const gchar *who = purple_buddy_get_name(buddy); + PurpleMediaCaps caps = purple_prpl_get_media_caps(account, who); + if (caps & PURPLE_MEDIA_CAPS_AUDIO) { + pidgin_new_item_from_stock(menu, _("_Audio Call"), + PIDGIN_STOCK_TOOLBAR_AUDIO_CALL, + G_CALLBACK(gtk_blist_menu_audio_call_cb), buddy, 0, 0, NULL); + } + if (caps & PURPLE_MEDIA_CAPS_AUDIO_VIDEO) { + pidgin_new_item_from_stock(menu, _("Audio/_Video Call"), + PIDGIN_STOCK_TOOLBAR_VIDEO_CALL, + G_CALLBACK(gtk_blist_menu_video_call_cb), buddy, 0, 0, NULL); + } else if (caps & PURPLE_MEDIA_CAPS_VIDEO) { + pidgin_new_item_from_stock(menu, _("_Video Call"), + PIDGIN_STOCK_TOOLBAR_VIDEO_CALL, + G_CALLBACK(gtk_blist_menu_video_call_cb), buddy, 0, 0, NULL); + } + } + +#endif + if (prpl_info && prpl_info->send_file) { if (!prpl_info->can_receive_file || prpl_info->can_receive_file(buddy->account->gc, buddy->name))
--- a/pidgin/gtkconv.c Fri Apr 03 02:43:22 2009 +0000 +++ b/pidgin/gtkconv.c Fri Apr 03 16:45:38 2009 +0000 @@ -1201,6 +1201,23 @@ gtk_widget_grab_focus(s->entry); } +#ifdef USE_VV +static void +menu_initiate_media_call_cb(gpointer data, guint action, GtkWidget *widget) +{ + PidginWindow *win = (PidginWindow *)data; + PurpleConversation *conv = pidgin_conv_window_get_active_conversation(win); + PurpleAccount *account = purple_conversation_get_account(conv); + + purple_prpl_initiate_media(account, + purple_conversation_get_name(conv), + action == 0 ? PURPLE_MEDIA_AUDIO : + action == 1 ? PURPLE_MEDIA_VIDEO : + action == 2 ? PURPLE_MEDIA_AUDIO | + PURPLE_MEDIA_VIDEO : PURPLE_MEDIA_NONE); +} +#endif + static void menu_send_file_cb(gpointer data, guint action, GtkWidget *widget) { @@ -3114,6 +3131,17 @@ { "/Conversation/sep1", NULL, NULL, 0, "<Separator>", NULL }, +#ifdef USE_VV + { N_("/Conversation/M_edia"), NULL, NULL, 0, "<Branch>", NULL }, + + { N_("/Conversation/Media/_Audio Call"), NULL, menu_initiate_media_call_cb, 0, + "<StockItem>", PIDGIN_STOCK_TOOLBAR_AUDIO_CALL }, + { N_("/Conversation/Media/_Video Call"), NULL, menu_initiate_media_call_cb, 1, + "<StockItem>", PIDGIN_STOCK_TOOLBAR_VIDEO_CALL }, + { N_("/Conversation/Media/Audio\\/Video _Call"), NULL, menu_initiate_media_call_cb, 2, + "<StockItem>", PIDGIN_STOCK_TOOLBAR_VIDEO_CALL }, +#endif + { N_("/Conversation/Se_nd File..."), NULL, menu_send_file_cb, 0, "<StockItem>", PIDGIN_STOCK_TOOLBAR_SEND_FILE }, { N_("/Conversation/Add Buddy _Pounce..."), NULL, menu_add_pounce_cb, 0, "<Item>", NULL }, @@ -3424,6 +3452,18 @@ gtk_item_factory_get_widget(win->menu.item_factory, N_("/Conversation/View Log")); +#ifdef USE_VV + win->audio_call = + gtk_item_factory_get_widget(win->menu.item_factory, + N_("/Conversation/Media/Audio Call")); + win->video_call = + gtk_item_factory_get_widget(win->menu.item_factory, + N_("/Conversation/Media/Video Call")); + win->audio_video_call = + gtk_item_factory_get_widget(win->menu.item_factory, + N_("/Conversation/Media/Audio\\/Video Call")); +#endif + /* --- */ win->menu.send_file = @@ -6407,6 +6447,36 @@ else buttons &= ~GTK_IMHTML_CUSTOM_SMILEY; +#ifdef USE_VV + /* check if account support voice calls, and if the current buddy + supports it */ + if (account != NULL && purple_conversation_get_type(conv) + == PURPLE_CONV_TYPE_IM) { + PurpleMediaCaps caps = + purple_prpl_get_media_caps(account, + purple_conversation_get_name(conv)); + + gtk_widget_set_sensitive(win->audio_call, + caps & PURPLE_MEDIA_CAPS_AUDIO + ? TRUE : FALSE); + gtk_widget_set_sensitive(win->video_call, + caps & PURPLE_MEDIA_CAPS_VIDEO + ? TRUE : FALSE); + gtk_widget_set_sensitive(win->audio_video_call, + caps & PURPLE_MEDIA_CAPS_AUDIO_VIDEO + ? TRUE : FALSE); + } else if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT) { + /* for now, don't care about chats... */ + gtk_widget_set_sensitive(win->audio_call, FALSE); + gtk_widget_set_sensitive(win->video_call, FALSE); + gtk_widget_set_sensitive(win->audio_video_call, FALSE); + } else { + gtk_widget_set_sensitive(win->audio_call, FALSE); + gtk_widget_set_sensitive(win->video_call, FALSE); + gtk_widget_set_sensitive(win->audio_video_call, FALSE); + } +#endif + gtk_imhtml_set_format_functions(GTK_IMHTML(gtkconv->entry), buttons); if (account != NULL) gtk_imhtmltoolbar_associate_smileys(GTK_IMHTMLTOOLBAR(gtkconv->toolbar), purple_account_get_protocol_id(account));
--- a/pidgin/gtkconvwin.h Fri Apr 03 02:43:22 2009 +0000 +++ b/pidgin/gtkconvwin.h Fri Apr 03 16:45:38 2009 +0000 @@ -96,6 +96,11 @@ gint drag_motion_signal; gint drag_leave_signal; + + /* Media menu options. */ + GtkWidget *audio_call; + GtkWidget *video_call; + GtkWidget *audio_video_call; }; /*@}*/
--- a/pidgin/gtkdialogs.c Fri Apr 03 02:43:22 2009 +0000 +++ b/pidgin/gtkdialogs.c Fri Apr 03 16:45:38 2009 +0000 @@ -644,6 +644,12 @@ g_string_append(str, " <b>Tk:</b> Disabled<br/>"); } +#ifdef USE_VV + g_string_append(str, " <b>Voice and Video:</b> Enabled<br/>"); +#else + g_string_append(str, " <b>Voice and Video:</b> Disabled<br/>"); +#endif + #ifndef _WIN32 #ifdef USE_SM g_string_append(str, " <b>X Session Management:</b> Enabled<br/>");
--- a/pidgin/gtkimhtmltoolbar.h Fri Apr 03 02:43:22 2009 +0000 +++ b/pidgin/gtkimhtmltoolbar.h Fri Apr 03 16:45:38 2009 +0000 @@ -76,6 +76,7 @@ char *sml; GtkWidget *strikethrough; GtkWidget *insert_hr; + GtkWidget *call; }; struct _GtkIMHtmlToolbarClass {
--- a/pidgin/gtkmain.c Fri Apr 03 02:43:22 2009 +0000 +++ b/pidgin/gtkmain.c Fri Apr 03 16:45:38 2009 +0000 @@ -53,6 +53,7 @@ #include "gtkft.h" #include "gtkidle.h" #include "gtklog.h" +#include "gtkmedia.h" #include "gtknotify.h" #include "gtkplugin.h" #include "gtkpounce.h" @@ -311,6 +312,7 @@ pidgin_docklet_init(); pidgin_smileys_init(); pidgin_utils_init(); + pidgin_medias_init(); } static GHashTable *ui_info = NULL;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pidgin/gtkmedia.c Fri Apr 03 16:45:38 2009 +0000 @@ -0,0 +1,1150 @@ +/** + * @file media.c Account API + * @ingroup core + * + * Pidgin + * + * Pidgin is the legal property of its developers, whose names are too numerous + * to list here. Please refer to the COPYRIGHT file distributed with this + * source distribution. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <string.h> +#include "debug.h" +#include "internal.h" +#include "connection.h" +#include "media.h" +#include "mediamanager.h" +#include "pidgin.h" +#include "request.h" + +#include "gtkmedia.h" +#include "gtkutils.h" + +#ifdef USE_VV +#include "media-gst.h" + +#include <gst/interfaces/xoverlay.h> + +#define PIDGIN_TYPE_MEDIA (pidgin_media_get_type()) +#define PIDGIN_MEDIA(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), PIDGIN_TYPE_MEDIA, PidginMedia)) +#define PIDGIN_MEDIA_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), PIDGIN_TYPE_MEDIA, PidginMediaClass)) +#define PIDGIN_IS_MEDIA(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), PIDGIN_TYPE_MEDIA)) +#define PIDGIN_IS_MEDIA_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), PIDGIN_TYPE_MEDIA)) +#define PIDGIN_MEDIA_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), PIDGIN_TYPE_MEDIA, PidginMediaClass)) + +typedef struct _PidginMedia PidginMedia; +typedef struct _PidginMediaClass PidginMediaClass; +typedef struct _PidginMediaPrivate PidginMediaPrivate; + +typedef enum +{ + /* Waiting for response */ + PIDGIN_MEDIA_WAITING = 1, + /* Got request */ + PIDGIN_MEDIA_REQUESTED, + /* Accepted call */ + PIDGIN_MEDIA_ACCEPTED, + /* Rejected call */ + PIDGIN_MEDIA_REJECTED, +} PidginMediaState; + +struct _PidginMediaClass +{ + GtkWindowClass parent_class; +}; + +struct _PidginMedia +{ + GtkWindow parent; + PidginMediaPrivate *priv; +}; + +struct _PidginMediaPrivate +{ + PurpleMedia *media; + gchar *screenname; + GstElement *send_level; + GstElement *recv_level; + + GtkItemFactory *item_factory; + GtkWidget *menubar; + GtkWidget *statusbar; + + GtkWidget *mute; + + GtkWidget *send_progress; + GtkWidget *recv_progress; + + PidginMediaState state; + + GtkWidget *display; + GtkWidget *send_widget; + GtkWidget *recv_widget; + GtkWidget *local_video; + GtkWidget *remote_video; + PurpleConnection *pc; + + guint timeout_id; + PurpleMediaSessionType request_type; +}; + +#define PIDGIN_MEDIA_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), PIDGIN_TYPE_MEDIA, PidginMediaPrivate)) + +static void pidgin_media_class_init (PidginMediaClass *klass); +static void pidgin_media_init (PidginMedia *media); +static void pidgin_media_dispose (GObject *object); +static void pidgin_media_finalize (GObject *object); +static void pidgin_media_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec); +static void pidgin_media_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec); +static void pidgin_media_set_state(PidginMedia *gtkmedia, PidginMediaState state); + +static GtkWindowClass *parent_class = NULL; + + +#if 0 +enum { + LAST_SIGNAL +}; +static guint pidgin_media_signals[LAST_SIGNAL] = {0}; +#endif + +enum { + PROP_0, + PROP_MEDIA, + PROP_SCREENNAME, + PROP_SEND_LEVEL, + PROP_RECV_LEVEL +}; + +static GType +pidgin_media_get_type(void) +{ + static GType type = 0; + + if (type == 0) { + static const GTypeInfo info = { + sizeof(PidginMediaClass), + NULL, + NULL, + (GClassInitFunc) pidgin_media_class_init, + NULL, + NULL, + sizeof(PidginMedia), + 0, + (GInstanceInitFunc) pidgin_media_init, + NULL + }; + type = g_type_register_static(GTK_TYPE_WINDOW, "PidginMedia", &info, 0); + } + return type; +} + + +static void +pidgin_media_class_init (PidginMediaClass *klass) +{ + GObjectClass *gobject_class = (GObjectClass*)klass; +/* GtkContainerClass *container_class = (GtkContainerClass*)klass; */ + parent_class = g_type_class_peek_parent(klass); + + gobject_class->dispose = pidgin_media_dispose; + gobject_class->finalize = pidgin_media_finalize; + gobject_class->set_property = pidgin_media_set_property; + gobject_class->get_property = pidgin_media_get_property; + + g_object_class_install_property(gobject_class, PROP_MEDIA, + g_param_spec_object("media", + "PurpleMedia", + "The PurpleMedia associated with this media.", + PURPLE_TYPE_MEDIA, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)); + g_object_class_install_property(gobject_class, PROP_SCREENNAME, + g_param_spec_string("screenname", + "Screenname", + "The screenname of the user this session is with.", + NULL, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)); + g_object_class_install_property(gobject_class, PROP_SEND_LEVEL, + g_param_spec_object("send-level", + "Send level", + "The GstElement of this media's send 'level'", + GST_TYPE_ELEMENT, + G_PARAM_READWRITE)); + g_object_class_install_property(gobject_class, PROP_RECV_LEVEL, + g_param_spec_object("recv-level", + "Receive level", + "The GstElement of this media's recv 'level'", + GST_TYPE_ELEMENT, + G_PARAM_READWRITE)); + + g_type_class_add_private(klass, sizeof(PidginMediaPrivate)); +} + +static void +pidgin_media_mute_toggled(GtkToggleButton *toggle, PidginMedia *media) +{ + purple_media_stream_info(media->priv->media, + gtk_toggle_button_get_active(toggle) ? + PURPLE_MEDIA_INFO_MUTE : PURPLE_MEDIA_INFO_UNMUTE, + NULL, NULL, TRUE); +} + +static gboolean +pidgin_media_delete_event_cb(GtkWidget *widget, + GdkEvent *event, PidginMedia *media) +{ + if (media->priv->media) + purple_media_stream_info(media->priv->media, + PURPLE_MEDIA_INFO_HANGUP, NULL, NULL, TRUE); + return FALSE; +} + +static int +pidgin_x_error_handler(Display *display, XErrorEvent *event) +{ + const gchar *error_type; + switch (event->error_code) { +#define XERRORCASE(type) case type: error_type = #type; break + XERRORCASE(BadAccess); + XERRORCASE(BadAlloc); + XERRORCASE(BadAtom); + XERRORCASE(BadColor); + XERRORCASE(BadCursor); + XERRORCASE(BadDrawable); + XERRORCASE(BadFont); + XERRORCASE(BadGC); + XERRORCASE(BadIDChoice); + XERRORCASE(BadImplementation); + XERRORCASE(BadLength); + XERRORCASE(BadMatch); + XERRORCASE(BadName); + XERRORCASE(BadPixmap); + XERRORCASE(BadRequest); + XERRORCASE(BadValue); + XERRORCASE(BadWindow); +#undef XERRORCASE + default: + error_type = "unknown"; + break; + } + purple_debug_error("media", "A %s Xlib error has occurred. " + "The program would normally crash now.\n", + error_type); + return 0; +} + +static void +menu_hangup(gpointer data, guint action, GtkWidget *item) +{ + PidginMedia *gtkmedia = PIDGIN_MEDIA(data); + purple_media_stream_info(gtkmedia->priv->media, + PURPLE_MEDIA_INFO_HANGUP, NULL, NULL, TRUE); +} + +static GtkItemFactoryEntry menu_items[] = { + { N_("/_Media"), NULL, NULL, 0, "<Branch>", NULL }, + { N_("/Media/_Hangup"), NULL, menu_hangup, 0, "<Item>", NULL }, +}; + +static gint menu_item_count = sizeof(menu_items) / sizeof(menu_items[0]); + +static const char * +item_factory_translate_func (const char *path, gpointer func_data) +{ + return _(path); +} + +static GtkWidget * +setup_menubar(PidginMedia *window) +{ + GtkAccelGroup *accel_group; + GtkWidget *menu; + + accel_group = gtk_accel_group_new (); + gtk_window_add_accel_group(GTK_WINDOW(window), accel_group); + g_object_unref(accel_group); + + window->priv->item_factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, + "<main>", accel_group); + + gtk_item_factory_set_translate_func(window->priv->item_factory, + (GtkTranslateFunc)item_factory_translate_func, + NULL, NULL); + + gtk_item_factory_create_items(window->priv->item_factory, + menu_item_count, menu_items, window); + g_signal_connect(G_OBJECT(accel_group), "accel-changed", + G_CALLBACK(pidgin_save_accels_cb), NULL); + + menu = gtk_item_factory_get_widget( + window->priv->item_factory, "<main>"); + + gtk_widget_show(menu); + return menu; +} + +static void +pidgin_media_init (PidginMedia *media) +{ + GtkWidget *vbox; + media->priv = PIDGIN_MEDIA_GET_PRIVATE(media); + + XSetErrorHandler(pidgin_x_error_handler); + + vbox = gtk_vbox_new(FALSE, 0); + gtk_container_add(GTK_CONTAINER(media), vbox); + + media->priv->statusbar = gtk_statusbar_new(); + gtk_box_pack_end(GTK_BOX(vbox), media->priv->statusbar, + FALSE, FALSE, 0); + gtk_statusbar_push(GTK_STATUSBAR(media->priv->statusbar), + 0, _("Calling...")); + gtk_widget_show(media->priv->statusbar); + + media->priv->menubar = setup_menubar(media); + gtk_box_pack_start(GTK_BOX(vbox), media->priv->menubar, + FALSE, TRUE, 0); + + media->priv->display = gtk_vbox_new(FALSE, PIDGIN_HIG_BOX_SPACE); + gtk_container_set_border_width(GTK_CONTAINER(media->priv->display), + PIDGIN_HIG_BOX_SPACE); + gtk_box_pack_start(GTK_BOX(vbox), media->priv->display, + TRUE, TRUE, PIDGIN_HIG_BOX_SPACE); + gtk_widget_show(vbox); + + g_signal_connect(G_OBJECT(media), "delete-event", + G_CALLBACK(pidgin_media_delete_event_cb), media); +} + +static gboolean +level_message_cb(GstBus *bus, GstMessage *message, PidginMedia *gtkmedia) +{ + gdouble rms_db; + gdouble percent; + const GValue *list; + const GValue *value; + + GstElement *src = GST_ELEMENT(GST_MESSAGE_SRC(message)); + GtkWidget *progress; + + if (message->type != GST_MESSAGE_ELEMENT) + return TRUE; + + if (!gst_structure_has_name( + gst_message_get_structure(message), "level")) + return TRUE; + + if (src == gtkmedia->priv->send_level) + progress = gtkmedia->priv->send_progress; + else if (src == gtkmedia->priv->recv_level) + progress = gtkmedia->priv->recv_progress; + else + return TRUE; + + list = gst_structure_get_value( + gst_message_get_structure(message), "rms"); + + /* Only bother with the first channel. */ + value = gst_value_list_get_value(list, 0); + rms_db = g_value_get_double(value); + + percent = pow(10, rms_db / 20) * 5; + + if(percent > 1.0) + percent = 1.0; + + gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress), percent); + return TRUE; +} + + +static void +pidgin_media_disconnect_levels(PurpleMedia *media, PidginMedia *gtkmedia) +{ + PurpleMediaManager *manager = purple_media_get_manager(media); + GstElement *element = purple_media_manager_get_pipeline(manager); + gulong handler_id = g_signal_handler_find(G_OBJECT(gst_pipeline_get_bus(GST_PIPELINE(element))), + G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA, 0, 0, + NULL, G_CALLBACK(level_message_cb), gtkmedia); + if (handler_id) + g_signal_handler_disconnect(G_OBJECT(gst_pipeline_get_bus(GST_PIPELINE(element))), + handler_id); +} + +static void +pidgin_media_dispose(GObject *media) +{ + PidginMedia *gtkmedia = PIDGIN_MEDIA(media); + purple_debug_info("gtkmedia", "pidgin_media_dispose\n"); + + if (gtkmedia->priv->media) { + purple_request_close_with_handle(gtkmedia); + purple_media_remove_output_windows(gtkmedia->priv->media); + pidgin_media_disconnect_levels(gtkmedia->priv->media, gtkmedia); + g_object_unref(gtkmedia->priv->media); + gtkmedia->priv->media = NULL; + } + + if (gtkmedia->priv->item_factory) { + g_object_unref(gtkmedia->priv->item_factory); + gtkmedia->priv->item_factory = NULL; + } + + if (gtkmedia->priv->send_level) { + gst_object_unref(gtkmedia->priv->send_level); + gtkmedia->priv->send_level = NULL; + } + + if (gtkmedia->priv->recv_level) { + gst_object_unref(gtkmedia->priv->recv_level); + gtkmedia->priv->recv_level = NULL; + } + + G_OBJECT_CLASS(parent_class)->dispose(media); +} + +static void +pidgin_media_finalize(GObject *media) +{ + /* PidginMedia *gtkmedia = PIDGIN_MEDIA(media); */ + purple_debug_info("gtkmedia", "pidgin_media_finalize\n"); + + G_OBJECT_CLASS(parent_class)->finalize(media); +} + +static void +pidgin_media_emit_message(PidginMedia *gtkmedia, const char *msg) +{ + PurpleConversation *conv = purple_find_conversation_with_account( + PURPLE_CONV_TYPE_ANY, gtkmedia->priv->screenname, + purple_connection_get_account(gtkmedia->priv->pc)); + if (conv != NULL) + purple_conversation_write(conv, NULL, msg, + PURPLE_MESSAGE_SYSTEM, time(NULL)); +} + +typedef struct +{ + PidginMedia *gtkmedia; + gchar *session_id; + gchar *participant; +} PidginMediaRealizeData; + +static gboolean +realize_cb_cb(PidginMediaRealizeData *data) +{ + PidginMediaPrivate *priv = data->gtkmedia->priv; + gulong window_id; + + if (data->participant == NULL) + window_id = GDK_WINDOW_XWINDOW(priv->local_video->window); + else + window_id = GDK_WINDOW_XWINDOW(priv->remote_video->window); + + purple_media_set_output_window(priv->media, data->session_id, + data->participant, window_id); + + g_free(data->session_id); + g_free(data->participant); + g_free(data); + return FALSE; +} + +static void +realize_cb(GtkWidget *widget, PidginMediaRealizeData *data) +{ + g_timeout_add(0, (GSourceFunc)realize_cb_cb, data); +} + +static void +pidgin_media_error_cb(PidginMedia *media, const char *error, PidginMedia *gtkmedia) +{ + PurpleConversation *conv = purple_find_conversation_with_account( + PURPLE_CONV_TYPE_ANY, gtkmedia->priv->screenname, + purple_connection_get_account(gtkmedia->priv->pc)); + if (conv != NULL) + purple_conversation_write(conv, NULL, error, + PURPLE_MESSAGE_ERROR, time(NULL)); + gtk_statusbar_push(GTK_STATUSBAR(gtkmedia->priv->statusbar), + 0, error); +} + +static void +pidgin_media_accepted_cb(PurpleMedia *media, const gchar *session_id, + const gchar *participant, PidginMedia *gtkmedia) +{ + pidgin_media_set_state(gtkmedia, PIDGIN_MEDIA_ACCEPTED); + pidgin_media_emit_message(gtkmedia, _("Call in progress.")); + gtk_statusbar_push(GTK_STATUSBAR(gtkmedia->priv->statusbar), + 0, _("Call in progress.")); + gtk_widget_show(GTK_WIDGET(gtkmedia)); +} + +static gboolean +plug_delete_event_cb(GtkWidget *widget, gpointer data) +{ + return TRUE; +} + +static gboolean +plug_removed_cb(GtkWidget *widget, gpointer data) +{ + return TRUE; +} + +static void +socket_realize_cb(GtkWidget *widget, gpointer data) +{ + gtk_socket_add_id(GTK_SOCKET(widget), + gtk_plug_get_id(GTK_PLUG(data))); +} + +static void +pidgin_media_accept_cb(PurpleMedia *media, int index) +{ + purple_media_stream_info(media, PURPLE_MEDIA_INFO_ACCEPT, + NULL, NULL, TRUE); +} + +static void +pidgin_media_reject_cb(PurpleMedia *media, int index) +{ + purple_media_stream_info(media, PURPLE_MEDIA_INFO_REJECT, + NULL, NULL, TRUE); +} + +static gboolean +pidgin_request_timeout_cb(PidginMedia *gtkmedia) +{ + PurpleConnection *pc; + PurpleBuddy *buddy; + const gchar *alias; + PurpleMediaSessionType type; + gchar *message = NULL; + + pc = purple_media_get_connection(gtkmedia->priv->media); + buddy = purple_find_buddy(purple_connection_get_account(pc), + gtkmedia->priv->screenname); + alias = buddy ? purple_buddy_get_contact_alias(buddy) : + gtkmedia->priv->screenname; + type = gtkmedia->priv->request_type; + gtkmedia->priv->timeout_id = 0; + + if (type & PURPLE_MEDIA_AUDIO && type & PURPLE_MEDIA_VIDEO) { + message = g_strdup_printf(_("%s wishes to start an audio/video session with you."), + alias); + } else if (type & PURPLE_MEDIA_AUDIO) { + message = g_strdup_printf(_("%s wishes to start an audio session with you."), + alias); + } else if (type & PURPLE_MEDIA_VIDEO) { + message = g_strdup_printf(_("%s wishes to start a video session with you."), + alias); + } + + gtkmedia->priv->request_type = PURPLE_MEDIA_NONE; + + purple_request_accept_cancel(gtkmedia, "Media invitation", + message, NULL, PURPLE_DEFAULT_ACTION_NONE, + (void*)pc, gtkmedia->priv->screenname, NULL, + gtkmedia->priv->media, + pidgin_media_accept_cb, + pidgin_media_reject_cb); + pidgin_media_emit_message(gtkmedia, message); + g_free(message); + return FALSE; +} + +static void +pidgin_media_input_volume_changed(GtkRange *range, PurpleMedia *media) +{ + double val = (double)gtk_range_get_value(GTK_RANGE(range)); + purple_prefs_set_int("/pidgin/media/audio/volume/input", val); + val /= 10.0; + purple_media_set_input_volume(media, NULL, val); +} + +static void +pidgin_media_output_volume_changed(GtkRange *range, PurpleMedia *media) +{ + double val = (double)gtk_range_get_value(GTK_RANGE(range)); + purple_prefs_set_int("/pidgin/media/audio/volume/output", val); + val /= 10.0; + purple_media_set_output_volume(media, NULL, NULL, val); +} + +static void +pidgin_media_ready_cb(PurpleMedia *media, PidginMedia *gtkmedia, const gchar *sid) +{ + PurpleMediaManager *manager = purple_media_get_manager(media); + GstElement *pipeline = purple_media_manager_get_pipeline(manager); + GtkWidget *send_widget = NULL, *recv_widget = NULL; + PurpleMediaSessionType type = + purple_media_get_session_type(media, sid); + + if (gtkmedia->priv->recv_widget == NULL + && type & (PURPLE_MEDIA_RECV_VIDEO | + PURPLE_MEDIA_RECV_AUDIO)) { + recv_widget = gtk_vbox_new(FALSE, PIDGIN_HIG_BOX_SPACE); + gtk_box_pack_start(GTK_BOX(gtkmedia->priv->display), + recv_widget, TRUE, TRUE, 0); + gtk_widget_show(recv_widget); + } else + recv_widget = gtkmedia->priv->recv_widget; + if (gtkmedia->priv->send_widget == NULL + && type & (PURPLE_MEDIA_SEND_VIDEO | + PURPLE_MEDIA_SEND_AUDIO)) { + send_widget = gtk_vbox_new(FALSE, PIDGIN_HIG_BOX_SPACE); + gtk_box_pack_start(GTK_BOX(gtkmedia->priv->display), + send_widget, TRUE, TRUE, 0); + gtk_widget_show(send_widget); + } else + send_widget = gtkmedia->priv->send_widget; + + if (type & PURPLE_MEDIA_RECV_VIDEO) { + PidginMediaRealizeData *data; + GtkWidget *aspect; + GtkWidget *remote_video; + GtkWidget *plug; + GtkWidget *socket; + GdkColor color = {0, 0, 0, 0}; + + aspect = gtk_aspect_frame_new(NULL, 0.5, 0.5, 4.0/3.0, FALSE); + gtk_frame_set_shadow_type(GTK_FRAME(aspect), GTK_SHADOW_IN); + gtk_box_pack_start(GTK_BOX(recv_widget), aspect, TRUE, TRUE, 0); + + plug = gtk_plug_new(0); + g_signal_connect(G_OBJECT(plug), "delete-event", + G_CALLBACK(plug_delete_event_cb), plug); + gtk_widget_show(plug); + + socket = gtk_socket_new(); + g_signal_connect(G_OBJECT(socket), "realize", + G_CALLBACK(socket_realize_cb), plug); + g_signal_connect(G_OBJECT(socket), "plug-removed", + G_CALLBACK(plug_removed_cb), NULL); + gtk_container_add(GTK_CONTAINER(aspect), socket); + gtk_widget_show(socket); + + data = g_new0(PidginMediaRealizeData, 1); + data->gtkmedia = gtkmedia; + data->session_id = g_strdup(sid); + data->participant = g_strdup(gtkmedia->priv->screenname); + + remote_video = gtk_drawing_area_new(); + gtk_widget_modify_bg(remote_video, GTK_STATE_NORMAL, &color); + g_signal_connect(G_OBJECT(remote_video), "realize", + G_CALLBACK(realize_cb), data); + gtk_container_add(GTK_CONTAINER(plug), remote_video); + gtk_widget_set_size_request (GTK_WIDGET(remote_video), 320, 240); + gtk_widget_show(remote_video); + gtk_widget_show(aspect); + + gtkmedia->priv->remote_video = remote_video; + } + if (type & PURPLE_MEDIA_SEND_VIDEO) { + PidginMediaRealizeData *data; + GtkWidget *aspect; + GtkWidget *local_video; + GtkWidget *plug; + GtkWidget *socket; + GdkColor color = {0, 0, 0, 0}; + + aspect = gtk_aspect_frame_new(NULL, 0.5, 0.5, 4.0/3.0, FALSE); + gtk_frame_set_shadow_type(GTK_FRAME(aspect), GTK_SHADOW_IN); + gtk_box_pack_start(GTK_BOX(send_widget), aspect, TRUE, TRUE, 0); + + plug = gtk_plug_new(0); + g_signal_connect(G_OBJECT(plug), "delete-event", + G_CALLBACK(plug_delete_event_cb), plug); + gtk_widget_show(plug); + + socket = gtk_socket_new(); + g_signal_connect(G_OBJECT(socket), "realize", + G_CALLBACK(socket_realize_cb), plug); + g_signal_connect(G_OBJECT(socket), "plug-removed", + G_CALLBACK(plug_removed_cb), NULL); + gtk_container_add(GTK_CONTAINER(aspect), socket); + gtk_widget_show(socket); + + data = g_new0(PidginMediaRealizeData, 1); + data->gtkmedia = gtkmedia; + data->session_id = g_strdup(sid); + data->participant = NULL; + + local_video = gtk_drawing_area_new(); + gtk_widget_modify_bg(local_video, GTK_STATE_NORMAL, &color); + g_signal_connect(G_OBJECT(local_video), "realize", + G_CALLBACK(realize_cb), data); + gtk_container_add(GTK_CONTAINER(plug), local_video); + gtk_widget_set_size_request (GTK_WIDGET(local_video), 160, 120); + + gtk_widget_show(local_video); + gtk_widget_show(aspect); + + gtkmedia->priv->local_video = local_video; + } + + if (type & PURPLE_MEDIA_RECV_AUDIO) { + GtkWidget *volume = gtk_hscale_new_with_range(0.0, 100.0, 5.0); + gtk_range_set_increments(GTK_RANGE(volume), 5.0, 25.0); + gtk_range_set_value(GTK_RANGE(volume), + purple_prefs_get_int( + "/pidgin/media/audio/volume/output")); + gtk_scale_set_draw_value(GTK_SCALE(volume), FALSE); + g_signal_connect (G_OBJECT(volume), "value-changed", + G_CALLBACK(pidgin_media_output_volume_changed), + media); + gtk_box_pack_end(GTK_BOX(recv_widget), + volume, FALSE, FALSE, 0); + gtk_widget_show(volume); + + gtkmedia->priv->recv_progress = gtk_progress_bar_new(); + gtk_widget_set_size_request(gtkmedia->priv->recv_progress, 320, 10); + gtk_box_pack_end(GTK_BOX(recv_widget), + gtkmedia->priv->recv_progress, FALSE, FALSE, 0); + gtk_widget_show(gtkmedia->priv->recv_progress); + } + if (type & PURPLE_MEDIA_SEND_AUDIO) { + GstElement *media_src; + GtkWidget *hbox, *volume; + + hbox = gtk_hbox_new(FALSE, PIDGIN_HIG_BOX_SPACE); + gtk_box_pack_end(GTK_BOX(send_widget), hbox, FALSE, FALSE, 0); + gtkmedia->priv->mute = + gtk_toggle_button_new_with_mnemonic("_Mute"); + g_signal_connect(gtkmedia->priv->mute, "toggled", + G_CALLBACK(pidgin_media_mute_toggled), + gtkmedia); + gtk_box_pack_end(GTK_BOX(hbox), gtkmedia->priv->mute, + FALSE, FALSE, 0); + gtk_widget_show(gtkmedia->priv->mute); + gtk_widget_show(GTK_WIDGET(hbox)); + + volume = gtk_hscale_new_with_range(0.0, 100.0, 5.0); + gtk_range_set_increments(GTK_RANGE(volume), 5.0, 25.0); + gtk_range_set_value(GTK_RANGE(volume), + purple_prefs_get_int( + "/pidgin/media/audio/volume/input")); + gtk_scale_set_draw_value(GTK_SCALE(volume), FALSE); + g_signal_connect (G_OBJECT(volume), "value-changed", + G_CALLBACK (pidgin_media_input_volume_changed), + media); + gtk_box_pack_end(GTK_BOX(send_widget), + volume, FALSE, FALSE, 0); + gtk_widget_show(volume); + + media_src = purple_media_get_src(media, sid); + gtkmedia->priv->send_level = gst_bin_get_by_name( + GST_BIN(media_src), "sendlevel"); + + gtkmedia->priv->send_progress = gtk_progress_bar_new(); + gtk_widget_set_size_request(gtkmedia->priv->send_progress, 320, 10); + gtk_box_pack_end(GTK_BOX(send_widget), + gtkmedia->priv->send_progress, FALSE, FALSE, 0); + gtk_widget_show(gtkmedia->priv->send_progress); + + gtk_widget_show(gtkmedia->priv->mute); + } + + + if (type & PURPLE_MEDIA_AUDIO) { + GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline)); + g_signal_connect(G_OBJECT(bus), "message::element", + G_CALLBACK(level_message_cb), gtkmedia); + gst_object_unref(bus); + } + + if (send_widget != NULL) + gtkmedia->priv->send_widget = send_widget; + if (recv_widget != NULL) + gtkmedia->priv->recv_widget = recv_widget; + + if (purple_media_is_initiator(media, sid, NULL) == FALSE) { + if (gtkmedia->priv->timeout_id != 0) + g_source_remove(gtkmedia->priv->timeout_id); + gtkmedia->priv->request_type |= type; + gtkmedia->priv->timeout_id = g_timeout_add(500, + (GSourceFunc)pidgin_request_timeout_cb, + gtkmedia); + } + + gtk_widget_show(gtkmedia->priv->display); +} + +static void +pidgin_media_state_changed_cb(PurpleMedia *media, PurpleMediaState state, + gchar *sid, gchar *name, PidginMedia *gtkmedia) +{ + purple_debug_info("gtkmedia", "state: %d sid: %s name: %s\n", + state, sid, name); + if (sid == NULL && name == NULL) { + if (state == PURPLE_MEDIA_STATE_END) { + pidgin_media_emit_message(gtkmedia, + _("The call has been terminated.")); + gtk_widget_destroy(GTK_WIDGET(gtkmedia)); + } + } else if (state == PURPLE_MEDIA_STATE_NEW && + sid != NULL && name != NULL) { + pidgin_media_ready_cb(media, gtkmedia, sid); + } else if (state == PURPLE_MEDIA_STATE_CONNECTED && + purple_media_get_session_type(media, sid) & + PURPLE_MEDIA_RECV_AUDIO) { + GstElement *tee = purple_media_get_tee(media, sid, name); + GstIterator *iter = gst_element_iterate_src_pads(tee); + GstPad *sinkpad; + if (gst_iterator_next(iter, (gpointer)&sinkpad) + == GST_ITERATOR_OK) { + GstPad *peer = gst_pad_get_peer(sinkpad); + if (peer != NULL) { + gtkmedia->priv->recv_level = + gst_bin_get_by_name( + GST_BIN(GST_OBJECT_PARENT( + peer)), "recvlevel"); + gst_object_unref(peer); + } + gst_object_unref(sinkpad); + } + gst_iterator_free(iter); + } +} + +static void +pidgin_media_stream_info_cb(PurpleMedia *media, PurpleMediaInfoType type, + gchar *sid, gchar *name, gboolean local, + PidginMedia *gtkmedia) +{ + if (type == PURPLE_MEDIA_INFO_REJECT) { + pidgin_media_emit_message(gtkmedia, + _("You have rejected the call.")); + } +} + +static void +pidgin_media_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) +{ + PidginMedia *media; + g_return_if_fail(PIDGIN_IS_MEDIA(object)); + + media = PIDGIN_MEDIA(object); + switch (prop_id) { + case PROP_MEDIA: + { + if (media->priv->media) + g_object_unref(media->priv->media); + media->priv->media = g_value_get_object(value); + g_object_ref(media->priv->media); + + if (purple_media_is_initiator(media->priv->media, + NULL, NULL) == TRUE) + pidgin_media_set_state(media, PIDGIN_MEDIA_WAITING); + else + pidgin_media_set_state(media, PIDGIN_MEDIA_REQUESTED); + + g_signal_connect(G_OBJECT(media->priv->media), "error", + G_CALLBACK(pidgin_media_error_cb), media); + g_signal_connect(G_OBJECT(media->priv->media), "accepted", + G_CALLBACK(pidgin_media_accepted_cb), media); + g_signal_connect(G_OBJECT(media->priv->media), "state-changed", + G_CALLBACK(pidgin_media_state_changed_cb), media); + g_signal_connect(G_OBJECT(media->priv->media), "stream-info", + G_CALLBACK(pidgin_media_stream_info_cb), media); + break; + } + case PROP_SCREENNAME: + if (media->priv->screenname) + g_free(media->priv->screenname); + media->priv->screenname = g_value_dup_string(value); + break; + case PROP_SEND_LEVEL: + if (media->priv->send_level) + gst_object_unref(media->priv->send_level); + media->priv->send_level = g_value_get_object(value); + g_object_ref(media->priv->send_level); + break; + case PROP_RECV_LEVEL: + if (media->priv->recv_level) + gst_object_unref(media->priv->recv_level); + media->priv->recv_level = g_value_get_object(value); + g_object_ref(media->priv->recv_level); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +pidgin_media_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) +{ + PidginMedia *media; + g_return_if_fail(PIDGIN_IS_MEDIA(object)); + + media = PIDGIN_MEDIA(object); + + switch (prop_id) { + case PROP_MEDIA: + g_value_set_object(value, media->priv->media); + break; + case PROP_SCREENNAME: + g_value_set_string(value, media->priv->screenname); + break; + case PROP_SEND_LEVEL: + g_value_set_object(value, media->priv->send_level); + break; + case PROP_RECV_LEVEL: + g_value_set_object(value, media->priv->recv_level); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static GtkWidget * +pidgin_media_new(PurpleMedia *media, const gchar *screenname) +{ + PidginMedia *gtkmedia = g_object_new(pidgin_media_get_type(), + "media", media, + "screenname", screenname, NULL); + return GTK_WIDGET(gtkmedia); +} + +static void +pidgin_media_set_state(PidginMedia *gtkmedia, PidginMediaState state) +{ + gtkmedia->priv->state = state; +} + +static gboolean +pidgin_media_new_cb(PurpleMediaManager *manager, PurpleMedia *media, + PurpleConnection *pc, gchar *screenname, gpointer nul) +{ + PidginMedia *gtkmedia = PIDGIN_MEDIA( + pidgin_media_new(media, screenname)); + PurpleBuddy *buddy = purple_find_buddy( + purple_connection_get_account(pc), screenname); + const gchar *alias = buddy ? + purple_buddy_get_contact_alias(buddy) : screenname; + gtkmedia->priv->pc = pc; + gtk_window_set_title(GTK_WINDOW(gtkmedia), alias); + + if (purple_media_is_initiator(media, NULL, NULL) == TRUE) + gtk_widget_show(GTK_WIDGET(gtkmedia)); + + return TRUE; +} + +static GstElement * +create_default_video_src(PurpleMedia *media, + const gchar *session_id, const gchar *participant) +{ + GstElement *sendbin, *src, *videoscale, *capsfilter; + GstPad *pad; + GstPad *ghost; + GstCaps *caps; + + src = gst_element_factory_make("gconfvideosrc", NULL); + if (src == NULL) + src = gst_element_factory_make("autovideosrc", NULL); + if (src == NULL) + src = gst_element_factory_make("v4l2src", NULL); + if (src == NULL) + src = gst_element_factory_make("v4lsrc", NULL); + if (src == NULL) + src = gst_element_factory_make("ksvideosrc", NULL); + if (src == NULL) + src = gst_element_factory_make("dshowvideosrc", NULL); + if (src == NULL) { + purple_debug_error("gtkmedia", "Unable to find a suitable " + "element for the default video source.\n"); + return NULL; + } + + sendbin = gst_bin_new("pidgindefaultvideosrc"); + videoscale = gst_element_factory_make("videoscale", NULL); + capsfilter = gst_element_factory_make("capsfilter", NULL); + + /* It was recommended to set the size <= 352x288 and framerate <= 20 */ + caps = gst_caps_from_string("video/x-raw-yuv , width=[250,352] , " + "height=[200,288] , framerate=[1/1,20/1]"); + g_object_set(G_OBJECT(capsfilter), "caps", caps, NULL); + + gst_bin_add_many(GST_BIN(sendbin), src, + videoscale, capsfilter, NULL); + gst_element_link_many(src, videoscale, capsfilter, NULL); + + pad = gst_element_get_static_pad(capsfilter, "src"); + ghost = gst_ghost_pad_new("ghostsrc", pad); + gst_object_unref(pad); + gst_element_add_pad(sendbin, ghost); + + return sendbin; +} + +static GstElement * +create_default_video_sink(PurpleMedia *media, + const gchar *session_id, const gchar *participant) +{ + GstElement *sink = gst_element_factory_make("gconfvideosink", NULL); + if (sink == NULL) + sink = gst_element_factory_make("autovideosink", NULL); + if (sink == NULL) + purple_debug_error("gtkmedia", "Unable to find a suitable " + "element for the default video sink.\n"); + return sink; +} + +static GstElement * +create_default_audio_src(PurpleMedia *media, + const gchar *session_id, const gchar *participant) +{ + GstElement *bin, *src, *volume, *level; + GstPad *pad, *ghost; + double input_volume = purple_prefs_get_int( + "/pidgin/media/audio/volume/input")/10.0; + + src = gst_element_factory_make("gconfaudiosrc", NULL); + if (src == NULL) + src = gst_element_factory_make("autoaudiosrc", NULL); + if (src == NULL) + src = gst_element_factory_make("alsasrc", NULL); + if (src == NULL) + src = gst_element_factory_make("osssrc", NULL); + if (src == NULL) + src = gst_element_factory_make("dshowaudiosrc", NULL); + if (src == NULL) { + purple_debug_error("gtkmedia", "Unable to find a suitable " + "element for the default audio source.\n"); + return NULL; + } + + bin = gst_bin_new("pidgindefaultaudiosrc"); + volume = gst_element_factory_make("volume", "purpleaudioinputvolume"); + g_object_set(volume, "volume", input_volume, NULL); + level = gst_element_factory_make("level", "sendlevel"); + gst_bin_add_many(GST_BIN(bin), src, volume, level, NULL); + gst_element_link(src, volume); + gst_element_link(volume, level); + pad = gst_element_get_pad(level, "src"); + ghost = gst_ghost_pad_new("ghostsrc", pad); + gst_element_add_pad(bin, ghost); + g_object_set(G_OBJECT(level), "message", TRUE, NULL); + + return bin; +} + +static GstElement * +create_default_audio_sink(PurpleMedia *media, + const gchar *session_id, const gchar *participant) +{ + GstElement *bin, *sink, *volume, *level, *queue; + GstPad *pad, *ghost; + double output_volume = purple_prefs_get_int( + "/pidgin/media/audio/volume/output")/10.0; + + sink = gst_element_factory_make("gconfaudiosink", NULL); + if (sink == NULL) + sink = gst_element_factory_make("autoaudiosink",NULL); + if (sink == NULL) { + purple_debug_error("gtkmedia", "Unable to find a suitable " + "element for the default audio sink.\n"); + return NULL; + } + + bin = gst_bin_new("pidginrecvaudiobin"); + volume = gst_element_factory_make("volume", "purpleaudiooutputvolume"); + g_object_set(volume, "volume", output_volume, NULL); + level = gst_element_factory_make("level", "recvlevel"); + queue = gst_element_factory_make("queue", NULL); + gst_bin_add_many(GST_BIN(bin), sink, volume, level, queue, NULL); + gst_element_link(level, sink); + gst_element_link(volume, level); + gst_element_link(queue, volume); + pad = gst_element_get_pad(queue, "sink"); + ghost = gst_ghost_pad_new("ghostsink", pad); + gst_element_add_pad(bin, ghost); + g_object_set(G_OBJECT(level), "message", TRUE, NULL); + + return bin; +} +#endif /* USE_VV */ + +void +pidgin_medias_init(void) +{ +#ifdef USE_VV + PurpleMediaManager *manager = purple_media_manager_get(); + PurpleMediaElementInfo *default_video_src = + g_object_new(PURPLE_TYPE_MEDIA_ELEMENT_INFO, + "id", "pidgindefaultvideosrc", + "name", "Pidgin Default Video Source", + "type", PURPLE_MEDIA_ELEMENT_VIDEO + | PURPLE_MEDIA_ELEMENT_SRC + | PURPLE_MEDIA_ELEMENT_ONE_SRC + | PURPLE_MEDIA_ELEMENT_UNIQUE, + "create-cb", create_default_video_src, NULL); + PurpleMediaElementInfo *default_video_sink = + g_object_new(PURPLE_TYPE_MEDIA_ELEMENT_INFO, + "id", "pidgindefaultvideosink", + "name", "Pidgin Default Video Sink", + "type", PURPLE_MEDIA_ELEMENT_VIDEO + | PURPLE_MEDIA_ELEMENT_SINK + | PURPLE_MEDIA_ELEMENT_ONE_SINK, + "create-cb", create_default_video_sink, NULL); + PurpleMediaElementInfo *default_audio_src = + g_object_new(PURPLE_TYPE_MEDIA_ELEMENT_INFO, + "id", "pidgindefaultaudiosrc", + "name", "Pidgin Default Audio Source", + "type", PURPLE_MEDIA_ELEMENT_AUDIO + | PURPLE_MEDIA_ELEMENT_SRC + | PURPLE_MEDIA_ELEMENT_ONE_SRC + | PURPLE_MEDIA_ELEMENT_UNIQUE, + "create-cb", create_default_audio_src, NULL); + PurpleMediaElementInfo *default_audio_sink = + g_object_new(PURPLE_TYPE_MEDIA_ELEMENT_INFO, + "id", "pidgindefaultaudiosink", + "name", "Pidgin Default Audio Sink", + "type", PURPLE_MEDIA_ELEMENT_AUDIO + | PURPLE_MEDIA_ELEMENT_SINK + | PURPLE_MEDIA_ELEMENT_ONE_SINK, + "create-cb", create_default_audio_sink, NULL); + + g_signal_connect(G_OBJECT(manager), "init-media", + G_CALLBACK(pidgin_media_new_cb), NULL); + + purple_media_manager_set_ui_caps(manager, + PURPLE_MEDIA_CAPS_AUDIO | + PURPLE_MEDIA_CAPS_AUDIO_SINGLE_DIRECTION | + PURPLE_MEDIA_CAPS_VIDEO | + PURPLE_MEDIA_CAPS_VIDEO_SINGLE_DIRECTION | + PURPLE_MEDIA_CAPS_AUDIO_VIDEO); + + purple_debug_info("gtkmedia", "Registering media element types\n"); + purple_media_manager_set_active_element(manager, default_video_src); + purple_media_manager_set_active_element(manager, default_video_sink); + purple_media_manager_set_active_element(manager, default_audio_src); + purple_media_manager_set_active_element(manager, default_audio_sink); + + purple_prefs_add_none("/pidgin/media"); + purple_prefs_add_none("/pidgin/media/audio"); + purple_prefs_add_none("/pidgin/media/audio/volume"); + purple_prefs_add_int("/pidgin/media/audio/volume/input", 10); + purple_prefs_add_int("/pidgin/media/audio/volume/output", 10); +#endif +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pidgin/gtkmedia.h Fri Apr 03 16:45:38 2009 +0000 @@ -0,0 +1,35 @@ +/** + * @file media.h Account API + * @ingroup core + * + * Pidgin + * + * Pidgin is the legal property of its developers, whose names are too numerous + * to list here. Please refer to the COPYRIGHT file distributed with this + * source distribution. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef __GTKMEDIA_H_ +#define __GTKMEDIA_H_ + +G_BEGIN_DECLS + +void pidgin_medias_init(void); + +G_END_DECLS + +#endif /* __GTKMEDIA_H_ */
--- a/pidgin/gtkprefs.c Fri Apr 03 02:43:22 2009 +0000 +++ b/pidgin/gtkprefs.c Fri Apr 03 16:45:38 2009 +0000 @@ -145,6 +145,26 @@ return pidgin_add_widget_to_vbox(GTK_BOX(page), title, sg, entry, TRUE, NULL); } +GtkWidget * +pidgin_prefs_labeled_password(GtkWidget *page, const gchar *title, + const char *key, GtkSizeGroup *sg) +{ + GtkWidget *entry; + const gchar *value; + + value = purple_prefs_get_string(key); + + entry = gtk_entry_new(); + gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE); + gtk_entry_set_text(GTK_ENTRY(entry), value); + g_signal_connect(G_OBJECT(entry), "changed", + G_CALLBACK(entry_set), (char*)key); + gtk_widget_show(entry); + + return pidgin_add_widget_to_vbox(GTK_BOX(page), title, sg, entry, TRUE, NULL); +} + + static void dropdown_set(GObject *w, const char *key) { @@ -1406,6 +1426,28 @@ purple_network_set_public_ip(gtk_entry_get_text(entry)); } +static gboolean network_stun_server_changed_cb(GtkWidget *widget, + GdkEventFocus *event, gpointer data) +{ + GtkEntry *entry = GTK_ENTRY(widget); + purple_prefs_set_string("/purple/network/stun_server", + gtk_entry_get_text(entry)); + purple_network_set_stun_server(gtk_entry_get_text(entry)); + + return FALSE; +} + +static gboolean network_turn_server_changed_cb(GtkWidget *widget, + GdkEventFocus *event, gpointer data) +{ + GtkEntry *entry = GTK_ENTRY(widget); + purple_prefs_set_string("/purple/network/turn_server", + gtk_entry_get_text(entry)); + purple_network_set_turn_server(gtk_entry_get_text(entry)); + + return FALSE; +} + static void proxy_changed_cb(const char *name, PurplePrefType type, gconstpointer value, gpointer data) @@ -1471,8 +1513,16 @@ vbox = pidgin_make_frame (ret, _("IP Address")); sg = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL); - pidgin_prefs_labeled_entry(vbox,_("ST_UN server:"), - "/purple/network/stun_server", sg); + + entry = gtk_entry_new(); + gtk_entry_set_text(GTK_ENTRY(entry), purple_prefs_get_string( + "/purple/network/stun_server")); + g_signal_connect(G_OBJECT(entry), "focus-out-event", + G_CALLBACK(network_stun_server_changed_cb), NULL); + gtk_widget_show(entry); + + pidgin_add_widget_to_vbox(GTK_BOX(vbox), "ST_UN server:", + sg, entry, TRUE, NULL); hbox = gtk_hbox_new(FALSE, PIDGIN_HIG_BOX_SPACE); gtk_container_add(GTK_CONTAINER(vbox), hbox); @@ -1550,6 +1600,29 @@ g_signal_connect(G_OBJECT(ports_checkbox), "clicked", G_CALLBACK(pidgin_toggle_sensitive), spin_button); + g_object_unref(sg); + + /* TURN server */ + vbox = pidgin_make_frame(ret, _("Relay Server (TURN)")); + sg = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL); + + entry = gtk_entry_new(); + gtk_entry_set_text(GTK_ENTRY(entry), purple_prefs_get_string( + "/purple/network/turn_server")); + g_signal_connect(G_OBJECT(entry), "focus-out-event", + G_CALLBACK(network_turn_server_changed_cb), NULL); + gtk_widget_show(entry); + + hbox = pidgin_add_widget_to_vbox(GTK_BOX(vbox), "_TURN server:", + sg, entry, TRUE, NULL); + + pidgin_prefs_labeled_spin_button(hbox, _("_Port:"), + "/purple/network/turn_port", 0, 65535, NULL); + hbox = pidgin_prefs_labeled_entry(vbox, "_Username:", + "/purple/network/turn_username", sg); + pidgin_prefs_labeled_password(hbox, "_Password:", + "/purple/network/turn_password", NULL); + if (purple_running_gnome()) { vbox = pidgin_make_frame(ret, _("Proxy Server & Browser")); prefs_proxy_frame = gtk_vbox_new(FALSE, 0);
--- a/pidgin/gtkprefs.h Fri Apr 03 02:43:22 2009 +0000 +++ b/pidgin/gtkprefs.h Fri Apr 03 16:45:38 2009 +0000 @@ -81,6 +81,22 @@ const char *key, GtkSizeGroup *sg); /** + * Add a new entry representing a password (string) preference + * The entry will use a password-style text entry (the text is substituded) + * + * @param page The page to which the entry will be added + * @param title The text to be displayed as the entry label + * @param key The key of the string pref that will be represented by the entry + * @param sg If not NULL, the size group to which the entry will be added + * + * @return An hbox containing both the label and the entry. Can be used to set + * the widgets to sensitive or insensitive based on the value of a + * checkbox. + */ +GtkWidget *pidgin_prefs_labeled_password(GtkWidget *page, const gchar *title, + const char *key, GtkSizeGroup *sg); + +/** * Add a new dropdown representing a preference of the specified type * * @param page The page to which the dropdown will be added
--- a/pidgin/pidginstock.c Fri Apr 03 02:43:22 2009 +0000 +++ b/pidgin/pidginstock.c Fri Apr 03 16:45:38 2009 +0000 @@ -197,7 +197,12 @@ { PIDGIN_STOCK_TOOLBAR_UNBLOCK, "toolbar", "unblock.png", FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL }, { PIDGIN_STOCK_TOOLBAR_SELECT_AVATAR, "toolbar", "select-avatar.png", FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, NULL }, { PIDGIN_STOCK_TOOLBAR_SEND_FILE, "toolbar", "send-file.png", FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL }, - { PIDGIN_STOCK_TOOLBAR_TRANSFER, "toolbar", "transfer.png", FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL } + { PIDGIN_STOCK_TOOLBAR_TRANSFER, "toolbar", "transfer.png", FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL }, +#ifdef USE_VV + { PIDGIN_STOCK_TOOLBAR_AUDIO_CALL, "toolbar", "audio-call.png", FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL }, + { PIDGIN_STOCK_TOOLBAR_VIDEO_CALL, "toolbar", "video-call.png", FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL }, + { PIDGIN_STOCK_TOOLBAR_AUDIO_VIDEO_CALL, "toolbar", "audio-video-call.png", FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL }, +#endif }; const SizedStockIcon sized_status_icons [] = {
--- a/pidgin/pidginstock.h Fri Apr 03 02:43:22 2009 +0000 +++ b/pidgin/pidginstock.h Fri Apr 03 16:45:38 2009 +0000 @@ -154,6 +154,11 @@ #define PIDGIN_STOCK_TOOLBAR_SELECT_AVATAR "pidgin-select-avatar" #define PIDGIN_STOCK_TOOLBAR_SEND_FILE "pidgin-send-file" #define PIDGIN_STOCK_TOOLBAR_TRANSFER "pidgin-transfer" +#ifdef USE_VV +#define PIDGIN_STOCK_TOOLBAR_AUDIO_CALL "pidgin-audio-call" +#define PIDGIN_STOCK_TOOLBAR_VIDEO_CALL "pidgin-video-call" +#define PIDGIN_STOCK_TOOLBAR_AUDIO_VIDEO_CALL "pidgin-audio-video-call" +#endif /* Tray icons */ #define PIDGIN_STOCK_TRAY_AVAILABLE "pidgin-tray-available"
--- a/pidgin/pixmaps/Makefile.am Fri Apr 03 02:43:22 2009 +0000 +++ b/pidgin/pixmaps/Makefile.am Fri Apr 03 16:45:38 2009 +0000 @@ -429,6 +429,7 @@ toolbar/16/scalable/font-size-up.svg TOOLBAR_16 = \ + toolbar/16/audio-call.png \ toolbar/16/change-bgcolor.png \ toolbar/16/change-fgcolor.png \ toolbar/16/emote-select.png \ @@ -442,7 +443,8 @@ toolbar/16/plugins.png \ toolbar/16/send-file.png \ toolbar/16/transfer.png \ - toolbar/16/unblock.png + toolbar/16/unblock.png \ + toolbar/16/video-call.png TOOLBAR_22_SCALABLE = \ toolbar/22/scalable/select-avatar.svg
--- a/po/POTFILES.in Fri Apr 03 02:43:22 2009 +0000 +++ b/po/POTFILES.in Fri Apr 03 16:45:38 2009 +0000 @@ -8,6 +8,7 @@ finch/gntdebug.c finch/gntft.c finch/gntlog.c +finch/gntmedia.c finch/gntnotify.c finch/gntplugin.c finch/gntpounce.c @@ -87,6 +88,7 @@ libpurple/protocols/jabber/buddy.c libpurple/protocols/jabber/chat.c libpurple/protocols/jabber/jabber.c +libpurple/protocols/jabber/jingle.c libpurple/protocols/jabber/libxmpp.c libpurple/protocols/jabber/message.c libpurple/protocols/jabber/parser.c @@ -202,6 +204,7 @@ pidgin/gtkimhtmltoolbar.c pidgin/gtklog.c pidgin/gtkmain.c +pidgin/gtkmedia.c pidgin/gtknotify.c pidgin/gtkplugin.c pidgin/gtkpounce.c