6 * Copyright (C) 2011-2021 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-conf.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 /* [MS-SDPEXT] 3.1.5.31.2 says a range size of 100 SHOULD be used for video and
52 * some clients really demand this. */
53 #define VIDEO_SSRC_COUNT 100
55 struct sipe_media_call_private
{
56 struct sipe_media_call
public;
58 /* private part starts here */
59 struct sipe_core_private
*sipe_private
;
61 struct sip_session
*session
;
62 struct sip_session
*conference_session
;
66 struct sipmsg
*invitation
;
67 SipeIceVersion ice_version
;
68 gboolean encryption_compatible
;
69 gchar
*extra_invite_section
;
70 gchar
*invite_content_type
;
79 #define SIPE_MEDIA_CALL ((struct sipe_media_call *) call_private)
80 #define SIPE_MEDIA_CALL_PRIVATE ((struct sipe_media_call_private *) call)
82 struct sipe_media_stream_private
{
83 struct sipe_media_stream
public;
87 guchar
*encryption_key
;
88 int encryption_key_id
;
89 gboolean remote_candidates_and_codecs_set
;
92 gboolean sdp_negotiation_concluded
;
102 /* User data associated with the stream. */
104 GDestroyNotify data_free_func
;
106 #define SIPE_MEDIA_STREAM ((struct sipe_media_stream *) stream_private)
107 #define SIPE_MEDIA_STREAM_PRIVATE ((struct sipe_media_stream_private *) stream)
109 #define SIPE_MEDIA_STREAM_CONNECTION_TIMEOUT_SECONDS 30
110 #define SIPE_MEDIA_CALL_RINGING_TIMEOUT_SECONDS 60
111 #define SIPE_MEDIA_CALL_TIMEOUT_SECONDS 120
113 struct async_read_data
{
116 sipe_media_stream_read_callback callback
;
119 static void stream_schedule_cancel_timeout(struct sipe_media_call
*call
,
120 struct sipe_media_stream_private
*stream_private
);
122 static void call_schedule_cancel_request_timeout(struct sipe_media_call
*call
);
123 static void call_schedule_cancel_ringing_timeout(struct sipe_media_call
*call
);
125 static void sipe_media_codec_list_free(GList
*codecs
)
127 for (; codecs
; codecs
= g_list_delete_link(codecs
, codecs
))
128 sipe_backend_codec_free(codecs
->data
);
131 static void sipe_media_candidate_list_free(GList
*candidates
)
133 for (; candidates
; candidates
= g_list_delete_link(candidates
, candidates
))
134 sipe_backend_candidate_free(candidates
->data
);
138 sipe_media_stream_free(struct sipe_media_stream_private
*stream_private
)
140 struct sipe_media_call_private
*call_private
;
142 call_private
= (struct sipe_media_call_private
*)SIPE_MEDIA_STREAM
->call
;
144 stream_schedule_cancel_timeout(SIPE_MEDIA_CALL
, stream_private
);
146 sipe_media_stream_set_data(SIPE_MEDIA_STREAM
, NULL
, NULL
);
149 call_private
->streams
= g_slist_remove(call_private
->streams
,
152 if (SIPE_MEDIA_STREAM
->ssrc_range
) {
153 call_private
->ssrc_ranges
=
154 g_slist_remove(call_private
->ssrc_ranges
,
155 SIPE_MEDIA_STREAM
->ssrc_range
);
156 g_free(SIPE_MEDIA_STREAM
->ssrc_range
);
160 if (SIPE_MEDIA_STREAM
->backend_private
) {
161 sipe_backend_media_stream_free(SIPE_MEDIA_STREAM
->backend_private
);
163 g_free(SIPE_MEDIA_STREAM
->id
);
164 g_free(stream_private
->encryption_key
);
165 g_queue_free_full(stream_private
->write_queue
,
166 (GDestroyNotify
)g_byte_array_unref
);
167 g_queue_free_full(stream_private
->async_reads
, g_free
);
168 sipe_utils_nameval_free(stream_private
->extra_sdp
);
169 g_free(stream_private
);
173 call_private_equals(SIPE_UNUSED_PARAMETER
const gchar
*callid
,
174 struct sipe_media_call_private
*call_private1
,
175 struct sipe_media_call_private
*call_private2
)
177 return call_private1
== call_private2
;
181 sipe_media_call_free(struct sipe_media_call_private
*call_private
)
184 g_hash_table_foreach_remove(call_private
->sipe_private
->media_calls
,
185 (GHRFunc
) call_private_equals
, call_private
);
187 call_schedule_cancel_request_timeout(SIPE_MEDIA_CALL
);
188 call_schedule_cancel_ringing_timeout(SIPE_MEDIA_CALL
);
190 while (call_private
->streams
) {
191 sipe_media_stream_free(call_private
->streams
->data
);
194 sipe_backend_media_free(call_private
->public.backend_private
);
196 if (call_private
->session
) {
197 sipe_session_remove(call_private
->sipe_private
,
198 call_private
->session
);
201 if (call_private
->invitation
)
202 sipmsg_free(call_private
->invitation
);
204 // Frees any referenced extra invite data.
205 sipe_media_add_extra_invite_section(SIPE_MEDIA_CALL
, NULL
, NULL
);
207 sipe_utils_slist_free_full(call_private
->ssrc_ranges
, g_free
);
209 sdpmsg_free(call_private
->smsg
);
210 sipe_utils_slist_free_full(call_private
->failed_media
,
211 (GDestroyNotify
)sdpmedia_free
);
212 g_free(SIPE_MEDIA_CALL
->with
);
213 g_free(call_private
);
218 candidate_sort_cb(struct sdpcandidate
*c1
, struct sdpcandidate
*c2
)
220 int cmp
= g_strcmp0(c1
->foundation
, c2
->foundation
);
222 cmp
= g_strcmp0(c1
->username
, c2
->username
);
224 cmp
= c1
->component
- c2
->component
;
231 backend_candidates_to_sdpcandidate(GList
*candidates
)
233 GSList
*result
= NULL
;
236 for (i
= candidates
; i
; i
= i
->next
) {
237 struct sipe_backend_candidate
*candidate
= i
->data
;
238 struct sdpcandidate
*c
;
240 gchar
*ip
= sipe_backend_candidate_get_ip(candidate
);
241 gchar
*base_ip
= sipe_backend_candidate_get_base_ip(candidate
);
242 if (is_empty(ip
) || strchr(ip
, ':') ||
243 (base_ip
&& strchr(base_ip
, ':'))) {
244 /* Ignore IPv6 candidates. */
250 c
= g_new(struct sdpcandidate
, 1);
251 c
->foundation
= sipe_backend_candidate_get_foundation(candidate
);
252 c
->component
= sipe_backend_candidate_get_component_type(candidate
);
253 c
->type
= sipe_backend_candidate_get_type(candidate
);
254 c
->protocol
= sipe_backend_candidate_get_protocol(candidate
);
256 c
->port
= sipe_backend_candidate_get_port(candidate
);
257 c
->base_ip
= base_ip
;
258 c
->base_port
= sipe_backend_candidate_get_base_port(candidate
);
259 c
->priority
= sipe_backend_candidate_get_priority(candidate
);
260 c
->username
= sipe_backend_candidate_get_username(candidate
);
261 c
->password
= sipe_backend_candidate_get_password(candidate
);
263 result
= g_slist_insert_sorted(result
, c
,
264 (GCompareFunc
)candidate_sort_cb
);
271 get_stream_ip_and_ports(GSList
*candidates
,
272 gchar
**ip
, guint
*rtp_port
, guint
*rtcp_port
)
274 guint32 rtp_max_priority
= 0;
275 guint32 rtcp_max_priority
= 0;
281 for (; candidates
; candidates
= candidates
->next
) {
282 struct sdpcandidate
*candidate
= candidates
->data
;
284 if (candidate
->component
== SIPE_COMPONENT_RTP
&&
285 candidate
->priority
> rtp_max_priority
) {
286 rtp_max_priority
= candidate
->priority
;
287 *rtp_port
= candidate
->port
;
290 *ip
= g_strdup(candidate
->ip
);
291 } else if (candidate
->component
== SIPE_COMPONENT_RTCP
&&
292 candidate
->priority
> rtcp_max_priority
) {
293 rtcp_max_priority
= candidate
->priority
;
294 *rtcp_port
= candidate
->port
;
300 sdpcodec_compare(gconstpointer a
, gconstpointer b
)
302 return ((const struct sdpcodec
*)a
)->id
-
303 ((const struct sdpcodec
*)b
)->id
;
307 remove_wrong_farstream_0_1_tcp_candidates(GList
*candidates
)
309 GList
*i
= candidates
;
310 GHashTable
*foundation_to_candidate
= g_hash_table_new_full(g_str_hash
,
316 GList
*next
= i
->next
;
317 struct sipe_backend_candidate
*c1
= i
->data
;
319 if (sipe_backend_candidate_get_protocol(c1
) == SIPE_NETWORK_PROTOCOL_UDP
) {
320 gchar
*foundation
= sipe_backend_candidate_get_foundation(c1
);
321 struct sipe_backend_candidate
*c2
= g_hash_table_lookup(foundation_to_candidate
,
327 if (sipe_backend_candidate_get_port(c1
) ==
328 sipe_backend_candidate_get_port(c2
) ||
329 (sipe_backend_candidate_get_type(c1
) !=
330 SIPE_CANDIDATE_TYPE_HOST
&&
331 sipe_backend_candidate_get_base_port(c1
) ==
332 sipe_backend_candidate_get_base_port(c2
))) {
334 * We assume that RTP+RTCP UDP pairs
335 * that share the same port are
336 * actually mistagged TCP candidates.
338 candidates
= g_list_remove(candidates
, c2
);
339 candidates
= g_list_delete_link(candidates
, i
);
340 sipe_backend_candidate_free(c1
);
341 sipe_backend_candidate_free(c2
);
344 /* hash table takes ownership of "foundation" */
345 g_hash_table_insert(foundation_to_candidate
, foundation
, c1
);
351 g_hash_table_destroy(foundation_to_candidate
);
357 fill_zero_tcp_act_ports_from_tcp_pass(GSList
*candidates
, GSList
*all_candidates
)
360 GHashTable
*ip_to_port
= g_hash_table_new(g_str_hash
, g_str_equal
);
362 for (i
= candidates
; i
; i
= i
->next
) {
363 struct sdpcandidate
*c
= i
->data
;
366 if (c
->protocol
!= SIPE_NETWORK_PROTOCOL_TCP_ACTIVE
) {
370 for (j
= all_candidates
; j
; j
= j
->next
) {
371 struct sdpcandidate
*passive
= j
->data
;
372 if (passive
->protocol
!= SIPE_NETWORK_PROTOCOL_TCP_PASSIVE
||
373 c
->type
!= passive
->type
) {
377 if (sipe_strequal(c
->ip
, passive
->ip
) &&
378 sipe_strequal(c
->base_ip
, passive
->base_ip
)) {
380 c
->port
= passive
->port
;
383 if (c
->base_port
== 0) {
384 c
->base_port
= passive
->base_port
;
391 for (i
= all_candidates
; i
; i
= i
->next
) {
392 struct sdpcandidate
*c
= i
->data
;
394 if (c
->protocol
== SIPE_NETWORK_PROTOCOL_TCP_PASSIVE
&&
395 c
->type
== SIPE_CANDIDATE_TYPE_HOST
) {
396 g_hash_table_insert(ip_to_port
, c
->ip
, &c
->port
);
400 /* Fill base ports of all TCP relay candidates using what we have
401 * collected from host candidates. */
402 for (i
= candidates
; i
; i
= i
->next
) {
403 struct sdpcandidate
*c
= i
->data
;
404 if (c
->type
== SIPE_CANDIDATE_TYPE_RELAY
&& c
->base_port
== 0) {
405 guint
*base_port
= (guint
*)g_hash_table_lookup(ip_to_port
, c
->base_ip
);
407 c
->base_port
= *base_port
;
409 SIPE_DEBUG_WARNING("Couldn't determine base port for candidate "
410 "with foundation %s", c
->foundation
);
415 g_hash_table_destroy(ip_to_port
);
418 static SipeEncryptionPolicy
419 get_encryption_policy(struct sipe_core_private
*sipe_private
)
421 SipeEncryptionPolicy result
=
422 sipe_backend_media_get_encryption_policy(SIPE_CORE_PUBLIC
);
423 if (result
== SIPE_ENCRYPTION_POLICY_OBEY_SERVER
) {
424 result
= sipe_private
->server_av_encryption_policy
;
430 static struct sdpmedia
*
431 media_stream_to_sdpmedia(struct sipe_media_call_private
*call_private
,
432 struct sipe_media_stream_private
*stream_private
)
434 struct sdpmedia
*sdpmedia
= g_new0(struct sdpmedia
, 1);
435 GList
*codecs
= sipe_backend_get_local_codecs(SIPE_MEDIA_CALL
,
437 SipeEncryptionPolicy encryption_policy
=
438 get_encryption_policy(call_private
->sipe_private
);
441 GSList
*attributes
= NULL
;
442 GSList
*sdpcandidates
;
443 GSList
*all_sdpcandidates
;
448 sdpmedia
->name
= g_strdup(SIPE_MEDIA_STREAM
->id
);
450 if (sipe_strequal(sdpmedia
->name
, "audio"))
451 type
= SIPE_MEDIA_AUDIO
;
452 else if (sipe_strequal(sdpmedia
->name
, "video"))
453 type
= SIPE_MEDIA_VIDEO
;
454 else if (sipe_strequal(sdpmedia
->name
, "data"))
455 type
= SIPE_MEDIA_APPLICATION
;
456 else if (sipe_strequal(sdpmedia
->name
, "applicationsharing"))
457 type
= SIPE_MEDIA_APPLICATION
;
459 // TODO: incompatible media, should not happen here
460 g_free(sdpmedia
->name
);
462 sipe_media_codec_list_free(codecs
);
467 for (i
= codecs
; i
; i
= i
->next
) {
468 struct sipe_backend_codec
*codec
= i
->data
;
469 struct sdpcodec
*c
= g_new0(struct sdpcodec
, 1);
472 c
->id
= sipe_backend_codec_get_id(codec
);
473 c
->name
= sipe_backend_codec_get_name(codec
);
474 c
->clock_rate
= sipe_backend_codec_get_clock_rate(codec
);
477 params
= sipe_backend_codec_get_optional_parameters(codec
);
478 for (; params
; params
= params
->next
) {
479 struct sipnameval
*param
= params
->data
;
480 struct sipnameval
*copy
= g_new0(struct sipnameval
, 1);
482 copy
->name
= g_strdup(param
->name
);
483 copy
->value
= g_strdup(param
->value
);
485 c
->parameters
= g_slist_append(c
->parameters
, copy
);
488 /* Buggy(?) codecs may report non-unique id (a.k.a. payload
489 * type) that must not appear in SDP messages we send. Thus,
490 * let's ignore any codec having the same id as one we already
491 * have in the converted list. */
492 if (g_slist_find_custom(sdpmedia
->codecs
, c
, sdpcodec_compare
)) {
495 sdpmedia
->codecs
= g_slist_append(sdpmedia
->codecs
, c
);
499 sipe_media_codec_list_free(codecs
);
501 // Process local candidates
502 // If we have established candidate pairs, send them in SDP response.
503 // Otherwise send all available local candidates.
504 candidates
= sipe_backend_media_stream_get_active_local_candidates(SIPE_MEDIA_STREAM
);
505 sdpcandidates
= backend_candidates_to_sdpcandidate(candidates
);
506 sipe_media_candidate_list_free(candidates
);
508 candidates
= sipe_backend_get_local_candidates(SIPE_MEDIA_CALL
,
510 candidates
= remove_wrong_farstream_0_1_tcp_candidates(candidates
);
511 all_sdpcandidates
= backend_candidates_to_sdpcandidate(candidates
);
512 sipe_media_candidate_list_free(candidates
);
514 if (!sdpcandidates
) {
515 sdpcandidates
= all_sdpcandidates
;
518 fill_zero_tcp_act_ports_from_tcp_pass(sdpcandidates
, all_sdpcandidates
);
520 sdpmedia
->candidates
= sdpcandidates
;
522 if (all_sdpcandidates
!= sdpcandidates
) {
523 sipe_utils_slist_free_full(all_sdpcandidates
,
524 (GDestroyNotify
)sdpcandidate_free
);
527 get_stream_ip_and_ports(sdpmedia
->candidates
, &sdpmedia
->ip
,
528 &sdpmedia
->port
, &rtcp_port
);
530 if (sipe_backend_stream_is_held(SIPE_MEDIA_STREAM
))
531 attributes
= sipe_utils_nameval_add(attributes
, "inactive", "");
534 gchar
*tmp
= g_strdup_printf("%u", rtcp_port
);
535 attributes
= sipe_utils_nameval_add(attributes
, "rtcp", tmp
);
539 if (encryption_policy
!= call_private
->sipe_private
->server_av_encryption_policy
) {
540 const gchar
*encryption
= NULL
;
541 switch (encryption_policy
) {
542 case SIPE_ENCRYPTION_POLICY_REJECTED
:
543 encryption
= "rejected";
545 case SIPE_ENCRYPTION_POLICY_OPTIONAL
:
546 encryption
= "optional";
548 case SIPE_ENCRYPTION_POLICY_REQUIRED
:
550 encryption
= "required";
554 attributes
= sipe_utils_nameval_add(attributes
, "encryption", encryption
);
557 if (SIPE_MEDIA_STREAM
->ssrc_range
) {
560 tmp
= g_strdup_printf("%u-%u",
561 SIPE_MEDIA_STREAM
->ssrc_range
->begin
,
562 SIPE_MEDIA_STREAM
->ssrc_range
->end
);
563 attributes
= sipe_utils_nameval_add(attributes
,
564 "x-ssrc-range", tmp
);
568 // Process remote candidates
569 candidates
= sipe_backend_media_stream_get_active_remote_candidates(SIPE_MEDIA_STREAM
);
570 sdpmedia
->remote_candidates
= backend_candidates_to_sdpcandidate(candidates
);
571 sipe_media_candidate_list_free(candidates
);
573 sdpmedia
->encryption_active
= stream_private
->encryption_key
&&
574 call_private
->encryption_compatible
&&
575 stream_private
->remote_candidates_and_codecs_set
&&
576 encryption_policy
!= SIPE_ENCRYPTION_POLICY_REJECTED
;
578 // Set our key if encryption is enabled.
579 if (stream_private
->encryption_key
&&
580 encryption_policy
!= SIPE_ENCRYPTION_POLICY_REJECTED
) {
581 sdpmedia
->encryption_key
= sipe_utils_memdup(stream_private
->encryption_key
,
583 sdpmedia
->encryption_key_id
= stream_private
->encryption_key_id
;
586 // Append extra attributes assigned to the stream.
587 for (j
= stream_private
->extra_sdp
; j
; j
= g_slist_next(j
)) {
588 struct sipnameval
*attr
= j
->data
;
589 attributes
= sipe_utils_nameval_add(attributes
,
590 attr
->name
, attr
->value
);
593 sdpmedia
->attributes
= attributes
;
598 static struct sdpmsg
*
599 sipe_media_to_sdpmsg(struct sipe_media_call_private
*call_private
)
601 struct sdpmsg
*msg
= g_new0(struct sdpmsg
, 1);
602 GSList
*streams
= call_private
->streams
;
604 for (; streams
; streams
= streams
->next
) {
605 struct sdpmedia
*media
= media_stream_to_sdpmedia(call_private
,
608 msg
->media
= g_slist_append(msg
->media
, media
);
611 msg
->ip
= g_strdup(media
->ip
);
615 msg
->media
= g_slist_concat(msg
->media
, call_private
->failed_media
);
616 call_private
->failed_media
= NULL
;
618 msg
->ice_version
= call_private
->ice_version
;
624 sipe_invite_call(struct sipe_media_call_private
*call_private
, TransCallback tc
)
626 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
629 gchar
*p_preferred_identity
= NULL
;
631 struct sip_dialog
*dialog
;
634 dialog
= sipe_media_get_sip_dialog(SIPE_MEDIA_CALL
);
636 contact
= get_contact(sipe_private
);
638 if (sipe_private
->uc_line_uri
) {
639 gchar
*self
= sip_uri_self(sipe_private
);
640 p_preferred_identity
= g_strdup_printf(
641 "P-Preferred-Identity: <%s>, <%s>\r\n",
642 self
, sipe_private
->uc_line_uri
);
646 hdr
= g_strdup_printf(
647 "ms-keep-alive: UAC;hop-hop=yes\r\n"
650 "Content-Type: %s%s\r\n",
652 p_preferred_identity
? p_preferred_identity
: "",
653 call_private
->invite_content_type
?
654 call_private
->invite_content_type
: "application/sdp",
655 call_private
->invite_content_type
?
656 ";boundary=\"----=_NextPart_000_001E_01CB4397.0B5EB570\"" : "");
659 g_free(p_preferred_identity
);
661 msg
= sipe_media_to_sdpmsg(call_private
);
662 body
= sdpmsg_to_string(msg
);
664 if (call_private
->extra_invite_section
) {
666 tmp
= g_strdup_printf(
667 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
670 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
671 "Content-Type: application/sdp\r\n"
672 "Content-Transfer-Encoding: 7bit\r\n"
673 "Content-Disposition: session; handling=optional\r\n"
677 "------=_NextPart_000_001E_01CB4397.0B5EB570--\r\n",
678 call_private
->extra_invite_section
, body
);
681 sipe_media_add_extra_invite_section(SIPE_MEDIA_CALL
, NULL
, NULL
);
686 dialog
->outgoing_invite
= sip_transport_invite(sipe_private
,
697 send_response_with_session_description(struct sipe_media_call_private
*call_private
, int code
, const gchar
*text
)
699 struct sdpmsg
*msg
= sipe_media_to_sdpmsg(call_private
);
700 gchar
*body
= sdpmsg_to_string(msg
);
702 sipmsg_add_header(call_private
->invitation
, "Content-Type", "application/sdp");
703 sip_transport_response(call_private
->sipe_private
, call_private
->invitation
, code
, text
, body
);
708 process_invite_call_response(struct sipe_core_private
*sipe_private
,
710 struct transaction
*trans
);
712 struct sipe_media_stream
*
713 sipe_core_media_get_stream_by_id(struct sipe_media_call
*call
, const gchar
*id
)
716 for (i
= SIPE_MEDIA_CALL_PRIVATE
->streams
; i
; i
= i
->next
) {
717 struct sipe_media_stream
*stream
= i
->data
;
718 if (sipe_strequal(stream
->id
, id
))
725 update_call_from_remote_sdp(struct sipe_media_call_private
* call_private
,
726 struct sdpmedia
*media
)
728 struct sipe_media_stream
*stream
;
729 GList
*backend_candidates
= NULL
;
730 GList
*backend_codecs
= NULL
;
732 gboolean result
= TRUE
;
734 stream
= sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL
, media
->name
);
735 if (media
->port
== 0) {
737 sipe_backend_media_stream_end(SIPE_MEDIA_CALL
, stream
);
745 if (sipe_utils_nameval_find(media
->attributes
, "inactive")) {
746 sipe_backend_stream_hold(SIPE_MEDIA_CALL
, stream
, FALSE
);
747 } else if (sipe_backend_stream_is_held(stream
)) {
748 sipe_backend_stream_unhold(SIPE_MEDIA_CALL
, stream
, FALSE
);
751 if (SIPE_MEDIA_STREAM_PRIVATE
->remote_candidates_and_codecs_set
) {
755 for (i
= media
->codecs
; i
; i
= i
->next
) {
756 struct sdpcodec
*c
= i
->data
;
757 struct sipe_backend_codec
*codec
;
760 codec
= sipe_backend_codec_new(c
->id
,
766 for (j
= c
->parameters
; j
; j
= j
->next
) {
767 struct sipnameval
*attr
= j
->data
;
769 sipe_backend_codec_add_optional_parameter(codec
,
774 backend_codecs
= g_list_append(backend_codecs
, codec
);
777 if (media
->encryption_key
&& SIPE_MEDIA_STREAM_PRIVATE
->encryption_key
) {
778 sipe_backend_media_set_encryption_keys(SIPE_MEDIA_CALL
, stream
,
779 SIPE_MEDIA_STREAM_PRIVATE
->encryption_key
,
780 media
->encryption_key
);
781 SIPE_MEDIA_STREAM_PRIVATE
->encryption_key_id
= media
->encryption_key_id
;
783 // We now know that the stream won't be encrypted.
784 // Allow unencrypted data to pass srtpdec freely
785 sipe_backend_media_set_require_encryption(SIPE_MEDIA_CALL
,
790 result
= sipe_backend_set_remote_codecs(SIPE_MEDIA_CALL
, stream
,
792 sipe_media_codec_list_free(backend_codecs
);
794 if (result
== FALSE
) {
795 sipe_backend_media_stream_end(SIPE_MEDIA_CALL
, stream
);
799 for (i
= media
->candidates
; i
; i
= i
->next
) {
800 struct sdpcandidate
*c
= i
->data
;
801 struct sipe_backend_candidate
*candidate
;
802 candidate
= sipe_backend_candidate_new(c
->foundation
,
810 sipe_backend_candidate_set_priority(candidate
, c
->priority
);
812 backend_candidates
= g_list_append(backend_candidates
, candidate
);
815 sipe_backend_media_add_remote_candidates(SIPE_MEDIA_CALL
, stream
,
817 sipe_media_candidate_list_free(backend_candidates
);
819 SIPE_MEDIA_STREAM_PRIVATE
->remote_candidates_and_codecs_set
= TRUE
;
825 apply_remote_message(struct sipe_media_call_private
* call_private
,
830 sipe_utils_slist_free_full(call_private
->failed_media
, (GDestroyNotify
)sdpmedia_free
);
831 call_private
->failed_media
= NULL
;
832 call_private
->encryption_compatible
= TRUE
;
834 for (i
= msg
->media
; i
; i
= i
->next
) {
835 struct sdpmedia
*media
= i
->data
;
836 const gchar
*enc_level
=
837 sipe_utils_nameval_find(media
->attributes
, "encryption");
838 if (sipe_strequal(enc_level
, "rejected") &&
839 get_encryption_policy(call_private
->sipe_private
) == SIPE_ENCRYPTION_POLICY_REQUIRED
) {
840 call_private
->encryption_compatible
= FALSE
;
843 if (!update_call_from_remote_sdp(call_private
, media
)) {
845 call_private
->failed_media
=
846 g_slist_append(call_private
->failed_media
, media
);
850 /* We need to keep failed medias until response is sent, remove them
851 * from sdpmsg that is to be freed. */
852 for (i
= call_private
->failed_media
; i
; i
= i
->next
) {
853 msg
->media
= g_slist_remove(msg
->media
, i
->data
);
858 call_initialized(struct sipe_media_call
*call
)
860 GSList
*streams
= SIPE_MEDIA_CALL_PRIVATE
->streams
;
861 for (; streams
; streams
= streams
->next
) {
862 if (!sipe_backend_stream_initialized(call
, streams
->data
)) {
871 stream_connection_timeout_cb(struct sipe_core_private
*sipe_private
,
874 struct sipe_media_call_private
*call_private
= data
;
876 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
877 _("Couldn't create stream"),
878 _("Connection timed out"));
879 sipe_backend_media_hangup(SIPE_MEDIA_CALL
->backend_private
, TRUE
);
883 stream_schedule_timeout(struct sipe_media_call
*call
)
886 for (i
= SIPE_MEDIA_CALL_PRIVATE
->streams
; i
; i
= i
->next
) {
887 struct sipe_media_stream_private
*stream_private
= i
->data
;
889 if (stream_private
->established
)
891 stream_private
->timeout_key
=
892 g_strdup_printf("<media-stream-connect><%s><%s>",
893 sipe_media_get_sip_dialog(call
)->callid
,
894 SIPE_MEDIA_STREAM
->id
);
896 sipe_schedule_seconds(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
,
897 stream_private
->timeout_key
,
898 SIPE_MEDIA_CALL_PRIVATE
,
899 SIPE_MEDIA_STREAM_CONNECTION_TIMEOUT_SECONDS
,
900 stream_connection_timeout_cb
,
906 stream_schedule_cancel_timeout(struct sipe_media_call
*call
,
907 struct sipe_media_stream_private
*stream_private
)
909 if (stream_private
->timeout_key
) {
910 sipe_schedule_cancel(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
,
911 stream_private
->timeout_key
);
912 g_free(stream_private
->timeout_key
);
914 stream_private
->timeout_key
= NULL
;
918 call_request_timeout_cb(struct sipe_core_private
*sipe_private
,
921 struct sipe_media_call_private
*call_private
= data
;
923 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
924 _("Request timed out"),
925 _("Call could not be answered"));
926 sipe_backend_media_hangup(SIPE_MEDIA_CALL
->backend_private
, TRUE
);
930 call_schedule_request_timeout(struct sipe_media_call
*call
)
932 SIPE_MEDIA_CALL_PRIVATE
->timeout_key
=
933 g_strdup_printf("<media-call-request><%s>", sipe_media_get_sip_dialog(call
)->callid
);
935 sipe_schedule_seconds(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
,
936 SIPE_MEDIA_CALL_PRIVATE
->timeout_key
,
937 SIPE_MEDIA_CALL_PRIVATE
,
938 SIPE_MEDIA_CALL_TIMEOUT_SECONDS
,
939 call_request_timeout_cb
,
944 call_schedule_cancel_request_timeout(struct sipe_media_call
*call
)
946 if (SIPE_MEDIA_CALL_PRIVATE
->timeout_key
) {
947 sipe_schedule_cancel(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
,
948 SIPE_MEDIA_CALL_PRIVATE
->timeout_key
);
949 g_free(SIPE_MEDIA_CALL_PRIVATE
->timeout_key
);
951 SIPE_MEDIA_CALL_PRIVATE
->timeout_key
= NULL
;
956 call_ringing_timeout_cb(struct sipe_core_private
*sipe_private
,
959 struct sipe_media_call_private
*call_private
= data
;
961 sip_transport_response(sipe_private
, call_private
->invitation
,
962 408, "Request Timeout", NULL
);
963 sipe_backend_media_hangup(SIPE_MEDIA_CALL
->backend_private
, FALSE
);
967 call_schedule_ringing_timeout(struct sipe_media_call
*call
)
969 SIPE_MEDIA_CALL_PRIVATE
->ringing_key
=
970 g_strdup_printf("<media-call-ringing><%s>", sipe_media_get_sip_dialog(call
)->callid
);
972 sipe_schedule_seconds(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
,
973 SIPE_MEDIA_CALL_PRIVATE
->ringing_key
,
974 SIPE_MEDIA_CALL_PRIVATE
,
975 SIPE_MEDIA_CALL_RINGING_TIMEOUT_SECONDS
,
976 call_ringing_timeout_cb
,
981 call_schedule_cancel_ringing_timeout(struct sipe_media_call
*call
)
983 if (SIPE_MEDIA_CALL_PRIVATE
->ringing_key
) {
984 sipe_schedule_cancel(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
,
985 SIPE_MEDIA_CALL_PRIVATE
->ringing_key
);
986 g_free(SIPE_MEDIA_CALL_PRIVATE
->ringing_key
);
988 SIPE_MEDIA_CALL_PRIVATE
->ringing_key
= NULL
;
991 // Sends an invite response when the call is accepted and local candidates were
992 // prepared, otherwise does nothing. If error response is sent, call_private is
993 // disposed before function returns.
995 maybe_send_first_invite_response(struct sipe_media_call_private
*call_private
)
997 struct sipe_backend_media
*backend_media
;
999 backend_media
= call_private
->public.backend_private
;
1001 if (!sipe_backend_media_accepted(backend_media
) ||
1002 !call_initialized(&call_private
->public))
1005 if (!call_private
->encryption_compatible
) {
1006 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
1008 sipmsg_add_header(call_private
->invitation
, "Warning",
1009 "308 lcs.microsoft.com \"Encryption Levels not compatible\"");
1010 sip_transport_response(sipe_private
,
1011 call_private
->invitation
,
1012 488, "Encryption Levels not compatible",
1014 sipe_backend_media_reject(backend_media
, FALSE
);
1015 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
1016 _("Unable to establish a call"),
1017 _("Encryption settings of peer are incompatible with ours."));
1019 send_response_with_session_description(call_private
, 200, "OK");
1020 stream_schedule_timeout(SIPE_MEDIA_CALL
);
1021 call_schedule_cancel_ringing_timeout(SIPE_MEDIA_CALL
);
1022 sipmsg_free(call_private
->invitation
);
1023 call_private
->invitation
= NULL
;
1028 stream_initialized_cb(struct sipe_media_call
*call
,
1029 struct sipe_media_stream
*stream
)
1031 if (call_initialized(call
)) {
1032 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
1034 if (sipe_backend_media_is_initiator(call
, stream
)) {
1035 sipe_invite_call(call_private
,
1036 process_invite_call_response
);
1037 } else if (call_private
->smsg
) {
1038 struct sdpmsg
*smsg
= call_private
->smsg
;
1039 call_private
->smsg
= NULL
;
1041 apply_remote_message(call_private
, smsg
);
1042 maybe_send_first_invite_response(call_private
);
1048 static void phone_state_publish(struct sipe_core_private
*sipe_private
)
1050 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
1051 sipe_ocs2007_phone_state_publish(sipe_private
);
1053 // TODO: OCS 2005 support. Is anyone still using it at all?
1058 sipe_core_media_stream_end(struct sipe_media_stream
*stream
)
1060 sipe_media_stream_free(SIPE_MEDIA_STREAM_PRIVATE
);
1064 media_end_cb(struct sipe_media_call
*call
)
1066 struct sipe_core_private
*sipe_private
;
1068 g_return_if_fail(call
);
1070 sipe_private
= SIPE_MEDIA_CALL_PRIVATE
->sipe_private
;
1072 sipe_media_call_free(SIPE_MEDIA_CALL_PRIVATE
);
1073 phone_state_publish(sipe_private
);
1077 call_accept_cb(struct sipe_media_call
*call
, gboolean local
)
1080 maybe_send_first_invite_response(SIPE_MEDIA_CALL_PRIVATE
);
1082 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
);
1086 call_reject_cb(struct sipe_media_call
*call
, gboolean local
)
1089 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
1091 sip_transport_response(call_private
->sipe_private
,
1092 call_private
->invitation
,
1093 603, "Decline", NULL
);
1095 if (call_private
->session
) {
1096 sipe_session_remove(call_private
->sipe_private
,
1097 call_private
->session
);
1098 call_private
->session
= NULL
;
1104 av_call_reject_cb(struct sipe_media_call
*call
, gboolean local
)
1107 struct sipe_core_private
*sipe_private
;
1110 sipe_private
= SIPE_MEDIA_CALL_PRIVATE
->sipe_private
;
1112 desc
= g_strdup_printf(_("User %s rejected call"), call
->with
);
1113 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, _("Call rejected"),
1118 call_reject_cb(call
, local
);
1122 sipe_media_send_ack(struct sipe_core_private
*sipe_private
, struct sipmsg
*msg
,
1123 struct transaction
*trans
);
1125 static void call_hold_cb(struct sipe_media_call
*call
,
1127 SIPE_UNUSED_PARAMETER gboolean state
)
1130 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE
, sipe_media_send_ack
);
1134 static void call_hangup_cb(struct sipe_media_call
*call
, gboolean local
)
1137 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
1139 if (call_private
->session
) {
1140 sipe_session_close(call_private
->sipe_private
,
1141 call_private
->session
);
1142 call_private
->session
= NULL
;
1148 error_cb(struct sipe_media_call
*call
, gchar
*message
)
1150 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
1151 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
1152 gboolean initiator
= sipe_backend_media_is_initiator(call
, NULL
);
1153 gboolean accepted
= sipe_backend_media_accepted(call
->backend_private
);
1155 gchar
*title
= g_strdup_printf("Call with %s failed", call
->with
);
1156 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, title
, message
);
1159 if (!initiator
&& !accepted
&& call_private
->invitation
) {
1160 sip_transport_response(sipe_private
,
1161 call_private
->invitation
,
1162 488, "Not Acceptable Here", NULL
);
1166 struct sipe_media_call
*
1167 sipe_media_call_new(struct sipe_core_private
*sipe_private
, const gchar
* with
,
1168 struct sipmsg
*msg
, SipeIceVersion ice_version
,
1169 SipeMediaCallFlags flags
)
1171 struct sipe_media_call_private
*call_private
;
1172 struct sip_session
*session
;
1173 struct sip_dialog
*dialog
;
1176 session
= sipe_session_add_call(sipe_private
, with
);
1178 dialog
= sipe_dialog_add(session
);
1179 dialog
->with
= g_strdup(with
);
1182 sipmsg_update_to_header_tag(msg
);
1183 dialog
->callid
= g_strdup(sipmsg_find_call_id_header(msg
));
1184 sipe_dialog_parse(dialog
, msg
, FALSE
);
1186 dialog
->callid
= gencallid();
1187 dialog
->ourtag
= gentag();
1188 flags
|= SIPE_MEDIA_CALL_INITIATOR
;
1191 if (g_hash_table_lookup(sipe_private
->media_calls
, dialog
->callid
)) {
1192 SIPE_DEBUG_ERROR("sipe_media_call_new: call already exists for "
1193 "Call-ID %s", dialog
->callid
);
1194 sipe_session_remove(sipe_private
, session
);
1198 call_private
= g_new0(struct sipe_media_call_private
, 1);
1199 call_private
->sipe_private
= sipe_private
;
1200 call_private
->session
= session
;
1201 SIPE_MEDIA_CALL
->with
= g_strdup(with
);
1203 g_hash_table_insert(sipe_private
->media_calls
,
1204 g_strdup(dialog
->callid
), call_private
);
1206 cname
= g_strdup(sipe_private
->contact
+ 1);
1207 cname
[strlen(cname
) - 1] = '\0';
1209 call_private
->public.backend_private
= sipe_backend_media_new(SIPE_CORE_PUBLIC
,
1213 sipe_backend_media_set_cname(call_private
->public.backend_private
, cname
);
1215 call_private
->ice_version
= ice_version
;
1216 call_private
->encryption_compatible
= TRUE
;
1218 call_private
->public.stream_initialized_cb
= stream_initialized_cb
;
1219 call_private
->public.media_end_cb
= media_end_cb
;
1220 call_private
->public.call_accept_cb
= call_accept_cb
;
1221 call_private
->public.call_reject_cb
= call_reject_cb
;
1222 call_private
->public.call_hold_cb
= call_hold_cb
;
1223 call_private
->public.call_hangup_cb
= call_hangup_cb
;
1224 call_private
->public.error_cb
= error_cb
;
1228 return SIPE_MEDIA_CALL
;
1232 find_call_cb(SIPE_UNUSED_PARAMETER
const gchar
*callid
,
1233 struct sipe_media_call
*call
,
1236 return sipe_strequal(call
->with
, with
);
1239 struct sipe_media_call
*
1240 sipe_media_call_find(struct sipe_core_private
*sipe_private
, const gchar
*with
)
1242 return g_hash_table_find(sipe_private
->media_calls
,
1243 (GHRFunc
)find_call_cb
,
1247 void sipe_media_hangup(struct sipe_media_call_private
*call_private
)
1250 sipe_backend_media_hangup(call_private
->public.backend_private
,
1256 ssrc_range_compare(const struct ssrc_range
*a
, const struct ssrc_range
*b
)
1258 if (a
->begin
< b
->begin
) {
1261 if (a
->begin
> b
->begin
) {
1268 ssrc_range_update(GSList
**ranges
, GSList
*media
)
1270 for (; media
; media
= media
->next
) {
1272 const char *ssrc_range
;
1276 ssrc_range
= sipe_utils_nameval_find(m
->attributes
,
1282 parts
= g_strsplit(ssrc_range
, "-", 2);
1284 if (parts
[0] && parts
[1]) {
1285 struct ssrc_range
*range
;
1287 range
= g_new0(struct ssrc_range
, 1);
1288 range
->begin
= atoi(parts
[0]);
1289 range
->end
= atoi(parts
[1]);
1291 *ranges
= sipe_utils_slist_insert_unique_sorted(
1293 (GCompareFunc
)ssrc_range_compare
,
1301 static struct ssrc_range
*
1302 ssrc_range_allocate(GSList
**ranges
, guint32 len
)
1304 struct ssrc_range
*range
;
1307 range
= g_new0(struct ssrc_range
, 1);
1309 range
->end
= range
->begin
+ (len
- 1);
1311 for (i
= *ranges
; i
; i
= i
->next
) {
1312 struct ssrc_range
*r
= i
->data
;
1314 if (range
->begin
< r
->begin
&& range
->end
< r
->begin
) {
1318 range
->begin
= r
->end
+ 1;
1319 range
->end
= range
->begin
+ (len
- 1);
1322 /* As per [MS-SDPEXT] 3.1.5.31.1, a SSRC MUST be from 1 to 4294967040
1324 if (range
->begin
> range
->end
|| range
->end
> 0xFFFFFF00) {
1326 SIPE_DEBUG_ERROR("Couldn't allocate SSRC range of %u", len
);
1330 *ranges
= g_slist_insert_sorted(*ranges
, range
,
1331 (GCompareFunc
)ssrc_range_compare
);
1336 struct sipe_media_stream
*
1337 sipe_media_stream_add(struct sipe_media_call
*call
, const gchar
*id
,
1338 SipeMediaType type
, SipeIceVersion ice_version
,
1339 gboolean initiator
, guint32 ssrc_count
)
1341 struct sipe_core_private
*sipe_private
;
1342 struct sipe_media_stream_private
*stream_private
;
1343 struct sipe_backend_media_relays
*backend_media_relays
;
1347 sipe_private
= SIPE_MEDIA_CALL_PRIVATE
->sipe_private
;
1349 backend_media_relays
= sipe_backend_media_relays_convert(
1350 sipe_private
->media_relays
,
1351 sipe_private
->media_relay_username
,
1352 sipe_private
->media_relay_password
);
1354 min_port
= sipe_private
->min_media_port
;
1355 max_port
= sipe_private
->max_media_port
;
1357 case SIPE_MEDIA_AUDIO
:
1358 min_port
= sipe_private
->min_audio_port
;
1359 max_port
= sipe_private
->max_audio_port
;
1361 case SIPE_MEDIA_VIDEO
:
1362 min_port
= sipe_private
->min_video_port
;
1363 max_port
= sipe_private
->max_audio_port
;
1365 case SIPE_MEDIA_APPLICATION
:
1366 if (sipe_strequal(id
, "data")) {
1367 min_port
= sipe_private
->min_filetransfer_port
;
1368 max_port
= sipe_private
->max_filetransfer_port
;
1369 } else if (sipe_strequal(id
, "applicationsharing")) {
1370 min_port
= sipe_private
->min_appsharing_port
;
1371 max_port
= sipe_private
->max_appsharing_port
;
1376 stream_private
= g_new0(struct sipe_media_stream_private
, 1);
1377 SIPE_MEDIA_STREAM
->call
= call
;
1378 SIPE_MEDIA_STREAM
->id
= g_strdup(id
);
1379 stream_private
->write_queue
= g_queue_new();
1380 stream_private
->async_reads
= g_queue_new();
1382 if (ssrc_count
> 0) {
1383 SIPE_MEDIA_STREAM
->ssrc_range
=
1384 ssrc_range_allocate(&SIPE_MEDIA_CALL_PRIVATE
->ssrc_ranges
,
1388 SIPE_MEDIA_STREAM
->backend_private
=
1389 sipe_backend_media_add_stream(SIPE_MEDIA_STREAM
,
1392 backend_media_relays
,
1393 min_port
, max_port
);
1395 sipe_backend_media_relays_free(backend_media_relays
);
1397 if (!SIPE_MEDIA_STREAM
->backend_private
) {
1398 sipe_media_stream_free(stream_private
);
1402 if (type
== SIPE_MEDIA_VIDEO
) {
1403 /* Declare that we can send and receive Video Source Requests
1404 * as per [MS-SDPEXT] 3.1.5.30.2. */
1405 sipe_media_stream_add_extra_attribute(SIPE_MEDIA_STREAM
,
1406 "rtcp-fb", "* x-message app send:src recv:src");
1408 sipe_media_stream_add_extra_attribute(SIPE_MEDIA_STREAM
,
1409 "rtcp-rsize", NULL
);
1410 sipe_media_stream_add_extra_attribute(SIPE_MEDIA_STREAM
,
1411 "label", "main-video");
1412 sipe_media_stream_add_extra_attribute(SIPE_MEDIA_STREAM
,
1413 "x-source", "main-video");
1417 if (get_encryption_policy(sipe_private
) != SIPE_ENCRYPTION_POLICY_REJECTED
) {
1419 stream_private
->encryption_key
= g_new0(guchar
, SIPE_SRTP_KEY_LEN
);
1420 for (i
= 0; i
!= SIPE_SRTP_KEY_LEN
; ++i
) {
1421 stream_private
->encryption_key
[i
] = rand() & 0xff;
1423 stream_private
->encryption_key_id
= 1;
1424 // We don't know yet whether the stream will be
1425 // encrypted or not. Enable the require-encryption
1426 // property at stream creation time anyway, we may
1427 // disable it later if we don't receive encryption keys.
1428 sipe_backend_media_set_require_encryption(call
,
1434 SIPE_MEDIA_CALL_PRIVATE
->streams
=
1435 g_slist_append(SIPE_MEDIA_CALL_PRIVATE
->streams
,
1438 return SIPE_MEDIA_STREAM
;
1442 append_2007_fallback_if_needed(struct sipe_media_call_private
*call_private
)
1444 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
1445 const gchar
*marker
= sip_transport_sdp_address_marker(sipe_private
);
1446 const gchar
*ip
= sip_transport_ip_address(sipe_private
);
1449 if (SIPE_CORE_PRIVATE_FLAG_IS(SFB
) ||
1450 sipe_media_get_sip_dialog(SIPE_MEDIA_CALL
)->cseq
!= 0 ||
1451 call_private
->ice_version
!= SIPE_ICE_RFC_5245
||
1452 sipe_strequal(SIPE_MEDIA_CALL
->with
, sipe_private
->test_call_bot_uri
)) {
1456 body
= g_strdup_printf("Content-Type: application/sdp\r\n"
1457 "Content-Transfer-Encoding: 7bit\r\n"
1458 "Content-Disposition: session; handling=optional; ms-proxy-2007fallback\r\n"
1460 "o=- 0 0 IN %s %s\r\n"
1463 "m=audio 0 RTP/AVP\r\n",
1466 sipe_media_add_extra_invite_section(SIPE_MEDIA_CALL
,
1467 "multipart/alternative", body
);
1471 sipe_media_initiate_call(struct sipe_core_private
*sipe_private
,
1472 const char *with
, SipeIceVersion ice_version
,
1473 gboolean with_video
)
1475 struct sipe_media_call_private
*call_private
;
1477 if (sipe_core_media_get_call(SIPE_CORE_PUBLIC
)) {
1481 call_private
= (struct sipe_media_call_private
*)
1482 sipe_media_call_new(sipe_private
, with
, NULL
,
1485 SIPE_MEDIA_CALL
->call_reject_cb
= av_call_reject_cb
;
1487 if (!sipe_media_stream_add(SIPE_MEDIA_CALL
, "audio", SIPE_MEDIA_AUDIO
,
1488 call_private
->ice_version
,
1490 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
1491 _("Error occurred"),
1492 _("Error creating audio stream"));
1493 sipe_media_hangup(call_private
);
1498 !sipe_media_stream_add(SIPE_MEDIA_CALL
, "video", SIPE_MEDIA_VIDEO
,
1499 call_private
->ice_version
,
1500 TRUE
, VIDEO_SSRC_COUNT
)) {
1501 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
1502 _("Error occurred"),
1503 _("Error creating video stream"));
1504 sipe_media_hangup(call_private
);
1508 append_2007_fallback_if_needed(call_private
);
1510 call_schedule_request_timeout(SIPE_MEDIA_CALL
);
1511 // Processing continues in stream_initialized_cb
1515 sipe_core_media_initiate_call(struct sipe_core_public
*sipe_public
,
1517 gboolean with_video
)
1519 sipe_media_initiate_call(SIPE_CORE_PRIVATE
, with
,
1520 SIPE_ICE_RFC_5245
, with_video
);
1524 conference_audio_muted_cb(struct sipe_media_stream
*stream
, gboolean is_muted
)
1526 struct sipe_media_call
*call
= stream
->call
;
1528 if (!SIPE_MEDIA_CALL_PRIVATE
->conference_session
) {
1532 sipe_conf_announce_audio_mute_state(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
,
1533 SIPE_MEDIA_CALL_PRIVATE
->conference_session
,
1537 void sipe_core_media_connect_conference(struct sipe_core_public
*sipe_public
,
1538 struct sipe_chat_session
*chat_session
)
1540 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1541 struct sipe_media_call_private
*call_private
;
1542 struct sipe_media_stream
*stream
;
1543 struct sip_session
*session
;
1544 SipeIceVersion ice_version
;
1547 if (!sipe_conf_supports_mcu_type(sipe_private
, "audio-video")) {
1548 sipe_backend_notify_error(sipe_public
, _("Join conference call"),
1549 _("Conference calls are not supported on this server."));
1553 session
= sipe_session_find_chat(sipe_private
, chat_session
);
1555 if (sipe_core_media_get_call(sipe_public
) || !session
) {
1559 av_uri
= sipe_conf_build_uri(sipe_core_chat_id(sipe_public
, chat_session
),
1565 session
->is_call
= TRUE
;
1567 ice_version
= SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013
) ? SIPE_ICE_RFC_5245
:
1570 call_private
= (struct sipe_media_call_private
*)
1571 sipe_media_call_new(sipe_private
, av_uri
, NULL
,
1573 call_private
->conference_session
= session
;
1574 SIPE_MEDIA_CALL
->call_reject_cb
= av_call_reject_cb
;
1576 stream
= sipe_media_stream_add(SIPE_MEDIA_CALL
, "audio",
1578 call_private
->ice_version
,
1581 sipe_backend_notify_error(sipe_public
,
1582 _("Error occurred"),
1583 _("Error creating audio stream"));
1585 sipe_media_hangup(call_private
);
1588 stream
->mute_cb
= conference_audio_muted_cb
;
1592 // Processing continues in stream_initialized_cb
1595 struct sipe_media_call
*
1596 sipe_core_media_get_call(struct sipe_core_public
*sipe_public
)
1598 struct sipe_media_call
* result
= NULL
;
1599 GList
*calls
= g_hash_table_get_values(SIPE_CORE_PRIVATE
->media_calls
);
1600 GList
*entry
= calls
;
1603 if (sipe_core_media_get_stream_by_id(entry
->data
, "audio")) {
1604 result
= entry
->data
;
1607 entry
= entry
->next
;
1614 static gboolean
phone_number_is_valid(const gchar
*phone_number
)
1616 if (!phone_number
|| sipe_strequal(phone_number
, "")) {
1620 if (*phone_number
== '+') {
1624 while (*phone_number
!= '\0') {
1625 if (!g_ascii_isdigit(*phone_number
)) {
1634 void sipe_core_media_phone_call(struct sipe_core_public
*sipe_public
,
1635 const gchar
*phone_number
)
1637 g_return_if_fail(sipe_public
);
1639 SIPE_DEBUG_INFO("sipe_core_media_phone_call: %s", phone_number
? phone_number
: "(null)");
1641 if (phone_number_is_valid(phone_number
)) {
1642 gchar
*phone_uri
= g_strdup_printf("sip:%s@%s;user=phone",
1643 phone_number
, sipe_public
->sip_domain
);
1645 sipe_core_media_initiate_call(sipe_public
, phone_uri
, FALSE
);
1649 sipe_backend_notify_error(sipe_public
,
1650 _("Unable to establish a call"),
1651 _("Invalid phone number"));
1655 void sipe_core_media_test_call(struct sipe_core_public
*sipe_public
)
1657 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1658 if (!sipe_private
->test_call_bot_uri
) {
1659 sipe_backend_notify_error(sipe_public
,
1660 _("Unable to establish a call"),
1661 _("Audio Test Service is not available."));
1665 sipe_core_media_initiate_call(sipe_public
,
1666 sipe_private
->test_call_bot_uri
, FALSE
);
1669 static struct sipe_media_call_private
*
1670 sipe_media_from_sipmsg(struct sipe_core_private
*sipe_private
,
1673 return g_hash_table_lookup(sipe_private
->media_calls
,
1674 sipmsg_find_call_id_header(msg
));
1678 transport_response_unsupported_sdp(struct sipe_core_private
*sipe_private
,
1681 sipmsg_add_header(msg
, "ms-client-diagnostics",
1682 "52063;reason=\"Unsupported session description\"");
1683 sip_transport_response(sipe_private
, msg
,
1684 488, "Not Acceptable Here", NULL
);
1688 maybe_send_second_invite_response(struct sipe_media_call_private
*call_private
)
1692 /* Second INVITE request had to be received and all streams must have
1693 * established candidate pairs before the response can be sent. */
1695 if (!call_private
->invitation
) {
1699 for (it
= call_private
->streams
; it
; it
= it
->next
) {
1700 struct sipe_media_stream_private
*stream_private
= it
->data
;
1701 if (!stream_private
->established
) {
1706 send_response_with_session_description(call_private
, 200, "OK");
1709 for (it
= call_private
->streams
; it
; it
= it
->next
) {
1710 struct sipe_media_stream_private
*stream_private
= it
->data
;
1712 stream_private
->sdp_negotiation_concluded
= TRUE
;
1713 if (stream_private
->writable
) {
1714 // We've become writable.
1715 sipe_core_media_stream_writable(SIPE_MEDIA_STREAM
, TRUE
);
1721 struct sipe_media_call
*
1722 process_incoming_invite_call(struct sipe_core_private
*sipe_private
,
1726 return(process_incoming_invite_call_parsed_sdp(sipe_private
,
1728 sdpmsg_parse_msg(sdp
)));
1731 struct sipe_media_call
*
1732 process_incoming_invite_call_parsed_sdp(struct sipe_core_private
*sipe_private
,
1734 struct sdpmsg
*smsg
)
1736 struct sipe_media_call_private
*call_private
;
1737 gboolean has_new_media
= FALSE
;
1740 // Don't allow two voice calls in parallel.
1741 if (!strstr(msg
->body
, "m=data") &&
1742 !strstr(msg
->body
, "m=applicationsharing")) {
1743 struct sipe_media_call
*call
=
1744 sipe_core_media_get_call(SIPE_CORE_PUBLIC
);
1745 if (call
&& !is_media_session_msg(SIPE_MEDIA_CALL_PRIVATE
, msg
)) {
1746 sip_transport_response(sipe_private
, msg
,
1747 486, "Busy Here", NULL
);
1753 call_private
= sipe_media_from_sipmsg(sipe_private
, msg
);
1756 char *self
= sip_uri_self(sipe_private
);
1757 if (sipe_strequal(SIPE_MEDIA_CALL
->with
, self
)) {
1759 sip_transport_response(sipe_private
, msg
, 488, "Not Acceptable Here", NULL
);
1767 transport_response_unsupported_sdp(sipe_private
, msg
);
1769 sipe_media_hangup(call_private
);
1774 if (!call_private
) {
1775 gchar
*with
= sipmsg_parse_from_address(msg
);
1776 SipeMediaCallFlags flags
= 0;
1778 if (strstr(msg
->body
, "m=data") ||
1779 strstr(msg
->body
, "m=applicationsharing")) {
1780 flags
|= SIPE_MEDIA_CALL_NO_UI
;
1783 call_private
= (struct sipe_media_call_private
*)
1784 sipe_media_call_new(sipe_private
, with
,
1785 msg
, smsg
->ice_version
,
1788 if (!(flags
& SIPE_MEDIA_CALL_NO_UI
)) {
1789 SIPE_MEDIA_CALL
->call_reject_cb
= av_call_reject_cb
;
1794 if (call_private
->invitation
)
1795 sipmsg_free(call_private
->invitation
);
1796 call_private
->invitation
= sipmsg_copy(msg
);
1798 ssrc_range_update(&call_private
->ssrc_ranges
, smsg
->media
);
1800 // Create any new media streams
1801 for (i
= smsg
->media
; i
; i
= i
->next
) {
1802 struct sdpmedia
*media
= i
->data
;
1803 gchar
*id
= media
->name
;
1806 if ( media
->port
!= 0
1807 && !sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL
, id
)) {
1808 guint32 ssrc_count
= 0;
1810 if (sipe_strequal(id
, "audio"))
1811 type
= SIPE_MEDIA_AUDIO
;
1812 else if (sipe_strequal(id
, "video")) {
1813 type
= SIPE_MEDIA_VIDEO
;
1814 ssrc_count
= VIDEO_SSRC_COUNT
;
1815 } else if (sipe_strequal(id
, "data"))
1816 type
= SIPE_MEDIA_APPLICATION
;
1817 else if (sipe_strequal(id
, "applicationsharing"))
1818 type
= SIPE_MEDIA_APPLICATION
;
1822 sipe_media_stream_add(SIPE_MEDIA_CALL
, id
, type
,
1823 smsg
->ice_version
, FALSE
,
1825 has_new_media
= TRUE
;
1829 if (has_new_media
) {
1830 sdpmsg_free(call_private
->smsg
);
1831 call_private
->smsg
= smsg
;
1832 sip_transport_response(sipe_private
, call_private
->invitation
,
1833 180, "Ringing", NULL
);
1834 call_schedule_ringing_timeout(SIPE_MEDIA_CALL
);
1835 // Processing continues in stream_initialized_cb
1837 apply_remote_message(call_private
, smsg
);
1839 maybe_send_second_invite_response(call_private
);
1842 return SIPE_MEDIA_CALL
;
1845 void process_incoming_cancel_call(struct sipe_media_call_private
*call_private
,
1848 // We respond to the CANCEL request with 200 OK response and
1849 // with 487 Request Terminated to the remote INVITE in progress.
1850 sip_transport_response(call_private
->sipe_private
, msg
, 200, "OK", NULL
);
1852 if (call_private
->invitation
) {
1853 sip_transport_response(call_private
->sipe_private
,
1854 call_private
->invitation
,
1855 487, "Request Terminated", NULL
);
1858 sipe_backend_media_reject(SIPE_MEDIA_CALL
->backend_private
, FALSE
);
1862 sipe_media_send_ack(struct sipe_core_private
*sipe_private
,
1864 struct transaction
*trans
)
1866 struct sipe_media_call_private
*call_private
;
1867 struct sip_dialog
*dialog
;
1870 call_private
= sipe_media_from_sipmsg(sipe_private
, msg
);
1872 if (!is_media_session_msg(call_private
, msg
))
1875 dialog
= sipe_media_get_sip_dialog(SIPE_MEDIA_CALL
);
1879 tmp_cseq
= dialog
->cseq
;
1881 dialog
->cseq
= sip_transaction_cseq(trans
) - 1;
1882 sip_transport_ack(sipe_private
, dialog
);
1883 dialog
->cseq
= tmp_cseq
;
1885 dialog
->outgoing_invite
= NULL
;
1891 sipe_media_send_final_ack(struct sipe_core_private
*sipe_private
,
1893 struct transaction
*trans
)
1895 struct sipe_media_call_private
*call_private
;
1900 if (!sipe_media_send_ack(sipe_private
, msg
, trans
))
1903 call_private
= sipe_media_from_sipmsg(sipe_private
, msg
);
1905 sipe_backend_media_accept(SIPE_MEDIA_CALL
->backend_private
, FALSE
);
1908 for (it
= call_private
->streams
; it
; it
= it
->next
) {
1909 struct sipe_media_stream_private
*stream_private
= it
->data
;
1911 stream_private
->sdp_negotiation_concluded
= TRUE
;
1912 if (stream_private
->writable
) {
1913 // We've become writable.
1914 sipe_core_media_stream_writable(SIPE_MEDIA_STREAM
, TRUE
);
1923 sipe_core_media_stream_candidate_pair_established(struct sipe_media_stream
*stream
)
1925 struct sipe_media_call
*call
= stream
->call
;
1927 GList
*active_candidates
=
1928 sipe_backend_media_stream_get_active_local_candidates(stream
);
1929 guint ready_components
= g_list_length(active_candidates
);
1931 sipe_media_candidate_list_free(active_candidates
);
1933 if (ready_components
!= 2) {
1934 // We must have both RTP+RTCP candidate pairs established first.
1938 if (SIPE_MEDIA_STREAM_PRIVATE
->established
) {
1941 SIPE_MEDIA_STREAM_PRIVATE
->established
= TRUE
;
1943 stream_schedule_cancel_timeout(call
, SIPE_MEDIA_STREAM_PRIVATE
);
1945 if (stream
->candidate_pairs_established_cb
) {
1946 stream
->candidate_pairs_established_cb(stream
);
1949 if (sipe_backend_media_is_initiator(stream
->call
, NULL
)) {
1950 GSList
*streams
= SIPE_MEDIA_CALL_PRIVATE
->streams
;
1951 for (; streams
; streams
= streams
->next
) {
1952 struct sipe_media_stream_private
*s
= streams
->data
;
1953 if (!s
->established
) {
1958 if (streams
== NULL
) {
1959 // All call streams have been established.
1960 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE
,
1961 sipe_media_send_final_ack
);
1964 maybe_send_second_invite_response(SIPE_MEDIA_CALL_PRIVATE
);
1969 maybe_retry_call_with_ice_version(struct sipe_core_private
*sipe_private
,
1970 struct sipe_media_call_private
*call_private
,
1971 SipeIceVersion ice_version
,
1972 struct transaction
*trans
)
1974 if (call_private
->ice_version
!= ice_version
&&
1975 sip_transaction_cseq(trans
) == 1) {
1978 gboolean with_video
= FALSE
;
1980 for (i
= call_private
->streams
; i
; i
= i
->next
) {
1981 struct sipe_media_stream
*stream
= i
->data
;
1983 if (sipe_strequal(stream
->id
, "video")) {
1985 } else if (!sipe_strequal(stream
->id
, "audio")) {
1986 /* Don't retry calls which are neither audio
1992 with
= g_strdup(SIPE_MEDIA_CALL
->with
);
1994 sipe_media_hangup(call_private
);
1995 SIPE_DEBUG_INFO("Retrying call with ICEv%d.",
1996 ice_version
== SIPE_ICE_DRAFT_6
? 6 : 19);
1997 sipe_media_initiate_call(sipe_private
,
2010 process_invite_call_response(struct sipe_core_private
*sipe_private
,
2012 struct transaction
*trans
)
2015 struct sipe_media_call_private
*call_private
;
2016 struct sip_dialog
*dialog
;
2017 struct sdpmsg
*smsg
;
2019 call_private
= sipe_media_from_sipmsg(sipe_private
,msg
);
2021 if (!is_media_session_msg(call_private
, msg
))
2024 dialog
= sipe_media_get_sip_dialog(SIPE_MEDIA_CALL
);
2026 with
= dialog
->with
;
2028 dialog
->outgoing_invite
= NULL
;
2030 if (msg
->response
== 603 || msg
->response
== 605) {
2031 // Call rejected by remote peer
2032 sipe_media_send_ack(sipe_private
, msg
, trans
);
2033 sipe_backend_media_reject(SIPE_MEDIA_CALL
->backend_private
, FALSE
);
2038 if (msg
->response
>= 400) {
2039 // An error occurred
2041 GString
*desc
= g_string_new("");
2042 gboolean append_responsestr
= FALSE
;
2044 switch (msg
->response
) {
2046 title
= _("User unavailable");
2048 if (sipmsg_parse_warning(msg
, NULL
) == 391) {
2049 g_string_append_printf(desc
, _("%s does not want to be disturbed"), with
);
2051 g_string_append_printf(desc
, _("User %s is not available"), with
);
2055 // OCS/Lync really sends response string with 'Mutipart' typo.
2056 if (sipe_strequal(msg
->responsestr
, "Mutipart mime in content type not supported by Archiving CDR service") &&
2057 maybe_retry_call_with_ice_version(sipe_private
,
2063 title
= _("Unsupported media type");
2066 /* Check for incompatible encryption levels error.
2069 * 488 Not Acceptable Here
2070 * ms-client-diagnostics: 52017;reason="Encryption levels dont match"
2072 * older clients (and SIPE itself):
2073 * 488 Encryption Levels not compatible
2075 const gchar
*ms_diag
= sipmsg_find_header(msg
, "ms-client-diagnostics");
2076 SipeIceVersion retry_ice_version
= SIPE_ICE_DRAFT_6
;
2078 if (sipe_strequal(msg
->responsestr
, "Encryption Levels not compatible") ||
2079 (ms_diag
&& g_str_has_prefix(ms_diag
, "52017;"))) {
2080 title
= _("Unable to establish a call");
2081 g_string_append(desc
, _("Encryption settings of peer are incompatible with ours."));
2085 /* Check if this is failed conference using
2086 * ICEv6 with reason "Error parsing SDP" and
2087 * retry using ICEv19. */
2088 ms_diag
= sipmsg_find_header(msg
, "ms-diagnostics");
2089 if (ms_diag
&& g_str_has_prefix(ms_diag
, "7008;")) {
2090 retry_ice_version
= SIPE_ICE_RFC_5245
;
2093 if (maybe_retry_call_with_ice_version(sipe_private
,
2102 title
= _("Error occurred");
2103 g_string_append(desc
, _("Unable to establish a call"));
2104 append_responsestr
= TRUE
;
2108 if (append_responsestr
) {
2109 gchar
*reason
= sipmsg_get_ms_diagnostics_reason(msg
);
2111 g_string_append_printf(desc
, "\n%d %s",
2112 msg
->response
, msg
->responsestr
);
2114 g_string_append_printf(desc
, "\n\n%s", reason
);
2119 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, title
, desc
->str
);
2120 g_string_free(desc
, TRUE
);
2122 sipe_media_send_ack(sipe_private
, msg
, trans
);
2123 sipe_media_hangup(call_private
);
2128 sipe_dialog_parse(dialog
, msg
, TRUE
);
2129 smsg
= sdpmsg_parse_msg(msg
->body
);
2131 transport_response_unsupported_sdp(sipe_private
, msg
);
2132 sipe_media_hangup(call_private
);
2136 ssrc_range_update(&call_private
->ssrc_ranges
, smsg
->media
);
2137 apply_remote_message(call_private
, smsg
);
2140 stream_schedule_timeout(SIPE_MEDIA_CALL
);
2141 call_schedule_cancel_request_timeout(SIPE_MEDIA_CALL
);
2142 sipe_media_send_ack(sipe_private
, msg
, trans
);
2146 // Waits until sipe_core_media_candidate_pair_established() is invoked.
2149 gboolean
is_media_session_msg(struct sipe_media_call_private
*call_private
,
2152 if (!call_private
) {
2156 return sipe_media_from_sipmsg(call_private
->sipe_private
, msg
) == call_private
;
2160 end_call(SIPE_UNUSED_PARAMETER gpointer key
,
2161 struct sipe_media_call_private
*call_private
,
2162 SIPE_UNUSED_PARAMETER gpointer user_data
)
2164 struct sipe_backend_media
*backend_private
;
2166 backend_private
= call_private
->public.backend_private
;
2168 if (!sipe_backend_media_is_initiator(SIPE_MEDIA_CALL
, NULL
) &&
2169 !sipe_backend_media_accepted(backend_private
)) {
2170 sip_transport_response(call_private
->sipe_private
,
2171 call_private
->invitation
,
2172 480, "Temporarily Unavailable", NULL
);
2173 } else if (call_private
->session
) {
2174 sipe_session_close(call_private
->sipe_private
,
2175 call_private
->session
);
2176 call_private
->session
= NULL
;
2179 sipe_media_hangup(call_private
);
2183 sipe_media_handle_going_offline(struct sipe_core_private
*sipe_private
)
2185 g_hash_table_foreach(sipe_private
->media_calls
, (GHFunc
) end_call
, NULL
);
2188 gboolean
sipe_media_is_conference_call(struct sipe_media_call_private
*call_private
)
2190 return g_strstr_len(SIPE_MEDIA_CALL
->with
, -1, "app:conf:audio-video:") != NULL
;
2193 struct sipe_core_private
*
2194 sipe_media_get_sipe_core_private(struct sipe_media_call
*call
)
2196 g_return_val_if_fail(call
, NULL
);
2198 return SIPE_MEDIA_CALL_PRIVATE
->sipe_private
;
2202 sipe_media_get_sip_dialog(struct sipe_media_call
*call
)
2204 struct sip_session
*session
;
2206 g_return_val_if_fail(call
, NULL
);
2208 session
= SIPE_MEDIA_CALL_PRIVATE
->session
;
2210 if (!session
|| !session
->dialogs
) {
2214 return session
->dialogs
->data
;
2218 sipe_media_relay_free(struct sipe_media_relay
*relay
)
2220 g_free(relay
->hostname
);
2221 if (relay
->dns_query
)
2222 sipe_backend_dns_query_cancel(relay
->dns_query
);
2227 sipe_media_relay_list_free(GSList
*list
)
2229 for (; list
; list
= g_slist_delete_link(list
, list
))
2230 sipe_media_relay_free(list
->data
);
2234 relay_ip_resolved_cb(struct sipe_media_relay
* relay
,
2235 const gchar
*ip
, SIPE_UNUSED_PARAMETER guint port
)
2237 gchar
*hostname
= relay
->hostname
;
2238 relay
->dns_query
= NULL
;
2241 relay
->hostname
= g_strdup(ip
);
2242 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname
, ip
);
2244 relay
->hostname
= NULL
;
2245 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname
);
2252 process_get_av_edge_credentials_response(struct sipe_core_private
*sipe_private
,
2254 SIPE_UNUSED_PARAMETER
struct transaction
*trans
)
2256 g_free(sipe_private
->media_relay_username
);
2257 g_free(sipe_private
->media_relay_password
);
2258 sipe_media_relay_list_free(sipe_private
->media_relays
);
2259 sipe_private
->media_relay_username
= NULL
;
2260 sipe_private
->media_relay_password
= NULL
;
2261 sipe_private
->media_relays
= NULL
;
2263 if (msg
->response
>= 400) {
2264 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
2265 "Failed to obtain A/V Edge credentials.");
2269 if (msg
->response
== 200) {
2270 sipe_xml
*xn_response
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
2272 if (sipe_strequal("OK", sipe_xml_attribute(xn_response
, "reasonPhrase"))) {
2273 const sipe_xml
*xn_credentials
= sipe_xml_child(xn_response
, "credentialsResponse/credentials");
2274 const sipe_xml
*xn_relays
= sipe_xml_child(xn_response
, "credentialsResponse/mediaRelayList");
2275 const sipe_xml
*item
;
2276 GSList
*relays
= NULL
;
2278 item
= sipe_xml_child(xn_credentials
, "username");
2279 sipe_private
->media_relay_username
= sipe_xml_data(item
);
2280 item
= sipe_xml_child(xn_credentials
, "password");
2281 sipe_private
->media_relay_password
= sipe_xml_data(item
);
2283 for (item
= sipe_xml_child(xn_relays
, "mediaRelay"); item
; item
= sipe_xml_twin(item
)) {
2284 struct sipe_media_relay
*relay
= g_new0(struct sipe_media_relay
, 1);
2285 const sipe_xml
*node
;
2288 node
= sipe_xml_child(item
, "hostName");
2289 relay
->hostname
= sipe_xml_data(node
);
2291 node
= sipe_xml_child(item
, "udpPort");
2293 tmp
= sipe_xml_data(node
);
2295 relay
->udp_port
= atoi(tmp
);
2300 node
= sipe_xml_child(item
, "tcpPort");
2302 tmp
= sipe_xml_data(node
);
2304 relay
->tcp_port
= atoi(tmp
);
2309 relays
= g_slist_append(relays
, relay
);
2311 relay
->dns_query
= sipe_backend_dns_query_a(
2315 (sipe_dns_resolved_cb
) relay_ip_resolved_cb
,
2318 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
2320 relay
->tcp_port
, relay
->udp_port
);
2323 sipe_private
->media_relays
= relays
;
2326 sipe_xml_free(xn_response
);
2333 sipe_media_get_av_edge_credentials(struct sipe_core_private
*sipe_private
)
2335 // TODO: re-request credentials after duration expires?
2336 static const char CRED_REQUEST_XML
[] =
2337 "<request requestID=\"%d\" "
2341 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
2342 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
2343 "<credentialsRequest credentialsRequestID=\"%d\">"
2344 "<identity>%s</identity>"
2345 "<location>%s</location>"
2346 "<duration>480</duration>"
2347 "</credentialsRequest>"
2350 int request_id
= rand();
2354 if (!sipe_private
->mras_uri
)
2357 self
= sip_uri_self(sipe_private
);
2359 body
= g_strdup_printf(
2363 sipe_private
->mras_uri
,
2366 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER
) ? "internet" : "intranet");
2369 sip_transport_service(sipe_private
,
2370 sipe_private
->mras_uri
,
2371 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
2373 process_get_av_edge_credentials_response
);
2379 sipe_media_add_extra_invite_section(struct sipe_media_call
*call
,
2380 const gchar
*invite_content_type
,
2383 g_free(SIPE_MEDIA_CALL_PRIVATE
->extra_invite_section
);
2384 g_free(SIPE_MEDIA_CALL_PRIVATE
->invite_content_type
);
2385 SIPE_MEDIA_CALL_PRIVATE
->extra_invite_section
= body
;
2386 SIPE_MEDIA_CALL_PRIVATE
->invite_content_type
=
2387 g_strdup(invite_content_type
);
2391 sipe_media_stream_add_extra_attribute(struct sipe_media_stream
*stream
,
2392 const gchar
*name
, const gchar
*value
)
2394 SIPE_MEDIA_STREAM_PRIVATE
->extra_sdp
=
2395 sipe_utils_nameval_add(SIPE_MEDIA_STREAM_PRIVATE
->extra_sdp
,
2401 sipe_core_media_stream_readable(struct sipe_media_stream
*stream
)
2403 g_return_if_fail(stream
);
2405 if (g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE
->async_reads
) &&
2407 stream
->read_cb(stream
);
2410 while (!g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE
->async_reads
)) {
2411 struct async_read_data
*data
;
2416 data
= g_queue_peek_head(SIPE_MEDIA_STREAM_PRIVATE
->async_reads
);
2417 pos
= data
->buffer
+ SIPE_MEDIA_STREAM_PRIVATE
->read_pos
;
2418 len
= data
->len
- SIPE_MEDIA_STREAM_PRIVATE
->read_pos
;
2420 bytes_read
= sipe_backend_media_stream_read(stream
, pos
, len
);
2421 if (bytes_read
== -1) {
2422 struct sipe_media_call
*call
= stream
->call
;
2423 struct sipe_core_private
*sipe_private
=
2424 SIPE_MEDIA_CALL_PRIVATE
->sipe_private
;
2426 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
2428 _("Error while reading from stream"));
2429 sipe_media_hangup(SIPE_MEDIA_CALL_PRIVATE
);
2433 SIPE_MEDIA_STREAM_PRIVATE
->read_pos
+= bytes_read
;
2435 if (SIPE_MEDIA_STREAM_PRIVATE
->read_pos
== data
->len
) {
2436 data
->callback(stream
, data
->buffer
, data
->len
);
2437 SIPE_MEDIA_STREAM_PRIVATE
->read_pos
= 0;
2438 g_queue_pop_head(SIPE_MEDIA_STREAM_PRIVATE
->async_reads
);
2441 // Still not enough data to finish the read.
2448 sipe_media_stream_read_async(struct sipe_media_stream
*stream
,
2449 gpointer buffer
, gsize len
,
2450 sipe_media_stream_read_callback callback
)
2452 struct async_read_data
*data
;
2454 g_return_if_fail(stream
&& buffer
&& callback
);
2456 data
= g_new0(struct async_read_data
, 1);
2457 data
->buffer
= buffer
;
2459 data
->callback
= callback
;
2461 g_queue_push_tail(SIPE_MEDIA_STREAM_PRIVATE
->async_reads
, data
);
2465 stream_append_buffer(struct sipe_media_stream
*stream
,
2466 guint8
*buffer
, guint len
)
2468 GByteArray
*b
= g_byte_array_sized_new(len
);
2469 g_byte_array_append(b
, buffer
, len
);
2470 g_queue_push_tail(SIPE_MEDIA_STREAM_PRIVATE
->write_queue
, b
);
2474 sipe_media_stream_write(struct sipe_media_stream
*stream
,
2475 gpointer buffer
, gsize len
)
2477 if (!sipe_media_stream_is_writable(stream
)) {
2478 stream_append_buffer(stream
, buffer
, len
);
2483 written
= sipe_backend_media_stream_write(stream
, buffer
, len
);
2484 if (written
== len
) {
2488 stream_append_buffer(stream
,
2489 (guint8
*)buffer
+ written
, len
- written
);
2495 sipe_core_media_stream_writable(struct sipe_media_stream
*stream
,
2498 SIPE_MEDIA_STREAM_PRIVATE
->writable
= writable
;
2504 while (!g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE
->write_queue
)) {
2508 b
= g_queue_peek_head(SIPE_MEDIA_STREAM_PRIVATE
->write_queue
);
2510 written
= sipe_backend_media_stream_write(stream
, b
->data
, b
->len
);
2511 if (written
!= b
->len
) {
2512 g_byte_array_remove_range(b
, 0, written
);
2516 g_byte_array_unref(b
);
2517 g_queue_pop_head(SIPE_MEDIA_STREAM_PRIVATE
->write_queue
);
2520 if (sipe_media_stream_is_writable(stream
) && stream
->writable_cb
) {
2521 stream
->writable_cb(stream
);
2526 sipe_media_stream_is_writable(struct sipe_media_stream
*stream
)
2528 return SIPE_MEDIA_STREAM_PRIVATE
->writable
&&
2529 SIPE_MEDIA_STREAM_PRIVATE
->sdp_negotiation_concluded
&&
2530 g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE
->write_queue
);
2535 sipe_media_stream_set_data(struct sipe_media_stream
*stream
, gpointer data
,
2536 GDestroyNotify free_func
)
2538 struct sipe_media_stream_private
*stream_private
=
2539 SIPE_MEDIA_STREAM_PRIVATE
;
2541 g_return_if_fail(stream_private
);
2543 if (stream_private
->data
&& stream_private
->data_free_func
) {
2544 stream_private
->data_free_func(stream_private
->data
);
2547 stream_private
->data
= data
;
2548 stream_private
->data_free_func
= free_func
;
2552 sipe_media_stream_get_data(struct sipe_media_stream
*stream
)
2554 g_return_val_if_fail(stream
, NULL
);
2556 return SIPE_MEDIA_STREAM_PRIVATE
->data
;