media: fix failing call when our host has multiple IP addresses
[siplcs.git] / src / core / sipe-media.c
blob2962f8ebf40abbf7f83fe615f61618bd84c2c5f2
1 /**
2 * @file sipe-media.c
4 * pidgin-sipe
6 * Copyright (C) 2011-13 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_call_free(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_call_free(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 gchar **parts;
814 gchar *av_uri;
816 session = sipe_session_find_chat(sipe_private, chat_session);
818 if (sipe_private->media_call || !session)
819 return;
821 session->is_call = TRUE;
823 parts = g_strsplit(chat_session->id, "app:conf:focus:", 2);
824 av_uri = g_strjoinv("app:conf:audio-video:", parts);
825 g_strfreev(parts);
827 sipe_private->media_call = sipe_media_call_new(sipe_private, av_uri,
828 TRUE, SIPE_ICE_DRAFT_6);
830 session = sipe_session_add_call(sipe_private, av_uri);
831 dialog = sipe_dialog_add(session);
832 dialog->callid = gencallid();
833 dialog->with = g_strdup(session->with);
834 dialog->ourtag = gentag();
836 g_free(av_uri);
838 sipe_private->media_call->with = g_strdup(session->with);
840 backend_media_relays =
841 sipe_backend_media_relays_convert(sipe_private->media_relays,
842 sipe_private->media_relay_username,
843 sipe_private->media_relay_password);
845 if (!sipe_backend_media_add_stream(sipe_private->media_call->public.backend_private,
846 "audio", dialog->with,
847 SIPE_MEDIA_AUDIO,
848 sipe_private->media_call->ice_version,
849 TRUE, backend_media_relays)) {
850 sipe_backend_notify_error(sipe_public,
851 _("Error occured"),
852 _("Error creating audio stream"));
853 sipe_media_call_free(sipe_private->media_call);
854 sipe_private->media_call = NULL;
857 sipe_backend_media_relays_free(backend_media_relays);
859 // Processing continues in stream_initialized_cb
862 gboolean sipe_core_media_in_call(struct sipe_core_public *sipe_public)
864 if (sipe_public) {
865 return SIPE_CORE_PRIVATE->media_call != NULL;
867 return FALSE;
870 static gboolean phone_number_is_valid(const gchar *phone_number)
872 if (!phone_number || sipe_strequal(phone_number, "")) {
873 return FALSE;
876 if (*phone_number == '+') {
877 ++phone_number;
880 while (*phone_number != '\0') {
881 if (!g_ascii_isdigit(*phone_number)) {
882 return FALSE;
884 ++phone_number;
887 return TRUE;
890 void sipe_core_media_phone_call(struct sipe_core_public *sipe_public,
891 const gchar *phone_number)
893 g_return_if_fail(sipe_public);
895 if (phone_number_is_valid(phone_number)) {
896 gchar *phone_uri = g_strdup_printf("sip:%s@%s;user=phone",
897 phone_number, sipe_public->sip_domain);
899 sipe_core_media_initiate_call(sipe_public, phone_uri, FALSE);
901 g_free(phone_uri);
902 } else {
903 sipe_backend_notify_error(sipe_public,
904 _("Unable to establish a call"),
905 _("Invalid phone number"));
909 void sipe_core_media_test_call(struct sipe_core_public *sipe_public)
911 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
912 if (!sipe_private->test_call_bot_uri) {
913 sipe_backend_notify_error(sipe_public,
914 _("Unable to establish a call"),
915 _("Audio Test Service is not available."));
916 return;
919 sipe_core_media_initiate_call(sipe_public,
920 sipe_private->test_call_bot_uri, FALSE);
923 void
924 process_incoming_invite_call(struct sipe_core_private *sipe_private,
925 struct sipmsg *msg)
927 struct sipe_media_call_private *call_private = sipe_private->media_call;
928 struct sipe_backend_media *backend_media;
929 struct sipe_backend_media_relays *backend_media_relays = NULL;
930 struct sdpmsg *smsg;
931 gboolean has_new_media = FALSE;
932 GSList *i;
934 if (call_private && !is_media_session_msg(call_private, msg)) {
935 sip_transport_response(sipe_private, msg, 486, "Busy Here", NULL);
936 return;
939 smsg = sdpmsg_parse_msg(msg->body);
940 if (!smsg) {
941 sip_transport_response(sipe_private, msg,
942 488, "Not Acceptable Here", NULL);
943 sipe_media_hangup(call_private);
944 return;
947 if (!call_private) {
948 gchar *with = parse_from(sipmsg_find_header(msg, "From"));
949 struct sip_session *session;
951 call_private = sipe_media_call_new(sipe_private, with, FALSE, smsg->ice_version);
952 session = sipe_session_add_call(sipe_private, with);
953 sipe_media_dialog_init(session, msg);
955 call_private->with = g_strdup(session->with);
956 sipe_private->media_call = call_private;
957 g_free(with);
960 backend_media = call_private->public.backend_private;
962 if (call_private->invitation)
963 sipmsg_free(call_private->invitation);
964 call_private->invitation = sipmsg_copy(msg);
966 if (smsg->media)
967 backend_media_relays = sipe_backend_media_relays_convert(
968 sipe_private->media_relays,
969 sipe_private->media_relay_username,
970 sipe_private->media_relay_password);
972 // Create any new media streams
973 for (i = smsg->media; i; i = i->next) {
974 struct sdpmedia *media = i->data;
975 gchar *id = media->name;
976 SipeMediaType type;
978 if ( media->port != 0
979 && !sipe_backend_media_get_stream_by_id(backend_media, id)) {
980 gchar *with;
982 if (sipe_strequal(id, "audio"))
983 type = SIPE_MEDIA_AUDIO;
984 else if (sipe_strequal(id, "video"))
985 type = SIPE_MEDIA_VIDEO;
986 else
987 continue;
989 with = parse_from(sipmsg_find_header(msg, "From"));
990 sipe_backend_media_add_stream(backend_media, id, with,
991 type,
992 smsg->ice_version,
993 FALSE,
994 backend_media_relays);
995 has_new_media = TRUE;
996 g_free(with);
1000 sipe_backend_media_relays_free(backend_media_relays);
1002 if (has_new_media) {
1003 sdpmsg_free(call_private->smsg);
1004 call_private->smsg = smsg;
1005 sip_transport_response(sipe_private, call_private->invitation,
1006 180, "Ringing", NULL);
1007 // Processing continues in stream_initialized_cb
1008 } else {
1009 apply_remote_message(call_private, smsg);
1010 send_response_with_session_description(call_private, 200, "OK");
1012 sdpmsg_free(smsg);
1016 void process_incoming_cancel_call(struct sipe_core_private *sipe_private,
1017 struct sipmsg *msg)
1019 struct sipe_media_call_private *call_private = sipe_private->media_call;
1021 // We respond to the CANCEL request with 200 OK response and
1022 // with 487 Request Terminated to the remote INVITE in progress.
1023 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
1025 if (call_private->invitation) {
1026 sip_transport_response(sipe_private, call_private->invitation,
1027 487, "Request Terminated", NULL);
1030 sipe_media_hangup(call_private);
1033 static gboolean
1034 sipe_media_send_ack(struct sipe_core_private *sipe_private,
1035 struct sipmsg *msg,
1036 struct transaction *trans)
1038 struct sipe_media_call_private *call_private = sipe_private->media_call;
1039 struct sip_session *session;
1040 struct sip_dialog *dialog;
1041 int tmp_cseq;
1043 if (!is_media_session_msg(call_private, msg))
1044 return FALSE;
1046 session = sipe_session_find_call(sipe_private, call_private->with);
1047 dialog = session->dialogs->data;
1048 if (!dialog)
1049 return FALSE;
1051 tmp_cseq = dialog->cseq;
1053 dialog->cseq = sip_transaction_cseq(trans) - 1;
1054 sip_transport_ack(sipe_private, dialog);
1055 dialog->cseq = tmp_cseq;
1057 dialog->outgoing_invite = NULL;
1059 return TRUE;
1062 static gboolean
1063 sipe_media_send_final_ack(struct sipe_core_private *sipe_private,
1064 struct sipmsg *msg,
1065 struct transaction *trans)
1067 if (!sipe_media_send_ack(sipe_private, msg, trans))
1068 return FALSE;
1070 sipe_backend_media_accept(sipe_private->media_call->public.backend_private,
1071 FALSE);
1073 return TRUE;
1076 static void
1077 reinvite_on_candidate_pair_cb(struct sipe_core_public *sipe_public)
1079 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1080 struct sipe_media_call_private *media_call = sipe_private->media_call;
1081 struct sipe_backend_media *backend_media;
1082 GSList *streams;
1084 if (!media_call)
1085 return;
1087 backend_media = media_call->public.backend_private;
1088 streams = sipe_backend_media_get_streams(backend_media);
1090 for (; streams; streams = streams->next) {
1091 struct sipe_backend_stream *s = streams->data;
1092 GList *remote_candidates = sipe_backend_media_get_active_remote_candidates(backend_media, s);
1093 guint components = g_list_length(remote_candidates);
1095 sipe_media_candidate_list_free(remote_candidates);
1097 // We must have candidates for both (RTP + RTCP) components ready
1098 if (components < 2) {
1099 sipe_schedule_mseconds(sipe_private,
1100 "<+media-reinvite-on-candidate-pair>",
1101 NULL,
1102 500,
1103 (sipe_schedule_action) reinvite_on_candidate_pair_cb,
1104 NULL);
1105 return;
1109 sipe_invite_call(sipe_private, sipe_media_send_final_ack);
1112 static gboolean
1113 process_invite_call_response(struct sipe_core_private *sipe_private,
1114 struct sipmsg *msg,
1115 struct transaction *trans)
1117 const gchar *with;
1118 struct sipe_media_call_private *call_private = sipe_private->media_call;
1119 struct sipe_backend_media *backend_private;
1120 struct sip_session *session;
1121 struct sip_dialog *dialog;
1122 struct sdpmsg *smsg;
1124 if (!is_media_session_msg(call_private, msg))
1125 return FALSE;
1127 session = sipe_session_find_call(sipe_private, call_private->with);
1128 dialog = session->dialogs->data;
1130 backend_private = call_private->public.backend_private;
1131 with = dialog->with;
1133 dialog->outgoing_invite = NULL;
1135 if (msg->response >= 400) {
1136 // Call rejected by remote peer or an error occurred
1137 const gchar *title;
1138 GString *desc = g_string_new("");
1139 gboolean append_responsestr = FALSE;
1141 switch (msg->response) {
1142 case 480: {
1143 title = _("User unavailable");
1145 if (sipmsg_parse_warning(msg, NULL) == 391) {
1146 g_string_append_printf(desc, _("%s does not want to be disturbed"), with);
1147 } else
1148 g_string_append_printf(desc, _("User %s is not available"), with);
1149 break;
1151 case 603:
1152 case 605:
1153 title = _("Call rejected");
1154 g_string_append_printf(desc, _("User %s rejected call"), with);
1155 break;
1156 case 488: {
1157 /* Check for incompatible encryption levels error.
1159 * MS Lync 2010:
1160 * 488 Not Acceptable Here
1161 * ms-client-diagnostics: 52017;reason="Encryption levels dont match"
1163 * older clients (and SIPE itself):
1164 * 488 Encryption Levels not compatible
1166 const gchar *ms_diag = sipmsg_find_header(msg, "ms-client-diagnostics");
1168 if (sipe_strequal(msg->responsestr, "Encryption Levels not compatible") ||
1169 (ms_diag && g_str_has_prefix(ms_diag, "52017;"))) {
1170 title = _("Unable to establish a call");
1171 g_string_append(desc, _("Encryption settings of peer are incompatible with ours."));
1172 break;
1175 if (call_private->ice_version == SIPE_ICE_RFC_5245 &&
1176 sip_transaction_cseq(trans) == 1) {
1177 gchar *with = g_strdup(call_private->with);
1178 gboolean with_video = sipe_backend_media_get_stream_by_id(backend_private, "video") != NULL;
1180 sipe_media_hangup(call_private);
1181 // We might be calling to OC 2007 instance, retry with ICEv6
1182 sipe_media_initiate_call(sipe_private, with,
1183 SIPE_ICE_DRAFT_6, with_video);
1185 g_free(with);
1186 return TRUE;
1188 // Break intentionally omitted
1190 default:
1191 title = _("Error occured");
1192 g_string_append(desc, _("Unable to establish a call"));
1193 append_responsestr = TRUE;
1194 break;
1197 if (append_responsestr) {
1198 gchar *reason = sipmsg_get_ms_diagnostics_reason(msg);
1200 g_string_append_printf(desc, "\n%d %s",
1201 msg->response, msg->responsestr);
1202 if (reason) {
1203 g_string_append_printf(desc, "\n\n%s", reason);
1204 g_free(reason);
1208 sipe_backend_notify_error(SIPE_CORE_PUBLIC, title, desc->str);
1209 g_string_free(desc, TRUE);
1211 sipe_media_send_ack(sipe_private, msg, trans);
1212 sipe_media_hangup(call_private);
1214 return TRUE;
1217 sipe_dialog_parse(dialog, msg, TRUE);
1218 smsg = sdpmsg_parse_msg(msg->body);
1219 if (!smsg) {
1220 sip_transport_response(sipe_private, msg,
1221 488, "Not Acceptable Here", NULL);
1222 sipe_media_hangup(call_private);
1223 return FALSE;
1226 apply_remote_message(call_private, smsg);
1227 sdpmsg_free(smsg);
1229 sipe_media_send_ack(sipe_private, msg, trans);
1230 reinvite_on_candidate_pair_cb(SIPE_CORE_PUBLIC);
1232 return TRUE;
1235 gboolean is_media_session_msg(struct sipe_media_call_private *call_private,
1236 struct sipmsg *msg)
1238 if (call_private) {
1239 const gchar *callid = sipmsg_find_header(msg, "Call-ID");
1240 struct sip_session *session;
1242 session = sipe_session_find_call(call_private->sipe_private,
1243 call_private->with);
1244 if (session) {
1245 struct sip_dialog *dialog = session->dialogs->data;
1246 return sipe_strequal(dialog->callid, callid);
1249 return FALSE;
1252 void sipe_media_handle_going_offline(struct sipe_media_call_private *call_private)
1254 struct sipe_backend_media *backend_private;
1256 backend_private = call_private->public.backend_private;
1258 if ( !sipe_backend_media_is_initiator(backend_private, NULL)
1259 && !sipe_backend_media_accepted(backend_private)) {
1260 sip_transport_response(call_private->sipe_private,
1261 call_private->invitation,
1262 480, "Temporarily Unavailable", NULL);
1263 } else {
1264 struct sip_session *session;
1266 session = sipe_session_find_call(call_private->sipe_private,
1267 call_private->with);
1268 if (session)
1269 sipe_session_close(call_private->sipe_private, session);
1272 sipe_media_hangup(call_private);
1275 gboolean sipe_media_is_conference_call(struct sipe_media_call_private *call_private)
1277 return g_strstr_len(call_private->with, -1, "app:conf:audio-video:") != NULL;
1280 static void
1281 sipe_media_relay_free(struct sipe_media_relay *relay)
1283 g_free(relay->hostname);
1284 if (relay->dns_query)
1285 sipe_backend_dns_query_cancel(relay->dns_query);
1286 g_free(relay);
1289 void
1290 sipe_media_relay_list_free(GSList *list)
1292 for (; list; list = g_slist_delete_link(list, list))
1293 sipe_media_relay_free(list->data);
1296 static void
1297 relay_ip_resolved_cb(struct sipe_media_relay* relay,
1298 const gchar *ip, SIPE_UNUSED_PARAMETER guint port)
1300 gchar *hostname = relay->hostname;
1301 relay->dns_query = NULL;
1303 if (ip && port) {
1304 relay->hostname = g_strdup(ip);
1305 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname, ip);
1306 } else {
1307 relay->hostname = NULL;
1308 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname);
1311 g_free(hostname);
1314 static gboolean
1315 process_get_av_edge_credentials_response(struct sipe_core_private *sipe_private,
1316 struct sipmsg *msg,
1317 SIPE_UNUSED_PARAMETER struct transaction *trans)
1319 g_free(sipe_private->media_relay_username);
1320 g_free(sipe_private->media_relay_password);
1321 sipe_media_relay_list_free(sipe_private->media_relays);
1322 sipe_private->media_relay_username = NULL;
1323 sipe_private->media_relay_password = NULL;
1324 sipe_private->media_relays = NULL;
1326 if (msg->response >= 400) {
1327 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
1328 "Failed to obtain A/V Edge credentials.");
1329 return FALSE;
1332 if (msg->response == 200) {
1333 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
1335 if (sipe_strequal("OK", sipe_xml_attribute(xn_response, "reasonPhrase"))) {
1336 const sipe_xml *xn_credentials = sipe_xml_child(xn_response, "credentialsResponse/credentials");
1337 const sipe_xml *xn_relays = sipe_xml_child(xn_response, "credentialsResponse/mediaRelayList");
1338 const sipe_xml *item;
1339 GSList *relays = NULL;
1341 item = sipe_xml_child(xn_credentials, "username");
1342 sipe_private->media_relay_username = sipe_xml_data(item);
1343 item = sipe_xml_child(xn_credentials, "password");
1344 sipe_private->media_relay_password = sipe_xml_data(item);
1346 for (item = sipe_xml_child(xn_relays, "mediaRelay"); item; item = sipe_xml_twin(item)) {
1347 struct sipe_media_relay *relay = g_new0(struct sipe_media_relay, 1);
1348 const sipe_xml *node;
1349 gchar *tmp;
1351 node = sipe_xml_child(item, "hostName");
1352 relay->hostname = sipe_xml_data(node);
1354 node = sipe_xml_child(item, "udpPort");
1355 if (node) {
1356 relay->udp_port = atoi(tmp = sipe_xml_data(node));
1357 g_free(tmp);
1360 node = sipe_xml_child(item, "tcpPort");
1361 if (node) {
1362 relay->tcp_port = atoi(tmp = sipe_xml_data(node));
1363 g_free(tmp);
1366 relays = g_slist_append(relays, relay);
1368 relay->dns_query = sipe_backend_dns_query_a(
1369 SIPE_CORE_PUBLIC,
1370 relay->hostname,
1371 relay->udp_port,
1372 (sipe_dns_resolved_cb) relay_ip_resolved_cb,
1373 relay);
1375 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
1376 relay->hostname,
1377 relay->tcp_port, relay->udp_port);
1380 sipe_private->media_relays = relays;
1383 sipe_xml_free(xn_response);
1386 return TRUE;
1389 void
1390 sipe_media_get_av_edge_credentials(struct sipe_core_private *sipe_private)
1392 // TODO: re-request credentials after duration expires?
1393 const char CRED_REQUEST_XML[] =
1394 "<request requestID=\"%d\" "
1395 "from=\"%s\" "
1396 "version=\"1.0\" "
1397 "to=\"%s\" "
1398 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
1399 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
1400 "<credentialsRequest credentialsRequestID=\"%d\">"
1401 "<identity>%s</identity>"
1402 "<location>%s</location>"
1403 "<duration>480</duration>"
1404 "</credentialsRequest>"
1405 "</request>";
1407 int request_id = rand();
1408 gchar *self;
1409 gchar *body;
1411 if (!sipe_private->mras_uri)
1412 return;
1414 self = sip_uri_self(sipe_private);
1416 body = g_strdup_printf(
1417 CRED_REQUEST_XML,
1418 request_id,
1419 self,
1420 sipe_private->mras_uri,
1421 request_id,
1422 self,
1423 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER) ? "internet" : "intranet");
1424 g_free(self);
1426 sip_transport_service(sipe_private,
1427 sipe_private->mras_uri,
1428 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
1429 body,
1430 process_get_av_edge_credentials_response);
1432 g_free(body);
1436 Local Variables:
1437 mode: c
1438 c-file-style: "bsd"
1439 indent-tabs-mode: t
1440 tab-width: 8
1441 End: