1 /**
2  * @file rtp.c
3  *
4  * purple
5  *
6  * Purple is the legal property of its developers, whose names are too numerous
7  * to list here.  Please refer to the COPYRIGHT file distributed with this
8  * source distribution.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
23  */
24 
25 #include "config.h"
26 
27 #ifdef USE_VV
28 
29 #include "jabber.h"
30 #include "jingle.h"
31 #include "media.h"
32 #include "mediamanager.h"
33 #include "iceudp.h"
34 #include "rawudp.h"
35 #include "rtp.h"
36 #include "session.h"
37 #include "debug.h"
38 
39 #include <string.h>
40 
41 struct _JingleRtpPrivate
42 {
43 	gchar *media_type;
44 	gchar *ssrc;
45 };
46 
47 #define JINGLE_RTP_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), JINGLE_TYPE_RTP, JingleRtpPrivate))
48 
49 static void jingle_rtp_class_init (JingleRtpClass *klass);
50 static void jingle_rtp_init (JingleRtp *rtp);
51 static void jingle_rtp_finalize (GObject *object);
52 static void jingle_rtp_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
53 static void jingle_rtp_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
54 static JingleContent *jingle_rtp_parse_internal(xmlnode *rtp);
55 static xmlnode *jingle_rtp_to_xml_internal(JingleContent *rtp, xmlnode *content, JingleActionType action);
56 static void jingle_rtp_handle_action_internal(JingleContent *content, xmlnode *jingle, JingleActionType action);
57 
58 static PurpleMedia *jingle_rtp_get_media(JingleSession *session);
59 
60 static JingleContentClass *parent_class = NULL;
61 #if 0
62 enum {
63 	LAST_SIGNAL
64 };
65 static guint jingle_rtp_signals[LAST_SIGNAL] = {0};
66 #endif
67 
68 enum {
69 	PROP_0,
70 	PROP_MEDIA_TYPE,
71 	PROP_SSRC,
72 };
73 
74 GType
jingle_rtp_get_type()75 jingle_rtp_get_type()
76 {
77 	static GType type = 0;
78 
79 	if (type == 0) {
80 		static const GTypeInfo info = {
81 			sizeof(JingleRtpClass),
82 			NULL,
83 			NULL,
84 			(GClassInitFunc) jingle_rtp_class_init,
85 			NULL,
86 			NULL,
87 			sizeof(JingleRtp),
88 			0,
89 			(GInstanceInitFunc) jingle_rtp_init,
90 			NULL
91 		};
92 		type = g_type_register_static(JINGLE_TYPE_CONTENT, "JingleRtp", &info, 0);
93 	}
94 	return type;
95 }
96 
97 static void
jingle_rtp_class_init(JingleRtpClass * klass)98 jingle_rtp_class_init (JingleRtpClass *klass)
99 {
100 	GObjectClass *gobject_class = (GObjectClass*)klass;
101 	parent_class = g_type_class_peek_parent(klass);
102 
103 	gobject_class->finalize = jingle_rtp_finalize;
104 	gobject_class->set_property = jingle_rtp_set_property;
105 	gobject_class->get_property = jingle_rtp_get_property;
106 	klass->parent_class.to_xml = jingle_rtp_to_xml_internal;
107 	klass->parent_class.parse = jingle_rtp_parse_internal;
108 	klass->parent_class.description_type = JINGLE_APP_RTP;
109 	klass->parent_class.handle_action = jingle_rtp_handle_action_internal;
110 
111 	g_object_class_install_property(gobject_class, PROP_MEDIA_TYPE,
112 			g_param_spec_string("media-type",
113 			"Media Type",
114 			"The media type (\"audio\" or \"video\") for this rtp session.",
115 			NULL,
116 			G_PARAM_READWRITE));
117 	g_object_class_install_property(gobject_class, PROP_SSRC,
118 			g_param_spec_string("ssrc",
119 			"ssrc",
120 			"The ssrc for this rtp session.",
121 			NULL,
122 			G_PARAM_READWRITE));
123 
124 	g_type_class_add_private(klass, sizeof(JingleRtpPrivate));
125 }
126 
127 static void
jingle_rtp_init(JingleRtp * rtp)128 jingle_rtp_init (JingleRtp *rtp)
129 {
130 	rtp->priv = JINGLE_RTP_GET_PRIVATE(rtp);
131 	memset(rtp->priv, 0, sizeof(*rtp->priv));
132 }
133 
134 static void
jingle_rtp_finalize(GObject * rtp)135 jingle_rtp_finalize (GObject *rtp)
136 {
137 	JingleRtpPrivate *priv = JINGLE_RTP_GET_PRIVATE(rtp);
138 	purple_debug_info("jingle-rtp","jingle_rtp_finalize\n");
139 
140 	g_free(priv->media_type);
141 	g_free(priv->ssrc);
142 
143 	G_OBJECT_CLASS(parent_class)->finalize(rtp);
144 }
145 
146 static void
jingle_rtp_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)147 jingle_rtp_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
148 {
149 	JingleRtp *rtp;
150 	g_return_if_fail(JINGLE_IS_RTP(object));
151 
152 	rtp = JINGLE_RTP(object);
153 
154 	switch (prop_id) {
155 		case PROP_MEDIA_TYPE:
156 			g_free(rtp->priv->media_type);
157 			rtp->priv->media_type = g_value_dup_string(value);
158 			break;
159 		case PROP_SSRC:
160 			g_free(rtp->priv->ssrc);
161 			rtp->priv->ssrc = g_value_dup_string(value);
162 			break;
163 		default:
164 			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
165 			break;
166 	}
167 }
168 
169 static void
jingle_rtp_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)170 jingle_rtp_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
171 {
172 	JingleRtp *rtp;
173 	g_return_if_fail(JINGLE_IS_RTP(object));
174 
175 	rtp = JINGLE_RTP(object);
176 
177 	switch (prop_id) {
178 		case PROP_MEDIA_TYPE:
179 			g_value_set_string(value, rtp->priv->media_type);
180 			break;
181 		case PROP_SSRC:
182 			g_value_set_string(value, rtp->priv->ssrc);
183 			break;
184 		default:
185 			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
186 			break;
187 	}
188 }
189 
190 gchar *
jingle_rtp_get_media_type(JingleContent * content)191 jingle_rtp_get_media_type(JingleContent *content)
192 {
193 	gchar *media_type;
194 	g_object_get(content, "media-type", &media_type, NULL);
195 	return media_type;
196 }
197 
198 gchar *
jingle_rtp_get_ssrc(JingleContent * content)199 jingle_rtp_get_ssrc(JingleContent *content)
200 {
201 	gchar *ssrc;
202 	g_object_get(content, "ssrc", &ssrc, NULL);
203 	return ssrc;
204 }
205 
206 static PurpleMedia *
jingle_rtp_get_media(JingleSession * session)207 jingle_rtp_get_media(JingleSession *session)
208 {
209 	JabberStream *js = jingle_session_get_js(session);
210 	PurpleMedia *media = NULL;
211 	GList *iter = purple_media_manager_get_media_by_account(
212 			purple_media_manager_get(),
213 			purple_connection_get_account(js->gc));
214 
215 	for (; iter; iter = g_list_delete_link(iter, iter)) {
216 		JingleSession *media_session =
217 				purple_media_get_prpl_data(iter->data);
218 		if (media_session == session) {
219 			media = iter->data;
220 			break;
221 		}
222 	}
223 	if (iter != NULL)
224 		g_list_free(iter);
225 
226 	return media;
227 }
228 
229 static JingleRawUdpCandidate *
jingle_rtp_candidate_to_rawudp(JingleSession * session,guint generation,PurpleMediaCandidate * candidate)230 jingle_rtp_candidate_to_rawudp(JingleSession *session, guint generation,
231 		PurpleMediaCandidate *candidate)
232 {
233 	gchar *id = jabber_get_next_id(jingle_session_get_js(session));
234 	gchar *ip = purple_media_candidate_get_ip(candidate);
235 	JingleRawUdpCandidate *rawudp_candidate =
236 			jingle_rawudp_candidate_new(id, generation,
237 			purple_media_candidate_get_component_id(candidate),
238 			ip, purple_media_candidate_get_port(candidate));
239 	g_free(ip);
240 	g_free(id);
241 	return rawudp_candidate;
242 }
243 
244 static JingleIceUdpCandidate *
jingle_rtp_candidate_to_iceudp(JingleSession * session,guint generation,PurpleMediaCandidate * candidate)245 jingle_rtp_candidate_to_iceudp(JingleSession *session, guint generation,
246 		PurpleMediaCandidate *candidate)
247 {
248 	gchar *id = jabber_get_next_id(jingle_session_get_js(session));
249 	gchar *ip = purple_media_candidate_get_ip(candidate);
250 	gchar *username = purple_media_candidate_get_username(candidate);
251 	gchar *password = purple_media_candidate_get_password(candidate);
252 	PurpleMediaCandidateType type =
253 			purple_media_candidate_get_candidate_type(candidate);
254 	gchar *foundation = purple_media_candidate_get_foundation(candidate);
255 
256 	JingleIceUdpCandidate *iceudp_candidate = jingle_iceudp_candidate_new(
257 			purple_media_candidate_get_component_id(candidate),
258 			foundation, generation, id, ip, 0,
259 			purple_media_candidate_get_port(candidate),
260 			purple_media_candidate_get_priority(candidate), "udp",
261 			type == PURPLE_MEDIA_CANDIDATE_TYPE_HOST ? "host" :
262 			type == PURPLE_MEDIA_CANDIDATE_TYPE_SRFLX ? "srflx" :
263 			type == PURPLE_MEDIA_CANDIDATE_TYPE_PRFLX ? "prflx" :
264 			type == PURPLE_MEDIA_CANDIDATE_TYPE_RELAY ? "relay" :
265 			"", username, password);
266 	iceudp_candidate->reladdr =
267 			purple_media_candidate_get_base_ip(candidate);
268 	iceudp_candidate->relport =
269 			purple_media_candidate_get_base_port(candidate);
270 	g_free(password);
271 	g_free(username);
272 	g_free(foundation);
273 	g_free(ip);
274 	g_free(id);
275 	return iceudp_candidate;
276 }
277 
278 static JingleTransport *
jingle_rtp_candidates_to_transport(JingleSession * session,GType type,guint generation,GList * candidates)279 jingle_rtp_candidates_to_transport(JingleSession *session, GType type, guint generation, GList *candidates)
280 {
281 	if (type == JINGLE_TYPE_RAWUDP) {
282 		JingleTransport *transport = jingle_transport_create(JINGLE_TRANSPORT_RAWUDP);
283 		JingleRawUdpCandidate *rawudp_candidate;
284 		for (; candidates; candidates = g_list_next(candidates)) {
285 			PurpleMediaCandidate *candidate = candidates->data;
286 			if (purple_media_candidate_get_protocol(candidate) ==
287 					PURPLE_MEDIA_NETWORK_PROTOCOL_UDP) {
288 				rawudp_candidate = jingle_rtp_candidate_to_rawudp(
289 						session, generation, candidate);
290 				jingle_rawudp_add_local_candidate(
291 						JINGLE_RAWUDP(transport),
292 						rawudp_candidate);
293 			}
294 		}
295 		return transport;
296 	} else if (type == JINGLE_TYPE_ICEUDP) {
297 		JingleTransport *transport = jingle_transport_create(JINGLE_TRANSPORT_ICEUDP);
298 		JingleIceUdpCandidate *iceudp_candidate;
299 		for (; candidates; candidates = g_list_next(candidates)) {
300 			PurpleMediaCandidate *candidate = candidates->data;
301 			if (purple_media_candidate_get_protocol(candidate) ==
302 					PURPLE_MEDIA_NETWORK_PROTOCOL_UDP) {
303 				iceudp_candidate = jingle_rtp_candidate_to_iceudp(
304 						session, generation, candidate);
305 				jingle_iceudp_add_local_candidate(
306 						JINGLE_ICEUDP(transport),
307 						iceudp_candidate);
308 			}
309 		}
310 		return transport;
311 	} else {
312 		return NULL;
313 	}
314 }
315 
316 static GList *
jingle_rtp_transport_to_candidates(JingleTransport * transport)317 jingle_rtp_transport_to_candidates(JingleTransport *transport)
318 {
319 	const gchar *type = jingle_transport_get_transport_type(transport);
320 	GList *ret = NULL;
321 	if (purple_strequal(type, JINGLE_TRANSPORT_RAWUDP)) {
322 		GList *candidates = jingle_rawudp_get_remote_candidates(JINGLE_RAWUDP(transport));
323 
324 		for (; candidates; candidates = g_list_delete_link(candidates, candidates)) {
325 			JingleRawUdpCandidate *candidate = candidates->data;
326 			ret = g_list_append(ret, purple_media_candidate_new(
327 					"", candidate->component,
328 					PURPLE_MEDIA_CANDIDATE_TYPE_SRFLX,
329 					PURPLE_MEDIA_NETWORK_PROTOCOL_UDP,
330 					candidate->ip, candidate->port));
331 		}
332 
333 		return ret;
334 	} else if (purple_strequal(type, JINGLE_TRANSPORT_ICEUDP)) {
335 		GList *candidates = jingle_iceudp_get_remote_candidates(JINGLE_ICEUDP(transport));
336 
337 		for (; candidates; candidates = g_list_delete_link(candidates, candidates)) {
338 			JingleIceUdpCandidate *candidate = candidates->data;
339 			PurpleMediaCandidate *new_candidate = purple_media_candidate_new(
340 					candidate->foundation, candidate->component,
341 					purple_strequal(candidate->type, "host") ?
342 					PURPLE_MEDIA_CANDIDATE_TYPE_HOST :
343 					purple_strequal(candidate->type, "srflx") ?
344 					PURPLE_MEDIA_CANDIDATE_TYPE_SRFLX :
345 					purple_strequal(candidate->type, "prflx") ?
346 					PURPLE_MEDIA_CANDIDATE_TYPE_PRFLX :
347 					purple_strequal(candidate->type, "relay") ?
348 					PURPLE_MEDIA_CANDIDATE_TYPE_RELAY : 0,
349 					PURPLE_MEDIA_NETWORK_PROTOCOL_UDP,
350 					candidate->ip, candidate->port);
351 			g_object_set(new_candidate,
352 					"base-ip", candidate->reladdr,
353 					"base-port", candidate->relport,
354 					"username", candidate->username,
355 					"password", candidate->password,
356 					"priority", candidate->priority, NULL);
357 			ret = g_list_append(ret, new_candidate);
358 		}
359 
360 		return ret;
361 	} else {
362 		return NULL;
363 	}
364 }
365 
366 static void jingle_rtp_ready(JingleSession *session);
367 
368 static void
jingle_rtp_candidates_prepared_cb(PurpleMedia * media,gchar * sid,gchar * name,JingleSession * session)369 jingle_rtp_candidates_prepared_cb(PurpleMedia *media,
370 		gchar *sid, gchar *name, JingleSession *session)
371 {
372 	JingleContent *content = jingle_session_find_content(
373 			session, sid, NULL);
374 	JingleTransport *oldtransport, *transport;
375 	GList *candidates;
376 
377 	purple_debug_info("jingle-rtp", "jingle_rtp_candidates_prepared_cb\n");
378 
379 	if (content == NULL) {
380 		purple_debug_error("jingle-rtp",
381 				"jingle_rtp_candidates_prepared_cb: "
382 				"Can't find session %s\n", sid);
383 		return;
384 	}
385 
386 	oldtransport = jingle_content_get_transport(content);
387 	candidates = purple_media_get_local_candidates(media, sid, name);
388 	transport = JINGLE_TRANSPORT(jingle_rtp_candidates_to_transport(
389 			session, JINGLE_IS_RAWUDP(oldtransport) ?
390 				JINGLE_TYPE_RAWUDP : JINGLE_TYPE_ICEUDP,
391 			0, candidates));
392 
393 	purple_media_candidate_list_free(candidates);
394 	g_object_unref(oldtransport);
395 
396 	jingle_content_set_pending_transport(content, transport);
397 	jingle_content_accept_transport(content);
398 
399 	jingle_rtp_ready(session);
400 }
401 
402 static void
jingle_rtp_codecs_changed_cb(PurpleMedia * media,gchar * sid,JingleSession * session)403 jingle_rtp_codecs_changed_cb(PurpleMedia *media, gchar *sid,
404 		JingleSession *session)
405 {
406 	purple_debug_info("jingle-rtp", "jingle_rtp_codecs_changed_cb: "
407 			"session_id: %s jingle_session: %p\n", sid, session);
408 	jingle_rtp_ready(session);
409 }
410 
411 static void
jingle_rtp_new_candidate_cb(PurpleMedia * media,gchar * sid,gchar * name,PurpleMediaCandidate * candidate,JingleSession * session)412 jingle_rtp_new_candidate_cb(PurpleMedia *media, gchar *sid, gchar *name, PurpleMediaCandidate *candidate, JingleSession *session)
413 {
414 	JingleContent *content = jingle_session_find_content(
415 			session, sid, NULL);
416 	JingleTransport *transport;
417 
418 	purple_debug_info("jingle-rtp", "jingle_rtp_new_candidate_cb\n");
419 
420 	if (content == NULL) {
421 		purple_debug_error("jingle-rtp",
422 				"jingle_rtp_new_candidate_cb: "
423 				"Can't find session %s\n", sid);
424 		return;
425 	}
426 
427 	transport = jingle_content_get_transport(content);
428 
429 	if (JINGLE_IS_ICEUDP(transport))
430 		jingle_iceudp_add_local_candidate(JINGLE_ICEUDP(transport),
431 				jingle_rtp_candidate_to_iceudp(
432 				session, 1, candidate));
433 	else if (JINGLE_IS_RAWUDP(transport))
434 		jingle_rawudp_add_local_candidate(JINGLE_RAWUDP(transport),
435 				jingle_rtp_candidate_to_rawudp(
436 				session, 1, candidate));
437 
438 	g_object_unref(transport);
439 
440 	jabber_iq_send(jingle_session_to_packet(session,
441 			JINGLE_TRANSPORT_INFO));
442 }
443 
444 static void
jingle_rtp_initiate_ack_cb(JabberStream * js,const char * from,JabberIqType type,const char * id,xmlnode * packet,gpointer data)445 jingle_rtp_initiate_ack_cb(JabberStream *js, const char *from,
446                            JabberIqType type, const char *id,
447                            xmlnode *packet, gpointer data)
448 {
449 	JingleSession *session = data;
450 
451 	if (type == JABBER_IQ_ERROR || xmlnode_get_child(packet, "error")) {
452 		purple_media_end(jingle_rtp_get_media(session), NULL, NULL);
453 		g_object_unref(session);
454 		return;
455 	}
456 }
457 
458 static void
jingle_rtp_state_changed_cb(PurpleMedia * media,PurpleMediaState state,gchar * sid,gchar * name,JingleSession * session)459 jingle_rtp_state_changed_cb(PurpleMedia *media, PurpleMediaState state,
460 		gchar *sid, gchar *name, JingleSession *session)
461 {
462 	purple_debug_info("jingle-rtp", "state-changed: state %d "
463 			"id: %s name: %s\n", state, sid ? sid : "(null)",
464 			name ? name : "(null)");
465 }
466 
467 static void
jingle_rtp_stream_info_cb(PurpleMedia * media,PurpleMediaInfoType type,gchar * sid,gchar * name,gboolean local,JingleSession * session)468 jingle_rtp_stream_info_cb(PurpleMedia *media, PurpleMediaInfoType type,
469 		gchar *sid, gchar *name, gboolean local,
470 		JingleSession *session)
471 {
472 	purple_debug_info("jingle-rtp", "stream-info: type %d "
473 			"id: %s name: %s\n", type, sid ? sid : "(null)",
474 			name ? name : "(null)");
475 
476 	g_return_if_fail(JINGLE_IS_SESSION(session));
477 
478 	if (type == PURPLE_MEDIA_INFO_HANGUP ||
479 			type == PURPLE_MEDIA_INFO_REJECT) {
480 		jabber_iq_send(jingle_session_terminate_packet(
481 				session, type == PURPLE_MEDIA_INFO_HANGUP ?
482 				"success" : "decline"));
483 
484 		g_signal_handlers_disconnect_by_func(G_OBJECT(media),
485 				G_CALLBACK(jingle_rtp_state_changed_cb),
486 				session);
487 		g_signal_handlers_disconnect_by_func(G_OBJECT(media),
488 				G_CALLBACK(jingle_rtp_stream_info_cb),
489 				session);
490 		g_signal_handlers_disconnect_by_func(G_OBJECT(media),
491 				G_CALLBACK(jingle_rtp_new_candidate_cb),
492 				session);
493 
494 		g_object_unref(session);
495 	/* The same signal is emited *four* times in case of acceptance
496 	 * by purple_media_stream_info() (stream acceptance, session
497 	 * acceptance, participant acceptance, and conference acceptance).
498 	 * We only react to the first one, where sid and name are given
499 	 * non-null values.
500 	 */
501 	} else if (type == PURPLE_MEDIA_INFO_ACCEPT && sid && name &&
502 			jingle_session_is_initiator(session) == FALSE) {
503 
504 		jingle_rtp_ready(session);
505 	}
506 }
507 
508 static void
jingle_rtp_ready(JingleSession * session)509 jingle_rtp_ready(JingleSession *session)
510 {
511 	PurpleMedia *media = jingle_rtp_get_media(session);
512 
513 	if (purple_media_candidates_prepared(media, NULL, NULL) &&
514 			purple_media_codecs_ready(media, NULL) &&
515 			(jingle_session_is_initiator(session) == TRUE ||
516 			purple_media_accepted(media, NULL, NULL))) {
517 		if (jingle_session_is_initiator(session)) {
518 			JabberIq *iq = jingle_session_to_packet(
519 					session, JINGLE_SESSION_INITIATE);
520 			jabber_iq_set_callback(iq,
521 					jingle_rtp_initiate_ack_cb, session);
522 			jabber_iq_send(iq);
523 		} else {
524 			jabber_iq_send(jingle_session_to_packet(session,
525 					JINGLE_SESSION_ACCEPT));
526 		}
527 
528 		g_signal_handlers_disconnect_by_func(G_OBJECT(media),
529 				G_CALLBACK(jingle_rtp_candidates_prepared_cb),
530 				session);
531 		g_signal_handlers_disconnect_by_func(G_OBJECT(media),
532 				G_CALLBACK(jingle_rtp_codecs_changed_cb),
533 				session);
534 		g_signal_connect(G_OBJECT(media), "new-candidate",
535 				G_CALLBACK(jingle_rtp_new_candidate_cb),
536 				session);
537 	}
538 }
539 
540 static PurpleMedia *
jingle_rtp_create_media(JingleContent * content)541 jingle_rtp_create_media(JingleContent *content)
542 {
543 	JingleSession *session = jingle_content_get_session(content);
544 	JabberStream *js = jingle_session_get_js(session);
545 	gchar *remote_jid = jingle_session_get_remote_jid(session);
546 
547 	PurpleMedia *media = purple_media_manager_create_media(
548 			purple_media_manager_get(),
549 			purple_connection_get_account(js->gc),
550 			"fsrtpconference", remote_jid,
551 			jingle_session_is_initiator(session));
552 	g_free(remote_jid);
553 
554 	if (!media) {
555 		purple_debug_error("jingle-rtp", "Couldn't create media session\n");
556 		return NULL;
557 	}
558 
559 	purple_media_set_prpl_data(media, session);
560 
561 	/* connect callbacks */
562 	g_signal_connect(G_OBJECT(media), "candidates-prepared",
563 				 G_CALLBACK(jingle_rtp_candidates_prepared_cb), session);
564 	g_signal_connect(G_OBJECT(media), "codecs-changed",
565 				 G_CALLBACK(jingle_rtp_codecs_changed_cb), session);
566 	g_signal_connect(G_OBJECT(media), "state-changed",
567 				 G_CALLBACK(jingle_rtp_state_changed_cb), session);
568 	g_signal_connect_object(G_OBJECT(media), "stream-info",
569 			G_CALLBACK(jingle_rtp_stream_info_cb), session, 0);
570 
571 	g_object_unref(session);
572 	return media;
573 }
574 
575 static gboolean
jingle_rtp_init_media(JingleContent * content)576 jingle_rtp_init_media(JingleContent *content)
577 {
578 	JingleSession *session = jingle_content_get_session(content);
579 	PurpleMedia *media = jingle_rtp_get_media(session);
580 	gchar *creator;
581 	gchar *media_type;
582 	gchar *remote_jid;
583 	gchar *senders;
584 	gchar *name;
585 	const gchar *transmitter;
586 	gboolean is_audio;
587 	gboolean is_creator;
588 	PurpleMediaSessionType type;
589 	JingleTransport *transport;
590 	GParameter *params = NULL;
591 	guint num_params;
592 
593 	/* maybe this create ought to just be in initiate and handle initiate */
594 	if (media == NULL) {
595 		media = jingle_rtp_create_media(content);
596 
597 		if (media == NULL)
598 			return FALSE;
599 	}
600 
601 	name = jingle_content_get_name(content);
602 	media_type = jingle_rtp_get_media_type(content);
603 	remote_jid = jingle_session_get_remote_jid(session);
604 	senders = jingle_content_get_senders(content);
605 	transport = jingle_content_get_transport(content);
606 
607 	if (media_type == NULL) {
608 		g_free(name);
609 		g_free(remote_jid);
610 		g_free(senders);
611 		g_free(params);
612 		g_object_unref(transport);
613 		g_object_unref(session);
614 		return FALSE;
615 	}
616 
617 	if (JINGLE_IS_RAWUDP(transport))
618 		transmitter = "rawudp";
619 	else if (JINGLE_IS_ICEUDP(transport))
620 		transmitter = "nice";
621 	else
622 		transmitter = "notransmitter";
623 	g_object_unref(transport);
624 
625 	is_audio = purple_strequal(media_type, "audio");
626 
627 	if (purple_strequal(senders, "both"))
628 		type = is_audio ? PURPLE_MEDIA_AUDIO
629 				: PURPLE_MEDIA_VIDEO;
630 	else if (purple_strequal(senders, "initiator") ==
631 			jingle_session_is_initiator(session))
632 		type = is_audio ? PURPLE_MEDIA_SEND_AUDIO
633 				: PURPLE_MEDIA_SEND_VIDEO;
634 	else
635 		type = is_audio ? PURPLE_MEDIA_RECV_AUDIO
636 				: PURPLE_MEDIA_RECV_VIDEO;
637 
638 	params =
639 		jingle_get_params(jingle_session_get_js(session), NULL, 0, 0, 0,
640 			NULL, NULL, &num_params);
641 
642 	creator = jingle_content_get_creator(content);
643 	if (creator == NULL) {
644 		g_free(name);
645 		g_free(media_type);
646 		g_free(remote_jid);
647 		g_free(senders);
648 		g_free(params);
649 		g_object_unref(session);
650 		return FALSE;
651 	}
652 
653 	if (purple_strequal(creator, "initiator"))
654 		is_creator = jingle_session_is_initiator(session);
655 	else
656 		is_creator = !jingle_session_is_initiator(session);
657 	g_free(creator);
658 
659 	if(!purple_media_add_stream(media, name, remote_jid,
660 			type, is_creator, transmitter, num_params, params)) {
661 		purple_media_end(media, NULL, NULL);
662 		/* TODO: How much clean-up is necessary here? (does calling
663 		         purple_media_end lead to cleaning up Jingle structs?) */
664 		return FALSE;
665 	}
666 
667 	g_free(name);
668 	g_free(media_type);
669 	g_free(remote_jid);
670 	g_free(senders);
671 	g_free(params);
672 	g_object_unref(session);
673 
674 	return TRUE;
675 }
676 
677 static GList *
jingle_rtp_parse_codecs(xmlnode * description)678 jingle_rtp_parse_codecs(xmlnode *description)
679 {
680 	GList *codecs = NULL;
681 	xmlnode *codec_element = NULL;
682 	const char *encoding_name,*id, *clock_rate;
683 	PurpleMediaCodec *codec;
684 	const gchar *media = xmlnode_get_attrib(description, "media");
685 	PurpleMediaSessionType type;
686 
687 	if (media == NULL) {
688 		purple_debug_warning("jingle-rtp", "missing media type\n");
689 		return NULL;
690 	}
691 
692 	if (purple_strequal(media, "video")) {
693 		type = PURPLE_MEDIA_VIDEO;
694 	} else if (purple_strequal(media, "audio")) {
695 		type = PURPLE_MEDIA_AUDIO;
696 	} else {
697 		purple_debug_warning("jingle-rtp", "unknown media type: %s\n",
698 				media);
699 		return NULL;
700 	}
701 
702 	for (codec_element = xmlnode_get_child(description, "payload-type") ;
703 		 codec_element ;
704 		 codec_element = xmlnode_get_next_twin(codec_element)) {
705 		xmlnode *param;
706 		gchar *codec_str;
707 		encoding_name = xmlnode_get_attrib(codec_element, "name");
708 
709 		id = xmlnode_get_attrib(codec_element, "id");
710 		clock_rate = xmlnode_get_attrib(codec_element, "clockrate");
711 
712 		codec = purple_media_codec_new(atoi(id), encoding_name,
713 				     type,
714 				     clock_rate ? atoi(clock_rate) : 0);
715 
716 		for (param = xmlnode_get_child(codec_element, "parameter");
717 				param; param = xmlnode_get_next_twin(param)) {
718 			purple_media_codec_add_optional_parameter(codec,
719 					xmlnode_get_attrib(param, "name"),
720 					xmlnode_get_attrib(param, "value"));
721 		}
722 
723 		codec_str = purple_media_codec_to_string(codec);
724 		purple_debug_info("jingle-rtp", "received codec: %s\n", codec_str);
725 		g_free(codec_str);
726 
727 		codecs = g_list_append(codecs, codec);
728 	}
729 	return codecs;
730 }
731 
732 static JingleContent *
jingle_rtp_parse_internal(xmlnode * rtp)733 jingle_rtp_parse_internal(xmlnode *rtp)
734 {
735 	JingleContent *content = parent_class->parse(rtp);
736 	xmlnode *description = xmlnode_get_child(rtp, "description");
737 	const gchar *media_type = xmlnode_get_attrib(description, "media");
738 	const gchar *ssrc = xmlnode_get_attrib(description, "ssrc");
739 	purple_debug_info("jingle-rtp", "rtp parse\n");
740 	g_object_set(content, "media-type", media_type, NULL);
741 	if (ssrc != NULL)
742 		g_object_set(content, "ssrc", ssrc, NULL);
743 	return content;
744 }
745 
746 static void
jingle_rtp_add_payloads(xmlnode * description,GList * codecs)747 jingle_rtp_add_payloads(xmlnode *description, GList *codecs)
748 {
749 	for (; codecs ; codecs = codecs->next) {
750 		PurpleMediaCodec *codec = (PurpleMediaCodec*)codecs->data;
751 		GList *iter = purple_media_codec_get_optional_parameters(codec);
752 		gchar *id, *name, *clockrate, *channels;
753 		gchar *codec_str;
754 		xmlnode *payload = xmlnode_new_child(description, "payload-type");
755 
756 		id = g_strdup_printf("%d",
757 				purple_media_codec_get_id(codec));
758 		name = purple_media_codec_get_encoding_name(codec);
759 		clockrate = g_strdup_printf("%d",
760 				purple_media_codec_get_clock_rate(codec));
761 		channels = g_strdup_printf("%d",
762 				purple_media_codec_get_channels(codec));
763 
764 		xmlnode_set_attrib(payload, "name", name);
765 		xmlnode_set_attrib(payload, "id", id);
766 		xmlnode_set_attrib(payload, "clockrate", clockrate);
767 		xmlnode_set_attrib(payload, "channels", channels);
768 
769 		g_free(channels);
770 		g_free(clockrate);
771 		g_free(name);
772 		g_free(id);
773 
774 		for (; iter; iter = g_list_next(iter)) {
775 			PurpleKeyValuePair *mparam = iter->data;
776 			xmlnode *param = xmlnode_new_child(payload, "parameter");
777 			xmlnode_set_attrib(param, "name", mparam->key);
778 			xmlnode_set_attrib(param, "value", mparam->value);
779 		}
780 
781 		codec_str = purple_media_codec_to_string(codec);
782 		purple_debug_info("jingle", "adding codec: %s\n", codec_str);
783 		g_free(codec_str);
784 	}
785 }
786 
787 static xmlnode *
jingle_rtp_to_xml_internal(JingleContent * rtp,xmlnode * content,JingleActionType action)788 jingle_rtp_to_xml_internal(JingleContent *rtp, xmlnode *content, JingleActionType action)
789 {
790 	xmlnode *node = parent_class->to_xml(rtp, content, action);
791 	xmlnode *description = xmlnode_get_child(node, "description");
792 	if (description != NULL) {
793 		JingleSession *session = jingle_content_get_session(rtp);
794 		PurpleMedia *media = jingle_rtp_get_media(session);
795 		gchar *media_type = jingle_rtp_get_media_type(rtp);
796 		gchar *ssrc = jingle_rtp_get_ssrc(rtp);
797 		gchar *name = jingle_content_get_name(rtp);
798 		GList *codecs = purple_media_get_codecs(media, name);
799 
800 		xmlnode_set_attrib(description, "media", media_type);
801 
802 		if (ssrc != NULL)
803 			xmlnode_set_attrib(description, "ssrc", ssrc);
804 
805 		g_free(media_type);
806 		g_free(name);
807 		g_object_unref(session);
808 
809 		jingle_rtp_add_payloads(description, codecs);
810 		purple_media_codec_list_free(codecs);
811 	}
812 	return node;
813 }
814 
815 static void
jingle_rtp_handle_action_internal(JingleContent * content,xmlnode * xmlcontent,JingleActionType action)816 jingle_rtp_handle_action_internal(JingleContent *content, xmlnode *xmlcontent, JingleActionType action)
817 {
818 	switch (action) {
819 		case JINGLE_SESSION_ACCEPT:
820 		case JINGLE_SESSION_INITIATE: {
821 			JingleSession *session;
822 			JingleTransport *transport;
823 			xmlnode *description;
824 			GList *candidates;
825 			GList *codecs;
826 			gchar *name;
827 			gchar *remote_jid;
828 			PurpleMedia *media;
829 
830 			session = jingle_content_get_session(content);
831 
832 			if (action == JINGLE_SESSION_INITIATE &&
833 					!jingle_rtp_init_media(content)) {
834 				/* XXX: send error */
835 				jabber_iq_send(jingle_session_terminate_packet(
836 						session, "general-error"));
837 				g_object_unref(session);
838 				break;
839 			}
840 
841 			transport = jingle_transport_parse(
842 					xmlnode_get_child(xmlcontent, "transport"));
843 			description = xmlnode_get_child(xmlcontent, "description");
844 			candidates = jingle_rtp_transport_to_candidates(transport);
845 			codecs = jingle_rtp_parse_codecs(description);
846 			name = jingle_content_get_name(content);
847 			remote_jid = jingle_session_get_remote_jid(session);
848 
849 			media = jingle_rtp_get_media(session);
850 			purple_media_set_remote_codecs(media,
851 					name, remote_jid, codecs);
852 			purple_media_add_remote_candidates(media,
853 					name, remote_jid, candidates);
854 
855 			if (action == JINGLE_SESSION_ACCEPT)
856 				purple_media_stream_info(media,
857 						PURPLE_MEDIA_INFO_ACCEPT,
858 						name, remote_jid, FALSE);
859 
860 			g_free(remote_jid);
861 			g_free(name);
862 			g_object_unref(session);
863 			g_object_unref(transport);
864 			purple_media_codec_list_free(codecs);
865 			purple_media_candidate_list_free(candidates);
866 			break;
867 		}
868 		case JINGLE_SESSION_TERMINATE: {
869 			JingleSession *session = jingle_content_get_session(content);
870 			PurpleMedia *media = jingle_rtp_get_media(session);
871 
872 			if (media != NULL) {
873 				purple_media_end(media, NULL, NULL);
874 			}
875 
876 			g_object_unref(session);
877 			break;
878 		}
879 		case JINGLE_TRANSPORT_INFO: {
880 			JingleSession *session = jingle_content_get_session(content);
881 			JingleTransport *transport = jingle_transport_parse(
882 					xmlnode_get_child(xmlcontent, "transport"));
883 			GList *candidates = jingle_rtp_transport_to_candidates(transport);
884 			gchar *name = jingle_content_get_name(content);
885 			gchar *remote_jid =
886 					jingle_session_get_remote_jid(session);
887 
888 			purple_media_add_remote_candidates(
889 					jingle_rtp_get_media(session),
890 					name, remote_jid, candidates);
891 
892 			g_free(remote_jid);
893 			g_free(name);
894 			g_object_unref(session);
895 			g_object_unref(transport);
896 			purple_media_candidate_list_free(candidates);
897 			break;
898 		}
899 		case JINGLE_DESCRIPTION_INFO: {
900 			JingleSession *session =
901 					jingle_content_get_session(content);
902 			xmlnode *description = xmlnode_get_child(
903 					xmlcontent, "description");
904 			GList *codecs, *iter, *iter2, *remote_codecs =
905 					jingle_rtp_parse_codecs(description);
906 			gchar *name = jingle_content_get_name(content);
907 			gchar *remote_jid =
908 					jingle_session_get_remote_jid(session);
909 			PurpleMedia *media;
910 
911 			media = jingle_rtp_get_media(session);
912 
913 			/*
914 			 * This may have problems if description-info is
915 			 * received without the optional parameters for a
916 			 * codec with configuration info (such as THEORA
917 			 * or H264). The local configuration info may be
918 			 * set for the remote codec.
919 			 *
920 			 * As of 2.6.3 there's no API to support getting
921 			 * the remote codecs specifically, just the
922 			 * intersection. Another option may be to cache
923 			 * the remote codecs received in initiate/accept.
924 			 */
925 			codecs = purple_media_get_codecs(media, name);
926 
927 			for (iter = codecs; iter; iter = g_list_next(iter)) {
928 				guint id;
929 
930 				id = purple_media_codec_get_id(iter->data);
931 				iter2 = remote_codecs;
932 
933 				for (; iter2; iter2 = g_list_next(iter2)) {
934 					if (purple_media_codec_get_id(
935 							iter2->data) != id)
936 						continue;
937 
938 					g_object_unref(iter->data);
939 					iter->data = iter2->data;
940 					remote_codecs = g_list_delete_link(
941 							remote_codecs, iter2);
942 					break;
943 				}
944 			}
945 
946 			codecs = g_list_concat(codecs, remote_codecs);
947 
948 			purple_media_set_remote_codecs(media,
949 					name, remote_jid, codecs);
950 
951 			purple_media_codec_list_free (codecs);
952 			g_free(remote_jid);
953 			g_free(name);
954 			g_object_unref(session);
955 			break;
956 		}
957 		default:
958 			break;
959 	}
960 }
961 
962 gboolean
jingle_rtp_initiate_media(JabberStream * js,const gchar * who,PurpleMediaSessionType type)963 jingle_rtp_initiate_media(JabberStream *js, const gchar *who,
964 		      PurpleMediaSessionType type)
965 {
966 	/* create content negotiation */
967 	JingleSession *session;
968 	JingleContent *content;
969 	JingleTransport *transport;
970 	JabberBuddy *jb;
971 	JabberBuddyResource *jbr;
972 	gboolean ret = FALSE;
973 	const gchar *transport_type;
974 
975 	gchar *resource = NULL, *me = NULL, *sid = NULL;
976 
977 	/* construct JID to send to */
978 	jb = jabber_buddy_find(js, who, FALSE);
979 	if (!jb) {
980 		purple_debug_error("jingle-rtp", "Could not find Jabber buddy\n");
981 		goto out;
982 	}
983 
984 	resource = jabber_get_resource(who);
985 	jbr = jabber_buddy_find_resource(jb, resource);
986 
987 	if (!jbr) {
988 		purple_debug_error("jingle-rtp", "Could not find buddy's resource - %s\n", resource);
989 		goto out;
990 	}
991 
992 	if (jabber_resource_has_capability(jbr, JINGLE_TRANSPORT_ICEUDP)) {
993 		transport_type = JINGLE_TRANSPORT_ICEUDP;
994 	} else if (jabber_resource_has_capability(jbr, JINGLE_TRANSPORT_RAWUDP)) {
995 		transport_type = JINGLE_TRANSPORT_RAWUDP;
996 	} else {
997 		purple_debug_error("jingle-rtp", "Resource doesn't support "
998 				"the same transport types\n");
999 		goto out;
1000 	}
1001 
1002 	/* set ourselves as initiator */
1003 	me = g_strdup_printf("%s@%s/%s", js->user->node, js->user->domain, js->user->resource);
1004 
1005 	sid = jabber_get_next_id(js);
1006 	session = jingle_session_create(js, sid, me, who, TRUE);
1007 
1008 
1009 	if (type & PURPLE_MEDIA_AUDIO) {
1010 		transport = jingle_transport_create(transport_type);
1011 		content = jingle_content_create(JINGLE_APP_RTP, "initiator",
1012 				"session", "audio-session", "both", transport);
1013 		jingle_session_add_content(session, content);
1014 		JINGLE_RTP(content)->priv->media_type = g_strdup("audio");
1015 		jingle_rtp_init_media(content);
1016 	}
1017 	if (type & PURPLE_MEDIA_VIDEO) {
1018 		transport = jingle_transport_create(transport_type);
1019 		content = jingle_content_create(JINGLE_APP_RTP, "initiator",
1020 				"session", "video-session", "both", transport);
1021 		jingle_session_add_content(session, content);
1022 		JINGLE_RTP(content)->priv->media_type = g_strdup("video");
1023 		jingle_rtp_init_media(content);
1024 	}
1025 
1026 	if (jingle_rtp_get_media(session) == NULL) {
1027 		goto out;
1028 	}
1029 
1030 	ret = TRUE;
1031 
1032 out:
1033 	g_free(me);
1034 	g_free(resource);
1035 	g_free(sid);
1036 	return ret;
1037 }
1038 
1039 void
jingle_rtp_terminate_session(JabberStream * js,const gchar * who)1040 jingle_rtp_terminate_session(JabberStream *js, const gchar *who)
1041 {
1042 	JingleSession *session;
1043 /* XXX: This may cause file transfers and xml sessions to stop as well */
1044 	session = jingle_session_find_by_jid(js, who);
1045 
1046 	if (session) {
1047 		PurpleMedia *media = jingle_rtp_get_media(session);
1048 		if (media) {
1049 			purple_debug_info("jingle-rtp", "hanging up media\n");
1050 			purple_media_stream_info(media,
1051 					PURPLE_MEDIA_INFO_HANGUP,
1052 					NULL, NULL, TRUE);
1053 		}
1054 	}
1055 }
1056 
1057 #endif /* USE_VV */
1058 
1059