6 * Copyright (C) 2011-2015 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
;
60 struct sipmsg
*invitation
;
61 SipeIceVersion ice_version
;
62 gboolean encryption_compatible
;
67 #define SIPE_MEDIA_CALL ((struct sipe_media_call *) call_private)
68 #define SIPE_MEDIA_CALL_PRIVATE ((struct sipe_media_call_private *) call)
70 struct sipe_media_stream_private
{
71 struct sipe_media_stream
public;
73 guchar
*encryption_key
;
74 int encryption_key_id
;
75 gboolean remote_candidates_and_codecs_set
;
79 /* User data associated with the stream. */
81 GDestroyNotify data_free_func
;
83 #define SIPE_MEDIA_STREAM ((struct sipe_media_stream *) stream_private)
84 #define SIPE_MEDIA_STREAM_PRIVATE ((struct sipe_media_stream_private *) stream)
86 static void sipe_media_codec_list_free(GList
*codecs
)
88 for (; codecs
; codecs
= g_list_delete_link(codecs
, codecs
))
89 sipe_backend_codec_free(codecs
->data
);
92 static void sipe_media_candidate_list_free(GList
*candidates
)
94 for (; candidates
; candidates
= g_list_delete_link(candidates
, candidates
))
95 sipe_backend_candidate_free(candidates
->data
);
99 remove_stream(struct sipe_media_call
* call
,
100 struct sipe_media_stream_private
*stream_private
)
102 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
104 sipe_media_stream_set_data(SIPE_MEDIA_STREAM
, NULL
, NULL
);
106 call_private
->streams
=
107 g_slist_remove(call_private
->streams
, stream_private
);
108 sipe_backend_media_stream_free(SIPE_MEDIA_STREAM
->backend_private
);
109 g_free(SIPE_MEDIA_STREAM
->id
);
110 g_free(stream_private
->encryption_key
);
111 sipe_utils_nameval_free(stream_private
->extra_sdp
);
112 g_free(stream_private
);
116 call_private_equals(SIPE_UNUSED_PARAMETER
const gchar
*callid
,
117 struct sipe_media_call_private
*call_private1
,
118 struct sipe_media_call_private
*call_private2
)
120 return call_private1
== call_private2
;
124 sipe_media_call_free(struct sipe_media_call_private
*call_private
)
127 struct sip_session
*session
;
129 g_hash_table_foreach_remove(call_private
->sipe_private
->media_calls
,
130 (GHRFunc
) call_private_equals
, call_private
);
132 while (call_private
->streams
) {
133 remove_stream(SIPE_MEDIA_CALL
,
134 call_private
->streams
->data
);
137 sipe_backend_media_free(call_private
->public.backend_private
);
139 session
= sipe_session_find_call(call_private
->sipe_private
,
140 SIPE_MEDIA_CALL
->with
);
142 sipe_session_remove(call_private
->sipe_private
, session
);
144 if (call_private
->invitation
)
145 sipmsg_free(call_private
->invitation
);
147 sdpmsg_free(call_private
->smsg
);
148 sipe_utils_slist_free_full(call_private
->failed_media
,
149 (GDestroyNotify
)sdpmedia_free
);
150 g_free(SIPE_MEDIA_CALL
->with
);
151 g_free(call_private
);
156 candidate_sort_cb(struct sdpcandidate
*c1
, struct sdpcandidate
*c2
)
158 int cmp
= sipe_strcompare(c1
->foundation
, c2
->foundation
);
160 cmp
= sipe_strcompare(c1
->username
, c2
->username
);
162 cmp
= c1
->component
- c2
->component
;
169 backend_candidates_to_sdpcandidate(GList
*candidates
)
171 GSList
*result
= NULL
;
174 for (i
= candidates
; i
; i
= i
->next
) {
175 struct sipe_backend_candidate
*candidate
= i
->data
;
176 struct sdpcandidate
*c
;
178 gchar
*ip
= sipe_backend_candidate_get_ip(candidate
);
179 gchar
*base_ip
= sipe_backend_candidate_get_base_ip(candidate
);
180 if (is_empty(ip
) || strchr(ip
, ':') ||
181 (base_ip
&& strchr(base_ip
, ':'))) {
182 /* Ignore IPv6 candidates. */
188 c
= g_new(struct sdpcandidate
, 1);
189 c
->foundation
= sipe_backend_candidate_get_foundation(candidate
);
190 c
->component
= sipe_backend_candidate_get_component_type(candidate
);
191 c
->type
= sipe_backend_candidate_get_type(candidate
);
192 c
->protocol
= sipe_backend_candidate_get_protocol(candidate
);
194 c
->port
= sipe_backend_candidate_get_port(candidate
);
195 c
->base_ip
= base_ip
;
196 c
->base_port
= sipe_backend_candidate_get_base_port(candidate
);
197 c
->priority
= sipe_backend_candidate_get_priority(candidate
);
198 c
->username
= sipe_backend_candidate_get_username(candidate
);
199 c
->password
= sipe_backend_candidate_get_password(candidate
);
201 result
= g_slist_insert_sorted(result
, c
,
202 (GCompareFunc
)candidate_sort_cb
);
209 get_stream_ip_and_ports(GSList
*candidates
,
210 gchar
**ip
, guint
*rtp_port
, guint
*rtcp_port
,
211 SipeCandidateType type
)
217 for (; candidates
; candidates
= candidates
->next
) {
218 struct sdpcandidate
*candidate
= candidates
->data
;
220 if (type
== SIPE_CANDIDATE_TYPE_ANY
|| candidate
->type
== type
) {
222 *ip
= g_strdup(candidate
->ip
);
223 } else if (!sipe_strequal(*ip
, candidate
->ip
)) {
227 if (candidate
->component
== SIPE_COMPONENT_RTP
) {
228 *rtp_port
= candidate
->port
;
229 } else if (candidate
->component
== SIPE_COMPONENT_RTCP
)
230 *rtcp_port
= candidate
->port
;
233 if (*rtp_port
!= 0 && *rtcp_port
!= 0)
239 sdpcodec_compare(gconstpointer a
, gconstpointer b
)
241 return ((const struct sdpcodec
*)a
)->id
-
242 ((const struct sdpcodec
*)b
)->id
;
246 remove_wrong_farstream_0_1_tcp_candidates(GList
*candidates
)
248 GList
*i
= candidates
;
249 GHashTable
*foundation_to_candidate
= g_hash_table_new_full(g_str_hash
,
255 GList
*next
= i
->next
;
256 struct sipe_backend_candidate
*c1
= i
->data
;
258 if (sipe_backend_candidate_get_protocol(c1
) == SIPE_NETWORK_PROTOCOL_UDP
) {
259 gchar
*foundation
= sipe_backend_candidate_get_foundation(c1
);
260 struct sipe_backend_candidate
*c2
= g_hash_table_lookup(foundation_to_candidate
,
266 if (sipe_backend_candidate_get_port(c1
) ==
267 sipe_backend_candidate_get_port(c2
) ||
268 (sipe_backend_candidate_get_type(c1
) !=
269 SIPE_CANDIDATE_TYPE_HOST
&&
270 sipe_backend_candidate_get_base_port(c1
) ==
271 sipe_backend_candidate_get_base_port(c2
))) {
273 * We assume that RTP+RTCP UDP pairs
274 * that share the same port are
275 * actually mistagged TCP candidates.
277 candidates
= g_list_remove(candidates
, c2
);
278 candidates
= g_list_delete_link(candidates
, i
);
279 sipe_backend_candidate_free(c1
);
280 sipe_backend_candidate_free(c2
);
283 /* hash table takes ownership of "foundation" */
284 g_hash_table_insert(foundation_to_candidate
, foundation
, c1
);
290 g_hash_table_destroy(foundation_to_candidate
);
296 fill_zero_tcp_act_ports_from_tcp_pass(GSList
*candidates
, GSList
*all_candidates
)
299 GHashTable
*ip_to_port
= g_hash_table_new(g_str_hash
, g_str_equal
);
301 for (i
= candidates
; i
; i
= i
->next
) {
302 struct sdpcandidate
*c
= i
->data
;
305 if (c
->protocol
!= SIPE_NETWORK_PROTOCOL_TCP_ACTIVE
) {
309 for (j
= all_candidates
; j
; j
= j
->next
) {
310 struct sdpcandidate
*passive
= j
->data
;
311 if (passive
->protocol
!= SIPE_NETWORK_PROTOCOL_TCP_PASSIVE
||
312 c
->type
!= passive
->type
) {
316 if (sipe_strequal(c
->ip
, passive
->ip
) &&
317 sipe_strequal(c
->base_ip
, passive
->base_ip
)) {
319 c
->port
= passive
->port
;
322 if (c
->base_port
== 0) {
323 c
->base_port
= passive
->base_port
;
330 for (i
= all_candidates
; i
; i
= i
->next
) {
331 struct sdpcandidate
*c
= i
->data
;
333 if (c
->protocol
== SIPE_NETWORK_PROTOCOL_TCP_PASSIVE
&&
334 c
->type
== SIPE_CANDIDATE_TYPE_HOST
) {
335 g_hash_table_insert(ip_to_port
, c
->ip
, &c
->port
);
339 /* Fill base ports of all TCP relay candidates using what we have
340 * collected from host candidates. */
341 for (i
= candidates
; i
; i
= i
->next
) {
342 struct sdpcandidate
*c
= i
->data
;
343 if (c
->type
== SIPE_CANDIDATE_TYPE_RELAY
&& c
->base_port
== 0) {
344 guint
*base_port
= (guint
*)g_hash_table_lookup(ip_to_port
, c
->base_ip
);
346 c
->base_port
= *base_port
;
348 SIPE_DEBUG_WARNING("Couldn't determine base port for candidate "
349 "with foundation %s", c
->foundation
);
354 g_hash_table_destroy(ip_to_port
);
357 static SipeEncryptionPolicy
358 get_encryption_policy(struct sipe_core_private
*sipe_private
)
360 SipeEncryptionPolicy result
=
361 sipe_backend_media_get_encryption_policy(SIPE_CORE_PUBLIC
);
362 if (result
== SIPE_ENCRYPTION_POLICY_OBEY_SERVER
) {
363 result
= sipe_private
->server_av_encryption_policy
;
369 static struct sdpmedia
*
370 media_stream_to_sdpmedia(struct sipe_media_call_private
*call_private
,
371 struct sipe_media_stream_private
*stream_private
)
373 struct sdpmedia
*sdpmedia
= g_new0(struct sdpmedia
, 1);
374 GList
*codecs
= sipe_backend_get_local_codecs(SIPE_MEDIA_CALL
,
376 SipeEncryptionPolicy encryption_policy
=
377 get_encryption_policy(call_private
->sipe_private
);
380 GSList
*attributes
= NULL
;
381 GSList
*sdpcandidates
;
382 GSList
*all_sdpcandidates
;
387 sdpmedia
->name
= g_strdup(SIPE_MEDIA_STREAM
->id
);
389 if (sipe_strequal(sdpmedia
->name
, "audio"))
390 type
= SIPE_MEDIA_AUDIO
;
391 else if (sipe_strequal(sdpmedia
->name
, "video"))
392 type
= SIPE_MEDIA_VIDEO
;
393 else if (sipe_strequal(sdpmedia
->name
, "data"))
394 type
= SIPE_MEDIA_APPLICATION
;
396 // TODO: incompatible media, should not happen here
397 g_free(sdpmedia
->name
);
399 sipe_media_codec_list_free(codecs
);
404 for (i
= codecs
; i
; i
= i
->next
) {
405 struct sipe_backend_codec
*codec
= i
->data
;
406 struct sdpcodec
*c
= g_new0(struct sdpcodec
, 1);
409 c
->id
= sipe_backend_codec_get_id(codec
);
410 c
->name
= sipe_backend_codec_get_name(codec
);
411 c
->clock_rate
= sipe_backend_codec_get_clock_rate(codec
);
414 params
= sipe_backend_codec_get_optional_parameters(codec
);
415 for (; params
; params
= params
->next
) {
416 struct sipnameval
*param
= params
->data
;
417 struct sipnameval
*copy
= g_new0(struct sipnameval
, 1);
419 copy
->name
= g_strdup(param
->name
);
420 copy
->value
= g_strdup(param
->value
);
422 c
->parameters
= g_slist_append(c
->parameters
, copy
);
425 /* Buggy(?) codecs may report non-unique id (a.k.a. payload
426 * type) that must not appear in SDP messages we send. Thus,
427 * let's ignore any codec having the same id as one we already
428 * have in the converted list. */
429 sdpmedia
->codecs
= sipe_utils_slist_insert_unique_sorted(
430 sdpmedia
->codecs
, c
, sdpcodec_compare
,
431 (GDestroyNotify
)sdpcodec_free
);
434 sipe_media_codec_list_free(codecs
);
436 // Process local candidates
437 // If we have established candidate pairs, send them in SDP response.
438 // Otherwise send all available local candidates.
439 candidates
= sipe_backend_media_get_active_local_candidates(SIPE_MEDIA_CALL
,
441 sdpcandidates
= backend_candidates_to_sdpcandidate(candidates
);
442 sipe_media_candidate_list_free(candidates
);
444 candidates
= sipe_backend_get_local_candidates(SIPE_MEDIA_CALL
,
446 candidates
= remove_wrong_farstream_0_1_tcp_candidates(candidates
);
447 all_sdpcandidates
= backend_candidates_to_sdpcandidate(candidates
);
448 sipe_media_candidate_list_free(candidates
);
450 if (!sdpcandidates
) {
451 sdpcandidates
= all_sdpcandidates
;
454 fill_zero_tcp_act_ports_from_tcp_pass(sdpcandidates
, all_sdpcandidates
);
456 sdpmedia
->candidates
= sdpcandidates
;
458 if (all_sdpcandidates
!= sdpcandidates
) {
459 sipe_utils_slist_free_full(all_sdpcandidates
,
460 (GDestroyNotify
)sdpcandidate_free
);
463 get_stream_ip_and_ports(sdpmedia
->candidates
, &sdpmedia
->ip
, &sdpmedia
->port
,
464 &rtcp_port
, SIPE_CANDIDATE_TYPE_HOST
);
465 // No usable HOST candidates, use any candidate
466 if (sdpmedia
->ip
== NULL
&& sdpmedia
->candidates
) {
467 get_stream_ip_and_ports(sdpmedia
->candidates
, &sdpmedia
->ip
, &sdpmedia
->port
,
468 &rtcp_port
, SIPE_CANDIDATE_TYPE_ANY
);
471 if (sipe_backend_stream_is_held(SIPE_MEDIA_STREAM
))
472 attributes
= sipe_utils_nameval_add(attributes
, "inactive", "");
475 gchar
*tmp
= g_strdup_printf("%u", rtcp_port
);
476 attributes
= sipe_utils_nameval_add(attributes
, "rtcp", tmp
);
480 if (encryption_policy
!= call_private
->sipe_private
->server_av_encryption_policy
) {
481 const gchar
*encryption
= NULL
;
482 switch (encryption_policy
) {
483 case SIPE_ENCRYPTION_POLICY_REJECTED
:
484 encryption
= "rejected";
486 case SIPE_ENCRYPTION_POLICY_OPTIONAL
:
487 encryption
= "optional";
489 case SIPE_ENCRYPTION_POLICY_REQUIRED
:
491 encryption
= "required";
495 attributes
= sipe_utils_nameval_add(attributes
, "encryption", encryption
);
498 // Process remote candidates
499 candidates
= sipe_backend_media_get_active_remote_candidates(SIPE_MEDIA_CALL
,
501 sdpmedia
->remote_candidates
= backend_candidates_to_sdpcandidate(candidates
);
502 sipe_media_candidate_list_free(candidates
);
504 sdpmedia
->encryption_active
= stream_private
->encryption_key
&&
505 call_private
->encryption_compatible
&&
506 stream_private
->remote_candidates_and_codecs_set
&&
507 encryption_policy
!= SIPE_ENCRYPTION_POLICY_REJECTED
;
509 // Set our key if encryption is enabled.
510 if (stream_private
->encryption_key
&&
511 encryption_policy
!= SIPE_ENCRYPTION_POLICY_REJECTED
) {
512 sdpmedia
->encryption_key
= g_memdup(stream_private
->encryption_key
,
514 sdpmedia
->encryption_key_id
= stream_private
->encryption_key_id
;
517 // Append extra attributes assigned to the stream.
518 for (j
= stream_private
->extra_sdp
; j
; j
= g_slist_next(j
)) {
519 struct sipnameval
*attr
= j
->data
;
520 attributes
= sipe_utils_nameval_add(attributes
,
521 attr
->name
, attr
->value
);
524 sdpmedia
->attributes
= attributes
;
529 static struct sdpmsg
*
530 sipe_media_to_sdpmsg(struct sipe_media_call_private
*call_private
)
532 struct sdpmsg
*msg
= g_new0(struct sdpmsg
, 1);
533 GSList
*streams
= call_private
->streams
;
535 for (; streams
; streams
= streams
->next
) {
536 struct sdpmedia
*media
= media_stream_to_sdpmedia(call_private
,
539 msg
->media
= g_slist_append(msg
->media
, media
);
542 msg
->ip
= g_strdup(media
->ip
);
546 msg
->media
= g_slist_concat(msg
->media
, call_private
->failed_media
);
547 call_private
->failed_media
= NULL
;
549 msg
->ice_version
= call_private
->ice_version
;
555 sipe_invite_call(struct sipe_media_call_private
*call_private
, TransCallback tc
)
557 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
560 gchar
*p_preferred_identity
= NULL
;
562 struct sip_session
*session
;
563 struct sip_dialog
*dialog
;
565 gboolean add_2007_fallback
= FALSE
;
567 session
= sipe_session_find_call(sipe_private
, SIPE_MEDIA_CALL
->with
);
568 dialog
= session
->dialogs
->data
;
569 add_2007_fallback
= dialog
->cseq
== 0 &&
570 call_private
->ice_version
== SIPE_ICE_RFC_5245
&&
571 !sipe_strequal(SIPE_MEDIA_CALL
->with
, sipe_private
->test_call_bot_uri
);
573 contact
= get_contact(sipe_private
);
575 if (sipe_private
->uc_line_uri
) {
576 gchar
*self
= sip_uri_self(sipe_private
);
577 p_preferred_identity
= g_strdup_printf(
578 "P-Preferred-Identity: <%s>, <%s>\r\n",
579 self
, sipe_private
->uc_line_uri
);
583 hdr
= g_strdup_printf(
584 "ms-keep-alive: UAC;hop-hop=yes\r\n"
587 "Content-Type: %s\r\n",
589 p_preferred_identity
? p_preferred_identity
: "",
591 "multipart/alternative;boundary=\"----=_NextPart_000_001E_01CB4397.0B5EB570\""
592 : "application/sdp");
594 g_free(p_preferred_identity
);
596 msg
= sipe_media_to_sdpmsg(call_private
);
597 body
= sdpmsg_to_string(msg
);
599 if (add_2007_fallback
) {
601 tmp
= g_strdup_printf(
602 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
603 "Content-Type: application/sdp\r\n"
604 "Content-Transfer-Encoding: 7bit\r\n"
605 "Content-Disposition: session; handling=optional; ms-proxy-2007fallback\r\n"
607 "o=- 0 0 IN IP4 %s\r\n"
610 "m=audio 0 RTP/AVP\r\n"
612 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
613 "Content-Type: application/sdp\r\n"
614 "Content-Transfer-Encoding: 7bit\r\n"
615 "Content-Disposition: session; handling=optional\r\n"
619 "------=_NextPart_000_001E_01CB4397.0B5EB570--\r\n",
620 msg
->ip
, msg
->ip
, body
);
627 dialog
->outgoing_invite
= sip_transport_invite(sipe_private
,
638 send_response_with_session_description(struct sipe_media_call_private
*call_private
, int code
, gchar
*text
)
640 struct sdpmsg
*msg
= sipe_media_to_sdpmsg(call_private
);
641 gchar
*body
= sdpmsg_to_string(msg
);
643 sipmsg_add_header(call_private
->invitation
, "Content-Type", "application/sdp");
644 sip_transport_response(call_private
->sipe_private
, call_private
->invitation
, code
, text
, body
);
649 process_invite_call_response(struct sipe_core_private
*sipe_private
,
651 struct transaction
*trans
);
653 struct sipe_media_stream
*
654 sipe_core_media_get_stream_by_id(struct sipe_media_call
*call
, const gchar
*id
)
657 for (i
= SIPE_MEDIA_CALL_PRIVATE
->streams
; i
; i
= i
->next
) {
658 struct sipe_media_stream
*stream
= i
->data
;
659 if (sipe_strequal(stream
->id
, id
))
666 update_call_from_remote_sdp(struct sipe_media_call_private
* call_private
,
667 struct sdpmedia
*media
)
669 struct sipe_media_stream
*stream
;
670 GList
*backend_candidates
= NULL
;
671 GList
*backend_codecs
= NULL
;
673 gboolean result
= TRUE
;
675 stream
= sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL
, media
->name
);
676 if (media
->port
== 0) {
678 sipe_backend_media_stream_end(SIPE_MEDIA_CALL
, stream
);
686 if (sipe_utils_nameval_find(media
->attributes
, "inactive")) {
687 sipe_backend_stream_hold(SIPE_MEDIA_CALL
, stream
, FALSE
);
688 } else if (sipe_backend_stream_is_held(stream
)) {
689 sipe_backend_stream_unhold(SIPE_MEDIA_CALL
, stream
, FALSE
);
692 if (SIPE_MEDIA_STREAM_PRIVATE
->remote_candidates_and_codecs_set
) {
696 for (i
= media
->codecs
; i
; i
= i
->next
) {
697 struct sdpcodec
*c
= i
->data
;
698 struct sipe_backend_codec
*codec
;
701 codec
= sipe_backend_codec_new(c
->id
,
706 for (j
= c
->parameters
; j
; j
= j
->next
) {
707 struct sipnameval
*attr
= j
->data
;
709 sipe_backend_codec_add_optional_parameter(codec
,
714 backend_codecs
= g_list_append(backend_codecs
, codec
);
717 if (media
->encryption_key
&& SIPE_MEDIA_STREAM_PRIVATE
->encryption_key
) {
718 sipe_backend_media_set_encryption_keys(SIPE_MEDIA_CALL
, stream
,
719 SIPE_MEDIA_STREAM_PRIVATE
->encryption_key
,
720 media
->encryption_key
);
721 SIPE_MEDIA_STREAM_PRIVATE
->encryption_key_id
= media
->encryption_key_id
;
724 result
= sipe_backend_set_remote_codecs(SIPE_MEDIA_CALL
, stream
,
726 sipe_media_codec_list_free(backend_codecs
);
728 if (result
== FALSE
) {
729 sipe_backend_media_stream_end(SIPE_MEDIA_CALL
, stream
);
733 for (i
= media
->candidates
; i
; i
= i
->next
) {
734 struct sdpcandidate
*c
= i
->data
;
735 struct sipe_backend_candidate
*candidate
;
736 candidate
= sipe_backend_candidate_new(c
->foundation
,
744 sipe_backend_candidate_set_priority(candidate
, c
->priority
);
746 backend_candidates
= g_list_append(backend_candidates
, candidate
);
749 sipe_backend_media_add_remote_candidates(SIPE_MEDIA_CALL
, stream
,
751 sipe_media_candidate_list_free(backend_candidates
);
753 SIPE_MEDIA_STREAM_PRIVATE
->remote_candidates_and_codecs_set
= TRUE
;
759 apply_remote_message(struct sipe_media_call_private
* call_private
,
764 sipe_utils_slist_free_full(call_private
->failed_media
, (GDestroyNotify
)sdpmedia_free
);
765 call_private
->failed_media
= NULL
;
766 call_private
->encryption_compatible
= TRUE
;
768 for (i
= msg
->media
; i
; i
= i
->next
) {
769 struct sdpmedia
*media
= i
->data
;
770 const gchar
*enc_level
=
771 sipe_utils_nameval_find(media
->attributes
, "encryption");
772 if (sipe_strequal(enc_level
, "rejected") &&
773 get_encryption_policy(call_private
->sipe_private
) == SIPE_ENCRYPTION_POLICY_REQUIRED
) {
774 call_private
->encryption_compatible
= FALSE
;
777 if (!update_call_from_remote_sdp(call_private
, media
)) {
779 call_private
->failed_media
=
780 g_slist_append(call_private
->failed_media
, media
);
784 /* We need to keep failed medias until response is sent, remove them
785 * from sdpmsg that is to be freed. */
786 for (i
= call_private
->failed_media
; i
; i
= i
->next
) {
787 msg
->media
= g_slist_remove(msg
->media
, i
->data
);
792 call_initialized(struct sipe_media_call
*call
)
794 GSList
*streams
= SIPE_MEDIA_CALL_PRIVATE
->streams
;
795 for (; streams
; streams
= streams
->next
) {
796 if (!sipe_backend_stream_initialized(call
, streams
->data
)) {
804 // Sends an invite response when the call is accepted and local candidates were
805 // prepared, otherwise does nothing. If error response is sent, call_private is
806 // disposed before function returns. Returns true when response was sent.
808 send_invite_response_if_ready(struct sipe_media_call_private
*call_private
)
810 struct sipe_backend_media
*backend_media
;
812 backend_media
= call_private
->public.backend_private
;
814 if (!sipe_backend_media_accepted(backend_media
) ||
815 !call_initialized(&call_private
->public))
818 if (!call_private
->encryption_compatible
) {
819 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
821 sipmsg_add_header(call_private
->invitation
, "Warning",
822 "308 lcs.microsoft.com \"Encryption Levels not compatible\"");
823 sip_transport_response(sipe_private
,
824 call_private
->invitation
,
825 488, "Encryption Levels not compatible",
827 sipe_backend_media_reject(backend_media
, FALSE
);
828 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
829 _("Unable to establish a call"),
830 _("Encryption settings of peer are incompatible with ours."));
832 send_response_with_session_description(call_private
, 200, "OK");
839 stream_initialized_cb(struct sipe_media_call
*call
,
840 struct sipe_media_stream
*stream
)
842 if (call_initialized(call
)) {
843 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
845 if (sipe_backend_media_is_initiator(call
, stream
)) {
846 sipe_invite_call(call_private
,
847 process_invite_call_response
);
848 } else if (call_private
->smsg
) {
849 struct sdpmsg
*smsg
= call_private
->smsg
;
850 call_private
->smsg
= NULL
;
852 apply_remote_message(call_private
, smsg
);
853 send_invite_response_if_ready(call_private
);
859 static void phone_state_publish(struct sipe_core_private
*sipe_private
)
861 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
862 sipe_ocs2007_phone_state_publish(sipe_private
);
864 // TODO: OCS 2005 support. Is anyone still using it at all?
869 stream_end_cb(struct sipe_media_call
* call
, struct sipe_media_stream
* stream
)
871 remove_stream(call
, SIPE_MEDIA_STREAM_PRIVATE
);
875 media_end_cb(struct sipe_media_call
*call
)
877 struct sipe_core_private
*sipe_private
;
879 g_return_if_fail(call
);
881 sipe_private
= SIPE_MEDIA_CALL_PRIVATE
->sipe_private
;
883 sipe_media_call_free(SIPE_MEDIA_CALL_PRIVATE
);
884 phone_state_publish(sipe_private
);
888 call_accept_cb(struct sipe_media_call
*call
, gboolean local
)
891 send_invite_response_if_ready(SIPE_MEDIA_CALL_PRIVATE
);
893 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
);
897 call_reject_cb(struct sipe_media_call
*call
, gboolean local
)
900 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
901 struct sip_session
*session
= NULL
;
903 sip_transport_response(call_private
->sipe_private
,
904 call_private
->invitation
,
905 603, "Decline", NULL
);
907 session
= sipe_session_find_call(call_private
->sipe_private
,
910 sipe_session_remove(call_private
->sipe_private
, session
);
916 sipe_media_send_ack(struct sipe_core_private
*sipe_private
, struct sipmsg
*msg
,
917 struct transaction
*trans
);
919 static void call_hold_cb(struct sipe_media_call
*call
,
921 SIPE_UNUSED_PARAMETER gboolean state
)
924 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE
, sipe_media_send_ack
);
928 static void call_hangup_cb(struct sipe_media_call
*call
, gboolean local
)
931 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
932 struct sip_session
*session
;
933 session
= sipe_session_find_call(call_private
->sipe_private
,
937 sipe_session_close(call_private
->sipe_private
, session
);
943 error_cb(struct sipe_media_call
*call
, gchar
*message
)
945 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
946 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
947 gboolean initiator
= sipe_backend_media_is_initiator(call
, NULL
);
948 gboolean accepted
= sipe_backend_media_accepted(call
->backend_private
);
950 gchar
*title
= g_strdup_printf("Call with %s failed", call
->with
);
951 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, title
, message
);
954 if (!initiator
&& !accepted
) {
955 sip_transport_response(sipe_private
,
956 call_private
->invitation
,
957 488, "Not Acceptable Here", NULL
);
960 sipe_backend_media_hangup(call
->backend_private
, initiator
|| accepted
);
963 static struct sipe_media_call_private
*
964 sipe_media_call_new(struct sipe_core_private
*sipe_private
, const gchar
* with
,
965 struct sipmsg
*msg
, SipeIceVersion ice_version
,
966 SipeMediaCallFlags flags
)
968 struct sipe_media_call_private
*call_private
;
969 struct sip_session
*session
;
970 struct sip_dialog
*dialog
;
973 session
= sipe_session_add_call(sipe_private
, with
);
975 dialog
= sipe_dialog_add(session
);
976 dialog
->with
= g_strdup(with
);
979 gchar
*newTag
= gentag();
980 const gchar
*oldHeader
;
983 oldHeader
= sipmsg_find_header(msg
, "To");
984 newHeader
= g_strdup_printf("%s;tag=%s", oldHeader
, newTag
);
985 sipmsg_remove_header_now(msg
, "To");
986 sipmsg_add_header_now(msg
, "To", newHeader
);
990 dialog
->callid
= g_strdup(sipmsg_find_header(msg
, "Call-ID"));
991 sipe_dialog_parse(dialog
, msg
, FALSE
);
993 dialog
->callid
= gencallid();
994 dialog
->ourtag
= gentag();
995 flags
|= SIPE_MEDIA_CALL_INITIATOR
;
998 if (g_hash_table_lookup(sipe_private
->media_calls
, dialog
->callid
)) {
999 SIPE_DEBUG_ERROR("sipe_media_call_new: call already exists for "
1000 "Call-ID %s", dialog
->callid
);
1001 sipe_session_remove(sipe_private
, session
);
1005 call_private
= g_new0(struct sipe_media_call_private
, 1);
1006 call_private
->sipe_private
= sipe_private
;
1007 SIPE_MEDIA_CALL
->with
= g_strdup(with
);
1009 g_hash_table_insert(sipe_private
->media_calls
,
1010 g_strdup(dialog
->callid
), call_private
);
1012 cname
= g_strdup(sipe_private
->contact
+ 1);
1013 cname
[strlen(cname
) - 1] = '\0';
1015 call_private
->public.backend_private
= sipe_backend_media_new(SIPE_CORE_PUBLIC
,
1019 sipe_backend_media_set_cname(call_private
->public.backend_private
, cname
);
1021 call_private
->ice_version
= ice_version
;
1022 call_private
->encryption_compatible
= TRUE
;
1024 call_private
->public.stream_initialized_cb
= stream_initialized_cb
;
1025 call_private
->public.stream_end_cb
= stream_end_cb
;
1026 call_private
->public.media_end_cb
= media_end_cb
;
1027 call_private
->public.call_accept_cb
= call_accept_cb
;
1028 call_private
->public.call_reject_cb
= call_reject_cb
;
1029 call_private
->public.call_hold_cb
= call_hold_cb
;
1030 call_private
->public.call_hangup_cb
= call_hangup_cb
;
1031 call_private
->public.error_cb
= error_cb
;
1035 return call_private
;
1038 void sipe_media_hangup(struct sipe_media_call_private
*call_private
)
1041 sipe_backend_media_hangup(call_private
->public.backend_private
,
1046 struct sipe_media_stream
*
1047 sipe_media_stream_add(struct sipe_media_call
*call
, const gchar
*id
,
1048 SipeMediaType type
, SipeIceVersion ice_version
,
1051 struct sipe_core_private
*sipe_private
;
1052 struct sipe_media_stream_private
*stream_private
;
1053 struct sipe_backend_media_stream
*backend_stream
;
1054 struct sipe_backend_media_relays
*backend_media_relays
;
1056 sipe_private
= SIPE_MEDIA_CALL_PRIVATE
->sipe_private
;
1058 backend_media_relays
= sipe_backend_media_relays_convert(
1059 sipe_private
->media_relays
,
1060 sipe_private
->media_relay_username
,
1061 sipe_private
->media_relay_password
);
1063 backend_stream
= sipe_backend_media_add_stream(call
, id
, call
->with
,
1066 backend_media_relays
);
1068 sipe_backend_media_relays_free(backend_media_relays
);
1070 if (!backend_stream
) {
1074 stream_private
= g_new0(struct sipe_media_stream_private
, 1);
1075 SIPE_MEDIA_STREAM
->id
= g_strdup(id
);
1076 SIPE_MEDIA_STREAM
->backend_private
= backend_stream
;
1081 stream_private
->encryption_key
= g_new0(guchar
, SIPE_SRTP_KEY_LEN
);
1082 for (i
= 0; i
!= SIPE_SRTP_KEY_LEN
; ++i
) {
1083 stream_private
->encryption_key
[i
] = rand() & 0xff;
1085 stream_private
->encryption_key_id
= 1;
1089 SIPE_MEDIA_CALL_PRIVATE
->streams
=
1090 g_slist_append(SIPE_MEDIA_CALL_PRIVATE
->streams
,
1093 return SIPE_MEDIA_STREAM
;
1097 sipe_media_initiate_call(struct sipe_core_private
*sipe_private
,
1098 const char *with
, SipeIceVersion ice_version
,
1099 gboolean with_video
)
1101 struct sipe_media_call_private
*call_private
;
1103 if (sipe_core_media_get_call(SIPE_CORE_PUBLIC
)) {
1107 call_private
= sipe_media_call_new(sipe_private
, with
, NULL
,
1110 if (!sipe_media_stream_add(SIPE_MEDIA_CALL
, "audio", SIPE_MEDIA_AUDIO
,
1111 call_private
->ice_version
,
1113 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
1115 _("Error creating audio stream"));
1116 sipe_media_hangup(call_private
);
1121 !sipe_media_stream_add(SIPE_MEDIA_CALL
, "video", SIPE_MEDIA_VIDEO
,
1122 call_private
->ice_version
,
1124 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
1126 _("Error creating video stream"));
1127 sipe_media_hangup(call_private
);
1131 // Processing continues in stream_initialized_cb
1135 sipe_core_media_initiate_call(struct sipe_core_public
*sipe_public
,
1137 gboolean with_video
)
1139 sipe_media_initiate_call(SIPE_CORE_PRIVATE
, with
,
1140 SIPE_ICE_RFC_5245
, with_video
);
1143 void sipe_core_media_connect_conference(struct sipe_core_public
*sipe_public
,
1144 struct sipe_chat_session
*chat_session
)
1146 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1147 struct sipe_media_call_private
*call_private
;
1148 struct sip_session
*session
;
1149 SipeIceVersion ice_version
;
1153 if (!sipe_conf_supports_mcu_type(sipe_private
, "audio-video")) {
1154 sipe_backend_notify_error(sipe_public
, _("Join conference call"),
1155 _("Conference calls are not supported on this server."));
1159 session
= sipe_session_find_chat(sipe_private
, chat_session
);
1161 if (sipe_core_media_get_call(sipe_public
) || !session
) {
1165 session
->is_call
= TRUE
;
1167 parts
= g_strsplit(chat_session
->id
, "app:conf:focus:", 2);
1168 av_uri
= g_strjoinv("app:conf:audio-video:", parts
);
1171 ice_version
= SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013
) ? SIPE_ICE_RFC_5245
:
1174 call_private
= sipe_media_call_new(sipe_private
, av_uri
, NULL
,
1177 if (!sipe_media_stream_add(SIPE_MEDIA_CALL
, "audio", SIPE_MEDIA_AUDIO
,
1178 call_private
->ice_version
, TRUE
)) {
1179 sipe_backend_notify_error(sipe_public
,
1180 _("Error occurred"),
1181 _("Error creating audio stream"));
1183 sipe_media_hangup(call_private
);
1188 // Processing continues in stream_initialized_cb
1191 struct sipe_media_call
*
1192 sipe_core_media_get_call(struct sipe_core_public
*sipe_public
)
1194 struct sipe_media_call
* result
= NULL
;
1195 GList
*calls
= g_hash_table_get_values(SIPE_CORE_PRIVATE
->media_calls
);
1197 for (; calls
; calls
= g_list_delete_link(calls
, calls
)) {
1198 if (sipe_core_media_get_stream_by_id(calls
->data
, "audio")) {
1199 result
= calls
->data
;
1207 static gboolean
phone_number_is_valid(const gchar
*phone_number
)
1209 if (!phone_number
|| sipe_strequal(phone_number
, "")) {
1213 if (*phone_number
== '+') {
1217 while (*phone_number
!= '\0') {
1218 if (!g_ascii_isdigit(*phone_number
)) {
1227 void sipe_core_media_phone_call(struct sipe_core_public
*sipe_public
,
1228 const gchar
*phone_number
)
1230 g_return_if_fail(sipe_public
);
1232 if (phone_number_is_valid(phone_number
)) {
1233 gchar
*phone_uri
= g_strdup_printf("sip:%s@%s;user=phone",
1234 phone_number
, sipe_public
->sip_domain
);
1236 sipe_core_media_initiate_call(sipe_public
, phone_uri
, FALSE
);
1240 sipe_backend_notify_error(sipe_public
,
1241 _("Unable to establish a call"),
1242 _("Invalid phone number"));
1246 void sipe_core_media_test_call(struct sipe_core_public
*sipe_public
)
1248 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1249 if (!sipe_private
->test_call_bot_uri
) {
1250 sipe_backend_notify_error(sipe_public
,
1251 _("Unable to establish a call"),
1252 _("Audio Test Service is not available."));
1256 sipe_core_media_initiate_call(sipe_public
,
1257 sipe_private
->test_call_bot_uri
, FALSE
);
1260 static struct sipe_media_call_private
*
1261 sipe_media_from_sipmsg(struct sipe_core_private
*sipe_private
,
1264 return g_hash_table_lookup(sipe_private
->media_calls
,
1265 sipmsg_find_header(msg
, "Call-ID"));
1268 struct sipe_media_call_private
*
1269 process_incoming_invite_call(struct sipe_core_private
*sipe_private
,
1272 struct sipe_media_call_private
*call_private
;
1273 struct sdpmsg
*smsg
;
1274 gboolean has_new_media
= FALSE
;
1277 // Don't allow two voice calls in parallel.
1278 if (!strstr(msg
->body
, "m=data") &&
1279 !strstr(msg
->body
, "m=applicationsharing")) {
1280 struct sipe_media_call
*call
=
1281 sipe_core_media_get_call(SIPE_CORE_PUBLIC
);
1282 if (call
&& !is_media_session_msg(SIPE_MEDIA_CALL_PRIVATE
, msg
)) {
1283 sip_transport_response(sipe_private
, msg
,
1284 486, "Busy Here", NULL
);
1289 call_private
= sipe_media_from_sipmsg(sipe_private
, msg
);
1292 char *self
= sip_uri_self(sipe_private
);
1293 if (sipe_strequal(SIPE_MEDIA_CALL
->with
, self
)) {
1295 sip_transport_response(sipe_private
, msg
, 488, "Not Acceptable Here", NULL
);
1301 smsg
= sdpmsg_parse_msg(msg
->body
);
1303 sip_transport_response(sipe_private
, msg
,
1304 488, "Not Acceptable Here", NULL
);
1306 sipe_media_hangup(call_private
);
1311 if (!call_private
) {
1312 gchar
*with
= parse_from(sipmsg_find_header(msg
, "From"));
1313 SipeMediaCallFlags flags
= 0;
1315 if (strstr(msg
->body
, "m=data")) {
1316 flags
|= SIPE_MEDIA_CALL_NO_UI
;
1319 call_private
= sipe_media_call_new(sipe_private
, with
, msg
,
1320 smsg
->ice_version
, flags
);
1325 if (call_private
->invitation
)
1326 sipmsg_free(call_private
->invitation
);
1327 call_private
->invitation
= sipmsg_copy(msg
);
1329 // Create any new media streams
1330 for (i
= smsg
->media
; i
; i
= i
->next
) {
1331 struct sdpmedia
*media
= i
->data
;
1332 gchar
*id
= media
->name
;
1335 if ( media
->port
!= 0
1336 && !sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL
, id
)) {
1337 if (sipe_strequal(id
, "audio"))
1338 type
= SIPE_MEDIA_AUDIO
;
1339 else if (sipe_strequal(id
, "video"))
1340 type
= SIPE_MEDIA_VIDEO
;
1341 else if (sipe_strequal(id
, "data"))
1342 type
= SIPE_MEDIA_APPLICATION
;
1346 sipe_media_stream_add(SIPE_MEDIA_CALL
, id
, type
,
1347 smsg
->ice_version
, FALSE
);
1348 has_new_media
= TRUE
;
1352 if (has_new_media
) {
1353 sdpmsg_free(call_private
->smsg
);
1354 call_private
->smsg
= smsg
;
1355 sip_transport_response(sipe_private
, call_private
->invitation
,
1356 180, "Ringing", NULL
);
1357 // Processing continues in stream_initialized_cb
1359 apply_remote_message(call_private
, smsg
);
1360 send_response_with_session_description(call_private
, 200, "OK");
1365 return call_private
;
1368 void process_incoming_cancel_call(struct sipe_media_call_private
*call_private
,
1371 // We respond to the CANCEL request with 200 OK response and
1372 // with 487 Request Terminated to the remote INVITE in progress.
1373 sip_transport_response(call_private
->sipe_private
, msg
, 200, "OK", NULL
);
1375 if (call_private
->invitation
) {
1376 sip_transport_response(call_private
->sipe_private
,
1377 call_private
->invitation
,
1378 487, "Request Terminated", NULL
);
1381 sipe_media_hangup(call_private
);
1385 sipe_media_send_ack(struct sipe_core_private
*sipe_private
,
1387 struct transaction
*trans
)
1389 struct sipe_media_call_private
*call_private
;
1390 struct sip_session
*session
;
1391 struct sip_dialog
*dialog
;
1394 call_private
= sipe_media_from_sipmsg(sipe_private
, msg
);
1396 if (!is_media_session_msg(call_private
, msg
))
1399 session
= sipe_session_find_call(sipe_private
, SIPE_MEDIA_CALL
->with
);
1400 dialog
= session
->dialogs
->data
;
1404 tmp_cseq
= dialog
->cseq
;
1406 dialog
->cseq
= sip_transaction_cseq(trans
) - 1;
1407 sip_transport_ack(sipe_private
, dialog
);
1408 dialog
->cseq
= tmp_cseq
;
1410 dialog
->outgoing_invite
= NULL
;
1416 sipe_media_send_final_ack(struct sipe_core_private
*sipe_private
,
1418 struct transaction
*trans
)
1420 struct sipe_media_call_private
*call_private
;
1422 if (!sipe_media_send_ack(sipe_private
, msg
, trans
))
1425 call_private
= sipe_media_from_sipmsg(sipe_private
, msg
);
1427 sipe_backend_media_accept(SIPE_MEDIA_CALL
->backend_private
, FALSE
);
1433 reinvite_on_candidate_pair_cb(struct sipe_core_public
*sipe_public
,
1434 struct sipe_media_call_private
*media_call
)
1436 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1442 streams
= media_call
->streams
;
1444 for (; streams
; streams
= streams
->next
) {
1445 struct sipe_media_stream
*s
= streams
->data
;
1446 GList
*remote_candidates
= sipe_backend_media_get_active_remote_candidates(&media_call
->public, s
);
1447 guint components
= g_list_length(remote_candidates
);
1449 sipe_media_candidate_list_free(remote_candidates
);
1451 // We must have candidates for both (RTP + RTCP) components ready
1452 if (components
< 2) {
1453 sipe_schedule_mseconds(sipe_private
,
1454 "<+media-reinvite-on-candidate-pair>",
1457 (sipe_schedule_action
) reinvite_on_candidate_pair_cb
,
1463 sipe_invite_call(media_call
, sipe_media_send_final_ack
);
1467 maybe_retry_call_with_ice_version(struct sipe_media_call_private
*call_private
,
1468 SipeIceVersion ice_version
,
1469 struct transaction
*trans
)
1471 if (call_private
->ice_version
!= ice_version
&&
1472 sip_transaction_cseq(trans
) == 1) {
1473 gchar
*with
= g_strdup(SIPE_MEDIA_CALL
->with
);
1474 gboolean with_video
= sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL
, "video") != NULL
;
1476 sipe_media_hangup(call_private
);
1477 SIPE_DEBUG_INFO("Retrying call with ICEv%d.",
1478 ice_version
== SIPE_ICE_DRAFT_6
? 6 : 19);
1479 sipe_media_initiate_call(call_private
->sipe_private
, with
,
1480 ice_version
, with_video
);
1490 process_invite_call_response(struct sipe_core_private
*sipe_private
,
1492 struct transaction
*trans
)
1495 struct sipe_media_call_private
*call_private
;
1496 struct sip_session
*session
;
1497 struct sip_dialog
*dialog
;
1498 struct sdpmsg
*smsg
;
1500 call_private
= sipe_media_from_sipmsg(sipe_private
,msg
);
1502 if (!is_media_session_msg(call_private
, msg
))
1505 session
= sipe_session_find_call(sipe_private
, SIPE_MEDIA_CALL
->with
);
1506 dialog
= session
->dialogs
->data
;
1508 with
= dialog
->with
;
1510 dialog
->outgoing_invite
= NULL
;
1512 if (msg
->response
>= 400) {
1513 // Call rejected by remote peer or an error occurred
1515 GString
*desc
= g_string_new("");
1516 gboolean append_responsestr
= FALSE
;
1518 switch (msg
->response
) {
1520 title
= _("User unavailable");
1522 if (sipmsg_parse_warning(msg
, NULL
) == 391) {
1523 g_string_append_printf(desc
, _("%s does not want to be disturbed"), with
);
1525 g_string_append_printf(desc
, _("User %s is not available"), with
);
1530 title
= _("Call rejected");
1531 g_string_append_printf(desc
, _("User %s rejected call"), with
);
1534 // OCS/Lync really sends response string with 'Mutipart' typo.
1535 if (sipe_strequal(msg
->responsestr
, "Mutipart mime in content type not supported by Archiving CDR service") &&
1536 maybe_retry_call_with_ice_version(call_private
, SIPE_ICE_DRAFT_6
, trans
)) {
1539 title
= _("Unsupported media type");
1542 /* Check for incompatible encryption levels error.
1545 * 488 Not Acceptable Here
1546 * ms-client-diagnostics: 52017;reason="Encryption levels dont match"
1548 * older clients (and SIPE itself):
1549 * 488 Encryption Levels not compatible
1551 const gchar
*ms_diag
= sipmsg_find_header(msg
, "ms-client-diagnostics");
1552 SipeIceVersion retry_ice_version
= SIPE_ICE_DRAFT_6
;
1554 if (sipe_strequal(msg
->responsestr
, "Encryption Levels not compatible") ||
1555 (ms_diag
&& g_str_has_prefix(ms_diag
, "52017;"))) {
1556 title
= _("Unable to establish a call");
1557 g_string_append(desc
, _("Encryption settings of peer are incompatible with ours."));
1561 /* Check if this is failed conference using
1562 * ICEv6 with reason "Error parsing SDP" and
1563 * retry using ICEv19. */
1564 ms_diag
= sipmsg_find_header(msg
, "ms-diagnostics");
1565 if (ms_diag
&& g_str_has_prefix(ms_diag
, "7008;")) {
1566 retry_ice_version
= SIPE_ICE_RFC_5245
;
1569 if (maybe_retry_call_with_ice_version(call_private
, retry_ice_version
, trans
)) {
1572 // Break intentionally omitted
1575 title
= _("Error occured");
1576 g_string_append(desc
, _("Unable to establish a call"));
1577 append_responsestr
= TRUE
;
1581 if (append_responsestr
) {
1582 gchar
*reason
= sipmsg_get_ms_diagnostics_reason(msg
);
1584 g_string_append_printf(desc
, "\n%d %s",
1585 msg
->response
, msg
->responsestr
);
1587 g_string_append_printf(desc
, "\n\n%s", reason
);
1592 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, title
, desc
->str
);
1593 g_string_free(desc
, TRUE
);
1595 sipe_media_send_ack(sipe_private
, msg
, trans
);
1596 sipe_media_hangup(call_private
);
1601 sipe_dialog_parse(dialog
, msg
, TRUE
);
1602 smsg
= sdpmsg_parse_msg(msg
->body
);
1604 sip_transport_response(sipe_private
, msg
,
1605 488, "Not Acceptable Here", NULL
);
1606 sipe_media_hangup(call_private
);
1610 apply_remote_message(call_private
, smsg
);
1613 sipe_media_send_ack(sipe_private
, msg
, trans
);
1614 reinvite_on_candidate_pair_cb(SIPE_CORE_PUBLIC
, call_private
);
1619 gboolean
is_media_session_msg(struct sipe_media_call_private
*call_private
,
1622 if (!call_private
) {
1626 return sipe_media_from_sipmsg(call_private
->sipe_private
, msg
) == call_private
;
1630 end_call(SIPE_UNUSED_PARAMETER gpointer key
,
1631 struct sipe_media_call_private
*call_private
,
1632 SIPE_UNUSED_PARAMETER gpointer user_data
)
1634 struct sipe_backend_media
*backend_private
;
1636 backend_private
= call_private
->public.backend_private
;
1638 if (!sipe_backend_media_is_initiator(SIPE_MEDIA_CALL
, NULL
) &&
1639 !sipe_backend_media_accepted(backend_private
)) {
1640 sip_transport_response(call_private
->sipe_private
,
1641 call_private
->invitation
,
1642 480, "Temporarily Unavailable", NULL
);
1644 struct sip_session
*session
;
1646 session
= sipe_session_find_call(call_private
->sipe_private
,
1647 SIPE_MEDIA_CALL
->with
);
1649 sipe_session_close(call_private
->sipe_private
, session
);
1652 sipe_media_hangup(call_private
);
1656 sipe_media_handle_going_offline(struct sipe_core_private
*sipe_private
)
1658 g_hash_table_foreach(sipe_private
->media_calls
, (GHFunc
) end_call
, NULL
);
1661 gboolean
sipe_media_is_conference_call(struct sipe_media_call_private
*call_private
)
1663 return g_strstr_len(SIPE_MEDIA_CALL
->with
, -1, "app:conf:audio-video:") != NULL
;
1667 sipe_media_relay_free(struct sipe_media_relay
*relay
)
1669 g_free(relay
->hostname
);
1670 if (relay
->dns_query
)
1671 sipe_backend_dns_query_cancel(relay
->dns_query
);
1676 sipe_media_relay_list_free(GSList
*list
)
1678 for (; list
; list
= g_slist_delete_link(list
, list
))
1679 sipe_media_relay_free(list
->data
);
1683 relay_ip_resolved_cb(struct sipe_media_relay
* relay
,
1684 const gchar
*ip
, SIPE_UNUSED_PARAMETER guint port
)
1686 gchar
*hostname
= relay
->hostname
;
1687 relay
->dns_query
= NULL
;
1690 relay
->hostname
= g_strdup(ip
);
1691 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname
, ip
);
1693 relay
->hostname
= NULL
;
1694 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname
);
1701 process_get_av_edge_credentials_response(struct sipe_core_private
*sipe_private
,
1703 SIPE_UNUSED_PARAMETER
struct transaction
*trans
)
1705 g_free(sipe_private
->media_relay_username
);
1706 g_free(sipe_private
->media_relay_password
);
1707 sipe_media_relay_list_free(sipe_private
->media_relays
);
1708 sipe_private
->media_relay_username
= NULL
;
1709 sipe_private
->media_relay_password
= NULL
;
1710 sipe_private
->media_relays
= NULL
;
1712 if (msg
->response
>= 400) {
1713 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
1714 "Failed to obtain A/V Edge credentials.");
1718 if (msg
->response
== 200) {
1719 sipe_xml
*xn_response
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1721 if (sipe_strequal("OK", sipe_xml_attribute(xn_response
, "reasonPhrase"))) {
1722 const sipe_xml
*xn_credentials
= sipe_xml_child(xn_response
, "credentialsResponse/credentials");
1723 const sipe_xml
*xn_relays
= sipe_xml_child(xn_response
, "credentialsResponse/mediaRelayList");
1724 const sipe_xml
*item
;
1725 GSList
*relays
= NULL
;
1727 item
= sipe_xml_child(xn_credentials
, "username");
1728 sipe_private
->media_relay_username
= sipe_xml_data(item
);
1729 item
= sipe_xml_child(xn_credentials
, "password");
1730 sipe_private
->media_relay_password
= sipe_xml_data(item
);
1732 for (item
= sipe_xml_child(xn_relays
, "mediaRelay"); item
; item
= sipe_xml_twin(item
)) {
1733 struct sipe_media_relay
*relay
= g_new0(struct sipe_media_relay
, 1);
1734 const sipe_xml
*node
;
1737 node
= sipe_xml_child(item
, "hostName");
1738 relay
->hostname
= sipe_xml_data(node
);
1740 node
= sipe_xml_child(item
, "udpPort");
1742 relay
->udp_port
= atoi(tmp
= sipe_xml_data(node
));
1746 node
= sipe_xml_child(item
, "tcpPort");
1748 relay
->tcp_port
= atoi(tmp
= sipe_xml_data(node
));
1752 relays
= g_slist_append(relays
, relay
);
1754 relay
->dns_query
= sipe_backend_dns_query_a(
1758 (sipe_dns_resolved_cb
) relay_ip_resolved_cb
,
1761 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
1763 relay
->tcp_port
, relay
->udp_port
);
1766 sipe_private
->media_relays
= relays
;
1769 sipe_xml_free(xn_response
);
1776 sipe_media_get_av_edge_credentials(struct sipe_core_private
*sipe_private
)
1778 // TODO: re-request credentials after duration expires?
1779 static const char CRED_REQUEST_XML
[] =
1780 "<request requestID=\"%d\" "
1784 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
1785 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
1786 "<credentialsRequest credentialsRequestID=\"%d\">"
1787 "<identity>%s</identity>"
1788 "<location>%s</location>"
1789 "<duration>480</duration>"
1790 "</credentialsRequest>"
1793 int request_id
= rand();
1797 if (!sipe_private
->mras_uri
)
1800 self
= sip_uri_self(sipe_private
);
1802 body
= g_strdup_printf(
1806 sipe_private
->mras_uri
,
1809 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER
) ? "internet" : "intranet");
1812 sip_transport_service(sipe_private
,
1813 sipe_private
->mras_uri
,
1814 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
1816 process_get_av_edge_credentials_response
);
1822 sipe_media_stream_add_extra_attribute(struct sipe_media_stream
*stream
,
1823 const gchar
*name
, const gchar
*value
)
1825 SIPE_MEDIA_STREAM_PRIVATE
->extra_sdp
=
1826 sipe_utils_nameval_add(SIPE_MEDIA_STREAM_PRIVATE
->extra_sdp
,
1831 sipe_media_stream_set_data(struct sipe_media_stream
*stream
, gpointer data
,
1832 GDestroyNotify free_func
)
1834 struct sipe_media_stream_private
*stream_private
=
1835 SIPE_MEDIA_STREAM_PRIVATE
;
1837 g_return_if_fail(stream_private
);
1839 if (stream_private
->data
&& stream_private
->data_free_func
) {
1840 stream_private
->data_free_func(stream_private
->data
);
1843 stream_private
->data
= data
;
1844 stream_private
->data_free_func
= free_func
;
1848 sipe_media_stream_get_data(struct sipe_media_stream
*stream
)
1850 g_return_val_if_fail(stream
, NULL
);
1852 return SIPE_MEDIA_STREAM_PRIVATE
->data
;