core: moved repeating code to sipmsg_parse_warning()
[siplcs.git] / src / core / sipe-media.c
blob9222b37e61ca7c5b95043c23afcfd544ad4183b2
1 /**
2 * @file sipe-media.c
4 * pidgin-sipe
6 * Copyright (C) 2010 Jakub Adam <jakub.adam@ktknet.cz>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
27 #include <stdio.h>
28 #include <stdlib.h>
30 #include <glib.h>
32 #include "sipe-common.h"
33 #include "sipmsg.h"
34 #include "sip-transport.h"
35 #include "sipe-backend.h"
36 #include "sdpmsg.h"
37 #include "sipe-chat.h"
38 #include "sipe-core.h"
39 #include "sipe-core-private.h"
40 #include "sipe-dialog.h"
41 #include "sipe-media.h"
42 #include "sipe-session.h"
43 #include "sipe-utils.h"
44 #include "sipe-nls.h"
45 #include "sipe-schedule.h"
46 #include "sipe-xml.h"
48 struct sipe_media_call_private {
49 struct sipe_media_call public;
51 /* private part starts here */
52 struct sipe_core_private *sipe_private;
53 gchar *with;
55 struct sipmsg *invitation;
56 SipeIceVersion ice_version;
57 gboolean encryption_compatible;
59 struct sdpmsg *smsg;
60 GSList *failed_media;
62 #define SIPE_MEDIA_CALL ((struct sipe_media_call *) call_private)
63 #define SIPE_MEDIA_CALL_PRIVATE ((struct sipe_media_call_private *) call)
65 static void sipe_media_codec_list_free(GList *codecs)
67 for (; codecs; codecs = g_list_delete_link(codecs, codecs))
68 sipe_backend_codec_free(codecs->data);
71 static void sipe_media_candidate_list_free(GList *candidates)
73 for (; candidates; candidates = g_list_delete_link(candidates, candidates))
74 sipe_backend_candidate_free(candidates->data);
77 static void
78 sipe_media_call_free(struct sipe_media_call_private *call_private)
80 if (call_private) {
81 struct sip_session *session;
82 sipe_backend_media_free(call_private->public.backend_private);
84 session = sipe_session_find_call(call_private->sipe_private,
85 call_private->with);
86 if (session)
87 sipe_session_remove(call_private->sipe_private, session);
89 if (call_private->invitation)
90 sipmsg_free(call_private->invitation);
92 sdpmsg_free(call_private->smsg);
93 g_slist_free_full(call_private->failed_media,
94 (GDestroyNotify)sdpmedia_free);
95 g_free(call_private->with);
96 g_free(call_private);
100 static GSList *
101 backend_candidates_to_sdpcandidate(GList *candidates)
103 GSList *result = NULL;
104 GList *i;
106 for (i = candidates; i; i = i->next) {
107 struct sipe_backend_candidate *candidate = i->data;
108 struct sdpcandidate *c = g_new(struct sdpcandidate, 1);
110 c->foundation = sipe_backend_candidate_get_foundation(candidate);
111 c->component = sipe_backend_candidate_get_component_type(candidate);
112 c->type = sipe_backend_candidate_get_type(candidate);
113 c->protocol = sipe_backend_candidate_get_protocol(candidate);
114 c->ip = sipe_backend_candidate_get_ip(candidate);
115 c->port = sipe_backend_candidate_get_port(candidate);
116 c->base_ip = sipe_backend_candidate_get_base_ip(candidate);
117 c->base_port = sipe_backend_candidate_get_base_port(candidate);
118 c->priority = sipe_backend_candidate_get_priority(candidate);
119 c->username = sipe_backend_candidate_get_username(candidate);
120 c->password = sipe_backend_candidate_get_password(candidate);
122 result = g_slist_append(result, c);
125 return result;
128 static void
129 get_stream_ip_and_ports(GSList *candidates,
130 gchar **ip, guint *rtp_port, guint *rtcp_port,
131 SipeCandidateType type)
133 *rtp_port = 0;
134 *rtcp_port = 0;
136 for (; candidates; candidates = candidates->next) {
137 struct sdpcandidate *candidate = candidates->data;
139 if (type == SIPE_CANDIDATE_TYPE_ANY || candidate->type == type) {
140 if (candidate->component == SIPE_COMPONENT_RTP) {
141 *rtp_port = candidate->port;
142 *ip = g_strdup(candidate->ip);
143 } else if (candidate->component == SIPE_COMPONENT_RTCP)
144 *rtcp_port = candidate->port;
147 if (*rtp_port != 0 && *rtcp_port != 0)
148 return;
152 static struct sdpmedia *
153 backend_stream_to_sdpmedia(struct sipe_backend_media *backend_media,
154 struct sipe_backend_stream *backend_stream)
156 struct sdpmedia *media = g_new0(struct sdpmedia, 1);
157 GList *codecs = sipe_backend_get_local_codecs(backend_media,
158 backend_stream);
159 guint rtcp_port = 0;
160 SipeMediaType type;
161 GSList *attributes = NULL;
162 GList *candidates;
163 GList *i;
165 media->name = g_strdup(sipe_backend_stream_get_id(backend_stream));
167 if (sipe_strequal(media->name, "audio"))
168 type = SIPE_MEDIA_AUDIO;
169 else if (sipe_strequal(media->name, "video"))
170 type = SIPE_MEDIA_VIDEO;
171 else {
172 // TODO: incompatible media, should not happen here
175 // Process codecs
176 for (i = codecs; i; i = i->next) {
177 struct sipe_backend_codec *codec = i->data;
178 struct sdpcodec *c = g_new0(struct sdpcodec, 1);
179 GList *params;
181 c->id = sipe_backend_codec_get_id(codec);
182 c->name = sipe_backend_codec_get_name(codec);
183 c->clock_rate = sipe_backend_codec_get_clock_rate(codec);
184 c->type = type;
186 params = sipe_backend_codec_get_optional_parameters(codec);
187 for (; params; params = params->next) {
188 struct sipnameval *param = params->data;
189 struct sipnameval *copy = g_new0(struct sipnameval, 1);
191 copy->name = g_strdup(param->name);
192 copy->value = g_strdup(param->value);
194 c->parameters = g_slist_append(c->parameters, copy);
197 media->codecs = g_slist_append(media->codecs, c);
200 sipe_media_codec_list_free(codecs);
202 // Process local candidates
203 // If we have established candidate pairs, send them in SDP response.
204 // Otherwise send all available local candidates.
205 candidates = sipe_backend_media_get_active_local_candidates(backend_media,
206 backend_stream);
207 if (!candidates)
208 candidates = sipe_backend_get_local_candidates(backend_media,
209 backend_stream);
211 media->candidates = backend_candidates_to_sdpcandidate(candidates);
213 sipe_media_candidate_list_free(candidates);
215 get_stream_ip_and_ports(media->candidates, &media->ip, &media->port,
216 &rtcp_port, SIPE_CANDIDATE_TYPE_HOST);
217 // No usable HOST candidates, use any candidate
218 if (media->ip == NULL && media->candidates) {
219 get_stream_ip_and_ports(media->candidates, &media->ip, &media->port,
220 &rtcp_port, SIPE_CANDIDATE_TYPE_ANY);
223 if (sipe_backend_stream_is_held(backend_stream))
224 attributes = sipe_utils_nameval_add(attributes, "inactive", "");
226 if (rtcp_port) {
227 gchar *tmp = g_strdup_printf("%u", rtcp_port);
228 attributes = sipe_utils_nameval_add(attributes, "rtcp", tmp);
229 g_free(tmp);
232 attributes = sipe_utils_nameval_add(attributes, "encryption", "rejected");
234 media->attributes = attributes;
236 // Process remote candidates
237 candidates = sipe_backend_media_get_active_remote_candidates(backend_media,
238 backend_stream);
239 media->remote_candidates = backend_candidates_to_sdpcandidate(candidates);
240 sipe_media_candidate_list_free(candidates);
242 return media;
245 static struct sdpmsg *
246 sipe_media_to_sdpmsg(struct sipe_media_call_private *call_private)
248 struct sipe_backend_media *backend_media = call_private->public.backend_private;
249 struct sdpmsg *msg = g_new0(struct sdpmsg, 1);
250 GSList *streams = sipe_backend_media_get_streams(backend_media);
252 for (; streams; streams = streams->next) {
253 struct sdpmedia *media;
254 media = backend_stream_to_sdpmedia(backend_media, streams->data);
255 msg->media = g_slist_append(msg->media, media);
257 if (msg->ip == NULL)
258 msg->ip = g_strdup(media->ip);
261 msg->media = g_slist_concat(msg->media, call_private->failed_media);
262 call_private->failed_media = NULL;
264 msg->ice_version = call_private->ice_version;
266 return msg;
269 static void
270 sipe_invite_call(struct sipe_core_private *sipe_private, TransCallback tc)
272 gchar *hdr;
273 gchar *contact;
274 gchar *body;
275 struct sipe_media_call_private *call_private = sipe_private->media_call;
276 struct sip_session *session;
277 struct sip_dialog *dialog;
278 struct sdpmsg *msg;
279 gboolean add_2007_fallback = FALSE;
281 session = sipe_session_find_call(sipe_private, call_private->with);
282 dialog = session->dialogs->data;
283 add_2007_fallback = dialog->cseq == 0 && call_private->ice_version == SIPE_ICE_RFC_5245;
285 contact = get_contact(sipe_private);
286 hdr = g_strdup_printf(
287 "Supported: ms-early-media\r\n"
288 "Supported: 100rel\r\n"
289 "ms-keep-alive: UAC;hop-hop=yes\r\n"
290 "Contact: %s\r\n"
291 "Content-Type: %s\r\n",
292 contact,
293 add_2007_fallback ?
294 "multipart/alternative;boundary=\"----=_NextPart_000_001E_01CB4397.0B5EB570\""
295 : "application/sdp");
296 g_free(contact);
298 msg = sipe_media_to_sdpmsg(call_private);
299 body = sdpmsg_to_string(msg);
301 if (add_2007_fallback) {
302 gchar *tmp;
303 tmp = g_strdup_printf(
304 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
305 "Content-Type: application/sdp\r\n"
306 "Content-Transfer-Encoding: 7bit\r\n"
307 "Content-Disposition: session; handling=optional; ms-proxy-2007fallback\r\n"
308 "\r\n"
309 "o=- 0 0 IN IP4 %s\r\n"
310 "s=session\r\n"
311 "c=IN IP4 %s\r\n"
312 "m=audio 0 RTP/AVP\r\n"
313 "\r\n"
314 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
315 "Content-Type: application/sdp\r\n"
316 "Content-Transfer-Encoding: 7bit\r\n"
317 "Content-Disposition: session; handling=optional\r\n"
318 "\r\n"
319 "%s"
320 "\r\n"
321 "------=_NextPart_000_001E_01CB4397.0B5EB570--\r\n",
322 msg->ip, msg->ip, body);
323 g_free(body);
324 body = tmp;
327 sdpmsg_free(msg);
329 dialog->outgoing_invite = sip_transport_invite(sipe_private,
330 hdr,
331 body,
332 dialog,
333 tc);
335 g_free(body);
336 g_free(hdr);
339 static struct sip_dialog *
340 sipe_media_dialog_init(struct sip_session* session, struct sipmsg *msg)
342 gchar *newTag = gentag();
343 const gchar *oldHeader;
344 gchar *newHeader;
345 struct sip_dialog *dialog;
347 oldHeader = sipmsg_find_header(msg, "To");
348 newHeader = g_strdup_printf("%s;tag=%s", oldHeader, newTag);
349 sipmsg_remove_header_now(msg, "To");
350 sipmsg_add_header_now(msg, "To", newHeader);
351 g_free(newHeader);
353 dialog = sipe_dialog_add(session);
354 dialog->callid = g_strdup(sipmsg_find_header(msg, "Call-ID"));
355 dialog->with = parse_from(sipmsg_find_header(msg, "From"));
356 sipe_dialog_parse(dialog, msg, FALSE);
358 return dialog;
361 static void
362 send_response_with_session_description(struct sipe_media_call_private *call_private, int code, gchar *text)
364 struct sdpmsg *msg = sipe_media_to_sdpmsg(call_private);
365 gchar *body = sdpmsg_to_string(msg);
366 sdpmsg_free(msg);
367 sipmsg_add_header(call_private->invitation, "Content-Type", "application/sdp");
368 sip_transport_response(call_private->sipe_private, call_private->invitation, code, text, body);
369 g_free(body);
372 static gboolean
373 encryption_levels_compatible(struct sdpmsg *msg)
375 GSList *i;
377 for (i = msg->media; i; i = i->next) {
378 const gchar *enc_level;
379 struct sdpmedia *m = i->data;
381 enc_level = sipe_utils_nameval_find(m->attributes, "encryption");
383 // Decline call if peer requires encryption as we don't support it yet.
384 if (sipe_strequal(enc_level, "required"))
385 return FALSE;
388 return TRUE;
391 static gboolean
392 process_invite_call_response(struct sipe_core_private *sipe_private,
393 struct sipmsg *msg,
394 struct transaction *trans);
396 static gboolean
397 update_remote_media(struct sipe_media_call_private* call_private,
398 struct sdpmedia *media)
400 struct sipe_backend_media *backend_media = SIPE_MEDIA_CALL->backend_private;
401 struct sipe_backend_stream *backend_stream;
402 GList *backend_candidates = NULL;
403 GList *backend_codecs = NULL;
404 GSList *i;
405 gboolean result = TRUE;
407 backend_stream = sipe_backend_media_get_stream_by_id(backend_media,
408 media->name);
409 if (media->port == 0) {
410 if (backend_stream)
411 sipe_backend_media_remove_stream(backend_media, backend_stream);
412 return TRUE;
415 if (!backend_stream)
416 return FALSE;
418 for (i = media->codecs; i; i = i->next) {
419 struct sdpcodec *c = i->data;
420 struct sipe_backend_codec *codec;
421 GSList *j;
423 codec = sipe_backend_codec_new(c->id,
424 c->name,
425 c->type,
426 c->clock_rate);
428 for (j = c->parameters; j; j = j->next) {
429 struct sipnameval *attr = j->data;
431 sipe_backend_codec_add_optional_parameter(codec,
432 attr->name,
433 attr->value);
436 backend_codecs = g_list_append(backend_codecs, codec);
439 result = sipe_backend_set_remote_codecs(backend_media,
440 backend_stream,
441 backend_codecs);
442 sipe_media_codec_list_free(backend_codecs);
444 if (result == FALSE) {
445 sipe_backend_media_remove_stream(backend_media, backend_stream);
446 return FALSE;
449 for (i = media->candidates; i; i = i->next) {
450 struct sdpcandidate *c = i->data;
451 struct sipe_backend_candidate *candidate;
452 candidate = sipe_backend_candidate_new(c->foundation,
453 c->component,
454 c->type,
455 c->protocol,
456 c->ip,
457 c->port,
458 c->username,
459 c->password);
460 sipe_backend_candidate_set_priority(candidate, c->priority);
462 backend_candidates = g_list_append(backend_candidates, candidate);
465 sipe_backend_media_add_remote_candidates(backend_media,
466 backend_stream,
467 backend_candidates);
468 sipe_media_candidate_list_free(backend_candidates);
470 if (sipe_utils_nameval_find(media->attributes, "inactive")) {
471 sipe_backend_stream_hold(backend_media, backend_stream, FALSE);
472 } else if (sipe_backend_stream_is_held(backend_stream)) {
473 sipe_backend_stream_unhold(backend_media, backend_stream, FALSE);
476 return TRUE;
479 static void
480 apply_remote_message(struct sipe_media_call_private* call_private,
481 struct sdpmsg* msg)
483 GSList *i;
485 g_slist_free_full(call_private->failed_media, (GDestroyNotify)sdpmedia_free);
486 call_private->failed_media = NULL;
488 for (i = msg->media; i; i = i->next) {
489 struct sdpmedia *media = i->data;
490 if (!update_remote_media(call_private, media)) {
491 media->port = 0;
492 call_private->failed_media =
493 g_slist_append(call_private->failed_media, media);
497 /* We need to keep failed medias until response is sent, remove them
498 * from sdpmsg that is to be freed. */
499 for (i = call_private->failed_media; i; i = i->next) {
500 msg->media = g_slist_remove(msg->media, i->data);
503 call_private->encryption_compatible = encryption_levels_compatible(msg);
506 // Sends an invite response when the call is locally accepted and local
507 // candidates were prepared, otherwise does nothing. If error response is sent,
508 // call_private is disposed before function returns. Returns true when response
509 // was sent.
510 static gboolean
511 send_invite_response_if_ready(struct sipe_media_call_private *call_private)
513 struct sipe_backend_media *backend_media;
515 backend_media = call_private->public.backend_private;
517 if (!sipe_backend_media_accepted(backend_media, SIPE_ENDPOINT_LOCAL) ||
518 !sipe_backend_candidates_prepared(backend_media))
519 return FALSE;
521 if (!call_private->encryption_compatible) {
522 sipmsg_add_header(call_private->invitation, "Warning",
523 "308 lcs.microsoft.com \"Encryption Levels not compatible\"");
524 sip_transport_response(call_private->sipe_private,
525 call_private->invitation,
526 488, "Encryption Levels not compatible",
527 NULL);
528 sipe_backend_media_reject(backend_media, FALSE);
529 sipe_backend_notify_error(_("Unable to establish a call"),
530 _("Encryption settings of peer are incompatible with ours."));
531 } else {
532 send_response_with_session_description(call_private, 200, "OK");
535 return TRUE;
538 static void
539 send_invite_if_ready(struct sipe_media_call_private *call_private)
541 struct sipe_backend_media *backend_media;
542 backend_media = call_private->public.backend_private;
544 if (sipe_backend_media_accepted(backend_media, SIPE_ENDPOINT_LOCAL) &&
545 sipe_backend_candidates_prepared(backend_media)) {
546 sipe_invite_call(call_private->sipe_private,
547 process_invite_call_response);
551 static void
552 candidates_prepared_cb(struct sipe_media_call *call,
553 struct sipe_backend_stream *stream)
555 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
556 struct sipe_backend_media *backend_private = call->backend_private;
558 if (sipe_backend_media_is_initiator(backend_private, stream)) {
559 send_invite_if_ready(call_private);
560 } else {
561 struct sdpmsg *smsg = call_private->smsg;
562 call_private->smsg = NULL;
564 apply_remote_message(call_private, smsg);
566 if (!send_invite_response_if_ready(call_private) &&
567 call_private->ice_version == SIPE_ICE_RFC_5245 &&
568 call_private->encryption_compatible) {
569 send_response_with_session_description(call_private,
570 183, "Session Progress");
573 sdpmsg_free(smsg);
577 static void
578 media_end_cb(struct sipe_media_call *call)
580 g_return_if_fail(call);
582 SIPE_MEDIA_CALL_PRIVATE->sipe_private->media_call = NULL;
583 sipe_media_call_free(SIPE_MEDIA_CALL_PRIVATE);
586 static void
587 call_accept_cb(struct sipe_media_call *call, gboolean local)
589 if (local) {
590 struct sipe_backend_media *backend_private = call->backend_private;
592 if (sipe_backend_media_is_initiator(backend_private, NULL))
593 send_invite_if_ready(SIPE_MEDIA_CALL_PRIVATE);
594 else
595 send_invite_response_if_ready(SIPE_MEDIA_CALL_PRIVATE);
599 static void
600 call_reject_cb(struct sipe_media_call *call, gboolean local)
602 if (local && !sipe_utils_is_avconf_uri(SIPE_MEDIA_CALL_PRIVATE->with)) {
603 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
604 sip_transport_response(call_private->sipe_private,
605 call_private->invitation,
606 603, "Decline", NULL);
610 static gboolean
611 sipe_media_send_ack(struct sipe_core_private *sipe_private, struct sipmsg *msg,
612 struct transaction *trans);
614 static void call_hold_cb(struct sipe_media_call *call,
615 gboolean local,
616 SIPE_UNUSED_PARAMETER gboolean state)
618 if (local)
619 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE->sipe_private,
620 sipe_media_send_ack);
623 static void call_hangup_cb(struct sipe_media_call *call, gboolean local)
625 if (local) {
626 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
627 struct sip_session *session;
628 session = sipe_session_find_call(call_private->sipe_private,
629 call_private->with);
631 if (session) {
632 sipe_session_close(call_private->sipe_private, session);
637 static void
638 error_cb(struct sipe_media_call *call, gchar *message)
640 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
641 gboolean initiator = sipe_backend_media_is_initiator(call->backend_private, NULL);
642 gboolean accepted = sipe_backend_media_accepted(call->backend_private,
643 SIPE_ENDPOINT_BOTH);
645 gchar *title = g_strdup_printf("Call with %s failed", call_private->with);
646 sipe_backend_notify_error(title, message);
647 g_free(title);
649 if (!initiator && !accepted) {
650 sip_transport_response(call_private->sipe_private,
651 call_private->invitation,
652 488, "Not Acceptable Here", NULL);
655 sipe_backend_media_hangup(call->backend_private, initiator || accepted);
658 static struct sipe_media_call_private *
659 sipe_media_call_new(struct sipe_core_private *sipe_private,
660 const gchar* with, gboolean initiator, SipeIceVersion ice_version)
662 struct sipe_media_call_private *call_private = g_new0(struct sipe_media_call_private, 1);
663 gchar *cname;
665 call_private->sipe_private = sipe_private;
667 cname = g_strdup(sipe_private->contact + 1);
668 cname[strlen(cname) - 1] = '\0';
670 call_private->public.backend_private = sipe_backend_media_new(SIPE_CORE_PUBLIC,
671 SIPE_MEDIA_CALL,
672 with,
673 initiator);
674 sipe_backend_media_set_cname(call_private->public.backend_private, cname);
676 call_private->ice_version = ice_version;
677 call_private->encryption_compatible = TRUE;
679 call_private->public.candidates_prepared_cb = candidates_prepared_cb;
680 call_private->public.media_end_cb = media_end_cb;
681 call_private->public.call_accept_cb = call_accept_cb;
682 call_private->public.call_reject_cb = call_reject_cb;
683 call_private->public.call_hold_cb = call_hold_cb;
684 call_private->public.call_hangup_cb = call_hangup_cb;
685 call_private->public.error_cb = error_cb;
687 g_free(cname);
689 return call_private;
692 void sipe_media_hangup(struct sipe_media_call_private *call_private)
694 if (call_private) {
695 sipe_backend_media_hangup(call_private->public.backend_private,
696 FALSE);
700 static void
701 sipe_media_initiate_call(struct sipe_core_private *sipe_private,
702 const char *with, SipeIceVersion ice_version,
703 gboolean with_video)
705 struct sipe_media_call_private *call_private;
706 struct sipe_backend_media *backend_media;
707 struct sipe_backend_media_relays *backend_media_relays;
708 struct sip_session *session;
709 struct sip_dialog *dialog;
711 if (sipe_private->media_call)
712 return;
714 call_private = sipe_media_call_new(sipe_private, with, TRUE, ice_version);
716 session = sipe_session_add_call(sipe_private, with);
717 dialog = sipe_dialog_add(session);
718 dialog->callid = gencallid();
719 dialog->with = g_strdup(session->with);
720 dialog->ourtag = gentag();
722 call_private->with = g_strdup(session->with);
724 backend_media = call_private->public.backend_private;
726 backend_media_relays =
727 sipe_backend_media_relays_convert(sipe_private->media_relays,
728 sipe_private->media_relay_username,
729 sipe_private->media_relay_password);
731 if (!sipe_backend_media_add_stream(backend_media,
732 "audio", with, SIPE_MEDIA_AUDIO,
733 call_private->ice_version, TRUE,
734 backend_media_relays)) {
735 sipe_backend_notify_error(_("Error occured"),
736 _("Error creating audio stream"));
737 sipe_media_call_free(call_private);
738 sipe_backend_media_relays_free(backend_media_relays);
739 return;
742 if ( with_video
743 && !sipe_backend_media_add_stream(backend_media,
744 "video", with, SIPE_MEDIA_VIDEO,
745 call_private->ice_version, TRUE,
746 backend_media_relays)) {
747 sipe_backend_notify_error(_("Error occured"),
748 _("Error creating video stream"));
749 sipe_media_call_free(call_private);
750 sipe_backend_media_relays_free(backend_media_relays);
751 return;
754 sipe_private->media_call = call_private;
756 sipe_backend_media_relays_free(backend_media_relays);
758 // Processing continues in candidates_prepared_cb
761 void
762 sipe_core_media_initiate_call(struct sipe_core_public *sipe_public,
763 const char *with,
764 gboolean with_video)
766 sipe_media_initiate_call(SIPE_CORE_PRIVATE, with,
767 SIPE_ICE_RFC_5245, with_video);
770 void sipe_core_media_connect_conference(struct sipe_core_public *sipe_public,
771 struct sipe_chat_session *chat_session,
772 gboolean initiator)
774 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
775 struct sipe_backend_media_relays *backend_media_relays;
776 struct sip_session *session;
777 struct sip_dialog *dialog;
778 gchar **parts;
779 gchar *av_uri;
781 if (sipe_private->media_call)
782 return;
784 parts = g_strsplit(chat_session->id, "app:conf:focus:", 2);
785 av_uri = g_strjoinv("app:conf:audio-video:", parts);
786 g_strfreev(parts);
788 sipe_private->media_call = sipe_media_call_new(sipe_private, av_uri,
789 initiator, SIPE_ICE_DRAFT_6);
791 session = sipe_session_add_call(sipe_private, av_uri);
792 dialog = sipe_dialog_add(session);
793 dialog->callid = gencallid();
794 dialog->with = g_strdup(session->with);
795 dialog->ourtag = gentag();
797 g_free(av_uri);
799 sipe_private->media_call->with = g_strdup(session->with);
800 sipe_private->media_call->ice_version = SIPE_ICE_DRAFT_6;
802 backend_media_relays =
803 sipe_backend_media_relays_convert(sipe_private->media_relays,
804 sipe_private->media_relay_username,
805 sipe_private->media_relay_password);
807 if (!sipe_backend_media_add_stream(sipe_private->media_call->public.backend_private,
808 "audio", dialog->with,
809 SIPE_MEDIA_AUDIO,
810 SIPE_ICE_DRAFT_6, initiator,
811 backend_media_relays)) {
812 sipe_backend_notify_error(_("Error occured"),
813 _("Error creating audio stream"));
814 sipe_media_call_free(sipe_private->media_call);
815 sipe_private->media_call = NULL;
818 sipe_backend_media_relays_free(backend_media_relays);
820 // Processing continues in candidates_prepared_cb
823 gboolean sipe_core_media_in_call(struct sipe_core_public *sipe_public)
825 if (sipe_public) {
826 return SIPE_CORE_PRIVATE->media_call != NULL;
828 return FALSE;
831 void
832 process_incoming_invite_call(struct sipe_core_private *sipe_private,
833 struct sipmsg *msg)
835 struct sipe_media_call_private *call_private = sipe_private->media_call;
836 struct sipe_backend_media *backend_media;
837 struct sipe_backend_media_relays *backend_media_relays = NULL;
838 struct sdpmsg *smsg;
839 gboolean has_new_media = FALSE;
840 GSList *i;
842 if (call_private && !is_media_session_msg(call_private, msg)) {
843 sip_transport_response(sipe_private, msg, 486, "Busy Here", NULL);
844 return;
847 smsg = sdpmsg_parse_msg(msg->body);
848 if (!smsg) {
849 sip_transport_response(sipe_private, msg,
850 488, "Not Acceptable Here", NULL);
851 sipe_media_hangup(call_private);
852 return;
855 if (!call_private) {
856 gchar *with = parse_from(sipmsg_find_header(msg, "From"));
857 struct sip_session *session;
858 struct sip_dialog *dialog;
860 call_private = sipe_media_call_new(sipe_private, with, FALSE, smsg->ice_version);
861 session = sipe_session_add_call(sipe_private, with);
862 dialog = sipe_media_dialog_init(session, msg);
864 call_private->with = g_strdup(session->with);
865 sipe_private->media_call = call_private;
866 g_free(with);
869 backend_media = call_private->public.backend_private;
871 if (call_private->invitation)
872 sipmsg_free(call_private->invitation);
873 call_private->invitation = sipmsg_copy(msg);
875 if (smsg->media)
876 backend_media_relays = sipe_backend_media_relays_convert(
877 sipe_private->media_relays,
878 sipe_private->media_relay_username,
879 sipe_private->media_relay_password);
881 // Create any new media streams
882 for (i = smsg->media; i; i = i->next) {
883 struct sdpmedia *media = i->data;
884 gchar *id = media->name;
885 SipeMediaType type;
887 if ( media->port != 0
888 && !sipe_backend_media_get_stream_by_id(backend_media, id)) {
889 gchar *with;
891 if (sipe_strequal(id, "audio"))
892 type = SIPE_MEDIA_AUDIO;
893 else if (sipe_strequal(id, "video"))
894 type = SIPE_MEDIA_VIDEO;
895 else
896 continue;
898 with = parse_from(sipmsg_find_header(msg, "From"));
899 sipe_backend_media_add_stream(backend_media, id, with,
900 type,
901 smsg->ice_version,
902 FALSE,
903 backend_media_relays);
904 has_new_media = TRUE;
905 g_free(with);
909 sipe_backend_media_relays_free(backend_media_relays);
911 if (has_new_media) {
912 sdpmsg_free(call_private->smsg);
913 call_private->smsg = smsg;
914 sip_transport_response(sipe_private, call_private->invitation,
915 180, "Ringing", NULL);
916 // Processing continues in candidates_prepared_cb
917 } else {
918 apply_remote_message(call_private, smsg);
919 send_response_with_session_description(call_private, 200, "OK");
921 sdpmsg_free(smsg);
925 void process_incoming_cancel_call(struct sipe_core_private *sipe_private,
926 struct sipmsg *msg)
928 struct sipe_media_call_private *call_private = sipe_private->media_call;
930 // We respond to the CANCEL request with 200 OK response and
931 // with 487 Request Terminated to the remote INVITE in progress.
932 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
934 if (call_private->invitation) {
935 sip_transport_response(sipe_private, call_private->invitation,
936 487, "Request Terminated", NULL);
939 sipe_media_hangup(call_private);
942 static gboolean
943 sipe_media_send_ack(struct sipe_core_private *sipe_private,
944 struct sipmsg *msg,
945 struct transaction *trans)
947 struct sipe_media_call_private *call_private = sipe_private->media_call;
948 struct sip_session *session;
949 struct sip_dialog *dialog;
950 int tmp_cseq;
952 if (!is_media_session_msg(call_private, msg))
953 return FALSE;
955 session = sipe_session_find_call(sipe_private, call_private->with);
956 dialog = session->dialogs->data;
957 if (!dialog)
958 return FALSE;
960 tmp_cseq = dialog->cseq;
962 dialog->cseq = sip_transaction_cseq(trans) - 1;
963 sip_transport_ack(sipe_private, dialog);
964 dialog->cseq = tmp_cseq;
966 dialog->outgoing_invite = NULL;
968 return TRUE;
971 static gboolean
972 sipe_media_send_final_ack(struct sipe_core_private *sipe_private,
973 struct sipmsg *msg,
974 struct transaction *trans)
976 if (!sipe_media_send_ack(sipe_private, msg, trans))
977 return FALSE;
979 sipe_backend_media_accept(sipe_private->media_call->public.backend_private,
980 FALSE);
982 return TRUE;
985 static void
986 reinvite_on_candidate_pair_cb(struct sipe_core_public *sipe_public)
988 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
989 struct sipe_media_call_private *media_call = sipe_private->media_call;
990 struct sipe_backend_media *backend_media;
991 GSList *streams;
993 if (!media_call)
994 return;
996 backend_media = media_call->public.backend_private;
997 streams = sipe_backend_media_get_streams(backend_media);
999 for (; streams; streams = streams->next) {
1000 struct sipe_backend_stream *s = streams->data;
1001 GList *remote_candidates = sipe_backend_media_get_active_remote_candidates(backend_media, s);
1002 guint components = g_list_length(remote_candidates);
1004 sipe_media_candidate_list_free(remote_candidates);
1006 // We must have candidates for both (RTP + RTCP) components ready
1007 if (components < 2) {
1008 sipe_schedule_mseconds(sipe_private,
1009 "<+media-reinvite-on-candidate-pair>",
1010 NULL,
1011 500,
1012 (sipe_schedule_action) reinvite_on_candidate_pair_cb,
1013 NULL);
1014 return;
1018 sipe_invite_call(sipe_private, sipe_media_send_final_ack);
1021 static gboolean
1022 process_invite_call_response(struct sipe_core_private *sipe_private,
1023 struct sipmsg *msg,
1024 struct transaction *trans)
1026 const gchar *with;
1027 struct sipe_media_call_private *call_private = sipe_private->media_call;
1028 struct sipe_backend_media *backend_private;
1029 struct sip_session *session;
1030 struct sip_dialog *dialog;
1031 struct sdpmsg *smsg;
1033 if (!is_media_session_msg(call_private, msg))
1034 return FALSE;
1036 session = sipe_session_find_call(sipe_private, call_private->with);
1037 dialog = session->dialogs->data;
1039 backend_private = call_private->public.backend_private;
1040 with = dialog->with;
1042 dialog->outgoing_invite = NULL;
1044 if (msg->response >= 400) {
1045 // Call rejected by remote peer or an error occurred
1046 gchar *title;
1047 GString *desc = g_string_new("");
1048 gboolean append_responsestr = FALSE;
1050 switch (msg->response) {
1051 case 480: {
1052 title = _("User unavailable");
1054 if (sipmsg_parse_warning(msg, NULL) == 391) {
1055 g_string_append_printf(desc, _("%s does not want to be disturbed"), with);
1056 } else
1057 g_string_append_printf(desc, _("User %s is not available"), with);
1058 break;
1060 case 603:
1061 case 605:
1062 title = _("Call rejected");
1063 g_string_append_printf(desc, _("User %s rejected call"), with);
1064 break;
1065 case 488:
1066 if (call_private->ice_version == SIPE_ICE_RFC_5245 &&
1067 sip_transaction_cseq(trans) == 1) {
1068 gchar *with = g_strdup(call_private->with);
1069 gboolean with_video = sipe_backend_media_get_stream_by_id(backend_private, "video") != NULL;
1071 sipe_media_hangup(call_private);
1072 // We might be calling to OC 2007 instance, retry with ICEv6
1073 sipe_media_initiate_call(sipe_private, with,
1074 SIPE_ICE_DRAFT_6, with_video);
1076 g_free(with);
1077 return TRUE;
1079 // Break intentionally omitted
1080 default:
1081 title = _("Error occured");
1082 g_string_append(desc, _("Unable to establish a call"));
1083 append_responsestr = TRUE;
1084 break;
1087 if (append_responsestr)
1088 g_string_append_printf(desc, "\n%d %s",
1089 msg->response, msg->responsestr);
1091 sipe_backend_notify_error(title, desc->str);
1092 g_string_free(desc, TRUE);
1094 sipe_media_send_ack(sipe_private, msg, trans);
1095 sipe_media_hangup(call_private);
1097 return TRUE;
1100 sipe_dialog_parse(dialog, msg, TRUE);
1101 smsg = sdpmsg_parse_msg(msg->body);
1102 if (!smsg) {
1103 sip_transport_response(sipe_private, msg,
1104 488, "Not Acceptable Here", NULL);
1105 sipe_media_hangup(call_private);
1106 return FALSE;
1109 apply_remote_message(call_private, smsg);
1111 if (msg->response == 183) {
1112 // Session in progress
1113 const gchar *rseq = sipmsg_find_header(msg, "RSeq");
1114 const gchar *cseq = sipmsg_find_header(msg, "CSeq");
1115 gchar *rack = g_strdup_printf("RAck: %s %s\r\n", rseq, cseq);
1116 sip_transport_request(sipe_private,
1117 "PRACK",
1118 with,
1119 with,
1120 rack,
1121 NULL,
1122 dialog,
1123 NULL);
1124 g_free(rack);
1125 } else {
1126 sipe_media_send_ack(sipe_private, msg, trans);
1127 reinvite_on_candidate_pair_cb(SIPE_CORE_PUBLIC);
1130 sdpmsg_free(smsg);
1132 return TRUE;
1135 gboolean is_media_session_msg(struct sipe_media_call_private *call_private,
1136 struct sipmsg *msg)
1138 if (call_private) {
1139 const gchar *callid = sipmsg_find_header(msg, "Call-ID");
1140 struct sip_session *session;
1142 session = sipe_session_find_call(call_private->sipe_private,
1143 call_private->with);
1144 if (session) {
1145 struct sip_dialog *dialog = session->dialogs->data;
1146 return sipe_strequal(dialog->callid, callid);
1149 return FALSE;
1152 void sipe_media_handle_going_offline(struct sipe_media_call_private *call_private)
1154 struct sipe_backend_media *backend_private;
1156 backend_private = call_private->public.backend_private;
1158 if ( !sipe_backend_media_is_initiator(backend_private, NULL)
1159 && !sipe_backend_media_accepted(backend_private, SIPE_ENDPOINT_BOTH)) {
1160 sip_transport_response(call_private->sipe_private,
1161 call_private->invitation,
1162 480, "Temporarily Unavailable", NULL);
1163 } else {
1164 struct sip_session *session;
1166 session = sipe_session_find_call(call_private->sipe_private,
1167 call_private->with);
1168 if (session)
1169 sipe_session_close(call_private->sipe_private, session);
1172 sipe_media_hangup(call_private);
1175 static void
1176 sipe_media_relay_free(struct sipe_media_relay *relay)
1178 g_free(relay->hostname);
1179 if (relay->dns_query)
1180 sipe_backend_dns_query_cancel(relay->dns_query);
1181 g_free(relay);
1184 void
1185 sipe_media_relay_list_free(GSList *list)
1187 for (; list; list = g_slist_delete_link(list, list))
1188 sipe_media_relay_free(list->data);
1191 static void
1192 relay_ip_resolved_cb(struct sipe_media_relay* relay,
1193 const gchar *ip, SIPE_UNUSED_PARAMETER guint port)
1195 gchar *hostname = relay->hostname;
1196 relay->dns_query = NULL;
1198 if (ip && port) {
1199 relay->hostname = g_strdup(ip);
1200 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname, ip);
1201 } else {
1202 relay->hostname = NULL;
1203 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname);
1206 g_free(hostname);
1209 static gboolean
1210 process_get_av_edge_credentials_response(struct sipe_core_private *sipe_private,
1211 struct sipmsg *msg,
1212 SIPE_UNUSED_PARAMETER struct transaction *trans)
1214 g_free(sipe_private->media_relay_username);
1215 g_free(sipe_private->media_relay_password);
1216 sipe_media_relay_list_free(sipe_private->media_relays);
1217 sipe_private->media_relay_username = NULL;
1218 sipe_private->media_relay_password = NULL;
1219 sipe_private->media_relays = NULL;
1221 if (msg->response >= 400) {
1222 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
1223 "Failed to obtain A/V Edge credentials.");
1224 return FALSE;
1227 if (msg->response == 200) {
1228 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
1230 if (sipe_strequal("OK", sipe_xml_attribute(xn_response, "reasonPhrase"))) {
1231 const sipe_xml *xn_credentials = sipe_xml_child(xn_response, "credentialsResponse/credentials");
1232 const sipe_xml *xn_relays = sipe_xml_child(xn_response, "credentialsResponse/mediaRelayList");
1233 const sipe_xml *item;
1234 GSList *relays = NULL;
1236 item = sipe_xml_child(xn_credentials, "username");
1237 sipe_private->media_relay_username = g_strdup(sipe_xml_data(item));
1238 item = sipe_xml_child(xn_credentials, "password");
1239 sipe_private->media_relay_password = g_strdup(sipe_xml_data(item));
1241 for (item = sipe_xml_child(xn_relays, "mediaRelay"); item; item = sipe_xml_twin(item)) {
1242 struct sipe_media_relay *relay = g_new0(struct sipe_media_relay, 1);
1243 const sipe_xml *node;
1245 node = sipe_xml_child(item, "hostName");
1246 relay->hostname = g_strdup(sipe_xml_data(node));
1248 node = sipe_xml_child(item, "udpPort");
1249 relay->udp_port = atoi(sipe_xml_data(node));
1251 node = sipe_xml_child(item, "tcpPort");
1252 relay->tcp_port = atoi(sipe_xml_data(node));
1254 relays = g_slist_append(relays, relay);
1256 relay->dns_query = sipe_backend_dns_query_a(
1257 relay->hostname,
1258 relay->udp_port,
1259 (sipe_dns_resolved_cb) relay_ip_resolved_cb,
1260 relay);
1262 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
1263 relay->hostname,
1264 relay->tcp_port, relay->udp_port);
1267 sipe_private->media_relays = relays;
1270 sipe_xml_free(xn_response);
1273 return TRUE;
1276 void
1277 sipe_media_get_av_edge_credentials(struct sipe_core_private *sipe_private)
1279 // TODO: re-request credentials after duration expires?
1280 const char CRED_REQUEST_XML[] =
1281 "<request requestID=\"%d\" "
1282 "from=\"%s\" "
1283 "version=\"1.0\" "
1284 "to=\"%s\" "
1285 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
1286 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
1287 "<credentialsRequest credentialsRequestID=\"%d\">"
1288 "<identity>%s</identity>"
1289 "<location>%s</location>"
1290 "<duration>480</duration>"
1291 "</credentialsRequest>"
1292 "</request>";
1294 int request_id = rand();
1295 gchar *self;
1296 gchar *body;
1298 if (!sipe_private->mras_uri)
1299 return;
1301 self = sip_uri_self(sipe_private);
1303 body = g_strdup_printf(
1304 CRED_REQUEST_XML,
1305 request_id,
1306 self,
1307 sipe_private->mras_uri,
1308 request_id,
1309 self,
1310 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER) ? "internet" : "intranet");
1311 g_free(self);
1313 sip_transport_service(sipe_private,
1314 sipe_private->mras_uri,
1315 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
1316 body,
1317 process_get_av_edge_credentials_response);
1319 g_free(body);
1323 Local Variables:
1324 mode: c
1325 c-file-style: "bsd"
1326 indent-tabs-mode: t
1327 tab-width: 8
1328 End: