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-conf.h"
41 #include "sipe-core.h"
42 #include "sipe-core-private.h"
43 #include "sipe-dialog.h"
44 #include "sipe-media.h"
45 #include "sipe-ocs2007.h"
46 #include "sipe-session.h"
47 #include "sipe-utils.h"
49 #include "sipe-schedule.h"
52 struct sipe_media_call_private
{
53 struct sipe_media_call
public;
55 /* private part starts here */
56 struct sipe_core_private
*sipe_private
;
59 struct sipmsg
*invitation
;
60 SipeIceVersion ice_version
;
61 gboolean encryption_compatible
;
66 #define SIPE_MEDIA_CALL ((struct sipe_media_call *) call_private)
67 #define SIPE_MEDIA_CALL_PRIVATE ((struct sipe_media_call_private *) call)
69 static void sipe_media_codec_list_free(GList
*codecs
)
71 for (; codecs
; codecs
= g_list_delete_link(codecs
, codecs
))
72 sipe_backend_codec_free(codecs
->data
);
75 static void sipe_media_candidate_list_free(GList
*candidates
)
77 for (; candidates
; candidates
= g_list_delete_link(candidates
, candidates
))
78 sipe_backend_candidate_free(candidates
->data
);
82 sipe_media_call_free(struct sipe_media_call_private
*call_private
)
85 struct sip_session
*session
;
86 sipe_backend_media_free(call_private
->public.backend_private
);
88 session
= sipe_session_find_call(call_private
->sipe_private
,
91 sipe_session_remove(call_private
->sipe_private
, session
);
93 if (call_private
->invitation
)
94 sipmsg_free(call_private
->invitation
);
96 sdpmsg_free(call_private
->smsg
);
97 sipe_utils_slist_free_full(call_private
->failed_media
,
98 (GDestroyNotify
)sdpmedia_free
);
99 g_free(call_private
->with
);
100 g_free(call_private
);
105 candidate_sort_cb(struct sdpcandidate
*c1
, struct sdpcandidate
*c2
)
107 int cmp
= sipe_strcompare(c1
->foundation
, c2
->foundation
);
109 cmp
= sipe_strcompare(c1
->username
, c2
->username
);
111 cmp
= c1
->component
- c2
->component
;
118 backend_candidates_to_sdpcandidate(GList
*candidates
)
120 GSList
*result
= NULL
;
123 for (i
= candidates
; i
; i
= i
->next
) {
124 struct sipe_backend_candidate
*candidate
= i
->data
;
125 struct sdpcandidate
*c
= g_new(struct sdpcandidate
, 1);
127 c
->foundation
= sipe_backend_candidate_get_foundation(candidate
);
128 c
->component
= sipe_backend_candidate_get_component_type(candidate
);
129 c
->type
= sipe_backend_candidate_get_type(candidate
);
130 c
->protocol
= sipe_backend_candidate_get_protocol(candidate
);
131 c
->ip
= sipe_backend_candidate_get_ip(candidate
);
132 c
->port
= sipe_backend_candidate_get_port(candidate
);
133 c
->base_ip
= sipe_backend_candidate_get_base_ip(candidate
);
134 c
->base_port
= sipe_backend_candidate_get_base_port(candidate
);
135 c
->priority
= sipe_backend_candidate_get_priority(candidate
);
136 c
->username
= sipe_backend_candidate_get_username(candidate
);
137 c
->password
= sipe_backend_candidate_get_password(candidate
);
139 result
= g_slist_insert_sorted(result
, c
,
140 (GCompareFunc
)candidate_sort_cb
);
147 get_stream_ip_and_ports(GSList
*candidates
,
148 gchar
**ip
, guint
*rtp_port
, guint
*rtcp_port
,
149 SipeCandidateType type
)
155 for (; candidates
; candidates
= candidates
->next
) {
156 struct sdpcandidate
*candidate
= candidates
->data
;
158 if (type
== SIPE_CANDIDATE_TYPE_ANY
|| candidate
->type
== type
) {
160 *ip
= g_strdup(candidate
->ip
);
161 } else if (!sipe_strequal(*ip
, candidate
->ip
)) {
165 if (candidate
->component
== SIPE_COMPONENT_RTP
) {
166 *rtp_port
= candidate
->port
;
167 } else if (candidate
->component
== SIPE_COMPONENT_RTCP
)
168 *rtcp_port
= candidate
->port
;
171 if (*rtp_port
!= 0 && *rtcp_port
!= 0)
177 sdpcodec_compare(gconstpointer a
, gconstpointer b
)
179 return ((const struct sdpcodec
*)a
)->id
-
180 ((const struct sdpcodec
*)b
)->id
;
183 static struct sdpmedia
*
184 backend_stream_to_sdpmedia(struct sipe_backend_media
*backend_media
,
185 struct sipe_backend_stream
*backend_stream
)
187 struct sdpmedia
*media
= g_new0(struct sdpmedia
, 1);
188 GList
*codecs
= sipe_backend_get_local_codecs(backend_media
,
192 GSList
*attributes
= NULL
;
196 media
->name
= g_strdup(sipe_backend_stream_get_id(backend_stream
));
198 if (sipe_strequal(media
->name
, "audio"))
199 type
= SIPE_MEDIA_AUDIO
;
200 else if (sipe_strequal(media
->name
, "video"))
201 type
= SIPE_MEDIA_VIDEO
;
203 // TODO: incompatible media, should not happen here
206 sipe_media_codec_list_free(codecs
);
211 for (i
= codecs
; i
; i
= i
->next
) {
212 struct sipe_backend_codec
*codec
= i
->data
;
213 struct sdpcodec
*c
= g_new0(struct sdpcodec
, 1);
216 c
->id
= sipe_backend_codec_get_id(codec
);
217 c
->name
= sipe_backend_codec_get_name(codec
);
218 c
->clock_rate
= sipe_backend_codec_get_clock_rate(codec
);
221 params
= sipe_backend_codec_get_optional_parameters(codec
);
222 for (; params
; params
= params
->next
) {
223 struct sipnameval
*param
= params
->data
;
224 struct sipnameval
*copy
= g_new0(struct sipnameval
, 1);
226 copy
->name
= g_strdup(param
->name
);
227 copy
->value
= g_strdup(param
->value
);
229 c
->parameters
= g_slist_append(c
->parameters
, copy
);
232 /* Buggy(?) codecs may report non-unique id (a.k.a. payload
233 * type) that must not appear in SDP messages we send. Thus,
234 * let's ignore any codec having the same id as one we already
235 * have in the converted list. */
236 media
->codecs
= sipe_utils_slist_insert_unique_sorted(
237 media
->codecs
, c
, sdpcodec_compare
,
238 (GDestroyNotify
)sdpcodec_free
);
241 sipe_media_codec_list_free(codecs
);
243 // Process local candidates
244 // If we have established candidate pairs, send them in SDP response.
245 // Otherwise send all available local candidates.
246 candidates
= sipe_backend_media_get_active_local_candidates(backend_media
,
249 candidates
= sipe_backend_get_local_candidates(backend_media
,
252 media
->candidates
= backend_candidates_to_sdpcandidate(candidates
);
254 sipe_media_candidate_list_free(candidates
);
256 get_stream_ip_and_ports(media
->candidates
, &media
->ip
, &media
->port
,
257 &rtcp_port
, SIPE_CANDIDATE_TYPE_HOST
);
258 // No usable HOST candidates, use any candidate
259 if (media
->ip
== NULL
&& media
->candidates
) {
260 get_stream_ip_and_ports(media
->candidates
, &media
->ip
, &media
->port
,
261 &rtcp_port
, SIPE_CANDIDATE_TYPE_ANY
);
264 if (sipe_backend_stream_is_held(backend_stream
))
265 attributes
= sipe_utils_nameval_add(attributes
, "inactive", "");
268 gchar
*tmp
= g_strdup_printf("%u", rtcp_port
);
269 attributes
= sipe_utils_nameval_add(attributes
, "rtcp", tmp
);
273 attributes
= sipe_utils_nameval_add(attributes
, "encryption", "rejected");
275 media
->attributes
= attributes
;
277 // Process remote candidates
278 candidates
= sipe_backend_media_get_active_remote_candidates(backend_media
,
280 media
->remote_candidates
= backend_candidates_to_sdpcandidate(candidates
);
281 sipe_media_candidate_list_free(candidates
);
286 static struct sdpmsg
*
287 sipe_media_to_sdpmsg(struct sipe_media_call_private
*call_private
)
289 struct sipe_backend_media
*backend_media
= call_private
->public.backend_private
;
290 struct sdpmsg
*msg
= g_new0(struct sdpmsg
, 1);
291 GSList
*streams
= sipe_backend_media_get_streams(backend_media
);
293 for (; streams
; streams
= streams
->next
) {
294 struct sdpmedia
*media
= backend_stream_to_sdpmedia(backend_media
, streams
->data
);
296 msg
->media
= g_slist_append(msg
->media
, media
);
299 msg
->ip
= g_strdup(media
->ip
);
303 msg
->media
= g_slist_concat(msg
->media
, call_private
->failed_media
);
304 call_private
->failed_media
= NULL
;
306 msg
->ice_version
= call_private
->ice_version
;
312 sipe_invite_call(struct sipe_core_private
*sipe_private
, TransCallback tc
)
316 gchar
*p_preferred_identity
= NULL
;
318 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
319 struct sip_session
*session
;
320 struct sip_dialog
*dialog
;
322 gboolean add_2007_fallback
= FALSE
;
324 session
= sipe_session_find_call(sipe_private
, call_private
->with
);
325 dialog
= session
->dialogs
->data
;
326 add_2007_fallback
= dialog
->cseq
== 0 &&
327 call_private
->ice_version
== SIPE_ICE_RFC_5245
&&
328 !sipe_strequal(call_private
->with
, sipe_private
->test_call_bot_uri
);
330 contact
= get_contact(sipe_private
);
332 if (sipe_private
->uc_line_uri
) {
333 gchar
*self
= sip_uri_self(sipe_private
);
334 p_preferred_identity
= g_strdup_printf(
335 "P-Preferred-Identity: <%s>, <%s>\r\n",
336 self
, sipe_private
->uc_line_uri
);
340 hdr
= g_strdup_printf(
341 "ms-keep-alive: UAC;hop-hop=yes\r\n"
344 "Content-Type: %s\r\n",
346 p_preferred_identity
? p_preferred_identity
: "",
348 "multipart/alternative;boundary=\"----=_NextPart_000_001E_01CB4397.0B5EB570\""
349 : "application/sdp");
351 g_free(p_preferred_identity
);
353 msg
= sipe_media_to_sdpmsg(call_private
);
354 body
= sdpmsg_to_string(msg
);
356 if (add_2007_fallback
) {
358 tmp
= g_strdup_printf(
359 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
360 "Content-Type: application/sdp\r\n"
361 "Content-Transfer-Encoding: 7bit\r\n"
362 "Content-Disposition: session; handling=optional; ms-proxy-2007fallback\r\n"
364 "o=- 0 0 IN IP4 %s\r\n"
367 "m=audio 0 RTP/AVP\r\n"
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\r\n"
376 "------=_NextPart_000_001E_01CB4397.0B5EB570--\r\n",
377 msg
->ip
, msg
->ip
, body
);
384 dialog
->outgoing_invite
= sip_transport_invite(sipe_private
,
394 static struct sip_dialog
*
395 sipe_media_dialog_init(struct sip_session
* session
, struct sipmsg
*msg
)
397 gchar
*newTag
= gentag();
398 const gchar
*oldHeader
;
400 struct sip_dialog
*dialog
;
402 oldHeader
= sipmsg_find_header(msg
, "To");
403 newHeader
= g_strdup_printf("%s;tag=%s", oldHeader
, newTag
);
404 sipmsg_remove_header_now(msg
, "To");
405 sipmsg_add_header_now(msg
, "To", newHeader
);
408 dialog
= sipe_dialog_add(session
);
409 dialog
->callid
= g_strdup(sipmsg_find_header(msg
, "Call-ID"));
410 dialog
->with
= parse_from(sipmsg_find_header(msg
, "From"));
411 sipe_dialog_parse(dialog
, msg
, FALSE
);
417 send_response_with_session_description(struct sipe_media_call_private
*call_private
, int code
, gchar
*text
)
419 struct sdpmsg
*msg
= sipe_media_to_sdpmsg(call_private
);
420 gchar
*body
= sdpmsg_to_string(msg
);
422 sipmsg_add_header(call_private
->invitation
, "Content-Type", "application/sdp");
423 sip_transport_response(call_private
->sipe_private
, call_private
->invitation
, code
, text
, body
);
428 encryption_levels_compatible(struct sdpmsg
*msg
)
432 for (i
= msg
->media
; i
; i
= i
->next
) {
433 const gchar
*enc_level
;
434 struct sdpmedia
*m
= i
->data
;
436 enc_level
= sipe_utils_nameval_find(m
->attributes
, "encryption");
438 // Decline call if peer requires encryption as we don't support it yet.
439 if (sipe_strequal(enc_level
, "required"))
447 process_invite_call_response(struct sipe_core_private
*sipe_private
,
449 struct transaction
*trans
);
452 update_remote_media(struct sipe_media_call_private
* call_private
,
453 struct sdpmedia
*media
)
455 struct sipe_backend_media
*backend_media
= SIPE_MEDIA_CALL
->backend_private
;
456 struct sipe_backend_stream
*backend_stream
;
457 GList
*backend_candidates
= NULL
;
458 GList
*backend_codecs
= NULL
;
460 gboolean result
= TRUE
;
462 backend_stream
= sipe_backend_media_get_stream_by_id(backend_media
,
464 if (media
->port
== 0) {
466 sipe_backend_media_remove_stream(backend_media
, backend_stream
);
473 for (i
= media
->codecs
; i
; i
= i
->next
) {
474 struct sdpcodec
*c
= i
->data
;
475 struct sipe_backend_codec
*codec
;
478 codec
= sipe_backend_codec_new(c
->id
,
483 for (j
= c
->parameters
; j
; j
= j
->next
) {
484 struct sipnameval
*attr
= j
->data
;
486 sipe_backend_codec_add_optional_parameter(codec
,
491 backend_codecs
= g_list_append(backend_codecs
, codec
);
494 result
= sipe_backend_set_remote_codecs(backend_media
,
497 sipe_media_codec_list_free(backend_codecs
);
499 if (result
== FALSE
) {
500 sipe_backend_media_remove_stream(backend_media
, backend_stream
);
504 for (i
= media
->candidates
; i
; i
= i
->next
) {
505 struct sdpcandidate
*c
= i
->data
;
506 struct sipe_backend_candidate
*candidate
;
507 candidate
= sipe_backend_candidate_new(c
->foundation
,
515 sipe_backend_candidate_set_priority(candidate
, c
->priority
);
517 backend_candidates
= g_list_append(backend_candidates
, candidate
);
520 sipe_backend_media_add_remote_candidates(backend_media
,
523 sipe_media_candidate_list_free(backend_candidates
);
525 if (sipe_utils_nameval_find(media
->attributes
, "inactive")) {
526 sipe_backend_stream_hold(backend_media
, backend_stream
, FALSE
);
527 } else if (sipe_backend_stream_is_held(backend_stream
)) {
528 sipe_backend_stream_unhold(backend_media
, backend_stream
, FALSE
);
535 apply_remote_message(struct sipe_media_call_private
* call_private
,
540 sipe_utils_slist_free_full(call_private
->failed_media
, (GDestroyNotify
)sdpmedia_free
);
541 call_private
->failed_media
= NULL
;
543 for (i
= msg
->media
; i
; i
= i
->next
) {
544 struct sdpmedia
*media
= i
->data
;
545 if (!update_remote_media(call_private
, media
)) {
547 call_private
->failed_media
=
548 g_slist_append(call_private
->failed_media
, media
);
552 /* We need to keep failed medias until response is sent, remove them
553 * from sdpmsg that is to be freed. */
554 for (i
= call_private
->failed_media
; i
; i
= i
->next
) {
555 msg
->media
= g_slist_remove(msg
->media
, i
->data
);
558 call_private
->encryption_compatible
= encryption_levels_compatible(msg
);
562 call_initialized(struct sipe_media_call
*call
)
565 sipe_backend_media_get_streams(call
->backend_private
);
567 for (; streams
; streams
= streams
->next
) {
568 if (!sipe_backend_stream_initialized(call
->backend_private
,
577 // Sends an invite response when the call is accepted and local candidates were
578 // prepared, otherwise does nothing. If error response is sent, call_private is
579 // disposed before function returns. Returns true when response was sent.
581 send_invite_response_if_ready(struct sipe_media_call_private
*call_private
)
583 struct sipe_backend_media
*backend_media
;
585 backend_media
= call_private
->public.backend_private
;
587 if (!sipe_backend_media_accepted(backend_media
) ||
588 !call_initialized(&call_private
->public))
591 if (!call_private
->encryption_compatible
) {
592 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
594 sipmsg_add_header(call_private
->invitation
, "Warning",
595 "308 lcs.microsoft.com \"Encryption Levels not compatible\"");
596 sip_transport_response(sipe_private
,
597 call_private
->invitation
,
598 488, "Encryption Levels not compatible",
600 sipe_backend_media_reject(backend_media
, FALSE
);
601 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
602 _("Unable to establish a call"),
603 _("Encryption settings of peer are incompatible with ours."));
605 send_response_with_session_description(call_private
, 200, "OK");
612 stream_initialized_cb(struct sipe_media_call
*call
,
613 struct sipe_backend_stream
*stream
)
615 if (call_initialized(call
)) {
616 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
617 struct sipe_backend_media
*backend_private
= call
->backend_private
;
619 if (sipe_backend_media_is_initiator(backend_private
, stream
)) {
620 sipe_invite_call(call_private
->sipe_private
,
621 process_invite_call_response
);
622 } else if (call_private
->smsg
) {
623 struct sdpmsg
*smsg
= call_private
->smsg
;
624 call_private
->smsg
= NULL
;
626 apply_remote_message(call_private
, smsg
);
627 send_invite_response_if_ready(call_private
);
633 static void phone_state_publish(struct sipe_core_private
*sipe_private
)
635 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
636 sipe_ocs2007_phone_state_publish(sipe_private
);
638 // TODO: OCS 2005 support. Is anyone still using it at all?
643 media_end_cb(struct sipe_media_call
*call
)
645 g_return_if_fail(call
);
647 SIPE_MEDIA_CALL_PRIVATE
->sipe_private
->media_call
= NULL
;
648 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
);
649 sipe_media_call_free(SIPE_MEDIA_CALL_PRIVATE
);
653 call_accept_cb(struct sipe_media_call
*call
, gboolean local
)
656 send_invite_response_if_ready(SIPE_MEDIA_CALL_PRIVATE
);
658 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
);
662 call_reject_cb(struct sipe_media_call
*call
, gboolean local
)
665 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
666 sip_transport_response(call_private
->sipe_private
,
667 call_private
->invitation
,
668 603, "Decline", NULL
);
673 sipe_media_send_ack(struct sipe_core_private
*sipe_private
, struct sipmsg
*msg
,
674 struct transaction
*trans
);
676 static void call_hold_cb(struct sipe_media_call
*call
,
678 SIPE_UNUSED_PARAMETER gboolean state
)
681 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
,
682 sipe_media_send_ack
);
685 static void call_hangup_cb(struct sipe_media_call
*call
, gboolean local
)
688 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
689 struct sip_session
*session
;
690 session
= sipe_session_find_call(call_private
->sipe_private
,
694 sipe_session_close(call_private
->sipe_private
, session
);
700 error_cb(struct sipe_media_call
*call
, gchar
*message
)
702 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
703 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
704 gboolean initiator
= sipe_backend_media_is_initiator(call
->backend_private
, NULL
);
705 gboolean accepted
= sipe_backend_media_accepted(call
->backend_private
);
707 gchar
*title
= g_strdup_printf("Call with %s failed", call_private
->with
);
708 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, title
, message
);
711 if (!initiator
&& !accepted
) {
712 sip_transport_response(sipe_private
,
713 call_private
->invitation
,
714 488, "Not Acceptable Here", NULL
);
717 sipe_backend_media_hangup(call
->backend_private
, initiator
|| accepted
);
720 static struct sipe_media_call_private
*
721 sipe_media_call_new(struct sipe_core_private
*sipe_private
,
722 const gchar
* with
, gboolean initiator
, SipeIceVersion ice_version
)
724 struct sipe_media_call_private
*call_private
= g_new0(struct sipe_media_call_private
, 1);
727 call_private
->sipe_private
= sipe_private
;
729 cname
= g_strdup(sipe_private
->contact
+ 1);
730 cname
[strlen(cname
) - 1] = '\0';
732 call_private
->public.backend_private
= sipe_backend_media_new(SIPE_CORE_PUBLIC
,
736 sipe_backend_media_set_cname(call_private
->public.backend_private
, cname
);
738 call_private
->ice_version
= ice_version
;
739 call_private
->encryption_compatible
= TRUE
;
741 call_private
->public.stream_initialized_cb
= stream_initialized_cb
;
742 call_private
->public.media_end_cb
= media_end_cb
;
743 call_private
->public.call_accept_cb
= call_accept_cb
;
744 call_private
->public.call_reject_cb
= call_reject_cb
;
745 call_private
->public.call_hold_cb
= call_hold_cb
;
746 call_private
->public.call_hangup_cb
= call_hangup_cb
;
747 call_private
->public.error_cb
= error_cb
;
754 void sipe_media_hangup(struct sipe_media_call_private
*call_private
)
757 sipe_backend_media_hangup(call_private
->public.backend_private
,
763 sipe_media_initiate_call(struct sipe_core_private
*sipe_private
,
764 const char *with
, SipeIceVersion ice_version
,
767 struct sipe_media_call_private
*call_private
;
768 struct sipe_backend_media
*backend_media
;
769 struct sipe_backend_media_relays
*backend_media_relays
;
770 struct sip_session
*session
;
771 struct sip_dialog
*dialog
;
773 if (sipe_private
->media_call
)
776 call_private
= sipe_media_call_new(sipe_private
, with
, TRUE
, ice_version
);
778 session
= sipe_session_add_call(sipe_private
, with
);
779 dialog
= sipe_dialog_add(session
);
780 dialog
->callid
= gencallid();
781 dialog
->with
= g_strdup(session
->with
);
782 dialog
->ourtag
= gentag();
784 call_private
->with
= g_strdup(session
->with
);
786 backend_media
= call_private
->public.backend_private
;
788 backend_media_relays
=
789 sipe_backend_media_relays_convert(sipe_private
->media_relays
,
790 sipe_private
->media_relay_username
,
791 sipe_private
->media_relay_password
);
793 if (!sipe_backend_media_add_stream(backend_media
,
794 "audio", with
, SIPE_MEDIA_AUDIO
,
795 call_private
->ice_version
, TRUE
,
796 backend_media_relays
)) {
797 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
799 _("Error creating audio stream"));
800 sipe_media_hangup(call_private
);
801 sipe_backend_media_relays_free(backend_media_relays
);
806 && !sipe_backend_media_add_stream(backend_media
,
807 "video", with
, SIPE_MEDIA_VIDEO
,
808 call_private
->ice_version
, TRUE
,
809 backend_media_relays
)) {
810 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
812 _("Error creating video stream"));
813 sipe_media_hangup(call_private
);
814 sipe_backend_media_relays_free(backend_media_relays
);
818 sipe_private
->media_call
= call_private
;
820 sipe_backend_media_relays_free(backend_media_relays
);
822 // Processing continues in stream_initialized_cb
826 sipe_core_media_initiate_call(struct sipe_core_public
*sipe_public
,
830 sipe_media_initiate_call(SIPE_CORE_PRIVATE
, with
,
831 SIPE_ICE_RFC_5245
, with_video
);
834 void sipe_core_media_connect_conference(struct sipe_core_public
*sipe_public
,
835 struct sipe_chat_session
*chat_session
)
837 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
838 struct sipe_backend_media_relays
*backend_media_relays
;
839 struct sip_session
*session
;
840 struct sip_dialog
*dialog
;
841 SipeIceVersion ice_version
;
845 if (!sipe_conf_supports_mcu_type(sipe_private
, "audio-video")) {
846 sipe_backend_notify_error(sipe_public
, _("Join conference call"),
847 _("Conference calls are not supported on this server."));
851 session
= sipe_session_find_chat(sipe_private
, chat_session
);
853 if (sipe_private
->media_call
|| !session
)
856 session
->is_call
= TRUE
;
858 parts
= g_strsplit(chat_session
->id
, "app:conf:focus:", 2);
859 av_uri
= g_strjoinv("app:conf:audio-video:", parts
);
862 ice_version
= SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013
) ? SIPE_ICE_RFC_5245
:
865 sipe_private
->media_call
= sipe_media_call_new(sipe_private
, av_uri
,
868 session
= sipe_session_add_call(sipe_private
, av_uri
);
869 dialog
= sipe_dialog_add(session
);
870 dialog
->callid
= gencallid();
871 dialog
->with
= g_strdup(session
->with
);
872 dialog
->ourtag
= gentag();
876 sipe_private
->media_call
->with
= g_strdup(session
->with
);
878 backend_media_relays
=
879 sipe_backend_media_relays_convert(sipe_private
->media_relays
,
880 sipe_private
->media_relay_username
,
881 sipe_private
->media_relay_password
);
883 if (!sipe_backend_media_add_stream(sipe_private
->media_call
->public.backend_private
,
884 "audio", dialog
->with
,
886 sipe_private
->media_call
->ice_version
,
887 TRUE
, backend_media_relays
)) {
888 sipe_backend_notify_error(sipe_public
,
890 _("Error creating audio stream"));
891 sipe_media_hangup(sipe_private
->media_call
);
892 sipe_private
->media_call
= NULL
;
895 sipe_backend_media_relays_free(backend_media_relays
);
897 // Processing continues in stream_initialized_cb
900 gboolean
sipe_core_media_in_call(struct sipe_core_public
*sipe_public
)
903 return SIPE_CORE_PRIVATE
->media_call
!= NULL
;
908 static gboolean
phone_number_is_valid(const gchar
*phone_number
)
910 if (!phone_number
|| sipe_strequal(phone_number
, "")) {
914 if (*phone_number
== '+') {
918 while (*phone_number
!= '\0') {
919 if (!g_ascii_isdigit(*phone_number
)) {
928 void sipe_core_media_phone_call(struct sipe_core_public
*sipe_public
,
929 const gchar
*phone_number
)
931 g_return_if_fail(sipe_public
);
933 if (phone_number_is_valid(phone_number
)) {
934 gchar
*phone_uri
= g_strdup_printf("sip:%s@%s;user=phone",
935 phone_number
, sipe_public
->sip_domain
);
937 sipe_core_media_initiate_call(sipe_public
, phone_uri
, FALSE
);
941 sipe_backend_notify_error(sipe_public
,
942 _("Unable to establish a call"),
943 _("Invalid phone number"));
947 void sipe_core_media_test_call(struct sipe_core_public
*sipe_public
)
949 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
950 if (!sipe_private
->test_call_bot_uri
) {
951 sipe_backend_notify_error(sipe_public
,
952 _("Unable to establish a call"),
953 _("Audio Test Service is not available."));
957 sipe_core_media_initiate_call(sipe_public
,
958 sipe_private
->test_call_bot_uri
, FALSE
);
962 process_incoming_invite_call(struct sipe_core_private
*sipe_private
,
965 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
966 struct sipe_backend_media
*backend_media
;
967 struct sipe_backend_media_relays
*backend_media_relays
= NULL
;
969 gboolean has_new_media
= FALSE
;
975 if (!is_media_session_msg(call_private
, msg
)) {
976 sip_transport_response(sipe_private
, msg
, 486, "Busy Here", NULL
);
980 self
= sip_uri_self(sipe_private
);
981 if (sipe_strequal(call_private
->with
, self
)) {
983 sip_transport_response(sipe_private
, msg
, 488, "Not Acceptable Here", NULL
);
989 smsg
= sdpmsg_parse_msg(msg
->body
);
991 sip_transport_response(sipe_private
, msg
,
992 488, "Not Acceptable Here", NULL
);
993 sipe_media_hangup(call_private
);
998 gchar
*with
= parse_from(sipmsg_find_header(msg
, "From"));
999 struct sip_session
*session
;
1001 call_private
= sipe_media_call_new(sipe_private
, with
, FALSE
, smsg
->ice_version
);
1002 session
= sipe_session_add_call(sipe_private
, with
);
1003 sipe_media_dialog_init(session
, msg
);
1005 call_private
->with
= g_strdup(session
->with
);
1006 sipe_private
->media_call
= call_private
;
1010 backend_media
= call_private
->public.backend_private
;
1012 if (call_private
->invitation
)
1013 sipmsg_free(call_private
->invitation
);
1014 call_private
->invitation
= sipmsg_copy(msg
);
1017 backend_media_relays
= sipe_backend_media_relays_convert(
1018 sipe_private
->media_relays
,
1019 sipe_private
->media_relay_username
,
1020 sipe_private
->media_relay_password
);
1022 // Create any new media streams
1023 for (i
= smsg
->media
; i
; i
= i
->next
) {
1024 struct sdpmedia
*media
= i
->data
;
1025 gchar
*id
= media
->name
;
1028 if ( media
->port
!= 0
1029 && !sipe_backend_media_get_stream_by_id(backend_media
, id
)) {
1032 if (sipe_strequal(id
, "audio"))
1033 type
= SIPE_MEDIA_AUDIO
;
1034 else if (sipe_strequal(id
, "video"))
1035 type
= SIPE_MEDIA_VIDEO
;
1039 with
= parse_from(sipmsg_find_header(msg
, "From"));
1040 sipe_backend_media_add_stream(backend_media
, id
, with
,
1044 backend_media_relays
);
1045 has_new_media
= TRUE
;
1050 sipe_backend_media_relays_free(backend_media_relays
);
1052 if (has_new_media
) {
1053 sdpmsg_free(call_private
->smsg
);
1054 call_private
->smsg
= smsg
;
1055 sip_transport_response(sipe_private
, call_private
->invitation
,
1056 180, "Ringing", NULL
);
1057 // Processing continues in stream_initialized_cb
1059 apply_remote_message(call_private
, smsg
);
1060 send_response_with_session_description(call_private
, 200, "OK");
1066 void process_incoming_cancel_call(struct sipe_core_private
*sipe_private
,
1069 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
1071 // We respond to the CANCEL request with 200 OK response and
1072 // with 487 Request Terminated to the remote INVITE in progress.
1073 sip_transport_response(sipe_private
, msg
, 200, "OK", NULL
);
1075 if (call_private
->invitation
) {
1076 sip_transport_response(sipe_private
, call_private
->invitation
,
1077 487, "Request Terminated", NULL
);
1080 sipe_media_hangup(call_private
);
1084 sipe_media_send_ack(struct sipe_core_private
*sipe_private
,
1086 struct transaction
*trans
)
1088 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
1089 struct sip_session
*session
;
1090 struct sip_dialog
*dialog
;
1093 if (!is_media_session_msg(call_private
, msg
))
1096 session
= sipe_session_find_call(sipe_private
, call_private
->with
);
1097 dialog
= session
->dialogs
->data
;
1101 tmp_cseq
= dialog
->cseq
;
1103 dialog
->cseq
= sip_transaction_cseq(trans
) - 1;
1104 sip_transport_ack(sipe_private
, dialog
);
1105 dialog
->cseq
= tmp_cseq
;
1107 dialog
->outgoing_invite
= NULL
;
1113 sipe_media_send_final_ack(struct sipe_core_private
*sipe_private
,
1115 struct transaction
*trans
)
1117 if (!sipe_media_send_ack(sipe_private
, msg
, trans
))
1120 sipe_backend_media_accept(sipe_private
->media_call
->public.backend_private
,
1127 reinvite_on_candidate_pair_cb(struct sipe_core_public
*sipe_public
)
1129 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1130 struct sipe_media_call_private
*media_call
= sipe_private
->media_call
;
1131 struct sipe_backend_media
*backend_media
;
1137 backend_media
= media_call
->public.backend_private
;
1138 streams
= sipe_backend_media_get_streams(backend_media
);
1140 for (; streams
; streams
= streams
->next
) {
1141 struct sipe_backend_stream
*s
= streams
->data
;
1142 GList
*remote_candidates
= sipe_backend_media_get_active_remote_candidates(backend_media
, s
);
1143 guint components
= g_list_length(remote_candidates
);
1145 sipe_media_candidate_list_free(remote_candidates
);
1147 // We must have candidates for both (RTP + RTCP) components ready
1148 if (components
< 2) {
1149 sipe_schedule_mseconds(sipe_private
,
1150 "<+media-reinvite-on-candidate-pair>",
1153 (sipe_schedule_action
) reinvite_on_candidate_pair_cb
,
1159 sipe_invite_call(sipe_private
, sipe_media_send_final_ack
);
1163 maybe_retry_call_with_ice_version(struct sipe_core_private
*sipe_private
,
1164 SipeIceVersion ice_version
,
1165 struct transaction
*trans
)
1167 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
1169 if (call_private
->ice_version
!= ice_version
&&
1170 sip_transaction_cseq(trans
) == 1) {
1171 gchar
*with
= g_strdup(call_private
->with
);
1172 struct sipe_backend_media
*backend_private
= call_private
->public.backend_private
;
1173 gboolean with_video
= sipe_backend_media_get_stream_by_id(backend_private
, "video") != NULL
;
1175 sipe_media_hangup(call_private
);
1176 SIPE_DEBUG_INFO("Retrying call with ICEv%d.",
1177 ice_version
== SIPE_ICE_DRAFT_6
? 6 : 19);
1178 sipe_media_initiate_call(sipe_private
, with
, ice_version
,
1189 process_invite_call_response(struct sipe_core_private
*sipe_private
,
1191 struct transaction
*trans
)
1194 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
1195 struct sip_session
*session
;
1196 struct sip_dialog
*dialog
;
1197 struct sdpmsg
*smsg
;
1199 if (!is_media_session_msg(call_private
, msg
))
1202 session
= sipe_session_find_call(sipe_private
, call_private
->with
);
1203 dialog
= session
->dialogs
->data
;
1205 with
= dialog
->with
;
1207 dialog
->outgoing_invite
= NULL
;
1209 if (msg
->response
>= 400) {
1210 // Call rejected by remote peer or an error occurred
1212 GString
*desc
= g_string_new("");
1213 gboolean append_responsestr
= FALSE
;
1215 switch (msg
->response
) {
1217 title
= _("User unavailable");
1219 if (sipmsg_parse_warning(msg
, NULL
) == 391) {
1220 g_string_append_printf(desc
, _("%s does not want to be disturbed"), with
);
1222 g_string_append_printf(desc
, _("User %s is not available"), with
);
1227 title
= _("Call rejected");
1228 g_string_append_printf(desc
, _("User %s rejected call"), with
);
1231 // OCS/Lync really sends response string with 'Mutipart' typo.
1232 if (sipe_strequal(msg
->responsestr
, "Mutipart mime in content type not supported by Archiving CDR service") &&
1233 maybe_retry_call_with_ice_version(sipe_private
, SIPE_ICE_DRAFT_6
, trans
)) {
1236 title
= _("Unsupported media type");
1239 /* Check for incompatible encryption levels error.
1242 * 488 Not Acceptable Here
1243 * ms-client-diagnostics: 52017;reason="Encryption levels dont match"
1245 * older clients (and SIPE itself):
1246 * 488 Encryption Levels not compatible
1248 const gchar
*ms_diag
= sipmsg_find_header(msg
, "ms-client-diagnostics");
1249 SipeIceVersion retry_ice_version
= SIPE_ICE_DRAFT_6
;
1251 if (sipe_strequal(msg
->responsestr
, "Encryption Levels not compatible") ||
1252 (ms_diag
&& g_str_has_prefix(ms_diag
, "52017;"))) {
1253 title
= _("Unable to establish a call");
1254 g_string_append(desc
, _("Encryption settings of peer are incompatible with ours."));
1258 /* Check if this is failed conference using
1259 * ICEv6 with reason "Error parsing SDP" and
1260 * retry using ICEv19. */
1261 ms_diag
= sipmsg_find_header(msg
, "ms-diagnostics");
1262 if (ms_diag
&& g_str_has_prefix(ms_diag
, "7008;")) {
1263 retry_ice_version
= SIPE_ICE_RFC_5245
;
1266 if (maybe_retry_call_with_ice_version(sipe_private
, retry_ice_version
, trans
)) {
1269 // Break intentionally omitted
1272 title
= _("Error occured");
1273 g_string_append(desc
, _("Unable to establish a call"));
1274 append_responsestr
= TRUE
;
1278 if (append_responsestr
) {
1279 gchar
*reason
= sipmsg_get_ms_diagnostics_reason(msg
);
1281 g_string_append_printf(desc
, "\n%d %s",
1282 msg
->response
, msg
->responsestr
);
1284 g_string_append_printf(desc
, "\n\n%s", reason
);
1289 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, title
, desc
->str
);
1290 g_string_free(desc
, TRUE
);
1292 sipe_media_send_ack(sipe_private
, msg
, trans
);
1293 sipe_media_hangup(call_private
);
1298 sipe_dialog_parse(dialog
, msg
, TRUE
);
1299 smsg
= sdpmsg_parse_msg(msg
->body
);
1301 sip_transport_response(sipe_private
, msg
,
1302 488, "Not Acceptable Here", NULL
);
1303 sipe_media_hangup(call_private
);
1307 apply_remote_message(call_private
, smsg
);
1310 sipe_media_send_ack(sipe_private
, msg
, trans
);
1311 reinvite_on_candidate_pair_cb(SIPE_CORE_PUBLIC
);
1316 gboolean
is_media_session_msg(struct sipe_media_call_private
*call_private
,
1320 const gchar
*callid
= sipmsg_find_header(msg
, "Call-ID");
1321 struct sip_session
*session
;
1323 session
= sipe_session_find_call(call_private
->sipe_private
,
1324 call_private
->with
);
1326 struct sip_dialog
*dialog
= session
->dialogs
->data
;
1327 return sipe_strequal(dialog
->callid
, callid
);
1333 void sipe_media_handle_going_offline(struct sipe_media_call_private
*call_private
)
1335 struct sipe_backend_media
*backend_private
;
1337 backend_private
= call_private
->public.backend_private
;
1339 if ( !sipe_backend_media_is_initiator(backend_private
, NULL
)
1340 && !sipe_backend_media_accepted(backend_private
)) {
1341 sip_transport_response(call_private
->sipe_private
,
1342 call_private
->invitation
,
1343 480, "Temporarily Unavailable", NULL
);
1345 struct sip_session
*session
;
1347 session
= sipe_session_find_call(call_private
->sipe_private
,
1348 call_private
->with
);
1350 sipe_session_close(call_private
->sipe_private
, session
);
1353 sipe_media_hangup(call_private
);
1356 gboolean
sipe_media_is_conference_call(struct sipe_media_call_private
*call_private
)
1358 return g_strstr_len(call_private
->with
, -1, "app:conf:audio-video:") != NULL
;
1362 sipe_media_relay_free(struct sipe_media_relay
*relay
)
1364 g_free(relay
->hostname
);
1365 if (relay
->dns_query
)
1366 sipe_backend_dns_query_cancel(relay
->dns_query
);
1371 sipe_media_relay_list_free(GSList
*list
)
1373 for (; list
; list
= g_slist_delete_link(list
, list
))
1374 sipe_media_relay_free(list
->data
);
1378 relay_ip_resolved_cb(struct sipe_media_relay
* relay
,
1379 const gchar
*ip
, SIPE_UNUSED_PARAMETER guint port
)
1381 gchar
*hostname
= relay
->hostname
;
1382 relay
->dns_query
= NULL
;
1385 relay
->hostname
= g_strdup(ip
);
1386 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname
, ip
);
1388 relay
->hostname
= NULL
;
1389 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname
);
1396 process_get_av_edge_credentials_response(struct sipe_core_private
*sipe_private
,
1398 SIPE_UNUSED_PARAMETER
struct transaction
*trans
)
1400 g_free(sipe_private
->media_relay_username
);
1401 g_free(sipe_private
->media_relay_password
);
1402 sipe_media_relay_list_free(sipe_private
->media_relays
);
1403 sipe_private
->media_relay_username
= NULL
;
1404 sipe_private
->media_relay_password
= NULL
;
1405 sipe_private
->media_relays
= NULL
;
1407 if (msg
->response
>= 400) {
1408 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
1409 "Failed to obtain A/V Edge credentials.");
1413 if (msg
->response
== 200) {
1414 sipe_xml
*xn_response
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1416 if (sipe_strequal("OK", sipe_xml_attribute(xn_response
, "reasonPhrase"))) {
1417 const sipe_xml
*xn_credentials
= sipe_xml_child(xn_response
, "credentialsResponse/credentials");
1418 const sipe_xml
*xn_relays
= sipe_xml_child(xn_response
, "credentialsResponse/mediaRelayList");
1419 const sipe_xml
*item
;
1420 GSList
*relays
= NULL
;
1422 item
= sipe_xml_child(xn_credentials
, "username");
1423 sipe_private
->media_relay_username
= sipe_xml_data(item
);
1424 item
= sipe_xml_child(xn_credentials
, "password");
1425 sipe_private
->media_relay_password
= sipe_xml_data(item
);
1427 for (item
= sipe_xml_child(xn_relays
, "mediaRelay"); item
; item
= sipe_xml_twin(item
)) {
1428 struct sipe_media_relay
*relay
= g_new0(struct sipe_media_relay
, 1);
1429 const sipe_xml
*node
;
1432 node
= sipe_xml_child(item
, "hostName");
1433 relay
->hostname
= sipe_xml_data(node
);
1435 node
= sipe_xml_child(item
, "udpPort");
1437 relay
->udp_port
= atoi(tmp
= sipe_xml_data(node
));
1441 node
= sipe_xml_child(item
, "tcpPort");
1443 relay
->tcp_port
= atoi(tmp
= sipe_xml_data(node
));
1447 relays
= g_slist_append(relays
, relay
);
1449 relay
->dns_query
= sipe_backend_dns_query_a(
1453 (sipe_dns_resolved_cb
) relay_ip_resolved_cb
,
1456 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
1458 relay
->tcp_port
, relay
->udp_port
);
1461 sipe_private
->media_relays
= relays
;
1464 sipe_xml_free(xn_response
);
1471 sipe_media_get_av_edge_credentials(struct sipe_core_private
*sipe_private
)
1473 // TODO: re-request credentials after duration expires?
1474 const char CRED_REQUEST_XML
[] =
1475 "<request requestID=\"%d\" "
1479 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
1480 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
1481 "<credentialsRequest credentialsRequestID=\"%d\">"
1482 "<identity>%s</identity>"
1483 "<location>%s</location>"
1484 "<duration>480</duration>"
1485 "</credentialsRequest>"
1488 int request_id
= rand();
1492 if (!sipe_private
->mras_uri
)
1495 self
= sip_uri_self(sipe_private
);
1497 body
= g_strdup_printf(
1501 sipe_private
->mras_uri
,
1504 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER
) ? "internet" : "intranet");
1507 sip_transport_service(sipe_private
,
1508 sipe_private
->mras_uri
,
1509 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
1511 process_get_av_edge_credentials_response
);