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
29 #include "sipe-core.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"
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
);
59 sipe_media_call_free(sipe_media_call
*call
)
62 sipe_utils_nameval_free(call
->sdp_attrs
);
64 sipmsg_free(call
->invitation
);
65 sipe_media_codec_list_free(call
->remote_codecs
);
66 sipe_media_candidate_list_free(call
->remote_candidates
);
72 sipe_media_parse_remote_codecs(const sipe_media_call
*call
)
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
);
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
);
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
;
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
;
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
++))) {
141 SipeComponentType component
;
142 SipeNetworkProtocol protocol
;
146 SipeCandidateType type
;
148 tokens
= g_strsplit_set(attr
, " ", 0);
150 foundation
= tokens
[0];
152 switch (atoi(tokens
[1])) {
154 component
= SIPE_COMPONENT_RTP
;
157 component
= SIPE_COMPONENT_RTCP
;
160 component
= SIPE_COMPONENT_NONE
;
163 if (sipe_strequal(tokens
[2], "UDP"))
164 protocol
= SIPE_NETWORK_PROTOCOL_UDP
;
166 // Ignore TCP candidates, at least for now...
171 priority
= atoi(tokens
[3]);
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
;
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
);
195 // No a=candidate in SDP message, revert to OC2005 behaviour
196 candidate
= sipe_backend_candidate_new("foundation",
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",
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
;
215 GList
*it
= candidates
;
217 sipe_backend_candidate_set_username_and_pwd(it
->data
, username
, password
);
226 sipe_media_sdp_codec_ids_format(GList
*codecs
)
228 GString
*result
= g_string_new(NULL
);
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
);
237 codecs
= codecs
->next
;
240 return g_string_free(result
, FALSE
);
244 sipe_media_sdp_codecs_format(GList
*codecs
)
246 GString
*result
= g_string_new(NULL
);
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
);
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
);
266 struct sipnameval
* par
= params
->data
;
267 tmp
= g_strdup_printf(" %s=%s", par
->name
, par
->value
);
268 g_string_append(result
, tmp
);
270 params
= params
->next
;
272 g_string_append(result
, "\r\n");
275 codecs
= codecs
->next
;
278 return g_string_free(result
, FALSE
);
282 sipe_media_sdp_candidates_format(GList
*candidates
, sipe_media_call
* call
, gboolean remote_candidate
)
284 GString
*result
= g_string_new("");
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
);
298 sipe_candidate
*c
= candidates
->data
;
305 port
= sipe_backend_candidate_get_port(c
);
307 switch (sipe_backend_candidate_get_component_type(c
)) {
308 case SIPE_COMPONENT_RTP
:
311 case SIPE_COMPONENT_RTCP
:
316 case SIPE_COMPONENT_NONE
:
320 switch (sipe_backend_candidate_get_protocol(c
)) {
321 case SIPE_NETWORK_PROTOCOL_TCP
:
324 case SIPE_NETWORK_PROTOCOL_UDP
:
329 switch (sipe_backend_candidate_get_type(c
)) {
330 case SIPE_CANDIDATE_TYPE_HOST
:
333 case SIPE_CANDIDATE_TYPE_RELAY
:
336 case SIPE_CANDIDATE_TYPE_SRFLX
:
340 // TODO: error unknown/unsupported type
344 tmp
= g_strdup_printf("a=candidate:%s %u %s %u %s %d typ %s \r\n",
345 sipe_backend_candidate_get_foundation(c
),
348 sipe_backend_candidate_get_priority(c
),
349 sipe_backend_candidate_get_ip(c
),
353 g_string_append(result
, 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
);
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
);
377 return g_string_free(result
, FALSE
);
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(
396 "o=- 0 0 IN IP4 %s\r\n"
401 "m=audio %d RTP/AVP%s\r\n"
405 "a=encryption:rejected\r\n"
406 ,ip
, ip
, local_port
, sdp_codec_ids
, sdp_candidates
, inactive
, sdp_codecs
);
409 g_free(sdp_codec_ids
);
410 g_free(sdp_candidates
);
416 sipe_invite_call(struct sipe_account_data
*sip
)
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"
429 "Supported: Replaces\r\n"
430 "Content-Type: application/sdp\r\n",
432 call
->state
== SIPE_CALL_HELD
? ";+sip.rendering=\"no\"" : "");
435 body
= sipe_media_create_sdp(call
, TRUE
);
437 send_sip_request(sip
->gc
, "INVITE", dialog
->with
, dialog
->with
, hdr
, body
,
445 notify_state_change(struct sipe_account_data
*sip
, gboolean local
) {
447 sipe_invite_call(sip
);
449 gchar
* body
= sipe_media_create_sdp(sip
->media_call
, TRUE
);
450 send_sip_response(sip
->gc
, sip
->media_call
->invitation
, 200, "OK", body
);
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;
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);
469 sipe_utils_nameval_free(sdp_attrs
);
474 sdp_attrs
= sipe_utils_nameval_add(sdp_attrs
, parts
[0], parts
[1]);
477 } else if (g_str_has_prefix(*ptr
, "o=")) {
478 gchar
**parts
= g_strsplit(*ptr
+ 2, " ", 6);
479 remote_ip
= g_strdup(parts
[5]);
481 } else if (g_str_has_prefix(*ptr
, "m=")) {
482 gchar
**parts
= g_strsplit(*ptr
+ 2, " ", 3);
483 remote_port
= atoi(parts
[1]);
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
;
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
;
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
);
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
);
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
)
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
)
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
)
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
;
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
) {
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
;
612 void sipe_media_hold(struct sipe_account_data
*sip
) {
614 sipe_backend_media_hold(sip
->media_call
->media
, FALSE
);
617 void sipe_media_unhold(struct sipe_account_data
*sip
) {
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");
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
)) {
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
);
654 if (sipe_utils_nameval_find(call
->sdp_attrs
, "inactive")) {
655 sipe_media_hold(sip
);
659 if (call
->state
== SIPE_CALL_HELD
) {
660 sipe_media_unhold(sip
);
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
);
676 // TODO: send Busy Here
677 printf("MEDIA SESSION ALREADY IN PROGRESS");
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
);
690 call
->session
= session
;
691 call
->dialog
= dialog
;
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");
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
)
719 sipe_backend_media_hangup(sip
->media_call
->media
, FALSE
);