6 * Copyright (C) 2010-12 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "glib/gstdio.h"
33 #include "sipe-common.h"
35 #include "mediamanager.h"
39 /* wrappers for write() & friends for socket handling */
40 #include "win32/win32dep.h"
43 #include "sipe-backend.h"
44 #include "sipe-core.h"
46 #include "purple-private.h"
49 * GValueArray has been marked deprecated in glib 2.32 but it is still used by
50 * libpurple and libfarsight APIs. Therefore we need to disable the deprecated
51 * warning so that the code still compiles for platforms that enable it.
53 * GStreamer interfaces fail to compile on ARM architecture with -Wcast-align
55 * Diagnostic #pragma was added in GCC 4.2.0
57 #if defined(__GNUC__) && (__GNUC__ >= 4) && (__GNUC_MINOR__ >= 2)
58 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
59 #if defined(__ARMEL__) || defined(__ARMEB__) || defined(__mips__) || defined(__sparc__)
60 #pragma GCC diagnostic ignored "-Wcast-align"
64 #include "media-gst.h"
66 struct sipe_backend_media
{
70 * Number of media streams that were not yet locally accepted or rejected.
72 guint unconfirmed_streams
;
75 struct sipe_backend_stream
{
78 gboolean candidates_prepared
;
79 gboolean local_on_hold
;
80 gboolean remote_on_hold
;
85 backend_stream_free(struct sipe_backend_stream
*stream
)
88 g_free(stream
->sessionid
);
89 g_free(stream
->participant
);
94 static PurpleMediaSessionType
sipe_media_to_purple(SipeMediaType type
);
95 static PurpleMediaCandidateType
sipe_candidate_type_to_purple(SipeCandidateType type
);
96 static SipeCandidateType
purple_candidate_type_to_sipe(PurpleMediaCandidateType type
);
97 static PurpleMediaNetworkProtocol
sipe_network_protocol_to_purple(SipeNetworkProtocol proto
);
98 static SipeNetworkProtocol
purple_network_protocol_to_sipe(PurpleMediaNetworkProtocol proto
);
101 on_candidates_prepared_cb(SIPE_UNUSED_PARAMETER PurpleMedia
*media
,
103 SIPE_UNUSED_PARAMETER gchar
*participant
,
104 struct sipe_media_call
*call
)
106 struct sipe_backend_stream
*stream
;
107 stream
= sipe_backend_media_get_stream_by_id(call
->backend_private
, sessionid
);
109 stream
->candidates_prepared
= TRUE
;
111 if (call
->candidates_prepared_cb
&&
112 sipe_backend_candidates_prepared(call
->backend_private
)) {
113 call
->candidates_prepared_cb(call
, stream
);
118 on_state_changed_cb(SIPE_UNUSED_PARAMETER PurpleMedia
*media
,
119 PurpleMediaState state
,
122 struct sipe_media_call
*call
)
124 SIPE_DEBUG_INFO("sipe_media_state_changed_cb: %d %s %s\n", state
, sessionid
, participant
);
125 if (state
== PURPLE_MEDIA_STATE_END
&&
126 !sessionid
&& !participant
&& call
->media_end_cb
)
127 call
->media_end_cb(call
);
131 capture_pipeline(const gchar
*label
) {
132 PurpleMediaManager
*manager
= purple_media_manager_get();
133 GstElement
*pipeline
= purple_media_manager_get_pipeline(manager
);
134 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS(GST_BIN(pipeline
), GST_DEBUG_GRAPH_SHOW_ALL
, label
);
138 on_error_cb(SIPE_UNUSED_PARAMETER PurpleMedia
*media
, gchar
*message
,
139 struct sipe_media_call
*call
)
141 capture_pipeline("ERROR");
144 call
->error_cb(call
, message
);
148 on_stream_info_cb(SIPE_UNUSED_PARAMETER PurpleMedia
*media
,
149 PurpleMediaInfoType type
,
153 struct sipe_media_call
*call
)
155 if (type
== PURPLE_MEDIA_INFO_ACCEPT
) {
156 if (call
->call_accept_cb
&& !sessionid
&& !participant
)
157 call
->call_accept_cb(call
, local
);
158 else if (sessionid
&& participant
) {
159 struct sipe_backend_stream
*stream
;
160 stream
= sipe_backend_media_get_stream_by_id(call
->backend_private
,
163 if (!stream
->accepted
&& local
)
164 --call
->backend_private
->unconfirmed_streams
;
165 stream
->accepted
= TRUE
;
168 } else if (type
== PURPLE_MEDIA_INFO_HOLD
|| type
== PURPLE_MEDIA_INFO_UNHOLD
) {
170 gboolean state
= (type
== PURPLE_MEDIA_INFO_HOLD
);
173 // Hold specific stream
174 struct sipe_backend_stream
*stream
;
175 stream
= sipe_backend_media_get_stream_by_id(call
->backend_private
,
179 stream
->local_on_hold
= state
;
181 stream
->remote_on_hold
= state
;
184 GSList
*i
= sipe_backend_media_get_streams(call
->backend_private
);
185 for (; i
; i
= i
->next
) {
186 struct sipe_backend_stream
*stream
= i
->data
;
189 stream
->local_on_hold
= state
;
191 stream
->remote_on_hold
= state
;
195 if (call
->call_hold_cb
)
196 call
->call_hold_cb(call
, local
, state
);
197 } else if (type
== PURPLE_MEDIA_INFO_HANGUP
|| type
== PURPLE_MEDIA_INFO_REJECT
) {
198 if (!sessionid
&& !participant
) {
199 if (type
== PURPLE_MEDIA_INFO_HANGUP
&& call
->call_hangup_cb
)
200 call
->call_hangup_cb(call
, local
);
201 else if (type
== PURPLE_MEDIA_INFO_REJECT
&& call
->call_reject_cb
&& !local
)
202 call
->call_reject_cb(call
, local
);
203 } else if (sessionid
&& participant
) {
204 struct sipe_backend_stream
*stream
;
205 stream
= sipe_backend_media_get_stream_by_id(call
->backend_private
,
209 call
->backend_private
->streams
= g_slist_remove(call
->backend_private
->streams
, stream
);
210 backend_stream_free(stream
);
211 if (local
&& --call
->backend_private
->unconfirmed_streams
== 0 &&
212 call
->call_reject_cb
)
213 call
->call_reject_cb(call
, local
);
219 struct sipe_backend_media
*
220 sipe_backend_media_new(struct sipe_core_public
*sipe_public
,
221 struct sipe_media_call
*call
,
222 const gchar
*participant
,
225 struct sipe_backend_media
*media
= g_new0(struct sipe_backend_media
, 1);
226 struct sipe_backend_private
*purple_private
= sipe_public
->backend_private
;
227 PurpleMediaManager
*manager
= purple_media_manager_get();
228 GstElement
*pipeline
;
230 media
->m
= purple_media_manager_create_media(manager
,
231 purple_private
->account
,
233 participant
, initiator
);
235 g_signal_connect(G_OBJECT(media
->m
), "candidates-prepared",
236 G_CALLBACK(on_candidates_prepared_cb
), call
);
237 g_signal_connect(G_OBJECT(media
->m
), "stream-info",
238 G_CALLBACK(on_stream_info_cb
), call
);
239 g_signal_connect(G_OBJECT(media
->m
), "error",
240 G_CALLBACK(on_error_cb
), call
);
241 g_signal_connect(G_OBJECT(media
->m
), "state-changed",
242 G_CALLBACK(on_state_changed_cb
), call
);
244 /* On error, the pipeline is no longer in PLAYING state and libpurple
245 * will not switch it back to PLAYING, preventing any more calls until
246 * application restart. We switch the state ourselves here to negate
247 * effect of any error in previous call (if any). */
248 pipeline
= purple_media_manager_get_pipeline(manager
);
249 gst_element_set_state(pipeline
, GST_STATE_PLAYING
);
255 sipe_backend_media_free(struct sipe_backend_media
*media
)
258 GSList
*stream
= media
->streams
;
260 for (; stream
; stream
= g_slist_delete_link(stream
, stream
))
261 backend_stream_free(stream
->data
);
268 sipe_backend_media_set_cname(struct sipe_backend_media
*media
, gchar
*cname
)
271 guint num_params
= 3;
272 GParameter
*params
= g_new0(GParameter
, num_params
);
273 params
[0].name
= "sdes-cname";
274 g_value_init(¶ms
[0].value
, G_TYPE_STRING
);
275 g_value_set_string(¶ms
[0].value
, cname
);
276 params
[1].name
= "sdes-name";
277 g_value_init(¶ms
[1].value
, G_TYPE_STRING
);
278 params
[2].name
= "sdes-tool";
279 g_value_init(¶ms
[2].value
, G_TYPE_STRING
);
281 purple_media_set_params(media
->m
, num_params
, params
);
283 g_value_unset(¶ms
[0].value
);
288 #define FS_CODECS_CONF \
289 "# Automatically created by SIPE plugin\n" \
291 "farsight-send-profile=videoscale ! ffmpegcolorspace ! fsvideoanyrate ! ffenc_h263 rtp-payload-size=30 ! rtph263pay\n" \
294 "farsight-send-profile=audioconvert ! audioresample ! audioconvert ! alawenc ! rtppcmapay min-ptime=20000000 max-ptime=20000000\n" \
297 "farsight-send-profile=audioconvert ! audioresample ! audioconvert ! mulawenc ! rtppcmupay min-ptime=20000000 max-ptime=20000000\n";
303 filename
= g_build_filename(purple_user_dir(), "fs-codec.conf", NULL
);
305 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
306 int fd
= g_open(filename
, O_CREAT
| O_WRONLY
, S_IRUSR
| S_IWUSR
);
307 gchar
*fs_codecs_conf
= FS_CODECS_CONF
;
308 if ((fd
< 0) || write(fd
, fs_codecs_conf
, strlen(fs_codecs_conf
)) == -1)
309 SIPE_DEBUG_ERROR_NOFORMAT("Can not create fs-codec.conf!");
318 append_relay(GValueArray
*relay_info
, const gchar
*ip
, guint port
, gchar
*type
,
319 gchar
*username
, gchar
*password
)
322 GstStructure
*gst_relay_info
;
324 gst_relay_info
= gst_structure_new("relay-info",
325 "ip", G_TYPE_STRING
, ip
,
326 "port", G_TYPE_UINT
, port
,
327 "relay-type", G_TYPE_STRING
, type
,
328 "username", G_TYPE_STRING
, username
,
329 "password", G_TYPE_STRING
, password
,
332 if (gst_relay_info
) {
333 memset(&value
, 0, sizeof(GValue
));
334 g_value_init(&value
, GST_TYPE_STRUCTURE
);
335 gst_value_set_structure(&value
, gst_relay_info
);
337 g_value_array_append(relay_info
, &value
);
338 gst_structure_free(gst_relay_info
);
342 struct sipe_backend_media_relays
*
343 sipe_backend_media_relays_convert(GSList
*media_relays
, gchar
*username
, gchar
*password
)
345 GValueArray
*relay_info
= g_value_array_new(0);
347 for (; media_relays
; media_relays
= media_relays
->next
) {\
348 struct sipe_media_relay
*relay
= media_relays
->data
;
350 /* Skip relays where IP could not be resolved. */
351 if (!relay
->hostname
)
354 if (relay
->udp_port
!= 0)
355 append_relay(relay_info
, relay
->hostname
, relay
->udp_port
,
356 "udp", username
, password
);
359 if (relay
->tcp_port
!= 0)
360 append_relay(relay_info
, relay
->hostname
, relay
->tcp_port
,
361 "tcp", username
, password
);
365 return (struct sipe_backend_media_relays
*)relay_info
;
369 sipe_backend_media_relays_free(struct sipe_backend_media_relays
*media_relays
)
371 g_value_array_free((GValueArray
*)media_relays
);
375 stream_demultiplex_cb(const gchar
*buf
, SIPE_UNUSED_PARAMETER gpointer
*user_data
)
377 guint8 payload_type
= buf
[1] & 0x7F;
378 if (payload_type
>= 200 && payload_type
<=204) {
380 return PURPLE_MEDIA_COMPONENT_RTCP
;
383 return PURPLE_MEDIA_COMPONENT_RTP
;
387 struct sipe_backend_stream
*
388 sipe_backend_media_add_stream(struct sipe_backend_media
*media
,
390 const gchar
*participant
,
392 SipeIceVersion ice_version
,
394 struct sipe_backend_media_relays
*media_relays
)
396 struct sipe_backend_stream
*stream
= NULL
;
397 PurpleMediaSessionType prpl_type
= sipe_media_to_purple(type
);
398 GParameter
*params
= NULL
;
399 guint params_cnt
= 0;
402 if (ice_version
!= SIPE_ICE_NO_ICE
) {
403 transmitter
= "nice";
406 params
= g_new0(GParameter
, params_cnt
);
408 params
[0].name
= "compatibility-mode";
409 g_value_init(¶ms
[0].value
, G_TYPE_UINT
);
410 g_value_set_uint(¶ms
[0].value
,
411 ice_version
== SIPE_ICE_DRAFT_6
?
412 NICE_COMPATIBILITY_OC2007
:
413 NICE_COMPATIBILITY_OC2007R2
);
415 params
[1].name
= "transport-protocols";
416 g_value_init(¶ms
[1].value
, G_TYPE_UINT
);
418 g_value_set_uint(¶ms
[1].value
,
419 PURPLE_MEDIA_NETWORK_PROTOCOL_UDP
|
420 PURPLE_MEDIA_NETWORK_PROTOCOL_TCP_ACTIVE
|
421 PURPLE_MEDIA_NETWORK_PROTOCOL_TCP_PASSIVE
);
423 g_value_set_uint(¶ms
[1].value
,
424 PURPLE_MEDIA_NETWORK_PROTOCOL_UDP
);
427 params
[2].name
= "demultiplex-func";
428 g_value_init(¶ms
[2].value
, G_TYPE_POINTER
);
429 g_value_set_pointer(¶ms
[2].value
, stream_demultiplex_cb
);
432 params
[3].name
= "relay-info";
433 g_value_init(¶ms
[3].value
, G_TYPE_VALUE_ARRAY
);
434 g_value_set_boxed(¶ms
[3].value
, media_relays
);
438 // TODO: session naming here, Communicator needs audio/video
439 transmitter
= "rawudp";
440 //sessionid = "sipe-voice-rawudp";
443 ensure_codecs_conf();
445 if (purple_media_add_stream(media
->m
, id
, participant
, prpl_type
,
446 initiator
, transmitter
, params_cnt
,
448 stream
= g_new0(struct sipe_backend_stream
, 1);
449 stream
->sessionid
= g_strdup(id
);
450 stream
->participant
= g_strdup(participant
);
451 stream
->candidates_prepared
= FALSE
;
453 media
->streams
= g_slist_append(media
->streams
, stream
);
455 ++media
->unconfirmed_streams
;
458 if (params
&& media_relays
)
459 g_value_unset(¶ms
[3].value
);
467 sipe_backend_media_remove_stream(struct sipe_backend_media
*media
,
468 struct sipe_backend_stream
*stream
)
470 g_return_if_fail(media
&& stream
);
472 purple_media_end(media
->m
, stream
->sessionid
, NULL
);
473 media
->streams
= g_slist_remove(media
->streams
, stream
);
474 backend_stream_free(stream
);
477 GSList
*sipe_backend_media_get_streams(struct sipe_backend_media
*media
)
479 return media
->streams
;
482 struct sipe_backend_stream
*
483 sipe_backend_media_get_stream_by_id(struct sipe_backend_media
*media
,
487 for (i
= media
->streams
; i
; i
= i
->next
) {
488 struct sipe_backend_stream
*stream
= i
->data
;
489 if (sipe_strequal(stream
->sessionid
, id
))
496 sipe_backend_media_add_remote_candidates(struct sipe_backend_media
*media
,
497 struct sipe_backend_stream
*stream
,
500 GList
*udp_candidates
= NULL
;
504 PurpleMediaCandidate
*candidate
= candidates
->data
;
505 PurpleMediaNetworkProtocol proto
;
507 proto
= purple_media_candidate_get_protocol(candidate
);
508 if (proto
== PURPLE_MEDIA_NETWORK_PROTOCOL_UDP
)
509 udp_candidates
= g_list_append(udp_candidates
, candidate
);
511 candidates
= candidates
->next
;
514 candidates
= udp_candidates
;
518 purple_media_add_remote_candidates(media
->m
, stream
->sessionid
,
519 stream
->participant
, candidates
);
521 g_list_free(udp_candidates
);
524 gboolean
sipe_backend_media_is_initiator(struct sipe_backend_media
*media
,
525 struct sipe_backend_stream
*stream
)
527 return purple_media_is_initiator(media
->m
,
528 stream
? stream
->sessionid
: NULL
,
529 stream
? stream
->participant
: NULL
);
532 gboolean
sipe_backend_media_accepted(struct sipe_backend_media
*media
)
534 return purple_media_accepted(media
->m
, NULL
, NULL
);
538 sipe_backend_candidates_prepared(struct sipe_backend_media
*media
)
540 GSList
*streams
= media
->streams
;
541 for (; streams
; streams
= streams
->next
) {
542 struct sipe_backend_stream
*s
= streams
->data
;
543 if (!s
->candidates_prepared
)
550 sipe_backend_media_get_active_local_candidates(struct sipe_backend_media
*media
,
551 struct sipe_backend_stream
*stream
)
553 return purple_media_get_active_local_candidates(media
->m
,
555 stream
->participant
);
559 sipe_backend_media_get_active_remote_candidates(struct sipe_backend_media
*media
,
560 struct sipe_backend_stream
*stream
)
562 return purple_media_get_active_remote_candidates(media
->m
,
564 stream
->participant
);
568 sipe_backend_stream_get_id(struct sipe_backend_stream
*stream
)
570 return stream
->sessionid
;
573 void sipe_backend_stream_hold(struct sipe_backend_media
*media
,
574 struct sipe_backend_stream
*stream
,
577 purple_media_stream_info(media
->m
, PURPLE_MEDIA_INFO_HOLD
,
578 stream
->sessionid
, stream
->participant
,
582 void sipe_backend_stream_unhold(struct sipe_backend_media
*media
,
583 struct sipe_backend_stream
*stream
,
586 purple_media_stream_info(media
->m
, PURPLE_MEDIA_INFO_UNHOLD
,
587 stream
->sessionid
, stream
->participant
,
591 gboolean
sipe_backend_stream_is_held(struct sipe_backend_stream
*stream
)
593 g_return_val_if_fail(stream
, FALSE
);
595 return stream
->local_on_hold
|| stream
->remote_on_hold
;
598 struct sipe_backend_codec
*
599 sipe_backend_codec_new(int id
, const char *name
, SipeMediaType type
, guint clock_rate
)
601 return (struct sipe_backend_codec
*)purple_media_codec_new(id
, name
,
602 sipe_media_to_purple(type
),
607 sipe_backend_codec_free(struct sipe_backend_codec
*codec
)
610 g_object_unref(codec
);
614 sipe_backend_codec_get_id(struct sipe_backend_codec
*codec
)
616 return purple_media_codec_get_id((PurpleMediaCodec
*)codec
);
620 sipe_backend_codec_get_name(struct sipe_backend_codec
*codec
)
622 /* Not explicitly documented, but return value must be g_free()'d */
623 return purple_media_codec_get_encoding_name((PurpleMediaCodec
*)codec
);
627 sipe_backend_codec_get_clock_rate(struct sipe_backend_codec
*codec
)
629 return purple_media_codec_get_clock_rate((PurpleMediaCodec
*)codec
);
633 sipe_backend_codec_add_optional_parameter(struct sipe_backend_codec
*codec
,
634 const gchar
*name
, const gchar
*value
)
636 purple_media_codec_add_optional_parameter((PurpleMediaCodec
*)codec
, name
, value
);
640 sipe_backend_codec_get_optional_parameters(struct sipe_backend_codec
*codec
)
642 return purple_media_codec_get_optional_parameters((PurpleMediaCodec
*)codec
);
646 sipe_backend_set_remote_codecs(struct sipe_backend_media
*media
,
647 struct sipe_backend_stream
*stream
,
650 return purple_media_set_remote_codecs(media
->m
,
657 sipe_backend_get_local_codecs(struct sipe_backend_media
*media
,
658 struct sipe_backend_stream
*stream
)
660 GList
*codecs
= purple_media_get_codecs(media
->m
,
663 gboolean is_conference
= (g_strstr_len(stream
->participant
,
664 strlen(stream
->participant
),
665 "app:conf:audio-video:") != NULL
);
668 * Do not announce Theora. Its optional parameters are too long,
669 * Communicator rejects such SDP message and does not support the codec
672 * For some yet unknown reason, A/V conferencing server does not accept
673 * voice stream sent by SIPE when SIREN codec is in use. Nevertheless,
674 * we are able to decode incoming SIREN from server and with MSOC
675 * client, bidirectional call using the codec works. Until resolved,
676 * do not try to negotiate SIREN usage when conferencing. PCMA or PCMU
677 * seems to work properly in this scenario.
680 PurpleMediaCodec
*codec
= i
->data
;
681 gchar
*encoding_name
= purple_media_codec_get_encoding_name(codec
);
683 if (sipe_strequal(encoding_name
,"THEORA") ||
684 (is_conference
&& sipe_strequal(encoding_name
,"SIREN"))) {
686 g_object_unref(codec
);
688 codecs
= g_list_delete_link(codecs
, i
);
693 g_free(encoding_name
);
699 struct sipe_backend_candidate
*
700 sipe_backend_candidate_new(const gchar
*foundation
,
701 SipeComponentType component
,
702 SipeCandidateType type
, SipeNetworkProtocol proto
,
703 const gchar
*ip
, guint port
,
704 const gchar
*username
,
705 const gchar
*password
)
707 PurpleMediaCandidate
*c
= purple_media_candidate_new(
708 /* Libnice and Farsight rely on non-NULL foundation to
709 * distinguish between candidates of a component. When NULL
710 * foundation is passed (ie. ICE draft 6 does not use foudation),
711 * use username instead. If no foundation is provided, Farsight
712 * may signal an active candidate different from the one actually
713 * in use. See Farsight's agent_new_selected_pair() in
714 * fs-nice-stream-transmitter.h where first candidate in the
715 * remote list is always selected when no foundation. */
716 foundation
? foundation
: username
,
718 sipe_candidate_type_to_purple(type
),
719 sipe_network_protocol_to_purple(proto
),
722 g_object_set(c
, "username", username
, "password", password
, NULL
);
723 return (struct sipe_backend_candidate
*)c
;
727 sipe_backend_candidate_free(struct sipe_backend_candidate
*candidate
)
730 g_object_unref(candidate
);
734 sipe_backend_candidate_get_username(struct sipe_backend_candidate
*candidate
)
736 /* Not explicitly documented, but return value must be g_free()'d */
737 return purple_media_candidate_get_username((PurpleMediaCandidate
*)candidate
);
741 sipe_backend_candidate_get_password(struct sipe_backend_candidate
*candidate
)
743 /* Not explicitly documented, but return value must be g_free()'d */
744 return purple_media_candidate_get_password((PurpleMediaCandidate
*)candidate
);
748 sipe_backend_candidate_get_foundation(struct sipe_backend_candidate
*candidate
)
750 /* Not explicitly documented, but return value must be g_free()'d */
751 return purple_media_candidate_get_foundation((PurpleMediaCandidate
*)candidate
);
755 sipe_backend_candidate_get_ip(struct sipe_backend_candidate
*candidate
)
757 /* Not explicitly documented, but return value must be g_free()'d */
758 return purple_media_candidate_get_ip((PurpleMediaCandidate
*)candidate
);
762 sipe_backend_candidate_get_port(struct sipe_backend_candidate
*candidate
)
764 return purple_media_candidate_get_port((PurpleMediaCandidate
*)candidate
);
768 sipe_backend_candidate_get_base_ip(struct sipe_backend_candidate
*candidate
)
770 /* Not explicitly documented, but return value must be g_free()'d */
771 return purple_media_candidate_get_base_ip((PurpleMediaCandidate
*)candidate
);
775 sipe_backend_candidate_get_base_port(struct sipe_backend_candidate
*candidate
)
777 return purple_media_candidate_get_base_port((PurpleMediaCandidate
*)candidate
);
781 sipe_backend_candidate_get_priority(struct sipe_backend_candidate
*candidate
)
783 return purple_media_candidate_get_priority((PurpleMediaCandidate
*)candidate
);
787 sipe_backend_candidate_set_priority(struct sipe_backend_candidate
*candidate
, guint32 priority
)
789 g_object_set(candidate
, "priority", priority
, NULL
);
793 sipe_backend_candidate_get_component_type(struct sipe_backend_candidate
*candidate
)
795 return purple_media_candidate_get_component_id((PurpleMediaCandidate
*)candidate
);
799 sipe_backend_candidate_get_type(struct sipe_backend_candidate
*candidate
)
801 PurpleMediaCandidateType type
=
802 purple_media_candidate_get_candidate_type((PurpleMediaCandidate
*)candidate
);
803 return purple_candidate_type_to_sipe(type
);
807 sipe_backend_candidate_get_protocol(struct sipe_backend_candidate
*candidate
)
809 PurpleMediaNetworkProtocol proto
=
810 purple_media_candidate_get_protocol((PurpleMediaCandidate
*)candidate
);
811 return purple_network_protocol_to_sipe(proto
);
815 remove_lone_candidate_cb(SIPE_UNUSED_PARAMETER gpointer key
,
819 GList
*entry
= value
;
820 GList
**candidates
= user_data
;
822 g_object_unref(entry
->data
);
823 *candidates
= g_list_delete_link(*candidates
, entry
);
827 ensure_candidate_pairs(GList
*candidates
)
829 GHashTable
*lone_cand_links
;
832 lone_cand_links
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, NULL
);
834 for (i
= candidates
; i
; i
= i
->next
) {
835 PurpleMediaCandidate
*c
= i
->data
;
836 gchar
*foundation
= purple_media_candidate_get_foundation(c
);
838 if (g_hash_table_lookup(lone_cand_links
, foundation
)) {
839 g_hash_table_remove(lone_cand_links
, foundation
);
842 g_hash_table_insert(lone_cand_links
, foundation
, i
);
846 g_hash_table_foreach(lone_cand_links
, remove_lone_candidate_cb
, &candidates
);
847 g_hash_table_destroy(lone_cand_links
);
853 sipe_backend_get_local_candidates(struct sipe_backend_media
*media
,
854 struct sipe_backend_stream
*stream
)
856 GList
*candidates
= purple_media_get_local_candidates(media
->m
,
858 stream
->participant
);
860 * Sometimes purple will not return complete list of candidates, even
861 * after "candidates-prepared" signal is emitted. This is a feature of
862 * libnice, namely affecting candidates discovered via UPnP. Nice does
863 * not wait until discovery is finished and can signal end of candidate
864 * gathering before all responses from UPnP enabled gateways are received.
866 * Remove any incomplete RTP+RTCP candidate pairs from the list.
868 candidates
= ensure_candidate_pairs(candidates
);
873 sipe_backend_media_accept(struct sipe_backend_media
*media
, gboolean local
)
876 purple_media_stream_info(media
->m
, PURPLE_MEDIA_INFO_ACCEPT
,
881 sipe_backend_media_hangup(struct sipe_backend_media
*media
, gboolean local
)
884 purple_media_stream_info(media
->m
, PURPLE_MEDIA_INFO_HANGUP
,
889 sipe_backend_media_reject(struct sipe_backend_media
*media
, gboolean local
)
892 purple_media_stream_info(media
->m
, PURPLE_MEDIA_INFO_REJECT
,
896 static PurpleMediaSessionType
sipe_media_to_purple(SipeMediaType type
)
899 case SIPE_MEDIA_AUDIO
: return PURPLE_MEDIA_AUDIO
;
900 case SIPE_MEDIA_VIDEO
: return PURPLE_MEDIA_VIDEO
;
901 default: return PURPLE_MEDIA_NONE
;
905 /*SipeMediaType purple_media_to_sipe(PurpleMediaSessionType type)
908 case PURPLE_MEDIA_AUDIO: return SIPE_MEDIA_AUDIO;
909 case PURPLE_MEDIA_VIDEO: return SIPE_MEDIA_VIDEO;
910 default: return SIPE_MEDIA_AUDIO;
914 static PurpleMediaCandidateType
915 sipe_candidate_type_to_purple(SipeCandidateType type
)
918 case SIPE_CANDIDATE_TYPE_HOST
: return PURPLE_MEDIA_CANDIDATE_TYPE_HOST
;
919 case SIPE_CANDIDATE_TYPE_RELAY
: return PURPLE_MEDIA_CANDIDATE_TYPE_RELAY
;
920 case SIPE_CANDIDATE_TYPE_SRFLX
: return PURPLE_MEDIA_CANDIDATE_TYPE_SRFLX
;
921 case SIPE_CANDIDATE_TYPE_PRFLX
: return PURPLE_MEDIA_CANDIDATE_TYPE_PRFLX
;
922 default: return PURPLE_MEDIA_CANDIDATE_TYPE_HOST
;
926 static SipeCandidateType
927 purple_candidate_type_to_sipe(PurpleMediaCandidateType type
)
930 case PURPLE_MEDIA_CANDIDATE_TYPE_HOST
: return SIPE_CANDIDATE_TYPE_HOST
;
931 case PURPLE_MEDIA_CANDIDATE_TYPE_RELAY
: return SIPE_CANDIDATE_TYPE_RELAY
;
932 case PURPLE_MEDIA_CANDIDATE_TYPE_SRFLX
: return SIPE_CANDIDATE_TYPE_SRFLX
;
933 case PURPLE_MEDIA_CANDIDATE_TYPE_PRFLX
: return SIPE_CANDIDATE_TYPE_PRFLX
;
934 default: return SIPE_CANDIDATE_TYPE_HOST
;
938 static PurpleMediaNetworkProtocol
939 sipe_network_protocol_to_purple(SipeNetworkProtocol proto
)
943 case SIPE_NETWORK_PROTOCOL_TCP_ACTIVE
:
944 return PURPLE_MEDIA_NETWORK_PROTOCOL_TCP_ACTIVE
;
945 case SIPE_NETWORK_PROTOCOL_TCP_PASSIVE
:
946 return PURPLE_MEDIA_NETWORK_PROTOCOL_TCP_PASSIVE
;
948 case SIPE_NETWORK_PROTOCOL_TCP_ACTIVE
:
949 case SIPE_NETWORK_PROTOCOL_TCP_PASSIVE
:
950 return PURPLE_MEDIA_NETWORK_PROTOCOL_TCP
;
952 case SIPE_NETWORK_PROTOCOL_UDP
:
953 return PURPLE_MEDIA_NETWORK_PROTOCOL_UDP
;
955 return PURPLE_MEDIA_NETWORK_PROTOCOL_UDP
;
959 static SipeNetworkProtocol
960 purple_network_protocol_to_sipe(PurpleMediaNetworkProtocol proto
)
964 case PURPLE_MEDIA_NETWORK_PROTOCOL_TCP_ACTIVE
:
965 return SIPE_NETWORK_PROTOCOL_TCP_ACTIVE
;
966 case PURPLE_MEDIA_NETWORK_PROTOCOL_TCP_PASSIVE
:
967 return SIPE_NETWORK_PROTOCOL_TCP_PASSIVE
;
969 case PURPLE_MEDIA_NETWORK_PROTOCOL_TCP
:
970 return SIPE_NETWORK_PROTOCOL_TCP_ACTIVE
;
972 case PURPLE_MEDIA_NETWORK_PROTOCOL_UDP
:
973 return SIPE_NETWORK_PROTOCOL_UDP
;
975 return SIPE_NETWORK_PROTOCOL_UDP
;