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
*ip
= sip_transport_ip_address(sipe_private
);
1285 if (SIPE_CORE_PRIVATE_FLAG_IS(SFB
) ||
1286 sipe_media_get_sip_dialog(SIPE_MEDIA_CALL
)->cseq
!= 0 ||
1287 call_private
->ice_version
!= SIPE_ICE_RFC_5245
||
1288 sipe_strequal(SIPE_MEDIA_CALL
->with
, sipe_private
->test_call_bot_uri
)) {
1292 body
= g_strdup_printf("Content-Type: application/sdp\r\n"
1293 "Content-Transfer-Encoding: 7bit\r\n"
1294 "Content-Disposition: session; handling=optional; ms-proxy-2007fallback\r\n"
1296 "o=- 0 0 IN IP4 %s\r\n"
1299 "m=audio 0 RTP/AVP\r\n",
1301 sipe_media_add_extra_invite_section(SIPE_MEDIA_CALL
,
1302 "multipart/alternative", body
);
1306 sipe_media_initiate_call(struct sipe_core_private
*sipe_private
,
1307 const char *with
, SipeIceVersion ice_version
,
1308 gboolean with_video
)
1310 struct sipe_media_call_private
*call_private
;
1312 if (sipe_core_media_get_call(SIPE_CORE_PUBLIC
)) {
1316 call_private
= (struct sipe_media_call_private
*)
1317 sipe_media_call_new(sipe_private
, with
, NULL
,
1320 SIPE_MEDIA_CALL
->call_reject_cb
= av_call_reject_cb
;
1322 if (!sipe_media_stream_add(SIPE_MEDIA_CALL
, "audio", SIPE_MEDIA_AUDIO
,
1323 call_private
->ice_version
,
1325 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
1326 _("Error occurred"),
1327 _("Error creating audio stream"));
1328 sipe_media_hangup(call_private
);
1333 !sipe_media_stream_add(SIPE_MEDIA_CALL
, "video", SIPE_MEDIA_VIDEO
,
1334 call_private
->ice_version
,
1335 TRUE
, VIDEO_SSRC_COUNT
)) {
1336 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
1337 _("Error occurred"),
1338 _("Error creating video stream"));
1339 sipe_media_hangup(call_private
);
1343 append_2007_fallback_if_needed(call_private
);
1345 // Processing continues in stream_initialized_cb
1349 sipe_core_media_initiate_call(struct sipe_core_public
*sipe_public
,
1351 gboolean with_video
)
1353 sipe_media_initiate_call(SIPE_CORE_PRIVATE
, with
,
1354 SIPE_ICE_RFC_5245
, with_video
);
1358 conference_audio_muted_cb(struct sipe_media_stream
*stream
, gboolean is_muted
)
1360 struct sipe_media_call
*call
= stream
->call
;
1362 if (!SIPE_MEDIA_CALL_PRIVATE
->conference_session
) {
1366 sipe_conf_announce_audio_mute_state(SIPE_MEDIA_CALL_PRIVATE
->sipe_private
,
1367 SIPE_MEDIA_CALL_PRIVATE
->conference_session
,
1371 void sipe_core_media_connect_conference(struct sipe_core_public
*sipe_public
,
1372 struct sipe_chat_session
*chat_session
)
1374 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1375 struct sipe_media_call_private
*call_private
;
1376 struct sipe_media_stream
*stream
;
1377 struct sip_session
*session
;
1378 SipeIceVersion ice_version
;
1381 if (!sipe_conf_supports_mcu_type(sipe_private
, "audio-video")) {
1382 sipe_backend_notify_error(sipe_public
, _("Join conference call"),
1383 _("Conference calls are not supported on this server."));
1387 session
= sipe_session_find_chat(sipe_private
, chat_session
);
1389 if (sipe_core_media_get_call(sipe_public
) || !session
) {
1393 av_uri
= sipe_conf_build_uri(sipe_core_chat_id(sipe_public
, chat_session
),
1399 session
->is_call
= TRUE
;
1401 ice_version
= SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013
) ? SIPE_ICE_RFC_5245
:
1404 call_private
= (struct sipe_media_call_private
*)
1405 sipe_media_call_new(sipe_private
, av_uri
, NULL
,
1407 call_private
->conference_session
= session
;
1408 SIPE_MEDIA_CALL
->call_reject_cb
= av_call_reject_cb
;
1410 stream
= sipe_media_stream_add(SIPE_MEDIA_CALL
, "audio",
1412 call_private
->ice_version
,
1415 sipe_backend_notify_error(sipe_public
,
1416 _("Error occurred"),
1417 _("Error creating audio stream"));
1419 sipe_media_hangup(call_private
);
1422 stream
->mute_cb
= conference_audio_muted_cb
;
1426 // Processing continues in stream_initialized_cb
1429 struct sipe_media_call
*
1430 sipe_core_media_get_call(struct sipe_core_public
*sipe_public
)
1432 struct sipe_media_call
* result
= NULL
;
1433 GList
*calls
= g_hash_table_get_values(SIPE_CORE_PRIVATE
->media_calls
);
1434 GList
*entry
= calls
;
1437 if (sipe_core_media_get_stream_by_id(entry
->data
, "audio")) {
1438 result
= entry
->data
;
1441 entry
= entry
->next
;
1448 static gboolean
phone_number_is_valid(const gchar
*phone_number
)
1450 if (!phone_number
|| sipe_strequal(phone_number
, "")) {
1454 if (*phone_number
== '+') {
1458 while (*phone_number
!= '\0') {
1459 if (!g_ascii_isdigit(*phone_number
)) {
1468 void sipe_core_media_phone_call(struct sipe_core_public
*sipe_public
,
1469 const gchar
*phone_number
)
1471 g_return_if_fail(sipe_public
);
1473 if (phone_number_is_valid(phone_number
)) {
1474 gchar
*phone_uri
= g_strdup_printf("sip:%s@%s;user=phone",
1475 phone_number
, sipe_public
->sip_domain
);
1477 sipe_core_media_initiate_call(sipe_public
, phone_uri
, FALSE
);
1481 sipe_backend_notify_error(sipe_public
,
1482 _("Unable to establish a call"),
1483 _("Invalid phone number"));
1487 void sipe_core_media_test_call(struct sipe_core_public
*sipe_public
)
1489 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1490 if (!sipe_private
->test_call_bot_uri
) {
1491 sipe_backend_notify_error(sipe_public
,
1492 _("Unable to establish a call"),
1493 _("Audio Test Service is not available."));
1497 sipe_core_media_initiate_call(sipe_public
,
1498 sipe_private
->test_call_bot_uri
, FALSE
);
1501 static struct sipe_media_call_private
*
1502 sipe_media_from_sipmsg(struct sipe_core_private
*sipe_private
,
1505 return g_hash_table_lookup(sipe_private
->media_calls
,
1506 sipmsg_find_header(msg
, "Call-ID"));
1510 transport_response_unsupported_sdp(struct sipe_core_private
*sipe_private
,
1513 sipmsg_add_header(msg
, "ms-client-diagnostics",
1514 "52063;reason=\"Unsupported session description\"");
1515 sip_transport_response(sipe_private
, msg
,
1516 488, "Not Acceptable Here", NULL
);
1520 maybe_send_second_invite_response(struct sipe_media_call_private
*call_private
)
1524 /* Second INVITE request had to be received and all streams must have
1525 * established candidate pairs before the response can be sent. */
1527 if (!call_private
->invitation
) {
1531 for (it
= call_private
->streams
; it
; it
= it
->next
) {
1532 struct sipe_media_stream_private
*stream_private
= it
->data
;
1533 if (!stream_private
->established
) {
1538 send_response_with_session_description(call_private
, 200, "OK");
1541 for (it
= call_private
->streams
; it
; it
= it
->next
) {
1542 struct sipe_media_stream_private
*stream_private
= it
->data
;
1544 stream_private
->sdp_negotiation_concluded
= TRUE
;
1545 if (stream_private
->writable
) {
1546 // We've become writable.
1547 sipe_core_media_stream_writable(SIPE_MEDIA_STREAM
, TRUE
);
1553 struct sipe_media_call
*
1554 process_incoming_invite_call(struct sipe_core_private
*sipe_private
,
1558 return(process_incoming_invite_call_parsed_sdp(sipe_private
,
1560 sdpmsg_parse_msg(sdp
)));
1563 struct sipe_media_call
*
1564 process_incoming_invite_call_parsed_sdp(struct sipe_core_private
*sipe_private
,
1566 struct sdpmsg
*smsg
)
1568 struct sipe_media_call_private
*call_private
;
1569 gboolean has_new_media
= FALSE
;
1572 // Don't allow two voice calls in parallel.
1573 if (!strstr(msg
->body
, "m=data") &&
1574 !strstr(msg
->body
, "m=applicationsharing")) {
1575 struct sipe_media_call
*call
=
1576 sipe_core_media_get_call(SIPE_CORE_PUBLIC
);
1577 if (call
&& !is_media_session_msg(SIPE_MEDIA_CALL_PRIVATE
, msg
)) {
1578 sip_transport_response(sipe_private
, msg
,
1579 486, "Busy Here", NULL
);
1585 call_private
= sipe_media_from_sipmsg(sipe_private
, msg
);
1588 char *self
= sip_uri_self(sipe_private
);
1589 if (sipe_strequal(SIPE_MEDIA_CALL
->with
, self
)) {
1591 sip_transport_response(sipe_private
, msg
, 488, "Not Acceptable Here", NULL
);
1599 transport_response_unsupported_sdp(sipe_private
, msg
);
1601 sipe_media_hangup(call_private
);
1606 if (!call_private
) {
1607 gchar
*with
= parse_from(sipmsg_find_header(msg
, "From"));
1608 SipeMediaCallFlags flags
= 0;
1610 if (strstr(msg
->body
, "m=data") ||
1611 strstr(msg
->body
, "m=applicationsharing")) {
1612 flags
|= SIPE_MEDIA_CALL_NO_UI
;
1615 call_private
= (struct sipe_media_call_private
*)
1616 sipe_media_call_new(sipe_private
, with
,
1617 msg
, smsg
->ice_version
,
1620 if (!(flags
& SIPE_MEDIA_CALL_NO_UI
)) {
1621 SIPE_MEDIA_CALL
->call_reject_cb
= av_call_reject_cb
;
1626 if (call_private
->invitation
)
1627 sipmsg_free(call_private
->invitation
);
1628 call_private
->invitation
= sipmsg_copy(msg
);
1630 ssrc_range_update(&call_private
->ssrc_ranges
, smsg
->media
);
1632 // Create any new media streams
1633 for (i
= smsg
->media
; i
; i
= i
->next
) {
1634 struct sdpmedia
*media
= i
->data
;
1635 gchar
*id
= media
->name
;
1638 if ( media
->port
!= 0
1639 && !sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL
, id
)) {
1640 guint32 ssrc_count
= 0;
1642 if (sipe_strequal(id
, "audio"))
1643 type
= SIPE_MEDIA_AUDIO
;
1644 else if (sipe_strequal(id
, "video")) {
1645 type
= SIPE_MEDIA_VIDEO
;
1646 ssrc_count
= VIDEO_SSRC_COUNT
;
1647 } else if (sipe_strequal(id
, "data"))
1648 type
= SIPE_MEDIA_APPLICATION
;
1649 else if (sipe_strequal(id
, "applicationsharing"))
1650 type
= SIPE_MEDIA_APPLICATION
;
1654 sipe_media_stream_add(SIPE_MEDIA_CALL
, id
, type
,
1655 smsg
->ice_version
, FALSE
,
1657 has_new_media
= TRUE
;
1661 if (has_new_media
) {
1662 sdpmsg_free(call_private
->smsg
);
1663 call_private
->smsg
= smsg
;
1664 sip_transport_response(sipe_private
, call_private
->invitation
,
1665 180, "Ringing", NULL
);
1666 // Processing continues in stream_initialized_cb
1668 apply_remote_message(call_private
, smsg
);
1670 maybe_send_second_invite_response(call_private
);
1673 return SIPE_MEDIA_CALL
;
1676 void process_incoming_cancel_call(struct sipe_media_call_private
*call_private
,
1679 // We respond to the CANCEL request with 200 OK response and
1680 // with 487 Request Terminated to the remote INVITE in progress.
1681 sip_transport_response(call_private
->sipe_private
, msg
, 200, "OK", NULL
);
1683 if (call_private
->invitation
) {
1684 sip_transport_response(call_private
->sipe_private
,
1685 call_private
->invitation
,
1686 487, "Request Terminated", NULL
);
1689 sipe_backend_media_reject(SIPE_MEDIA_CALL
->backend_private
, FALSE
);
1693 sipe_media_send_ack(struct sipe_core_private
*sipe_private
,
1695 struct transaction
*trans
)
1697 struct sipe_media_call_private
*call_private
;
1698 struct sip_dialog
*dialog
;
1701 call_private
= sipe_media_from_sipmsg(sipe_private
, msg
);
1703 if (!is_media_session_msg(call_private
, msg
))
1706 dialog
= sipe_media_get_sip_dialog(SIPE_MEDIA_CALL
);
1710 tmp_cseq
= dialog
->cseq
;
1712 dialog
->cseq
= sip_transaction_cseq(trans
) - 1;
1713 sip_transport_ack(sipe_private
, dialog
);
1714 dialog
->cseq
= tmp_cseq
;
1716 dialog
->outgoing_invite
= NULL
;
1722 sipe_media_send_final_ack(struct sipe_core_private
*sipe_private
,
1724 struct transaction
*trans
)
1726 struct sipe_media_call_private
*call_private
;
1731 if (!sipe_media_send_ack(sipe_private
, msg
, trans
))
1734 call_private
= sipe_media_from_sipmsg(sipe_private
, msg
);
1736 sipe_backend_media_accept(SIPE_MEDIA_CALL
->backend_private
, FALSE
);
1739 for (it
= call_private
->streams
; it
; it
= it
->next
) {
1740 struct sipe_media_stream_private
*stream_private
= it
->data
;
1742 stream_private
->sdp_negotiation_concluded
= TRUE
;
1743 if (stream_private
->writable
) {
1744 // We've become writable.
1745 sipe_core_media_stream_writable(SIPE_MEDIA_STREAM
, TRUE
);
1754 sipe_core_media_stream_candidate_pair_established(struct sipe_media_stream
*stream
)
1756 struct sipe_media_call
*call
= stream
->call
;
1758 GList
*active_candidates
=
1759 sipe_backend_media_stream_get_active_local_candidates(stream
);
1760 guint ready_components
= g_list_length(active_candidates
);
1762 sipe_media_candidate_list_free(active_candidates
);
1764 if (ready_components
!= 2) {
1765 // We must have both RTP+RTCP candidate pairs established first.
1769 if (SIPE_MEDIA_STREAM_PRIVATE
->established
) {
1772 SIPE_MEDIA_STREAM_PRIVATE
->established
= TRUE
;
1774 if (stream
->candidate_pairs_established_cb
) {
1775 stream
->candidate_pairs_established_cb(stream
);
1778 if (sipe_backend_media_is_initiator(stream
->call
, NULL
)) {
1779 GSList
*streams
= SIPE_MEDIA_CALL_PRIVATE
->streams
;
1780 for (; streams
; streams
= streams
->next
) {
1781 struct sipe_media_stream_private
*s
= streams
->data
;
1782 if (!s
->established
) {
1787 if (streams
== NULL
) {
1788 // All call streams have been established.
1789 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE
,
1790 sipe_media_send_final_ack
);
1793 maybe_send_second_invite_response(SIPE_MEDIA_CALL_PRIVATE
);
1798 maybe_retry_call_with_ice_version(struct sipe_core_private
*sipe_private
,
1799 struct sipe_media_call_private
*call_private
,
1800 SipeIceVersion ice_version
,
1801 struct transaction
*trans
)
1803 if (call_private
->ice_version
!= ice_version
&&
1804 sip_transaction_cseq(trans
) == 1) {
1807 gboolean with_video
= FALSE
;
1809 for (i
= call_private
->streams
; i
; i
= i
->next
) {
1810 struct sipe_media_stream
*stream
= i
->data
;
1812 if (sipe_strequal(stream
->id
, "video")) {
1814 } else if (!sipe_strequal(stream
->id
, "audio")) {
1815 /* Don't retry calls which are neither audio
1821 with
= g_strdup(SIPE_MEDIA_CALL
->with
);
1823 sipe_media_hangup(call_private
);
1824 SIPE_DEBUG_INFO("Retrying call with ICEv%d.",
1825 ice_version
== SIPE_ICE_DRAFT_6
? 6 : 19);
1826 sipe_media_initiate_call(sipe_private
,
1839 process_invite_call_response(struct sipe_core_private
*sipe_private
,
1841 struct transaction
*trans
)
1844 struct sipe_media_call_private
*call_private
;
1845 struct sip_dialog
*dialog
;
1846 struct sdpmsg
*smsg
;
1848 call_private
= sipe_media_from_sipmsg(sipe_private
,msg
);
1850 if (!is_media_session_msg(call_private
, msg
))
1853 dialog
= sipe_media_get_sip_dialog(SIPE_MEDIA_CALL
);
1855 with
= dialog
->with
;
1857 dialog
->outgoing_invite
= NULL
;
1859 if (msg
->response
== 603 || msg
->response
== 605) {
1860 // Call rejected by remote peer
1861 sipe_media_send_ack(sipe_private
, msg
, trans
);
1862 sipe_backend_media_reject(SIPE_MEDIA_CALL
->backend_private
, FALSE
);
1867 if (msg
->response
>= 400) {
1868 // An error occurred
1870 GString
*desc
= g_string_new("");
1871 gboolean append_responsestr
= FALSE
;
1873 switch (msg
->response
) {
1875 title
= _("User unavailable");
1877 if (sipmsg_parse_warning(msg
, NULL
) == 391) {
1878 g_string_append_printf(desc
, _("%s does not want to be disturbed"), with
);
1880 g_string_append_printf(desc
, _("User %s is not available"), with
);
1884 // OCS/Lync really sends response string with 'Mutipart' typo.
1885 if (sipe_strequal(msg
->responsestr
, "Mutipart mime in content type not supported by Archiving CDR service") &&
1886 maybe_retry_call_with_ice_version(sipe_private
,
1892 title
= _("Unsupported media type");
1895 /* Check for incompatible encryption levels error.
1898 * 488 Not Acceptable Here
1899 * ms-client-diagnostics: 52017;reason="Encryption levels dont match"
1901 * older clients (and SIPE itself):
1902 * 488 Encryption Levels not compatible
1904 const gchar
*ms_diag
= sipmsg_find_header(msg
, "ms-client-diagnostics");
1905 SipeIceVersion retry_ice_version
= SIPE_ICE_DRAFT_6
;
1907 if (sipe_strequal(msg
->responsestr
, "Encryption Levels not compatible") ||
1908 (ms_diag
&& g_str_has_prefix(ms_diag
, "52017;"))) {
1909 title
= _("Unable to establish a call");
1910 g_string_append(desc
, _("Encryption settings of peer are incompatible with ours."));
1914 /* Check if this is failed conference using
1915 * ICEv6 with reason "Error parsing SDP" and
1916 * retry using ICEv19. */
1917 ms_diag
= sipmsg_find_header(msg
, "ms-diagnostics");
1918 if (ms_diag
&& g_str_has_prefix(ms_diag
, "7008;")) {
1919 retry_ice_version
= SIPE_ICE_RFC_5245
;
1922 if (maybe_retry_call_with_ice_version(sipe_private
,
1931 title
= _("Error occurred");
1932 g_string_append(desc
, _("Unable to establish a call"));
1933 append_responsestr
= TRUE
;
1937 if (append_responsestr
) {
1938 gchar
*reason
= sipmsg_get_ms_diagnostics_reason(msg
);
1940 g_string_append_printf(desc
, "\n%d %s",
1941 msg
->response
, msg
->responsestr
);
1943 g_string_append_printf(desc
, "\n\n%s", reason
);
1948 sipe_backend_notify_error(SIPE_CORE_PUBLIC
, title
, desc
->str
);
1949 g_string_free(desc
, TRUE
);
1951 sipe_media_send_ack(sipe_private
, msg
, trans
);
1952 sipe_media_hangup(call_private
);
1957 sipe_dialog_parse(dialog
, msg
, TRUE
);
1958 smsg
= sdpmsg_parse_msg(msg
->body
);
1960 transport_response_unsupported_sdp(sipe_private
, msg
);
1961 sipe_media_hangup(call_private
);
1965 ssrc_range_update(&call_private
->ssrc_ranges
, smsg
->media
);
1966 apply_remote_message(call_private
, smsg
);
1969 sipe_media_send_ack(sipe_private
, msg
, trans
);
1973 // Waits until sipe_core_media_candidate_pair_established() is invoked.
1976 gboolean
is_media_session_msg(struct sipe_media_call_private
*call_private
,
1979 if (!call_private
) {
1983 return sipe_media_from_sipmsg(call_private
->sipe_private
, msg
) == call_private
;
1987 end_call(SIPE_UNUSED_PARAMETER gpointer key
,
1988 struct sipe_media_call_private
*call_private
,
1989 SIPE_UNUSED_PARAMETER gpointer user_data
)
1991 struct sipe_backend_media
*backend_private
;
1993 backend_private
= call_private
->public.backend_private
;
1995 if (!sipe_backend_media_is_initiator(SIPE_MEDIA_CALL
, NULL
) &&
1996 !sipe_backend_media_accepted(backend_private
)) {
1997 sip_transport_response(call_private
->sipe_private
,
1998 call_private
->invitation
,
1999 480, "Temporarily Unavailable", NULL
);
2000 } else if (call_private
->session
) {
2001 sipe_session_close(call_private
->sipe_private
,
2002 call_private
->session
);
2003 call_private
->session
= NULL
;
2006 sipe_media_hangup(call_private
);
2010 sipe_media_handle_going_offline(struct sipe_core_private
*sipe_private
)
2012 g_hash_table_foreach(sipe_private
->media_calls
, (GHFunc
) end_call
, NULL
);
2015 gboolean
sipe_media_is_conference_call(struct sipe_media_call_private
*call_private
)
2017 return g_strstr_len(SIPE_MEDIA_CALL
->with
, -1, "app:conf:audio-video:") != NULL
;
2020 struct sipe_core_private
*
2021 sipe_media_get_sipe_core_private(struct sipe_media_call
*call
)
2023 g_return_val_if_fail(call
, NULL
);
2025 return SIPE_MEDIA_CALL_PRIVATE
->sipe_private
;
2029 sipe_media_get_sip_dialog(struct sipe_media_call
*call
)
2031 struct sip_session
*session
;
2033 g_return_val_if_fail(call
, NULL
);
2035 session
= SIPE_MEDIA_CALL_PRIVATE
->session
;
2037 if (!session
|| !session
->dialogs
) {
2041 return session
->dialogs
->data
;
2045 sipe_media_relay_free(struct sipe_media_relay
*relay
)
2047 g_free(relay
->hostname
);
2048 if (relay
->dns_query
)
2049 sipe_backend_dns_query_cancel(relay
->dns_query
);
2054 sipe_media_relay_list_free(GSList
*list
)
2056 for (; list
; list
= g_slist_delete_link(list
, list
))
2057 sipe_media_relay_free(list
->data
);
2061 relay_ip_resolved_cb(struct sipe_media_relay
* relay
,
2062 const gchar
*ip
, SIPE_UNUSED_PARAMETER guint port
)
2064 gchar
*hostname
= relay
->hostname
;
2065 relay
->dns_query
= NULL
;
2068 relay
->hostname
= g_strdup(ip
);
2069 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname
, ip
);
2071 relay
->hostname
= NULL
;
2072 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname
);
2079 process_get_av_edge_credentials_response(struct sipe_core_private
*sipe_private
,
2081 SIPE_UNUSED_PARAMETER
struct transaction
*trans
)
2083 g_free(sipe_private
->media_relay_username
);
2084 g_free(sipe_private
->media_relay_password
);
2085 sipe_media_relay_list_free(sipe_private
->media_relays
);
2086 sipe_private
->media_relay_username
= NULL
;
2087 sipe_private
->media_relay_password
= NULL
;
2088 sipe_private
->media_relays
= NULL
;
2090 if (msg
->response
>= 400) {
2091 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
2092 "Failed to obtain A/V Edge credentials.");
2096 if (msg
->response
== 200) {
2097 sipe_xml
*xn_response
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
2099 if (sipe_strequal("OK", sipe_xml_attribute(xn_response
, "reasonPhrase"))) {
2100 const sipe_xml
*xn_credentials
= sipe_xml_child(xn_response
, "credentialsResponse/credentials");
2101 const sipe_xml
*xn_relays
= sipe_xml_child(xn_response
, "credentialsResponse/mediaRelayList");
2102 const sipe_xml
*item
;
2103 GSList
*relays
= NULL
;
2105 item
= sipe_xml_child(xn_credentials
, "username");
2106 sipe_private
->media_relay_username
= sipe_xml_data(item
);
2107 item
= sipe_xml_child(xn_credentials
, "password");
2108 sipe_private
->media_relay_password
= sipe_xml_data(item
);
2110 for (item
= sipe_xml_child(xn_relays
, "mediaRelay"); item
; item
= sipe_xml_twin(item
)) {
2111 struct sipe_media_relay
*relay
= g_new0(struct sipe_media_relay
, 1);
2112 const sipe_xml
*node
;
2115 node
= sipe_xml_child(item
, "hostName");
2116 relay
->hostname
= sipe_xml_data(node
);
2118 node
= sipe_xml_child(item
, "udpPort");
2120 tmp
= sipe_xml_data(node
);
2122 relay
->udp_port
= atoi(tmp
);
2127 node
= sipe_xml_child(item
, "tcpPort");
2129 tmp
= sipe_xml_data(node
);
2131 relay
->tcp_port
= atoi(tmp
);
2136 relays
= g_slist_append(relays
, relay
);
2138 relay
->dns_query
= sipe_backend_dns_query_a(
2142 (sipe_dns_resolved_cb
) relay_ip_resolved_cb
,
2145 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
2147 relay
->tcp_port
, relay
->udp_port
);
2150 sipe_private
->media_relays
= relays
;
2153 sipe_xml_free(xn_response
);
2160 sipe_media_get_av_edge_credentials(struct sipe_core_private
*sipe_private
)
2162 // TODO: re-request credentials after duration expires?
2163 static const char CRED_REQUEST_XML
[] =
2164 "<request requestID=\"%d\" "
2168 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
2169 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
2170 "<credentialsRequest credentialsRequestID=\"%d\">"
2171 "<identity>%s</identity>"
2172 "<location>%s</location>"
2173 "<duration>480</duration>"
2174 "</credentialsRequest>"
2177 int request_id
= rand();
2181 if (!sipe_private
->mras_uri
)
2184 self
= sip_uri_self(sipe_private
);
2186 body
= g_strdup_printf(
2190 sipe_private
->mras_uri
,
2193 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER
) ? "internet" : "intranet");
2196 sip_transport_service(sipe_private
,
2197 sipe_private
->mras_uri
,
2198 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
2200 process_get_av_edge_credentials_response
);
2206 sipe_media_add_extra_invite_section(struct sipe_media_call
*call
,
2207 const gchar
*invite_content_type
,
2210 g_free(SIPE_MEDIA_CALL_PRIVATE
->extra_invite_section
);
2211 g_free(SIPE_MEDIA_CALL_PRIVATE
->invite_content_type
);
2212 SIPE_MEDIA_CALL_PRIVATE
->extra_invite_section
= body
;
2213 SIPE_MEDIA_CALL_PRIVATE
->invite_content_type
=
2214 g_strdup(invite_content_type
);
2218 sipe_media_stream_add_extra_attribute(struct sipe_media_stream
*stream
,
2219 const gchar
*name
, const gchar
*value
)
2221 SIPE_MEDIA_STREAM_PRIVATE
->extra_sdp
=
2222 sipe_utils_nameval_add(SIPE_MEDIA_STREAM_PRIVATE
->extra_sdp
,
2228 sipe_core_media_stream_readable(struct sipe_media_stream
*stream
)
2230 g_return_if_fail(stream
);
2232 if (g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE
->async_reads
) &&
2234 stream
->read_cb(stream
);
2237 while (!g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE
->async_reads
)) {
2238 struct async_read_data
*data
;
2243 data
= g_queue_peek_head(SIPE_MEDIA_STREAM_PRIVATE
->async_reads
);
2244 pos
= data
->buffer
+ SIPE_MEDIA_STREAM_PRIVATE
->read_pos
;
2245 len
= data
->len
- SIPE_MEDIA_STREAM_PRIVATE
->read_pos
;
2247 bytes_read
= sipe_backend_media_stream_read(stream
, pos
, len
);
2248 if (bytes_read
== -1) {
2249 struct sipe_media_call
*call
= stream
->call
;
2250 struct sipe_core_private
*sipe_private
=
2251 SIPE_MEDIA_CALL_PRIVATE
->sipe_private
;
2253 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
2255 _("Error while reading from stream"));
2256 sipe_media_hangup(SIPE_MEDIA_CALL_PRIVATE
);
2260 SIPE_MEDIA_STREAM_PRIVATE
->read_pos
+= bytes_read
;
2262 if (SIPE_MEDIA_STREAM_PRIVATE
->read_pos
== data
->len
) {
2263 data
->callback(stream
, data
->buffer
, data
->len
);
2264 SIPE_MEDIA_STREAM_PRIVATE
->read_pos
= 0;
2265 g_queue_pop_head(SIPE_MEDIA_STREAM_PRIVATE
->async_reads
);
2268 // Still not enough data to finish the read.
2275 sipe_media_stream_read_async(struct sipe_media_stream
*stream
,
2276 gpointer buffer
, gsize len
,
2277 sipe_media_stream_read_callback callback
)
2279 struct async_read_data
*data
;
2281 g_return_if_fail(stream
&& buffer
&& callback
);
2283 data
= g_new0(struct async_read_data
, 1);
2284 data
->buffer
= buffer
;
2286 data
->callback
= callback
;
2288 g_queue_push_tail(SIPE_MEDIA_STREAM_PRIVATE
->async_reads
, data
);
2292 stream_append_buffer(struct sipe_media_stream
*stream
,
2293 guint8
*buffer
, guint len
)
2295 GByteArray
*b
= g_byte_array_sized_new(len
);
2296 g_byte_array_append(b
, buffer
, len
);
2297 g_queue_push_tail(SIPE_MEDIA_STREAM_PRIVATE
->write_queue
, b
);
2301 sipe_media_stream_write(struct sipe_media_stream
*stream
,
2302 gpointer buffer
, gsize len
)
2304 if (!sipe_media_stream_is_writable(stream
)) {
2305 stream_append_buffer(stream
, buffer
, len
);
2310 written
= sipe_backend_media_stream_write(stream
, buffer
, len
);
2311 if (written
== len
) {
2315 stream_append_buffer(stream
,
2316 (guint8
*)buffer
+ written
, len
- written
);
2322 sipe_core_media_stream_writable(struct sipe_media_stream
*stream
,
2325 SIPE_MEDIA_STREAM_PRIVATE
->writable
= writable
;
2331 while (!g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE
->write_queue
)) {
2335 b
= g_queue_peek_head(SIPE_MEDIA_STREAM_PRIVATE
->write_queue
);
2337 written
= sipe_backend_media_stream_write(stream
, b
->data
, b
->len
);
2338 if (written
!= b
->len
) {
2339 g_byte_array_remove_range(b
, 0, written
);
2343 g_byte_array_unref(b
);
2344 g_queue_pop_head(SIPE_MEDIA_STREAM_PRIVATE
->write_queue
);
2347 if (sipe_media_stream_is_writable(stream
) && stream
->writable_cb
) {
2348 stream
->writable_cb(stream
);
2353 sipe_media_stream_is_writable(struct sipe_media_stream
*stream
)
2355 return SIPE_MEDIA_STREAM_PRIVATE
->writable
&&
2356 SIPE_MEDIA_STREAM_PRIVATE
->sdp_negotiation_concluded
&&
2357 g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE
->write_queue
);
2362 sipe_media_stream_set_data(struct sipe_media_stream
*stream
, gpointer data
,
2363 GDestroyNotify free_func
)
2365 struct sipe_media_stream_private
*stream_private
=
2366 SIPE_MEDIA_STREAM_PRIVATE
;
2368 g_return_if_fail(stream_private
);
2370 if (stream_private
->data
&& stream_private
->data_free_func
) {
2371 stream_private
->data_free_func(stream_private
->data
);
2374 stream_private
->data
= data
;
2375 stream_private
->data_free_func
= free_func
;
2379 sipe_media_stream_get_data(struct sipe_media_stream
*stream
)
2381 g_return_val_if_fail(stream
, NULL
);
2383 return SIPE_MEDIA_STREAM_PRIVATE
->data
;