Updated media patches README
[siplcs.git] / src / core / sipe-media.c
bloba34e86f529aaa030c8b001381af59b0fcc8814a8
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 accepted and local candidates were
507 // prepared, otherwise does nothing. If error response is sent, call_private is
508 // disposed before function returns. Returns true when response was sent.
509 static gboolean
510 send_invite_response_if_ready(struct sipe_media_call_private *call_private)
512 struct sipe_backend_media *backend_media;
514 backend_media = call_private->public.backend_private;
516 if (!sipe_backend_media_accepted(backend_media) ||
517 !sipe_backend_candidates_prepared(backend_media))
518 return FALSE;
520 if (!call_private->encryption_compatible) {
521 sipmsg_add_header(call_private->invitation, "Warning",
522 "308 lcs.microsoft.com \"Encryption Levels not compatible\"");
523 sip_transport_response(call_private->sipe_private,
524 call_private->invitation,
525 488, "Encryption Levels not compatible",
526 NULL);
527 sipe_backend_media_reject(backend_media, FALSE);
528 sipe_backend_notify_error(_("Unable to establish a call"),
529 _("Encryption settings of peer are incompatible with ours."));
530 } else {
531 send_response_with_session_description(call_private, 200, "OK");
534 return TRUE;
537 static void candidates_prepared_cb(struct sipe_media_call *call,
538 struct sipe_backend_stream *stream)
540 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
542 if (sipe_backend_media_is_initiator(call_private->public.backend_private,
543 stream)) {
544 sipe_invite_call(call_private->sipe_private,
545 process_invite_call_response);
546 } else {
547 struct sdpmsg *smsg = call_private->smsg;
548 call_private->smsg = NULL;
550 apply_remote_message(call_private, smsg);
552 if (!send_invite_response_if_ready(call_private) &&
553 call_private->ice_version == SIPE_ICE_RFC_5245 &&
554 call_private->encryption_compatible) {
555 send_response_with_session_description(call_private,
556 183, "Session Progress");
559 sdpmsg_free(smsg);
563 static void
564 media_end_cb(struct sipe_media_call *call)
566 g_return_if_fail(call);
568 SIPE_MEDIA_CALL_PRIVATE->sipe_private->media_call = NULL;
569 sipe_media_call_free(SIPE_MEDIA_CALL_PRIVATE);
572 static void call_accept_cb(struct sipe_media_call *call, gboolean local)
574 if (local) {
575 send_invite_response_if_ready(SIPE_MEDIA_CALL_PRIVATE);
579 static void call_reject_cb(struct sipe_media_call *call, gboolean local)
581 if (local) {
582 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
583 sip_transport_response(call_private->sipe_private,
584 call_private->invitation,
585 603, "Decline", NULL);
589 static gboolean
590 sipe_media_send_ack(struct sipe_core_private *sipe_private, struct sipmsg *msg,
591 struct transaction *trans);
593 static void call_hold_cb(struct sipe_media_call *call,
594 gboolean local,
595 SIPE_UNUSED_PARAMETER gboolean state)
597 if (local)
598 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE->sipe_private,
599 sipe_media_send_ack);
602 static void call_hangup_cb(struct sipe_media_call *call, gboolean local)
604 if (local) {
605 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
606 struct sip_session *session;
607 session = sipe_session_find_call(call_private->sipe_private,
608 call_private->with);
610 if (session) {
611 sipe_session_close(call_private->sipe_private, session);
616 static void
617 error_cb(struct sipe_media_call *call, gchar *message)
619 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
620 gboolean initiator = sipe_backend_media_is_initiator(call->backend_private, NULL);
621 gboolean accepted = sipe_backend_media_accepted(call->backend_private);
623 gchar *title = g_strdup_printf("Call with %s failed", call_private->with);
624 sipe_backend_notify_error(title, message);
625 g_free(title);
627 if (!initiator && !accepted) {
628 sip_transport_response(call_private->sipe_private,
629 call_private->invitation,
630 488, "Not Acceptable Here", NULL);
633 sipe_backend_media_hangup(call->backend_private, initiator || accepted);
636 static struct sipe_media_call_private *
637 sipe_media_call_new(struct sipe_core_private *sipe_private,
638 const gchar* with, gboolean initiator, SipeIceVersion ice_version)
640 struct sipe_media_call_private *call_private = g_new0(struct sipe_media_call_private, 1);
641 gchar *cname;
643 call_private->sipe_private = sipe_private;
645 cname = g_strdup(sipe_private->contact + 1);
646 cname[strlen(cname) - 1] = '\0';
648 call_private->public.backend_private = sipe_backend_media_new(SIPE_CORE_PUBLIC,
649 SIPE_MEDIA_CALL,
650 with,
651 initiator);
652 sipe_backend_media_set_cname(call_private->public.backend_private, cname);
654 call_private->ice_version = ice_version;
655 call_private->encryption_compatible = TRUE;
657 call_private->public.candidates_prepared_cb = candidates_prepared_cb;
658 call_private->public.media_end_cb = media_end_cb;
659 call_private->public.call_accept_cb = call_accept_cb;
660 call_private->public.call_reject_cb = call_reject_cb;
661 call_private->public.call_hold_cb = call_hold_cb;
662 call_private->public.call_hangup_cb = call_hangup_cb;
663 call_private->public.error_cb = error_cb;
665 g_free(cname);
667 return call_private;
670 void sipe_media_hangup(struct sipe_media_call_private *call_private)
672 if (call_private) {
673 sipe_backend_media_hangup(call_private->public.backend_private,
674 FALSE);
678 static void
679 sipe_media_initiate_call(struct sipe_core_private *sipe_private,
680 const char *with, SipeIceVersion ice_version,
681 gboolean with_video)
683 struct sipe_media_call_private *call_private;
684 struct sipe_backend_media *backend_media;
685 struct sipe_backend_media_relays *backend_media_relays;
686 struct sip_session *session;
687 struct sip_dialog *dialog;
689 if (sipe_private->media_call)
690 return;
692 call_private = sipe_media_call_new(sipe_private, with, TRUE, ice_version);
694 session = sipe_session_add_call(sipe_private, with);
695 dialog = sipe_dialog_add(session);
696 dialog->callid = gencallid();
697 dialog->with = g_strdup(session->with);
698 dialog->ourtag = gentag();
700 call_private->with = g_strdup(session->with);
702 backend_media = call_private->public.backend_private;
704 backend_media_relays =
705 sipe_backend_media_relays_convert(sipe_private->media_relays,
706 sipe_private->media_relay_username,
707 sipe_private->media_relay_password);
709 if (!sipe_backend_media_add_stream(backend_media,
710 "audio", with, SIPE_MEDIA_AUDIO,
711 call_private->ice_version, TRUE,
712 backend_media_relays)) {
713 sipe_backend_notify_error(_("Error occured"),
714 _("Error creating audio stream"));
715 sipe_media_call_free(call_private);
716 sipe_backend_media_relays_free(backend_media_relays);
717 return;
720 if ( with_video
721 && !sipe_backend_media_add_stream(backend_media,
722 "video", with, SIPE_MEDIA_VIDEO,
723 call_private->ice_version, TRUE,
724 backend_media_relays)) {
725 sipe_backend_notify_error(_("Error occured"),
726 _("Error creating video stream"));
727 sipe_media_call_free(call_private);
728 sipe_backend_media_relays_free(backend_media_relays);
729 return;
732 sipe_private->media_call = call_private;
734 sipe_backend_media_relays_free(backend_media_relays);
736 // Processing continues in candidates_prepared_cb
739 void
740 sipe_core_media_initiate_call(struct sipe_core_public *sipe_public,
741 const char *with,
742 gboolean with_video)
744 sipe_media_initiate_call(SIPE_CORE_PRIVATE, with,
745 SIPE_ICE_RFC_5245, with_video);
748 void sipe_core_media_connect_conference(struct sipe_core_public *sipe_public,
749 struct sipe_chat_session *chat_session)
751 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
752 struct sipe_backend_media_relays *backend_media_relays;
753 struct sip_session *session;
754 struct sip_dialog *dialog;
755 gchar **parts;
756 gchar *av_uri;
758 if (sipe_private->media_call)
759 return;
761 parts = g_strsplit(chat_session->id, "app:conf:focus:", 2);
762 av_uri = g_strjoinv("app:conf:audio-video:", parts);
763 g_strfreev(parts);
765 sipe_private->media_call = sipe_media_call_new(sipe_private, av_uri, TRUE, SIPE_ICE_DRAFT_6);
767 session = sipe_session_add_call(sipe_private, av_uri);
768 dialog = sipe_dialog_add(session);
769 dialog->callid = gencallid();
770 dialog->with = g_strdup(session->with);
771 dialog->ourtag = gentag();
773 g_free(av_uri);
775 sipe_private->media_call->with = g_strdup(session->with);
776 sipe_private->media_call->ice_version = SIPE_ICE_DRAFT_6;
778 backend_media_relays =
779 sipe_backend_media_relays_convert(sipe_private->media_relays,
780 sipe_private->media_relay_username,
781 sipe_private->media_relay_password);
783 if (!sipe_backend_media_add_stream(sipe_private->media_call->public.backend_private,
784 "audio", dialog->with,
785 SIPE_MEDIA_AUDIO,
786 SIPE_ICE_DRAFT_6, TRUE,
787 backend_media_relays)) {
788 sipe_backend_notify_error(_("Error occured"),
789 _("Error creating audio stream"));
790 sipe_media_call_free(sipe_private->media_call);
791 sipe_private->media_call = NULL;
794 sipe_backend_media_relays_free(backend_media_relays);
796 // Processing continues in candidates_prepared_cb
799 gboolean sipe_core_media_in_call(struct sipe_core_public *sipe_public)
801 if (sipe_public) {
802 return SIPE_CORE_PRIVATE->media_call != NULL;
804 return FALSE;
807 void
808 process_incoming_invite_call(struct sipe_core_private *sipe_private,
809 struct sipmsg *msg)
811 struct sipe_media_call_private *call_private = sipe_private->media_call;
812 struct sipe_backend_media *backend_media;
813 struct sipe_backend_media_relays *backend_media_relays = NULL;
814 struct sdpmsg *smsg;
815 gboolean has_new_media = FALSE;
816 GSList *i;
818 if (call_private && !is_media_session_msg(call_private, msg)) {
819 sip_transport_response(sipe_private, msg, 486, "Busy Here", NULL);
820 return;
823 smsg = sdpmsg_parse_msg(msg->body);
824 if (!smsg) {
825 sip_transport_response(sipe_private, msg,
826 488, "Not Acceptable Here", NULL);
827 sipe_media_hangup(call_private);
828 return;
831 if (!call_private) {
832 gchar *with = parse_from(sipmsg_find_header(msg, "From"));
833 struct sip_session *session;
834 struct sip_dialog *dialog;
836 call_private = sipe_media_call_new(sipe_private, with, FALSE, smsg->ice_version);
837 session = sipe_session_add_call(sipe_private, with);
838 dialog = sipe_media_dialog_init(session, msg);
840 call_private->with = g_strdup(session->with);
841 sipe_private->media_call = call_private;
842 g_free(with);
845 backend_media = call_private->public.backend_private;
847 if (call_private->invitation)
848 sipmsg_free(call_private->invitation);
849 call_private->invitation = sipmsg_copy(msg);
851 if (smsg->media)
852 backend_media_relays = sipe_backend_media_relays_convert(
853 sipe_private->media_relays,
854 sipe_private->media_relay_username,
855 sipe_private->media_relay_password);
857 // Create any new media streams
858 for (i = smsg->media; i; i = i->next) {
859 struct sdpmedia *media = i->data;
860 gchar *id = media->name;
861 SipeMediaType type;
863 if ( media->port != 0
864 && !sipe_backend_media_get_stream_by_id(backend_media, id)) {
865 gchar *with;
867 if (sipe_strequal(id, "audio"))
868 type = SIPE_MEDIA_AUDIO;
869 else if (sipe_strequal(id, "video"))
870 type = SIPE_MEDIA_VIDEO;
871 else
872 continue;
874 with = parse_from(sipmsg_find_header(msg, "From"));
875 sipe_backend_media_add_stream(backend_media, id, with,
876 type,
877 smsg->ice_version,
878 FALSE,
879 backend_media_relays);
880 has_new_media = TRUE;
881 g_free(with);
885 sipe_backend_media_relays_free(backend_media_relays);
887 if (has_new_media) {
888 sdpmsg_free(call_private->smsg);
889 call_private->smsg = smsg;
890 sip_transport_response(sipe_private, call_private->invitation,
891 180, "Ringing", NULL);
892 // Processing continues in candidates_prepared_cb
893 } else {
894 apply_remote_message(call_private, smsg);
895 send_response_with_session_description(call_private, 200, "OK");
897 sdpmsg_free(smsg);
901 void process_incoming_cancel_call(struct sipe_core_private *sipe_private,
902 struct sipmsg *msg)
904 struct sipe_media_call_private *call_private = sipe_private->media_call;
906 // We respond to the CANCEL request with 200 OK response and
907 // with 487 Request Terminated to the remote INVITE in progress.
908 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
910 if (call_private->invitation) {
911 sip_transport_response(sipe_private, call_private->invitation,
912 487, "Request Terminated", NULL);
915 sipe_media_hangup(call_private);
918 static gboolean
919 sipe_media_send_ack(struct sipe_core_private *sipe_private,
920 struct sipmsg *msg,
921 struct transaction *trans)
923 struct sipe_media_call_private *call_private = sipe_private->media_call;
924 struct sip_session *session;
925 struct sip_dialog *dialog;
926 int tmp_cseq;
928 if (!is_media_session_msg(call_private, msg))
929 return FALSE;
931 session = sipe_session_find_call(sipe_private, call_private->with);
932 dialog = session->dialogs->data;
933 if (!dialog)
934 return FALSE;
936 tmp_cseq = dialog->cseq;
938 dialog->cseq = sip_transaction_cseq(trans) - 1;
939 sip_transport_ack(sipe_private, dialog);
940 dialog->cseq = tmp_cseq;
942 dialog->outgoing_invite = NULL;
944 return TRUE;
947 static gboolean
948 sipe_media_send_final_ack(struct sipe_core_private *sipe_private,
949 struct sipmsg *msg,
950 struct transaction *trans)
952 if (!sipe_media_send_ack(sipe_private, msg, trans))
953 return FALSE;
955 sipe_backend_media_accept(sipe_private->media_call->public.backend_private,
956 FALSE);
958 return TRUE;
961 static void
962 reinvite_on_candidate_pair_cb(struct sipe_core_public *sipe_public)
964 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
965 struct sipe_media_call_private *media_call = sipe_private->media_call;
966 struct sipe_backend_media *backend_media;
967 GSList *streams;
969 if (!media_call)
970 return;
972 backend_media = media_call->public.backend_private;
973 streams = sipe_backend_media_get_streams(backend_media);
975 for (; streams; streams = streams->next) {
976 struct sipe_backend_stream *s = streams->data;
977 GList *remote_candidates = sipe_backend_media_get_active_remote_candidates(backend_media, s);
978 guint components = g_list_length(remote_candidates);
980 sipe_media_candidate_list_free(remote_candidates);
982 // We must have candidates for both (RTP + RTCP) components ready
983 if (components < 2) {
984 sipe_schedule_mseconds(sipe_private,
985 "<+media-reinvite-on-candidate-pair>",
986 NULL,
987 500,
988 (sipe_schedule_action) reinvite_on_candidate_pair_cb,
989 NULL);
990 return;
994 sipe_invite_call(sipe_private, sipe_media_send_final_ack);
997 static gboolean
998 process_invite_call_response(struct sipe_core_private *sipe_private,
999 struct sipmsg *msg,
1000 struct transaction *trans)
1002 const gchar *with;
1003 struct sipe_media_call_private *call_private = sipe_private->media_call;
1004 struct sipe_backend_media *backend_private;
1005 struct sip_session *session;
1006 struct sip_dialog *dialog;
1007 struct sdpmsg *smsg;
1009 if (!is_media_session_msg(call_private, msg))
1010 return FALSE;
1012 session = sipe_session_find_call(sipe_private, call_private->with);
1013 dialog = session->dialogs->data;
1015 backend_private = call_private->public.backend_private;
1016 with = dialog->with;
1018 dialog->outgoing_invite = NULL;
1020 if (msg->response >= 400) {
1021 // Call rejected by remote peer or an error occurred
1022 gchar *title;
1023 GString *desc = g_string_new("");
1024 gboolean append_responsestr = FALSE;
1026 switch (msg->response) {
1027 case 480: {
1028 const gchar *warn = sipmsg_find_header(msg, "Warning");
1029 title = _("User unavailable");
1031 if (warn && g_str_has_prefix(warn, "391 lcs.microsoft.com")) {
1032 g_string_append_printf(desc, _("%s does not want to be disturbed"), with);
1033 } else
1034 g_string_append_printf(desc, _("User %s is not available"), with);
1035 break;
1037 case 603:
1038 case 605:
1039 title = _("Call rejected");
1040 g_string_append_printf(desc, _("User %s rejected call"), with);
1041 break;
1042 case 488:
1043 if (call_private->ice_version == SIPE_ICE_RFC_5245 &&
1044 sip_transaction_cseq(trans) == 1) {
1045 gchar *with = g_strdup(call_private->with);
1046 gboolean with_video = sipe_backend_media_get_stream_by_id(backend_private, "video") != NULL;
1048 sipe_media_hangup(call_private);
1049 // We might be calling to OC 2007 instance, retry with ICEv6
1050 sipe_media_initiate_call(sipe_private, with,
1051 SIPE_ICE_DRAFT_6, with_video);
1053 g_free(with);
1054 return TRUE;
1056 // Break intentionally omitted
1057 default:
1058 title = _("Error occured");
1059 g_string_append(desc, _("Unable to establish a call"));
1060 append_responsestr = TRUE;
1061 break;
1064 if (append_responsestr)
1065 g_string_append_printf(desc, "\n%d %s",
1066 msg->response, msg->responsestr);
1068 sipe_backend_notify_error(title, desc->str);
1069 g_string_free(desc, TRUE);
1071 sipe_media_send_ack(sipe_private, msg, trans);
1072 sipe_media_hangup(call_private);
1074 return TRUE;
1077 sipe_dialog_parse(dialog, msg, TRUE);
1078 smsg = sdpmsg_parse_msg(msg->body);
1079 if (!smsg) {
1080 sip_transport_response(sipe_private, msg,
1081 488, "Not Acceptable Here", NULL);
1082 sipe_media_hangup(call_private);
1083 return FALSE;
1086 apply_remote_message(call_private, smsg);
1088 if (msg->response == 183) {
1089 // Session in progress
1090 const gchar *rseq = sipmsg_find_header(msg, "RSeq");
1091 const gchar *cseq = sipmsg_find_header(msg, "CSeq");
1092 gchar *rack = g_strdup_printf("RAck: %s %s\r\n", rseq, cseq);
1093 sip_transport_request(sipe_private,
1094 "PRACK",
1095 with,
1096 with,
1097 rack,
1098 NULL,
1099 dialog,
1100 NULL);
1101 g_free(rack);
1102 } else {
1103 sipe_media_send_ack(sipe_private, msg, trans);
1104 reinvite_on_candidate_pair_cb(SIPE_CORE_PUBLIC);
1107 sdpmsg_free(smsg);
1109 return TRUE;
1112 gboolean is_media_session_msg(struct sipe_media_call_private *call_private,
1113 struct sipmsg *msg)
1115 if (call_private) {
1116 const gchar *callid = sipmsg_find_header(msg, "Call-ID");
1117 struct sip_session *session;
1119 session = sipe_session_find_call(call_private->sipe_private,
1120 call_private->with);
1121 if (session) {
1122 struct sip_dialog *dialog = session->dialogs->data;
1123 return sipe_strequal(dialog->callid, callid);
1126 return FALSE;
1129 void sipe_media_handle_going_offline(struct sipe_media_call_private *call_private)
1131 struct sipe_backend_media *backend_private;
1133 backend_private = call_private->public.backend_private;
1135 if ( !sipe_backend_media_is_initiator(backend_private, NULL)
1136 && !sipe_backend_media_accepted(backend_private)) {
1137 sip_transport_response(call_private->sipe_private,
1138 call_private->invitation,
1139 480, "Temporarily Unavailable", NULL);
1140 } else {
1141 struct sip_session *session;
1143 session = sipe_session_find_call(call_private->sipe_private,
1144 call_private->with);
1145 if (session)
1146 sipe_session_close(call_private->sipe_private, session);
1149 sipe_media_hangup(call_private);
1152 static void
1153 sipe_media_relay_free(struct sipe_media_relay *relay)
1155 g_free(relay->hostname);
1156 if (relay->dns_query)
1157 sipe_backend_dns_query_cancel(relay->dns_query);
1158 g_free(relay);
1161 void
1162 sipe_media_relay_list_free(GSList *list)
1164 for (; list; list = g_slist_delete_link(list, list))
1165 sipe_media_relay_free(list->data);
1168 static void
1169 relay_ip_resolved_cb(struct sipe_media_relay* relay,
1170 const gchar *ip, SIPE_UNUSED_PARAMETER guint port)
1172 gchar *hostname = relay->hostname;
1173 relay->dns_query = NULL;
1175 if (ip && port) {
1176 relay->hostname = g_strdup(ip);
1177 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname, ip);
1178 } else {
1179 relay->hostname = NULL;
1180 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname);
1183 g_free(hostname);
1186 static gboolean
1187 process_get_av_edge_credentials_response(struct sipe_core_private *sipe_private,
1188 struct sipmsg *msg,
1189 SIPE_UNUSED_PARAMETER struct transaction *trans)
1191 g_free(sipe_private->media_relay_username);
1192 g_free(sipe_private->media_relay_password);
1193 sipe_media_relay_list_free(sipe_private->media_relays);
1194 sipe_private->media_relay_username = NULL;
1195 sipe_private->media_relay_password = NULL;
1196 sipe_private->media_relays = NULL;
1198 if (msg->response >= 400) {
1199 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
1200 "Failed to obtain A/V Edge credentials.");
1201 return FALSE;
1204 if (msg->response == 200) {
1205 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
1207 if (sipe_strequal("OK", sipe_xml_attribute(xn_response, "reasonPhrase"))) {
1208 const sipe_xml *xn_credentials = sipe_xml_child(xn_response, "credentialsResponse/credentials");
1209 const sipe_xml *xn_relays = sipe_xml_child(xn_response, "credentialsResponse/mediaRelayList");
1210 const sipe_xml *item;
1211 GSList *relays = NULL;
1213 item = sipe_xml_child(xn_credentials, "username");
1214 sipe_private->media_relay_username = g_strdup(sipe_xml_data(item));
1215 item = sipe_xml_child(xn_credentials, "password");
1216 sipe_private->media_relay_password = g_strdup(sipe_xml_data(item));
1218 for (item = sipe_xml_child(xn_relays, "mediaRelay"); item; item = sipe_xml_twin(item)) {
1219 struct sipe_media_relay *relay = g_new0(struct sipe_media_relay, 1);
1220 const sipe_xml *node;
1222 node = sipe_xml_child(item, "hostName");
1223 relay->hostname = g_strdup(sipe_xml_data(node));
1225 node = sipe_xml_child(item, "udpPort");
1226 relay->udp_port = atoi(sipe_xml_data(node));
1228 node = sipe_xml_child(item, "tcpPort");
1229 relay->tcp_port = atoi(sipe_xml_data(node));
1231 relays = g_slist_append(relays, relay);
1233 relay->dns_query = sipe_backend_dns_query_a(
1234 relay->hostname,
1235 relay->udp_port,
1236 (sipe_dns_resolved_cb) relay_ip_resolved_cb,
1237 relay);
1239 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
1240 relay->hostname,
1241 relay->tcp_port, relay->udp_port);
1244 sipe_private->media_relays = relays;
1247 sipe_xml_free(xn_response);
1250 return TRUE;
1253 void
1254 sipe_media_get_av_edge_credentials(struct sipe_core_private *sipe_private)
1256 // TODO: re-request credentials after duration expires?
1257 const char CRED_REQUEST_XML[] =
1258 "<request requestID=\"%d\" "
1259 "from=\"%s\" "
1260 "version=\"1.0\" "
1261 "to=\"%s\" "
1262 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
1263 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
1264 "<credentialsRequest credentialsRequestID=\"%d\">"
1265 "<identity>%s</identity>"
1266 "<location>%s</location>"
1267 "<duration>480</duration>"
1268 "</credentialsRequest>"
1269 "</request>";
1271 int request_id = rand();
1272 gchar *self;
1273 gchar *body;
1275 if (!sipe_private->mras_uri)
1276 return;
1278 self = sip_uri_self(sipe_private);
1280 body = g_strdup_printf(
1281 CRED_REQUEST_XML,
1282 request_id,
1283 self,
1284 sipe_private->mras_uri,
1285 request_id,
1286 self,
1287 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER) ? "internet" : "intranet");
1288 g_free(self);
1290 sip_transport_service(sipe_private,
1291 sipe_private->mras_uri,
1292 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
1293 body,
1294 process_get_av_edge_credentials_response);
1296 g_free(body);
1300 Local Variables:
1301 mode: c
1302 c-file-style: "bsd"
1303 indent-tabs-mode: t
1304 tab-width: 8
1305 End: