6 * Copyright (C) 2011-13 SIPE Project <http://sipe.sourceforge.net/>
7 * Copyright (C) 2010 Jakub Adam <jakub.adam@ktknet.cz>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include "sipe-common.h"
36 #include "sip-transport.h"
37 #include "sipe-backend.h"
39 #include "sipe-chat.h"
40 #include "sipe-core.h"
41 #include "sipe-core-private.h"
42 #include "sipe-dialog.h"
43 #include "sipe-media.h"
44 #include "sipe-ocs2007.h"
45 #include "sipe-session.h"
46 #include "sipe-utils.h"
48 #include "sipe-schedule.h"
51 struct sipe_media_call_private
{
52 struct sipe_media_call
public;
54 /* private part starts here */
55 struct sipe_core_private
*sipe_private
;
58 struct sipmsg
*invitation
;
59 SipeIceVersion ice_version
;
60 gboolean encryption_compatible
;
65 #define SIPE_MEDIA_CALL ((struct sipe_media_call *) call_private)
66 #define SIPE_MEDIA_CALL_PRIVATE ((struct sipe_media_call_private *) call)
68 static void sipe_media_codec_list_free(GList
*codecs
)
70 for (; codecs
; codecs
= g_list_delete_link(codecs
, codecs
))
71 sipe_backend_codec_free(codecs
->data
);
74 static void sipe_media_candidate_list_free(GList
*candidates
)
76 for (; candidates
; candidates
= g_list_delete_link(candidates
, candidates
))
77 sipe_backend_candidate_free(candidates
->data
);
81 sipe_media_call_free(struct sipe_media_call_private
*call_private
)
84 struct sip_session
*session
;
85 sipe_backend_media_free(call_private
->public.backend_private
);
87 session
= sipe_session_find_call(call_private
->sipe_private
,
90 sipe_session_remove(call_private
->sipe_private
, session
);
92 if (call_private
->invitation
)
93 sipmsg_free(call_private
->invitation
);
95 sdpmsg_free(call_private
->smsg
);
96 sipe_utils_slist_free_full(call_private
->failed_media
,
97 (GDestroyNotify
)sdpmedia_free
);
98 g_free(call_private
->with
);
104 backend_candidates_to_sdpcandidate(GList
*candidates
)
106 GSList
*result
= NULL
;
109 for (i
= candidates
; i
; i
= i
->next
) {
110 struct sipe_backend_candidate
*candidate
= i
->data
;
111 struct sdpcandidate
*c
= g_new(struct sdpcandidate
, 1);
113 c
->foundation
= sipe_backend_candidate_get_foundation(candidate
);
114 c
->component
= sipe_backend_candidate_get_component_type(candidate
);
115 c
->type
= sipe_backend_candidate_get_type(candidate
);
116 c
->protocol
= sipe_backend_candidate_get_protocol(candidate
);
117 c
->ip
= sipe_backend_candidate_get_ip(candidate
);
118 c
->port
= sipe_backend_candidate_get_port(candidate
);
119 c
->base_ip
= sipe_backend_candidate_get_base_ip(candidate
);
120 c
->base_port
= sipe_backend_candidate_get_base_port(candidate
);
121 c
->priority
= sipe_backend_candidate_get_priority(candidate
);
122 c
->username
= sipe_backend_candidate_get_username(candidate
);
123 c
->password
= sipe_backend_candidate_get_password(candidate
);
125 result
= g_slist_append(result
, c
);
132 get_stream_ip_and_ports(GSList
*candidates
,
133 gchar
**ip
, guint
*rtp_port
, guint
*rtcp_port
,
134 SipeCandidateType type
)
140 for (; candidates
; candidates
= candidates
->next
) {
141 struct sdpcandidate
*candidate
= candidates
->data
;
143 if (type
== SIPE_CANDIDATE_TYPE_ANY
|| candidate
->type
== type
) {
145 *ip
= g_strdup(candidate
->ip
);
146 } else if (!sipe_strequal(*ip
, candidate
->ip
)) {
150 if (candidate
->component
== SIPE_COMPONENT_RTP
) {
151 *rtp_port
= candidate
->port
;
152 } else if (candidate
->component
== SIPE_COMPONENT_RTCP
)
153 *rtcp_port
= candidate
->port
;
156 if (*rtp_port
!= 0 && *rtcp_port
!= 0)
161 static struct sdpmedia
*
162 backend_stream_to_sdpmedia(struct sipe_backend_media
*backend_media
,
163 struct sipe_backend_stream
*backend_stream
)
165 struct sdpmedia
*media
= g_new0(struct sdpmedia
, 1);
166 GList
*codecs
= sipe_backend_get_local_codecs(backend_media
,
170 GSList
*attributes
= NULL
;
174 media
->name
= g_strdup(sipe_backend_stream_get_id(backend_stream
));
176 if (sipe_strequal(media
->name
, "audio"))
177 type
= SIPE_MEDIA_AUDIO
;
178 else if (sipe_strequal(media
->name
, "video"))
179 type
= SIPE_MEDIA_VIDEO
;
181 // TODO: incompatible media, should not happen here
184 sipe_media_codec_list_free(codecs
);
189 for (i
= codecs
; i
; i
= i
->next
) {
190 struct sipe_backend_codec
*codec
= i
->data
;
191 struct sdpcodec
*c
= g_new0(struct sdpcodec
, 1);
194 c
->id
= sipe_backend_codec_get_id(codec
);
195 c
->name
= sipe_backend_codec_get_name(codec
);
196 c
->clock_rate
= sipe_backend_codec_get_clock_rate(codec
);
199 params
= sipe_backend_codec_get_optional_parameters(codec
);
200 for (; params
; params
= params
->next
) {
201 struct sipnameval
*param
= params
->data
;
202 struct sipnameval
*copy
= g_new0(struct sipnameval
, 1);
204 copy
->name
= g_strdup(param
->name
);
205 copy
->value
= g_strdup(param
->value
);
207 c
->parameters
= g_slist_append(c
->parameters
, copy
);
210 media
->codecs
= g_slist_append(media
->codecs
, c
);
213 sipe_media_codec_list_free(codecs
);
215 // Process local candidates
216 // If we have established candidate pairs, send them in SDP response.
217 // Otherwise send all available local candidates.
218 candidates
= sipe_backend_media_get_active_local_candidates(backend_media
,
221 candidates
= sipe_backend_get_local_candidates(backend_media
,
224 media
->candidates
= backend_candidates_to_sdpcandidate(candidates
);
226 sipe_media_candidate_list_free(candidates
);
228 get_stream_ip_and_ports(media
->candidates
, &media
->ip
, &media
->port
,
229 &rtcp_port
, SIPE_CANDIDATE_TYPE_HOST
);
230 // No usable HOST candidates, use any candidate
231 if (media
->ip
== NULL
&& media
->candidates
) {
232 get_stream_ip_and_ports(media
->candidates
, &media
->ip
, &media
->port
,
233 &rtcp_port
, SIPE_CANDIDATE_TYPE_ANY
);
236 if (sipe_backend_stream_is_held(backend_stream
))
237 attributes
= sipe_utils_nameval_add(attributes
, "inactive", "");
240 gchar
*tmp
= g_strdup_printf("%u", rtcp_port
);
241 attributes
= sipe_utils_nameval_add(attributes
, "rtcp", tmp
);
245 attributes
= sipe_utils_nameval_add(attributes
, "encryption", "rejected");
247 media
->attributes
= attributes
;
249 // Process remote candidates
250 candidates
= sipe_backend_media_get_active_remote_candidates(backend_media
,
252 media
->remote_candidates
= backend_candidates_to_sdpcandidate(candidates
);
253 sipe_media_candidate_list_free(candidates
);
258 static struct sdpmsg
*
259 sipe_media_to_sdpmsg(struct sipe_media_call_private
*call_private
)
261 struct sipe_backend_media
*backend_media
= call_private
->public.backend_private
;
262 struct sdpmsg
*msg
= g_new0(struct sdpmsg
, 1);
263 GSList
*streams
= sipe_backend_media_get_streams(backend_media
);
265 for (; streams
; streams
= streams
->next
) {
266 struct sdpmedia
*media
= backend_stream_to_sdpmedia(backend_media
, streams
->data
);
268 msg
->media
= g_slist_append(msg
->media
, media
);
271 msg
->ip
= g_strdup(media
->ip
);
275 msg
->media
= g_slist_concat(msg
->media
, call_private
->failed_media
);
276 call_private
->failed_media
= NULL
;
278 msg
->ice_version
= call_private
->ice_version
;
284 sipe_invite_call(struct sipe_core_private
*sipe_private
, TransCallback tc
)
288 gchar
*p_preferred_identity
= NULL
;
290 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
291 struct sip_session
*session
;
292 struct sip_dialog
*dialog
;
294 gboolean add_2007_fallback
= FALSE
;
296 session
= sipe_session_find_call(sipe_private
, call_private
->with
);
297 dialog
= session
->dialogs
->data
;
298 add_2007_fallback
= dialog
->cseq
== 0 &&
299 call_private
->ice_version
== SIPE_ICE_RFC_5245
&&
300 !sipe_strequal(call_private
->with
, sipe_private
->test_call_bot_uri
);
302 contact
= get_contact(sipe_private
);
304 if (sipe_private
->uc_line_uri
) {
305 gchar
*self
= sip_uri_self(sipe_private
);
306 p_preferred_identity
= g_strdup_printf(
307 "P-Preferred-Identity: <%s>, <%s>\r\n",
308 self
, sipe_private
->uc_line_uri
);
312 hdr
= g_strdup_printf(
313 "ms-keep-alive: UAC;hop-hop=yes\r\n"
316 "Content-Type: %s\r\n",
318 p_preferred_identity
? p_preferred_identity
: "",
320 "multipart/alternative;boundary=\"----=_NextPart_000_001E_01CB4397.0B5EB570\""
321 : "application/sdp");
323 g_free(p_preferred_identity
);
325 msg
= sipe_media_to_sdpmsg(call_private
);
326 body
= sdpmsg_to_string(msg
);
328 if (add_2007_fallback
) {
330 tmp
= g_strdup_printf(
331 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
332 "Content-Type: application/sdp\r\n"
333 "Content-Transfer-Encoding: 7bit\r\n"
334 "Content-Disposition: session; handling=optional; ms-proxy-2007fallback\r\n"
336 "o=- 0 0 IN IP4 %s\r\n"
339 "m=audio 0 RTP/AVP\r\n"
341 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
342 "Content-Type: application/sdp\r\n"
343 "Content-Transfer-Encoding: 7bit\r\n"
344 "Content-Disposition: session; handling=optional\r\n"
348 "------=_NextPart_000_001E_01CB4397.0B5EB570--\r\n",
349 msg
->ip
, msg
->ip
, body
);
356 dialog
->outgoing_invite
= sip_transport_invite(sipe_private
,
366 static struct sip_dialog
*
367 sipe_media_dialog_init(struct sip_session
* session
, struct sipmsg
*msg
)
369 gchar
*newTag
= gentag();
370 const gchar
*oldHeader
;
372 struct sip_dialog
*dialog
;
374 oldHeader
= sipmsg_find_header(msg
, "To");
375 newHeader
= g_strdup_printf("%s;tag=%s", oldHeader
, newTag
);
376 sipmsg_remove_header_now(msg
, "To");
377 sipmsg_add_header_now(msg
, "To", newHeader
);
380 dialog
= sipe_dialog_add(session
);
381 dialog
->callid
= g_strdup(sipmsg_find_header(msg
, "Call-ID"));
382 dialog
->with
= parse_from(sipmsg_find_header(msg
, "From"));
383 sipe_dialog_parse(dialog
, msg
, FALSE
);
389 send_response_with_session_description(struct sipe_media_call_private
*call_private
, int code
, gchar
*text
)
391 struct sdpmsg
*msg
= sipe_media_to_sdpmsg(call_private
);
392 gchar
*body
= sdpmsg_to_string(msg
);
394 sipmsg_add_header(call_private
->invitation
, "Content-Type", "application/sdp");
395 sip_transport_response(call_private
->sipe_private
, call_private
->invitation
, code
, text
, body
);
400 encryption_levels_compatible(struct sdpmsg
*msg
)
404 for (i
= msg
->media
; i
; i
= i
->next
) {
405 const gchar
*enc_level
;
406 struct sdpmedia
*m
= i
->data
;
408 enc_level
= sipe_utils_nameval_find(m
->attributes
, "encryption");
410 // Decline call if peer requires encryption as we don't support it yet.
411 if (sipe_strequal(enc_level
, "required"))
419 process_invite_call_response(struct sipe_core_private
*sipe_private
,
421 struct transaction
*trans
);
424 update_remote_media(struct sipe_media_call_private
* call_private
,
425 struct sdpmedia
*media
)
427 struct sipe_backend_media
*backend_media
= SIPE_MEDIA_CALL
->backend_private
;
428 struct sipe_backend_stream
*backend_stream
;
429 GList
*backend_candidates
= NULL
;
430 GList
*backend_codecs
= NULL
;
432 gboolean result
= TRUE
;
434 backend_stream
= sipe_backend_media_get_stream_by_id(backend_media
,
436 if (media
->port
== 0) {
438 sipe_backend_media_remove_stream(backend_media
, backend_stream
);
445 for (i
= media
->codecs
; i
; i
= i
->next
) {
446 struct sdpcodec
*c
= i
->data
;
447 struct sipe_backend_codec
*codec
;
450 codec
= sipe_backend_codec_new(c
->id
,
455 for (j
= c
->parameters
; j
; j
= j
->next
) {
456 struct sipnameval
*attr
= j
->data
;
458 sipe_backend_codec_add_optional_parameter(codec
,
463 backend_codecs
= g_list_append(backend_codecs
, codec
);
466 result
= sipe_backend_set_remote_codecs(backend_media
,
469 sipe_media_codec_list_free(backend_codecs
);
471 if (result
== FALSE
) {
472 sipe_backend_media_remove_stream(backend_media
, backend_stream
);
476 for (i
= media
->candidates
; i
; i
= i
->next
) {
477 struct sdpcandidate
*c
= i
->data
;
478 struct sipe_backend_candidate
*candidate
;
479 candidate
= sipe_backend_candidate_new(c
->foundation
,
487 sipe_backend_candidate_set_priority(candidate
, c
->priority
);
489 backend_candidates
= g_list_append(backend_candidates
, candidate
);
492 sipe_backend_media_add_remote_candidates(backend_media
,
495 sipe_media_candidate_list_free(backend_candidates
);
497 if (sipe_utils_nameval_find(media
->attributes
, "inactive")) {
498 sipe_backend_stream_hold(backend_media
, backend_stream
, FALSE
);
499 } else if (sipe_backend_stream_is_held(backend_stream
)) {
500 sipe_backend_stream_unhold(backend_media
, backend_stream
, FALSE
);
507 apply_remote_message(struct sipe_media_call_private
* call_private
,
512 sipe_utils_slist_free_full(call_private
->failed_media
, (GDestroyNotify
)sdpmedia_free
);
513 call_private
->failed_media
= NULL
;
515 for (i
= msg
->media
; i
; i
= i
->next
) {
516 struct sdpmedia
*media
= i
->data
;
517 if (!update_remote_media(call_private
, media
)) {
519 call_private
->failed_media
=
520 g_slist_append(call_private
->failed_media
, media
);
524 /* We need to keep failed medias until response is sent, remove them
525 * from sdpmsg that is to be freed. */
526 for (i
= call_private
->failed_media
; i
; i
= i
->next
) {
527 msg
->media
= g_slist_remove(msg
->media
, i
->data
);
530 call_private
->encryption_compatible
= encryption_levels_compatible(msg
);
534 call_initialized(struct sipe_media_call
*call
)
537 sipe_backend_media_get_streams(call
->backend_private
);
539 for (; streams
; streams
= streams
->next
) {
540 if (!sipe_backend_stream_initialized(call
->backend_private
,
549 // Sends an invite response when the call is accepted and local candidates were
550 // prepared, otherwise does nothing. If error response is sent, call_private is
551 // disposed before function returns. Returns true when response was sent.
553 send_invite_response_if_ready(struct sipe_media_call_private
*call_private
)
555 struct sipe_backend_media
*backend_media
;
557 backend_media
= call_private
->public.backend_private
;
559 if (!sipe_backend_media_accepted(backend_media
) ||
560 !call_initialized(&call_private
->public))
563 if (!call_private
->encryption_compatible
) {
564 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
566 sipmsg_add_header(call_private
->invitation
, "Warning",
567 "308 lcs.microsoft.com \"Encryption Levels not compatible\"");
568 sip_transport_response(sipe_private
,
569 call_private
->invitation
,
570 488, "Encryption Levels not compatible",
572 sipe_backend_media_reject(backend_media
, FALSE
);
573 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
574 _("Unable to establish a call"),
575 _("Encryption settings of peer are incompatible with ours."));
577 send_response_with_session_description(call_private
, 200, "OK");
584 stream_initialized_cb(struct sipe_media_call
*call
,
585 struct sipe_backend_stream
*stream
)
587 if (call_initialized(call
)) {
588 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
589 struct sipe_backend_media
*backend_private
= call
->backend_private
;
591 if (sipe_backend_media_is_initiator(backend_private
, stream
)) {
592 sipe_invite_call(call_private
->sipe_private
,
593 process_invite_call_response
);
594 } else if (call_private
->smsg
) {
595 struct sdpmsg
*smsg
= call_private
->smsg
;
596 call_private
->smsg
= NULL
;
598 apply_remote_message(call_private
, smsg
);
599 send_invite_response_if_ready(call_private
);
605 static void phone_state_publish(struct sipe_core_private
*sipe_private
)
607 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
608 sipe_ocs2007_phone_state_publish(sipe_private
);
610 // TODO: OCS 2005 support. Is anyone still using it at all?
615 media_end_cb(struct sipe_media_call
*call
)
617 g_return_if_fail(call
);
619 SIPE_MEDIA_CALL_PRIVATE
->sipe_private
->media_call
= NULL
;
620 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
);
621 sipe_media_call_free(SIPE_MEDIA_CALL_PRIVATE
);
625 call_accept_cb(struct sipe_media_call
*call
, gboolean local
)
628 send_invite_response_if_ready(SIPE_MEDIA_CALL_PRIVATE
);
630 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
);
634 call_reject_cb(struct sipe_media_call
*call
, gboolean local
)
637 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
638 sip_transport_response(call_private
->sipe_private
,
639 call_private
->invitation
,
640 603, "Decline", NULL
);
645 sipe_media_send_ack(struct sipe_core_private
*sipe_private
, struct sipmsg
*msg
,
646 struct transaction
*trans
);
648 static void call_hold_cb(struct sipe_media_call
*call
,
650 SIPE_UNUSED_PARAMETER gboolean state
)
653 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
,
654 sipe_media_send_ack
);
657 static void call_hangup_cb(struct sipe_media_call
*call
, gboolean local
)
660 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
661 struct sip_session
*session
;
662 session
= sipe_session_find_call(call_private
->sipe_private
,
666 sipe_session_close(call_private
->sipe_private
, session
);
672 error_cb(struct sipe_media_call
*call
, gchar
*message
)
674 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
675 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
676 gboolean initiator
= sipe_backend_media_is_initiator(call
->backend_private
, NULL
);
677 gboolean accepted
= sipe_backend_media_accepted(call
->backend_private
);
679 gchar
*title
= g_strdup_printf("Call with %s failed", call_private
->with
);
680 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, title
, message
);
683 if (!initiator
&& !accepted
) {
684 sip_transport_response(sipe_private
,
685 call_private
->invitation
,
686 488, "Not Acceptable Here", NULL
);
689 sipe_backend_media_hangup(call
->backend_private
, initiator
|| accepted
);
692 static struct sipe_media_call_private
*
693 sipe_media_call_new(struct sipe_core_private
*sipe_private
,
694 const gchar
* with
, gboolean initiator
, SipeIceVersion ice_version
)
696 struct sipe_media_call_private
*call_private
= g_new0(struct sipe_media_call_private
, 1);
699 call_private
->sipe_private
= sipe_private
;
701 cname
= g_strdup(sipe_private
->contact
+ 1);
702 cname
[strlen(cname
) - 1] = '\0';
704 call_private
->public.backend_private
= sipe_backend_media_new(SIPE_CORE_PUBLIC
,
708 sipe_backend_media_set_cname(call_private
->public.backend_private
, cname
);
710 call_private
->ice_version
= ice_version
;
711 call_private
->encryption_compatible
= TRUE
;
713 call_private
->public.stream_initialized_cb
= stream_initialized_cb
;
714 call_private
->public.media_end_cb
= media_end_cb
;
715 call_private
->public.call_accept_cb
= call_accept_cb
;
716 call_private
->public.call_reject_cb
= call_reject_cb
;
717 call_private
->public.call_hold_cb
= call_hold_cb
;
718 call_private
->public.call_hangup_cb
= call_hangup_cb
;
719 call_private
->public.error_cb
= error_cb
;
726 void sipe_media_hangup(struct sipe_media_call_private
*call_private
)
729 sipe_backend_media_hangup(call_private
->public.backend_private
,
735 sipe_media_initiate_call(struct sipe_core_private
*sipe_private
,
736 const char *with
, SipeIceVersion ice_version
,
739 struct sipe_media_call_private
*call_private
;
740 struct sipe_backend_media
*backend_media
;
741 struct sipe_backend_media_relays
*backend_media_relays
;
742 struct sip_session
*session
;
743 struct sip_dialog
*dialog
;
745 if (sipe_private
->media_call
)
748 call_private
= sipe_media_call_new(sipe_private
, with
, TRUE
, ice_version
);
750 session
= sipe_session_add_call(sipe_private
, with
);
751 dialog
= sipe_dialog_add(session
);
752 dialog
->callid
= gencallid();
753 dialog
->with
= g_strdup(session
->with
);
754 dialog
->ourtag
= gentag();
756 call_private
->with
= g_strdup(session
->with
);
758 backend_media
= call_private
->public.backend_private
;
760 backend_media_relays
=
761 sipe_backend_media_relays_convert(sipe_private
->media_relays
,
762 sipe_private
->media_relay_username
,
763 sipe_private
->media_relay_password
);
765 if (!sipe_backend_media_add_stream(backend_media
,
766 "audio", with
, SIPE_MEDIA_AUDIO
,
767 call_private
->ice_version
, TRUE
,
768 backend_media_relays
)) {
769 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
771 _("Error creating audio stream"));
772 sipe_media_call_free(call_private
);
773 sipe_backend_media_relays_free(backend_media_relays
);
778 && !sipe_backend_media_add_stream(backend_media
,
779 "video", with
, SIPE_MEDIA_VIDEO
,
780 call_private
->ice_version
, TRUE
,
781 backend_media_relays
)) {
782 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
784 _("Error creating video stream"));
785 sipe_media_call_free(call_private
);
786 sipe_backend_media_relays_free(backend_media_relays
);
790 sipe_private
->media_call
= call_private
;
792 sipe_backend_media_relays_free(backend_media_relays
);
794 // Processing continues in stream_initialized_cb
798 sipe_core_media_initiate_call(struct sipe_core_public
*sipe_public
,
802 sipe_media_initiate_call(SIPE_CORE_PRIVATE
, with
,
803 SIPE_ICE_RFC_5245
, with_video
);
806 void sipe_core_media_connect_conference(struct sipe_core_public
*sipe_public
,
807 struct sipe_chat_session
*chat_session
)
809 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
810 struct sipe_backend_media_relays
*backend_media_relays
;
811 struct sip_session
*session
;
812 struct sip_dialog
*dialog
;
816 session
= sipe_session_find_chat(sipe_private
, chat_session
);
818 if (sipe_private
->media_call
|| !session
)
821 session
->is_call
= TRUE
;
823 parts
= g_strsplit(chat_session
->id
, "app:conf:focus:", 2);
824 av_uri
= g_strjoinv("app:conf:audio-video:", parts
);
827 sipe_private
->media_call
= sipe_media_call_new(sipe_private
, av_uri
,
828 TRUE
, SIPE_ICE_DRAFT_6
);
830 session
= sipe_session_add_call(sipe_private
, av_uri
);
831 dialog
= sipe_dialog_add(session
);
832 dialog
->callid
= gencallid();
833 dialog
->with
= g_strdup(session
->with
);
834 dialog
->ourtag
= gentag();
838 sipe_private
->media_call
->with
= g_strdup(session
->with
);
840 backend_media_relays
=
841 sipe_backend_media_relays_convert(sipe_private
->media_relays
,
842 sipe_private
->media_relay_username
,
843 sipe_private
->media_relay_password
);
845 if (!sipe_backend_media_add_stream(sipe_private
->media_call
->public.backend_private
,
846 "audio", dialog
->with
,
848 sipe_private
->media_call
->ice_version
,
849 TRUE
, backend_media_relays
)) {
850 sipe_backend_notify_error(sipe_public
,
852 _("Error creating audio stream"));
853 sipe_media_call_free(sipe_private
->media_call
);
854 sipe_private
->media_call
= NULL
;
857 sipe_backend_media_relays_free(backend_media_relays
);
859 // Processing continues in stream_initialized_cb
862 gboolean
sipe_core_media_in_call(struct sipe_core_public
*sipe_public
)
865 return SIPE_CORE_PRIVATE
->media_call
!= NULL
;
870 static gboolean
phone_number_is_valid(const gchar
*phone_number
)
872 if (!phone_number
|| sipe_strequal(phone_number
, "")) {
876 if (*phone_number
== '+') {
880 while (*phone_number
!= '\0') {
881 if (!g_ascii_isdigit(*phone_number
)) {
890 void sipe_core_media_phone_call(struct sipe_core_public
*sipe_public
,
891 const gchar
*phone_number
)
893 g_return_if_fail(sipe_public
);
895 if (phone_number_is_valid(phone_number
)) {
896 gchar
*phone_uri
= g_strdup_printf("sip:%s@%s;user=phone",
897 phone_number
, sipe_public
->sip_domain
);
899 sipe_core_media_initiate_call(sipe_public
, phone_uri
, FALSE
);
903 sipe_backend_notify_error(sipe_public
,
904 _("Unable to establish a call"),
905 _("Invalid phone number"));
909 void sipe_core_media_test_call(struct sipe_core_public
*sipe_public
)
911 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
912 if (!sipe_private
->test_call_bot_uri
) {
913 sipe_backend_notify_error(sipe_public
,
914 _("Unable to establish a call"),
915 _("Audio Test Service is not available."));
919 sipe_core_media_initiate_call(sipe_public
,
920 sipe_private
->test_call_bot_uri
, FALSE
);
924 process_incoming_invite_call(struct sipe_core_private
*sipe_private
,
927 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
928 struct sipe_backend_media
*backend_media
;
929 struct sipe_backend_media_relays
*backend_media_relays
= NULL
;
931 gboolean has_new_media
= FALSE
;
934 if (call_private
&& !is_media_session_msg(call_private
, msg
)) {
935 sip_transport_response(sipe_private
, msg
, 486, "Busy Here", NULL
);
939 smsg
= sdpmsg_parse_msg(msg
->body
);
941 sip_transport_response(sipe_private
, msg
,
942 488, "Not Acceptable Here", NULL
);
943 sipe_media_hangup(call_private
);
948 gchar
*with
= parse_from(sipmsg_find_header(msg
, "From"));
949 struct sip_session
*session
;
951 call_private
= sipe_media_call_new(sipe_private
, with
, FALSE
, smsg
->ice_version
);
952 session
= sipe_session_add_call(sipe_private
, with
);
953 sipe_media_dialog_init(session
, msg
);
955 call_private
->with
= g_strdup(session
->with
);
956 sipe_private
->media_call
= call_private
;
960 backend_media
= call_private
->public.backend_private
;
962 if (call_private
->invitation
)
963 sipmsg_free(call_private
->invitation
);
964 call_private
->invitation
= sipmsg_copy(msg
);
967 backend_media_relays
= sipe_backend_media_relays_convert(
968 sipe_private
->media_relays
,
969 sipe_private
->media_relay_username
,
970 sipe_private
->media_relay_password
);
972 // Create any new media streams
973 for (i
= smsg
->media
; i
; i
= i
->next
) {
974 struct sdpmedia
*media
= i
->data
;
975 gchar
*id
= media
->name
;
978 if ( media
->port
!= 0
979 && !sipe_backend_media_get_stream_by_id(backend_media
, id
)) {
982 if (sipe_strequal(id
, "audio"))
983 type
= SIPE_MEDIA_AUDIO
;
984 else if (sipe_strequal(id
, "video"))
985 type
= SIPE_MEDIA_VIDEO
;
989 with
= parse_from(sipmsg_find_header(msg
, "From"));
990 sipe_backend_media_add_stream(backend_media
, id
, with
,
994 backend_media_relays
);
995 has_new_media
= TRUE
;
1000 sipe_backend_media_relays_free(backend_media_relays
);
1002 if (has_new_media
) {
1003 sdpmsg_free(call_private
->smsg
);
1004 call_private
->smsg
= smsg
;
1005 sip_transport_response(sipe_private
, call_private
->invitation
,
1006 180, "Ringing", NULL
);
1007 // Processing continues in stream_initialized_cb
1009 apply_remote_message(call_private
, smsg
);
1010 send_response_with_session_description(call_private
, 200, "OK");
1016 void process_incoming_cancel_call(struct sipe_core_private
*sipe_private
,
1019 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
1021 // We respond to the CANCEL request with 200 OK response and
1022 // with 487 Request Terminated to the remote INVITE in progress.
1023 sip_transport_response(sipe_private
, msg
, 200, "OK", NULL
);
1025 if (call_private
->invitation
) {
1026 sip_transport_response(sipe_private
, call_private
->invitation
,
1027 487, "Request Terminated", NULL
);
1030 sipe_media_hangup(call_private
);
1034 sipe_media_send_ack(struct sipe_core_private
*sipe_private
,
1036 struct transaction
*trans
)
1038 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
1039 struct sip_session
*session
;
1040 struct sip_dialog
*dialog
;
1043 if (!is_media_session_msg(call_private
, msg
))
1046 session
= sipe_session_find_call(sipe_private
, call_private
->with
);
1047 dialog
= session
->dialogs
->data
;
1051 tmp_cseq
= dialog
->cseq
;
1053 dialog
->cseq
= sip_transaction_cseq(trans
) - 1;
1054 sip_transport_ack(sipe_private
, dialog
);
1055 dialog
->cseq
= tmp_cseq
;
1057 dialog
->outgoing_invite
= NULL
;
1063 sipe_media_send_final_ack(struct sipe_core_private
*sipe_private
,
1065 struct transaction
*trans
)
1067 if (!sipe_media_send_ack(sipe_private
, msg
, trans
))
1070 sipe_backend_media_accept(sipe_private
->media_call
->public.backend_private
,
1077 reinvite_on_candidate_pair_cb(struct sipe_core_public
*sipe_public
)
1079 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1080 struct sipe_media_call_private
*media_call
= sipe_private
->media_call
;
1081 struct sipe_backend_media
*backend_media
;
1087 backend_media
= media_call
->public.backend_private
;
1088 streams
= sipe_backend_media_get_streams(backend_media
);
1090 for (; streams
; streams
= streams
->next
) {
1091 struct sipe_backend_stream
*s
= streams
->data
;
1092 GList
*remote_candidates
= sipe_backend_media_get_active_remote_candidates(backend_media
, s
);
1093 guint components
= g_list_length(remote_candidates
);
1095 sipe_media_candidate_list_free(remote_candidates
);
1097 // We must have candidates for both (RTP + RTCP) components ready
1098 if (components
< 2) {
1099 sipe_schedule_mseconds(sipe_private
,
1100 "<+media-reinvite-on-candidate-pair>",
1103 (sipe_schedule_action
) reinvite_on_candidate_pair_cb
,
1109 sipe_invite_call(sipe_private
, sipe_media_send_final_ack
);
1113 maybe_retry_call_with_ice_v6(struct sipe_core_private
*sipe_private
,
1114 struct transaction
*trans
)
1116 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
1118 if (call_private
->ice_version
== SIPE_ICE_RFC_5245
&&
1119 sip_transaction_cseq(trans
) == 1) {
1120 gchar
*with
= g_strdup(call_private
->with
);
1121 struct sipe_backend_media
*backend_private
= call_private
->public.backend_private
;
1122 gboolean with_video
= sipe_backend_media_get_stream_by_id(backend_private
, "video") != NULL
;
1124 sipe_media_hangup(call_private
);
1125 SIPE_DEBUG_INFO_NOFORMAT("Retrying call witn ICEv6.");
1126 // We might be calling to OC 2007 instance, retry with ICEv6
1127 sipe_media_initiate_call(sipe_private
, with
,
1128 SIPE_ICE_DRAFT_6
, with_video
);
1138 process_invite_call_response(struct sipe_core_private
*sipe_private
,
1140 struct transaction
*trans
)
1143 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
1144 struct sip_session
*session
;
1145 struct sip_dialog
*dialog
;
1146 struct sdpmsg
*smsg
;
1148 if (!is_media_session_msg(call_private
, msg
))
1151 session
= sipe_session_find_call(sipe_private
, call_private
->with
);
1152 dialog
= session
->dialogs
->data
;
1154 with
= dialog
->with
;
1156 dialog
->outgoing_invite
= NULL
;
1158 if (msg
->response
>= 400) {
1159 // Call rejected by remote peer or an error occurred
1161 GString
*desc
= g_string_new("");
1162 gboolean append_responsestr
= FALSE
;
1164 switch (msg
->response
) {
1166 title
= _("User unavailable");
1168 if (sipmsg_parse_warning(msg
, NULL
) == 391) {
1169 g_string_append_printf(desc
, _("%s does not want to be disturbed"), with
);
1171 g_string_append_printf(desc
, _("User %s is not available"), with
);
1176 title
= _("Call rejected");
1177 g_string_append_printf(desc
, _("User %s rejected call"), with
);
1180 // OCS/Lync really sends response string with 'Mutipart' typo.
1181 if (sipe_strequal(msg
->responsestr
, "Mutipart mime in content type not supported by Archiving CDR service") &&
1182 maybe_retry_call_with_ice_v6(sipe_private
, trans
)) {
1185 title
= _("Unsupported media type");
1188 /* Check for incompatible encryption levels error.
1191 * 488 Not Acceptable Here
1192 * ms-client-diagnostics: 52017;reason="Encryption levels dont match"
1194 * older clients (and SIPE itself):
1195 * 488 Encryption Levels not compatible
1197 const gchar
*ms_diag
= sipmsg_find_header(msg
, "ms-client-diagnostics");
1199 if (sipe_strequal(msg
->responsestr
, "Encryption Levels not compatible") ||
1200 (ms_diag
&& g_str_has_prefix(ms_diag
, "52017;"))) {
1201 title
= _("Unable to establish a call");
1202 g_string_append(desc
, _("Encryption settings of peer are incompatible with ours."));
1206 if (maybe_retry_call_with_ice_v6(sipe_private
, trans
)) {
1209 // Break intentionally omitted
1212 title
= _("Error occured");
1213 g_string_append(desc
, _("Unable to establish a call"));
1214 append_responsestr
= TRUE
;
1218 if (append_responsestr
) {
1219 gchar
*reason
= sipmsg_get_ms_diagnostics_reason(msg
);
1221 g_string_append_printf(desc
, "\n%d %s",
1222 msg
->response
, msg
->responsestr
);
1224 g_string_append_printf(desc
, "\n\n%s", reason
);
1229 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, title
, desc
->str
);
1230 g_string_free(desc
, TRUE
);
1232 sipe_media_send_ack(sipe_private
, msg
, trans
);
1233 sipe_media_hangup(call_private
);
1238 sipe_dialog_parse(dialog
, msg
, TRUE
);
1239 smsg
= sdpmsg_parse_msg(msg
->body
);
1241 sip_transport_response(sipe_private
, msg
,
1242 488, "Not Acceptable Here", NULL
);
1243 sipe_media_hangup(call_private
);
1247 apply_remote_message(call_private
, smsg
);
1250 sipe_media_send_ack(sipe_private
, msg
, trans
);
1251 reinvite_on_candidate_pair_cb(SIPE_CORE_PUBLIC
);
1256 gboolean
is_media_session_msg(struct sipe_media_call_private
*call_private
,
1260 const gchar
*callid
= sipmsg_find_header(msg
, "Call-ID");
1261 struct sip_session
*session
;
1263 session
= sipe_session_find_call(call_private
->sipe_private
,
1264 call_private
->with
);
1266 struct sip_dialog
*dialog
= session
->dialogs
->data
;
1267 return sipe_strequal(dialog
->callid
, callid
);
1273 void sipe_media_handle_going_offline(struct sipe_media_call_private
*call_private
)
1275 struct sipe_backend_media
*backend_private
;
1277 backend_private
= call_private
->public.backend_private
;
1279 if ( !sipe_backend_media_is_initiator(backend_private
, NULL
)
1280 && !sipe_backend_media_accepted(backend_private
)) {
1281 sip_transport_response(call_private
->sipe_private
,
1282 call_private
->invitation
,
1283 480, "Temporarily Unavailable", NULL
);
1285 struct sip_session
*session
;
1287 session
= sipe_session_find_call(call_private
->sipe_private
,
1288 call_private
->with
);
1290 sipe_session_close(call_private
->sipe_private
, session
);
1293 sipe_media_hangup(call_private
);
1296 gboolean
sipe_media_is_conference_call(struct sipe_media_call_private
*call_private
)
1298 return g_strstr_len(call_private
->with
, -1, "app:conf:audio-video:") != NULL
;
1302 sipe_media_relay_free(struct sipe_media_relay
*relay
)
1304 g_free(relay
->hostname
);
1305 if (relay
->dns_query
)
1306 sipe_backend_dns_query_cancel(relay
->dns_query
);
1311 sipe_media_relay_list_free(GSList
*list
)
1313 for (; list
; list
= g_slist_delete_link(list
, list
))
1314 sipe_media_relay_free(list
->data
);
1318 relay_ip_resolved_cb(struct sipe_media_relay
* relay
,
1319 const gchar
*ip
, SIPE_UNUSED_PARAMETER guint port
)
1321 gchar
*hostname
= relay
->hostname
;
1322 relay
->dns_query
= NULL
;
1325 relay
->hostname
= g_strdup(ip
);
1326 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname
, ip
);
1328 relay
->hostname
= NULL
;
1329 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname
);
1336 process_get_av_edge_credentials_response(struct sipe_core_private
*sipe_private
,
1338 SIPE_UNUSED_PARAMETER
struct transaction
*trans
)
1340 g_free(sipe_private
->media_relay_username
);
1341 g_free(sipe_private
->media_relay_password
);
1342 sipe_media_relay_list_free(sipe_private
->media_relays
);
1343 sipe_private
->media_relay_username
= NULL
;
1344 sipe_private
->media_relay_password
= NULL
;
1345 sipe_private
->media_relays
= NULL
;
1347 if (msg
->response
>= 400) {
1348 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
1349 "Failed to obtain A/V Edge credentials.");
1353 if (msg
->response
== 200) {
1354 sipe_xml
*xn_response
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1356 if (sipe_strequal("OK", sipe_xml_attribute(xn_response
, "reasonPhrase"))) {
1357 const sipe_xml
*xn_credentials
= sipe_xml_child(xn_response
, "credentialsResponse/credentials");
1358 const sipe_xml
*xn_relays
= sipe_xml_child(xn_response
, "credentialsResponse/mediaRelayList");
1359 const sipe_xml
*item
;
1360 GSList
*relays
= NULL
;
1362 item
= sipe_xml_child(xn_credentials
, "username");
1363 sipe_private
->media_relay_username
= sipe_xml_data(item
);
1364 item
= sipe_xml_child(xn_credentials
, "password");
1365 sipe_private
->media_relay_password
= sipe_xml_data(item
);
1367 for (item
= sipe_xml_child(xn_relays
, "mediaRelay"); item
; item
= sipe_xml_twin(item
)) {
1368 struct sipe_media_relay
*relay
= g_new0(struct sipe_media_relay
, 1);
1369 const sipe_xml
*node
;
1372 node
= sipe_xml_child(item
, "hostName");
1373 relay
->hostname
= sipe_xml_data(node
);
1375 node
= sipe_xml_child(item
, "udpPort");
1377 relay
->udp_port
= atoi(tmp
= sipe_xml_data(node
));
1381 node
= sipe_xml_child(item
, "tcpPort");
1383 relay
->tcp_port
= atoi(tmp
= sipe_xml_data(node
));
1387 relays
= g_slist_append(relays
, relay
);
1389 relay
->dns_query
= sipe_backend_dns_query_a(
1393 (sipe_dns_resolved_cb
) relay_ip_resolved_cb
,
1396 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
1398 relay
->tcp_port
, relay
->udp_port
);
1401 sipe_private
->media_relays
= relays
;
1404 sipe_xml_free(xn_response
);
1411 sipe_media_get_av_edge_credentials(struct sipe_core_private
*sipe_private
)
1413 // TODO: re-request credentials after duration expires?
1414 const char CRED_REQUEST_XML
[] =
1415 "<request requestID=\"%d\" "
1419 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
1420 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
1421 "<credentialsRequest credentialsRequestID=\"%d\">"
1422 "<identity>%s</identity>"
1423 "<location>%s</location>"
1424 "<duration>480</duration>"
1425 "</credentialsRequest>"
1428 int request_id
= rand();
1432 if (!sipe_private
->mras_uri
)
1435 self
= sip_uri_self(sipe_private
);
1437 body
= g_strdup_printf(
1441 sipe_private
->mras_uri
,
1444 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER
) ? "internet" : "intranet");
1447 sip_transport_service(sipe_private
,
1448 sipe_private
->mras_uri
,
1449 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
1451 process_get_av_edge_credentials_response
);