conf: factor out session URI formation
[siplcs.git] / src / core / sipe-media.c
blob500d641fcc97943664010a3bd334faa113571428
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;
76 gboolean established;
78 GSList *extra_sdp;
80 GQueue *async_reads;
81 gssize read_pos;
83 /* User data associated with the stream. */
84 gpointer data;
85 GDestroyNotify data_free_func;
87 #define SIPE_MEDIA_STREAM ((struct sipe_media_stream *) stream_private)
88 #define SIPE_MEDIA_STREAM_PRIVATE ((struct sipe_media_stream_private *) stream)
90 struct async_read_data {
91 guint8 *buffer;
92 gssize len;
93 sipe_media_stream_read_callback callback;
96 static void sipe_media_codec_list_free(GList *codecs)
98 for (; codecs; codecs = g_list_delete_link(codecs, codecs))
99 sipe_backend_codec_free(codecs->data);
102 static void sipe_media_candidate_list_free(GList *candidates)
104 for (; candidates; candidates = g_list_delete_link(candidates, candidates))
105 sipe_backend_candidate_free(candidates->data);
108 static void
109 remove_stream(struct sipe_media_call* call,
110 struct sipe_media_stream_private *stream_private)
112 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
114 sipe_media_stream_set_data(SIPE_MEDIA_STREAM, NULL, NULL);
116 call_private->streams =
117 g_slist_remove(call_private->streams, stream_private);
118 sipe_backend_media_stream_free(SIPE_MEDIA_STREAM->backend_private);
119 g_free(SIPE_MEDIA_STREAM->id);
120 g_free(stream_private->encryption_key);
121 g_queue_free_full(stream_private->async_reads, g_free);
122 sipe_utils_nameval_free(stream_private->extra_sdp);
123 g_free(stream_private);
126 static gboolean
127 call_private_equals(SIPE_UNUSED_PARAMETER const gchar *callid,
128 struct sipe_media_call_private *call_private1,
129 struct sipe_media_call_private *call_private2)
131 return call_private1 == call_private2;
134 static void
135 sipe_media_call_free(struct sipe_media_call_private *call_private)
137 if (call_private) {
138 struct sip_session *session;
140 g_hash_table_foreach_remove(call_private->sipe_private->media_calls,
141 (GHRFunc) call_private_equals, call_private);
143 while (call_private->streams) {
144 remove_stream(SIPE_MEDIA_CALL,
145 call_private->streams->data);
148 sipe_backend_media_free(call_private->public.backend_private);
150 session = sipe_session_find_call(call_private->sipe_private,
151 SIPE_MEDIA_CALL->with);
152 if (session)
153 sipe_session_remove(call_private->sipe_private, session);
155 if (call_private->invitation)
156 sipmsg_free(call_private->invitation);
158 sdpmsg_free(call_private->smsg);
159 sipe_utils_slist_free_full(call_private->failed_media,
160 (GDestroyNotify)sdpmedia_free);
161 g_free(SIPE_MEDIA_CALL->with);
162 g_free(call_private);
166 static gint
167 candidate_sort_cb(struct sdpcandidate *c1, struct sdpcandidate *c2)
169 int cmp = sipe_strcompare(c1->foundation, c2->foundation);
170 if (cmp == 0) {
171 cmp = sipe_strcompare(c1->username, c2->username);
172 if (cmp == 0)
173 cmp = c1->component - c2->component;
176 return cmp;
179 static GSList *
180 backend_candidates_to_sdpcandidate(GList *candidates)
182 GSList *result = NULL;
183 GList *i;
185 for (i = candidates; i; i = i->next) {
186 struct sipe_backend_candidate *candidate = i->data;
187 struct sdpcandidate *c;
189 gchar *ip = sipe_backend_candidate_get_ip(candidate);
190 gchar *base_ip = sipe_backend_candidate_get_base_ip(candidate);
191 if (is_empty(ip) || strchr(ip, ':') ||
192 (base_ip && strchr(base_ip, ':'))) {
193 /* Ignore IPv6 candidates. */
194 g_free(ip);
195 g_free(base_ip);
196 continue;
199 c = g_new(struct sdpcandidate, 1);
200 c->foundation = sipe_backend_candidate_get_foundation(candidate);
201 c->component = sipe_backend_candidate_get_component_type(candidate);
202 c->type = sipe_backend_candidate_get_type(candidate);
203 c->protocol = sipe_backend_candidate_get_protocol(candidate);
204 c->ip = ip;
205 c->port = sipe_backend_candidate_get_port(candidate);
206 c->base_ip = base_ip;
207 c->base_port = sipe_backend_candidate_get_base_port(candidate);
208 c->priority = sipe_backend_candidate_get_priority(candidate);
209 c->username = sipe_backend_candidate_get_username(candidate);
210 c->password = sipe_backend_candidate_get_password(candidate);
212 result = g_slist_insert_sorted(result, c,
213 (GCompareFunc)candidate_sort_cb);
216 return result;
219 static void
220 get_stream_ip_and_ports(GSList *candidates,
221 gchar **ip, guint *rtp_port, guint *rtcp_port)
223 guint32 rtp_max_priority = 0;
224 guint32 rtcp_max_priority = 0;
226 *ip = 0;
227 *rtp_port = 0;
228 *rtcp_port = 0;
230 for (; candidates; candidates = candidates->next) {
231 struct sdpcandidate *candidate = candidates->data;
233 if (candidate->component == SIPE_COMPONENT_RTP &&
234 candidate->priority > rtp_max_priority) {
235 rtp_max_priority = candidate->priority;
236 *rtp_port = candidate->port;
238 g_free(*ip);
239 *ip = g_strdup(candidate->ip);
240 } else if (candidate->component == SIPE_COMPONENT_RTCP &&
241 candidate->priority > rtcp_max_priority) {
242 rtcp_max_priority = candidate->priority;
243 *rtcp_port = candidate->port;
248 static gint
249 sdpcodec_compare(gconstpointer a, gconstpointer b)
251 return ((const struct sdpcodec *)a)->id -
252 ((const struct sdpcodec *)b)->id;
255 static GList *
256 remove_wrong_farstream_0_1_tcp_candidates(GList *candidates)
258 GList *i = candidates;
259 GHashTable *foundation_to_candidate = g_hash_table_new_full(g_str_hash,
260 g_str_equal,
261 g_free,
262 NULL);
264 while (i) {
265 GList *next = i->next;
266 struct sipe_backend_candidate *c1 = i->data;
268 if (sipe_backend_candidate_get_protocol(c1) == SIPE_NETWORK_PROTOCOL_UDP) {
269 gchar *foundation = sipe_backend_candidate_get_foundation(c1);
270 struct sipe_backend_candidate *c2 = g_hash_table_lookup(foundation_to_candidate,
271 foundation);
273 if (c2) {
274 g_free(foundation);
276 if (sipe_backend_candidate_get_port(c1) ==
277 sipe_backend_candidate_get_port(c2) ||
278 (sipe_backend_candidate_get_type(c1) !=
279 SIPE_CANDIDATE_TYPE_HOST &&
280 sipe_backend_candidate_get_base_port(c1) ==
281 sipe_backend_candidate_get_base_port(c2))) {
283 * We assume that RTP+RTCP UDP pairs
284 * that share the same port are
285 * actually mistagged TCP candidates.
287 candidates = g_list_remove(candidates, c2);
288 candidates = g_list_delete_link(candidates, i);
289 sipe_backend_candidate_free(c1);
290 sipe_backend_candidate_free(c2);
292 } else
293 /* hash table takes ownership of "foundation" */
294 g_hash_table_insert(foundation_to_candidate, foundation, c1);
297 i = next;
300 g_hash_table_destroy(foundation_to_candidate);
302 return candidates;
305 static void
306 fill_zero_tcp_act_ports_from_tcp_pass(GSList *candidates, GSList *all_candidates)
308 GSList *i;
309 GHashTable *ip_to_port = g_hash_table_new(g_str_hash, g_str_equal);
311 for (i = candidates; i; i = i->next) {
312 struct sdpcandidate *c = i->data;
313 GSList *j;
315 if (c->protocol != SIPE_NETWORK_PROTOCOL_TCP_ACTIVE) {
316 continue;
319 for (j = all_candidates; j; j = j->next) {
320 struct sdpcandidate *passive = j->data;
321 if (passive->protocol != SIPE_NETWORK_PROTOCOL_TCP_PASSIVE ||
322 c->type != passive->type) {
323 continue;
326 if (sipe_strequal(c->ip, passive->ip) &&
327 sipe_strequal(c->base_ip, passive->base_ip)) {
328 if (c->port == 0) {
329 c->port = passive->port;
332 if (c->base_port == 0) {
333 c->base_port = passive->base_port;
335 break;
340 for (i = all_candidates; i; i = i->next) {
341 struct sdpcandidate *c = i->data;
343 if (c->protocol == SIPE_NETWORK_PROTOCOL_TCP_PASSIVE &&
344 c->type == SIPE_CANDIDATE_TYPE_HOST) {
345 g_hash_table_insert(ip_to_port, c->ip, &c->port);
349 /* Fill base ports of all TCP relay candidates using what we have
350 * collected from host candidates. */
351 for (i = candidates; i; i = i->next) {
352 struct sdpcandidate *c = i->data;
353 if (c->type == SIPE_CANDIDATE_TYPE_RELAY && c->base_port == 0) {
354 guint *base_port = (guint*)g_hash_table_lookup(ip_to_port, c->base_ip);
355 if (base_port) {
356 c->base_port = *base_port;
357 } else {
358 SIPE_DEBUG_WARNING("Couldn't determine base port for candidate "
359 "with foundation %s", c->foundation);
364 g_hash_table_destroy(ip_to_port);
367 static SipeEncryptionPolicy
368 get_encryption_policy(struct sipe_core_private *sipe_private)
370 SipeEncryptionPolicy result =
371 sipe_backend_media_get_encryption_policy(SIPE_CORE_PUBLIC);
372 if (result == SIPE_ENCRYPTION_POLICY_OBEY_SERVER) {
373 result = sipe_private->server_av_encryption_policy;
376 return result;
379 static struct sdpmedia *
380 media_stream_to_sdpmedia(struct sipe_media_call_private *call_private,
381 struct sipe_media_stream_private *stream_private)
383 struct sdpmedia *sdpmedia = g_new0(struct sdpmedia, 1);
384 GList *codecs = sipe_backend_get_local_codecs(SIPE_MEDIA_CALL,
385 SIPE_MEDIA_STREAM);
386 SipeEncryptionPolicy encryption_policy =
387 get_encryption_policy(call_private->sipe_private);
388 guint rtcp_port = 0;
389 SipeMediaType type;
390 GSList *attributes = NULL;
391 GSList *sdpcandidates;
392 GSList *all_sdpcandidates;
393 GList *candidates;
394 GList *i;
395 GSList *j;
397 sdpmedia->name = g_strdup(SIPE_MEDIA_STREAM->id);
399 if (sipe_strequal(sdpmedia->name, "audio"))
400 type = SIPE_MEDIA_AUDIO;
401 else if (sipe_strequal(sdpmedia->name, "video"))
402 type = SIPE_MEDIA_VIDEO;
403 else if (sipe_strequal(sdpmedia->name, "data"))
404 type = SIPE_MEDIA_APPLICATION;
405 else {
406 // TODO: incompatible media, should not happen here
407 g_free(sdpmedia->name);
408 g_free(sdpmedia);
409 sipe_media_codec_list_free(codecs);
410 return(NULL);
413 // Process codecs
414 for (i = codecs; i; i = i->next) {
415 struct sipe_backend_codec *codec = i->data;
416 struct sdpcodec *c = g_new0(struct sdpcodec, 1);
417 GList *params;
419 c->id = sipe_backend_codec_get_id(codec);
420 c->name = sipe_backend_codec_get_name(codec);
421 c->clock_rate = sipe_backend_codec_get_clock_rate(codec);
422 c->type = type;
424 params = sipe_backend_codec_get_optional_parameters(codec);
425 for (; params; params = params->next) {
426 struct sipnameval *param = params->data;
427 struct sipnameval *copy = g_new0(struct sipnameval, 1);
429 copy->name = g_strdup(param->name);
430 copy->value = g_strdup(param->value);
432 c->parameters = g_slist_append(c->parameters, copy);
435 /* Buggy(?) codecs may report non-unique id (a.k.a. payload
436 * type) that must not appear in SDP messages we send. Thus,
437 * let's ignore any codec having the same id as one we already
438 * have in the converted list. */
439 sdpmedia->codecs = sipe_utils_slist_insert_unique_sorted(
440 sdpmedia->codecs, c, sdpcodec_compare,
441 (GDestroyNotify)sdpcodec_free);
444 sipe_media_codec_list_free(codecs);
446 // Process local candidates
447 // If we have established candidate pairs, send them in SDP response.
448 // Otherwise send all available local candidates.
449 candidates = sipe_backend_media_stream_get_active_local_candidates(SIPE_MEDIA_STREAM);
450 sdpcandidates = backend_candidates_to_sdpcandidate(candidates);
451 sipe_media_candidate_list_free(candidates);
453 candidates = sipe_backend_get_local_candidates(SIPE_MEDIA_CALL,
454 SIPE_MEDIA_STREAM);
455 candidates = remove_wrong_farstream_0_1_tcp_candidates(candidates);
456 all_sdpcandidates = backend_candidates_to_sdpcandidate(candidates);
457 sipe_media_candidate_list_free(candidates);
459 if (!sdpcandidates) {
460 sdpcandidates = all_sdpcandidates;
463 fill_zero_tcp_act_ports_from_tcp_pass(sdpcandidates, all_sdpcandidates);
465 sdpmedia->candidates = sdpcandidates;
467 if (all_sdpcandidates != sdpcandidates) {
468 sipe_utils_slist_free_full(all_sdpcandidates,
469 (GDestroyNotify)sdpcandidate_free);
472 get_stream_ip_and_ports(sdpmedia->candidates, &sdpmedia->ip,
473 &sdpmedia->port, &rtcp_port);
475 if (sipe_backend_stream_is_held(SIPE_MEDIA_STREAM))
476 attributes = sipe_utils_nameval_add(attributes, "inactive", "");
478 if (rtcp_port) {
479 gchar *tmp = g_strdup_printf("%u", rtcp_port);
480 attributes = sipe_utils_nameval_add(attributes, "rtcp", tmp);
481 g_free(tmp);
484 if (encryption_policy != call_private->sipe_private->server_av_encryption_policy) {
485 const gchar *encryption = NULL;
486 switch (encryption_policy) {
487 case SIPE_ENCRYPTION_POLICY_REJECTED:
488 encryption = "rejected";
489 break;
490 case SIPE_ENCRYPTION_POLICY_OPTIONAL:
491 encryption = "optional";
492 break;
493 case SIPE_ENCRYPTION_POLICY_REQUIRED:
494 default:
495 encryption = "required";
496 break;
499 attributes = sipe_utils_nameval_add(attributes, "encryption", encryption);
502 // Process remote candidates
503 candidates = sipe_backend_media_stream_get_active_remote_candidates(SIPE_MEDIA_STREAM);
504 sdpmedia->remote_candidates = backend_candidates_to_sdpcandidate(candidates);
505 sipe_media_candidate_list_free(candidates);
507 sdpmedia->encryption_active = stream_private->encryption_key &&
508 call_private->encryption_compatible &&
509 stream_private->remote_candidates_and_codecs_set &&
510 encryption_policy != SIPE_ENCRYPTION_POLICY_REJECTED;
512 // Set our key if encryption is enabled.
513 if (stream_private->encryption_key &&
514 encryption_policy != SIPE_ENCRYPTION_POLICY_REJECTED) {
515 sdpmedia->encryption_key = g_memdup(stream_private->encryption_key,
516 SIPE_SRTP_KEY_LEN);
517 sdpmedia->encryption_key_id = stream_private->encryption_key_id;
520 // Append extra attributes assigned to the stream.
521 for (j = stream_private->extra_sdp; j; j = g_slist_next(j)) {
522 struct sipnameval *attr = j->data;
523 attributes = sipe_utils_nameval_add(attributes,
524 attr->name, attr->value);
527 sdpmedia->attributes = attributes;
529 return sdpmedia;
532 static struct sdpmsg *
533 sipe_media_to_sdpmsg(struct sipe_media_call_private *call_private)
535 struct sdpmsg *msg = g_new0(struct sdpmsg, 1);
536 GSList *streams = call_private->streams;
538 for (; streams; streams = streams->next) {
539 struct sdpmedia *media = media_stream_to_sdpmedia(call_private,
540 streams->data);
541 if (media) {
542 msg->media = g_slist_append(msg->media, media);
544 if (msg->ip == NULL)
545 msg->ip = g_strdup(media->ip);
549 msg->media = g_slist_concat(msg->media, call_private->failed_media);
550 call_private->failed_media = NULL;
552 msg->ice_version = call_private->ice_version;
554 return msg;
557 static void
558 sipe_invite_call(struct sipe_media_call_private *call_private, TransCallback tc)
560 struct sipe_core_private *sipe_private = call_private->sipe_private;
561 gchar *hdr;
562 gchar *contact;
563 gchar *p_preferred_identity = NULL;
564 gchar *body;
565 struct sip_session *session;
566 struct sip_dialog *dialog;
567 struct sdpmsg *msg;
568 gboolean add_2007_fallback = FALSE;
570 session = sipe_session_find_call(sipe_private, SIPE_MEDIA_CALL->with);
571 dialog = session->dialogs->data;
572 add_2007_fallback = dialog->cseq == 0 &&
573 call_private->ice_version == SIPE_ICE_RFC_5245 &&
574 !sipe_strequal(SIPE_MEDIA_CALL->with, sipe_private->test_call_bot_uri);
576 contact = get_contact(sipe_private);
578 if (sipe_private->uc_line_uri) {
579 gchar *self = sip_uri_self(sipe_private);
580 p_preferred_identity = g_strdup_printf(
581 "P-Preferred-Identity: <%s>, <%s>\r\n",
582 self, sipe_private->uc_line_uri);
583 g_free(self);
586 hdr = g_strdup_printf(
587 "ms-keep-alive: UAC;hop-hop=yes\r\n"
588 "Contact: %s\r\n"
589 "%s"
590 "Content-Type: %s\r\n",
591 contact,
592 p_preferred_identity ? p_preferred_identity : "",
593 add_2007_fallback ?
594 "multipart/alternative;boundary=\"----=_NextPart_000_001E_01CB4397.0B5EB570\""
595 : "application/sdp");
596 g_free(contact);
597 g_free(p_preferred_identity);
599 msg = sipe_media_to_sdpmsg(call_private);
600 body = sdpmsg_to_string(msg);
602 if (add_2007_fallback) {
603 gchar *tmp;
604 tmp = g_strdup_printf(
605 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
606 "Content-Type: application/sdp\r\n"
607 "Content-Transfer-Encoding: 7bit\r\n"
608 "Content-Disposition: session; handling=optional; ms-proxy-2007fallback\r\n"
609 "\r\n"
610 "o=- 0 0 IN IP4 %s\r\n"
611 "s=session\r\n"
612 "c=IN IP4 %s\r\n"
613 "m=audio 0 RTP/AVP\r\n"
614 "\r\n"
615 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
616 "Content-Type: application/sdp\r\n"
617 "Content-Transfer-Encoding: 7bit\r\n"
618 "Content-Disposition: session; handling=optional\r\n"
619 "\r\n"
620 "%s"
621 "\r\n"
622 "------=_NextPart_000_001E_01CB4397.0B5EB570--\r\n",
623 msg->ip, msg->ip, body);
624 g_free(body);
625 body = tmp;
628 sdpmsg_free(msg);
630 dialog->outgoing_invite = sip_transport_invite(sipe_private,
631 hdr,
632 body,
633 dialog,
634 tc);
636 g_free(body);
637 g_free(hdr);
640 static void
641 send_response_with_session_description(struct sipe_media_call_private *call_private, int code, gchar *text)
643 struct sdpmsg *msg = sipe_media_to_sdpmsg(call_private);
644 gchar *body = sdpmsg_to_string(msg);
645 sdpmsg_free(msg);
646 sipmsg_add_header(call_private->invitation, "Content-Type", "application/sdp");
647 sip_transport_response(call_private->sipe_private, call_private->invitation, code, text, body);
648 g_free(body);
651 static gboolean
652 process_invite_call_response(struct sipe_core_private *sipe_private,
653 struct sipmsg *msg,
654 struct transaction *trans);
656 struct sipe_media_stream *
657 sipe_core_media_get_stream_by_id(struct sipe_media_call *call, const gchar *id)
659 GSList *i;
660 for (i = SIPE_MEDIA_CALL_PRIVATE->streams; i; i = i->next) {
661 struct sipe_media_stream *stream = i->data;
662 if (sipe_strequal(stream->id, id))
663 return stream;
665 return NULL;
668 static gboolean
669 update_call_from_remote_sdp(struct sipe_media_call_private* call_private,
670 struct sdpmedia *media)
672 struct sipe_media_stream *stream;
673 GList *backend_candidates = NULL;
674 GList *backend_codecs = NULL;
675 GSList *i;
676 gboolean result = TRUE;
678 stream = sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL, media->name);
679 if (media->port == 0) {
680 if (stream) {
681 sipe_backend_media_stream_end(SIPE_MEDIA_CALL, stream);
683 return TRUE;
686 if (!stream)
687 return FALSE;
689 if (sipe_utils_nameval_find(media->attributes, "inactive")) {
690 sipe_backend_stream_hold(SIPE_MEDIA_CALL, stream, FALSE);
691 } else if (sipe_backend_stream_is_held(stream)) {
692 sipe_backend_stream_unhold(SIPE_MEDIA_CALL, stream, FALSE);
695 if (SIPE_MEDIA_STREAM_PRIVATE->remote_candidates_and_codecs_set) {
696 return TRUE;
699 for (i = media->codecs; i; i = i->next) {
700 struct sdpcodec *c = i->data;
701 struct sipe_backend_codec *codec;
702 GSList *j;
704 codec = sipe_backend_codec_new(c->id,
705 c->name,
706 c->type,
707 c->clock_rate);
709 for (j = c->parameters; j; j = j->next) {
710 struct sipnameval *attr = j->data;
712 sipe_backend_codec_add_optional_parameter(codec,
713 attr->name,
714 attr->value);
717 backend_codecs = g_list_append(backend_codecs, codec);
720 if (media->encryption_key && SIPE_MEDIA_STREAM_PRIVATE->encryption_key) {
721 sipe_backend_media_set_encryption_keys(SIPE_MEDIA_CALL, stream,
722 SIPE_MEDIA_STREAM_PRIVATE->encryption_key,
723 media->encryption_key);
724 SIPE_MEDIA_STREAM_PRIVATE->encryption_key_id = media->encryption_key_id;
727 result = sipe_backend_set_remote_codecs(SIPE_MEDIA_CALL, stream,
728 backend_codecs);
729 sipe_media_codec_list_free(backend_codecs);
731 if (result == FALSE) {
732 sipe_backend_media_stream_end(SIPE_MEDIA_CALL, stream);
733 return FALSE;
736 for (i = media->candidates; i; i = i->next) {
737 struct sdpcandidate *c = i->data;
738 struct sipe_backend_candidate *candidate;
739 candidate = sipe_backend_candidate_new(c->foundation,
740 c->component,
741 c->type,
742 c->protocol,
743 c->ip,
744 c->port,
745 c->username,
746 c->password);
747 sipe_backend_candidate_set_priority(candidate, c->priority);
749 backend_candidates = g_list_append(backend_candidates, candidate);
752 sipe_backend_media_add_remote_candidates(SIPE_MEDIA_CALL, stream,
753 backend_candidates);
754 sipe_media_candidate_list_free(backend_candidates);
756 SIPE_MEDIA_STREAM_PRIVATE->remote_candidates_and_codecs_set = TRUE;
758 return TRUE;
761 static void
762 apply_remote_message(struct sipe_media_call_private* call_private,
763 struct sdpmsg* msg)
765 GSList *i;
767 sipe_utils_slist_free_full(call_private->failed_media, (GDestroyNotify)sdpmedia_free);
768 call_private->failed_media = NULL;
769 call_private->encryption_compatible = TRUE;
771 for (i = msg->media; i; i = i->next) {
772 struct sdpmedia *media = i->data;
773 const gchar *enc_level =
774 sipe_utils_nameval_find(media->attributes, "encryption");
775 if (sipe_strequal(enc_level, "rejected") &&
776 get_encryption_policy(call_private->sipe_private) == SIPE_ENCRYPTION_POLICY_REQUIRED) {
777 call_private->encryption_compatible = FALSE;
780 if (!update_call_from_remote_sdp(call_private, media)) {
781 media->port = 0;
782 call_private->failed_media =
783 g_slist_append(call_private->failed_media, media);
787 /* We need to keep failed medias until response is sent, remove them
788 * from sdpmsg that is to be freed. */
789 for (i = call_private->failed_media; i; i = i->next) {
790 msg->media = g_slist_remove(msg->media, i->data);
794 static gboolean
795 call_initialized(struct sipe_media_call *call)
797 GSList *streams = SIPE_MEDIA_CALL_PRIVATE->streams;
798 for (; streams; streams = streams->next) {
799 if (!sipe_backend_stream_initialized(call, streams->data)) {
800 return FALSE;
804 return TRUE;
807 // Sends an invite response when the call is accepted and local candidates were
808 // prepared, otherwise does nothing. If error response is sent, call_private is
809 // disposed before function returns. Returns true when response was sent.
810 static gboolean
811 send_invite_response_if_ready(struct sipe_media_call_private *call_private)
813 struct sipe_backend_media *backend_media;
815 backend_media = call_private->public.backend_private;
817 if (!sipe_backend_media_accepted(backend_media) ||
818 !call_initialized(&call_private->public))
819 return FALSE;
821 if (!call_private->encryption_compatible) {
822 struct sipe_core_private *sipe_private = call_private->sipe_private;
824 sipmsg_add_header(call_private->invitation, "Warning",
825 "308 lcs.microsoft.com \"Encryption Levels not compatible\"");
826 sip_transport_response(sipe_private,
827 call_private->invitation,
828 488, "Encryption Levels not compatible",
829 NULL);
830 sipe_backend_media_reject(backend_media, FALSE);
831 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
832 _("Unable to establish a call"),
833 _("Encryption settings of peer are incompatible with ours."));
834 } else {
835 send_response_with_session_description(call_private, 200, "OK");
838 return TRUE;
841 static void
842 stream_initialized_cb(struct sipe_media_call *call,
843 struct sipe_media_stream *stream)
845 if (call_initialized(call)) {
846 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
848 if (sipe_backend_media_is_initiator(call, stream)) {
849 sipe_invite_call(call_private,
850 process_invite_call_response);
851 } else if (call_private->smsg) {
852 struct sdpmsg *smsg = call_private->smsg;
853 call_private->smsg = NULL;
855 apply_remote_message(call_private, smsg);
856 send_invite_response_if_ready(call_private);
857 sdpmsg_free(smsg);
862 static void phone_state_publish(struct sipe_core_private *sipe_private)
864 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
865 sipe_ocs2007_phone_state_publish(sipe_private);
866 } else {
867 // TODO: OCS 2005 support. Is anyone still using it at all?
871 static void
872 stream_end_cb(struct sipe_media_call* call, struct sipe_media_stream* stream)
874 remove_stream(call, SIPE_MEDIA_STREAM_PRIVATE);
877 static void
878 media_end_cb(struct sipe_media_call *call)
880 struct sipe_core_private *sipe_private;
882 g_return_if_fail(call);
884 sipe_private = SIPE_MEDIA_CALL_PRIVATE->sipe_private;
886 sipe_media_call_free(SIPE_MEDIA_CALL_PRIVATE);
887 phone_state_publish(sipe_private);
890 static void
891 call_accept_cb(struct sipe_media_call *call, gboolean local)
893 if (local) {
894 send_invite_response_if_ready(SIPE_MEDIA_CALL_PRIVATE);
896 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE->sipe_private);
899 static void
900 call_reject_cb(struct sipe_media_call *call, gboolean local)
902 if (local) {
903 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
904 struct sip_session *session = NULL;
906 sip_transport_response(call_private->sipe_private,
907 call_private->invitation,
908 603, "Decline", NULL);
910 session = sipe_session_find_call(call_private->sipe_private,
911 call->with);
912 if (session) {
913 sipe_session_remove(call_private->sipe_private, session);
918 static gboolean
919 sipe_media_send_ack(struct sipe_core_private *sipe_private, struct sipmsg *msg,
920 struct transaction *trans);
922 static void call_hold_cb(struct sipe_media_call *call,
923 gboolean local,
924 SIPE_UNUSED_PARAMETER gboolean state)
926 if (local) {
927 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE, sipe_media_send_ack);
931 static void call_hangup_cb(struct sipe_media_call *call, gboolean local)
933 if (local) {
934 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
935 struct sip_session *session;
936 session = sipe_session_find_call(call_private->sipe_private,
937 call->with);
939 if (session) {
940 sipe_session_close(call_private->sipe_private, session);
945 static void
946 error_cb(struct sipe_media_call *call, gchar *message)
948 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
949 struct sipe_core_private *sipe_private = call_private->sipe_private;
950 gboolean initiator = sipe_backend_media_is_initiator(call, NULL);
951 gboolean accepted = sipe_backend_media_accepted(call->backend_private);
953 gchar *title = g_strdup_printf("Call with %s failed", call->with);
954 sipe_backend_notify_error(SIPE_CORE_PUBLIC, title, message);
955 g_free(title);
957 if (!initiator && !accepted) {
958 sip_transport_response(sipe_private,
959 call_private->invitation,
960 488, "Not Acceptable Here", NULL);
963 sipe_backend_media_hangup(call->backend_private, initiator || accepted);
966 static struct sipe_media_call_private *
967 sipe_media_call_new(struct sipe_core_private *sipe_private, const gchar* with,
968 struct sipmsg *msg, SipeIceVersion ice_version,
969 SipeMediaCallFlags flags)
971 struct sipe_media_call_private *call_private;
972 struct sip_session *session;
973 struct sip_dialog *dialog;
974 gchar *cname;
976 session = sipe_session_add_call(sipe_private, with);
978 dialog = sipe_dialog_add(session);
979 dialog->with = g_strdup(with);
981 if (msg) {
982 gchar *newTag = gentag();
983 const gchar *oldHeader;
984 gchar *newHeader;
986 oldHeader = sipmsg_find_header(msg, "To");
987 newHeader = g_strdup_printf("%s;tag=%s", oldHeader, newTag);
988 sipmsg_remove_header_now(msg, "To");
989 sipmsg_add_header_now(msg, "To", newHeader);
990 g_free(newTag);
991 g_free(newHeader);
993 dialog->callid = g_strdup(sipmsg_find_header(msg, "Call-ID"));
994 sipe_dialog_parse(dialog, msg, FALSE);
995 } else {
996 dialog->callid = gencallid();
997 dialog->ourtag = gentag();
998 flags |= SIPE_MEDIA_CALL_INITIATOR;
1001 if (g_hash_table_lookup(sipe_private->media_calls, dialog->callid)) {
1002 SIPE_DEBUG_ERROR("sipe_media_call_new: call already exists for "
1003 "Call-ID %s", dialog->callid);
1004 sipe_session_remove(sipe_private, session);
1005 return NULL;
1008 call_private = g_new0(struct sipe_media_call_private, 1);
1009 call_private->sipe_private = sipe_private;
1010 SIPE_MEDIA_CALL->with = g_strdup(with);
1012 g_hash_table_insert(sipe_private->media_calls,
1013 g_strdup(dialog->callid), call_private);
1015 cname = g_strdup(sipe_private->contact + 1);
1016 cname[strlen(cname) - 1] = '\0';
1018 call_private->public.backend_private = sipe_backend_media_new(SIPE_CORE_PUBLIC,
1019 SIPE_MEDIA_CALL,
1020 with,
1021 flags);
1022 sipe_backend_media_set_cname(call_private->public.backend_private, cname);
1024 call_private->ice_version = ice_version;
1025 call_private->encryption_compatible = TRUE;
1027 call_private->public.stream_initialized_cb = stream_initialized_cb;
1028 call_private->public.stream_end_cb = stream_end_cb;
1029 call_private->public.media_end_cb = media_end_cb;
1030 call_private->public.call_accept_cb = call_accept_cb;
1031 call_private->public.call_reject_cb = call_reject_cb;
1032 call_private->public.call_hold_cb = call_hold_cb;
1033 call_private->public.call_hangup_cb = call_hangup_cb;
1034 call_private->public.error_cb = error_cb;
1036 g_free(cname);
1038 return call_private;
1041 void sipe_media_hangup(struct sipe_media_call_private *call_private)
1043 if (call_private) {
1044 sipe_backend_media_hangup(call_private->public.backend_private,
1045 FALSE);
1049 struct sipe_media_stream *
1050 sipe_media_stream_add(struct sipe_media_call *call, const gchar *id,
1051 SipeMediaType type, SipeIceVersion ice_version,
1052 gboolean initiator)
1054 struct sipe_core_private *sipe_private;
1055 struct sipe_media_stream_private *stream_private;
1056 struct sipe_backend_media_stream *backend_stream;
1057 struct sipe_backend_media_relays *backend_media_relays;
1059 sipe_private = SIPE_MEDIA_CALL_PRIVATE->sipe_private;
1061 backend_media_relays = sipe_backend_media_relays_convert(
1062 sipe_private->media_relays,
1063 sipe_private->media_relay_username,
1064 sipe_private->media_relay_password);
1066 backend_stream = sipe_backend_media_add_stream(call, id, call->with,
1067 type, ice_version,
1068 initiator,
1069 backend_media_relays);
1071 sipe_backend_media_relays_free(backend_media_relays);
1073 if (!backend_stream) {
1074 return NULL;
1077 stream_private = g_new0(struct sipe_media_stream_private, 1);
1078 SIPE_MEDIA_STREAM->call = call;
1079 SIPE_MEDIA_STREAM->id = g_strdup(id);
1080 SIPE_MEDIA_STREAM->backend_private = backend_stream;
1082 #ifdef HAVE_SRTP
1084 int i;
1085 stream_private->encryption_key = g_new0(guchar, SIPE_SRTP_KEY_LEN);
1086 for (i = 0; i != SIPE_SRTP_KEY_LEN; ++i) {
1087 stream_private->encryption_key[i] = rand() & 0xff;
1089 stream_private->encryption_key_id = 1;
1091 #endif
1093 stream_private->async_reads = g_queue_new();
1095 SIPE_MEDIA_CALL_PRIVATE->streams =
1096 g_slist_append(SIPE_MEDIA_CALL_PRIVATE->streams,
1097 stream_private);
1099 return SIPE_MEDIA_STREAM;
1102 static void
1103 sipe_media_initiate_call(struct sipe_core_private *sipe_private,
1104 const char *with, SipeIceVersion ice_version,
1105 gboolean with_video)
1107 struct sipe_media_call_private *call_private;
1109 if (sipe_core_media_get_call(SIPE_CORE_PUBLIC)) {
1110 return;
1113 call_private = sipe_media_call_new(sipe_private, with, NULL,
1114 ice_version, 0);
1116 if (!sipe_media_stream_add(SIPE_MEDIA_CALL, "audio", SIPE_MEDIA_AUDIO,
1117 call_private->ice_version,
1118 TRUE)) {
1119 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
1120 _("Error occured"),
1121 _("Error creating audio stream"));
1122 sipe_media_hangup(call_private);
1123 return;
1126 if (with_video &&
1127 !sipe_media_stream_add(SIPE_MEDIA_CALL, "video", SIPE_MEDIA_VIDEO,
1128 call_private->ice_version,
1129 TRUE)) {
1130 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
1131 _("Error occured"),
1132 _("Error creating video stream"));
1133 sipe_media_hangup(call_private);
1134 return;
1137 // Processing continues in stream_initialized_cb
1140 void
1141 sipe_core_media_initiate_call(struct sipe_core_public *sipe_public,
1142 const char *with,
1143 gboolean with_video)
1145 sipe_media_initiate_call(SIPE_CORE_PRIVATE, with,
1146 SIPE_ICE_RFC_5245, with_video);
1149 void sipe_core_media_connect_conference(struct sipe_core_public *sipe_public,
1150 struct sipe_chat_session *chat_session)
1152 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1153 struct sipe_media_call_private *call_private;
1154 struct sip_session *session;
1155 SipeIceVersion ice_version;
1156 gchar *av_uri;
1158 if (!sipe_conf_supports_mcu_type(sipe_private, "audio-video")) {
1159 sipe_backend_notify_error(sipe_public, _("Join conference call"),
1160 _("Conference calls are not supported on this server."));
1161 return;
1164 session = sipe_session_find_chat(sipe_private, chat_session);
1166 if (sipe_core_media_get_call(sipe_public) || !session) {
1167 return;
1170 av_uri = sipe_conf_build_uri(chat_session->id, "audio-video");
1171 if (!av_uri) {
1172 return;
1175 session->is_call = TRUE;
1177 ice_version = SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013) ? SIPE_ICE_RFC_5245 :
1178 SIPE_ICE_DRAFT_6;
1180 call_private = sipe_media_call_new(sipe_private, av_uri, NULL,
1181 ice_version, 0);
1183 if (!sipe_media_stream_add(SIPE_MEDIA_CALL, "audio", SIPE_MEDIA_AUDIO,
1184 call_private->ice_version, TRUE)) {
1185 sipe_backend_notify_error(sipe_public,
1186 _("Error occurred"),
1187 _("Error creating audio stream"));
1189 sipe_media_hangup(call_private);
1192 g_free(av_uri);
1194 // Processing continues in stream_initialized_cb
1197 struct sipe_media_call *
1198 sipe_core_media_get_call(struct sipe_core_public *sipe_public)
1200 struct sipe_media_call * result = NULL;
1201 GList *calls = g_hash_table_get_values(SIPE_CORE_PRIVATE->media_calls);
1203 for (; calls; calls = g_list_delete_link(calls, calls)) {
1204 if (sipe_core_media_get_stream_by_id(calls->data, "audio")) {
1205 result = calls->data;
1206 break;
1210 return result;
1213 static gboolean phone_number_is_valid(const gchar *phone_number)
1215 if (!phone_number || sipe_strequal(phone_number, "")) {
1216 return FALSE;
1219 if (*phone_number == '+') {
1220 ++phone_number;
1223 while (*phone_number != '\0') {
1224 if (!g_ascii_isdigit(*phone_number)) {
1225 return FALSE;
1227 ++phone_number;
1230 return TRUE;
1233 void sipe_core_media_phone_call(struct sipe_core_public *sipe_public,
1234 const gchar *phone_number)
1236 g_return_if_fail(sipe_public);
1238 if (phone_number_is_valid(phone_number)) {
1239 gchar *phone_uri = g_strdup_printf("sip:%s@%s;user=phone",
1240 phone_number, sipe_public->sip_domain);
1242 sipe_core_media_initiate_call(sipe_public, phone_uri, FALSE);
1244 g_free(phone_uri);
1245 } else {
1246 sipe_backend_notify_error(sipe_public,
1247 _("Unable to establish a call"),
1248 _("Invalid phone number"));
1252 void sipe_core_media_test_call(struct sipe_core_public *sipe_public)
1254 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1255 if (!sipe_private->test_call_bot_uri) {
1256 sipe_backend_notify_error(sipe_public,
1257 _("Unable to establish a call"),
1258 _("Audio Test Service is not available."));
1259 return;
1262 sipe_core_media_initiate_call(sipe_public,
1263 sipe_private->test_call_bot_uri, FALSE);
1266 static struct sipe_media_call_private *
1267 sipe_media_from_sipmsg(struct sipe_core_private *sipe_private,
1268 struct sipmsg *msg)
1270 return g_hash_table_lookup(sipe_private->media_calls,
1271 sipmsg_find_header(msg, "Call-ID"));
1274 static void
1275 transport_response_unsupported_sdp(struct sipe_core_private *sipe_private,
1276 struct sipmsg *msg)
1278 sipmsg_add_header(msg, "ms-client-diagnostics",
1279 "52063;reason=\"Unsupported session description\"");
1280 sip_transport_response(sipe_private, msg,
1281 488, "Not Acceptable Here", NULL);
1284 struct sipe_media_call *
1285 process_incoming_invite_call(struct sipe_core_private *sipe_private,
1286 struct sipmsg *msg)
1288 struct sipe_media_call_private *call_private;
1289 struct sdpmsg *smsg;
1290 gboolean has_new_media = FALSE;
1291 GSList *i;
1293 // Don't allow two voice calls in parallel.
1294 if (!strstr(msg->body, "m=data") &&
1295 !strstr(msg->body, "m=applicationsharing")) {
1296 struct sipe_media_call *call =
1297 sipe_core_media_get_call(SIPE_CORE_PUBLIC);
1298 if (call && !is_media_session_msg(SIPE_MEDIA_CALL_PRIVATE, msg)) {
1299 sip_transport_response(sipe_private, msg,
1300 486, "Busy Here", NULL);
1301 return NULL;
1305 call_private = sipe_media_from_sipmsg(sipe_private, msg);
1307 if (call_private) {
1308 char *self = sip_uri_self(sipe_private);
1309 if (sipe_strequal(SIPE_MEDIA_CALL->with, self)) {
1310 g_free(self);
1311 sip_transport_response(sipe_private, msg, 488, "Not Acceptable Here", NULL);
1312 return NULL;
1314 g_free(self);
1317 smsg = sdpmsg_parse_msg(msg->body);
1318 if (!smsg) {
1319 transport_response_unsupported_sdp(sipe_private, msg);
1320 if (call_private) {
1321 sipe_media_hangup(call_private);
1323 return NULL;
1326 if (!call_private) {
1327 gchar *with = parse_from(sipmsg_find_header(msg, "From"));
1328 SipeMediaCallFlags flags = 0;
1330 if (strstr(msg->body, "m=data")) {
1331 flags |= SIPE_MEDIA_CALL_NO_UI;
1334 call_private = sipe_media_call_new(sipe_private, with, msg,
1335 smsg->ice_version, flags);
1337 g_free(with);
1340 if (call_private->invitation)
1341 sipmsg_free(call_private->invitation);
1342 call_private->invitation = sipmsg_copy(msg);
1344 // Create any new media streams
1345 for (i = smsg->media; i; i = i->next) {
1346 struct sdpmedia *media = i->data;
1347 gchar *id = media->name;
1348 SipeMediaType type;
1350 if ( media->port != 0
1351 && !sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL, id)) {
1352 if (sipe_strequal(id, "audio"))
1353 type = SIPE_MEDIA_AUDIO;
1354 else if (sipe_strequal(id, "video"))
1355 type = SIPE_MEDIA_VIDEO;
1356 else if (sipe_strequal(id, "data"))
1357 type = SIPE_MEDIA_APPLICATION;
1358 else
1359 continue;
1361 sipe_media_stream_add(SIPE_MEDIA_CALL, id, type,
1362 smsg->ice_version, FALSE);
1363 has_new_media = TRUE;
1367 if (has_new_media) {
1368 sdpmsg_free(call_private->smsg);
1369 call_private->smsg = smsg;
1370 sip_transport_response(sipe_private, call_private->invitation,
1371 180, "Ringing", NULL);
1372 // Processing continues in stream_initialized_cb
1373 } else {
1374 apply_remote_message(call_private, smsg);
1375 send_response_with_session_description(call_private, 200, "OK");
1377 sdpmsg_free(smsg);
1380 return SIPE_MEDIA_CALL;
1383 void process_incoming_cancel_call(struct sipe_media_call_private *call_private,
1384 struct sipmsg *msg)
1386 // We respond to the CANCEL request with 200 OK response and
1387 // with 487 Request Terminated to the remote INVITE in progress.
1388 sip_transport_response(call_private->sipe_private, msg, 200, "OK", NULL);
1390 if (call_private->invitation) {
1391 sip_transport_response(call_private->sipe_private,
1392 call_private->invitation,
1393 487, "Request Terminated", NULL);
1396 sipe_backend_media_reject(SIPE_MEDIA_CALL->backend_private, FALSE);
1399 static gboolean
1400 sipe_media_send_ack(struct sipe_core_private *sipe_private,
1401 struct sipmsg *msg,
1402 struct transaction *trans)
1404 struct sipe_media_call_private *call_private;
1405 struct sip_session *session;
1406 struct sip_dialog *dialog;
1407 int tmp_cseq;
1409 call_private = sipe_media_from_sipmsg(sipe_private, msg);
1411 if (!is_media_session_msg(call_private, msg))
1412 return FALSE;
1414 session = sipe_session_find_call(sipe_private, SIPE_MEDIA_CALL->with);
1415 dialog = session->dialogs->data;
1416 if (!dialog)
1417 return FALSE;
1419 tmp_cseq = dialog->cseq;
1421 dialog->cseq = sip_transaction_cseq(trans) - 1;
1422 sip_transport_ack(sipe_private, dialog);
1423 dialog->cseq = tmp_cseq;
1425 dialog->outgoing_invite = NULL;
1427 return TRUE;
1430 static gboolean
1431 sipe_media_send_final_ack(struct sipe_core_private *sipe_private,
1432 struct sipmsg *msg,
1433 struct transaction *trans)
1435 struct sipe_media_call_private *call_private;
1437 if (!sipe_media_send_ack(sipe_private, msg, trans))
1438 return FALSE;
1440 call_private = sipe_media_from_sipmsg(sipe_private, msg);
1442 sipe_backend_media_accept(SIPE_MEDIA_CALL->backend_private, FALSE);
1444 return TRUE;
1447 void
1448 sipe_core_media_stream_candidate_pair_established(struct sipe_media_stream *stream)
1450 struct sipe_media_call *call = stream->call;
1452 GList *active_candidates =
1453 sipe_backend_media_stream_get_active_local_candidates(stream);
1454 guint ready_components = g_list_length(active_candidates);
1456 sipe_media_candidate_list_free(active_candidates);
1458 if (ready_components != 2) {
1459 // We must have both RTP+RTCP candidate pairs established first.
1460 return;
1463 if (SIPE_MEDIA_STREAM_PRIVATE->established) {
1464 return;
1466 SIPE_MEDIA_STREAM_PRIVATE->established = TRUE;
1468 if (sipe_backend_media_is_initiator(stream->call, NULL)) {
1469 GSList *streams = SIPE_MEDIA_CALL_PRIVATE->streams;
1470 for (; streams; streams = streams->next) {
1471 struct sipe_media_stream_private *s = streams->data;
1472 if (!s->established) {
1473 break;
1477 if (streams == NULL) {
1478 // All call streams have been established.
1479 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE,
1480 sipe_media_send_final_ack);
1484 if (stream->candidate_pairs_established_cb) {
1485 stream->candidate_pairs_established_cb(stream);
1489 static gboolean
1490 maybe_retry_call_with_ice_version(struct sipe_media_call_private *call_private,
1491 SipeIceVersion ice_version,
1492 struct transaction *trans)
1494 if (call_private->ice_version != ice_version &&
1495 sip_transaction_cseq(trans) == 1) {
1496 gchar *with = g_strdup(SIPE_MEDIA_CALL->with);
1497 gboolean with_video = sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL, "video") != NULL;
1499 sipe_media_hangup(call_private);
1500 SIPE_DEBUG_INFO("Retrying call with ICEv%d.",
1501 ice_version == SIPE_ICE_DRAFT_6 ? 6 : 19);
1502 sipe_media_initiate_call(call_private->sipe_private, with,
1503 ice_version, with_video);
1505 g_free(with);
1506 return TRUE;
1509 return FALSE;
1512 static gboolean
1513 process_invite_call_response(struct sipe_core_private *sipe_private,
1514 struct sipmsg *msg,
1515 struct transaction *trans)
1517 const gchar *with;
1518 struct sipe_media_call_private *call_private;
1519 struct sip_session *session;
1520 struct sip_dialog *dialog;
1521 struct sdpmsg *smsg;
1523 call_private = sipe_media_from_sipmsg(sipe_private,msg);
1525 if (!is_media_session_msg(call_private, msg))
1526 return FALSE;
1528 session = sipe_session_find_call(sipe_private, SIPE_MEDIA_CALL->with);
1529 dialog = session->dialogs->data;
1531 with = dialog->with;
1533 dialog->outgoing_invite = NULL;
1535 if (msg->response >= 400) {
1536 // Call rejected by remote peer or an error occurred
1537 const gchar *title;
1538 GString *desc = g_string_new("");
1539 gboolean append_responsestr = FALSE;
1541 switch (msg->response) {
1542 case 480: {
1543 title = _("User unavailable");
1545 if (sipmsg_parse_warning(msg, NULL) == 391) {
1546 g_string_append_printf(desc, _("%s does not want to be disturbed"), with);
1547 } else
1548 g_string_append_printf(desc, _("User %s is not available"), with);
1549 break;
1551 case 603:
1552 case 605:
1553 title = _("Call rejected");
1554 g_string_append_printf(desc, _("User %s rejected call"), with);
1555 break;
1556 case 415:
1557 // OCS/Lync really sends response string with 'Mutipart' typo.
1558 if (sipe_strequal(msg->responsestr, "Mutipart mime in content type not supported by Archiving CDR service") &&
1559 maybe_retry_call_with_ice_version(call_private, SIPE_ICE_DRAFT_6, trans)) {
1560 return TRUE;
1562 title = _("Unsupported media type");
1563 break;
1564 case 488: {
1565 /* Check for incompatible encryption levels error.
1567 * MS Lync 2010:
1568 * 488 Not Acceptable Here
1569 * ms-client-diagnostics: 52017;reason="Encryption levels dont match"
1571 * older clients (and SIPE itself):
1572 * 488 Encryption Levels not compatible
1574 const gchar *ms_diag = sipmsg_find_header(msg, "ms-client-diagnostics");
1575 SipeIceVersion retry_ice_version = SIPE_ICE_DRAFT_6;
1577 if (sipe_strequal(msg->responsestr, "Encryption Levels not compatible") ||
1578 (ms_diag && g_str_has_prefix(ms_diag, "52017;"))) {
1579 title = _("Unable to establish a call");
1580 g_string_append(desc, _("Encryption settings of peer are incompatible with ours."));
1581 break;
1584 /* Check if this is failed conference using
1585 * ICEv6 with reason "Error parsing SDP" and
1586 * retry using ICEv19. */
1587 ms_diag = sipmsg_find_header(msg, "ms-diagnostics");
1588 if (ms_diag && g_str_has_prefix(ms_diag, "7008;")) {
1589 retry_ice_version = SIPE_ICE_RFC_5245;
1592 if (maybe_retry_call_with_ice_version(call_private, retry_ice_version, trans)) {
1593 return TRUE;
1595 // Break intentionally omitted
1597 default:
1598 title = _("Error occured");
1599 g_string_append(desc, _("Unable to establish a call"));
1600 append_responsestr = TRUE;
1601 break;
1604 if (append_responsestr) {
1605 gchar *reason = sipmsg_get_ms_diagnostics_reason(msg);
1607 g_string_append_printf(desc, "\n%d %s",
1608 msg->response, msg->responsestr);
1609 if (reason) {
1610 g_string_append_printf(desc, "\n\n%s", reason);
1611 g_free(reason);
1615 sipe_backend_notify_error(SIPE_CORE_PUBLIC, title, desc->str);
1616 g_string_free(desc, TRUE);
1618 sipe_media_send_ack(sipe_private, msg, trans);
1619 sipe_media_hangup(call_private);
1621 return TRUE;
1624 sipe_dialog_parse(dialog, msg, TRUE);
1625 smsg = sdpmsg_parse_msg(msg->body);
1626 if (!smsg) {
1627 transport_response_unsupported_sdp(sipe_private, msg);
1628 sipe_media_hangup(call_private);
1629 return FALSE;
1632 apply_remote_message(call_private, smsg);
1633 sdpmsg_free(smsg);
1635 sipe_media_send_ack(sipe_private, msg, trans);
1637 return TRUE;
1639 // Waits until sipe_core_media_candidate_pair_established() is invoked.
1642 gboolean is_media_session_msg(struct sipe_media_call_private *call_private,
1643 struct sipmsg *msg)
1645 if (!call_private) {
1646 return FALSE;
1649 return sipe_media_from_sipmsg(call_private->sipe_private, msg) == call_private;
1652 static void
1653 end_call(SIPE_UNUSED_PARAMETER gpointer key,
1654 struct sipe_media_call_private *call_private,
1655 SIPE_UNUSED_PARAMETER gpointer user_data)
1657 struct sipe_backend_media *backend_private;
1659 backend_private = call_private->public.backend_private;
1661 if (!sipe_backend_media_is_initiator(SIPE_MEDIA_CALL, NULL) &&
1662 !sipe_backend_media_accepted(backend_private)) {
1663 sip_transport_response(call_private->sipe_private,
1664 call_private->invitation,
1665 480, "Temporarily Unavailable", NULL);
1666 } else {
1667 struct sip_session *session;
1669 session = sipe_session_find_call(call_private->sipe_private,
1670 SIPE_MEDIA_CALL->with);
1671 if (session)
1672 sipe_session_close(call_private->sipe_private, session);
1675 sipe_media_hangup(call_private);
1678 void
1679 sipe_media_handle_going_offline(struct sipe_core_private *sipe_private)
1681 g_hash_table_foreach(sipe_private->media_calls, (GHFunc) end_call, NULL);
1684 gboolean sipe_media_is_conference_call(struct sipe_media_call_private *call_private)
1686 return g_strstr_len(SIPE_MEDIA_CALL->with, -1, "app:conf:audio-video:") != NULL;
1689 struct sipe_core_private *
1690 sipe_media_get_sipe_core_private(struct sipe_media_call *call)
1692 g_return_val_if_fail(call, NULL);
1694 return SIPE_MEDIA_CALL_PRIVATE->sipe_private;
1697 struct sip_dialog *
1698 sipe_media_get_sip_dialog(struct sipe_media_call *call)
1700 struct sip_session *session;
1702 g_return_val_if_fail(call, NULL);
1704 session = sipe_session_find_call(SIPE_MEDIA_CALL_PRIVATE->sipe_private,
1705 call->with);
1707 if (!session || !session->dialogs) {
1708 return NULL;
1711 return session->dialogs->data;
1714 static void
1715 sipe_media_relay_free(struct sipe_media_relay *relay)
1717 g_free(relay->hostname);
1718 if (relay->dns_query)
1719 sipe_backend_dns_query_cancel(relay->dns_query);
1720 g_free(relay);
1723 void
1724 sipe_media_relay_list_free(GSList *list)
1726 for (; list; list = g_slist_delete_link(list, list))
1727 sipe_media_relay_free(list->data);
1730 static void
1731 relay_ip_resolved_cb(struct sipe_media_relay* relay,
1732 const gchar *ip, SIPE_UNUSED_PARAMETER guint port)
1734 gchar *hostname = relay->hostname;
1735 relay->dns_query = NULL;
1737 if (ip && port) {
1738 relay->hostname = g_strdup(ip);
1739 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname, ip);
1740 } else {
1741 relay->hostname = NULL;
1742 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname);
1745 g_free(hostname);
1748 static gboolean
1749 process_get_av_edge_credentials_response(struct sipe_core_private *sipe_private,
1750 struct sipmsg *msg,
1751 SIPE_UNUSED_PARAMETER struct transaction *trans)
1753 g_free(sipe_private->media_relay_username);
1754 g_free(sipe_private->media_relay_password);
1755 sipe_media_relay_list_free(sipe_private->media_relays);
1756 sipe_private->media_relay_username = NULL;
1757 sipe_private->media_relay_password = NULL;
1758 sipe_private->media_relays = NULL;
1760 if (msg->response >= 400) {
1761 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
1762 "Failed to obtain A/V Edge credentials.");
1763 return FALSE;
1766 if (msg->response == 200) {
1767 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
1769 if (sipe_strequal("OK", sipe_xml_attribute(xn_response, "reasonPhrase"))) {
1770 const sipe_xml *xn_credentials = sipe_xml_child(xn_response, "credentialsResponse/credentials");
1771 const sipe_xml *xn_relays = sipe_xml_child(xn_response, "credentialsResponse/mediaRelayList");
1772 const sipe_xml *item;
1773 GSList *relays = NULL;
1775 item = sipe_xml_child(xn_credentials, "username");
1776 sipe_private->media_relay_username = sipe_xml_data(item);
1777 item = sipe_xml_child(xn_credentials, "password");
1778 sipe_private->media_relay_password = sipe_xml_data(item);
1780 for (item = sipe_xml_child(xn_relays, "mediaRelay"); item; item = sipe_xml_twin(item)) {
1781 struct sipe_media_relay *relay = g_new0(struct sipe_media_relay, 1);
1782 const sipe_xml *node;
1783 gchar *tmp;
1785 node = sipe_xml_child(item, "hostName");
1786 relay->hostname = sipe_xml_data(node);
1788 node = sipe_xml_child(item, "udpPort");
1789 if (node) {
1790 tmp = sipe_xml_data(node);
1791 if (tmp) {
1792 relay->udp_port = atoi(tmp);
1793 g_free(tmp);
1797 node = sipe_xml_child(item, "tcpPort");
1798 if (node) {
1799 tmp = sipe_xml_data(node);
1800 if (tmp) {
1801 relay->tcp_port = atoi(tmp);
1802 g_free(tmp);
1806 relays = g_slist_append(relays, relay);
1808 relay->dns_query = sipe_backend_dns_query_a(
1809 SIPE_CORE_PUBLIC,
1810 relay->hostname,
1811 relay->udp_port,
1812 (sipe_dns_resolved_cb) relay_ip_resolved_cb,
1813 relay);
1815 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
1816 relay->hostname,
1817 relay->tcp_port, relay->udp_port);
1820 sipe_private->media_relays = relays;
1823 sipe_xml_free(xn_response);
1826 return TRUE;
1829 void
1830 sipe_media_get_av_edge_credentials(struct sipe_core_private *sipe_private)
1832 // TODO: re-request credentials after duration expires?
1833 static const char CRED_REQUEST_XML[] =
1834 "<request requestID=\"%d\" "
1835 "from=\"%s\" "
1836 "version=\"1.0\" "
1837 "to=\"%s\" "
1838 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
1839 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
1840 "<credentialsRequest credentialsRequestID=\"%d\">"
1841 "<identity>%s</identity>"
1842 "<location>%s</location>"
1843 "<duration>480</duration>"
1844 "</credentialsRequest>"
1845 "</request>";
1847 int request_id = rand();
1848 gchar *self;
1849 gchar *body;
1851 if (!sipe_private->mras_uri)
1852 return;
1854 self = sip_uri_self(sipe_private);
1856 body = g_strdup_printf(
1857 CRED_REQUEST_XML,
1858 request_id,
1859 self,
1860 sipe_private->mras_uri,
1861 request_id,
1862 self,
1863 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER) ? "internet" : "intranet");
1864 g_free(self);
1866 sip_transport_service(sipe_private,
1867 sipe_private->mras_uri,
1868 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
1869 body,
1870 process_get_av_edge_credentials_response);
1872 g_free(body);
1875 void
1876 sipe_media_stream_add_extra_attribute(struct sipe_media_stream *stream,
1877 const gchar *name, const gchar *value)
1879 SIPE_MEDIA_STREAM_PRIVATE->extra_sdp =
1880 sipe_utils_nameval_add(SIPE_MEDIA_STREAM_PRIVATE->extra_sdp,
1881 name, value);
1884 #ifdef HAVE_XDATA
1885 void
1886 sipe_core_media_stream_readable(struct sipe_media_stream *stream)
1888 g_return_if_fail(stream);
1890 if (g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE->async_reads) &&
1891 stream->read_cb) {
1892 stream->read_cb(stream);
1895 while (!g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE->async_reads)) {
1896 struct async_read_data *data;
1897 guint8 *pos;
1898 gssize len;
1899 gssize bytes_read;
1901 data = g_queue_peek_head(SIPE_MEDIA_STREAM_PRIVATE->async_reads);
1902 pos = data->buffer + SIPE_MEDIA_STREAM_PRIVATE->read_pos;
1903 len = data->len - SIPE_MEDIA_STREAM_PRIVATE->read_pos;
1905 bytes_read = sipe_backend_media_stream_read(stream, pos, len);
1906 if (bytes_read == -1) {
1907 struct sipe_media_call *call = stream->call;
1908 struct sipe_core_private *sipe_private =
1909 SIPE_MEDIA_CALL_PRIVATE->sipe_private;
1911 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
1912 _("Media error"),
1913 _("Error while reading from stream"));
1914 sipe_media_hangup(SIPE_MEDIA_CALL_PRIVATE);
1915 return;
1918 SIPE_MEDIA_STREAM_PRIVATE->read_pos += bytes_read;
1920 if (SIPE_MEDIA_STREAM_PRIVATE->read_pos == data->len) {
1921 data->callback(stream, data->buffer, data->len);
1922 SIPE_MEDIA_STREAM_PRIVATE->read_pos = 0;
1923 g_queue_pop_head(SIPE_MEDIA_STREAM_PRIVATE->async_reads);
1924 g_free(data);
1925 } else {
1926 // Still not enough data to finish the read.
1927 return;
1932 void
1933 sipe_media_stream_read_async(struct sipe_media_stream *stream,
1934 gpointer buffer, gsize len,
1935 sipe_media_stream_read_callback callback)
1937 struct async_read_data *data;
1939 g_return_if_fail(stream && buffer && callback);
1941 data = g_new0(struct async_read_data, 1);
1942 data->buffer = buffer;
1943 data->len = len;
1944 data->callback = callback;
1946 g_queue_push_tail(SIPE_MEDIA_STREAM_PRIVATE->async_reads, data);
1948 #endif
1950 void
1951 sipe_media_stream_set_data(struct sipe_media_stream *stream, gpointer data,
1952 GDestroyNotify free_func)
1954 struct sipe_media_stream_private *stream_private =
1955 SIPE_MEDIA_STREAM_PRIVATE;
1957 g_return_if_fail(stream_private);
1959 if (stream_private->data && stream_private->data_free_func) {
1960 stream_private->data_free_func(stream_private->data);
1963 stream_private->data = data;
1964 stream_private->data_free_func = free_func;
1967 gpointer
1968 sipe_media_stream_get_data(struct sipe_media_stream *stream)
1970 g_return_val_if_fail(stream, NULL);
1972 return SIPE_MEDIA_STREAM_PRIVATE->data;
1976 Local Variables:
1977 mode: c
1978 c-file-style: "bsd"
1979 indent-tabs-mode: t
1980 tab-width: 8
1981 End: