conference: allow correct dominant speaker detection
[siplcs.git] / src / core / sipe-media.c
blob0fcdba80af6bf76503b10975ee40c274f6f443cd
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;
61 unsigned short medias_initialized;
63 #define SIPE_MEDIA_CALL ((struct sipe_media_call *) call_private)
64 #define SIPE_MEDIA_CALL_PRIVATE ((struct sipe_media_call_private *) call)
66 static void sipe_media_codec_list_free(GList *codecs)
68 for (; codecs; codecs = g_list_delete_link(codecs, codecs))
69 sipe_backend_codec_free(codecs->data);
72 static void sipe_media_candidate_list_free(GList *candidates)
74 for (; candidates; candidates = g_list_delete_link(candidates, candidates))
75 sipe_backend_candidate_free(candidates->data);
78 static void
79 sipe_media_call_free(struct sipe_media_call_private *call_private)
81 if (call_private) {
82 struct sip_session *session;
83 sipe_backend_media_free(call_private->public.backend_private);
84 sipe_backend_media_free(call_private->public.backend_private_legacy);
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_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_backend_media *backend_media, SipeIceVersion ice_version)
248 struct sdpmsg *msg = g_new0(struct sdpmsg, 1);
249 GSList *streams = sipe_backend_media_get_streams(backend_media);
251 for (; streams; streams = streams->next) {
252 struct sdpmedia *media;
253 media = backend_stream_to_sdpmedia(backend_media, streams->data);
254 msg->media = g_slist_append(msg->media, media);
256 if (msg->ip == NULL)
257 msg->ip = g_strdup(media->ip);
260 msg->ice_version = ice_version;
262 return msg;
265 static void
266 sipe_invite_call(struct sipe_core_private *sipe_private, TransCallback tc)
268 gchar *hdr;
269 gchar *contact;
270 gchar *body;
271 struct sipe_media_call_private *call_private = sipe_private->media_call;
272 struct sip_session *session;
273 struct sip_dialog *dialog;
274 struct sdpmsg *msg1;
275 struct sdpmsg *msg2 = NULL;
277 session = sipe_session_find_call(sipe_private, call_private->with);
278 dialog = session->dialogs->data;
280 contact = get_contact(sipe_private);
281 hdr = g_strdup_printf(
282 "Supported: ms-early-media\r\n"
283 "Supported: 100rel\r\n"
284 "ms-keep-alive: UAC;hop-hop=yes\r\n"
285 "Contact: %s\r\n"
286 "Content-Type: %s\r\n",
287 contact,
288 call_private->public.backend_private_legacy ?
289 "multipart/alternative;boundary=\"----=_NextPart_000_001E_01CB4397.0B5EB570\""
290 : "application/sdp");
291 g_free(contact);
293 msg1 = sipe_media_to_sdpmsg(call_private->public.backend_private,
294 call_private->ice_version);
296 if (call_private->public.backend_private_legacy) {
297 gchar *body1 = body = sdpmsg_to_string(msg1);
298 gchar *body2;
300 msg2 = sipe_media_to_sdpmsg(call_private->public.backend_private_legacy,
301 SIPE_ICE_DRAFT_6);
303 body2 = sdpmsg_to_string(msg2);
305 body = g_strdup_printf(
306 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
307 "Content-Type: application/sdp\r\n"
308 "Content-Transfer-Encoding: 7bit\r\n"
309 "Content-Disposition: session; handling=optional; ms-proxy-2007fallback\r\n"
310 "\r\n"
311 "%s"
312 "\r\n"
313 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
314 "Content-Type: application/sdp\r\n"
315 "Content-Transfer-Encoding: 7bit\r\n"
316 "Content-Disposition: session; handling=optional\r\n"
317 "\r\n"
318 "%s"
319 "\r\n"
320 "------=_NextPart_000_001E_01CB4397.0B5EB570--\r\n",
321 body2,
322 body1);
324 g_free(body1);
325 g_free(body2);
326 } else
327 body = sdpmsg_to_string(msg1);
329 sdpmsg_free(msg1);
330 sdpmsg_free(msg2);
332 dialog->outgoing_invite = sip_transport_invite(sipe_private,
333 hdr,
334 body,
335 dialog,
336 tc);
338 g_free(body);
339 g_free(hdr);
342 static struct sip_dialog *
343 sipe_media_dialog_init(struct sip_session* session, struct sipmsg *msg)
345 gchar *newTag = gentag();
346 const gchar *oldHeader;
347 gchar *newHeader;
348 struct sip_dialog *dialog;
350 oldHeader = sipmsg_find_header(msg, "To");
351 newHeader = g_strdup_printf("%s;tag=%s", oldHeader, newTag);
352 sipmsg_remove_header_now(msg, "To");
353 sipmsg_add_header_now(msg, "To", newHeader);
354 g_free(newHeader);
356 dialog = sipe_dialog_add(session);
357 dialog->callid = g_strdup(sipmsg_find_header(msg, "Call-ID"));
358 dialog->with = parse_from(sipmsg_find_header(msg, "From"));
359 sipe_dialog_parse(dialog, msg, FALSE);
361 return dialog;
364 static void
365 send_response_with_session_description(struct sipe_media_call_private *call_private, int code, gchar *text)
367 struct sdpmsg *msg = sipe_media_to_sdpmsg(call_private->public.backend_private,
368 call_private->ice_version);
369 gchar *body = sdpmsg_to_string(msg);
370 sdpmsg_free(msg);
371 sipmsg_add_header(call_private->invitation, "Content-Type", "application/sdp");
372 sip_transport_response(call_private->sipe_private, call_private->invitation, code, text, body);
373 g_free(body);
376 static gboolean
377 encryption_levels_compatible(struct sdpmsg *msg)
379 GSList *i;
381 for (i = msg->media; i; i = i->next) {
382 const gchar *enc_level;
383 struct sdpmedia *m = i->data;
385 enc_level = sipe_utils_nameval_find(m->attributes, "encryption");
387 // Decline call if peer requires encryption as we don't support it yet.
388 if (sipe_strequal(enc_level, "required"))
389 return FALSE;
392 return TRUE;
395 static gboolean
396 process_invite_call_response(struct sipe_core_private *sipe_private,
397 struct sipmsg *msg,
398 struct transaction *trans);
400 static gboolean
401 update_remote_media(struct sipe_media_call_private* call_private,
402 struct sdpmedia *media)
404 struct sipe_backend_media *backend_media = SIPE_MEDIA_CALL->backend_private;
405 struct sipe_backend_stream *backend_stream;
406 GList *backend_candidates = NULL;
407 GList *backend_codecs = NULL;
408 GSList *i;
409 gboolean result = TRUE;
411 backend_stream = sipe_backend_media_get_stream_by_id(backend_media,
412 media->name);
413 if (media->port == 0) {
414 if (backend_stream)
415 sipe_backend_media_remove_stream(backend_media, backend_stream);
416 return TRUE;
419 if (!backend_stream)
420 return FALSE;
423 for (i = media->candidates; i; i = i->next) {
424 struct sdpcandidate *c = i->data;
425 struct sipe_backend_candidate *candidate;
426 candidate = sipe_backend_candidate_new(c->foundation,
427 c->component,
428 c->type,
429 c->protocol,
430 c->ip,
431 c->port,
432 c->username,
433 c->password);
434 sipe_backend_candidate_set_priority(candidate, c->priority);
436 backend_candidates = g_list_append(backend_candidates, candidate);
439 sipe_backend_media_add_remote_candidates(backend_media,
440 backend_stream,
441 backend_candidates);
442 sipe_media_candidate_list_free(backend_candidates);
444 for (i = media->codecs; i; i = i->next) {
445 struct sdpcodec *c = i->data;
446 struct sipe_backend_codec *codec;
447 GSList *j;
449 codec = sipe_backend_codec_new(c->id,
450 c->name,
451 c->type,
452 c->clock_rate);
454 for (j = c->parameters; j; j = j->next) {
455 struct sipnameval *attr = j->data;
457 sipe_backend_codec_add_optional_parameter(codec,
458 attr->name,
459 attr->value);
462 backend_codecs = g_list_append(backend_codecs, codec);
465 result = sipe_backend_set_remote_codecs(backend_media,
466 backend_stream,
467 backend_codecs);
468 sipe_media_codec_list_free(backend_codecs);
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 result;
479 static gboolean
480 apply_remote_message(struct sipe_media_call_private* call_private,
481 struct sdpmsg* msg)
483 GSList *i;
484 for (i = msg->media; i; i = i->next) {
485 if (!update_remote_media(call_private, i->data))
486 return FALSE;
489 call_private->ice_version = msg->ice_version;
490 call_private->encryption_compatible = encryption_levels_compatible(msg);
492 return TRUE;
495 static gboolean
496 do_apply_remote_message(struct sipe_media_call_private *call_private,
497 struct sdpmsg *smsg)
499 if (!apply_remote_message(call_private, smsg)) {
500 sip_transport_response(call_private->sipe_private,
501 call_private->invitation,
502 487, "Request Terminated", NULL);
503 sipe_media_hangup(call_private);
504 return FALSE;
506 return TRUE;
509 // Sends an invite response when the call is accepted and local candidates were
510 // prepared, otherwise does nothing. If error response is sent, call_private is
511 // disposed before function returns. Returns true when response was sent.
512 static gboolean
513 send_invite_response_if_ready(struct sipe_media_call_private *call_private)
515 struct sipe_backend_media *backend_media;
517 backend_media = call_private->public.backend_private;
519 if (!sipe_backend_media_accepted(backend_media) ||
520 !sipe_backend_candidates_prepared(backend_media))
521 return FALSE;
523 if (!call_private->encryption_compatible) {
524 sipmsg_add_header(call_private->invitation, "Warning",
525 "308 lcs.microsoft.com \"Encryption Levels not compatible\"");
526 sip_transport_response(call_private->sipe_private,
527 call_private->invitation,
528 488, "Encryption Levels not compatible",
529 NULL);
530 sipe_backend_media_reject(backend_media, FALSE);
531 sipe_backend_notify_error(_("Unable to establish a call"),
532 _("Encryption settings of peer are incompatible with ours."));
533 } else {
534 send_response_with_session_description(call_private, 200, "OK");
537 return TRUE;
540 static void candidates_prepared_cb(struct sipe_media_call *call,
541 struct sipe_backend_stream *stream)
543 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
545 ++call_private->medias_initialized;
546 if (call->backend_private_legacy && call_private->medias_initialized == 1)
547 return;
549 if (sipe_backend_media_is_initiator(call_private->public.backend_private,
550 stream)) {
551 sipe_invite_call(call_private->sipe_private,
552 process_invite_call_response);
553 } else {
554 struct sdpmsg *smsg = call_private->smsg;
555 call_private->smsg = NULL;
557 if (do_apply_remote_message(call_private, smsg) &&
558 !send_invite_response_if_ready(call_private) &&
559 call_private->ice_version == SIPE_ICE_RFC_5245 &&
560 call_private->encryption_compatible) {
561 send_response_with_session_description(call_private,
562 183, "Session Progress");
565 sdpmsg_free(smsg);
569 static void media_connected_cb(SIPE_UNUSED_PARAMETER struct sipe_media_call_private *call_private)
573 static void call_accept_cb(struct sipe_media_call *call, gboolean local)
575 if (local) {
576 send_invite_response_if_ready(SIPE_MEDIA_CALL_PRIVATE);
580 static void call_reject_cb(struct sipe_media_call *call, gboolean local)
582 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
584 if (local) {
585 sip_transport_response(call_private->sipe_private, call_private->invitation, 603, "Decline", NULL);
587 call_private->sipe_private->media_call = NULL;
588 sipe_media_call_free(call_private);
591 static gboolean
592 sipe_media_send_ack(struct sipe_core_private *sipe_private, struct sipmsg *msg,
593 struct transaction *trans);
595 static void call_hold_cb(struct sipe_media_call *call,
596 gboolean local,
597 SIPE_UNUSED_PARAMETER gboolean state)
599 if (local)
600 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE->sipe_private,
601 sipe_media_send_ack);
604 static void call_hangup_cb(struct sipe_media_call *call,
605 struct sipe_backend_media *backend_media,
606 gboolean local)
608 sipe_backend_media_free(backend_media);
610 if (call->backend_private == backend_media)
611 call->backend_private = NULL;
612 else if (call->backend_private_legacy == backend_media)
613 call->backend_private_legacy = NULL;
615 if (!call->backend_private && !call->backend_private_legacy) {
616 // All backend medias freed, hangup whole media call
617 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
619 if (local) {
620 struct sip_session *session;
621 session = sipe_session_find_call(call_private->sipe_private,
622 call_private->with);
624 if (session) {
625 sipe_session_close(call_private->sipe_private, session);
629 call_private->sipe_private->media_call = NULL;
630 sipe_media_call_free(call_private);
634 static void
635 error_cb(struct sipe_media_call *call, struct sipe_backend_media *backend_media,
636 gchar *message)
638 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
640 if (backend_media == call->backend_private) {
641 gchar *title = g_strdup_printf("Call with %s failed",
642 call_private->with);
643 sipe_backend_notify_error(title, message);
644 g_free(title);
646 call->backend_private = NULL;
647 } else if (backend_media == call->backend_private_legacy) {
648 call->backend_private_legacy = NULL;
651 if (call->backend_private == NULL &&
652 call->backend_private_legacy == NULL &&
653 !sipe_backend_media_is_initiator(backend_media, NULL)) {
654 sip_transport_response(call_private->sipe_private,
655 call_private->invitation,
656 488, "Not Acceptable Here", NULL);
659 sipe_backend_media_hangup(backend_media, FALSE);
662 static struct sipe_media_call_private *
663 sipe_media_call_new(struct sipe_core_private *sipe_private,
664 const gchar* with, gboolean initiator, gboolean with_legacy)
666 struct sipe_media_call_private *call_private = g_new0(struct sipe_media_call_private, 1);
667 gchar *cname;
669 call_private->sipe_private = sipe_private;
671 cname = g_strdup(sipe_private->contact + 1);
672 cname[strlen(cname) - 1] = '\0';
674 call_private->public.backend_private = sipe_backend_media_new(SIPE_CORE_PUBLIC,
675 SIPE_MEDIA_CALL,
676 with,
677 initiator);
678 sipe_backend_media_set_cname(call_private->public.backend_private, cname);
680 if (with_legacy) {
681 call_private->public.backend_private_legacy
682 = sipe_backend_media_new(SIPE_CORE_PUBLIC, SIPE_MEDIA_CALL,
683 with, initiator);
684 sipe_backend_media_set_cname(call_private->public.backend_private_legacy, cname);
687 call_private->ice_version = SIPE_ICE_RFC_5245;
688 call_private->encryption_compatible = TRUE;
690 call_private->public.candidates_prepared_cb = candidates_prepared_cb;
691 call_private->public.media_connected_cb = media_connected_cb;
692 call_private->public.call_accept_cb = call_accept_cb;
693 call_private->public.call_reject_cb = call_reject_cb;
694 call_private->public.call_hold_cb = call_hold_cb;
695 call_private->public.call_hangup_cb = call_hangup_cb;
696 call_private->public.error_cb = error_cb;
698 g_free(cname);
700 return call_private;
703 void sipe_media_hangup(struct sipe_media_call_private *call_private)
705 if (call_private) {
706 // This MUST be freed first
707 sipe_backend_media_hangup(call_private->public.backend_private_legacy,
708 FALSE);
709 sipe_backend_media_hangup(call_private->public.backend_private,
710 FALSE);
714 void
715 sipe_core_media_initiate_call(struct sipe_core_public *sipe_public,
716 const char *with,
717 gboolean with_video)
719 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
720 struct sipe_media_call_private *call_private;
721 struct sipe_backend_media *backend_media;
722 struct sipe_backend_media *backend_media_legacy;
723 struct sipe_backend_media_relays *backend_media_relays;
724 struct sip_session *session;
725 struct sip_dialog *dialog;
727 if (sipe_private->media_call)
728 return;
730 call_private = sipe_media_call_new(sipe_private, with, TRUE, TRUE);
732 session = sipe_session_add_call(sipe_private, with);
733 dialog = sipe_dialog_add(session);
734 dialog->callid = gencallid();
735 dialog->with = g_strdup(session->with);
736 dialog->ourtag = gentag();
738 call_private->with = g_strdup(session->with);
740 backend_media = call_private->public.backend_private;
742 backend_media_relays =
743 sipe_backend_media_relays_convert(sipe_private->media_relays,
744 sipe_private->media_relay_username,
745 sipe_private->media_relay_password);
747 if (!sipe_backend_media_add_stream(backend_media,
748 "audio", with, SIPE_MEDIA_AUDIO,
749 SIPE_ICE_RFC_5245, TRUE,
750 backend_media_relays)) {
751 sipe_backend_notify_error(_("Error occured"),
752 _("Error creating audio stream"));
753 sipe_media_call_free(call_private);
754 sipe_backend_media_relays_free(backend_media_relays);
755 return;
758 if ( with_video
759 && !sipe_backend_media_add_stream(backend_media,
760 "video", with, SIPE_MEDIA_VIDEO,
761 SIPE_ICE_RFC_5245, TRUE,
762 backend_media_relays)) {
763 sipe_backend_notify_error(_("Error occured"),
764 _("Error creating video stream"));
765 sipe_media_call_free(call_private);
766 sipe_backend_media_relays_free(backend_media_relays);
767 return;
770 backend_media_legacy = call_private->public.backend_private_legacy;
772 sipe_backend_media_add_stream(backend_media_legacy,
773 "audio", with, SIPE_MEDIA_AUDIO,
774 SIPE_ICE_DRAFT_6, TRUE,
775 backend_media_relays);
777 if (with_video)
778 sipe_backend_media_add_stream(backend_media_legacy,
779 "video", with, SIPE_MEDIA_VIDEO,
780 SIPE_ICE_DRAFT_6, TRUE,
781 backend_media_relays);
783 sipe_private->media_call = call_private;
785 sipe_backend_media_relays_free(backend_media_relays);
787 // Processing continues in candidates_prepared_cb
790 void sipe_core_media_connect_conference(struct sipe_core_public *sipe_public,
791 struct sipe_chat_session *chat_session)
793 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
794 struct sipe_backend_media_relays *backend_media_relays;
795 struct sip_session *session;
796 struct sip_dialog *dialog;
797 gchar **parts;
798 gchar *av_uri;
800 if (sipe_private->media_call)
801 return;
803 parts = g_strsplit(chat_session->id, "app:conf:focus:", 2);
804 av_uri = g_strjoinv("app:conf:audio-video:", parts);
805 g_strfreev(parts);
807 sipe_private->media_call = sipe_media_call_new(sipe_private, av_uri, TRUE, FALSE);
809 session = sipe_session_add_call(sipe_private, av_uri);
810 dialog = sipe_dialog_add(session);
811 dialog->callid = gencallid();
812 dialog->with = g_strdup(session->with);
813 dialog->ourtag = gentag();
815 g_free(av_uri);
817 sipe_private->media_call->with = g_strdup(session->with);
818 sipe_private->media_call->ice_version = SIPE_ICE_DRAFT_6;
820 backend_media_relays =
821 sipe_backend_media_relays_convert(sipe_private->media_relays,
822 sipe_private->media_relay_username,
823 sipe_private->media_relay_password);
825 if (!sipe_backend_media_add_stream(sipe_private->media_call->public.backend_private,
826 "audio", dialog->with,
827 SIPE_MEDIA_AUDIO,
828 SIPE_ICE_DRAFT_6, TRUE,
829 backend_media_relays)) {
830 sipe_backend_notify_error(_("Error occured"),
831 _("Error creating audio stream"));
832 sipe_media_call_free(sipe_private->media_call);
833 sipe_private->media_call = NULL;
836 sipe_backend_media_relays_free(backend_media_relays);
838 // Processing continues in candidates_prepared_cb
841 gboolean sipe_core_media_in_call(struct sipe_core_public *sipe_public)
843 if (sipe_public) {
844 return SIPE_CORE_PRIVATE->media_call != NULL;
846 return FALSE;
849 void
850 process_incoming_invite_call(struct sipe_core_private *sipe_private,
851 struct sipmsg *msg)
853 struct sipe_media_call_private *call_private = sipe_private->media_call;
854 struct sipe_backend_media *backend_media;
855 struct sipe_backend_media_relays *backend_media_relays = NULL;
856 struct sdpmsg *smsg;
857 gboolean has_new_media = FALSE;
858 GSList *i;
860 if (call_private && !is_media_session_msg(call_private, msg)) {
861 sip_transport_response(sipe_private, msg, 486, "Busy Here", NULL);
862 return;
865 smsg = sdpmsg_parse_msg(msg->body);
866 if (!smsg) {
867 sip_transport_response(sipe_private, msg,
868 488, "Not Acceptable Here", NULL);
869 sipe_media_hangup(call_private);
870 return;
873 if (!call_private) {
874 gchar *with = parse_from(sipmsg_find_header(msg, "From"));
875 struct sip_session *session;
876 struct sip_dialog *dialog;
878 call_private = sipe_media_call_new(sipe_private, with, FALSE, FALSE);
879 session = sipe_session_add_call(sipe_private, with);
880 dialog = sipe_media_dialog_init(session, msg);
882 call_private->with = g_strdup(session->with);
883 sipe_private->media_call = call_private;
884 g_free(with);
887 backend_media = call_private->public.backend_private;
889 if (call_private->invitation)
890 sipmsg_free(call_private->invitation);
891 call_private->invitation = sipmsg_copy(msg);
893 if (smsg->media)
894 backend_media_relays = sipe_backend_media_relays_convert(
895 sipe_private->media_relays,
896 sipe_private->media_relay_username,
897 sipe_private->media_relay_password);
899 // Create any new media streams
900 for (i = smsg->media; i; i = i->next) {
901 struct sdpmedia *media = i->data;
902 gchar *id = media->name;
903 SipeMediaType type;
905 if ( media->port != 0
906 && !sipe_backend_media_get_stream_by_id(backend_media, id)) {
907 gchar *with;
909 if (sipe_strequal(id, "audio"))
910 type = SIPE_MEDIA_AUDIO;
911 else if (sipe_strequal(id, "video"))
912 type = SIPE_MEDIA_VIDEO;
913 else
914 continue;
916 with = parse_from(sipmsg_find_header(msg, "From"));
917 sipe_backend_media_add_stream(backend_media, id, with,
918 type,
919 smsg->ice_version,
920 FALSE,
921 backend_media_relays);
922 has_new_media = TRUE;
923 g_free(with);
927 sipe_backend_media_relays_free(backend_media_relays);
929 if (has_new_media) {
930 sdpmsg_free(call_private->smsg);
931 call_private->smsg = smsg;
932 sip_transport_response(sipe_private, call_private->invitation,
933 180, "Ringing", NULL);
934 // Processing continues in candidates_prepared_cb
935 } else {
936 if (do_apply_remote_message(call_private, smsg))
937 send_response_with_session_description(call_private, 200, "OK");
939 sdpmsg_free(smsg);
943 void process_incoming_cancel_call(struct sipe_core_private *sipe_private,
944 struct sipmsg *msg)
946 struct sipe_media_call_private *call_private = sipe_private->media_call;
948 // We respond to the CANCEL request with 200 OK response and
949 // with 487 Request Terminated to the remote INVITE in progress.
950 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
952 if (call_private->invitation) {
953 sip_transport_response(sipe_private, call_private->invitation,
954 487, "Request Terminated", NULL);
957 sipe_media_hangup(call_private);
960 static gboolean
961 sipe_media_send_ack(struct sipe_core_private *sipe_private,
962 struct sipmsg *msg,
963 struct transaction *trans)
965 struct sipe_media_call_private *call_private = sipe_private->media_call;
966 struct sip_session *session;
967 struct sip_dialog *dialog;
968 int trans_cseq;
969 int tmp_cseq;
971 if (!is_media_session_msg(call_private, msg))
972 return FALSE;
974 session = sipe_session_find_call(sipe_private, call_private->with);
975 dialog = session->dialogs->data;
976 if (!dialog)
977 return FALSE;
979 tmp_cseq = dialog->cseq;
981 sscanf(trans->key, "<%*[a-zA-Z0-9]><%d INVITE>", &trans_cseq);
982 dialog->cseq = trans_cseq - 1;
983 sip_transport_ack(sipe_private, dialog);
984 dialog->cseq = tmp_cseq;
986 dialog->outgoing_invite = NULL;
988 return TRUE;
991 static gboolean
992 sipe_media_send_final_ack(struct sipe_core_private *sipe_private,
993 struct sipmsg *msg,
994 struct transaction *trans)
996 if (!sipe_media_send_ack(sipe_private, msg, trans))
997 return FALSE;
999 sipe_backend_media_accept(sipe_private->media_call->public.backend_private,
1000 FALSE);
1002 return TRUE;
1005 static void
1006 reinvite_on_candidate_pair_cb(struct sipe_core_public *sipe_public)
1008 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1009 struct sipe_media_call_private *media_call = sipe_private->media_call;
1010 struct sipe_backend_media *backend_media;
1011 GSList *streams;
1013 if (!media_call)
1014 return;
1016 backend_media = media_call->public.backend_private;
1017 streams = sipe_backend_media_get_streams(backend_media);
1019 for (; streams; streams = streams->next) {
1020 struct sipe_backend_stream *s = streams->data;
1021 GList *remote_candidates = sipe_backend_media_get_active_remote_candidates(backend_media, s);
1022 if (remote_candidates) {
1023 sipe_media_candidate_list_free(remote_candidates);
1024 continue;
1027 sipe_schedule_mseconds(sipe_private,
1028 "<+media-reinvite-on-candidate-pair>",
1029 NULL,
1030 500,
1031 (sipe_schedule_action) reinvite_on_candidate_pair_cb,
1032 NULL);
1033 return;
1036 sipe_invite_call(sipe_private, sipe_media_send_final_ack);
1039 static gboolean
1040 process_invite_call_response(struct sipe_core_private *sipe_private,
1041 struct sipmsg *msg,
1042 struct transaction *trans)
1044 const gchar *with;
1045 struct sipe_media_call_private *call_private = sipe_private->media_call;
1046 struct sipe_backend_media *backend_private;
1047 struct sip_session *session;
1048 struct sip_dialog *dialog;
1049 struct sdpmsg *smsg;
1051 if (!is_media_session_msg(call_private, msg))
1052 return FALSE;
1054 session = sipe_session_find_call(sipe_private, call_private->with);
1055 dialog = session->dialogs->data;
1057 backend_private = call_private->public.backend_private;
1058 with = dialog->with;
1060 dialog->outgoing_invite = NULL;
1062 if (msg->response >= 400) {
1063 // Call rejected by remote peer or an error occurred
1064 gchar *title;
1065 GString *desc = g_string_new("");
1066 gboolean append_responsestr = FALSE;
1068 switch (msg->response) {
1069 case 480: {
1070 const gchar *warn = sipmsg_find_header(msg, "Warning");
1071 title = _("User unavailable");
1073 if (warn && g_str_has_prefix(warn, "391 lcs.microsoft.com")) {
1074 g_string_append_printf(desc, _("%s does not want to be disturbed"), with);
1075 } else
1076 g_string_append_printf(desc, _("User %s is not available"), with);
1077 break;
1079 case 603:
1080 case 605:
1081 title = _("Call rejected");
1082 g_string_append_printf(desc, _("User %s rejected call"), with);
1083 break;
1084 default:
1085 title = _("Error occured");
1086 g_string_append(desc, _("Unable to establish a call"));
1087 append_responsestr = TRUE;
1088 break;
1091 if (append_responsestr)
1092 g_string_append_printf(desc, "\n%d %s",
1093 msg->response, msg->responsestr);
1095 sipe_backend_notify_error(title, desc->str);
1096 g_string_free(desc, TRUE);
1098 sipe_media_send_ack(sipe_private, msg, trans);
1099 sipe_media_hangup(call_private);
1101 return TRUE;
1104 sipe_dialog_parse(dialog, msg, TRUE);
1105 smsg = sdpmsg_parse_msg(msg->body);
1106 if (!smsg) {
1107 sip_transport_response(sipe_private, msg,
1108 488, "Not Acceptable Here", NULL);
1109 sipe_media_hangup(call_private);
1110 return FALSE;
1113 if (call_private->public.backend_private_legacy) {
1114 if (smsg->ice_version == SIPE_ICE_RFC_5245) {
1115 sipe_backend_media_hangup(call_private->public.backend_private_legacy, FALSE);
1116 } else {
1117 sipe_backend_media_hangup(call_private->public.backend_private, FALSE);
1118 call_private->public.backend_private = call_private->public.backend_private_legacy;
1121 call_private->public.backend_private_legacy = NULL;
1124 if (!apply_remote_message(call_private, smsg)) {
1125 sip_transport_response(sipe_private, msg,
1126 487, "Request Terminated", NULL);
1127 sipe_media_hangup(call_private);
1128 } else if (msg->response == 183) {
1129 // Session in progress
1130 const gchar *rseq = sipmsg_find_header(msg, "RSeq");
1131 const gchar *cseq = sipmsg_find_header(msg, "CSeq");
1132 gchar *rack = g_strdup_printf("RAck: %s %s\r\n", rseq, cseq);
1133 sip_transport_request(sipe_private,
1134 "PRACK",
1135 with,
1136 with,
1137 rack,
1138 NULL,
1139 dialog,
1140 NULL);
1141 g_free(rack);
1142 } else {
1143 sipe_media_send_ack(sipe_private, msg, trans);
1144 reinvite_on_candidate_pair_cb(SIPE_CORE_PUBLIC);
1147 sdpmsg_free(smsg);
1149 return TRUE;
1152 gboolean is_media_session_msg(struct sipe_media_call_private *call_private,
1153 struct sipmsg *msg)
1155 if (call_private) {
1156 const gchar *callid = sipmsg_find_header(msg, "Call-ID");
1157 struct sip_session *session;
1159 session = sipe_session_find_call(call_private->sipe_private,
1160 call_private->with);
1161 if (session) {
1162 struct sip_dialog *dialog = session->dialogs->data;
1163 return sipe_strequal(dialog->callid, callid);
1166 return FALSE;
1169 void sipe_media_handle_going_offline(struct sipe_media_call_private *call_private)
1171 struct sipe_backend_media *backend_private;
1173 backend_private = call_private->public.backend_private;
1175 if ( !sipe_backend_media_is_initiator(backend_private, NULL)
1176 && !sipe_backend_media_accepted(backend_private)) {
1177 sip_transport_response(call_private->sipe_private,
1178 call_private->invitation,
1179 480, "Temporarily Unavailable", NULL);
1180 } else {
1181 struct sip_session *session;
1183 session = sipe_session_find_call(call_private->sipe_private,
1184 call_private->with);
1185 if (session)
1186 sipe_session_close(call_private->sipe_private, session);
1189 sipe_media_hangup(call_private);
1192 static void
1193 sipe_media_relay_free(struct sipe_media_relay *relay)
1195 g_free(relay->hostname);
1196 if (relay->dns_query)
1197 sipe_backend_dns_query_cancel(relay->dns_query);
1198 g_free(relay);
1201 void
1202 sipe_media_relay_list_free(GSList *list)
1204 for (; list; list = g_slist_delete_link(list, list))
1205 sipe_media_relay_free(list->data);
1208 static void
1209 relay_ip_resolved_cb(struct sipe_media_relay* relay,
1210 const gchar *ip, SIPE_UNUSED_PARAMETER guint port)
1212 gchar *hostname = relay->hostname;
1213 relay->dns_query = NULL;
1215 if (ip && port) {
1216 relay->hostname = g_strdup(ip);
1217 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname, ip);
1218 } else {
1219 relay->hostname = NULL;
1220 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname);
1223 g_free(hostname);
1226 static gboolean
1227 process_get_av_edge_credentials_response(struct sipe_core_private *sipe_private,
1228 struct sipmsg *msg,
1229 SIPE_UNUSED_PARAMETER struct transaction *trans)
1231 g_free(sipe_private->media_relay_username);
1232 g_free(sipe_private->media_relay_password);
1233 sipe_media_relay_list_free(sipe_private->media_relays);
1234 sipe_private->media_relay_username = NULL;
1235 sipe_private->media_relay_password = NULL;
1236 sipe_private->media_relays = NULL;
1238 if (msg->response >= 400) {
1239 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
1240 "Failed to obtain A/V Edge credentials.");
1241 return FALSE;
1244 if (msg->response == 200) {
1245 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
1247 if (sipe_strequal("OK", sipe_xml_attribute(xn_response, "reasonPhrase"))) {
1248 const sipe_xml *xn_credentials = sipe_xml_child(xn_response, "credentialsResponse/credentials");
1249 const sipe_xml *xn_relays = sipe_xml_child(xn_response, "credentialsResponse/mediaRelayList");
1250 const sipe_xml *item;
1251 GSList *relays = NULL;
1253 item = sipe_xml_child(xn_credentials, "username");
1254 sipe_private->media_relay_username = g_strdup(sipe_xml_data(item));
1255 item = sipe_xml_child(xn_credentials, "password");
1256 sipe_private->media_relay_password = g_strdup(sipe_xml_data(item));
1258 for (item = sipe_xml_child(xn_relays, "mediaRelay"); item; item = sipe_xml_twin(item)) {
1259 struct sipe_media_relay *relay = g_new0(struct sipe_media_relay, 1);
1260 const sipe_xml *node;
1262 node = sipe_xml_child(item, "hostName");
1263 relay->hostname = g_strdup(sipe_xml_data(node));
1265 node = sipe_xml_child(item, "udpPort");
1266 relay->udp_port = atoi(sipe_xml_data(node));
1268 node = sipe_xml_child(item, "tcpPort");
1269 relay->tcp_port = atoi(sipe_xml_data(node));
1271 relays = g_slist_append(relays, relay);
1273 relay->dns_query = sipe_backend_dns_query_a(
1274 relay->hostname,
1275 relay->udp_port,
1276 (sipe_dns_resolved_cb) relay_ip_resolved_cb,
1277 relay);
1279 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
1280 relay->hostname,
1281 relay->tcp_port, relay->udp_port);
1284 sipe_private->media_relays = relays;
1287 sipe_xml_free(xn_response);
1290 return TRUE;
1293 void
1294 sipe_media_get_av_edge_credentials(struct sipe_core_private *sipe_private)
1296 // TODO: re-request credentials after duration expires?
1297 const char CRED_REQUEST_XML[] =
1298 "<request requestID=\"%d\" "
1299 "from=\"%s\" "
1300 "version=\"1.0\" "
1301 "to=\"%s\" "
1302 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
1303 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
1304 "<credentialsRequest credentialsRequestID=\"%d\">"
1305 "<identity>%s</identity>"
1306 "<location>%s</location>"
1307 "<duration>480</duration>"
1308 "</credentialsRequest>"
1309 "</request>";
1311 int request_id = rand();
1312 gchar *self;
1313 gchar *body;
1315 if (!sipe_private->mras_uri)
1316 return;
1318 self = sip_uri_self(sipe_private);
1320 body = g_strdup_printf(
1321 CRED_REQUEST_XML,
1322 request_id,
1323 self,
1324 sipe_private->mras_uri,
1325 request_id,
1326 self,
1327 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER) ? "internet" : "intranet");
1328 g_free(self);
1330 sip_transport_service(sipe_private,
1331 sipe_private->mras_uri,
1332 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
1333 body,
1334 process_get_av_edge_credentials_response);
1336 g_free(body);
1340 Local Variables:
1341 mode: c
1342 c-file-style: "bsd"
1343 indent-tabs-mode: t
1344 tab-width: 8
1345 End: