l10n: sync translations with transifex.com (es, pt_BR)
[siplcs.git] / src / core / sipe-media.c
blob2401b25929ce34a8c8997d9ff0827586613b8169
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 = g_strcmp0(c1->foundation, c2->foundation);
200 if (cmp == 0) {
201 cmp = g_strcmp0(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 *marker = sip_transport_sdp_address_marker(sipe_private);
1283 const gchar *ip = sip_transport_ip_address(sipe_private);
1284 gchar *body;
1286 if (SIPE_CORE_PRIVATE_FLAG_IS(SFB) ||
1287 sipe_media_get_sip_dialog(SIPE_MEDIA_CALL)->cseq != 0 ||
1288 call_private->ice_version != SIPE_ICE_RFC_5245 ||
1289 sipe_strequal(SIPE_MEDIA_CALL->with, sipe_private->test_call_bot_uri)) {
1290 return;
1293 body = g_strdup_printf("Content-Type: application/sdp\r\n"
1294 "Content-Transfer-Encoding: 7bit\r\n"
1295 "Content-Disposition: session; handling=optional; ms-proxy-2007fallback\r\n"
1296 "\r\n"
1297 "o=- 0 0 IN %s %s\r\n"
1298 "s=session\r\n"
1299 "c=IN %s %s\r\n"
1300 "m=audio 0 RTP/AVP\r\n",
1301 marker, ip,
1302 marker, ip);
1303 sipe_media_add_extra_invite_section(SIPE_MEDIA_CALL,
1304 "multipart/alternative", body);
1307 static void
1308 sipe_media_initiate_call(struct sipe_core_private *sipe_private,
1309 const char *with, SipeIceVersion ice_version,
1310 gboolean with_video)
1312 struct sipe_media_call_private *call_private;
1314 if (sipe_core_media_get_call(SIPE_CORE_PUBLIC)) {
1315 return;
1318 call_private = (struct sipe_media_call_private *)
1319 sipe_media_call_new(sipe_private, with, NULL,
1320 ice_version, 0);
1322 SIPE_MEDIA_CALL->call_reject_cb = av_call_reject_cb;
1324 if (!sipe_media_stream_add(SIPE_MEDIA_CALL, "audio", SIPE_MEDIA_AUDIO,
1325 call_private->ice_version,
1326 TRUE, 0)) {
1327 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
1328 _("Error occurred"),
1329 _("Error creating audio stream"));
1330 sipe_media_hangup(call_private);
1331 return;
1334 if (with_video &&
1335 !sipe_media_stream_add(SIPE_MEDIA_CALL, "video", SIPE_MEDIA_VIDEO,
1336 call_private->ice_version,
1337 TRUE, VIDEO_SSRC_COUNT)) {
1338 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
1339 _("Error occurred"),
1340 _("Error creating video stream"));
1341 sipe_media_hangup(call_private);
1342 return;
1345 append_2007_fallback_if_needed(call_private);
1347 // Processing continues in stream_initialized_cb
1350 void
1351 sipe_core_media_initiate_call(struct sipe_core_public *sipe_public,
1352 const char *with,
1353 gboolean with_video)
1355 sipe_media_initiate_call(SIPE_CORE_PRIVATE, with,
1356 SIPE_ICE_RFC_5245, with_video);
1359 static void
1360 conference_audio_muted_cb(struct sipe_media_stream *stream, gboolean is_muted)
1362 struct sipe_media_call *call = stream->call;
1364 if (!SIPE_MEDIA_CALL_PRIVATE->conference_session) {
1365 return;
1368 sipe_conf_announce_audio_mute_state(SIPE_MEDIA_CALL_PRIVATE->sipe_private,
1369 SIPE_MEDIA_CALL_PRIVATE->conference_session,
1370 is_muted);
1373 void sipe_core_media_connect_conference(struct sipe_core_public *sipe_public,
1374 struct sipe_chat_session *chat_session)
1376 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1377 struct sipe_media_call_private *call_private;
1378 struct sipe_media_stream *stream;
1379 struct sip_session *session;
1380 SipeIceVersion ice_version;
1381 gchar *av_uri;
1383 if (!sipe_conf_supports_mcu_type(sipe_private, "audio-video")) {
1384 sipe_backend_notify_error(sipe_public, _("Join conference call"),
1385 _("Conference calls are not supported on this server."));
1386 return;
1389 session = sipe_session_find_chat(sipe_private, chat_session);
1391 if (sipe_core_media_get_call(sipe_public) || !session) {
1392 return;
1395 av_uri = sipe_conf_build_uri(sipe_core_chat_id(sipe_public, chat_session),
1396 "audio-video");
1397 if (!av_uri) {
1398 return;
1401 session->is_call = TRUE;
1403 ice_version = SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013) ? SIPE_ICE_RFC_5245 :
1404 SIPE_ICE_DRAFT_6;
1406 call_private = (struct sipe_media_call_private *)
1407 sipe_media_call_new(sipe_private, av_uri, NULL,
1408 ice_version, 0);
1409 call_private->conference_session = session;
1410 SIPE_MEDIA_CALL->call_reject_cb = av_call_reject_cb;
1412 stream = sipe_media_stream_add(SIPE_MEDIA_CALL, "audio",
1413 SIPE_MEDIA_AUDIO,
1414 call_private->ice_version,
1415 TRUE, 0);
1416 if (!stream) {
1417 sipe_backend_notify_error(sipe_public,
1418 _("Error occurred"),
1419 _("Error creating audio stream"));
1421 sipe_media_hangup(call_private);
1424 stream->mute_cb = conference_audio_muted_cb;
1426 g_free(av_uri);
1428 // Processing continues in stream_initialized_cb
1431 struct sipe_media_call *
1432 sipe_core_media_get_call(struct sipe_core_public *sipe_public)
1434 struct sipe_media_call * result = NULL;
1435 GList *calls = g_hash_table_get_values(SIPE_CORE_PRIVATE->media_calls);
1436 GList *entry = calls;
1438 while (entry) {
1439 if (sipe_core_media_get_stream_by_id(entry->data, "audio")) {
1440 result = entry->data;
1441 break;
1443 entry = entry->next;
1445 g_list_free(calls);
1447 return result;
1450 static gboolean phone_number_is_valid(const gchar *phone_number)
1452 if (!phone_number || sipe_strequal(phone_number, "")) {
1453 return FALSE;
1456 if (*phone_number == '+') {
1457 ++phone_number;
1460 while (*phone_number != '\0') {
1461 if (!g_ascii_isdigit(*phone_number)) {
1462 return FALSE;
1464 ++phone_number;
1467 return TRUE;
1470 void sipe_core_media_phone_call(struct sipe_core_public *sipe_public,
1471 const gchar *phone_number)
1473 g_return_if_fail(sipe_public);
1475 SIPE_DEBUG_INFO("sipe_core_media_phone_call: %s", phone_number ? phone_number : "(null)");
1477 if (phone_number_is_valid(phone_number)) {
1478 gchar *phone_uri = g_strdup_printf("sip:%s@%s;user=phone",
1479 phone_number, sipe_public->sip_domain);
1481 sipe_core_media_initiate_call(sipe_public, phone_uri, FALSE);
1483 g_free(phone_uri);
1484 } else {
1485 sipe_backend_notify_error(sipe_public,
1486 _("Unable to establish a call"),
1487 _("Invalid phone number"));
1491 void sipe_core_media_test_call(struct sipe_core_public *sipe_public)
1493 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1494 if (!sipe_private->test_call_bot_uri) {
1495 sipe_backend_notify_error(sipe_public,
1496 _("Unable to establish a call"),
1497 _("Audio Test Service is not available."));
1498 return;
1501 sipe_core_media_initiate_call(sipe_public,
1502 sipe_private->test_call_bot_uri, FALSE);
1505 static struct sipe_media_call_private *
1506 sipe_media_from_sipmsg(struct sipe_core_private *sipe_private,
1507 struct sipmsg *msg)
1509 return g_hash_table_lookup(sipe_private->media_calls,
1510 sipmsg_find_header(msg, "Call-ID"));
1513 static void
1514 transport_response_unsupported_sdp(struct sipe_core_private *sipe_private,
1515 struct sipmsg *msg)
1517 sipmsg_add_header(msg, "ms-client-diagnostics",
1518 "52063;reason=\"Unsupported session description\"");
1519 sip_transport_response(sipe_private, msg,
1520 488, "Not Acceptable Here", NULL);
1523 static void
1524 maybe_send_second_invite_response(struct sipe_media_call_private *call_private)
1526 GSList *it;
1528 /* Second INVITE request had to be received and all streams must have
1529 * established candidate pairs before the response can be sent. */
1531 if (!call_private->invitation) {
1532 return;
1535 for (it = call_private->streams; it; it = it->next) {
1536 struct sipe_media_stream_private *stream_private = it->data;
1537 if (!stream_private->established) {
1538 return;
1542 send_response_with_session_description(call_private, 200, "OK");
1544 #ifdef HAVE_XDATA
1545 for (it = call_private->streams; it; it = it->next) {
1546 struct sipe_media_stream_private *stream_private = it->data;
1548 stream_private->sdp_negotiation_concluded = TRUE;
1549 if (stream_private->writable) {
1550 // We've become writable.
1551 sipe_core_media_stream_writable(SIPE_MEDIA_STREAM, TRUE);
1554 #endif
1557 struct sipe_media_call *
1558 process_incoming_invite_call(struct sipe_core_private *sipe_private,
1559 struct sipmsg *msg,
1560 const gchar *sdp)
1562 return(process_incoming_invite_call_parsed_sdp(sipe_private,
1563 msg,
1564 sdpmsg_parse_msg(sdp)));
1567 struct sipe_media_call *
1568 process_incoming_invite_call_parsed_sdp(struct sipe_core_private *sipe_private,
1569 struct sipmsg *msg,
1570 struct sdpmsg *smsg)
1572 struct sipe_media_call_private *call_private;
1573 gboolean has_new_media = FALSE;
1574 GSList *i;
1576 // Don't allow two voice calls in parallel.
1577 if (!strstr(msg->body, "m=data") &&
1578 !strstr(msg->body, "m=applicationsharing")) {
1579 struct sipe_media_call *call =
1580 sipe_core_media_get_call(SIPE_CORE_PUBLIC);
1581 if (call && !is_media_session_msg(SIPE_MEDIA_CALL_PRIVATE, msg)) {
1582 sip_transport_response(sipe_private, msg,
1583 486, "Busy Here", NULL);
1584 sdpmsg_free(smsg);
1585 return NULL;
1589 call_private = sipe_media_from_sipmsg(sipe_private, msg);
1591 if (call_private) {
1592 char *self = sip_uri_self(sipe_private);
1593 if (sipe_strequal(SIPE_MEDIA_CALL->with, self)) {
1594 g_free(self);
1595 sip_transport_response(sipe_private, msg, 488, "Not Acceptable Here", NULL);
1596 sdpmsg_free(smsg);
1597 return NULL;
1599 g_free(self);
1602 if (!smsg) {
1603 transport_response_unsupported_sdp(sipe_private, msg);
1604 if (call_private) {
1605 sipe_media_hangup(call_private);
1607 return NULL;
1610 if (!call_private) {
1611 gchar *with = parse_from(sipmsg_find_header(msg, "From"));
1612 SipeMediaCallFlags flags = 0;
1614 if (strstr(msg->body, "m=data") ||
1615 strstr(msg->body, "m=applicationsharing")) {
1616 flags |= SIPE_MEDIA_CALL_NO_UI;
1619 call_private = (struct sipe_media_call_private *)
1620 sipe_media_call_new(sipe_private, with,
1621 msg, smsg->ice_version,
1622 flags);
1624 if (!(flags & SIPE_MEDIA_CALL_NO_UI)) {
1625 SIPE_MEDIA_CALL->call_reject_cb = av_call_reject_cb;
1627 g_free(with);
1630 if (call_private->invitation)
1631 sipmsg_free(call_private->invitation);
1632 call_private->invitation = sipmsg_copy(msg);
1634 ssrc_range_update(&call_private->ssrc_ranges, smsg->media);
1636 // Create any new media streams
1637 for (i = smsg->media; i; i = i->next) {
1638 struct sdpmedia *media = i->data;
1639 gchar *id = media->name;
1640 SipeMediaType type;
1642 if ( media->port != 0
1643 && !sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL, id)) {
1644 guint32 ssrc_count = 0;
1646 if (sipe_strequal(id, "audio"))
1647 type = SIPE_MEDIA_AUDIO;
1648 else if (sipe_strequal(id, "video")) {
1649 type = SIPE_MEDIA_VIDEO;
1650 ssrc_count = VIDEO_SSRC_COUNT;
1651 } else if (sipe_strequal(id, "data"))
1652 type = SIPE_MEDIA_APPLICATION;
1653 else if (sipe_strequal(id, "applicationsharing"))
1654 type = SIPE_MEDIA_APPLICATION;
1655 else
1656 continue;
1658 sipe_media_stream_add(SIPE_MEDIA_CALL, id, type,
1659 smsg->ice_version, FALSE,
1660 ssrc_count);
1661 has_new_media = TRUE;
1665 if (has_new_media) {
1666 sdpmsg_free(call_private->smsg);
1667 call_private->smsg = smsg;
1668 sip_transport_response(sipe_private, call_private->invitation,
1669 180, "Ringing", NULL);
1670 // Processing continues in stream_initialized_cb
1671 } else {
1672 apply_remote_message(call_private, smsg);
1673 sdpmsg_free(smsg);
1674 maybe_send_second_invite_response(call_private);
1677 return SIPE_MEDIA_CALL;
1680 void process_incoming_cancel_call(struct sipe_media_call_private *call_private,
1681 struct sipmsg *msg)
1683 // We respond to the CANCEL request with 200 OK response and
1684 // with 487 Request Terminated to the remote INVITE in progress.
1685 sip_transport_response(call_private->sipe_private, msg, 200, "OK", NULL);
1687 if (call_private->invitation) {
1688 sip_transport_response(call_private->sipe_private,
1689 call_private->invitation,
1690 487, "Request Terminated", NULL);
1693 sipe_backend_media_reject(SIPE_MEDIA_CALL->backend_private, FALSE);
1696 static gboolean
1697 sipe_media_send_ack(struct sipe_core_private *sipe_private,
1698 struct sipmsg *msg,
1699 struct transaction *trans)
1701 struct sipe_media_call_private *call_private;
1702 struct sip_dialog *dialog;
1703 int tmp_cseq;
1705 call_private = sipe_media_from_sipmsg(sipe_private, msg);
1707 if (!is_media_session_msg(call_private, msg))
1708 return FALSE;
1710 dialog = sipe_media_get_sip_dialog(SIPE_MEDIA_CALL);
1711 if (!dialog)
1712 return FALSE;
1714 tmp_cseq = dialog->cseq;
1716 dialog->cseq = sip_transaction_cseq(trans) - 1;
1717 sip_transport_ack(sipe_private, dialog);
1718 dialog->cseq = tmp_cseq;
1720 dialog->outgoing_invite = NULL;
1722 return TRUE;
1725 static gboolean
1726 sipe_media_send_final_ack(struct sipe_core_private *sipe_private,
1727 struct sipmsg *msg,
1728 struct transaction *trans)
1730 struct sipe_media_call_private *call_private;
1731 #ifdef HAVE_XDATA
1732 GSList *it;
1733 #endif
1735 if (!sipe_media_send_ack(sipe_private, msg, trans))
1736 return FALSE;
1738 call_private = sipe_media_from_sipmsg(sipe_private, msg);
1740 sipe_backend_media_accept(SIPE_MEDIA_CALL->backend_private, FALSE);
1742 #ifdef HAVE_XDATA
1743 for (it = call_private->streams; it; it = it->next) {
1744 struct sipe_media_stream_private *stream_private = it->data;
1746 stream_private->sdp_negotiation_concluded = TRUE;
1747 if (stream_private->writable) {
1748 // We've become writable.
1749 sipe_core_media_stream_writable(SIPE_MEDIA_STREAM, TRUE);
1752 #endif
1754 return TRUE;
1757 void
1758 sipe_core_media_stream_candidate_pair_established(struct sipe_media_stream *stream)
1760 struct sipe_media_call *call = stream->call;
1762 GList *active_candidates =
1763 sipe_backend_media_stream_get_active_local_candidates(stream);
1764 guint ready_components = g_list_length(active_candidates);
1766 sipe_media_candidate_list_free(active_candidates);
1768 if (ready_components != 2) {
1769 // We must have both RTP+RTCP candidate pairs established first.
1770 return;
1773 if (SIPE_MEDIA_STREAM_PRIVATE->established) {
1774 return;
1776 SIPE_MEDIA_STREAM_PRIVATE->established = TRUE;
1778 if (stream->candidate_pairs_established_cb) {
1779 stream->candidate_pairs_established_cb(stream);
1782 if (sipe_backend_media_is_initiator(stream->call, NULL)) {
1783 GSList *streams = SIPE_MEDIA_CALL_PRIVATE->streams;
1784 for (; streams; streams = streams->next) {
1785 struct sipe_media_stream_private *s = streams->data;
1786 if (!s->established) {
1787 break;
1791 if (streams == NULL) {
1792 // All call streams have been established.
1793 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE,
1794 sipe_media_send_final_ack);
1796 } else {
1797 maybe_send_second_invite_response(SIPE_MEDIA_CALL_PRIVATE);
1801 static gboolean
1802 maybe_retry_call_with_ice_version(struct sipe_core_private *sipe_private,
1803 struct sipe_media_call_private *call_private,
1804 SipeIceVersion ice_version,
1805 struct transaction *trans)
1807 if (call_private->ice_version != ice_version &&
1808 sip_transaction_cseq(trans) == 1) {
1809 GSList *i;
1810 gchar *with;
1811 gboolean with_video = FALSE;
1813 for (i = call_private->streams; i; i = i->next) {
1814 struct sipe_media_stream *stream = i->data;
1816 if (sipe_strequal(stream->id, "video")) {
1817 with_video = TRUE;
1818 } else if (!sipe_strequal(stream->id, "audio")) {
1819 /* Don't retry calls which are neither audio
1820 * nor video. */
1821 return FALSE;
1825 with = g_strdup(SIPE_MEDIA_CALL->with);
1827 sipe_media_hangup(call_private);
1828 SIPE_DEBUG_INFO("Retrying call with ICEv%d.",
1829 ice_version == SIPE_ICE_DRAFT_6 ? 6 : 19);
1830 sipe_media_initiate_call(sipe_private,
1831 with,
1832 ice_version,
1833 with_video);
1835 g_free(with);
1836 return TRUE;
1839 return FALSE;
1842 static gboolean
1843 process_invite_call_response(struct sipe_core_private *sipe_private,
1844 struct sipmsg *msg,
1845 struct transaction *trans)
1847 const gchar *with;
1848 struct sipe_media_call_private *call_private;
1849 struct sip_dialog *dialog;
1850 struct sdpmsg *smsg;
1852 call_private = sipe_media_from_sipmsg(sipe_private,msg);
1854 if (!is_media_session_msg(call_private, msg))
1855 return FALSE;
1857 dialog = sipe_media_get_sip_dialog(SIPE_MEDIA_CALL);
1859 with = dialog->with;
1861 dialog->outgoing_invite = NULL;
1863 if (msg->response == 603 || msg->response == 605) {
1864 // Call rejected by remote peer
1865 sipe_media_send_ack(sipe_private, msg, trans);
1866 sipe_backend_media_reject(SIPE_MEDIA_CALL->backend_private, FALSE);
1868 return TRUE;
1871 if (msg->response >= 400) {
1872 // An error occurred
1873 const gchar *title;
1874 GString *desc = g_string_new("");
1875 gboolean append_responsestr = FALSE;
1877 switch (msg->response) {
1878 case 480: {
1879 title = _("User unavailable");
1881 if (sipmsg_parse_warning(msg, NULL) == 391) {
1882 g_string_append_printf(desc, _("%s does not want to be disturbed"), with);
1883 } else
1884 g_string_append_printf(desc, _("User %s is not available"), with);
1885 break;
1887 case 415:
1888 // OCS/Lync really sends response string with 'Mutipart' typo.
1889 if (sipe_strequal(msg->responsestr, "Mutipart mime in content type not supported by Archiving CDR service") &&
1890 maybe_retry_call_with_ice_version(sipe_private,
1891 call_private,
1892 SIPE_ICE_DRAFT_6,
1893 trans)) {
1894 return TRUE;
1896 title = _("Unsupported media type");
1897 break;
1898 case 488: {
1899 /* Check for incompatible encryption levels error.
1901 * MS Lync 2010:
1902 * 488 Not Acceptable Here
1903 * ms-client-diagnostics: 52017;reason="Encryption levels dont match"
1905 * older clients (and SIPE itself):
1906 * 488 Encryption Levels not compatible
1908 const gchar *ms_diag = sipmsg_find_header(msg, "ms-client-diagnostics");
1909 SipeIceVersion retry_ice_version = SIPE_ICE_DRAFT_6;
1911 if (sipe_strequal(msg->responsestr, "Encryption Levels not compatible") ||
1912 (ms_diag && g_str_has_prefix(ms_diag, "52017;"))) {
1913 title = _("Unable to establish a call");
1914 g_string_append(desc, _("Encryption settings of peer are incompatible with ours."));
1915 break;
1918 /* Check if this is failed conference using
1919 * ICEv6 with reason "Error parsing SDP" and
1920 * retry using ICEv19. */
1921 ms_diag = sipmsg_find_header(msg, "ms-diagnostics");
1922 if (ms_diag && g_str_has_prefix(ms_diag, "7008;")) {
1923 retry_ice_version = SIPE_ICE_RFC_5245;
1926 if (maybe_retry_call_with_ice_version(sipe_private,
1927 call_private,
1928 retry_ice_version,
1929 trans)) {
1930 return TRUE;
1932 SIPE_FALLTHROUGH
1934 default:
1935 title = _("Error occurred");
1936 g_string_append(desc, _("Unable to establish a call"));
1937 append_responsestr = TRUE;
1938 break;
1941 if (append_responsestr) {
1942 gchar *reason = sipmsg_get_ms_diagnostics_reason(msg);
1944 g_string_append_printf(desc, "\n%d %s",
1945 msg->response, msg->responsestr);
1946 if (reason) {
1947 g_string_append_printf(desc, "\n\n%s", reason);
1948 g_free(reason);
1952 sipe_backend_notify_error(SIPE_CORE_PUBLIC, title, desc->str);
1953 g_string_free(desc, TRUE);
1955 sipe_media_send_ack(sipe_private, msg, trans);
1956 sipe_media_hangup(call_private);
1958 return TRUE;
1961 sipe_dialog_parse(dialog, msg, TRUE);
1962 smsg = sdpmsg_parse_msg(msg->body);
1963 if (!smsg) {
1964 transport_response_unsupported_sdp(sipe_private, msg);
1965 sipe_media_hangup(call_private);
1966 return FALSE;
1969 ssrc_range_update(&call_private->ssrc_ranges, smsg->media);
1970 apply_remote_message(call_private, smsg);
1971 sdpmsg_free(smsg);
1973 sipe_media_send_ack(sipe_private, msg, trans);
1975 return TRUE;
1977 // Waits until sipe_core_media_candidate_pair_established() is invoked.
1980 gboolean is_media_session_msg(struct sipe_media_call_private *call_private,
1981 struct sipmsg *msg)
1983 if (!call_private) {
1984 return FALSE;
1987 return sipe_media_from_sipmsg(call_private->sipe_private, msg) == call_private;
1990 static void
1991 end_call(SIPE_UNUSED_PARAMETER gpointer key,
1992 struct sipe_media_call_private *call_private,
1993 SIPE_UNUSED_PARAMETER gpointer user_data)
1995 struct sipe_backend_media *backend_private;
1997 backend_private = call_private->public.backend_private;
1999 if (!sipe_backend_media_is_initiator(SIPE_MEDIA_CALL, NULL) &&
2000 !sipe_backend_media_accepted(backend_private)) {
2001 sip_transport_response(call_private->sipe_private,
2002 call_private->invitation,
2003 480, "Temporarily Unavailable", NULL);
2004 } else if (call_private->session) {
2005 sipe_session_close(call_private->sipe_private,
2006 call_private->session);
2007 call_private->session = NULL;
2010 sipe_media_hangup(call_private);
2013 void
2014 sipe_media_handle_going_offline(struct sipe_core_private *sipe_private)
2016 g_hash_table_foreach(sipe_private->media_calls, (GHFunc) end_call, NULL);
2019 gboolean sipe_media_is_conference_call(struct sipe_media_call_private *call_private)
2021 return g_strstr_len(SIPE_MEDIA_CALL->with, -1, "app:conf:audio-video:") != NULL;
2024 struct sipe_core_private *
2025 sipe_media_get_sipe_core_private(struct sipe_media_call *call)
2027 g_return_val_if_fail(call, NULL);
2029 return SIPE_MEDIA_CALL_PRIVATE->sipe_private;
2032 struct sip_dialog *
2033 sipe_media_get_sip_dialog(struct sipe_media_call *call)
2035 struct sip_session *session;
2037 g_return_val_if_fail(call, NULL);
2039 session = SIPE_MEDIA_CALL_PRIVATE->session;
2041 if (!session || !session->dialogs) {
2042 return NULL;
2045 return session->dialogs->data;
2048 static void
2049 sipe_media_relay_free(struct sipe_media_relay *relay)
2051 g_free(relay->hostname);
2052 if (relay->dns_query)
2053 sipe_backend_dns_query_cancel(relay->dns_query);
2054 g_free(relay);
2057 void
2058 sipe_media_relay_list_free(GSList *list)
2060 for (; list; list = g_slist_delete_link(list, list))
2061 sipe_media_relay_free(list->data);
2064 static void
2065 relay_ip_resolved_cb(struct sipe_media_relay* relay,
2066 const gchar *ip, SIPE_UNUSED_PARAMETER guint port)
2068 gchar *hostname = relay->hostname;
2069 relay->dns_query = NULL;
2071 if (ip && port) {
2072 relay->hostname = g_strdup(ip);
2073 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname, ip);
2074 } else {
2075 relay->hostname = NULL;
2076 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname);
2079 g_free(hostname);
2082 static gboolean
2083 process_get_av_edge_credentials_response(struct sipe_core_private *sipe_private,
2084 struct sipmsg *msg,
2085 SIPE_UNUSED_PARAMETER struct transaction *trans)
2087 g_free(sipe_private->media_relay_username);
2088 g_free(sipe_private->media_relay_password);
2089 sipe_media_relay_list_free(sipe_private->media_relays);
2090 sipe_private->media_relay_username = NULL;
2091 sipe_private->media_relay_password = NULL;
2092 sipe_private->media_relays = NULL;
2094 if (msg->response >= 400) {
2095 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
2096 "Failed to obtain A/V Edge credentials.");
2097 return FALSE;
2100 if (msg->response == 200) {
2101 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
2103 if (sipe_strequal("OK", sipe_xml_attribute(xn_response, "reasonPhrase"))) {
2104 const sipe_xml *xn_credentials = sipe_xml_child(xn_response, "credentialsResponse/credentials");
2105 const sipe_xml *xn_relays = sipe_xml_child(xn_response, "credentialsResponse/mediaRelayList");
2106 const sipe_xml *item;
2107 GSList *relays = NULL;
2109 item = sipe_xml_child(xn_credentials, "username");
2110 sipe_private->media_relay_username = sipe_xml_data(item);
2111 item = sipe_xml_child(xn_credentials, "password");
2112 sipe_private->media_relay_password = sipe_xml_data(item);
2114 for (item = sipe_xml_child(xn_relays, "mediaRelay"); item; item = sipe_xml_twin(item)) {
2115 struct sipe_media_relay *relay = g_new0(struct sipe_media_relay, 1);
2116 const sipe_xml *node;
2117 gchar *tmp;
2119 node = sipe_xml_child(item, "hostName");
2120 relay->hostname = sipe_xml_data(node);
2122 node = sipe_xml_child(item, "udpPort");
2123 if (node) {
2124 tmp = sipe_xml_data(node);
2125 if (tmp) {
2126 relay->udp_port = atoi(tmp);
2127 g_free(tmp);
2131 node = sipe_xml_child(item, "tcpPort");
2132 if (node) {
2133 tmp = sipe_xml_data(node);
2134 if (tmp) {
2135 relay->tcp_port = atoi(tmp);
2136 g_free(tmp);
2140 relays = g_slist_append(relays, relay);
2142 relay->dns_query = sipe_backend_dns_query_a(
2143 SIPE_CORE_PUBLIC,
2144 relay->hostname,
2145 relay->udp_port,
2146 (sipe_dns_resolved_cb) relay_ip_resolved_cb,
2147 relay);
2149 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
2150 relay->hostname,
2151 relay->tcp_port, relay->udp_port);
2154 sipe_private->media_relays = relays;
2157 sipe_xml_free(xn_response);
2160 return TRUE;
2163 void
2164 sipe_media_get_av_edge_credentials(struct sipe_core_private *sipe_private)
2166 // TODO: re-request credentials after duration expires?
2167 static const char CRED_REQUEST_XML[] =
2168 "<request requestID=\"%d\" "
2169 "from=\"%s\" "
2170 "version=\"1.0\" "
2171 "to=\"%s\" "
2172 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
2173 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
2174 "<credentialsRequest credentialsRequestID=\"%d\">"
2175 "<identity>%s</identity>"
2176 "<location>%s</location>"
2177 "<duration>480</duration>"
2178 "</credentialsRequest>"
2179 "</request>";
2181 int request_id = rand();
2182 gchar *self;
2183 gchar *body;
2185 if (!sipe_private->mras_uri)
2186 return;
2188 self = sip_uri_self(sipe_private);
2190 body = g_strdup_printf(
2191 CRED_REQUEST_XML,
2192 request_id,
2193 self,
2194 sipe_private->mras_uri,
2195 request_id,
2196 self,
2197 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER) ? "internet" : "intranet");
2198 g_free(self);
2200 sip_transport_service(sipe_private,
2201 sipe_private->mras_uri,
2202 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
2203 body,
2204 process_get_av_edge_credentials_response);
2206 g_free(body);
2209 void
2210 sipe_media_add_extra_invite_section(struct sipe_media_call *call,
2211 const gchar *invite_content_type,
2212 gchar *body)
2214 g_free(SIPE_MEDIA_CALL_PRIVATE->extra_invite_section);
2215 g_free(SIPE_MEDIA_CALL_PRIVATE->invite_content_type);
2216 SIPE_MEDIA_CALL_PRIVATE->extra_invite_section = body;
2217 SIPE_MEDIA_CALL_PRIVATE->invite_content_type =
2218 g_strdup(invite_content_type);
2221 void
2222 sipe_media_stream_add_extra_attribute(struct sipe_media_stream *stream,
2223 const gchar *name, const gchar *value)
2225 SIPE_MEDIA_STREAM_PRIVATE->extra_sdp =
2226 sipe_utils_nameval_add(SIPE_MEDIA_STREAM_PRIVATE->extra_sdp,
2227 name, value);
2230 #ifdef HAVE_XDATA
2231 void
2232 sipe_core_media_stream_readable(struct sipe_media_stream *stream)
2234 g_return_if_fail(stream);
2236 if (g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE->async_reads) &&
2237 stream->read_cb) {
2238 stream->read_cb(stream);
2241 while (!g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE->async_reads)) {
2242 struct async_read_data *data;
2243 guint8 *pos;
2244 gssize len;
2245 gssize bytes_read;
2247 data = g_queue_peek_head(SIPE_MEDIA_STREAM_PRIVATE->async_reads);
2248 pos = data->buffer + SIPE_MEDIA_STREAM_PRIVATE->read_pos;
2249 len = data->len - SIPE_MEDIA_STREAM_PRIVATE->read_pos;
2251 bytes_read = sipe_backend_media_stream_read(stream, pos, len);
2252 if (bytes_read == -1) {
2253 struct sipe_media_call *call = stream->call;
2254 struct sipe_core_private *sipe_private =
2255 SIPE_MEDIA_CALL_PRIVATE->sipe_private;
2257 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
2258 _("Media error"),
2259 _("Error while reading from stream"));
2260 sipe_media_hangup(SIPE_MEDIA_CALL_PRIVATE);
2261 return;
2264 SIPE_MEDIA_STREAM_PRIVATE->read_pos += bytes_read;
2266 if (SIPE_MEDIA_STREAM_PRIVATE->read_pos == data->len) {
2267 data->callback(stream, data->buffer, data->len);
2268 SIPE_MEDIA_STREAM_PRIVATE->read_pos = 0;
2269 g_queue_pop_head(SIPE_MEDIA_STREAM_PRIVATE->async_reads);
2270 g_free(data);
2271 } else {
2272 // Still not enough data to finish the read.
2273 return;
2278 void
2279 sipe_media_stream_read_async(struct sipe_media_stream *stream,
2280 gpointer buffer, gsize len,
2281 sipe_media_stream_read_callback callback)
2283 struct async_read_data *data;
2285 g_return_if_fail(stream && buffer && callback);
2287 data = g_new0(struct async_read_data, 1);
2288 data->buffer = buffer;
2289 data->len = len;
2290 data->callback = callback;
2292 g_queue_push_tail(SIPE_MEDIA_STREAM_PRIVATE->async_reads, data);
2295 static void
2296 stream_append_buffer(struct sipe_media_stream *stream,
2297 guint8 *buffer, guint len)
2299 GByteArray *b = g_byte_array_sized_new(len);
2300 g_byte_array_append(b, buffer, len);
2301 g_queue_push_tail(SIPE_MEDIA_STREAM_PRIVATE->write_queue, b);
2304 gboolean
2305 sipe_media_stream_write(struct sipe_media_stream *stream,
2306 gpointer buffer, gsize len)
2308 if (!sipe_media_stream_is_writable(stream)) {
2309 stream_append_buffer(stream, buffer, len);
2310 return FALSE;
2311 } else {
2312 guint written;
2314 written = sipe_backend_media_stream_write(stream, buffer, len);
2315 if (written == len) {
2316 return TRUE;
2319 stream_append_buffer(stream,
2320 (guint8 *)buffer + written, len - written);
2321 return FALSE;
2325 void
2326 sipe_core_media_stream_writable(struct sipe_media_stream *stream,
2327 gboolean writable)
2329 SIPE_MEDIA_STREAM_PRIVATE->writable = writable;
2331 if (!writable) {
2332 return;
2335 while (!g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE->write_queue)) {
2336 GByteArray *b;
2337 guint written;
2339 b = g_queue_peek_head(SIPE_MEDIA_STREAM_PRIVATE->write_queue);
2341 written = sipe_backend_media_stream_write(stream, b->data, b->len);
2342 if (written != b->len) {
2343 g_byte_array_remove_range(b, 0, written);
2344 return;
2347 g_byte_array_unref(b);
2348 g_queue_pop_head(SIPE_MEDIA_STREAM_PRIVATE->write_queue);
2351 if (sipe_media_stream_is_writable(stream) && stream->writable_cb) {
2352 stream->writable_cb(stream);
2356 gboolean
2357 sipe_media_stream_is_writable(struct sipe_media_stream *stream)
2359 return SIPE_MEDIA_STREAM_PRIVATE->writable &&
2360 SIPE_MEDIA_STREAM_PRIVATE->sdp_negotiation_concluded &&
2361 g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE->write_queue);
2363 #endif
2365 void
2366 sipe_media_stream_set_data(struct sipe_media_stream *stream, gpointer data,
2367 GDestroyNotify free_func)
2369 struct sipe_media_stream_private *stream_private =
2370 SIPE_MEDIA_STREAM_PRIVATE;
2372 g_return_if_fail(stream_private);
2374 if (stream_private->data && stream_private->data_free_func) {
2375 stream_private->data_free_func(stream_private->data);
2378 stream_private->data = data;
2379 stream_private->data_free_func = free_func;
2382 gpointer
2383 sipe_media_stream_get_data(struct sipe_media_stream *stream)
2385 g_return_val_if_fail(stream, NULL);
2387 return SIPE_MEDIA_STREAM_PRIVATE->data;
2391 Local Variables:
2392 mode: c
2393 c-file-style: "bsd"
2394 indent-tabs-mode: t
2395 tab-width: 8
2396 End: