media: Add a request timeout to cancel an unanswered invite
[siplcs.git] / src / core / sipe-media.c
blob47e05051d18a85f65cb8cc8c624be246ec2d0d55
1 /**
2 * @file sipe-media.c
4 * pidgin-sipe
6 * Copyright (C) 2011-2017 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 *timeout_key;
78 #define SIPE_MEDIA_CALL ((struct sipe_media_call *) call_private)
79 #define SIPE_MEDIA_CALL_PRIVATE ((struct sipe_media_call_private *) call)
81 struct sipe_media_stream_private {
82 struct sipe_media_stream public;
84 gchar *timeout_key;
86 guchar *encryption_key;
87 int encryption_key_id;
88 gboolean remote_candidates_and_codecs_set;
89 gboolean established;
90 #ifdef HAVE_XDATA
91 gboolean sdp_negotiation_concluded;
92 gboolean writable;
93 #endif
95 GSList *extra_sdp;
97 GQueue *write_queue;
98 GQueue *async_reads;
99 gssize read_pos;
101 /* User data associated with the stream. */
102 gpointer data;
103 GDestroyNotify data_free_func;
105 #define SIPE_MEDIA_STREAM ((struct sipe_media_stream *) stream_private)
106 #define SIPE_MEDIA_STREAM_PRIVATE ((struct sipe_media_stream_private *) stream)
108 #define SIPE_MEDIA_STREAM_CONNECTION_TIMEOUT_SECONDS 30
109 #define SIPE_MEDIA_CALL_TIMEOUT_SECONDS 120
111 struct async_read_data {
112 guint8 *buffer;
113 gssize len;
114 sipe_media_stream_read_callback callback;
117 static void stream_schedule_cancel_timeout(struct sipe_media_call *call,
118 struct sipe_media_stream_private *stream_private);
120 static void call_schedule_cancel_request_timeout(struct sipe_media_call *call);
122 static void sipe_media_codec_list_free(GList *codecs)
124 for (; codecs; codecs = g_list_delete_link(codecs, codecs))
125 sipe_backend_codec_free(codecs->data);
128 static void sipe_media_candidate_list_free(GList *candidates)
130 for (; candidates; candidates = g_list_delete_link(candidates, candidates))
131 sipe_backend_candidate_free(candidates->data);
134 static void
135 sipe_media_stream_free(struct sipe_media_stream_private *stream_private)
137 struct sipe_media_call_private *call_private;
139 call_private = (struct sipe_media_call_private *)SIPE_MEDIA_STREAM->call;
141 stream_schedule_cancel_timeout(SIPE_MEDIA_CALL, stream_private);
143 sipe_media_stream_set_data(SIPE_MEDIA_STREAM, NULL, NULL);
145 if (call_private) {
146 call_private->streams = g_slist_remove(call_private->streams,
147 stream_private);
149 if (SIPE_MEDIA_STREAM->ssrc_range) {
150 call_private->ssrc_ranges =
151 g_slist_remove(call_private->ssrc_ranges,
152 SIPE_MEDIA_STREAM->ssrc_range);
156 if (SIPE_MEDIA_STREAM->backend_private) {
157 sipe_backend_media_stream_free(SIPE_MEDIA_STREAM->backend_private);
159 g_free(SIPE_MEDIA_STREAM->id);
160 g_free(stream_private->encryption_key);
161 g_queue_free_full(stream_private->write_queue,
162 (GDestroyNotify)g_byte_array_unref);
163 g_queue_free_full(stream_private->async_reads, g_free);
164 sipe_utils_nameval_free(stream_private->extra_sdp);
165 g_free(stream_private);
168 static gboolean
169 call_private_equals(SIPE_UNUSED_PARAMETER const gchar *callid,
170 struct sipe_media_call_private *call_private1,
171 struct sipe_media_call_private *call_private2)
173 return call_private1 == call_private2;
176 static void
177 sipe_media_call_free(struct sipe_media_call_private *call_private)
179 if (call_private) {
180 g_hash_table_foreach_remove(call_private->sipe_private->media_calls,
181 (GHRFunc) call_private_equals, call_private);
183 call_schedule_cancel_request_timeout(SIPE_MEDIA_CALL);
185 while (call_private->streams) {
186 sipe_media_stream_free(call_private->streams->data);
189 sipe_backend_media_free(call_private->public.backend_private);
191 if (call_private->session) {
192 sipe_session_remove(call_private->sipe_private,
193 call_private->session);
196 if (call_private->invitation)
197 sipmsg_free(call_private->invitation);
199 // Frees any referenced extra invite data.
200 sipe_media_add_extra_invite_section(SIPE_MEDIA_CALL, NULL, NULL);
202 sipe_utils_slist_free_full(call_private->ssrc_ranges, g_free);
204 sdpmsg_free(call_private->smsg);
205 sipe_utils_slist_free_full(call_private->failed_media,
206 (GDestroyNotify)sdpmedia_free);
207 g_free(SIPE_MEDIA_CALL->with);
208 g_free(call_private);
212 static gint
213 candidate_sort_cb(struct sdpcandidate *c1, struct sdpcandidate *c2)
215 int cmp = g_strcmp0(c1->foundation, c2->foundation);
216 if (cmp == 0) {
217 cmp = g_strcmp0(c1->username, c2->username);
218 if (cmp == 0)
219 cmp = c1->component - c2->component;
222 return cmp;
225 static GSList *
226 backend_candidates_to_sdpcandidate(GList *candidates)
228 GSList *result = NULL;
229 GList *i;
231 for (i = candidates; i; i = i->next) {
232 struct sipe_backend_candidate *candidate = i->data;
233 struct sdpcandidate *c;
235 gchar *ip = sipe_backend_candidate_get_ip(candidate);
236 gchar *base_ip = sipe_backend_candidate_get_base_ip(candidate);
237 if (is_empty(ip) || strchr(ip, ':') ||
238 (base_ip && strchr(base_ip, ':'))) {
239 /* Ignore IPv6 candidates. */
240 g_free(ip);
241 g_free(base_ip);
242 continue;
245 c = g_new(struct sdpcandidate, 1);
246 c->foundation = sipe_backend_candidate_get_foundation(candidate);
247 c->component = sipe_backend_candidate_get_component_type(candidate);
248 c->type = sipe_backend_candidate_get_type(candidate);
249 c->protocol = sipe_backend_candidate_get_protocol(candidate);
250 c->ip = ip;
251 c->port = sipe_backend_candidate_get_port(candidate);
252 c->base_ip = base_ip;
253 c->base_port = sipe_backend_candidate_get_base_port(candidate);
254 c->priority = sipe_backend_candidate_get_priority(candidate);
255 c->username = sipe_backend_candidate_get_username(candidate);
256 c->password = sipe_backend_candidate_get_password(candidate);
258 result = g_slist_insert_sorted(result, c,
259 (GCompareFunc)candidate_sort_cb);
262 return result;
265 static void
266 get_stream_ip_and_ports(GSList *candidates,
267 gchar **ip, guint *rtp_port, guint *rtcp_port)
269 guint32 rtp_max_priority = 0;
270 guint32 rtcp_max_priority = 0;
272 *ip = 0;
273 *rtp_port = 0;
274 *rtcp_port = 0;
276 for (; candidates; candidates = candidates->next) {
277 struct sdpcandidate *candidate = candidates->data;
279 if (candidate->component == SIPE_COMPONENT_RTP &&
280 candidate->priority > rtp_max_priority) {
281 rtp_max_priority = candidate->priority;
282 *rtp_port = candidate->port;
284 g_free(*ip);
285 *ip = g_strdup(candidate->ip);
286 } else if (candidate->component == SIPE_COMPONENT_RTCP &&
287 candidate->priority > rtcp_max_priority) {
288 rtcp_max_priority = candidate->priority;
289 *rtcp_port = candidate->port;
294 static gint
295 sdpcodec_compare(gconstpointer a, gconstpointer b)
297 return ((const struct sdpcodec *)a)->id -
298 ((const struct sdpcodec *)b)->id;
301 static GList *
302 remove_wrong_farstream_0_1_tcp_candidates(GList *candidates)
304 GList *i = candidates;
305 GHashTable *foundation_to_candidate = g_hash_table_new_full(g_str_hash,
306 g_str_equal,
307 g_free,
308 NULL);
310 while (i) {
311 GList *next = i->next;
312 struct sipe_backend_candidate *c1 = i->data;
314 if (sipe_backend_candidate_get_protocol(c1) == SIPE_NETWORK_PROTOCOL_UDP) {
315 gchar *foundation = sipe_backend_candidate_get_foundation(c1);
316 struct sipe_backend_candidate *c2 = g_hash_table_lookup(foundation_to_candidate,
317 foundation);
319 if (c2) {
320 g_free(foundation);
322 if (sipe_backend_candidate_get_port(c1) ==
323 sipe_backend_candidate_get_port(c2) ||
324 (sipe_backend_candidate_get_type(c1) !=
325 SIPE_CANDIDATE_TYPE_HOST &&
326 sipe_backend_candidate_get_base_port(c1) ==
327 sipe_backend_candidate_get_base_port(c2))) {
329 * We assume that RTP+RTCP UDP pairs
330 * that share the same port are
331 * actually mistagged TCP candidates.
333 candidates = g_list_remove(candidates, c2);
334 candidates = g_list_delete_link(candidates, i);
335 sipe_backend_candidate_free(c1);
336 sipe_backend_candidate_free(c2);
338 } else
339 /* hash table takes ownership of "foundation" */
340 g_hash_table_insert(foundation_to_candidate, foundation, c1);
343 i = next;
346 g_hash_table_destroy(foundation_to_candidate);
348 return candidates;
351 static void
352 fill_zero_tcp_act_ports_from_tcp_pass(GSList *candidates, GSList *all_candidates)
354 GSList *i;
355 GHashTable *ip_to_port = g_hash_table_new(g_str_hash, g_str_equal);
357 for (i = candidates; i; i = i->next) {
358 struct sdpcandidate *c = i->data;
359 GSList *j;
361 if (c->protocol != SIPE_NETWORK_PROTOCOL_TCP_ACTIVE) {
362 continue;
365 for (j = all_candidates; j; j = j->next) {
366 struct sdpcandidate *passive = j->data;
367 if (passive->protocol != SIPE_NETWORK_PROTOCOL_TCP_PASSIVE ||
368 c->type != passive->type) {
369 continue;
372 if (sipe_strequal(c->ip, passive->ip) &&
373 sipe_strequal(c->base_ip, passive->base_ip)) {
374 if (c->port == 0) {
375 c->port = passive->port;
378 if (c->base_port == 0) {
379 c->base_port = passive->base_port;
381 break;
386 for (i = all_candidates; i; i = i->next) {
387 struct sdpcandidate *c = i->data;
389 if (c->protocol == SIPE_NETWORK_PROTOCOL_TCP_PASSIVE &&
390 c->type == SIPE_CANDIDATE_TYPE_HOST) {
391 g_hash_table_insert(ip_to_port, c->ip, &c->port);
395 /* Fill base ports of all TCP relay candidates using what we have
396 * collected from host candidates. */
397 for (i = candidates; i; i = i->next) {
398 struct sdpcandidate *c = i->data;
399 if (c->type == SIPE_CANDIDATE_TYPE_RELAY && c->base_port == 0) {
400 guint *base_port = (guint*)g_hash_table_lookup(ip_to_port, c->base_ip);
401 if (base_port) {
402 c->base_port = *base_port;
403 } else {
404 SIPE_DEBUG_WARNING("Couldn't determine base port for candidate "
405 "with foundation %s", c->foundation);
410 g_hash_table_destroy(ip_to_port);
413 static SipeEncryptionPolicy
414 get_encryption_policy(struct sipe_core_private *sipe_private)
416 SipeEncryptionPolicy result =
417 sipe_backend_media_get_encryption_policy(SIPE_CORE_PUBLIC);
418 if (result == SIPE_ENCRYPTION_POLICY_OBEY_SERVER) {
419 result = sipe_private->server_av_encryption_policy;
422 return result;
425 static struct sdpmedia *
426 media_stream_to_sdpmedia(struct sipe_media_call_private *call_private,
427 struct sipe_media_stream_private *stream_private)
429 struct sdpmedia *sdpmedia = g_new0(struct sdpmedia, 1);
430 GList *codecs = sipe_backend_get_local_codecs(SIPE_MEDIA_CALL,
431 SIPE_MEDIA_STREAM);
432 SipeEncryptionPolicy encryption_policy =
433 get_encryption_policy(call_private->sipe_private);
434 guint rtcp_port = 0;
435 SipeMediaType type;
436 GSList *attributes = NULL;
437 GSList *sdpcandidates;
438 GSList *all_sdpcandidates;
439 GList *candidates;
440 GList *i;
441 GSList *j;
443 sdpmedia->name = g_strdup(SIPE_MEDIA_STREAM->id);
445 if (sipe_strequal(sdpmedia->name, "audio"))
446 type = SIPE_MEDIA_AUDIO;
447 else if (sipe_strequal(sdpmedia->name, "video"))
448 type = SIPE_MEDIA_VIDEO;
449 else if (sipe_strequal(sdpmedia->name, "data"))
450 type = SIPE_MEDIA_APPLICATION;
451 else if (sipe_strequal(sdpmedia->name, "applicationsharing"))
452 type = SIPE_MEDIA_APPLICATION;
453 else {
454 // TODO: incompatible media, should not happen here
455 g_free(sdpmedia->name);
456 g_free(sdpmedia);
457 sipe_media_codec_list_free(codecs);
458 return(NULL);
461 // Process codecs
462 for (i = codecs; i; i = i->next) {
463 struct sipe_backend_codec *codec = i->data;
464 struct sdpcodec *c = g_new0(struct sdpcodec, 1);
465 GList *params;
467 c->id = sipe_backend_codec_get_id(codec);
468 c->name = sipe_backend_codec_get_name(codec);
469 c->clock_rate = sipe_backend_codec_get_clock_rate(codec);
470 c->type = type;
472 params = sipe_backend_codec_get_optional_parameters(codec);
473 for (; params; params = params->next) {
474 struct sipnameval *param = params->data;
475 struct sipnameval *copy = g_new0(struct sipnameval, 1);
477 copy->name = g_strdup(param->name);
478 copy->value = g_strdup(param->value);
480 c->parameters = g_slist_append(c->parameters, copy);
483 /* Buggy(?) codecs may report non-unique id (a.k.a. payload
484 * type) that must not appear in SDP messages we send. Thus,
485 * let's ignore any codec having the same id as one we already
486 * have in the converted list. */
487 if (g_slist_find_custom(sdpmedia->codecs, c, sdpcodec_compare)) {
488 sdpcodec_free(c);
489 } else {
490 sdpmedia->codecs = g_slist_append(sdpmedia->codecs, c);
494 sipe_media_codec_list_free(codecs);
496 // Process local candidates
497 // If we have established candidate pairs, send them in SDP response.
498 // Otherwise send all available local candidates.
499 candidates = sipe_backend_media_stream_get_active_local_candidates(SIPE_MEDIA_STREAM);
500 sdpcandidates = backend_candidates_to_sdpcandidate(candidates);
501 sipe_media_candidate_list_free(candidates);
503 candidates = sipe_backend_get_local_candidates(SIPE_MEDIA_CALL,
504 SIPE_MEDIA_STREAM);
505 candidates = remove_wrong_farstream_0_1_tcp_candidates(candidates);
506 all_sdpcandidates = backend_candidates_to_sdpcandidate(candidates);
507 sipe_media_candidate_list_free(candidates);
509 if (!sdpcandidates) {
510 sdpcandidates = all_sdpcandidates;
513 fill_zero_tcp_act_ports_from_tcp_pass(sdpcandidates, all_sdpcandidates);
515 sdpmedia->candidates = sdpcandidates;
517 if (all_sdpcandidates != sdpcandidates) {
518 sipe_utils_slist_free_full(all_sdpcandidates,
519 (GDestroyNotify)sdpcandidate_free);
522 get_stream_ip_and_ports(sdpmedia->candidates, &sdpmedia->ip,
523 &sdpmedia->port, &rtcp_port);
525 if (sipe_backend_stream_is_held(SIPE_MEDIA_STREAM))
526 attributes = sipe_utils_nameval_add(attributes, "inactive", "");
528 if (rtcp_port) {
529 gchar *tmp = g_strdup_printf("%u", rtcp_port);
530 attributes = sipe_utils_nameval_add(attributes, "rtcp", tmp);
531 g_free(tmp);
534 if (encryption_policy != call_private->sipe_private->server_av_encryption_policy) {
535 const gchar *encryption = NULL;
536 switch (encryption_policy) {
537 case SIPE_ENCRYPTION_POLICY_REJECTED:
538 encryption = "rejected";
539 break;
540 case SIPE_ENCRYPTION_POLICY_OPTIONAL:
541 encryption = "optional";
542 break;
543 case SIPE_ENCRYPTION_POLICY_REQUIRED:
544 default:
545 encryption = "required";
546 break;
549 attributes = sipe_utils_nameval_add(attributes, "encryption", encryption);
552 if (SIPE_MEDIA_STREAM->ssrc_range) {
553 gchar *tmp;
555 tmp = g_strdup_printf("%u-%u",
556 SIPE_MEDIA_STREAM->ssrc_range->begin,
557 SIPE_MEDIA_STREAM->ssrc_range->end);
558 attributes = sipe_utils_nameval_add(attributes,
559 "x-ssrc-range", tmp);
560 g_free(tmp);
563 // Process remote candidates
564 candidates = sipe_backend_media_stream_get_active_remote_candidates(SIPE_MEDIA_STREAM);
565 sdpmedia->remote_candidates = backend_candidates_to_sdpcandidate(candidates);
566 sipe_media_candidate_list_free(candidates);
568 sdpmedia->encryption_active = stream_private->encryption_key &&
569 call_private->encryption_compatible &&
570 stream_private->remote_candidates_and_codecs_set &&
571 encryption_policy != SIPE_ENCRYPTION_POLICY_REJECTED;
573 // Set our key if encryption is enabled.
574 if (stream_private->encryption_key &&
575 encryption_policy != SIPE_ENCRYPTION_POLICY_REJECTED) {
576 sdpmedia->encryption_key = g_memdup(stream_private->encryption_key,
577 SIPE_SRTP_KEY_LEN);
578 sdpmedia->encryption_key_id = stream_private->encryption_key_id;
581 // Append extra attributes assigned to the stream.
582 for (j = stream_private->extra_sdp; j; j = g_slist_next(j)) {
583 struct sipnameval *attr = j->data;
584 attributes = sipe_utils_nameval_add(attributes,
585 attr->name, attr->value);
588 sdpmedia->attributes = attributes;
590 return sdpmedia;
593 static struct sdpmsg *
594 sipe_media_to_sdpmsg(struct sipe_media_call_private *call_private)
596 struct sdpmsg *msg = g_new0(struct sdpmsg, 1);
597 GSList *streams = call_private->streams;
599 for (; streams; streams = streams->next) {
600 struct sdpmedia *media = media_stream_to_sdpmedia(call_private,
601 streams->data);
602 if (media) {
603 msg->media = g_slist_append(msg->media, media);
605 if (msg->ip == NULL)
606 msg->ip = g_strdup(media->ip);
610 msg->media = g_slist_concat(msg->media, call_private->failed_media);
611 call_private->failed_media = NULL;
613 msg->ice_version = call_private->ice_version;
615 return msg;
618 static void
619 sipe_invite_call(struct sipe_media_call_private *call_private, TransCallback tc)
621 struct sipe_core_private *sipe_private = call_private->sipe_private;
622 gchar *hdr;
623 gchar *contact;
624 gchar *p_preferred_identity = NULL;
625 gchar *body;
626 struct sip_dialog *dialog;
627 struct sdpmsg *msg;
629 dialog = sipe_media_get_sip_dialog(SIPE_MEDIA_CALL);
631 contact = get_contact(sipe_private);
633 if (sipe_private->uc_line_uri) {
634 gchar *self = sip_uri_self(sipe_private);
635 p_preferred_identity = g_strdup_printf(
636 "P-Preferred-Identity: <%s>, <%s>\r\n",
637 self, sipe_private->uc_line_uri);
638 g_free(self);
641 hdr = g_strdup_printf(
642 "ms-keep-alive: UAC;hop-hop=yes\r\n"
643 "Contact: %s\r\n"
644 "%s"
645 "Content-Type: %s%s\r\n",
646 contact,
647 p_preferred_identity ? p_preferred_identity : "",
648 call_private->invite_content_type ?
649 call_private->invite_content_type : "application/sdp",
650 call_private->invite_content_type ?
651 ";boundary=\"----=_NextPart_000_001E_01CB4397.0B5EB570\"" : "");
653 g_free(contact);
654 g_free(p_preferred_identity);
656 msg = sipe_media_to_sdpmsg(call_private);
657 body = sdpmsg_to_string(msg);
659 if (call_private->extra_invite_section) {
660 gchar *tmp;
661 tmp = g_strdup_printf(
662 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
663 "%s"
664 "\r\n"
665 "------=_NextPart_000_001E_01CB4397.0B5EB570\r\n"
666 "Content-Type: application/sdp\r\n"
667 "Content-Transfer-Encoding: 7bit\r\n"
668 "Content-Disposition: session; handling=optional\r\n"
669 "\r\n"
670 "%s"
671 "\r\n"
672 "------=_NextPart_000_001E_01CB4397.0B5EB570--\r\n",
673 call_private->extra_invite_section, body);
674 g_free(body);
675 body = tmp;
676 sipe_media_add_extra_invite_section(SIPE_MEDIA_CALL, NULL, NULL);
679 sdpmsg_free(msg);
681 dialog->outgoing_invite = sip_transport_invite(sipe_private,
682 hdr,
683 body,
684 dialog,
685 tc);
687 g_free(body);
688 g_free(hdr);
691 static void
692 send_response_with_session_description(struct sipe_media_call_private *call_private, int code, gchar *text)
694 struct sdpmsg *msg = sipe_media_to_sdpmsg(call_private);
695 gchar *body = sdpmsg_to_string(msg);
696 sdpmsg_free(msg);
697 sipmsg_add_header(call_private->invitation, "Content-Type", "application/sdp");
698 sip_transport_response(call_private->sipe_private, call_private->invitation, code, text, body);
699 g_free(body);
702 static gboolean
703 process_invite_call_response(struct sipe_core_private *sipe_private,
704 struct sipmsg *msg,
705 struct transaction *trans);
707 struct sipe_media_stream *
708 sipe_core_media_get_stream_by_id(struct sipe_media_call *call, const gchar *id)
710 GSList *i;
711 for (i = SIPE_MEDIA_CALL_PRIVATE->streams; i; i = i->next) {
712 struct sipe_media_stream *stream = i->data;
713 if (sipe_strequal(stream->id, id))
714 return stream;
716 return NULL;
719 static gboolean
720 update_call_from_remote_sdp(struct sipe_media_call_private* call_private,
721 struct sdpmedia *media)
723 struct sipe_media_stream *stream;
724 GList *backend_candidates = NULL;
725 GList *backend_codecs = NULL;
726 GSList *i;
727 gboolean result = TRUE;
729 stream = sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL, media->name);
730 if (media->port == 0) {
731 if (stream) {
732 sipe_backend_media_stream_end(SIPE_MEDIA_CALL, stream);
734 return TRUE;
737 if (!stream)
738 return FALSE;
740 if (sipe_utils_nameval_find(media->attributes, "inactive")) {
741 sipe_backend_stream_hold(SIPE_MEDIA_CALL, stream, FALSE);
742 } else if (sipe_backend_stream_is_held(stream)) {
743 sipe_backend_stream_unhold(SIPE_MEDIA_CALL, stream, FALSE);
746 if (SIPE_MEDIA_STREAM_PRIVATE->remote_candidates_and_codecs_set) {
747 return TRUE;
750 for (i = media->codecs; i; i = i->next) {
751 struct sdpcodec *c = i->data;
752 struct sipe_backend_codec *codec;
753 GSList *j;
755 codec = sipe_backend_codec_new(c->id,
756 c->name,
757 c->type,
758 c->clock_rate,
759 c->channels);
761 for (j = c->parameters; j; j = j->next) {
762 struct sipnameval *attr = j->data;
764 sipe_backend_codec_add_optional_parameter(codec,
765 attr->name,
766 attr->value);
769 backend_codecs = g_list_append(backend_codecs, codec);
772 if (media->encryption_key && SIPE_MEDIA_STREAM_PRIVATE->encryption_key) {
773 sipe_backend_media_set_encryption_keys(SIPE_MEDIA_CALL, stream,
774 SIPE_MEDIA_STREAM_PRIVATE->encryption_key,
775 media->encryption_key);
776 SIPE_MEDIA_STREAM_PRIVATE->encryption_key_id = media->encryption_key_id;
779 result = sipe_backend_set_remote_codecs(SIPE_MEDIA_CALL, stream,
780 backend_codecs);
781 sipe_media_codec_list_free(backend_codecs);
783 if (result == FALSE) {
784 sipe_backend_media_stream_end(SIPE_MEDIA_CALL, stream);
785 return FALSE;
788 for (i = media->candidates; i; i = i->next) {
789 struct sdpcandidate *c = i->data;
790 struct sipe_backend_candidate *candidate;
791 candidate = sipe_backend_candidate_new(c->foundation,
792 c->component,
793 c->type,
794 c->protocol,
795 c->ip,
796 c->port,
797 c->username,
798 c->password);
799 sipe_backend_candidate_set_priority(candidate, c->priority);
801 backend_candidates = g_list_append(backend_candidates, candidate);
804 sipe_backend_media_add_remote_candidates(SIPE_MEDIA_CALL, stream,
805 backend_candidates);
806 sipe_media_candidate_list_free(backend_candidates);
808 SIPE_MEDIA_STREAM_PRIVATE->remote_candidates_and_codecs_set = TRUE;
810 return TRUE;
813 static void
814 apply_remote_message(struct sipe_media_call_private* call_private,
815 struct sdpmsg* msg)
817 GSList *i;
819 sipe_utils_slist_free_full(call_private->failed_media, (GDestroyNotify)sdpmedia_free);
820 call_private->failed_media = NULL;
821 call_private->encryption_compatible = TRUE;
823 for (i = msg->media; i; i = i->next) {
824 struct sdpmedia *media = i->data;
825 const gchar *enc_level =
826 sipe_utils_nameval_find(media->attributes, "encryption");
827 if (sipe_strequal(enc_level, "rejected") &&
828 get_encryption_policy(call_private->sipe_private) == SIPE_ENCRYPTION_POLICY_REQUIRED) {
829 call_private->encryption_compatible = FALSE;
832 if (!update_call_from_remote_sdp(call_private, media)) {
833 media->port = 0;
834 call_private->failed_media =
835 g_slist_append(call_private->failed_media, media);
839 /* We need to keep failed medias until response is sent, remove them
840 * from sdpmsg that is to be freed. */
841 for (i = call_private->failed_media; i; i = i->next) {
842 msg->media = g_slist_remove(msg->media, i->data);
846 static gboolean
847 call_initialized(struct sipe_media_call *call)
849 GSList *streams = SIPE_MEDIA_CALL_PRIVATE->streams;
850 for (; streams; streams = streams->next) {
851 if (!sipe_backend_stream_initialized(call, streams->data)) {
852 return FALSE;
856 return TRUE;
859 static void
860 stream_connection_timeout_cb(struct sipe_core_private *sipe_private,
861 gpointer data)
863 struct sipe_media_call_private *call_private = data;
865 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
866 _("Couldn't create stream"),
867 _("Connection timed out"));
868 sipe_backend_media_hangup(SIPE_MEDIA_CALL->backend_private, TRUE);
871 static void
872 stream_schedule_timeout(struct sipe_media_call *call)
874 GSList *i;
875 for (i = SIPE_MEDIA_CALL_PRIVATE->streams; i; i = i->next) {
876 struct sipe_media_stream_private *stream_private = i->data;
878 stream_private->timeout_key =
879 g_strdup_printf("<media-stream-connect><%s><%s>",
880 sipe_media_get_sip_dialog(call)->callid,
881 SIPE_MEDIA_STREAM->id);
883 sipe_schedule_seconds(SIPE_MEDIA_CALL_PRIVATE->sipe_private,
884 stream_private->timeout_key,
885 SIPE_MEDIA_CALL_PRIVATE,
886 SIPE_MEDIA_STREAM_CONNECTION_TIMEOUT_SECONDS,
887 stream_connection_timeout_cb,
888 NULL);
892 static void
893 stream_schedule_cancel_timeout(struct sipe_media_call *call,
894 struct sipe_media_stream_private *stream_private)
896 if (stream_private->timeout_key) {
897 sipe_schedule_cancel(SIPE_MEDIA_CALL_PRIVATE->sipe_private,
898 stream_private->timeout_key);
899 g_free(stream_private->timeout_key);
901 stream_private->timeout_key = NULL;
904 static void
905 call_request_timeout_cb(struct sipe_core_private *sipe_private,
906 gpointer data)
908 struct sipe_media_call_private *call_private = data;
910 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
911 _("Request timed out"),
912 _("Call could not be answered"));
913 sipe_backend_media_hangup(SIPE_MEDIA_CALL->backend_private, TRUE);
916 static void
917 call_schedule_request_timeout(struct sipe_media_call *call)
919 SIPE_MEDIA_CALL_PRIVATE->timeout_key =
920 g_strdup_printf("<media-call-request><%s>", sipe_media_get_sip_dialog(call)->callid);
922 sipe_schedule_seconds(SIPE_MEDIA_CALL_PRIVATE->sipe_private,
923 SIPE_MEDIA_CALL_PRIVATE->timeout_key,
924 SIPE_MEDIA_CALL_PRIVATE,
925 SIPE_MEDIA_CALL_TIMEOUT_SECONDS,
926 call_request_timeout_cb,
927 NULL);
930 static void
931 call_schedule_cancel_request_timeout(struct sipe_media_call *call)
933 if (SIPE_MEDIA_CALL_PRIVATE->timeout_key) {
934 sipe_schedule_cancel(SIPE_MEDIA_CALL_PRIVATE->sipe_private,
935 SIPE_MEDIA_CALL_PRIVATE->timeout_key);
936 g_free(SIPE_MEDIA_CALL_PRIVATE->timeout_key);
938 SIPE_MEDIA_CALL_PRIVATE->timeout_key = NULL;
942 // Sends an invite response when the call is accepted and local candidates were
943 // prepared, otherwise does nothing. If error response is sent, call_private is
944 // disposed before function returns.
945 static void
946 maybe_send_first_invite_response(struct sipe_media_call_private *call_private)
948 struct sipe_backend_media *backend_media;
950 backend_media = call_private->public.backend_private;
952 if (!sipe_backend_media_accepted(backend_media) ||
953 !call_initialized(&call_private->public))
954 return;
956 if (!call_private->encryption_compatible) {
957 struct sipe_core_private *sipe_private = call_private->sipe_private;
959 sipmsg_add_header(call_private->invitation, "Warning",
960 "308 lcs.microsoft.com \"Encryption Levels not compatible\"");
961 sip_transport_response(sipe_private,
962 call_private->invitation,
963 488, "Encryption Levels not compatible",
964 NULL);
965 sipe_backend_media_reject(backend_media, FALSE);
966 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
967 _("Unable to establish a call"),
968 _("Encryption settings of peer are incompatible with ours."));
969 } else {
970 send_response_with_session_description(call_private, 200, "OK");
971 stream_schedule_timeout(SIPE_MEDIA_CALL);
972 sipmsg_free(call_private->invitation);
973 call_private->invitation = NULL;
977 static void
978 stream_initialized_cb(struct sipe_media_call *call,
979 struct sipe_media_stream *stream)
981 if (call_initialized(call)) {
982 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
984 if (sipe_backend_media_is_initiator(call, stream)) {
985 sipe_invite_call(call_private,
986 process_invite_call_response);
987 } else if (call_private->smsg) {
988 struct sdpmsg *smsg = call_private->smsg;
989 call_private->smsg = NULL;
991 apply_remote_message(call_private, smsg);
992 maybe_send_first_invite_response(call_private);
993 sdpmsg_free(smsg);
998 static void phone_state_publish(struct sipe_core_private *sipe_private)
1000 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1001 sipe_ocs2007_phone_state_publish(sipe_private);
1002 } else {
1003 // TODO: OCS 2005 support. Is anyone still using it at all?
1007 void
1008 sipe_core_media_stream_end(struct sipe_media_stream *stream)
1010 sipe_media_stream_free(SIPE_MEDIA_STREAM_PRIVATE);
1013 static void
1014 media_end_cb(struct sipe_media_call *call)
1016 struct sipe_core_private *sipe_private;
1018 g_return_if_fail(call);
1020 sipe_private = SIPE_MEDIA_CALL_PRIVATE->sipe_private;
1022 sipe_media_call_free(SIPE_MEDIA_CALL_PRIVATE);
1023 phone_state_publish(sipe_private);
1026 static void
1027 call_accept_cb(struct sipe_media_call *call, gboolean local)
1029 if (local) {
1030 maybe_send_first_invite_response(SIPE_MEDIA_CALL_PRIVATE);
1032 phone_state_publish(SIPE_MEDIA_CALL_PRIVATE->sipe_private);
1035 static void
1036 call_reject_cb(struct sipe_media_call *call, gboolean local)
1038 if (local) {
1039 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
1041 sip_transport_response(call_private->sipe_private,
1042 call_private->invitation,
1043 603, "Decline", NULL);
1045 if (call_private->session) {
1046 sipe_session_remove(call_private->sipe_private,
1047 call_private->session);
1048 call_private->session = NULL;
1053 static void
1054 av_call_reject_cb(struct sipe_media_call *call, gboolean local)
1056 if (!local) {
1057 struct sipe_core_private *sipe_private;
1058 gchar *desc;
1060 sipe_private = SIPE_MEDIA_CALL_PRIVATE->sipe_private;
1062 desc = g_strdup_printf(_("User %s rejected call"), call->with);
1063 sipe_backend_notify_error(SIPE_CORE_PUBLIC, _("Call rejected"),
1064 desc);
1065 g_free(desc);
1068 call_reject_cb(call, local);
1071 static gboolean
1072 sipe_media_send_ack(struct sipe_core_private *sipe_private, struct sipmsg *msg,
1073 struct transaction *trans);
1075 static void call_hold_cb(struct sipe_media_call *call,
1076 gboolean local,
1077 SIPE_UNUSED_PARAMETER gboolean state)
1079 if (local) {
1080 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE, sipe_media_send_ack);
1084 static void call_hangup_cb(struct sipe_media_call *call, gboolean local)
1086 if (local) {
1087 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
1089 if (call_private->session) {
1090 sipe_session_close(call_private->sipe_private,
1091 call_private->session);
1092 call_private->session = NULL;
1097 static void
1098 error_cb(struct sipe_media_call *call, gchar *message)
1100 struct sipe_media_call_private *call_private = SIPE_MEDIA_CALL_PRIVATE;
1101 struct sipe_core_private *sipe_private = call_private->sipe_private;
1102 gboolean initiator = sipe_backend_media_is_initiator(call, NULL);
1103 gboolean accepted = sipe_backend_media_accepted(call->backend_private);
1105 gchar *title = g_strdup_printf("Call with %s failed", call->with);
1106 sipe_backend_notify_error(SIPE_CORE_PUBLIC, title, message);
1107 g_free(title);
1109 if (!initiator && !accepted) {
1110 sip_transport_response(sipe_private,
1111 call_private->invitation,
1112 488, "Not Acceptable Here", NULL);
1116 struct sipe_media_call *
1117 sipe_media_call_new(struct sipe_core_private *sipe_private, const gchar* with,
1118 struct sipmsg *msg, SipeIceVersion ice_version,
1119 SipeMediaCallFlags flags)
1121 struct sipe_media_call_private *call_private;
1122 struct sip_session *session;
1123 struct sip_dialog *dialog;
1124 gchar *cname;
1126 session = sipe_session_add_call(sipe_private, with);
1128 dialog = sipe_dialog_add(session);
1129 dialog->with = g_strdup(with);
1131 if (msg) {
1132 gchar *newTag = gentag();
1133 const gchar *oldHeader;
1134 gchar *newHeader;
1136 oldHeader = sipmsg_find_header(msg, "To");
1137 newHeader = g_strdup_printf("%s;tag=%s", oldHeader, newTag);
1138 sipmsg_remove_header_now(msg, "To");
1139 sipmsg_add_header_now(msg, "To", newHeader);
1140 g_free(newTag);
1141 g_free(newHeader);
1143 dialog->callid = g_strdup(sipmsg_find_header(msg, "Call-ID"));
1144 sipe_dialog_parse(dialog, msg, FALSE);
1145 } else {
1146 dialog->callid = gencallid();
1147 dialog->ourtag = gentag();
1148 flags |= SIPE_MEDIA_CALL_INITIATOR;
1151 if (g_hash_table_lookup(sipe_private->media_calls, dialog->callid)) {
1152 SIPE_DEBUG_ERROR("sipe_media_call_new: call already exists for "
1153 "Call-ID %s", dialog->callid);
1154 sipe_session_remove(sipe_private, session);
1155 return NULL;
1158 call_private = g_new0(struct sipe_media_call_private, 1);
1159 call_private->sipe_private = sipe_private;
1160 call_private->session = session;
1161 SIPE_MEDIA_CALL->with = g_strdup(with);
1163 g_hash_table_insert(sipe_private->media_calls,
1164 g_strdup(dialog->callid), call_private);
1166 cname = g_strdup(sipe_private->contact + 1);
1167 cname[strlen(cname) - 1] = '\0';
1169 call_private->public.backend_private = sipe_backend_media_new(SIPE_CORE_PUBLIC,
1170 SIPE_MEDIA_CALL,
1171 with,
1172 flags);
1173 sipe_backend_media_set_cname(call_private->public.backend_private, cname);
1175 call_private->ice_version = ice_version;
1176 call_private->encryption_compatible = TRUE;
1178 call_private->public.stream_initialized_cb = stream_initialized_cb;
1179 call_private->public.media_end_cb = media_end_cb;
1180 call_private->public.call_accept_cb = call_accept_cb;
1181 call_private->public.call_reject_cb = call_reject_cb;
1182 call_private->public.call_hold_cb = call_hold_cb;
1183 call_private->public.call_hangup_cb = call_hangup_cb;
1184 call_private->public.error_cb = error_cb;
1186 g_free(cname);
1188 return SIPE_MEDIA_CALL;
1191 void sipe_media_hangup(struct sipe_media_call_private *call_private)
1193 if (call_private) {
1194 sipe_backend_media_hangup(call_private->public.backend_private,
1195 FALSE);
1199 static gint
1200 ssrc_range_compare(const struct ssrc_range *a, const struct ssrc_range *b)
1202 if (a->begin < b->begin) {
1203 return -1;
1205 if (a->begin > b->begin) {
1206 return 1;
1208 return 0;
1211 static void
1212 ssrc_range_update(GSList **ranges, GSList *media)
1214 for (; media; media = media->next) {
1215 struct sdpmedia *m;
1216 const char *ssrc_range;
1217 gchar **parts;
1219 m = media->data;
1220 ssrc_range = sipe_utils_nameval_find(m->attributes,
1221 "x-ssrc-range");
1222 if (!ssrc_range) {
1223 continue;
1226 parts = g_strsplit(ssrc_range, "-", 2);
1228 if (parts[0] && parts[1]) {
1229 struct ssrc_range *range;
1231 range = g_new0(struct ssrc_range, 1);
1232 range->begin = atoi(parts[0]);
1233 range->end = atoi(parts[1]);
1235 *ranges = sipe_utils_slist_insert_unique_sorted(
1236 *ranges, range,
1237 (GCompareFunc)ssrc_range_compare,
1238 g_free);
1241 g_strfreev(parts);
1245 static struct ssrc_range *
1246 ssrc_range_allocate(GSList **ranges, guint32 len)
1248 struct ssrc_range *range;
1249 GSList *i;
1251 range = g_new0(struct ssrc_range, 1);
1252 range->begin = 1;
1253 range->end = range->begin + (len - 1);
1255 for (i = *ranges; i; i = i->next) {
1256 struct ssrc_range *r = i->data;
1258 if (range->begin < r->begin && range->end < r->begin) {
1259 break;
1262 range->begin = r->end + 1;
1263 range->end = range->begin + (len - 1);
1266 /* As per [MS-SDPEXT] 3.1.5.31.1, a SSRC MUST be from 1 to 4294967040
1267 * inclusive. */
1268 if (range->begin > range->end || range->end > 0xFFFFFF00) {
1269 g_free(range);
1270 SIPE_DEBUG_ERROR("Couldn't allocate SSRC range of %u", len);
1271 return NULL;
1274 *ranges = g_slist_insert_sorted(*ranges, range,
1275 (GCompareFunc)ssrc_range_compare);
1277 return range;
1280 struct sipe_media_stream *
1281 sipe_media_stream_add(struct sipe_media_call *call, const gchar *id,
1282 SipeMediaType type, SipeIceVersion ice_version,
1283 gboolean initiator, guint32 ssrc_count)
1285 struct sipe_core_private *sipe_private;
1286 struct sipe_media_stream_private *stream_private;
1287 struct sipe_backend_media_relays *backend_media_relays;
1288 guint min_port;
1289 guint max_port;
1291 sipe_private = SIPE_MEDIA_CALL_PRIVATE->sipe_private;
1293 backend_media_relays = sipe_backend_media_relays_convert(
1294 sipe_private->media_relays,
1295 sipe_private->media_relay_username,
1296 sipe_private->media_relay_password);
1298 min_port = sipe_private->min_media_port;
1299 max_port = sipe_private->max_media_port;
1300 switch (type) {
1301 case SIPE_MEDIA_AUDIO:
1302 min_port = sipe_private->min_audio_port;
1303 max_port = sipe_private->max_audio_port;
1304 break;
1305 case SIPE_MEDIA_VIDEO:
1306 min_port = sipe_private->min_video_port;
1307 max_port = sipe_private->max_audio_port;
1308 break;
1309 case SIPE_MEDIA_APPLICATION:
1310 if (sipe_strequal(id, "data")) {
1311 min_port = sipe_private->min_filetransfer_port;
1312 max_port = sipe_private->max_filetransfer_port;
1313 } else if (sipe_strequal(id, "applicationsharing")) {
1314 min_port = sipe_private->min_appsharing_port;
1315 max_port = sipe_private->max_appsharing_port;
1317 break;
1320 stream_private = g_new0(struct sipe_media_stream_private, 1);
1321 SIPE_MEDIA_STREAM->call = call;
1322 SIPE_MEDIA_STREAM->id = g_strdup(id);
1323 stream_private->write_queue = g_queue_new();
1324 stream_private->async_reads = g_queue_new();
1326 if (ssrc_count > 0) {
1327 SIPE_MEDIA_STREAM->ssrc_range =
1328 ssrc_range_allocate(&SIPE_MEDIA_CALL_PRIVATE->ssrc_ranges,
1329 ssrc_count);
1332 SIPE_MEDIA_STREAM->backend_private =
1333 sipe_backend_media_add_stream(SIPE_MEDIA_STREAM,
1334 type, ice_version,
1335 initiator,
1336 backend_media_relays,
1337 min_port, max_port);
1339 sipe_backend_media_relays_free(backend_media_relays);
1341 if (!SIPE_MEDIA_STREAM->backend_private) {
1342 sipe_media_stream_free(stream_private);
1343 return NULL;
1346 if (type == SIPE_MEDIA_VIDEO) {
1347 /* Declare that we can send and receive Video Source Requests
1348 * as per [MS-SDPEXT] 3.1.5.30.2. */
1349 sipe_media_stream_add_extra_attribute(SIPE_MEDIA_STREAM,
1350 "rtcp-fb", "* x-message app send:src recv:src");
1352 sipe_media_stream_add_extra_attribute(SIPE_MEDIA_STREAM,
1353 "rtcp-rsize", NULL);
1354 sipe_media_stream_add_extra_attribute(SIPE_MEDIA_STREAM,
1355 "label", "main-video");
1356 sipe_media_stream_add_extra_attribute(SIPE_MEDIA_STREAM,
1357 "x-source", "main-video");
1360 #ifdef HAVE_SRTP
1361 if (get_encryption_policy(sipe_private) != SIPE_ENCRYPTION_POLICY_REJECTED) {
1362 int i;
1363 stream_private->encryption_key = g_new0(guchar, SIPE_SRTP_KEY_LEN);
1364 for (i = 0; i != SIPE_SRTP_KEY_LEN; ++i) {
1365 stream_private->encryption_key[i] = rand() & 0xff;
1367 stream_private->encryption_key_id = 1;
1369 #endif
1371 SIPE_MEDIA_CALL_PRIVATE->streams =
1372 g_slist_append(SIPE_MEDIA_CALL_PRIVATE->streams,
1373 stream_private);
1375 return SIPE_MEDIA_STREAM;
1378 static void
1379 append_2007_fallback_if_needed(struct sipe_media_call_private *call_private)
1381 struct sipe_core_private *sipe_private = call_private->sipe_private;
1382 const gchar *marker = sip_transport_sdp_address_marker(sipe_private);
1383 const gchar *ip = sip_transport_ip_address(sipe_private);
1384 gchar *body;
1386 if (SIPE_CORE_PRIVATE_FLAG_IS(SFB) ||
1387 sipe_media_get_sip_dialog(SIPE_MEDIA_CALL)->cseq != 0 ||
1388 call_private->ice_version != SIPE_ICE_RFC_5245 ||
1389 sipe_strequal(SIPE_MEDIA_CALL->with, sipe_private->test_call_bot_uri)) {
1390 return;
1393 body = g_strdup_printf("Content-Type: application/sdp\r\n"
1394 "Content-Transfer-Encoding: 7bit\r\n"
1395 "Content-Disposition: session; handling=optional; ms-proxy-2007fallback\r\n"
1396 "\r\n"
1397 "o=- 0 0 IN %s %s\r\n"
1398 "s=session\r\n"
1399 "c=IN %s %s\r\n"
1400 "m=audio 0 RTP/AVP\r\n",
1401 marker, ip,
1402 marker, ip);
1403 sipe_media_add_extra_invite_section(SIPE_MEDIA_CALL,
1404 "multipart/alternative", body);
1407 static void
1408 sipe_media_initiate_call(struct sipe_core_private *sipe_private,
1409 const char *with, SipeIceVersion ice_version,
1410 gboolean with_video)
1412 struct sipe_media_call_private *call_private;
1414 if (sipe_core_media_get_call(SIPE_CORE_PUBLIC)) {
1415 return;
1418 call_private = (struct sipe_media_call_private *)
1419 sipe_media_call_new(sipe_private, with, NULL,
1420 ice_version, 0);
1422 SIPE_MEDIA_CALL->call_reject_cb = av_call_reject_cb;
1424 if (!sipe_media_stream_add(SIPE_MEDIA_CALL, "audio", SIPE_MEDIA_AUDIO,
1425 call_private->ice_version,
1426 TRUE, 0)) {
1427 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
1428 _("Error occurred"),
1429 _("Error creating audio stream"));
1430 sipe_media_hangup(call_private);
1431 return;
1434 if (with_video &&
1435 !sipe_media_stream_add(SIPE_MEDIA_CALL, "video", SIPE_MEDIA_VIDEO,
1436 call_private->ice_version,
1437 TRUE, VIDEO_SSRC_COUNT)) {
1438 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
1439 _("Error occurred"),
1440 _("Error creating video stream"));
1441 sipe_media_hangup(call_private);
1442 return;
1445 append_2007_fallback_if_needed(call_private);
1447 call_schedule_request_timeout(SIPE_MEDIA_CALL);
1448 // Processing continues in stream_initialized_cb
1451 void
1452 sipe_core_media_initiate_call(struct sipe_core_public *sipe_public,
1453 const char *with,
1454 gboolean with_video)
1456 sipe_media_initiate_call(SIPE_CORE_PRIVATE, with,
1457 SIPE_ICE_RFC_5245, with_video);
1460 static void
1461 conference_audio_muted_cb(struct sipe_media_stream *stream, gboolean is_muted)
1463 struct sipe_media_call *call = stream->call;
1465 if (!SIPE_MEDIA_CALL_PRIVATE->conference_session) {
1466 return;
1469 sipe_conf_announce_audio_mute_state(SIPE_MEDIA_CALL_PRIVATE->sipe_private,
1470 SIPE_MEDIA_CALL_PRIVATE->conference_session,
1471 is_muted);
1474 void sipe_core_media_connect_conference(struct sipe_core_public *sipe_public,
1475 struct sipe_chat_session *chat_session)
1477 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1478 struct sipe_media_call_private *call_private;
1479 struct sipe_media_stream *stream;
1480 struct sip_session *session;
1481 SipeIceVersion ice_version;
1482 gchar *av_uri;
1484 if (!sipe_conf_supports_mcu_type(sipe_private, "audio-video")) {
1485 sipe_backend_notify_error(sipe_public, _("Join conference call"),
1486 _("Conference calls are not supported on this server."));
1487 return;
1490 session = sipe_session_find_chat(sipe_private, chat_session);
1492 if (sipe_core_media_get_call(sipe_public) || !session) {
1493 return;
1496 av_uri = sipe_conf_build_uri(sipe_core_chat_id(sipe_public, chat_session),
1497 "audio-video");
1498 if (!av_uri) {
1499 return;
1502 session->is_call = TRUE;
1504 ice_version = SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013) ? SIPE_ICE_RFC_5245 :
1505 SIPE_ICE_DRAFT_6;
1507 call_private = (struct sipe_media_call_private *)
1508 sipe_media_call_new(sipe_private, av_uri, NULL,
1509 ice_version, 0);
1510 call_private->conference_session = session;
1511 SIPE_MEDIA_CALL->call_reject_cb = av_call_reject_cb;
1513 stream = sipe_media_stream_add(SIPE_MEDIA_CALL, "audio",
1514 SIPE_MEDIA_AUDIO,
1515 call_private->ice_version,
1516 TRUE, 0);
1517 if (!stream) {
1518 sipe_backend_notify_error(sipe_public,
1519 _("Error occurred"),
1520 _("Error creating audio stream"));
1522 sipe_media_hangup(call_private);
1525 stream->mute_cb = conference_audio_muted_cb;
1527 g_free(av_uri);
1529 // Processing continues in stream_initialized_cb
1532 struct sipe_media_call *
1533 sipe_core_media_get_call(struct sipe_core_public *sipe_public)
1535 struct sipe_media_call * result = NULL;
1536 GList *calls = g_hash_table_get_values(SIPE_CORE_PRIVATE->media_calls);
1537 GList *entry = calls;
1539 while (entry) {
1540 if (sipe_core_media_get_stream_by_id(entry->data, "audio")) {
1541 result = entry->data;
1542 break;
1544 entry = entry->next;
1546 g_list_free(calls);
1548 return result;
1551 static gboolean phone_number_is_valid(const gchar *phone_number)
1553 if (!phone_number || sipe_strequal(phone_number, "")) {
1554 return FALSE;
1557 if (*phone_number == '+') {
1558 ++phone_number;
1561 while (*phone_number != '\0') {
1562 if (!g_ascii_isdigit(*phone_number)) {
1563 return FALSE;
1565 ++phone_number;
1568 return TRUE;
1571 void sipe_core_media_phone_call(struct sipe_core_public *sipe_public,
1572 const gchar *phone_number)
1574 g_return_if_fail(sipe_public);
1576 SIPE_DEBUG_INFO("sipe_core_media_phone_call: %s", phone_number ? phone_number : "(null)");
1578 if (phone_number_is_valid(phone_number)) {
1579 gchar *phone_uri = g_strdup_printf("sip:%s@%s;user=phone",
1580 phone_number, sipe_public->sip_domain);
1582 sipe_core_media_initiate_call(sipe_public, phone_uri, FALSE);
1584 g_free(phone_uri);
1585 } else {
1586 sipe_backend_notify_error(sipe_public,
1587 _("Unable to establish a call"),
1588 _("Invalid phone number"));
1592 void sipe_core_media_test_call(struct sipe_core_public *sipe_public)
1594 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1595 if (!sipe_private->test_call_bot_uri) {
1596 sipe_backend_notify_error(sipe_public,
1597 _("Unable to establish a call"),
1598 _("Audio Test Service is not available."));
1599 return;
1602 sipe_core_media_initiate_call(sipe_public,
1603 sipe_private->test_call_bot_uri, FALSE);
1606 static struct sipe_media_call_private *
1607 sipe_media_from_sipmsg(struct sipe_core_private *sipe_private,
1608 struct sipmsg *msg)
1610 return g_hash_table_lookup(sipe_private->media_calls,
1611 sipmsg_find_header(msg, "Call-ID"));
1614 static void
1615 transport_response_unsupported_sdp(struct sipe_core_private *sipe_private,
1616 struct sipmsg *msg)
1618 sipmsg_add_header(msg, "ms-client-diagnostics",
1619 "52063;reason=\"Unsupported session description\"");
1620 sip_transport_response(sipe_private, msg,
1621 488, "Not Acceptable Here", NULL);
1624 static void
1625 maybe_send_second_invite_response(struct sipe_media_call_private *call_private)
1627 GSList *it;
1629 /* Second INVITE request had to be received and all streams must have
1630 * established candidate pairs before the response can be sent. */
1632 if (!call_private->invitation) {
1633 return;
1636 for (it = call_private->streams; it; it = it->next) {
1637 struct sipe_media_stream_private *stream_private = it->data;
1638 if (!stream_private->established) {
1639 return;
1643 send_response_with_session_description(call_private, 200, "OK");
1645 #ifdef HAVE_XDATA
1646 for (it = call_private->streams; it; it = it->next) {
1647 struct sipe_media_stream_private *stream_private = it->data;
1649 stream_private->sdp_negotiation_concluded = TRUE;
1650 if (stream_private->writable) {
1651 // We've become writable.
1652 sipe_core_media_stream_writable(SIPE_MEDIA_STREAM, TRUE);
1655 #endif
1658 struct sipe_media_call *
1659 process_incoming_invite_call(struct sipe_core_private *sipe_private,
1660 struct sipmsg *msg,
1661 const gchar *sdp)
1663 return(process_incoming_invite_call_parsed_sdp(sipe_private,
1664 msg,
1665 sdpmsg_parse_msg(sdp)));
1668 struct sipe_media_call *
1669 process_incoming_invite_call_parsed_sdp(struct sipe_core_private *sipe_private,
1670 struct sipmsg *msg,
1671 struct sdpmsg *smsg)
1673 struct sipe_media_call_private *call_private;
1674 gboolean has_new_media = FALSE;
1675 GSList *i;
1677 // Don't allow two voice calls in parallel.
1678 if (!strstr(msg->body, "m=data") &&
1679 !strstr(msg->body, "m=applicationsharing")) {
1680 struct sipe_media_call *call =
1681 sipe_core_media_get_call(SIPE_CORE_PUBLIC);
1682 if (call && !is_media_session_msg(SIPE_MEDIA_CALL_PRIVATE, msg)) {
1683 sip_transport_response(sipe_private, msg,
1684 486, "Busy Here", NULL);
1685 sdpmsg_free(smsg);
1686 return NULL;
1690 call_private = sipe_media_from_sipmsg(sipe_private, msg);
1692 if (call_private) {
1693 char *self = sip_uri_self(sipe_private);
1694 if (sipe_strequal(SIPE_MEDIA_CALL->with, self)) {
1695 g_free(self);
1696 sip_transport_response(sipe_private, msg, 488, "Not Acceptable Here", NULL);
1697 sdpmsg_free(smsg);
1698 return NULL;
1700 g_free(self);
1703 if (!smsg) {
1704 transport_response_unsupported_sdp(sipe_private, msg);
1705 if (call_private) {
1706 sipe_media_hangup(call_private);
1708 return NULL;
1711 if (!call_private) {
1712 gchar *with = parse_from(sipmsg_find_header(msg, "From"));
1713 SipeMediaCallFlags flags = 0;
1715 if (strstr(msg->body, "m=data") ||
1716 strstr(msg->body, "m=applicationsharing")) {
1717 flags |= SIPE_MEDIA_CALL_NO_UI;
1720 call_private = (struct sipe_media_call_private *)
1721 sipe_media_call_new(sipe_private, with,
1722 msg, smsg->ice_version,
1723 flags);
1725 if (!(flags & SIPE_MEDIA_CALL_NO_UI)) {
1726 SIPE_MEDIA_CALL->call_reject_cb = av_call_reject_cb;
1728 g_free(with);
1731 if (call_private->invitation)
1732 sipmsg_free(call_private->invitation);
1733 call_private->invitation = sipmsg_copy(msg);
1735 ssrc_range_update(&call_private->ssrc_ranges, smsg->media);
1737 // Create any new media streams
1738 for (i = smsg->media; i; i = i->next) {
1739 struct sdpmedia *media = i->data;
1740 gchar *id = media->name;
1741 SipeMediaType type;
1743 if ( media->port != 0
1744 && !sipe_core_media_get_stream_by_id(SIPE_MEDIA_CALL, id)) {
1745 guint32 ssrc_count = 0;
1747 if (sipe_strequal(id, "audio"))
1748 type = SIPE_MEDIA_AUDIO;
1749 else if (sipe_strequal(id, "video")) {
1750 type = SIPE_MEDIA_VIDEO;
1751 ssrc_count = VIDEO_SSRC_COUNT;
1752 } else if (sipe_strequal(id, "data"))
1753 type = SIPE_MEDIA_APPLICATION;
1754 else if (sipe_strequal(id, "applicationsharing"))
1755 type = SIPE_MEDIA_APPLICATION;
1756 else
1757 continue;
1759 sipe_media_stream_add(SIPE_MEDIA_CALL, id, type,
1760 smsg->ice_version, FALSE,
1761 ssrc_count);
1762 has_new_media = TRUE;
1766 if (has_new_media) {
1767 sdpmsg_free(call_private->smsg);
1768 call_private->smsg = smsg;
1769 sip_transport_response(sipe_private, call_private->invitation,
1770 180, "Ringing", NULL);
1771 // Processing continues in stream_initialized_cb
1772 } else {
1773 apply_remote_message(call_private, smsg);
1774 sdpmsg_free(smsg);
1775 maybe_send_second_invite_response(call_private);
1778 return SIPE_MEDIA_CALL;
1781 void process_incoming_cancel_call(struct sipe_media_call_private *call_private,
1782 struct sipmsg *msg)
1784 // We respond to the CANCEL request with 200 OK response and
1785 // with 487 Request Terminated to the remote INVITE in progress.
1786 sip_transport_response(call_private->sipe_private, msg, 200, "OK", NULL);
1788 if (call_private->invitation) {
1789 sip_transport_response(call_private->sipe_private,
1790 call_private->invitation,
1791 487, "Request Terminated", NULL);
1794 sipe_backend_media_reject(SIPE_MEDIA_CALL->backend_private, FALSE);
1797 static gboolean
1798 sipe_media_send_ack(struct sipe_core_private *sipe_private,
1799 struct sipmsg *msg,
1800 struct transaction *trans)
1802 struct sipe_media_call_private *call_private;
1803 struct sip_dialog *dialog;
1804 int tmp_cseq;
1806 call_private = sipe_media_from_sipmsg(sipe_private, msg);
1808 if (!is_media_session_msg(call_private, msg))
1809 return FALSE;
1811 dialog = sipe_media_get_sip_dialog(SIPE_MEDIA_CALL);
1812 if (!dialog)
1813 return FALSE;
1815 tmp_cseq = dialog->cseq;
1817 dialog->cseq = sip_transaction_cseq(trans) - 1;
1818 sip_transport_ack(sipe_private, dialog);
1819 dialog->cseq = tmp_cseq;
1821 dialog->outgoing_invite = NULL;
1823 return TRUE;
1826 static gboolean
1827 sipe_media_send_final_ack(struct sipe_core_private *sipe_private,
1828 struct sipmsg *msg,
1829 struct transaction *trans)
1831 struct sipe_media_call_private *call_private;
1832 #ifdef HAVE_XDATA
1833 GSList *it;
1834 #endif
1836 if (!sipe_media_send_ack(sipe_private, msg, trans))
1837 return FALSE;
1839 call_private = sipe_media_from_sipmsg(sipe_private, msg);
1841 sipe_backend_media_accept(SIPE_MEDIA_CALL->backend_private, FALSE);
1843 #ifdef HAVE_XDATA
1844 for (it = call_private->streams; it; it = it->next) {
1845 struct sipe_media_stream_private *stream_private = it->data;
1847 stream_private->sdp_negotiation_concluded = TRUE;
1848 if (stream_private->writable) {
1849 // We've become writable.
1850 sipe_core_media_stream_writable(SIPE_MEDIA_STREAM, TRUE);
1853 #endif
1855 return TRUE;
1858 void
1859 sipe_core_media_stream_candidate_pair_established(struct sipe_media_stream *stream)
1861 struct sipe_media_call *call = stream->call;
1863 GList *active_candidates =
1864 sipe_backend_media_stream_get_active_local_candidates(stream);
1865 guint ready_components = g_list_length(active_candidates);
1867 sipe_media_candidate_list_free(active_candidates);
1869 if (ready_components != 2) {
1870 // We must have both RTP+RTCP candidate pairs established first.
1871 return;
1874 if (SIPE_MEDIA_STREAM_PRIVATE->established) {
1875 return;
1877 SIPE_MEDIA_STREAM_PRIVATE->established = TRUE;
1879 stream_schedule_cancel_timeout(call, SIPE_MEDIA_STREAM_PRIVATE);
1881 if (stream->candidate_pairs_established_cb) {
1882 stream->candidate_pairs_established_cb(stream);
1885 if (sipe_backend_media_is_initiator(stream->call, NULL)) {
1886 GSList *streams = SIPE_MEDIA_CALL_PRIVATE->streams;
1887 for (; streams; streams = streams->next) {
1888 struct sipe_media_stream_private *s = streams->data;
1889 if (!s->established) {
1890 break;
1894 if (streams == NULL) {
1895 // All call streams have been established.
1896 sipe_invite_call(SIPE_MEDIA_CALL_PRIVATE,
1897 sipe_media_send_final_ack);
1899 } else {
1900 maybe_send_second_invite_response(SIPE_MEDIA_CALL_PRIVATE);
1904 static gboolean
1905 maybe_retry_call_with_ice_version(struct sipe_core_private *sipe_private,
1906 struct sipe_media_call_private *call_private,
1907 SipeIceVersion ice_version,
1908 struct transaction *trans)
1910 if (call_private->ice_version != ice_version &&
1911 sip_transaction_cseq(trans) == 1) {
1912 GSList *i;
1913 gchar *with;
1914 gboolean with_video = FALSE;
1916 for (i = call_private->streams; i; i = i->next) {
1917 struct sipe_media_stream *stream = i->data;
1919 if (sipe_strequal(stream->id, "video")) {
1920 with_video = TRUE;
1921 } else if (!sipe_strequal(stream->id, "audio")) {
1922 /* Don't retry calls which are neither audio
1923 * nor video. */
1924 return FALSE;
1928 with = g_strdup(SIPE_MEDIA_CALL->with);
1930 sipe_media_hangup(call_private);
1931 SIPE_DEBUG_INFO("Retrying call with ICEv%d.",
1932 ice_version == SIPE_ICE_DRAFT_6 ? 6 : 19);
1933 sipe_media_initiate_call(sipe_private,
1934 with,
1935 ice_version,
1936 with_video);
1938 g_free(with);
1939 return TRUE;
1942 return FALSE;
1945 static gboolean
1946 process_invite_call_response(struct sipe_core_private *sipe_private,
1947 struct sipmsg *msg,
1948 struct transaction *trans)
1950 const gchar *with;
1951 struct sipe_media_call_private *call_private;
1952 struct sip_dialog *dialog;
1953 struct sdpmsg *smsg;
1955 call_private = sipe_media_from_sipmsg(sipe_private,msg);
1957 if (!is_media_session_msg(call_private, msg))
1958 return FALSE;
1960 dialog = sipe_media_get_sip_dialog(SIPE_MEDIA_CALL);
1962 with = dialog->with;
1964 dialog->outgoing_invite = NULL;
1966 if (msg->response == 603 || msg->response == 605) {
1967 // Call rejected by remote peer
1968 sipe_media_send_ack(sipe_private, msg, trans);
1969 sipe_backend_media_reject(SIPE_MEDIA_CALL->backend_private, FALSE);
1971 return TRUE;
1974 if (msg->response >= 400) {
1975 // An error occurred
1976 const gchar *title;
1977 GString *desc = g_string_new("");
1978 gboolean append_responsestr = FALSE;
1980 switch (msg->response) {
1981 case 480: {
1982 title = _("User unavailable");
1984 if (sipmsg_parse_warning(msg, NULL) == 391) {
1985 g_string_append_printf(desc, _("%s does not want to be disturbed"), with);
1986 } else
1987 g_string_append_printf(desc, _("User %s is not available"), with);
1988 break;
1990 case 415:
1991 // OCS/Lync really sends response string with 'Mutipart' typo.
1992 if (sipe_strequal(msg->responsestr, "Mutipart mime in content type not supported by Archiving CDR service") &&
1993 maybe_retry_call_with_ice_version(sipe_private,
1994 call_private,
1995 SIPE_ICE_DRAFT_6,
1996 trans)) {
1997 return TRUE;
1999 title = _("Unsupported media type");
2000 break;
2001 case 488: {
2002 /* Check for incompatible encryption levels error.
2004 * MS Lync 2010:
2005 * 488 Not Acceptable Here
2006 * ms-client-diagnostics: 52017;reason="Encryption levels dont match"
2008 * older clients (and SIPE itself):
2009 * 488 Encryption Levels not compatible
2011 const gchar *ms_diag = sipmsg_find_header(msg, "ms-client-diagnostics");
2012 SipeIceVersion retry_ice_version = SIPE_ICE_DRAFT_6;
2014 if (sipe_strequal(msg->responsestr, "Encryption Levels not compatible") ||
2015 (ms_diag && g_str_has_prefix(ms_diag, "52017;"))) {
2016 title = _("Unable to establish a call");
2017 g_string_append(desc, _("Encryption settings of peer are incompatible with ours."));
2018 break;
2021 /* Check if this is failed conference using
2022 * ICEv6 with reason "Error parsing SDP" and
2023 * retry using ICEv19. */
2024 ms_diag = sipmsg_find_header(msg, "ms-diagnostics");
2025 if (ms_diag && g_str_has_prefix(ms_diag, "7008;")) {
2026 retry_ice_version = SIPE_ICE_RFC_5245;
2029 if (maybe_retry_call_with_ice_version(sipe_private,
2030 call_private,
2031 retry_ice_version,
2032 trans)) {
2033 return TRUE;
2035 SIPE_FALLTHROUGH
2037 default:
2038 title = _("Error occurred");
2039 g_string_append(desc, _("Unable to establish a call"));
2040 append_responsestr = TRUE;
2041 break;
2044 if (append_responsestr) {
2045 gchar *reason = sipmsg_get_ms_diagnostics_reason(msg);
2047 g_string_append_printf(desc, "\n%d %s",
2048 msg->response, msg->responsestr);
2049 if (reason) {
2050 g_string_append_printf(desc, "\n\n%s", reason);
2051 g_free(reason);
2055 sipe_backend_notify_error(SIPE_CORE_PUBLIC, title, desc->str);
2056 g_string_free(desc, TRUE);
2058 sipe_media_send_ack(sipe_private, msg, trans);
2059 sipe_media_hangup(call_private);
2061 return TRUE;
2064 sipe_dialog_parse(dialog, msg, TRUE);
2065 smsg = sdpmsg_parse_msg(msg->body);
2066 if (!smsg) {
2067 transport_response_unsupported_sdp(sipe_private, msg);
2068 sipe_media_hangup(call_private);
2069 return FALSE;
2072 ssrc_range_update(&call_private->ssrc_ranges, smsg->media);
2073 apply_remote_message(call_private, smsg);
2074 sdpmsg_free(smsg);
2076 stream_schedule_timeout(SIPE_MEDIA_CALL);
2077 call_schedule_cancel_request_timeout(SIPE_MEDIA_CALL);
2078 sipe_media_send_ack(sipe_private, msg, trans);
2080 return TRUE;
2082 // Waits until sipe_core_media_candidate_pair_established() is invoked.
2085 gboolean is_media_session_msg(struct sipe_media_call_private *call_private,
2086 struct sipmsg *msg)
2088 if (!call_private) {
2089 return FALSE;
2092 return sipe_media_from_sipmsg(call_private->sipe_private, msg) == call_private;
2095 static void
2096 end_call(SIPE_UNUSED_PARAMETER gpointer key,
2097 struct sipe_media_call_private *call_private,
2098 SIPE_UNUSED_PARAMETER gpointer user_data)
2100 struct sipe_backend_media *backend_private;
2102 backend_private = call_private->public.backend_private;
2104 if (!sipe_backend_media_is_initiator(SIPE_MEDIA_CALL, NULL) &&
2105 !sipe_backend_media_accepted(backend_private)) {
2106 sip_transport_response(call_private->sipe_private,
2107 call_private->invitation,
2108 480, "Temporarily Unavailable", NULL);
2109 } else if (call_private->session) {
2110 sipe_session_close(call_private->sipe_private,
2111 call_private->session);
2112 call_private->session = NULL;
2115 sipe_media_hangup(call_private);
2118 void
2119 sipe_media_handle_going_offline(struct sipe_core_private *sipe_private)
2121 g_hash_table_foreach(sipe_private->media_calls, (GHFunc) end_call, NULL);
2124 gboolean sipe_media_is_conference_call(struct sipe_media_call_private *call_private)
2126 return g_strstr_len(SIPE_MEDIA_CALL->with, -1, "app:conf:audio-video:") != NULL;
2129 struct sipe_core_private *
2130 sipe_media_get_sipe_core_private(struct sipe_media_call *call)
2132 g_return_val_if_fail(call, NULL);
2134 return SIPE_MEDIA_CALL_PRIVATE->sipe_private;
2137 struct sip_dialog *
2138 sipe_media_get_sip_dialog(struct sipe_media_call *call)
2140 struct sip_session *session;
2142 g_return_val_if_fail(call, NULL);
2144 session = SIPE_MEDIA_CALL_PRIVATE->session;
2146 if (!session || !session->dialogs) {
2147 return NULL;
2150 return session->dialogs->data;
2153 static void
2154 sipe_media_relay_free(struct sipe_media_relay *relay)
2156 g_free(relay->hostname);
2157 if (relay->dns_query)
2158 sipe_backend_dns_query_cancel(relay->dns_query);
2159 g_free(relay);
2162 void
2163 sipe_media_relay_list_free(GSList *list)
2165 for (; list; list = g_slist_delete_link(list, list))
2166 sipe_media_relay_free(list->data);
2169 static void
2170 relay_ip_resolved_cb(struct sipe_media_relay* relay,
2171 const gchar *ip, SIPE_UNUSED_PARAMETER guint port)
2173 gchar *hostname = relay->hostname;
2174 relay->dns_query = NULL;
2176 if (ip && port) {
2177 relay->hostname = g_strdup(ip);
2178 SIPE_DEBUG_INFO("Media relay %s resolved to %s.", hostname, ip);
2179 } else {
2180 relay->hostname = NULL;
2181 SIPE_DEBUG_INFO("Unable to resolve media relay %s.", hostname);
2184 g_free(hostname);
2187 static gboolean
2188 process_get_av_edge_credentials_response(struct sipe_core_private *sipe_private,
2189 struct sipmsg *msg,
2190 SIPE_UNUSED_PARAMETER struct transaction *trans)
2192 g_free(sipe_private->media_relay_username);
2193 g_free(sipe_private->media_relay_password);
2194 sipe_media_relay_list_free(sipe_private->media_relays);
2195 sipe_private->media_relay_username = NULL;
2196 sipe_private->media_relay_password = NULL;
2197 sipe_private->media_relays = NULL;
2199 if (msg->response >= 400) {
2200 SIPE_DEBUG_INFO_NOFORMAT("process_get_av_edge_credentials_response: SERVICE response is not 200. "
2201 "Failed to obtain A/V Edge credentials.");
2202 return FALSE;
2205 if (msg->response == 200) {
2206 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
2208 if (sipe_strequal("OK", sipe_xml_attribute(xn_response, "reasonPhrase"))) {
2209 const sipe_xml *xn_credentials = sipe_xml_child(xn_response, "credentialsResponse/credentials");
2210 const sipe_xml *xn_relays = sipe_xml_child(xn_response, "credentialsResponse/mediaRelayList");
2211 const sipe_xml *item;
2212 GSList *relays = NULL;
2214 item = sipe_xml_child(xn_credentials, "username");
2215 sipe_private->media_relay_username = sipe_xml_data(item);
2216 item = sipe_xml_child(xn_credentials, "password");
2217 sipe_private->media_relay_password = sipe_xml_data(item);
2219 for (item = sipe_xml_child(xn_relays, "mediaRelay"); item; item = sipe_xml_twin(item)) {
2220 struct sipe_media_relay *relay = g_new0(struct sipe_media_relay, 1);
2221 const sipe_xml *node;
2222 gchar *tmp;
2224 node = sipe_xml_child(item, "hostName");
2225 relay->hostname = sipe_xml_data(node);
2227 node = sipe_xml_child(item, "udpPort");
2228 if (node) {
2229 tmp = sipe_xml_data(node);
2230 if (tmp) {
2231 relay->udp_port = atoi(tmp);
2232 g_free(tmp);
2236 node = sipe_xml_child(item, "tcpPort");
2237 if (node) {
2238 tmp = sipe_xml_data(node);
2239 if (tmp) {
2240 relay->tcp_port = atoi(tmp);
2241 g_free(tmp);
2245 relays = g_slist_append(relays, relay);
2247 relay->dns_query = sipe_backend_dns_query_a(
2248 SIPE_CORE_PUBLIC,
2249 relay->hostname,
2250 relay->udp_port,
2251 (sipe_dns_resolved_cb) relay_ip_resolved_cb,
2252 relay);
2254 SIPE_DEBUG_INFO("Media relay: %s TCP: %d UDP: %d",
2255 relay->hostname,
2256 relay->tcp_port, relay->udp_port);
2259 sipe_private->media_relays = relays;
2262 sipe_xml_free(xn_response);
2265 return TRUE;
2268 void
2269 sipe_media_get_av_edge_credentials(struct sipe_core_private *sipe_private)
2271 // TODO: re-request credentials after duration expires?
2272 static const char CRED_REQUEST_XML[] =
2273 "<request requestID=\"%d\" "
2274 "from=\"%s\" "
2275 "version=\"1.0\" "
2276 "to=\"%s\" "
2277 "xmlns=\"http://schemas.microsoft.com/2006/09/sip/mrasp\" "
2278 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
2279 "<credentialsRequest credentialsRequestID=\"%d\">"
2280 "<identity>%s</identity>"
2281 "<location>%s</location>"
2282 "<duration>480</duration>"
2283 "</credentialsRequest>"
2284 "</request>";
2286 int request_id = rand();
2287 gchar *self;
2288 gchar *body;
2290 if (!sipe_private->mras_uri)
2291 return;
2293 self = sip_uri_self(sipe_private);
2295 body = g_strdup_printf(
2296 CRED_REQUEST_XML,
2297 request_id,
2298 self,
2299 sipe_private->mras_uri,
2300 request_id,
2301 self,
2302 SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER) ? "internet" : "intranet");
2303 g_free(self);
2305 sip_transport_service(sipe_private,
2306 sipe_private->mras_uri,
2307 "Content-Type: application/msrtc-media-relay-auth+xml\r\n",
2308 body,
2309 process_get_av_edge_credentials_response);
2311 g_free(body);
2314 void
2315 sipe_media_add_extra_invite_section(struct sipe_media_call *call,
2316 const gchar *invite_content_type,
2317 gchar *body)
2319 g_free(SIPE_MEDIA_CALL_PRIVATE->extra_invite_section);
2320 g_free(SIPE_MEDIA_CALL_PRIVATE->invite_content_type);
2321 SIPE_MEDIA_CALL_PRIVATE->extra_invite_section = body;
2322 SIPE_MEDIA_CALL_PRIVATE->invite_content_type =
2323 g_strdup(invite_content_type);
2326 void
2327 sipe_media_stream_add_extra_attribute(struct sipe_media_stream *stream,
2328 const gchar *name, const gchar *value)
2330 SIPE_MEDIA_STREAM_PRIVATE->extra_sdp =
2331 sipe_utils_nameval_add(SIPE_MEDIA_STREAM_PRIVATE->extra_sdp,
2332 name, value);
2335 #ifdef HAVE_XDATA
2336 void
2337 sipe_core_media_stream_readable(struct sipe_media_stream *stream)
2339 g_return_if_fail(stream);
2341 if (g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE->async_reads) &&
2342 stream->read_cb) {
2343 stream->read_cb(stream);
2346 while (!g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE->async_reads)) {
2347 struct async_read_data *data;
2348 guint8 *pos;
2349 gssize len;
2350 gssize bytes_read;
2352 data = g_queue_peek_head(SIPE_MEDIA_STREAM_PRIVATE->async_reads);
2353 pos = data->buffer + SIPE_MEDIA_STREAM_PRIVATE->read_pos;
2354 len = data->len - SIPE_MEDIA_STREAM_PRIVATE->read_pos;
2356 bytes_read = sipe_backend_media_stream_read(stream, pos, len);
2357 if (bytes_read == -1) {
2358 struct sipe_media_call *call = stream->call;
2359 struct sipe_core_private *sipe_private =
2360 SIPE_MEDIA_CALL_PRIVATE->sipe_private;
2362 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
2363 _("Media error"),
2364 _("Error while reading from stream"));
2365 sipe_media_hangup(SIPE_MEDIA_CALL_PRIVATE);
2366 return;
2369 SIPE_MEDIA_STREAM_PRIVATE->read_pos += bytes_read;
2371 if (SIPE_MEDIA_STREAM_PRIVATE->read_pos == data->len) {
2372 data->callback(stream, data->buffer, data->len);
2373 SIPE_MEDIA_STREAM_PRIVATE->read_pos = 0;
2374 g_queue_pop_head(SIPE_MEDIA_STREAM_PRIVATE->async_reads);
2375 g_free(data);
2376 } else {
2377 // Still not enough data to finish the read.
2378 return;
2383 void
2384 sipe_media_stream_read_async(struct sipe_media_stream *stream,
2385 gpointer buffer, gsize len,
2386 sipe_media_stream_read_callback callback)
2388 struct async_read_data *data;
2390 g_return_if_fail(stream && buffer && callback);
2392 data = g_new0(struct async_read_data, 1);
2393 data->buffer = buffer;
2394 data->len = len;
2395 data->callback = callback;
2397 g_queue_push_tail(SIPE_MEDIA_STREAM_PRIVATE->async_reads, data);
2400 static void
2401 stream_append_buffer(struct sipe_media_stream *stream,
2402 guint8 *buffer, guint len)
2404 GByteArray *b = g_byte_array_sized_new(len);
2405 g_byte_array_append(b, buffer, len);
2406 g_queue_push_tail(SIPE_MEDIA_STREAM_PRIVATE->write_queue, b);
2409 gboolean
2410 sipe_media_stream_write(struct sipe_media_stream *stream,
2411 gpointer buffer, gsize len)
2413 if (!sipe_media_stream_is_writable(stream)) {
2414 stream_append_buffer(stream, buffer, len);
2415 return FALSE;
2416 } else {
2417 guint written;
2419 written = sipe_backend_media_stream_write(stream, buffer, len);
2420 if (written == len) {
2421 return TRUE;
2424 stream_append_buffer(stream,
2425 (guint8 *)buffer + written, len - written);
2426 return FALSE;
2430 void
2431 sipe_core_media_stream_writable(struct sipe_media_stream *stream,
2432 gboolean writable)
2434 SIPE_MEDIA_STREAM_PRIVATE->writable = writable;
2436 if (!writable) {
2437 return;
2440 while (!g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE->write_queue)) {
2441 GByteArray *b;
2442 guint written;
2444 b = g_queue_peek_head(SIPE_MEDIA_STREAM_PRIVATE->write_queue);
2446 written = sipe_backend_media_stream_write(stream, b->data, b->len);
2447 if (written != b->len) {
2448 g_byte_array_remove_range(b, 0, written);
2449 return;
2452 g_byte_array_unref(b);
2453 g_queue_pop_head(SIPE_MEDIA_STREAM_PRIVATE->write_queue);
2456 if (sipe_media_stream_is_writable(stream) && stream->writable_cb) {
2457 stream->writable_cb(stream);
2461 gboolean
2462 sipe_media_stream_is_writable(struct sipe_media_stream *stream)
2464 return SIPE_MEDIA_STREAM_PRIVATE->writable &&
2465 SIPE_MEDIA_STREAM_PRIVATE->sdp_negotiation_concluded &&
2466 g_queue_is_empty(SIPE_MEDIA_STREAM_PRIVATE->write_queue);
2468 #endif
2470 void
2471 sipe_media_stream_set_data(struct sipe_media_stream *stream, gpointer data,
2472 GDestroyNotify free_func)
2474 struct sipe_media_stream_private *stream_private =
2475 SIPE_MEDIA_STREAM_PRIVATE;
2477 g_return_if_fail(stream_private);
2479 if (stream_private->data && stream_private->data_free_func) {
2480 stream_private->data_free_func(stream_private->data);
2483 stream_private->data = data;
2484 stream_private->data_free_func = free_func;
2487 gpointer
2488 sipe_media_stream_get_data(struct sipe_media_stream *stream)
2490 g_return_val_if_fail(stream, NULL);
2492 return SIPE_MEDIA_STREAM_PRIVATE->data;
2496 Local Variables:
2497 mode: c
2498 c-file-style: "bsd"
2499 indent-tabs-mode: t
2500 tab-width: 8
2501 End: