mingw: fix build break
[siplcs.git] / src / core / sipe-media.c
blobc904e1b2e9effe567578edb008f56a5bea28365e
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 *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 if (phone_number_is_valid(phone_number)) {
1476 gchar *phone_uri = g_strdup_printf("sip:%s@%s;user=phone",
1477 phone_number, sipe_public->sip_domain);
1479 sipe_core_media_initiate_call(sipe_public, phone_uri, FALSE);
1481 g_free(phone_uri);
1482 } else {
1483 sipe_backend_notify_error(sipe_public,
1484 _("Unable to establish a call"),
1485 _("Invalid phone number"));
1489 void sipe_core_media_test_call(struct sipe_core_public *sipe_public)
1491 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1492 if (!sipe_private->test_call_bot_uri) {
1493 sipe_backend_notify_error(sipe_public,
1494 _("Unable to establish a call"),
1495 _("Audio Test Service is not available."));
1496 return;
1499 sipe_core_media_initiate_call(sipe_public,
1500 sipe_private->test_call_bot_uri, FALSE);
1503 static struct sipe_media_call_private *
1504 sipe_media_from_sipmsg(struct sipe_core_private *sipe_private,
1505 struct sipmsg *msg)
1507 return g_hash_table_lookup(sipe_private->media_calls,
1508 sipmsg_find_header(msg, "Call-ID"));
1511 static void
1512 transport_response_unsupported_sdp(struct sipe_core_private *sipe_private,
1513 struct sipmsg *msg)
1515 sipmsg_add_header(msg, "ms-client-diagnostics",
1516 "52063;reason=\"Unsupported session description\"");
1517 sip_transport_response(sipe_private, msg,
1518 488, "Not Acceptable Here", NULL);
1521 static void
1522 maybe_send_second_invite_response(struct sipe_media_call_private *call_private)
1524 GSList *it;
1526 /* Second INVITE request had to be received and all streams must have
1527 * established candidate pairs before the response can be sent. */
1529 if (!call_private->invitation) {
1530 return;
1533 for (it = call_private->streams; it; it = it->next) {
1534 struct sipe_media_stream_private *stream_private = it->data;
1535 if (!stream_private->established) {
1536 return;
1540 send_response_with_session_description(call_private, 200, "OK");
1542 #ifdef HAVE_XDATA
1543 for (it = call_private->streams; it; it = it->next) {
1544 struct sipe_media_stream_private *stream_private = it->data;
1546 stream_private->sdp_negotiation_concluded = TRUE;
1547 if (stream_private->writable) {
1548 // We've become writable.
1549 sipe_core_media_stream_writable(SIPE_MEDIA_STREAM, TRUE);
1552 #endif
1555 struct sipe_media_call *
1556 process_incoming_invite_call(struct sipe_core_private *sipe_private,
1557 struct sipmsg *msg,
1558 const gchar *sdp)
1560 return(process_incoming_invite_call_parsed_sdp(sipe_private,
1561 msg,
1562 sdpmsg_parse_msg(sdp)));
1565 struct sipe_media_call *
1566 process_incoming_invite_call_parsed_sdp(struct sipe_core_private *sipe_private,
1567 struct sipmsg *msg,
1568 struct sdpmsg *smsg)
1570 struct sipe_media_call_private *call_private;
1571 gboolean has_new_media = FALSE;
1572 GSList *i;
1574 // Don't allow two voice calls in parallel.
1575 if (!strstr(msg->body, "m=data") &&
1576 !strstr(msg->body, "m=applicationsharing")) {
1577 struct sipe_media_call *call =
1578 sipe_core_media_get_call(SIPE_CORE_PUBLIC);
1579 if (call && !is_media_session_msg(SIPE_MEDIA_CALL_PRIVATE, msg)) {
1580 sip_transport_response(sipe_private, msg,
1581 486, "Busy Here", NULL);
1582 sdpmsg_free(smsg);
1583 return NULL;
1587 call_private = sipe_media_from_sipmsg(sipe_private, msg);
1589 if (call_private) {
1590 char *self = sip_uri_self(sipe_private);
1591 if (sipe_strequal(SIPE_MEDIA_CALL->with, self)) {
1592 g_free(self);
1593 sip_transport_response(sipe_private, msg, 488, "Not Acceptable Here", NULL);
1594 sdpmsg_free(smsg);
1595 return NULL;
1597 g_free(self);
1600 if (!smsg) {
1601 transport_response_unsupported_sdp(sipe_private, msg);
1602 if (call_private) {
1603 sipe_media_hangup(call_private);
1605 return NULL;
1608 if (!call_private) {
1609 gchar *with = parse_from(sipmsg_find_header(msg, "From"));
1610 SipeMediaCallFlags flags = 0;
1612 if (strstr(msg->body, "m=data") ||
1613 strstr(msg->body, "m=applicationsharing")) {
1614 flags |= SIPE_MEDIA_CALL_NO_UI;
1617 call_private = (struct sipe_media_call_private *)
1618 sipe_media_call_new(sipe_private, with,
1619 msg, smsg->ice_version,
1620 flags);
1622 if (!(flags & SIPE_MEDIA_CALL_NO_UI)) {
1623 SIPE_MEDIA_CALL->call_reject_cb = av_call_reject_cb;
1625 g_free(with);
1628 if (call_private->invitation)
1629 sipmsg_free(call_private->invitation);
1630 call_private->invitation = sipmsg_copy(msg);
1632 ssrc_range_update(&call_private->ssrc_ranges, smsg->media);
1634 // Create any new media streams
1635 for (i = smsg->media; i; i = i->next) {
1636 struct sdpmedia *media = i->data;
1637 gchar *id = media->name;
1638 SipeMediaType type;
1640 if ( media->port != 0
1641 && !sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL, id)) {
1642 guint32 ssrc_count = 0;
1644 if (sipe_strequal(id, "audio"))
1645 type = SIPE_MEDIA_AUDIO;
1646 else if (sipe_strequal(id, "video")) {
1647 type = SIPE_MEDIA_VIDEO;
1648 ssrc_count = VIDEO_SSRC_COUNT;
1649 } else if (sipe_strequal(id, "data"))
1650 type = SIPE_MEDIA_APPLICATION;
1651 else if (sipe_strequal(id, "applicationsharing"))
1652 type = SIPE_MEDIA_APPLICATION;
1653 else
1654 continue;
1656 sipe_media_stream_add(SIPE_MEDIA_CALL, id, type,
1657 smsg->ice_version, FALSE,
1658 ssrc_count);
1659 has_new_media = TRUE;
1663 if (has_new_media) {
1664 sdpmsg_free(call_private->smsg);
1665 call_private->smsg = smsg;
1666 sip_transport_response(sipe_private, call_private->invitation,
1667 180, "Ringing", NULL);
1668 // Processing continues in stream_initialized_cb
1669 } else {
1670 apply_remote_message(call_private, smsg);
1671 sdpmsg_free(smsg);
1672 maybe_send_second_invite_response(call_private);
1675 return SIPE_MEDIA_CALL;
1678 void process_incoming_cancel_call(struct sipe_media_call_private *call_private,
1679 struct sipmsg *msg)
1681 // We respond to the CANCEL request with 200 OK response and
1682 // with 487 Request Terminated to the remote INVITE in progress.
1683 sip_transport_response(call_private->sipe_private, msg, 200, "OK", NULL);
1685 if (call_private->invitation) {
1686 sip_transport_response(call_private->sipe_private,
1687 call_private->invitation,
1688 487, "Request Terminated", NULL);
1691 sipe_backend_media_reject(SIPE_MEDIA_CALL->backend_private, FALSE);
1694 static gboolean
1695 sipe_media_send_ack(struct sipe_core_private *sipe_private,
1696 struct sipmsg *msg,
1697 struct transaction *trans)
1699 struct sipe_media_call_private *call_private;
1700 struct sip_dialog *dialog;
1701 int tmp_cseq;
1703 call_private = sipe_media_from_sipmsg(sipe_private, msg);
1705 if (!is_media_session_msg(call_private, msg))
1706 return FALSE;
1708 dialog = sipe_media_get_sip_dialog(SIPE_MEDIA_CALL);
1709 if (!dialog)
1710 return FALSE;
1712 tmp_cseq = dialog->cseq;
1714 dialog->cseq = sip_transaction_cseq(trans) - 1;
1715 sip_transport_ack(sipe_private, dialog);
1716 dialog->cseq = tmp_cseq;
1718 dialog->outgoing_invite = NULL;
1720 return TRUE;
1723 static gboolean
1724 sipe_media_send_final_ack(struct sipe_core_private *sipe_private,
1725 struct sipmsg *msg,
1726 struct transaction *trans)
1728 struct sipe_media_call_private *call_private;
1729 #ifdef HAVE_XDATA
1730 GSList *it;
1731 #endif
1733 if (!sipe_media_send_ack(sipe_private, msg, trans))
1734 return FALSE;
1736 call_private = sipe_media_from_sipmsg(sipe_private, msg);
1738 sipe_backend_media_accept(SIPE_MEDIA_CALL->backend_private, FALSE);
1740 #ifdef HAVE_XDATA
1741 for (it = call_private->streams; it; it = it->next) {
1742 struct sipe_media_stream_private *stream_private = it->data;
1744 stream_private->sdp_negotiation_concluded = TRUE;
1745 if (stream_private->writable) {
1746 // We've become writable.
1747 sipe_core_media_stream_writable(SIPE_MEDIA_STREAM, TRUE);
1750 #endif
1752 return TRUE;
1755 void
1756 sipe_core_media_stream_candidate_pair_established(struct sipe_media_stream *stream)
1758 struct sipe_media_call *call = stream->call;
1760 GList *active_candidates =
1761 sipe_backend_media_stream_get_active_local_candidates(stream);
1762 guint ready_components = g_list_length(active_candidates);
1764 sipe_media_candidate_list_free(active_candidates);
1766 if (ready_components != 2) {
1767 // We must have both RTP+RTCP candidate pairs established first.
1768 return;
1771 if (SIPE_MEDIA_STREAM_PRIVATE->established) {
1772 return;
1774 SIPE_MEDIA_STREAM_PRIVATE->established = TRUE;
1776 if (stream->candidate_pairs_established_cb) {
1777 stream->candidate_pairs_established_cb(stream);
1780 if (sipe_backend_media_is_initiator(stream->call, NULL)) {
1781 GSList *streams = SIPE_MEDIA_CALL_PRIVATE->streams;
1782 for (; streams; streams = streams->next) {
1783 struct sipe_media_stream_private *s = streams->data;
1784 if (!s->established) {
1785 break;
1789 if (streams == NULL) {
1790 // All call streams have been established.
1791 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE,
1792 sipe_media_send_final_ack);
1794 } else {
1795 maybe_send_second_invite_response(SIPE_MEDIA_CALL_PRIVATE);
1799 static gboolean
1800 maybe_retry_call_with_ice_version(struct sipe_core_private *sipe_private,
1801 struct sipe_media_call_private *call_private,
1802 SipeIceVersion ice_version,
1803 struct transaction *trans)
1805 if (call_private->ice_version != ice_version &&
1806 sip_transaction_cseq(trans) == 1) {
1807 GSList *i;
1808 gchar *with;
1809 gboolean with_video = FALSE;
1811 for (i = call_private->streams; i; i = i->next) {
1812 struct sipe_media_stream *stream = i->data;
1814 if (sipe_strequal(stream->id, "video")) {
1815 with_video = TRUE;
1816 } else if (!sipe_strequal(stream->id, "audio")) {
1817 /* Don't retry calls which are neither audio
1818 * nor video. */
1819 return FALSE;
1823 with = g_strdup(SIPE_MEDIA_CALL->with);
1825 sipe_media_hangup(call_private);
1826 SIPE_DEBUG_INFO("Retrying call with ICEv%d.",
1827 ice_version == SIPE_ICE_DRAFT_6 ? 6 : 19);
1828 sipe_media_initiate_call(sipe_private,
1829 with,
1830 ice_version,
1831 with_video);
1833 g_free(with);
1834 return TRUE;
1837 return FALSE;
1840 static gboolean
1841 process_invite_call_response(struct sipe_core_private *sipe_private,
1842 struct sipmsg *msg,
1843 struct transaction *trans)
1845 const gchar *with;
1846 struct sipe_media_call_private *call_private;
1847 struct sip_dialog *dialog;
1848 struct sdpmsg *smsg;
1850 call_private = sipe_media_from_sipmsg(sipe_private,msg);
1852 if (!is_media_session_msg(call_private, msg))
1853 return FALSE;
1855 dialog = sipe_media_get_sip_dialog(SIPE_MEDIA_CALL);
1857 with = dialog->with;
1859 dialog->outgoing_invite = NULL;
1861 if (msg->response == 603 || msg->response == 605) {
1862 // Call rejected by remote peer
1863 sipe_media_send_ack(sipe_private, msg, trans);
1864 sipe_backend_media_reject(SIPE_MEDIA_CALL->backend_private, FALSE);
1866 return TRUE;
1869 if (msg->response >= 400) {
1870 // An error occurred
1871 const gchar *title;
1872 GString *desc = g_string_new("");
1873 gboolean append_responsestr = FALSE;
1875 switch (msg->response) {
1876 case 480: {
1877 title = _("User unavailable");
1879 if (sipmsg_parse_warning(msg, NULL) == 391) {
1880 g_string_append_printf(desc, _("%s does not want to be disturbed"), with);
1881 } else
1882 g_string_append_printf(desc, _("User %s is not available"), with);
1883 break;
1885 case 415:
1886 // OCS/Lync really sends response string with 'Mutipart' typo.
1887 if (sipe_strequal(msg->responsestr, "Mutipart mime in content type not supported by Archiving CDR service") &&
1888 maybe_retry_call_with_ice_version(sipe_private,
1889 call_private,
1890 SIPE_ICE_DRAFT_6,
1891 trans)) {
1892 return TRUE;
1894 title = _("Unsupported media type");
1895 break;
1896 case 488: {
1897 /* Check for incompatible encryption levels error.
1899 * MS Lync 2010:
1900 * 488 Not Acceptable Here
1901 * ms-client-diagnostics: 52017;reason="Encryption levels dont match"
1903 * older clients (and SIPE itself):
1904 * 488 Encryption Levels not compatible
1906 const gchar *ms_diag = sipmsg_find_header(msg, "ms-client-diagnostics");
1907 SipeIceVersion retry_ice_version = SIPE_ICE_DRAFT_6;
1909 if (sipe_strequal(msg->responsestr, "Encryption Levels not compatible") ||
1910 (ms_diag && g_str_has_prefix(ms_diag, "52017;"))) {
1911 title = _("Unable to establish a call");
1912 g_string_append(desc, _("Encryption settings of peer are incompatible with ours."));
1913 break;
1916 /* Check if this is failed conference using
1917 * ICEv6 with reason "Error parsing SDP" and
1918 * retry using ICEv19. */
1919 ms_diag = sipmsg_find_header(msg, "ms-diagnostics");
1920 if (ms_diag && g_str_has_prefix(ms_diag, "7008;")) {
1921 retry_ice_version = SIPE_ICE_RFC_5245;
1924 if (maybe_retry_call_with_ice_version(sipe_private,
1925 call_private,
1926 retry_ice_version,
1927 trans)) {
1928 return TRUE;
1930 SIPE_FALLTHROUGH
1932 default:
1933 title = _("Error occurred");
1934 g_string_append(desc, _("Unable to establish a call"));
1935 append_responsestr = TRUE;
1936 break;
1939 if (append_responsestr) {
1940 gchar *reason = sipmsg_get_ms_diagnostics_reason(msg);
1942 g_string_append_printf(desc, "\n%d %s",
1943 msg->response, msg->responsestr);
1944 if (reason) {
1945 g_string_append_printf(desc, "\n\n%s", reason);
1946 g_free(reason);
1950 sipe_backend_notify_error(SIPE_CORE_PUBLIC, title, desc->str);
1951 g_string_free(desc, TRUE);
1953 sipe_media_send_ack(sipe_private, msg, trans);
1954 sipe_media_hangup(call_private);
1956 return TRUE;
1959 sipe_dialog_parse(dialog, msg, TRUE);
1960 smsg = sdpmsg_parse_msg(msg->body);
1961 if (!smsg) {
1962 transport_response_unsupported_sdp(sipe_private, msg);
1963 sipe_media_hangup(call_private);
1964 return FALSE;
1967 ssrc_range_update(&call_private->ssrc_ranges, smsg->media);
1968 apply_remote_message(call_private, smsg);
1969 sdpmsg_free(smsg);
1971 sipe_media_send_ack(sipe_private, msg, trans);
1973 return TRUE;
1975 // Waits until sipe_core_media_candidate_pair_established() is invoked.
1978 gboolean is_media_session_msg(struct sipe_media_call_private *call_private,
1979 struct sipmsg *msg)
1981 if (!call_private) {
1982 return FALSE;
1985 return sipe_media_from_sipmsg(call_private->sipe_private, msg) == call_private;
1988 static void
1989 end_call(SIPE_UNUSED_PARAMETER gpointer key,
1990 struct sipe_media_call_private *call_private,
1991 SIPE_UNUSED_PARAMETER gpointer user_data)
1993 struct sipe_backend_media *backend_private;
1995 backend_private = call_private->public.backend_private;
1997 if (!sipe_backend_media_is_initiator(SIPE_MEDIA_CALL, NULL) &&
1998 !sipe_backend_media_accepted(backend_private)) {
1999 sip_transport_response(call_private->sipe_private,
2000 call_private->invitation,
2001 480, "Temporarily Unavailable", NULL);
2002 } else if (call_private->session) {
2003 sipe_session_close(call_private->sipe_private,
2004 call_private->session);
2005 call_private->session = NULL;
2008 sipe_media_hangup(call_private);
2011 void
2012 sipe_media_handle_going_offline(struct sipe_core_private *sipe_private)
2014 g_hash_table_foreach(sipe_private->media_calls, (GHFunc) end_call, NULL);
2017 gboolean sipe_media_is_conference_call(struct sipe_media_call_private *call_private)
2019 return g_strstr_len(SIPE_MEDIA_CALL->with, -1, "app:conf:audio-video:") != NULL;
2022 struct sipe_core_private *
2023 sipe_media_get_sipe_core_private(struct sipe_media_call *call)
2025 g_return_val_if_fail(call, NULL);
2027 return SIPE_MEDIA_CALL_PRIVATE->sipe_private;
2030 struct sip_dialog *
2031 sipe_media_get_sip_dialog(struct sipe_media_call *call)
2033 struct sip_session *session;
2035 g_return_val_if_fail(call, NULL);
2037 session = SIPE_MEDIA_CALL_PRIVATE->session;
2039 if (!session || !session->dialogs) {
2040 return NULL;
2043 return session->dialogs->data;
2046 static void
2047 sipe_media_relay_free(struct sipe_media_relay *relay)
2049 g_free(relay->hostname);
2050 if (relay->dns_query)
2051 sipe_backend_dns_query_cancel(relay->dns_query);
2052 g_free(relay);
2055 void
2056 sipe_media_relay_list_free(GSList *list)
2058 for (; list; list = g_slist_delete_link(list, list))
2059 sipe_media_relay_free(list->data);
2062 static void
2063 relay_ip_resolved_cb(struct sipe_media_relay* relay,
2064 const gchar *ip, SIPE_UNUSED_PARAMETER guint port)
2066 gchar *hostname = relay->hostname;
2067 relay->dns_query = NULL;
2069 if (ip && port) {
2070 relay->hostname = g_strdup(ip);
2071 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname, ip);
2072 } else {
2073 relay->hostname = NULL;
2074 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname);
2077 g_free(hostname);
2080 static gboolean
2081 process_get_av_edge_credentials_response(struct sipe_core_private *sipe_private,
2082 struct sipmsg *msg,
2083 SIPE_UNUSED_PARAMETER struct transaction *trans)
2085 g_free(sipe_private->media_relay_username);
2086 g_free(sipe_private->media_relay_password);
2087 sipe_media_relay_list_free(sipe_private->media_relays);
2088 sipe_private->media_relay_username = NULL;
2089 sipe_private->media_relay_password = NULL;
2090 sipe_private->media_relays = NULL;
2092 if (msg->response >= 400) {
2093 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
2094 "Failed to obtain A/V Edge credentials.");
2095 return FALSE;
2098 if (msg->response == 200) {
2099 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
2101 if (sipe_strequal("OK", sipe_xml_attribute(xn_response, "reasonPhrase"))) {
2102 const sipe_xml *xn_credentials = sipe_xml_child(xn_response, "credentialsResponse/credentials");
2103 const sipe_xml *xn_relays = sipe_xml_child(xn_response, "credentialsResponse/mediaRelayList");
2104 const sipe_xml *item;
2105 GSList *relays = NULL;
2107 item = sipe_xml_child(xn_credentials, "username");
2108 sipe_private->media_relay_username = sipe_xml_data(item);
2109 item = sipe_xml_child(xn_credentials, "password");
2110 sipe_private->media_relay_password = sipe_xml_data(item);
2112 for (item = sipe_xml_child(xn_relays, "mediaRelay"); item; item = sipe_xml_twin(item)) {
2113 struct sipe_media_relay *relay = g_new0(struct sipe_media_relay, 1);
2114 const sipe_xml *node;
2115 gchar *tmp;
2117 node = sipe_xml_child(item, "hostName");
2118 relay->hostname = sipe_xml_data(node);
2120 node = sipe_xml_child(item, "udpPort");
2121 if (node) {
2122 tmp = sipe_xml_data(node);
2123 if (tmp) {
2124 relay->udp_port = atoi(tmp);
2125 g_free(tmp);
2129 node = sipe_xml_child(item, "tcpPort");
2130 if (node) {
2131 tmp = sipe_xml_data(node);
2132 if (tmp) {
2133 relay->tcp_port = atoi(tmp);
2134 g_free(tmp);
2138 relays = g_slist_append(relays, relay);
2140 relay->dns_query = sipe_backend_dns_query_a(
2141 SIPE_CORE_PUBLIC,
2142 relay->hostname,
2143 relay->udp_port,
2144 (sipe_dns_resolved_cb) relay_ip_resolved_cb,
2145 relay);
2147 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
2148 relay->hostname,
2149 relay->tcp_port, relay->udp_port);
2152 sipe_private->media_relays = relays;
2155 sipe_xml_free(xn_response);
2158 return TRUE;
2161 void
2162 sipe_media_get_av_edge_credentials(struct sipe_core_private *sipe_private)
2164 // TODO: re-request credentials after duration expires?
2165 static const char CRED_REQUEST_XML[] =
2166 "<request requestID=\"%d\" "
2167 "from=\"%s\" "
2168 "version=\"1.0\" "
2169 "to=\"%s\" "
2170 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
2171 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
2172 "<credentialsRequest credentialsRequestID=\"%d\">"
2173 "<identity>%s</identity>"
2174 "<location>%s</location>"
2175 "<duration>480</duration>"
2176 "</credentialsRequest>"
2177 "</request>";
2179 int request_id = rand();
2180 gchar *self;
2181 gchar *body;
2183 if (!sipe_private->mras_uri)
2184 return;
2186 self = sip_uri_self(sipe_private);
2188 body = g_strdup_printf(
2189 CRED_REQUEST_XML,
2190 request_id,
2191 self,
2192 sipe_private->mras_uri,
2193 request_id,
2194 self,
2195 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER) ? "internet" : "intranet");
2196 g_free(self);
2198 sip_transport_service(sipe_private,
2199 sipe_private->mras_uri,
2200 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
2201 body,
2202 process_get_av_edge_credentials_response);
2204 g_free(body);
2207 void
2208 sipe_media_add_extra_invite_section(struct sipe_media_call *call,
2209 const gchar *invite_content_type,
2210 gchar *body)
2212 g_free(SIPE_MEDIA_CALL_PRIVATE->extra_invite_section);
2213 g_free(SIPE_MEDIA_CALL_PRIVATE->invite_content_type);
2214 SIPE_MEDIA_CALL_PRIVATE->extra_invite_section = body;
2215 SIPE_MEDIA_CALL_PRIVATE->invite_content_type =
2216 g_strdup(invite_content_type);
2219 void
2220 sipe_media_stream_add_extra_attribute(struct sipe_media_stream *stream,
2221 const gchar *name, const gchar *value)
2223 SIPE_MEDIA_STREAM_PRIVATE->extra_sdp =
2224 sipe_utils_nameval_add(SIPE_MEDIA_STREAM_PRIVATE->extra_sdp,
2225 name, value);
2228 #ifdef HAVE_XDATA
2229 void
2230 sipe_core_media_stream_readable(struct sipe_media_stream *stream)
2232 g_return_if_fail(stream);
2234 if (g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE->async_reads) &&
2235 stream->read_cb) {
2236 stream->read_cb(stream);
2239 while (!g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE->async_reads)) {
2240 struct async_read_data *data;
2241 guint8 *pos;
2242 gssize len;
2243 gssize bytes_read;
2245 data = g_queue_peek_head(SIPE_MEDIA_STREAM_PRIVATE->async_reads);
2246 pos = data->buffer + SIPE_MEDIA_STREAM_PRIVATE->read_pos;
2247 len = data->len - SIPE_MEDIA_STREAM_PRIVATE->read_pos;
2249 bytes_read = sipe_backend_media_stream_read(stream, pos, len);
2250 if (bytes_read == -1) {
2251 struct sipe_media_call *call = stream->call;
2252 struct sipe_core_private *sipe_private =
2253 SIPE_MEDIA_CALL_PRIVATE->sipe_private;
2255 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
2256 _("Media error"),
2257 _("Error while reading from stream"));
2258 sipe_media_hangup(SIPE_MEDIA_CALL_PRIVATE);
2259 return;
2262 SIPE_MEDIA_STREAM_PRIVATE->read_pos += bytes_read;
2264 if (SIPE_MEDIA_STREAM_PRIVATE->read_pos == data->len) {
2265 data->callback(stream, data->buffer, data->len);
2266 SIPE_MEDIA_STREAM_PRIVATE->read_pos = 0;
2267 g_queue_pop_head(SIPE_MEDIA_STREAM_PRIVATE->async_reads);
2268 g_free(data);
2269 } else {
2270 // Still not enough data to finish the read.
2271 return;
2276 void
2277 sipe_media_stream_read_async(struct sipe_media_stream *stream,
2278 gpointer buffer, gsize len,
2279 sipe_media_stream_read_callback callback)
2281 struct async_read_data *data;
2283 g_return_if_fail(stream && buffer && callback);
2285 data = g_new0(struct async_read_data, 1);
2286 data->buffer = buffer;
2287 data->len = len;
2288 data->callback = callback;
2290 g_queue_push_tail(SIPE_MEDIA_STREAM_PRIVATE->async_reads, data);
2293 static void
2294 stream_append_buffer(struct sipe_media_stream *stream,
2295 guint8 *buffer, guint len)
2297 GByteArray *b = g_byte_array_sized_new(len);
2298 g_byte_array_append(b, buffer, len);
2299 g_queue_push_tail(SIPE_MEDIA_STREAM_PRIVATE->write_queue, b);
2302 gboolean
2303 sipe_media_stream_write(struct sipe_media_stream *stream,
2304 gpointer buffer, gsize len)
2306 if (!sipe_media_stream_is_writable(stream)) {
2307 stream_append_buffer(stream, buffer, len);
2308 return FALSE;
2309 } else {
2310 guint written;
2312 written = sipe_backend_media_stream_write(stream, buffer, len);
2313 if (written == len) {
2314 return TRUE;
2317 stream_append_buffer(stream,
2318 (guint8 *)buffer + written, len - written);
2319 return FALSE;
2323 void
2324 sipe_core_media_stream_writable(struct sipe_media_stream *stream,
2325 gboolean writable)
2327 SIPE_MEDIA_STREAM_PRIVATE->writable = writable;
2329 if (!writable) {
2330 return;
2333 while (!g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE->write_queue)) {
2334 GByteArray *b;
2335 guint written;
2337 b = g_queue_peek_head(SIPE_MEDIA_STREAM_PRIVATE->write_queue);
2339 written = sipe_backend_media_stream_write(stream, b->data, b->len);
2340 if (written != b->len) {
2341 g_byte_array_remove_range(b, 0, written);
2342 return;
2345 g_byte_array_unref(b);
2346 g_queue_pop_head(SIPE_MEDIA_STREAM_PRIVATE->write_queue);
2349 if (sipe_media_stream_is_writable(stream) && stream->writable_cb) {
2350 stream->writable_cb(stream);
2354 gboolean
2355 sipe_media_stream_is_writable(struct sipe_media_stream *stream)
2357 return SIPE_MEDIA_STREAM_PRIVATE->writable &&
2358 SIPE_MEDIA_STREAM_PRIVATE->sdp_negotiation_concluded &&
2359 g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE->write_queue);
2361 #endif
2363 void
2364 sipe_media_stream_set_data(struct sipe_media_stream *stream, gpointer data,
2365 GDestroyNotify free_func)
2367 struct sipe_media_stream_private *stream_private =
2368 SIPE_MEDIA_STREAM_PRIVATE;
2370 g_return_if_fail(stream_private);
2372 if (stream_private->data && stream_private->data_free_func) {
2373 stream_private->data_free_func(stream_private->data);
2376 stream_private->data = data;
2377 stream_private->data_free_func = free_func;
2380 gpointer
2381 sipe_media_stream_get_data(struct sipe_media_stream *stream)
2383 g_return_val_if_fail(stream, NULL);
2385 return SIPE_MEDIA_STREAM_PRIVATE->data;
2389 Local Variables:
2390 mode: c
2391 c-file-style: "bsd"
2392 indent-tabs-mode: t
2393 tab-width: 8
2394 End: