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
= g_new(struct sdpcandidate
, 1);
126 c
->foundation
= sipe_backend_candidate_get_foundation(candidate
);
127 c
->component
= sipe_backend_candidate_get_component_type(candidate
);
128 c
->type
= sipe_backend_candidate_get_type(candidate
);
129 c
->protocol
= sipe_backend_candidate_get_protocol(candidate
);
130 c
->ip
= sipe_backend_candidate_get_ip(candidate
);
131 c
->port
= sipe_backend_candidate_get_port(candidate
);
132 c
->base_ip
= sipe_backend_candidate_get_base_ip(candidate
);
133 c
->base_port
= sipe_backend_candidate_get_base_port(candidate
);
134 c
->priority
= sipe_backend_candidate_get_priority(candidate
);
135 c
->username
= sipe_backend_candidate_get_username(candidate
);
136 c
->password
= sipe_backend_candidate_get_password(candidate
);
138 result
= g_slist_insert_sorted(result
, c
,
139 (GCompareFunc
)candidate_sort_cb
);
146 get_stream_ip_and_ports(GSList
*candidates
,
147 gchar
**ip
, guint
*rtp_port
, guint
*rtcp_port
,
148 SipeCandidateType type
)
154 for (; candidates
; candidates
= candidates
->next
) {
155 struct sdpcandidate
*candidate
= candidates
->data
;
157 if (type
== SIPE_CANDIDATE_TYPE_ANY
|| candidate
->type
== type
) {
159 *ip
= g_strdup(candidate
->ip
);
160 } else if (!sipe_strequal(*ip
, candidate
->ip
)) {
164 if (candidate
->component
== SIPE_COMPONENT_RTP
) {
165 *rtp_port
= candidate
->port
;
166 } else if (candidate
->component
== SIPE_COMPONENT_RTCP
)
167 *rtcp_port
= candidate
->port
;
170 if (*rtp_port
!= 0 && *rtcp_port
!= 0)
176 sdpcodec_compare(gconstpointer a
, gconstpointer b
)
178 return ((const struct sdpcodec
*)a
)->id
-
179 ((const struct sdpcodec
*)b
)->id
;
182 static struct sdpmedia
*
183 backend_stream_to_sdpmedia(struct sipe_backend_media
*backend_media
,
184 struct sipe_backend_stream
*backend_stream
)
186 struct sdpmedia
*media
= g_new0(struct sdpmedia
, 1);
187 GList
*codecs
= sipe_backend_get_local_codecs(backend_media
,
191 GSList
*attributes
= NULL
;
195 media
->name
= g_strdup(sipe_backend_stream_get_id(backend_stream
));
197 if (sipe_strequal(media
->name
, "audio"))
198 type
= SIPE_MEDIA_AUDIO
;
199 else if (sipe_strequal(media
->name
, "video"))
200 type
= SIPE_MEDIA_VIDEO
;
202 // TODO: incompatible media, should not happen here
205 sipe_media_codec_list_free(codecs
);
210 for (i
= codecs
; i
; i
= i
->next
) {
211 struct sipe_backend_codec
*codec
= i
->data
;
212 struct sdpcodec
*c
= g_new0(struct sdpcodec
, 1);
215 c
->id
= sipe_backend_codec_get_id(codec
);
216 c
->name
= sipe_backend_codec_get_name(codec
);
217 c
->clock_rate
= sipe_backend_codec_get_clock_rate(codec
);
220 params
= sipe_backend_codec_get_optional_parameters(codec
);
221 for (; params
; params
= params
->next
) {
222 struct sipnameval
*param
= params
->data
;
223 struct sipnameval
*copy
= g_new0(struct sipnameval
, 1);
225 copy
->name
= g_strdup(param
->name
);
226 copy
->value
= g_strdup(param
->value
);
228 c
->parameters
= g_slist_append(c
->parameters
, copy
);
231 /* Buggy(?) codecs may report non-unique id (a.k.a. payload
232 * type) that must not appear in SDP messages we send. Thus,
233 * let's ignore any codec having the same id as one we already
234 * have in the converted list. */
235 media
->codecs
= sipe_utils_slist_insert_unique_sorted(
236 media
->codecs
, c
, sdpcodec_compare
,
237 (GDestroyNotify
)sdpcodec_free
);
240 sipe_media_codec_list_free(codecs
);
242 // Process local candidates
243 // If we have established candidate pairs, send them in SDP response.
244 // Otherwise send all available local candidates.
245 candidates
= sipe_backend_media_get_active_local_candidates(backend_media
,
248 candidates
= sipe_backend_get_local_candidates(backend_media
,
251 media
->candidates
= backend_candidates_to_sdpcandidate(candidates
);
253 sipe_media_candidate_list_free(candidates
);
255 get_stream_ip_and_ports(media
->candidates
, &media
->ip
, &media
->port
,
256 &rtcp_port
, SIPE_CANDIDATE_TYPE_HOST
);
257 // No usable HOST candidates, use any candidate
258 if (media
->ip
== NULL
&& media
->candidates
) {
259 get_stream_ip_and_ports(media
->candidates
, &media
->ip
, &media
->port
,
260 &rtcp_port
, SIPE_CANDIDATE_TYPE_ANY
);
263 if (sipe_backend_stream_is_held(backend_stream
))
264 attributes
= sipe_utils_nameval_add(attributes
, "inactive", "");
267 gchar
*tmp
= g_strdup_printf("%u", rtcp_port
);
268 attributes
= sipe_utils_nameval_add(attributes
, "rtcp", tmp
);
272 attributes
= sipe_utils_nameval_add(attributes
, "encryption", "rejected");
274 media
->attributes
= attributes
;
276 // Process remote candidates
277 candidates
= sipe_backend_media_get_active_remote_candidates(backend_media
,
279 media
->remote_candidates
= backend_candidates_to_sdpcandidate(candidates
);
280 sipe_media_candidate_list_free(candidates
);
285 static struct sdpmsg
*
286 sipe_media_to_sdpmsg(struct sipe_media_call_private
*call_private
)
288 struct sipe_backend_media
*backend_media
= call_private
->public.backend_private
;
289 struct sdpmsg
*msg
= g_new0(struct sdpmsg
, 1);
290 GSList
*streams
= sipe_backend_media_get_streams(backend_media
);
292 for (; streams
; streams
= streams
->next
) {
293 struct sdpmedia
*media
= backend_stream_to_sdpmedia(backend_media
, streams
->data
);
295 msg
->media
= g_slist_append(msg
->media
, media
);
298 msg
->ip
= g_strdup(media
->ip
);
302 msg
->media
= g_slist_concat(msg
->media
, call_private
->failed_media
);
303 call_private
->failed_media
= NULL
;
305 msg
->ice_version
= call_private
->ice_version
;
311 sipe_invite_call(struct sipe_core_private
*sipe_private
, TransCallback tc
)
315 gchar
*p_preferred_identity
= NULL
;
317 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
318 struct sip_session
*session
;
319 struct sip_dialog
*dialog
;
321 gboolean add_2007_fallback
= FALSE
;
323 session
= sipe_session_find_call(sipe_private
, call_private
->with
);
324 dialog
= session
->dialogs
->data
;
325 add_2007_fallback
= dialog
->cseq
== 0 &&
326 call_private
->ice_version
== SIPE_ICE_RFC_5245
&&
327 !sipe_strequal(call_private
->with
, sipe_private
->test_call_bot_uri
);
329 contact
= get_contact(sipe_private
);
331 if (sipe_private
->uc_line_uri
) {
332 gchar
*self
= sip_uri_self(sipe_private
);
333 p_preferred_identity
= g_strdup_printf(
334 "P-Preferred-Identity: <%s>, <%s>\r\n",
335 self
, sipe_private
->uc_line_uri
);
339 hdr
= g_strdup_printf(
340 "ms-keep-alive: UAC;hop-hop=yes\r\n"
343 "Content-Type: %s\r\n",
345 p_preferred_identity
? p_preferred_identity
: "",
347 "multipart/alternative;boundary=\"----=_NextPart_000_001E_01CB4397.0B5EB570\""
348 : "application/sdp");
350 g_free(p_preferred_identity
);
352 msg
= sipe_media_to_sdpmsg(call_private
);
353 body
= sdpmsg_to_string(msg
);
355 if (add_2007_fallback
) {
357 tmp
= g_strdup_printf(
358 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
359 "Content-Type: application/sdp\r\n"
360 "Content-Transfer-Encoding: 7bit\r\n"
361 "Content-Disposition: session; handling=optional; ms-proxy-2007fallback\r\n"
363 "o=- 0 0 IN IP4 %s\r\n"
366 "m=audio 0 RTP/AVP\r\n"
368 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
369 "Content-Type: application/sdp\r\n"
370 "Content-Transfer-Encoding: 7bit\r\n"
371 "Content-Disposition: session; handling=optional\r\n"
375 "------=_NextPart_000_001E_01CB4397.0B5EB570--\r\n",
376 msg
->ip
, msg
->ip
, body
);
383 dialog
->outgoing_invite
= sip_transport_invite(sipe_private
,
393 static struct sip_dialog
*
394 sipe_media_dialog_init(struct sip_session
* session
, struct sipmsg
*msg
)
396 gchar
*newTag
= gentag();
397 const gchar
*oldHeader
;
399 struct sip_dialog
*dialog
;
401 oldHeader
= sipmsg_find_header(msg
, "To");
402 newHeader
= g_strdup_printf("%s;tag=%s", oldHeader
, newTag
);
403 sipmsg_remove_header_now(msg
, "To");
404 sipmsg_add_header_now(msg
, "To", newHeader
);
407 dialog
= sipe_dialog_add(session
);
408 dialog
->callid
= g_strdup(sipmsg_find_header(msg
, "Call-ID"));
409 dialog
->with
= parse_from(sipmsg_find_header(msg
, "From"));
410 sipe_dialog_parse(dialog
, msg
, FALSE
);
416 send_response_with_session_description(struct sipe_media_call_private
*call_private
, int code
, gchar
*text
)
418 struct sdpmsg
*msg
= sipe_media_to_sdpmsg(call_private
);
419 gchar
*body
= sdpmsg_to_string(msg
);
421 sipmsg_add_header(call_private
->invitation
, "Content-Type", "application/sdp");
422 sip_transport_response(call_private
->sipe_private
, call_private
->invitation
, code
, text
, body
);
427 encryption_levels_compatible(struct sdpmsg
*msg
)
431 for (i
= msg
->media
; i
; i
= i
->next
) {
432 const gchar
*enc_level
;
433 struct sdpmedia
*m
= i
->data
;
435 enc_level
= sipe_utils_nameval_find(m
->attributes
, "encryption");
437 // Decline call if peer requires encryption as we don't support it yet.
438 if (sipe_strequal(enc_level
, "required"))
446 process_invite_call_response(struct sipe_core_private
*sipe_private
,
448 struct transaction
*trans
);
451 update_remote_media(struct sipe_media_call_private
* call_private
,
452 struct sdpmedia
*media
)
454 struct sipe_backend_media
*backend_media
= SIPE_MEDIA_CALL
->backend_private
;
455 struct sipe_backend_stream
*backend_stream
;
456 GList
*backend_candidates
= NULL
;
457 GList
*backend_codecs
= NULL
;
459 gboolean result
= TRUE
;
461 backend_stream
= sipe_backend_media_get_stream_by_id(backend_media
,
463 if (media
->port
== 0) {
465 sipe_backend_media_remove_stream(backend_media
, backend_stream
);
472 for (i
= media
->codecs
; i
; i
= i
->next
) {
473 struct sdpcodec
*c
= i
->data
;
474 struct sipe_backend_codec
*codec
;
477 codec
= sipe_backend_codec_new(c
->id
,
482 for (j
= c
->parameters
; j
; j
= j
->next
) {
483 struct sipnameval
*attr
= j
->data
;
485 sipe_backend_codec_add_optional_parameter(codec
,
490 backend_codecs
= g_list_append(backend_codecs
, codec
);
493 result
= sipe_backend_set_remote_codecs(backend_media
,
496 sipe_media_codec_list_free(backend_codecs
);
498 if (result
== FALSE
) {
499 sipe_backend_media_remove_stream(backend_media
, backend_stream
);
503 for (i
= media
->candidates
; i
; i
= i
->next
) {
504 struct sdpcandidate
*c
= i
->data
;
505 struct sipe_backend_candidate
*candidate
;
506 candidate
= sipe_backend_candidate_new(c
->foundation
,
514 sipe_backend_candidate_set_priority(candidate
, c
->priority
);
516 backend_candidates
= g_list_append(backend_candidates
, candidate
);
519 sipe_backend_media_add_remote_candidates(backend_media
,
522 sipe_media_candidate_list_free(backend_candidates
);
524 if (sipe_utils_nameval_find(media
->attributes
, "inactive")) {
525 sipe_backend_stream_hold(backend_media
, backend_stream
, FALSE
);
526 } else if (sipe_backend_stream_is_held(backend_stream
)) {
527 sipe_backend_stream_unhold(backend_media
, backend_stream
, FALSE
);
534 apply_remote_message(struct sipe_media_call_private
* call_private
,
539 sipe_utils_slist_free_full(call_private
->failed_media
, (GDestroyNotify
)sdpmedia_free
);
540 call_private
->failed_media
= NULL
;
542 for (i
= msg
->media
; i
; i
= i
->next
) {
543 struct sdpmedia
*media
= i
->data
;
544 if (!update_remote_media(call_private
, media
)) {
546 call_private
->failed_media
=
547 g_slist_append(call_private
->failed_media
, media
);
551 /* We need to keep failed medias until response is sent, remove them
552 * from sdpmsg that is to be freed. */
553 for (i
= call_private
->failed_media
; i
; i
= i
->next
) {
554 msg
->media
= g_slist_remove(msg
->media
, i
->data
);
557 call_private
->encryption_compatible
= encryption_levels_compatible(msg
);
561 call_initialized(struct sipe_media_call
*call
)
564 sipe_backend_media_get_streams(call
->backend_private
);
566 for (; streams
; streams
= streams
->next
) {
567 if (!sipe_backend_stream_initialized(call
->backend_private
,
576 // Sends an invite response when the call is accepted and local candidates were
577 // prepared, otherwise does nothing. If error response is sent, call_private is
578 // disposed before function returns. Returns true when response was sent.
580 send_invite_response_if_ready(struct sipe_media_call_private
*call_private
)
582 struct sipe_backend_media
*backend_media
;
584 backend_media
= call_private
->public.backend_private
;
586 if (!sipe_backend_media_accepted(backend_media
) ||
587 !call_initialized(&call_private
->public))
590 if (!call_private
->encryption_compatible
) {
591 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
593 sipmsg_add_header(call_private
->invitation
, "Warning",
594 "308 lcs.microsoft.com \"Encryption Levels not compatible\"");
595 sip_transport_response(sipe_private
,
596 call_private
->invitation
,
597 488, "Encryption Levels not compatible",
599 sipe_backend_media_reject(backend_media
, FALSE
);
600 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
601 _("Unable to establish a call"),
602 _("Encryption settings of peer are incompatible with ours."));
604 send_response_with_session_description(call_private
, 200, "OK");
611 stream_initialized_cb(struct sipe_media_call
*call
,
612 struct sipe_backend_stream
*stream
)
614 if (call_initialized(call
)) {
615 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
616 struct sipe_backend_media
*backend_private
= call
->backend_private
;
618 if (sipe_backend_media_is_initiator(backend_private
, stream
)) {
619 sipe_invite_call(call_private
->sipe_private
,
620 process_invite_call_response
);
621 } else if (call_private
->smsg
) {
622 struct sdpmsg
*smsg
= call_private
->smsg
;
623 call_private
->smsg
= NULL
;
625 apply_remote_message(call_private
, smsg
);
626 send_invite_response_if_ready(call_private
);
632 static void phone_state_publish(struct sipe_core_private
*sipe_private
)
634 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
635 sipe_ocs2007_phone_state_publish(sipe_private
);
637 // TODO: OCS 2005 support. Is anyone still using it at all?
642 media_end_cb(struct sipe_media_call
*call
)
644 g_return_if_fail(call
);
646 SIPE_MEDIA_CALL_PRIVATE
->sipe_private
->media_call
= NULL
;
647 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
);
648 sipe_media_call_free(SIPE_MEDIA_CALL_PRIVATE
);
652 call_accept_cb(struct sipe_media_call
*call
, gboolean local
)
655 send_invite_response_if_ready(SIPE_MEDIA_CALL_PRIVATE
);
657 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
);
661 call_reject_cb(struct sipe_media_call
*call
, gboolean local
)
664 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
665 sip_transport_response(call_private
->sipe_private
,
666 call_private
->invitation
,
667 603, "Decline", NULL
);
672 sipe_media_send_ack(struct sipe_core_private
*sipe_private
, struct sipmsg
*msg
,
673 struct transaction
*trans
);
675 static void call_hold_cb(struct sipe_media_call
*call
,
677 SIPE_UNUSED_PARAMETER gboolean state
)
680 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
,
681 sipe_media_send_ack
);
684 static void call_hangup_cb(struct sipe_media_call
*call
, gboolean local
)
687 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
688 struct sip_session
*session
;
689 session
= sipe_session_find_call(call_private
->sipe_private
,
693 sipe_session_close(call_private
->sipe_private
, session
);
699 error_cb(struct sipe_media_call
*call
, gchar
*message
)
701 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
702 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
703 gboolean initiator
= sipe_backend_media_is_initiator(call
->backend_private
, NULL
);
704 gboolean accepted
= sipe_backend_media_accepted(call
->backend_private
);
706 gchar
*title
= g_strdup_printf("Call with %s failed", call_private
->with
);
707 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, title
, message
);
710 if (!initiator
&& !accepted
) {
711 sip_transport_response(sipe_private
,
712 call_private
->invitation
,
713 488, "Not Acceptable Here", NULL
);
716 sipe_backend_media_hangup(call
->backend_private
, initiator
|| accepted
);
719 static struct sipe_media_call_private
*
720 sipe_media_call_new(struct sipe_core_private
*sipe_private
,
721 const gchar
* with
, gboolean initiator
, SipeIceVersion ice_version
)
723 struct sipe_media_call_private
*call_private
= g_new0(struct sipe_media_call_private
, 1);
726 call_private
->sipe_private
= sipe_private
;
728 cname
= g_strdup(sipe_private
->contact
+ 1);
729 cname
[strlen(cname
) - 1] = '\0';
731 call_private
->public.backend_private
= sipe_backend_media_new(SIPE_CORE_PUBLIC
,
735 sipe_backend_media_set_cname(call_private
->public.backend_private
, cname
);
737 call_private
->ice_version
= ice_version
;
738 call_private
->encryption_compatible
= TRUE
;
740 call_private
->public.stream_initialized_cb
= stream_initialized_cb
;
741 call_private
->public.media_end_cb
= media_end_cb
;
742 call_private
->public.call_accept_cb
= call_accept_cb
;
743 call_private
->public.call_reject_cb
= call_reject_cb
;
744 call_private
->public.call_hold_cb
= call_hold_cb
;
745 call_private
->public.call_hangup_cb
= call_hangup_cb
;
746 call_private
->public.error_cb
= error_cb
;
753 void sipe_media_hangup(struct sipe_media_call_private
*call_private
)
756 sipe_backend_media_hangup(call_private
->public.backend_private
,
762 sipe_media_initiate_call(struct sipe_core_private
*sipe_private
,
763 const char *with
, SipeIceVersion ice_version
,
766 struct sipe_media_call_private
*call_private
;
767 struct sipe_backend_media
*backend_media
;
768 struct sipe_backend_media_relays
*backend_media_relays
;
769 struct sip_session
*session
;
770 struct sip_dialog
*dialog
;
772 if (sipe_private
->media_call
)
775 call_private
= sipe_media_call_new(sipe_private
, with
, TRUE
, ice_version
);
777 session
= sipe_session_add_call(sipe_private
, with
);
778 dialog
= sipe_dialog_add(session
);
779 dialog
->callid
= gencallid();
780 dialog
->with
= g_strdup(session
->with
);
781 dialog
->ourtag
= gentag();
783 call_private
->with
= g_strdup(session
->with
);
785 backend_media
= call_private
->public.backend_private
;
787 backend_media_relays
=
788 sipe_backend_media_relays_convert(sipe_private
->media_relays
,
789 sipe_private
->media_relay_username
,
790 sipe_private
->media_relay_password
);
792 if (!sipe_backend_media_add_stream(backend_media
,
793 "audio", with
, SIPE_MEDIA_AUDIO
,
794 call_private
->ice_version
, TRUE
,
795 backend_media_relays
)) {
796 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
798 _("Error creating audio stream"));
799 sipe_media_hangup(call_private
);
800 sipe_backend_media_relays_free(backend_media_relays
);
805 && !sipe_backend_media_add_stream(backend_media
,
806 "video", with
, SIPE_MEDIA_VIDEO
,
807 call_private
->ice_version
, TRUE
,
808 backend_media_relays
)) {
809 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
811 _("Error creating video stream"));
812 sipe_media_hangup(call_private
);
813 sipe_backend_media_relays_free(backend_media_relays
);
817 sipe_private
->media_call
= call_private
;
819 sipe_backend_media_relays_free(backend_media_relays
);
821 // Processing continues in stream_initialized_cb
825 sipe_core_media_initiate_call(struct sipe_core_public
*sipe_public
,
829 sipe_media_initiate_call(SIPE_CORE_PRIVATE
, with
,
830 SIPE_ICE_RFC_5245
, with_video
);
833 void sipe_core_media_connect_conference(struct sipe_core_public
*sipe_public
,
834 struct sipe_chat_session
*chat_session
)
836 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
837 struct sipe_backend_media_relays
*backend_media_relays
;
838 struct sip_session
*session
;
839 struct sip_dialog
*dialog
;
840 SipeIceVersion ice_version
;
844 session
= sipe_session_find_chat(sipe_private
, chat_session
);
846 if (sipe_private
->media_call
|| !session
)
849 session
->is_call
= TRUE
;
851 parts
= g_strsplit(chat_session
->id
, "app:conf:focus:", 2);
852 av_uri
= g_strjoinv("app:conf:audio-video:", parts
);
855 ice_version
= SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013
) ? SIPE_ICE_RFC_5245
:
858 sipe_private
->media_call
= sipe_media_call_new(sipe_private
, av_uri
,
861 session
= sipe_session_add_call(sipe_private
, av_uri
);
862 dialog
= sipe_dialog_add(session
);
863 dialog
->callid
= gencallid();
864 dialog
->with
= g_strdup(session
->with
);
865 dialog
->ourtag
= gentag();
869 sipe_private
->media_call
->with
= g_strdup(session
->with
);
871 backend_media_relays
=
872 sipe_backend_media_relays_convert(sipe_private
->media_relays
,
873 sipe_private
->media_relay_username
,
874 sipe_private
->media_relay_password
);
876 if (!sipe_backend_media_add_stream(sipe_private
->media_call
->public.backend_private
,
877 "audio", dialog
->with
,
879 sipe_private
->media_call
->ice_version
,
880 TRUE
, backend_media_relays
)) {
881 sipe_backend_notify_error(sipe_public
,
883 _("Error creating audio stream"));
884 sipe_media_hangup(sipe_private
->media_call
);
885 sipe_private
->media_call
= NULL
;
888 sipe_backend_media_relays_free(backend_media_relays
);
890 // Processing continues in stream_initialized_cb
893 gboolean
sipe_core_media_in_call(struct sipe_core_public
*sipe_public
)
896 return SIPE_CORE_PRIVATE
->media_call
!= NULL
;
901 static gboolean
phone_number_is_valid(const gchar
*phone_number
)
903 if (!phone_number
|| sipe_strequal(phone_number
, "")) {
907 if (*phone_number
== '+') {
911 while (*phone_number
!= '\0') {
912 if (!g_ascii_isdigit(*phone_number
)) {
921 void sipe_core_media_phone_call(struct sipe_core_public
*sipe_public
,
922 const gchar
*phone_number
)
924 g_return_if_fail(sipe_public
);
926 if (phone_number_is_valid(phone_number
)) {
927 gchar
*phone_uri
= g_strdup_printf("sip:%s@%s;user=phone",
928 phone_number
, sipe_public
->sip_domain
);
930 sipe_core_media_initiate_call(sipe_public
, phone_uri
, FALSE
);
934 sipe_backend_notify_error(sipe_public
,
935 _("Unable to establish a call"),
936 _("Invalid phone number"));
940 void sipe_core_media_test_call(struct sipe_core_public
*sipe_public
)
942 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
943 if (!sipe_private
->test_call_bot_uri
) {
944 sipe_backend_notify_error(sipe_public
,
945 _("Unable to establish a call"),
946 _("Audio Test Service is not available."));
950 sipe_core_media_initiate_call(sipe_public
,
951 sipe_private
->test_call_bot_uri
, FALSE
);
955 process_incoming_invite_call(struct sipe_core_private
*sipe_private
,
958 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
959 struct sipe_backend_media
*backend_media
;
960 struct sipe_backend_media_relays
*backend_media_relays
= NULL
;
962 gboolean has_new_media
= FALSE
;
968 if (!is_media_session_msg(call_private
, msg
)) {
969 sip_transport_response(sipe_private
, msg
, 486, "Busy Here", NULL
);
973 self
= sip_uri_self(sipe_private
);
974 if (sipe_strequal(call_private
->with
, self
)) {
976 sip_transport_response(sipe_private
, msg
, 488, "Not Acceptable Here", NULL
);
982 smsg
= sdpmsg_parse_msg(msg
->body
);
984 sip_transport_response(sipe_private
, msg
,
985 488, "Not Acceptable Here", NULL
);
986 sipe_media_hangup(call_private
);
991 gchar
*with
= parse_from(sipmsg_find_header(msg
, "From"));
992 struct sip_session
*session
;
994 call_private
= sipe_media_call_new(sipe_private
, with
, FALSE
, smsg
->ice_version
);
995 session
= sipe_session_add_call(sipe_private
, with
);
996 sipe_media_dialog_init(session
, msg
);
998 call_private
->with
= g_strdup(session
->with
);
999 sipe_private
->media_call
= call_private
;
1003 backend_media
= call_private
->public.backend_private
;
1005 if (call_private
->invitation
)
1006 sipmsg_free(call_private
->invitation
);
1007 call_private
->invitation
= sipmsg_copy(msg
);
1010 backend_media_relays
= sipe_backend_media_relays_convert(
1011 sipe_private
->media_relays
,
1012 sipe_private
->media_relay_username
,
1013 sipe_private
->media_relay_password
);
1015 // Create any new media streams
1016 for (i
= smsg
->media
; i
; i
= i
->next
) {
1017 struct sdpmedia
*media
= i
->data
;
1018 gchar
*id
= media
->name
;
1021 if ( media
->port
!= 0
1022 && !sipe_backend_media_get_stream_by_id(backend_media
, id
)) {
1025 if (sipe_strequal(id
, "audio"))
1026 type
= SIPE_MEDIA_AUDIO
;
1027 else if (sipe_strequal(id
, "video"))
1028 type
= SIPE_MEDIA_VIDEO
;
1032 with
= parse_from(sipmsg_find_header(msg
, "From"));
1033 sipe_backend_media_add_stream(backend_media
, id
, with
,
1037 backend_media_relays
);
1038 has_new_media
= TRUE
;
1043 sipe_backend_media_relays_free(backend_media_relays
);
1045 if (has_new_media
) {
1046 sdpmsg_free(call_private
->smsg
);
1047 call_private
->smsg
= smsg
;
1048 sip_transport_response(sipe_private
, call_private
->invitation
,
1049 180, "Ringing", NULL
);
1050 // Processing continues in stream_initialized_cb
1052 apply_remote_message(call_private
, smsg
);
1053 send_response_with_session_description(call_private
, 200, "OK");
1059 void process_incoming_cancel_call(struct sipe_core_private
*sipe_private
,
1062 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
1064 // We respond to the CANCEL request with 200 OK response and
1065 // with 487 Request Terminated to the remote INVITE in progress.
1066 sip_transport_response(sipe_private
, msg
, 200, "OK", NULL
);
1068 if (call_private
->invitation
) {
1069 sip_transport_response(sipe_private
, call_private
->invitation
,
1070 487, "Request Terminated", NULL
);
1073 sipe_media_hangup(call_private
);
1077 sipe_media_send_ack(struct sipe_core_private
*sipe_private
,
1079 struct transaction
*trans
)
1081 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
1082 struct sip_session
*session
;
1083 struct sip_dialog
*dialog
;
1086 if (!is_media_session_msg(call_private
, msg
))
1089 session
= sipe_session_find_call(sipe_private
, call_private
->with
);
1090 dialog
= session
->dialogs
->data
;
1094 tmp_cseq
= dialog
->cseq
;
1096 dialog
->cseq
= sip_transaction_cseq(trans
) - 1;
1097 sip_transport_ack(sipe_private
, dialog
);
1098 dialog
->cseq
= tmp_cseq
;
1100 dialog
->outgoing_invite
= NULL
;
1106 sipe_media_send_final_ack(struct sipe_core_private
*sipe_private
,
1108 struct transaction
*trans
)
1110 if (!sipe_media_send_ack(sipe_private
, msg
, trans
))
1113 sipe_backend_media_accept(sipe_private
->media_call
->public.backend_private
,
1120 reinvite_on_candidate_pair_cb(struct sipe_core_public
*sipe_public
)
1122 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1123 struct sipe_media_call_private
*media_call
= sipe_private
->media_call
;
1124 struct sipe_backend_media
*backend_media
;
1130 backend_media
= media_call
->public.backend_private
;
1131 streams
= sipe_backend_media_get_streams(backend_media
);
1133 for (; streams
; streams
= streams
->next
) {
1134 struct sipe_backend_stream
*s
= streams
->data
;
1135 GList
*remote_candidates
= sipe_backend_media_get_active_remote_candidates(backend_media
, s
);
1136 guint components
= g_list_length(remote_candidates
);
1138 sipe_media_candidate_list_free(remote_candidates
);
1140 // We must have candidates for both (RTP + RTCP) components ready
1141 if (components
< 2) {
1142 sipe_schedule_mseconds(sipe_private
,
1143 "<+media-reinvite-on-candidate-pair>",
1146 (sipe_schedule_action
) reinvite_on_candidate_pair_cb
,
1152 sipe_invite_call(sipe_private
, sipe_media_send_final_ack
);
1156 maybe_retry_call_with_ice_version(struct sipe_core_private
*sipe_private
,
1157 SipeIceVersion ice_version
,
1158 struct transaction
*trans
)
1160 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
1162 if (call_private
->ice_version
!= ice_version
&&
1163 sip_transaction_cseq(trans
) == 1) {
1164 gchar
*with
= g_strdup(call_private
->with
);
1165 struct sipe_backend_media
*backend_private
= call_private
->public.backend_private
;
1166 gboolean with_video
= sipe_backend_media_get_stream_by_id(backend_private
, "video") != NULL
;
1168 sipe_media_hangup(call_private
);
1169 SIPE_DEBUG_INFO("Retrying call with ICEv%d.",
1170 ice_version
== SIPE_ICE_DRAFT_6
? 6 : 19);
1171 sipe_media_initiate_call(sipe_private
, with
, ice_version
,
1182 process_invite_call_response(struct sipe_core_private
*sipe_private
,
1184 struct transaction
*trans
)
1187 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
1188 struct sip_session
*session
;
1189 struct sip_dialog
*dialog
;
1190 struct sdpmsg
*smsg
;
1192 if (!is_media_session_msg(call_private
, msg
))
1195 session
= sipe_session_find_call(sipe_private
, call_private
->with
);
1196 dialog
= session
->dialogs
->data
;
1198 with
= dialog
->with
;
1200 dialog
->outgoing_invite
= NULL
;
1202 if (msg
->response
>= 400) {
1203 // Call rejected by remote peer or an error occurred
1205 GString
*desc
= g_string_new("");
1206 gboolean append_responsestr
= FALSE
;
1208 switch (msg
->response
) {
1210 title
= _("User unavailable");
1212 if (sipmsg_parse_warning(msg
, NULL
) == 391) {
1213 g_string_append_printf(desc
, _("%s does not want to be disturbed"), with
);
1215 g_string_append_printf(desc
, _("User %s is not available"), with
);
1220 title
= _("Call rejected");
1221 g_string_append_printf(desc
, _("User %s rejected call"), with
);
1224 // OCS/Lync really sends response string with 'Mutipart' typo.
1225 if (sipe_strequal(msg
->responsestr
, "Mutipart mime in content type not supported by Archiving CDR service") &&
1226 maybe_retry_call_with_ice_version(sipe_private
, SIPE_ICE_DRAFT_6
, trans
)) {
1229 title
= _("Unsupported media type");
1232 /* Check for incompatible encryption levels error.
1235 * 488 Not Acceptable Here
1236 * ms-client-diagnostics: 52017;reason="Encryption levels dont match"
1238 * older clients (and SIPE itself):
1239 * 488 Encryption Levels not compatible
1241 const gchar
*ms_diag
= sipmsg_find_header(msg
, "ms-client-diagnostics");
1242 SipeIceVersion retry_ice_version
= SIPE_ICE_DRAFT_6
;
1244 if (sipe_strequal(msg
->responsestr
, "Encryption Levels not compatible") ||
1245 (ms_diag
&& g_str_has_prefix(ms_diag
, "52017;"))) {
1246 title
= _("Unable to establish a call");
1247 g_string_append(desc
, _("Encryption settings of peer are incompatible with ours."));
1251 /* Check if this is failed conference using
1252 * ICEv6 with reason "Error parsing SDP" and
1253 * retry using ICEv19. */
1254 ms_diag
= sipmsg_find_header(msg
, "ms-diagnostics");
1255 if (ms_diag
&& g_str_has_prefix(ms_diag
, "7008;")) {
1256 retry_ice_version
= SIPE_ICE_RFC_5245
;
1259 if (maybe_retry_call_with_ice_version(sipe_private
, retry_ice_version
, trans
)) {
1262 // Break intentionally omitted
1265 title
= _("Error occured");
1266 g_string_append(desc
, _("Unable to establish a call"));
1267 append_responsestr
= TRUE
;
1271 if (append_responsestr
) {
1272 gchar
*reason
= sipmsg_get_ms_diagnostics_reason(msg
);
1274 g_string_append_printf(desc
, "\n%d %s",
1275 msg
->response
, msg
->responsestr
);
1277 g_string_append_printf(desc
, "\n\n%s", reason
);
1282 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, title
, desc
->str
);
1283 g_string_free(desc
, TRUE
);
1285 sipe_media_send_ack(sipe_private
, msg
, trans
);
1286 sipe_media_hangup(call_private
);
1291 sipe_dialog_parse(dialog
, msg
, TRUE
);
1292 smsg
= sdpmsg_parse_msg(msg
->body
);
1294 sip_transport_response(sipe_private
, msg
,
1295 488, "Not Acceptable Here", NULL
);
1296 sipe_media_hangup(call_private
);
1300 apply_remote_message(call_private
, smsg
);
1303 sipe_media_send_ack(sipe_private
, msg
, trans
);
1304 reinvite_on_candidate_pair_cb(SIPE_CORE_PUBLIC
);
1309 gboolean
is_media_session_msg(struct sipe_media_call_private
*call_private
,
1313 const gchar
*callid
= sipmsg_find_header(msg
, "Call-ID");
1314 struct sip_session
*session
;
1316 session
= sipe_session_find_call(call_private
->sipe_private
,
1317 call_private
->with
);
1319 struct sip_dialog
*dialog
= session
->dialogs
->data
;
1320 return sipe_strequal(dialog
->callid
, callid
);
1326 void sipe_media_handle_going_offline(struct sipe_media_call_private
*call_private
)
1328 struct sipe_backend_media
*backend_private
;
1330 backend_private
= call_private
->public.backend_private
;
1332 if ( !sipe_backend_media_is_initiator(backend_private
, NULL
)
1333 && !sipe_backend_media_accepted(backend_private
)) {
1334 sip_transport_response(call_private
->sipe_private
,
1335 call_private
->invitation
,
1336 480, "Temporarily Unavailable", NULL
);
1338 struct sip_session
*session
;
1340 session
= sipe_session_find_call(call_private
->sipe_private
,
1341 call_private
->with
);
1343 sipe_session_close(call_private
->sipe_private
, session
);
1346 sipe_media_hangup(call_private
);
1349 gboolean
sipe_media_is_conference_call(struct sipe_media_call_private
*call_private
)
1351 return g_strstr_len(call_private
->with
, -1, "app:conf:audio-video:") != NULL
;
1355 sipe_media_relay_free(struct sipe_media_relay
*relay
)
1357 g_free(relay
->hostname
);
1358 if (relay
->dns_query
)
1359 sipe_backend_dns_query_cancel(relay
->dns_query
);
1364 sipe_media_relay_list_free(GSList
*list
)
1366 for (; list
; list
= g_slist_delete_link(list
, list
))
1367 sipe_media_relay_free(list
->data
);
1371 relay_ip_resolved_cb(struct sipe_media_relay
* relay
,
1372 const gchar
*ip
, SIPE_UNUSED_PARAMETER guint port
)
1374 gchar
*hostname
= relay
->hostname
;
1375 relay
->dns_query
= NULL
;
1378 relay
->hostname
= g_strdup(ip
);
1379 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname
, ip
);
1381 relay
->hostname
= NULL
;
1382 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname
);
1389 process_get_av_edge_credentials_response(struct sipe_core_private
*sipe_private
,
1391 SIPE_UNUSED_PARAMETER
struct transaction
*trans
)
1393 g_free(sipe_private
->media_relay_username
);
1394 g_free(sipe_private
->media_relay_password
);
1395 sipe_media_relay_list_free(sipe_private
->media_relays
);
1396 sipe_private
->media_relay_username
= NULL
;
1397 sipe_private
->media_relay_password
= NULL
;
1398 sipe_private
->media_relays
= NULL
;
1400 if (msg
->response
>= 400) {
1401 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
1402 "Failed to obtain A/V Edge credentials.");
1406 if (msg
->response
== 200) {
1407 sipe_xml
*xn_response
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1409 if (sipe_strequal("OK", sipe_xml_attribute(xn_response
, "reasonPhrase"))) {
1410 const sipe_xml
*xn_credentials
= sipe_xml_child(xn_response
, "credentialsResponse/credentials");
1411 const sipe_xml
*xn_relays
= sipe_xml_child(xn_response
, "credentialsResponse/mediaRelayList");
1412 const sipe_xml
*item
;
1413 GSList
*relays
= NULL
;
1415 item
= sipe_xml_child(xn_credentials
, "username");
1416 sipe_private
->media_relay_username
= sipe_xml_data(item
);
1417 item
= sipe_xml_child(xn_credentials
, "password");
1418 sipe_private
->media_relay_password
= sipe_xml_data(item
);
1420 for (item
= sipe_xml_child(xn_relays
, "mediaRelay"); item
; item
= sipe_xml_twin(item
)) {
1421 struct sipe_media_relay
*relay
= g_new0(struct sipe_media_relay
, 1);
1422 const sipe_xml
*node
;
1425 node
= sipe_xml_child(item
, "hostName");
1426 relay
->hostname
= sipe_xml_data(node
);
1428 node
= sipe_xml_child(item
, "udpPort");
1430 relay
->udp_port
= atoi(tmp
= sipe_xml_data(node
));
1434 node
= sipe_xml_child(item
, "tcpPort");
1436 relay
->tcp_port
= atoi(tmp
= sipe_xml_data(node
));
1440 relays
= g_slist_append(relays
, relay
);
1442 relay
->dns_query
= sipe_backend_dns_query_a(
1446 (sipe_dns_resolved_cb
) relay_ip_resolved_cb
,
1449 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
1451 relay
->tcp_port
, relay
->udp_port
);
1454 sipe_private
->media_relays
= relays
;
1457 sipe_xml_free(xn_response
);
1464 sipe_media_get_av_edge_credentials(struct sipe_core_private
*sipe_private
)
1466 // TODO: re-request credentials after duration expires?
1467 const char CRED_REQUEST_XML
[] =
1468 "<request requestID=\"%d\" "
1472 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
1473 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
1474 "<credentialsRequest credentialsRequestID=\"%d\">"
1475 "<identity>%s</identity>"
1476 "<location>%s</location>"
1477 "<duration>480</duration>"
1478 "</credentialsRequest>"
1481 int request_id
= rand();
1485 if (!sipe_private
->mras_uri
)
1488 self
= sip_uri_self(sipe_private
);
1490 body
= g_strdup_printf(
1494 sipe_private
->mras_uri
,
1497 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER
) ? "internet" : "intranet");
1500 sip_transport_service(sipe_private
,
1501 sipe_private
->mras_uri
,
1502 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
1504 process_get_av_edge_credentials_response
);