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-session.h"
45 #include "sipe-utils.h"
47 #include "sipe-schedule.h"
50 struct sipe_media_call_private
{
51 struct sipe_media_call
public;
53 /* private part starts here */
54 struct sipe_core_private
*sipe_private
;
57 struct sipmsg
*invitation
;
58 SipeIceVersion ice_version
;
59 gboolean encryption_compatible
;
64 #define SIPE_MEDIA_CALL ((struct sipe_media_call *) call_private)
65 #define SIPE_MEDIA_CALL_PRIVATE ((struct sipe_media_call_private *) call)
67 static void sipe_media_codec_list_free(GList
*codecs
)
69 for (; codecs
; codecs
= g_list_delete_link(codecs
, codecs
))
70 sipe_backend_codec_free(codecs
->data
);
73 static void sipe_media_candidate_list_free(GList
*candidates
)
75 for (; candidates
; candidates
= g_list_delete_link(candidates
, candidates
))
76 sipe_backend_candidate_free(candidates
->data
);
80 sipe_media_call_free(struct sipe_media_call_private
*call_private
)
83 struct sip_session
*session
;
84 sipe_backend_media_free(call_private
->public.backend_private
);
86 session
= sipe_session_find_call(call_private
->sipe_private
,
89 sipe_session_remove(call_private
->sipe_private
, session
);
91 if (call_private
->invitation
)
92 sipmsg_free(call_private
->invitation
);
94 sdpmsg_free(call_private
->smsg
);
95 g_slist_free_full(call_private
->failed_media
,
96 (GDestroyNotify
)sdpmedia_free
);
97 g_free(call_private
->with
);
103 backend_candidates_to_sdpcandidate(GList
*candidates
)
105 GSList
*result
= NULL
;
108 for (i
= candidates
; i
; i
= i
->next
) {
109 struct sipe_backend_candidate
*candidate
= i
->data
;
110 struct sdpcandidate
*c
= g_new(struct sdpcandidate
, 1);
112 c
->foundation
= sipe_backend_candidate_get_foundation(candidate
);
113 c
->component
= sipe_backend_candidate_get_component_type(candidate
);
114 c
->type
= sipe_backend_candidate_get_type(candidate
);
115 c
->protocol
= sipe_backend_candidate_get_protocol(candidate
);
116 c
->ip
= sipe_backend_candidate_get_ip(candidate
);
117 c
->port
= sipe_backend_candidate_get_port(candidate
);
118 c
->base_ip
= sipe_backend_candidate_get_base_ip(candidate
);
119 c
->base_port
= sipe_backend_candidate_get_base_port(candidate
);
120 c
->priority
= sipe_backend_candidate_get_priority(candidate
);
121 c
->username
= sipe_backend_candidate_get_username(candidate
);
122 c
->password
= sipe_backend_candidate_get_password(candidate
);
124 result
= g_slist_append(result
, c
);
131 get_stream_ip_and_ports(GSList
*candidates
,
132 gchar
**ip
, guint
*rtp_port
, guint
*rtcp_port
,
133 SipeCandidateType type
)
138 for (; candidates
; candidates
= candidates
->next
) {
139 struct sdpcandidate
*candidate
= candidates
->data
;
141 if (type
== SIPE_CANDIDATE_TYPE_ANY
|| candidate
->type
== type
) {
142 if (candidate
->component
== SIPE_COMPONENT_RTP
) {
143 *rtp_port
= candidate
->port
;
144 *ip
= g_strdup(candidate
->ip
);
145 } else if (candidate
->component
== SIPE_COMPONENT_RTCP
)
146 *rtcp_port
= candidate
->port
;
149 if (*rtp_port
!= 0 && *rtcp_port
!= 0)
154 static struct sdpmedia
*
155 backend_stream_to_sdpmedia(struct sipe_backend_media
*backend_media
,
156 struct sipe_backend_stream
*backend_stream
)
158 struct sdpmedia
*media
= g_new0(struct sdpmedia
, 1);
159 GList
*codecs
= sipe_backend_get_local_codecs(backend_media
,
163 GSList
*attributes
= NULL
;
167 media
->name
= g_strdup(sipe_backend_stream_get_id(backend_stream
));
169 if (sipe_strequal(media
->name
, "audio"))
170 type
= SIPE_MEDIA_AUDIO
;
171 else if (sipe_strequal(media
->name
, "video"))
172 type
= SIPE_MEDIA_VIDEO
;
174 // TODO: incompatible media, should not happen here
177 sipe_media_codec_list_free(codecs
);
182 for (i
= codecs
; i
; i
= i
->next
) {
183 struct sipe_backend_codec
*codec
= i
->data
;
184 struct sdpcodec
*c
= g_new0(struct sdpcodec
, 1);
187 c
->id
= sipe_backend_codec_get_id(codec
);
188 c
->name
= sipe_backend_codec_get_name(codec
);
189 c
->clock_rate
= sipe_backend_codec_get_clock_rate(codec
);
192 params
= sipe_backend_codec_get_optional_parameters(codec
);
193 for (; params
; params
= params
->next
) {
194 struct sipnameval
*param
= params
->data
;
195 struct sipnameval
*copy
= g_new0(struct sipnameval
, 1);
197 copy
->name
= g_strdup(param
->name
);
198 copy
->value
= g_strdup(param
->value
);
200 c
->parameters
= g_slist_append(c
->parameters
, copy
);
203 media
->codecs
= g_slist_append(media
->codecs
, c
);
206 sipe_media_codec_list_free(codecs
);
208 // Process local candidates
209 // If we have established candidate pairs, send them in SDP response.
210 // Otherwise send all available local candidates.
211 candidates
= sipe_backend_media_get_active_local_candidates(backend_media
,
214 candidates
= sipe_backend_get_local_candidates(backend_media
,
217 media
->candidates
= backend_candidates_to_sdpcandidate(candidates
);
219 sipe_media_candidate_list_free(candidates
);
221 get_stream_ip_and_ports(media
->candidates
, &media
->ip
, &media
->port
,
222 &rtcp_port
, SIPE_CANDIDATE_TYPE_HOST
);
223 // No usable HOST candidates, use any candidate
224 if (media
->ip
== NULL
&& media
->candidates
) {
225 get_stream_ip_and_ports(media
->candidates
, &media
->ip
, &media
->port
,
226 &rtcp_port
, SIPE_CANDIDATE_TYPE_ANY
);
229 if (sipe_backend_stream_is_held(backend_stream
))
230 attributes
= sipe_utils_nameval_add(attributes
, "inactive", "");
233 gchar
*tmp
= g_strdup_printf("%u", rtcp_port
);
234 attributes
= sipe_utils_nameval_add(attributes
, "rtcp", tmp
);
238 attributes
= sipe_utils_nameval_add(attributes
, "encryption", "rejected");
240 media
->attributes
= attributes
;
242 // Process remote candidates
243 candidates
= sipe_backend_media_get_active_remote_candidates(backend_media
,
245 media
->remote_candidates
= backend_candidates_to_sdpcandidate(candidates
);
246 sipe_media_candidate_list_free(candidates
);
251 static struct sdpmsg
*
252 sipe_media_to_sdpmsg(struct sipe_media_call_private
*call_private
)
254 struct sipe_backend_media
*backend_media
= call_private
->public.backend_private
;
255 struct sdpmsg
*msg
= g_new0(struct sdpmsg
, 1);
256 GSList
*streams
= sipe_backend_media_get_streams(backend_media
);
258 for (; streams
; streams
= streams
->next
) {
259 struct sdpmedia
*media
= backend_stream_to_sdpmedia(backend_media
, streams
->data
);
261 msg
->media
= g_slist_append(msg
->media
, media
);
264 msg
->ip
= g_strdup(media
->ip
);
268 msg
->media
= g_slist_concat(msg
->media
, call_private
->failed_media
);
269 call_private
->failed_media
= NULL
;
271 msg
->ice_version
= call_private
->ice_version
;
277 sipe_invite_call(struct sipe_core_private
*sipe_private
, TransCallback tc
)
282 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
283 struct sip_session
*session
;
284 struct sip_dialog
*dialog
;
286 gboolean add_2007_fallback
= FALSE
;
288 session
= sipe_session_find_call(sipe_private
, call_private
->with
);
289 dialog
= session
->dialogs
->data
;
290 add_2007_fallback
= dialog
->cseq
== 0 && call_private
->ice_version
== SIPE_ICE_RFC_5245
;
292 contact
= get_contact(sipe_private
);
293 hdr
= g_strdup_printf(
294 "ms-keep-alive: UAC;hop-hop=yes\r\n"
296 "Content-Type: %s\r\n",
299 "multipart/alternative;boundary=\"----=_NextPart_000_001E_01CB4397.0B5EB570\""
300 : "application/sdp");
303 msg
= sipe_media_to_sdpmsg(call_private
);
304 body
= sdpmsg_to_string(msg
);
306 if (add_2007_fallback
) {
308 tmp
= g_strdup_printf(
309 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
310 "Content-Type: application/sdp\r\n"
311 "Content-Transfer-Encoding: 7bit\r\n"
312 "Content-Disposition: session; handling=optional; ms-proxy-2007fallback\r\n"
314 "o=- 0 0 IN IP4 %s\r\n"
317 "m=audio 0 RTP/AVP\r\n"
319 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
320 "Content-Type: application/sdp\r\n"
321 "Content-Transfer-Encoding: 7bit\r\n"
322 "Content-Disposition: session; handling=optional\r\n"
326 "------=_NextPart_000_001E_01CB4397.0B5EB570--\r\n",
327 msg
->ip
, msg
->ip
, body
);
334 dialog
->outgoing_invite
= sip_transport_invite(sipe_private
,
344 static struct sip_dialog
*
345 sipe_media_dialog_init(struct sip_session
* session
, struct sipmsg
*msg
)
347 gchar
*newTag
= gentag();
348 const gchar
*oldHeader
;
350 struct sip_dialog
*dialog
;
352 oldHeader
= sipmsg_find_header(msg
, "To");
353 newHeader
= g_strdup_printf("%s;tag=%s", oldHeader
, newTag
);
354 sipmsg_remove_header_now(msg
, "To");
355 sipmsg_add_header_now(msg
, "To", newHeader
);
358 dialog
= sipe_dialog_add(session
);
359 dialog
->callid
= g_strdup(sipmsg_find_header(msg
, "Call-ID"));
360 dialog
->with
= parse_from(sipmsg_find_header(msg
, "From"));
361 sipe_dialog_parse(dialog
, msg
, FALSE
);
367 send_response_with_session_description(struct sipe_media_call_private
*call_private
, int code
, gchar
*text
)
369 struct sdpmsg
*msg
= sipe_media_to_sdpmsg(call_private
);
370 gchar
*body
= sdpmsg_to_string(msg
);
372 sipmsg_add_header(call_private
->invitation
, "Content-Type", "application/sdp");
373 sip_transport_response(call_private
->sipe_private
, call_private
->invitation
, code
, text
, body
);
378 encryption_levels_compatible(struct sdpmsg
*msg
)
382 for (i
= msg
->media
; i
; i
= i
->next
) {
383 const gchar
*enc_level
;
384 struct sdpmedia
*m
= i
->data
;
386 enc_level
= sipe_utils_nameval_find(m
->attributes
, "encryption");
388 // Decline call if peer requires encryption as we don't support it yet.
389 if (sipe_strequal(enc_level
, "required"))
397 process_invite_call_response(struct sipe_core_private
*sipe_private
,
399 struct transaction
*trans
);
402 update_remote_media(struct sipe_media_call_private
* call_private
,
403 struct sdpmedia
*media
)
405 struct sipe_backend_media
*backend_media
= SIPE_MEDIA_CALL
->backend_private
;
406 struct sipe_backend_stream
*backend_stream
;
407 GList
*backend_candidates
= NULL
;
408 GList
*backend_codecs
= NULL
;
410 gboolean result
= TRUE
;
412 backend_stream
= sipe_backend_media_get_stream_by_id(backend_media
,
414 if (media
->port
== 0) {
416 sipe_backend_media_remove_stream(backend_media
, backend_stream
);
423 for (i
= media
->codecs
; i
; i
= i
->next
) {
424 struct sdpcodec
*c
= i
->data
;
425 struct sipe_backend_codec
*codec
;
428 codec
= sipe_backend_codec_new(c
->id
,
433 for (j
= c
->parameters
; j
; j
= j
->next
) {
434 struct sipnameval
*attr
= j
->data
;
436 sipe_backend_codec_add_optional_parameter(codec
,
441 backend_codecs
= g_list_append(backend_codecs
, codec
);
444 result
= sipe_backend_set_remote_codecs(backend_media
,
447 sipe_media_codec_list_free(backend_codecs
);
449 if (result
== FALSE
) {
450 sipe_backend_media_remove_stream(backend_media
, backend_stream
);
454 for (i
= media
->candidates
; i
; i
= i
->next
) {
455 struct sdpcandidate
*c
= i
->data
;
456 struct sipe_backend_candidate
*candidate
;
457 candidate
= sipe_backend_candidate_new(c
->foundation
,
465 sipe_backend_candidate_set_priority(candidate
, c
->priority
);
467 backend_candidates
= g_list_append(backend_candidates
, candidate
);
470 sipe_backend_media_add_remote_candidates(backend_media
,
473 sipe_media_candidate_list_free(backend_candidates
);
475 if (sipe_utils_nameval_find(media
->attributes
, "inactive")) {
476 sipe_backend_stream_hold(backend_media
, backend_stream
, FALSE
);
477 } else if (sipe_backend_stream_is_held(backend_stream
)) {
478 sipe_backend_stream_unhold(backend_media
, backend_stream
, FALSE
);
485 apply_remote_message(struct sipe_media_call_private
* call_private
,
490 g_slist_free_full(call_private
->failed_media
, (GDestroyNotify
)sdpmedia_free
);
491 call_private
->failed_media
= NULL
;
493 for (i
= msg
->media
; i
; i
= i
->next
) {
494 struct sdpmedia
*media
= i
->data
;
495 if (!update_remote_media(call_private
, media
)) {
497 call_private
->failed_media
=
498 g_slist_append(call_private
->failed_media
, media
);
502 /* We need to keep failed medias until response is sent, remove them
503 * from sdpmsg that is to be freed. */
504 for (i
= call_private
->failed_media
; i
; i
= i
->next
) {
505 msg
->media
= g_slist_remove(msg
->media
, i
->data
);
508 call_private
->encryption_compatible
= encryption_levels_compatible(msg
);
511 // Sends an invite response when the call is accepted and local candidates were
512 // prepared, otherwise does nothing. If error response is sent, call_private is
513 // disposed before function returns. Returns true when response was sent.
515 send_invite_response_if_ready(struct sipe_media_call_private
*call_private
)
517 struct sipe_backend_media
*backend_media
;
519 backend_media
= call_private
->public.backend_private
;
521 if (!sipe_backend_media_accepted(backend_media
) ||
522 !sipe_backend_candidates_prepared(backend_media
))
525 if (!call_private
->encryption_compatible
) {
526 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
528 sipmsg_add_header(call_private
->invitation
, "Warning",
529 "308 lcs.microsoft.com \"Encryption Levels not compatible\"");
530 sip_transport_response(sipe_private
,
531 call_private
->invitation
,
532 488, "Encryption Levels not compatible",
534 sipe_backend_media_reject(backend_media
, FALSE
);
535 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
536 _("Unable to establish a call"),
537 _("Encryption settings of peer are incompatible with ours."));
539 send_response_with_session_description(call_private
, 200, "OK");
546 candidates_prepared_cb(struct sipe_media_call
*call
,
547 struct sipe_backend_stream
*stream
)
549 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
550 struct sipe_backend_media
*backend_private
= call
->backend_private
;
552 if (sipe_backend_media_is_initiator(backend_private
, stream
)) {
553 sipe_invite_call(call_private
->sipe_private
,
554 process_invite_call_response
);
556 struct sdpmsg
*smsg
= call_private
->smsg
;
557 call_private
->smsg
= NULL
;
559 apply_remote_message(call_private
, smsg
);
560 send_invite_response_if_ready(call_private
);
566 media_end_cb(struct sipe_media_call
*call
)
568 g_return_if_fail(call
);
570 SIPE_MEDIA_CALL_PRIVATE
->sipe_private
->media_call
= NULL
;
571 sipe_media_call_free(SIPE_MEDIA_CALL_PRIVATE
);
575 call_accept_cb(struct sipe_media_call
*call
, gboolean local
)
578 send_invite_response_if_ready(SIPE_MEDIA_CALL_PRIVATE
);
583 call_reject_cb(struct sipe_media_call
*call
, gboolean local
)
586 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
587 sip_transport_response(call_private
->sipe_private
,
588 call_private
->invitation
,
589 603, "Decline", NULL
);
594 sipe_media_send_ack(struct sipe_core_private
*sipe_private
, struct sipmsg
*msg
,
595 struct transaction
*trans
);
597 static void call_hold_cb(struct sipe_media_call
*call
,
599 SIPE_UNUSED_PARAMETER gboolean state
)
602 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
,
603 sipe_media_send_ack
);
606 static void call_hangup_cb(struct sipe_media_call
*call
, gboolean local
)
609 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
610 struct sip_session
*session
;
611 session
= sipe_session_find_call(call_private
->sipe_private
,
615 sipe_session_close(call_private
->sipe_private
, session
);
621 error_cb(struct sipe_media_call
*call
, gchar
*message
)
623 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
624 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
625 gboolean initiator
= sipe_backend_media_is_initiator(call
->backend_private
, NULL
);
626 gboolean accepted
= sipe_backend_media_accepted(call
->backend_private
);
628 gchar
*title
= g_strdup_printf("Call with %s failed", call_private
->with
);
629 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, title
, message
);
632 if (!initiator
&& !accepted
) {
633 sip_transport_response(sipe_private
,
634 call_private
->invitation
,
635 488, "Not Acceptable Here", NULL
);
638 sipe_backend_media_hangup(call
->backend_private
, initiator
|| accepted
);
641 static struct sipe_media_call_private
*
642 sipe_media_call_new(struct sipe_core_private
*sipe_private
,
643 const gchar
* with
, gboolean initiator
, SipeIceVersion ice_version
)
645 struct sipe_media_call_private
*call_private
= g_new0(struct sipe_media_call_private
, 1);
648 call_private
->sipe_private
= sipe_private
;
650 cname
= g_strdup(sipe_private
->contact
+ 1);
651 cname
[strlen(cname
) - 1] = '\0';
653 call_private
->public.backend_private
= sipe_backend_media_new(SIPE_CORE_PUBLIC
,
657 sipe_backend_media_set_cname(call_private
->public.backend_private
, cname
);
659 call_private
->ice_version
= ice_version
;
660 call_private
->encryption_compatible
= TRUE
;
662 call_private
->public.candidates_prepared_cb
= candidates_prepared_cb
;
663 call_private
->public.media_end_cb
= media_end_cb
;
664 call_private
->public.call_accept_cb
= call_accept_cb
;
665 call_private
->public.call_reject_cb
= call_reject_cb
;
666 call_private
->public.call_hold_cb
= call_hold_cb
;
667 call_private
->public.call_hangup_cb
= call_hangup_cb
;
668 call_private
->public.error_cb
= error_cb
;
675 void sipe_media_hangup(struct sipe_media_call_private
*call_private
)
678 sipe_backend_media_hangup(call_private
->public.backend_private
,
684 sipe_media_initiate_call(struct sipe_core_private
*sipe_private
,
685 const char *with
, SipeIceVersion ice_version
,
688 struct sipe_media_call_private
*call_private
;
689 struct sipe_backend_media
*backend_media
;
690 struct sipe_backend_media_relays
*backend_media_relays
;
691 struct sip_session
*session
;
692 struct sip_dialog
*dialog
;
694 if (sipe_private
->media_call
)
697 call_private
= sipe_media_call_new(sipe_private
, with
, TRUE
, ice_version
);
699 session
= sipe_session_add_call(sipe_private
, with
);
700 dialog
= sipe_dialog_add(session
);
701 dialog
->callid
= gencallid();
702 dialog
->with
= g_strdup(session
->with
);
703 dialog
->ourtag
= gentag();
705 call_private
->with
= g_strdup(session
->with
);
707 backend_media
= call_private
->public.backend_private
;
709 backend_media_relays
=
710 sipe_backend_media_relays_convert(sipe_private
->media_relays
,
711 sipe_private
->media_relay_username
,
712 sipe_private
->media_relay_password
);
714 if (!sipe_backend_media_add_stream(backend_media
,
715 "audio", with
, SIPE_MEDIA_AUDIO
,
716 call_private
->ice_version
, TRUE
,
717 backend_media_relays
)) {
718 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
720 _("Error creating audio stream"));
721 sipe_media_call_free(call_private
);
722 sipe_backend_media_relays_free(backend_media_relays
);
727 && !sipe_backend_media_add_stream(backend_media
,
728 "video", with
, SIPE_MEDIA_VIDEO
,
729 call_private
->ice_version
, TRUE
,
730 backend_media_relays
)) {
731 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
733 _("Error creating video stream"));
734 sipe_media_call_free(call_private
);
735 sipe_backend_media_relays_free(backend_media_relays
);
739 sipe_private
->media_call
= call_private
;
741 sipe_backend_media_relays_free(backend_media_relays
);
743 // Processing continues in candidates_prepared_cb
747 sipe_core_media_initiate_call(struct sipe_core_public
*sipe_public
,
751 sipe_media_initiate_call(SIPE_CORE_PRIVATE
, with
,
752 SIPE_ICE_RFC_5245
, with_video
);
755 void sipe_core_media_connect_conference(struct sipe_core_public
*sipe_public
,
756 struct sipe_chat_session
*chat_session
)
758 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
759 struct sipe_backend_media_relays
*backend_media_relays
;
760 struct sip_session
*session
;
761 struct sip_dialog
*dialog
;
765 session
= sipe_session_find_chat(sipe_private
, chat_session
);
767 if (sipe_private
->media_call
|| !session
)
770 session
->is_call
= TRUE
;
772 parts
= g_strsplit(chat_session
->id
, "app:conf:focus:", 2);
773 av_uri
= g_strjoinv("app:conf:audio-video:", parts
);
776 sipe_private
->media_call
= sipe_media_call_new(sipe_private
, av_uri
,
777 TRUE
, SIPE_ICE_DRAFT_6
);
779 session
= sipe_session_add_call(sipe_private
, av_uri
);
780 dialog
= sipe_dialog_add(session
);
781 dialog
->callid
= gencallid();
782 dialog
->with
= g_strdup(session
->with
);
783 dialog
->ourtag
= gentag();
787 sipe_private
->media_call
->with
= g_strdup(session
->with
);
788 sipe_private
->media_call
->ice_version
= SIPE_ICE_DRAFT_6
;
790 backend_media_relays
=
791 sipe_backend_media_relays_convert(sipe_private
->media_relays
,
792 sipe_private
->media_relay_username
,
793 sipe_private
->media_relay_password
);
795 if (!sipe_backend_media_add_stream(sipe_private
->media_call
->public.backend_private
,
796 "audio", dialog
->with
,
798 SIPE_ICE_DRAFT_6
, TRUE
,
799 backend_media_relays
)) {
800 sipe_backend_notify_error(sipe_public
,
802 _("Error creating audio stream"));
803 sipe_media_call_free(sipe_private
->media_call
);
804 sipe_private
->media_call
= NULL
;
807 sipe_backend_media_relays_free(backend_media_relays
);
809 // Processing continues in candidates_prepared_cb
812 gboolean
sipe_core_media_in_call(struct sipe_core_public
*sipe_public
)
815 return SIPE_CORE_PRIVATE
->media_call
!= NULL
;
820 void sipe_core_media_test_call(struct sipe_core_public
*sipe_public
)
822 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
823 if (!sipe_private
->test_call_bot_uri
) {
824 sipe_backend_notify_error(sipe_public
,
825 _("Unable to establish a call"),
826 _("Audio Test Service is not available."));
830 sipe_core_media_initiate_call(sipe_public
,
831 sipe_private
->test_call_bot_uri
, FALSE
);
835 process_incoming_invite_call(struct sipe_core_private
*sipe_private
,
838 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
839 struct sipe_backend_media
*backend_media
;
840 struct sipe_backend_media_relays
*backend_media_relays
= NULL
;
842 gboolean has_new_media
= FALSE
;
845 if (call_private
&& !is_media_session_msg(call_private
, msg
)) {
846 sip_transport_response(sipe_private
, msg
, 486, "Busy Here", NULL
);
850 smsg
= sdpmsg_parse_msg(msg
->body
);
852 sip_transport_response(sipe_private
, msg
,
853 488, "Not Acceptable Here", NULL
);
854 sipe_media_hangup(call_private
);
859 gchar
*with
= parse_from(sipmsg_find_header(msg
, "From"));
860 struct sip_session
*session
;
862 call_private
= sipe_media_call_new(sipe_private
, with
, FALSE
, smsg
->ice_version
);
863 session
= sipe_session_add_call(sipe_private
, with
);
864 sipe_media_dialog_init(session
, msg
);
866 call_private
->with
= g_strdup(session
->with
);
867 sipe_private
->media_call
= call_private
;
871 backend_media
= call_private
->public.backend_private
;
873 if (call_private
->invitation
)
874 sipmsg_free(call_private
->invitation
);
875 call_private
->invitation
= sipmsg_copy(msg
);
878 backend_media_relays
= sipe_backend_media_relays_convert(
879 sipe_private
->media_relays
,
880 sipe_private
->media_relay_username
,
881 sipe_private
->media_relay_password
);
883 // Create any new media streams
884 for (i
= smsg
->media
; i
; i
= i
->next
) {
885 struct sdpmedia
*media
= i
->data
;
886 gchar
*id
= media
->name
;
889 if ( media
->port
!= 0
890 && !sipe_backend_media_get_stream_by_id(backend_media
, id
)) {
893 if (sipe_strequal(id
, "audio"))
894 type
= SIPE_MEDIA_AUDIO
;
895 else if (sipe_strequal(id
, "video"))
896 type
= SIPE_MEDIA_VIDEO
;
900 with
= parse_from(sipmsg_find_header(msg
, "From"));
901 sipe_backend_media_add_stream(backend_media
, id
, with
,
905 backend_media_relays
);
906 has_new_media
= TRUE
;
911 sipe_backend_media_relays_free(backend_media_relays
);
914 sdpmsg_free(call_private
->smsg
);
915 call_private
->smsg
= smsg
;
916 sip_transport_response(sipe_private
, call_private
->invitation
,
917 180, "Ringing", NULL
);
918 // Processing continues in candidates_prepared_cb
920 apply_remote_message(call_private
, smsg
);
921 send_response_with_session_description(call_private
, 200, "OK");
927 void process_incoming_cancel_call(struct sipe_core_private
*sipe_private
,
930 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
932 // We respond to the CANCEL request with 200 OK response and
933 // with 487 Request Terminated to the remote INVITE in progress.
934 sip_transport_response(sipe_private
, msg
, 200, "OK", NULL
);
936 if (call_private
->invitation
) {
937 sip_transport_response(sipe_private
, call_private
->invitation
,
938 487, "Request Terminated", NULL
);
941 sipe_media_hangup(call_private
);
945 sipe_media_send_ack(struct sipe_core_private
*sipe_private
,
947 struct transaction
*trans
)
949 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
950 struct sip_session
*session
;
951 struct sip_dialog
*dialog
;
954 if (!is_media_session_msg(call_private
, msg
))
957 session
= sipe_session_find_call(sipe_private
, call_private
->with
);
958 dialog
= session
->dialogs
->data
;
962 tmp_cseq
= dialog
->cseq
;
964 dialog
->cseq
= sip_transaction_cseq(trans
) - 1;
965 sip_transport_ack(sipe_private
, dialog
);
966 dialog
->cseq
= tmp_cseq
;
968 dialog
->outgoing_invite
= NULL
;
974 sipe_media_send_final_ack(struct sipe_core_private
*sipe_private
,
976 struct transaction
*trans
)
978 if (!sipe_media_send_ack(sipe_private
, msg
, trans
))
981 sipe_backend_media_accept(sipe_private
->media_call
->public.backend_private
,
988 reinvite_on_candidate_pair_cb(struct sipe_core_public
*sipe_public
)
990 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
991 struct sipe_media_call_private
*media_call
= sipe_private
->media_call
;
992 struct sipe_backend_media
*backend_media
;
998 backend_media
= media_call
->public.backend_private
;
999 streams
= sipe_backend_media_get_streams(backend_media
);
1001 for (; streams
; streams
= streams
->next
) {
1002 struct sipe_backend_stream
*s
= streams
->data
;
1003 GList
*remote_candidates
= sipe_backend_media_get_active_remote_candidates(backend_media
, s
);
1004 guint components
= g_list_length(remote_candidates
);
1006 sipe_media_candidate_list_free(remote_candidates
);
1008 // We must have candidates for both (RTP + RTCP) components ready
1009 if (components
< 2) {
1010 sipe_schedule_mseconds(sipe_private
,
1011 "<+media-reinvite-on-candidate-pair>",
1014 (sipe_schedule_action
) reinvite_on_candidate_pair_cb
,
1020 sipe_invite_call(sipe_private
, sipe_media_send_final_ack
);
1024 process_invite_call_response(struct sipe_core_private
*sipe_private
,
1026 struct transaction
*trans
)
1029 struct sipe_media_call_private
*call_private
= sipe_private
->media_call
;
1030 struct sipe_backend_media
*backend_private
;
1031 struct sip_session
*session
;
1032 struct sip_dialog
*dialog
;
1033 struct sdpmsg
*smsg
;
1035 if (!is_media_session_msg(call_private
, msg
))
1038 session
= sipe_session_find_call(sipe_private
, call_private
->with
);
1039 dialog
= session
->dialogs
->data
;
1041 backend_private
= call_private
->public.backend_private
;
1042 with
= dialog
->with
;
1044 dialog
->outgoing_invite
= NULL
;
1046 if (msg
->response
>= 400) {
1047 // Call rejected by remote peer or an error occurred
1049 GString
*desc
= g_string_new("");
1050 gboolean append_responsestr
= FALSE
;
1052 switch (msg
->response
) {
1054 title
= _("User unavailable");
1056 if (sipmsg_parse_warning(msg
, NULL
) == 391) {
1057 g_string_append_printf(desc
, _("%s does not want to be disturbed"), with
);
1059 g_string_append_printf(desc
, _("User %s is not available"), with
);
1064 title
= _("Call rejected");
1065 g_string_append_printf(desc
, _("User %s rejected call"), with
);
1068 /* Check for incompatible encryption levels error.
1071 * 488 Not Acceptable Here
1072 * ms-client-diagnostics: 52017;reason="Encryption levels dont match"
1074 * older clients (and SIPE itself):
1075 * 488 Encryption Levels not compatible
1077 const gchar
*ms_diag
= sipmsg_find_header(msg
, "ms-client-diagnostics");
1079 if (sipe_strequal(msg
->responsestr
, "Encryption Levels not compatible") ||
1080 (ms_diag
&& g_str_has_prefix(ms_diag
, "52017;"))) {
1081 title
= _("Unable to establish a call");
1082 g_string_append(desc
, _("Encryption settings of peer are incompatible with ours."));
1086 if (call_private
->ice_version
== SIPE_ICE_RFC_5245
&&
1087 sip_transaction_cseq(trans
) == 1) {
1088 gchar
*with
= g_strdup(call_private
->with
);
1089 gboolean with_video
= sipe_backend_media_get_stream_by_id(backend_private
, "video") != NULL
;
1091 sipe_media_hangup(call_private
);
1092 // We might be calling to OC 2007 instance, retry with ICEv6
1093 sipe_media_initiate_call(sipe_private
, with
,
1094 SIPE_ICE_DRAFT_6
, with_video
);
1099 // Break intentionally omitted
1102 title
= _("Error occured");
1103 g_string_append(desc
, _("Unable to establish a call"));
1104 append_responsestr
= TRUE
;
1108 if (append_responsestr
)
1109 g_string_append_printf(desc
, "\n%d %s",
1110 msg
->response
, msg
->responsestr
);
1112 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, title
, desc
->str
);
1113 g_string_free(desc
, TRUE
);
1115 sipe_media_send_ack(sipe_private
, msg
, trans
);
1116 sipe_media_hangup(call_private
);
1121 sipe_dialog_parse(dialog
, msg
, TRUE
);
1122 smsg
= sdpmsg_parse_msg(msg
->body
);
1124 sip_transport_response(sipe_private
, msg
,
1125 488, "Not Acceptable Here", NULL
);
1126 sipe_media_hangup(call_private
);
1130 apply_remote_message(call_private
, smsg
);
1133 sipe_media_send_ack(sipe_private
, msg
, trans
);
1134 reinvite_on_candidate_pair_cb(SIPE_CORE_PUBLIC
);
1139 gboolean
is_media_session_msg(struct sipe_media_call_private
*call_private
,
1143 const gchar
*callid
= sipmsg_find_header(msg
, "Call-ID");
1144 struct sip_session
*session
;
1146 session
= sipe_session_find_call(call_private
->sipe_private
,
1147 call_private
->with
);
1149 struct sip_dialog
*dialog
= session
->dialogs
->data
;
1150 return sipe_strequal(dialog
->callid
, callid
);
1156 void sipe_media_handle_going_offline(struct sipe_media_call_private
*call_private
)
1158 struct sipe_backend_media
*backend_private
;
1160 backend_private
= call_private
->public.backend_private
;
1162 if ( !sipe_backend_media_is_initiator(backend_private
, NULL
)
1163 && !sipe_backend_media_accepted(backend_private
)) {
1164 sip_transport_response(call_private
->sipe_private
,
1165 call_private
->invitation
,
1166 480, "Temporarily Unavailable", NULL
);
1168 struct sip_session
*session
;
1170 session
= sipe_session_find_call(call_private
->sipe_private
,
1171 call_private
->with
);
1173 sipe_session_close(call_private
->sipe_private
, session
);
1176 sipe_media_hangup(call_private
);
1180 sipe_media_relay_free(struct sipe_media_relay
*relay
)
1182 g_free(relay
->hostname
);
1183 if (relay
->dns_query
)
1184 sipe_backend_dns_query_cancel(relay
->dns_query
);
1189 sipe_media_relay_list_free(GSList
*list
)
1191 for (; list
; list
= g_slist_delete_link(list
, list
))
1192 sipe_media_relay_free(list
->data
);
1196 relay_ip_resolved_cb(struct sipe_media_relay
* relay
,
1197 const gchar
*ip
, SIPE_UNUSED_PARAMETER guint port
)
1199 gchar
*hostname
= relay
->hostname
;
1200 relay
->dns_query
= NULL
;
1203 relay
->hostname
= g_strdup(ip
);
1204 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname
, ip
);
1206 relay
->hostname
= NULL
;
1207 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname
);
1214 process_get_av_edge_credentials_response(struct sipe_core_private
*sipe_private
,
1216 SIPE_UNUSED_PARAMETER
struct transaction
*trans
)
1218 g_free(sipe_private
->media_relay_username
);
1219 g_free(sipe_private
->media_relay_password
);
1220 sipe_media_relay_list_free(sipe_private
->media_relays
);
1221 sipe_private
->media_relay_username
= NULL
;
1222 sipe_private
->media_relay_password
= NULL
;
1223 sipe_private
->media_relays
= NULL
;
1225 if (msg
->response
>= 400) {
1226 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
1227 "Failed to obtain A/V Edge credentials.");
1231 if (msg
->response
== 200) {
1232 sipe_xml
*xn_response
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1234 if (sipe_strequal("OK", sipe_xml_attribute(xn_response
, "reasonPhrase"))) {
1235 const sipe_xml
*xn_credentials
= sipe_xml_child(xn_response
, "credentialsResponse/credentials");
1236 const sipe_xml
*xn_relays
= sipe_xml_child(xn_response
, "credentialsResponse/mediaRelayList");
1237 const sipe_xml
*item
;
1238 GSList
*relays
= NULL
;
1240 item
= sipe_xml_child(xn_credentials
, "username");
1241 sipe_private
->media_relay_username
= sipe_xml_data(item
);
1242 item
= sipe_xml_child(xn_credentials
, "password");
1243 sipe_private
->media_relay_password
= sipe_xml_data(item
);
1245 for (item
= sipe_xml_child(xn_relays
, "mediaRelay"); item
; item
= sipe_xml_twin(item
)) {
1246 struct sipe_media_relay
*relay
= g_new0(struct sipe_media_relay
, 1);
1247 const sipe_xml
*node
;
1250 node
= sipe_xml_child(item
, "hostName");
1251 relay
->hostname
= sipe_xml_data(node
);
1253 node
= sipe_xml_child(item
, "udpPort");
1254 relay
->udp_port
= atoi(tmp
= sipe_xml_data(node
));
1257 node
= sipe_xml_child(item
, "tcpPort");
1258 relay
->tcp_port
= atoi(tmp
= sipe_xml_data(node
));
1261 relays
= g_slist_append(relays
, relay
);
1263 relay
->dns_query
= sipe_backend_dns_query_a(
1267 (sipe_dns_resolved_cb
) relay_ip_resolved_cb
,
1270 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
1272 relay
->tcp_port
, relay
->udp_port
);
1275 sipe_private
->media_relays
= relays
;
1278 sipe_xml_free(xn_response
);
1285 sipe_media_get_av_edge_credentials(struct sipe_core_private
*sipe_private
)
1287 // TODO: re-request credentials after duration expires?
1288 const char CRED_REQUEST_XML
[] =
1289 "<request requestID=\"%d\" "
1293 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
1294 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
1295 "<credentialsRequest credentialsRequestID=\"%d\">"
1296 "<identity>%s</identity>"
1297 "<location>%s</location>"
1298 "<duration>480</duration>"
1299 "</credentialsRequest>"
1302 int request_id
= rand();
1306 if (!sipe_private
->mras_uri
)
1309 self
= sip_uri_self(sipe_private
);
1311 body
= g_strdup_printf(
1315 sipe_private
->mras_uri
,
1318 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER
) ? "internet" : "intranet");
1321 sip_transport_service(sipe_private
,
1322 sipe_private
->mras_uri
,
1323 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
1325 process_get_av_edge_credentials_response
);