digest: add support for OpenSSL 1.1.0
[siplcs.git] / src / core / sipe-media.c
blob1ae3677b8d746fb5d59f9acc7fa6039ea771c90f
1 /**
2 * @file sipe-media.c
4 * pidgin-sipe
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
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
32 #include <glib.h>
34 #include "sipe-common.h"
35 #include "sipmsg.h"
36 #include "sip-transport.h"
37 #include "sipe-backend.h"
38 #include "sdpmsg.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"
47 #include "sipe-nls.h"
48 #include "sipe-xml.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;
63 GSList *streams;
65 struct sipmsg *invitation;
66 SipeIceVersion ice_version;
67 gboolean encryption_compatible;
68 gchar *extra_invite_section;
69 gchar *invite_content_type;
71 GSList *ssrc_ranges;
73 struct sdpmsg *smsg;
74 GSList *failed_media;
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;
85 gboolean established;
86 #ifdef HAVE_XDATA
87 gboolean sdp_negotiation_concluded;
88 gboolean writable;
89 #endif
91 GSList *extra_sdp;
93 GQueue *write_queue;
94 GQueue *async_reads;
95 gssize read_pos;
97 /* User data associated with the stream. */
98 gpointer data;
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 {
105 guint8 *buffer;
106 gssize len;
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);
122 static void
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);
131 if (call_private) {
132 call_private->streams = g_slist_remove(call_private->streams,
133 stream_private);
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);
154 static gboolean
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;
162 static void
163 sipe_media_call_free(struct sipe_media_call_private *call_private)
165 if (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);
196 static gint
197 candidate_sort_cb(struct sdpcandidate *c1, struct sdpcandidate *c2)
199 int cmp = sipe_strcompare(c1->foundation, c2->foundation);
200 if (cmp == 0) {
201 cmp = sipe_strcompare(c1->username, c2->username);
202 if (cmp == 0)
203 cmp = c1->component - c2->component;
206 return cmp;
209 static GSList *
210 backend_candidates_to_sdpcandidate(GList *candidates)
212 GSList *result = NULL;
213 GList *i;
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. */
224 g_free(ip);
225 g_free(base_ip);
226 continue;
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);
234 c->ip = ip;
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);
246 return result;
249 static void
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;
256 *ip = 0;
257 *rtp_port = 0;
258 *rtcp_port = 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;
268 g_free(*ip);
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;
278 static gint
279 sdpcodec_compare(gconstpointer a, gconstpointer b)
281 return ((const struct sdpcodec *)a)->id -
282 ((const struct sdpcodec *)b)->id;
285 static GList *
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,
290 g_str_equal,
291 g_free,
292 NULL);
294 while (i) {
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,
301 foundation);
303 if (c2) {
304 g_free(foundation);
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);
322 } else
323 /* hash table takes ownership of "foundation" */
324 g_hash_table_insert(foundation_to_candidate, foundation, c1);
327 i = next;
330 g_hash_table_destroy(foundation_to_candidate);
332 return candidates;
335 static void
336 fill_zero_tcp_act_ports_from_tcp_pass(GSList *candidates, GSList *all_candidates)
338 GSList *i;
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;
343 GSList *j;
345 if (c->protocol != SIPE_NETWORK_PROTOCOL_TCP_ACTIVE) {
346 continue;
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) {
353 continue;
356 if (sipe_strequal(c->ip, passive->ip) &&
357 sipe_strequal(c->base_ip, passive->base_ip)) {
358 if (c->port == 0) {
359 c->port = passive->port;
362 if (c->base_port == 0) {
363 c->base_port = passive->base_port;
365 break;
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);
385 if (base_port) {
386 c->base_port = *base_port;
387 } else {
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;
406 return result;
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,
415 SIPE_MEDIA_STREAM);
416 SipeEncryptionPolicy encryption_policy =
417 get_encryption_policy(call_private->sipe_private);
418 guint rtcp_port = 0;
419 SipeMediaType type;
420 GSList *attributes = NULL;
421 GSList *sdpcandidates;
422 GSList *all_sdpcandidates;
423 GList *candidates;
424 GList *i;
425 GSList *j;
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;
437 else {
438 // TODO: incompatible media, should not happen here
439 g_free(sdpmedia->name);
440 g_free(sdpmedia);
441 sipe_media_codec_list_free(codecs);
442 return(NULL);
445 // Process 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);
449 GList *params;
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);
454 c->type = type;
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)) {
472 sdpcodec_free(c);
473 } else {
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,
488 SIPE_MEDIA_STREAM);
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", "");
512 if (rtcp_port) {
513 gchar *tmp = g_strdup_printf("%u", rtcp_port);
514 attributes = sipe_utils_nameval_add(attributes, "rtcp", tmp);
515 g_free(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";
523 break;
524 case SIPE_ENCRYPTION_POLICY_OPTIONAL:
525 encryption = "optional";
526 break;
527 case SIPE_ENCRYPTION_POLICY_REQUIRED:
528 default:
529 encryption = "required";
530 break;
533 attributes = sipe_utils_nameval_add(attributes, "encryption", encryption);
536 if (SIPE_MEDIA_STREAM->ssrc_range) {
537 gchar *tmp;
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);
544 g_free(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,
561 SIPE_SRTP_KEY_LEN);
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;
574 return sdpmedia;
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,
585 streams->data);
586 if (media) {
587 msg->media = g_slist_append(msg->media, media);
589 if (msg->ip == NULL)
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;
599 return msg;
602 static void
603 sipe_invite_call(struct sipe_media_call_private *call_private, TransCallback tc)
605 struct sipe_core_private *sipe_private = call_private->sipe_private;
606 gchar *hdr;
607 gchar *contact;
608 gchar *p_preferred_identity = NULL;
609 gchar *body;
610 struct sip_dialog *dialog;
611 struct sdpmsg *msg;
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);
622 g_free(self);
625 hdr = g_strdup_printf(
626 "ms-keep-alive: UAC;hop-hop=yes\r\n"
627 "Contact: %s\r\n"
628 "%s"
629 "Content-Type: %s%s\r\n",
630 contact,
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\"" : "");
637 g_free(contact);
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) {
644 gchar *tmp;
645 tmp = g_strdup_printf(
646 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
647 "%s"
648 "\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"
653 "\r\n"
654 "%s"
655 "\r\n"
656 "------=_NextPart_000_001E_01CB4397.0B5EB570--\r\n",
657 call_private->extra_invite_section, body);
658 g_free(body);
659 body = tmp;
660 sipe_media_add_extra_invite_section(SIPE_MEDIA_CALL, NULL, NULL);
663 sdpmsg_free(msg);
665 dialog->outgoing_invite = sip_transport_invite(sipe_private,
666 hdr,
667 body,
668 dialog,
669 tc);
671 g_free(body);
672 g_free(hdr);
675 static void
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);
680 sdpmsg_free(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);
683 g_free(body);
686 static gboolean
687 process_invite_call_response(struct sipe_core_private *sipe_private,
688 struct sipmsg *msg,
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)
694 GSList *i;
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))
698 return stream;
700 return NULL;
703 static gboolean
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;
710 GSList *i;
711 gboolean result = TRUE;
713 stream = sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL, media->name);
714 if (media->port == 0) {
715 if (stream) {
716 sipe_backend_media_stream_end(SIPE_MEDIA_CALL, stream);
718 return TRUE;
721 if (!stream)
722 return FALSE;
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) {
731 return TRUE;
734 for (i = media->codecs; i; i = i->next) {
735 struct sdpcodec *c = i->data;
736 struct sipe_backend_codec *codec;
737 GSList *j;
739 codec = sipe_backend_codec_new(c->id,
740 c->name,
741 c->type,
742 c->clock_rate,
743 c->channels);
745 for (j = c->parameters; j; j = j->next) {
746 struct sipnameval *attr = j->data;
748 sipe_backend_codec_add_optional_parameter(codec,
749 attr->name,
750 attr->value);
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,
764 backend_codecs);
765 sipe_media_codec_list_free(backend_codecs);
767 if (result == FALSE) {
768 sipe_backend_media_stream_end(SIPE_MEDIA_CALL, stream);
769 return FALSE;
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,
776 c->component,
777 c->type,
778 c->protocol,
779 c->ip,
780 c->port,
781 c->username,
782 c->password);
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,
789 backend_candidates);
790 sipe_media_candidate_list_free(backend_candidates);
792 SIPE_MEDIA_STREAM_PRIVATE->remote_candidates_and_codecs_set = TRUE;
794 return TRUE;
797 static void
798 apply_remote_message(struct sipe_media_call_private* call_private,
799 struct sdpmsg* msg)
801 GSList *i;
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)) {
817 media->port = 0;
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);
830 static gboolean
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)) {
836 return FALSE;
840 return TRUE;
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.
846 static void
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))
855 return;
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",
865 NULL);
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."));
870 } else {
871 send_response_with_session_description(call_private, 200, "OK");
872 sipmsg_free(call_private->invitation);
873 call_private->invitation = NULL;
877 static void
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);
893 sdpmsg_free(smsg);
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);
902 } else {
903 // TODO: OCS 2005 support. Is anyone still using it at all?
907 void
908 sipe_core_media_stream_end(struct sipe_media_stream *stream)
910 sipe_media_stream_free(SIPE_MEDIA_STREAM_PRIVATE);
913 static void
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);
926 static void
927 call_accept_cb(struct sipe_media_call *call, gboolean local)
929 if (local) {
930 maybe_send_first_invite_response(SIPE_MEDIA_CALL_PRIVATE);
932 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE->sipe_private);
935 static void
936 call_reject_cb(struct sipe_media_call *call, gboolean local)
938 if (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;
953 static void
954 av_call_reject_cb(struct sipe_media_call *call, gboolean local)
956 if (!local) {
957 struct sipe_core_private *sipe_private;
958 gchar *desc;
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"),
964 desc);
965 g_free(desc);
968 call_reject_cb(call, local);
971 static gboolean
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,
976 gboolean local,
977 SIPE_UNUSED_PARAMETER gboolean state)
979 if (local) {
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)
986 if (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;
997 static void
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);
1007 g_free(title);
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;
1024 gchar *cname;
1026 session = sipe_session_add_call(sipe_private, with);
1028 dialog = sipe_dialog_add(session);
1029 dialog->with = g_strdup(with);
1031 if (msg) {
1032 gchar *newTag = gentag();
1033 const gchar *oldHeader;
1034 gchar *newHeader;
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);
1040 g_free(newTag);
1041 g_free(newHeader);
1043 dialog->callid = g_strdup(sipmsg_find_header(msg, "Call-ID"));
1044 sipe_dialog_parse(dialog, msg, FALSE);
1045 } else {
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);
1055 return NULL;
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,
1070 SIPE_MEDIA_CALL,
1071 with,
1072 flags);
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;
1086 g_free(cname);
1088 return SIPE_MEDIA_CALL;
1091 void sipe_media_hangup(struct sipe_media_call_private *call_private)
1093 if (call_private) {
1094 sipe_backend_media_hangup(call_private->public.backend_private,
1095 FALSE);
1099 static gint
1100 ssrc_range_compare(const struct ssrc_range *a, const struct ssrc_range *b)
1102 if (a->begin < b->begin) {
1103 return -1;
1105 if (a->begin > b->begin) {
1106 return 1;
1108 return 0;
1111 static void
1112 ssrc_range_update(GSList **ranges, GSList *media)
1114 for (; media; media = media->next) {
1115 struct sdpmedia *m;
1116 const char *ssrc_range;
1117 gchar **parts;
1119 m = media->data;
1120 ssrc_range = sipe_utils_nameval_find(m->attributes,
1121 "x-ssrc-range");
1122 if (!ssrc_range) {
1123 continue;
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(
1136 *ranges, range,
1137 (GCompareFunc)ssrc_range_compare,
1138 g_free);
1141 g_strfreev(parts);
1145 static struct ssrc_range *
1146 ssrc_range_allocate(GSList **ranges, guint32 len)
1148 struct ssrc_range *range;
1149 GSList *i;
1151 range = g_new0(struct ssrc_range, 1);
1152 range->begin = 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) {
1159 break;
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
1167 * inclusive. */
1168 if (range->begin > range->end || range->end > 0xFFFFFF00) {
1169 g_free(range);
1170 SIPE_DEBUG_ERROR("Couldn't allocate SSRC range of %u", len);
1171 return NULL;
1174 *ranges = g_slist_insert_sorted(*ranges, range,
1175 (GCompareFunc)ssrc_range_compare);
1177 return range;
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;
1188 guint min_port;
1189 guint max_port;
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;
1200 switch (type) {
1201 case SIPE_MEDIA_AUDIO:
1202 min_port = sipe_private->min_audio_port;
1203 max_port = sipe_private->max_audio_port;
1204 break;
1205 case SIPE_MEDIA_VIDEO:
1206 min_port = sipe_private->min_video_port;
1207 max_port = sipe_private->max_audio_port;
1208 break;
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;
1217 break;
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,
1229 ssrc_count);
1232 SIPE_MEDIA_STREAM->backend_private =
1233 sipe_backend_media_add_stream(SIPE_MEDIA_STREAM,
1234 type, ice_version,
1235 initiator,
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);
1243 return NULL;
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");
1260 #ifdef HAVE_SRTP
1261 if (get_encryption_policy(sipe_private) != SIPE_ENCRYPTION_POLICY_REJECTED) {
1262 int i;
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;
1269 #endif
1271 SIPE_MEDIA_CALL_PRIVATE->streams =
1272 g_slist_append(SIPE_MEDIA_CALL_PRIVATE->streams,
1273 stream_private);
1275 return SIPE_MEDIA_STREAM;
1278 static void
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);
1283 gchar *body;
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)) {
1289 return;
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"
1295 "\r\n"
1296 "o=- 0 0 IN IP4 %s\r\n"
1297 "s=session\r\n"
1298 "c=IN IP4 %s\r\n"
1299 "m=audio 0 RTP/AVP\r\n",
1300 ip, ip);
1301 sipe_media_add_extra_invite_section(SIPE_MEDIA_CALL,
1302 "multipart/alternative", body);
1305 static void
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)) {
1313 return;
1316 call_private = (struct sipe_media_call_private *)
1317 sipe_media_call_new(sipe_private, with, NULL,
1318 ice_version, 0);
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,
1324 TRUE, 0)) {
1325 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
1326 _("Error occurred"),
1327 _("Error creating audio stream"));
1328 sipe_media_hangup(call_private);
1329 return;
1332 if (with_video &&
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);
1340 return;
1343 append_2007_fallback_if_needed(call_private);
1345 // Processing continues in stream_initialized_cb
1348 void
1349 sipe_core_media_initiate_call(struct sipe_core_public *sipe_public,
1350 const char *with,
1351 gboolean with_video)
1353 sipe_media_initiate_call(SIPE_CORE_PRIVATE, with,
1354 SIPE_ICE_RFC_5245, with_video);
1357 static void
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) {
1363 return;
1366 sipe_conf_announce_audio_mute_state(SIPE_MEDIA_CALL_PRIVATE->sipe_private,
1367 SIPE_MEDIA_CALL_PRIVATE->conference_session,
1368 is_muted);
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;
1379 gchar *av_uri;
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."));
1384 return;
1387 session = sipe_session_find_chat(sipe_private, chat_session);
1389 if (sipe_core_media_get_call(sipe_public) || !session) {
1390 return;
1393 av_uri = sipe_conf_build_uri(sipe_core_chat_id(sipe_public, chat_session),
1394 "audio-video");
1395 if (!av_uri) {
1396 return;
1399 session->is_call = TRUE;
1401 ice_version = SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013) ? SIPE_ICE_RFC_5245 :
1402 SIPE_ICE_DRAFT_6;
1404 call_private = (struct sipe_media_call_private *)
1405 sipe_media_call_new(sipe_private, av_uri, NULL,
1406 ice_version, 0);
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",
1411 SIPE_MEDIA_AUDIO,
1412 call_private->ice_version,
1413 TRUE, 0);
1414 if (!stream) {
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;
1424 g_free(av_uri);
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;
1436 while (entry) {
1437 if (sipe_core_media_get_stream_by_id(entry->data, "audio")) {
1438 result = entry->data;
1439 break;
1441 entry = entry->next;
1443 g_list_free(calls);
1445 return result;
1448 static gboolean phone_number_is_valid(const gchar *phone_number)
1450 if (!phone_number || sipe_strequal(phone_number, "")) {
1451 return FALSE;
1454 if (*phone_number == '+') {
1455 ++phone_number;
1458 while (*phone_number != '\0') {
1459 if (!g_ascii_isdigit(*phone_number)) {
1460 return FALSE;
1462 ++phone_number;
1465 return TRUE;
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);
1479 g_free(phone_uri);
1480 } else {
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."));
1494 return;
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,
1503 struct sipmsg *msg)
1505 return g_hash_table_lookup(sipe_private->media_calls,
1506 sipmsg_find_header(msg, "Call-ID"));
1509 static void
1510 transport_response_unsupported_sdp(struct sipe_core_private *sipe_private,
1511 struct sipmsg *msg)
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);
1519 static void
1520 maybe_send_second_invite_response(struct sipe_media_call_private *call_private)
1522 GSList *it;
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) {
1528 return;
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) {
1534 return;
1538 send_response_with_session_description(call_private, 200, "OK");
1540 #ifdef HAVE_XDATA
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);
1550 #endif
1553 struct sipe_media_call *
1554 process_incoming_invite_call(struct sipe_core_private *sipe_private,
1555 struct sipmsg *msg,
1556 const gchar *sdp)
1558 return(process_incoming_invite_call_parsed_sdp(sipe_private,
1559 msg,
1560 sdpmsg_parse_msg(sdp)));
1563 struct sipe_media_call *
1564 process_incoming_invite_call_parsed_sdp(struct sipe_core_private *sipe_private,
1565 struct sipmsg *msg,
1566 struct sdpmsg *smsg)
1568 struct sipe_media_call_private *call_private;
1569 gboolean has_new_media = FALSE;
1570 GSList *i;
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);
1580 sdpmsg_free(smsg);
1581 return NULL;
1585 call_private = sipe_media_from_sipmsg(sipe_private, msg);
1587 if (call_private) {
1588 char *self = sip_uri_self(sipe_private);
1589 if (sipe_strequal(SIPE_MEDIA_CALL->with, self)) {
1590 g_free(self);
1591 sip_transport_response(sipe_private, msg, 488, "Not Acceptable Here", NULL);
1592 sdpmsg_free(smsg);
1593 return NULL;
1595 g_free(self);
1598 if (!smsg) {
1599 transport_response_unsupported_sdp(sipe_private, msg);
1600 if (call_private) {
1601 sipe_media_hangup(call_private);
1603 return NULL;
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,
1618 flags);
1620 if (!(flags & SIPE_MEDIA_CALL_NO_UI)) {
1621 SIPE_MEDIA_CALL->call_reject_cb = av_call_reject_cb;
1623 g_free(with);
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;
1636 SipeMediaType type;
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;
1651 else
1652 continue;
1654 sipe_media_stream_add(SIPE_MEDIA_CALL, id, type,
1655 smsg->ice_version, FALSE,
1656 ssrc_count);
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
1667 } else {
1668 apply_remote_message(call_private, smsg);
1669 sdpmsg_free(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,
1677 struct sipmsg *msg)
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);
1692 static gboolean
1693 sipe_media_send_ack(struct sipe_core_private *sipe_private,
1694 struct sipmsg *msg,
1695 struct transaction *trans)
1697 struct sipe_media_call_private *call_private;
1698 struct sip_dialog *dialog;
1699 int tmp_cseq;
1701 call_private = sipe_media_from_sipmsg(sipe_private, msg);
1703 if (!is_media_session_msg(call_private, msg))
1704 return FALSE;
1706 dialog = sipe_media_get_sip_dialog(SIPE_MEDIA_CALL);
1707 if (!dialog)
1708 return FALSE;
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;
1718 return TRUE;
1721 static gboolean
1722 sipe_media_send_final_ack(struct sipe_core_private *sipe_private,
1723 struct sipmsg *msg,
1724 struct transaction *trans)
1726 struct sipe_media_call_private *call_private;
1727 #ifdef HAVE_XDATA
1728 GSList *it;
1729 #endif
1731 if (!sipe_media_send_ack(sipe_private, msg, trans))
1732 return FALSE;
1734 call_private = sipe_media_from_sipmsg(sipe_private, msg);
1736 sipe_backend_media_accept(SIPE_MEDIA_CALL->backend_private, FALSE);
1738 #ifdef HAVE_XDATA
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);
1748 #endif
1750 return TRUE;
1753 void
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.
1766 return;
1769 if (SIPE_MEDIA_STREAM_PRIVATE->established) {
1770 return;
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) {
1783 break;
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);
1792 } else {
1793 maybe_send_second_invite_response(SIPE_MEDIA_CALL_PRIVATE);
1797 static gboolean
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) {
1805 GSList *i;
1806 gchar *with;
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")) {
1813 with_video = TRUE;
1814 } else if (!sipe_strequal(stream->id, "audio")) {
1815 /* Don't retry calls which are neither audio
1816 * nor video. */
1817 return FALSE;
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,
1827 with,
1828 ice_version,
1829 with_video);
1831 g_free(with);
1832 return TRUE;
1835 return FALSE;
1838 static gboolean
1839 process_invite_call_response(struct sipe_core_private *sipe_private,
1840 struct sipmsg *msg,
1841 struct transaction *trans)
1843 const gchar *with;
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))
1851 return FALSE;
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);
1864 return TRUE;
1867 if (msg->response >= 400) {
1868 // An error occurred
1869 const gchar *title;
1870 GString *desc = g_string_new("");
1871 gboolean append_responsestr = FALSE;
1873 switch (msg->response) {
1874 case 480: {
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);
1879 } else
1880 g_string_append_printf(desc, _("User %s is not available"), with);
1881 break;
1883 case 415:
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,
1887 call_private,
1888 SIPE_ICE_DRAFT_6,
1889 trans)) {
1890 return TRUE;
1892 title = _("Unsupported media type");
1893 break;
1894 case 488: {
1895 /* Check for incompatible encryption levels error.
1897 * MS Lync 2010:
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."));
1911 break;
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,
1923 call_private,
1924 retry_ice_version,
1925 trans)) {
1926 return TRUE;
1928 SIPE_FALLTHROUGH
1930 default:
1931 title = _("Error occurred");
1932 g_string_append(desc, _("Unable to establish a call"));
1933 append_responsestr = TRUE;
1934 break;
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);
1942 if (reason) {
1943 g_string_append_printf(desc, "\n\n%s", reason);
1944 g_free(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);
1954 return TRUE;
1957 sipe_dialog_parse(dialog, msg, TRUE);
1958 smsg = sdpmsg_parse_msg(msg->body);
1959 if (!smsg) {
1960 transport_response_unsupported_sdp(sipe_private, msg);
1961 sipe_media_hangup(call_private);
1962 return FALSE;
1965 ssrc_range_update(&call_private->ssrc_ranges, smsg->media);
1966 apply_remote_message(call_private, smsg);
1967 sdpmsg_free(smsg);
1969 sipe_media_send_ack(sipe_private, msg, trans);
1971 return TRUE;
1973 // Waits until sipe_core_media_candidate_pair_established() is invoked.
1976 gboolean is_media_session_msg(struct sipe_media_call_private *call_private,
1977 struct sipmsg *msg)
1979 if (!call_private) {
1980 return FALSE;
1983 return sipe_media_from_sipmsg(call_private->sipe_private, msg) == call_private;
1986 static void
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);
2009 void
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;
2028 struct sip_dialog *
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) {
2038 return NULL;
2041 return session->dialogs->data;
2044 static void
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);
2050 g_free(relay);
2053 void
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);
2060 static void
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;
2067 if (ip && port) {
2068 relay->hostname = g_strdup(ip);
2069 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname, ip);
2070 } else {
2071 relay->hostname = NULL;
2072 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname);
2075 g_free(hostname);
2078 static gboolean
2079 process_get_av_edge_credentials_response(struct sipe_core_private *sipe_private,
2080 struct sipmsg *msg,
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.");
2093 return FALSE;
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;
2113 gchar *tmp;
2115 node = sipe_xml_child(item, "hostName");
2116 relay->hostname = sipe_xml_data(node);
2118 node = sipe_xml_child(item, "udpPort");
2119 if (node) {
2120 tmp = sipe_xml_data(node);
2121 if (tmp) {
2122 relay->udp_port = atoi(tmp);
2123 g_free(tmp);
2127 node = sipe_xml_child(item, "tcpPort");
2128 if (node) {
2129 tmp = sipe_xml_data(node);
2130 if (tmp) {
2131 relay->tcp_port = atoi(tmp);
2132 g_free(tmp);
2136 relays = g_slist_append(relays, relay);
2138 relay->dns_query = sipe_backend_dns_query_a(
2139 SIPE_CORE_PUBLIC,
2140 relay->hostname,
2141 relay->udp_port,
2142 (sipe_dns_resolved_cb) relay_ip_resolved_cb,
2143 relay);
2145 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
2146 relay->hostname,
2147 relay->tcp_port, relay->udp_port);
2150 sipe_private->media_relays = relays;
2153 sipe_xml_free(xn_response);
2156 return TRUE;
2159 void
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\" "
2165 "from=\"%s\" "
2166 "version=\"1.0\" "
2167 "to=\"%s\" "
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>"
2175 "</request>";
2177 int request_id = rand();
2178 gchar *self;
2179 gchar *body;
2181 if (!sipe_private->mras_uri)
2182 return;
2184 self = sip_uri_self(sipe_private);
2186 body = g_strdup_printf(
2187 CRED_REQUEST_XML,
2188 request_id,
2189 self,
2190 sipe_private->mras_uri,
2191 request_id,
2192 self,
2193 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER) ? "internet" : "intranet");
2194 g_free(self);
2196 sip_transport_service(sipe_private,
2197 sipe_private->mras_uri,
2198 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
2199 body,
2200 process_get_av_edge_credentials_response);
2202 g_free(body);
2205 void
2206 sipe_media_add_extra_invite_section(struct sipe_media_call *call,
2207 const gchar *invite_content_type,
2208 gchar *body)
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);
2217 void
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,
2223 name, value);
2226 #ifdef HAVE_XDATA
2227 void
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) &&
2233 stream->read_cb) {
2234 stream->read_cb(stream);
2237 while (!g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE->async_reads)) {
2238 struct async_read_data *data;
2239 guint8 *pos;
2240 gssize len;
2241 gssize bytes_read;
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,
2254 _("Media error"),
2255 _("Error while reading from stream"));
2256 sipe_media_hangup(SIPE_MEDIA_CALL_PRIVATE);
2257 return;
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);
2266 g_free(data);
2267 } else {
2268 // Still not enough data to finish the read.
2269 return;
2274 void
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;
2285 data->len = len;
2286 data->callback = callback;
2288 g_queue_push_tail(SIPE_MEDIA_STREAM_PRIVATE->async_reads, data);
2291 static void
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);
2300 gboolean
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);
2306 return FALSE;
2307 } else {
2308 guint written;
2310 written = sipe_backend_media_stream_write(stream, buffer, len);
2311 if (written == len) {
2312 return TRUE;
2315 stream_append_buffer(stream,
2316 (guint8 *)buffer + written, len - written);
2317 return FALSE;
2321 void
2322 sipe_core_media_stream_writable(struct sipe_media_stream *stream,
2323 gboolean writable)
2325 SIPE_MEDIA_STREAM_PRIVATE->writable = writable;
2327 if (!writable) {
2328 return;
2331 while (!g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE->write_queue)) {
2332 GByteArray *b;
2333 guint written;
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);
2340 return;
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);
2352 gboolean
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);
2359 #endif
2361 void
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;
2378 gpointer
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;
2387 Local Variables:
2388 mode: c
2389 c-file-style: "bsd"
2390 indent-tabs-mode: t
2391 tab-width: 8
2392 End: