media: reply with 488 Not Acceptable Here when INVITE from self is received
[siplcs.git] / src / core / sipe-media.c
blob6bfd1cbb7e201dccd0818afb2a02e70c22fcfd9d
1 /**
2 * @file sipe-media.c
4 * pidgin-sipe
6 * Copyright (C) 2011-2014 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-core.h"
41 #include "sipe-core-private.h"
42 #include "sipe-dialog.h"
43 #include "sipe-media.h"
44 #include "sipe-ocs2007.h"
45 #include "sipe-session.h"
46 #include "sipe-utils.h"
47 #include "sipe-nls.h"
48 #include "sipe-schedule.h"
49 #include "sipe-xml.h"
51 struct sipe_media_call_private {
52 struct sipe_media_call public;
54 /* private part starts here */
55 struct sipe_core_private *sipe_private;
56 gchar *with;
58 struct sipmsg *invitation;
59 SipeIceVersion ice_version;
60 gboolean encryption_compatible;
62 struct sdpmsg *smsg;
63 GSList *failed_media;
65 #define SIPE_MEDIA_CALL ((struct sipe_media_call *) call_private)
66 #define SIPE_MEDIA_CALL_PRIVATE ((struct sipe_media_call_private *) call)
68 static void sipe_media_codec_list_free(GList *codecs)
70 for (; codecs; codecs = g_list_delete_link(codecs, codecs))
71 sipe_backend_codec_free(codecs->data);
74 static void sipe_media_candidate_list_free(GList *candidates)
76 for (; candidates; candidates = g_list_delete_link(candidates, candidates))
77 sipe_backend_candidate_free(candidates->data);
80 static void
81 sipe_media_call_free(struct sipe_media_call_private *call_private)
83 if (call_private) {
84 struct sip_session *session;
85 sipe_backend_media_free(call_private->public.backend_private);
87 session = sipe_session_find_call(call_private->sipe_private,
88 call_private->with);
89 if (session)
90 sipe_session_remove(call_private->sipe_private, session);
92 if (call_private->invitation)
93 sipmsg_free(call_private->invitation);
95 sdpmsg_free(call_private->smsg);
96 sipe_utils_slist_free_full(call_private->failed_media,
97 (GDestroyNotify)sdpmedia_free);
98 g_free(call_private->with);
99 g_free(call_private);
103 static GSList *
104 backend_candidates_to_sdpcandidate(GList *candidates)
106 GSList *result = NULL;
107 GList *i;
109 for (i = candidates; i; i = i->next) {
110 struct sipe_backend_candidate *candidate = i->data;
111 struct sdpcandidate *c = g_new(struct sdpcandidate, 1);
113 c->foundation = sipe_backend_candidate_get_foundation(candidate);
114 c->component = sipe_backend_candidate_get_component_type(candidate);
115 c->type = sipe_backend_candidate_get_type(candidate);
116 c->protocol = sipe_backend_candidate_get_protocol(candidate);
117 c->ip = sipe_backend_candidate_get_ip(candidate);
118 c->port = sipe_backend_candidate_get_port(candidate);
119 c->base_ip = sipe_backend_candidate_get_base_ip(candidate);
120 c->base_port = sipe_backend_candidate_get_base_port(candidate);
121 c->priority = sipe_backend_candidate_get_priority(candidate);
122 c->username = sipe_backend_candidate_get_username(candidate);
123 c->password = sipe_backend_candidate_get_password(candidate);
125 result = g_slist_append(result, c);
128 return result;
131 static void
132 get_stream_ip_and_ports(GSList *candidates,
133 gchar **ip, guint *rtp_port, guint *rtcp_port,
134 SipeCandidateType type)
136 *ip = 0;
137 *rtp_port = 0;
138 *rtcp_port = 0;
140 for (; candidates; candidates = candidates->next) {
141 struct sdpcandidate *candidate = candidates->data;
143 if (type == SIPE_CANDIDATE_TYPE_ANY || candidate->type == type) {
144 if (!*ip) {
145 *ip = g_strdup(candidate->ip);
146 } else if (!sipe_strequal(*ip, candidate->ip)) {
147 continue;
150 if (candidate->component == SIPE_COMPONENT_RTP) {
151 *rtp_port = candidate->port;
152 } else if (candidate->component == SIPE_COMPONENT_RTCP)
153 *rtcp_port = candidate->port;
156 if (*rtp_port != 0 && *rtcp_port != 0)
157 return;
161 static struct sdpmedia *
162 backend_stream_to_sdpmedia(struct sipe_backend_media *backend_media,
163 struct sipe_backend_stream *backend_stream)
165 struct sdpmedia *media = g_new0(struct sdpmedia, 1);
166 GList *codecs = sipe_backend_get_local_codecs(backend_media,
167 backend_stream);
168 guint rtcp_port = 0;
169 SipeMediaType type;
170 GSList *attributes = NULL;
171 GList *candidates;
172 GList *i;
174 media->name = g_strdup(sipe_backend_stream_get_id(backend_stream));
176 if (sipe_strequal(media->name, "audio"))
177 type = SIPE_MEDIA_AUDIO;
178 else if (sipe_strequal(media->name, "video"))
179 type = SIPE_MEDIA_VIDEO;
180 else {
181 // TODO: incompatible media, should not happen here
182 g_free(media->name);
183 g_free(media);
184 sipe_media_codec_list_free(codecs);
185 return(NULL);
188 // Process codecs
189 for (i = codecs; i; i = i->next) {
190 struct sipe_backend_codec *codec = i->data;
191 struct sdpcodec *c = g_new0(struct sdpcodec, 1);
192 GList *params;
194 c->id = sipe_backend_codec_get_id(codec);
195 c->name = sipe_backend_codec_get_name(codec);
196 c->clock_rate = sipe_backend_codec_get_clock_rate(codec);
197 c->type = type;
199 params = sipe_backend_codec_get_optional_parameters(codec);
200 for (; params; params = params->next) {
201 struct sipnameval *param = params->data;
202 struct sipnameval *copy = g_new0(struct sipnameval, 1);
204 copy->name = g_strdup(param->name);
205 copy->value = g_strdup(param->value);
207 c->parameters = g_slist_append(c->parameters, copy);
210 media->codecs = g_slist_append(media->codecs, c);
213 sipe_media_codec_list_free(codecs);
215 // Process local candidates
216 // If we have established candidate pairs, send them in SDP response.
217 // Otherwise send all available local candidates.
218 candidates = sipe_backend_media_get_active_local_candidates(backend_media,
219 backend_stream);
220 if (!candidates)
221 candidates = sipe_backend_get_local_candidates(backend_media,
222 backend_stream);
224 media->candidates = backend_candidates_to_sdpcandidate(candidates);
226 sipe_media_candidate_list_free(candidates);
228 get_stream_ip_and_ports(media->candidates, &media->ip, &media->port,
229 &rtcp_port, SIPE_CANDIDATE_TYPE_HOST);
230 // No usable HOST candidates, use any candidate
231 if (media->ip == NULL && media->candidates) {
232 get_stream_ip_and_ports(media->candidates, &media->ip, &media->port,
233 &rtcp_port, SIPE_CANDIDATE_TYPE_ANY);
236 if (sipe_backend_stream_is_held(backend_stream))
237 attributes = sipe_utils_nameval_add(attributes, "inactive", "");
239 if (rtcp_port) {
240 gchar *tmp = g_strdup_printf("%u", rtcp_port);
241 attributes = sipe_utils_nameval_add(attributes, "rtcp", tmp);
242 g_free(tmp);
245 attributes = sipe_utils_nameval_add(attributes, "encryption", "rejected");
247 media->attributes = attributes;
249 // Process remote candidates
250 candidates = sipe_backend_media_get_active_remote_candidates(backend_media,
251 backend_stream);
252 media->remote_candidates = backend_candidates_to_sdpcandidate(candidates);
253 sipe_media_candidate_list_free(candidates);
255 return media;
258 static struct sdpmsg *
259 sipe_media_to_sdpmsg(struct sipe_media_call_private *call_private)
261 struct sipe_backend_media *backend_media = call_private->public.backend_private;
262 struct sdpmsg *msg = g_new0(struct sdpmsg, 1);
263 GSList *streams = sipe_backend_media_get_streams(backend_media);
265 for (; streams; streams = streams->next) {
266 struct sdpmedia *media = backend_stream_to_sdpmedia(backend_media, streams->data);
267 if (media) {
268 msg->media = g_slist_append(msg->media, media);
270 if (msg->ip == NULL)
271 msg->ip = g_strdup(media->ip);
275 msg->media = g_slist_concat(msg->media, call_private->failed_media);
276 call_private->failed_media = NULL;
278 msg->ice_version = call_private->ice_version;
280 return msg;
283 static void
284 sipe_invite_call(struct sipe_core_private *sipe_private, TransCallback tc)
286 gchar *hdr;
287 gchar *contact;
288 gchar *p_preferred_identity = NULL;
289 gchar *body;
290 struct sipe_media_call_private *call_private = sipe_private->media_call;
291 struct sip_session *session;
292 struct sip_dialog *dialog;
293 struct sdpmsg *msg;
294 gboolean add_2007_fallback = FALSE;
296 session = sipe_session_find_call(sipe_private, call_private->with);
297 dialog = session->dialogs->data;
298 add_2007_fallback = dialog->cseq == 0 &&
299 call_private->ice_version == SIPE_ICE_RFC_5245 &&
300 !sipe_strequal(call_private->with, sipe_private->test_call_bot_uri);
302 contact = get_contact(sipe_private);
304 if (sipe_private->uc_line_uri) {
305 gchar *self = sip_uri_self(sipe_private);
306 p_preferred_identity = g_strdup_printf(
307 "P-Preferred-Identity: <%s>, <%s>\r\n",
308 self, sipe_private->uc_line_uri);
309 g_free(self);
312 hdr = g_strdup_printf(
313 "ms-keep-alive: UAC;hop-hop=yes\r\n"
314 "Contact: %s\r\n"
315 "%s"
316 "Content-Type: %s\r\n",
317 contact,
318 p_preferred_identity ? p_preferred_identity : "",
319 add_2007_fallback ?
320 "multipart/alternative;boundary=\"----=_NextPart_000_001E_01CB4397.0B5EB570\""
321 : "application/sdp");
322 g_free(contact);
323 g_free(p_preferred_identity);
325 msg = sipe_media_to_sdpmsg(call_private);
326 body = sdpmsg_to_string(msg);
328 if (add_2007_fallback) {
329 gchar *tmp;
330 tmp = g_strdup_printf(
331 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
332 "Content-Type: application/sdp\r\n"
333 "Content-Transfer-Encoding: 7bit\r\n"
334 "Content-Disposition: session; handling=optional; ms-proxy-2007fallback\r\n"
335 "\r\n"
336 "o=- 0 0 IN IP4 %s\r\n"
337 "s=session\r\n"
338 "c=IN IP4 %s\r\n"
339 "m=audio 0 RTP/AVP\r\n"
340 "\r\n"
341 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
342 "Content-Type: application/sdp\r\n"
343 "Content-Transfer-Encoding: 7bit\r\n"
344 "Content-Disposition: session; handling=optional\r\n"
345 "\r\n"
346 "%s"
347 "\r\n"
348 "------=_NextPart_000_001E_01CB4397.0B5EB570--\r\n",
349 msg->ip, msg->ip, body);
350 g_free(body);
351 body = tmp;
354 sdpmsg_free(msg);
356 dialog->outgoing_invite = sip_transport_invite(sipe_private,
357 hdr,
358 body,
359 dialog,
360 tc);
362 g_free(body);
363 g_free(hdr);
366 static struct sip_dialog *
367 sipe_media_dialog_init(struct sip_session* session, struct sipmsg *msg)
369 gchar *newTag = gentag();
370 const gchar *oldHeader;
371 gchar *newHeader;
372 struct sip_dialog *dialog;
374 oldHeader = sipmsg_find_header(msg, "To");
375 newHeader = g_strdup_printf("%s;tag=%s", oldHeader, newTag);
376 sipmsg_remove_header_now(msg, "To");
377 sipmsg_add_header_now(msg, "To", newHeader);
378 g_free(newHeader);
380 dialog = sipe_dialog_add(session);
381 dialog->callid = g_strdup(sipmsg_find_header(msg, "Call-ID"));
382 dialog->with = parse_from(sipmsg_find_header(msg, "From"));
383 sipe_dialog_parse(dialog, msg, FALSE);
385 return dialog;
388 static void
389 send_response_with_session_description(struct sipe_media_call_private *call_private, int code, gchar *text)
391 struct sdpmsg *msg = sipe_media_to_sdpmsg(call_private);
392 gchar *body = sdpmsg_to_string(msg);
393 sdpmsg_free(msg);
394 sipmsg_add_header(call_private->invitation, "Content-Type", "application/sdp");
395 sip_transport_response(call_private->sipe_private, call_private->invitation, code, text, body);
396 g_free(body);
399 static gboolean
400 encryption_levels_compatible(struct sdpmsg *msg)
402 GSList *i;
404 for (i = msg->media; i; i = i->next) {
405 const gchar *enc_level;
406 struct sdpmedia *m = i->data;
408 enc_level = sipe_utils_nameval_find(m->attributes, "encryption");
410 // Decline call if peer requires encryption as we don't support it yet.
411 if (sipe_strequal(enc_level, "required"))
412 return FALSE;
415 return TRUE;
418 static gboolean
419 process_invite_call_response(struct sipe_core_private *sipe_private,
420 struct sipmsg *msg,
421 struct transaction *trans);
423 static gboolean
424 update_remote_media(struct sipe_media_call_private* call_private,
425 struct sdpmedia *media)
427 struct sipe_backend_media *backend_media = SIPE_MEDIA_CALL->backend_private;
428 struct sipe_backend_stream *backend_stream;
429 GList *backend_candidates = NULL;
430 GList *backend_codecs = NULL;
431 GSList *i;
432 gboolean result = TRUE;
434 backend_stream = sipe_backend_media_get_stream_by_id(backend_media,
435 media->name);
436 if (media->port == 0) {
437 if (backend_stream)
438 sipe_backend_media_remove_stream(backend_media, backend_stream);
439 return TRUE;
442 if (!backend_stream)
443 return FALSE;
445 for (i = media->codecs; i; i = i->next) {
446 struct sdpcodec *c = i->data;
447 struct sipe_backend_codec *codec;
448 GSList *j;
450 codec = sipe_backend_codec_new(c->id,
451 c->name,
452 c->type,
453 c->clock_rate);
455 for (j = c->parameters; j; j = j->next) {
456 struct sipnameval *attr = j->data;
458 sipe_backend_codec_add_optional_parameter(codec,
459 attr->name,
460 attr->value);
463 backend_codecs = g_list_append(backend_codecs, codec);
466 result = sipe_backend_set_remote_codecs(backend_media,
467 backend_stream,
468 backend_codecs);
469 sipe_media_codec_list_free(backend_codecs);
471 if (result == FALSE) {
472 sipe_backend_media_remove_stream(backend_media, backend_stream);
473 return FALSE;
476 for (i = media->candidates; i; i = i->next) {
477 struct sdpcandidate *c = i->data;
478 struct sipe_backend_candidate *candidate;
479 candidate = sipe_backend_candidate_new(c->foundation,
480 c->component,
481 c->type,
482 c->protocol,
483 c->ip,
484 c->port,
485 c->username,
486 c->password);
487 sipe_backend_candidate_set_priority(candidate, c->priority);
489 backend_candidates = g_list_append(backend_candidates, candidate);
492 sipe_backend_media_add_remote_candidates(backend_media,
493 backend_stream,
494 backend_candidates);
495 sipe_media_candidate_list_free(backend_candidates);
497 if (sipe_utils_nameval_find(media->attributes, "inactive")) {
498 sipe_backend_stream_hold(backend_media, backend_stream, FALSE);
499 } else if (sipe_backend_stream_is_held(backend_stream)) {
500 sipe_backend_stream_unhold(backend_media, backend_stream, FALSE);
503 return TRUE;
506 static void
507 apply_remote_message(struct sipe_media_call_private* call_private,
508 struct sdpmsg* msg)
510 GSList *i;
512 sipe_utils_slist_free_full(call_private->failed_media, (GDestroyNotify)sdpmedia_free);
513 call_private->failed_media = NULL;
515 for (i = msg->media; i; i = i->next) {
516 struct sdpmedia *media = i->data;
517 if (!update_remote_media(call_private, media)) {
518 media->port = 0;
519 call_private->failed_media =
520 g_slist_append(call_private->failed_media, media);
524 /* We need to keep failed medias until response is sent, remove them
525 * from sdpmsg that is to be freed. */
526 for (i = call_private->failed_media; i; i = i->next) {
527 msg->media = g_slist_remove(msg->media, i->data);
530 call_private->encryption_compatible = encryption_levels_compatible(msg);
533 static gboolean
534 call_initialized(struct sipe_media_call *call)
536 GSList *streams =
537 sipe_backend_media_get_streams(call->backend_private);
539 for (; streams; streams = streams->next) {
540 if (!sipe_backend_stream_initialized(call->backend_private,
541 streams->data)) {
542 return FALSE;
546 return TRUE;
549 // Sends an invite response when the call is accepted and local candidates were
550 // prepared, otherwise does nothing. If error response is sent, call_private is
551 // disposed before function returns. Returns true when response was sent.
552 static gboolean
553 send_invite_response_if_ready(struct sipe_media_call_private *call_private)
555 struct sipe_backend_media *backend_media;
557 backend_media = call_private->public.backend_private;
559 if (!sipe_backend_media_accepted(backend_media) ||
560 !call_initialized(&call_private->public))
561 return FALSE;
563 if (!call_private->encryption_compatible) {
564 struct sipe_core_private *sipe_private = call_private->sipe_private;
566 sipmsg_add_header(call_private->invitation, "Warning",
567 "308 lcs.microsoft.com \"Encryption Levels not compatible\"");
568 sip_transport_response(sipe_private,
569 call_private->invitation,
570 488, "Encryption Levels not compatible",
571 NULL);
572 sipe_backend_media_reject(backend_media, FALSE);
573 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
574 _("Unable to establish a call"),
575 _("Encryption settings of peer are incompatible with ours."));
576 } else {
577 send_response_with_session_description(call_private, 200, "OK");
580 return TRUE;
583 static void
584 stream_initialized_cb(struct sipe_media_call *call,
585 struct sipe_backend_stream *stream)
587 if (call_initialized(call)) {
588 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
589 struct sipe_backend_media *backend_private = call->backend_private;
591 if (sipe_backend_media_is_initiator(backend_private, stream)) {
592 sipe_invite_call(call_private->sipe_private,
593 process_invite_call_response);
594 } else if (call_private->smsg) {
595 struct sdpmsg *smsg = call_private->smsg;
596 call_private->smsg = NULL;
598 apply_remote_message(call_private, smsg);
599 send_invite_response_if_ready(call_private);
600 sdpmsg_free(smsg);
605 static void phone_state_publish(struct sipe_core_private *sipe_private)
607 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
608 sipe_ocs2007_phone_state_publish(sipe_private);
609 } else {
610 // TODO: OCS 2005 support. Is anyone still using it at all?
614 static void
615 media_end_cb(struct sipe_media_call *call)
617 g_return_if_fail(call);
619 SIPE_MEDIA_CALL_PRIVATE->sipe_private->media_call = NULL;
620 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE->sipe_private);
621 sipe_media_call_free(SIPE_MEDIA_CALL_PRIVATE);
624 static void
625 call_accept_cb(struct sipe_media_call *call, gboolean local)
627 if (local) {
628 send_invite_response_if_ready(SIPE_MEDIA_CALL_PRIVATE);
630 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE->sipe_private);
633 static void
634 call_reject_cb(struct sipe_media_call *call, gboolean local)
636 if (local) {
637 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
638 sip_transport_response(call_private->sipe_private,
639 call_private->invitation,
640 603, "Decline", NULL);
644 static gboolean
645 sipe_media_send_ack(struct sipe_core_private *sipe_private, struct sipmsg *msg,
646 struct transaction *trans);
648 static void call_hold_cb(struct sipe_media_call *call,
649 gboolean local,
650 SIPE_UNUSED_PARAMETER gboolean state)
652 if (local)
653 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE->sipe_private,
654 sipe_media_send_ack);
657 static void call_hangup_cb(struct sipe_media_call *call, gboolean local)
659 if (local) {
660 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
661 struct sip_session *session;
662 session = sipe_session_find_call(call_private->sipe_private,
663 call_private->with);
665 if (session) {
666 sipe_session_close(call_private->sipe_private, session);
671 static void
672 error_cb(struct sipe_media_call *call, gchar *message)
674 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
675 struct sipe_core_private *sipe_private = call_private->sipe_private;
676 gboolean initiator = sipe_backend_media_is_initiator(call->backend_private, NULL);
677 gboolean accepted = sipe_backend_media_accepted(call->backend_private);
679 gchar *title = g_strdup_printf("Call with %s failed", call_private->with);
680 sipe_backend_notify_error(SIPE_CORE_PUBLIC, title, message);
681 g_free(title);
683 if (!initiator && !accepted) {
684 sip_transport_response(sipe_private,
685 call_private->invitation,
686 488, "Not Acceptable Here", NULL);
689 sipe_backend_media_hangup(call->backend_private, initiator || accepted);
692 static struct sipe_media_call_private *
693 sipe_media_call_new(struct sipe_core_private *sipe_private,
694 const gchar* with, gboolean initiator, SipeIceVersion ice_version)
696 struct sipe_media_call_private *call_private = g_new0(struct sipe_media_call_private, 1);
697 gchar *cname;
699 call_private->sipe_private = sipe_private;
701 cname = g_strdup(sipe_private->contact + 1);
702 cname[strlen(cname) - 1] = '\0';
704 call_private->public.backend_private = sipe_backend_media_new(SIPE_CORE_PUBLIC,
705 SIPE_MEDIA_CALL,
706 with,
707 initiator);
708 sipe_backend_media_set_cname(call_private->public.backend_private, cname);
710 call_private->ice_version = ice_version;
711 call_private->encryption_compatible = TRUE;
713 call_private->public.stream_initialized_cb = stream_initialized_cb;
714 call_private->public.media_end_cb = media_end_cb;
715 call_private->public.call_accept_cb = call_accept_cb;
716 call_private->public.call_reject_cb = call_reject_cb;
717 call_private->public.call_hold_cb = call_hold_cb;
718 call_private->public.call_hangup_cb = call_hangup_cb;
719 call_private->public.error_cb = error_cb;
721 g_free(cname);
723 return call_private;
726 void sipe_media_hangup(struct sipe_media_call_private *call_private)
728 if (call_private) {
729 sipe_backend_media_hangup(call_private->public.backend_private,
730 FALSE);
734 static void
735 sipe_media_initiate_call(struct sipe_core_private *sipe_private,
736 const char *with, SipeIceVersion ice_version,
737 gboolean with_video)
739 struct sipe_media_call_private *call_private;
740 struct sipe_backend_media *backend_media;
741 struct sipe_backend_media_relays *backend_media_relays;
742 struct sip_session *session;
743 struct sip_dialog *dialog;
745 if (sipe_private->media_call)
746 return;
748 call_private = sipe_media_call_new(sipe_private, with, TRUE, ice_version);
750 session = sipe_session_add_call(sipe_private, with);
751 dialog = sipe_dialog_add(session);
752 dialog->callid = gencallid();
753 dialog->with = g_strdup(session->with);
754 dialog->ourtag = gentag();
756 call_private->with = g_strdup(session->with);
758 backend_media = call_private->public.backend_private;
760 backend_media_relays =
761 sipe_backend_media_relays_convert(sipe_private->media_relays,
762 sipe_private->media_relay_username,
763 sipe_private->media_relay_password);
765 if (!sipe_backend_media_add_stream(backend_media,
766 "audio", with, SIPE_MEDIA_AUDIO,
767 call_private->ice_version, TRUE,
768 backend_media_relays)) {
769 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
770 _("Error occured"),
771 _("Error creating audio stream"));
772 sipe_media_hangup(call_private);
773 sipe_backend_media_relays_free(backend_media_relays);
774 return;
777 if ( with_video
778 && !sipe_backend_media_add_stream(backend_media,
779 "video", with, SIPE_MEDIA_VIDEO,
780 call_private->ice_version, TRUE,
781 backend_media_relays)) {
782 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
783 _("Error occured"),
784 _("Error creating video stream"));
785 sipe_media_hangup(call_private);
786 sipe_backend_media_relays_free(backend_media_relays);
787 return;
790 sipe_private->media_call = call_private;
792 sipe_backend_media_relays_free(backend_media_relays);
794 // Processing continues in stream_initialized_cb
797 void
798 sipe_core_media_initiate_call(struct sipe_core_public *sipe_public,
799 const char *with,
800 gboolean with_video)
802 sipe_media_initiate_call(SIPE_CORE_PRIVATE, with,
803 SIPE_ICE_RFC_5245, with_video);
806 void sipe_core_media_connect_conference(struct sipe_core_public *sipe_public,
807 struct sipe_chat_session *chat_session)
809 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
810 struct sipe_backend_media_relays *backend_media_relays;
811 struct sip_session *session;
812 struct sip_dialog *dialog;
813 SipeIceVersion ice_version;
814 gchar **parts;
815 gchar *av_uri;
817 session = sipe_session_find_chat(sipe_private, chat_session);
819 if (sipe_private->media_call || !session)
820 return;
822 session->is_call = TRUE;
824 parts = g_strsplit(chat_session->id, "app:conf:focus:", 2);
825 av_uri = g_strjoinv("app:conf:audio-video:", parts);
826 g_strfreev(parts);
828 ice_version = SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013) ? SIPE_ICE_RFC_5245 :
829 SIPE_ICE_DRAFT_6;
831 sipe_private->media_call = sipe_media_call_new(sipe_private, av_uri,
832 TRUE, ice_version);
834 session = sipe_session_add_call(sipe_private, av_uri);
835 dialog = sipe_dialog_add(session);
836 dialog->callid = gencallid();
837 dialog->with = g_strdup(session->with);
838 dialog->ourtag = gentag();
840 g_free(av_uri);
842 sipe_private->media_call->with = g_strdup(session->with);
844 backend_media_relays =
845 sipe_backend_media_relays_convert(sipe_private->media_relays,
846 sipe_private->media_relay_username,
847 sipe_private->media_relay_password);
849 if (!sipe_backend_media_add_stream(sipe_private->media_call->public.backend_private,
850 "audio", dialog->with,
851 SIPE_MEDIA_AUDIO,
852 sipe_private->media_call->ice_version,
853 TRUE, backend_media_relays)) {
854 sipe_backend_notify_error(sipe_public,
855 _("Error occured"),
856 _("Error creating audio stream"));
857 sipe_media_hangup(sipe_private->media_call);
858 sipe_private->media_call = NULL;
861 sipe_backend_media_relays_free(backend_media_relays);
863 // Processing continues in stream_initialized_cb
866 gboolean sipe_core_media_in_call(struct sipe_core_public *sipe_public)
868 if (sipe_public) {
869 return SIPE_CORE_PRIVATE->media_call != NULL;
871 return FALSE;
874 static gboolean phone_number_is_valid(const gchar *phone_number)
876 if (!phone_number || sipe_strequal(phone_number, "")) {
877 return FALSE;
880 if (*phone_number == '+') {
881 ++phone_number;
884 while (*phone_number != '\0') {
885 if (!g_ascii_isdigit(*phone_number)) {
886 return FALSE;
888 ++phone_number;
891 return TRUE;
894 void sipe_core_media_phone_call(struct sipe_core_public *sipe_public,
895 const gchar *phone_number)
897 g_return_if_fail(sipe_public);
899 if (phone_number_is_valid(phone_number)) {
900 gchar *phone_uri = g_strdup_printf("sip:%s@%s;user=phone",
901 phone_number, sipe_public->sip_domain);
903 sipe_core_media_initiate_call(sipe_public, phone_uri, FALSE);
905 g_free(phone_uri);
906 } else {
907 sipe_backend_notify_error(sipe_public,
908 _("Unable to establish a call"),
909 _("Invalid phone number"));
913 void sipe_core_media_test_call(struct sipe_core_public *sipe_public)
915 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
916 if (!sipe_private->test_call_bot_uri) {
917 sipe_backend_notify_error(sipe_public,
918 _("Unable to establish a call"),
919 _("Audio Test Service is not available."));
920 return;
923 sipe_core_media_initiate_call(sipe_public,
924 sipe_private->test_call_bot_uri, FALSE);
927 void
928 process_incoming_invite_call(struct sipe_core_private *sipe_private,
929 struct sipmsg *msg)
931 struct sipe_media_call_private *call_private = sipe_private->media_call;
932 struct sipe_backend_media *backend_media;
933 struct sipe_backend_media_relays *backend_media_relays = NULL;
934 struct sdpmsg *smsg;
935 gboolean has_new_media = FALSE;
936 GSList *i;
938 if (call_private) {
939 char *self;
941 if (!is_media_session_msg(call_private, msg)) {
942 sip_transport_response(sipe_private, msg, 486, "Busy Here", NULL);
943 return;
946 self = sip_uri_self(sipe_private);
947 if (sipe_strequal(call_private->with, self)) {
948 g_free(self);
949 sip_transport_response(sipe_private, msg, 488, "Not Acceptable Here", NULL);
950 return;
952 g_free(self);
955 smsg = sdpmsg_parse_msg(msg->body);
956 if (!smsg) {
957 sip_transport_response(sipe_private, msg,
958 488, "Not Acceptable Here", NULL);
959 sipe_media_hangup(call_private);
960 return;
963 if (!call_private) {
964 gchar *with = parse_from(sipmsg_find_header(msg, "From"));
965 struct sip_session *session;
967 call_private = sipe_media_call_new(sipe_private, with, FALSE, smsg->ice_version);
968 session = sipe_session_add_call(sipe_private, with);
969 sipe_media_dialog_init(session, msg);
971 call_private->with = g_strdup(session->with);
972 sipe_private->media_call = call_private;
973 g_free(with);
976 backend_media = call_private->public.backend_private;
978 if (call_private->invitation)
979 sipmsg_free(call_private->invitation);
980 call_private->invitation = sipmsg_copy(msg);
982 if (smsg->media)
983 backend_media_relays = sipe_backend_media_relays_convert(
984 sipe_private->media_relays,
985 sipe_private->media_relay_username,
986 sipe_private->media_relay_password);
988 // Create any new media streams
989 for (i = smsg->media; i; i = i->next) {
990 struct sdpmedia *media = i->data;
991 gchar *id = media->name;
992 SipeMediaType type;
994 if ( media->port != 0
995 && !sipe_backend_media_get_stream_by_id(backend_media, id)) {
996 gchar *with;
998 if (sipe_strequal(id, "audio"))
999 type = SIPE_MEDIA_AUDIO;
1000 else if (sipe_strequal(id, "video"))
1001 type = SIPE_MEDIA_VIDEO;
1002 else
1003 continue;
1005 with = parse_from(sipmsg_find_header(msg, "From"));
1006 sipe_backend_media_add_stream(backend_media, id, with,
1007 type,
1008 smsg->ice_version,
1009 FALSE,
1010 backend_media_relays);
1011 has_new_media = TRUE;
1012 g_free(with);
1016 sipe_backend_media_relays_free(backend_media_relays);
1018 if (has_new_media) {
1019 sdpmsg_free(call_private->smsg);
1020 call_private->smsg = smsg;
1021 sip_transport_response(sipe_private, call_private->invitation,
1022 180, "Ringing", NULL);
1023 // Processing continues in stream_initialized_cb
1024 } else {
1025 apply_remote_message(call_private, smsg);
1026 send_response_with_session_description(call_private, 200, "OK");
1028 sdpmsg_free(smsg);
1032 void process_incoming_cancel_call(struct sipe_core_private *sipe_private,
1033 struct sipmsg *msg)
1035 struct sipe_media_call_private *call_private = sipe_private->media_call;
1037 // We respond to the CANCEL request with 200 OK response and
1038 // with 487 Request Terminated to the remote INVITE in progress.
1039 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
1041 if (call_private->invitation) {
1042 sip_transport_response(sipe_private, call_private->invitation,
1043 487, "Request Terminated", NULL);
1046 sipe_media_hangup(call_private);
1049 static gboolean
1050 sipe_media_send_ack(struct sipe_core_private *sipe_private,
1051 struct sipmsg *msg,
1052 struct transaction *trans)
1054 struct sipe_media_call_private *call_private = sipe_private->media_call;
1055 struct sip_session *session;
1056 struct sip_dialog *dialog;
1057 int tmp_cseq;
1059 if (!is_media_session_msg(call_private, msg))
1060 return FALSE;
1062 session = sipe_session_find_call(sipe_private, call_private->with);
1063 dialog = session->dialogs->data;
1064 if (!dialog)
1065 return FALSE;
1067 tmp_cseq = dialog->cseq;
1069 dialog->cseq = sip_transaction_cseq(trans) - 1;
1070 sip_transport_ack(sipe_private, dialog);
1071 dialog->cseq = tmp_cseq;
1073 dialog->outgoing_invite = NULL;
1075 return TRUE;
1078 static gboolean
1079 sipe_media_send_final_ack(struct sipe_core_private *sipe_private,
1080 struct sipmsg *msg,
1081 struct transaction *trans)
1083 if (!sipe_media_send_ack(sipe_private, msg, trans))
1084 return FALSE;
1086 sipe_backend_media_accept(sipe_private->media_call->public.backend_private,
1087 FALSE);
1089 return TRUE;
1092 static void
1093 reinvite_on_candidate_pair_cb(struct sipe_core_public *sipe_public)
1095 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1096 struct sipe_media_call_private *media_call = sipe_private->media_call;
1097 struct sipe_backend_media *backend_media;
1098 GSList *streams;
1100 if (!media_call)
1101 return;
1103 backend_media = media_call->public.backend_private;
1104 streams = sipe_backend_media_get_streams(backend_media);
1106 for (; streams; streams = streams->next) {
1107 struct sipe_backend_stream *s = streams->data;
1108 GList *remote_candidates = sipe_backend_media_get_active_remote_candidates(backend_media, s);
1109 guint components = g_list_length(remote_candidates);
1111 sipe_media_candidate_list_free(remote_candidates);
1113 // We must have candidates for both (RTP + RTCP) components ready
1114 if (components < 2) {
1115 sipe_schedule_mseconds(sipe_private,
1116 "<+media-reinvite-on-candidate-pair>",
1117 NULL,
1118 500,
1119 (sipe_schedule_action) reinvite_on_candidate_pair_cb,
1120 NULL);
1121 return;
1125 sipe_invite_call(sipe_private, sipe_media_send_final_ack);
1128 static gboolean
1129 maybe_retry_call_with_ice_v6(struct sipe_core_private *sipe_private,
1130 struct transaction *trans)
1132 struct sipe_media_call_private *call_private = sipe_private->media_call;
1134 if (call_private->ice_version == SIPE_ICE_RFC_5245 &&
1135 sip_transaction_cseq(trans) == 1) {
1136 gchar *with = g_strdup(call_private->with);
1137 struct sipe_backend_media *backend_private = call_private->public.backend_private;
1138 gboolean with_video = sipe_backend_media_get_stream_by_id(backend_private, "video") != NULL;
1140 sipe_media_hangup(call_private);
1141 SIPE_DEBUG_INFO_NOFORMAT("Retrying call witn ICEv6.");
1142 // We might be calling to OC 2007 instance, retry with ICEv6
1143 sipe_media_initiate_call(sipe_private, with,
1144 SIPE_ICE_DRAFT_6, with_video);
1146 g_free(with);
1147 return TRUE;
1150 return FALSE;
1153 static gboolean
1154 process_invite_call_response(struct sipe_core_private *sipe_private,
1155 struct sipmsg *msg,
1156 struct transaction *trans)
1158 const gchar *with;
1159 struct sipe_media_call_private *call_private = sipe_private->media_call;
1160 struct sip_session *session;
1161 struct sip_dialog *dialog;
1162 struct sdpmsg *smsg;
1164 if (!is_media_session_msg(call_private, msg))
1165 return FALSE;
1167 session = sipe_session_find_call(sipe_private, call_private->with);
1168 dialog = session->dialogs->data;
1170 with = dialog->with;
1172 dialog->outgoing_invite = NULL;
1174 if (msg->response >= 400) {
1175 // Call rejected by remote peer or an error occurred
1176 const gchar *title;
1177 GString *desc = g_string_new("");
1178 gboolean append_responsestr = FALSE;
1180 switch (msg->response) {
1181 case 480: {
1182 title = _("User unavailable");
1184 if (sipmsg_parse_warning(msg, NULL) == 391) {
1185 g_string_append_printf(desc, _("%s does not want to be disturbed"), with);
1186 } else
1187 g_string_append_printf(desc, _("User %s is not available"), with);
1188 break;
1190 case 603:
1191 case 605:
1192 title = _("Call rejected");
1193 g_string_append_printf(desc, _("User %s rejected call"), with);
1194 break;
1195 case 415:
1196 // OCS/Lync really sends response string with 'Mutipart' typo.
1197 if (sipe_strequal(msg->responsestr, "Mutipart mime in content type not supported by Archiving CDR service") &&
1198 maybe_retry_call_with_ice_v6(sipe_private, trans)) {
1199 return TRUE;
1201 title = _("Unsupported media type");
1202 break;
1203 case 488: {
1204 /* Check for incompatible encryption levels error.
1206 * MS Lync 2010:
1207 * 488 Not Acceptable Here
1208 * ms-client-diagnostics: 52017;reason="Encryption levels dont match"
1210 * older clients (and SIPE itself):
1211 * 488 Encryption Levels not compatible
1213 const gchar *ms_diag = sipmsg_find_header(msg, "ms-client-diagnostics");
1215 if (sipe_strequal(msg->responsestr, "Encryption Levels not compatible") ||
1216 (ms_diag && g_str_has_prefix(ms_diag, "52017;"))) {
1217 title = _("Unable to establish a call");
1218 g_string_append(desc, _("Encryption settings of peer are incompatible with ours."));
1219 break;
1222 if (maybe_retry_call_with_ice_v6(sipe_private, trans)) {
1223 return TRUE;
1225 // Break intentionally omitted
1227 default:
1228 title = _("Error occured");
1229 g_string_append(desc, _("Unable to establish a call"));
1230 append_responsestr = TRUE;
1231 break;
1234 if (append_responsestr) {
1235 gchar *reason = sipmsg_get_ms_diagnostics_reason(msg);
1237 g_string_append_printf(desc, "\n%d %s",
1238 msg->response, msg->responsestr);
1239 if (reason) {
1240 g_string_append_printf(desc, "\n\n%s", reason);
1241 g_free(reason);
1245 sipe_backend_notify_error(SIPE_CORE_PUBLIC, title, desc->str);
1246 g_string_free(desc, TRUE);
1248 sipe_media_send_ack(sipe_private, msg, trans);
1249 sipe_media_hangup(call_private);
1251 return TRUE;
1254 sipe_dialog_parse(dialog, msg, TRUE);
1255 smsg = sdpmsg_parse_msg(msg->body);
1256 if (!smsg) {
1257 sip_transport_response(sipe_private, msg,
1258 488, "Not Acceptable Here", NULL);
1259 sipe_media_hangup(call_private);
1260 return FALSE;
1263 apply_remote_message(call_private, smsg);
1264 sdpmsg_free(smsg);
1266 sipe_media_send_ack(sipe_private, msg, trans);
1267 reinvite_on_candidate_pair_cb(SIPE_CORE_PUBLIC);
1269 return TRUE;
1272 gboolean is_media_session_msg(struct sipe_media_call_private *call_private,
1273 struct sipmsg *msg)
1275 if (call_private) {
1276 const gchar *callid = sipmsg_find_header(msg, "Call-ID");
1277 struct sip_session *session;
1279 session = sipe_session_find_call(call_private->sipe_private,
1280 call_private->with);
1281 if (session) {
1282 struct sip_dialog *dialog = session->dialogs->data;
1283 return sipe_strequal(dialog->callid, callid);
1286 return FALSE;
1289 void sipe_media_handle_going_offline(struct sipe_media_call_private *call_private)
1291 struct sipe_backend_media *backend_private;
1293 backend_private = call_private->public.backend_private;
1295 if ( !sipe_backend_media_is_initiator(backend_private, NULL)
1296 && !sipe_backend_media_accepted(backend_private)) {
1297 sip_transport_response(call_private->sipe_private,
1298 call_private->invitation,
1299 480, "Temporarily Unavailable", NULL);
1300 } else {
1301 struct sip_session *session;
1303 session = sipe_session_find_call(call_private->sipe_private,
1304 call_private->with);
1305 if (session)
1306 sipe_session_close(call_private->sipe_private, session);
1309 sipe_media_hangup(call_private);
1312 gboolean sipe_media_is_conference_call(struct sipe_media_call_private *call_private)
1314 return g_strstr_len(call_private->with, -1, "app:conf:audio-video:") != NULL;
1317 static void
1318 sipe_media_relay_free(struct sipe_media_relay *relay)
1320 g_free(relay->hostname);
1321 if (relay->dns_query)
1322 sipe_backend_dns_query_cancel(relay->dns_query);
1323 g_free(relay);
1326 void
1327 sipe_media_relay_list_free(GSList *list)
1329 for (; list; list = g_slist_delete_link(list, list))
1330 sipe_media_relay_free(list->data);
1333 static void
1334 relay_ip_resolved_cb(struct sipe_media_relay* relay,
1335 const gchar *ip, SIPE_UNUSED_PARAMETER guint port)
1337 gchar *hostname = relay->hostname;
1338 relay->dns_query = NULL;
1340 if (ip && port) {
1341 relay->hostname = g_strdup(ip);
1342 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname, ip);
1343 } else {
1344 relay->hostname = NULL;
1345 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname);
1348 g_free(hostname);
1351 static gboolean
1352 process_get_av_edge_credentials_response(struct sipe_core_private *sipe_private,
1353 struct sipmsg *msg,
1354 SIPE_UNUSED_PARAMETER struct transaction *trans)
1356 g_free(sipe_private->media_relay_username);
1357 g_free(sipe_private->media_relay_password);
1358 sipe_media_relay_list_free(sipe_private->media_relays);
1359 sipe_private->media_relay_username = NULL;
1360 sipe_private->media_relay_password = NULL;
1361 sipe_private->media_relays = NULL;
1363 if (msg->response >= 400) {
1364 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
1365 "Failed to obtain A/V Edge credentials.");
1366 return FALSE;
1369 if (msg->response == 200) {
1370 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
1372 if (sipe_strequal("OK", sipe_xml_attribute(xn_response, "reasonPhrase"))) {
1373 const sipe_xml *xn_credentials = sipe_xml_child(xn_response, "credentialsResponse/credentials");
1374 const sipe_xml *xn_relays = sipe_xml_child(xn_response, "credentialsResponse/mediaRelayList");
1375 const sipe_xml *item;
1376 GSList *relays = NULL;
1378 item = sipe_xml_child(xn_credentials, "username");
1379 sipe_private->media_relay_username = sipe_xml_data(item);
1380 item = sipe_xml_child(xn_credentials, "password");
1381 sipe_private->media_relay_password = sipe_xml_data(item);
1383 for (item = sipe_xml_child(xn_relays, "mediaRelay"); item; item = sipe_xml_twin(item)) {
1384 struct sipe_media_relay *relay = g_new0(struct sipe_media_relay, 1);
1385 const sipe_xml *node;
1386 gchar *tmp;
1388 node = sipe_xml_child(item, "hostName");
1389 relay->hostname = sipe_xml_data(node);
1391 node = sipe_xml_child(item, "udpPort");
1392 if (node) {
1393 relay->udp_port = atoi(tmp = sipe_xml_data(node));
1394 g_free(tmp);
1397 node = sipe_xml_child(item, "tcpPort");
1398 if (node) {
1399 relay->tcp_port = atoi(tmp = sipe_xml_data(node));
1400 g_free(tmp);
1403 relays = g_slist_append(relays, relay);
1405 relay->dns_query = sipe_backend_dns_query_a(
1406 SIPE_CORE_PUBLIC,
1407 relay->hostname,
1408 relay->udp_port,
1409 (sipe_dns_resolved_cb) relay_ip_resolved_cb,
1410 relay);
1412 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
1413 relay->hostname,
1414 relay->tcp_port, relay->udp_port);
1417 sipe_private->media_relays = relays;
1420 sipe_xml_free(xn_response);
1423 return TRUE;
1426 void
1427 sipe_media_get_av_edge_credentials(struct sipe_core_private *sipe_private)
1429 // TODO: re-request credentials after duration expires?
1430 const char CRED_REQUEST_XML[] =
1431 "<request requestID=\"%d\" "
1432 "from=\"%s\" "
1433 "version=\"1.0\" "
1434 "to=\"%s\" "
1435 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
1436 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
1437 "<credentialsRequest credentialsRequestID=\"%d\">"
1438 "<identity>%s</identity>"
1439 "<location>%s</location>"
1440 "<duration>480</duration>"
1441 "</credentialsRequest>"
1442 "</request>";
1444 int request_id = rand();
1445 gchar *self;
1446 gchar *body;
1448 if (!sipe_private->mras_uri)
1449 return;
1451 self = sip_uri_self(sipe_private);
1453 body = g_strdup_printf(
1454 CRED_REQUEST_XML,
1455 request_id,
1456 self,
1457 sipe_private->mras_uri,
1458 request_id,
1459 self,
1460 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER) ? "internet" : "intranet");
1461 g_free(self);
1463 sip_transport_service(sipe_private,
1464 sipe_private->mras_uri,
1465 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
1466 body,
1467 process_get_av_edge_credentials_response);
1469 g_free(body);
1473 Local Variables:
1474 mode: c
1475 c-file-style: "bsd"
1476 indent-tabs-mode: t
1477 tab-width: 8
1478 End: