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
;
75 #define SIPE_MEDIA_STREAM ((struct sipe_media_stream *) stream_private)
76 #define SIPE_MEDIA_STREAM_PRIVATE ((struct sipe_media_stream_private *) stream)
78 static void sipe_media_codec_list_free(GList
*codecs
)
80 for (; codecs
; codecs
= g_list_delete_link(codecs
, codecs
))
81 sipe_backend_codec_free(codecs
->data
);
84 static void sipe_media_candidate_list_free(GList
*candidates
)
86 for (; candidates
; candidates
= g_list_delete_link(candidates
, candidates
))
87 sipe_backend_candidate_free(candidates
->data
);
91 remove_stream(struct sipe_media_call
* call
,
92 struct sipe_media_stream_private
*stream_private
)
94 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
95 call_private
->streams
=
96 g_slist_remove(call_private
->streams
, stream_private
);
97 sipe_backend_media_stream_free(SIPE_MEDIA_STREAM
->backend_private
);
98 g_free(SIPE_MEDIA_STREAM
->id
);
99 g_free(stream_private
->encryption_key
);
100 g_free(stream_private
);
104 sipe_media_call_free(struct sipe_media_call_private
*call_private
)
107 struct sip_session
*session
;
109 while (call_private
->streams
) {
110 remove_stream(SIPE_MEDIA_CALL
,
111 call_private
->streams
->data
);
114 sipe_backend_media_free(call_private
->public.backend_private
);
116 session
= sipe_session_find_call(call_private
->sipe_private
,
117 SIPE_MEDIA_CALL
->with
);
119 sipe_session_remove(call_private
->sipe_private
, session
);
121 if (call_private
->invitation
)
122 sipmsg_free(call_private
->invitation
);
124 sdpmsg_free(call_private
->smsg
);
125 sipe_utils_slist_free_full(call_private
->failed_media
,
126 (GDestroyNotify
)sdpmedia_free
);
127 g_free(SIPE_MEDIA_CALL
->with
);
128 g_free(call_private
);
133 candidate_sort_cb(struct sdpcandidate
*c1
, struct sdpcandidate
*c2
)
135 int cmp
= sipe_strcompare(c1
->foundation
, c2
->foundation
);
137 cmp
= sipe_strcompare(c1
->username
, c2
->username
);
139 cmp
= c1
->component
- c2
->component
;
146 backend_candidates_to_sdpcandidate(GList
*candidates
)
148 GSList
*result
= NULL
;
151 for (i
= candidates
; i
; i
= i
->next
) {
152 struct sipe_backend_candidate
*candidate
= i
->data
;
153 struct sdpcandidate
*c
;
155 gchar
*ip
= sipe_backend_candidate_get_ip(candidate
);
156 gchar
*base_ip
= sipe_backend_candidate_get_base_ip(candidate
);
157 if (is_empty(ip
) || strchr(ip
, ':') ||
158 (base_ip
&& strchr(base_ip
, ':'))) {
159 /* Ignore IPv6 candidates. */
165 c
= g_new(struct sdpcandidate
, 1);
166 c
->foundation
= sipe_backend_candidate_get_foundation(candidate
);
167 c
->component
= sipe_backend_candidate_get_component_type(candidate
);
168 c
->type
= sipe_backend_candidate_get_type(candidate
);
169 c
->protocol
= sipe_backend_candidate_get_protocol(candidate
);
171 c
->port
= sipe_backend_candidate_get_port(candidate
);
172 c
->base_ip
= base_ip
;
173 c
->base_port
= sipe_backend_candidate_get_base_port(candidate
);
174 c
->priority
= sipe_backend_candidate_get_priority(candidate
);
175 c
->username
= sipe_backend_candidate_get_username(candidate
);
176 c
->password
= sipe_backend_candidate_get_password(candidate
);
178 result
= g_slist_insert_sorted(result
, c
,
179 (GCompareFunc
)candidate_sort_cb
);
186 get_stream_ip_and_ports(GSList
*candidates
,
187 gchar
**ip
, guint
*rtp_port
, guint
*rtcp_port
,
188 SipeCandidateType type
)
194 for (; candidates
; candidates
= candidates
->next
) {
195 struct sdpcandidate
*candidate
= candidates
->data
;
197 if (type
== SIPE_CANDIDATE_TYPE_ANY
|| candidate
->type
== type
) {
199 *ip
= g_strdup(candidate
->ip
);
200 } else if (!sipe_strequal(*ip
, candidate
->ip
)) {
204 if (candidate
->component
== SIPE_COMPONENT_RTP
) {
205 *rtp_port
= candidate
->port
;
206 } else if (candidate
->component
== SIPE_COMPONENT_RTCP
)
207 *rtcp_port
= candidate
->port
;
210 if (*rtp_port
!= 0 && *rtcp_port
!= 0)
216 sdpcodec_compare(gconstpointer a
, gconstpointer b
)
218 return ((const struct sdpcodec
*)a
)->id
-
219 ((const struct sdpcodec
*)b
)->id
;
223 remove_wrong_farstream_0_1_tcp_candidates(GList
*candidates
)
225 GList
*i
= candidates
;
226 GHashTable
*foundation_to_candidate
= g_hash_table_new_full(g_str_hash
,
232 GList
*next
= i
->next
;
233 struct sipe_backend_candidate
*c1
= i
->data
;
235 if (sipe_backend_candidate_get_protocol(c1
) == SIPE_NETWORK_PROTOCOL_UDP
) {
236 gchar
*foundation
= sipe_backend_candidate_get_foundation(c1
);
237 struct sipe_backend_candidate
*c2
= g_hash_table_lookup(foundation_to_candidate
,
243 if (sipe_backend_candidate_get_port(c1
) ==
244 sipe_backend_candidate_get_port(c2
) ||
245 (sipe_backend_candidate_get_type(c1
) !=
246 SIPE_CANDIDATE_TYPE_HOST
&&
247 sipe_backend_candidate_get_base_port(c1
) ==
248 sipe_backend_candidate_get_base_port(c2
))) {
250 * We assume that RTP+RTCP UDP pairs
251 * that share the same port are
252 * actually mistagged TCP candidates.
254 candidates
= g_list_remove(candidates
, c2
);
255 candidates
= g_list_delete_link(candidates
, i
);
256 sipe_backend_candidate_free(c1
);
257 sipe_backend_candidate_free(c2
);
260 /* hash table takes ownership of "foundation" */
261 g_hash_table_insert(foundation_to_candidate
, foundation
, c1
);
267 g_hash_table_destroy(foundation_to_candidate
);
273 fill_zero_tcp_act_ports_from_tcp_pass(GSList
*candidates
)
276 GHashTable
*ip_to_port
= g_hash_table_new(g_str_hash
, g_str_equal
);
278 for (i
= candidates
; i
; i
= i
->next
) {
279 struct sdpcandidate
*c
= i
->data
;
282 if (c
->protocol
== SIPE_NETWORK_PROTOCOL_TCP_PASSIVE
&&
283 c
->type
== SIPE_CANDIDATE_TYPE_HOST
) {
284 g_hash_table_insert(ip_to_port
, c
->ip
,
288 if (c
->protocol
!= SIPE_NETWORK_PROTOCOL_TCP_ACTIVE
) {
292 for (j
= candidates
; j
; j
= j
->next
) {
293 struct sdpcandidate
*passive
= j
->data
;
294 if (passive
->protocol
!= SIPE_NETWORK_PROTOCOL_TCP_PASSIVE
||
295 c
->type
!= passive
->type
) {
299 if (sipe_strequal(c
->ip
, passive
->ip
) &&
300 sipe_strequal(c
->base_ip
, passive
->base_ip
)) {
302 c
->port
= passive
->port
;
305 if (c
->base_port
== 0) {
306 c
->base_port
= passive
->base_port
;
313 /* Fill base ports of all TCP relay candidates using what we have
314 * collected from host candidates. */
315 for (i
= candidates
; i
; i
= i
->next
) {
316 struct sdpcandidate
*c
= i
->data
;
317 if (c
->type
== SIPE_CANDIDATE_TYPE_RELAY
&& c
->base_port
== 0) {
318 guint
*base_port
= (guint
*)g_hash_table_lookup(ip_to_port
, c
->base_ip
);
320 c
->base_port
= *base_port
;
322 SIPE_DEBUG_WARNING("Couldn't determine base port for candidate "
323 "with foundation %s", c
->foundation
);
328 g_hash_table_destroy(ip_to_port
);
331 static SipeEncryptionPolicy
332 get_encryption_policy(struct sipe_core_private
*sipe_private
)
334 SipeEncryptionPolicy result
=
335 sipe_backend_media_get_encryption_policy(SIPE_CORE_PUBLIC
);
336 if (result
== SIPE_ENCRYPTION_POLICY_OBEY_SERVER
) {
337 result
= sipe_private
->server_av_encryption_policy
;
343 static struct sdpmedia
*
344 media_stream_to_sdpmedia(struct sipe_media_call_private
*call_private
,
345 struct sipe_media_stream_private
*stream_private
)
347 struct sip_session
*session
=
348 sipe_session_find_call(call_private
->sipe_private
,
349 SIPE_MEDIA_CALL
->with
);
350 struct sip_dialog
*dialog
= session
->dialogs
->data
;
351 struct sdpmedia
*sdpmedia
= g_new0(struct sdpmedia
, 1);
352 GList
*codecs
= sipe_backend_get_local_codecs(SIPE_MEDIA_CALL
,
354 SipeEncryptionPolicy encryption_policy
=
355 get_encryption_policy(call_private
->sipe_private
);
358 GSList
*attributes
= NULL
;
362 sdpmedia
->name
= g_strdup(SIPE_MEDIA_STREAM
->id
);
364 if (sipe_strequal(sdpmedia
->name
, "audio"))
365 type
= SIPE_MEDIA_AUDIO
;
366 else if (sipe_strequal(sdpmedia
->name
, "video"))
367 type
= SIPE_MEDIA_VIDEO
;
369 // TODO: incompatible media, should not happen here
370 g_free(sdpmedia
->name
);
372 sipe_media_codec_list_free(codecs
);
377 for (i
= codecs
; i
; i
= i
->next
) {
378 struct sipe_backend_codec
*codec
= i
->data
;
379 struct sdpcodec
*c
= g_new0(struct sdpcodec
, 1);
382 c
->id
= sipe_backend_codec_get_id(codec
);
383 c
->name
= sipe_backend_codec_get_name(codec
);
384 c
->clock_rate
= sipe_backend_codec_get_clock_rate(codec
);
387 params
= sipe_backend_codec_get_optional_parameters(codec
);
388 for (; params
; params
= params
->next
) {
389 struct sipnameval
*param
= params
->data
;
390 struct sipnameval
*copy
= g_new0(struct sipnameval
, 1);
392 copy
->name
= g_strdup(param
->name
);
393 copy
->value
= g_strdup(param
->value
);
395 c
->parameters
= g_slist_append(c
->parameters
, copy
);
398 /* Buggy(?) codecs may report non-unique id (a.k.a. payload
399 * type) that must not appear in SDP messages we send. Thus,
400 * let's ignore any codec having the same id as one we already
401 * have in the converted list. */
402 sdpmedia
->codecs
= sipe_utils_slist_insert_unique_sorted(
403 sdpmedia
->codecs
, c
, sdpcodec_compare
,
404 (GDestroyNotify
)sdpcodec_free
);
407 sipe_media_codec_list_free(codecs
);
409 // Process local candidates
410 // If we have established candidate pairs, send them in SDP response.
411 // Otherwise send all available local candidates.
412 candidates
= sipe_backend_media_get_active_local_candidates(SIPE_MEDIA_CALL
,
415 candidates
= sipe_backend_get_local_candidates(SIPE_MEDIA_CALL
,
417 candidates
= remove_wrong_farstream_0_1_tcp_candidates(candidates
);
420 sdpmedia
->candidates
= backend_candidates_to_sdpcandidate(candidates
);
421 fill_zero_tcp_act_ports_from_tcp_pass(sdpmedia
->candidates
);
423 sipe_media_candidate_list_free(candidates
);
425 get_stream_ip_and_ports(sdpmedia
->candidates
, &sdpmedia
->ip
, &sdpmedia
->port
,
426 &rtcp_port
, SIPE_CANDIDATE_TYPE_HOST
);
427 // No usable HOST candidates, use any candidate
428 if (sdpmedia
->ip
== NULL
&& sdpmedia
->candidates
) {
429 get_stream_ip_and_ports(sdpmedia
->candidates
, &sdpmedia
->ip
, &sdpmedia
->port
,
430 &rtcp_port
, SIPE_CANDIDATE_TYPE_ANY
);
433 if (sipe_backend_stream_is_held(SIPE_MEDIA_STREAM
))
434 attributes
= sipe_utils_nameval_add(attributes
, "inactive", "");
437 gchar
*tmp
= g_strdup_printf("%u", rtcp_port
);
438 attributes
= sipe_utils_nameval_add(attributes
, "rtcp", tmp
);
442 if (encryption_policy
!= call_private
->sipe_private
->server_av_encryption_policy
) {
443 const gchar
*encryption
= NULL
;
444 switch (encryption_policy
) {
445 case SIPE_ENCRYPTION_POLICY_REJECTED
:
446 encryption
= "rejected";
448 case SIPE_ENCRYPTION_POLICY_OPTIONAL
:
449 encryption
= "optional";
451 case SIPE_ENCRYPTION_POLICY_REQUIRED
:
453 encryption
= "required";
457 attributes
= sipe_utils_nameval_add(attributes
, "encryption", encryption
);
460 sdpmedia
->attributes
= attributes
;
462 // Process remote candidates
463 candidates
= sipe_backend_media_get_active_remote_candidates(SIPE_MEDIA_CALL
,
465 sdpmedia
->remote_candidates
= backend_candidates_to_sdpcandidate(candidates
);
466 sipe_media_candidate_list_free(candidates
);
468 sdpmedia
->encryption_active
= stream_private
->encryption_key
&&
469 call_private
->encryption_compatible
&&
470 (dialog
->cseq
!= 0 || call_private
->invitation
);
472 // Set our key if encryption is enabled.
473 if (stream_private
->encryption_key
&&
474 encryption_policy
!= SIPE_ENCRYPTION_POLICY_REJECTED
) {
475 sdpmedia
->encryption_key
= g_memdup(stream_private
->encryption_key
,
482 static struct sdpmsg
*
483 sipe_media_to_sdpmsg(struct sipe_media_call_private
*call_private
)
485 struct sdpmsg
*msg
= g_new0(struct sdpmsg
, 1);
486 GSList
*streams
= call_private
->streams
;
488 for (; streams
; streams
= streams
->next
) {
489 struct sdpmedia
*media
= media_stream_to_sdpmedia(call_private
,
492 msg
->media
= g_slist_append(msg
->media
, media
);
495 msg
->ip
= g_strdup(media
->ip
);
499 msg
->media
= g_slist_concat(msg
->media
, call_private
->failed_media
);
500 call_private
->failed_media
= NULL
;
502 msg
->ice_version
= call_private
->ice_version
;
508 sipe_invite_call(struct sipe_core_private
*sipe_private
, TransCallback tc
)
512 gchar
*p_preferred_identity
= NULL
;
514 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
515 struct sip_session
*session
;
516 struct sip_dialog
*dialog
;
518 gboolean add_2007_fallback
= FALSE
;
520 session
= sipe_session_find_call(sipe_private
, SIPE_MEDIA_CALL
->with
);
521 dialog
= session
->dialogs
->data
;
522 add_2007_fallback
= dialog
->cseq
== 0 &&
523 call_private
->ice_version
== SIPE_ICE_RFC_5245
&&
524 !sipe_strequal(SIPE_MEDIA_CALL
->with
, sipe_private
->test_call_bot_uri
);
526 contact
= get_contact(sipe_private
);
528 if (sipe_private
->uc_line_uri
) {
529 gchar
*self
= sip_uri_self(sipe_private
);
530 p_preferred_identity
= g_strdup_printf(
531 "P-Preferred-Identity: <%s>, <%s>\r\n",
532 self
, sipe_private
->uc_line_uri
);
536 hdr
= g_strdup_printf(
537 "ms-keep-alive: UAC;hop-hop=yes\r\n"
540 "Content-Type: %s\r\n",
542 p_preferred_identity
? p_preferred_identity
: "",
544 "multipart/alternative;boundary=\"----=_NextPart_000_001E_01CB4397.0B5EB570\""
545 : "application/sdp");
547 g_free(p_preferred_identity
);
549 msg
= sipe_media_to_sdpmsg(call_private
);
550 body
= sdpmsg_to_string(msg
);
552 if (add_2007_fallback
) {
554 tmp
= g_strdup_printf(
555 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
556 "Content-Type: application/sdp\r\n"
557 "Content-Transfer-Encoding: 7bit\r\n"
558 "Content-Disposition: session; handling=optional; ms-proxy-2007fallback\r\n"
560 "o=- 0 0 IN IP4 %s\r\n"
563 "m=audio 0 RTP/AVP\r\n"
565 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
566 "Content-Type: application/sdp\r\n"
567 "Content-Transfer-Encoding: 7bit\r\n"
568 "Content-Disposition: session; handling=optional\r\n"
572 "------=_NextPart_000_001E_01CB4397.0B5EB570--\r\n",
573 msg
->ip
, msg
->ip
, body
);
580 dialog
->outgoing_invite
= sip_transport_invite(sipe_private
,
590 static struct sip_dialog
*
591 sipe_media_dialog_init(struct sip_session
* session
, struct sipmsg
*msg
)
593 gchar
*newTag
= gentag();
594 const gchar
*oldHeader
;
596 struct sip_dialog
*dialog
;
598 oldHeader
= sipmsg_find_header(msg
, "To");
599 newHeader
= g_strdup_printf("%s;tag=%s", oldHeader
, newTag
);
600 sipmsg_remove_header_now(msg
, "To");
601 sipmsg_add_header_now(msg
, "To", newHeader
);
604 dialog
= sipe_dialog_add(session
);
605 dialog
->callid
= g_strdup(sipmsg_find_header(msg
, "Call-ID"));
606 dialog
->with
= parse_from(sipmsg_find_header(msg
, "From"));
607 sipe_dialog_parse(dialog
, msg
, FALSE
);
613 send_response_with_session_description(struct sipe_media_call_private
*call_private
, int code
, gchar
*text
)
615 struct sdpmsg
*msg
= sipe_media_to_sdpmsg(call_private
);
616 gchar
*body
= sdpmsg_to_string(msg
);
618 sipmsg_add_header(call_private
->invitation
, "Content-Type", "application/sdp");
619 sip_transport_response(call_private
->sipe_private
, call_private
->invitation
, code
, text
, body
);
624 process_invite_call_response(struct sipe_core_private
*sipe_private
,
626 struct transaction
*trans
);
628 struct sipe_media_stream
*
629 sipe_core_media_get_stream_by_id(struct sipe_media_call
*call
, const gchar
*id
)
632 for (i
= SIPE_MEDIA_CALL_PRIVATE
->streams
; i
; i
= i
->next
) {
633 struct sipe_media_stream
*stream
= i
->data
;
634 if (sipe_strequal(stream
->id
, id
))
641 update_call_from_remote_sdp(struct sipe_media_call_private
* call_private
,
642 struct sdpmedia
*media
)
644 struct sipe_media_stream
*stream
;
645 GList
*backend_candidates
= NULL
;
646 GList
*backend_codecs
= NULL
;
648 gboolean result
= TRUE
;
650 stream
= sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL
, media
->name
);
651 if (media
->port
== 0) {
653 sipe_backend_media_stream_end(SIPE_MEDIA_CALL
, stream
);
661 for (i
= media
->codecs
; i
; i
= i
->next
) {
662 struct sdpcodec
*c
= i
->data
;
663 struct sipe_backend_codec
*codec
;
666 codec
= sipe_backend_codec_new(c
->id
,
671 for (j
= c
->parameters
; j
; j
= j
->next
) {
672 struct sipnameval
*attr
= j
->data
;
674 sipe_backend_codec_add_optional_parameter(codec
,
679 backend_codecs
= g_list_append(backend_codecs
, codec
);
682 if (media
->encryption_key
&& SIPE_MEDIA_STREAM_PRIVATE
->encryption_key
) {
683 sipe_backend_media_set_encryption_keys(SIPE_MEDIA_CALL
, stream
,
684 SIPE_MEDIA_STREAM_PRIVATE
->encryption_key
,
685 media
->encryption_key
);
688 result
= sipe_backend_set_remote_codecs(SIPE_MEDIA_CALL
, stream
,
690 sipe_media_codec_list_free(backend_codecs
);
692 if (result
== FALSE
) {
693 sipe_backend_media_stream_end(SIPE_MEDIA_CALL
, stream
);
697 for (i
= media
->candidates
; i
; i
= i
->next
) {
698 struct sdpcandidate
*c
= i
->data
;
699 struct sipe_backend_candidate
*candidate
;
700 candidate
= sipe_backend_candidate_new(c
->foundation
,
708 sipe_backend_candidate_set_priority(candidate
, c
->priority
);
710 backend_candidates
= g_list_append(backend_candidates
, candidate
);
713 sipe_backend_media_add_remote_candidates(SIPE_MEDIA_CALL
, stream
,
715 sipe_media_candidate_list_free(backend_candidates
);
717 if (sipe_utils_nameval_find(media
->attributes
, "inactive")) {
718 sipe_backend_stream_hold(SIPE_MEDIA_CALL
, stream
, FALSE
);
719 } else if (sipe_backend_stream_is_held(stream
)) {
720 sipe_backend_stream_unhold(SIPE_MEDIA_CALL
, stream
, FALSE
);
727 apply_remote_message(struct sipe_media_call_private
* call_private
,
732 sipe_utils_slist_free_full(call_private
->failed_media
, (GDestroyNotify
)sdpmedia_free
);
733 call_private
->failed_media
= NULL
;
734 call_private
->encryption_compatible
= TRUE
;
736 for (i
= msg
->media
; i
; i
= i
->next
) {
737 struct sdpmedia
*media
= i
->data
;
738 const gchar
*enc_level
=
739 sipe_utils_nameval_find(media
->attributes
, "encryption");
740 if (sipe_strequal(enc_level
, "rejected") &&
741 get_encryption_policy(call_private
->sipe_private
) == SIPE_ENCRYPTION_POLICY_REQUIRED
) {
742 call_private
->encryption_compatible
= FALSE
;
745 if (!update_call_from_remote_sdp(call_private
, media
)) {
747 call_private
->failed_media
=
748 g_slist_append(call_private
->failed_media
, media
);
752 /* We need to keep failed medias until response is sent, remove them
753 * from sdpmsg that is to be freed. */
754 for (i
= call_private
->failed_media
; i
; i
= i
->next
) {
755 msg
->media
= g_slist_remove(msg
->media
, i
->data
);
760 call_initialized(struct sipe_media_call
*call
)
762 GSList
*streams
= SIPE_MEDIA_CALL_PRIVATE
->streams
;
763 for (; streams
; streams
= streams
->next
) {
764 if (!sipe_backend_stream_initialized(call
, streams
->data
)) {
772 // Sends an invite response when the call is accepted and local candidates were
773 // prepared, otherwise does nothing. If error response is sent, call_private is
774 // disposed before function returns. Returns true when response was sent.
776 send_invite_response_if_ready(struct sipe_media_call_private
*call_private
)
778 struct sipe_backend_media
*backend_media
;
780 backend_media
= call_private
->public.backend_private
;
782 if (!sipe_backend_media_accepted(backend_media
) ||
783 !call_initialized(&call_private
->public))
786 if (!call_private
->encryption_compatible
) {
787 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
789 sipmsg_add_header(call_private
->invitation
, "Warning",
790 "308 lcs.microsoft.com \"Encryption Levels not compatible\"");
791 sip_transport_response(sipe_private
,
792 call_private
->invitation
,
793 488, "Encryption Levels not compatible",
795 sipe_backend_media_reject(backend_media
, FALSE
);
796 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
797 _("Unable to establish a call"),
798 _("Encryption settings of peer are incompatible with ours."));
800 send_response_with_session_description(call_private
, 200, "OK");
807 stream_initialized_cb(struct sipe_media_call
*call
,
808 struct sipe_media_stream
*stream
)
810 if (call_initialized(call
)) {
811 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
813 if (sipe_backend_media_is_initiator(call
, stream
)) {
814 sipe_invite_call(call_private
->sipe_private
,
815 process_invite_call_response
);
816 } else if (call_private
->smsg
) {
817 struct sdpmsg
*smsg
= call_private
->smsg
;
818 call_private
->smsg
= NULL
;
820 apply_remote_message(call_private
, smsg
);
821 send_invite_response_if_ready(call_private
);
827 static void phone_state_publish(struct sipe_core_private
*sipe_private
)
829 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
830 sipe_ocs2007_phone_state_publish(sipe_private
);
832 // TODO: OCS 2005 support. Is anyone still using it at all?
837 stream_end_cb(struct sipe_media_call
* call
, struct sipe_media_stream
* stream
)
839 remove_stream(call
, SIPE_MEDIA_STREAM_PRIVATE
);
843 media_end_cb(struct sipe_media_call
*call
)
845 g_return_if_fail(call
);
847 SIPE_MEDIA_CALL_PRIVATE
->sipe_private
->media_call
= NULL
;
848 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
);
849 sipe_media_call_free(SIPE_MEDIA_CALL_PRIVATE
);
853 call_accept_cb(struct sipe_media_call
*call
, gboolean local
)
856 send_invite_response_if_ready(SIPE_MEDIA_CALL_PRIVATE
);
858 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
);
862 call_reject_cb(struct sipe_media_call
*call
, gboolean local
)
865 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
866 sip_transport_response(call_private
->sipe_private
,
867 call_private
->invitation
,
868 603, "Decline", NULL
);
873 sipe_media_send_ack(struct sipe_core_private
*sipe_private
, struct sipmsg
*msg
,
874 struct transaction
*trans
);
876 static void call_hold_cb(struct sipe_media_call
*call
,
878 SIPE_UNUSED_PARAMETER gboolean state
)
881 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
,
882 sipe_media_send_ack
);
885 static void call_hangup_cb(struct sipe_media_call
*call
, gboolean local
)
888 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
889 struct sip_session
*session
;
890 session
= sipe_session_find_call(call_private
->sipe_private
,
894 sipe_session_close(call_private
->sipe_private
, session
);
900 error_cb(struct sipe_media_call
*call
, gchar
*message
)
902 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
903 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
904 gboolean initiator
= sipe_backend_media_is_initiator(call
, NULL
);
905 gboolean accepted
= sipe_backend_media_accepted(call
->backend_private
);
907 gchar
*title
= g_strdup_printf("Call with %s failed", call
->with
);
908 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, title
, message
);
911 if (!initiator
&& !accepted
) {
912 sip_transport_response(sipe_private
,
913 call_private
->invitation
,
914 488, "Not Acceptable Here", NULL
);
917 sipe_backend_media_hangup(call
->backend_private
, initiator
|| accepted
);
920 static struct sipe_media_call_private
*
921 sipe_media_call_new(struct sipe_core_private
*sipe_private
,
922 const gchar
* with
, gboolean initiator
, SipeIceVersion ice_version
)
924 struct sipe_media_call_private
*call_private
= g_new0(struct sipe_media_call_private
, 1);
927 call_private
->sipe_private
= sipe_private
;
929 cname
= g_strdup(sipe_private
->contact
+ 1);
930 cname
[strlen(cname
) - 1] = '\0';
932 call_private
->public.backend_private
= sipe_backend_media_new(SIPE_CORE_PUBLIC
,
936 sipe_backend_media_set_cname(call_private
->public.backend_private
, cname
);
938 call_private
->ice_version
= ice_version
;
939 call_private
->encryption_compatible
= TRUE
;
941 call_private
->public.stream_initialized_cb
= stream_initialized_cb
;
942 call_private
->public.stream_end_cb
= stream_end_cb
;
943 call_private
->public.media_end_cb
= media_end_cb
;
944 call_private
->public.call_accept_cb
= call_accept_cb
;
945 call_private
->public.call_reject_cb
= call_reject_cb
;
946 call_private
->public.call_hold_cb
= call_hold_cb
;
947 call_private
->public.call_hangup_cb
= call_hangup_cb
;
948 call_private
->public.error_cb
= error_cb
;
955 void sipe_media_hangup(struct sipe_media_call_private
*call_private
)
958 sipe_backend_media_hangup(call_private
->public.backend_private
,
964 sipe_media_stream_add(struct sipe_core_private
*sipe_private
, const gchar
*id
,
965 const gchar
*with
, SipeMediaType type
,
966 SipeIceVersion ice_version
, gboolean initiator
)
968 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
969 struct sipe_media_stream_private
*stream_private
;
970 struct sipe_backend_media_stream
*backend_stream
;
971 struct sipe_backend_media_relays
*backend_media_relays
;
973 backend_media_relays
= sipe_backend_media_relays_convert(
974 sipe_private
->media_relays
,
975 sipe_private
->media_relay_username
,
976 sipe_private
->media_relay_password
);
978 backend_stream
= sipe_backend_media_add_stream(SIPE_MEDIA_CALL
,
979 id
, with
, type
, ice_version
,
980 initiator
, backend_media_relays
);
982 sipe_backend_media_relays_free(backend_media_relays
);
984 if (!backend_stream
) {
988 stream_private
= g_new0(struct sipe_media_stream_private
, 1);
989 SIPE_MEDIA_STREAM
->id
= g_strdup(id
);
990 SIPE_MEDIA_STREAM
->backend_private
= backend_stream
;
995 stream_private
->encryption_key
= g_new0(guchar
, SIPE_SRTP_KEY_LEN
);
996 for (i
= 0; i
!= SIPE_SRTP_KEY_LEN
; ++i
) {
997 stream_private
->encryption_key
[i
] = rand() & 0xff;
1002 sipe_private
->media_call
->streams
=
1003 g_slist_append(sipe_private
->media_call
->streams
,
1010 sipe_media_initiate_call(struct sipe_core_private
*sipe_private
,
1011 const char *with
, SipeIceVersion ice_version
,
1012 gboolean with_video
)
1014 struct sip_session
*session
;
1015 struct sip_dialog
*dialog
;
1017 if (sipe_private
->media_call
)
1020 sipe_private
->media_call
= sipe_media_call_new(sipe_private
, with
, TRUE
,
1023 session
= sipe_session_add_call(sipe_private
, with
);
1024 dialog
= sipe_dialog_add(session
);
1025 dialog
->callid
= gencallid();
1026 dialog
->with
= g_strdup(session
->with
);
1027 dialog
->ourtag
= gentag();
1029 sipe_private
->media_call
->public.with
= g_strdup(session
->with
);
1031 if (!sipe_media_stream_add(sipe_private
, "audio", with
, SIPE_MEDIA_AUDIO
,
1032 sipe_private
->media_call
->ice_version
,
1034 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
1036 _("Error creating audio stream"));
1037 sipe_media_hangup(sipe_private
->media_call
);
1042 !sipe_media_stream_add(sipe_private
, "video", with
, SIPE_MEDIA_VIDEO
,
1043 sipe_private
->media_call
->ice_version
,
1045 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
1047 _("Error creating video stream"));
1048 sipe_media_hangup(sipe_private
->media_call
);
1052 // Processing continues in stream_initialized_cb
1056 sipe_core_media_initiate_call(struct sipe_core_public
*sipe_public
,
1058 gboolean with_video
)
1060 sipe_media_initiate_call(SIPE_CORE_PRIVATE
, with
,
1061 SIPE_ICE_RFC_5245
, with_video
);
1064 void sipe_core_media_connect_conference(struct sipe_core_public
*sipe_public
,
1065 struct sipe_chat_session
*chat_session
)
1067 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1068 struct sip_session
*session
;
1069 struct sip_dialog
*dialog
;
1070 SipeIceVersion ice_version
;
1074 if (!sipe_conf_supports_mcu_type(sipe_private
, "audio-video")) {
1075 sipe_backend_notify_error(sipe_public
, _("Join conference call"),
1076 _("Conference calls are not supported on this server."));
1080 session
= sipe_session_find_chat(sipe_private
, chat_session
);
1082 if (sipe_private
->media_call
|| !session
)
1085 session
->is_call
= TRUE
;
1087 parts
= g_strsplit(chat_session
->id
, "app:conf:focus:", 2);
1088 av_uri
= g_strjoinv("app:conf:audio-video:", parts
);
1091 ice_version
= SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013
) ? SIPE_ICE_RFC_5245
:
1094 sipe_private
->media_call
= sipe_media_call_new(sipe_private
, av_uri
,
1097 session
= sipe_session_add_call(sipe_private
, av_uri
);
1098 dialog
= sipe_dialog_add(session
);
1099 dialog
->callid
= gencallid();
1100 dialog
->with
= g_strdup(session
->with
);
1101 dialog
->ourtag
= gentag();
1105 sipe_private
->media_call
->public.with
= g_strdup(session
->with
);
1107 if (!sipe_media_stream_add(sipe_private
, "audio", dialog
->with
,
1109 sipe_private
->media_call
->ice_version
,
1111 sipe_backend_notify_error(sipe_public
,
1113 _("Error creating audio stream"));
1114 sipe_media_hangup(sipe_private
->media_call
);
1115 sipe_private
->media_call
= NULL
;
1118 // Processing continues in stream_initialized_cb
1121 gboolean
sipe_core_media_in_call(struct sipe_core_public
*sipe_public
)
1124 return SIPE_CORE_PRIVATE
->media_call
!= NULL
;
1129 static gboolean
phone_number_is_valid(const gchar
*phone_number
)
1131 if (!phone_number
|| sipe_strequal(phone_number
, "")) {
1135 if (*phone_number
== '+') {
1139 while (*phone_number
!= '\0') {
1140 if (!g_ascii_isdigit(*phone_number
)) {
1149 void sipe_core_media_phone_call(struct sipe_core_public
*sipe_public
,
1150 const gchar
*phone_number
)
1152 g_return_if_fail(sipe_public
);
1154 if (phone_number_is_valid(phone_number
)) {
1155 gchar
*phone_uri
= g_strdup_printf("sip:%s@%s;user=phone",
1156 phone_number
, sipe_public
->sip_domain
);
1158 sipe_core_media_initiate_call(sipe_public
, phone_uri
, FALSE
);
1162 sipe_backend_notify_error(sipe_public
,
1163 _("Unable to establish a call"),
1164 _("Invalid phone number"));
1168 void sipe_core_media_test_call(struct sipe_core_public
*sipe_public
)
1170 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1171 if (!sipe_private
->test_call_bot_uri
) {
1172 sipe_backend_notify_error(sipe_public
,
1173 _("Unable to establish a call"),
1174 _("Audio Test Service is not available."));
1178 sipe_core_media_initiate_call(sipe_public
,
1179 sipe_private
->test_call_bot_uri
, FALSE
);
1183 process_incoming_invite_call(struct sipe_core_private
*sipe_private
,
1186 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
1187 struct sdpmsg
*smsg
;
1188 gboolean has_new_media
= FALSE
;
1194 if (!is_media_session_msg(call_private
, msg
)) {
1195 sip_transport_response(sipe_private
, msg
, 486, "Busy Here", NULL
);
1199 self
= sip_uri_self(sipe_private
);
1200 if (sipe_strequal(SIPE_MEDIA_CALL
->with
, self
)) {
1202 sip_transport_response(sipe_private
, msg
, 488, "Not Acceptable Here", NULL
);
1208 smsg
= sdpmsg_parse_msg(msg
->body
);
1210 sip_transport_response(sipe_private
, msg
,
1211 488, "Not Acceptable Here", NULL
);
1212 sipe_media_hangup(call_private
);
1216 if (!call_private
) {
1217 gchar
*with
= parse_from(sipmsg_find_header(msg
, "From"));
1218 struct sip_session
*session
;
1220 call_private
= sipe_media_call_new(sipe_private
, with
, FALSE
, smsg
->ice_version
);
1221 session
= sipe_session_add_call(sipe_private
, with
);
1222 sipe_media_dialog_init(session
, msg
);
1224 SIPE_MEDIA_CALL
->with
= g_strdup(session
->with
);
1225 sipe_private
->media_call
= call_private
;
1229 if (call_private
->invitation
)
1230 sipmsg_free(call_private
->invitation
);
1231 call_private
->invitation
= sipmsg_copy(msg
);
1233 // Create any new media streams
1234 for (i
= smsg
->media
; i
; i
= i
->next
) {
1235 struct sdpmedia
*media
= i
->data
;
1236 gchar
*id
= media
->name
;
1239 if ( media
->port
!= 0
1240 && !sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL
, id
)) {
1243 if (sipe_strequal(id
, "audio"))
1244 type
= SIPE_MEDIA_AUDIO
;
1245 else if (sipe_strequal(id
, "video"))
1246 type
= SIPE_MEDIA_VIDEO
;
1250 with
= parse_from(sipmsg_find_header(msg
, "From"));
1251 sipe_media_stream_add(sipe_private
, id
, with
, type
,
1252 smsg
->ice_version
, FALSE
);
1253 has_new_media
= TRUE
;
1258 if (has_new_media
) {
1259 sdpmsg_free(call_private
->smsg
);
1260 call_private
->smsg
= smsg
;
1261 sip_transport_response(sipe_private
, call_private
->invitation
,
1262 180, "Ringing", NULL
);
1263 // Processing continues in stream_initialized_cb
1265 apply_remote_message(call_private
, smsg
);
1266 send_response_with_session_description(call_private
, 200, "OK");
1272 void process_incoming_cancel_call(struct sipe_core_private
*sipe_private
,
1275 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
1277 // We respond to the CANCEL request with 200 OK response and
1278 // with 487 Request Terminated to the remote INVITE in progress.
1279 sip_transport_response(sipe_private
, msg
, 200, "OK", NULL
);
1281 if (call_private
->invitation
) {
1282 sip_transport_response(sipe_private
, call_private
->invitation
,
1283 487, "Request Terminated", NULL
);
1286 sipe_media_hangup(call_private
);
1290 sipe_media_send_ack(struct sipe_core_private
*sipe_private
,
1292 struct transaction
*trans
)
1294 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
1295 struct sip_session
*session
;
1296 struct sip_dialog
*dialog
;
1299 if (!is_media_session_msg(call_private
, msg
))
1302 session
= sipe_session_find_call(sipe_private
, SIPE_MEDIA_CALL
->with
);
1303 dialog
= session
->dialogs
->data
;
1307 tmp_cseq
= dialog
->cseq
;
1309 dialog
->cseq
= sip_transaction_cseq(trans
) - 1;
1310 sip_transport_ack(sipe_private
, dialog
);
1311 dialog
->cseq
= tmp_cseq
;
1313 dialog
->outgoing_invite
= NULL
;
1319 sipe_media_send_final_ack(struct sipe_core_private
*sipe_private
,
1321 struct transaction
*trans
)
1323 if (!sipe_media_send_ack(sipe_private
, msg
, trans
))
1326 sipe_backend_media_accept(sipe_private
->media_call
->public.backend_private
,
1333 reinvite_on_candidate_pair_cb(struct sipe_core_public
*sipe_public
)
1335 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1336 struct sipe_media_call_private
*media_call
= sipe_private
->media_call
;
1342 streams
= media_call
->streams
;
1344 for (; streams
; streams
= streams
->next
) {
1345 struct sipe_media_stream
*s
= streams
->data
;
1346 GList
*remote_candidates
= sipe_backend_media_get_active_remote_candidates(&media_call
->public, s
);
1347 guint components
= g_list_length(remote_candidates
);
1349 sipe_media_candidate_list_free(remote_candidates
);
1351 // We must have candidates for both (RTP + RTCP) components ready
1352 if (components
< 2) {
1353 sipe_schedule_mseconds(sipe_private
,
1354 "<+media-reinvite-on-candidate-pair>",
1357 (sipe_schedule_action
) reinvite_on_candidate_pair_cb
,
1363 sipe_invite_call(sipe_private
, sipe_media_send_final_ack
);
1367 maybe_retry_call_with_ice_version(struct sipe_core_private
*sipe_private
,
1368 SipeIceVersion ice_version
,
1369 struct transaction
*trans
)
1371 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
1373 if (call_private
->ice_version
!= ice_version
&&
1374 sip_transaction_cseq(trans
) == 1) {
1375 gchar
*with
= g_strdup(SIPE_MEDIA_CALL
->with
);
1376 gboolean with_video
= sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL
, "video") != NULL
;
1378 sipe_media_hangup(call_private
);
1379 SIPE_DEBUG_INFO("Retrying call with ICEv%d.",
1380 ice_version
== SIPE_ICE_DRAFT_6
? 6 : 19);
1381 sipe_media_initiate_call(sipe_private
, with
, ice_version
,
1392 process_invite_call_response(struct sipe_core_private
*sipe_private
,
1394 struct transaction
*trans
)
1397 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
1398 struct sip_session
*session
;
1399 struct sip_dialog
*dialog
;
1400 struct sdpmsg
*smsg
;
1402 if (!is_media_session_msg(call_private
, msg
))
1405 session
= sipe_session_find_call(sipe_private
, SIPE_MEDIA_CALL
->with
);
1406 dialog
= session
->dialogs
->data
;
1408 with
= dialog
->with
;
1410 dialog
->outgoing_invite
= NULL
;
1412 if (msg
->response
>= 400) {
1413 // Call rejected by remote peer or an error occurred
1415 GString
*desc
= g_string_new("");
1416 gboolean append_responsestr
= FALSE
;
1418 switch (msg
->response
) {
1420 title
= _("User unavailable");
1422 if (sipmsg_parse_warning(msg
, NULL
) == 391) {
1423 g_string_append_printf(desc
, _("%s does not want to be disturbed"), with
);
1425 g_string_append_printf(desc
, _("User %s is not available"), with
);
1430 title
= _("Call rejected");
1431 g_string_append_printf(desc
, _("User %s rejected call"), with
);
1434 // OCS/Lync really sends response string with 'Mutipart' typo.
1435 if (sipe_strequal(msg
->responsestr
, "Mutipart mime in content type not supported by Archiving CDR service") &&
1436 maybe_retry_call_with_ice_version(sipe_private
, SIPE_ICE_DRAFT_6
, trans
)) {
1439 title
= _("Unsupported media type");
1442 /* Check for incompatible encryption levels error.
1445 * 488 Not Acceptable Here
1446 * ms-client-diagnostics: 52017;reason="Encryption levels dont match"
1448 * older clients (and SIPE itself):
1449 * 488 Encryption Levels not compatible
1451 const gchar
*ms_diag
= sipmsg_find_header(msg
, "ms-client-diagnostics");
1452 SipeIceVersion retry_ice_version
= SIPE_ICE_DRAFT_6
;
1454 if (sipe_strequal(msg
->responsestr
, "Encryption Levels not compatible") ||
1455 (ms_diag
&& g_str_has_prefix(ms_diag
, "52017;"))) {
1456 title
= _("Unable to establish a call");
1457 g_string_append(desc
, _("Encryption settings of peer are incompatible with ours."));
1461 /* Check if this is failed conference using
1462 * ICEv6 with reason "Error parsing SDP" and
1463 * retry using ICEv19. */
1464 ms_diag
= sipmsg_find_header(msg
, "ms-diagnostics");
1465 if (ms_diag
&& g_str_has_prefix(ms_diag
, "7008;")) {
1466 retry_ice_version
= SIPE_ICE_RFC_5245
;
1469 if (maybe_retry_call_with_ice_version(sipe_private
, retry_ice_version
, trans
)) {
1472 // Break intentionally omitted
1475 title
= _("Error occured");
1476 g_string_append(desc
, _("Unable to establish a call"));
1477 append_responsestr
= TRUE
;
1481 if (append_responsestr
) {
1482 gchar
*reason
= sipmsg_get_ms_diagnostics_reason(msg
);
1484 g_string_append_printf(desc
, "\n%d %s",
1485 msg
->response
, msg
->responsestr
);
1487 g_string_append_printf(desc
, "\n\n%s", reason
);
1492 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, title
, desc
->str
);
1493 g_string_free(desc
, TRUE
);
1495 sipe_media_send_ack(sipe_private
, msg
, trans
);
1496 sipe_media_hangup(call_private
);
1501 sipe_dialog_parse(dialog
, msg
, TRUE
);
1502 smsg
= sdpmsg_parse_msg(msg
->body
);
1504 sip_transport_response(sipe_private
, msg
,
1505 488, "Not Acceptable Here", NULL
);
1506 sipe_media_hangup(call_private
);
1510 apply_remote_message(call_private
, smsg
);
1513 sipe_media_send_ack(sipe_private
, msg
, trans
);
1514 reinvite_on_candidate_pair_cb(SIPE_CORE_PUBLIC
);
1519 gboolean
is_media_session_msg(struct sipe_media_call_private
*call_private
,
1523 const gchar
*callid
= sipmsg_find_header(msg
, "Call-ID");
1524 struct sip_session
*session
;
1526 session
= sipe_session_find_call(call_private
->sipe_private
,
1527 SIPE_MEDIA_CALL
->with
);
1529 struct sip_dialog
*dialog
= session
->dialogs
->data
;
1530 return sipe_strequal(dialog
->callid
, callid
);
1536 void sipe_media_handle_going_offline(struct sipe_media_call_private
*call_private
)
1538 struct sipe_backend_media
*backend_private
;
1540 backend_private
= call_private
->public.backend_private
;
1542 if (!sipe_backend_media_is_initiator(SIPE_MEDIA_CALL
, NULL
) &&
1543 !sipe_backend_media_accepted(backend_private
)) {
1544 sip_transport_response(call_private
->sipe_private
,
1545 call_private
->invitation
,
1546 480, "Temporarily Unavailable", NULL
);
1548 struct sip_session
*session
;
1550 session
= sipe_session_find_call(call_private
->sipe_private
,
1551 SIPE_MEDIA_CALL
->with
);
1553 sipe_session_close(call_private
->sipe_private
, session
);
1556 sipe_media_hangup(call_private
);
1559 gboolean
sipe_media_is_conference_call(struct sipe_media_call_private
*call_private
)
1561 return g_strstr_len(SIPE_MEDIA_CALL
->with
, -1, "app:conf:audio-video:") != NULL
;
1565 sipe_media_relay_free(struct sipe_media_relay
*relay
)
1567 g_free(relay
->hostname
);
1568 if (relay
->dns_query
)
1569 sipe_backend_dns_query_cancel(relay
->dns_query
);
1574 sipe_media_relay_list_free(GSList
*list
)
1576 for (; list
; list
= g_slist_delete_link(list
, list
))
1577 sipe_media_relay_free(list
->data
);
1581 relay_ip_resolved_cb(struct sipe_media_relay
* relay
,
1582 const gchar
*ip
, SIPE_UNUSED_PARAMETER guint port
)
1584 gchar
*hostname
= relay
->hostname
;
1585 relay
->dns_query
= NULL
;
1588 relay
->hostname
= g_strdup(ip
);
1589 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname
, ip
);
1591 relay
->hostname
= NULL
;
1592 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname
);
1599 process_get_av_edge_credentials_response(struct sipe_core_private
*sipe_private
,
1601 SIPE_UNUSED_PARAMETER
struct transaction
*trans
)
1603 g_free(sipe_private
->media_relay_username
);
1604 g_free(sipe_private
->media_relay_password
);
1605 sipe_media_relay_list_free(sipe_private
->media_relays
);
1606 sipe_private
->media_relay_username
= NULL
;
1607 sipe_private
->media_relay_password
= NULL
;
1608 sipe_private
->media_relays
= NULL
;
1610 if (msg
->response
>= 400) {
1611 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
1612 "Failed to obtain A/V Edge credentials.");
1616 if (msg
->response
== 200) {
1617 sipe_xml
*xn_response
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1619 if (sipe_strequal("OK", sipe_xml_attribute(xn_response
, "reasonPhrase"))) {
1620 const sipe_xml
*xn_credentials
= sipe_xml_child(xn_response
, "credentialsResponse/credentials");
1621 const sipe_xml
*xn_relays
= sipe_xml_child(xn_response
, "credentialsResponse/mediaRelayList");
1622 const sipe_xml
*item
;
1623 GSList
*relays
= NULL
;
1625 item
= sipe_xml_child(xn_credentials
, "username");
1626 sipe_private
->media_relay_username
= sipe_xml_data(item
);
1627 item
= sipe_xml_child(xn_credentials
, "password");
1628 sipe_private
->media_relay_password
= sipe_xml_data(item
);
1630 for (item
= sipe_xml_child(xn_relays
, "mediaRelay"); item
; item
= sipe_xml_twin(item
)) {
1631 struct sipe_media_relay
*relay
= g_new0(struct sipe_media_relay
, 1);
1632 const sipe_xml
*node
;
1635 node
= sipe_xml_child(item
, "hostName");
1636 relay
->hostname
= sipe_xml_data(node
);
1638 node
= sipe_xml_child(item
, "udpPort");
1640 relay
->udp_port
= atoi(tmp
= sipe_xml_data(node
));
1644 node
= sipe_xml_child(item
, "tcpPort");
1646 relay
->tcp_port
= atoi(tmp
= sipe_xml_data(node
));
1650 relays
= g_slist_append(relays
, relay
);
1652 relay
->dns_query
= sipe_backend_dns_query_a(
1656 (sipe_dns_resolved_cb
) relay_ip_resolved_cb
,
1659 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
1661 relay
->tcp_port
, relay
->udp_port
);
1664 sipe_private
->media_relays
= relays
;
1667 sipe_xml_free(xn_response
);
1674 sipe_media_get_av_edge_credentials(struct sipe_core_private
*sipe_private
)
1676 // TODO: re-request credentials after duration expires?
1677 static const char CRED_REQUEST_XML
[] =
1678 "<request requestID=\"%d\" "
1682 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
1683 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
1684 "<credentialsRequest credentialsRequestID=\"%d\">"
1685 "<identity>%s</identity>"
1686 "<location>%s</location>"
1687 "<duration>480</duration>"
1688 "</credentialsRequest>"
1691 int request_id
= rand();
1695 if (!sipe_private
->mras_uri
)
1698 self
= sip_uri_self(sipe_private
);
1700 body
= g_strdup_printf(
1704 sipe_private
->mras_uri
,
1707 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER
) ? "internet" : "intranet");
1710 sip_transport_service(sipe_private
,
1711 sipe_private
->mras_uri
,
1712 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
1714 process_get_av_edge_credentials_response
);