core: factor out code that adds tag to To: header
[siplcs.git] / src / core / sipe-media.c
blob49a280be6309c0cc9a5bb87549d6539125a1b452
1 /**
2 * @file sipe-media.c
4 * pidgin-sipe
6 * Copyright (C) 2011-2019 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-conf.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 /* [MS-SDPEXT] 3.1.5.31.2 says a range size of 100 SHOULD be used for video and
52 * some clients really demand this. */
53 #define VIDEO_SSRC_COUNT 100
55 struct sipe_media_call_private {
56 struct sipe_media_call public;
58 /* private part starts here */
59 struct sipe_core_private *sipe_private;
61 struct sip_session *session;
62 struct sip_session *conference_session;
64 GSList *streams;
66 struct sipmsg *invitation;
67 SipeIceVersion ice_version;
68 gboolean encryption_compatible;
69 gchar *extra_invite_section;
70 gchar *invite_content_type;
72 GSList *ssrc_ranges;
74 struct sdpmsg *smsg;
75 GSList *failed_media;
76 gchar *ringing_key;
77 gchar *timeout_key;
79 #define SIPE_MEDIA_CALL ((struct sipe_media_call *) call_private)
80 #define SIPE_MEDIA_CALL_PRIVATE ((struct sipe_media_call_private *) call)
82 struct sipe_media_stream_private {
83 struct sipe_media_stream public;
85 gchar *timeout_key;
87 guchar *encryption_key;
88 int encryption_key_id;
89 gboolean remote_candidates_and_codecs_set;
90 gboolean established;
91 #ifdef HAVE_XDATA
92 gboolean sdp_negotiation_concluded;
93 gboolean writable;
94 #endif
96 GSList *extra_sdp;
98 GQueue *write_queue;
99 GQueue *async_reads;
100 gssize read_pos;
102 /* User data associated with the stream. */
103 gpointer data;
104 GDestroyNotify data_free_func;
106 #define SIPE_MEDIA_STREAM ((struct sipe_media_stream *) stream_private)
107 #define SIPE_MEDIA_STREAM_PRIVATE ((struct sipe_media_stream_private *) stream)
109 #define SIPE_MEDIA_STREAM_CONNECTION_TIMEOUT_SECONDS 30
110 #define SIPE_MEDIA_CALL_RINGING_TIMEOUT_SECONDS 60
111 #define SIPE_MEDIA_CALL_TIMEOUT_SECONDS 120
113 struct async_read_data {
114 guint8 *buffer;
115 gssize len;
116 sipe_media_stream_read_callback callback;
119 static void stream_schedule_cancel_timeout(struct sipe_media_call *call,
120 struct sipe_media_stream_private *stream_private);
122 static void call_schedule_cancel_request_timeout(struct sipe_media_call *call);
123 static void call_schedule_cancel_ringing_timeout(struct sipe_media_call *call);
125 static void sipe_media_codec_list_free(GList *codecs)
127 for (; codecs; codecs = g_list_delete_link(codecs, codecs))
128 sipe_backend_codec_free(codecs->data);
131 static void sipe_media_candidate_list_free(GList *candidates)
133 for (; candidates; candidates = g_list_delete_link(candidates, candidates))
134 sipe_backend_candidate_free(candidates->data);
137 static void
138 sipe_media_stream_free(struct sipe_media_stream_private *stream_private)
140 struct sipe_media_call_private *call_private;
142 call_private = (struct sipe_media_call_private *)SIPE_MEDIA_STREAM->call;
144 stream_schedule_cancel_timeout(SIPE_MEDIA_CALL, stream_private);
146 sipe_media_stream_set_data(SIPE_MEDIA_STREAM, NULL, NULL);
148 if (call_private) {
149 call_private->streams = g_slist_remove(call_private->streams,
150 stream_private);
152 if (SIPE_MEDIA_STREAM->ssrc_range) {
153 call_private->ssrc_ranges =
154 g_slist_remove(call_private->ssrc_ranges,
155 SIPE_MEDIA_STREAM->ssrc_range);
159 if (SIPE_MEDIA_STREAM->backend_private) {
160 sipe_backend_media_stream_free(SIPE_MEDIA_STREAM->backend_private);
162 g_free(SIPE_MEDIA_STREAM->id);
163 g_free(stream_private->encryption_key);
164 g_queue_free_full(stream_private->write_queue,
165 (GDestroyNotify)g_byte_array_unref);
166 g_queue_free_full(stream_private->async_reads, g_free);
167 sipe_utils_nameval_free(stream_private->extra_sdp);
168 g_free(stream_private);
171 static gboolean
172 call_private_equals(SIPE_UNUSED_PARAMETER const gchar *callid,
173 struct sipe_media_call_private *call_private1,
174 struct sipe_media_call_private *call_private2)
176 return call_private1 == call_private2;
179 static void
180 sipe_media_call_free(struct sipe_media_call_private *call_private)
182 if (call_private) {
183 g_hash_table_foreach_remove(call_private->sipe_private->media_calls,
184 (GHRFunc) call_private_equals, call_private);
186 call_schedule_cancel_request_timeout(SIPE_MEDIA_CALL);
187 call_schedule_cancel_ringing_timeout(SIPE_MEDIA_CALL);
189 while (call_private->streams) {
190 sipe_media_stream_free(call_private->streams->data);
193 sipe_backend_media_free(call_private->public.backend_private);
195 if (call_private->session) {
196 sipe_session_remove(call_private->sipe_private,
197 call_private->session);
200 if (call_private->invitation)
201 sipmsg_free(call_private->invitation);
203 // Frees any referenced extra invite data.
204 sipe_media_add_extra_invite_section(SIPE_MEDIA_CALL, NULL, NULL);
206 sipe_utils_slist_free_full(call_private->ssrc_ranges, g_free);
208 sdpmsg_free(call_private->smsg);
209 sipe_utils_slist_free_full(call_private->failed_media,
210 (GDestroyNotify)sdpmedia_free);
211 g_free(SIPE_MEDIA_CALL->with);
212 g_free(call_private);
216 static gint
217 candidate_sort_cb(struct sdpcandidate *c1, struct sdpcandidate *c2)
219 int cmp = g_strcmp0(c1->foundation, c2->foundation);
220 if (cmp == 0) {
221 cmp = g_strcmp0(c1->username, c2->username);
222 if (cmp == 0)
223 cmp = c1->component - c2->component;
226 return cmp;
229 static GSList *
230 backend_candidates_to_sdpcandidate(GList *candidates)
232 GSList *result = NULL;
233 GList *i;
235 for (i = candidates; i; i = i->next) {
236 struct sipe_backend_candidate *candidate = i->data;
237 struct sdpcandidate *c;
239 gchar *ip = sipe_backend_candidate_get_ip(candidate);
240 gchar *base_ip = sipe_backend_candidate_get_base_ip(candidate);
241 if (is_empty(ip) || strchr(ip, ':') ||
242 (base_ip && strchr(base_ip, ':'))) {
243 /* Ignore IPv6 candidates. */
244 g_free(ip);
245 g_free(base_ip);
246 continue;
249 c = g_new(struct sdpcandidate, 1);
250 c->foundation = sipe_backend_candidate_get_foundation(candidate);
251 c->component = sipe_backend_candidate_get_component_type(candidate);
252 c->type = sipe_backend_candidate_get_type(candidate);
253 c->protocol = sipe_backend_candidate_get_protocol(candidate);
254 c->ip = ip;
255 c->port = sipe_backend_candidate_get_port(candidate);
256 c->base_ip = base_ip;
257 c->base_port = sipe_backend_candidate_get_base_port(candidate);
258 c->priority = sipe_backend_candidate_get_priority(candidate);
259 c->username = sipe_backend_candidate_get_username(candidate);
260 c->password = sipe_backend_candidate_get_password(candidate);
262 result = g_slist_insert_sorted(result, c,
263 (GCompareFunc)candidate_sort_cb);
266 return result;
269 static void
270 get_stream_ip_and_ports(GSList *candidates,
271 gchar **ip, guint *rtp_port, guint *rtcp_port)
273 guint32 rtp_max_priority = 0;
274 guint32 rtcp_max_priority = 0;
276 *ip = 0;
277 *rtp_port = 0;
278 *rtcp_port = 0;
280 for (; candidates; candidates = candidates->next) {
281 struct sdpcandidate *candidate = candidates->data;
283 if (candidate->component == SIPE_COMPONENT_RTP &&
284 candidate->priority > rtp_max_priority) {
285 rtp_max_priority = candidate->priority;
286 *rtp_port = candidate->port;
288 g_free(*ip);
289 *ip = g_strdup(candidate->ip);
290 } else if (candidate->component == SIPE_COMPONENT_RTCP &&
291 candidate->priority > rtcp_max_priority) {
292 rtcp_max_priority = candidate->priority;
293 *rtcp_port = candidate->port;
298 static gint
299 sdpcodec_compare(gconstpointer a, gconstpointer b)
301 return ((const struct sdpcodec *)a)->id -
302 ((const struct sdpcodec *)b)->id;
305 static GList *
306 remove_wrong_farstream_0_1_tcp_candidates(GList *candidates)
308 GList *i = candidates;
309 GHashTable *foundation_to_candidate = g_hash_table_new_full(g_str_hash,
310 g_str_equal,
311 g_free,
312 NULL);
314 while (i) {
315 GList *next = i->next;
316 struct sipe_backend_candidate *c1 = i->data;
318 if (sipe_backend_candidate_get_protocol(c1) == SIPE_NETWORK_PROTOCOL_UDP) {
319 gchar *foundation = sipe_backend_candidate_get_foundation(c1);
320 struct sipe_backend_candidate *c2 = g_hash_table_lookup(foundation_to_candidate,
321 foundation);
323 if (c2) {
324 g_free(foundation);
326 if (sipe_backend_candidate_get_port(c1) ==
327 sipe_backend_candidate_get_port(c2) ||
328 (sipe_backend_candidate_get_type(c1) !=
329 SIPE_CANDIDATE_TYPE_HOST &&
330 sipe_backend_candidate_get_base_port(c1) ==
331 sipe_backend_candidate_get_base_port(c2))) {
333 * We assume that RTP+RTCP UDP pairs
334 * that share the same port are
335 * actually mistagged TCP candidates.
337 candidates = g_list_remove(candidates, c2);
338 candidates = g_list_delete_link(candidates, i);
339 sipe_backend_candidate_free(c1);
340 sipe_backend_candidate_free(c2);
342 } else
343 /* hash table takes ownership of "foundation" */
344 g_hash_table_insert(foundation_to_candidate, foundation, c1);
347 i = next;
350 g_hash_table_destroy(foundation_to_candidate);
352 return candidates;
355 static void
356 fill_zero_tcp_act_ports_from_tcp_pass(GSList *candidates, GSList *all_candidates)
358 GSList *i;
359 GHashTable *ip_to_port = g_hash_table_new(g_str_hash, g_str_equal);
361 for (i = candidates; i; i = i->next) {
362 struct sdpcandidate *c = i->data;
363 GSList *j;
365 if (c->protocol != SIPE_NETWORK_PROTOCOL_TCP_ACTIVE) {
366 continue;
369 for (j = all_candidates; j; j = j->next) {
370 struct sdpcandidate *passive = j->data;
371 if (passive->protocol != SIPE_NETWORK_PROTOCOL_TCP_PASSIVE ||
372 c->type != passive->type) {
373 continue;
376 if (sipe_strequal(c->ip, passive->ip) &&
377 sipe_strequal(c->base_ip, passive->base_ip)) {
378 if (c->port == 0) {
379 c->port = passive->port;
382 if (c->base_port == 0) {
383 c->base_port = passive->base_port;
385 break;
390 for (i = all_candidates; i; i = i->next) {
391 struct sdpcandidate *c = i->data;
393 if (c->protocol == SIPE_NETWORK_PROTOCOL_TCP_PASSIVE &&
394 c->type == SIPE_CANDIDATE_TYPE_HOST) {
395 g_hash_table_insert(ip_to_port, c->ip, &c->port);
399 /* Fill base ports of all TCP relay candidates using what we have
400 * collected from host candidates. */
401 for (i = candidates; i; i = i->next) {
402 struct sdpcandidate *c = i->data;
403 if (c->type == SIPE_CANDIDATE_TYPE_RELAY && c->base_port == 0) {
404 guint *base_port = (guint*)g_hash_table_lookup(ip_to_port, c->base_ip);
405 if (base_port) {
406 c->base_port = *base_port;
407 } else {
408 SIPE_DEBUG_WARNING("Couldn't determine base port for candidate "
409 "with foundation %s", c->foundation);
414 g_hash_table_destroy(ip_to_port);
417 static SipeEncryptionPolicy
418 get_encryption_policy(struct sipe_core_private *sipe_private)
420 SipeEncryptionPolicy result =
421 sipe_backend_media_get_encryption_policy(SIPE_CORE_PUBLIC);
422 if (result == SIPE_ENCRYPTION_POLICY_OBEY_SERVER) {
423 result = sipe_private->server_av_encryption_policy;
426 return result;
429 static struct sdpmedia *
430 media_stream_to_sdpmedia(struct sipe_media_call_private *call_private,
431 struct sipe_media_stream_private *stream_private)
433 struct sdpmedia *sdpmedia = g_new0(struct sdpmedia, 1);
434 GList *codecs = sipe_backend_get_local_codecs(SIPE_MEDIA_CALL,
435 SIPE_MEDIA_STREAM);
436 SipeEncryptionPolicy encryption_policy =
437 get_encryption_policy(call_private->sipe_private);
438 guint rtcp_port = 0;
439 SipeMediaType type;
440 GSList *attributes = NULL;
441 GSList *sdpcandidates;
442 GSList *all_sdpcandidates;
443 GList *candidates;
444 GList *i;
445 GSList *j;
447 sdpmedia->name = g_strdup(SIPE_MEDIA_STREAM->id);
449 if (sipe_strequal(sdpmedia->name, "audio"))
450 type = SIPE_MEDIA_AUDIO;
451 else if (sipe_strequal(sdpmedia->name, "video"))
452 type = SIPE_MEDIA_VIDEO;
453 else if (sipe_strequal(sdpmedia->name, "data"))
454 type = SIPE_MEDIA_APPLICATION;
455 else if (sipe_strequal(sdpmedia->name, "applicationsharing"))
456 type = SIPE_MEDIA_APPLICATION;
457 else {
458 // TODO: incompatible media, should not happen here
459 g_free(sdpmedia->name);
460 g_free(sdpmedia);
461 sipe_media_codec_list_free(codecs);
462 return(NULL);
465 // Process codecs
466 for (i = codecs; i; i = i->next) {
467 struct sipe_backend_codec *codec = i->data;
468 struct sdpcodec *c = g_new0(struct sdpcodec, 1);
469 GList *params;
471 c->id = sipe_backend_codec_get_id(codec);
472 c->name = sipe_backend_codec_get_name(codec);
473 c->clock_rate = sipe_backend_codec_get_clock_rate(codec);
474 c->type = type;
476 params = sipe_backend_codec_get_optional_parameters(codec);
477 for (; params; params = params->next) {
478 struct sipnameval *param = params->data;
479 struct sipnameval *copy = g_new0(struct sipnameval, 1);
481 copy->name = g_strdup(param->name);
482 copy->value = g_strdup(param->value);
484 c->parameters = g_slist_append(c->parameters, copy);
487 /* Buggy(?) codecs may report non-unique id (a.k.a. payload
488 * type) that must not appear in SDP messages we send. Thus,
489 * let's ignore any codec having the same id as one we already
490 * have in the converted list. */
491 if (g_slist_find_custom(sdpmedia->codecs, c, sdpcodec_compare)) {
492 sdpcodec_free(c);
493 } else {
494 sdpmedia->codecs = g_slist_append(sdpmedia->codecs, c);
498 sipe_media_codec_list_free(codecs);
500 // Process local candidates
501 // If we have established candidate pairs, send them in SDP response.
502 // Otherwise send all available local candidates.
503 candidates = sipe_backend_media_stream_get_active_local_candidates(SIPE_MEDIA_STREAM);
504 sdpcandidates = backend_candidates_to_sdpcandidate(candidates);
505 sipe_media_candidate_list_free(candidates);
507 candidates = sipe_backend_get_local_candidates(SIPE_MEDIA_CALL,
508 SIPE_MEDIA_STREAM);
509 candidates = remove_wrong_farstream_0_1_tcp_candidates(candidates);
510 all_sdpcandidates = backend_candidates_to_sdpcandidate(candidates);
511 sipe_media_candidate_list_free(candidates);
513 if (!sdpcandidates) {
514 sdpcandidates = all_sdpcandidates;
517 fill_zero_tcp_act_ports_from_tcp_pass(sdpcandidates, all_sdpcandidates);
519 sdpmedia->candidates = sdpcandidates;
521 if (all_sdpcandidates != sdpcandidates) {
522 sipe_utils_slist_free_full(all_sdpcandidates,
523 (GDestroyNotify)sdpcandidate_free);
526 get_stream_ip_and_ports(sdpmedia->candidates, &sdpmedia->ip,
527 &sdpmedia->port, &rtcp_port);
529 if (sipe_backend_stream_is_held(SIPE_MEDIA_STREAM))
530 attributes = sipe_utils_nameval_add(attributes, "inactive", "");
532 if (rtcp_port) {
533 gchar *tmp = g_strdup_printf("%u", rtcp_port);
534 attributes = sipe_utils_nameval_add(attributes, "rtcp", tmp);
535 g_free(tmp);
538 if (encryption_policy != call_private->sipe_private->server_av_encryption_policy) {
539 const gchar *encryption = NULL;
540 switch (encryption_policy) {
541 case SIPE_ENCRYPTION_POLICY_REJECTED:
542 encryption = "rejected";
543 break;
544 case SIPE_ENCRYPTION_POLICY_OPTIONAL:
545 encryption = "optional";
546 break;
547 case SIPE_ENCRYPTION_POLICY_REQUIRED:
548 default:
549 encryption = "required";
550 break;
553 attributes = sipe_utils_nameval_add(attributes, "encryption", encryption);
556 if (SIPE_MEDIA_STREAM->ssrc_range) {
557 gchar *tmp;
559 tmp = g_strdup_printf("%u-%u",
560 SIPE_MEDIA_STREAM->ssrc_range->begin,
561 SIPE_MEDIA_STREAM->ssrc_range->end);
562 attributes = sipe_utils_nameval_add(attributes,
563 "x-ssrc-range", tmp);
564 g_free(tmp);
567 // Process remote candidates
568 candidates = sipe_backend_media_stream_get_active_remote_candidates(SIPE_MEDIA_STREAM);
569 sdpmedia->remote_candidates = backend_candidates_to_sdpcandidate(candidates);
570 sipe_media_candidate_list_free(candidates);
572 sdpmedia->encryption_active = stream_private->encryption_key &&
573 call_private->encryption_compatible &&
574 stream_private->remote_candidates_and_codecs_set &&
575 encryption_policy != SIPE_ENCRYPTION_POLICY_REJECTED;
577 // Set our key if encryption is enabled.
578 if (stream_private->encryption_key &&
579 encryption_policy != SIPE_ENCRYPTION_POLICY_REJECTED) {
580 sdpmedia->encryption_key = g_memdup(stream_private->encryption_key,
581 SIPE_SRTP_KEY_LEN);
582 sdpmedia->encryption_key_id = stream_private->encryption_key_id;
585 // Append extra attributes assigned to the stream.
586 for (j = stream_private->extra_sdp; j; j = g_slist_next(j)) {
587 struct sipnameval *attr = j->data;
588 attributes = sipe_utils_nameval_add(attributes,
589 attr->name, attr->value);
592 sdpmedia->attributes = attributes;
594 return sdpmedia;
597 static struct sdpmsg *
598 sipe_media_to_sdpmsg(struct sipe_media_call_private *call_private)
600 struct sdpmsg *msg = g_new0(struct sdpmsg, 1);
601 GSList *streams = call_private->streams;
603 for (; streams; streams = streams->next) {
604 struct sdpmedia *media = media_stream_to_sdpmedia(call_private,
605 streams->data);
606 if (media) {
607 msg->media = g_slist_append(msg->media, media);
609 if (msg->ip == NULL)
610 msg->ip = g_strdup(media->ip);
614 msg->media = g_slist_concat(msg->media, call_private->failed_media);
615 call_private->failed_media = NULL;
617 msg->ice_version = call_private->ice_version;
619 return msg;
622 static void
623 sipe_invite_call(struct sipe_media_call_private *call_private, TransCallback tc)
625 struct sipe_core_private *sipe_private = call_private->sipe_private;
626 gchar *hdr;
627 gchar *contact;
628 gchar *p_preferred_identity = NULL;
629 gchar *body;
630 struct sip_dialog *dialog;
631 struct sdpmsg *msg;
633 dialog = sipe_media_get_sip_dialog(SIPE_MEDIA_CALL);
635 contact = get_contact(sipe_private);
637 if (sipe_private->uc_line_uri) {
638 gchar *self = sip_uri_self(sipe_private);
639 p_preferred_identity = g_strdup_printf(
640 "P-Preferred-Identity: <%s>, <%s>\r\n",
641 self, sipe_private->uc_line_uri);
642 g_free(self);
645 hdr = g_strdup_printf(
646 "ms-keep-alive: UAC;hop-hop=yes\r\n"
647 "Contact: %s\r\n"
648 "%s"
649 "Content-Type: %s%s\r\n",
650 contact,
651 p_preferred_identity ? p_preferred_identity : "",
652 call_private->invite_content_type ?
653 call_private->invite_content_type : "application/sdp",
654 call_private->invite_content_type ?
655 ";boundary=\"----=_NextPart_000_001E_01CB4397.0B5EB570\"" : "");
657 g_free(contact);
658 g_free(p_preferred_identity);
660 msg = sipe_media_to_sdpmsg(call_private);
661 body = sdpmsg_to_string(msg);
663 if (call_private->extra_invite_section) {
664 gchar *tmp;
665 tmp = g_strdup_printf(
666 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
667 "%s"
668 "\r\n"
669 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
670 "Content-Type: application/sdp\r\n"
671 "Content-Transfer-Encoding: 7bit\r\n"
672 "Content-Disposition: session; handling=optional\r\n"
673 "\r\n"
674 "%s"
675 "\r\n"
676 "------=_NextPart_000_001E_01CB4397.0B5EB570--\r\n",
677 call_private->extra_invite_section, body);
678 g_free(body);
679 body = tmp;
680 sipe_media_add_extra_invite_section(SIPE_MEDIA_CALL, NULL, NULL);
683 sdpmsg_free(msg);
685 dialog->outgoing_invite = sip_transport_invite(sipe_private,
686 hdr,
687 body,
688 dialog,
689 tc);
691 g_free(body);
692 g_free(hdr);
695 static void
696 send_response_with_session_description(struct sipe_media_call_private *call_private, int code, gchar *text)
698 struct sdpmsg *msg = sipe_media_to_sdpmsg(call_private);
699 gchar *body = sdpmsg_to_string(msg);
700 sdpmsg_free(msg);
701 sipmsg_add_header(call_private->invitation, "Content-Type", "application/sdp");
702 sip_transport_response(call_private->sipe_private, call_private->invitation, code, text, body);
703 g_free(body);
706 static gboolean
707 process_invite_call_response(struct sipe_core_private *sipe_private,
708 struct sipmsg *msg,
709 struct transaction *trans);
711 struct sipe_media_stream *
712 sipe_core_media_get_stream_by_id(struct sipe_media_call *call, const gchar *id)
714 GSList *i;
715 for (i = SIPE_MEDIA_CALL_PRIVATE->streams; i; i = i->next) {
716 struct sipe_media_stream *stream = i->data;
717 if (sipe_strequal(stream->id, id))
718 return stream;
720 return NULL;
723 static gboolean
724 update_call_from_remote_sdp(struct sipe_media_call_private* call_private,
725 struct sdpmedia *media)
727 struct sipe_media_stream *stream;
728 GList *backend_candidates = NULL;
729 GList *backend_codecs = NULL;
730 GSList *i;
731 gboolean result = TRUE;
733 stream = sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL, media->name);
734 if (media->port == 0) {
735 if (stream) {
736 sipe_backend_media_stream_end(SIPE_MEDIA_CALL, stream);
738 return FALSE;
741 if (!stream)
742 return FALSE;
744 if (sipe_utils_nameval_find(media->attributes, "inactive")) {
745 sipe_backend_stream_hold(SIPE_MEDIA_CALL, stream, FALSE);
746 } else if (sipe_backend_stream_is_held(stream)) {
747 sipe_backend_stream_unhold(SIPE_MEDIA_CALL, stream, FALSE);
750 if (SIPE_MEDIA_STREAM_PRIVATE->remote_candidates_and_codecs_set) {
751 return TRUE;
754 for (i = media->codecs; i; i = i->next) {
755 struct sdpcodec *c = i->data;
756 struct sipe_backend_codec *codec;
757 GSList *j;
759 codec = sipe_backend_codec_new(c->id,
760 c->name,
761 c->type,
762 c->clock_rate,
763 c->channels);
765 for (j = c->parameters; j; j = j->next) {
766 struct sipnameval *attr = j->data;
768 sipe_backend_codec_add_optional_parameter(codec,
769 attr->name,
770 attr->value);
773 backend_codecs = g_list_append(backend_codecs, codec);
776 if (media->encryption_key && SIPE_MEDIA_STREAM_PRIVATE->encryption_key) {
777 sipe_backend_media_set_encryption_keys(SIPE_MEDIA_CALL, stream,
778 SIPE_MEDIA_STREAM_PRIVATE->encryption_key,
779 media->encryption_key);
780 SIPE_MEDIA_STREAM_PRIVATE->encryption_key_id = media->encryption_key_id;
783 result = sipe_backend_set_remote_codecs(SIPE_MEDIA_CALL, stream,
784 backend_codecs);
785 sipe_media_codec_list_free(backend_codecs);
787 if (result == FALSE) {
788 sipe_backend_media_stream_end(SIPE_MEDIA_CALL, stream);
789 return FALSE;
792 for (i = media->candidates; i; i = i->next) {
793 struct sdpcandidate *c = i->data;
794 struct sipe_backend_candidate *candidate;
795 candidate = sipe_backend_candidate_new(c->foundation,
796 c->component,
797 c->type,
798 c->protocol,
799 c->ip,
800 c->port,
801 c->username,
802 c->password);
803 sipe_backend_candidate_set_priority(candidate, c->priority);
805 backend_candidates = g_list_append(backend_candidates, candidate);
808 sipe_backend_media_add_remote_candidates(SIPE_MEDIA_CALL, stream,
809 backend_candidates);
810 sipe_media_candidate_list_free(backend_candidates);
812 SIPE_MEDIA_STREAM_PRIVATE->remote_candidates_and_codecs_set = TRUE;
814 return TRUE;
817 static void
818 apply_remote_message(struct sipe_media_call_private* call_private,
819 struct sdpmsg* msg)
821 GSList *i;
823 sipe_utils_slist_free_full(call_private->failed_media, (GDestroyNotify)sdpmedia_free);
824 call_private->failed_media = NULL;
825 call_private->encryption_compatible = TRUE;
827 for (i = msg->media; i; i = i->next) {
828 struct sdpmedia *media = i->data;
829 const gchar *enc_level =
830 sipe_utils_nameval_find(media->attributes, "encryption");
831 if (sipe_strequal(enc_level, "rejected") &&
832 get_encryption_policy(call_private->sipe_private) == SIPE_ENCRYPTION_POLICY_REQUIRED) {
833 call_private->encryption_compatible = FALSE;
836 if (!update_call_from_remote_sdp(call_private, media)) {
837 media->port = 0;
838 call_private->failed_media =
839 g_slist_append(call_private->failed_media, media);
843 /* We need to keep failed medias until response is sent, remove them
844 * from sdpmsg that is to be freed. */
845 for (i = call_private->failed_media; i; i = i->next) {
846 msg->media = g_slist_remove(msg->media, i->data);
850 static gboolean
851 call_initialized(struct sipe_media_call *call)
853 GSList *streams = SIPE_MEDIA_CALL_PRIVATE->streams;
854 for (; streams; streams = streams->next) {
855 if (!sipe_backend_stream_initialized(call, streams->data)) {
856 return FALSE;
860 return TRUE;
863 static void
864 stream_connection_timeout_cb(struct sipe_core_private *sipe_private,
865 gpointer data)
867 struct sipe_media_call_private *call_private = data;
869 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
870 _("Couldn't create stream"),
871 _("Connection timed out"));
872 sipe_backend_media_hangup(SIPE_MEDIA_CALL->backend_private, TRUE);
875 static void
876 stream_schedule_timeout(struct sipe_media_call *call)
878 GSList *i;
879 for (i = SIPE_MEDIA_CALL_PRIVATE->streams; i; i = i->next) {
880 struct sipe_media_stream_private *stream_private = i->data;
882 stream_private->timeout_key =
883 g_strdup_printf("<media-stream-connect><%s><%s>",
884 sipe_media_get_sip_dialog(call)->callid,
885 SIPE_MEDIA_STREAM->id);
887 sipe_schedule_seconds(SIPE_MEDIA_CALL_PRIVATE->sipe_private,
888 stream_private->timeout_key,
889 SIPE_MEDIA_CALL_PRIVATE,
890 SIPE_MEDIA_STREAM_CONNECTION_TIMEOUT_SECONDS,
891 stream_connection_timeout_cb,
892 NULL);
896 static void
897 stream_schedule_cancel_timeout(struct sipe_media_call *call,
898 struct sipe_media_stream_private *stream_private)
900 if (stream_private->timeout_key) {
901 sipe_schedule_cancel(SIPE_MEDIA_CALL_PRIVATE->sipe_private,
902 stream_private->timeout_key);
903 g_free(stream_private->timeout_key);
905 stream_private->timeout_key = NULL;
908 static void
909 call_request_timeout_cb(struct sipe_core_private *sipe_private,
910 gpointer data)
912 struct sipe_media_call_private *call_private = data;
914 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
915 _("Request timed out"),
916 _("Call could not be answered"));
917 sipe_backend_media_hangup(SIPE_MEDIA_CALL->backend_private, TRUE);
920 static void
921 call_schedule_request_timeout(struct sipe_media_call *call)
923 SIPE_MEDIA_CALL_PRIVATE->timeout_key =
924 g_strdup_printf("<media-call-request><%s>", sipe_media_get_sip_dialog(call)->callid);
926 sipe_schedule_seconds(SIPE_MEDIA_CALL_PRIVATE->sipe_private,
927 SIPE_MEDIA_CALL_PRIVATE->timeout_key,
928 SIPE_MEDIA_CALL_PRIVATE,
929 SIPE_MEDIA_CALL_TIMEOUT_SECONDS,
930 call_request_timeout_cb,
931 NULL);
934 static void
935 call_schedule_cancel_request_timeout(struct sipe_media_call *call)
937 if (SIPE_MEDIA_CALL_PRIVATE->timeout_key) {
938 sipe_schedule_cancel(SIPE_MEDIA_CALL_PRIVATE->sipe_private,
939 SIPE_MEDIA_CALL_PRIVATE->timeout_key);
940 g_free(SIPE_MEDIA_CALL_PRIVATE->timeout_key);
942 SIPE_MEDIA_CALL_PRIVATE->timeout_key = NULL;
946 static void
947 call_ringing_timeout_cb(struct sipe_core_private *sipe_private,
948 gpointer data)
950 struct sipe_media_call_private *call_private = data;
952 sip_transport_response(sipe_private, call_private->invitation,
953 408, "Request Timeout", NULL);
954 sipe_backend_media_hangup(SIPE_MEDIA_CALL->backend_private, FALSE);
957 static void
958 call_schedule_ringing_timeout(struct sipe_media_call *call)
960 SIPE_MEDIA_CALL_PRIVATE->ringing_key =
961 g_strdup_printf("<media-call-ringing><%s>", sipe_media_get_sip_dialog(call)->callid);
963 sipe_schedule_seconds(SIPE_MEDIA_CALL_PRIVATE->sipe_private,
964 SIPE_MEDIA_CALL_PRIVATE->ringing_key,
965 SIPE_MEDIA_CALL_PRIVATE,
966 SIPE_MEDIA_CALL_RINGING_TIMEOUT_SECONDS,
967 call_ringing_timeout_cb,
968 NULL);
971 static void
972 call_schedule_cancel_ringing_timeout(struct sipe_media_call *call)
974 if (SIPE_MEDIA_CALL_PRIVATE->ringing_key) {
975 sipe_schedule_cancel(SIPE_MEDIA_CALL_PRIVATE->sipe_private,
976 SIPE_MEDIA_CALL_PRIVATE->ringing_key);
977 g_free(SIPE_MEDIA_CALL_PRIVATE->ringing_key);
979 SIPE_MEDIA_CALL_PRIVATE->ringing_key = NULL;
982 // Sends an invite response when the call is accepted and local candidates were
983 // prepared, otherwise does nothing. If error response is sent, call_private is
984 // disposed before function returns.
985 static void
986 maybe_send_first_invite_response(struct sipe_media_call_private *call_private)
988 struct sipe_backend_media *backend_media;
990 backend_media = call_private->public.backend_private;
992 if (!sipe_backend_media_accepted(backend_media) ||
993 !call_initialized(&call_private->public))
994 return;
996 if (!call_private->encryption_compatible) {
997 struct sipe_core_private *sipe_private = call_private->sipe_private;
999 sipmsg_add_header(call_private->invitation, "Warning",
1000 "308 lcs.microsoft.com \"Encryption Levels not compatible\"");
1001 sip_transport_response(sipe_private,
1002 call_private->invitation,
1003 488, "Encryption Levels not compatible",
1004 NULL);
1005 sipe_backend_media_reject(backend_media, FALSE);
1006 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
1007 _("Unable to establish a call"),
1008 _("Encryption settings of peer are incompatible with ours."));
1009 } else {
1010 send_response_with_session_description(call_private, 200, "OK");
1011 stream_schedule_timeout(SIPE_MEDIA_CALL);
1012 call_schedule_cancel_ringing_timeout(SIPE_MEDIA_CALL);
1013 sipmsg_free(call_private->invitation);
1014 call_private->invitation = NULL;
1018 static void
1019 stream_initialized_cb(struct sipe_media_call *call,
1020 struct sipe_media_stream *stream)
1022 if (call_initialized(call)) {
1023 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
1025 if (sipe_backend_media_is_initiator(call, stream)) {
1026 sipe_invite_call(call_private,
1027 process_invite_call_response);
1028 } else if (call_private->smsg) {
1029 struct sdpmsg *smsg = call_private->smsg;
1030 call_private->smsg = NULL;
1032 apply_remote_message(call_private, smsg);
1033 maybe_send_first_invite_response(call_private);
1034 sdpmsg_free(smsg);
1039 static void phone_state_publish(struct sipe_core_private *sipe_private)
1041 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1042 sipe_ocs2007_phone_state_publish(sipe_private);
1043 } else {
1044 // TODO: OCS 2005 support. Is anyone still using it at all?
1048 void
1049 sipe_core_media_stream_end(struct sipe_media_stream *stream)
1051 sipe_media_stream_free(SIPE_MEDIA_STREAM_PRIVATE);
1054 static void
1055 media_end_cb(struct sipe_media_call *call)
1057 struct sipe_core_private *sipe_private;
1059 g_return_if_fail(call);
1061 sipe_private = SIPE_MEDIA_CALL_PRIVATE->sipe_private;
1063 sipe_media_call_free(SIPE_MEDIA_CALL_PRIVATE);
1064 phone_state_publish(sipe_private);
1067 static void
1068 call_accept_cb(struct sipe_media_call *call, gboolean local)
1070 if (local) {
1071 maybe_send_first_invite_response(SIPE_MEDIA_CALL_PRIVATE);
1073 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE->sipe_private);
1076 static void
1077 call_reject_cb(struct sipe_media_call *call, gboolean local)
1079 if (local) {
1080 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
1082 sip_transport_response(call_private->sipe_private,
1083 call_private->invitation,
1084 603, "Decline", NULL);
1086 if (call_private->session) {
1087 sipe_session_remove(call_private->sipe_private,
1088 call_private->session);
1089 call_private->session = NULL;
1094 static void
1095 av_call_reject_cb(struct sipe_media_call *call, gboolean local)
1097 if (!local) {
1098 struct sipe_core_private *sipe_private;
1099 gchar *desc;
1101 sipe_private = SIPE_MEDIA_CALL_PRIVATE->sipe_private;
1103 desc = g_strdup_printf(_("User %s rejected call"), call->with);
1104 sipe_backend_notify_error(SIPE_CORE_PUBLIC, _("Call rejected"),
1105 desc);
1106 g_free(desc);
1109 call_reject_cb(call, local);
1112 static gboolean
1113 sipe_media_send_ack(struct sipe_core_private *sipe_private, struct sipmsg *msg,
1114 struct transaction *trans);
1116 static void call_hold_cb(struct sipe_media_call *call,
1117 gboolean local,
1118 SIPE_UNUSED_PARAMETER gboolean state)
1120 if (local) {
1121 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE, sipe_media_send_ack);
1125 static void call_hangup_cb(struct sipe_media_call *call, gboolean local)
1127 if (local) {
1128 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
1130 if (call_private->session) {
1131 sipe_session_close(call_private->sipe_private,
1132 call_private->session);
1133 call_private->session = NULL;
1138 static void
1139 error_cb(struct sipe_media_call *call, gchar *message)
1141 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
1142 struct sipe_core_private *sipe_private = call_private->sipe_private;
1143 gboolean initiator = sipe_backend_media_is_initiator(call, NULL);
1144 gboolean accepted = sipe_backend_media_accepted(call->backend_private);
1146 gchar *title = g_strdup_printf("Call with %s failed", call->with);
1147 sipe_backend_notify_error(SIPE_CORE_PUBLIC, title, message);
1148 g_free(title);
1150 if (!initiator && !accepted && call_private->invitation) {
1151 sip_transport_response(sipe_private,
1152 call_private->invitation,
1153 488, "Not Acceptable Here", NULL);
1157 struct sipe_media_call *
1158 sipe_media_call_new(struct sipe_core_private *sipe_private, const gchar* with,
1159 struct sipmsg *msg, SipeIceVersion ice_version,
1160 SipeMediaCallFlags flags)
1162 struct sipe_media_call_private *call_private;
1163 struct sip_session *session;
1164 struct sip_dialog *dialog;
1165 gchar *cname;
1167 session = sipe_session_add_call(sipe_private, with);
1169 dialog = sipe_dialog_add(session);
1170 dialog->with = g_strdup(with);
1172 if (msg) {
1173 sipmsg_update_to_header_tag(msg);
1174 dialog->callid = g_strdup(sipmsg_find_header(msg, "Call-ID"));
1175 sipe_dialog_parse(dialog, msg, FALSE);
1176 } else {
1177 dialog->callid = gencallid();
1178 dialog->ourtag = gentag();
1179 flags |= SIPE_MEDIA_CALL_INITIATOR;
1182 if (g_hash_table_lookup(sipe_private->media_calls, dialog->callid)) {
1183 SIPE_DEBUG_ERROR("sipe_media_call_new: call already exists for "
1184 "Call-ID %s", dialog->callid);
1185 sipe_session_remove(sipe_private, session);
1186 return NULL;
1189 call_private = g_new0(struct sipe_media_call_private, 1);
1190 call_private->sipe_private = sipe_private;
1191 call_private->session = session;
1192 SIPE_MEDIA_CALL->with = g_strdup(with);
1194 g_hash_table_insert(sipe_private->media_calls,
1195 g_strdup(dialog->callid), call_private);
1197 cname = g_strdup(sipe_private->contact + 1);
1198 cname[strlen(cname) - 1] = '\0';
1200 call_private->public.backend_private = sipe_backend_media_new(SIPE_CORE_PUBLIC,
1201 SIPE_MEDIA_CALL,
1202 with,
1203 flags);
1204 sipe_backend_media_set_cname(call_private->public.backend_private, cname);
1206 call_private->ice_version = ice_version;
1207 call_private->encryption_compatible = TRUE;
1209 call_private->public.stream_initialized_cb = stream_initialized_cb;
1210 call_private->public.media_end_cb = media_end_cb;
1211 call_private->public.call_accept_cb = call_accept_cb;
1212 call_private->public.call_reject_cb = call_reject_cb;
1213 call_private->public.call_hold_cb = call_hold_cb;
1214 call_private->public.call_hangup_cb = call_hangup_cb;
1215 call_private->public.error_cb = error_cb;
1217 g_free(cname);
1219 return SIPE_MEDIA_CALL;
1222 static gboolean
1223 find_call_cb(SIPE_UNUSED_PARAMETER const gchar *callid,
1224 struct sipe_media_call *call,
1225 const gchar *with)
1227 return sipe_strequal(call->with, with);
1230 struct sipe_media_call *
1231 sipe_media_call_find(struct sipe_core_private *sipe_private, const gchar *with)
1233 return g_hash_table_find(sipe_private->media_calls,
1234 (GHRFunc)find_call_cb,
1235 (gpointer)with);
1238 void sipe_media_hangup(struct sipe_media_call_private *call_private)
1240 if (call_private) {
1241 sipe_backend_media_hangup(call_private->public.backend_private,
1242 FALSE);
1246 static gint
1247 ssrc_range_compare(const struct ssrc_range *a, const struct ssrc_range *b)
1249 if (a->begin < b->begin) {
1250 return -1;
1252 if (a->begin > b->begin) {
1253 return 1;
1255 return 0;
1258 static void
1259 ssrc_range_update(GSList **ranges, GSList *media)
1261 for (; media; media = media->next) {
1262 struct sdpmedia *m;
1263 const char *ssrc_range;
1264 gchar **parts;
1266 m = media->data;
1267 ssrc_range = sipe_utils_nameval_find(m->attributes,
1268 "x-ssrc-range");
1269 if (!ssrc_range) {
1270 continue;
1273 parts = g_strsplit(ssrc_range, "-", 2);
1275 if (parts[0] && parts[1]) {
1276 struct ssrc_range *range;
1278 range = g_new0(struct ssrc_range, 1);
1279 range->begin = atoi(parts[0]);
1280 range->end = atoi(parts[1]);
1282 *ranges = sipe_utils_slist_insert_unique_sorted(
1283 *ranges, range,
1284 (GCompareFunc)ssrc_range_compare,
1285 g_free);
1288 g_strfreev(parts);
1292 static struct ssrc_range *
1293 ssrc_range_allocate(GSList **ranges, guint32 len)
1295 struct ssrc_range *range;
1296 GSList *i;
1298 range = g_new0(struct ssrc_range, 1);
1299 range->begin = 1;
1300 range->end = range->begin + (len - 1);
1302 for (i = *ranges; i; i = i->next) {
1303 struct ssrc_range *r = i->data;
1305 if (range->begin < r->begin && range->end < r->begin) {
1306 break;
1309 range->begin = r->end + 1;
1310 range->end = range->begin + (len - 1);
1313 /* As per [MS-SDPEXT] 3.1.5.31.1, a SSRC MUST be from 1 to 4294967040
1314 * inclusive. */
1315 if (range->begin > range->end || range->end > 0xFFFFFF00) {
1316 g_free(range);
1317 SIPE_DEBUG_ERROR("Couldn't allocate SSRC range of %u", len);
1318 return NULL;
1321 *ranges = g_slist_insert_sorted(*ranges, range,
1322 (GCompareFunc)ssrc_range_compare);
1324 return range;
1327 struct sipe_media_stream *
1328 sipe_media_stream_add(struct sipe_media_call *call, const gchar *id,
1329 SipeMediaType type, SipeIceVersion ice_version,
1330 gboolean initiator, guint32 ssrc_count)
1332 struct sipe_core_private *sipe_private;
1333 struct sipe_media_stream_private *stream_private;
1334 struct sipe_backend_media_relays *backend_media_relays;
1335 guint min_port;
1336 guint max_port;
1338 sipe_private = SIPE_MEDIA_CALL_PRIVATE->sipe_private;
1340 backend_media_relays = sipe_backend_media_relays_convert(
1341 sipe_private->media_relays,
1342 sipe_private->media_relay_username,
1343 sipe_private->media_relay_password);
1345 min_port = sipe_private->min_media_port;
1346 max_port = sipe_private->max_media_port;
1347 switch (type) {
1348 case SIPE_MEDIA_AUDIO:
1349 min_port = sipe_private->min_audio_port;
1350 max_port = sipe_private->max_audio_port;
1351 break;
1352 case SIPE_MEDIA_VIDEO:
1353 min_port = sipe_private->min_video_port;
1354 max_port = sipe_private->max_audio_port;
1355 break;
1356 case SIPE_MEDIA_APPLICATION:
1357 if (sipe_strequal(id, "data")) {
1358 min_port = sipe_private->min_filetransfer_port;
1359 max_port = sipe_private->max_filetransfer_port;
1360 } else if (sipe_strequal(id, "applicationsharing")) {
1361 min_port = sipe_private->min_appsharing_port;
1362 max_port = sipe_private->max_appsharing_port;
1364 break;
1367 stream_private = g_new0(struct sipe_media_stream_private, 1);
1368 SIPE_MEDIA_STREAM->call = call;
1369 SIPE_MEDIA_STREAM->id = g_strdup(id);
1370 stream_private->write_queue = g_queue_new();
1371 stream_private->async_reads = g_queue_new();
1373 if (ssrc_count > 0) {
1374 SIPE_MEDIA_STREAM->ssrc_range =
1375 ssrc_range_allocate(&SIPE_MEDIA_CALL_PRIVATE->ssrc_ranges,
1376 ssrc_count);
1379 SIPE_MEDIA_STREAM->backend_private =
1380 sipe_backend_media_add_stream(SIPE_MEDIA_STREAM,
1381 type, ice_version,
1382 initiator,
1383 backend_media_relays,
1384 min_port, max_port);
1386 sipe_backend_media_relays_free(backend_media_relays);
1388 if (!SIPE_MEDIA_STREAM->backend_private) {
1389 sipe_media_stream_free(stream_private);
1390 return NULL;
1393 if (type == SIPE_MEDIA_VIDEO) {
1394 /* Declare that we can send and receive Video Source Requests
1395 * as per [MS-SDPEXT] 3.1.5.30.2. */
1396 sipe_media_stream_add_extra_attribute(SIPE_MEDIA_STREAM,
1397 "rtcp-fb", "* x-message app send:src recv:src");
1399 sipe_media_stream_add_extra_attribute(SIPE_MEDIA_STREAM,
1400 "rtcp-rsize", NULL);
1401 sipe_media_stream_add_extra_attribute(SIPE_MEDIA_STREAM,
1402 "label", "main-video");
1403 sipe_media_stream_add_extra_attribute(SIPE_MEDIA_STREAM,
1404 "x-source", "main-video");
1407 #ifdef HAVE_SRTP
1408 if (get_encryption_policy(sipe_private) != SIPE_ENCRYPTION_POLICY_REJECTED) {
1409 int i;
1410 stream_private->encryption_key = g_new0(guchar, SIPE_SRTP_KEY_LEN);
1411 for (i = 0; i != SIPE_SRTP_KEY_LEN; ++i) {
1412 stream_private->encryption_key[i] = rand() & 0xff;
1414 stream_private->encryption_key_id = 1;
1416 #endif
1418 SIPE_MEDIA_CALL_PRIVATE->streams =
1419 g_slist_append(SIPE_MEDIA_CALL_PRIVATE->streams,
1420 stream_private);
1422 return SIPE_MEDIA_STREAM;
1425 static void
1426 append_2007_fallback_if_needed(struct sipe_media_call_private *call_private)
1428 struct sipe_core_private *sipe_private = call_private->sipe_private;
1429 const gchar *marker = sip_transport_sdp_address_marker(sipe_private);
1430 const gchar *ip = sip_transport_ip_address(sipe_private);
1431 gchar *body;
1433 if (SIPE_CORE_PRIVATE_FLAG_IS(SFB) ||
1434 sipe_media_get_sip_dialog(SIPE_MEDIA_CALL)->cseq != 0 ||
1435 call_private->ice_version != SIPE_ICE_RFC_5245 ||
1436 sipe_strequal(SIPE_MEDIA_CALL->with, sipe_private->test_call_bot_uri)) {
1437 return;
1440 body = g_strdup_printf("Content-Type: application/sdp\r\n"
1441 "Content-Transfer-Encoding: 7bit\r\n"
1442 "Content-Disposition: session; handling=optional; ms-proxy-2007fallback\r\n"
1443 "\r\n"
1444 "o=- 0 0 IN %s %s\r\n"
1445 "s=session\r\n"
1446 "c=IN %s %s\r\n"
1447 "m=audio 0 RTP/AVP\r\n",
1448 marker, ip,
1449 marker, ip);
1450 sipe_media_add_extra_invite_section(SIPE_MEDIA_CALL,
1451 "multipart/alternative", body);
1454 static void
1455 sipe_media_initiate_call(struct sipe_core_private *sipe_private,
1456 const char *with, SipeIceVersion ice_version,
1457 gboolean with_video)
1459 struct sipe_media_call_private *call_private;
1461 if (sipe_core_media_get_call(SIPE_CORE_PUBLIC)) {
1462 return;
1465 call_private = (struct sipe_media_call_private *)
1466 sipe_media_call_new(sipe_private, with, NULL,
1467 ice_version, 0);
1469 SIPE_MEDIA_CALL->call_reject_cb = av_call_reject_cb;
1471 if (!sipe_media_stream_add(SIPE_MEDIA_CALL, "audio", SIPE_MEDIA_AUDIO,
1472 call_private->ice_version,
1473 TRUE, 0)) {
1474 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
1475 _("Error occurred"),
1476 _("Error creating audio stream"));
1477 sipe_media_hangup(call_private);
1478 return;
1481 if (with_video &&
1482 !sipe_media_stream_add(SIPE_MEDIA_CALL, "video", SIPE_MEDIA_VIDEO,
1483 call_private->ice_version,
1484 TRUE, VIDEO_SSRC_COUNT)) {
1485 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
1486 _("Error occurred"),
1487 _("Error creating video stream"));
1488 sipe_media_hangup(call_private);
1489 return;
1492 append_2007_fallback_if_needed(call_private);
1494 call_schedule_request_timeout(SIPE_MEDIA_CALL);
1495 // Processing continues in stream_initialized_cb
1498 void
1499 sipe_core_media_initiate_call(struct sipe_core_public *sipe_public,
1500 const char *with,
1501 gboolean with_video)
1503 sipe_media_initiate_call(SIPE_CORE_PRIVATE, with,
1504 SIPE_ICE_RFC_5245, with_video);
1507 static void
1508 conference_audio_muted_cb(struct sipe_media_stream *stream, gboolean is_muted)
1510 struct sipe_media_call *call = stream->call;
1512 if (!SIPE_MEDIA_CALL_PRIVATE->conference_session) {
1513 return;
1516 sipe_conf_announce_audio_mute_state(SIPE_MEDIA_CALL_PRIVATE->sipe_private,
1517 SIPE_MEDIA_CALL_PRIVATE->conference_session,
1518 is_muted);
1521 void sipe_core_media_connect_conference(struct sipe_core_public *sipe_public,
1522 struct sipe_chat_session *chat_session)
1524 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1525 struct sipe_media_call_private *call_private;
1526 struct sipe_media_stream *stream;
1527 struct sip_session *session;
1528 SipeIceVersion ice_version;
1529 gchar *av_uri;
1531 if (!sipe_conf_supports_mcu_type(sipe_private, "audio-video")) {
1532 sipe_backend_notify_error(sipe_public, _("Join conference call"),
1533 _("Conference calls are not supported on this server."));
1534 return;
1537 session = sipe_session_find_chat(sipe_private, chat_session);
1539 if (sipe_core_media_get_call(sipe_public) || !session) {
1540 return;
1543 av_uri = sipe_conf_build_uri(sipe_core_chat_id(sipe_public, chat_session),
1544 "audio-video");
1545 if (!av_uri) {
1546 return;
1549 session->is_call = TRUE;
1551 ice_version = SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013) ? SIPE_ICE_RFC_5245 :
1552 SIPE_ICE_DRAFT_6;
1554 call_private = (struct sipe_media_call_private *)
1555 sipe_media_call_new(sipe_private, av_uri, NULL,
1556 ice_version, 0);
1557 call_private->conference_session = session;
1558 SIPE_MEDIA_CALL->call_reject_cb = av_call_reject_cb;
1560 stream = sipe_media_stream_add(SIPE_MEDIA_CALL, "audio",
1561 SIPE_MEDIA_AUDIO,
1562 call_private->ice_version,
1563 TRUE, 0);
1564 if (!stream) {
1565 sipe_backend_notify_error(sipe_public,
1566 _("Error occurred"),
1567 _("Error creating audio stream"));
1569 sipe_media_hangup(call_private);
1572 stream->mute_cb = conference_audio_muted_cb;
1574 g_free(av_uri);
1576 // Processing continues in stream_initialized_cb
1579 struct sipe_media_call *
1580 sipe_core_media_get_call(struct sipe_core_public *sipe_public)
1582 struct sipe_media_call * result = NULL;
1583 GList *calls = g_hash_table_get_values(SIPE_CORE_PRIVATE->media_calls);
1584 GList *entry = calls;
1586 while (entry) {
1587 if (sipe_core_media_get_stream_by_id(entry->data, "audio")) {
1588 result = entry->data;
1589 break;
1591 entry = entry->next;
1593 g_list_free(calls);
1595 return result;
1598 static gboolean phone_number_is_valid(const gchar *phone_number)
1600 if (!phone_number || sipe_strequal(phone_number, "")) {
1601 return FALSE;
1604 if (*phone_number == '+') {
1605 ++phone_number;
1608 while (*phone_number != '\0') {
1609 if (!g_ascii_isdigit(*phone_number)) {
1610 return FALSE;
1612 ++phone_number;
1615 return TRUE;
1618 void sipe_core_media_phone_call(struct sipe_core_public *sipe_public,
1619 const gchar *phone_number)
1621 g_return_if_fail(sipe_public);
1623 SIPE_DEBUG_INFO("sipe_core_media_phone_call: %s", phone_number ? phone_number : "(null)");
1625 if (phone_number_is_valid(phone_number)) {
1626 gchar *phone_uri = g_strdup_printf("sip:%s@%s;user=phone",
1627 phone_number, sipe_public->sip_domain);
1629 sipe_core_media_initiate_call(sipe_public, phone_uri, FALSE);
1631 g_free(phone_uri);
1632 } else {
1633 sipe_backend_notify_error(sipe_public,
1634 _("Unable to establish a call"),
1635 _("Invalid phone number"));
1639 void sipe_core_media_test_call(struct sipe_core_public *sipe_public)
1641 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1642 if (!sipe_private->test_call_bot_uri) {
1643 sipe_backend_notify_error(sipe_public,
1644 _("Unable to establish a call"),
1645 _("Audio Test Service is not available."));
1646 return;
1649 sipe_core_media_initiate_call(sipe_public,
1650 sipe_private->test_call_bot_uri, FALSE);
1653 static struct sipe_media_call_private *
1654 sipe_media_from_sipmsg(struct sipe_core_private *sipe_private,
1655 struct sipmsg *msg)
1657 return g_hash_table_lookup(sipe_private->media_calls,
1658 sipmsg_find_header(msg, "Call-ID"));
1661 static void
1662 transport_response_unsupported_sdp(struct sipe_core_private *sipe_private,
1663 struct sipmsg *msg)
1665 sipmsg_add_header(msg, "ms-client-diagnostics",
1666 "52063;reason=\"Unsupported session description\"");
1667 sip_transport_response(sipe_private, msg,
1668 488, "Not Acceptable Here", NULL);
1671 static void
1672 maybe_send_second_invite_response(struct sipe_media_call_private *call_private)
1674 GSList *it;
1676 /* Second INVITE request had to be received and all streams must have
1677 * established candidate pairs before the response can be sent. */
1679 if (!call_private->invitation) {
1680 return;
1683 for (it = call_private->streams; it; it = it->next) {
1684 struct sipe_media_stream_private *stream_private = it->data;
1685 if (!stream_private->established) {
1686 return;
1690 send_response_with_session_description(call_private, 200, "OK");
1692 #ifdef HAVE_XDATA
1693 for (it = call_private->streams; it; it = it->next) {
1694 struct sipe_media_stream_private *stream_private = it->data;
1696 stream_private->sdp_negotiation_concluded = TRUE;
1697 if (stream_private->writable) {
1698 // We've become writable.
1699 sipe_core_media_stream_writable(SIPE_MEDIA_STREAM, TRUE);
1702 #endif
1705 struct sipe_media_call *
1706 process_incoming_invite_call(struct sipe_core_private *sipe_private,
1707 struct sipmsg *msg,
1708 const gchar *sdp)
1710 return(process_incoming_invite_call_parsed_sdp(sipe_private,
1711 msg,
1712 sdpmsg_parse_msg(sdp)));
1715 struct sipe_media_call *
1716 process_incoming_invite_call_parsed_sdp(struct sipe_core_private *sipe_private,
1717 struct sipmsg *msg,
1718 struct sdpmsg *smsg)
1720 struct sipe_media_call_private *call_private;
1721 gboolean has_new_media = FALSE;
1722 GSList *i;
1724 // Don't allow two voice calls in parallel.
1725 if (!strstr(msg->body, "m=data") &&
1726 !strstr(msg->body, "m=applicationsharing")) {
1727 struct sipe_media_call *call =
1728 sipe_core_media_get_call(SIPE_CORE_PUBLIC);
1729 if (call && !is_media_session_msg(SIPE_MEDIA_CALL_PRIVATE, msg)) {
1730 sip_transport_response(sipe_private, msg,
1731 486, "Busy Here", NULL);
1732 sdpmsg_free(smsg);
1733 return NULL;
1737 call_private = sipe_media_from_sipmsg(sipe_private, msg);
1739 if (call_private) {
1740 char *self = sip_uri_self(sipe_private);
1741 if (sipe_strequal(SIPE_MEDIA_CALL->with, self)) {
1742 g_free(self);
1743 sip_transport_response(sipe_private, msg, 488, "Not Acceptable Here", NULL);
1744 sdpmsg_free(smsg);
1745 return NULL;
1747 g_free(self);
1750 if (!smsg) {
1751 transport_response_unsupported_sdp(sipe_private, msg);
1752 if (call_private) {
1753 sipe_media_hangup(call_private);
1755 return NULL;
1758 if (!call_private) {
1759 gchar *with = sipmsg_parse_from_address(msg);
1760 SipeMediaCallFlags flags = 0;
1762 if (strstr(msg->body, "m=data") ||
1763 strstr(msg->body, "m=applicationsharing")) {
1764 flags |= SIPE_MEDIA_CALL_NO_UI;
1767 call_private = (struct sipe_media_call_private *)
1768 sipe_media_call_new(sipe_private, with,
1769 msg, smsg->ice_version,
1770 flags);
1772 if (!(flags & SIPE_MEDIA_CALL_NO_UI)) {
1773 SIPE_MEDIA_CALL->call_reject_cb = av_call_reject_cb;
1775 g_free(with);
1778 if (call_private->invitation)
1779 sipmsg_free(call_private->invitation);
1780 call_private->invitation = sipmsg_copy(msg);
1782 ssrc_range_update(&call_private->ssrc_ranges, smsg->media);
1784 // Create any new media streams
1785 for (i = smsg->media; i; i = i->next) {
1786 struct sdpmedia *media = i->data;
1787 gchar *id = media->name;
1788 SipeMediaType type;
1790 if ( media->port != 0
1791 && !sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL, id)) {
1792 guint32 ssrc_count = 0;
1794 if (sipe_strequal(id, "audio"))
1795 type = SIPE_MEDIA_AUDIO;
1796 else if (sipe_strequal(id, "video")) {
1797 type = SIPE_MEDIA_VIDEO;
1798 ssrc_count = VIDEO_SSRC_COUNT;
1799 } else if (sipe_strequal(id, "data"))
1800 type = SIPE_MEDIA_APPLICATION;
1801 else if (sipe_strequal(id, "applicationsharing"))
1802 type = SIPE_MEDIA_APPLICATION;
1803 else
1804 continue;
1806 sipe_media_stream_add(SIPE_MEDIA_CALL, id, type,
1807 smsg->ice_version, FALSE,
1808 ssrc_count);
1809 has_new_media = TRUE;
1813 if (has_new_media) {
1814 sdpmsg_free(call_private->smsg);
1815 call_private->smsg = smsg;
1816 sip_transport_response(sipe_private, call_private->invitation,
1817 180, "Ringing", NULL);
1818 call_schedule_ringing_timeout(SIPE_MEDIA_CALL);
1819 // Processing continues in stream_initialized_cb
1820 } else {
1821 apply_remote_message(call_private, smsg);
1822 sdpmsg_free(smsg);
1823 maybe_send_second_invite_response(call_private);
1826 return SIPE_MEDIA_CALL;
1829 void process_incoming_cancel_call(struct sipe_media_call_private *call_private,
1830 struct sipmsg *msg)
1832 // We respond to the CANCEL request with 200 OK response and
1833 // with 487 Request Terminated to the remote INVITE in progress.
1834 sip_transport_response(call_private->sipe_private, msg, 200, "OK", NULL);
1836 if (call_private->invitation) {
1837 sip_transport_response(call_private->sipe_private,
1838 call_private->invitation,
1839 487, "Request Terminated", NULL);
1842 sipe_backend_media_reject(SIPE_MEDIA_CALL->backend_private, FALSE);
1845 static gboolean
1846 sipe_media_send_ack(struct sipe_core_private *sipe_private,
1847 struct sipmsg *msg,
1848 struct transaction *trans)
1850 struct sipe_media_call_private *call_private;
1851 struct sip_dialog *dialog;
1852 int tmp_cseq;
1854 call_private = sipe_media_from_sipmsg(sipe_private, msg);
1856 if (!is_media_session_msg(call_private, msg))
1857 return FALSE;
1859 dialog = sipe_media_get_sip_dialog(SIPE_MEDIA_CALL);
1860 if (!dialog)
1861 return FALSE;
1863 tmp_cseq = dialog->cseq;
1865 dialog->cseq = sip_transaction_cseq(trans) - 1;
1866 sip_transport_ack(sipe_private, dialog);
1867 dialog->cseq = tmp_cseq;
1869 dialog->outgoing_invite = NULL;
1871 return TRUE;
1874 static gboolean
1875 sipe_media_send_final_ack(struct sipe_core_private *sipe_private,
1876 struct sipmsg *msg,
1877 struct transaction *trans)
1879 struct sipe_media_call_private *call_private;
1880 #ifdef HAVE_XDATA
1881 GSList *it;
1882 #endif
1884 if (!sipe_media_send_ack(sipe_private, msg, trans))
1885 return FALSE;
1887 call_private = sipe_media_from_sipmsg(sipe_private, msg);
1889 sipe_backend_media_accept(SIPE_MEDIA_CALL->backend_private, FALSE);
1891 #ifdef HAVE_XDATA
1892 for (it = call_private->streams; it; it = it->next) {
1893 struct sipe_media_stream_private *stream_private = it->data;
1895 stream_private->sdp_negotiation_concluded = TRUE;
1896 if (stream_private->writable) {
1897 // We've become writable.
1898 sipe_core_media_stream_writable(SIPE_MEDIA_STREAM, TRUE);
1901 #endif
1903 return TRUE;
1906 void
1907 sipe_core_media_stream_candidate_pair_established(struct sipe_media_stream *stream)
1909 struct sipe_media_call *call = stream->call;
1911 GList *active_candidates =
1912 sipe_backend_media_stream_get_active_local_candidates(stream);
1913 guint ready_components = g_list_length(active_candidates);
1915 sipe_media_candidate_list_free(active_candidates);
1917 if (ready_components != 2) {
1918 // We must have both RTP+RTCP candidate pairs established first.
1919 return;
1922 if (SIPE_MEDIA_STREAM_PRIVATE->established) {
1923 return;
1925 SIPE_MEDIA_STREAM_PRIVATE->established = TRUE;
1927 stream_schedule_cancel_timeout(call, SIPE_MEDIA_STREAM_PRIVATE);
1929 if (stream->candidate_pairs_established_cb) {
1930 stream->candidate_pairs_established_cb(stream);
1933 if (sipe_backend_media_is_initiator(stream->call, NULL)) {
1934 GSList *streams = SIPE_MEDIA_CALL_PRIVATE->streams;
1935 for (; streams; streams = streams->next) {
1936 struct sipe_media_stream_private *s = streams->data;
1937 if (!s->established) {
1938 break;
1942 if (streams == NULL) {
1943 // All call streams have been established.
1944 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE,
1945 sipe_media_send_final_ack);
1947 } else {
1948 maybe_send_second_invite_response(SIPE_MEDIA_CALL_PRIVATE);
1952 static gboolean
1953 maybe_retry_call_with_ice_version(struct sipe_core_private *sipe_private,
1954 struct sipe_media_call_private *call_private,
1955 SipeIceVersion ice_version,
1956 struct transaction *trans)
1958 if (call_private->ice_version != ice_version &&
1959 sip_transaction_cseq(trans) == 1) {
1960 GSList *i;
1961 gchar *with;
1962 gboolean with_video = FALSE;
1964 for (i = call_private->streams; i; i = i->next) {
1965 struct sipe_media_stream *stream = i->data;
1967 if (sipe_strequal(stream->id, "video")) {
1968 with_video = TRUE;
1969 } else if (!sipe_strequal(stream->id, "audio")) {
1970 /* Don't retry calls which are neither audio
1971 * nor video. */
1972 return FALSE;
1976 with = g_strdup(SIPE_MEDIA_CALL->with);
1978 sipe_media_hangup(call_private);
1979 SIPE_DEBUG_INFO("Retrying call with ICEv%d.",
1980 ice_version == SIPE_ICE_DRAFT_6 ? 6 : 19);
1981 sipe_media_initiate_call(sipe_private,
1982 with,
1983 ice_version,
1984 with_video);
1986 g_free(with);
1987 return TRUE;
1990 return FALSE;
1993 static gboolean
1994 process_invite_call_response(struct sipe_core_private *sipe_private,
1995 struct sipmsg *msg,
1996 struct transaction *trans)
1998 const gchar *with;
1999 struct sipe_media_call_private *call_private;
2000 struct sip_dialog *dialog;
2001 struct sdpmsg *smsg;
2003 call_private = sipe_media_from_sipmsg(sipe_private,msg);
2005 if (!is_media_session_msg(call_private, msg))
2006 return FALSE;
2008 dialog = sipe_media_get_sip_dialog(SIPE_MEDIA_CALL);
2010 with = dialog->with;
2012 dialog->outgoing_invite = NULL;
2014 if (msg->response == 603 || msg->response == 605) {
2015 // Call rejected by remote peer
2016 sipe_media_send_ack(sipe_private, msg, trans);
2017 sipe_backend_media_reject(SIPE_MEDIA_CALL->backend_private, FALSE);
2019 return TRUE;
2022 if (msg->response >= 400) {
2023 // An error occurred
2024 const gchar *title;
2025 GString *desc = g_string_new("");
2026 gboolean append_responsestr = FALSE;
2028 switch (msg->response) {
2029 case 480: {
2030 title = _("User unavailable");
2032 if (sipmsg_parse_warning(msg, NULL) == 391) {
2033 g_string_append_printf(desc, _("%s does not want to be disturbed"), with);
2034 } else
2035 g_string_append_printf(desc, _("User %s is not available"), with);
2036 break;
2038 case 415:
2039 // OCS/Lync really sends response string with 'Mutipart' typo.
2040 if (sipe_strequal(msg->responsestr, "Mutipart mime in content type not supported by Archiving CDR service") &&
2041 maybe_retry_call_with_ice_version(sipe_private,
2042 call_private,
2043 SIPE_ICE_DRAFT_6,
2044 trans)) {
2045 return TRUE;
2047 title = _("Unsupported media type");
2048 break;
2049 case 488: {
2050 /* Check for incompatible encryption levels error.
2052 * MS Lync 2010:
2053 * 488 Not Acceptable Here
2054 * ms-client-diagnostics: 52017;reason="Encryption levels dont match"
2056 * older clients (and SIPE itself):
2057 * 488 Encryption Levels not compatible
2059 const gchar *ms_diag = sipmsg_find_header(msg, "ms-client-diagnostics");
2060 SipeIceVersion retry_ice_version = SIPE_ICE_DRAFT_6;
2062 if (sipe_strequal(msg->responsestr, "Encryption Levels not compatible") ||
2063 (ms_diag && g_str_has_prefix(ms_diag, "52017;"))) {
2064 title = _("Unable to establish a call");
2065 g_string_append(desc, _("Encryption settings of peer are incompatible with ours."));
2066 break;
2069 /* Check if this is failed conference using
2070 * ICEv6 with reason "Error parsing SDP" and
2071 * retry using ICEv19. */
2072 ms_diag = sipmsg_find_header(msg, "ms-diagnostics");
2073 if (ms_diag && g_str_has_prefix(ms_diag, "7008;")) {
2074 retry_ice_version = SIPE_ICE_RFC_5245;
2077 if (maybe_retry_call_with_ice_version(sipe_private,
2078 call_private,
2079 retry_ice_version,
2080 trans)) {
2081 return TRUE;
2083 SIPE_FALLTHROUGH
2085 default:
2086 title = _("Error occurred");
2087 g_string_append(desc, _("Unable to establish a call"));
2088 append_responsestr = TRUE;
2089 break;
2092 if (append_responsestr) {
2093 gchar *reason = sipmsg_get_ms_diagnostics_reason(msg);
2095 g_string_append_printf(desc, "\n%d %s",
2096 msg->response, msg->responsestr);
2097 if (reason) {
2098 g_string_append_printf(desc, "\n\n%s", reason);
2099 g_free(reason);
2103 sipe_backend_notify_error(SIPE_CORE_PUBLIC, title, desc->str);
2104 g_string_free(desc, TRUE);
2106 sipe_media_send_ack(sipe_private, msg, trans);
2107 sipe_media_hangup(call_private);
2109 return TRUE;
2112 sipe_dialog_parse(dialog, msg, TRUE);
2113 smsg = sdpmsg_parse_msg(msg->body);
2114 if (!smsg) {
2115 transport_response_unsupported_sdp(sipe_private, msg);
2116 sipe_media_hangup(call_private);
2117 return FALSE;
2120 ssrc_range_update(&call_private->ssrc_ranges, smsg->media);
2121 apply_remote_message(call_private, smsg);
2122 sdpmsg_free(smsg);
2124 stream_schedule_timeout(SIPE_MEDIA_CALL);
2125 call_schedule_cancel_request_timeout(SIPE_MEDIA_CALL);
2126 sipe_media_send_ack(sipe_private, msg, trans);
2128 return TRUE;
2130 // Waits until sipe_core_media_candidate_pair_established() is invoked.
2133 gboolean is_media_session_msg(struct sipe_media_call_private *call_private,
2134 struct sipmsg *msg)
2136 if (!call_private) {
2137 return FALSE;
2140 return sipe_media_from_sipmsg(call_private->sipe_private, msg) == call_private;
2143 static void
2144 end_call(SIPE_UNUSED_PARAMETER gpointer key,
2145 struct sipe_media_call_private *call_private,
2146 SIPE_UNUSED_PARAMETER gpointer user_data)
2148 struct sipe_backend_media *backend_private;
2150 backend_private = call_private->public.backend_private;
2152 if (!sipe_backend_media_is_initiator(SIPE_MEDIA_CALL, NULL) &&
2153 !sipe_backend_media_accepted(backend_private)) {
2154 sip_transport_response(call_private->sipe_private,
2155 call_private->invitation,
2156 480, "Temporarily Unavailable", NULL);
2157 } else if (call_private->session) {
2158 sipe_session_close(call_private->sipe_private,
2159 call_private->session);
2160 call_private->session = NULL;
2163 sipe_media_hangup(call_private);
2166 void
2167 sipe_media_handle_going_offline(struct sipe_core_private *sipe_private)
2169 g_hash_table_foreach(sipe_private->media_calls, (GHFunc) end_call, NULL);
2172 gboolean sipe_media_is_conference_call(struct sipe_media_call_private *call_private)
2174 return g_strstr_len(SIPE_MEDIA_CALL->with, -1, "app:conf:audio-video:") != NULL;
2177 struct sipe_core_private *
2178 sipe_media_get_sipe_core_private(struct sipe_media_call *call)
2180 g_return_val_if_fail(call, NULL);
2182 return SIPE_MEDIA_CALL_PRIVATE->sipe_private;
2185 struct sip_dialog *
2186 sipe_media_get_sip_dialog(struct sipe_media_call *call)
2188 struct sip_session *session;
2190 g_return_val_if_fail(call, NULL);
2192 session = SIPE_MEDIA_CALL_PRIVATE->session;
2194 if (!session || !session->dialogs) {
2195 return NULL;
2198 return session->dialogs->data;
2201 static void
2202 sipe_media_relay_free(struct sipe_media_relay *relay)
2204 g_free(relay->hostname);
2205 if (relay->dns_query)
2206 sipe_backend_dns_query_cancel(relay->dns_query);
2207 g_free(relay);
2210 void
2211 sipe_media_relay_list_free(GSList *list)
2213 for (; list; list = g_slist_delete_link(list, list))
2214 sipe_media_relay_free(list->data);
2217 static void
2218 relay_ip_resolved_cb(struct sipe_media_relay* relay,
2219 const gchar *ip, SIPE_UNUSED_PARAMETER guint port)
2221 gchar *hostname = relay->hostname;
2222 relay->dns_query = NULL;
2224 if (ip && port) {
2225 relay->hostname = g_strdup(ip);
2226 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname, ip);
2227 } else {
2228 relay->hostname = NULL;
2229 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname);
2232 g_free(hostname);
2235 static gboolean
2236 process_get_av_edge_credentials_response(struct sipe_core_private *sipe_private,
2237 struct sipmsg *msg,
2238 SIPE_UNUSED_PARAMETER struct transaction *trans)
2240 g_free(sipe_private->media_relay_username);
2241 g_free(sipe_private->media_relay_password);
2242 sipe_media_relay_list_free(sipe_private->media_relays);
2243 sipe_private->media_relay_username = NULL;
2244 sipe_private->media_relay_password = NULL;
2245 sipe_private->media_relays = NULL;
2247 if (msg->response >= 400) {
2248 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
2249 "Failed to obtain A/V Edge credentials.");
2250 return FALSE;
2253 if (msg->response == 200) {
2254 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
2256 if (sipe_strequal("OK", sipe_xml_attribute(xn_response, "reasonPhrase"))) {
2257 const sipe_xml *xn_credentials = sipe_xml_child(xn_response, "credentialsResponse/credentials");
2258 const sipe_xml *xn_relays = sipe_xml_child(xn_response, "credentialsResponse/mediaRelayList");
2259 const sipe_xml *item;
2260 GSList *relays = NULL;
2262 item = sipe_xml_child(xn_credentials, "username");
2263 sipe_private->media_relay_username = sipe_xml_data(item);
2264 item = sipe_xml_child(xn_credentials, "password");
2265 sipe_private->media_relay_password = sipe_xml_data(item);
2267 for (item = sipe_xml_child(xn_relays, "mediaRelay"); item; item = sipe_xml_twin(item)) {
2268 struct sipe_media_relay *relay = g_new0(struct sipe_media_relay, 1);
2269 const sipe_xml *node;
2270 gchar *tmp;
2272 node = sipe_xml_child(item, "hostName");
2273 relay->hostname = sipe_xml_data(node);
2275 node = sipe_xml_child(item, "udpPort");
2276 if (node) {
2277 tmp = sipe_xml_data(node);
2278 if (tmp) {
2279 relay->udp_port = atoi(tmp);
2280 g_free(tmp);
2284 node = sipe_xml_child(item, "tcpPort");
2285 if (node) {
2286 tmp = sipe_xml_data(node);
2287 if (tmp) {
2288 relay->tcp_port = atoi(tmp);
2289 g_free(tmp);
2293 relays = g_slist_append(relays, relay);
2295 relay->dns_query = sipe_backend_dns_query_a(
2296 SIPE_CORE_PUBLIC,
2297 relay->hostname,
2298 relay->udp_port,
2299 (sipe_dns_resolved_cb) relay_ip_resolved_cb,
2300 relay);
2302 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
2303 relay->hostname,
2304 relay->tcp_port, relay->udp_port);
2307 sipe_private->media_relays = relays;
2310 sipe_xml_free(xn_response);
2313 return TRUE;
2316 void
2317 sipe_media_get_av_edge_credentials(struct sipe_core_private *sipe_private)
2319 // TODO: re-request credentials after duration expires?
2320 static const char CRED_REQUEST_XML[] =
2321 "<request requestID=\"%d\" "
2322 "from=\"%s\" "
2323 "version=\"1.0\" "
2324 "to=\"%s\" "
2325 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
2326 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
2327 "<credentialsRequest credentialsRequestID=\"%d\">"
2328 "<identity>%s</identity>"
2329 "<location>%s</location>"
2330 "<duration>480</duration>"
2331 "</credentialsRequest>"
2332 "</request>";
2334 int request_id = rand();
2335 gchar *self;
2336 gchar *body;
2338 if (!sipe_private->mras_uri)
2339 return;
2341 self = sip_uri_self(sipe_private);
2343 body = g_strdup_printf(
2344 CRED_REQUEST_XML,
2345 request_id,
2346 self,
2347 sipe_private->mras_uri,
2348 request_id,
2349 self,
2350 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER) ? "internet" : "intranet");
2351 g_free(self);
2353 sip_transport_service(sipe_private,
2354 sipe_private->mras_uri,
2355 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
2356 body,
2357 process_get_av_edge_credentials_response);
2359 g_free(body);
2362 void
2363 sipe_media_add_extra_invite_section(struct sipe_media_call *call,
2364 const gchar *invite_content_type,
2365 gchar *body)
2367 g_free(SIPE_MEDIA_CALL_PRIVATE->extra_invite_section);
2368 g_free(SIPE_MEDIA_CALL_PRIVATE->invite_content_type);
2369 SIPE_MEDIA_CALL_PRIVATE->extra_invite_section = body;
2370 SIPE_MEDIA_CALL_PRIVATE->invite_content_type =
2371 g_strdup(invite_content_type);
2374 void
2375 sipe_media_stream_add_extra_attribute(struct sipe_media_stream *stream,
2376 const gchar *name, const gchar *value)
2378 SIPE_MEDIA_STREAM_PRIVATE->extra_sdp =
2379 sipe_utils_nameval_add(SIPE_MEDIA_STREAM_PRIVATE->extra_sdp,
2380 name, value);
2383 #ifdef HAVE_XDATA
2384 void
2385 sipe_core_media_stream_readable(struct sipe_media_stream *stream)
2387 g_return_if_fail(stream);
2389 if (g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE->async_reads) &&
2390 stream->read_cb) {
2391 stream->read_cb(stream);
2394 while (!g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE->async_reads)) {
2395 struct async_read_data *data;
2396 guint8 *pos;
2397 gssize len;
2398 gssize bytes_read;
2400 data = g_queue_peek_head(SIPE_MEDIA_STREAM_PRIVATE->async_reads);
2401 pos = data->buffer + SIPE_MEDIA_STREAM_PRIVATE->read_pos;
2402 len = data->len - SIPE_MEDIA_STREAM_PRIVATE->read_pos;
2404 bytes_read = sipe_backend_media_stream_read(stream, pos, len);
2405 if (bytes_read == -1) {
2406 struct sipe_media_call *call = stream->call;
2407 struct sipe_core_private *sipe_private =
2408 SIPE_MEDIA_CALL_PRIVATE->sipe_private;
2410 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
2411 _("Media error"),
2412 _("Error while reading from stream"));
2413 sipe_media_hangup(SIPE_MEDIA_CALL_PRIVATE);
2414 return;
2417 SIPE_MEDIA_STREAM_PRIVATE->read_pos += bytes_read;
2419 if (SIPE_MEDIA_STREAM_PRIVATE->read_pos == data->len) {
2420 data->callback(stream, data->buffer, data->len);
2421 SIPE_MEDIA_STREAM_PRIVATE->read_pos = 0;
2422 g_queue_pop_head(SIPE_MEDIA_STREAM_PRIVATE->async_reads);
2423 g_free(data);
2424 } else {
2425 // Still not enough data to finish the read.
2426 return;
2431 void
2432 sipe_media_stream_read_async(struct sipe_media_stream *stream,
2433 gpointer buffer, gsize len,
2434 sipe_media_stream_read_callback callback)
2436 struct async_read_data *data;
2438 g_return_if_fail(stream && buffer && callback);
2440 data = g_new0(struct async_read_data, 1);
2441 data->buffer = buffer;
2442 data->len = len;
2443 data->callback = callback;
2445 g_queue_push_tail(SIPE_MEDIA_STREAM_PRIVATE->async_reads, data);
2448 static void
2449 stream_append_buffer(struct sipe_media_stream *stream,
2450 guint8 *buffer, guint len)
2452 GByteArray *b = g_byte_array_sized_new(len);
2453 g_byte_array_append(b, buffer, len);
2454 g_queue_push_tail(SIPE_MEDIA_STREAM_PRIVATE->write_queue, b);
2457 gboolean
2458 sipe_media_stream_write(struct sipe_media_stream *stream,
2459 gpointer buffer, gsize len)
2461 if (!sipe_media_stream_is_writable(stream)) {
2462 stream_append_buffer(stream, buffer, len);
2463 return FALSE;
2464 } else {
2465 guint written;
2467 written = sipe_backend_media_stream_write(stream, buffer, len);
2468 if (written == len) {
2469 return TRUE;
2472 stream_append_buffer(stream,
2473 (guint8 *)buffer + written, len - written);
2474 return FALSE;
2478 void
2479 sipe_core_media_stream_writable(struct sipe_media_stream *stream,
2480 gboolean writable)
2482 SIPE_MEDIA_STREAM_PRIVATE->writable = writable;
2484 if (!writable) {
2485 return;
2488 while (!g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE->write_queue)) {
2489 GByteArray *b;
2490 guint written;
2492 b = g_queue_peek_head(SIPE_MEDIA_STREAM_PRIVATE->write_queue);
2494 written = sipe_backend_media_stream_write(stream, b->data, b->len);
2495 if (written != b->len) {
2496 g_byte_array_remove_range(b, 0, written);
2497 return;
2500 g_byte_array_unref(b);
2501 g_queue_pop_head(SIPE_MEDIA_STREAM_PRIVATE->write_queue);
2504 if (sipe_media_stream_is_writable(stream) && stream->writable_cb) {
2505 stream->writable_cb(stream);
2509 gboolean
2510 sipe_media_stream_is_writable(struct sipe_media_stream *stream)
2512 return SIPE_MEDIA_STREAM_PRIVATE->writable &&
2513 SIPE_MEDIA_STREAM_PRIVATE->sdp_negotiation_concluded &&
2514 g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE->write_queue);
2516 #endif
2518 void
2519 sipe_media_stream_set_data(struct sipe_media_stream *stream, gpointer data,
2520 GDestroyNotify free_func)
2522 struct sipe_media_stream_private *stream_private =
2523 SIPE_MEDIA_STREAM_PRIVATE;
2525 g_return_if_fail(stream_private);
2527 if (stream_private->data && stream_private->data_free_func) {
2528 stream_private->data_free_func(stream_private->data);
2531 stream_private->data = data;
2532 stream_private->data_free_func = free_func;
2535 gpointer
2536 sipe_media_stream_get_data(struct sipe_media_stream *stream)
2538 g_return_val_if_fail(stream, NULL);
2540 return SIPE_MEDIA_STREAM_PRIVATE->data;
2544 Local Variables:
2545 mode: c
2546 c-file-style: "bsd"
2547 indent-tabs-mode: t
2548 tab-width: 8
2549 End: