media: update comments
[siplcs.git] / src / core / sipe-media.c
blob5675eabaa705fe794b7c92f60e26377e02e3bf20
1 /**
2 * @file sipe-media.c
4 * pidgin-sipe
6 * Copyright (C) 2011-13 SIPE Project <http://sipe.sourceforge.net/>
7 * Copyright (C) 2010 Jakub Adam <jakub.adam@ktknet.cz>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
32 #include <glib.h>
34 #include "sipe-common.h"
35 #include "sipmsg.h"
36 #include "sip-transport.h"
37 #include "sipe-backend.h"
38 #include "sdpmsg.h"
39 #include "sipe-chat.h"
40 #include "sipe-core.h"
41 #include "sipe-core-private.h"
42 #include "sipe-dialog.h"
43 #include "sipe-media.h"
44 #include "sipe-ocs2007.h"
45 #include "sipe-session.h"
46 #include "sipe-utils.h"
47 #include "sipe-nls.h"
48 #include "sipe-schedule.h"
49 #include "sipe-xml.h"
51 struct sipe_media_call_private {
52 struct sipe_media_call public;
54 /* private part starts here */
55 struct sipe_core_private *sipe_private;
56 gchar *with;
58 struct sipmsg *invitation;
59 SipeIceVersion ice_version;
60 gboolean encryption_compatible;
62 struct sdpmsg *smsg;
63 GSList *failed_media;
65 #define SIPE_MEDIA_CALL ((struct sipe_media_call *) call_private)
66 #define SIPE_MEDIA_CALL_PRIVATE ((struct sipe_media_call_private *) call)
68 static void sipe_media_codec_list_free(GList *codecs)
70 for (; codecs; codecs = g_list_delete_link(codecs, codecs))
71 sipe_backend_codec_free(codecs->data);
74 static void sipe_media_candidate_list_free(GList *candidates)
76 for (; candidates; candidates = g_list_delete_link(candidates, candidates))
77 sipe_backend_candidate_free(candidates->data);
80 static void
81 sipe_media_call_free(struct sipe_media_call_private *call_private)
83 if (call_private) {
84 struct sip_session *session;
85 sipe_backend_media_free(call_private->public.backend_private);
87 session = sipe_session_find_call(call_private->sipe_private,
88 call_private->with);
89 if (session)
90 sipe_session_remove(call_private->sipe_private, session);
92 if (call_private->invitation)
93 sipmsg_free(call_private->invitation);
95 sdpmsg_free(call_private->smsg);
96 sipe_utils_slist_free_full(call_private->failed_media,
97 (GDestroyNotify)sdpmedia_free);
98 g_free(call_private->with);
99 g_free(call_private);
103 static GSList *
104 backend_candidates_to_sdpcandidate(GList *candidates)
106 GSList *result = NULL;
107 GList *i;
109 for (i = candidates; i; i = i->next) {
110 struct sipe_backend_candidate *candidate = i->data;
111 struct sdpcandidate *c = g_new(struct sdpcandidate, 1);
113 c->foundation = sipe_backend_candidate_get_foundation(candidate);
114 c->component = sipe_backend_candidate_get_component_type(candidate);
115 c->type = sipe_backend_candidate_get_type(candidate);
116 c->protocol = sipe_backend_candidate_get_protocol(candidate);
117 c->ip = sipe_backend_candidate_get_ip(candidate);
118 c->port = sipe_backend_candidate_get_port(candidate);
119 c->base_ip = sipe_backend_candidate_get_base_ip(candidate);
120 c->base_port = sipe_backend_candidate_get_base_port(candidate);
121 c->priority = sipe_backend_candidate_get_priority(candidate);
122 c->username = sipe_backend_candidate_get_username(candidate);
123 c->password = sipe_backend_candidate_get_password(candidate);
125 result = g_slist_append(result, c);
128 return result;
131 static void
132 get_stream_ip_and_ports(GSList *candidates,
133 gchar **ip, guint *rtp_port, guint *rtcp_port,
134 SipeCandidateType type)
136 *rtp_port = 0;
137 *rtcp_port = 0;
139 for (; candidates; candidates = candidates->next) {
140 struct sdpcandidate *candidate = candidates->data;
142 if (type == SIPE_CANDIDATE_TYPE_ANY || candidate->type == type) {
143 if (candidate->component == SIPE_COMPONENT_RTP) {
144 *rtp_port = candidate->port;
145 *ip = g_strdup(candidate->ip);
146 } else if (candidate->component == SIPE_COMPONENT_RTCP)
147 *rtcp_port = candidate->port;
150 if (*rtp_port != 0 && *rtcp_port != 0)
151 return;
155 static struct sdpmedia *
156 backend_stream_to_sdpmedia(struct sipe_backend_media *backend_media,
157 struct sipe_backend_stream *backend_stream)
159 struct sdpmedia *media = g_new0(struct sdpmedia, 1);
160 GList *codecs = sipe_backend_get_local_codecs(backend_media,
161 backend_stream);
162 guint rtcp_port = 0;
163 SipeMediaType type;
164 GSList *attributes = NULL;
165 GList *candidates;
166 GList *i;
168 media->name = g_strdup(sipe_backend_stream_get_id(backend_stream));
170 if (sipe_strequal(media->name, "audio"))
171 type = SIPE_MEDIA_AUDIO;
172 else if (sipe_strequal(media->name, "video"))
173 type = SIPE_MEDIA_VIDEO;
174 else {
175 // TODO: incompatible media, should not happen here
176 g_free(media->name);
177 g_free(media);
178 sipe_media_codec_list_free(codecs);
179 return(NULL);
182 // Process codecs
183 for (i = codecs; i; i = i->next) {
184 struct sipe_backend_codec *codec = i->data;
185 struct sdpcodec *c = g_new0(struct sdpcodec, 1);
186 GList *params;
188 c->id = sipe_backend_codec_get_id(codec);
189 c->name = sipe_backend_codec_get_name(codec);
190 c->clock_rate = sipe_backend_codec_get_clock_rate(codec);
191 c->type = type;
193 params = sipe_backend_codec_get_optional_parameters(codec);
194 for (; params; params = params->next) {
195 struct sipnameval *param = params->data;
196 struct sipnameval *copy = g_new0(struct sipnameval, 1);
198 copy->name = g_strdup(param->name);
199 copy->value = g_strdup(param->value);
201 c->parameters = g_slist_append(c->parameters, copy);
204 media->codecs = g_slist_append(media->codecs, c);
207 sipe_media_codec_list_free(codecs);
209 // Process local candidates
210 // If we have established candidate pairs, send them in SDP response.
211 // Otherwise send all available local candidates.
212 candidates = sipe_backend_media_get_active_local_candidates(backend_media,
213 backend_stream);
214 if (!candidates)
215 candidates = sipe_backend_get_local_candidates(backend_media,
216 backend_stream);
218 media->candidates = backend_candidates_to_sdpcandidate(candidates);
220 sipe_media_candidate_list_free(candidates);
222 get_stream_ip_and_ports(media->candidates, &media->ip, &media->port,
223 &rtcp_port, SIPE_CANDIDATE_TYPE_HOST);
224 // No usable HOST candidates, use any candidate
225 if (media->ip == NULL && media->candidates) {
226 get_stream_ip_and_ports(media->candidates, &media->ip, &media->port,
227 &rtcp_port, SIPE_CANDIDATE_TYPE_ANY);
230 if (sipe_backend_stream_is_held(backend_stream))
231 attributes = sipe_utils_nameval_add(attributes, "inactive", "");
233 if (rtcp_port) {
234 gchar *tmp = g_strdup_printf("%u", rtcp_port);
235 attributes = sipe_utils_nameval_add(attributes, "rtcp", tmp);
236 g_free(tmp);
239 attributes = sipe_utils_nameval_add(attributes, "encryption", "rejected");
241 media->attributes = attributes;
243 // Process remote candidates
244 candidates = sipe_backend_media_get_active_remote_candidates(backend_media,
245 backend_stream);
246 media->remote_candidates = backend_candidates_to_sdpcandidate(candidates);
247 sipe_media_candidate_list_free(candidates);
249 return media;
252 static struct sdpmsg *
253 sipe_media_to_sdpmsg(struct sipe_media_call_private *call_private)
255 struct sipe_backend_media *backend_media = call_private->public.backend_private;
256 struct sdpmsg *msg = g_new0(struct sdpmsg, 1);
257 GSList *streams = sipe_backend_media_get_streams(backend_media);
259 for (; streams; streams = streams->next) {
260 struct sdpmedia *media = backend_stream_to_sdpmedia(backend_media, streams->data);
261 if (media) {
262 msg->media = g_slist_append(msg->media, media);
264 if (msg->ip == NULL)
265 msg->ip = g_strdup(media->ip);
269 msg->media = g_slist_concat(msg->media, call_private->failed_media);
270 call_private->failed_media = NULL;
272 msg->ice_version = call_private->ice_version;
274 return msg;
277 static void
278 sipe_invite_call(struct sipe_core_private *sipe_private, TransCallback tc)
280 gchar *hdr;
281 gchar *contact;
282 gchar *body;
283 struct sipe_media_call_private *call_private = sipe_private->media_call;
284 struct sip_session *session;
285 struct sip_dialog *dialog;
286 struct sdpmsg *msg;
287 gboolean add_2007_fallback = FALSE;
289 session = sipe_session_find_call(sipe_private, call_private->with);
290 dialog = session->dialogs->data;
291 add_2007_fallback = dialog->cseq == 0 &&
292 call_private->ice_version == SIPE_ICE_RFC_5245 &&
293 !sipe_strequal(call_private->with, sipe_private->test_call_bot_uri);
295 contact = get_contact(sipe_private);
296 hdr = g_strdup_printf(
297 "ms-keep-alive: UAC;hop-hop=yes\r\n"
298 "Contact: %s\r\n"
299 "Content-Type: %s\r\n",
300 contact,
301 add_2007_fallback ?
302 "multipart/alternative;boundary=\"----=_NextPart_000_001E_01CB4397.0B5EB570\""
303 : "application/sdp");
304 g_free(contact);
306 msg = sipe_media_to_sdpmsg(call_private);
307 body = sdpmsg_to_string(msg);
309 if (add_2007_fallback) {
310 gchar *tmp;
311 tmp = g_strdup_printf(
312 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
313 "Content-Type: application/sdp\r\n"
314 "Content-Transfer-Encoding: 7bit\r\n"
315 "Content-Disposition: session; handling=optional; ms-proxy-2007fallback\r\n"
316 "\r\n"
317 "o=- 0 0 IN IP4 %s\r\n"
318 "s=session\r\n"
319 "c=IN IP4 %s\r\n"
320 "m=audio 0 RTP/AVP\r\n"
321 "\r\n"
322 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
323 "Content-Type: application/sdp\r\n"
324 "Content-Transfer-Encoding: 7bit\r\n"
325 "Content-Disposition: session; handling=optional\r\n"
326 "\r\n"
327 "%s"
328 "\r\n"
329 "------=_NextPart_000_001E_01CB4397.0B5EB570--\r\n",
330 msg->ip, msg->ip, body);
331 g_free(body);
332 body = tmp;
335 sdpmsg_free(msg);
337 dialog->outgoing_invite = sip_transport_invite(sipe_private,
338 hdr,
339 body,
340 dialog,
341 tc);
343 g_free(body);
344 g_free(hdr);
347 static struct sip_dialog *
348 sipe_media_dialog_init(struct sip_session* session, struct sipmsg *msg)
350 gchar *newTag = gentag();
351 const gchar *oldHeader;
352 gchar *newHeader;
353 struct sip_dialog *dialog;
355 oldHeader = sipmsg_find_header(msg, "To");
356 newHeader = g_strdup_printf("%s;tag=%s", oldHeader, newTag);
357 sipmsg_remove_header_now(msg, "To");
358 sipmsg_add_header_now(msg, "To", newHeader);
359 g_free(newHeader);
361 dialog = sipe_dialog_add(session);
362 dialog->callid = g_strdup(sipmsg_find_header(msg, "Call-ID"));
363 dialog->with = parse_from(sipmsg_find_header(msg, "From"));
364 sipe_dialog_parse(dialog, msg, FALSE);
366 return dialog;
369 static void
370 send_response_with_session_description(struct sipe_media_call_private *call_private, int code, gchar *text)
372 struct sdpmsg *msg = sipe_media_to_sdpmsg(call_private);
373 gchar *body = sdpmsg_to_string(msg);
374 sdpmsg_free(msg);
375 sipmsg_add_header(call_private->invitation, "Content-Type", "application/sdp");
376 sip_transport_response(call_private->sipe_private, call_private->invitation, code, text, body);
377 g_free(body);
380 static gboolean
381 encryption_levels_compatible(struct sdpmsg *msg)
383 GSList *i;
385 for (i = msg->media; i; i = i->next) {
386 const gchar *enc_level;
387 struct sdpmedia *m = i->data;
389 enc_level = sipe_utils_nameval_find(m->attributes, "encryption");
391 // Decline call if peer requires encryption as we don't support it yet.
392 if (sipe_strequal(enc_level, "required"))
393 return FALSE;
396 return TRUE;
399 static gboolean
400 process_invite_call_response(struct sipe_core_private *sipe_private,
401 struct sipmsg *msg,
402 struct transaction *trans);
404 static gboolean
405 update_remote_media(struct sipe_media_call_private* call_private,
406 struct sdpmedia *media)
408 struct sipe_backend_media *backend_media = SIPE_MEDIA_CALL->backend_private;
409 struct sipe_backend_stream *backend_stream;
410 GList *backend_candidates = NULL;
411 GList *backend_codecs = NULL;
412 GSList *i;
413 gboolean result = TRUE;
415 backend_stream = sipe_backend_media_get_stream_by_id(backend_media,
416 media->name);
417 if (media->port == 0) {
418 if (backend_stream)
419 sipe_backend_media_remove_stream(backend_media, backend_stream);
420 return TRUE;
423 if (!backend_stream)
424 return FALSE;
426 for (i = media->codecs; i; i = i->next) {
427 struct sdpcodec *c = i->data;
428 struct sipe_backend_codec *codec;
429 GSList *j;
431 codec = sipe_backend_codec_new(c->id,
432 c->name,
433 c->type,
434 c->clock_rate);
436 for (j = c->parameters; j; j = j->next) {
437 struct sipnameval *attr = j->data;
439 sipe_backend_codec_add_optional_parameter(codec,
440 attr->name,
441 attr->value);
444 backend_codecs = g_list_append(backend_codecs, codec);
447 result = sipe_backend_set_remote_codecs(backend_media,
448 backend_stream,
449 backend_codecs);
450 sipe_media_codec_list_free(backend_codecs);
452 if (result == FALSE) {
453 sipe_backend_media_remove_stream(backend_media, backend_stream);
454 return FALSE;
457 for (i = media->candidates; i; i = i->next) {
458 struct sdpcandidate *c = i->data;
459 struct sipe_backend_candidate *candidate;
460 candidate = sipe_backend_candidate_new(c->foundation,
461 c->component,
462 c->type,
463 c->protocol,
464 c->ip,
465 c->port,
466 c->username,
467 c->password);
468 sipe_backend_candidate_set_priority(candidate, c->priority);
470 backend_candidates = g_list_append(backend_candidates, candidate);
473 sipe_backend_media_add_remote_candidates(backend_media,
474 backend_stream,
475 backend_candidates);
476 sipe_media_candidate_list_free(backend_candidates);
478 if (sipe_utils_nameval_find(media->attributes, "inactive")) {
479 sipe_backend_stream_hold(backend_media, backend_stream, FALSE);
480 } else if (sipe_backend_stream_is_held(backend_stream)) {
481 sipe_backend_stream_unhold(backend_media, backend_stream, FALSE);
484 return TRUE;
487 static void
488 apply_remote_message(struct sipe_media_call_private* call_private,
489 struct sdpmsg* msg)
491 GSList *i;
493 sipe_utils_slist_free_full(call_private->failed_media, (GDestroyNotify)sdpmedia_free);
494 call_private->failed_media = NULL;
496 for (i = msg->media; i; i = i->next) {
497 struct sdpmedia *media = i->data;
498 if (!update_remote_media(call_private, media)) {
499 media->port = 0;
500 call_private->failed_media =
501 g_slist_append(call_private->failed_media, media);
505 /* We need to keep failed medias until response is sent, remove them
506 * from sdpmsg that is to be freed. */
507 for (i = call_private->failed_media; i; i = i->next) {
508 msg->media = g_slist_remove(msg->media, i->data);
511 call_private->encryption_compatible = encryption_levels_compatible(msg);
514 static gboolean
515 call_initialized(struct sipe_media_call *call)
517 GSList *streams =
518 sipe_backend_media_get_streams(call->backend_private);
520 for (; streams; streams = streams->next) {
521 if (!sipe_backend_stream_initialized(call->backend_private,
522 streams->data)) {
523 return FALSE;
527 return TRUE;
530 // Sends an invite response when the call is accepted and local candidates were
531 // prepared, otherwise does nothing. If error response is sent, call_private is
532 // disposed before function returns. Returns true when response was sent.
533 static gboolean
534 send_invite_response_if_ready(struct sipe_media_call_private *call_private)
536 struct sipe_backend_media *backend_media;
538 backend_media = call_private->public.backend_private;
540 if (!sipe_backend_media_accepted(backend_media) ||
541 !call_initialized(&call_private->public))
542 return FALSE;
544 if (!call_private->encryption_compatible) {
545 struct sipe_core_private *sipe_private = call_private->sipe_private;
547 sipmsg_add_header(call_private->invitation, "Warning",
548 "308 lcs.microsoft.com \"Encryption Levels not compatible\"");
549 sip_transport_response(sipe_private,
550 call_private->invitation,
551 488, "Encryption Levels not compatible",
552 NULL);
553 sipe_backend_media_reject(backend_media, FALSE);
554 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
555 _("Unable to establish a call"),
556 _("Encryption settings of peer are incompatible with ours."));
557 } else {
558 send_response_with_session_description(call_private, 200, "OK");
561 return TRUE;
564 static void
565 stream_initialized_cb(struct sipe_media_call *call,
566 struct sipe_backend_stream *stream)
568 if (call_initialized(call)) {
569 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
570 struct sipe_backend_media *backend_private = call->backend_private;
572 if (sipe_backend_media_is_initiator(backend_private, stream)) {
573 sipe_invite_call(call_private->sipe_private,
574 process_invite_call_response);
575 } else if (call_private->smsg) {
576 struct sdpmsg *smsg = call_private->smsg;
577 call_private->smsg = NULL;
579 apply_remote_message(call_private, smsg);
580 send_invite_response_if_ready(call_private);
581 sdpmsg_free(smsg);
586 static void phone_state_publish(struct sipe_core_private *sipe_private)
588 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
589 sipe_ocs2007_phone_state_publish(sipe_private);
590 } else {
591 // TODO: OCS 2005 support. Is anyone still using it at all?
595 static void
596 media_end_cb(struct sipe_media_call *call)
598 g_return_if_fail(call);
600 SIPE_MEDIA_CALL_PRIVATE->sipe_private->media_call = NULL;
601 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE->sipe_private);
602 sipe_media_call_free(SIPE_MEDIA_CALL_PRIVATE);
605 static void
606 call_accept_cb(struct sipe_media_call *call, gboolean local)
608 if (local) {
609 send_invite_response_if_ready(SIPE_MEDIA_CALL_PRIVATE);
611 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE->sipe_private);
614 static void
615 call_reject_cb(struct sipe_media_call *call, gboolean local)
617 if (local) {
618 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
619 sip_transport_response(call_private->sipe_private,
620 call_private->invitation,
621 603, "Decline", NULL);
625 static gboolean
626 sipe_media_send_ack(struct sipe_core_private *sipe_private, struct sipmsg *msg,
627 struct transaction *trans);
629 static void call_hold_cb(struct sipe_media_call *call,
630 gboolean local,
631 SIPE_UNUSED_PARAMETER gboolean state)
633 if (local)
634 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE->sipe_private,
635 sipe_media_send_ack);
638 static void call_hangup_cb(struct sipe_media_call *call, gboolean local)
640 if (local) {
641 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
642 struct sip_session *session;
643 session = sipe_session_find_call(call_private->sipe_private,
644 call_private->with);
646 if (session) {
647 sipe_session_close(call_private->sipe_private, session);
652 static void
653 error_cb(struct sipe_media_call *call, gchar *message)
655 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
656 struct sipe_core_private *sipe_private = call_private->sipe_private;
657 gboolean initiator = sipe_backend_media_is_initiator(call->backend_private, NULL);
658 gboolean accepted = sipe_backend_media_accepted(call->backend_private);
660 gchar *title = g_strdup_printf("Call with %s failed", call_private->with);
661 sipe_backend_notify_error(SIPE_CORE_PUBLIC, title, message);
662 g_free(title);
664 if (!initiator && !accepted) {
665 sip_transport_response(sipe_private,
666 call_private->invitation,
667 488, "Not Acceptable Here", NULL);
670 sipe_backend_media_hangup(call->backend_private, initiator || accepted);
673 static struct sipe_media_call_private *
674 sipe_media_call_new(struct sipe_core_private *sipe_private,
675 const gchar* with, gboolean initiator, SipeIceVersion ice_version)
677 struct sipe_media_call_private *call_private = g_new0(struct sipe_media_call_private, 1);
678 gchar *cname;
680 call_private->sipe_private = sipe_private;
682 cname = g_strdup(sipe_private->contact + 1);
683 cname[strlen(cname) - 1] = '\0';
685 call_private->public.backend_private = sipe_backend_media_new(SIPE_CORE_PUBLIC,
686 SIPE_MEDIA_CALL,
687 with,
688 initiator);
689 sipe_backend_media_set_cname(call_private->public.backend_private, cname);
691 call_private->ice_version = ice_version;
692 call_private->encryption_compatible = TRUE;
694 call_private->public.stream_initialized_cb = stream_initialized_cb;
695 call_private->public.media_end_cb = media_end_cb;
696 call_private->public.call_accept_cb = call_accept_cb;
697 call_private->public.call_reject_cb = call_reject_cb;
698 call_private->public.call_hold_cb = call_hold_cb;
699 call_private->public.call_hangup_cb = call_hangup_cb;
700 call_private->public.error_cb = error_cb;
702 g_free(cname);
704 return call_private;
707 void sipe_media_hangup(struct sipe_media_call_private *call_private)
709 if (call_private) {
710 sipe_backend_media_hangup(call_private->public.backend_private,
711 FALSE);
715 static void
716 sipe_media_initiate_call(struct sipe_core_private *sipe_private,
717 const char *with, SipeIceVersion ice_version,
718 gboolean with_video)
720 struct sipe_media_call_private *call_private;
721 struct sipe_backend_media *backend_media;
722 struct sipe_backend_media_relays *backend_media_relays;
723 struct sip_session *session;
724 struct sip_dialog *dialog;
726 if (sipe_private->media_call)
727 return;
729 call_private = sipe_media_call_new(sipe_private, with, TRUE, ice_version);
731 session = sipe_session_add_call(sipe_private, with);
732 dialog = sipe_dialog_add(session);
733 dialog->callid = gencallid();
734 dialog->with = g_strdup(session->with);
735 dialog->ourtag = gentag();
737 call_private->with = g_strdup(session->with);
739 backend_media = call_private->public.backend_private;
741 backend_media_relays =
742 sipe_backend_media_relays_convert(sipe_private->media_relays,
743 sipe_private->media_relay_username,
744 sipe_private->media_relay_password);
746 if (!sipe_backend_media_add_stream(backend_media,
747 "audio", with, SIPE_MEDIA_AUDIO,
748 call_private->ice_version, TRUE,
749 backend_media_relays)) {
750 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
751 _("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 call_private->ice_version, TRUE,
762 backend_media_relays)) {
763 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
764 _("Error occured"),
765 _("Error creating video stream"));
766 sipe_media_call_free(call_private);
767 sipe_backend_media_relays_free(backend_media_relays);
768 return;
771 sipe_private->media_call = call_private;
773 sipe_backend_media_relays_free(backend_media_relays);
775 // Processing continues in stream_initialized_cb
778 void
779 sipe_core_media_initiate_call(struct sipe_core_public *sipe_public,
780 const char *with,
781 gboolean with_video)
783 sipe_media_initiate_call(SIPE_CORE_PRIVATE, with,
784 SIPE_ICE_RFC_5245, with_video);
787 void sipe_core_media_connect_conference(struct sipe_core_public *sipe_public,
788 struct sipe_chat_session *chat_session)
790 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
791 struct sipe_backend_media_relays *backend_media_relays;
792 struct sip_session *session;
793 struct sip_dialog *dialog;
794 gchar **parts;
795 gchar *av_uri;
797 session = sipe_session_find_chat(sipe_private, chat_session);
799 if (sipe_private->media_call || !session)
800 return;
802 session->is_call = TRUE;
804 parts = g_strsplit(chat_session->id, "app:conf:focus:", 2);
805 av_uri = g_strjoinv("app:conf:audio-video:", parts);
806 g_strfreev(parts);
808 sipe_private->media_call = sipe_media_call_new(sipe_private, av_uri,
809 TRUE, SIPE_ICE_DRAFT_6);
811 session = sipe_session_add_call(sipe_private, av_uri);
812 dialog = sipe_dialog_add(session);
813 dialog->callid = gencallid();
814 dialog->with = g_strdup(session->with);
815 dialog->ourtag = gentag();
817 g_free(av_uri);
819 sipe_private->media_call->with = g_strdup(session->with);
821 backend_media_relays =
822 sipe_backend_media_relays_convert(sipe_private->media_relays,
823 sipe_private->media_relay_username,
824 sipe_private->media_relay_password);
826 if (!sipe_backend_media_add_stream(sipe_private->media_call->public.backend_private,
827 "audio", dialog->with,
828 SIPE_MEDIA_AUDIO,
829 sipe_private->media_call->ice_version,
830 TRUE, backend_media_relays)) {
831 sipe_backend_notify_error(sipe_public,
832 _("Error occured"),
833 _("Error creating audio stream"));
834 sipe_media_call_free(sipe_private->media_call);
835 sipe_private->media_call = NULL;
838 sipe_backend_media_relays_free(backend_media_relays);
840 // Processing continues in stream_initialized_cb
843 gboolean sipe_core_media_in_call(struct sipe_core_public *sipe_public)
845 if (sipe_public) {
846 return SIPE_CORE_PRIVATE->media_call != NULL;
848 return FALSE;
851 static gboolean phone_number_is_valid(const gchar *phone_number)
853 if (!phone_number || sipe_strequal(phone_number, "")) {
854 return FALSE;
857 if (*phone_number == '+') {
858 ++phone_number;
861 while (*phone_number != '\0') {
862 if (!g_ascii_isdigit(*phone_number)) {
863 return FALSE;
865 ++phone_number;
868 return TRUE;
871 void sipe_core_media_phone_call(struct sipe_core_public *sipe_public,
872 const gchar *phone_number)
874 g_return_if_fail(sipe_public);
876 if (phone_number_is_valid(phone_number)) {
877 gchar *phone_uri = g_strdup_printf("sip:%s@%s;user=phone",
878 phone_number, sipe_public->sip_domain);
880 sipe_core_media_initiate_call(sipe_public, phone_uri, FALSE);
882 g_free(phone_uri);
883 } else {
884 sipe_backend_notify_error(sipe_public,
885 _("Unable to establish a call"),
886 _("Invalid phone number"));
890 void sipe_core_media_test_call(struct sipe_core_public *sipe_public)
892 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
893 if (!sipe_private->test_call_bot_uri) {
894 sipe_backend_notify_error(sipe_public,
895 _("Unable to establish a call"),
896 _("Audio Test Service is not available."));
897 return;
900 sipe_core_media_initiate_call(sipe_public,
901 sipe_private->test_call_bot_uri, FALSE);
904 void
905 process_incoming_invite_call(struct sipe_core_private *sipe_private,
906 struct sipmsg *msg)
908 struct sipe_media_call_private *call_private = sipe_private->media_call;
909 struct sipe_backend_media *backend_media;
910 struct sipe_backend_media_relays *backend_media_relays = NULL;
911 struct sdpmsg *smsg;
912 gboolean has_new_media = FALSE;
913 GSList *i;
915 if (call_private && !is_media_session_msg(call_private, msg)) {
916 sip_transport_response(sipe_private, msg, 486, "Busy Here", NULL);
917 return;
920 smsg = sdpmsg_parse_msg(msg->body);
921 if (!smsg) {
922 sip_transport_response(sipe_private, msg,
923 488, "Not Acceptable Here", NULL);
924 sipe_media_hangup(call_private);
925 return;
928 if (!call_private) {
929 gchar *with = parse_from(sipmsg_find_header(msg, "From"));
930 struct sip_session *session;
932 call_private = sipe_media_call_new(sipe_private, with, FALSE, smsg->ice_version);
933 session = sipe_session_add_call(sipe_private, with);
934 sipe_media_dialog_init(session, msg);
936 call_private->with = g_strdup(session->with);
937 sipe_private->media_call = call_private;
938 g_free(with);
941 backend_media = call_private->public.backend_private;
943 if (call_private->invitation)
944 sipmsg_free(call_private->invitation);
945 call_private->invitation = sipmsg_copy(msg);
947 if (smsg->media)
948 backend_media_relays = sipe_backend_media_relays_convert(
949 sipe_private->media_relays,
950 sipe_private->media_relay_username,
951 sipe_private->media_relay_password);
953 // Create any new media streams
954 for (i = smsg->media; i; i = i->next) {
955 struct sdpmedia *media = i->data;
956 gchar *id = media->name;
957 SipeMediaType type;
959 if ( media->port != 0
960 && !sipe_backend_media_get_stream_by_id(backend_media, id)) {
961 gchar *with;
963 if (sipe_strequal(id, "audio"))
964 type = SIPE_MEDIA_AUDIO;
965 else if (sipe_strequal(id, "video"))
966 type = SIPE_MEDIA_VIDEO;
967 else
968 continue;
970 with = parse_from(sipmsg_find_header(msg, "From"));
971 sipe_backend_media_add_stream(backend_media, id, with,
972 type,
973 smsg->ice_version,
974 FALSE,
975 backend_media_relays);
976 has_new_media = TRUE;
977 g_free(with);
981 sipe_backend_media_relays_free(backend_media_relays);
983 if (has_new_media) {
984 sdpmsg_free(call_private->smsg);
985 call_private->smsg = smsg;
986 sip_transport_response(sipe_private, call_private->invitation,
987 180, "Ringing", NULL);
988 // Processing continues in stream_initialized_cb
989 } else {
990 apply_remote_message(call_private, smsg);
991 send_response_with_session_description(call_private, 200, "OK");
993 sdpmsg_free(smsg);
997 void process_incoming_cancel_call(struct sipe_core_private *sipe_private,
998 struct sipmsg *msg)
1000 struct sipe_media_call_private *call_private = sipe_private->media_call;
1002 // We respond to the CANCEL request with 200 OK response and
1003 // with 487 Request Terminated to the remote INVITE in progress.
1004 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
1006 if (call_private->invitation) {
1007 sip_transport_response(sipe_private, call_private->invitation,
1008 487, "Request Terminated", NULL);
1011 sipe_media_hangup(call_private);
1014 static gboolean
1015 sipe_media_send_ack(struct sipe_core_private *sipe_private,
1016 struct sipmsg *msg,
1017 struct transaction *trans)
1019 struct sipe_media_call_private *call_private = sipe_private->media_call;
1020 struct sip_session *session;
1021 struct sip_dialog *dialog;
1022 int tmp_cseq;
1024 if (!is_media_session_msg(call_private, msg))
1025 return FALSE;
1027 session = sipe_session_find_call(sipe_private, call_private->with);
1028 dialog = session->dialogs->data;
1029 if (!dialog)
1030 return FALSE;
1032 tmp_cseq = dialog->cseq;
1034 dialog->cseq = sip_transaction_cseq(trans) - 1;
1035 sip_transport_ack(sipe_private, dialog);
1036 dialog->cseq = tmp_cseq;
1038 dialog->outgoing_invite = NULL;
1040 return TRUE;
1043 static gboolean
1044 sipe_media_send_final_ack(struct sipe_core_private *sipe_private,
1045 struct sipmsg *msg,
1046 struct transaction *trans)
1048 if (!sipe_media_send_ack(sipe_private, msg, trans))
1049 return FALSE;
1051 sipe_backend_media_accept(sipe_private->media_call->public.backend_private,
1052 FALSE);
1054 return TRUE;
1057 static void
1058 reinvite_on_candidate_pair_cb(struct sipe_core_public *sipe_public)
1060 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1061 struct sipe_media_call_private *media_call = sipe_private->media_call;
1062 struct sipe_backend_media *backend_media;
1063 GSList *streams;
1065 if (!media_call)
1066 return;
1068 backend_media = media_call->public.backend_private;
1069 streams = sipe_backend_media_get_streams(backend_media);
1071 for (; streams; streams = streams->next) {
1072 struct sipe_backend_stream *s = streams->data;
1073 GList *remote_candidates = sipe_backend_media_get_active_remote_candidates(backend_media, s);
1074 guint components = g_list_length(remote_candidates);
1076 sipe_media_candidate_list_free(remote_candidates);
1078 // We must have candidates for both (RTP + RTCP) components ready
1079 if (components < 2) {
1080 sipe_schedule_mseconds(sipe_private,
1081 "<+media-reinvite-on-candidate-pair>",
1082 NULL,
1083 500,
1084 (sipe_schedule_action) reinvite_on_candidate_pair_cb,
1085 NULL);
1086 return;
1090 sipe_invite_call(sipe_private, sipe_media_send_final_ack);
1093 static gboolean
1094 process_invite_call_response(struct sipe_core_private *sipe_private,
1095 struct sipmsg *msg,
1096 struct transaction *trans)
1098 const gchar *with;
1099 struct sipe_media_call_private *call_private = sipe_private->media_call;
1100 struct sipe_backend_media *backend_private;
1101 struct sip_session *session;
1102 struct sip_dialog *dialog;
1103 struct sdpmsg *smsg;
1105 if (!is_media_session_msg(call_private, msg))
1106 return FALSE;
1108 session = sipe_session_find_call(sipe_private, call_private->with);
1109 dialog = session->dialogs->data;
1111 backend_private = call_private->public.backend_private;
1112 with = dialog->with;
1114 dialog->outgoing_invite = NULL;
1116 if (msg->response >= 400) {
1117 // Call rejected by remote peer or an error occurred
1118 const gchar *title;
1119 GString *desc = g_string_new("");
1120 gboolean append_responsestr = FALSE;
1122 switch (msg->response) {
1123 case 480: {
1124 title = _("User unavailable");
1126 if (sipmsg_parse_warning(msg, NULL) == 391) {
1127 g_string_append_printf(desc, _("%s does not want to be disturbed"), with);
1128 } else
1129 g_string_append_printf(desc, _("User %s is not available"), with);
1130 break;
1132 case 603:
1133 case 605:
1134 title = _("Call rejected");
1135 g_string_append_printf(desc, _("User %s rejected call"), with);
1136 break;
1137 case 488: {
1138 /* Check for incompatible encryption levels error.
1140 * MS Lync 2010:
1141 * 488 Not Acceptable Here
1142 * ms-client-diagnostics: 52017;reason="Encryption levels dont match"
1144 * older clients (and SIPE itself):
1145 * 488 Encryption Levels not compatible
1147 const gchar *ms_diag = sipmsg_find_header(msg, "ms-client-diagnostics");
1149 if (sipe_strequal(msg->responsestr, "Encryption Levels not compatible") ||
1150 (ms_diag && g_str_has_prefix(ms_diag, "52017;"))) {
1151 title = _("Unable to establish a call");
1152 g_string_append(desc, _("Encryption settings of peer are incompatible with ours."));
1153 break;
1156 if (call_private->ice_version == SIPE_ICE_RFC_5245 &&
1157 sip_transaction_cseq(trans) == 1) {
1158 gchar *with = g_strdup(call_private->with);
1159 gboolean with_video = sipe_backend_media_get_stream_by_id(backend_private, "video") != NULL;
1161 sipe_media_hangup(call_private);
1162 // We might be calling to OC 2007 instance, retry with ICEv6
1163 sipe_media_initiate_call(sipe_private, with,
1164 SIPE_ICE_DRAFT_6, with_video);
1166 g_free(with);
1167 return TRUE;
1169 // Break intentionally omitted
1171 default:
1172 title = _("Error occured");
1173 g_string_append(desc, _("Unable to establish a call"));
1174 append_responsestr = TRUE;
1175 break;
1178 if (append_responsestr)
1179 g_string_append_printf(desc, "\n%d %s",
1180 msg->response, msg->responsestr);
1182 sipe_backend_notify_error(SIPE_CORE_PUBLIC, title, desc->str);
1183 g_string_free(desc, TRUE);
1185 sipe_media_send_ack(sipe_private, msg, trans);
1186 sipe_media_hangup(call_private);
1188 return TRUE;
1191 sipe_dialog_parse(dialog, msg, TRUE);
1192 smsg = sdpmsg_parse_msg(msg->body);
1193 if (!smsg) {
1194 sip_transport_response(sipe_private, msg,
1195 488, "Not Acceptable Here", NULL);
1196 sipe_media_hangup(call_private);
1197 return FALSE;
1200 apply_remote_message(call_private, smsg);
1201 sdpmsg_free(smsg);
1203 sipe_media_send_ack(sipe_private, msg, trans);
1204 reinvite_on_candidate_pair_cb(SIPE_CORE_PUBLIC);
1206 return TRUE;
1209 gboolean is_media_session_msg(struct sipe_media_call_private *call_private,
1210 struct sipmsg *msg)
1212 if (call_private) {
1213 const gchar *callid = sipmsg_find_header(msg, "Call-ID");
1214 struct sip_session *session;
1216 session = sipe_session_find_call(call_private->sipe_private,
1217 call_private->with);
1218 if (session) {
1219 struct sip_dialog *dialog = session->dialogs->data;
1220 return sipe_strequal(dialog->callid, callid);
1223 return FALSE;
1226 void sipe_media_handle_going_offline(struct sipe_media_call_private *call_private)
1228 struct sipe_backend_media *backend_private;
1230 backend_private = call_private->public.backend_private;
1232 if ( !sipe_backend_media_is_initiator(backend_private, NULL)
1233 && !sipe_backend_media_accepted(backend_private)) {
1234 sip_transport_response(call_private->sipe_private,
1235 call_private->invitation,
1236 480, "Temporarily Unavailable", NULL);
1237 } else {
1238 struct sip_session *session;
1240 session = sipe_session_find_call(call_private->sipe_private,
1241 call_private->with);
1242 if (session)
1243 sipe_session_close(call_private->sipe_private, session);
1246 sipe_media_hangup(call_private);
1249 gboolean sipe_media_is_conference_call(struct sipe_media_call_private *call_private)
1251 return g_strstr_len(call_private->with, -1, "app:conf:audio-video:") != NULL;
1254 static void
1255 sipe_media_relay_free(struct sipe_media_relay *relay)
1257 g_free(relay->hostname);
1258 if (relay->dns_query)
1259 sipe_backend_dns_query_cancel(relay->dns_query);
1260 g_free(relay);
1263 void
1264 sipe_media_relay_list_free(GSList *list)
1266 for (; list; list = g_slist_delete_link(list, list))
1267 sipe_media_relay_free(list->data);
1270 static void
1271 relay_ip_resolved_cb(struct sipe_media_relay* relay,
1272 const gchar *ip, SIPE_UNUSED_PARAMETER guint port)
1274 gchar *hostname = relay->hostname;
1275 relay->dns_query = NULL;
1277 if (ip && port) {
1278 relay->hostname = g_strdup(ip);
1279 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname, ip);
1280 } else {
1281 relay->hostname = NULL;
1282 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname);
1285 g_free(hostname);
1288 static gboolean
1289 process_get_av_edge_credentials_response(struct sipe_core_private *sipe_private,
1290 struct sipmsg *msg,
1291 SIPE_UNUSED_PARAMETER struct transaction *trans)
1293 g_free(sipe_private->media_relay_username);
1294 g_free(sipe_private->media_relay_password);
1295 sipe_media_relay_list_free(sipe_private->media_relays);
1296 sipe_private->media_relay_username = NULL;
1297 sipe_private->media_relay_password = NULL;
1298 sipe_private->media_relays = NULL;
1300 if (msg->response >= 400) {
1301 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
1302 "Failed to obtain A/V Edge credentials.");
1303 return FALSE;
1306 if (msg->response == 200) {
1307 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
1309 if (sipe_strequal("OK", sipe_xml_attribute(xn_response, "reasonPhrase"))) {
1310 const sipe_xml *xn_credentials = sipe_xml_child(xn_response, "credentialsResponse/credentials");
1311 const sipe_xml *xn_relays = sipe_xml_child(xn_response, "credentialsResponse/mediaRelayList");
1312 const sipe_xml *item;
1313 GSList *relays = NULL;
1315 item = sipe_xml_child(xn_credentials, "username");
1316 sipe_private->media_relay_username = sipe_xml_data(item);
1317 item = sipe_xml_child(xn_credentials, "password");
1318 sipe_private->media_relay_password = sipe_xml_data(item);
1320 for (item = sipe_xml_child(xn_relays, "mediaRelay"); item; item = sipe_xml_twin(item)) {
1321 struct sipe_media_relay *relay = g_new0(struct sipe_media_relay, 1);
1322 const sipe_xml *node;
1323 gchar *tmp;
1325 node = sipe_xml_child(item, "hostName");
1326 relay->hostname = sipe_xml_data(node);
1328 node = sipe_xml_child(item, "udpPort");
1329 if (node) {
1330 relay->udp_port = atoi(tmp = sipe_xml_data(node));
1331 g_free(tmp);
1334 node = sipe_xml_child(item, "tcpPort");
1335 if (node) {
1336 relay->tcp_port = atoi(tmp = sipe_xml_data(node));
1337 g_free(tmp);
1340 relays = g_slist_append(relays, relay);
1342 relay->dns_query = sipe_backend_dns_query_a(
1343 SIPE_CORE_PUBLIC,
1344 relay->hostname,
1345 relay->udp_port,
1346 (sipe_dns_resolved_cb) relay_ip_resolved_cb,
1347 relay);
1349 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
1350 relay->hostname,
1351 relay->tcp_port, relay->udp_port);
1354 sipe_private->media_relays = relays;
1357 sipe_xml_free(xn_response);
1360 return TRUE;
1363 void
1364 sipe_media_get_av_edge_credentials(struct sipe_core_private *sipe_private)
1366 // TODO: re-request credentials after duration expires?
1367 const char CRED_REQUEST_XML[] =
1368 "<request requestID=\"%d\" "
1369 "from=\"%s\" "
1370 "version=\"1.0\" "
1371 "to=\"%s\" "
1372 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
1373 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
1374 "<credentialsRequest credentialsRequestID=\"%d\">"
1375 "<identity>%s</identity>"
1376 "<location>%s</location>"
1377 "<duration>480</duration>"
1378 "</credentialsRequest>"
1379 "</request>";
1381 int request_id = rand();
1382 gchar *self;
1383 gchar *body;
1385 if (!sipe_private->mras_uri)
1386 return;
1388 self = sip_uri_self(sipe_private);
1390 body = g_strdup_printf(
1391 CRED_REQUEST_XML,
1392 request_id,
1393 self,
1394 sipe_private->mras_uri,
1395 request_id,
1396 self,
1397 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER) ? "internet" : "intranet");
1398 g_free(self);
1400 sip_transport_service(sipe_private,
1401 sipe_private->mras_uri,
1402 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
1403 body,
1404 process_get_av_edge_credentials_response);
1406 g_free(body);
1410 Local Variables:
1411 mode: c
1412 c-file-style: "bsd"
1413 indent-tabs-mode: t
1414 tab-width: 8
1415 End: