6 * Copyright (C) 2011-12 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 g_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
)
139 for (; candidates
; candidates
= candidates
->next
) {
140 struct sdpcandidate
*candidate
= candidates
->data
;
142 if (type
== SIPE_CANDIDATE_TYPE_ANY
|| candidate
->type
== type
) {
143 if (candidate
->component
== SIPE_COMPONENT_RTP
) {
144 *rtp_port
= candidate
->port
;
145 *ip
= g_strdup(candidate
->ip
);
146 } else if (candidate
->component
== SIPE_COMPONENT_RTCP
)
147 *rtcp_port
= candidate
->port
;
150 if (*rtp_port
!= 0 && *rtcp_port
!= 0)
155 static struct sdpmedia
*
156 backend_stream_to_sdpmedia(struct sipe_backend_media
*backend_media
,
157 struct sipe_backend_stream
*backend_stream
)
159 struct sdpmedia
*media
= g_new0(struct sdpmedia
, 1);
160 GList
*codecs
= sipe_backend_get_local_codecs(backend_media
,
164 GSList
*attributes
= NULL
;
168 media
->name
= g_strdup(sipe_backend_stream_get_id(backend_stream
));
170 if (sipe_strequal(media
->name
, "audio"))
171 type
= SIPE_MEDIA_AUDIO
;
172 else if (sipe_strequal(media
->name
, "video"))
173 type
= SIPE_MEDIA_VIDEO
;
175 // TODO: incompatible media, should not happen here
178 sipe_media_codec_list_free(codecs
);
183 for (i
= codecs
; i
; i
= i
->next
) {
184 struct sipe_backend_codec
*codec
= i
->data
;
185 struct sdpcodec
*c
= g_new0(struct sdpcodec
, 1);
188 c
->id
= sipe_backend_codec_get_id(codec
);
189 c
->name
= sipe_backend_codec_get_name(codec
);
190 c
->clock_rate
= sipe_backend_codec_get_clock_rate(codec
);
193 params
= sipe_backend_codec_get_optional_parameters(codec
);
194 for (; params
; params
= params
->next
) {
195 struct sipnameval
*param
= params
->data
;
196 struct sipnameval
*copy
= g_new0(struct sipnameval
, 1);
198 copy
->name
= g_strdup(param
->name
);
199 copy
->value
= g_strdup(param
->value
);
201 c
->parameters
= g_slist_append(c
->parameters
, copy
);
204 media
->codecs
= g_slist_append(media
->codecs
, c
);
207 sipe_media_codec_list_free(codecs
);
209 // Process local candidates
210 // If we have established candidate pairs, send them in SDP response.
211 // Otherwise send all available local candidates.
212 candidates
= sipe_backend_media_get_active_local_candidates(backend_media
,
215 candidates
= sipe_backend_get_local_candidates(backend_media
,
218 media
->candidates
= backend_candidates_to_sdpcandidate(candidates
);
220 sipe_media_candidate_list_free(candidates
);
222 get_stream_ip_and_ports(media
->candidates
, &media
->ip
, &media
->port
,
223 &rtcp_port
, SIPE_CANDIDATE_TYPE_HOST
);
224 // No usable HOST candidates, use any candidate
225 if (media
->ip
== NULL
&& media
->candidates
) {
226 get_stream_ip_and_ports(media
->candidates
, &media
->ip
, &media
->port
,
227 &rtcp_port
, SIPE_CANDIDATE_TYPE_ANY
);
230 if (sipe_backend_stream_is_held(backend_stream
))
231 attributes
= sipe_utils_nameval_add(attributes
, "inactive", "");
234 gchar
*tmp
= g_strdup_printf("%u", rtcp_port
);
235 attributes
= sipe_utils_nameval_add(attributes
, "rtcp", tmp
);
239 attributes
= sipe_utils_nameval_add(attributes
, "encryption", "rejected");
241 media
->attributes
= attributes
;
243 // Process remote candidates
244 candidates
= sipe_backend_media_get_active_remote_candidates(backend_media
,
246 media
->remote_candidates
= backend_candidates_to_sdpcandidate(candidates
);
247 sipe_media_candidate_list_free(candidates
);
252 static struct sdpmsg
*
253 sipe_media_to_sdpmsg(struct sipe_media_call_private
*call_private
)
255 struct sipe_backend_media
*backend_media
= call_private
->public.backend_private
;
256 struct sdpmsg
*msg
= g_new0(struct sdpmsg
, 1);
257 GSList
*streams
= sipe_backend_media_get_streams(backend_media
);
259 for (; streams
; streams
= streams
->next
) {
260 struct sdpmedia
*media
= backend_stream_to_sdpmedia(backend_media
, streams
->data
);
262 msg
->media
= g_slist_append(msg
->media
, media
);
265 msg
->ip
= g_strdup(media
->ip
);
269 msg
->media
= g_slist_concat(msg
->media
, call_private
->failed_media
);
270 call_private
->failed_media
= NULL
;
272 msg
->ice_version
= call_private
->ice_version
;
278 sipe_invite_call(struct sipe_core_private
*sipe_private
, TransCallback tc
)
283 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
284 struct sip_session
*session
;
285 struct sip_dialog
*dialog
;
287 gboolean add_2007_fallback
= FALSE
;
289 session
= sipe_session_find_call(sipe_private
, call_private
->with
);
290 dialog
= session
->dialogs
->data
;
291 add_2007_fallback
= dialog
->cseq
== 0 && call_private
->ice_version
== SIPE_ICE_RFC_5245
;
293 contact
= get_contact(sipe_private
);
294 hdr
= g_strdup_printf(
295 "ms-keep-alive: UAC;hop-hop=yes\r\n"
297 "Content-Type: %s\r\n",
300 "multipart/alternative;boundary=\"----=_NextPart_000_001E_01CB4397.0B5EB570\""
301 : "application/sdp");
304 msg
= sipe_media_to_sdpmsg(call_private
);
305 body
= sdpmsg_to_string(msg
);
307 if (add_2007_fallback
) {
309 tmp
= g_strdup_printf(
310 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
311 "Content-Type: application/sdp\r\n"
312 "Content-Transfer-Encoding: 7bit\r\n"
313 "Content-Disposition: session; handling=optional; ms-proxy-2007fallback\r\n"
315 "o=- 0 0 IN IP4 %s\r\n"
318 "m=audio 0 RTP/AVP\r\n"
320 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
321 "Content-Type: application/sdp\r\n"
322 "Content-Transfer-Encoding: 7bit\r\n"
323 "Content-Disposition: session; handling=optional\r\n"
327 "------=_NextPart_000_001E_01CB4397.0B5EB570--\r\n",
328 msg
->ip
, msg
->ip
, body
);
335 dialog
->outgoing_invite
= sip_transport_invite(sipe_private
,
345 static struct sip_dialog
*
346 sipe_media_dialog_init(struct sip_session
* session
, struct sipmsg
*msg
)
348 gchar
*newTag
= gentag();
349 const gchar
*oldHeader
;
351 struct sip_dialog
*dialog
;
353 oldHeader
= sipmsg_find_header(msg
, "To");
354 newHeader
= g_strdup_printf("%s;tag=%s", oldHeader
, newTag
);
355 sipmsg_remove_header_now(msg
, "To");
356 sipmsg_add_header_now(msg
, "To", newHeader
);
359 dialog
= sipe_dialog_add(session
);
360 dialog
->callid
= g_strdup(sipmsg_find_header(msg
, "Call-ID"));
361 dialog
->with
= parse_from(sipmsg_find_header(msg
, "From"));
362 sipe_dialog_parse(dialog
, msg
, FALSE
);
368 send_response_with_session_description(struct sipe_media_call_private
*call_private
, int code
, gchar
*text
)
370 struct sdpmsg
*msg
= sipe_media_to_sdpmsg(call_private
);
371 gchar
*body
= sdpmsg_to_string(msg
);
373 sipmsg_add_header(call_private
->invitation
, "Content-Type", "application/sdp");
374 sip_transport_response(call_private
->sipe_private
, call_private
->invitation
, code
, text
, body
);
379 encryption_levels_compatible(struct sdpmsg
*msg
)
383 for (i
= msg
->media
; i
; i
= i
->next
) {
384 const gchar
*enc_level
;
385 struct sdpmedia
*m
= i
->data
;
387 enc_level
= sipe_utils_nameval_find(m
->attributes
, "encryption");
389 // Decline call if peer requires encryption as we don't support it yet.
390 if (sipe_strequal(enc_level
, "required"))
398 process_invite_call_response(struct sipe_core_private
*sipe_private
,
400 struct transaction
*trans
);
403 update_remote_media(struct sipe_media_call_private
* call_private
,
404 struct sdpmedia
*media
)
406 struct sipe_backend_media
*backend_media
= SIPE_MEDIA_CALL
->backend_private
;
407 struct sipe_backend_stream
*backend_stream
;
408 GList
*backend_candidates
= NULL
;
409 GList
*backend_codecs
= NULL
;
411 gboolean result
= TRUE
;
413 backend_stream
= sipe_backend_media_get_stream_by_id(backend_media
,
415 if (media
->port
== 0) {
417 sipe_backend_media_remove_stream(backend_media
, backend_stream
);
424 for (i
= media
->codecs
; i
; i
= i
->next
) {
425 struct sdpcodec
*c
= i
->data
;
426 struct sipe_backend_codec
*codec
;
429 codec
= sipe_backend_codec_new(c
->id
,
434 for (j
= c
->parameters
; j
; j
= j
->next
) {
435 struct sipnameval
*attr
= j
->data
;
437 sipe_backend_codec_add_optional_parameter(codec
,
442 backend_codecs
= g_list_append(backend_codecs
, codec
);
445 result
= sipe_backend_set_remote_codecs(backend_media
,
448 sipe_media_codec_list_free(backend_codecs
);
450 if (result
== FALSE
) {
451 sipe_backend_media_remove_stream(backend_media
, backend_stream
);
455 for (i
= media
->candidates
; i
; i
= i
->next
) {
456 struct sdpcandidate
*c
= i
->data
;
457 struct sipe_backend_candidate
*candidate
;
458 candidate
= sipe_backend_candidate_new(c
->foundation
,
466 sipe_backend_candidate_set_priority(candidate
, c
->priority
);
468 backend_candidates
= g_list_append(backend_candidates
, candidate
);
471 sipe_backend_media_add_remote_candidates(backend_media
,
474 sipe_media_candidate_list_free(backend_candidates
);
476 if (sipe_utils_nameval_find(media
->attributes
, "inactive")) {
477 sipe_backend_stream_hold(backend_media
, backend_stream
, FALSE
);
478 } else if (sipe_backend_stream_is_held(backend_stream
)) {
479 sipe_backend_stream_unhold(backend_media
, backend_stream
, FALSE
);
486 apply_remote_message(struct sipe_media_call_private
* call_private
,
491 g_slist_free_full(call_private
->failed_media
, (GDestroyNotify
)sdpmedia_free
);
492 call_private
->failed_media
= NULL
;
494 for (i
= msg
->media
; i
; i
= i
->next
) {
495 struct sdpmedia
*media
= i
->data
;
496 if (!update_remote_media(call_private
, media
)) {
498 call_private
->failed_media
=
499 g_slist_append(call_private
->failed_media
, media
);
503 /* We need to keep failed medias until response is sent, remove them
504 * from sdpmsg that is to be freed. */
505 for (i
= call_private
->failed_media
; i
; i
= i
->next
) {
506 msg
->media
= g_slist_remove(msg
->media
, i
->data
);
509 call_private
->encryption_compatible
= encryption_levels_compatible(msg
);
512 // Sends an invite response when the call is accepted and local candidates were
513 // prepared, otherwise does nothing. If error response is sent, call_private is
514 // disposed before function returns. Returns true when response was sent.
516 send_invite_response_if_ready(struct sipe_media_call_private
*call_private
)
518 struct sipe_backend_media
*backend_media
;
520 backend_media
= call_private
->public.backend_private
;
522 if (!sipe_backend_media_accepted(backend_media
) ||
523 !sipe_backend_candidates_prepared(backend_media
))
526 if (!call_private
->encryption_compatible
) {
527 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
529 sipmsg_add_header(call_private
->invitation
, "Warning",
530 "308 lcs.microsoft.com \"Encryption Levels not compatible\"");
531 sip_transport_response(sipe_private
,
532 call_private
->invitation
,
533 488, "Encryption Levels not compatible",
535 sipe_backend_media_reject(backend_media
, FALSE
);
536 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
537 _("Unable to establish a call"),
538 _("Encryption settings of peer are incompatible with ours."));
540 send_response_with_session_description(call_private
, 200, "OK");
547 candidates_prepared_cb(struct sipe_media_call
*call
,
548 struct sipe_backend_stream
*stream
)
550 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
551 struct sipe_backend_media
*backend_private
= call
->backend_private
;
553 if (sipe_backend_media_is_initiator(backend_private
, stream
)) {
554 sipe_invite_call(call_private
->sipe_private
,
555 process_invite_call_response
);
557 struct sdpmsg
*smsg
= call_private
->smsg
;
558 call_private
->smsg
= NULL
;
560 apply_remote_message(call_private
, smsg
);
561 send_invite_response_if_ready(call_private
);
566 static void phone_state_publish(struct sipe_core_private
*sipe_private
)
568 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
569 sipe_ocs2007_phone_state_publish(sipe_private
);
571 // TODO: OCS 2005 support. Is anyone still using it at all?
576 media_end_cb(struct sipe_media_call
*call
)
578 g_return_if_fail(call
);
580 SIPE_MEDIA_CALL_PRIVATE
->sipe_private
->media_call
= NULL
;
581 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
);
582 sipe_media_call_free(SIPE_MEDIA_CALL_PRIVATE
);
586 call_accept_cb(struct sipe_media_call
*call
, gboolean local
)
589 send_invite_response_if_ready(SIPE_MEDIA_CALL_PRIVATE
);
591 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
);
595 call_reject_cb(struct sipe_media_call
*call
, gboolean local
)
598 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
599 sip_transport_response(call_private
->sipe_private
,
600 call_private
->invitation
,
601 603, "Decline", NULL
);
606 sipe_media_send_ack(struct sipe_core_private
*sipe_private
, struct sipmsg
*msg
,
607 struct transaction
*trans
);
609 static void call_hold_cb(struct sipe_media_call
*call
,
611 SIPE_UNUSED_PARAMETER gboolean state
)
614 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
,
615 sipe_media_send_ack
);
618 static void call_hangup_cb(struct sipe_media_call
*call
, gboolean local
)
621 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
622 struct sip_session
*session
;
623 session
= sipe_session_find_call(call_private
->sipe_private
,
627 sipe_session_close(call_private
->sipe_private
, session
);
633 error_cb(struct sipe_media_call
*call
, gchar
*message
)
635 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
636 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
637 gboolean initiator
= sipe_backend_media_is_initiator(call
->backend_private
, NULL
);
638 gboolean accepted
= sipe_backend_media_accepted(call
->backend_private
);
640 gchar
*title
= g_strdup_printf("Call with %s failed", call_private
->with
);
641 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, title
, message
);
644 if (!initiator
&& !accepted
) {
645 sip_transport_response(sipe_private
,
646 call_private
->invitation
,
647 488, "Not Acceptable Here", NULL
);
650 sipe_backend_media_hangup(call
->backend_private
, initiator
|| accepted
);
653 static struct sipe_media_call_private
*
654 sipe_media_call_new(struct sipe_core_private
*sipe_private
,
655 const gchar
* with
, gboolean initiator
, SipeIceVersion ice_version
)
657 struct sipe_media_call_private
*call_private
= g_new0(struct sipe_media_call_private
, 1);
660 call_private
->sipe_private
= sipe_private
;
662 cname
= g_strdup(sipe_private
->contact
+ 1);
663 cname
[strlen(cname
) - 1] = '\0';
665 call_private
->public.backend_private
= sipe_backend_media_new(SIPE_CORE_PUBLIC
,
669 sipe_backend_media_set_cname(call_private
->public.backend_private
, cname
);
671 call_private
->ice_version
= ice_version
;
672 call_private
->encryption_compatible
= TRUE
;
674 call_private
->public.candidates_prepared_cb
= candidates_prepared_cb
;
675 call_private
->public.media_end_cb
= media_end_cb
;
676 call_private
->public.call_accept_cb
= call_accept_cb
;
677 call_private
->public.call_reject_cb
= call_reject_cb
;
678 call_private
->public.call_hold_cb
= call_hold_cb
;
679 call_private
->public.call_hangup_cb
= call_hangup_cb
;
680 call_private
->public.error_cb
= error_cb
;
687 void sipe_media_hangup(struct sipe_media_call_private
*call_private
)
690 sipe_backend_media_hangup(call_private
->public.backend_private
,
696 sipe_media_initiate_call(struct sipe_core_private
*sipe_private
,
697 const char *with
, SipeIceVersion ice_version
,
700 struct sipe_media_call_private
*call_private
;
701 struct sipe_backend_media
*backend_media
;
702 struct sipe_backend_media_relays
*backend_media_relays
;
703 struct sip_session
*session
;
704 struct sip_dialog
*dialog
;
706 if (sipe_private
->media_call
)
709 call_private
= sipe_media_call_new(sipe_private
, with
, TRUE
, ice_version
);
711 session
= sipe_session_add_call(sipe_private
, with
);
712 dialog
= sipe_dialog_add(session
);
713 dialog
->callid
= gencallid();
714 dialog
->with
= g_strdup(session
->with
);
715 dialog
->ourtag
= gentag();
717 call_private
->with
= g_strdup(session
->with
);
719 backend_media
= call_private
->public.backend_private
;
721 backend_media_relays
=
722 sipe_backend_media_relays_convert(sipe_private
->media_relays
,
723 sipe_private
->media_relay_username
,
724 sipe_private
->media_relay_password
);
726 if (!sipe_backend_media_add_stream(backend_media
,
727 "audio", with
, SIPE_MEDIA_AUDIO
,
728 call_private
->ice_version
, TRUE
,
729 backend_media_relays
)) {
730 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
732 _("Error creating audio stream"));
733 sipe_media_call_free(call_private
);
734 sipe_backend_media_relays_free(backend_media_relays
);
739 && !sipe_backend_media_add_stream(backend_media
,
740 "video", with
, SIPE_MEDIA_VIDEO
,
741 call_private
->ice_version
, TRUE
,
742 backend_media_relays
)) {
743 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
745 _("Error creating video stream"));
746 sipe_media_call_free(call_private
);
747 sipe_backend_media_relays_free(backend_media_relays
);
751 sipe_private
->media_call
= call_private
;
753 sipe_backend_media_relays_free(backend_media_relays
);
755 // Processing continues in candidates_prepared_cb
759 sipe_core_media_initiate_call(struct sipe_core_public
*sipe_public
,
763 sipe_media_initiate_call(SIPE_CORE_PRIVATE
, with
,
764 SIPE_ICE_RFC_5245
, with_video
);
767 void sipe_core_media_connect_conference(struct sipe_core_public
*sipe_public
,
768 struct sipe_chat_session
*chat_session
)
770 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
771 struct sipe_backend_media_relays
*backend_media_relays
;
772 struct sip_session
*session
;
773 struct sip_dialog
*dialog
;
777 session
= sipe_session_find_chat(sipe_private
, chat_session
);
779 if (sipe_private
->media_call
|| !session
)
782 session
->is_call
= TRUE
;
784 parts
= g_strsplit(chat_session
->id
, "app:conf:focus:", 2);
785 av_uri
= g_strjoinv("app:conf:audio-video:", parts
);
788 sipe_private
->media_call
= sipe_media_call_new(sipe_private
, av_uri
,
789 TRUE
, SIPE_ICE_DRAFT_6
);
791 session
= sipe_session_add_call(sipe_private
, av_uri
);
792 dialog
= sipe_dialog_add(session
);
793 dialog
->callid
= gencallid();
794 dialog
->with
= g_strdup(session
->with
);
795 dialog
->ourtag
= gentag();
799 sipe_private
->media_call
->with
= g_strdup(session
->with
);
800 sipe_private
->media_call
->ice_version
= SIPE_ICE_DRAFT_6
;
802 backend_media_relays
=
803 sipe_backend_media_relays_convert(sipe_private
->media_relays
,
804 sipe_private
->media_relay_username
,
805 sipe_private
->media_relay_password
);
807 if (!sipe_backend_media_add_stream(sipe_private
->media_call
->public.backend_private
,
808 "audio", dialog
->with
,
810 SIPE_ICE_DRAFT_6
, TRUE
,
811 backend_media_relays
)) {
812 sipe_backend_notify_error(sipe_public
,
814 _("Error creating audio stream"));
815 sipe_media_call_free(sipe_private
->media_call
);
816 sipe_private
->media_call
= NULL
;
819 sipe_backend_media_relays_free(backend_media_relays
);
821 // Processing continues in candidates_prepared_cb
824 gboolean
sipe_core_media_in_call(struct sipe_core_public
*sipe_public
)
827 return SIPE_CORE_PRIVATE
->media_call
!= NULL
;
832 void sipe_core_media_test_call(struct sipe_core_public
*sipe_public
)
834 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
835 if (!sipe_private
->test_call_bot_uri
) {
836 sipe_backend_notify_error(sipe_public
,
837 _("Unable to establish a call"),
838 _("Audio Test Service is not available."));
842 sipe_core_media_initiate_call(sipe_public
,
843 sipe_private
->test_call_bot_uri
, FALSE
);
847 process_incoming_invite_call(struct sipe_core_private
*sipe_private
,
850 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
851 struct sipe_backend_media
*backend_media
;
852 struct sipe_backend_media_relays
*backend_media_relays
= NULL
;
854 gboolean has_new_media
= FALSE
;
857 if (call_private
&& !is_media_session_msg(call_private
, msg
)) {
858 sip_transport_response(sipe_private
, msg
, 486, "Busy Here", NULL
);
862 smsg
= sdpmsg_parse_msg(msg
->body
);
864 sip_transport_response(sipe_private
, msg
,
865 488, "Not Acceptable Here", NULL
);
866 sipe_media_hangup(call_private
);
871 gchar
*with
= parse_from(sipmsg_find_header(msg
, "From"));
872 struct sip_session
*session
;
874 call_private
= sipe_media_call_new(sipe_private
, with
, FALSE
, smsg
->ice_version
);
875 session
= sipe_session_add_call(sipe_private
, with
);
876 sipe_media_dialog_init(session
, msg
);
878 call_private
->with
= g_strdup(session
->with
);
879 sipe_private
->media_call
= call_private
;
883 backend_media
= call_private
->public.backend_private
;
885 if (call_private
->invitation
)
886 sipmsg_free(call_private
->invitation
);
887 call_private
->invitation
= sipmsg_copy(msg
);
890 backend_media_relays
= sipe_backend_media_relays_convert(
891 sipe_private
->media_relays
,
892 sipe_private
->media_relay_username
,
893 sipe_private
->media_relay_password
);
895 // Create any new media streams
896 for (i
= smsg
->media
; i
; i
= i
->next
) {
897 struct sdpmedia
*media
= i
->data
;
898 gchar
*id
= media
->name
;
901 if ( media
->port
!= 0
902 && !sipe_backend_media_get_stream_by_id(backend_media
, id
)) {
905 if (sipe_strequal(id
, "audio"))
906 type
= SIPE_MEDIA_AUDIO
;
907 else if (sipe_strequal(id
, "video"))
908 type
= SIPE_MEDIA_VIDEO
;
912 with
= parse_from(sipmsg_find_header(msg
, "From"));
913 sipe_backend_media_add_stream(backend_media
, id
, with
,
917 backend_media_relays
);
918 has_new_media
= TRUE
;
923 sipe_backend_media_relays_free(backend_media_relays
);
926 sdpmsg_free(call_private
->smsg
);
927 call_private
->smsg
= smsg
;
928 sip_transport_response(sipe_private
, call_private
->invitation
,
929 180, "Ringing", NULL
);
930 // Processing continues in candidates_prepared_cb
932 apply_remote_message(call_private
, smsg
);
933 send_response_with_session_description(call_private
, 200, "OK");
939 void process_incoming_cancel_call(struct sipe_core_private
*sipe_private
,
942 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
944 // We respond to the CANCEL request with 200 OK response and
945 // with 487 Request Terminated to the remote INVITE in progress.
946 sip_transport_response(sipe_private
, msg
, 200, "OK", NULL
);
948 if (call_private
->invitation
) {
949 sip_transport_response(sipe_private
, call_private
->invitation
,
950 487, "Request Terminated", NULL
);
953 sipe_media_hangup(call_private
);
957 sipe_media_send_ack(struct sipe_core_private
*sipe_private
,
959 struct transaction
*trans
)
961 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
962 struct sip_session
*session
;
963 struct sip_dialog
*dialog
;
966 if (!is_media_session_msg(call_private
, msg
))
969 session
= sipe_session_find_call(sipe_private
, call_private
->with
);
970 dialog
= session
->dialogs
->data
;
974 tmp_cseq
= dialog
->cseq
;
976 dialog
->cseq
= sip_transaction_cseq(trans
) - 1;
977 sip_transport_ack(sipe_private
, dialog
);
978 dialog
->cseq
= tmp_cseq
;
980 dialog
->outgoing_invite
= NULL
;
986 sipe_media_send_final_ack(struct sipe_core_private
*sipe_private
,
988 struct transaction
*trans
)
990 if (!sipe_media_send_ack(sipe_private
, msg
, trans
))
993 sipe_backend_media_accept(sipe_private
->media_call
->public.backend_private
,
1000 reinvite_on_candidate_pair_cb(struct sipe_core_public
*sipe_public
)
1002 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1003 struct sipe_media_call_private
*media_call
= sipe_private
->media_call
;
1004 struct sipe_backend_media
*backend_media
;
1010 backend_media
= media_call
->public.backend_private
;
1011 streams
= sipe_backend_media_get_streams(backend_media
);
1013 for (; streams
; streams
= streams
->next
) {
1014 struct sipe_backend_stream
*s
= streams
->data
;
1015 GList
*remote_candidates
= sipe_backend_media_get_active_remote_candidates(backend_media
, s
);
1016 guint components
= g_list_length(remote_candidates
);
1018 sipe_media_candidate_list_free(remote_candidates
);
1020 // We must have candidates for both (RTP + RTCP) components ready
1021 if (components
< 2) {
1022 sipe_schedule_mseconds(sipe_private
,
1023 "<+media-reinvite-on-candidate-pair>",
1026 (sipe_schedule_action
) reinvite_on_candidate_pair_cb
,
1032 sipe_invite_call(sipe_private
, sipe_media_send_final_ack
);
1036 process_invite_call_response(struct sipe_core_private
*sipe_private
,
1038 struct transaction
*trans
)
1041 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
1042 struct sipe_backend_media
*backend_private
;
1043 struct sip_session
*session
;
1044 struct sip_dialog
*dialog
;
1045 struct sdpmsg
*smsg
;
1047 if (!is_media_session_msg(call_private
, msg
))
1050 session
= sipe_session_find_call(sipe_private
, call_private
->with
);
1051 dialog
= session
->dialogs
->data
;
1053 backend_private
= call_private
->public.backend_private
;
1054 with
= dialog
->with
;
1056 dialog
->outgoing_invite
= NULL
;
1058 if (msg
->response
>= 400) {
1059 // Call rejected by remote peer or an error occurred
1061 GString
*desc
= g_string_new("");
1062 gboolean append_responsestr
= FALSE
;
1064 switch (msg
->response
) {
1066 title
= _("User unavailable");
1068 if (sipmsg_parse_warning(msg
, NULL
) == 391) {
1069 g_string_append_printf(desc
, _("%s does not want to be disturbed"), with
);
1071 g_string_append_printf(desc
, _("User %s is not available"), with
);
1076 title
= _("Call rejected");
1077 g_string_append_printf(desc
, _("User %s rejected call"), with
);
1080 /* Check for incompatible encryption levels error.
1083 * 488 Not Acceptable Here
1084 * ms-client-diagnostics: 52017;reason="Encryption levels dont match"
1086 * older clients (and SIPE itself):
1087 * 488 Encryption Levels not compatible
1089 const gchar
*ms_diag
= sipmsg_find_header(msg
, "ms-client-diagnostics");
1091 if (sipe_strequal(msg
->responsestr
, "Encryption Levels not compatible") ||
1092 (ms_diag
&& g_str_has_prefix(ms_diag
, "52017;"))) {
1093 title
= _("Unable to establish a call");
1094 g_string_append(desc
, _("Encryption settings of peer are incompatible with ours."));
1098 if (call_private
->ice_version
== SIPE_ICE_RFC_5245
&&
1099 sip_transaction_cseq(trans
) == 1) {
1100 gchar
*with
= g_strdup(call_private
->with
);
1101 gboolean with_video
= sipe_backend_media_get_stream_by_id(backend_private
, "video") != NULL
;
1103 sipe_media_hangup(call_private
);
1104 // We might be calling to OC 2007 instance, retry with ICEv6
1105 sipe_media_initiate_call(sipe_private
, with
,
1106 SIPE_ICE_DRAFT_6
, with_video
);
1111 // Break intentionally omitted
1114 title
= _("Error occured");
1115 g_string_append(desc
, _("Unable to establish a call"));
1116 append_responsestr
= TRUE
;
1120 if (append_responsestr
)
1121 g_string_append_printf(desc
, "\n%d %s",
1122 msg
->response
, msg
->responsestr
);
1124 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, title
, desc
->str
);
1125 g_string_free(desc
, TRUE
);
1127 sipe_media_send_ack(sipe_private
, msg
, trans
);
1128 sipe_media_hangup(call_private
);
1133 sipe_dialog_parse(dialog
, msg
, TRUE
);
1134 smsg
= sdpmsg_parse_msg(msg
->body
);
1136 sip_transport_response(sipe_private
, msg
,
1137 488, "Not Acceptable Here", NULL
);
1138 sipe_media_hangup(call_private
);
1142 apply_remote_message(call_private
, smsg
);
1145 sipe_media_send_ack(sipe_private
, msg
, trans
);
1146 reinvite_on_candidate_pair_cb(SIPE_CORE_PUBLIC
);
1151 gboolean
is_media_session_msg(struct sipe_media_call_private
*call_private
,
1155 const gchar
*callid
= sipmsg_find_header(msg
, "Call-ID");
1156 struct sip_session
*session
;
1158 session
= sipe_session_find_call(call_private
->sipe_private
,
1159 call_private
->with
);
1161 struct sip_dialog
*dialog
= session
->dialogs
->data
;
1162 return sipe_strequal(dialog
->callid
, callid
);
1168 void sipe_media_handle_going_offline(struct sipe_media_call_private
*call_private
)
1170 struct sipe_backend_media
*backend_private
;
1172 backend_private
= call_private
->public.backend_private
;
1174 if ( !sipe_backend_media_is_initiator(backend_private
, NULL
)
1175 && !sipe_backend_media_accepted(backend_private
)) {
1176 sip_transport_response(call_private
->sipe_private
,
1177 call_private
->invitation
,
1178 480, "Temporarily Unavailable", NULL
);
1180 struct sip_session
*session
;
1182 session
= sipe_session_find_call(call_private
->sipe_private
,
1183 call_private
->with
);
1185 sipe_session_close(call_private
->sipe_private
, session
);
1188 sipe_media_hangup(call_private
);
1191 gboolean
sipe_media_is_conference_call(struct sipe_media_call_private
*call_private
)
1193 return g_strstr_len(call_private
->with
, -1, "app:conf:audio-video:") != NULL
;
1197 sipe_media_relay_free(struct sipe_media_relay
*relay
)
1199 g_free(relay
->hostname
);
1200 if (relay
->dns_query
)
1201 sipe_backend_dns_query_cancel(relay
->dns_query
);
1206 sipe_media_relay_list_free(GSList
*list
)
1208 for (; list
; list
= g_slist_delete_link(list
, list
))
1209 sipe_media_relay_free(list
->data
);
1213 relay_ip_resolved_cb(struct sipe_media_relay
* relay
,
1214 const gchar
*ip
, SIPE_UNUSED_PARAMETER guint port
)
1216 gchar
*hostname
= relay
->hostname
;
1217 relay
->dns_query
= NULL
;
1220 relay
->hostname
= g_strdup(ip
);
1221 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname
, ip
);
1223 relay
->hostname
= NULL
;
1224 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname
);
1231 process_get_av_edge_credentials_response(struct sipe_core_private
*sipe_private
,
1233 SIPE_UNUSED_PARAMETER
struct transaction
*trans
)
1235 g_free(sipe_private
->media_relay_username
);
1236 g_free(sipe_private
->media_relay_password
);
1237 sipe_media_relay_list_free(sipe_private
->media_relays
);
1238 sipe_private
->media_relay_username
= NULL
;
1239 sipe_private
->media_relay_password
= NULL
;
1240 sipe_private
->media_relays
= NULL
;
1242 if (msg
->response
>= 400) {
1243 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
1244 "Failed to obtain A/V Edge credentials.");
1248 if (msg
->response
== 200) {
1249 sipe_xml
*xn_response
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1251 if (sipe_strequal("OK", sipe_xml_attribute(xn_response
, "reasonPhrase"))) {
1252 const sipe_xml
*xn_credentials
= sipe_xml_child(xn_response
, "credentialsResponse/credentials");
1253 const sipe_xml
*xn_relays
= sipe_xml_child(xn_response
, "credentialsResponse/mediaRelayList");
1254 const sipe_xml
*item
;
1255 GSList
*relays
= NULL
;
1257 item
= sipe_xml_child(xn_credentials
, "username");
1258 sipe_private
->media_relay_username
= sipe_xml_data(item
);
1259 item
= sipe_xml_child(xn_credentials
, "password");
1260 sipe_private
->media_relay_password
= sipe_xml_data(item
);
1262 for (item
= sipe_xml_child(xn_relays
, "mediaRelay"); item
; item
= sipe_xml_twin(item
)) {
1263 struct sipe_media_relay
*relay
= g_new0(struct sipe_media_relay
, 1);
1264 const sipe_xml
*node
;
1267 node
= sipe_xml_child(item
, "hostName");
1268 relay
->hostname
= sipe_xml_data(node
);
1270 node
= sipe_xml_child(item
, "udpPort");
1271 relay
->udp_port
= atoi(tmp
= sipe_xml_data(node
));
1274 node
= sipe_xml_child(item
, "tcpPort");
1275 relay
->tcp_port
= atoi(tmp
= sipe_xml_data(node
));
1278 relays
= g_slist_append(relays
, relay
);
1280 relay
->dns_query
= sipe_backend_dns_query_a(
1284 (sipe_dns_resolved_cb
) relay_ip_resolved_cb
,
1287 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
1289 relay
->tcp_port
, relay
->udp_port
);
1292 sipe_private
->media_relays
= relays
;
1295 sipe_xml_free(xn_response
);
1302 sipe_media_get_av_edge_credentials(struct sipe_core_private
*sipe_private
)
1304 // TODO: re-request credentials after duration expires?
1305 const char CRED_REQUEST_XML
[] =
1306 "<request requestID=\"%d\" "
1310 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
1311 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
1312 "<credentialsRequest credentialsRequestID=\"%d\">"
1313 "<identity>%s</identity>"
1314 "<location>%s</location>"
1315 "<duration>480</duration>"
1316 "</credentialsRequest>"
1319 int request_id
= rand();
1323 if (!sipe_private
->mras_uri
)
1326 self
= sip_uri_self(sipe_private
);
1328 body
= g_strdup_printf(
1332 sipe_private
->mras_uri
,
1335 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER
) ? "internet" : "intranet");
1338 sip_transport_service(sipe_private
,
1339 sipe_private
->mras_uri
,
1340 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
1342 process_get_av_edge_credentials_response
);