filetransfer: create media session for incoming data
[siplcs.git] / src / core / sipe-media.c
blob4ad2681a888964649c69f0e2d6b18b0be8a8abc2
1 /**
2 * @file sipe-media.c
4 * pidgin-sipe
6 * Copyright (C) 2011-2015 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-chat.h"
40 #include "sipe-conf.h"
41 #include "sipe-core.h"
42 #include "sipe-core-private.h"
43 #include "sipe-dialog.h"
44 #include "sipe-media.h"
45 #include "sipe-ocs2007.h"
46 #include "sipe-session.h"
47 #include "sipe-utils.h"
48 #include "sipe-nls.h"
49 #include "sipe-schedule.h"
50 #include "sipe-xml.h"
52 struct sipe_media_call_private {
53 struct sipe_media_call public;
55 /* private part starts here */
56 struct sipe_core_private *sipe_private;
58 GSList *streams;
60 struct sipmsg *invitation;
61 SipeIceVersion ice_version;
62 gboolean encryption_compatible;
64 struct sdpmsg *smsg;
65 GSList *failed_media;
67 #define SIPE_MEDIA_CALL ((struct sipe_media_call *) call_private)
68 #define SIPE_MEDIA_CALL_PRIVATE ((struct sipe_media_call_private *) call)
70 struct sipe_media_stream_private {
71 struct sipe_media_stream public;
73 guchar *encryption_key;
74 int encryption_key_id;
75 gboolean remote_candidates_and_codecs_set;
77 GSList *extra_sdp;
79 /* User data associated with the stream. */
80 gpointer data;
81 GDestroyNotify data_free_func;
83 #define SIPE_MEDIA_STREAM ((struct sipe_media_stream *) stream_private)
84 #define SIPE_MEDIA_STREAM_PRIVATE ((struct sipe_media_stream_private *) stream)
86 static void sipe_media_codec_list_free(GList *codecs)
88 for (; codecs; codecs = g_list_delete_link(codecs, codecs))
89 sipe_backend_codec_free(codecs->data);
92 static void sipe_media_candidate_list_free(GList *candidates)
94 for (; candidates; candidates = g_list_delete_link(candidates, candidates))
95 sipe_backend_candidate_free(candidates->data);
98 static void
99 remove_stream(struct sipe_media_call* call,
100 struct sipe_media_stream_private *stream_private)
102 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
104 sipe_media_stream_set_data(SIPE_MEDIA_STREAM, NULL, NULL);
106 call_private->streams =
107 g_slist_remove(call_private->streams, stream_private);
108 sipe_backend_media_stream_free(SIPE_MEDIA_STREAM->backend_private);
109 g_free(SIPE_MEDIA_STREAM->id);
110 g_free(stream_private->encryption_key);
111 sipe_utils_nameval_free(stream_private->extra_sdp);
112 g_free(stream_private);
115 static gboolean
116 call_private_equals(SIPE_UNUSED_PARAMETER const gchar *callid,
117 struct sipe_media_call_private *call_private1,
118 struct sipe_media_call_private *call_private2)
120 return call_private1 == call_private2;
123 static void
124 sipe_media_call_free(struct sipe_media_call_private *call_private)
126 if (call_private) {
127 struct sip_session *session;
129 g_hash_table_foreach_remove(call_private->sipe_private->media_calls,
130 (GHRFunc) call_private_equals, call_private);
132 while (call_private->streams) {
133 remove_stream(SIPE_MEDIA_CALL,
134 call_private->streams->data);
137 sipe_backend_media_free(call_private->public.backend_private);
139 session = sipe_session_find_call(call_private->sipe_private,
140 SIPE_MEDIA_CALL->with);
141 if (session)
142 sipe_session_remove(call_private->sipe_private, session);
144 if (call_private->invitation)
145 sipmsg_free(call_private->invitation);
147 sdpmsg_free(call_private->smsg);
148 sipe_utils_slist_free_full(call_private->failed_media,
149 (GDestroyNotify)sdpmedia_free);
150 g_free(SIPE_MEDIA_CALL->with);
151 g_free(call_private);
155 static gint
156 candidate_sort_cb(struct sdpcandidate *c1, struct sdpcandidate *c2)
158 int cmp = sipe_strcompare(c1->foundation, c2->foundation);
159 if (cmp == 0) {
160 cmp = sipe_strcompare(c1->username, c2->username);
161 if (cmp == 0)
162 cmp = c1->component - c2->component;
165 return cmp;
168 static GSList *
169 backend_candidates_to_sdpcandidate(GList *candidates)
171 GSList *result = NULL;
172 GList *i;
174 for (i = candidates; i; i = i->next) {
175 struct sipe_backend_candidate *candidate = i->data;
176 struct sdpcandidate *c;
178 gchar *ip = sipe_backend_candidate_get_ip(candidate);
179 gchar *base_ip = sipe_backend_candidate_get_base_ip(candidate);
180 if (is_empty(ip) || strchr(ip, ':') ||
181 (base_ip && strchr(base_ip, ':'))) {
182 /* Ignore IPv6 candidates. */
183 g_free(ip);
184 g_free(base_ip);
185 continue;
188 c = g_new(struct sdpcandidate, 1);
189 c->foundation = sipe_backend_candidate_get_foundation(candidate);
190 c->component = sipe_backend_candidate_get_component_type(candidate);
191 c->type = sipe_backend_candidate_get_type(candidate);
192 c->protocol = sipe_backend_candidate_get_protocol(candidate);
193 c->ip = ip;
194 c->port = sipe_backend_candidate_get_port(candidate);
195 c->base_ip = base_ip;
196 c->base_port = sipe_backend_candidate_get_base_port(candidate);
197 c->priority = sipe_backend_candidate_get_priority(candidate);
198 c->username = sipe_backend_candidate_get_username(candidate);
199 c->password = sipe_backend_candidate_get_password(candidate);
201 result = g_slist_insert_sorted(result, c,
202 (GCompareFunc)candidate_sort_cb);
205 return result;
208 static void
209 get_stream_ip_and_ports(GSList *candidates,
210 gchar **ip, guint *rtp_port, guint *rtcp_port,
211 SipeCandidateType type)
213 *ip = 0;
214 *rtp_port = 0;
215 *rtcp_port = 0;
217 for (; candidates; candidates = candidates->next) {
218 struct sdpcandidate *candidate = candidates->data;
220 if (type == SIPE_CANDIDATE_TYPE_ANY || candidate->type == type) {
221 if (!*ip) {
222 *ip = g_strdup(candidate->ip);
223 } else if (!sipe_strequal(*ip, candidate->ip)) {
224 continue;
227 if (candidate->component == SIPE_COMPONENT_RTP) {
228 *rtp_port = candidate->port;
229 } else if (candidate->component == SIPE_COMPONENT_RTCP)
230 *rtcp_port = candidate->port;
233 if (*rtp_port != 0 && *rtcp_port != 0)
234 return;
238 static gint
239 sdpcodec_compare(gconstpointer a, gconstpointer b)
241 return ((const struct sdpcodec *)a)->id -
242 ((const struct sdpcodec *)b)->id;
245 static GList *
246 remove_wrong_farstream_0_1_tcp_candidates(GList *candidates)
248 GList *i = candidates;
249 GHashTable *foundation_to_candidate = g_hash_table_new_full(g_str_hash,
250 g_str_equal,
251 g_free,
252 NULL);
254 while (i) {
255 GList *next = i->next;
256 struct sipe_backend_candidate *c1 = i->data;
258 if (sipe_backend_candidate_get_protocol(c1) == SIPE_NETWORK_PROTOCOL_UDP) {
259 gchar *foundation = sipe_backend_candidate_get_foundation(c1);
260 struct sipe_backend_candidate *c2 = g_hash_table_lookup(foundation_to_candidate,
261 foundation);
263 if (c2) {
264 g_free(foundation);
266 if (sipe_backend_candidate_get_port(c1) ==
267 sipe_backend_candidate_get_port(c2) ||
268 (sipe_backend_candidate_get_type(c1) !=
269 SIPE_CANDIDATE_TYPE_HOST &&
270 sipe_backend_candidate_get_base_port(c1) ==
271 sipe_backend_candidate_get_base_port(c2))) {
273 * We assume that RTP+RTCP UDP pairs
274 * that share the same port are
275 * actually mistagged TCP candidates.
277 candidates = g_list_remove(candidates, c2);
278 candidates = g_list_delete_link(candidates, i);
279 sipe_backend_candidate_free(c1);
280 sipe_backend_candidate_free(c2);
282 } else
283 /* hash table takes ownership of "foundation" */
284 g_hash_table_insert(foundation_to_candidate, foundation, c1);
287 i = next;
290 g_hash_table_destroy(foundation_to_candidate);
292 return candidates;
295 static void
296 fill_zero_tcp_act_ports_from_tcp_pass(GSList *candidates, GSList *all_candidates)
298 GSList *i;
299 GHashTable *ip_to_port = g_hash_table_new(g_str_hash, g_str_equal);
301 for (i = candidates; i; i = i->next) {
302 struct sdpcandidate *c = i->data;
303 GSList *j;
305 if (c->protocol != SIPE_NETWORK_PROTOCOL_TCP_ACTIVE) {
306 continue;
309 for (j = all_candidates; j; j = j->next) {
310 struct sdpcandidate *passive = j->data;
311 if (passive->protocol != SIPE_NETWORK_PROTOCOL_TCP_PASSIVE ||
312 c->type != passive->type) {
313 continue;
316 if (sipe_strequal(c->ip, passive->ip) &&
317 sipe_strequal(c->base_ip, passive->base_ip)) {
318 if (c->port == 0) {
319 c->port = passive->port;
322 if (c->base_port == 0) {
323 c->base_port = passive->base_port;
325 break;
330 for (i = all_candidates; i; i = i->next) {
331 struct sdpcandidate *c = i->data;
333 if (c->protocol == SIPE_NETWORK_PROTOCOL_TCP_PASSIVE &&
334 c->type == SIPE_CANDIDATE_TYPE_HOST) {
335 g_hash_table_insert(ip_to_port, c->ip, &c->port);
339 /* Fill base ports of all TCP relay candidates using what we have
340 * collected from host candidates. */
341 for (i = candidates; i; i = i->next) {
342 struct sdpcandidate *c = i->data;
343 if (c->type == SIPE_CANDIDATE_TYPE_RELAY && c->base_port == 0) {
344 guint *base_port = (guint*)g_hash_table_lookup(ip_to_port, c->base_ip);
345 if (base_port) {
346 c->base_port = *base_port;
347 } else {
348 SIPE_DEBUG_WARNING("Couldn't determine base port for candidate "
349 "with foundation %s", c->foundation);
354 g_hash_table_destroy(ip_to_port);
357 static SipeEncryptionPolicy
358 get_encryption_policy(struct sipe_core_private *sipe_private)
360 SipeEncryptionPolicy result =
361 sipe_backend_media_get_encryption_policy(SIPE_CORE_PUBLIC);
362 if (result == SIPE_ENCRYPTION_POLICY_OBEY_SERVER) {
363 result = sipe_private->server_av_encryption_policy;
366 return result;
369 static struct sdpmedia *
370 media_stream_to_sdpmedia(struct sipe_media_call_private *call_private,
371 struct sipe_media_stream_private *stream_private)
373 struct sdpmedia *sdpmedia = g_new0(struct sdpmedia, 1);
374 GList *codecs = sipe_backend_get_local_codecs(SIPE_MEDIA_CALL,
375 SIPE_MEDIA_STREAM);
376 SipeEncryptionPolicy encryption_policy =
377 get_encryption_policy(call_private->sipe_private);
378 guint rtcp_port = 0;
379 SipeMediaType type;
380 GSList *attributes = NULL;
381 GSList *sdpcandidates;
382 GSList *all_sdpcandidates;
383 GList *candidates;
384 GList *i;
385 GSList *j;
387 sdpmedia->name = g_strdup(SIPE_MEDIA_STREAM->id);
389 if (sipe_strequal(sdpmedia->name, "audio"))
390 type = SIPE_MEDIA_AUDIO;
391 else if (sipe_strequal(sdpmedia->name, "video"))
392 type = SIPE_MEDIA_VIDEO;
393 else if (sipe_strequal(sdpmedia->name, "data"))
394 type = SIPE_MEDIA_APPLICATION;
395 else {
396 // TODO: incompatible media, should not happen here
397 g_free(sdpmedia->name);
398 g_free(sdpmedia);
399 sipe_media_codec_list_free(codecs);
400 return(NULL);
403 // Process codecs
404 for (i = codecs; i; i = i->next) {
405 struct sipe_backend_codec *codec = i->data;
406 struct sdpcodec *c = g_new0(struct sdpcodec, 1);
407 GList *params;
409 c->id = sipe_backend_codec_get_id(codec);
410 c->name = sipe_backend_codec_get_name(codec);
411 c->clock_rate = sipe_backend_codec_get_clock_rate(codec);
412 c->type = type;
414 params = sipe_backend_codec_get_optional_parameters(codec);
415 for (; params; params = params->next) {
416 struct sipnameval *param = params->data;
417 struct sipnameval *copy = g_new0(struct sipnameval, 1);
419 copy->name = g_strdup(param->name);
420 copy->value = g_strdup(param->value);
422 c->parameters = g_slist_append(c->parameters, copy);
425 /* Buggy(?) codecs may report non-unique id (a.k.a. payload
426 * type) that must not appear in SDP messages we send. Thus,
427 * let's ignore any codec having the same id as one we already
428 * have in the converted list. */
429 sdpmedia->codecs = sipe_utils_slist_insert_unique_sorted(
430 sdpmedia->codecs, c, sdpcodec_compare,
431 (GDestroyNotify)sdpcodec_free);
434 sipe_media_codec_list_free(codecs);
436 // Process local candidates
437 // If we have established candidate pairs, send them in SDP response.
438 // Otherwise send all available local candidates.
439 candidates = sipe_backend_media_get_active_local_candidates(SIPE_MEDIA_CALL,
440 SIPE_MEDIA_STREAM);
441 sdpcandidates = backend_candidates_to_sdpcandidate(candidates);
442 sipe_media_candidate_list_free(candidates);
444 candidates = sipe_backend_get_local_candidates(SIPE_MEDIA_CALL,
445 SIPE_MEDIA_STREAM);
446 candidates = remove_wrong_farstream_0_1_tcp_candidates(candidates);
447 all_sdpcandidates = backend_candidates_to_sdpcandidate(candidates);
448 sipe_media_candidate_list_free(candidates);
450 if (!sdpcandidates) {
451 sdpcandidates = all_sdpcandidates;
454 fill_zero_tcp_act_ports_from_tcp_pass(sdpcandidates, all_sdpcandidates);
456 sdpmedia->candidates = sdpcandidates;
458 if (all_sdpcandidates != sdpcandidates) {
459 sipe_utils_slist_free_full(all_sdpcandidates,
460 (GDestroyNotify)sdpcandidate_free);
463 get_stream_ip_and_ports(sdpmedia->candidates, &sdpmedia->ip, &sdpmedia->port,
464 &rtcp_port, SIPE_CANDIDATE_TYPE_HOST);
465 // No usable HOST candidates, use any candidate
466 if (sdpmedia->ip == NULL && sdpmedia->candidates) {
467 get_stream_ip_and_ports(sdpmedia->candidates, &sdpmedia->ip, &sdpmedia->port,
468 &rtcp_port, SIPE_CANDIDATE_TYPE_ANY);
471 if (sipe_backend_stream_is_held(SIPE_MEDIA_STREAM))
472 attributes = sipe_utils_nameval_add(attributes, "inactive", "");
474 if (rtcp_port) {
475 gchar *tmp = g_strdup_printf("%u", rtcp_port);
476 attributes = sipe_utils_nameval_add(attributes, "rtcp", tmp);
477 g_free(tmp);
480 if (encryption_policy != call_private->sipe_private->server_av_encryption_policy) {
481 const gchar *encryption = NULL;
482 switch (encryption_policy) {
483 case SIPE_ENCRYPTION_POLICY_REJECTED:
484 encryption = "rejected";
485 break;
486 case SIPE_ENCRYPTION_POLICY_OPTIONAL:
487 encryption = "optional";
488 break;
489 case SIPE_ENCRYPTION_POLICY_REQUIRED:
490 default:
491 encryption = "required";
492 break;
495 attributes = sipe_utils_nameval_add(attributes, "encryption", encryption);
498 // Process remote candidates
499 candidates = sipe_backend_media_get_active_remote_candidates(SIPE_MEDIA_CALL,
500 SIPE_MEDIA_STREAM);
501 sdpmedia->remote_candidates = backend_candidates_to_sdpcandidate(candidates);
502 sipe_media_candidate_list_free(candidates);
504 sdpmedia->encryption_active = stream_private->encryption_key &&
505 call_private->encryption_compatible &&
506 stream_private->remote_candidates_and_codecs_set &&
507 encryption_policy != SIPE_ENCRYPTION_POLICY_REJECTED;
509 // Set our key if encryption is enabled.
510 if (stream_private->encryption_key &&
511 encryption_policy != SIPE_ENCRYPTION_POLICY_REJECTED) {
512 sdpmedia->encryption_key = g_memdup(stream_private->encryption_key,
513 SIPE_SRTP_KEY_LEN);
514 sdpmedia->encryption_key_id = stream_private->encryption_key_id;
517 // Append extra attributes assigned to the stream.
518 for (j = stream_private->extra_sdp; j; j = g_slist_next(j)) {
519 struct sipnameval *attr = j->data;
520 attributes = sipe_utils_nameval_add(attributes,
521 attr->name, attr->value);
524 sdpmedia->attributes = attributes;
526 return sdpmedia;
529 static struct sdpmsg *
530 sipe_media_to_sdpmsg(struct sipe_media_call_private *call_private)
532 struct sdpmsg *msg = g_new0(struct sdpmsg, 1);
533 GSList *streams = call_private->streams;
535 for (; streams; streams = streams->next) {
536 struct sdpmedia *media = media_stream_to_sdpmedia(call_private,
537 streams->data);
538 if (media) {
539 msg->media = g_slist_append(msg->media, media);
541 if (msg->ip == NULL)
542 msg->ip = g_strdup(media->ip);
546 msg->media = g_slist_concat(msg->media, call_private->failed_media);
547 call_private->failed_media = NULL;
549 msg->ice_version = call_private->ice_version;
551 return msg;
554 static void
555 sipe_invite_call(struct sipe_media_call_private *call_private, TransCallback tc)
557 struct sipe_core_private *sipe_private = call_private->sipe_private;
558 gchar *hdr;
559 gchar *contact;
560 gchar *p_preferred_identity = NULL;
561 gchar *body;
562 struct sip_session *session;
563 struct sip_dialog *dialog;
564 struct sdpmsg *msg;
565 gboolean add_2007_fallback = FALSE;
567 session = sipe_session_find_call(sipe_private, SIPE_MEDIA_CALL->with);
568 dialog = session->dialogs->data;
569 add_2007_fallback = dialog->cseq == 0 &&
570 call_private->ice_version == SIPE_ICE_RFC_5245 &&
571 !sipe_strequal(SIPE_MEDIA_CALL->with, sipe_private->test_call_bot_uri);
573 contact = get_contact(sipe_private);
575 if (sipe_private->uc_line_uri) {
576 gchar *self = sip_uri_self(sipe_private);
577 p_preferred_identity = g_strdup_printf(
578 "P-Preferred-Identity: <%s>, <%s>\r\n",
579 self, sipe_private->uc_line_uri);
580 g_free(self);
583 hdr = g_strdup_printf(
584 "ms-keep-alive: UAC;hop-hop=yes\r\n"
585 "Contact: %s\r\n"
586 "%s"
587 "Content-Type: %s\r\n",
588 contact,
589 p_preferred_identity ? p_preferred_identity : "",
590 add_2007_fallback ?
591 "multipart/alternative;boundary=\"----=_NextPart_000_001E_01CB4397.0B5EB570\""
592 : "application/sdp");
593 g_free(contact);
594 g_free(p_preferred_identity);
596 msg = sipe_media_to_sdpmsg(call_private);
597 body = sdpmsg_to_string(msg);
599 if (add_2007_fallback) {
600 gchar *tmp;
601 tmp = g_strdup_printf(
602 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
603 "Content-Type: application/sdp\r\n"
604 "Content-Transfer-Encoding: 7bit\r\n"
605 "Content-Disposition: session; handling=optional; ms-proxy-2007fallback\r\n"
606 "\r\n"
607 "o=- 0 0 IN IP4 %s\r\n"
608 "s=session\r\n"
609 "c=IN IP4 %s\r\n"
610 "m=audio 0 RTP/AVP\r\n"
611 "\r\n"
612 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
613 "Content-Type: application/sdp\r\n"
614 "Content-Transfer-Encoding: 7bit\r\n"
615 "Content-Disposition: session; handling=optional\r\n"
616 "\r\n"
617 "%s"
618 "\r\n"
619 "------=_NextPart_000_001E_01CB4397.0B5EB570--\r\n",
620 msg->ip, msg->ip, body);
621 g_free(body);
622 body = tmp;
625 sdpmsg_free(msg);
627 dialog->outgoing_invite = sip_transport_invite(sipe_private,
628 hdr,
629 body,
630 dialog,
631 tc);
633 g_free(body);
634 g_free(hdr);
637 static void
638 send_response_with_session_description(struct sipe_media_call_private *call_private, int code, gchar *text)
640 struct sdpmsg *msg = sipe_media_to_sdpmsg(call_private);
641 gchar *body = sdpmsg_to_string(msg);
642 sdpmsg_free(msg);
643 sipmsg_add_header(call_private->invitation, "Content-Type", "application/sdp");
644 sip_transport_response(call_private->sipe_private, call_private->invitation, code, text, body);
645 g_free(body);
648 static gboolean
649 process_invite_call_response(struct sipe_core_private *sipe_private,
650 struct sipmsg *msg,
651 struct transaction *trans);
653 struct sipe_media_stream *
654 sipe_core_media_get_stream_by_id(struct sipe_media_call *call, const gchar *id)
656 GSList *i;
657 for (i = SIPE_MEDIA_CALL_PRIVATE->streams; i; i = i->next) {
658 struct sipe_media_stream *stream = i->data;
659 if (sipe_strequal(stream->id, id))
660 return stream;
662 return NULL;
665 static gboolean
666 update_call_from_remote_sdp(struct sipe_media_call_private* call_private,
667 struct sdpmedia *media)
669 struct sipe_media_stream *stream;
670 GList *backend_candidates = NULL;
671 GList *backend_codecs = NULL;
672 GSList *i;
673 gboolean result = TRUE;
675 stream = sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL, media->name);
676 if (media->port == 0) {
677 if (stream) {
678 sipe_backend_media_stream_end(SIPE_MEDIA_CALL, stream);
680 return TRUE;
683 if (!stream)
684 return FALSE;
686 if (sipe_utils_nameval_find(media->attributes, "inactive")) {
687 sipe_backend_stream_hold(SIPE_MEDIA_CALL, stream, FALSE);
688 } else if (sipe_backend_stream_is_held(stream)) {
689 sipe_backend_stream_unhold(SIPE_MEDIA_CALL, stream, FALSE);
692 if (SIPE_MEDIA_STREAM_PRIVATE->remote_candidates_and_codecs_set) {
693 return TRUE;
696 for (i = media->codecs; i; i = i->next) {
697 struct sdpcodec *c = i->data;
698 struct sipe_backend_codec *codec;
699 GSList *j;
701 codec = sipe_backend_codec_new(c->id,
702 c->name,
703 c->type,
704 c->clock_rate);
706 for (j = c->parameters; j; j = j->next) {
707 struct sipnameval *attr = j->data;
709 sipe_backend_codec_add_optional_parameter(codec,
710 attr->name,
711 attr->value);
714 backend_codecs = g_list_append(backend_codecs, codec);
717 if (media->encryption_key && SIPE_MEDIA_STREAM_PRIVATE->encryption_key) {
718 sipe_backend_media_set_encryption_keys(SIPE_MEDIA_CALL, stream,
719 SIPE_MEDIA_STREAM_PRIVATE->encryption_key,
720 media->encryption_key);
721 SIPE_MEDIA_STREAM_PRIVATE->encryption_key_id = media->encryption_key_id;
724 result = sipe_backend_set_remote_codecs(SIPE_MEDIA_CALL, stream,
725 backend_codecs);
726 sipe_media_codec_list_free(backend_codecs);
728 if (result == FALSE) {
729 sipe_backend_media_stream_end(SIPE_MEDIA_CALL, stream);
730 return FALSE;
733 for (i = media->candidates; i; i = i->next) {
734 struct sdpcandidate *c = i->data;
735 struct sipe_backend_candidate *candidate;
736 candidate = sipe_backend_candidate_new(c->foundation,
737 c->component,
738 c->type,
739 c->protocol,
740 c->ip,
741 c->port,
742 c->username,
743 c->password);
744 sipe_backend_candidate_set_priority(candidate, c->priority);
746 backend_candidates = g_list_append(backend_candidates, candidate);
749 sipe_backend_media_add_remote_candidates(SIPE_MEDIA_CALL, stream,
750 backend_candidates);
751 sipe_media_candidate_list_free(backend_candidates);
753 SIPE_MEDIA_STREAM_PRIVATE->remote_candidates_and_codecs_set = TRUE;
755 return TRUE;
758 static void
759 apply_remote_message(struct sipe_media_call_private* call_private,
760 struct sdpmsg* msg)
762 GSList *i;
764 sipe_utils_slist_free_full(call_private->failed_media, (GDestroyNotify)sdpmedia_free);
765 call_private->failed_media = NULL;
766 call_private->encryption_compatible = TRUE;
768 for (i = msg->media; i; i = i->next) {
769 struct sdpmedia *media = i->data;
770 const gchar *enc_level =
771 sipe_utils_nameval_find(media->attributes, "encryption");
772 if (sipe_strequal(enc_level, "rejected") &&
773 get_encryption_policy(call_private->sipe_private) == SIPE_ENCRYPTION_POLICY_REQUIRED) {
774 call_private->encryption_compatible = FALSE;
777 if (!update_call_from_remote_sdp(call_private, media)) {
778 media->port = 0;
779 call_private->failed_media =
780 g_slist_append(call_private->failed_media, media);
784 /* We need to keep failed medias until response is sent, remove them
785 * from sdpmsg that is to be freed. */
786 for (i = call_private->failed_media; i; i = i->next) {
787 msg->media = g_slist_remove(msg->media, i->data);
791 static gboolean
792 call_initialized(struct sipe_media_call *call)
794 GSList *streams = SIPE_MEDIA_CALL_PRIVATE->streams;
795 for (; streams; streams = streams->next) {
796 if (!sipe_backend_stream_initialized(call, streams->data)) {
797 return FALSE;
801 return TRUE;
804 // Sends an invite response when the call is accepted and local candidates were
805 // prepared, otherwise does nothing. If error response is sent, call_private is
806 // disposed before function returns. Returns true when response was sent.
807 static gboolean
808 send_invite_response_if_ready(struct sipe_media_call_private *call_private)
810 struct sipe_backend_media *backend_media;
812 backend_media = call_private->public.backend_private;
814 if (!sipe_backend_media_accepted(backend_media) ||
815 !call_initialized(&call_private->public))
816 return FALSE;
818 if (!call_private->encryption_compatible) {
819 struct sipe_core_private *sipe_private = call_private->sipe_private;
821 sipmsg_add_header(call_private->invitation, "Warning",
822 "308 lcs.microsoft.com \"Encryption Levels not compatible\"");
823 sip_transport_response(sipe_private,
824 call_private->invitation,
825 488, "Encryption Levels not compatible",
826 NULL);
827 sipe_backend_media_reject(backend_media, FALSE);
828 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
829 _("Unable to establish a call"),
830 _("Encryption settings of peer are incompatible with ours."));
831 } else {
832 send_response_with_session_description(call_private, 200, "OK");
835 return TRUE;
838 static void
839 stream_initialized_cb(struct sipe_media_call *call,
840 struct sipe_media_stream *stream)
842 if (call_initialized(call)) {
843 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
845 if (sipe_backend_media_is_initiator(call, stream)) {
846 sipe_invite_call(call_private,
847 process_invite_call_response);
848 } else if (call_private->smsg) {
849 struct sdpmsg *smsg = call_private->smsg;
850 call_private->smsg = NULL;
852 apply_remote_message(call_private, smsg);
853 send_invite_response_if_ready(call_private);
854 sdpmsg_free(smsg);
859 static void phone_state_publish(struct sipe_core_private *sipe_private)
861 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
862 sipe_ocs2007_phone_state_publish(sipe_private);
863 } else {
864 // TODO: OCS 2005 support. Is anyone still using it at all?
868 static void
869 stream_end_cb(struct sipe_media_call* call, struct sipe_media_stream* stream)
871 remove_stream(call, SIPE_MEDIA_STREAM_PRIVATE);
874 static void
875 media_end_cb(struct sipe_media_call *call)
877 struct sipe_core_private *sipe_private;
879 g_return_if_fail(call);
881 sipe_private = SIPE_MEDIA_CALL_PRIVATE->sipe_private;
883 sipe_media_call_free(SIPE_MEDIA_CALL_PRIVATE);
884 phone_state_publish(sipe_private);
887 static void
888 call_accept_cb(struct sipe_media_call *call, gboolean local)
890 if (local) {
891 send_invite_response_if_ready(SIPE_MEDIA_CALL_PRIVATE);
893 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE->sipe_private);
896 static void
897 call_reject_cb(struct sipe_media_call *call, gboolean local)
899 if (local) {
900 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
901 struct sip_session *session = NULL;
903 sip_transport_response(call_private->sipe_private,
904 call_private->invitation,
905 603, "Decline", NULL);
907 session = sipe_session_find_call(call_private->sipe_private,
908 call->with);
909 if (session) {
910 sipe_session_remove(call_private->sipe_private, session);
915 static gboolean
916 sipe_media_send_ack(struct sipe_core_private *sipe_private, struct sipmsg *msg,
917 struct transaction *trans);
919 static void call_hold_cb(struct sipe_media_call *call,
920 gboolean local,
921 SIPE_UNUSED_PARAMETER gboolean state)
923 if (local) {
924 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE, sipe_media_send_ack);
928 static void call_hangup_cb(struct sipe_media_call *call, gboolean local)
930 if (local) {
931 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
932 struct sip_session *session;
933 session = sipe_session_find_call(call_private->sipe_private,
934 call->with);
936 if (session) {
937 sipe_session_close(call_private->sipe_private, session);
942 static void
943 error_cb(struct sipe_media_call *call, gchar *message)
945 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
946 struct sipe_core_private *sipe_private = call_private->sipe_private;
947 gboolean initiator = sipe_backend_media_is_initiator(call, NULL);
948 gboolean accepted = sipe_backend_media_accepted(call->backend_private);
950 gchar *title = g_strdup_printf("Call with %s failed", call->with);
951 sipe_backend_notify_error(SIPE_CORE_PUBLIC, title, message);
952 g_free(title);
954 if (!initiator && !accepted) {
955 sip_transport_response(sipe_private,
956 call_private->invitation,
957 488, "Not Acceptable Here", NULL);
960 sipe_backend_media_hangup(call->backend_private, initiator || accepted);
963 static struct sipe_media_call_private *
964 sipe_media_call_new(struct sipe_core_private *sipe_private, const gchar* with,
965 struct sipmsg *msg, SipeIceVersion ice_version,
966 SipeMediaCallFlags flags)
968 struct sipe_media_call_private *call_private;
969 struct sip_session *session;
970 struct sip_dialog *dialog;
971 gchar *cname;
973 session = sipe_session_add_call(sipe_private, with);
975 dialog = sipe_dialog_add(session);
976 dialog->with = g_strdup(with);
978 if (msg) {
979 gchar *newTag = gentag();
980 const gchar *oldHeader;
981 gchar *newHeader;
983 oldHeader = sipmsg_find_header(msg, "To");
984 newHeader = g_strdup_printf("%s;tag=%s", oldHeader, newTag);
985 sipmsg_remove_header_now(msg, "To");
986 sipmsg_add_header_now(msg, "To", newHeader);
987 g_free(newTag);
988 g_free(newHeader);
990 dialog->callid = g_strdup(sipmsg_find_header(msg, "Call-ID"));
991 sipe_dialog_parse(dialog, msg, FALSE);
992 } else {
993 dialog->callid = gencallid();
994 dialog->ourtag = gentag();
995 flags |= SIPE_MEDIA_CALL_INITIATOR;
998 if (g_hash_table_lookup(sipe_private->media_calls, dialog->callid)) {
999 SIPE_DEBUG_ERROR("sipe_media_call_new: call already exists for "
1000 "Call-ID %s", dialog->callid);
1001 sipe_session_remove(sipe_private, session);
1002 return NULL;
1005 call_private = g_new0(struct sipe_media_call_private, 1);
1006 call_private->sipe_private = sipe_private;
1007 SIPE_MEDIA_CALL->with = g_strdup(with);
1009 g_hash_table_insert(sipe_private->media_calls,
1010 g_strdup(dialog->callid), call_private);
1012 cname = g_strdup(sipe_private->contact + 1);
1013 cname[strlen(cname) - 1] = '\0';
1015 call_private->public.backend_private = sipe_backend_media_new(SIPE_CORE_PUBLIC,
1016 SIPE_MEDIA_CALL,
1017 with,
1018 flags);
1019 sipe_backend_media_set_cname(call_private->public.backend_private, cname);
1021 call_private->ice_version = ice_version;
1022 call_private->encryption_compatible = TRUE;
1024 call_private->public.stream_initialized_cb = stream_initialized_cb;
1025 call_private->public.stream_end_cb = stream_end_cb;
1026 call_private->public.media_end_cb = media_end_cb;
1027 call_private->public.call_accept_cb = call_accept_cb;
1028 call_private->public.call_reject_cb = call_reject_cb;
1029 call_private->public.call_hold_cb = call_hold_cb;
1030 call_private->public.call_hangup_cb = call_hangup_cb;
1031 call_private->public.error_cb = error_cb;
1033 g_free(cname);
1035 return call_private;
1038 void sipe_media_hangup(struct sipe_media_call_private *call_private)
1040 if (call_private) {
1041 sipe_backend_media_hangup(call_private->public.backend_private,
1042 FALSE);
1046 struct sipe_media_stream *
1047 sipe_media_stream_add(struct sipe_media_call *call, const gchar *id,
1048 SipeMediaType type, SipeIceVersion ice_version,
1049 gboolean initiator)
1051 struct sipe_core_private *sipe_private;
1052 struct sipe_media_stream_private *stream_private;
1053 struct sipe_backend_media_stream *backend_stream;
1054 struct sipe_backend_media_relays *backend_media_relays;
1056 sipe_private = SIPE_MEDIA_CALL_PRIVATE->sipe_private;
1058 backend_media_relays = sipe_backend_media_relays_convert(
1059 sipe_private->media_relays,
1060 sipe_private->media_relay_username,
1061 sipe_private->media_relay_password);
1063 backend_stream = sipe_backend_media_add_stream(call, id, call->with,
1064 type, ice_version,
1065 initiator,
1066 backend_media_relays);
1068 sipe_backend_media_relays_free(backend_media_relays);
1070 if (!backend_stream) {
1071 return NULL;
1074 stream_private = g_new0(struct sipe_media_stream_private, 1);
1075 SIPE_MEDIA_STREAM->id = g_strdup(id);
1076 SIPE_MEDIA_STREAM->backend_private = backend_stream;
1078 #ifdef HAVE_SRTP
1080 int i;
1081 stream_private->encryption_key = g_new0(guchar, SIPE_SRTP_KEY_LEN);
1082 for (i = 0; i != SIPE_SRTP_KEY_LEN; ++i) {
1083 stream_private->encryption_key[i] = rand() & 0xff;
1085 stream_private->encryption_key_id = 1;
1087 #endif
1089 SIPE_MEDIA_CALL_PRIVATE->streams =
1090 g_slist_append(SIPE_MEDIA_CALL_PRIVATE->streams,
1091 stream_private);
1093 return SIPE_MEDIA_STREAM;
1096 static void
1097 sipe_media_initiate_call(struct sipe_core_private *sipe_private,
1098 const char *with, SipeIceVersion ice_version,
1099 gboolean with_video)
1101 struct sipe_media_call_private *call_private;
1103 if (sipe_core_media_get_call(SIPE_CORE_PUBLIC)) {
1104 return;
1107 call_private = sipe_media_call_new(sipe_private, with, NULL,
1108 ice_version, 0);
1110 if (!sipe_media_stream_add(SIPE_MEDIA_CALL, "audio", SIPE_MEDIA_AUDIO,
1111 call_private->ice_version,
1112 TRUE)) {
1113 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
1114 _("Error occured"),
1115 _("Error creating audio stream"));
1116 sipe_media_hangup(call_private);
1117 return;
1120 if (with_video &&
1121 !sipe_media_stream_add(SIPE_MEDIA_CALL, "video", SIPE_MEDIA_VIDEO,
1122 call_private->ice_version,
1123 TRUE)) {
1124 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
1125 _("Error occured"),
1126 _("Error creating video stream"));
1127 sipe_media_hangup(call_private);
1128 return;
1131 // Processing continues in stream_initialized_cb
1134 void
1135 sipe_core_media_initiate_call(struct sipe_core_public *sipe_public,
1136 const char *with,
1137 gboolean with_video)
1139 sipe_media_initiate_call(SIPE_CORE_PRIVATE, with,
1140 SIPE_ICE_RFC_5245, with_video);
1143 void sipe_core_media_connect_conference(struct sipe_core_public *sipe_public,
1144 struct sipe_chat_session *chat_session)
1146 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1147 struct sipe_media_call_private *call_private;
1148 struct sip_session *session;
1149 SipeIceVersion ice_version;
1150 gchar **parts;
1151 gchar *av_uri;
1153 if (!sipe_conf_supports_mcu_type(sipe_private, "audio-video")) {
1154 sipe_backend_notify_error(sipe_public, _("Join conference call"),
1155 _("Conference calls are not supported on this server."));
1156 return;
1159 session = sipe_session_find_chat(sipe_private, chat_session);
1161 if (sipe_core_media_get_call(sipe_public) || !session) {
1162 return;
1165 session->is_call = TRUE;
1167 parts = g_strsplit(chat_session->id, "app:conf:focus:", 2);
1168 av_uri = g_strjoinv("app:conf:audio-video:", parts);
1169 g_strfreev(parts);
1171 ice_version = SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013) ? SIPE_ICE_RFC_5245 :
1172 SIPE_ICE_DRAFT_6;
1174 call_private = sipe_media_call_new(sipe_private, av_uri, NULL,
1175 ice_version, 0);
1177 if (!sipe_media_stream_add(SIPE_MEDIA_CALL, "audio", SIPE_MEDIA_AUDIO,
1178 call_private->ice_version, TRUE)) {
1179 sipe_backend_notify_error(sipe_public,
1180 _("Error occurred"),
1181 _("Error creating audio stream"));
1183 sipe_media_hangup(call_private);
1186 g_free(av_uri);
1188 // Processing continues in stream_initialized_cb
1191 struct sipe_media_call *
1192 sipe_core_media_get_call(struct sipe_core_public *sipe_public)
1194 struct sipe_media_call * result = NULL;
1195 GList *calls = g_hash_table_get_values(SIPE_CORE_PRIVATE->media_calls);
1197 for (; calls; calls = g_list_delete_link(calls, calls)) {
1198 if (sipe_core_media_get_stream_by_id(calls->data, "audio")) {
1199 result = calls->data;
1200 break;
1204 return result;
1207 static gboolean phone_number_is_valid(const gchar *phone_number)
1209 if (!phone_number || sipe_strequal(phone_number, "")) {
1210 return FALSE;
1213 if (*phone_number == '+') {
1214 ++phone_number;
1217 while (*phone_number != '\0') {
1218 if (!g_ascii_isdigit(*phone_number)) {
1219 return FALSE;
1221 ++phone_number;
1224 return TRUE;
1227 void sipe_core_media_phone_call(struct sipe_core_public *sipe_public,
1228 const gchar *phone_number)
1230 g_return_if_fail(sipe_public);
1232 if (phone_number_is_valid(phone_number)) {
1233 gchar *phone_uri = g_strdup_printf("sip:%s@%s;user=phone",
1234 phone_number, sipe_public->sip_domain);
1236 sipe_core_media_initiate_call(sipe_public, phone_uri, FALSE);
1238 g_free(phone_uri);
1239 } else {
1240 sipe_backend_notify_error(sipe_public,
1241 _("Unable to establish a call"),
1242 _("Invalid phone number"));
1246 void sipe_core_media_test_call(struct sipe_core_public *sipe_public)
1248 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1249 if (!sipe_private->test_call_bot_uri) {
1250 sipe_backend_notify_error(sipe_public,
1251 _("Unable to establish a call"),
1252 _("Audio Test Service is not available."));
1253 return;
1256 sipe_core_media_initiate_call(sipe_public,
1257 sipe_private->test_call_bot_uri, FALSE);
1260 static struct sipe_media_call_private *
1261 sipe_media_from_sipmsg(struct sipe_core_private *sipe_private,
1262 struct sipmsg *msg)
1264 return g_hash_table_lookup(sipe_private->media_calls,
1265 sipmsg_find_header(msg, "Call-ID"));
1268 struct sipe_media_call_private *
1269 process_incoming_invite_call(struct sipe_core_private *sipe_private,
1270 struct sipmsg *msg)
1272 struct sipe_media_call_private *call_private;
1273 struct sdpmsg *smsg;
1274 gboolean has_new_media = FALSE;
1275 GSList *i;
1277 // Don't allow two voice calls in parallel.
1278 if (!strstr(msg->body, "m=data") &&
1279 !strstr(msg->body, "m=applicationsharing")) {
1280 struct sipe_media_call *call =
1281 sipe_core_media_get_call(SIPE_CORE_PUBLIC);
1282 if (call && !is_media_session_msg(SIPE_MEDIA_CALL_PRIVATE, msg)) {
1283 sip_transport_response(sipe_private, msg,
1284 486, "Busy Here", NULL);
1285 return NULL;
1289 call_private = sipe_media_from_sipmsg(sipe_private, msg);
1291 if (call_private) {
1292 char *self = sip_uri_self(sipe_private);
1293 if (sipe_strequal(SIPE_MEDIA_CALL->with, self)) {
1294 g_free(self);
1295 sip_transport_response(sipe_private, msg, 488, "Not Acceptable Here", NULL);
1296 return NULL;
1298 g_free(self);
1301 smsg = sdpmsg_parse_msg(msg->body);
1302 if (!smsg) {
1303 sip_transport_response(sipe_private, msg,
1304 488, "Not Acceptable Here", NULL);
1305 if (call_private) {
1306 sipe_media_hangup(call_private);
1308 return NULL;
1311 if (!call_private) {
1312 gchar *with = parse_from(sipmsg_find_header(msg, "From"));
1313 SipeMediaCallFlags flags = 0;
1315 if (strstr(msg->body, "m=data")) {
1316 flags |= SIPE_MEDIA_CALL_NO_UI;
1319 call_private = sipe_media_call_new(sipe_private, with, msg,
1320 smsg->ice_version, flags);
1322 g_free(with);
1325 if (call_private->invitation)
1326 sipmsg_free(call_private->invitation);
1327 call_private->invitation = sipmsg_copy(msg);
1329 // Create any new media streams
1330 for (i = smsg->media; i; i = i->next) {
1331 struct sdpmedia *media = i->data;
1332 gchar *id = media->name;
1333 SipeMediaType type;
1335 if ( media->port != 0
1336 && !sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL, id)) {
1337 if (sipe_strequal(id, "audio"))
1338 type = SIPE_MEDIA_AUDIO;
1339 else if (sipe_strequal(id, "video"))
1340 type = SIPE_MEDIA_VIDEO;
1341 else if (sipe_strequal(id, "data"))
1342 type = SIPE_MEDIA_APPLICATION;
1343 else
1344 continue;
1346 sipe_media_stream_add(SIPE_MEDIA_CALL, id, type,
1347 smsg->ice_version, FALSE);
1348 has_new_media = TRUE;
1352 if (has_new_media) {
1353 sdpmsg_free(call_private->smsg);
1354 call_private->smsg = smsg;
1355 sip_transport_response(sipe_private, call_private->invitation,
1356 180, "Ringing", NULL);
1357 // Processing continues in stream_initialized_cb
1358 } else {
1359 apply_remote_message(call_private, smsg);
1360 send_response_with_session_description(call_private, 200, "OK");
1362 sdpmsg_free(smsg);
1365 return call_private;
1368 void process_incoming_cancel_call(struct sipe_media_call_private *call_private,
1369 struct sipmsg *msg)
1371 // We respond to the CANCEL request with 200 OK response and
1372 // with 487 Request Terminated to the remote INVITE in progress.
1373 sip_transport_response(call_private->sipe_private, msg, 200, "OK", NULL);
1375 if (call_private->invitation) {
1376 sip_transport_response(call_private->sipe_private,
1377 call_private->invitation,
1378 487, "Request Terminated", NULL);
1381 sipe_media_hangup(call_private);
1384 static gboolean
1385 sipe_media_send_ack(struct sipe_core_private *sipe_private,
1386 struct sipmsg *msg,
1387 struct transaction *trans)
1389 struct sipe_media_call_private *call_private;
1390 struct sip_session *session;
1391 struct sip_dialog *dialog;
1392 int tmp_cseq;
1394 call_private = sipe_media_from_sipmsg(sipe_private, msg);
1396 if (!is_media_session_msg(call_private, msg))
1397 return FALSE;
1399 session = sipe_session_find_call(sipe_private, SIPE_MEDIA_CALL->with);
1400 dialog = session->dialogs->data;
1401 if (!dialog)
1402 return FALSE;
1404 tmp_cseq = dialog->cseq;
1406 dialog->cseq = sip_transaction_cseq(trans) - 1;
1407 sip_transport_ack(sipe_private, dialog);
1408 dialog->cseq = tmp_cseq;
1410 dialog->outgoing_invite = NULL;
1412 return TRUE;
1415 static gboolean
1416 sipe_media_send_final_ack(struct sipe_core_private *sipe_private,
1417 struct sipmsg *msg,
1418 struct transaction *trans)
1420 struct sipe_media_call_private *call_private;
1422 if (!sipe_media_send_ack(sipe_private, msg, trans))
1423 return FALSE;
1425 call_private = sipe_media_from_sipmsg(sipe_private, msg);
1427 sipe_backend_media_accept(SIPE_MEDIA_CALL->backend_private, FALSE);
1429 return TRUE;
1432 static void
1433 reinvite_on_candidate_pair_cb(struct sipe_core_public *sipe_public,
1434 struct sipe_media_call_private *media_call)
1436 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1437 GSList *streams;
1439 if (!media_call)
1440 return;
1442 streams = media_call->streams;
1444 for (; streams; streams = streams->next) {
1445 struct sipe_media_stream *s = streams->data;
1446 GList *remote_candidates = sipe_backend_media_get_active_remote_candidates(&media_call->public, s);
1447 guint components = g_list_length(remote_candidates);
1449 sipe_media_candidate_list_free(remote_candidates);
1451 // We must have candidates for both (RTP + RTCP) components ready
1452 if (components < 2) {
1453 sipe_schedule_mseconds(sipe_private,
1454 "<+media-reinvite-on-candidate-pair>",
1455 media_call,
1456 500,
1457 (sipe_schedule_action) reinvite_on_candidate_pair_cb,
1458 NULL);
1459 return;
1463 sipe_invite_call(media_call, sipe_media_send_final_ack);
1466 static gboolean
1467 maybe_retry_call_with_ice_version(struct sipe_media_call_private *call_private,
1468 SipeIceVersion ice_version,
1469 struct transaction *trans)
1471 if (call_private->ice_version != ice_version &&
1472 sip_transaction_cseq(trans) == 1) {
1473 gchar *with = g_strdup(SIPE_MEDIA_CALL->with);
1474 gboolean with_video = sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL, "video") != NULL;
1476 sipe_media_hangup(call_private);
1477 SIPE_DEBUG_INFO("Retrying call with ICEv%d.",
1478 ice_version == SIPE_ICE_DRAFT_6 ? 6 : 19);
1479 sipe_media_initiate_call(call_private->sipe_private, with,
1480 ice_version, with_video);
1482 g_free(with);
1483 return TRUE;
1486 return FALSE;
1489 static gboolean
1490 process_invite_call_response(struct sipe_core_private *sipe_private,
1491 struct sipmsg *msg,
1492 struct transaction *trans)
1494 const gchar *with;
1495 struct sipe_media_call_private *call_private;
1496 struct sip_session *session;
1497 struct sip_dialog *dialog;
1498 struct sdpmsg *smsg;
1500 call_private = sipe_media_from_sipmsg(sipe_private,msg);
1502 if (!is_media_session_msg(call_private, msg))
1503 return FALSE;
1505 session = sipe_session_find_call(sipe_private, SIPE_MEDIA_CALL->with);
1506 dialog = session->dialogs->data;
1508 with = dialog->with;
1510 dialog->outgoing_invite = NULL;
1512 if (msg->response >= 400) {
1513 // Call rejected by remote peer or an error occurred
1514 const gchar *title;
1515 GString *desc = g_string_new("");
1516 gboolean append_responsestr = FALSE;
1518 switch (msg->response) {
1519 case 480: {
1520 title = _("User unavailable");
1522 if (sipmsg_parse_warning(msg, NULL) == 391) {
1523 g_string_append_printf(desc, _("%s does not want to be disturbed"), with);
1524 } else
1525 g_string_append_printf(desc, _("User %s is not available"), with);
1526 break;
1528 case 603:
1529 case 605:
1530 title = _("Call rejected");
1531 g_string_append_printf(desc, _("User %s rejected call"), with);
1532 break;
1533 case 415:
1534 // OCS/Lync really sends response string with 'Mutipart' typo.
1535 if (sipe_strequal(msg->responsestr, "Mutipart mime in content type not supported by Archiving CDR service") &&
1536 maybe_retry_call_with_ice_version(call_private, SIPE_ICE_DRAFT_6, trans)) {
1537 return TRUE;
1539 title = _("Unsupported media type");
1540 break;
1541 case 488: {
1542 /* Check for incompatible encryption levels error.
1544 * MS Lync 2010:
1545 * 488 Not Acceptable Here
1546 * ms-client-diagnostics: 52017;reason="Encryption levels dont match"
1548 * older clients (and SIPE itself):
1549 * 488 Encryption Levels not compatible
1551 const gchar *ms_diag = sipmsg_find_header(msg, "ms-client-diagnostics");
1552 SipeIceVersion retry_ice_version = SIPE_ICE_DRAFT_6;
1554 if (sipe_strequal(msg->responsestr, "Encryption Levels not compatible") ||
1555 (ms_diag && g_str_has_prefix(ms_diag, "52017;"))) {
1556 title = _("Unable to establish a call");
1557 g_string_append(desc, _("Encryption settings of peer are incompatible with ours."));
1558 break;
1561 /* Check if this is failed conference using
1562 * ICEv6 with reason "Error parsing SDP" and
1563 * retry using ICEv19. */
1564 ms_diag = sipmsg_find_header(msg, "ms-diagnostics");
1565 if (ms_diag && g_str_has_prefix(ms_diag, "7008;")) {
1566 retry_ice_version = SIPE_ICE_RFC_5245;
1569 if (maybe_retry_call_with_ice_version(call_private, retry_ice_version, trans)) {
1570 return TRUE;
1572 // Break intentionally omitted
1574 default:
1575 title = _("Error occured");
1576 g_string_append(desc, _("Unable to establish a call"));
1577 append_responsestr = TRUE;
1578 break;
1581 if (append_responsestr) {
1582 gchar *reason = sipmsg_get_ms_diagnostics_reason(msg);
1584 g_string_append_printf(desc, "\n%d %s",
1585 msg->response, msg->responsestr);
1586 if (reason) {
1587 g_string_append_printf(desc, "\n\n%s", reason);
1588 g_free(reason);
1592 sipe_backend_notify_error(SIPE_CORE_PUBLIC, title, desc->str);
1593 g_string_free(desc, TRUE);
1595 sipe_media_send_ack(sipe_private, msg, trans);
1596 sipe_media_hangup(call_private);
1598 return TRUE;
1601 sipe_dialog_parse(dialog, msg, TRUE);
1602 smsg = sdpmsg_parse_msg(msg->body);
1603 if (!smsg) {
1604 sip_transport_response(sipe_private, msg,
1605 488, "Not Acceptable Here", NULL);
1606 sipe_media_hangup(call_private);
1607 return FALSE;
1610 apply_remote_message(call_private, smsg);
1611 sdpmsg_free(smsg);
1613 sipe_media_send_ack(sipe_private, msg, trans);
1614 reinvite_on_candidate_pair_cb(SIPE_CORE_PUBLIC, call_private);
1616 return TRUE;
1619 gboolean is_media_session_msg(struct sipe_media_call_private *call_private,
1620 struct sipmsg *msg)
1622 if (!call_private) {
1623 return FALSE;
1626 return sipe_media_from_sipmsg(call_private->sipe_private, msg) == call_private;
1629 static void
1630 end_call(SIPE_UNUSED_PARAMETER gpointer key,
1631 struct sipe_media_call_private *call_private,
1632 SIPE_UNUSED_PARAMETER gpointer user_data)
1634 struct sipe_backend_media *backend_private;
1636 backend_private = call_private->public.backend_private;
1638 if (!sipe_backend_media_is_initiator(SIPE_MEDIA_CALL, NULL) &&
1639 !sipe_backend_media_accepted(backend_private)) {
1640 sip_transport_response(call_private->sipe_private,
1641 call_private->invitation,
1642 480, "Temporarily Unavailable", NULL);
1643 } else {
1644 struct sip_session *session;
1646 session = sipe_session_find_call(call_private->sipe_private,
1647 SIPE_MEDIA_CALL->with);
1648 if (session)
1649 sipe_session_close(call_private->sipe_private, session);
1652 sipe_media_hangup(call_private);
1655 void
1656 sipe_media_handle_going_offline(struct sipe_core_private *sipe_private)
1658 g_hash_table_foreach(sipe_private->media_calls, (GHFunc) end_call, NULL);
1661 gboolean sipe_media_is_conference_call(struct sipe_media_call_private *call_private)
1663 return g_strstr_len(SIPE_MEDIA_CALL->with, -1, "app:conf:audio-video:") != NULL;
1666 static void
1667 sipe_media_relay_free(struct sipe_media_relay *relay)
1669 g_free(relay->hostname);
1670 if (relay->dns_query)
1671 sipe_backend_dns_query_cancel(relay->dns_query);
1672 g_free(relay);
1675 void
1676 sipe_media_relay_list_free(GSList *list)
1678 for (; list; list = g_slist_delete_link(list, list))
1679 sipe_media_relay_free(list->data);
1682 static void
1683 relay_ip_resolved_cb(struct sipe_media_relay* relay,
1684 const gchar *ip, SIPE_UNUSED_PARAMETER guint port)
1686 gchar *hostname = relay->hostname;
1687 relay->dns_query = NULL;
1689 if (ip && port) {
1690 relay->hostname = g_strdup(ip);
1691 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname, ip);
1692 } else {
1693 relay->hostname = NULL;
1694 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname);
1697 g_free(hostname);
1700 static gboolean
1701 process_get_av_edge_credentials_response(struct sipe_core_private *sipe_private,
1702 struct sipmsg *msg,
1703 SIPE_UNUSED_PARAMETER struct transaction *trans)
1705 g_free(sipe_private->media_relay_username);
1706 g_free(sipe_private->media_relay_password);
1707 sipe_media_relay_list_free(sipe_private->media_relays);
1708 sipe_private->media_relay_username = NULL;
1709 sipe_private->media_relay_password = NULL;
1710 sipe_private->media_relays = NULL;
1712 if (msg->response >= 400) {
1713 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
1714 "Failed to obtain A/V Edge credentials.");
1715 return FALSE;
1718 if (msg->response == 200) {
1719 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
1721 if (sipe_strequal("OK", sipe_xml_attribute(xn_response, "reasonPhrase"))) {
1722 const sipe_xml *xn_credentials = sipe_xml_child(xn_response, "credentialsResponse/credentials");
1723 const sipe_xml *xn_relays = sipe_xml_child(xn_response, "credentialsResponse/mediaRelayList");
1724 const sipe_xml *item;
1725 GSList *relays = NULL;
1727 item = sipe_xml_child(xn_credentials, "username");
1728 sipe_private->media_relay_username = sipe_xml_data(item);
1729 item = sipe_xml_child(xn_credentials, "password");
1730 sipe_private->media_relay_password = sipe_xml_data(item);
1732 for (item = sipe_xml_child(xn_relays, "mediaRelay"); item; item = sipe_xml_twin(item)) {
1733 struct sipe_media_relay *relay = g_new0(struct sipe_media_relay, 1);
1734 const sipe_xml *node;
1735 gchar *tmp;
1737 node = sipe_xml_child(item, "hostName");
1738 relay->hostname = sipe_xml_data(node);
1740 node = sipe_xml_child(item, "udpPort");
1741 if (node) {
1742 relay->udp_port = atoi(tmp = sipe_xml_data(node));
1743 g_free(tmp);
1746 node = sipe_xml_child(item, "tcpPort");
1747 if (node) {
1748 relay->tcp_port = atoi(tmp = sipe_xml_data(node));
1749 g_free(tmp);
1752 relays = g_slist_append(relays, relay);
1754 relay->dns_query = sipe_backend_dns_query_a(
1755 SIPE_CORE_PUBLIC,
1756 relay->hostname,
1757 relay->udp_port,
1758 (sipe_dns_resolved_cb) relay_ip_resolved_cb,
1759 relay);
1761 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
1762 relay->hostname,
1763 relay->tcp_port, relay->udp_port);
1766 sipe_private->media_relays = relays;
1769 sipe_xml_free(xn_response);
1772 return TRUE;
1775 void
1776 sipe_media_get_av_edge_credentials(struct sipe_core_private *sipe_private)
1778 // TODO: re-request credentials after duration expires?
1779 static const char CRED_REQUEST_XML[] =
1780 "<request requestID=\"%d\" "
1781 "from=\"%s\" "
1782 "version=\"1.0\" "
1783 "to=\"%s\" "
1784 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
1785 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
1786 "<credentialsRequest credentialsRequestID=\"%d\">"
1787 "<identity>%s</identity>"
1788 "<location>%s</location>"
1789 "<duration>480</duration>"
1790 "</credentialsRequest>"
1791 "</request>";
1793 int request_id = rand();
1794 gchar *self;
1795 gchar *body;
1797 if (!sipe_private->mras_uri)
1798 return;
1800 self = sip_uri_self(sipe_private);
1802 body = g_strdup_printf(
1803 CRED_REQUEST_XML,
1804 request_id,
1805 self,
1806 sipe_private->mras_uri,
1807 request_id,
1808 self,
1809 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER) ? "internet" : "intranet");
1810 g_free(self);
1812 sip_transport_service(sipe_private,
1813 sipe_private->mras_uri,
1814 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
1815 body,
1816 process_get_av_edge_credentials_response);
1818 g_free(body);
1821 void
1822 sipe_media_stream_add_extra_attribute(struct sipe_media_stream *stream,
1823 const gchar *name, const gchar *value)
1825 SIPE_MEDIA_STREAM_PRIVATE->extra_sdp =
1826 sipe_utils_nameval_add(SIPE_MEDIA_STREAM_PRIVATE->extra_sdp,
1827 name, value);
1830 void
1831 sipe_media_stream_set_data(struct sipe_media_stream *stream, gpointer data,
1832 GDestroyNotify free_func)
1834 struct sipe_media_stream_private *stream_private =
1835 SIPE_MEDIA_STREAM_PRIVATE;
1837 g_return_if_fail(stream_private);
1839 if (stream_private->data && stream_private->data_free_func) {
1840 stream_private->data_free_func(stream_private->data);
1843 stream_private->data = data;
1844 stream_private->data_free_func = free_func;
1847 gpointer
1848 sipe_media_stream_get_data(struct sipe_media_stream *stream)
1850 g_return_val_if_fail(stream, NULL);
1852 return SIPE_MEDIA_STREAM_PRIVATE->data;
1856 Local Variables:
1857 mode: c
1858 c-file-style: "bsd"
1859 indent-tabs-mode: t
1860 tab-width: 8
1861 End: