media: allow to make a call to Audio Test Service when it is available
[siplcs.git] / src / core / sipe-media.c
blob22f1bded045d4e90effd3825f91d4fbb03fd5b3f
1 /**
2 * @file sipe-media.c
4 * pidgin-sipe
6 * Copyright (C) 2011-12 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-session.h"
45 #include "sipe-utils.h"
46 #include "sipe-nls.h"
47 #include "sipe-schedule.h"
48 #include "sipe-xml.h"
50 struct sipe_media_call_private {
51 struct sipe_media_call public;
53 /* private part starts here */
54 struct sipe_core_private *sipe_private;
55 gchar *with;
57 struct sipmsg *invitation;
58 SipeIceVersion ice_version;
59 gboolean encryption_compatible;
61 struct sdpmsg *smsg;
62 GSList *failed_media;
64 #define SIPE_MEDIA_CALL ((struct sipe_media_call *) call_private)
65 #define SIPE_MEDIA_CALL_PRIVATE ((struct sipe_media_call_private *) call)
67 static void sipe_media_codec_list_free(GList *codecs)
69 for (; codecs; codecs = g_list_delete_link(codecs, codecs))
70 sipe_backend_codec_free(codecs->data);
73 static void sipe_media_candidate_list_free(GList *candidates)
75 for (; candidates; candidates = g_list_delete_link(candidates, candidates))
76 sipe_backend_candidate_free(candidates->data);
79 static void
80 sipe_media_call_free(struct sipe_media_call_private *call_private)
82 if (call_private) {
83 struct sip_session *session;
84 sipe_backend_media_free(call_private->public.backend_private);
86 session = sipe_session_find_call(call_private->sipe_private,
87 call_private->with);
88 if (session)
89 sipe_session_remove(call_private->sipe_private, session);
91 if (call_private->invitation)
92 sipmsg_free(call_private->invitation);
94 sdpmsg_free(call_private->smsg);
95 g_slist_free_full(call_private->failed_media,
96 (GDestroyNotify)sdpmedia_free);
97 g_free(call_private->with);
98 g_free(call_private);
102 static GSList *
103 backend_candidates_to_sdpcandidate(GList *candidates)
105 GSList *result = NULL;
106 GList *i;
108 for (i = candidates; i; i = i->next) {
109 struct sipe_backend_candidate *candidate = i->data;
110 struct sdpcandidate *c = g_new(struct sdpcandidate, 1);
112 c->foundation = sipe_backend_candidate_get_foundation(candidate);
113 c->component = sipe_backend_candidate_get_component_type(candidate);
114 c->type = sipe_backend_candidate_get_type(candidate);
115 c->protocol = sipe_backend_candidate_get_protocol(candidate);
116 c->ip = sipe_backend_candidate_get_ip(candidate);
117 c->port = sipe_backend_candidate_get_port(candidate);
118 c->base_ip = sipe_backend_candidate_get_base_ip(candidate);
119 c->base_port = sipe_backend_candidate_get_base_port(candidate);
120 c->priority = sipe_backend_candidate_get_priority(candidate);
121 c->username = sipe_backend_candidate_get_username(candidate);
122 c->password = sipe_backend_candidate_get_password(candidate);
124 result = g_slist_append(result, c);
127 return result;
130 static void
131 get_stream_ip_and_ports(GSList *candidates,
132 gchar **ip, guint *rtp_port, guint *rtcp_port,
133 SipeCandidateType type)
135 *rtp_port = 0;
136 *rtcp_port = 0;
138 for (; candidates; candidates = candidates->next) {
139 struct sdpcandidate *candidate = candidates->data;
141 if (type == SIPE_CANDIDATE_TYPE_ANY || candidate->type == type) {
142 if (candidate->component == SIPE_COMPONENT_RTP) {
143 *rtp_port = candidate->port;
144 *ip = g_strdup(candidate->ip);
145 } else if (candidate->component == SIPE_COMPONENT_RTCP)
146 *rtcp_port = candidate->port;
149 if (*rtp_port != 0 && *rtcp_port != 0)
150 return;
154 static struct sdpmedia *
155 backend_stream_to_sdpmedia(struct sipe_backend_media *backend_media,
156 struct sipe_backend_stream *backend_stream)
158 struct sdpmedia *media = g_new0(struct sdpmedia, 1);
159 GList *codecs = sipe_backend_get_local_codecs(backend_media,
160 backend_stream);
161 guint rtcp_port = 0;
162 SipeMediaType type;
163 GSList *attributes = NULL;
164 GList *candidates;
165 GList *i;
167 media->name = g_strdup(sipe_backend_stream_get_id(backend_stream));
169 if (sipe_strequal(media->name, "audio"))
170 type = SIPE_MEDIA_AUDIO;
171 else if (sipe_strequal(media->name, "video"))
172 type = SIPE_MEDIA_VIDEO;
173 else {
174 // TODO: incompatible media, should not happen here
175 g_free(media->name);
176 g_free(media);
177 sipe_media_codec_list_free(codecs);
178 return(NULL);
181 // Process codecs
182 for (i = codecs; i; i = i->next) {
183 struct sipe_backend_codec *codec = i->data;
184 struct sdpcodec *c = g_new0(struct sdpcodec, 1);
185 GList *params;
187 c->id = sipe_backend_codec_get_id(codec);
188 c->name = sipe_backend_codec_get_name(codec);
189 c->clock_rate = sipe_backend_codec_get_clock_rate(codec);
190 c->type = type;
192 params = sipe_backend_codec_get_optional_parameters(codec);
193 for (; params; params = params->next) {
194 struct sipnameval *param = params->data;
195 struct sipnameval *copy = g_new0(struct sipnameval, 1);
197 copy->name = g_strdup(param->name);
198 copy->value = g_strdup(param->value);
200 c->parameters = g_slist_append(c->parameters, copy);
203 media->codecs = g_slist_append(media->codecs, c);
206 sipe_media_codec_list_free(codecs);
208 // Process local candidates
209 // If we have established candidate pairs, send them in SDP response.
210 // Otherwise send all available local candidates.
211 candidates = sipe_backend_media_get_active_local_candidates(backend_media,
212 backend_stream);
213 if (!candidates)
214 candidates = sipe_backend_get_local_candidates(backend_media,
215 backend_stream);
217 media->candidates = backend_candidates_to_sdpcandidate(candidates);
219 sipe_media_candidate_list_free(candidates);
221 get_stream_ip_and_ports(media->candidates, &media->ip, &media->port,
222 &rtcp_port, SIPE_CANDIDATE_TYPE_HOST);
223 // No usable HOST candidates, use any candidate
224 if (media->ip == NULL && media->candidates) {
225 get_stream_ip_and_ports(media->candidates, &media->ip, &media->port,
226 &rtcp_port, SIPE_CANDIDATE_TYPE_ANY);
229 if (sipe_backend_stream_is_held(backend_stream))
230 attributes = sipe_utils_nameval_add(attributes, "inactive", "");
232 if (rtcp_port) {
233 gchar *tmp = g_strdup_printf("%u", rtcp_port);
234 attributes = sipe_utils_nameval_add(attributes, "rtcp", tmp);
235 g_free(tmp);
238 attributes = sipe_utils_nameval_add(attributes, "encryption", "rejected");
240 media->attributes = attributes;
242 // Process remote candidates
243 candidates = sipe_backend_media_get_active_remote_candidates(backend_media,
244 backend_stream);
245 media->remote_candidates = backend_candidates_to_sdpcandidate(candidates);
246 sipe_media_candidate_list_free(candidates);
248 return media;
251 static struct sdpmsg *
252 sipe_media_to_sdpmsg(struct sipe_media_call_private *call_private)
254 struct sipe_backend_media *backend_media = call_private->public.backend_private;
255 struct sdpmsg *msg = g_new0(struct sdpmsg, 1);
256 GSList *streams = sipe_backend_media_get_streams(backend_media);
258 for (; streams; streams = streams->next) {
259 struct sdpmedia *media = backend_stream_to_sdpmedia(backend_media, streams->data);
260 if (media) {
261 msg->media = g_slist_append(msg->media, media);
263 if (msg->ip == NULL)
264 msg->ip = g_strdup(media->ip);
268 msg->media = g_slist_concat(msg->media, call_private->failed_media);
269 call_private->failed_media = NULL;
271 msg->ice_version = call_private->ice_version;
273 return msg;
276 static void
277 sipe_invite_call(struct sipe_core_private *sipe_private, TransCallback tc)
279 gchar *hdr;
280 gchar *contact;
281 gchar *body;
282 struct sipe_media_call_private *call_private = sipe_private->media_call;
283 struct sip_session *session;
284 struct sip_dialog *dialog;
285 struct sdpmsg *msg;
286 gboolean add_2007_fallback = FALSE;
288 session = sipe_session_find_call(sipe_private, call_private->with);
289 dialog = session->dialogs->data;
290 add_2007_fallback = dialog->cseq == 0 && call_private->ice_version == SIPE_ICE_RFC_5245;
292 contact = get_contact(sipe_private);
293 hdr = g_strdup_printf(
294 "ms-keep-alive: UAC;hop-hop=yes\r\n"
295 "Contact: %s\r\n"
296 "Content-Type: %s\r\n",
297 contact,
298 add_2007_fallback ?
299 "multipart/alternative;boundary=\"----=_NextPart_000_001E_01CB4397.0B5EB570\""
300 : "application/sdp");
301 g_free(contact);
303 msg = sipe_media_to_sdpmsg(call_private);
304 body = sdpmsg_to_string(msg);
306 if (add_2007_fallback) {
307 gchar *tmp;
308 tmp = g_strdup_printf(
309 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
310 "Content-Type: application/sdp\r\n"
311 "Content-Transfer-Encoding: 7bit\r\n"
312 "Content-Disposition: session; handling=optional; ms-proxy-2007fallback\r\n"
313 "\r\n"
314 "o=- 0 0 IN IP4 %s\r\n"
315 "s=session\r\n"
316 "c=IN IP4 %s\r\n"
317 "m=audio 0 RTP/AVP\r\n"
318 "\r\n"
319 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
320 "Content-Type: application/sdp\r\n"
321 "Content-Transfer-Encoding: 7bit\r\n"
322 "Content-Disposition: session; handling=optional\r\n"
323 "\r\n"
324 "%s"
325 "\r\n"
326 "------=_NextPart_000_001E_01CB4397.0B5EB570--\r\n",
327 msg->ip, msg->ip, body);
328 g_free(body);
329 body = tmp;
332 sdpmsg_free(msg);
334 dialog->outgoing_invite = sip_transport_invite(sipe_private,
335 hdr,
336 body,
337 dialog,
338 tc);
340 g_free(body);
341 g_free(hdr);
344 static struct sip_dialog *
345 sipe_media_dialog_init(struct sip_session* session, struct sipmsg *msg)
347 gchar *newTag = gentag();
348 const gchar *oldHeader;
349 gchar *newHeader;
350 struct sip_dialog *dialog;
352 oldHeader = sipmsg_find_header(msg, "To");
353 newHeader = g_strdup_printf("%s;tag=%s", oldHeader, newTag);
354 sipmsg_remove_header_now(msg, "To");
355 sipmsg_add_header_now(msg, "To", newHeader);
356 g_free(newHeader);
358 dialog = sipe_dialog_add(session);
359 dialog->callid = g_strdup(sipmsg_find_header(msg, "Call-ID"));
360 dialog->with = parse_from(sipmsg_find_header(msg, "From"));
361 sipe_dialog_parse(dialog, msg, FALSE);
363 return dialog;
366 static void
367 send_response_with_session_description(struct sipe_media_call_private *call_private, int code, gchar *text)
369 struct sdpmsg *msg = sipe_media_to_sdpmsg(call_private);
370 gchar *body = sdpmsg_to_string(msg);
371 sdpmsg_free(msg);
372 sipmsg_add_header(call_private->invitation, "Content-Type", "application/sdp");
373 sip_transport_response(call_private->sipe_private, call_private->invitation, code, text, body);
374 g_free(body);
377 static gboolean
378 encryption_levels_compatible(struct sdpmsg *msg)
380 GSList *i;
382 for (i = msg->media; i; i = i->next) {
383 const gchar *enc_level;
384 struct sdpmedia *m = i->data;
386 enc_level = sipe_utils_nameval_find(m->attributes, "encryption");
388 // Decline call if peer requires encryption as we don't support it yet.
389 if (sipe_strequal(enc_level, "required"))
390 return FALSE;
393 return TRUE;
396 static gboolean
397 process_invite_call_response(struct sipe_core_private *sipe_private,
398 struct sipmsg *msg,
399 struct transaction *trans);
401 static gboolean
402 update_remote_media(struct sipe_media_call_private* call_private,
403 struct sdpmedia *media)
405 struct sipe_backend_media *backend_media = SIPE_MEDIA_CALL->backend_private;
406 struct sipe_backend_stream *backend_stream;
407 GList *backend_candidates = NULL;
408 GList *backend_codecs = NULL;
409 GSList *i;
410 gboolean result = TRUE;
412 backend_stream = sipe_backend_media_get_stream_by_id(backend_media,
413 media->name);
414 if (media->port == 0) {
415 if (backend_stream)
416 sipe_backend_media_remove_stream(backend_media, backend_stream);
417 return TRUE;
420 if (!backend_stream)
421 return FALSE;
423 for (i = media->codecs; i; i = i->next) {
424 struct sdpcodec *c = i->data;
425 struct sipe_backend_codec *codec;
426 GSList *j;
428 codec = sipe_backend_codec_new(c->id,
429 c->name,
430 c->type,
431 c->clock_rate);
433 for (j = c->parameters; j; j = j->next) {
434 struct sipnameval *attr = j->data;
436 sipe_backend_codec_add_optional_parameter(codec,
437 attr->name,
438 attr->value);
441 backend_codecs = g_list_append(backend_codecs, codec);
444 result = sipe_backend_set_remote_codecs(backend_media,
445 backend_stream,
446 backend_codecs);
447 sipe_media_codec_list_free(backend_codecs);
449 if (result == FALSE) {
450 sipe_backend_media_remove_stream(backend_media, backend_stream);
451 return FALSE;
454 for (i = media->candidates; i; i = i->next) {
455 struct sdpcandidate *c = i->data;
456 struct sipe_backend_candidate *candidate;
457 candidate = sipe_backend_candidate_new(c->foundation,
458 c->component,
459 c->type,
460 c->protocol,
461 c->ip,
462 c->port,
463 c->username,
464 c->password);
465 sipe_backend_candidate_set_priority(candidate, c->priority);
467 backend_candidates = g_list_append(backend_candidates, candidate);
470 sipe_backend_media_add_remote_candidates(backend_media,
471 backend_stream,
472 backend_candidates);
473 sipe_media_candidate_list_free(backend_candidates);
475 if (sipe_utils_nameval_find(media->attributes, "inactive")) {
476 sipe_backend_stream_hold(backend_media, backend_stream, FALSE);
477 } else if (sipe_backend_stream_is_held(backend_stream)) {
478 sipe_backend_stream_unhold(backend_media, backend_stream, FALSE);
481 return TRUE;
484 static void
485 apply_remote_message(struct sipe_media_call_private* call_private,
486 struct sdpmsg* msg)
488 GSList *i;
490 g_slist_free_full(call_private->failed_media, (GDestroyNotify)sdpmedia_free);
491 call_private->failed_media = NULL;
493 for (i = msg->media; i; i = i->next) {
494 struct sdpmedia *media = i->data;
495 if (!update_remote_media(call_private, media)) {
496 media->port = 0;
497 call_private->failed_media =
498 g_slist_append(call_private->failed_media, media);
502 /* We need to keep failed medias until response is sent, remove them
503 * from sdpmsg that is to be freed. */
504 for (i = call_private->failed_media; i; i = i->next) {
505 msg->media = g_slist_remove(msg->media, i->data);
508 call_private->encryption_compatible = encryption_levels_compatible(msg);
511 // Sends an invite response when the call is accepted and local candidates were
512 // prepared, otherwise does nothing. If error response is sent, call_private is
513 // disposed before function returns. Returns true when response was sent.
514 static gboolean
515 send_invite_response_if_ready(struct sipe_media_call_private *call_private)
517 struct sipe_backend_media *backend_media;
519 backend_media = call_private->public.backend_private;
521 if (!sipe_backend_media_accepted(backend_media) ||
522 !sipe_backend_candidates_prepared(backend_media))
523 return FALSE;
525 if (!call_private->encryption_compatible) {
526 struct sipe_core_private *sipe_private = call_private->sipe_private;
528 sipmsg_add_header(call_private->invitation, "Warning",
529 "308 lcs.microsoft.com \"Encryption Levels not compatible\"");
530 sip_transport_response(sipe_private,
531 call_private->invitation,
532 488, "Encryption Levels not compatible",
533 NULL);
534 sipe_backend_media_reject(backend_media, FALSE);
535 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
536 _("Unable to establish a call"),
537 _("Encryption settings of peer are incompatible with ours."));
538 } else {
539 send_response_with_session_description(call_private, 200, "OK");
542 return TRUE;
545 static void
546 candidates_prepared_cb(struct sipe_media_call *call,
547 struct sipe_backend_stream *stream)
549 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
550 struct sipe_backend_media *backend_private = call->backend_private;
552 if (sipe_backend_media_is_initiator(backend_private, stream)) {
553 sipe_invite_call(call_private->sipe_private,
554 process_invite_call_response);
555 } else {
556 struct sdpmsg *smsg = call_private->smsg;
557 call_private->smsg = NULL;
559 apply_remote_message(call_private, smsg);
560 send_invite_response_if_ready(call_private);
561 sdpmsg_free(smsg);
565 static void
566 media_end_cb(struct sipe_media_call *call)
568 g_return_if_fail(call);
570 SIPE_MEDIA_CALL_PRIVATE->sipe_private->media_call = NULL;
571 sipe_media_call_free(SIPE_MEDIA_CALL_PRIVATE);
574 static void
575 call_accept_cb(struct sipe_media_call *call, gboolean local)
577 if (local) {
578 send_invite_response_if_ready(SIPE_MEDIA_CALL_PRIVATE);
582 static void
583 call_reject_cb(struct sipe_media_call *call, gboolean local)
585 if (local) {
586 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
587 sip_transport_response(call_private->sipe_private,
588 call_private->invitation,
589 603, "Decline", NULL);
593 static gboolean
594 sipe_media_send_ack(struct sipe_core_private *sipe_private, struct sipmsg *msg,
595 struct transaction *trans);
597 static void call_hold_cb(struct sipe_media_call *call,
598 gboolean local,
599 SIPE_UNUSED_PARAMETER gboolean state)
601 if (local)
602 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE->sipe_private,
603 sipe_media_send_ack);
606 static void call_hangup_cb(struct sipe_media_call *call, gboolean local)
608 if (local) {
609 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
610 struct sip_session *session;
611 session = sipe_session_find_call(call_private->sipe_private,
612 call_private->with);
614 if (session) {
615 sipe_session_close(call_private->sipe_private, session);
620 static void
621 error_cb(struct sipe_media_call *call, gchar *message)
623 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
624 struct sipe_core_private *sipe_private = call_private->sipe_private;
625 gboolean initiator = sipe_backend_media_is_initiator(call->backend_private, NULL);
626 gboolean accepted = sipe_backend_media_accepted(call->backend_private);
628 gchar *title = g_strdup_printf("Call with %s failed", call_private->with);
629 sipe_backend_notify_error(SIPE_CORE_PUBLIC, title, message);
630 g_free(title);
632 if (!initiator && !accepted) {
633 sip_transport_response(sipe_private,
634 call_private->invitation,
635 488, "Not Acceptable Here", NULL);
638 sipe_backend_media_hangup(call->backend_private, initiator || accepted);
641 static struct sipe_media_call_private *
642 sipe_media_call_new(struct sipe_core_private *sipe_private,
643 const gchar* with, gboolean initiator, SipeIceVersion ice_version)
645 struct sipe_media_call_private *call_private = g_new0(struct sipe_media_call_private, 1);
646 gchar *cname;
648 call_private->sipe_private = sipe_private;
650 cname = g_strdup(sipe_private->contact + 1);
651 cname[strlen(cname) - 1] = '\0';
653 call_private->public.backend_private = sipe_backend_media_new(SIPE_CORE_PUBLIC,
654 SIPE_MEDIA_CALL,
655 with,
656 initiator);
657 sipe_backend_media_set_cname(call_private->public.backend_private, cname);
659 call_private->ice_version = ice_version;
660 call_private->encryption_compatible = TRUE;
662 call_private->public.candidates_prepared_cb = candidates_prepared_cb;
663 call_private->public.media_end_cb = media_end_cb;
664 call_private->public.call_accept_cb = call_accept_cb;
665 call_private->public.call_reject_cb = call_reject_cb;
666 call_private->public.call_hold_cb = call_hold_cb;
667 call_private->public.call_hangup_cb = call_hangup_cb;
668 call_private->public.error_cb = error_cb;
670 g_free(cname);
672 return call_private;
675 void sipe_media_hangup(struct sipe_media_call_private *call_private)
677 if (call_private) {
678 sipe_backend_media_hangup(call_private->public.backend_private,
679 FALSE);
683 static void
684 sipe_media_initiate_call(struct sipe_core_private *sipe_private,
685 const char *with, SipeIceVersion ice_version,
686 gboolean with_video)
688 struct sipe_media_call_private *call_private;
689 struct sipe_backend_media *backend_media;
690 struct sipe_backend_media_relays *backend_media_relays;
691 struct sip_session *session;
692 struct sip_dialog *dialog;
694 if (sipe_private->media_call)
695 return;
697 call_private = sipe_media_call_new(sipe_private, with, TRUE, ice_version);
699 session = sipe_session_add_call(sipe_private, with);
700 dialog = sipe_dialog_add(session);
701 dialog->callid = gencallid();
702 dialog->with = g_strdup(session->with);
703 dialog->ourtag = gentag();
705 call_private->with = g_strdup(session->with);
707 backend_media = call_private->public.backend_private;
709 backend_media_relays =
710 sipe_backend_media_relays_convert(sipe_private->media_relays,
711 sipe_private->media_relay_username,
712 sipe_private->media_relay_password);
714 if (!sipe_backend_media_add_stream(backend_media,
715 "audio", with, SIPE_MEDIA_AUDIO,
716 call_private->ice_version, TRUE,
717 backend_media_relays)) {
718 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
719 _("Error occured"),
720 _("Error creating audio stream"));
721 sipe_media_call_free(call_private);
722 sipe_backend_media_relays_free(backend_media_relays);
723 return;
726 if ( with_video
727 && !sipe_backend_media_add_stream(backend_media,
728 "video", with, SIPE_MEDIA_VIDEO,
729 call_private->ice_version, TRUE,
730 backend_media_relays)) {
731 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
732 _("Error occured"),
733 _("Error creating video stream"));
734 sipe_media_call_free(call_private);
735 sipe_backend_media_relays_free(backend_media_relays);
736 return;
739 sipe_private->media_call = call_private;
741 sipe_backend_media_relays_free(backend_media_relays);
743 // Processing continues in candidates_prepared_cb
746 void
747 sipe_core_media_initiate_call(struct sipe_core_public *sipe_public,
748 const char *with,
749 gboolean with_video)
751 sipe_media_initiate_call(SIPE_CORE_PRIVATE, with,
752 SIPE_ICE_RFC_5245, with_video);
755 void sipe_core_media_connect_conference(struct sipe_core_public *sipe_public,
756 struct sipe_chat_session *chat_session)
758 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
759 struct sipe_backend_media_relays *backend_media_relays;
760 struct sip_session *session;
761 struct sip_dialog *dialog;
762 gchar **parts;
763 gchar *av_uri;
765 session = sipe_session_find_chat(sipe_private, chat_session);
767 if (sipe_private->media_call || !session)
768 return;
770 session->is_call = TRUE;
772 parts = g_strsplit(chat_session->id, "app:conf:focus:", 2);
773 av_uri = g_strjoinv("app:conf:audio-video:", parts);
774 g_strfreev(parts);
776 sipe_private->media_call = sipe_media_call_new(sipe_private, av_uri,
777 TRUE, SIPE_ICE_DRAFT_6);
779 session = sipe_session_add_call(sipe_private, av_uri);
780 dialog = sipe_dialog_add(session);
781 dialog->callid = gencallid();
782 dialog->with = g_strdup(session->with);
783 dialog->ourtag = gentag();
785 g_free(av_uri);
787 sipe_private->media_call->with = g_strdup(session->with);
788 sipe_private->media_call->ice_version = SIPE_ICE_DRAFT_6;
790 backend_media_relays =
791 sipe_backend_media_relays_convert(sipe_private->media_relays,
792 sipe_private->media_relay_username,
793 sipe_private->media_relay_password);
795 if (!sipe_backend_media_add_stream(sipe_private->media_call->public.backend_private,
796 "audio", dialog->with,
797 SIPE_MEDIA_AUDIO,
798 SIPE_ICE_DRAFT_6, TRUE,
799 backend_media_relays)) {
800 sipe_backend_notify_error(sipe_public,
801 _("Error occured"),
802 _("Error creating audio stream"));
803 sipe_media_call_free(sipe_private->media_call);
804 sipe_private->media_call = NULL;
807 sipe_backend_media_relays_free(backend_media_relays);
809 // Processing continues in candidates_prepared_cb
812 gboolean sipe_core_media_in_call(struct sipe_core_public *sipe_public)
814 if (sipe_public) {
815 return SIPE_CORE_PRIVATE->media_call != NULL;
817 return FALSE;
820 void sipe_core_media_test_call(struct sipe_core_public *sipe_public)
822 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
823 if (!sipe_private->test_call_bot_uri) {
824 sipe_backend_notify_error(sipe_public,
825 _("Unable to establish a call"),
826 _("Audio Test Service is not available."));
827 return;
830 sipe_core_media_initiate_call(sipe_public,
831 sipe_private->test_call_bot_uri, FALSE);
834 void
835 process_incoming_invite_call(struct sipe_core_private *sipe_private,
836 struct sipmsg *msg)
838 struct sipe_media_call_private *call_private = sipe_private->media_call;
839 struct sipe_backend_media *backend_media;
840 struct sipe_backend_media_relays *backend_media_relays = NULL;
841 struct sdpmsg *smsg;
842 gboolean has_new_media = FALSE;
843 GSList *i;
845 if (call_private && !is_media_session_msg(call_private, msg)) {
846 sip_transport_response(sipe_private, msg, 486, "Busy Here", NULL);
847 return;
850 smsg = sdpmsg_parse_msg(msg->body);
851 if (!smsg) {
852 sip_transport_response(sipe_private, msg,
853 488, "Not Acceptable Here", NULL);
854 sipe_media_hangup(call_private);
855 return;
858 if (!call_private) {
859 gchar *with = parse_from(sipmsg_find_header(msg, "From"));
860 struct sip_session *session;
862 call_private = sipe_media_call_new(sipe_private, with, FALSE, smsg->ice_version);
863 session = sipe_session_add_call(sipe_private, with);
864 sipe_media_dialog_init(session, msg);
866 call_private->with = g_strdup(session->with);
867 sipe_private->media_call = call_private;
868 g_free(with);
871 backend_media = call_private->public.backend_private;
873 if (call_private->invitation)
874 sipmsg_free(call_private->invitation);
875 call_private->invitation = sipmsg_copy(msg);
877 if (smsg->media)
878 backend_media_relays = sipe_backend_media_relays_convert(
879 sipe_private->media_relays,
880 sipe_private->media_relay_username,
881 sipe_private->media_relay_password);
883 // Create any new media streams
884 for (i = smsg->media; i; i = i->next) {
885 struct sdpmedia *media = i->data;
886 gchar *id = media->name;
887 SipeMediaType type;
889 if ( media->port != 0
890 && !sipe_backend_media_get_stream_by_id(backend_media, id)) {
891 gchar *with;
893 if (sipe_strequal(id, "audio"))
894 type = SIPE_MEDIA_AUDIO;
895 else if (sipe_strequal(id, "video"))
896 type = SIPE_MEDIA_VIDEO;
897 else
898 continue;
900 with = parse_from(sipmsg_find_header(msg, "From"));
901 sipe_backend_media_add_stream(backend_media, id, with,
902 type,
903 smsg->ice_version,
904 FALSE,
905 backend_media_relays);
906 has_new_media = TRUE;
907 g_free(with);
911 sipe_backend_media_relays_free(backend_media_relays);
913 if (has_new_media) {
914 sdpmsg_free(call_private->smsg);
915 call_private->smsg = smsg;
916 sip_transport_response(sipe_private, call_private->invitation,
917 180, "Ringing", NULL);
918 // Processing continues in candidates_prepared_cb
919 } else {
920 apply_remote_message(call_private, smsg);
921 send_response_with_session_description(call_private, 200, "OK");
923 sdpmsg_free(smsg);
927 void process_incoming_cancel_call(struct sipe_core_private *sipe_private,
928 struct sipmsg *msg)
930 struct sipe_media_call_private *call_private = sipe_private->media_call;
932 // We respond to the CANCEL request with 200 OK response and
933 // with 487 Request Terminated to the remote INVITE in progress.
934 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
936 if (call_private->invitation) {
937 sip_transport_response(sipe_private, call_private->invitation,
938 487, "Request Terminated", NULL);
941 sipe_media_hangup(call_private);
944 static gboolean
945 sipe_media_send_ack(struct sipe_core_private *sipe_private,
946 struct sipmsg *msg,
947 struct transaction *trans)
949 struct sipe_media_call_private *call_private = sipe_private->media_call;
950 struct sip_session *session;
951 struct sip_dialog *dialog;
952 int tmp_cseq;
954 if (!is_media_session_msg(call_private, msg))
955 return FALSE;
957 session = sipe_session_find_call(sipe_private, call_private->with);
958 dialog = session->dialogs->data;
959 if (!dialog)
960 return FALSE;
962 tmp_cseq = dialog->cseq;
964 dialog->cseq = sip_transaction_cseq(trans) - 1;
965 sip_transport_ack(sipe_private, dialog);
966 dialog->cseq = tmp_cseq;
968 dialog->outgoing_invite = NULL;
970 return TRUE;
973 static gboolean
974 sipe_media_send_final_ack(struct sipe_core_private *sipe_private,
975 struct sipmsg *msg,
976 struct transaction *trans)
978 if (!sipe_media_send_ack(sipe_private, msg, trans))
979 return FALSE;
981 sipe_backend_media_accept(sipe_private->media_call->public.backend_private,
982 FALSE);
984 return TRUE;
987 static void
988 reinvite_on_candidate_pair_cb(struct sipe_core_public *sipe_public)
990 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
991 struct sipe_media_call_private *media_call = sipe_private->media_call;
992 struct sipe_backend_media *backend_media;
993 GSList *streams;
995 if (!media_call)
996 return;
998 backend_media = media_call->public.backend_private;
999 streams = sipe_backend_media_get_streams(backend_media);
1001 for (; streams; streams = streams->next) {
1002 struct sipe_backend_stream *s = streams->data;
1003 GList *remote_candidates = sipe_backend_media_get_active_remote_candidates(backend_media, s);
1004 guint components = g_list_length(remote_candidates);
1006 sipe_media_candidate_list_free(remote_candidates);
1008 // We must have candidates for both (RTP + RTCP) components ready
1009 if (components < 2) {
1010 sipe_schedule_mseconds(sipe_private,
1011 "<+media-reinvite-on-candidate-pair>",
1012 NULL,
1013 500,
1014 (sipe_schedule_action) reinvite_on_candidate_pair_cb,
1015 NULL);
1016 return;
1020 sipe_invite_call(sipe_private, sipe_media_send_final_ack);
1023 static gboolean
1024 process_invite_call_response(struct sipe_core_private *sipe_private,
1025 struct sipmsg *msg,
1026 struct transaction *trans)
1028 const gchar *with;
1029 struct sipe_media_call_private *call_private = sipe_private->media_call;
1030 struct sipe_backend_media *backend_private;
1031 struct sip_session *session;
1032 struct sip_dialog *dialog;
1033 struct sdpmsg *smsg;
1035 if (!is_media_session_msg(call_private, msg))
1036 return FALSE;
1038 session = sipe_session_find_call(sipe_private, call_private->with);
1039 dialog = session->dialogs->data;
1041 backend_private = call_private->public.backend_private;
1042 with = dialog->with;
1044 dialog->outgoing_invite = NULL;
1046 if (msg->response >= 400) {
1047 // Call rejected by remote peer or an error occurred
1048 const gchar *title;
1049 GString *desc = g_string_new("");
1050 gboolean append_responsestr = FALSE;
1052 switch (msg->response) {
1053 case 480: {
1054 title = _("User unavailable");
1056 if (sipmsg_parse_warning(msg, NULL) == 391) {
1057 g_string_append_printf(desc, _("%s does not want to be disturbed"), with);
1058 } else
1059 g_string_append_printf(desc, _("User %s is not available"), with);
1060 break;
1062 case 603:
1063 case 605:
1064 title = _("Call rejected");
1065 g_string_append_printf(desc, _("User %s rejected call"), with);
1066 break;
1067 case 488: {
1068 /* Check for incompatible encryption levels error.
1070 * MS Lync 2010:
1071 * 488 Not Acceptable Here
1072 * ms-client-diagnostics: 52017;reason="Encryption levels dont match"
1074 * older clients (and SIPE itself):
1075 * 488 Encryption Levels not compatible
1077 const gchar *ms_diag = sipmsg_find_header(msg, "ms-client-diagnostics");
1079 if (sipe_strequal(msg->responsestr, "Encryption Levels not compatible") ||
1080 (ms_diag && g_str_has_prefix(ms_diag, "52017;"))) {
1081 title = _("Unable to establish a call");
1082 g_string_append(desc, _("Encryption settings of peer are incompatible with ours."));
1083 break;
1086 if (call_private->ice_version == SIPE_ICE_RFC_5245 &&
1087 sip_transaction_cseq(trans) == 1) {
1088 gchar *with = g_strdup(call_private->with);
1089 gboolean with_video = sipe_backend_media_get_stream_by_id(backend_private, "video") != NULL;
1091 sipe_media_hangup(call_private);
1092 // We might be calling to OC 2007 instance, retry with ICEv6
1093 sipe_media_initiate_call(sipe_private, with,
1094 SIPE_ICE_DRAFT_6, with_video);
1096 g_free(with);
1097 return TRUE;
1099 // Break intentionally omitted
1101 default:
1102 title = _("Error occured");
1103 g_string_append(desc, _("Unable to establish a call"));
1104 append_responsestr = TRUE;
1105 break;
1108 if (append_responsestr)
1109 g_string_append_printf(desc, "\n%d %s",
1110 msg->response, msg->responsestr);
1112 sipe_backend_notify_error(SIPE_CORE_PUBLIC, title, desc->str);
1113 g_string_free(desc, TRUE);
1115 sipe_media_send_ack(sipe_private, msg, trans);
1116 sipe_media_hangup(call_private);
1118 return TRUE;
1121 sipe_dialog_parse(dialog, msg, TRUE);
1122 smsg = sdpmsg_parse_msg(msg->body);
1123 if (!smsg) {
1124 sip_transport_response(sipe_private, msg,
1125 488, "Not Acceptable Here", NULL);
1126 sipe_media_hangup(call_private);
1127 return FALSE;
1130 apply_remote_message(call_private, smsg);
1131 sdpmsg_free(smsg);
1133 sipe_media_send_ack(sipe_private, msg, trans);
1134 reinvite_on_candidate_pair_cb(SIPE_CORE_PUBLIC);
1136 return TRUE;
1139 gboolean is_media_session_msg(struct sipe_media_call_private *call_private,
1140 struct sipmsg *msg)
1142 if (call_private) {
1143 const gchar *callid = sipmsg_find_header(msg, "Call-ID");
1144 struct sip_session *session;
1146 session = sipe_session_find_call(call_private->sipe_private,
1147 call_private->with);
1148 if (session) {
1149 struct sip_dialog *dialog = session->dialogs->data;
1150 return sipe_strequal(dialog->callid, callid);
1153 return FALSE;
1156 void sipe_media_handle_going_offline(struct sipe_media_call_private *call_private)
1158 struct sipe_backend_media *backend_private;
1160 backend_private = call_private->public.backend_private;
1162 if ( !sipe_backend_media_is_initiator(backend_private, NULL)
1163 && !sipe_backend_media_accepted(backend_private)) {
1164 sip_transport_response(call_private->sipe_private,
1165 call_private->invitation,
1166 480, "Temporarily Unavailable", NULL);
1167 } else {
1168 struct sip_session *session;
1170 session = sipe_session_find_call(call_private->sipe_private,
1171 call_private->with);
1172 if (session)
1173 sipe_session_close(call_private->sipe_private, session);
1176 sipe_media_hangup(call_private);
1179 static void
1180 sipe_media_relay_free(struct sipe_media_relay *relay)
1182 g_free(relay->hostname);
1183 if (relay->dns_query)
1184 sipe_backend_dns_query_cancel(relay->dns_query);
1185 g_free(relay);
1188 void
1189 sipe_media_relay_list_free(GSList *list)
1191 for (; list; list = g_slist_delete_link(list, list))
1192 sipe_media_relay_free(list->data);
1195 static void
1196 relay_ip_resolved_cb(struct sipe_media_relay* relay,
1197 const gchar *ip, SIPE_UNUSED_PARAMETER guint port)
1199 gchar *hostname = relay->hostname;
1200 relay->dns_query = NULL;
1202 if (ip && port) {
1203 relay->hostname = g_strdup(ip);
1204 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname, ip);
1205 } else {
1206 relay->hostname = NULL;
1207 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname);
1210 g_free(hostname);
1213 static gboolean
1214 process_get_av_edge_credentials_response(struct sipe_core_private *sipe_private,
1215 struct sipmsg *msg,
1216 SIPE_UNUSED_PARAMETER struct transaction *trans)
1218 g_free(sipe_private->media_relay_username);
1219 g_free(sipe_private->media_relay_password);
1220 sipe_media_relay_list_free(sipe_private->media_relays);
1221 sipe_private->media_relay_username = NULL;
1222 sipe_private->media_relay_password = NULL;
1223 sipe_private->media_relays = NULL;
1225 if (msg->response >= 400) {
1226 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
1227 "Failed to obtain A/V Edge credentials.");
1228 return FALSE;
1231 if (msg->response == 200) {
1232 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
1234 if (sipe_strequal("OK", sipe_xml_attribute(xn_response, "reasonPhrase"))) {
1235 const sipe_xml *xn_credentials = sipe_xml_child(xn_response, "credentialsResponse/credentials");
1236 const sipe_xml *xn_relays = sipe_xml_child(xn_response, "credentialsResponse/mediaRelayList");
1237 const sipe_xml *item;
1238 GSList *relays = NULL;
1240 item = sipe_xml_child(xn_credentials, "username");
1241 sipe_private->media_relay_username = sipe_xml_data(item);
1242 item = sipe_xml_child(xn_credentials, "password");
1243 sipe_private->media_relay_password = sipe_xml_data(item);
1245 for (item = sipe_xml_child(xn_relays, "mediaRelay"); item; item = sipe_xml_twin(item)) {
1246 struct sipe_media_relay *relay = g_new0(struct sipe_media_relay, 1);
1247 const sipe_xml *node;
1248 gchar *tmp;
1250 node = sipe_xml_child(item, "hostName");
1251 relay->hostname = sipe_xml_data(node);
1253 node = sipe_xml_child(item, "udpPort");
1254 relay->udp_port = atoi(tmp = sipe_xml_data(node));
1255 g_free(tmp);
1257 node = sipe_xml_child(item, "tcpPort");
1258 relay->tcp_port = atoi(tmp = sipe_xml_data(node));
1259 g_free(tmp);
1261 relays = g_slist_append(relays, relay);
1263 relay->dns_query = sipe_backend_dns_query_a(
1264 SIPE_CORE_PUBLIC,
1265 relay->hostname,
1266 relay->udp_port,
1267 (sipe_dns_resolved_cb) relay_ip_resolved_cb,
1268 relay);
1270 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
1271 relay->hostname,
1272 relay->tcp_port, relay->udp_port);
1275 sipe_private->media_relays = relays;
1278 sipe_xml_free(xn_response);
1281 return TRUE;
1284 void
1285 sipe_media_get_av_edge_credentials(struct sipe_core_private *sipe_private)
1287 // TODO: re-request credentials after duration expires?
1288 const char CRED_REQUEST_XML[] =
1289 "<request requestID=\"%d\" "
1290 "from=\"%s\" "
1291 "version=\"1.0\" "
1292 "to=\"%s\" "
1293 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
1294 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
1295 "<credentialsRequest credentialsRequestID=\"%d\">"
1296 "<identity>%s</identity>"
1297 "<location>%s</location>"
1298 "<duration>480</duration>"
1299 "</credentialsRequest>"
1300 "</request>";
1302 int request_id = rand();
1303 gchar *self;
1304 gchar *body;
1306 if (!sipe_private->mras_uri)
1307 return;
1309 self = sip_uri_self(sipe_private);
1311 body = g_strdup_printf(
1312 CRED_REQUEST_XML,
1313 request_id,
1314 self,
1315 sipe_private->mras_uri,
1316 request_id,
1317 self,
1318 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER) ? "internet" : "intranet");
1319 g_free(self);
1321 sip_transport_service(sipe_private,
1322 sipe_private->mras_uri,
1323 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
1324 body,
1325 process_get_av_edge_credentials_response);
1327 g_free(body);
1331 Local Variables:
1332 mode: c
1333 c-file-style: "bsd"
1334 indent-tabs-mode: t
1335 tab-width: 8
1336 End: