2 * This file is part of the Nice GLib ICE library.
4 * (C) 2006-2010 Collabora Ltd.
5 * Contact: Youness Alaoui
6 * (C) 2006-2010 Nokia Corporation. All rights reserved.
7 * Contact: Kai Vehmanen
9 * The contents of this file are subject to the Mozilla Public License Version
10 * 1.1 (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
12 * http://www.mozilla.org/MPL/
14 * Software distributed under the License is distributed on an "AS IS" basis,
15 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16 * for the specific language governing rights and limitations under the
19 * The Original Code is the Nice GLib ICE library.
21 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
22 * Corporation. All Rights Reserved.
25 * Dafydd Harries, Collabora Ltd.
26 * Youness Alaoui, Collabora Ltd.
29 * Alternatively, the contents of this file may be used under the terms of the
30 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
31 * case the provisions of LGPL are applicable instead of those above. If you
32 * wish to allow use of your version of this file only under the terms of the
33 * LGPL and not to allow others to use your version of this file under the
34 * MPL, indicate your decision by deleting the provisions above and replace
35 * them with the notice and other provisions required by the LGPL. If you do
36 * not delete the provisions above, a recipient may use your version of this
37 * file under either the MPL or the LGPL.
45 * @short_description: ICE agent API implementation
46 * @see_also: #NiceCandidate
47 * @see_also: #NiceAddress
51 * The #NiceAgent is your main object when using libnice.
52 * It is the agent that will take care of everything relating to ICE.
53 * It will take care of discovering your local candidates and do
54 * connectivity checks to create a stream of data between you and your peer.
57 <title>Simple example on how to use libnice</title>
60 gchar buffer[] = "hello world!";
61 GSList *lcands = NULL;
63 // Create a nice agent
64 NiceAgent *agent = nice_agent_new (NULL, NICE_COMPATIBILITY_RFC5245);
66 // Connect the signals
67 g_signal_connect (G_OBJECT (agent), "candidate-gathering-done",
68 G_CALLBACK (cb_candidate_gathering_done), NULL);
69 g_signal_connect (G_OBJECT (agent), "component-state-changed",
70 G_CALLBACK (cb_component_state_changed), NULL);
71 g_signal_connect (G_OBJECT (agent), "new-selected-pair",
72 G_CALLBACK (cb_new_selected_pair), NULL);
74 // Create a new stream with one component and start gathering candidates
75 stream_id = nice_agent_add_stream (agent, 1);
76 nice_agent_gather_candidates (agent, stream_id);
78 // Attach to the component to receive the data
79 nice_agent_attach_recv (agent, stream_id, 1, NULL,
82 // ... Wait until the signal candidate-gathering-done is fired ...
83 lcands = nice_agent_get_local_candidates(agent, stream_id, 1);
85 // ... Send local candidates to the peer and set the peer's remote candidates
86 nice_agent_set_remote_candidates (agent, stream_id, 1, rcands);
88 // ... Wait until the signal new-selected-pair is fired ...
90 nice_agent_send (agent, stream_id, 1, sizeof(buffer), buffer);
92 // Anything received will be received through the cb_nice_recv callback
95 g_object_unref(agent);
102 #include <glib-object.h>
107 * The #NiceAgent is the main GObject of the libnice library and represents
110 typedef struct _NiceAgent NiceAgent
;
113 #include "candidate.h"
119 #define NICE_TYPE_AGENT nice_agent_get_type()
121 #define NICE_AGENT(obj) \
122 (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
123 NICE_TYPE_AGENT, NiceAgent))
125 #define NICE_AGENT_CLASS(klass) \
126 (G_TYPE_CHECK_CLASS_CAST ((klass), \
127 NICE_TYPE_AGENT, NiceAgentClass))
129 #define NICE_IS_AGENT(obj) \
130 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
133 #define NICE_IS_AGENT_CLASS(klass) \
134 (G_TYPE_CHECK_CLASS_TYPE ((klass), \
137 #define NICE_AGENT_GET_CLASS(obj) \
138 (G_TYPE_INSTANCE_GET_CLASS ((obj), \
139 NICE_TYPE_AGENT, NiceAgentClass))
141 typedef struct _NiceAgentClass NiceAgentClass
;
143 struct _NiceAgentClass
145 GObjectClass parent_class
;
149 GType
nice_agent_get_type (void);
153 * NICE_AGENT_MAX_REMOTE_CANDIDATES:
155 * A hard limit for the number of remote candidates. This
156 * limit is enforced to protect against malevolent remote
159 #define NICE_AGENT_MAX_REMOTE_CANDIDATES 25
162 * NiceComponentState:
163 * @NICE_COMPONENT_STATE_DISCONNECTED: No activity scheduled
164 * @NICE_COMPONENT_STATE_GATHERING: Gathering local candidates
165 * @NICE_COMPONENT_STATE_CONNECTING: Establishing connectivity
166 * @NICE_COMPONENT_STATE_CONNECTED: At least one working candidate pair
167 * @NICE_COMPONENT_STATE_READY: ICE concluded, candidate pair selection
169 * @NICE_COMPONENT_STATE_FAILED: Connectivity checks have been completed,
170 * but connectivity was not established
171 * @NICE_COMPONENT_STATE_LAST: Dummy state
173 * An enum representing the state of a component.
174 * <para> See also: #NiceAgent::component-state-changed </para>
178 NICE_COMPONENT_STATE_DISCONNECTED
,
179 NICE_COMPONENT_STATE_GATHERING
,
180 NICE_COMPONENT_STATE_CONNECTING
,
181 NICE_COMPONENT_STATE_CONNECTED
,
182 NICE_COMPONENT_STATE_READY
,
183 NICE_COMPONENT_STATE_FAILED
,
184 NICE_COMPONENT_STATE_LAST
185 } NiceComponentState
;
190 * @NICE_COMPONENT_TYPE_RTP: RTP Component type
191 * @NICE_COMPONENT_TYPE_RTCP: RTCP Component type
193 * Convenience enum representing the type of a component for use as the
194 * component_id for RTP/RTCP usages.
196 <title>Example of use.</title>
198 nice_agent_send (agent, stream_id, NICE_COMPONENT_TYPE_RTP, len, buf);
204 NICE_COMPONENT_TYPE_RTP
= 1,
205 NICE_COMPONENT_TYPE_RTCP
= 2
211 * @NICE_COMPATIBILITY_RFC5245: Use compatibility with the RFC5245 ICE specs
212 * @NICE_COMPATIBILITY_GOOGLE: Use compatibility for Google Talk specs
213 * @NICE_COMPATIBILITY_MSN: Use compatibility for MSN Messenger specs
214 * @NICE_COMPATIBILITY_WLM2009: Use compatibility with Windows Live Messenger
216 * @NICE_COMPATIBILITY_DRAFT19: Use compatibility for ICE Draft 19 specs
217 * @NICE_COMPATIBILITY_LAST: Dummy last compatibility mode
219 * An enum to specify which compatible specifications the #NiceAgent should use.
220 * Use with nice_agent_new()
222 * <warning>@NICE_COMPATIBILITY_DRAFT19 is deprecated and should not be used
223 * in newly-written code. It is kept for compatibility reasons and
224 * represents the same compatibility as @NICE_COMPATIBILITY_RFC5245 </warning>
228 NICE_COMPATIBILITY_RFC5245
= 0,
229 NICE_COMPATIBILITY_GOOGLE
,
230 NICE_COMPATIBILITY_MSN
,
231 NICE_COMPATIBILITY_WLM2009
,
232 NICE_COMPATIBILITY_DRAFT19
= NICE_COMPATIBILITY_RFC5245
,
233 NICE_COMPATIBILITY_LAST
= NICE_COMPATIBILITY_WLM2009
,
238 * @NICE_PROXY_TYPE_NONE: Do not use a proxy
239 * @NICE_PROXY_TYPE_SOCKS5: Use a SOCKS5 proxy
240 * @NICE_PROXY_TYPE_HTTP: Use an HTTP proxy
241 * @NICE_PROXY_TYPE_LAST: Dummy last proxy type
243 * An enum to specify which proxy type to use for relaying.
244 * Note that the proxies will only be used with TCP TURN relaying.
245 * <para> See also: #NiceAgent:proxy-type </para>
251 NICE_PROXY_TYPE_NONE
= 0,
252 NICE_PROXY_TYPE_SOCKS5
,
253 NICE_PROXY_TYPE_HTTP
,
254 NICE_PROXY_TYPE_LAST
= NICE_PROXY_TYPE_HTTP
,
260 * @agent: The #NiceAgent Object
261 * @stream_id: The id of the stream
262 * @component_id: The id of the component of the stream
263 * which received the data
264 * @len: The length of the data
265 * @buf: The buffer containing the data received
266 * @user_data: The user data set in nice_agent_attach_recv()
268 * Callback function when data is received on a component
271 typedef void (*NiceAgentRecvFunc
) (
272 NiceAgent
*agent
, guint stream_id
, guint component_id
, guint len
,
273 gchar
*buf
, gpointer user_data
);
278 * @ctx: The Glib Mainloop Context to use for timers
279 * @compat: The compatibility mode of the agent
281 * Create a new #NiceAgent.
282 * The returned object must be freed with g_object_unref()
284 * Returns: The new agent GObject
287 nice_agent_new (GMainContext
*ctx
, NiceCompatibility compat
);
291 * nice_agent_new_reliable:
292 * @ctx: The Glib Mainloop Context to use for timers
293 * @compat: The compatibility mode of the agent
295 * Create a new #NiceAgent in reliable mode, which uses #PseudoTcpSocket to
296 * assure reliability of the messages.
297 * The returned object must be freed with g_object_unref()
298 * <para> See also: #NiceAgent::reliable-transport-writable </para>
302 * Returns: The new agent GObject
305 nice_agent_new_reliable (GMainContext
*ctx
, NiceCompatibility compat
);
308 * nice_agent_add_local_address:
309 * @agent: The #NiceAgent Object
310 * @addr: The address to listen to
311 * If the port is 0, then a random port will be chosen by the system
313 * Add a local address from which to derive local host candidates for
314 * candidate gathering.
316 * Since 0.0.5, if this method is not called, libnice will automatically
317 * discover the local addresses available
320 * See also: nice_agent_gather_candidates()
321 * Returns: %TRUE on success, %FALSE on fatal (memory allocation) errors
324 nice_agent_add_local_address (NiceAgent
*agent
, NiceAddress
*addr
);
328 * nice_agent_add_stream:
329 * @agent: The #NiceAgent Object
330 * @n_components: The number of components to add to the stream
332 * Adds a data stream to @agent containing @n_components components.
334 * Returns: The ID of the new stream, 0 on failure
337 nice_agent_add_stream (
342 * nice_agent_remove_stream:
343 * @agent: The #NiceAgent Object
344 * @stream_id: The ID of the stream to remove
346 * Remove and free a previously created data stream from @agent
350 nice_agent_remove_stream (
355 * nice_agent_set_relay_info:
356 * @agent: The #NiceAgent Object
357 * @stream_id: The ID of the stream
358 * @component_id: The ID of the component
359 * @server_ip: The IP address of the TURN server
360 * @server_port: The port of the TURN server
361 * @username: The TURN username to use for the allocate
362 * @password: The TURN password to use for the allocate
363 * @type: The type of relay to use
365 * Sets the settings for using a relay server during the candidate discovery.
367 * Returns: %TRUE if the TURN settings were accepted.
368 * %FALSE if the address was invalid.
370 gboolean
nice_agent_set_relay_info(
374 const gchar
*server_ip
,
376 const gchar
*username
,
377 const gchar
*password
,
381 * nice_agent_gather_candidates:
382 * @agent: The #NiceAgent Object
383 * @stream_id: The id of the stream to start
385 * Start the candidate gathering process.
386 * Once done, #NiceAgent::candidate-gathering-done is called for the stream
388 * See also: nice_agent_add_local_address()
391 Local addresses can be previously set with nice_agent_add_local_address()
394 Since 0.0.5, If no local address was previously added, then the nice agent
395 will automatically detect the local address using
396 nice_interfaces_get_local_ips()
401 nice_agent_gather_candidates (
406 * nice_agent_set_remote_credentials:
407 * @agent: The #NiceAgent Object
408 * @stream_id: The ID of the stream
409 * @ufrag: NULL-terminated string containing an ICE username fragment
410 * (length must be between 22 and 256 chars)
411 * @pwd: NULL-terminated string containing an ICE password
412 * (length must be between 4 and 256 chars)
414 * Sets the remote credentials for stream @stream_id.
418 Stream credentials do not override per-candidate credentials if set
422 * Returns: %TRUE on success, %FALSE on error.
425 nice_agent_set_remote_credentials (
428 const gchar
*ufrag
, const gchar
*pwd
);
433 * nice_agent_get_local_credentials:
434 * @agent: The #NiceAgent Object
435 * @stream_id: The ID of the stream
436 * @ufrag: a pointer to a NULL-terminated string containing
437 * an ICE username fragment [OUT].
438 * This string must be freed with g_free()
439 * @pwd: a pointer to a NULL-terminated string containing an ICE
441 * This string must be freed with g_free()
443 * Gets the local credentials for stream @stream_id.
445 * Returns: %TRUE on success, %FALSE on error.
448 nice_agent_get_local_credentials (
451 gchar
**ufrag
, gchar
**pwd
);
454 * nice_agent_set_remote_candidates:
455 * @agent: The #NiceAgent Object
456 * @stream_id: The ID of the stream the candidates are for
457 * @component_id: The ID of the component the candidates are for
458 * @candidates: a #GList of #NiceCandidate items describing each candidate to add
460 * Sets, adds or updates the remote candidates for a component of a stream.
464 NICE_AGENT_MAX_REMOTE_CANDIDATES is the absolute maximum limit
465 for remote candidates.
468 You must first call nice_agent_gather_candidates() and wait for the
469 #NiceAgent::candidate-gathering-done signale before
470 calling nice_agent_set_remote_candidates()
474 * Returns: The number of candidates added, negative on errors (memory allocation
475 * or if the local candidates are not done gathering yet)
478 nice_agent_set_remote_candidates (
482 const GSList
*candidates
);
487 * @agent: The #NiceAgent Object
488 * @stream_id: The ID of the stream to send to
489 * @component_id: The ID of the component to send to
490 * @len: The length of the buffer to send
491 * @buf: The buffer of data to send
493 * Sends a data payload over a stream's component.
497 Component state MUST be NICE_COMPONENT_STATE_READY, or as a special case,
498 in any state if component was in READY state before and was then restarted
502 * Returns: The number of bytes sent, or negative error code
513 * nice_agent_get_local_candidates:
514 * @agent: The #NiceAgent Object
515 * @stream_id: The ID of the stream
516 * @component_id: The ID of the component
518 * Retreive from the agent the list of all local candidates
519 * for a stream's component
523 The caller owns the returned GSList as well as the candidates contained
525 To get full results, the client should wait for the
526 #NiceAgent::candidate-gathering-done signal.
530 * Returns: a #GSList of #NiceCandidate objects representing
531 * the local candidates of @agent
534 nice_agent_get_local_candidates (
541 * nice_agent_get_remote_candidates:
542 * @agent: The #NiceAgent Object
543 * @stream_id: The ID of the stream
544 * @component_id: The ID of the component
546 * Get a list of the remote candidates set on a stream's component
550 The caller owns the returned GSList but not the candidates
554 The list of remote candidates can change during processing.
555 The client should register for the #NiceAgent::new-remote-candidate signal
556 to get notified of new remote candidates.
560 * Returns: a #GSList of #NiceCandidates objects representing
561 * the remote candidates set on the @agent
564 nice_agent_get_remote_candidates (
570 * nice_agent_restart:
571 * @agent: The #NiceAgent Object
573 * Restarts the session as defined in ICE draft 19. This function
574 * needs to be called both when initiating (ICE spec section 9.1.1.1.
575 * "ICE Restarts"), as well as when reacting (spec section 9.2.1.1.
576 * "Detecting ICE Restart") to a restart.
578 * Returns: %TRUE on success %FALSE on error
586 * nice_agent_attach_recv:
587 * @agent: The #NiceAgent Object
588 * @stream_id: The ID of stream
589 * @component_id: The ID of the component
590 * @ctx: The Glib Mainloop Context to use for listening on the component
591 * @func: The callback function to be called when data is received on
592 * the stream's component
593 * @data: user data associated with the callback
595 * Attaches the stream's component's sockets to the Glib Mainloop Context in
596 * order to be notified whenever data becomes available for a component.
598 * Returns: %TRUE on success, %FALSE if the stream or component IDs are invalid.
601 nice_agent_attach_recv (
606 NiceAgentRecvFunc func
,
611 * nice_agent_set_selected_pair:
612 * @agent: The #NiceAgent Object
613 * @stream_id: The ID of the stream
614 * @component_id: The ID of the component
615 * @lfoundation: The local foundation of the candidate to use
616 * @rfoundation: The remote foundation of the candidate to use
618 * Sets the selected candidate pair for media transmission
619 * for a given stream's component. Calling this function will
620 * disable all further ICE processing (connection check,
621 * state machine updates, etc). Note that keepalives will
622 * continue to be sent.
624 * Returns: %TRUE on success, %FALSE if the candidate pair cannot be found
627 nice_agent_set_selected_pair (
631 const gchar
*lfoundation
,
632 const gchar
*rfoundation
);
635 * nice_agent_set_selected_remote_candidate:
636 * @agent: The #NiceAgent Object
637 * @stream_id: The ID of the stream
638 * @component_id: The ID of the component
639 * @candidate: The #NiceCandidate to select
641 * Sets the selected remote candidate for media transmission
642 * for a given stream's component. This is used to force the selection of
643 * a specific remote candidate even when connectivity checks are failing
644 * (e.g. non-ICE compatible candidates).
645 * Calling this function will disable all further ICE processing
646 * (connection check, state machine updates, etc). Note that keepalives will
647 * continue to be sent.
649 * Returns: %TRUE on success, %FALSE on failure
652 nice_agent_set_selected_remote_candidate (
656 NiceCandidate
*candidate
);
660 * nice_agent_set_stream_tos:
661 * @agent: The #NiceAgent Object
662 * @stream_id: The ID of the stream
663 * @tos: The ToS to set
665 * Sets the IP_TOS and/or IPV6_TCLASS field on the stream's sockets' options
669 void nice_agent_set_stream_tos (
677 * nice_agent_set_software:
678 * @agent: The #NiceAgent Object
679 * @software: The value of the SOFTWARE attribute to add.
681 * This function will set the value of the SOFTWARE attribute to be added to
682 * STUN requests, responses and error responses sent during connectivity checks.
684 * The SOFTWARE attribute will only be added in the #NICE_COMPATIBILITY_RFC5245
685 * and #NICE_COMPATIBILITY_WLM2009 compatibility modes.
690 The @software argument will be appended with the libnice version before
694 The @software argument must be in UTF-8 encoding and only the first
695 128 characters will be sent.
702 void nice_agent_set_software (NiceAgent
*agent
, const gchar
*software
);
706 #endif /* _AGENT_H */