6 * Copyright (C) 2011-2017 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
;
78 #define SIPE_MEDIA_CALL ((struct sipe_media_call *) call_private)
79 #define SIPE_MEDIA_CALL_PRIVATE ((struct sipe_media_call_private *) call)
81 struct sipe_media_stream_private
{
82 struct sipe_media_stream
public;
86 guchar
*encryption_key
;
87 int encryption_key_id
;
88 gboolean remote_candidates_and_codecs_set
;
91 gboolean sdp_negotiation_concluded
;
101 /* User data associated with the stream. */
103 GDestroyNotify data_free_func
;
105 #define SIPE_MEDIA_STREAM ((struct sipe_media_stream *) stream_private)
106 #define SIPE_MEDIA_STREAM_PRIVATE ((struct sipe_media_stream_private *) stream)
108 #define SIPE_MEDIA_STREAM_CONNECTION_TIMEOUT_SECONDS 30
109 #define SIPE_MEDIA_CALL_TIMEOUT_SECONDS 120
111 struct async_read_data
{
114 sipe_media_stream_read_callback callback
;
117 static void stream_schedule_cancel_timeout(struct sipe_media_call
*call
,
118 struct sipe_media_stream_private
*stream_private
);
120 static void call_schedule_cancel_request_timeout(struct sipe_media_call
*call
);
122 static void sipe_media_codec_list_free(GList
*codecs
)
124 for (; codecs
; codecs
= g_list_delete_link(codecs
, codecs
))
125 sipe_backend_codec_free(codecs
->data
);
128 static void sipe_media_candidate_list_free(GList
*candidates
)
130 for (; candidates
; candidates
= g_list_delete_link(candidates
, candidates
))
131 sipe_backend_candidate_free(candidates
->data
);
135 sipe_media_stream_free(struct sipe_media_stream_private
*stream_private
)
137 struct sipe_media_call_private
*call_private
;
139 call_private
= (struct sipe_media_call_private
*)SIPE_MEDIA_STREAM
->call
;
141 stream_schedule_cancel_timeout(SIPE_MEDIA_CALL
, stream_private
);
143 sipe_media_stream_set_data(SIPE_MEDIA_STREAM
, NULL
, NULL
);
146 call_private
->streams
= g_slist_remove(call_private
->streams
,
149 if (SIPE_MEDIA_STREAM
->ssrc_range
) {
150 call_private
->ssrc_ranges
=
151 g_slist_remove(call_private
->ssrc_ranges
,
152 SIPE_MEDIA_STREAM
->ssrc_range
);
156 if (SIPE_MEDIA_STREAM
->backend_private
) {
157 sipe_backend_media_stream_free(SIPE_MEDIA_STREAM
->backend_private
);
159 g_free(SIPE_MEDIA_STREAM
->id
);
160 g_free(stream_private
->encryption_key
);
161 g_queue_free_full(stream_private
->write_queue
,
162 (GDestroyNotify
)g_byte_array_unref
);
163 g_queue_free_full(stream_private
->async_reads
, g_free
);
164 sipe_utils_nameval_free(stream_private
->extra_sdp
);
165 g_free(stream_private
);
169 call_private_equals(SIPE_UNUSED_PARAMETER
const gchar
*callid
,
170 struct sipe_media_call_private
*call_private1
,
171 struct sipe_media_call_private
*call_private2
)
173 return call_private1
== call_private2
;
177 sipe_media_call_free(struct sipe_media_call_private
*call_private
)
180 g_hash_table_foreach_remove(call_private
->sipe_private
->media_calls
,
181 (GHRFunc
) call_private_equals
, call_private
);
183 call_schedule_cancel_request_timeout(SIPE_MEDIA_CALL
);
185 while (call_private
->streams
) {
186 sipe_media_stream_free(call_private
->streams
->data
);
189 sipe_backend_media_free(call_private
->public.backend_private
);
191 if (call_private
->session
) {
192 sipe_session_remove(call_private
->sipe_private
,
193 call_private
->session
);
196 if (call_private
->invitation
)
197 sipmsg_free(call_private
->invitation
);
199 // Frees any referenced extra invite data.
200 sipe_media_add_extra_invite_section(SIPE_MEDIA_CALL
, NULL
, NULL
);
202 sipe_utils_slist_free_full(call_private
->ssrc_ranges
, g_free
);
204 sdpmsg_free(call_private
->smsg
);
205 sipe_utils_slist_free_full(call_private
->failed_media
,
206 (GDestroyNotify
)sdpmedia_free
);
207 g_free(SIPE_MEDIA_CALL
->with
);
208 g_free(call_private
);
213 candidate_sort_cb(struct sdpcandidate
*c1
, struct sdpcandidate
*c2
)
215 int cmp
= g_strcmp0(c1
->foundation
, c2
->foundation
);
217 cmp
= g_strcmp0(c1
->username
, c2
->username
);
219 cmp
= c1
->component
- c2
->component
;
226 backend_candidates_to_sdpcandidate(GList
*candidates
)
228 GSList
*result
= NULL
;
231 for (i
= candidates
; i
; i
= i
->next
) {
232 struct sipe_backend_candidate
*candidate
= i
->data
;
233 struct sdpcandidate
*c
;
235 gchar
*ip
= sipe_backend_candidate_get_ip(candidate
);
236 gchar
*base_ip
= sipe_backend_candidate_get_base_ip(candidate
);
237 if (is_empty(ip
) || strchr(ip
, ':') ||
238 (base_ip
&& strchr(base_ip
, ':'))) {
239 /* Ignore IPv6 candidates. */
245 c
= g_new(struct sdpcandidate
, 1);
246 c
->foundation
= sipe_backend_candidate_get_foundation(candidate
);
247 c
->component
= sipe_backend_candidate_get_component_type(candidate
);
248 c
->type
= sipe_backend_candidate_get_type(candidate
);
249 c
->protocol
= sipe_backend_candidate_get_protocol(candidate
);
251 c
->port
= sipe_backend_candidate_get_port(candidate
);
252 c
->base_ip
= base_ip
;
253 c
->base_port
= sipe_backend_candidate_get_base_port(candidate
);
254 c
->priority
= sipe_backend_candidate_get_priority(candidate
);
255 c
->username
= sipe_backend_candidate_get_username(candidate
);
256 c
->password
= sipe_backend_candidate_get_password(candidate
);
258 result
= g_slist_insert_sorted(result
, c
,
259 (GCompareFunc
)candidate_sort_cb
);
266 get_stream_ip_and_ports(GSList
*candidates
,
267 gchar
**ip
, guint
*rtp_port
, guint
*rtcp_port
)
269 guint32 rtp_max_priority
= 0;
270 guint32 rtcp_max_priority
= 0;
276 for (; candidates
; candidates
= candidates
->next
) {
277 struct sdpcandidate
*candidate
= candidates
->data
;
279 if (candidate
->component
== SIPE_COMPONENT_RTP
&&
280 candidate
->priority
> rtp_max_priority
) {
281 rtp_max_priority
= candidate
->priority
;
282 *rtp_port
= candidate
->port
;
285 *ip
= g_strdup(candidate
->ip
);
286 } else if (candidate
->component
== SIPE_COMPONENT_RTCP
&&
287 candidate
->priority
> rtcp_max_priority
) {
288 rtcp_max_priority
= candidate
->priority
;
289 *rtcp_port
= candidate
->port
;
295 sdpcodec_compare(gconstpointer a
, gconstpointer b
)
297 return ((const struct sdpcodec
*)a
)->id
-
298 ((const struct sdpcodec
*)b
)->id
;
302 remove_wrong_farstream_0_1_tcp_candidates(GList
*candidates
)
304 GList
*i
= candidates
;
305 GHashTable
*foundation_to_candidate
= g_hash_table_new_full(g_str_hash
,
311 GList
*next
= i
->next
;
312 struct sipe_backend_candidate
*c1
= i
->data
;
314 if (sipe_backend_candidate_get_protocol(c1
) == SIPE_NETWORK_PROTOCOL_UDP
) {
315 gchar
*foundation
= sipe_backend_candidate_get_foundation(c1
);
316 struct sipe_backend_candidate
*c2
= g_hash_table_lookup(foundation_to_candidate
,
322 if (sipe_backend_candidate_get_port(c1
) ==
323 sipe_backend_candidate_get_port(c2
) ||
324 (sipe_backend_candidate_get_type(c1
) !=
325 SIPE_CANDIDATE_TYPE_HOST
&&
326 sipe_backend_candidate_get_base_port(c1
) ==
327 sipe_backend_candidate_get_base_port(c2
))) {
329 * We assume that RTP+RTCP UDP pairs
330 * that share the same port are
331 * actually mistagged TCP candidates.
333 candidates
= g_list_remove(candidates
, c2
);
334 candidates
= g_list_delete_link(candidates
, i
);
335 sipe_backend_candidate_free(c1
);
336 sipe_backend_candidate_free(c2
);
339 /* hash table takes ownership of "foundation" */
340 g_hash_table_insert(foundation_to_candidate
, foundation
, c1
);
346 g_hash_table_destroy(foundation_to_candidate
);
352 fill_zero_tcp_act_ports_from_tcp_pass(GSList
*candidates
, GSList
*all_candidates
)
355 GHashTable
*ip_to_port
= g_hash_table_new(g_str_hash
, g_str_equal
);
357 for (i
= candidates
; i
; i
= i
->next
) {
358 struct sdpcandidate
*c
= i
->data
;
361 if (c
->protocol
!= SIPE_NETWORK_PROTOCOL_TCP_ACTIVE
) {
365 for (j
= all_candidates
; j
; j
= j
->next
) {
366 struct sdpcandidate
*passive
= j
->data
;
367 if (passive
->protocol
!= SIPE_NETWORK_PROTOCOL_TCP_PASSIVE
||
368 c
->type
!= passive
->type
) {
372 if (sipe_strequal(c
->ip
, passive
->ip
) &&
373 sipe_strequal(c
->base_ip
, passive
->base_ip
)) {
375 c
->port
= passive
->port
;
378 if (c
->base_port
== 0) {
379 c
->base_port
= passive
->base_port
;
386 for (i
= all_candidates
; i
; i
= i
->next
) {
387 struct sdpcandidate
*c
= i
->data
;
389 if (c
->protocol
== SIPE_NETWORK_PROTOCOL_TCP_PASSIVE
&&
390 c
->type
== SIPE_CANDIDATE_TYPE_HOST
) {
391 g_hash_table_insert(ip_to_port
, c
->ip
, &c
->port
);
395 /* Fill base ports of all TCP relay candidates using what we have
396 * collected from host candidates. */
397 for (i
= candidates
; i
; i
= i
->next
) {
398 struct sdpcandidate
*c
= i
->data
;
399 if (c
->type
== SIPE_CANDIDATE_TYPE_RELAY
&& c
->base_port
== 0) {
400 guint
*base_port
= (guint
*)g_hash_table_lookup(ip_to_port
, c
->base_ip
);
402 c
->base_port
= *base_port
;
404 SIPE_DEBUG_WARNING("Couldn't determine base port for candidate "
405 "with foundation %s", c
->foundation
);
410 g_hash_table_destroy(ip_to_port
);
413 static SipeEncryptionPolicy
414 get_encryption_policy(struct sipe_core_private
*sipe_private
)
416 SipeEncryptionPolicy result
=
417 sipe_backend_media_get_encryption_policy(SIPE_CORE_PUBLIC
);
418 if (result
== SIPE_ENCRYPTION_POLICY_OBEY_SERVER
) {
419 result
= sipe_private
->server_av_encryption_policy
;
425 static struct sdpmedia
*
426 media_stream_to_sdpmedia(struct sipe_media_call_private
*call_private
,
427 struct sipe_media_stream_private
*stream_private
)
429 struct sdpmedia
*sdpmedia
= g_new0(struct sdpmedia
, 1);
430 GList
*codecs
= sipe_backend_get_local_codecs(SIPE_MEDIA_CALL
,
432 SipeEncryptionPolicy encryption_policy
=
433 get_encryption_policy(call_private
->sipe_private
);
436 GSList
*attributes
= NULL
;
437 GSList
*sdpcandidates
;
438 GSList
*all_sdpcandidates
;
443 sdpmedia
->name
= g_strdup(SIPE_MEDIA_STREAM
->id
);
445 if (sipe_strequal(sdpmedia
->name
, "audio"))
446 type
= SIPE_MEDIA_AUDIO
;
447 else if (sipe_strequal(sdpmedia
->name
, "video"))
448 type
= SIPE_MEDIA_VIDEO
;
449 else if (sipe_strequal(sdpmedia
->name
, "data"))
450 type
= SIPE_MEDIA_APPLICATION
;
451 else if (sipe_strequal(sdpmedia
->name
, "applicationsharing"))
452 type
= SIPE_MEDIA_APPLICATION
;
454 // TODO: incompatible media, should not happen here
455 g_free(sdpmedia
->name
);
457 sipe_media_codec_list_free(codecs
);
462 for (i
= codecs
; i
; i
= i
->next
) {
463 struct sipe_backend_codec
*codec
= i
->data
;
464 struct sdpcodec
*c
= g_new0(struct sdpcodec
, 1);
467 c
->id
= sipe_backend_codec_get_id(codec
);
468 c
->name
= sipe_backend_codec_get_name(codec
);
469 c
->clock_rate
= sipe_backend_codec_get_clock_rate(codec
);
472 params
= sipe_backend_codec_get_optional_parameters(codec
);
473 for (; params
; params
= params
->next
) {
474 struct sipnameval
*param
= params
->data
;
475 struct sipnameval
*copy
= g_new0(struct sipnameval
, 1);
477 copy
->name
= g_strdup(param
->name
);
478 copy
->value
= g_strdup(param
->value
);
480 c
->parameters
= g_slist_append(c
->parameters
, copy
);
483 /* Buggy(?) codecs may report non-unique id (a.k.a. payload
484 * type) that must not appear in SDP messages we send. Thus,
485 * let's ignore any codec having the same id as one we already
486 * have in the converted list. */
487 if (g_slist_find_custom(sdpmedia
->codecs
, c
, sdpcodec_compare
)) {
490 sdpmedia
->codecs
= g_slist_append(sdpmedia
->codecs
, c
);
494 sipe_media_codec_list_free(codecs
);
496 // Process local candidates
497 // If we have established candidate pairs, send them in SDP response.
498 // Otherwise send all available local candidates.
499 candidates
= sipe_backend_media_stream_get_active_local_candidates(SIPE_MEDIA_STREAM
);
500 sdpcandidates
= backend_candidates_to_sdpcandidate(candidates
);
501 sipe_media_candidate_list_free(candidates
);
503 candidates
= sipe_backend_get_local_candidates(SIPE_MEDIA_CALL
,
505 candidates
= remove_wrong_farstream_0_1_tcp_candidates(candidates
);
506 all_sdpcandidates
= backend_candidates_to_sdpcandidate(candidates
);
507 sipe_media_candidate_list_free(candidates
);
509 if (!sdpcandidates
) {
510 sdpcandidates
= all_sdpcandidates
;
513 fill_zero_tcp_act_ports_from_tcp_pass(sdpcandidates
, all_sdpcandidates
);
515 sdpmedia
->candidates
= sdpcandidates
;
517 if (all_sdpcandidates
!= sdpcandidates
) {
518 sipe_utils_slist_free_full(all_sdpcandidates
,
519 (GDestroyNotify
)sdpcandidate_free
);
522 get_stream_ip_and_ports(sdpmedia
->candidates
, &sdpmedia
->ip
,
523 &sdpmedia
->port
, &rtcp_port
);
525 if (sipe_backend_stream_is_held(SIPE_MEDIA_STREAM
))
526 attributes
= sipe_utils_nameval_add(attributes
, "inactive", "");
529 gchar
*tmp
= g_strdup_printf("%u", rtcp_port
);
530 attributes
= sipe_utils_nameval_add(attributes
, "rtcp", tmp
);
534 if (encryption_policy
!= call_private
->sipe_private
->server_av_encryption_policy
) {
535 const gchar
*encryption
= NULL
;
536 switch (encryption_policy
) {
537 case SIPE_ENCRYPTION_POLICY_REJECTED
:
538 encryption
= "rejected";
540 case SIPE_ENCRYPTION_POLICY_OPTIONAL
:
541 encryption
= "optional";
543 case SIPE_ENCRYPTION_POLICY_REQUIRED
:
545 encryption
= "required";
549 attributes
= sipe_utils_nameval_add(attributes
, "encryption", encryption
);
552 if (SIPE_MEDIA_STREAM
->ssrc_range
) {
555 tmp
= g_strdup_printf("%u-%u",
556 SIPE_MEDIA_STREAM
->ssrc_range
->begin
,
557 SIPE_MEDIA_STREAM
->ssrc_range
->end
);
558 attributes
= sipe_utils_nameval_add(attributes
,
559 "x-ssrc-range", tmp
);
563 // Process remote candidates
564 candidates
= sipe_backend_media_stream_get_active_remote_candidates(SIPE_MEDIA_STREAM
);
565 sdpmedia
->remote_candidates
= backend_candidates_to_sdpcandidate(candidates
);
566 sipe_media_candidate_list_free(candidates
);
568 sdpmedia
->encryption_active
= stream_private
->encryption_key
&&
569 call_private
->encryption_compatible
&&
570 stream_private
->remote_candidates_and_codecs_set
&&
571 encryption_policy
!= SIPE_ENCRYPTION_POLICY_REJECTED
;
573 // Set our key if encryption is enabled.
574 if (stream_private
->encryption_key
&&
575 encryption_policy
!= SIPE_ENCRYPTION_POLICY_REJECTED
) {
576 sdpmedia
->encryption_key
= g_memdup(stream_private
->encryption_key
,
578 sdpmedia
->encryption_key_id
= stream_private
->encryption_key_id
;
581 // Append extra attributes assigned to the stream.
582 for (j
= stream_private
->extra_sdp
; j
; j
= g_slist_next(j
)) {
583 struct sipnameval
*attr
= j
->data
;
584 attributes
= sipe_utils_nameval_add(attributes
,
585 attr
->name
, attr
->value
);
588 sdpmedia
->attributes
= attributes
;
593 static struct sdpmsg
*
594 sipe_media_to_sdpmsg(struct sipe_media_call_private
*call_private
)
596 struct sdpmsg
*msg
= g_new0(struct sdpmsg
, 1);
597 GSList
*streams
= call_private
->streams
;
599 for (; streams
; streams
= streams
->next
) {
600 struct sdpmedia
*media
= media_stream_to_sdpmedia(call_private
,
603 msg
->media
= g_slist_append(msg
->media
, media
);
606 msg
->ip
= g_strdup(media
->ip
);
610 msg
->media
= g_slist_concat(msg
->media
, call_private
->failed_media
);
611 call_private
->failed_media
= NULL
;
613 msg
->ice_version
= call_private
->ice_version
;
619 sipe_invite_call(struct sipe_media_call_private
*call_private
, TransCallback tc
)
621 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
624 gchar
*p_preferred_identity
= NULL
;
626 struct sip_dialog
*dialog
;
629 dialog
= sipe_media_get_sip_dialog(SIPE_MEDIA_CALL
);
631 contact
= get_contact(sipe_private
);
633 if (sipe_private
->uc_line_uri
) {
634 gchar
*self
= sip_uri_self(sipe_private
);
635 p_preferred_identity
= g_strdup_printf(
636 "P-Preferred-Identity: <%s>, <%s>\r\n",
637 self
, sipe_private
->uc_line_uri
);
641 hdr
= g_strdup_printf(
642 "ms-keep-alive: UAC;hop-hop=yes\r\n"
645 "Content-Type: %s%s\r\n",
647 p_preferred_identity
? p_preferred_identity
: "",
648 call_private
->invite_content_type
?
649 call_private
->invite_content_type
: "application/sdp",
650 call_private
->invite_content_type
?
651 ";boundary=\"----=_NextPart_000_001E_01CB4397.0B5EB570\"" : "");
654 g_free(p_preferred_identity
);
656 msg
= sipe_media_to_sdpmsg(call_private
);
657 body
= sdpmsg_to_string(msg
);
659 if (call_private
->extra_invite_section
) {
661 tmp
= g_strdup_printf(
662 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
665 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
666 "Content-Type: application/sdp\r\n"
667 "Content-Transfer-Encoding: 7bit\r\n"
668 "Content-Disposition: session; handling=optional\r\n"
672 "------=_NextPart_000_001E_01CB4397.0B5EB570--\r\n",
673 call_private
->extra_invite_section
, body
);
676 sipe_media_add_extra_invite_section(SIPE_MEDIA_CALL
, NULL
, NULL
);
681 dialog
->outgoing_invite
= sip_transport_invite(sipe_private
,
692 send_response_with_session_description(struct sipe_media_call_private
*call_private
, int code
, gchar
*text
)
694 struct sdpmsg
*msg
= sipe_media_to_sdpmsg(call_private
);
695 gchar
*body
= sdpmsg_to_string(msg
);
697 sipmsg_add_header(call_private
->invitation
, "Content-Type", "application/sdp");
698 sip_transport_response(call_private
->sipe_private
, call_private
->invitation
, code
, text
, body
);
703 process_invite_call_response(struct sipe_core_private
*sipe_private
,
705 struct transaction
*trans
);
707 struct sipe_media_stream
*
708 sipe_core_media_get_stream_by_id(struct sipe_media_call
*call
, const gchar
*id
)
711 for (i
= SIPE_MEDIA_CALL_PRIVATE
->streams
; i
; i
= i
->next
) {
712 struct sipe_media_stream
*stream
= i
->data
;
713 if (sipe_strequal(stream
->id
, id
))
720 update_call_from_remote_sdp(struct sipe_media_call_private
* call_private
,
721 struct sdpmedia
*media
)
723 struct sipe_media_stream
*stream
;
724 GList
*backend_candidates
= NULL
;
725 GList
*backend_codecs
= NULL
;
727 gboolean result
= TRUE
;
729 stream
= sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL
, media
->name
);
730 if (media
->port
== 0) {
732 sipe_backend_media_stream_end(SIPE_MEDIA_CALL
, stream
);
740 if (sipe_utils_nameval_find(media
->attributes
, "inactive")) {
741 sipe_backend_stream_hold(SIPE_MEDIA_CALL
, stream
, FALSE
);
742 } else if (sipe_backend_stream_is_held(stream
)) {
743 sipe_backend_stream_unhold(SIPE_MEDIA_CALL
, stream
, FALSE
);
746 if (SIPE_MEDIA_STREAM_PRIVATE
->remote_candidates_and_codecs_set
) {
750 for (i
= media
->codecs
; i
; i
= i
->next
) {
751 struct sdpcodec
*c
= i
->data
;
752 struct sipe_backend_codec
*codec
;
755 codec
= sipe_backend_codec_new(c
->id
,
761 for (j
= c
->parameters
; j
; j
= j
->next
) {
762 struct sipnameval
*attr
= j
->data
;
764 sipe_backend_codec_add_optional_parameter(codec
,
769 backend_codecs
= g_list_append(backend_codecs
, codec
);
772 if (media
->encryption_key
&& SIPE_MEDIA_STREAM_PRIVATE
->encryption_key
) {
773 sipe_backend_media_set_encryption_keys(SIPE_MEDIA_CALL
, stream
,
774 SIPE_MEDIA_STREAM_PRIVATE
->encryption_key
,
775 media
->encryption_key
);
776 SIPE_MEDIA_STREAM_PRIVATE
->encryption_key_id
= media
->encryption_key_id
;
779 result
= sipe_backend_set_remote_codecs(SIPE_MEDIA_CALL
, stream
,
781 sipe_media_codec_list_free(backend_codecs
);
783 if (result
== FALSE
) {
784 sipe_backend_media_stream_end(SIPE_MEDIA_CALL
, stream
);
788 for (i
= media
->candidates
; i
; i
= i
->next
) {
789 struct sdpcandidate
*c
= i
->data
;
790 struct sipe_backend_candidate
*candidate
;
791 candidate
= sipe_backend_candidate_new(c
->foundation
,
799 sipe_backend_candidate_set_priority(candidate
, c
->priority
);
801 backend_candidates
= g_list_append(backend_candidates
, candidate
);
804 sipe_backend_media_add_remote_candidates(SIPE_MEDIA_CALL
, stream
,
806 sipe_media_candidate_list_free(backend_candidates
);
808 SIPE_MEDIA_STREAM_PRIVATE
->remote_candidates_and_codecs_set
= TRUE
;
814 apply_remote_message(struct sipe_media_call_private
* call_private
,
819 sipe_utils_slist_free_full(call_private
->failed_media
, (GDestroyNotify
)sdpmedia_free
);
820 call_private
->failed_media
= NULL
;
821 call_private
->encryption_compatible
= TRUE
;
823 for (i
= msg
->media
; i
; i
= i
->next
) {
824 struct sdpmedia
*media
= i
->data
;
825 const gchar
*enc_level
=
826 sipe_utils_nameval_find(media
->attributes
, "encryption");
827 if (sipe_strequal(enc_level
, "rejected") &&
828 get_encryption_policy(call_private
->sipe_private
) == SIPE_ENCRYPTION_POLICY_REQUIRED
) {
829 call_private
->encryption_compatible
= FALSE
;
832 if (!update_call_from_remote_sdp(call_private
, media
)) {
834 call_private
->failed_media
=
835 g_slist_append(call_private
->failed_media
, media
);
839 /* We need to keep failed medias until response is sent, remove them
840 * from sdpmsg that is to be freed. */
841 for (i
= call_private
->failed_media
; i
; i
= i
->next
) {
842 msg
->media
= g_slist_remove(msg
->media
, i
->data
);
847 call_initialized(struct sipe_media_call
*call
)
849 GSList
*streams
= SIPE_MEDIA_CALL_PRIVATE
->streams
;
850 for (; streams
; streams
= streams
->next
) {
851 if (!sipe_backend_stream_initialized(call
, streams
->data
)) {
860 stream_connection_timeout_cb(struct sipe_core_private
*sipe_private
,
863 struct sipe_media_call_private
*call_private
= data
;
865 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
866 _("Couldn't create stream"),
867 _("Connection timed out"));
868 sipe_backend_media_hangup(SIPE_MEDIA_CALL
->backend_private
, TRUE
);
872 stream_schedule_timeout(struct sipe_media_call
*call
)
875 for (i
= SIPE_MEDIA_CALL_PRIVATE
->streams
; i
; i
= i
->next
) {
876 struct sipe_media_stream_private
*stream_private
= i
->data
;
878 stream_private
->timeout_key
=
879 g_strdup_printf("<media-stream-connect><%s><%s>",
880 sipe_media_get_sip_dialog(call
)->callid
,
881 SIPE_MEDIA_STREAM
->id
);
883 sipe_schedule_seconds(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
,
884 stream_private
->timeout_key
,
885 SIPE_MEDIA_CALL_PRIVATE
,
886 SIPE_MEDIA_STREAM_CONNECTION_TIMEOUT_SECONDS
,
887 stream_connection_timeout_cb
,
893 stream_schedule_cancel_timeout(struct sipe_media_call
*call
,
894 struct sipe_media_stream_private
*stream_private
)
896 if (stream_private
->timeout_key
) {
897 sipe_schedule_cancel(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
,
898 stream_private
->timeout_key
);
899 g_free(stream_private
->timeout_key
);
901 stream_private
->timeout_key
= NULL
;
905 call_request_timeout_cb(struct sipe_core_private
*sipe_private
,
908 struct sipe_media_call_private
*call_private
= data
;
910 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
911 _("Request timed out"),
912 _("Call could not be answered"));
913 sipe_backend_media_hangup(SIPE_MEDIA_CALL
->backend_private
, TRUE
);
917 call_schedule_request_timeout(struct sipe_media_call
*call
)
919 SIPE_MEDIA_CALL_PRIVATE
->timeout_key
=
920 g_strdup_printf("<media-call-request><%s>", sipe_media_get_sip_dialog(call
)->callid
);
922 sipe_schedule_seconds(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
,
923 SIPE_MEDIA_CALL_PRIVATE
->timeout_key
,
924 SIPE_MEDIA_CALL_PRIVATE
,
925 SIPE_MEDIA_CALL_TIMEOUT_SECONDS
,
926 call_request_timeout_cb
,
931 call_schedule_cancel_request_timeout(struct sipe_media_call
*call
)
933 if (SIPE_MEDIA_CALL_PRIVATE
->timeout_key
) {
934 sipe_schedule_cancel(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
,
935 SIPE_MEDIA_CALL_PRIVATE
->timeout_key
);
936 g_free(SIPE_MEDIA_CALL_PRIVATE
->timeout_key
);
938 SIPE_MEDIA_CALL_PRIVATE
->timeout_key
= NULL
;
942 // Sends an invite response when the call is accepted and local candidates were
943 // prepared, otherwise does nothing. If error response is sent, call_private is
944 // disposed before function returns.
946 maybe_send_first_invite_response(struct sipe_media_call_private
*call_private
)
948 struct sipe_backend_media
*backend_media
;
950 backend_media
= call_private
->public.backend_private
;
952 if (!sipe_backend_media_accepted(backend_media
) ||
953 !call_initialized(&call_private
->public))
956 if (!call_private
->encryption_compatible
) {
957 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
959 sipmsg_add_header(call_private
->invitation
, "Warning",
960 "308 lcs.microsoft.com \"Encryption Levels not compatible\"");
961 sip_transport_response(sipe_private
,
962 call_private
->invitation
,
963 488, "Encryption Levels not compatible",
965 sipe_backend_media_reject(backend_media
, FALSE
);
966 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
967 _("Unable to establish a call"),
968 _("Encryption settings of peer are incompatible with ours."));
970 send_response_with_session_description(call_private
, 200, "OK");
971 stream_schedule_timeout(SIPE_MEDIA_CALL
);
972 sipmsg_free(call_private
->invitation
);
973 call_private
->invitation
= NULL
;
978 stream_initialized_cb(struct sipe_media_call
*call
,
979 struct sipe_media_stream
*stream
)
981 if (call_initialized(call
)) {
982 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
984 if (sipe_backend_media_is_initiator(call
, stream
)) {
985 sipe_invite_call(call_private
,
986 process_invite_call_response
);
987 } else if (call_private
->smsg
) {
988 struct sdpmsg
*smsg
= call_private
->smsg
;
989 call_private
->smsg
= NULL
;
991 apply_remote_message(call_private
, smsg
);
992 maybe_send_first_invite_response(call_private
);
998 static void phone_state_publish(struct sipe_core_private
*sipe_private
)
1000 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
1001 sipe_ocs2007_phone_state_publish(sipe_private
);
1003 // TODO: OCS 2005 support. Is anyone still using it at all?
1008 sipe_core_media_stream_end(struct sipe_media_stream
*stream
)
1010 sipe_media_stream_free(SIPE_MEDIA_STREAM_PRIVATE
);
1014 media_end_cb(struct sipe_media_call
*call
)
1016 struct sipe_core_private
*sipe_private
;
1018 g_return_if_fail(call
);
1020 sipe_private
= SIPE_MEDIA_CALL_PRIVATE
->sipe_private
;
1022 sipe_media_call_free(SIPE_MEDIA_CALL_PRIVATE
);
1023 phone_state_publish(sipe_private
);
1027 call_accept_cb(struct sipe_media_call
*call
, gboolean local
)
1030 maybe_send_first_invite_response(SIPE_MEDIA_CALL_PRIVATE
);
1032 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
);
1036 call_reject_cb(struct sipe_media_call
*call
, gboolean local
)
1039 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
1041 sip_transport_response(call_private
->sipe_private
,
1042 call_private
->invitation
,
1043 603, "Decline", NULL
);
1045 if (call_private
->session
) {
1046 sipe_session_remove(call_private
->sipe_private
,
1047 call_private
->session
);
1048 call_private
->session
= NULL
;
1054 av_call_reject_cb(struct sipe_media_call
*call
, gboolean local
)
1057 struct sipe_core_private
*sipe_private
;
1060 sipe_private
= SIPE_MEDIA_CALL_PRIVATE
->sipe_private
;
1062 desc
= g_strdup_printf(_("User %s rejected call"), call
->with
);
1063 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, _("Call rejected"),
1068 call_reject_cb(call
, local
);
1072 sipe_media_send_ack(struct sipe_core_private
*sipe_private
, struct sipmsg
*msg
,
1073 struct transaction
*trans
);
1075 static void call_hold_cb(struct sipe_media_call
*call
,
1077 SIPE_UNUSED_PARAMETER gboolean state
)
1080 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE
, sipe_media_send_ack
);
1084 static void call_hangup_cb(struct sipe_media_call
*call
, gboolean local
)
1087 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
1089 if (call_private
->session
) {
1090 sipe_session_close(call_private
->sipe_private
,
1091 call_private
->session
);
1092 call_private
->session
= NULL
;
1098 error_cb(struct sipe_media_call
*call
, gchar
*message
)
1100 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
1101 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
1102 gboolean initiator
= sipe_backend_media_is_initiator(call
, NULL
);
1103 gboolean accepted
= sipe_backend_media_accepted(call
->backend_private
);
1105 gchar
*title
= g_strdup_printf("Call with %s failed", call
->with
);
1106 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, title
, message
);
1109 if (!initiator
&& !accepted
) {
1110 sip_transport_response(sipe_private
,
1111 call_private
->invitation
,
1112 488, "Not Acceptable Here", NULL
);
1116 struct sipe_media_call
*
1117 sipe_media_call_new(struct sipe_core_private
*sipe_private
, const gchar
* with
,
1118 struct sipmsg
*msg
, SipeIceVersion ice_version
,
1119 SipeMediaCallFlags flags
)
1121 struct sipe_media_call_private
*call_private
;
1122 struct sip_session
*session
;
1123 struct sip_dialog
*dialog
;
1126 session
= sipe_session_add_call(sipe_private
, with
);
1128 dialog
= sipe_dialog_add(session
);
1129 dialog
->with
= g_strdup(with
);
1132 gchar
*newTag
= gentag();
1133 const gchar
*oldHeader
;
1136 oldHeader
= sipmsg_find_header(msg
, "To");
1137 newHeader
= g_strdup_printf("%s;tag=%s", oldHeader
, newTag
);
1138 sipmsg_remove_header_now(msg
, "To");
1139 sipmsg_add_header_now(msg
, "To", newHeader
);
1143 dialog
->callid
= g_strdup(sipmsg_find_header(msg
, "Call-ID"));
1144 sipe_dialog_parse(dialog
, msg
, FALSE
);
1146 dialog
->callid
= gencallid();
1147 dialog
->ourtag
= gentag();
1148 flags
|= SIPE_MEDIA_CALL_INITIATOR
;
1151 if (g_hash_table_lookup(sipe_private
->media_calls
, dialog
->callid
)) {
1152 SIPE_DEBUG_ERROR("sipe_media_call_new: call already exists for "
1153 "Call-ID %s", dialog
->callid
);
1154 sipe_session_remove(sipe_private
, session
);
1158 call_private
= g_new0(struct sipe_media_call_private
, 1);
1159 call_private
->sipe_private
= sipe_private
;
1160 call_private
->session
= session
;
1161 SIPE_MEDIA_CALL
->with
= g_strdup(with
);
1163 g_hash_table_insert(sipe_private
->media_calls
,
1164 g_strdup(dialog
->callid
), call_private
);
1166 cname
= g_strdup(sipe_private
->contact
+ 1);
1167 cname
[strlen(cname
) - 1] = '\0';
1169 call_private
->public.backend_private
= sipe_backend_media_new(SIPE_CORE_PUBLIC
,
1173 sipe_backend_media_set_cname(call_private
->public.backend_private
, cname
);
1175 call_private
->ice_version
= ice_version
;
1176 call_private
->encryption_compatible
= TRUE
;
1178 call_private
->public.stream_initialized_cb
= stream_initialized_cb
;
1179 call_private
->public.media_end_cb
= media_end_cb
;
1180 call_private
->public.call_accept_cb
= call_accept_cb
;
1181 call_private
->public.call_reject_cb
= call_reject_cb
;
1182 call_private
->public.call_hold_cb
= call_hold_cb
;
1183 call_private
->public.call_hangup_cb
= call_hangup_cb
;
1184 call_private
->public.error_cb
= error_cb
;
1188 return SIPE_MEDIA_CALL
;
1191 void sipe_media_hangup(struct sipe_media_call_private
*call_private
)
1194 sipe_backend_media_hangup(call_private
->public.backend_private
,
1200 ssrc_range_compare(const struct ssrc_range
*a
, const struct ssrc_range
*b
)
1202 if (a
->begin
< b
->begin
) {
1205 if (a
->begin
> b
->begin
) {
1212 ssrc_range_update(GSList
**ranges
, GSList
*media
)
1214 for (; media
; media
= media
->next
) {
1216 const char *ssrc_range
;
1220 ssrc_range
= sipe_utils_nameval_find(m
->attributes
,
1226 parts
= g_strsplit(ssrc_range
, "-", 2);
1228 if (parts
[0] && parts
[1]) {
1229 struct ssrc_range
*range
;
1231 range
= g_new0(struct ssrc_range
, 1);
1232 range
->begin
= atoi(parts
[0]);
1233 range
->end
= atoi(parts
[1]);
1235 *ranges
= sipe_utils_slist_insert_unique_sorted(
1237 (GCompareFunc
)ssrc_range_compare
,
1245 static struct ssrc_range
*
1246 ssrc_range_allocate(GSList
**ranges
, guint32 len
)
1248 struct ssrc_range
*range
;
1251 range
= g_new0(struct ssrc_range
, 1);
1253 range
->end
= range
->begin
+ (len
- 1);
1255 for (i
= *ranges
; i
; i
= i
->next
) {
1256 struct ssrc_range
*r
= i
->data
;
1258 if (range
->begin
< r
->begin
&& range
->end
< r
->begin
) {
1262 range
->begin
= r
->end
+ 1;
1263 range
->end
= range
->begin
+ (len
- 1);
1266 /* As per [MS-SDPEXT] 3.1.5.31.1, a SSRC MUST be from 1 to 4294967040
1268 if (range
->begin
> range
->end
|| range
->end
> 0xFFFFFF00) {
1270 SIPE_DEBUG_ERROR("Couldn't allocate SSRC range of %u", len
);
1274 *ranges
= g_slist_insert_sorted(*ranges
, range
,
1275 (GCompareFunc
)ssrc_range_compare
);
1280 struct sipe_media_stream
*
1281 sipe_media_stream_add(struct sipe_media_call
*call
, const gchar
*id
,
1282 SipeMediaType type
, SipeIceVersion ice_version
,
1283 gboolean initiator
, guint32 ssrc_count
)
1285 struct sipe_core_private
*sipe_private
;
1286 struct sipe_media_stream_private
*stream_private
;
1287 struct sipe_backend_media_relays
*backend_media_relays
;
1291 sipe_private
= SIPE_MEDIA_CALL_PRIVATE
->sipe_private
;
1293 backend_media_relays
= sipe_backend_media_relays_convert(
1294 sipe_private
->media_relays
,
1295 sipe_private
->media_relay_username
,
1296 sipe_private
->media_relay_password
);
1298 min_port
= sipe_private
->min_media_port
;
1299 max_port
= sipe_private
->max_media_port
;
1301 case SIPE_MEDIA_AUDIO
:
1302 min_port
= sipe_private
->min_audio_port
;
1303 max_port
= sipe_private
->max_audio_port
;
1305 case SIPE_MEDIA_VIDEO
:
1306 min_port
= sipe_private
->min_video_port
;
1307 max_port
= sipe_private
->max_audio_port
;
1309 case SIPE_MEDIA_APPLICATION
:
1310 if (sipe_strequal(id
, "data")) {
1311 min_port
= sipe_private
->min_filetransfer_port
;
1312 max_port
= sipe_private
->max_filetransfer_port
;
1313 } else if (sipe_strequal(id
, "applicationsharing")) {
1314 min_port
= sipe_private
->min_appsharing_port
;
1315 max_port
= sipe_private
->max_appsharing_port
;
1320 stream_private
= g_new0(struct sipe_media_stream_private
, 1);
1321 SIPE_MEDIA_STREAM
->call
= call
;
1322 SIPE_MEDIA_STREAM
->id
= g_strdup(id
);
1323 stream_private
->write_queue
= g_queue_new();
1324 stream_private
->async_reads
= g_queue_new();
1326 if (ssrc_count
> 0) {
1327 SIPE_MEDIA_STREAM
->ssrc_range
=
1328 ssrc_range_allocate(&SIPE_MEDIA_CALL_PRIVATE
->ssrc_ranges
,
1332 SIPE_MEDIA_STREAM
->backend_private
=
1333 sipe_backend_media_add_stream(SIPE_MEDIA_STREAM
,
1336 backend_media_relays
,
1337 min_port
, max_port
);
1339 sipe_backend_media_relays_free(backend_media_relays
);
1341 if (!SIPE_MEDIA_STREAM
->backend_private
) {
1342 sipe_media_stream_free(stream_private
);
1346 if (type
== SIPE_MEDIA_VIDEO
) {
1347 /* Declare that we can send and receive Video Source Requests
1348 * as per [MS-SDPEXT] 3.1.5.30.2. */
1349 sipe_media_stream_add_extra_attribute(SIPE_MEDIA_STREAM
,
1350 "rtcp-fb", "* x-message app send:src recv:src");
1352 sipe_media_stream_add_extra_attribute(SIPE_MEDIA_STREAM
,
1353 "rtcp-rsize", NULL
);
1354 sipe_media_stream_add_extra_attribute(SIPE_MEDIA_STREAM
,
1355 "label", "main-video");
1356 sipe_media_stream_add_extra_attribute(SIPE_MEDIA_STREAM
,
1357 "x-source", "main-video");
1361 if (get_encryption_policy(sipe_private
) != SIPE_ENCRYPTION_POLICY_REJECTED
) {
1363 stream_private
->encryption_key
= g_new0(guchar
, SIPE_SRTP_KEY_LEN
);
1364 for (i
= 0; i
!= SIPE_SRTP_KEY_LEN
; ++i
) {
1365 stream_private
->encryption_key
[i
] = rand() & 0xff;
1367 stream_private
->encryption_key_id
= 1;
1371 SIPE_MEDIA_CALL_PRIVATE
->streams
=
1372 g_slist_append(SIPE_MEDIA_CALL_PRIVATE
->streams
,
1375 return SIPE_MEDIA_STREAM
;
1379 append_2007_fallback_if_needed(struct sipe_media_call_private
*call_private
)
1381 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
1382 const gchar
*marker
= sip_transport_sdp_address_marker(sipe_private
);
1383 const gchar
*ip
= sip_transport_ip_address(sipe_private
);
1386 if (SIPE_CORE_PRIVATE_FLAG_IS(SFB
) ||
1387 sipe_media_get_sip_dialog(SIPE_MEDIA_CALL
)->cseq
!= 0 ||
1388 call_private
->ice_version
!= SIPE_ICE_RFC_5245
||
1389 sipe_strequal(SIPE_MEDIA_CALL
->with
, sipe_private
->test_call_bot_uri
)) {
1393 body
= g_strdup_printf("Content-Type: application/sdp\r\n"
1394 "Content-Transfer-Encoding: 7bit\r\n"
1395 "Content-Disposition: session; handling=optional; ms-proxy-2007fallback\r\n"
1397 "o=- 0 0 IN %s %s\r\n"
1400 "m=audio 0 RTP/AVP\r\n",
1403 sipe_media_add_extra_invite_section(SIPE_MEDIA_CALL
,
1404 "multipart/alternative", body
);
1408 sipe_media_initiate_call(struct sipe_core_private
*sipe_private
,
1409 const char *with
, SipeIceVersion ice_version
,
1410 gboolean with_video
)
1412 struct sipe_media_call_private
*call_private
;
1414 if (sipe_core_media_get_call(SIPE_CORE_PUBLIC
)) {
1418 call_private
= (struct sipe_media_call_private
*)
1419 sipe_media_call_new(sipe_private
, with
, NULL
,
1422 SIPE_MEDIA_CALL
->call_reject_cb
= av_call_reject_cb
;
1424 if (!sipe_media_stream_add(SIPE_MEDIA_CALL
, "audio", SIPE_MEDIA_AUDIO
,
1425 call_private
->ice_version
,
1427 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
1428 _("Error occurred"),
1429 _("Error creating audio stream"));
1430 sipe_media_hangup(call_private
);
1435 !sipe_media_stream_add(SIPE_MEDIA_CALL
, "video", SIPE_MEDIA_VIDEO
,
1436 call_private
->ice_version
,
1437 TRUE
, VIDEO_SSRC_COUNT
)) {
1438 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
1439 _("Error occurred"),
1440 _("Error creating video stream"));
1441 sipe_media_hangup(call_private
);
1445 append_2007_fallback_if_needed(call_private
);
1447 call_schedule_request_timeout(SIPE_MEDIA_CALL
);
1448 // Processing continues in stream_initialized_cb
1452 sipe_core_media_initiate_call(struct sipe_core_public
*sipe_public
,
1454 gboolean with_video
)
1456 sipe_media_initiate_call(SIPE_CORE_PRIVATE
, with
,
1457 SIPE_ICE_RFC_5245
, with_video
);
1461 conference_audio_muted_cb(struct sipe_media_stream
*stream
, gboolean is_muted
)
1463 struct sipe_media_call
*call
= stream
->call
;
1465 if (!SIPE_MEDIA_CALL_PRIVATE
->conference_session
) {
1469 sipe_conf_announce_audio_mute_state(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
,
1470 SIPE_MEDIA_CALL_PRIVATE
->conference_session
,
1474 void sipe_core_media_connect_conference(struct sipe_core_public
*sipe_public
,
1475 struct sipe_chat_session
*chat_session
)
1477 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1478 struct sipe_media_call_private
*call_private
;
1479 struct sipe_media_stream
*stream
;
1480 struct sip_session
*session
;
1481 SipeIceVersion ice_version
;
1484 if (!sipe_conf_supports_mcu_type(sipe_private
, "audio-video")) {
1485 sipe_backend_notify_error(sipe_public
, _("Join conference call"),
1486 _("Conference calls are not supported on this server."));
1490 session
= sipe_session_find_chat(sipe_private
, chat_session
);
1492 if (sipe_core_media_get_call(sipe_public
) || !session
) {
1496 av_uri
= sipe_conf_build_uri(sipe_core_chat_id(sipe_public
, chat_session
),
1502 session
->is_call
= TRUE
;
1504 ice_version
= SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013
) ? SIPE_ICE_RFC_5245
:
1507 call_private
= (struct sipe_media_call_private
*)
1508 sipe_media_call_new(sipe_private
, av_uri
, NULL
,
1510 call_private
->conference_session
= session
;
1511 SIPE_MEDIA_CALL
->call_reject_cb
= av_call_reject_cb
;
1513 stream
= sipe_media_stream_add(SIPE_MEDIA_CALL
, "audio",
1515 call_private
->ice_version
,
1518 sipe_backend_notify_error(sipe_public
,
1519 _("Error occurred"),
1520 _("Error creating audio stream"));
1522 sipe_media_hangup(call_private
);
1525 stream
->mute_cb
= conference_audio_muted_cb
;
1529 // Processing continues in stream_initialized_cb
1532 struct sipe_media_call
*
1533 sipe_core_media_get_call(struct sipe_core_public
*sipe_public
)
1535 struct sipe_media_call
* result
= NULL
;
1536 GList
*calls
= g_hash_table_get_values(SIPE_CORE_PRIVATE
->media_calls
);
1537 GList
*entry
= calls
;
1540 if (sipe_core_media_get_stream_by_id(entry
->data
, "audio")) {
1541 result
= entry
->data
;
1544 entry
= entry
->next
;
1551 static gboolean
phone_number_is_valid(const gchar
*phone_number
)
1553 if (!phone_number
|| sipe_strequal(phone_number
, "")) {
1557 if (*phone_number
== '+') {
1561 while (*phone_number
!= '\0') {
1562 if (!g_ascii_isdigit(*phone_number
)) {
1571 void sipe_core_media_phone_call(struct sipe_core_public
*sipe_public
,
1572 const gchar
*phone_number
)
1574 g_return_if_fail(sipe_public
);
1576 SIPE_DEBUG_INFO("sipe_core_media_phone_call: %s", phone_number
? phone_number
: "(null)");
1578 if (phone_number_is_valid(phone_number
)) {
1579 gchar
*phone_uri
= g_strdup_printf("sip:%s@%s;user=phone",
1580 phone_number
, sipe_public
->sip_domain
);
1582 sipe_core_media_initiate_call(sipe_public
, phone_uri
, FALSE
);
1586 sipe_backend_notify_error(sipe_public
,
1587 _("Unable to establish a call"),
1588 _("Invalid phone number"));
1592 void sipe_core_media_test_call(struct sipe_core_public
*sipe_public
)
1594 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1595 if (!sipe_private
->test_call_bot_uri
) {
1596 sipe_backend_notify_error(sipe_public
,
1597 _("Unable to establish a call"),
1598 _("Audio Test Service is not available."));
1602 sipe_core_media_initiate_call(sipe_public
,
1603 sipe_private
->test_call_bot_uri
, FALSE
);
1606 static struct sipe_media_call_private
*
1607 sipe_media_from_sipmsg(struct sipe_core_private
*sipe_private
,
1610 return g_hash_table_lookup(sipe_private
->media_calls
,
1611 sipmsg_find_header(msg
, "Call-ID"));
1615 transport_response_unsupported_sdp(struct sipe_core_private
*sipe_private
,
1618 sipmsg_add_header(msg
, "ms-client-diagnostics",
1619 "52063;reason=\"Unsupported session description\"");
1620 sip_transport_response(sipe_private
, msg
,
1621 488, "Not Acceptable Here", NULL
);
1625 maybe_send_second_invite_response(struct sipe_media_call_private
*call_private
)
1629 /* Second INVITE request had to be received and all streams must have
1630 * established candidate pairs before the response can be sent. */
1632 if (!call_private
->invitation
) {
1636 for (it
= call_private
->streams
; it
; it
= it
->next
) {
1637 struct sipe_media_stream_private
*stream_private
= it
->data
;
1638 if (!stream_private
->established
) {
1643 send_response_with_session_description(call_private
, 200, "OK");
1646 for (it
= call_private
->streams
; it
; it
= it
->next
) {
1647 struct sipe_media_stream_private
*stream_private
= it
->data
;
1649 stream_private
->sdp_negotiation_concluded
= TRUE
;
1650 if (stream_private
->writable
) {
1651 // We've become writable.
1652 sipe_core_media_stream_writable(SIPE_MEDIA_STREAM
, TRUE
);
1658 struct sipe_media_call
*
1659 process_incoming_invite_call(struct sipe_core_private
*sipe_private
,
1663 return(process_incoming_invite_call_parsed_sdp(sipe_private
,
1665 sdpmsg_parse_msg(sdp
)));
1668 struct sipe_media_call
*
1669 process_incoming_invite_call_parsed_sdp(struct sipe_core_private
*sipe_private
,
1671 struct sdpmsg
*smsg
)
1673 struct sipe_media_call_private
*call_private
;
1674 gboolean has_new_media
= FALSE
;
1677 // Don't allow two voice calls in parallel.
1678 if (!strstr(msg
->body
, "m=data") &&
1679 !strstr(msg
->body
, "m=applicationsharing")) {
1680 struct sipe_media_call
*call
=
1681 sipe_core_media_get_call(SIPE_CORE_PUBLIC
);
1682 if (call
&& !is_media_session_msg(SIPE_MEDIA_CALL_PRIVATE
, msg
)) {
1683 sip_transport_response(sipe_private
, msg
,
1684 486, "Busy Here", NULL
);
1690 call_private
= sipe_media_from_sipmsg(sipe_private
, msg
);
1693 char *self
= sip_uri_self(sipe_private
);
1694 if (sipe_strequal(SIPE_MEDIA_CALL
->with
, self
)) {
1696 sip_transport_response(sipe_private
, msg
, 488, "Not Acceptable Here", NULL
);
1704 transport_response_unsupported_sdp(sipe_private
, msg
);
1706 sipe_media_hangup(call_private
);
1711 if (!call_private
) {
1712 gchar
*with
= parse_from(sipmsg_find_header(msg
, "From"));
1713 SipeMediaCallFlags flags
= 0;
1715 if (strstr(msg
->body
, "m=data") ||
1716 strstr(msg
->body
, "m=applicationsharing")) {
1717 flags
|= SIPE_MEDIA_CALL_NO_UI
;
1720 call_private
= (struct sipe_media_call_private
*)
1721 sipe_media_call_new(sipe_private
, with
,
1722 msg
, smsg
->ice_version
,
1725 if (!(flags
& SIPE_MEDIA_CALL_NO_UI
)) {
1726 SIPE_MEDIA_CALL
->call_reject_cb
= av_call_reject_cb
;
1731 if (call_private
->invitation
)
1732 sipmsg_free(call_private
->invitation
);
1733 call_private
->invitation
= sipmsg_copy(msg
);
1735 ssrc_range_update(&call_private
->ssrc_ranges
, smsg
->media
);
1737 // Create any new media streams
1738 for (i
= smsg
->media
; i
; i
= i
->next
) {
1739 struct sdpmedia
*media
= i
->data
;
1740 gchar
*id
= media
->name
;
1743 if ( media
->port
!= 0
1744 && !sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL
, id
)) {
1745 guint32 ssrc_count
= 0;
1747 if (sipe_strequal(id
, "audio"))
1748 type
= SIPE_MEDIA_AUDIO
;
1749 else if (sipe_strequal(id
, "video")) {
1750 type
= SIPE_MEDIA_VIDEO
;
1751 ssrc_count
= VIDEO_SSRC_COUNT
;
1752 } else if (sipe_strequal(id
, "data"))
1753 type
= SIPE_MEDIA_APPLICATION
;
1754 else if (sipe_strequal(id
, "applicationsharing"))
1755 type
= SIPE_MEDIA_APPLICATION
;
1759 sipe_media_stream_add(SIPE_MEDIA_CALL
, id
, type
,
1760 smsg
->ice_version
, FALSE
,
1762 has_new_media
= TRUE
;
1766 if (has_new_media
) {
1767 sdpmsg_free(call_private
->smsg
);
1768 call_private
->smsg
= smsg
;
1769 sip_transport_response(sipe_private
, call_private
->invitation
,
1770 180, "Ringing", NULL
);
1771 // Processing continues in stream_initialized_cb
1773 apply_remote_message(call_private
, smsg
);
1775 maybe_send_second_invite_response(call_private
);
1778 return SIPE_MEDIA_CALL
;
1781 void process_incoming_cancel_call(struct sipe_media_call_private
*call_private
,
1784 // We respond to the CANCEL request with 200 OK response and
1785 // with 487 Request Terminated to the remote INVITE in progress.
1786 sip_transport_response(call_private
->sipe_private
, msg
, 200, "OK", NULL
);
1788 if (call_private
->invitation
) {
1789 sip_transport_response(call_private
->sipe_private
,
1790 call_private
->invitation
,
1791 487, "Request Terminated", NULL
);
1794 sipe_backend_media_reject(SIPE_MEDIA_CALL
->backend_private
, FALSE
);
1798 sipe_media_send_ack(struct sipe_core_private
*sipe_private
,
1800 struct transaction
*trans
)
1802 struct sipe_media_call_private
*call_private
;
1803 struct sip_dialog
*dialog
;
1806 call_private
= sipe_media_from_sipmsg(sipe_private
, msg
);
1808 if (!is_media_session_msg(call_private
, msg
))
1811 dialog
= sipe_media_get_sip_dialog(SIPE_MEDIA_CALL
);
1815 tmp_cseq
= dialog
->cseq
;
1817 dialog
->cseq
= sip_transaction_cseq(trans
) - 1;
1818 sip_transport_ack(sipe_private
, dialog
);
1819 dialog
->cseq
= tmp_cseq
;
1821 dialog
->outgoing_invite
= NULL
;
1827 sipe_media_send_final_ack(struct sipe_core_private
*sipe_private
,
1829 struct transaction
*trans
)
1831 struct sipe_media_call_private
*call_private
;
1836 if (!sipe_media_send_ack(sipe_private
, msg
, trans
))
1839 call_private
= sipe_media_from_sipmsg(sipe_private
, msg
);
1841 sipe_backend_media_accept(SIPE_MEDIA_CALL
->backend_private
, FALSE
);
1844 for (it
= call_private
->streams
; it
; it
= it
->next
) {
1845 struct sipe_media_stream_private
*stream_private
= it
->data
;
1847 stream_private
->sdp_negotiation_concluded
= TRUE
;
1848 if (stream_private
->writable
) {
1849 // We've become writable.
1850 sipe_core_media_stream_writable(SIPE_MEDIA_STREAM
, TRUE
);
1859 sipe_core_media_stream_candidate_pair_established(struct sipe_media_stream
*stream
)
1861 struct sipe_media_call
*call
= stream
->call
;
1863 GList
*active_candidates
=
1864 sipe_backend_media_stream_get_active_local_candidates(stream
);
1865 guint ready_components
= g_list_length(active_candidates
);
1867 sipe_media_candidate_list_free(active_candidates
);
1869 if (ready_components
!= 2) {
1870 // We must have both RTP+RTCP candidate pairs established first.
1874 if (SIPE_MEDIA_STREAM_PRIVATE
->established
) {
1877 SIPE_MEDIA_STREAM_PRIVATE
->established
= TRUE
;
1879 stream_schedule_cancel_timeout(call
, SIPE_MEDIA_STREAM_PRIVATE
);
1881 if (stream
->candidate_pairs_established_cb
) {
1882 stream
->candidate_pairs_established_cb(stream
);
1885 if (sipe_backend_media_is_initiator(stream
->call
, NULL
)) {
1886 GSList
*streams
= SIPE_MEDIA_CALL_PRIVATE
->streams
;
1887 for (; streams
; streams
= streams
->next
) {
1888 struct sipe_media_stream_private
*s
= streams
->data
;
1889 if (!s
->established
) {
1894 if (streams
== NULL
) {
1895 // All call streams have been established.
1896 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE
,
1897 sipe_media_send_final_ack
);
1900 maybe_send_second_invite_response(SIPE_MEDIA_CALL_PRIVATE
);
1905 maybe_retry_call_with_ice_version(struct sipe_core_private
*sipe_private
,
1906 struct sipe_media_call_private
*call_private
,
1907 SipeIceVersion ice_version
,
1908 struct transaction
*trans
)
1910 if (call_private
->ice_version
!= ice_version
&&
1911 sip_transaction_cseq(trans
) == 1) {
1914 gboolean with_video
= FALSE
;
1916 for (i
= call_private
->streams
; i
; i
= i
->next
) {
1917 struct sipe_media_stream
*stream
= i
->data
;
1919 if (sipe_strequal(stream
->id
, "video")) {
1921 } else if (!sipe_strequal(stream
->id
, "audio")) {
1922 /* Don't retry calls which are neither audio
1928 with
= g_strdup(SIPE_MEDIA_CALL
->with
);
1930 sipe_media_hangup(call_private
);
1931 SIPE_DEBUG_INFO("Retrying call with ICEv%d.",
1932 ice_version
== SIPE_ICE_DRAFT_6
? 6 : 19);
1933 sipe_media_initiate_call(sipe_private
,
1946 process_invite_call_response(struct sipe_core_private
*sipe_private
,
1948 struct transaction
*trans
)
1951 struct sipe_media_call_private
*call_private
;
1952 struct sip_dialog
*dialog
;
1953 struct sdpmsg
*smsg
;
1955 call_private
= sipe_media_from_sipmsg(sipe_private
,msg
);
1957 if (!is_media_session_msg(call_private
, msg
))
1960 dialog
= sipe_media_get_sip_dialog(SIPE_MEDIA_CALL
);
1962 with
= dialog
->with
;
1964 dialog
->outgoing_invite
= NULL
;
1966 if (msg
->response
== 603 || msg
->response
== 605) {
1967 // Call rejected by remote peer
1968 sipe_media_send_ack(sipe_private
, msg
, trans
);
1969 sipe_backend_media_reject(SIPE_MEDIA_CALL
->backend_private
, FALSE
);
1974 if (msg
->response
>= 400) {
1975 // An error occurred
1977 GString
*desc
= g_string_new("");
1978 gboolean append_responsestr
= FALSE
;
1980 switch (msg
->response
) {
1982 title
= _("User unavailable");
1984 if (sipmsg_parse_warning(msg
, NULL
) == 391) {
1985 g_string_append_printf(desc
, _("%s does not want to be disturbed"), with
);
1987 g_string_append_printf(desc
, _("User %s is not available"), with
);
1991 // OCS/Lync really sends response string with 'Mutipart' typo.
1992 if (sipe_strequal(msg
->responsestr
, "Mutipart mime in content type not supported by Archiving CDR service") &&
1993 maybe_retry_call_with_ice_version(sipe_private
,
1999 title
= _("Unsupported media type");
2002 /* Check for incompatible encryption levels error.
2005 * 488 Not Acceptable Here
2006 * ms-client-diagnostics: 52017;reason="Encryption levels dont match"
2008 * older clients (and SIPE itself):
2009 * 488 Encryption Levels not compatible
2011 const gchar
*ms_diag
= sipmsg_find_header(msg
, "ms-client-diagnostics");
2012 SipeIceVersion retry_ice_version
= SIPE_ICE_DRAFT_6
;
2014 if (sipe_strequal(msg
->responsestr
, "Encryption Levels not compatible") ||
2015 (ms_diag
&& g_str_has_prefix(ms_diag
, "52017;"))) {
2016 title
= _("Unable to establish a call");
2017 g_string_append(desc
, _("Encryption settings of peer are incompatible with ours."));
2021 /* Check if this is failed conference using
2022 * ICEv6 with reason "Error parsing SDP" and
2023 * retry using ICEv19. */
2024 ms_diag
= sipmsg_find_header(msg
, "ms-diagnostics");
2025 if (ms_diag
&& g_str_has_prefix(ms_diag
, "7008;")) {
2026 retry_ice_version
= SIPE_ICE_RFC_5245
;
2029 if (maybe_retry_call_with_ice_version(sipe_private
,
2038 title
= _("Error occurred");
2039 g_string_append(desc
, _("Unable to establish a call"));
2040 append_responsestr
= TRUE
;
2044 if (append_responsestr
) {
2045 gchar
*reason
= sipmsg_get_ms_diagnostics_reason(msg
);
2047 g_string_append_printf(desc
, "\n%d %s",
2048 msg
->response
, msg
->responsestr
);
2050 g_string_append_printf(desc
, "\n\n%s", reason
);
2055 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, title
, desc
->str
);
2056 g_string_free(desc
, TRUE
);
2058 sipe_media_send_ack(sipe_private
, msg
, trans
);
2059 sipe_media_hangup(call_private
);
2064 sipe_dialog_parse(dialog
, msg
, TRUE
);
2065 smsg
= sdpmsg_parse_msg(msg
->body
);
2067 transport_response_unsupported_sdp(sipe_private
, msg
);
2068 sipe_media_hangup(call_private
);
2072 ssrc_range_update(&call_private
->ssrc_ranges
, smsg
->media
);
2073 apply_remote_message(call_private
, smsg
);
2076 stream_schedule_timeout(SIPE_MEDIA_CALL
);
2077 call_schedule_cancel_request_timeout(SIPE_MEDIA_CALL
);
2078 sipe_media_send_ack(sipe_private
, msg
, trans
);
2082 // Waits until sipe_core_media_candidate_pair_established() is invoked.
2085 gboolean
is_media_session_msg(struct sipe_media_call_private
*call_private
,
2088 if (!call_private
) {
2092 return sipe_media_from_sipmsg(call_private
->sipe_private
, msg
) == call_private
;
2096 end_call(SIPE_UNUSED_PARAMETER gpointer key
,
2097 struct sipe_media_call_private
*call_private
,
2098 SIPE_UNUSED_PARAMETER gpointer user_data
)
2100 struct sipe_backend_media
*backend_private
;
2102 backend_private
= call_private
->public.backend_private
;
2104 if (!sipe_backend_media_is_initiator(SIPE_MEDIA_CALL
, NULL
) &&
2105 !sipe_backend_media_accepted(backend_private
)) {
2106 sip_transport_response(call_private
->sipe_private
,
2107 call_private
->invitation
,
2108 480, "Temporarily Unavailable", NULL
);
2109 } else if (call_private
->session
) {
2110 sipe_session_close(call_private
->sipe_private
,
2111 call_private
->session
);
2112 call_private
->session
= NULL
;
2115 sipe_media_hangup(call_private
);
2119 sipe_media_handle_going_offline(struct sipe_core_private
*sipe_private
)
2121 g_hash_table_foreach(sipe_private
->media_calls
, (GHFunc
) end_call
, NULL
);
2124 gboolean
sipe_media_is_conference_call(struct sipe_media_call_private
*call_private
)
2126 return g_strstr_len(SIPE_MEDIA_CALL
->with
, -1, "app:conf:audio-video:") != NULL
;
2129 struct sipe_core_private
*
2130 sipe_media_get_sipe_core_private(struct sipe_media_call
*call
)
2132 g_return_val_if_fail(call
, NULL
);
2134 return SIPE_MEDIA_CALL_PRIVATE
->sipe_private
;
2138 sipe_media_get_sip_dialog(struct sipe_media_call
*call
)
2140 struct sip_session
*session
;
2142 g_return_val_if_fail(call
, NULL
);
2144 session
= SIPE_MEDIA_CALL_PRIVATE
->session
;
2146 if (!session
|| !session
->dialogs
) {
2150 return session
->dialogs
->data
;
2154 sipe_media_relay_free(struct sipe_media_relay
*relay
)
2156 g_free(relay
->hostname
);
2157 if (relay
->dns_query
)
2158 sipe_backend_dns_query_cancel(relay
->dns_query
);
2163 sipe_media_relay_list_free(GSList
*list
)
2165 for (; list
; list
= g_slist_delete_link(list
, list
))
2166 sipe_media_relay_free(list
->data
);
2170 relay_ip_resolved_cb(struct sipe_media_relay
* relay
,
2171 const gchar
*ip
, SIPE_UNUSED_PARAMETER guint port
)
2173 gchar
*hostname
= relay
->hostname
;
2174 relay
->dns_query
= NULL
;
2177 relay
->hostname
= g_strdup(ip
);
2178 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname
, ip
);
2180 relay
->hostname
= NULL
;
2181 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname
);
2188 process_get_av_edge_credentials_response(struct sipe_core_private
*sipe_private
,
2190 SIPE_UNUSED_PARAMETER
struct transaction
*trans
)
2192 g_free(sipe_private
->media_relay_username
);
2193 g_free(sipe_private
->media_relay_password
);
2194 sipe_media_relay_list_free(sipe_private
->media_relays
);
2195 sipe_private
->media_relay_username
= NULL
;
2196 sipe_private
->media_relay_password
= NULL
;
2197 sipe_private
->media_relays
= NULL
;
2199 if (msg
->response
>= 400) {
2200 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
2201 "Failed to obtain A/V Edge credentials.");
2205 if (msg
->response
== 200) {
2206 sipe_xml
*xn_response
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
2208 if (sipe_strequal("OK", sipe_xml_attribute(xn_response
, "reasonPhrase"))) {
2209 const sipe_xml
*xn_credentials
= sipe_xml_child(xn_response
, "credentialsResponse/credentials");
2210 const sipe_xml
*xn_relays
= sipe_xml_child(xn_response
, "credentialsResponse/mediaRelayList");
2211 const sipe_xml
*item
;
2212 GSList
*relays
= NULL
;
2214 item
= sipe_xml_child(xn_credentials
, "username");
2215 sipe_private
->media_relay_username
= sipe_xml_data(item
);
2216 item
= sipe_xml_child(xn_credentials
, "password");
2217 sipe_private
->media_relay_password
= sipe_xml_data(item
);
2219 for (item
= sipe_xml_child(xn_relays
, "mediaRelay"); item
; item
= sipe_xml_twin(item
)) {
2220 struct sipe_media_relay
*relay
= g_new0(struct sipe_media_relay
, 1);
2221 const sipe_xml
*node
;
2224 node
= sipe_xml_child(item
, "hostName");
2225 relay
->hostname
= sipe_xml_data(node
);
2227 node
= sipe_xml_child(item
, "udpPort");
2229 tmp
= sipe_xml_data(node
);
2231 relay
->udp_port
= atoi(tmp
);
2236 node
= sipe_xml_child(item
, "tcpPort");
2238 tmp
= sipe_xml_data(node
);
2240 relay
->tcp_port
= atoi(tmp
);
2245 relays
= g_slist_append(relays
, relay
);
2247 relay
->dns_query
= sipe_backend_dns_query_a(
2251 (sipe_dns_resolved_cb
) relay_ip_resolved_cb
,
2254 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
2256 relay
->tcp_port
, relay
->udp_port
);
2259 sipe_private
->media_relays
= relays
;
2262 sipe_xml_free(xn_response
);
2269 sipe_media_get_av_edge_credentials(struct sipe_core_private
*sipe_private
)
2271 // TODO: re-request credentials after duration expires?
2272 static const char CRED_REQUEST_XML
[] =
2273 "<request requestID=\"%d\" "
2277 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
2278 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
2279 "<credentialsRequest credentialsRequestID=\"%d\">"
2280 "<identity>%s</identity>"
2281 "<location>%s</location>"
2282 "<duration>480</duration>"
2283 "</credentialsRequest>"
2286 int request_id
= rand();
2290 if (!sipe_private
->mras_uri
)
2293 self
= sip_uri_self(sipe_private
);
2295 body
= g_strdup_printf(
2299 sipe_private
->mras_uri
,
2302 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER
) ? "internet" : "intranet");
2305 sip_transport_service(sipe_private
,
2306 sipe_private
->mras_uri
,
2307 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
2309 process_get_av_edge_credentials_response
);
2315 sipe_media_add_extra_invite_section(struct sipe_media_call
*call
,
2316 const gchar
*invite_content_type
,
2319 g_free(SIPE_MEDIA_CALL_PRIVATE
->extra_invite_section
);
2320 g_free(SIPE_MEDIA_CALL_PRIVATE
->invite_content_type
);
2321 SIPE_MEDIA_CALL_PRIVATE
->extra_invite_section
= body
;
2322 SIPE_MEDIA_CALL_PRIVATE
->invite_content_type
=
2323 g_strdup(invite_content_type
);
2327 sipe_media_stream_add_extra_attribute(struct sipe_media_stream
*stream
,
2328 const gchar
*name
, const gchar
*value
)
2330 SIPE_MEDIA_STREAM_PRIVATE
->extra_sdp
=
2331 sipe_utils_nameval_add(SIPE_MEDIA_STREAM_PRIVATE
->extra_sdp
,
2337 sipe_core_media_stream_readable(struct sipe_media_stream
*stream
)
2339 g_return_if_fail(stream
);
2341 if (g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE
->async_reads
) &&
2343 stream
->read_cb(stream
);
2346 while (!g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE
->async_reads
)) {
2347 struct async_read_data
*data
;
2352 data
= g_queue_peek_head(SIPE_MEDIA_STREAM_PRIVATE
->async_reads
);
2353 pos
= data
->buffer
+ SIPE_MEDIA_STREAM_PRIVATE
->read_pos
;
2354 len
= data
->len
- SIPE_MEDIA_STREAM_PRIVATE
->read_pos
;
2356 bytes_read
= sipe_backend_media_stream_read(stream
, pos
, len
);
2357 if (bytes_read
== -1) {
2358 struct sipe_media_call
*call
= stream
->call
;
2359 struct sipe_core_private
*sipe_private
=
2360 SIPE_MEDIA_CALL_PRIVATE
->sipe_private
;
2362 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
2364 _("Error while reading from stream"));
2365 sipe_media_hangup(SIPE_MEDIA_CALL_PRIVATE
);
2369 SIPE_MEDIA_STREAM_PRIVATE
->read_pos
+= bytes_read
;
2371 if (SIPE_MEDIA_STREAM_PRIVATE
->read_pos
== data
->len
) {
2372 data
->callback(stream
, data
->buffer
, data
->len
);
2373 SIPE_MEDIA_STREAM_PRIVATE
->read_pos
= 0;
2374 g_queue_pop_head(SIPE_MEDIA_STREAM_PRIVATE
->async_reads
);
2377 // Still not enough data to finish the read.
2384 sipe_media_stream_read_async(struct sipe_media_stream
*stream
,
2385 gpointer buffer
, gsize len
,
2386 sipe_media_stream_read_callback callback
)
2388 struct async_read_data
*data
;
2390 g_return_if_fail(stream
&& buffer
&& callback
);
2392 data
= g_new0(struct async_read_data
, 1);
2393 data
->buffer
= buffer
;
2395 data
->callback
= callback
;
2397 g_queue_push_tail(SIPE_MEDIA_STREAM_PRIVATE
->async_reads
, data
);
2401 stream_append_buffer(struct sipe_media_stream
*stream
,
2402 guint8
*buffer
, guint len
)
2404 GByteArray
*b
= g_byte_array_sized_new(len
);
2405 g_byte_array_append(b
, buffer
, len
);
2406 g_queue_push_tail(SIPE_MEDIA_STREAM_PRIVATE
->write_queue
, b
);
2410 sipe_media_stream_write(struct sipe_media_stream
*stream
,
2411 gpointer buffer
, gsize len
)
2413 if (!sipe_media_stream_is_writable(stream
)) {
2414 stream_append_buffer(stream
, buffer
, len
);
2419 written
= sipe_backend_media_stream_write(stream
, buffer
, len
);
2420 if (written
== len
) {
2424 stream_append_buffer(stream
,
2425 (guint8
*)buffer
+ written
, len
- written
);
2431 sipe_core_media_stream_writable(struct sipe_media_stream
*stream
,
2434 SIPE_MEDIA_STREAM_PRIVATE
->writable
= writable
;
2440 while (!g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE
->write_queue
)) {
2444 b
= g_queue_peek_head(SIPE_MEDIA_STREAM_PRIVATE
->write_queue
);
2446 written
= sipe_backend_media_stream_write(stream
, b
->data
, b
->len
);
2447 if (written
!= b
->len
) {
2448 g_byte_array_remove_range(b
, 0, written
);
2452 g_byte_array_unref(b
);
2453 g_queue_pop_head(SIPE_MEDIA_STREAM_PRIVATE
->write_queue
);
2456 if (sipe_media_stream_is_writable(stream
) && stream
->writable_cb
) {
2457 stream
->writable_cb(stream
);
2462 sipe_media_stream_is_writable(struct sipe_media_stream
*stream
)
2464 return SIPE_MEDIA_STREAM_PRIVATE
->writable
&&
2465 SIPE_MEDIA_STREAM_PRIVATE
->sdp_negotiation_concluded
&&
2466 g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE
->write_queue
);
2471 sipe_media_stream_set_data(struct sipe_media_stream
*stream
, gpointer data
,
2472 GDestroyNotify free_func
)
2474 struct sipe_media_stream_private
*stream_private
=
2475 SIPE_MEDIA_STREAM_PRIVATE
;
2477 g_return_if_fail(stream_private
);
2479 if (stream_private
->data
&& stream_private
->data_free_func
) {
2480 stream_private
->data_free_func(stream_private
->data
);
2483 stream_private
->data
= data
;
2484 stream_private
->data_free_func
= free_func
;
2488 sipe_media_stream_get_data(struct sipe_media_stream
*stream
)
2490 g_return_val_if_fail(stream
, NULL
);
2492 return SIPE_MEDIA_STREAM_PRIVATE
->data
;