audio: sipe-media.c is purple-free
[siplcs.git] / src / core / sipe-media.c
blob93ed5e7de0c5346f9ce2947a683925206feec633
1 /**
2 * @file sipe-media.c
4 * pidgin-sipe
6 * Copyright (C) 2010 Jakub Adam <jakub.adam@tieto.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
27 #include "account.h"
29 #include "sipe-core.h"
30 #include "sipe.h"
31 #include "sipmsg.h"
32 #include "sipe-session.h"
33 #include "sipe-media.h"
34 #include "sipe-dialog.h"
35 #include "sipe-utils.h"
36 #include "sipe-common.h"
38 gchar *
39 sipe_media_get_callid(sipe_media_call *call)
41 return call->dialog->callid;
44 void sipe_media_codec_list_free(GList *codecs)
46 for (; codecs; codecs = g_list_delete_link(codecs, codecs)) {
47 sipe_backend_codec_free(codecs->data);
51 void sipe_media_candidate_list_free(GList *candidates)
53 for (; candidates; candidates = g_list_delete_link(candidates, candidates)) {
54 sipe_backend_candidate_free(candidates->data);
58 static void
59 sipe_media_call_free(sipe_media_call *call)
61 if (call) {
62 sipe_utils_nameval_free(call->sdp_attrs);
63 if (call->invitation)
64 sipmsg_free(call->invitation);
65 sipe_media_codec_list_free(call->remote_codecs);
66 sipe_media_candidate_list_free(call->remote_candidates);
67 g_free(call);
71 static GList *
72 sipe_media_parse_remote_codecs(const sipe_media_call *call)
74 int i = 0;
75 const gchar *attr;
76 GList *codecs = NULL;
78 while ((attr = sipe_utils_nameval_find_instance(call->sdp_attrs, "rtpmap", i++))) {
79 gchar **tokens = g_strsplit_set(attr, " /", 3);
81 int id = atoi(tokens[0]);
82 gchar *name = tokens[1];
83 int clock_rate = atoi(tokens[2]);
84 SipeMediaType type = SIPE_MEDIA_AUDIO;
86 sipe_codec *codec = sipe_backend_codec_new(id, name, clock_rate, type);
88 codecs = g_list_append(codecs, codec);
89 g_strfreev(tokens);
92 return codecs;
95 static gint
96 codec_name_compare(sipe_codec* codec1, sipe_codec* codec2)
98 gchar *name1 = sipe_backend_codec_get_name(codec1);
99 gchar *name2 = sipe_backend_codec_get_name(codec2);
101 return g_strcmp0(name1, name2);
104 static GList *
105 sipe_media_prune_remote_codecs(sipe_media_call *call, GList *codecs)
107 GList *remote_codecs = codecs;
108 GList *local_codecs = sipe_backend_get_local_codecs(call);
109 GList *pruned_codecs = NULL;
111 while (remote_codecs) {
112 sipe_codec *c = remote_codecs->data;
114 if (g_list_find_custom(local_codecs, c, (GCompareFunc)codec_name_compare)) {
115 pruned_codecs = g_list_append(pruned_codecs, c);
116 remote_codecs->data = NULL;
118 remote_codecs = remote_codecs->next;
121 sipe_media_codec_list_free(codecs);
123 return pruned_codecs;
126 static GList *
127 sipe_media_parse_remote_candidates(sipe_media_call *call)
129 GSList *sdp_attrs = call->sdp_attrs;
130 sipe_candidate *candidate;
131 GList *candidates = NULL;
132 const gchar *attr;
133 int i = 0;
135 const gchar* username = sipe_utils_nameval_find(sdp_attrs, "ice-ufrag");
136 const gchar* password = sipe_utils_nameval_find(sdp_attrs, "ice-pwd");
138 while ((attr = sipe_utils_nameval_find_instance(sdp_attrs, "candidate", i++))) {
139 gchar **tokens;
140 gchar *foundation;
141 SipeComponentType component;
142 SipeNetworkProtocol protocol;
143 guint32 priority;
144 gchar* ip;
145 guint16 port;
146 SipeCandidateType type;
148 tokens = g_strsplit_set(attr, " ", 0);
150 foundation = tokens[0];
152 switch (atoi(tokens[1])) {
153 case 1:
154 component = SIPE_COMPONENT_RTP;
155 break;
156 case 2:
157 component = SIPE_COMPONENT_RTCP;
158 break;
159 default:
160 component = SIPE_COMPONENT_NONE;
163 if (sipe_strequal(tokens[2], "UDP"))
164 protocol = SIPE_NETWORK_PROTOCOL_UDP;
165 else {
166 // Ignore TCP candidates, at least for now...
167 g_strfreev(tokens);
168 continue;
171 priority = atoi(tokens[3]);
172 ip = tokens[4];
173 port = atoi(tokens[5]);
175 if (sipe_strequal(tokens[7], "host"))
176 type = SIPE_CANDIDATE_TYPE_HOST;
177 else if (sipe_strequal(tokens[7], "relay"))
178 type = SIPE_CANDIDATE_TYPE_RELAY;
179 else if (sipe_strequal(tokens[7], "srflx"))
180 type = SIPE_CANDIDATE_TYPE_SRFLX;
181 else {
182 g_strfreev(tokens);
183 continue;
186 candidate = sipe_backend_candidate_new(foundation, component,
187 type, protocol, ip, port);
188 sipe_backend_candidate_set_priority(candidate, priority);
189 candidates = g_list_append(candidates, candidate);
191 g_strfreev(tokens);
194 if (!candidates) {
195 // No a=candidate in SDP message, revert to OC2005 behaviour
196 candidate = sipe_backend_candidate_new("foundation",
197 SIPE_COMPONENT_RTP,
198 SIPE_CANDIDATE_TYPE_HOST,
199 SIPE_NETWORK_PROTOCOL_UDP,
200 call->remote_ip, call->remote_port);
201 candidates = g_list_append(candidates, candidate);
203 candidate = sipe_backend_candidate_new("foundation",
204 SIPE_COMPONENT_RTCP,
205 SIPE_CANDIDATE_TYPE_HOST,
206 SIPE_NETWORK_PROTOCOL_UDP,
207 call->remote_ip, call->remote_port + 1);
208 candidates = g_list_append(candidates, candidate);
210 // This seems to be pre-OC2007 R2 UAC
211 call->legacy_mode = TRUE;
214 if (username) {
215 GList *it = candidates;
216 while (it) {
217 sipe_backend_candidate_set_username_and_pwd(it->data, username, password);
218 it = it->next;
222 return candidates;
225 static gchar *
226 sipe_media_sdp_codec_ids_format(GList *codecs)
228 GString *result = g_string_new(NULL);
230 while (codecs) {
231 sipe_codec *c = codecs->data;
233 gchar *tmp = g_strdup_printf(" %d", sipe_backend_codec_get_id(c));
234 g_string_append(result,tmp);
235 g_free(tmp);
237 codecs = codecs->next;
240 return g_string_free(result, FALSE);
243 static gchar *
244 sipe_media_sdp_codecs_format(GList *codecs)
246 GString *result = g_string_new(NULL);
248 while (codecs) {
249 sipe_codec *c = codecs->data;
250 GList *params = NULL;
252 gchar *tmp = g_strdup_printf("a=rtpmap:%d %s/%d\r\n",
253 sipe_backend_codec_get_id(c),
254 sipe_backend_codec_get_name(c),
255 sipe_backend_codec_get_clock_rate(c));
257 g_string_append(result, tmp);
258 g_free(tmp);
260 if ((params = sipe_backend_codec_get_optional_parameters(c))) {
261 tmp = g_strdup_printf("a=fmtp:%d",sipe_backend_codec_get_id(c));
262 g_string_append(result, tmp);
263 g_free(tmp);
265 while (params) {
266 struct sipnameval* par = params->data;
267 tmp = g_strdup_printf(" %s=%s", par->name, par->value);
268 g_string_append(result, tmp);
269 g_free(tmp);
270 params = params->next;
272 g_string_append(result, "\r\n");
275 codecs = codecs->next;
278 return g_string_free(result, FALSE);
281 static gchar *
282 sipe_media_sdp_candidates_format(GList *candidates, sipe_media_call* call, gboolean remote_candidate)
284 GString *result = g_string_new("");
285 gchar *tmp;
286 gchar *username = sipe_backend_candidate_get_username(candidates->data);
287 gchar *password = sipe_backend_candidate_get_password(candidates->data);
288 guint16 rtcp_port = 0;
290 if (call->legacy_mode)
291 return g_string_free(result, FALSE);
293 tmp = g_strdup_printf("a=ice-ufrag:%s\r\na=ice-pwd:%s\r\n",username, password);
294 g_string_append(result, tmp);
295 g_free(tmp);
297 while (candidates) {
298 sipe_candidate *c = candidates->data;
300 guint16 port;
301 guint16 component;
302 gchar *protocol;
303 gchar *type;
305 port = sipe_backend_candidate_get_port(c);
307 switch (sipe_backend_candidate_get_component_type(c)) {
308 case SIPE_COMPONENT_RTP:
309 component = 1;
310 break;
311 case SIPE_COMPONENT_RTCP:
312 component = 2;
313 if (rtcp_port == 0)
314 rtcp_port = port;
315 break;
316 case SIPE_COMPONENT_NONE:
317 component = 0;
320 switch (sipe_backend_candidate_get_protocol(c)) {
321 case SIPE_NETWORK_PROTOCOL_TCP:
322 protocol = "TCP";
323 break;
324 case SIPE_NETWORK_PROTOCOL_UDP:
325 protocol = "UDP";
326 break;
329 switch (sipe_backend_candidate_get_type(c)) {
330 case SIPE_CANDIDATE_TYPE_HOST:
331 type = "host";
332 break;
333 case SIPE_CANDIDATE_TYPE_RELAY:
334 type = "relay";
335 break;
336 case SIPE_CANDIDATE_TYPE_SRFLX:
337 type = "srflx";
338 break;
339 default:
340 // TODO: error unknown/unsupported type
341 break;
344 tmp = g_strdup_printf("a=candidate:%s %u %s %u %s %d typ %s \r\n",
345 sipe_backend_candidate_get_foundation(c),
346 component,
347 protocol,
348 sipe_backend_candidate_get_priority(c),
349 sipe_backend_candidate_get_ip(c),
350 port,
351 type);
353 g_string_append(result, tmp);
354 g_free(tmp);
356 candidates = candidates->next;
359 if (remote_candidate) {
360 sipe_candidate *first = call->remote_candidates->data;
361 sipe_candidate *second = call->remote_candidates->next->data;
362 tmp = g_strdup_printf("a=remote-candidates:1 %s %u 2 %s %u\r\n",
363 sipe_backend_candidate_get_ip(first), sipe_backend_candidate_get_port(first),
364 sipe_backend_candidate_get_ip(second), sipe_backend_candidate_get_port(second));
366 g_string_append(result, tmp);
367 g_free(tmp);
371 if (rtcp_port != 0) {
372 tmp = g_strdup_printf("a=maxptime:200\r\na=rtcp:%u\r\n", rtcp_port);
373 g_string_append(result, tmp);
374 g_free(tmp);
377 return g_string_free(result, FALSE);
380 static gchar*
381 sipe_media_create_sdp(sipe_media_call *call, gboolean remote_candidate) {
382 GList *local_codecs = sipe_backend_get_local_codecs(call);
383 GList *local_candidates = sipe_backend_get_local_candidates(call, call->dialog->with);
385 // TODO: more sophisticated
386 guint16 local_port = sipe_backend_candidate_get_port(local_candidates->data);
387 const char *ip = sipe_utils_get_suitable_local_ip(-1);
389 gchar *sdp_codecs = sipe_media_sdp_codecs_format(local_codecs);
390 gchar *sdp_codec_ids = sipe_media_sdp_codec_ids_format(local_codecs);
391 gchar *sdp_candidates = sipe_media_sdp_candidates_format(local_candidates, call, remote_candidate);
392 gchar *inactive = call->state == SIPE_CALL_HELD ? "a=inactive\r\n" : "";
394 gchar *body = g_strdup_printf(
395 "v=0\r\n"
396 "o=- 0 0 IN IP4 %s\r\n"
397 "s=session\r\n"
398 "c=IN IP4 %s\r\n"
399 "b=CT:99980\r\n"
400 "t=0 0\r\n"
401 "m=audio %d RTP/AVP%s\r\n"
402 "%s"
403 "%s"
404 "%s"
405 "a=encryption:rejected\r\n"
406 ,ip, ip, local_port, sdp_codec_ids, sdp_candidates, inactive, sdp_codecs);
408 g_free(sdp_codecs);
409 g_free(sdp_codec_ids);
410 g_free(sdp_candidates);
412 return body;
415 static void
416 sipe_invite_call(struct sipe_account_data *sip)
418 gchar *hdr;
419 gchar *contact;
420 gchar *body;
421 sipe_media_call *call = sip->media_call;
422 struct sip_dialog *dialog = call->dialog;
424 contact = get_contact(sip);
425 hdr = g_strdup_printf(
426 "Supported: ms-sender\r\n"
427 "ms-keep-alive: UAC;hop-hop=yes\r\n"
428 "Contact: %s%s\r\n"
429 "Supported: Replaces\r\n"
430 "Content-Type: application/sdp\r\n",
431 contact,
432 call->state == SIPE_CALL_HELD ? ";+sip.rendering=\"no\"" : "");
433 g_free(contact);
435 body = sipe_media_create_sdp(call, TRUE);
437 send_sip_request(sip->gc, "INVITE", dialog->with, dialog->with, hdr, body,
438 dialog, NULL);
440 g_free(body);
441 g_free(hdr);
444 static void
445 notify_state_change(struct sipe_account_data *sip, gboolean local) {
446 if (local) {
447 sipe_invite_call(sip);
448 } else {
449 gchar* body = sipe_media_create_sdp(sip->media_call, TRUE);
450 send_sip_response(sip->gc, sip->media_call->invitation, 200, "OK", body);
451 g_free(body);
455 static gboolean
456 sipe_media_parse_sdp_frame(sipe_media_call* call, gchar *frame) {
457 gchar **lines = g_strsplit(frame, "\r\n", 0);
458 GSList *sdp_attrs = NULL;
459 gchar *remote_ip = NULL;
460 guint16 remote_port = 0;
461 gchar **ptr;
462 gboolean no_error = TRUE;
464 for (ptr = lines; *ptr != NULL; ++ptr) {
465 if (g_str_has_prefix(*ptr, "a=")) {
466 gchar **parts = g_strsplit(*ptr + 2, ":", 2);
467 if(!parts[0]) {
468 g_strfreev(parts);
469 sipe_utils_nameval_free(sdp_attrs);
470 sdp_attrs = NULL;
471 no_error = FALSE;
472 break;
474 sdp_attrs = sipe_utils_nameval_add(sdp_attrs, parts[0], parts[1]);
475 g_strfreev(parts);
477 } else if (g_str_has_prefix(*ptr, "o=")) {
478 gchar **parts = g_strsplit(*ptr + 2, " ", 6);
479 remote_ip = g_strdup(parts[5]);
480 g_strfreev(parts);
481 } else if (g_str_has_prefix(*ptr, "m=")) {
482 gchar **parts = g_strsplit(*ptr + 2, " ", 3);
483 remote_port = atoi(parts[1]);
484 g_strfreev(parts);
488 g_strfreev(lines);
490 if (no_error) {
491 sipe_utils_nameval_free(call->sdp_attrs);
492 call->sdp_attrs = sdp_attrs;
493 call->remote_ip = remote_ip;
494 call->remote_port = remote_port;
497 return no_error;
500 static struct sip_dialog *
501 sipe_media_dialog_init(struct sip_session* session, struct sipmsg *msg)
503 gchar *newTag = gentag();
504 const gchar *oldHeader;
505 gchar *newHeader;
506 struct sip_dialog *dialog;
508 oldHeader = sipmsg_find_header(msg, "To");
509 newHeader = g_strdup_printf("%s;tag=%s", oldHeader, newTag);
510 sipmsg_remove_header_now(msg, "To");
511 sipmsg_add_header_now(msg, "To", newHeader);
512 g_free(newHeader);
514 dialog = sipe_dialog_add(session);
515 dialog->callid = g_strdup(session->callid);
516 dialog->with = parse_from(sipmsg_find_header(msg, "From"));
517 sipe_dialog_parse(dialog, msg, FALSE);
519 return dialog;
522 static void candidates_prepared_cb(sipe_media_call *call)
524 if (!call->legacy_mode) {
525 PurpleAccount* account = purple_media_get_account(call->media);
527 if (!call->sdp_response)
528 call->sdp_response = sipe_media_create_sdp(call, FALSE);
530 send_sip_response(account->gc, call->invitation, 183, "Session Progress", call->sdp_response);
534 static void call_accept_cb(sipe_media_call *call, SIPE_UNUSED_PARAMETER gboolean local)
536 PurpleAccount* account = call->sip->account;
538 if (!call->sdp_response)
539 call->sdp_response = sipe_media_create_sdp(call, FALSE);
541 send_sip_response(account->gc, call->invitation, 200, "OK", call->sdp_response);
542 call->state = SIPE_CALL_RUNNING;
545 static void call_reject_cb(sipe_media_call *call, gboolean local)
547 if (local) {
548 PurpleAccount *account = call->sip->account;
549 send_sip_response(account->gc, call->invitation, 603, "Decline", NULL);
550 call->sip->media_call = NULL;
551 sipe_media_call_free(call);
555 static void call_hold_cb(sipe_media_call *call, gboolean local)
557 if (call->state == SIPE_CALL_HELD)
558 return;
560 call->state = SIPE_CALL_HELD;
561 notify_state_change(call->sip, local);
562 sipe_backend_media_hold(call->media, TRUE);
565 static void call_unhold_cb(sipe_media_call *call, gboolean local)
567 if (call->state == SIPE_CALL_RUNNING)
568 return;
570 call->state = SIPE_CALL_RUNNING;
571 notify_state_change(call->sip, local);
572 sipe_backend_media_unhold(call->media, TRUE);
575 static void call_hangup_cb(sipe_media_call *call, gboolean local)
577 call->state = SIPE_CALL_FINISHED;
578 if (local)
579 send_sip_request(call->sip->gc, "BYE", call->dialog->with, call->dialog->with,
580 NULL, NULL, call->dialog, NULL);
581 call->sip->media_call = NULL;
582 sipe_media_call_free(call);
585 static sipe_media_call *
586 sipe_media_call_init(struct sipmsg *msg)
588 sipe_media_call *call;
590 call = g_new0(sipe_media_call, 1);
592 if (sipe_media_parse_sdp_frame(call, msg->body) == FALSE) {
593 g_free(call);
594 return NULL;
597 call->invitation = msg;
598 call->legacy_mode = FALSE;
599 call->state = SIPE_CALL_CONNECTING;
600 call->remote_candidates = sipe_media_parse_remote_candidates(call);
602 call->candidates_prepared_cb = candidates_prepared_cb;
603 call->call_accept_cb = call_accept_cb;
604 call->call_reject_cb = call_reject_cb;
605 call->call_hold_cb = call_hold_cb;
606 call->call_unhold_cb = call_unhold_cb;
607 call->call_hangup_cb = call_hangup_cb;
609 return call;
612 void sipe_media_hold(struct sipe_account_data *sip) {
613 if (sip->media_call)
614 sipe_backend_media_hold(sip->media_call->media, FALSE);
617 void sipe_media_unhold(struct sipe_account_data *sip) {
618 if (sip->media_call)
619 sipe_backend_media_unhold(sip->media_call->media, FALSE);
622 void sipe_media_incoming_invite(struct sipe_account_data *sip, struct sipmsg *msg)
624 const gchar *callid = sipmsg_find_header(msg, "Call-ID");
626 sipe_media *media;
627 sipe_media_call *call;
628 struct sip_session *session;
629 struct sip_dialog *dialog;
631 if (sip->media_call) {
632 if (sipe_strequal(sip->media_call->dialog->callid, callid)) {
633 gchar *rsp;
635 call = sip->media_call;
637 sipmsg_free(call->invitation);
638 msg->dont_free = TRUE;
639 call->invitation = msg;
641 sipmsg_add_header(msg, "Supported", "Replaces");
643 sipe_utils_nameval_free(call->sdp_attrs);
644 call->sdp_attrs = NULL;
645 if (!sipe_media_parse_sdp_frame(call, msg->body)) {
646 // TODO: handle error
649 if (call->legacy_mode && call->state == SIPE_CALL_RUNNING) {
650 sipe_media_hold(sip);
651 return;
654 if (sipe_utils_nameval_find(call->sdp_attrs, "inactive")) {
655 sipe_media_hold(sip);
656 return;
659 if (call->state == SIPE_CALL_HELD) {
660 sipe_media_unhold(sip);
661 return;
664 call->remote_codecs = sipe_media_parse_remote_codecs(call);
665 call->remote_codecs = sipe_media_prune_remote_codecs(call, call->remote_codecs);
666 if (!call->remote_codecs) {
667 // TODO: error no remote codecs
669 if (sipe_backend_set_remote_codecs(call, call->dialog->with) == FALSE)
670 printf("ERROR SET REMOTE CODECS"); // TODO
672 rsp = sipe_media_create_sdp(sip->media_call, TRUE);
673 send_sip_response(sip->gc, msg, 200, "OK", rsp);
674 g_free(rsp);
675 } else {
676 // TODO: send Busy Here
677 printf("MEDIA SESSION ALREADY IN PROGRESS");
679 return;
682 call = sipe_media_call_init(msg);
684 session = sipe_session_find_or_add_chat_by_callid(sip, callid);
685 dialog = sipe_media_dialog_init(session, msg);
687 media = sipe_backend_media_new(call, sip->account, dialog->with, FALSE);
689 call->sip = sip;
690 call->session = session;
691 call->dialog = dialog;
692 call->media = media;
694 sipe_backend_media_add_stream(media, dialog->with, SIPE_MEDIA_AUDIO, !call->legacy_mode, FALSE);
696 sipe_backend_media_add_remote_candidates(media, dialog->with, call->remote_candidates);
698 call->remote_codecs = sipe_media_parse_remote_codecs(call);
699 call->remote_codecs = sipe_media_prune_remote_codecs(call, call->remote_codecs);
700 if (!call->remote_candidates || !call->remote_codecs) {
701 sipe_media_call_free(call);
702 sip->media_call = NULL;
703 printf("ERROR NO CANDIDATES OR CODECS");
704 return;
706 if (sipe_backend_set_remote_codecs(call, dialog->with) == FALSE)
707 printf("ERROR SET REMOTE CODECS"); // TODO
709 sip->media_call = call;
711 // TODO: copy message instead of this don't free thing
712 msg->dont_free = TRUE;
713 send_sip_response(sip->gc, msg, 180, "Ringing", NULL);
716 void sipe_media_hangup(struct sipe_account_data *sip)
718 if (sip->media_call)
719 sipe_backend_media_hangup(sip->media_call->media, FALSE);
723 Local Variables:
724 mode: c
725 c-file-style: "bsd"
726 indent-tabs-mode: t
727 tab-width: 8
728 End: