6 * Copyright (C) 2011-2014 SIPE Project <http://sipe.sourceforge.net/>
7 * Copyright (C) 2010 Jakub Adam <jakub.adam@ktknet.cz>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include "sipe-common.h"
36 #include "sip-transport.h"
37 #include "sipe-backend.h"
39 #include "sipe-chat.h"
40 #include "sipe-core.h"
41 #include "sipe-core-private.h"
42 #include "sipe-dialog.h"
43 #include "sipe-media.h"
44 #include "sipe-ocs2007.h"
45 #include "sipe-session.h"
46 #include "sipe-utils.h"
48 #include "sipe-schedule.h"
51 struct sipe_media_call_private
{
52 struct sipe_media_call
public;
54 /* private part starts here */
55 struct sipe_core_private
*sipe_private
;
58 struct sipmsg
*invitation
;
59 SipeIceVersion ice_version
;
60 gboolean encryption_compatible
;
65 #define SIPE_MEDIA_CALL ((struct sipe_media_call *) call_private)
66 #define SIPE_MEDIA_CALL_PRIVATE ((struct sipe_media_call_private *) call)
68 static void sipe_media_codec_list_free(GList
*codecs
)
70 for (; codecs
; codecs
= g_list_delete_link(codecs
, codecs
))
71 sipe_backend_codec_free(codecs
->data
);
74 static void sipe_media_candidate_list_free(GList
*candidates
)
76 for (; candidates
; candidates
= g_list_delete_link(candidates
, candidates
))
77 sipe_backend_candidate_free(candidates
->data
);
81 sipe_media_call_free(struct sipe_media_call_private
*call_private
)
84 struct sip_session
*session
;
85 sipe_backend_media_free(call_private
->public.backend_private
);
87 session
= sipe_session_find_call(call_private
->sipe_private
,
90 sipe_session_remove(call_private
->sipe_private
, session
);
92 if (call_private
->invitation
)
93 sipmsg_free(call_private
->invitation
);
95 sdpmsg_free(call_private
->smsg
);
96 sipe_utils_slist_free_full(call_private
->failed_media
,
97 (GDestroyNotify
)sdpmedia_free
);
98 g_free(call_private
->with
);
104 candidate_sort_cb(struct sdpcandidate
*c1
, struct sdpcandidate
*c2
)
106 int cmp
= sipe_strcompare(c1
->foundation
, c2
->foundation
);
108 cmp
= sipe_strcompare(c1
->username
, c2
->username
);
110 cmp
= c1
->component
- c2
->component
;
117 backend_candidates_to_sdpcandidate(GList
*candidates
)
119 GSList
*result
= NULL
;
122 for (i
= candidates
; i
; i
= i
->next
) {
123 struct sipe_backend_candidate
*candidate
= i
->data
;
124 struct sdpcandidate
*c
;
126 gchar
*ip
= sipe_backend_candidate_get_ip(candidate
);
127 gchar
*base_ip
= sipe_backend_candidate_get_base_ip(candidate
);
128 if (is_empty(ip
) || strchr(ip
, ':') ||
129 (base_ip
&& strchr(base_ip
, ':'))) {
130 /* Ignore IPv6 candidates. */
136 c
= g_new(struct sdpcandidate
, 1);
137 c
->foundation
= sipe_backend_candidate_get_foundation(candidate
);
138 c
->component
= sipe_backend_candidate_get_component_type(candidate
);
139 c
->type
= sipe_backend_candidate_get_type(candidate
);
140 c
->protocol
= sipe_backend_candidate_get_protocol(candidate
);
142 c
->port
= sipe_backend_candidate_get_port(candidate
);
143 c
->base_ip
= base_ip
;
144 c
->base_port
= sipe_backend_candidate_get_base_port(candidate
);
145 c
->priority
= sipe_backend_candidate_get_priority(candidate
);
146 c
->username
= sipe_backend_candidate_get_username(candidate
);
147 c
->password
= sipe_backend_candidate_get_password(candidate
);
149 result
= g_slist_insert_sorted(result
, c
,
150 (GCompareFunc
)candidate_sort_cb
);
157 get_stream_ip_and_ports(GSList
*candidates
,
158 gchar
**ip
, guint
*rtp_port
, guint
*rtcp_port
,
159 SipeCandidateType type
)
165 for (; candidates
; candidates
= candidates
->next
) {
166 struct sdpcandidate
*candidate
= candidates
->data
;
168 if (type
== SIPE_CANDIDATE_TYPE_ANY
|| candidate
->type
== type
) {
170 *ip
= g_strdup(candidate
->ip
);
171 } else if (!sipe_strequal(*ip
, candidate
->ip
)) {
175 if (candidate
->component
== SIPE_COMPONENT_RTP
) {
176 *rtp_port
= candidate
->port
;
177 } else if (candidate
->component
== SIPE_COMPONENT_RTCP
)
178 *rtcp_port
= candidate
->port
;
181 if (*rtp_port
!= 0 && *rtcp_port
!= 0)
187 sdpcodec_compare(gconstpointer a
, gconstpointer b
)
189 return ((const struct sdpcodec
*)a
)->id
-
190 ((const struct sdpcodec
*)b
)->id
;
193 static struct sdpmedia
*
194 backend_stream_to_sdpmedia(struct sipe_backend_media
*backend_media
,
195 struct sipe_backend_stream
*backend_stream
)
197 struct sdpmedia
*media
= g_new0(struct sdpmedia
, 1);
198 GList
*codecs
= sipe_backend_get_local_codecs(backend_media
,
202 GSList
*attributes
= NULL
;
206 media
->name
= g_strdup(sipe_backend_stream_get_id(backend_stream
));
208 if (sipe_strequal(media
->name
, "audio"))
209 type
= SIPE_MEDIA_AUDIO
;
210 else if (sipe_strequal(media
->name
, "video"))
211 type
= SIPE_MEDIA_VIDEO
;
213 // TODO: incompatible media, should not happen here
216 sipe_media_codec_list_free(codecs
);
221 for (i
= codecs
; i
; i
= i
->next
) {
222 struct sipe_backend_codec
*codec
= i
->data
;
223 struct sdpcodec
*c
= g_new0(struct sdpcodec
, 1);
226 c
->id
= sipe_backend_codec_get_id(codec
);
227 c
->name
= sipe_backend_codec_get_name(codec
);
228 c
->clock_rate
= sipe_backend_codec_get_clock_rate(codec
);
231 params
= sipe_backend_codec_get_optional_parameters(codec
);
232 for (; params
; params
= params
->next
) {
233 struct sipnameval
*param
= params
->data
;
234 struct sipnameval
*copy
= g_new0(struct sipnameval
, 1);
236 copy
->name
= g_strdup(param
->name
);
237 copy
->value
= g_strdup(param
->value
);
239 c
->parameters
= g_slist_append(c
->parameters
, copy
);
242 /* Buggy(?) codecs may report non-unique id (a.k.a. payload
243 * type) that must not appear in SDP messages we send. Thus,
244 * let's ignore any codec having the same id as one we already
245 * have in the converted list. */
246 media
->codecs
= sipe_utils_slist_insert_unique_sorted(
247 media
->codecs
, c
, sdpcodec_compare
,
248 (GDestroyNotify
)sdpcodec_free
);
251 sipe_media_codec_list_free(codecs
);
253 // Process local candidates
254 // If we have established candidate pairs, send them in SDP response.
255 // Otherwise send all available local candidates.
256 candidates
= sipe_backend_media_get_active_local_candidates(backend_media
,
259 candidates
= sipe_backend_get_local_candidates(backend_media
,
262 media
->candidates
= backend_candidates_to_sdpcandidate(candidates
);
264 sipe_media_candidate_list_free(candidates
);
266 get_stream_ip_and_ports(media
->candidates
, &media
->ip
, &media
->port
,
267 &rtcp_port
, SIPE_CANDIDATE_TYPE_HOST
);
268 // No usable HOST candidates, use any candidate
269 if (media
->ip
== NULL
&& media
->candidates
) {
270 get_stream_ip_and_ports(media
->candidates
, &media
->ip
, &media
->port
,
271 &rtcp_port
, SIPE_CANDIDATE_TYPE_ANY
);
274 if (sipe_backend_stream_is_held(backend_stream
))
275 attributes
= sipe_utils_nameval_add(attributes
, "inactive", "");
278 gchar
*tmp
= g_strdup_printf("%u", rtcp_port
);
279 attributes
= sipe_utils_nameval_add(attributes
, "rtcp", tmp
);
283 attributes
= sipe_utils_nameval_add(attributes
, "encryption", "rejected");
285 media
->attributes
= attributes
;
287 // Process remote candidates
288 candidates
= sipe_backend_media_get_active_remote_candidates(backend_media
,
290 media
->remote_candidates
= backend_candidates_to_sdpcandidate(candidates
);
291 sipe_media_candidate_list_free(candidates
);
296 static struct sdpmsg
*
297 sipe_media_to_sdpmsg(struct sipe_media_call_private
*call_private
)
299 struct sipe_backend_media
*backend_media
= call_private
->public.backend_private
;
300 struct sdpmsg
*msg
= g_new0(struct sdpmsg
, 1);
301 GSList
*streams
= sipe_backend_media_get_streams(backend_media
);
303 for (; streams
; streams
= streams
->next
) {
304 struct sdpmedia
*media
= backend_stream_to_sdpmedia(backend_media
, streams
->data
);
306 msg
->media
= g_slist_append(msg
->media
, media
);
309 msg
->ip
= g_strdup(media
->ip
);
313 msg
->media
= g_slist_concat(msg
->media
, call_private
->failed_media
);
314 call_private
->failed_media
= NULL
;
316 msg
->ice_version
= call_private
->ice_version
;
322 sipe_invite_call(struct sipe_core_private
*sipe_private
, TransCallback tc
)
326 gchar
*p_preferred_identity
= NULL
;
328 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
329 struct sip_session
*session
;
330 struct sip_dialog
*dialog
;
332 gboolean add_2007_fallback
= FALSE
;
334 session
= sipe_session_find_call(sipe_private
, call_private
->with
);
335 dialog
= session
->dialogs
->data
;
336 add_2007_fallback
= dialog
->cseq
== 0 &&
337 call_private
->ice_version
== SIPE_ICE_RFC_5245
&&
338 !sipe_strequal(call_private
->with
, sipe_private
->test_call_bot_uri
);
340 contact
= get_contact(sipe_private
);
342 if (sipe_private
->uc_line_uri
) {
343 gchar
*self
= sip_uri_self(sipe_private
);
344 p_preferred_identity
= g_strdup_printf(
345 "P-Preferred-Identity: <%s>, <%s>\r\n",
346 self
, sipe_private
->uc_line_uri
);
350 hdr
= g_strdup_printf(
351 "ms-keep-alive: UAC;hop-hop=yes\r\n"
354 "Content-Type: %s\r\n",
356 p_preferred_identity
? p_preferred_identity
: "",
358 "multipart/alternative;boundary=\"----=_NextPart_000_001E_01CB4397.0B5EB570\""
359 : "application/sdp");
361 g_free(p_preferred_identity
);
363 msg
= sipe_media_to_sdpmsg(call_private
);
364 body
= sdpmsg_to_string(msg
);
366 if (add_2007_fallback
) {
368 tmp
= g_strdup_printf(
369 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
370 "Content-Type: application/sdp\r\n"
371 "Content-Transfer-Encoding: 7bit\r\n"
372 "Content-Disposition: session; handling=optional; ms-proxy-2007fallback\r\n"
374 "o=- 0 0 IN IP4 %s\r\n"
377 "m=audio 0 RTP/AVP\r\n"
379 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
380 "Content-Type: application/sdp\r\n"
381 "Content-Transfer-Encoding: 7bit\r\n"
382 "Content-Disposition: session; handling=optional\r\n"
386 "------=_NextPart_000_001E_01CB4397.0B5EB570--\r\n",
387 msg
->ip
, msg
->ip
, body
);
394 dialog
->outgoing_invite
= sip_transport_invite(sipe_private
,
404 static struct sip_dialog
*
405 sipe_media_dialog_init(struct sip_session
* session
, struct sipmsg
*msg
)
407 gchar
*newTag
= gentag();
408 const gchar
*oldHeader
;
410 struct sip_dialog
*dialog
;
412 oldHeader
= sipmsg_find_header(msg
, "To");
413 newHeader
= g_strdup_printf("%s;tag=%s", oldHeader
, newTag
);
414 sipmsg_remove_header_now(msg
, "To");
415 sipmsg_add_header_now(msg
, "To", newHeader
);
418 dialog
= sipe_dialog_add(session
);
419 dialog
->callid
= g_strdup(sipmsg_find_header(msg
, "Call-ID"));
420 dialog
->with
= parse_from(sipmsg_find_header(msg
, "From"));
421 sipe_dialog_parse(dialog
, msg
, FALSE
);
427 send_response_with_session_description(struct sipe_media_call_private
*call_private
, int code
, gchar
*text
)
429 struct sdpmsg
*msg
= sipe_media_to_sdpmsg(call_private
);
430 gchar
*body
= sdpmsg_to_string(msg
);
432 sipmsg_add_header(call_private
->invitation
, "Content-Type", "application/sdp");
433 sip_transport_response(call_private
->sipe_private
, call_private
->invitation
, code
, text
, body
);
438 encryption_levels_compatible(struct sdpmsg
*msg
)
442 for (i
= msg
->media
; i
; i
= i
->next
) {
443 const gchar
*enc_level
;
444 struct sdpmedia
*m
= i
->data
;
446 enc_level
= sipe_utils_nameval_find(m
->attributes
, "encryption");
448 // Decline call if peer requires encryption as we don't support it yet.
449 if (sipe_strequal(enc_level
, "required"))
457 process_invite_call_response(struct sipe_core_private
*sipe_private
,
459 struct transaction
*trans
);
462 update_remote_media(struct sipe_media_call_private
* call_private
,
463 struct sdpmedia
*media
)
465 struct sipe_backend_media
*backend_media
= SIPE_MEDIA_CALL
->backend_private
;
466 struct sipe_backend_stream
*backend_stream
;
467 GList
*backend_candidates
= NULL
;
468 GList
*backend_codecs
= NULL
;
470 gboolean result
= TRUE
;
472 backend_stream
= sipe_backend_media_get_stream_by_id(backend_media
,
474 if (media
->port
== 0) {
476 sipe_backend_media_remove_stream(backend_media
, backend_stream
);
483 for (i
= media
->codecs
; i
; i
= i
->next
) {
484 struct sdpcodec
*c
= i
->data
;
485 struct sipe_backend_codec
*codec
;
488 codec
= sipe_backend_codec_new(c
->id
,
493 for (j
= c
->parameters
; j
; j
= j
->next
) {
494 struct sipnameval
*attr
= j
->data
;
496 sipe_backend_codec_add_optional_parameter(codec
,
501 backend_codecs
= g_list_append(backend_codecs
, codec
);
504 result
= sipe_backend_set_remote_codecs(backend_media
,
507 sipe_media_codec_list_free(backend_codecs
);
509 if (result
== FALSE
) {
510 sipe_backend_media_remove_stream(backend_media
, backend_stream
);
514 for (i
= media
->candidates
; i
; i
= i
->next
) {
515 struct sdpcandidate
*c
= i
->data
;
516 struct sipe_backend_candidate
*candidate
;
517 candidate
= sipe_backend_candidate_new(c
->foundation
,
525 sipe_backend_candidate_set_priority(candidate
, c
->priority
);
527 backend_candidates
= g_list_append(backend_candidates
, candidate
);
530 sipe_backend_media_add_remote_candidates(backend_media
,
533 sipe_media_candidate_list_free(backend_candidates
);
535 if (sipe_utils_nameval_find(media
->attributes
, "inactive")) {
536 sipe_backend_stream_hold(backend_media
, backend_stream
, FALSE
);
537 } else if (sipe_backend_stream_is_held(backend_stream
)) {
538 sipe_backend_stream_unhold(backend_media
, backend_stream
, FALSE
);
545 apply_remote_message(struct sipe_media_call_private
* call_private
,
550 sipe_utils_slist_free_full(call_private
->failed_media
, (GDestroyNotify
)sdpmedia_free
);
551 call_private
->failed_media
= NULL
;
553 for (i
= msg
->media
; i
; i
= i
->next
) {
554 struct sdpmedia
*media
= i
->data
;
555 if (!update_remote_media(call_private
, media
)) {
557 call_private
->failed_media
=
558 g_slist_append(call_private
->failed_media
, media
);
562 /* We need to keep failed medias until response is sent, remove them
563 * from sdpmsg that is to be freed. */
564 for (i
= call_private
->failed_media
; i
; i
= i
->next
) {
565 msg
->media
= g_slist_remove(msg
->media
, i
->data
);
568 call_private
->encryption_compatible
= encryption_levels_compatible(msg
);
572 call_initialized(struct sipe_media_call
*call
)
575 sipe_backend_media_get_streams(call
->backend_private
);
577 for (; streams
; streams
= streams
->next
) {
578 if (!sipe_backend_stream_initialized(call
->backend_private
,
587 // Sends an invite response when the call is accepted and local candidates were
588 // prepared, otherwise does nothing. If error response is sent, call_private is
589 // disposed before function returns. Returns true when response was sent.
591 send_invite_response_if_ready(struct sipe_media_call_private
*call_private
)
593 struct sipe_backend_media
*backend_media
;
595 backend_media
= call_private
->public.backend_private
;
597 if (!sipe_backend_media_accepted(backend_media
) ||
598 !call_initialized(&call_private
->public))
601 if (!call_private
->encryption_compatible
) {
602 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
604 sipmsg_add_header(call_private
->invitation
, "Warning",
605 "308 lcs.microsoft.com \"Encryption Levels not compatible\"");
606 sip_transport_response(sipe_private
,
607 call_private
->invitation
,
608 488, "Encryption Levels not compatible",
610 sipe_backend_media_reject(backend_media
, FALSE
);
611 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
612 _("Unable to establish a call"),
613 _("Encryption settings of peer are incompatible with ours."));
615 send_response_with_session_description(call_private
, 200, "OK");
622 stream_initialized_cb(struct sipe_media_call
*call
,
623 struct sipe_backend_stream
*stream
)
625 if (call_initialized(call
)) {
626 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
627 struct sipe_backend_media
*backend_private
= call
->backend_private
;
629 if (sipe_backend_media_is_initiator(backend_private
, stream
)) {
630 sipe_invite_call(call_private
->sipe_private
,
631 process_invite_call_response
);
632 } else if (call_private
->smsg
) {
633 struct sdpmsg
*smsg
= call_private
->smsg
;
634 call_private
->smsg
= NULL
;
636 apply_remote_message(call_private
, smsg
);
637 send_invite_response_if_ready(call_private
);
643 static void phone_state_publish(struct sipe_core_private
*sipe_private
)
645 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
646 sipe_ocs2007_phone_state_publish(sipe_private
);
648 // TODO: OCS 2005 support. Is anyone still using it at all?
653 media_end_cb(struct sipe_media_call
*call
)
655 g_return_if_fail(call
);
657 SIPE_MEDIA_CALL_PRIVATE
->sipe_private
->media_call
= NULL
;
658 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
);
659 sipe_media_call_free(SIPE_MEDIA_CALL_PRIVATE
);
663 call_accept_cb(struct sipe_media_call
*call
, gboolean local
)
666 send_invite_response_if_ready(SIPE_MEDIA_CALL_PRIVATE
);
668 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
);
672 call_reject_cb(struct sipe_media_call
*call
, gboolean local
)
675 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
676 sip_transport_response(call_private
->sipe_private
,
677 call_private
->invitation
,
678 603, "Decline", NULL
);
683 sipe_media_send_ack(struct sipe_core_private
*sipe_private
, struct sipmsg
*msg
,
684 struct transaction
*trans
);
686 static void call_hold_cb(struct sipe_media_call
*call
,
688 SIPE_UNUSED_PARAMETER gboolean state
)
691 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
,
692 sipe_media_send_ack
);
695 static void call_hangup_cb(struct sipe_media_call
*call
, gboolean local
)
698 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
699 struct sip_session
*session
;
700 session
= sipe_session_find_call(call_private
->sipe_private
,
704 sipe_session_close(call_private
->sipe_private
, session
);
710 error_cb(struct sipe_media_call
*call
, gchar
*message
)
712 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
713 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
714 gboolean initiator
= sipe_backend_media_is_initiator(call
->backend_private
, NULL
);
715 gboolean accepted
= sipe_backend_media_accepted(call
->backend_private
);
717 gchar
*title
= g_strdup_printf("Call with %s failed", call_private
->with
);
718 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, title
, message
);
721 if (!initiator
&& !accepted
) {
722 sip_transport_response(sipe_private
,
723 call_private
->invitation
,
724 488, "Not Acceptable Here", NULL
);
727 sipe_backend_media_hangup(call
->backend_private
, initiator
|| accepted
);
730 static struct sipe_media_call_private
*
731 sipe_media_call_new(struct sipe_core_private
*sipe_private
,
732 const gchar
* with
, gboolean initiator
, SipeIceVersion ice_version
)
734 struct sipe_media_call_private
*call_private
= g_new0(struct sipe_media_call_private
, 1);
737 call_private
->sipe_private
= sipe_private
;
739 cname
= g_strdup(sipe_private
->contact
+ 1);
740 cname
[strlen(cname
) - 1] = '\0';
742 call_private
->public.backend_private
= sipe_backend_media_new(SIPE_CORE_PUBLIC
,
746 sipe_backend_media_set_cname(call_private
->public.backend_private
, cname
);
748 call_private
->ice_version
= ice_version
;
749 call_private
->encryption_compatible
= TRUE
;
751 call_private
->public.stream_initialized_cb
= stream_initialized_cb
;
752 call_private
->public.media_end_cb
= media_end_cb
;
753 call_private
->public.call_accept_cb
= call_accept_cb
;
754 call_private
->public.call_reject_cb
= call_reject_cb
;
755 call_private
->public.call_hold_cb
= call_hold_cb
;
756 call_private
->public.call_hangup_cb
= call_hangup_cb
;
757 call_private
->public.error_cb
= error_cb
;
764 void sipe_media_hangup(struct sipe_media_call_private
*call_private
)
767 sipe_backend_media_hangup(call_private
->public.backend_private
,
773 sipe_media_initiate_call(struct sipe_core_private
*sipe_private
,
774 const char *with
, SipeIceVersion ice_version
,
777 struct sipe_media_call_private
*call_private
;
778 struct sipe_backend_media
*backend_media
;
779 struct sipe_backend_media_relays
*backend_media_relays
;
780 struct sip_session
*session
;
781 struct sip_dialog
*dialog
;
783 if (sipe_private
->media_call
)
786 call_private
= sipe_media_call_new(sipe_private
, with
, TRUE
, ice_version
);
788 session
= sipe_session_add_call(sipe_private
, with
);
789 dialog
= sipe_dialog_add(session
);
790 dialog
->callid
= gencallid();
791 dialog
->with
= g_strdup(session
->with
);
792 dialog
->ourtag
= gentag();
794 call_private
->with
= g_strdup(session
->with
);
796 backend_media
= call_private
->public.backend_private
;
798 backend_media_relays
=
799 sipe_backend_media_relays_convert(sipe_private
->media_relays
,
800 sipe_private
->media_relay_username
,
801 sipe_private
->media_relay_password
);
803 if (!sipe_backend_media_add_stream(backend_media
,
804 "audio", with
, SIPE_MEDIA_AUDIO
,
805 call_private
->ice_version
, TRUE
,
806 backend_media_relays
)) {
807 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
809 _("Error creating audio stream"));
810 sipe_media_hangup(call_private
);
811 sipe_backend_media_relays_free(backend_media_relays
);
816 && !sipe_backend_media_add_stream(backend_media
,
817 "video", with
, SIPE_MEDIA_VIDEO
,
818 call_private
->ice_version
, TRUE
,
819 backend_media_relays
)) {
820 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
822 _("Error creating video stream"));
823 sipe_media_hangup(call_private
);
824 sipe_backend_media_relays_free(backend_media_relays
);
828 sipe_private
->media_call
= call_private
;
830 sipe_backend_media_relays_free(backend_media_relays
);
832 // Processing continues in stream_initialized_cb
836 sipe_core_media_initiate_call(struct sipe_core_public
*sipe_public
,
840 sipe_media_initiate_call(SIPE_CORE_PRIVATE
, with
,
841 SIPE_ICE_RFC_5245
, with_video
);
844 void sipe_core_media_connect_conference(struct sipe_core_public
*sipe_public
,
845 struct sipe_chat_session
*chat_session
)
847 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
848 struct sipe_backend_media_relays
*backend_media_relays
;
849 struct sip_session
*session
;
850 struct sip_dialog
*dialog
;
851 SipeIceVersion ice_version
;
855 session
= sipe_session_find_chat(sipe_private
, chat_session
);
857 if (sipe_private
->media_call
|| !session
)
860 session
->is_call
= TRUE
;
862 parts
= g_strsplit(chat_session
->id
, "app:conf:focus:", 2);
863 av_uri
= g_strjoinv("app:conf:audio-video:", parts
);
866 ice_version
= SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013
) ? SIPE_ICE_RFC_5245
:
869 sipe_private
->media_call
= sipe_media_call_new(sipe_private
, av_uri
,
872 session
= sipe_session_add_call(sipe_private
, av_uri
);
873 dialog
= sipe_dialog_add(session
);
874 dialog
->callid
= gencallid();
875 dialog
->with
= g_strdup(session
->with
);
876 dialog
->ourtag
= gentag();
880 sipe_private
->media_call
->with
= g_strdup(session
->with
);
882 backend_media_relays
=
883 sipe_backend_media_relays_convert(sipe_private
->media_relays
,
884 sipe_private
->media_relay_username
,
885 sipe_private
->media_relay_password
);
887 if (!sipe_backend_media_add_stream(sipe_private
->media_call
->public.backend_private
,
888 "audio", dialog
->with
,
890 sipe_private
->media_call
->ice_version
,
891 TRUE
, backend_media_relays
)) {
892 sipe_backend_notify_error(sipe_public
,
894 _("Error creating audio stream"));
895 sipe_media_hangup(sipe_private
->media_call
);
896 sipe_private
->media_call
= NULL
;
899 sipe_backend_media_relays_free(backend_media_relays
);
901 // Processing continues in stream_initialized_cb
904 gboolean
sipe_core_media_in_call(struct sipe_core_public
*sipe_public
)
907 return SIPE_CORE_PRIVATE
->media_call
!= NULL
;
912 static gboolean
phone_number_is_valid(const gchar
*phone_number
)
914 if (!phone_number
|| sipe_strequal(phone_number
, "")) {
918 if (*phone_number
== '+') {
922 while (*phone_number
!= '\0') {
923 if (!g_ascii_isdigit(*phone_number
)) {
932 void sipe_core_media_phone_call(struct sipe_core_public
*sipe_public
,
933 const gchar
*phone_number
)
935 g_return_if_fail(sipe_public
);
937 if (phone_number_is_valid(phone_number
)) {
938 gchar
*phone_uri
= g_strdup_printf("sip:%s@%s;user=phone",
939 phone_number
, sipe_public
->sip_domain
);
941 sipe_core_media_initiate_call(sipe_public
, phone_uri
, FALSE
);
945 sipe_backend_notify_error(sipe_public
,
946 _("Unable to establish a call"),
947 _("Invalid phone number"));
951 void sipe_core_media_test_call(struct sipe_core_public
*sipe_public
)
953 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
954 if (!sipe_private
->test_call_bot_uri
) {
955 sipe_backend_notify_error(sipe_public
,
956 _("Unable to establish a call"),
957 _("Audio Test Service is not available."));
961 sipe_core_media_initiate_call(sipe_public
,
962 sipe_private
->test_call_bot_uri
, FALSE
);
966 process_incoming_invite_call(struct sipe_core_private
*sipe_private
,
969 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
970 struct sipe_backend_media
*backend_media
;
971 struct sipe_backend_media_relays
*backend_media_relays
= NULL
;
973 gboolean has_new_media
= FALSE
;
979 if (!is_media_session_msg(call_private
, msg
)) {
980 sip_transport_response(sipe_private
, msg
, 486, "Busy Here", NULL
);
984 self
= sip_uri_self(sipe_private
);
985 if (sipe_strequal(call_private
->with
, self
)) {
987 sip_transport_response(sipe_private
, msg
, 488, "Not Acceptable Here", NULL
);
993 smsg
= sdpmsg_parse_msg(msg
->body
);
995 sip_transport_response(sipe_private
, msg
,
996 488, "Not Acceptable Here", NULL
);
997 sipe_media_hangup(call_private
);
1001 if (!call_private
) {
1002 gchar
*with
= parse_from(sipmsg_find_header(msg
, "From"));
1003 struct sip_session
*session
;
1005 call_private
= sipe_media_call_new(sipe_private
, with
, FALSE
, smsg
->ice_version
);
1006 session
= sipe_session_add_call(sipe_private
, with
);
1007 sipe_media_dialog_init(session
, msg
);
1009 call_private
->with
= g_strdup(session
->with
);
1010 sipe_private
->media_call
= call_private
;
1014 backend_media
= call_private
->public.backend_private
;
1016 if (call_private
->invitation
)
1017 sipmsg_free(call_private
->invitation
);
1018 call_private
->invitation
= sipmsg_copy(msg
);
1021 backend_media_relays
= sipe_backend_media_relays_convert(
1022 sipe_private
->media_relays
,
1023 sipe_private
->media_relay_username
,
1024 sipe_private
->media_relay_password
);
1026 // Create any new media streams
1027 for (i
= smsg
->media
; i
; i
= i
->next
) {
1028 struct sdpmedia
*media
= i
->data
;
1029 gchar
*id
= media
->name
;
1032 if ( media
->port
!= 0
1033 && !sipe_backend_media_get_stream_by_id(backend_media
, id
)) {
1036 if (sipe_strequal(id
, "audio"))
1037 type
= SIPE_MEDIA_AUDIO
;
1038 else if (sipe_strequal(id
, "video"))
1039 type
= SIPE_MEDIA_VIDEO
;
1043 with
= parse_from(sipmsg_find_header(msg
, "From"));
1044 sipe_backend_media_add_stream(backend_media
, id
, with
,
1048 backend_media_relays
);
1049 has_new_media
= TRUE
;
1054 sipe_backend_media_relays_free(backend_media_relays
);
1056 if (has_new_media
) {
1057 sdpmsg_free(call_private
->smsg
);
1058 call_private
->smsg
= smsg
;
1059 sip_transport_response(sipe_private
, call_private
->invitation
,
1060 180, "Ringing", NULL
);
1061 // Processing continues in stream_initialized_cb
1063 apply_remote_message(call_private
, smsg
);
1064 send_response_with_session_description(call_private
, 200, "OK");
1070 void process_incoming_cancel_call(struct sipe_core_private
*sipe_private
,
1073 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
1075 // We respond to the CANCEL request with 200 OK response and
1076 // with 487 Request Terminated to the remote INVITE in progress.
1077 sip_transport_response(sipe_private
, msg
, 200, "OK", NULL
);
1079 if (call_private
->invitation
) {
1080 sip_transport_response(sipe_private
, call_private
->invitation
,
1081 487, "Request Terminated", NULL
);
1084 sipe_media_hangup(call_private
);
1088 sipe_media_send_ack(struct sipe_core_private
*sipe_private
,
1090 struct transaction
*trans
)
1092 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
1093 struct sip_session
*session
;
1094 struct sip_dialog
*dialog
;
1097 if (!is_media_session_msg(call_private
, msg
))
1100 session
= sipe_session_find_call(sipe_private
, call_private
->with
);
1101 dialog
= session
->dialogs
->data
;
1105 tmp_cseq
= dialog
->cseq
;
1107 dialog
->cseq
= sip_transaction_cseq(trans
) - 1;
1108 sip_transport_ack(sipe_private
, dialog
);
1109 dialog
->cseq
= tmp_cseq
;
1111 dialog
->outgoing_invite
= NULL
;
1117 sipe_media_send_final_ack(struct sipe_core_private
*sipe_private
,
1119 struct transaction
*trans
)
1121 if (!sipe_media_send_ack(sipe_private
, msg
, trans
))
1124 sipe_backend_media_accept(sipe_private
->media_call
->public.backend_private
,
1131 reinvite_on_candidate_pair_cb(struct sipe_core_public
*sipe_public
)
1133 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1134 struct sipe_media_call_private
*media_call
= sipe_private
->media_call
;
1135 struct sipe_backend_media
*backend_media
;
1141 backend_media
= media_call
->public.backend_private
;
1142 streams
= sipe_backend_media_get_streams(backend_media
);
1144 for (; streams
; streams
= streams
->next
) {
1145 struct sipe_backend_stream
*s
= streams
->data
;
1146 GList
*remote_candidates
= sipe_backend_media_get_active_remote_candidates(backend_media
, s
);
1147 guint components
= g_list_length(remote_candidates
);
1149 sipe_media_candidate_list_free(remote_candidates
);
1151 // We must have candidates for both (RTP + RTCP) components ready
1152 if (components
< 2) {
1153 sipe_schedule_mseconds(sipe_private
,
1154 "<+media-reinvite-on-candidate-pair>",
1157 (sipe_schedule_action
) reinvite_on_candidate_pair_cb
,
1163 sipe_invite_call(sipe_private
, sipe_media_send_final_ack
);
1167 maybe_retry_call_with_ice_version(struct sipe_core_private
*sipe_private
,
1168 SipeIceVersion ice_version
,
1169 struct transaction
*trans
)
1171 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
1173 if (call_private
->ice_version
!= ice_version
&&
1174 sip_transaction_cseq(trans
) == 1) {
1175 gchar
*with
= g_strdup(call_private
->with
);
1176 struct sipe_backend_media
*backend_private
= call_private
->public.backend_private
;
1177 gboolean with_video
= sipe_backend_media_get_stream_by_id(backend_private
, "video") != NULL
;
1179 sipe_media_hangup(call_private
);
1180 SIPE_DEBUG_INFO("Retrying call with ICEv%d.",
1181 ice_version
== SIPE_ICE_DRAFT_6
? 6 : 19);
1182 sipe_media_initiate_call(sipe_private
, with
, ice_version
,
1193 process_invite_call_response(struct sipe_core_private
*sipe_private
,
1195 struct transaction
*trans
)
1198 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
1199 struct sip_session
*session
;
1200 struct sip_dialog
*dialog
;
1201 struct sdpmsg
*smsg
;
1203 if (!is_media_session_msg(call_private
, msg
))
1206 session
= sipe_session_find_call(sipe_private
, call_private
->with
);
1207 dialog
= session
->dialogs
->data
;
1209 with
= dialog
->with
;
1211 dialog
->outgoing_invite
= NULL
;
1213 if (msg
->response
>= 400) {
1214 // Call rejected by remote peer or an error occurred
1216 GString
*desc
= g_string_new("");
1217 gboolean append_responsestr
= FALSE
;
1219 switch (msg
->response
) {
1221 title
= _("User unavailable");
1223 if (sipmsg_parse_warning(msg
, NULL
) == 391) {
1224 g_string_append_printf(desc
, _("%s does not want to be disturbed"), with
);
1226 g_string_append_printf(desc
, _("User %s is not available"), with
);
1231 title
= _("Call rejected");
1232 g_string_append_printf(desc
, _("User %s rejected call"), with
);
1235 // OCS/Lync really sends response string with 'Mutipart' typo.
1236 if (sipe_strequal(msg
->responsestr
, "Mutipart mime in content type not supported by Archiving CDR service") &&
1237 maybe_retry_call_with_ice_version(sipe_private
, SIPE_ICE_DRAFT_6
, trans
)) {
1240 title
= _("Unsupported media type");
1243 /* Check for incompatible encryption levels error.
1246 * 488 Not Acceptable Here
1247 * ms-client-diagnostics: 52017;reason="Encryption levels dont match"
1249 * older clients (and SIPE itself):
1250 * 488 Encryption Levels not compatible
1252 const gchar
*ms_diag
= sipmsg_find_header(msg
, "ms-client-diagnostics");
1253 SipeIceVersion retry_ice_version
= SIPE_ICE_DRAFT_6
;
1255 if (sipe_strequal(msg
->responsestr
, "Encryption Levels not compatible") ||
1256 (ms_diag
&& g_str_has_prefix(ms_diag
, "52017;"))) {
1257 title
= _("Unable to establish a call");
1258 g_string_append(desc
, _("Encryption settings of peer are incompatible with ours."));
1262 /* Check if this is failed conference using
1263 * ICEv6 with reason "Error parsing SDP" and
1264 * retry using ICEv19. */
1265 ms_diag
= sipmsg_find_header(msg
, "ms-diagnostics");
1266 if (ms_diag
&& g_str_has_prefix(ms_diag
, "7008;")) {
1267 retry_ice_version
= SIPE_ICE_RFC_5245
;
1270 if (maybe_retry_call_with_ice_version(sipe_private
, retry_ice_version
, trans
)) {
1273 // Break intentionally omitted
1276 title
= _("Error occured");
1277 g_string_append(desc
, _("Unable to establish a call"));
1278 append_responsestr
= TRUE
;
1282 if (append_responsestr
) {
1283 gchar
*reason
= sipmsg_get_ms_diagnostics_reason(msg
);
1285 g_string_append_printf(desc
, "\n%d %s",
1286 msg
->response
, msg
->responsestr
);
1288 g_string_append_printf(desc
, "\n\n%s", reason
);
1293 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, title
, desc
->str
);
1294 g_string_free(desc
, TRUE
);
1296 sipe_media_send_ack(sipe_private
, msg
, trans
);
1297 sipe_media_hangup(call_private
);
1302 sipe_dialog_parse(dialog
, msg
, TRUE
);
1303 smsg
= sdpmsg_parse_msg(msg
->body
);
1305 sip_transport_response(sipe_private
, msg
,
1306 488, "Not Acceptable Here", NULL
);
1307 sipe_media_hangup(call_private
);
1311 apply_remote_message(call_private
, smsg
);
1314 sipe_media_send_ack(sipe_private
, msg
, trans
);
1315 reinvite_on_candidate_pair_cb(SIPE_CORE_PUBLIC
);
1320 gboolean
is_media_session_msg(struct sipe_media_call_private
*call_private
,
1324 const gchar
*callid
= sipmsg_find_header(msg
, "Call-ID");
1325 struct sip_session
*session
;
1327 session
= sipe_session_find_call(call_private
->sipe_private
,
1328 call_private
->with
);
1330 struct sip_dialog
*dialog
= session
->dialogs
->data
;
1331 return sipe_strequal(dialog
->callid
, callid
);
1337 void sipe_media_handle_going_offline(struct sipe_media_call_private
*call_private
)
1339 struct sipe_backend_media
*backend_private
;
1341 backend_private
= call_private
->public.backend_private
;
1343 if ( !sipe_backend_media_is_initiator(backend_private
, NULL
)
1344 && !sipe_backend_media_accepted(backend_private
)) {
1345 sip_transport_response(call_private
->sipe_private
,
1346 call_private
->invitation
,
1347 480, "Temporarily Unavailable", NULL
);
1349 struct sip_session
*session
;
1351 session
= sipe_session_find_call(call_private
->sipe_private
,
1352 call_private
->with
);
1354 sipe_session_close(call_private
->sipe_private
, session
);
1357 sipe_media_hangup(call_private
);
1360 gboolean
sipe_media_is_conference_call(struct sipe_media_call_private
*call_private
)
1362 return g_strstr_len(call_private
->with
, -1, "app:conf:audio-video:") != NULL
;
1366 sipe_media_relay_free(struct sipe_media_relay
*relay
)
1368 g_free(relay
->hostname
);
1369 if (relay
->dns_query
)
1370 sipe_backend_dns_query_cancel(relay
->dns_query
);
1375 sipe_media_relay_list_free(GSList
*list
)
1377 for (; list
; list
= g_slist_delete_link(list
, list
))
1378 sipe_media_relay_free(list
->data
);
1382 relay_ip_resolved_cb(struct sipe_media_relay
* relay
,
1383 const gchar
*ip
, SIPE_UNUSED_PARAMETER guint port
)
1385 gchar
*hostname
= relay
->hostname
;
1386 relay
->dns_query
= NULL
;
1389 relay
->hostname
= g_strdup(ip
);
1390 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname
, ip
);
1392 relay
->hostname
= NULL
;
1393 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname
);
1400 process_get_av_edge_credentials_response(struct sipe_core_private
*sipe_private
,
1402 SIPE_UNUSED_PARAMETER
struct transaction
*trans
)
1404 g_free(sipe_private
->media_relay_username
);
1405 g_free(sipe_private
->media_relay_password
);
1406 sipe_media_relay_list_free(sipe_private
->media_relays
);
1407 sipe_private
->media_relay_username
= NULL
;
1408 sipe_private
->media_relay_password
= NULL
;
1409 sipe_private
->media_relays
= NULL
;
1411 if (msg
->response
>= 400) {
1412 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
1413 "Failed to obtain A/V Edge credentials.");
1417 if (msg
->response
== 200) {
1418 sipe_xml
*xn_response
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1420 if (sipe_strequal("OK", sipe_xml_attribute(xn_response
, "reasonPhrase"))) {
1421 const sipe_xml
*xn_credentials
= sipe_xml_child(xn_response
, "credentialsResponse/credentials");
1422 const sipe_xml
*xn_relays
= sipe_xml_child(xn_response
, "credentialsResponse/mediaRelayList");
1423 const sipe_xml
*item
;
1424 GSList
*relays
= NULL
;
1426 item
= sipe_xml_child(xn_credentials
, "username");
1427 sipe_private
->media_relay_username
= sipe_xml_data(item
);
1428 item
= sipe_xml_child(xn_credentials
, "password");
1429 sipe_private
->media_relay_password
= sipe_xml_data(item
);
1431 for (item
= sipe_xml_child(xn_relays
, "mediaRelay"); item
; item
= sipe_xml_twin(item
)) {
1432 struct sipe_media_relay
*relay
= g_new0(struct sipe_media_relay
, 1);
1433 const sipe_xml
*node
;
1436 node
= sipe_xml_child(item
, "hostName");
1437 relay
->hostname
= sipe_xml_data(node
);
1439 node
= sipe_xml_child(item
, "udpPort");
1441 relay
->udp_port
= atoi(tmp
= sipe_xml_data(node
));
1445 node
= sipe_xml_child(item
, "tcpPort");
1447 relay
->tcp_port
= atoi(tmp
= sipe_xml_data(node
));
1451 relays
= g_slist_append(relays
, relay
);
1453 relay
->dns_query
= sipe_backend_dns_query_a(
1457 (sipe_dns_resolved_cb
) relay_ip_resolved_cb
,
1460 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
1462 relay
->tcp_port
, relay
->udp_port
);
1465 sipe_private
->media_relays
= relays
;
1468 sipe_xml_free(xn_response
);
1475 sipe_media_get_av_edge_credentials(struct sipe_core_private
*sipe_private
)
1477 // TODO: re-request credentials after duration expires?
1478 const char CRED_REQUEST_XML
[] =
1479 "<request requestID=\"%d\" "
1483 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
1484 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
1485 "<credentialsRequest credentialsRequestID=\"%d\">"
1486 "<identity>%s</identity>"
1487 "<location>%s</location>"
1488 "<duration>480</duration>"
1489 "</credentialsRequest>"
1492 int request_id
= rand();
1496 if (!sipe_private
->mras_uri
)
1499 self
= sip_uri_self(sipe_private
);
1501 body
= g_strdup_printf(
1505 sipe_private
->mras_uri
,
1508 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER
) ? "internet" : "intranet");
1511 sip_transport_service(sipe_private
,
1512 sipe_private
->mras_uri
,
1513 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
1515 process_get_av_edge_credentials_response
);