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"
50 /* [MS-SDPEXT] 3.1.5.31.2 says a range size of 100 SHOULD be used for video and
51 * some clients really demand this. */
52 #define VIDEO_SSRC_COUNT 100
54 struct sipe_media_call_private
{
55 struct sipe_media_call
public;
57 /* private part starts here */
58 struct sipe_core_private
*sipe_private
;
60 struct sip_session
*session
;
61 struct sip_session
*conference_session
;
65 struct sipmsg
*invitation
;
66 SipeIceVersion ice_version
;
67 gboolean encryption_compatible
;
68 gchar
*extra_invite_section
;
69 gchar
*invite_content_type
;
76 #define SIPE_MEDIA_CALL ((struct sipe_media_call *) call_private)
77 #define SIPE_MEDIA_CALL_PRIVATE ((struct sipe_media_call_private *) call)
79 struct sipe_media_stream_private
{
80 struct sipe_media_stream
public;
82 guchar
*encryption_key
;
83 int encryption_key_id
;
84 gboolean remote_candidates_and_codecs_set
;
87 gboolean sdp_negotiation_concluded
;
97 /* User data associated with the stream. */
99 GDestroyNotify data_free_func
;
101 #define SIPE_MEDIA_STREAM ((struct sipe_media_stream *) stream_private)
102 #define SIPE_MEDIA_STREAM_PRIVATE ((struct sipe_media_stream_private *) stream)
104 struct async_read_data
{
107 sipe_media_stream_read_callback callback
;
110 static void sipe_media_codec_list_free(GList
*codecs
)
112 for (; codecs
; codecs
= g_list_delete_link(codecs
, codecs
))
113 sipe_backend_codec_free(codecs
->data
);
116 static void sipe_media_candidate_list_free(GList
*candidates
)
118 for (; candidates
; candidates
= g_list_delete_link(candidates
, candidates
))
119 sipe_backend_candidate_free(candidates
->data
);
123 sipe_media_stream_free(struct sipe_media_stream_private
*stream_private
)
125 struct sipe_media_call_private
*call_private
;
127 call_private
= (struct sipe_media_call_private
*)SIPE_MEDIA_STREAM
->call
;
129 sipe_media_stream_set_data(SIPE_MEDIA_STREAM
, NULL
, NULL
);
132 call_private
->streams
= g_slist_remove(call_private
->streams
,
135 if (SIPE_MEDIA_STREAM
->ssrc_range
) {
136 call_private
->ssrc_ranges
=
137 g_slist_remove(call_private
->ssrc_ranges
,
138 SIPE_MEDIA_STREAM
->ssrc_range
);
142 if (SIPE_MEDIA_STREAM
->backend_private
) {
143 sipe_backend_media_stream_free(SIPE_MEDIA_STREAM
->backend_private
);
145 g_free(SIPE_MEDIA_STREAM
->id
);
146 g_free(stream_private
->encryption_key
);
147 g_queue_free_full(stream_private
->write_queue
,
148 (GDestroyNotify
)g_byte_array_unref
);
149 g_queue_free_full(stream_private
->async_reads
, g_free
);
150 sipe_utils_nameval_free(stream_private
->extra_sdp
);
151 g_free(stream_private
);
155 call_private_equals(SIPE_UNUSED_PARAMETER
const gchar
*callid
,
156 struct sipe_media_call_private
*call_private1
,
157 struct sipe_media_call_private
*call_private2
)
159 return call_private1
== call_private2
;
163 sipe_media_call_free(struct sipe_media_call_private
*call_private
)
166 g_hash_table_foreach_remove(call_private
->sipe_private
->media_calls
,
167 (GHRFunc
) call_private_equals
, call_private
);
169 while (call_private
->streams
) {
170 sipe_media_stream_free(call_private
->streams
->data
);
173 sipe_backend_media_free(call_private
->public.backend_private
);
175 if (call_private
->session
) {
176 sipe_session_remove(call_private
->sipe_private
,
177 call_private
->session
);
180 if (call_private
->invitation
)
181 sipmsg_free(call_private
->invitation
);
183 // Frees any referenced extra invite data.
184 sipe_media_add_extra_invite_section(SIPE_MEDIA_CALL
, NULL
, NULL
);
186 sipe_utils_slist_free_full(call_private
->ssrc_ranges
, g_free
);
188 sdpmsg_free(call_private
->smsg
);
189 sipe_utils_slist_free_full(call_private
->failed_media
,
190 (GDestroyNotify
)sdpmedia_free
);
191 g_free(SIPE_MEDIA_CALL
->with
);
192 g_free(call_private
);
197 candidate_sort_cb(struct sdpcandidate
*c1
, struct sdpcandidate
*c2
)
199 int cmp
= sipe_strcompare(c1
->foundation
, c2
->foundation
);
201 cmp
= sipe_strcompare(c1
->username
, c2
->username
);
203 cmp
= c1
->component
- c2
->component
;
210 backend_candidates_to_sdpcandidate(GList
*candidates
)
212 GSList
*result
= NULL
;
215 for (i
= candidates
; i
; i
= i
->next
) {
216 struct sipe_backend_candidate
*candidate
= i
->data
;
217 struct sdpcandidate
*c
;
219 gchar
*ip
= sipe_backend_candidate_get_ip(candidate
);
220 gchar
*base_ip
= sipe_backend_candidate_get_base_ip(candidate
);
221 if (is_empty(ip
) || strchr(ip
, ':') ||
222 (base_ip
&& strchr(base_ip
, ':'))) {
223 /* Ignore IPv6 candidates. */
229 c
= g_new(struct sdpcandidate
, 1);
230 c
->foundation
= sipe_backend_candidate_get_foundation(candidate
);
231 c
->component
= sipe_backend_candidate_get_component_type(candidate
);
232 c
->type
= sipe_backend_candidate_get_type(candidate
);
233 c
->protocol
= sipe_backend_candidate_get_protocol(candidate
);
235 c
->port
= sipe_backend_candidate_get_port(candidate
);
236 c
->base_ip
= base_ip
;
237 c
->base_port
= sipe_backend_candidate_get_base_port(candidate
);
238 c
->priority
= sipe_backend_candidate_get_priority(candidate
);
239 c
->username
= sipe_backend_candidate_get_username(candidate
);
240 c
->password
= sipe_backend_candidate_get_password(candidate
);
242 result
= g_slist_insert_sorted(result
, c
,
243 (GCompareFunc
)candidate_sort_cb
);
250 get_stream_ip_and_ports(GSList
*candidates
,
251 gchar
**ip
, guint
*rtp_port
, guint
*rtcp_port
)
253 guint32 rtp_max_priority
= 0;
254 guint32 rtcp_max_priority
= 0;
260 for (; candidates
; candidates
= candidates
->next
) {
261 struct sdpcandidate
*candidate
= candidates
->data
;
263 if (candidate
->component
== SIPE_COMPONENT_RTP
&&
264 candidate
->priority
> rtp_max_priority
) {
265 rtp_max_priority
= candidate
->priority
;
266 *rtp_port
= candidate
->port
;
269 *ip
= g_strdup(candidate
->ip
);
270 } else if (candidate
->component
== SIPE_COMPONENT_RTCP
&&
271 candidate
->priority
> rtcp_max_priority
) {
272 rtcp_max_priority
= candidate
->priority
;
273 *rtcp_port
= candidate
->port
;
279 sdpcodec_compare(gconstpointer a
, gconstpointer b
)
281 return ((const struct sdpcodec
*)a
)->id
-
282 ((const struct sdpcodec
*)b
)->id
;
286 remove_wrong_farstream_0_1_tcp_candidates(GList
*candidates
)
288 GList
*i
= candidates
;
289 GHashTable
*foundation_to_candidate
= g_hash_table_new_full(g_str_hash
,
295 GList
*next
= i
->next
;
296 struct sipe_backend_candidate
*c1
= i
->data
;
298 if (sipe_backend_candidate_get_protocol(c1
) == SIPE_NETWORK_PROTOCOL_UDP
) {
299 gchar
*foundation
= sipe_backend_candidate_get_foundation(c1
);
300 struct sipe_backend_candidate
*c2
= g_hash_table_lookup(foundation_to_candidate
,
306 if (sipe_backend_candidate_get_port(c1
) ==
307 sipe_backend_candidate_get_port(c2
) ||
308 (sipe_backend_candidate_get_type(c1
) !=
309 SIPE_CANDIDATE_TYPE_HOST
&&
310 sipe_backend_candidate_get_base_port(c1
) ==
311 sipe_backend_candidate_get_base_port(c2
))) {
313 * We assume that RTP+RTCP UDP pairs
314 * that share the same port are
315 * actually mistagged TCP candidates.
317 candidates
= g_list_remove(candidates
, c2
);
318 candidates
= g_list_delete_link(candidates
, i
);
319 sipe_backend_candidate_free(c1
);
320 sipe_backend_candidate_free(c2
);
323 /* hash table takes ownership of "foundation" */
324 g_hash_table_insert(foundation_to_candidate
, foundation
, c1
);
330 g_hash_table_destroy(foundation_to_candidate
);
336 fill_zero_tcp_act_ports_from_tcp_pass(GSList
*candidates
, GSList
*all_candidates
)
339 GHashTable
*ip_to_port
= g_hash_table_new(g_str_hash
, g_str_equal
);
341 for (i
= candidates
; i
; i
= i
->next
) {
342 struct sdpcandidate
*c
= i
->data
;
345 if (c
->protocol
!= SIPE_NETWORK_PROTOCOL_TCP_ACTIVE
) {
349 for (j
= all_candidates
; j
; j
= j
->next
) {
350 struct sdpcandidate
*passive
= j
->data
;
351 if (passive
->protocol
!= SIPE_NETWORK_PROTOCOL_TCP_PASSIVE
||
352 c
->type
!= passive
->type
) {
356 if (sipe_strequal(c
->ip
, passive
->ip
) &&
357 sipe_strequal(c
->base_ip
, passive
->base_ip
)) {
359 c
->port
= passive
->port
;
362 if (c
->base_port
== 0) {
363 c
->base_port
= passive
->base_port
;
370 for (i
= all_candidates
; i
; i
= i
->next
) {
371 struct sdpcandidate
*c
= i
->data
;
373 if (c
->protocol
== SIPE_NETWORK_PROTOCOL_TCP_PASSIVE
&&
374 c
->type
== SIPE_CANDIDATE_TYPE_HOST
) {
375 g_hash_table_insert(ip_to_port
, c
->ip
, &c
->port
);
379 /* Fill base ports of all TCP relay candidates using what we have
380 * collected from host candidates. */
381 for (i
= candidates
; i
; i
= i
->next
) {
382 struct sdpcandidate
*c
= i
->data
;
383 if (c
->type
== SIPE_CANDIDATE_TYPE_RELAY
&& c
->base_port
== 0) {
384 guint
*base_port
= (guint
*)g_hash_table_lookup(ip_to_port
, c
->base_ip
);
386 c
->base_port
= *base_port
;
388 SIPE_DEBUG_WARNING("Couldn't determine base port for candidate "
389 "with foundation %s", c
->foundation
);
394 g_hash_table_destroy(ip_to_port
);
397 static SipeEncryptionPolicy
398 get_encryption_policy(struct sipe_core_private
*sipe_private
)
400 SipeEncryptionPolicy result
=
401 sipe_backend_media_get_encryption_policy(SIPE_CORE_PUBLIC
);
402 if (result
== SIPE_ENCRYPTION_POLICY_OBEY_SERVER
) {
403 result
= sipe_private
->server_av_encryption_policy
;
409 static struct sdpmedia
*
410 media_stream_to_sdpmedia(struct sipe_media_call_private
*call_private
,
411 struct sipe_media_stream_private
*stream_private
)
413 struct sdpmedia
*sdpmedia
= g_new0(struct sdpmedia
, 1);
414 GList
*codecs
= sipe_backend_get_local_codecs(SIPE_MEDIA_CALL
,
416 SipeEncryptionPolicy encryption_policy
=
417 get_encryption_policy(call_private
->sipe_private
);
420 GSList
*attributes
= NULL
;
421 GSList
*sdpcandidates
;
422 GSList
*all_sdpcandidates
;
427 sdpmedia
->name
= g_strdup(SIPE_MEDIA_STREAM
->id
);
429 if (sipe_strequal(sdpmedia
->name
, "audio"))
430 type
= SIPE_MEDIA_AUDIO
;
431 else if (sipe_strequal(sdpmedia
->name
, "video"))
432 type
= SIPE_MEDIA_VIDEO
;
433 else if (sipe_strequal(sdpmedia
->name
, "data"))
434 type
= SIPE_MEDIA_APPLICATION
;
435 else if (sipe_strequal(sdpmedia
->name
, "applicationsharing"))
436 type
= SIPE_MEDIA_APPLICATION
;
438 // TODO: incompatible media, should not happen here
439 g_free(sdpmedia
->name
);
441 sipe_media_codec_list_free(codecs
);
446 for (i
= codecs
; i
; i
= i
->next
) {
447 struct sipe_backend_codec
*codec
= i
->data
;
448 struct sdpcodec
*c
= g_new0(struct sdpcodec
, 1);
451 c
->id
= sipe_backend_codec_get_id(codec
);
452 c
->name
= sipe_backend_codec_get_name(codec
);
453 c
->clock_rate
= sipe_backend_codec_get_clock_rate(codec
);
456 params
= sipe_backend_codec_get_optional_parameters(codec
);
457 for (; params
; params
= params
->next
) {
458 struct sipnameval
*param
= params
->data
;
459 struct sipnameval
*copy
= g_new0(struct sipnameval
, 1);
461 copy
->name
= g_strdup(param
->name
);
462 copy
->value
= g_strdup(param
->value
);
464 c
->parameters
= g_slist_append(c
->parameters
, copy
);
467 /* Buggy(?) codecs may report non-unique id (a.k.a. payload
468 * type) that must not appear in SDP messages we send. Thus,
469 * let's ignore any codec having the same id as one we already
470 * have in the converted list. */
471 if (g_slist_find_custom(sdpmedia
->codecs
, c
, sdpcodec_compare
)) {
474 sdpmedia
->codecs
= g_slist_append(sdpmedia
->codecs
, c
);
478 sipe_media_codec_list_free(codecs
);
480 // Process local candidates
481 // If we have established candidate pairs, send them in SDP response.
482 // Otherwise send all available local candidates.
483 candidates
= sipe_backend_media_stream_get_active_local_candidates(SIPE_MEDIA_STREAM
);
484 sdpcandidates
= backend_candidates_to_sdpcandidate(candidates
);
485 sipe_media_candidate_list_free(candidates
);
487 candidates
= sipe_backend_get_local_candidates(SIPE_MEDIA_CALL
,
489 candidates
= remove_wrong_farstream_0_1_tcp_candidates(candidates
);
490 all_sdpcandidates
= backend_candidates_to_sdpcandidate(candidates
);
491 sipe_media_candidate_list_free(candidates
);
493 if (!sdpcandidates
) {
494 sdpcandidates
= all_sdpcandidates
;
497 fill_zero_tcp_act_ports_from_tcp_pass(sdpcandidates
, all_sdpcandidates
);
499 sdpmedia
->candidates
= sdpcandidates
;
501 if (all_sdpcandidates
!= sdpcandidates
) {
502 sipe_utils_slist_free_full(all_sdpcandidates
,
503 (GDestroyNotify
)sdpcandidate_free
);
506 get_stream_ip_and_ports(sdpmedia
->candidates
, &sdpmedia
->ip
,
507 &sdpmedia
->port
, &rtcp_port
);
509 if (sipe_backend_stream_is_held(SIPE_MEDIA_STREAM
))
510 attributes
= sipe_utils_nameval_add(attributes
, "inactive", "");
513 gchar
*tmp
= g_strdup_printf("%u", rtcp_port
);
514 attributes
= sipe_utils_nameval_add(attributes
, "rtcp", tmp
);
518 if (encryption_policy
!= call_private
->sipe_private
->server_av_encryption_policy
) {
519 const gchar
*encryption
= NULL
;
520 switch (encryption_policy
) {
521 case SIPE_ENCRYPTION_POLICY_REJECTED
:
522 encryption
= "rejected";
524 case SIPE_ENCRYPTION_POLICY_OPTIONAL
:
525 encryption
= "optional";
527 case SIPE_ENCRYPTION_POLICY_REQUIRED
:
529 encryption
= "required";
533 attributes
= sipe_utils_nameval_add(attributes
, "encryption", encryption
);
536 if (SIPE_MEDIA_STREAM
->ssrc_range
) {
539 tmp
= g_strdup_printf("%u-%u",
540 SIPE_MEDIA_STREAM
->ssrc_range
->begin
,
541 SIPE_MEDIA_STREAM
->ssrc_range
->end
);
542 attributes
= sipe_utils_nameval_add(attributes
,
543 "x-ssrc-range", tmp
);
547 // Process remote candidates
548 candidates
= sipe_backend_media_stream_get_active_remote_candidates(SIPE_MEDIA_STREAM
);
549 sdpmedia
->remote_candidates
= backend_candidates_to_sdpcandidate(candidates
);
550 sipe_media_candidate_list_free(candidates
);
552 sdpmedia
->encryption_active
= stream_private
->encryption_key
&&
553 call_private
->encryption_compatible
&&
554 stream_private
->remote_candidates_and_codecs_set
&&
555 encryption_policy
!= SIPE_ENCRYPTION_POLICY_REJECTED
;
557 // Set our key if encryption is enabled.
558 if (stream_private
->encryption_key
&&
559 encryption_policy
!= SIPE_ENCRYPTION_POLICY_REJECTED
) {
560 sdpmedia
->encryption_key
= g_memdup(stream_private
->encryption_key
,
562 sdpmedia
->encryption_key_id
= stream_private
->encryption_key_id
;
565 // Append extra attributes assigned to the stream.
566 for (j
= stream_private
->extra_sdp
; j
; j
= g_slist_next(j
)) {
567 struct sipnameval
*attr
= j
->data
;
568 attributes
= sipe_utils_nameval_add(attributes
,
569 attr
->name
, attr
->value
);
572 sdpmedia
->attributes
= attributes
;
577 static struct sdpmsg
*
578 sipe_media_to_sdpmsg(struct sipe_media_call_private
*call_private
)
580 struct sdpmsg
*msg
= g_new0(struct sdpmsg
, 1);
581 GSList
*streams
= call_private
->streams
;
583 for (; streams
; streams
= streams
->next
) {
584 struct sdpmedia
*media
= media_stream_to_sdpmedia(call_private
,
587 msg
->media
= g_slist_append(msg
->media
, media
);
590 msg
->ip
= g_strdup(media
->ip
);
594 msg
->media
= g_slist_concat(msg
->media
, call_private
->failed_media
);
595 call_private
->failed_media
= NULL
;
597 msg
->ice_version
= call_private
->ice_version
;
603 sipe_invite_call(struct sipe_media_call_private
*call_private
, TransCallback tc
)
605 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
608 gchar
*p_preferred_identity
= NULL
;
610 struct sip_dialog
*dialog
;
613 dialog
= sipe_media_get_sip_dialog(SIPE_MEDIA_CALL
);
615 contact
= get_contact(sipe_private
);
617 if (sipe_private
->uc_line_uri
) {
618 gchar
*self
= sip_uri_self(sipe_private
);
619 p_preferred_identity
= g_strdup_printf(
620 "P-Preferred-Identity: <%s>, <%s>\r\n",
621 self
, sipe_private
->uc_line_uri
);
625 hdr
= g_strdup_printf(
626 "ms-keep-alive: UAC;hop-hop=yes\r\n"
629 "Content-Type: %s%s\r\n",
631 p_preferred_identity
? p_preferred_identity
: "",
632 call_private
->invite_content_type
?
633 call_private
->invite_content_type
: "application/sdp",
634 call_private
->invite_content_type
?
635 ";boundary=\"----=_NextPart_000_001E_01CB4397.0B5EB570\"" : "");
638 g_free(p_preferred_identity
);
640 msg
= sipe_media_to_sdpmsg(call_private
);
641 body
= sdpmsg_to_string(msg
);
643 if (call_private
->extra_invite_section
) {
645 tmp
= g_strdup_printf(
646 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
649 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
650 "Content-Type: application/sdp\r\n"
651 "Content-Transfer-Encoding: 7bit\r\n"
652 "Content-Disposition: session; handling=optional\r\n"
656 "------=_NextPart_000_001E_01CB4397.0B5EB570--\r\n",
657 call_private
->extra_invite_section
, body
);
660 sipe_media_add_extra_invite_section(SIPE_MEDIA_CALL
, NULL
, NULL
);
665 dialog
->outgoing_invite
= sip_transport_invite(sipe_private
,
676 send_response_with_session_description(struct sipe_media_call_private
*call_private
, int code
, gchar
*text
)
678 struct sdpmsg
*msg
= sipe_media_to_sdpmsg(call_private
);
679 gchar
*body
= sdpmsg_to_string(msg
);
681 sipmsg_add_header(call_private
->invitation
, "Content-Type", "application/sdp");
682 sip_transport_response(call_private
->sipe_private
, call_private
->invitation
, code
, text
, body
);
687 process_invite_call_response(struct sipe_core_private
*sipe_private
,
689 struct transaction
*trans
);
691 struct sipe_media_stream
*
692 sipe_core_media_get_stream_by_id(struct sipe_media_call
*call
, const gchar
*id
)
695 for (i
= SIPE_MEDIA_CALL_PRIVATE
->streams
; i
; i
= i
->next
) {
696 struct sipe_media_stream
*stream
= i
->data
;
697 if (sipe_strequal(stream
->id
, id
))
704 update_call_from_remote_sdp(struct sipe_media_call_private
* call_private
,
705 struct sdpmedia
*media
)
707 struct sipe_media_stream
*stream
;
708 GList
*backend_candidates
= NULL
;
709 GList
*backend_codecs
= NULL
;
711 gboolean result
= TRUE
;
713 stream
= sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL
, media
->name
);
714 if (media
->port
== 0) {
716 sipe_backend_media_stream_end(SIPE_MEDIA_CALL
, stream
);
724 if (sipe_utils_nameval_find(media
->attributes
, "inactive")) {
725 sipe_backend_stream_hold(SIPE_MEDIA_CALL
, stream
, FALSE
);
726 } else if (sipe_backend_stream_is_held(stream
)) {
727 sipe_backend_stream_unhold(SIPE_MEDIA_CALL
, stream
, FALSE
);
730 if (SIPE_MEDIA_STREAM_PRIVATE
->remote_candidates_and_codecs_set
) {
734 for (i
= media
->codecs
; i
; i
= i
->next
) {
735 struct sdpcodec
*c
= i
->data
;
736 struct sipe_backend_codec
*codec
;
739 codec
= sipe_backend_codec_new(c
->id
,
745 for (j
= c
->parameters
; j
; j
= j
->next
) {
746 struct sipnameval
*attr
= j
->data
;
748 sipe_backend_codec_add_optional_parameter(codec
,
753 backend_codecs
= g_list_append(backend_codecs
, codec
);
756 if (media
->encryption_key
&& SIPE_MEDIA_STREAM_PRIVATE
->encryption_key
) {
757 sipe_backend_media_set_encryption_keys(SIPE_MEDIA_CALL
, stream
,
758 SIPE_MEDIA_STREAM_PRIVATE
->encryption_key
,
759 media
->encryption_key
);
760 SIPE_MEDIA_STREAM_PRIVATE
->encryption_key_id
= media
->encryption_key_id
;
763 result
= sipe_backend_set_remote_codecs(SIPE_MEDIA_CALL
, stream
,
765 sipe_media_codec_list_free(backend_codecs
);
767 if (result
== FALSE
) {
768 sipe_backend_media_stream_end(SIPE_MEDIA_CALL
, stream
);
772 for (i
= media
->candidates
; i
; i
= i
->next
) {
773 struct sdpcandidate
*c
= i
->data
;
774 struct sipe_backend_candidate
*candidate
;
775 candidate
= sipe_backend_candidate_new(c
->foundation
,
783 sipe_backend_candidate_set_priority(candidate
, c
->priority
);
785 backend_candidates
= g_list_append(backend_candidates
, candidate
);
788 sipe_backend_media_add_remote_candidates(SIPE_MEDIA_CALL
, stream
,
790 sipe_media_candidate_list_free(backend_candidates
);
792 SIPE_MEDIA_STREAM_PRIVATE
->remote_candidates_and_codecs_set
= TRUE
;
798 apply_remote_message(struct sipe_media_call_private
* call_private
,
803 sipe_utils_slist_free_full(call_private
->failed_media
, (GDestroyNotify
)sdpmedia_free
);
804 call_private
->failed_media
= NULL
;
805 call_private
->encryption_compatible
= TRUE
;
807 for (i
= msg
->media
; i
; i
= i
->next
) {
808 struct sdpmedia
*media
= i
->data
;
809 const gchar
*enc_level
=
810 sipe_utils_nameval_find(media
->attributes
, "encryption");
811 if (sipe_strequal(enc_level
, "rejected") &&
812 get_encryption_policy(call_private
->sipe_private
) == SIPE_ENCRYPTION_POLICY_REQUIRED
) {
813 call_private
->encryption_compatible
= FALSE
;
816 if (!update_call_from_remote_sdp(call_private
, media
)) {
818 call_private
->failed_media
=
819 g_slist_append(call_private
->failed_media
, media
);
823 /* We need to keep failed medias until response is sent, remove them
824 * from sdpmsg that is to be freed. */
825 for (i
= call_private
->failed_media
; i
; i
= i
->next
) {
826 msg
->media
= g_slist_remove(msg
->media
, i
->data
);
831 call_initialized(struct sipe_media_call
*call
)
833 GSList
*streams
= SIPE_MEDIA_CALL_PRIVATE
->streams
;
834 for (; streams
; streams
= streams
->next
) {
835 if (!sipe_backend_stream_initialized(call
, streams
->data
)) {
843 // Sends an invite response when the call is accepted and local candidates were
844 // prepared, otherwise does nothing. If error response is sent, call_private is
845 // disposed before function returns.
847 maybe_send_first_invite_response(struct sipe_media_call_private
*call_private
)
849 struct sipe_backend_media
*backend_media
;
851 backend_media
= call_private
->public.backend_private
;
853 if (!sipe_backend_media_accepted(backend_media
) ||
854 !call_initialized(&call_private
->public))
857 if (!call_private
->encryption_compatible
) {
858 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
860 sipmsg_add_header(call_private
->invitation
, "Warning",
861 "308 lcs.microsoft.com \"Encryption Levels not compatible\"");
862 sip_transport_response(sipe_private
,
863 call_private
->invitation
,
864 488, "Encryption Levels not compatible",
866 sipe_backend_media_reject(backend_media
, FALSE
);
867 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
868 _("Unable to establish a call"),
869 _("Encryption settings of peer are incompatible with ours."));
871 send_response_with_session_description(call_private
, 200, "OK");
872 sipmsg_free(call_private
->invitation
);
873 call_private
->invitation
= NULL
;
878 stream_initialized_cb(struct sipe_media_call
*call
,
879 struct sipe_media_stream
*stream
)
881 if (call_initialized(call
)) {
882 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
884 if (sipe_backend_media_is_initiator(call
, stream
)) {
885 sipe_invite_call(call_private
,
886 process_invite_call_response
);
887 } else if (call_private
->smsg
) {
888 struct sdpmsg
*smsg
= call_private
->smsg
;
889 call_private
->smsg
= NULL
;
891 apply_remote_message(call_private
, smsg
);
892 maybe_send_first_invite_response(call_private
);
898 static void phone_state_publish(struct sipe_core_private
*sipe_private
)
900 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
901 sipe_ocs2007_phone_state_publish(sipe_private
);
903 // TODO: OCS 2005 support. Is anyone still using it at all?
908 sipe_core_media_stream_end(struct sipe_media_stream
*stream
)
910 sipe_media_stream_free(SIPE_MEDIA_STREAM_PRIVATE
);
914 media_end_cb(struct sipe_media_call
*call
)
916 struct sipe_core_private
*sipe_private
;
918 g_return_if_fail(call
);
920 sipe_private
= SIPE_MEDIA_CALL_PRIVATE
->sipe_private
;
922 sipe_media_call_free(SIPE_MEDIA_CALL_PRIVATE
);
923 phone_state_publish(sipe_private
);
927 call_accept_cb(struct sipe_media_call
*call
, gboolean local
)
930 maybe_send_first_invite_response(SIPE_MEDIA_CALL_PRIVATE
);
932 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
);
936 call_reject_cb(struct sipe_media_call
*call
, gboolean local
)
939 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
941 sip_transport_response(call_private
->sipe_private
,
942 call_private
->invitation
,
943 603, "Decline", NULL
);
945 if (call_private
->session
) {
946 sipe_session_remove(call_private
->sipe_private
,
947 call_private
->session
);
948 call_private
->session
= NULL
;
954 av_call_reject_cb(struct sipe_media_call
*call
, gboolean local
)
957 struct sipe_core_private
*sipe_private
;
960 sipe_private
= SIPE_MEDIA_CALL_PRIVATE
->sipe_private
;
962 desc
= g_strdup_printf(_("User %s rejected call"), call
->with
);
963 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, _("Call rejected"),
968 call_reject_cb(call
, local
);
972 sipe_media_send_ack(struct sipe_core_private
*sipe_private
, struct sipmsg
*msg
,
973 struct transaction
*trans
);
975 static void call_hold_cb(struct sipe_media_call
*call
,
977 SIPE_UNUSED_PARAMETER gboolean state
)
980 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE
, sipe_media_send_ack
);
984 static void call_hangup_cb(struct sipe_media_call
*call
, gboolean local
)
987 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
989 if (call_private
->session
) {
990 sipe_session_close(call_private
->sipe_private
,
991 call_private
->session
);
992 call_private
->session
= NULL
;
998 error_cb(struct sipe_media_call
*call
, gchar
*message
)
1000 struct sipe_media_call_private
*call_private
= SIPE_MEDIA_CALL_PRIVATE
;
1001 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
1002 gboolean initiator
= sipe_backend_media_is_initiator(call
, NULL
);
1003 gboolean accepted
= sipe_backend_media_accepted(call
->backend_private
);
1005 gchar
*title
= g_strdup_printf("Call with %s failed", call
->with
);
1006 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, title
, message
);
1009 if (!initiator
&& !accepted
) {
1010 sip_transport_response(sipe_private
,
1011 call_private
->invitation
,
1012 488, "Not Acceptable Here", NULL
);
1016 struct sipe_media_call
*
1017 sipe_media_call_new(struct sipe_core_private
*sipe_private
, const gchar
* with
,
1018 struct sipmsg
*msg
, SipeIceVersion ice_version
,
1019 SipeMediaCallFlags flags
)
1021 struct sipe_media_call_private
*call_private
;
1022 struct sip_session
*session
;
1023 struct sip_dialog
*dialog
;
1026 session
= sipe_session_add_call(sipe_private
, with
);
1028 dialog
= sipe_dialog_add(session
);
1029 dialog
->with
= g_strdup(with
);
1032 gchar
*newTag
= gentag();
1033 const gchar
*oldHeader
;
1036 oldHeader
= sipmsg_find_header(msg
, "To");
1037 newHeader
= g_strdup_printf("%s;tag=%s", oldHeader
, newTag
);
1038 sipmsg_remove_header_now(msg
, "To");
1039 sipmsg_add_header_now(msg
, "To", newHeader
);
1043 dialog
->callid
= g_strdup(sipmsg_find_header(msg
, "Call-ID"));
1044 sipe_dialog_parse(dialog
, msg
, FALSE
);
1046 dialog
->callid
= gencallid();
1047 dialog
->ourtag
= gentag();
1048 flags
|= SIPE_MEDIA_CALL_INITIATOR
;
1051 if (g_hash_table_lookup(sipe_private
->media_calls
, dialog
->callid
)) {
1052 SIPE_DEBUG_ERROR("sipe_media_call_new: call already exists for "
1053 "Call-ID %s", dialog
->callid
);
1054 sipe_session_remove(sipe_private
, session
);
1058 call_private
= g_new0(struct sipe_media_call_private
, 1);
1059 call_private
->sipe_private
= sipe_private
;
1060 call_private
->session
= session
;
1061 SIPE_MEDIA_CALL
->with
= g_strdup(with
);
1063 g_hash_table_insert(sipe_private
->media_calls
,
1064 g_strdup(dialog
->callid
), call_private
);
1066 cname
= g_strdup(sipe_private
->contact
+ 1);
1067 cname
[strlen(cname
) - 1] = '\0';
1069 call_private
->public.backend_private
= sipe_backend_media_new(SIPE_CORE_PUBLIC
,
1073 sipe_backend_media_set_cname(call_private
->public.backend_private
, cname
);
1075 call_private
->ice_version
= ice_version
;
1076 call_private
->encryption_compatible
= TRUE
;
1078 call_private
->public.stream_initialized_cb
= stream_initialized_cb
;
1079 call_private
->public.media_end_cb
= media_end_cb
;
1080 call_private
->public.call_accept_cb
= call_accept_cb
;
1081 call_private
->public.call_reject_cb
= call_reject_cb
;
1082 call_private
->public.call_hold_cb
= call_hold_cb
;
1083 call_private
->public.call_hangup_cb
= call_hangup_cb
;
1084 call_private
->public.error_cb
= error_cb
;
1088 return SIPE_MEDIA_CALL
;
1091 void sipe_media_hangup(struct sipe_media_call_private
*call_private
)
1094 sipe_backend_media_hangup(call_private
->public.backend_private
,
1100 ssrc_range_compare(const struct ssrc_range
*a
, const struct ssrc_range
*b
)
1102 if (a
->begin
< b
->begin
) {
1105 if (a
->begin
> b
->begin
) {
1112 ssrc_range_update(GSList
**ranges
, GSList
*media
)
1114 for (; media
; media
= media
->next
) {
1116 const char *ssrc_range
;
1120 ssrc_range
= sipe_utils_nameval_find(m
->attributes
,
1126 parts
= g_strsplit(ssrc_range
, "-", 2);
1128 if (parts
[0] && parts
[1]) {
1129 struct ssrc_range
*range
;
1131 range
= g_new0(struct ssrc_range
, 1);
1132 range
->begin
= atoi(parts
[0]);
1133 range
->end
= atoi(parts
[1]);
1135 *ranges
= sipe_utils_slist_insert_unique_sorted(
1137 (GCompareFunc
)ssrc_range_compare
,
1145 static struct ssrc_range
*
1146 ssrc_range_allocate(GSList
**ranges
, guint32 len
)
1148 struct ssrc_range
*range
;
1151 range
= g_new0(struct ssrc_range
, 1);
1153 range
->end
= range
->begin
+ (len
- 1);
1155 for (i
= *ranges
; i
; i
= i
->next
) {
1156 struct ssrc_range
*r
= i
->data
;
1158 if (range
->begin
< r
->begin
&& range
->end
< r
->begin
) {
1162 range
->begin
= r
->end
+ 1;
1163 range
->end
= range
->begin
+ (len
- 1);
1166 /* As per [MS-SDPEXT] 3.1.5.31.1, a SSRC MUST be from 1 to 4294967040
1168 if (range
->begin
> range
->end
|| range
->end
> 0xFFFFFF00) {
1170 SIPE_DEBUG_ERROR("Couldn't allocate SSRC range of %u", len
);
1174 *ranges
= g_slist_insert_sorted(*ranges
, range
,
1175 (GCompareFunc
)ssrc_range_compare
);
1180 struct sipe_media_stream
*
1181 sipe_media_stream_add(struct sipe_media_call
*call
, const gchar
*id
,
1182 SipeMediaType type
, SipeIceVersion ice_version
,
1183 gboolean initiator
, guint32 ssrc_count
)
1185 struct sipe_core_private
*sipe_private
;
1186 struct sipe_media_stream_private
*stream_private
;
1187 struct sipe_backend_media_relays
*backend_media_relays
;
1191 sipe_private
= SIPE_MEDIA_CALL_PRIVATE
->sipe_private
;
1193 backend_media_relays
= sipe_backend_media_relays_convert(
1194 sipe_private
->media_relays
,
1195 sipe_private
->media_relay_username
,
1196 sipe_private
->media_relay_password
);
1198 min_port
= sipe_private
->min_media_port
;
1199 max_port
= sipe_private
->max_media_port
;
1201 case SIPE_MEDIA_AUDIO
:
1202 min_port
= sipe_private
->min_audio_port
;
1203 max_port
= sipe_private
->max_audio_port
;
1205 case SIPE_MEDIA_VIDEO
:
1206 min_port
= sipe_private
->min_video_port
;
1207 max_port
= sipe_private
->max_audio_port
;
1209 case SIPE_MEDIA_APPLICATION
:
1210 if (sipe_strequal(id
, "data")) {
1211 min_port
= sipe_private
->min_filetransfer_port
;
1212 max_port
= sipe_private
->max_filetransfer_port
;
1213 } else if (sipe_strequal(id
, "applicationsharing")) {
1214 min_port
= sipe_private
->min_appsharing_port
;
1215 max_port
= sipe_private
->max_appsharing_port
;
1220 stream_private
= g_new0(struct sipe_media_stream_private
, 1);
1221 SIPE_MEDIA_STREAM
->call
= call
;
1222 SIPE_MEDIA_STREAM
->id
= g_strdup(id
);
1223 stream_private
->write_queue
= g_queue_new();
1224 stream_private
->async_reads
= g_queue_new();
1226 if (ssrc_count
> 0) {
1227 SIPE_MEDIA_STREAM
->ssrc_range
=
1228 ssrc_range_allocate(&SIPE_MEDIA_CALL_PRIVATE
->ssrc_ranges
,
1232 SIPE_MEDIA_STREAM
->backend_private
=
1233 sipe_backend_media_add_stream(SIPE_MEDIA_STREAM
,
1236 backend_media_relays
,
1237 min_port
, max_port
);
1239 sipe_backend_media_relays_free(backend_media_relays
);
1241 if (!SIPE_MEDIA_STREAM
->backend_private
) {
1242 sipe_media_stream_free(stream_private
);
1246 if (type
== SIPE_MEDIA_VIDEO
) {
1247 /* Declare that we can send and receive Video Source Requests
1248 * as per [MS-SDPEXT] 3.1.5.30.2. */
1249 sipe_media_stream_add_extra_attribute(SIPE_MEDIA_STREAM
,
1250 "rtcp-fb", "* x-message app send:src recv:src");
1252 sipe_media_stream_add_extra_attribute(SIPE_MEDIA_STREAM
,
1253 "rtcp-rsize", NULL
);
1254 sipe_media_stream_add_extra_attribute(SIPE_MEDIA_STREAM
,
1255 "label", "main-video");
1256 sipe_media_stream_add_extra_attribute(SIPE_MEDIA_STREAM
,
1257 "x-source", "main-video");
1261 if (get_encryption_policy(sipe_private
) != SIPE_ENCRYPTION_POLICY_REJECTED
) {
1263 stream_private
->encryption_key
= g_new0(guchar
, SIPE_SRTP_KEY_LEN
);
1264 for (i
= 0; i
!= SIPE_SRTP_KEY_LEN
; ++i
) {
1265 stream_private
->encryption_key
[i
] = rand() & 0xff;
1267 stream_private
->encryption_key_id
= 1;
1271 SIPE_MEDIA_CALL_PRIVATE
->streams
=
1272 g_slist_append(SIPE_MEDIA_CALL_PRIVATE
->streams
,
1275 return SIPE_MEDIA_STREAM
;
1279 append_2007_fallback_if_needed(struct sipe_media_call_private
*call_private
)
1281 struct sipe_core_private
*sipe_private
= call_private
->sipe_private
;
1282 const gchar
*marker
= sip_transport_sdp_address_marker(sipe_private
);
1283 const gchar
*ip
= sip_transport_ip_address(sipe_private
);
1286 if (SIPE_CORE_PRIVATE_FLAG_IS(SFB
) ||
1287 sipe_media_get_sip_dialog(SIPE_MEDIA_CALL
)->cseq
!= 0 ||
1288 call_private
->ice_version
!= SIPE_ICE_RFC_5245
||
1289 sipe_strequal(SIPE_MEDIA_CALL
->with
, sipe_private
->test_call_bot_uri
)) {
1293 body
= g_strdup_printf("Content-Type: application/sdp\r\n"
1294 "Content-Transfer-Encoding: 7bit\r\n"
1295 "Content-Disposition: session; handling=optional; ms-proxy-2007fallback\r\n"
1297 "o=- 0 0 IN %s %s\r\n"
1300 "m=audio 0 RTP/AVP\r\n",
1303 sipe_media_add_extra_invite_section(SIPE_MEDIA_CALL
,
1304 "multipart/alternative", body
);
1308 sipe_media_initiate_call(struct sipe_core_private
*sipe_private
,
1309 const char *with
, SipeIceVersion ice_version
,
1310 gboolean with_video
)
1312 struct sipe_media_call_private
*call_private
;
1314 if (sipe_core_media_get_call(SIPE_CORE_PUBLIC
)) {
1318 call_private
= (struct sipe_media_call_private
*)
1319 sipe_media_call_new(sipe_private
, with
, NULL
,
1322 SIPE_MEDIA_CALL
->call_reject_cb
= av_call_reject_cb
;
1324 if (!sipe_media_stream_add(SIPE_MEDIA_CALL
, "audio", SIPE_MEDIA_AUDIO
,
1325 call_private
->ice_version
,
1327 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
1328 _("Error occurred"),
1329 _("Error creating audio stream"));
1330 sipe_media_hangup(call_private
);
1335 !sipe_media_stream_add(SIPE_MEDIA_CALL
, "video", SIPE_MEDIA_VIDEO
,
1336 call_private
->ice_version
,
1337 TRUE
, VIDEO_SSRC_COUNT
)) {
1338 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
1339 _("Error occurred"),
1340 _("Error creating video stream"));
1341 sipe_media_hangup(call_private
);
1345 append_2007_fallback_if_needed(call_private
);
1347 // Processing continues in stream_initialized_cb
1351 sipe_core_media_initiate_call(struct sipe_core_public
*sipe_public
,
1353 gboolean with_video
)
1355 sipe_media_initiate_call(SIPE_CORE_PRIVATE
, with
,
1356 SIPE_ICE_RFC_5245
, with_video
);
1360 conference_audio_muted_cb(struct sipe_media_stream
*stream
, gboolean is_muted
)
1362 struct sipe_media_call
*call
= stream
->call
;
1364 if (!SIPE_MEDIA_CALL_PRIVATE
->conference_session
) {
1368 sipe_conf_announce_audio_mute_state(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
,
1369 SIPE_MEDIA_CALL_PRIVATE
->conference_session
,
1373 void sipe_core_media_connect_conference(struct sipe_core_public
*sipe_public
,
1374 struct sipe_chat_session
*chat_session
)
1376 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1377 struct sipe_media_call_private
*call_private
;
1378 struct sipe_media_stream
*stream
;
1379 struct sip_session
*session
;
1380 SipeIceVersion ice_version
;
1383 if (!sipe_conf_supports_mcu_type(sipe_private
, "audio-video")) {
1384 sipe_backend_notify_error(sipe_public
, _("Join conference call"),
1385 _("Conference calls are not supported on this server."));
1389 session
= sipe_session_find_chat(sipe_private
, chat_session
);
1391 if (sipe_core_media_get_call(sipe_public
) || !session
) {
1395 av_uri
= sipe_conf_build_uri(sipe_core_chat_id(sipe_public
, chat_session
),
1401 session
->is_call
= TRUE
;
1403 ice_version
= SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013
) ? SIPE_ICE_RFC_5245
:
1406 call_private
= (struct sipe_media_call_private
*)
1407 sipe_media_call_new(sipe_private
, av_uri
, NULL
,
1409 call_private
->conference_session
= session
;
1410 SIPE_MEDIA_CALL
->call_reject_cb
= av_call_reject_cb
;
1412 stream
= sipe_media_stream_add(SIPE_MEDIA_CALL
, "audio",
1414 call_private
->ice_version
,
1417 sipe_backend_notify_error(sipe_public
,
1418 _("Error occurred"),
1419 _("Error creating audio stream"));
1421 sipe_media_hangup(call_private
);
1424 stream
->mute_cb
= conference_audio_muted_cb
;
1428 // Processing continues in stream_initialized_cb
1431 struct sipe_media_call
*
1432 sipe_core_media_get_call(struct sipe_core_public
*sipe_public
)
1434 struct sipe_media_call
* result
= NULL
;
1435 GList
*calls
= g_hash_table_get_values(SIPE_CORE_PRIVATE
->media_calls
);
1436 GList
*entry
= calls
;
1439 if (sipe_core_media_get_stream_by_id(entry
->data
, "audio")) {
1440 result
= entry
->data
;
1443 entry
= entry
->next
;
1450 static gboolean
phone_number_is_valid(const gchar
*phone_number
)
1452 if (!phone_number
|| sipe_strequal(phone_number
, "")) {
1456 if (*phone_number
== '+') {
1460 while (*phone_number
!= '\0') {
1461 if (!g_ascii_isdigit(*phone_number
)) {
1470 void sipe_core_media_phone_call(struct sipe_core_public
*sipe_public
,
1471 const gchar
*phone_number
)
1473 g_return_if_fail(sipe_public
);
1475 if (phone_number_is_valid(phone_number
)) {
1476 gchar
*phone_uri
= g_strdup_printf("sip:%s@%s;user=phone",
1477 phone_number
, sipe_public
->sip_domain
);
1479 sipe_core_media_initiate_call(sipe_public
, phone_uri
, FALSE
);
1483 sipe_backend_notify_error(sipe_public
,
1484 _("Unable to establish a call"),
1485 _("Invalid phone number"));
1489 void sipe_core_media_test_call(struct sipe_core_public
*sipe_public
)
1491 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1492 if (!sipe_private
->test_call_bot_uri
) {
1493 sipe_backend_notify_error(sipe_public
,
1494 _("Unable to establish a call"),
1495 _("Audio Test Service is not available."));
1499 sipe_core_media_initiate_call(sipe_public
,
1500 sipe_private
->test_call_bot_uri
, FALSE
);
1503 static struct sipe_media_call_private
*
1504 sipe_media_from_sipmsg(struct sipe_core_private
*sipe_private
,
1507 return g_hash_table_lookup(sipe_private
->media_calls
,
1508 sipmsg_find_header(msg
, "Call-ID"));
1512 transport_response_unsupported_sdp(struct sipe_core_private
*sipe_private
,
1515 sipmsg_add_header(msg
, "ms-client-diagnostics",
1516 "52063;reason=\"Unsupported session description\"");
1517 sip_transport_response(sipe_private
, msg
,
1518 488, "Not Acceptable Here", NULL
);
1522 maybe_send_second_invite_response(struct sipe_media_call_private
*call_private
)
1526 /* Second INVITE request had to be received and all streams must have
1527 * established candidate pairs before the response can be sent. */
1529 if (!call_private
->invitation
) {
1533 for (it
= call_private
->streams
; it
; it
= it
->next
) {
1534 struct sipe_media_stream_private
*stream_private
= it
->data
;
1535 if (!stream_private
->established
) {
1540 send_response_with_session_description(call_private
, 200, "OK");
1543 for (it
= call_private
->streams
; it
; it
= it
->next
) {
1544 struct sipe_media_stream_private
*stream_private
= it
->data
;
1546 stream_private
->sdp_negotiation_concluded
= TRUE
;
1547 if (stream_private
->writable
) {
1548 // We've become writable.
1549 sipe_core_media_stream_writable(SIPE_MEDIA_STREAM
, TRUE
);
1555 struct sipe_media_call
*
1556 process_incoming_invite_call(struct sipe_core_private
*sipe_private
,
1560 return(process_incoming_invite_call_parsed_sdp(sipe_private
,
1562 sdpmsg_parse_msg(sdp
)));
1565 struct sipe_media_call
*
1566 process_incoming_invite_call_parsed_sdp(struct sipe_core_private
*sipe_private
,
1568 struct sdpmsg
*smsg
)
1570 struct sipe_media_call_private
*call_private
;
1571 gboolean has_new_media
= FALSE
;
1574 // Don't allow two voice calls in parallel.
1575 if (!strstr(msg
->body
, "m=data") &&
1576 !strstr(msg
->body
, "m=applicationsharing")) {
1577 struct sipe_media_call
*call
=
1578 sipe_core_media_get_call(SIPE_CORE_PUBLIC
);
1579 if (call
&& !is_media_session_msg(SIPE_MEDIA_CALL_PRIVATE
, msg
)) {
1580 sip_transport_response(sipe_private
, msg
,
1581 486, "Busy Here", NULL
);
1587 call_private
= sipe_media_from_sipmsg(sipe_private
, msg
);
1590 char *self
= sip_uri_self(sipe_private
);
1591 if (sipe_strequal(SIPE_MEDIA_CALL
->with
, self
)) {
1593 sip_transport_response(sipe_private
, msg
, 488, "Not Acceptable Here", NULL
);
1601 transport_response_unsupported_sdp(sipe_private
, msg
);
1603 sipe_media_hangup(call_private
);
1608 if (!call_private
) {
1609 gchar
*with
= parse_from(sipmsg_find_header(msg
, "From"));
1610 SipeMediaCallFlags flags
= 0;
1612 if (strstr(msg
->body
, "m=data") ||
1613 strstr(msg
->body
, "m=applicationsharing")) {
1614 flags
|= SIPE_MEDIA_CALL_NO_UI
;
1617 call_private
= (struct sipe_media_call_private
*)
1618 sipe_media_call_new(sipe_private
, with
,
1619 msg
, smsg
->ice_version
,
1622 if (!(flags
& SIPE_MEDIA_CALL_NO_UI
)) {
1623 SIPE_MEDIA_CALL
->call_reject_cb
= av_call_reject_cb
;
1628 if (call_private
->invitation
)
1629 sipmsg_free(call_private
->invitation
);
1630 call_private
->invitation
= sipmsg_copy(msg
);
1632 ssrc_range_update(&call_private
->ssrc_ranges
, smsg
->media
);
1634 // Create any new media streams
1635 for (i
= smsg
->media
; i
; i
= i
->next
) {
1636 struct sdpmedia
*media
= i
->data
;
1637 gchar
*id
= media
->name
;
1640 if ( media
->port
!= 0
1641 && !sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL
, id
)) {
1642 guint32 ssrc_count
= 0;
1644 if (sipe_strequal(id
, "audio"))
1645 type
= SIPE_MEDIA_AUDIO
;
1646 else if (sipe_strequal(id
, "video")) {
1647 type
= SIPE_MEDIA_VIDEO
;
1648 ssrc_count
= VIDEO_SSRC_COUNT
;
1649 } else if (sipe_strequal(id
, "data"))
1650 type
= SIPE_MEDIA_APPLICATION
;
1651 else if (sipe_strequal(id
, "applicationsharing"))
1652 type
= SIPE_MEDIA_APPLICATION
;
1656 sipe_media_stream_add(SIPE_MEDIA_CALL
, id
, type
,
1657 smsg
->ice_version
, FALSE
,
1659 has_new_media
= TRUE
;
1663 if (has_new_media
) {
1664 sdpmsg_free(call_private
->smsg
);
1665 call_private
->smsg
= smsg
;
1666 sip_transport_response(sipe_private
, call_private
->invitation
,
1667 180, "Ringing", NULL
);
1668 // Processing continues in stream_initialized_cb
1670 apply_remote_message(call_private
, smsg
);
1672 maybe_send_second_invite_response(call_private
);
1675 return SIPE_MEDIA_CALL
;
1678 void process_incoming_cancel_call(struct sipe_media_call_private
*call_private
,
1681 // We respond to the CANCEL request with 200 OK response and
1682 // with 487 Request Terminated to the remote INVITE in progress.
1683 sip_transport_response(call_private
->sipe_private
, msg
, 200, "OK", NULL
);
1685 if (call_private
->invitation
) {
1686 sip_transport_response(call_private
->sipe_private
,
1687 call_private
->invitation
,
1688 487, "Request Terminated", NULL
);
1691 sipe_backend_media_reject(SIPE_MEDIA_CALL
->backend_private
, FALSE
);
1695 sipe_media_send_ack(struct sipe_core_private
*sipe_private
,
1697 struct transaction
*trans
)
1699 struct sipe_media_call_private
*call_private
;
1700 struct sip_dialog
*dialog
;
1703 call_private
= sipe_media_from_sipmsg(sipe_private
, msg
);
1705 if (!is_media_session_msg(call_private
, msg
))
1708 dialog
= sipe_media_get_sip_dialog(SIPE_MEDIA_CALL
);
1712 tmp_cseq
= dialog
->cseq
;
1714 dialog
->cseq
= sip_transaction_cseq(trans
) - 1;
1715 sip_transport_ack(sipe_private
, dialog
);
1716 dialog
->cseq
= tmp_cseq
;
1718 dialog
->outgoing_invite
= NULL
;
1724 sipe_media_send_final_ack(struct sipe_core_private
*sipe_private
,
1726 struct transaction
*trans
)
1728 struct sipe_media_call_private
*call_private
;
1733 if (!sipe_media_send_ack(sipe_private
, msg
, trans
))
1736 call_private
= sipe_media_from_sipmsg(sipe_private
, msg
);
1738 sipe_backend_media_accept(SIPE_MEDIA_CALL
->backend_private
, FALSE
);
1741 for (it
= call_private
->streams
; it
; it
= it
->next
) {
1742 struct sipe_media_stream_private
*stream_private
= it
->data
;
1744 stream_private
->sdp_negotiation_concluded
= TRUE
;
1745 if (stream_private
->writable
) {
1746 // We've become writable.
1747 sipe_core_media_stream_writable(SIPE_MEDIA_STREAM
, TRUE
);
1756 sipe_core_media_stream_candidate_pair_established(struct sipe_media_stream
*stream
)
1758 struct sipe_media_call
*call
= stream
->call
;
1760 GList
*active_candidates
=
1761 sipe_backend_media_stream_get_active_local_candidates(stream
);
1762 guint ready_components
= g_list_length(active_candidates
);
1764 sipe_media_candidate_list_free(active_candidates
);
1766 if (ready_components
!= 2) {
1767 // We must have both RTP+RTCP candidate pairs established first.
1771 if (SIPE_MEDIA_STREAM_PRIVATE
->established
) {
1774 SIPE_MEDIA_STREAM_PRIVATE
->established
= TRUE
;
1776 if (stream
->candidate_pairs_established_cb
) {
1777 stream
->candidate_pairs_established_cb(stream
);
1780 if (sipe_backend_media_is_initiator(stream
->call
, NULL
)) {
1781 GSList
*streams
= SIPE_MEDIA_CALL_PRIVATE
->streams
;
1782 for (; streams
; streams
= streams
->next
) {
1783 struct sipe_media_stream_private
*s
= streams
->data
;
1784 if (!s
->established
) {
1789 if (streams
== NULL
) {
1790 // All call streams have been established.
1791 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE
,
1792 sipe_media_send_final_ack
);
1795 maybe_send_second_invite_response(SIPE_MEDIA_CALL_PRIVATE
);
1800 maybe_retry_call_with_ice_version(struct sipe_core_private
*sipe_private
,
1801 struct sipe_media_call_private
*call_private
,
1802 SipeIceVersion ice_version
,
1803 struct transaction
*trans
)
1805 if (call_private
->ice_version
!= ice_version
&&
1806 sip_transaction_cseq(trans
) == 1) {
1809 gboolean with_video
= FALSE
;
1811 for (i
= call_private
->streams
; i
; i
= i
->next
) {
1812 struct sipe_media_stream
*stream
= i
->data
;
1814 if (sipe_strequal(stream
->id
, "video")) {
1816 } else if (!sipe_strequal(stream
->id
, "audio")) {
1817 /* Don't retry calls which are neither audio
1823 with
= g_strdup(SIPE_MEDIA_CALL
->with
);
1825 sipe_media_hangup(call_private
);
1826 SIPE_DEBUG_INFO("Retrying call with ICEv%d.",
1827 ice_version
== SIPE_ICE_DRAFT_6
? 6 : 19);
1828 sipe_media_initiate_call(sipe_private
,
1841 process_invite_call_response(struct sipe_core_private
*sipe_private
,
1843 struct transaction
*trans
)
1846 struct sipe_media_call_private
*call_private
;
1847 struct sip_dialog
*dialog
;
1848 struct sdpmsg
*smsg
;
1850 call_private
= sipe_media_from_sipmsg(sipe_private
,msg
);
1852 if (!is_media_session_msg(call_private
, msg
))
1855 dialog
= sipe_media_get_sip_dialog(SIPE_MEDIA_CALL
);
1857 with
= dialog
->with
;
1859 dialog
->outgoing_invite
= NULL
;
1861 if (msg
->response
== 603 || msg
->response
== 605) {
1862 // Call rejected by remote peer
1863 sipe_media_send_ack(sipe_private
, msg
, trans
);
1864 sipe_backend_media_reject(SIPE_MEDIA_CALL
->backend_private
, FALSE
);
1869 if (msg
->response
>= 400) {
1870 // An error occurred
1872 GString
*desc
= g_string_new("");
1873 gboolean append_responsestr
= FALSE
;
1875 switch (msg
->response
) {
1877 title
= _("User unavailable");
1879 if (sipmsg_parse_warning(msg
, NULL
) == 391) {
1880 g_string_append_printf(desc
, _("%s does not want to be disturbed"), with
);
1882 g_string_append_printf(desc
, _("User %s is not available"), with
);
1886 // OCS/Lync really sends response string with 'Mutipart' typo.
1887 if (sipe_strequal(msg
->responsestr
, "Mutipart mime in content type not supported by Archiving CDR service") &&
1888 maybe_retry_call_with_ice_version(sipe_private
,
1894 title
= _("Unsupported media type");
1897 /* Check for incompatible encryption levels error.
1900 * 488 Not Acceptable Here
1901 * ms-client-diagnostics: 52017;reason="Encryption levels dont match"
1903 * older clients (and SIPE itself):
1904 * 488 Encryption Levels not compatible
1906 const gchar
*ms_diag
= sipmsg_find_header(msg
, "ms-client-diagnostics");
1907 SipeIceVersion retry_ice_version
= SIPE_ICE_DRAFT_6
;
1909 if (sipe_strequal(msg
->responsestr
, "Encryption Levels not compatible") ||
1910 (ms_diag
&& g_str_has_prefix(ms_diag
, "52017;"))) {
1911 title
= _("Unable to establish a call");
1912 g_string_append(desc
, _("Encryption settings of peer are incompatible with ours."));
1916 /* Check if this is failed conference using
1917 * ICEv6 with reason "Error parsing SDP" and
1918 * retry using ICEv19. */
1919 ms_diag
= sipmsg_find_header(msg
, "ms-diagnostics");
1920 if (ms_diag
&& g_str_has_prefix(ms_diag
, "7008;")) {
1921 retry_ice_version
= SIPE_ICE_RFC_5245
;
1924 if (maybe_retry_call_with_ice_version(sipe_private
,
1933 title
= _("Error occurred");
1934 g_string_append(desc
, _("Unable to establish a call"));
1935 append_responsestr
= TRUE
;
1939 if (append_responsestr
) {
1940 gchar
*reason
= sipmsg_get_ms_diagnostics_reason(msg
);
1942 g_string_append_printf(desc
, "\n%d %s",
1943 msg
->response
, msg
->responsestr
);
1945 g_string_append_printf(desc
, "\n\n%s", reason
);
1950 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, title
, desc
->str
);
1951 g_string_free(desc
, TRUE
);
1953 sipe_media_send_ack(sipe_private
, msg
, trans
);
1954 sipe_media_hangup(call_private
);
1959 sipe_dialog_parse(dialog
, msg
, TRUE
);
1960 smsg
= sdpmsg_parse_msg(msg
->body
);
1962 transport_response_unsupported_sdp(sipe_private
, msg
);
1963 sipe_media_hangup(call_private
);
1967 ssrc_range_update(&call_private
->ssrc_ranges
, smsg
->media
);
1968 apply_remote_message(call_private
, smsg
);
1971 sipe_media_send_ack(sipe_private
, msg
, trans
);
1975 // Waits until sipe_core_media_candidate_pair_established() is invoked.
1978 gboolean
is_media_session_msg(struct sipe_media_call_private
*call_private
,
1981 if (!call_private
) {
1985 return sipe_media_from_sipmsg(call_private
->sipe_private
, msg
) == call_private
;
1989 end_call(SIPE_UNUSED_PARAMETER gpointer key
,
1990 struct sipe_media_call_private
*call_private
,
1991 SIPE_UNUSED_PARAMETER gpointer user_data
)
1993 struct sipe_backend_media
*backend_private
;
1995 backend_private
= call_private
->public.backend_private
;
1997 if (!sipe_backend_media_is_initiator(SIPE_MEDIA_CALL
, NULL
) &&
1998 !sipe_backend_media_accepted(backend_private
)) {
1999 sip_transport_response(call_private
->sipe_private
,
2000 call_private
->invitation
,
2001 480, "Temporarily Unavailable", NULL
);
2002 } else if (call_private
->session
) {
2003 sipe_session_close(call_private
->sipe_private
,
2004 call_private
->session
);
2005 call_private
->session
= NULL
;
2008 sipe_media_hangup(call_private
);
2012 sipe_media_handle_going_offline(struct sipe_core_private
*sipe_private
)
2014 g_hash_table_foreach(sipe_private
->media_calls
, (GHFunc
) end_call
, NULL
);
2017 gboolean
sipe_media_is_conference_call(struct sipe_media_call_private
*call_private
)
2019 return g_strstr_len(SIPE_MEDIA_CALL
->with
, -1, "app:conf:audio-video:") != NULL
;
2022 struct sipe_core_private
*
2023 sipe_media_get_sipe_core_private(struct sipe_media_call
*call
)
2025 g_return_val_if_fail(call
, NULL
);
2027 return SIPE_MEDIA_CALL_PRIVATE
->sipe_private
;
2031 sipe_media_get_sip_dialog(struct sipe_media_call
*call
)
2033 struct sip_session
*session
;
2035 g_return_val_if_fail(call
, NULL
);
2037 session
= SIPE_MEDIA_CALL_PRIVATE
->session
;
2039 if (!session
|| !session
->dialogs
) {
2043 return session
->dialogs
->data
;
2047 sipe_media_relay_free(struct sipe_media_relay
*relay
)
2049 g_free(relay
->hostname
);
2050 if (relay
->dns_query
)
2051 sipe_backend_dns_query_cancel(relay
->dns_query
);
2056 sipe_media_relay_list_free(GSList
*list
)
2058 for (; list
; list
= g_slist_delete_link(list
, list
))
2059 sipe_media_relay_free(list
->data
);
2063 relay_ip_resolved_cb(struct sipe_media_relay
* relay
,
2064 const gchar
*ip
, SIPE_UNUSED_PARAMETER guint port
)
2066 gchar
*hostname
= relay
->hostname
;
2067 relay
->dns_query
= NULL
;
2070 relay
->hostname
= g_strdup(ip
);
2071 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname
, ip
);
2073 relay
->hostname
= NULL
;
2074 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname
);
2081 process_get_av_edge_credentials_response(struct sipe_core_private
*sipe_private
,
2083 SIPE_UNUSED_PARAMETER
struct transaction
*trans
)
2085 g_free(sipe_private
->media_relay_username
);
2086 g_free(sipe_private
->media_relay_password
);
2087 sipe_media_relay_list_free(sipe_private
->media_relays
);
2088 sipe_private
->media_relay_username
= NULL
;
2089 sipe_private
->media_relay_password
= NULL
;
2090 sipe_private
->media_relays
= NULL
;
2092 if (msg
->response
>= 400) {
2093 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
2094 "Failed to obtain A/V Edge credentials.");
2098 if (msg
->response
== 200) {
2099 sipe_xml
*xn_response
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
2101 if (sipe_strequal("OK", sipe_xml_attribute(xn_response
, "reasonPhrase"))) {
2102 const sipe_xml
*xn_credentials
= sipe_xml_child(xn_response
, "credentialsResponse/credentials");
2103 const sipe_xml
*xn_relays
= sipe_xml_child(xn_response
, "credentialsResponse/mediaRelayList");
2104 const sipe_xml
*item
;
2105 GSList
*relays
= NULL
;
2107 item
= sipe_xml_child(xn_credentials
, "username");
2108 sipe_private
->media_relay_username
= sipe_xml_data(item
);
2109 item
= sipe_xml_child(xn_credentials
, "password");
2110 sipe_private
->media_relay_password
= sipe_xml_data(item
);
2112 for (item
= sipe_xml_child(xn_relays
, "mediaRelay"); item
; item
= sipe_xml_twin(item
)) {
2113 struct sipe_media_relay
*relay
= g_new0(struct sipe_media_relay
, 1);
2114 const sipe_xml
*node
;
2117 node
= sipe_xml_child(item
, "hostName");
2118 relay
->hostname
= sipe_xml_data(node
);
2120 node
= sipe_xml_child(item
, "udpPort");
2122 tmp
= sipe_xml_data(node
);
2124 relay
->udp_port
= atoi(tmp
);
2129 node
= sipe_xml_child(item
, "tcpPort");
2131 tmp
= sipe_xml_data(node
);
2133 relay
->tcp_port
= atoi(tmp
);
2138 relays
= g_slist_append(relays
, relay
);
2140 relay
->dns_query
= sipe_backend_dns_query_a(
2144 (sipe_dns_resolved_cb
) relay_ip_resolved_cb
,
2147 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
2149 relay
->tcp_port
, relay
->udp_port
);
2152 sipe_private
->media_relays
= relays
;
2155 sipe_xml_free(xn_response
);
2162 sipe_media_get_av_edge_credentials(struct sipe_core_private
*sipe_private
)
2164 // TODO: re-request credentials after duration expires?
2165 static const char CRED_REQUEST_XML
[] =
2166 "<request requestID=\"%d\" "
2170 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
2171 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
2172 "<credentialsRequest credentialsRequestID=\"%d\">"
2173 "<identity>%s</identity>"
2174 "<location>%s</location>"
2175 "<duration>480</duration>"
2176 "</credentialsRequest>"
2179 int request_id
= rand();
2183 if (!sipe_private
->mras_uri
)
2186 self
= sip_uri_self(sipe_private
);
2188 body
= g_strdup_printf(
2192 sipe_private
->mras_uri
,
2195 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER
) ? "internet" : "intranet");
2198 sip_transport_service(sipe_private
,
2199 sipe_private
->mras_uri
,
2200 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
2202 process_get_av_edge_credentials_response
);
2208 sipe_media_add_extra_invite_section(struct sipe_media_call
*call
,
2209 const gchar
*invite_content_type
,
2212 g_free(SIPE_MEDIA_CALL_PRIVATE
->extra_invite_section
);
2213 g_free(SIPE_MEDIA_CALL_PRIVATE
->invite_content_type
);
2214 SIPE_MEDIA_CALL_PRIVATE
->extra_invite_section
= body
;
2215 SIPE_MEDIA_CALL_PRIVATE
->invite_content_type
=
2216 g_strdup(invite_content_type
);
2220 sipe_media_stream_add_extra_attribute(struct sipe_media_stream
*stream
,
2221 const gchar
*name
, const gchar
*value
)
2223 SIPE_MEDIA_STREAM_PRIVATE
->extra_sdp
=
2224 sipe_utils_nameval_add(SIPE_MEDIA_STREAM_PRIVATE
->extra_sdp
,
2230 sipe_core_media_stream_readable(struct sipe_media_stream
*stream
)
2232 g_return_if_fail(stream
);
2234 if (g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE
->async_reads
) &&
2236 stream
->read_cb(stream
);
2239 while (!g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE
->async_reads
)) {
2240 struct async_read_data
*data
;
2245 data
= g_queue_peek_head(SIPE_MEDIA_STREAM_PRIVATE
->async_reads
);
2246 pos
= data
->buffer
+ SIPE_MEDIA_STREAM_PRIVATE
->read_pos
;
2247 len
= data
->len
- SIPE_MEDIA_STREAM_PRIVATE
->read_pos
;
2249 bytes_read
= sipe_backend_media_stream_read(stream
, pos
, len
);
2250 if (bytes_read
== -1) {
2251 struct sipe_media_call
*call
= stream
->call
;
2252 struct sipe_core_private
*sipe_private
=
2253 SIPE_MEDIA_CALL_PRIVATE
->sipe_private
;
2255 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
2257 _("Error while reading from stream"));
2258 sipe_media_hangup(SIPE_MEDIA_CALL_PRIVATE
);
2262 SIPE_MEDIA_STREAM_PRIVATE
->read_pos
+= bytes_read
;
2264 if (SIPE_MEDIA_STREAM_PRIVATE
->read_pos
== data
->len
) {
2265 data
->callback(stream
, data
->buffer
, data
->len
);
2266 SIPE_MEDIA_STREAM_PRIVATE
->read_pos
= 0;
2267 g_queue_pop_head(SIPE_MEDIA_STREAM_PRIVATE
->async_reads
);
2270 // Still not enough data to finish the read.
2277 sipe_media_stream_read_async(struct sipe_media_stream
*stream
,
2278 gpointer buffer
, gsize len
,
2279 sipe_media_stream_read_callback callback
)
2281 struct async_read_data
*data
;
2283 g_return_if_fail(stream
&& buffer
&& callback
);
2285 data
= g_new0(struct async_read_data
, 1);
2286 data
->buffer
= buffer
;
2288 data
->callback
= callback
;
2290 g_queue_push_tail(SIPE_MEDIA_STREAM_PRIVATE
->async_reads
, data
);
2294 stream_append_buffer(struct sipe_media_stream
*stream
,
2295 guint8
*buffer
, guint len
)
2297 GByteArray
*b
= g_byte_array_sized_new(len
);
2298 g_byte_array_append(b
, buffer
, len
);
2299 g_queue_push_tail(SIPE_MEDIA_STREAM_PRIVATE
->write_queue
, b
);
2303 sipe_media_stream_write(struct sipe_media_stream
*stream
,
2304 gpointer buffer
, gsize len
)
2306 if (!sipe_media_stream_is_writable(stream
)) {
2307 stream_append_buffer(stream
, buffer
, len
);
2312 written
= sipe_backend_media_stream_write(stream
, buffer
, len
);
2313 if (written
== len
) {
2317 stream_append_buffer(stream
,
2318 (guint8
*)buffer
+ written
, len
- written
);
2324 sipe_core_media_stream_writable(struct sipe_media_stream
*stream
,
2327 SIPE_MEDIA_STREAM_PRIVATE
->writable
= writable
;
2333 while (!g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE
->write_queue
)) {
2337 b
= g_queue_peek_head(SIPE_MEDIA_STREAM_PRIVATE
->write_queue
);
2339 written
= sipe_backend_media_stream_write(stream
, b
->data
, b
->len
);
2340 if (written
!= b
->len
) {
2341 g_byte_array_remove_range(b
, 0, written
);
2345 g_byte_array_unref(b
);
2346 g_queue_pop_head(SIPE_MEDIA_STREAM_PRIVATE
->write_queue
);
2349 if (sipe_media_stream_is_writable(stream
) && stream
->writable_cb
) {
2350 stream
->writable_cb(stream
);
2355 sipe_media_stream_is_writable(struct sipe_media_stream
*stream
)
2357 return SIPE_MEDIA_STREAM_PRIVATE
->writable
&&
2358 SIPE_MEDIA_STREAM_PRIVATE
->sdp_negotiation_concluded
&&
2359 g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE
->write_queue
);
2364 sipe_media_stream_set_data(struct sipe_media_stream
*stream
, gpointer data
,
2365 GDestroyNotify free_func
)
2367 struct sipe_media_stream_private
*stream_private
=
2368 SIPE_MEDIA_STREAM_PRIVATE
;
2370 g_return_if_fail(stream_private
);
2372 if (stream_private
->data
&& stream_private
->data_free_func
) {
2373 stream_private
->data_free_func(stream_private
->data
);
2376 stream_private
->data
= data
;
2377 stream_private
->data_free_func
= free_func
;
2381 sipe_media_stream_get_data(struct sipe_media_stream
*stream
)
2383 g_return_val_if_fail(stream
, NULL
);
2385 return SIPE_MEDIA_STREAM_PRIVATE
->data
;