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 is_empty(candidate
->ip
) ||
171 /* reject IPv6 addresses */
172 strchr(candidate
->ip
, ':'))) {
173 *ip
= g_strdup(candidate
->ip
);
174 } else if (!(*ip
&& sipe_strequal(*ip
, candidate
->ip
))) {
178 if (candidate
->component
== SIPE_COMPONENT_RTP
) {
179 *rtp_port
= candidate
->port
;
180 } else if (candidate
->component
== SIPE_COMPONENT_RTCP
)
181 *rtcp_port
= candidate
->port
;
184 if (*rtp_port
!= 0 && *rtcp_port
!= 0)
190 sdpcodec_compare(gconstpointer a
, gconstpointer b
)
192 return ((const struct sdpcodec
*)a
)->id
-
193 ((const struct sdpcodec
*)b
)->id
;
196 static struct sdpmedia
*
197 backend_stream_to_sdpmedia(struct sipe_backend_media
*backend_media
,
198 struct sipe_backend_stream
*backend_stream
)
200 struct sdpmedia
*media
= g_new0(struct sdpmedia
, 1);
201 GList
*codecs
= sipe_backend_get_local_codecs(backend_media
,
205 GSList
*attributes
= NULL
;
209 media
->name
= g_strdup(sipe_backend_stream_get_id(backend_stream
));
211 if (sipe_strequal(media
->name
, "audio"))
212 type
= SIPE_MEDIA_AUDIO
;
213 else if (sipe_strequal(media
->name
, "video"))
214 type
= SIPE_MEDIA_VIDEO
;
216 // TODO: incompatible media, should not happen here
219 sipe_media_codec_list_free(codecs
);
224 for (i
= codecs
; i
; i
= i
->next
) {
225 struct sipe_backend_codec
*codec
= i
->data
;
226 struct sdpcodec
*c
= g_new0(struct sdpcodec
, 1);
229 c
->id
= sipe_backend_codec_get_id(codec
);
230 c
->name
= sipe_backend_codec_get_name(codec
);
231 c
->clock_rate
= sipe_backend_codec_get_clock_rate(codec
);
234 params
= sipe_backend_codec_get_optional_parameters(codec
);
235 for (; params
; params
= params
->next
) {
236 struct sipnameval
*param
= params
->data
;
237 struct sipnameval
*copy
= g_new0(struct sipnameval
, 1);
239 copy
->name
= g_strdup(param
->name
);
240 copy
->value
= g_strdup(param
->value
);
242 c
->parameters
= g_slist_append(c
->parameters
, copy
);
245 /* Buggy(?) codecs may report non-unique id (a.k.a. payload
246 * type) that must not appear in SDP messages we send. Thus,
247 * let's ignore any codec having the same id as one we already
248 * have in the converted list. */
249 media
->codecs
= sipe_utils_slist_insert_unique_sorted(
250 media
->codecs
, c
, sdpcodec_compare
,
251 (GDestroyNotify
)sdpcodec_free
);
254 sipe_media_codec_list_free(codecs
);
256 // Process local candidates
257 // If we have established candidate pairs, send them in SDP response.
258 // Otherwise send all available local candidates.
259 candidates
= sipe_backend_media_get_active_local_candidates(backend_media
,
262 candidates
= sipe_backend_get_local_candidates(backend_media
,
265 media
->candidates
= backend_candidates_to_sdpcandidate(candidates
);
267 sipe_media_candidate_list_free(candidates
);
269 get_stream_ip_and_ports(media
->candidates
, &media
->ip
, &media
->port
,
270 &rtcp_port
, SIPE_CANDIDATE_TYPE_HOST
);
271 // No usable HOST candidates, use any candidate
272 if (media
->ip
== NULL
&& media
->candidates
) {
273 get_stream_ip_and_ports(media
->candidates
, &media
->ip
, &media
->port
,
274 &rtcp_port
, SIPE_CANDIDATE_TYPE_ANY
);
277 if (sipe_backend_stream_is_held(backend_stream
))
278 attributes
= sipe_utils_nameval_add(attributes
, "inactive", "");
281 gchar
*tmp
= g_strdup_printf("%u", rtcp_port
);
282 attributes
= sipe_utils_nameval_add(attributes
, "rtcp", tmp
);
286 attributes
= sipe_utils_nameval_add(attributes
, "encryption", "rejected");
288 media
->attributes
= attributes
;
290 // Process remote candidates
291 candidates
= sipe_backend_media_get_active_remote_candidates(backend_media
,
293 media
->remote_candidates
= backend_candidates_to_sdpcandidate(candidates
);
294 sipe_media_candidate_list_free(candidates
);
299 static struct sdpmsg
*
300 sipe_media_to_sdpmsg(struct sipe_media_call_private
*call_private
)
302 struct sipe_backend_media
*backend_media
= call_private
->public.backend_private
;
303 struct sdpmsg
*msg
= g_new0(struct sdpmsg
, 1);
304 GSList
*streams
= sipe_backend_media_get_streams(backend_media
);
306 for (; streams
; streams
= streams
->next
) {
307 struct sdpmedia
*media
= backend_stream_to_sdpmedia(backend_media
, streams
->data
);
309 msg
->media
= g_slist_append(msg
->media
, media
);
312 msg
->ip
= g_strdup(media
->ip
);
316 msg
->media
= g_slist_concat(msg
->media
, call_private
->failed_media
);
317 call_private
->failed_media
= NULL
;
319 msg
->ice_version
= call_private
->ice_version
;
325 sipe_invite_call(struct sipe_core_private
*sipe_private
, TransCallback tc
)
329 gchar
*p_preferred_identity
= NULL
;
331 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
332 struct sip_session
*session
;
333 struct sip_dialog
*dialog
;
335 gboolean add_2007_fallback
= FALSE
;
337 session
= sipe_session_find_call(sipe_private
, call_private
->with
);
338 dialog
= session
->dialogs
->data
;
339 add_2007_fallback
= dialog
->cseq
== 0 &&
340 call_private
->ice_version
== SIPE_ICE_RFC_5245
&&
341 !sipe_strequal(call_private
->with
, sipe_private
->test_call_bot_uri
);
343 contact
= get_contact(sipe_private
);
345 if (sipe_private
->uc_line_uri
) {
346 gchar
*self
= sip_uri_self(sipe_private
);
347 p_preferred_identity
= g_strdup_printf(
348 "P-Preferred-Identity: <%s>, <%s>\r\n",
349 self
, sipe_private
->uc_line_uri
);
353 hdr
= g_strdup_printf(
354 "ms-keep-alive: UAC;hop-hop=yes\r\n"
357 "Content-Type: %s\r\n",
359 p_preferred_identity
? p_preferred_identity
: "",
361 "multipart/alternative;boundary=\"----=_NextPart_000_001E_01CB4397.0B5EB570\""
362 : "application/sdp");
364 g_free(p_preferred_identity
);
366 msg
= sipe_media_to_sdpmsg(call_private
);
367 body
= sdpmsg_to_string(msg
);
369 if (add_2007_fallback
) {
371 tmp
= g_strdup_printf(
372 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
373 "Content-Type: application/sdp\r\n"
374 "Content-Transfer-Encoding: 7bit\r\n"
375 "Content-Disposition: session; handling=optional; ms-proxy-2007fallback\r\n"
377 "o=- 0 0 IN IP4 %s\r\n"
380 "m=audio 0 RTP/AVP\r\n"
382 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
383 "Content-Type: application/sdp\r\n"
384 "Content-Transfer-Encoding: 7bit\r\n"
385 "Content-Disposition: session; handling=optional\r\n"
389 "------=_NextPart_000_001E_01CB4397.0B5EB570--\r\n",
390 msg
->ip
, msg
->ip
, body
);
397 dialog
->outgoing_invite
= sip_transport_invite(sipe_private
,
407 static struct sip_dialog
*
408 sipe_media_dialog_init(struct sip_session
* session
, struct sipmsg
*msg
)
410 gchar
*newTag
= gentag();
411 const gchar
*oldHeader
;
413 struct sip_dialog
*dialog
;
415 oldHeader
= sipmsg_find_header(msg
, "To");
416 newHeader
= g_strdup_printf("%s;tag=%s", oldHeader
, newTag
);
417 sipmsg_remove_header_now(msg
, "To");
418 sipmsg_add_header_now(msg
, "To", newHeader
);
421 dialog
= sipe_dialog_add(session
);
422 dialog
->callid
= g_strdup(sipmsg_find_header(msg
, "Call-ID"));
423 dialog
->with
= parse_from(sipmsg_find_header(msg
, "From"));
424 sipe_dialog_parse(dialog
, msg
, FALSE
);
430 send_response_with_session_description(struct sipe_media_call_private
*call_private
, int code
, gchar
*text
)
432 struct sdpmsg
*msg
= sipe_media_to_sdpmsg(call_private
);
433 gchar
*body
= sdpmsg_to_string(msg
);
435 sipmsg_add_header(call_private
->invitation
, "Content-Type", "application/sdp");
436 sip_transport_response(call_private
->sipe_private
, call_private
->invitation
, code
, text
, body
);
441 encryption_levels_compatible(struct sdpmsg
*msg
)
445 for (i
= msg
->media
; i
; i
= i
->next
) {
446 const gchar
*enc_level
;
447 struct sdpmedia
*m
= i
->data
;
449 enc_level
= sipe_utils_nameval_find(m
->attributes
, "encryption");
451 // Decline call if peer requires encryption as we don't support it yet.
452 if (sipe_strequal(enc_level
, "required"))
460 process_invite_call_response(struct sipe_core_private
*sipe_private
,
462 struct transaction
*trans
);
465 update_remote_media(struct sipe_media_call_private
* call_private
,
466 struct sdpmedia
*media
)
468 struct sipe_backend_media
*backend_media
= SIPE_MEDIA_CALL
->backend_private
;
469 struct sipe_backend_stream
*backend_stream
;
470 GList
*backend_candidates
= NULL
;
471 GList
*backend_codecs
= NULL
;
473 gboolean result
= TRUE
;
475 backend_stream
= sipe_backend_media_get_stream_by_id(backend_media
,
477 if (media
->port
== 0) {
479 sipe_backend_media_remove_stream(backend_media
, backend_stream
);
486 for (i
= media
->codecs
; i
; i
= i
->next
) {
487 struct sdpcodec
*c
= i
->data
;
488 struct sipe_backend_codec
*codec
;
491 codec
= sipe_backend_codec_new(c
->id
,
496 for (j
= c
->parameters
; j
; j
= j
->next
) {
497 struct sipnameval
*attr
= j
->data
;
499 sipe_backend_codec_add_optional_parameter(codec
,
504 backend_codecs
= g_list_append(backend_codecs
, codec
);
507 result
= sipe_backend_set_remote_codecs(backend_media
,
510 sipe_media_codec_list_free(backend_codecs
);
512 if (result
== FALSE
) {
513 sipe_backend_media_remove_stream(backend_media
, backend_stream
);
517 for (i
= media
->candidates
; i
; i
= i
->next
) {
518 struct sdpcandidate
*c
= i
->data
;
519 struct sipe_backend_candidate
*candidate
;
520 candidate
= sipe_backend_candidate_new(c
->foundation
,
528 sipe_backend_candidate_set_priority(candidate
, c
->priority
);
530 backend_candidates
= g_list_append(backend_candidates
, candidate
);
533 sipe_backend_media_add_remote_candidates(backend_media
,
536 sipe_media_candidate_list_free(backend_candidates
);
538 if (sipe_utils_nameval_find(media
->attributes
, "inactive")) {
539 sipe_backend_stream_hold(backend_media
, backend_stream
, FALSE
);
540 } else if (sipe_backend_stream_is_held(backend_stream
)) {
541 sipe_backend_stream_unhold(backend_media
, backend_stream
, FALSE
);
548 apply_remote_message(struct sipe_media_call_private
* call_private
,
553 sipe_utils_slist_free_full(call_private
->failed_media
, (GDestroyNotify
)sdpmedia_free
);
554 call_private
->failed_media
= NULL
;
556 for (i
= msg
->media
; i
; i
= i
->next
) {
557 struct sdpmedia
*media
= i
->data
;
558 if (!update_remote_media(call_private
, media
)) {
560 call_private
->failed_media
=
561 g_slist_append(call_private
->failed_media
, media
);
565 /* We need to keep failed medias until response is sent, remove them
566 * from sdpmsg that is to be freed. */
567 for (i
= call_private
->failed_media
; i
; i
= i
->next
) {
568 msg
->media
= g_slist_remove(msg
->media
, i
->data
);
571 call_private
->encryption_compatible
= encryption_levels_compatible(msg
);
575 call_initialized(struct sipe_media_call
*call
)
578 sipe_backend_media_get_streams(call
->backend_private
);
580 for (; streams
; streams
= streams
->next
) {
581 if (!sipe_backend_stream_initialized(call
->backend_private
,
590 // Sends an invite response when the call is accepted and local candidates were
591 // prepared, otherwise does nothing. If error response is sent, call_private is
592 // disposed before function returns. Returns true when response was sent.
594 send_invite_response_if_ready(struct sipe_media_call_private
*call_private
)
596 struct sipe_backend_media
*backend_media
;
598 backend_media
= call_private
->public.backend_private
;
600 if (!sipe_backend_media_accepted(backend_media
) ||
601 !call_initialized(&call_private
->public))
604 if (!call_private
->encryption_compatible
) {
605 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
607 sipmsg_add_header(call_private
->invitation
, "Warning",
608 "308 lcs.microsoft.com \"Encryption Levels not compatible\"");
609 sip_transport_response(sipe_private
,
610 call_private
->invitation
,
611 488, "Encryption Levels not compatible",
613 sipe_backend_media_reject(backend_media
, FALSE
);
614 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
615 _("Unable to establish a call"),
616 _("Encryption settings of peer are incompatible with ours."));
618 send_response_with_session_description(call_private
, 200, "OK");
625 stream_initialized_cb(struct sipe_media_call
*call
,
626 struct sipe_backend_stream
*stream
)
628 if (call_initialized(call
)) {
629 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
630 struct sipe_backend_media
*backend_private
= call
->backend_private
;
632 if (sipe_backend_media_is_initiator(backend_private
, stream
)) {
633 sipe_invite_call(call_private
->sipe_private
,
634 process_invite_call_response
);
635 } else if (call_private
->smsg
) {
636 struct sdpmsg
*smsg
= call_private
->smsg
;
637 call_private
->smsg
= NULL
;
639 apply_remote_message(call_private
, smsg
);
640 send_invite_response_if_ready(call_private
);
646 static void phone_state_publish(struct sipe_core_private
*sipe_private
)
648 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
649 sipe_ocs2007_phone_state_publish(sipe_private
);
651 // TODO: OCS 2005 support. Is anyone still using it at all?
656 media_end_cb(struct sipe_media_call
*call
)
658 g_return_if_fail(call
);
660 SIPE_MEDIA_CALL_PRIVATE
->sipe_private
->media_call
= NULL
;
661 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
);
662 sipe_media_call_free(SIPE_MEDIA_CALL_PRIVATE
);
666 call_accept_cb(struct sipe_media_call
*call
, gboolean local
)
669 send_invite_response_if_ready(SIPE_MEDIA_CALL_PRIVATE
);
671 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
);
675 call_reject_cb(struct sipe_media_call
*call
, gboolean local
)
678 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
679 sip_transport_response(call_private
->sipe_private
,
680 call_private
->invitation
,
681 603, "Decline", NULL
);
686 sipe_media_send_ack(struct sipe_core_private
*sipe_private
, struct sipmsg
*msg
,
687 struct transaction
*trans
);
689 static void call_hold_cb(struct sipe_media_call
*call
,
691 SIPE_UNUSED_PARAMETER gboolean state
)
694 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
,
695 sipe_media_send_ack
);
698 static void call_hangup_cb(struct sipe_media_call
*call
, gboolean local
)
701 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
702 struct sip_session
*session
;
703 session
= sipe_session_find_call(call_private
->sipe_private
,
707 sipe_session_close(call_private
->sipe_private
, session
);
713 error_cb(struct sipe_media_call
*call
, gchar
*message
)
715 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
716 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
717 gboolean initiator
= sipe_backend_media_is_initiator(call
->backend_private
, NULL
);
718 gboolean accepted
= sipe_backend_media_accepted(call
->backend_private
);
720 gchar
*title
= g_strdup_printf("Call with %s failed", call_private
->with
);
721 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, title
, message
);
724 if (!initiator
&& !accepted
) {
725 sip_transport_response(sipe_private
,
726 call_private
->invitation
,
727 488, "Not Acceptable Here", NULL
);
730 sipe_backend_media_hangup(call
->backend_private
, initiator
|| accepted
);
733 static struct sipe_media_call_private
*
734 sipe_media_call_new(struct sipe_core_private
*sipe_private
,
735 const gchar
* with
, gboolean initiator
, SipeIceVersion ice_version
)
737 struct sipe_media_call_private
*call_private
= g_new0(struct sipe_media_call_private
, 1);
740 call_private
->sipe_private
= sipe_private
;
742 cname
= g_strdup(sipe_private
->contact
+ 1);
743 cname
[strlen(cname
) - 1] = '\0';
745 call_private
->public.backend_private
= sipe_backend_media_new(SIPE_CORE_PUBLIC
,
749 sipe_backend_media_set_cname(call_private
->public.backend_private
, cname
);
751 call_private
->ice_version
= ice_version
;
752 call_private
->encryption_compatible
= TRUE
;
754 call_private
->public.stream_initialized_cb
= stream_initialized_cb
;
755 call_private
->public.media_end_cb
= media_end_cb
;
756 call_private
->public.call_accept_cb
= call_accept_cb
;
757 call_private
->public.call_reject_cb
= call_reject_cb
;
758 call_private
->public.call_hold_cb
= call_hold_cb
;
759 call_private
->public.call_hangup_cb
= call_hangup_cb
;
760 call_private
->public.error_cb
= error_cb
;
767 void sipe_media_hangup(struct sipe_media_call_private
*call_private
)
770 sipe_backend_media_hangup(call_private
->public.backend_private
,
776 sipe_media_initiate_call(struct sipe_core_private
*sipe_private
,
777 const char *with
, SipeIceVersion ice_version
,
780 struct sipe_media_call_private
*call_private
;
781 struct sipe_backend_media
*backend_media
;
782 struct sipe_backend_media_relays
*backend_media_relays
;
783 struct sip_session
*session
;
784 struct sip_dialog
*dialog
;
786 if (sipe_private
->media_call
)
789 call_private
= sipe_media_call_new(sipe_private
, with
, TRUE
, ice_version
);
791 session
= sipe_session_add_call(sipe_private
, with
);
792 dialog
= sipe_dialog_add(session
);
793 dialog
->callid
= gencallid();
794 dialog
->with
= g_strdup(session
->with
);
795 dialog
->ourtag
= gentag();
797 call_private
->with
= g_strdup(session
->with
);
799 backend_media
= call_private
->public.backend_private
;
801 backend_media_relays
=
802 sipe_backend_media_relays_convert(sipe_private
->media_relays
,
803 sipe_private
->media_relay_username
,
804 sipe_private
->media_relay_password
);
806 if (!sipe_backend_media_add_stream(backend_media
,
807 "audio", with
, SIPE_MEDIA_AUDIO
,
808 call_private
->ice_version
, TRUE
,
809 backend_media_relays
)) {
810 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
812 _("Error creating audio stream"));
813 sipe_media_hangup(call_private
);
814 sipe_backend_media_relays_free(backend_media_relays
);
819 && !sipe_backend_media_add_stream(backend_media
,
820 "video", with
, SIPE_MEDIA_VIDEO
,
821 call_private
->ice_version
, TRUE
,
822 backend_media_relays
)) {
823 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
825 _("Error creating video stream"));
826 sipe_media_hangup(call_private
);
827 sipe_backend_media_relays_free(backend_media_relays
);
831 sipe_private
->media_call
= call_private
;
833 sipe_backend_media_relays_free(backend_media_relays
);
835 // Processing continues in stream_initialized_cb
839 sipe_core_media_initiate_call(struct sipe_core_public
*sipe_public
,
843 sipe_media_initiate_call(SIPE_CORE_PRIVATE
, with
,
844 SIPE_ICE_RFC_5245
, with_video
);
847 void sipe_core_media_connect_conference(struct sipe_core_public
*sipe_public
,
848 struct sipe_chat_session
*chat_session
)
850 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
851 struct sipe_backend_media_relays
*backend_media_relays
;
852 struct sip_session
*session
;
853 struct sip_dialog
*dialog
;
854 SipeIceVersion ice_version
;
858 session
= sipe_session_find_chat(sipe_private
, chat_session
);
860 if (sipe_private
->media_call
|| !session
)
863 session
->is_call
= TRUE
;
865 parts
= g_strsplit(chat_session
->id
, "app:conf:focus:", 2);
866 av_uri
= g_strjoinv("app:conf:audio-video:", parts
);
869 ice_version
= SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013
) ? SIPE_ICE_RFC_5245
:
872 sipe_private
->media_call
= sipe_media_call_new(sipe_private
, av_uri
,
875 session
= sipe_session_add_call(sipe_private
, av_uri
);
876 dialog
= sipe_dialog_add(session
);
877 dialog
->callid
= gencallid();
878 dialog
->with
= g_strdup(session
->with
);
879 dialog
->ourtag
= gentag();
883 sipe_private
->media_call
->with
= g_strdup(session
->with
);
885 backend_media_relays
=
886 sipe_backend_media_relays_convert(sipe_private
->media_relays
,
887 sipe_private
->media_relay_username
,
888 sipe_private
->media_relay_password
);
890 if (!sipe_backend_media_add_stream(sipe_private
->media_call
->public.backend_private
,
891 "audio", dialog
->with
,
893 sipe_private
->media_call
->ice_version
,
894 TRUE
, backend_media_relays
)) {
895 sipe_backend_notify_error(sipe_public
,
897 _("Error creating audio stream"));
898 sipe_media_hangup(sipe_private
->media_call
);
899 sipe_private
->media_call
= NULL
;
902 sipe_backend_media_relays_free(backend_media_relays
);
904 // Processing continues in stream_initialized_cb
907 gboolean
sipe_core_media_in_call(struct sipe_core_public
*sipe_public
)
910 return SIPE_CORE_PRIVATE
->media_call
!= NULL
;
915 static gboolean
phone_number_is_valid(const gchar
*phone_number
)
917 if (!phone_number
|| sipe_strequal(phone_number
, "")) {
921 if (*phone_number
== '+') {
925 while (*phone_number
!= '\0') {
926 if (!g_ascii_isdigit(*phone_number
)) {
935 void sipe_core_media_phone_call(struct sipe_core_public
*sipe_public
,
936 const gchar
*phone_number
)
938 g_return_if_fail(sipe_public
);
940 if (phone_number_is_valid(phone_number
)) {
941 gchar
*phone_uri
= g_strdup_printf("sip:%s@%s;user=phone",
942 phone_number
, sipe_public
->sip_domain
);
944 sipe_core_media_initiate_call(sipe_public
, phone_uri
, FALSE
);
948 sipe_backend_notify_error(sipe_public
,
949 _("Unable to establish a call"),
950 _("Invalid phone number"));
954 void sipe_core_media_test_call(struct sipe_core_public
*sipe_public
)
956 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
957 if (!sipe_private
->test_call_bot_uri
) {
958 sipe_backend_notify_error(sipe_public
,
959 _("Unable to establish a call"),
960 _("Audio Test Service is not available."));
964 sipe_core_media_initiate_call(sipe_public
,
965 sipe_private
->test_call_bot_uri
, FALSE
);
969 process_incoming_invite_call(struct sipe_core_private
*sipe_private
,
972 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
973 struct sipe_backend_media
*backend_media
;
974 struct sipe_backend_media_relays
*backend_media_relays
= NULL
;
976 gboolean has_new_media
= FALSE
;
982 if (!is_media_session_msg(call_private
, msg
)) {
983 sip_transport_response(sipe_private
, msg
, 486, "Busy Here", NULL
);
987 self
= sip_uri_self(sipe_private
);
988 if (sipe_strequal(call_private
->with
, self
)) {
990 sip_transport_response(sipe_private
, msg
, 488, "Not Acceptable Here", NULL
);
996 smsg
= sdpmsg_parse_msg(msg
->body
);
998 sip_transport_response(sipe_private
, msg
,
999 488, "Not Acceptable Here", NULL
);
1000 sipe_media_hangup(call_private
);
1004 if (!call_private
) {
1005 gchar
*with
= parse_from(sipmsg_find_header(msg
, "From"));
1006 struct sip_session
*session
;
1008 call_private
= sipe_media_call_new(sipe_private
, with
, FALSE
, smsg
->ice_version
);
1009 session
= sipe_session_add_call(sipe_private
, with
);
1010 sipe_media_dialog_init(session
, msg
);
1012 call_private
->with
= g_strdup(session
->with
);
1013 sipe_private
->media_call
= call_private
;
1017 backend_media
= call_private
->public.backend_private
;
1019 if (call_private
->invitation
)
1020 sipmsg_free(call_private
->invitation
);
1021 call_private
->invitation
= sipmsg_copy(msg
);
1024 backend_media_relays
= sipe_backend_media_relays_convert(
1025 sipe_private
->media_relays
,
1026 sipe_private
->media_relay_username
,
1027 sipe_private
->media_relay_password
);
1029 // Create any new media streams
1030 for (i
= smsg
->media
; i
; i
= i
->next
) {
1031 struct sdpmedia
*media
= i
->data
;
1032 gchar
*id
= media
->name
;
1035 if ( media
->port
!= 0
1036 && !sipe_backend_media_get_stream_by_id(backend_media
, id
)) {
1039 if (sipe_strequal(id
, "audio"))
1040 type
= SIPE_MEDIA_AUDIO
;
1041 else if (sipe_strequal(id
, "video"))
1042 type
= SIPE_MEDIA_VIDEO
;
1046 with
= parse_from(sipmsg_find_header(msg
, "From"));
1047 sipe_backend_media_add_stream(backend_media
, id
, with
,
1051 backend_media_relays
);
1052 has_new_media
= TRUE
;
1057 sipe_backend_media_relays_free(backend_media_relays
);
1059 if (has_new_media
) {
1060 sdpmsg_free(call_private
->smsg
);
1061 call_private
->smsg
= smsg
;
1062 sip_transport_response(sipe_private
, call_private
->invitation
,
1063 180, "Ringing", NULL
);
1064 // Processing continues in stream_initialized_cb
1066 apply_remote_message(call_private
, smsg
);
1067 send_response_with_session_description(call_private
, 200, "OK");
1073 void process_incoming_cancel_call(struct sipe_core_private
*sipe_private
,
1076 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
1078 // We respond to the CANCEL request with 200 OK response and
1079 // with 487 Request Terminated to the remote INVITE in progress.
1080 sip_transport_response(sipe_private
, msg
, 200, "OK", NULL
);
1082 if (call_private
->invitation
) {
1083 sip_transport_response(sipe_private
, call_private
->invitation
,
1084 487, "Request Terminated", NULL
);
1087 sipe_media_hangup(call_private
);
1091 sipe_media_send_ack(struct sipe_core_private
*sipe_private
,
1093 struct transaction
*trans
)
1095 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
1096 struct sip_session
*session
;
1097 struct sip_dialog
*dialog
;
1100 if (!is_media_session_msg(call_private
, msg
))
1103 session
= sipe_session_find_call(sipe_private
, call_private
->with
);
1104 dialog
= session
->dialogs
->data
;
1108 tmp_cseq
= dialog
->cseq
;
1110 dialog
->cseq
= sip_transaction_cseq(trans
) - 1;
1111 sip_transport_ack(sipe_private
, dialog
);
1112 dialog
->cseq
= tmp_cseq
;
1114 dialog
->outgoing_invite
= NULL
;
1120 sipe_media_send_final_ack(struct sipe_core_private
*sipe_private
,
1122 struct transaction
*trans
)
1124 if (!sipe_media_send_ack(sipe_private
, msg
, trans
))
1127 sipe_backend_media_accept(sipe_private
->media_call
->public.backend_private
,
1134 reinvite_on_candidate_pair_cb(struct sipe_core_public
*sipe_public
)
1136 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1137 struct sipe_media_call_private
*media_call
= sipe_private
->media_call
;
1138 struct sipe_backend_media
*backend_media
;
1144 backend_media
= media_call
->public.backend_private
;
1145 streams
= sipe_backend_media_get_streams(backend_media
);
1147 for (; streams
; streams
= streams
->next
) {
1148 struct sipe_backend_stream
*s
= streams
->data
;
1149 GList
*remote_candidates
= sipe_backend_media_get_active_remote_candidates(backend_media
, s
);
1150 guint components
= g_list_length(remote_candidates
);
1152 sipe_media_candidate_list_free(remote_candidates
);
1154 // We must have candidates for both (RTP + RTCP) components ready
1155 if (components
< 2) {
1156 sipe_schedule_mseconds(sipe_private
,
1157 "<+media-reinvite-on-candidate-pair>",
1160 (sipe_schedule_action
) reinvite_on_candidate_pair_cb
,
1166 sipe_invite_call(sipe_private
, sipe_media_send_final_ack
);
1170 maybe_retry_call_with_ice_version(struct sipe_core_private
*sipe_private
,
1171 SipeIceVersion ice_version
,
1172 struct transaction
*trans
)
1174 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
1176 if (call_private
->ice_version
!= ice_version
&&
1177 sip_transaction_cseq(trans
) == 1) {
1178 gchar
*with
= g_strdup(call_private
->with
);
1179 struct sipe_backend_media
*backend_private
= call_private
->public.backend_private
;
1180 gboolean with_video
= sipe_backend_media_get_stream_by_id(backend_private
, "video") != NULL
;
1182 sipe_media_hangup(call_private
);
1183 SIPE_DEBUG_INFO("Retrying call with ICEv%d.",
1184 ice_version
== SIPE_ICE_DRAFT_6
? 6 : 19);
1185 sipe_media_initiate_call(sipe_private
, with
, ice_version
,
1196 process_invite_call_response(struct sipe_core_private
*sipe_private
,
1198 struct transaction
*trans
)
1201 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
1202 struct sip_session
*session
;
1203 struct sip_dialog
*dialog
;
1204 struct sdpmsg
*smsg
;
1206 if (!is_media_session_msg(call_private
, msg
))
1209 session
= sipe_session_find_call(sipe_private
, call_private
->with
);
1210 dialog
= session
->dialogs
->data
;
1212 with
= dialog
->with
;
1214 dialog
->outgoing_invite
= NULL
;
1216 if (msg
->response
>= 400) {
1217 // Call rejected by remote peer or an error occurred
1219 GString
*desc
= g_string_new("");
1220 gboolean append_responsestr
= FALSE
;
1222 switch (msg
->response
) {
1224 title
= _("User unavailable");
1226 if (sipmsg_parse_warning(msg
, NULL
) == 391) {
1227 g_string_append_printf(desc
, _("%s does not want to be disturbed"), with
);
1229 g_string_append_printf(desc
, _("User %s is not available"), with
);
1234 title
= _("Call rejected");
1235 g_string_append_printf(desc
, _("User %s rejected call"), with
);
1238 // OCS/Lync really sends response string with 'Mutipart' typo.
1239 if (sipe_strequal(msg
->responsestr
, "Mutipart mime in content type not supported by Archiving CDR service") &&
1240 maybe_retry_call_with_ice_version(sipe_private
, SIPE_ICE_DRAFT_6
, trans
)) {
1243 title
= _("Unsupported media type");
1246 /* Check for incompatible encryption levels error.
1249 * 488 Not Acceptable Here
1250 * ms-client-diagnostics: 52017;reason="Encryption levels dont match"
1252 * older clients (and SIPE itself):
1253 * 488 Encryption Levels not compatible
1255 const gchar
*ms_diag
= sipmsg_find_header(msg
, "ms-client-diagnostics");
1256 SipeIceVersion retry_ice_version
= SIPE_ICE_DRAFT_6
;
1258 if (sipe_strequal(msg
->responsestr
, "Encryption Levels not compatible") ||
1259 (ms_diag
&& g_str_has_prefix(ms_diag
, "52017;"))) {
1260 title
= _("Unable to establish a call");
1261 g_string_append(desc
, _("Encryption settings of peer are incompatible with ours."));
1265 /* Check if this is failed conference using
1266 * ICEv6 with reason "Error parsing SDP" and
1267 * retry using ICEv19. */
1268 ms_diag
= sipmsg_find_header(msg
, "ms-diagnostics");
1269 if (ms_diag
&& g_str_has_prefix(ms_diag
, "7008;")) {
1270 retry_ice_version
= SIPE_ICE_RFC_5245
;
1273 if (maybe_retry_call_with_ice_version(sipe_private
, retry_ice_version
, trans
)) {
1276 // Break intentionally omitted
1279 title
= _("Error occured");
1280 g_string_append(desc
, _("Unable to establish a call"));
1281 append_responsestr
= TRUE
;
1285 if (append_responsestr
) {
1286 gchar
*reason
= sipmsg_get_ms_diagnostics_reason(msg
);
1288 g_string_append_printf(desc
, "\n%d %s",
1289 msg
->response
, msg
->responsestr
);
1291 g_string_append_printf(desc
, "\n\n%s", reason
);
1296 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, title
, desc
->str
);
1297 g_string_free(desc
, TRUE
);
1299 sipe_media_send_ack(sipe_private
, msg
, trans
);
1300 sipe_media_hangup(call_private
);
1305 sipe_dialog_parse(dialog
, msg
, TRUE
);
1306 smsg
= sdpmsg_parse_msg(msg
->body
);
1308 sip_transport_response(sipe_private
, msg
,
1309 488, "Not Acceptable Here", NULL
);
1310 sipe_media_hangup(call_private
);
1314 apply_remote_message(call_private
, smsg
);
1317 sipe_media_send_ack(sipe_private
, msg
, trans
);
1318 reinvite_on_candidate_pair_cb(SIPE_CORE_PUBLIC
);
1323 gboolean
is_media_session_msg(struct sipe_media_call_private
*call_private
,
1327 const gchar
*callid
= sipmsg_find_header(msg
, "Call-ID");
1328 struct sip_session
*session
;
1330 session
= sipe_session_find_call(call_private
->sipe_private
,
1331 call_private
->with
);
1333 struct sip_dialog
*dialog
= session
->dialogs
->data
;
1334 return sipe_strequal(dialog
->callid
, callid
);
1340 void sipe_media_handle_going_offline(struct sipe_media_call_private
*call_private
)
1342 struct sipe_backend_media
*backend_private
;
1344 backend_private
= call_private
->public.backend_private
;
1346 if ( !sipe_backend_media_is_initiator(backend_private
, NULL
)
1347 && !sipe_backend_media_accepted(backend_private
)) {
1348 sip_transport_response(call_private
->sipe_private
,
1349 call_private
->invitation
,
1350 480, "Temporarily Unavailable", NULL
);
1352 struct sip_session
*session
;
1354 session
= sipe_session_find_call(call_private
->sipe_private
,
1355 call_private
->with
);
1357 sipe_session_close(call_private
->sipe_private
, session
);
1360 sipe_media_hangup(call_private
);
1363 gboolean
sipe_media_is_conference_call(struct sipe_media_call_private
*call_private
)
1365 return g_strstr_len(call_private
->with
, -1, "app:conf:audio-video:") != NULL
;
1369 sipe_media_relay_free(struct sipe_media_relay
*relay
)
1371 g_free(relay
->hostname
);
1372 if (relay
->dns_query
)
1373 sipe_backend_dns_query_cancel(relay
->dns_query
);
1378 sipe_media_relay_list_free(GSList
*list
)
1380 for (; list
; list
= g_slist_delete_link(list
, list
))
1381 sipe_media_relay_free(list
->data
);
1385 relay_ip_resolved_cb(struct sipe_media_relay
* relay
,
1386 const gchar
*ip
, SIPE_UNUSED_PARAMETER guint port
)
1388 gchar
*hostname
= relay
->hostname
;
1389 relay
->dns_query
= NULL
;
1392 relay
->hostname
= g_strdup(ip
);
1393 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname
, ip
);
1395 relay
->hostname
= NULL
;
1396 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname
);
1403 process_get_av_edge_credentials_response(struct sipe_core_private
*sipe_private
,
1405 SIPE_UNUSED_PARAMETER
struct transaction
*trans
)
1407 g_free(sipe_private
->media_relay_username
);
1408 g_free(sipe_private
->media_relay_password
);
1409 sipe_media_relay_list_free(sipe_private
->media_relays
);
1410 sipe_private
->media_relay_username
= NULL
;
1411 sipe_private
->media_relay_password
= NULL
;
1412 sipe_private
->media_relays
= NULL
;
1414 if (msg
->response
>= 400) {
1415 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
1416 "Failed to obtain A/V Edge credentials.");
1420 if (msg
->response
== 200) {
1421 sipe_xml
*xn_response
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1423 if (sipe_strequal("OK", sipe_xml_attribute(xn_response
, "reasonPhrase"))) {
1424 const sipe_xml
*xn_credentials
= sipe_xml_child(xn_response
, "credentialsResponse/credentials");
1425 const sipe_xml
*xn_relays
= sipe_xml_child(xn_response
, "credentialsResponse/mediaRelayList");
1426 const sipe_xml
*item
;
1427 GSList
*relays
= NULL
;
1429 item
= sipe_xml_child(xn_credentials
, "username");
1430 sipe_private
->media_relay_username
= sipe_xml_data(item
);
1431 item
= sipe_xml_child(xn_credentials
, "password");
1432 sipe_private
->media_relay_password
= sipe_xml_data(item
);
1434 for (item
= sipe_xml_child(xn_relays
, "mediaRelay"); item
; item
= sipe_xml_twin(item
)) {
1435 struct sipe_media_relay
*relay
= g_new0(struct sipe_media_relay
, 1);
1436 const sipe_xml
*node
;
1439 node
= sipe_xml_child(item
, "hostName");
1440 relay
->hostname
= sipe_xml_data(node
);
1442 node
= sipe_xml_child(item
, "udpPort");
1444 relay
->udp_port
= atoi(tmp
= sipe_xml_data(node
));
1448 node
= sipe_xml_child(item
, "tcpPort");
1450 relay
->tcp_port
= atoi(tmp
= sipe_xml_data(node
));
1454 relays
= g_slist_append(relays
, relay
);
1456 relay
->dns_query
= sipe_backend_dns_query_a(
1460 (sipe_dns_resolved_cb
) relay_ip_resolved_cb
,
1463 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
1465 relay
->tcp_port
, relay
->udp_port
);
1468 sipe_private
->media_relays
= relays
;
1471 sipe_xml_free(xn_response
);
1478 sipe_media_get_av_edge_credentials(struct sipe_core_private
*sipe_private
)
1480 // TODO: re-request credentials after duration expires?
1481 const char CRED_REQUEST_XML
[] =
1482 "<request requestID=\"%d\" "
1486 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
1487 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
1488 "<credentialsRequest credentialsRequestID=\"%d\">"
1489 "<identity>%s</identity>"
1490 "<location>%s</location>"
1491 "<duration>480</duration>"
1492 "</credentialsRequest>"
1495 int request_id
= rand();
1499 if (!sipe_private
->mras_uri
)
1502 self
= sip_uri_self(sipe_private
);
1504 body
= g_strdup_printf(
1508 sipe_private
->mras_uri
,
1511 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER
) ? "internet" : "intranet");
1514 sip_transport_service(sipe_private
,
1515 sipe_private
->mras_uri
,
1516 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
1518 process_get_av_edge_credentials_response
);