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_OC2007: Use compatibility with Microsoft Office Communicator 2007
217 * @NICE_COMPATIBILITY_OC2007R2: Use compatibility with Microsoft Office Communicator 2007 R2
218 * @NICE_COMPATIBILITY_DRAFT19: Use compatibility for ICE Draft 19 specs
219 * @NICE_COMPATIBILITY_LAST: Dummy last compatibility mode
221 * An enum to specify which compatible specifications the #NiceAgent should use.
222 * Use with nice_agent_new()
224 * <warning>@NICE_COMPATIBILITY_DRAFT19 is deprecated and should not be used
225 * in newly-written code. It is kept for compatibility reasons and
226 * represents the same compatibility as @NICE_COMPATIBILITY_RFC5245 </warning>
230 NICE_COMPATIBILITY_RFC5245
= 0,
231 NICE_COMPATIBILITY_GOOGLE
,
232 NICE_COMPATIBILITY_MSN
,
233 NICE_COMPATIBILITY_WLM2009
,
234 NICE_COMPATIBILITY_OC2007
,
235 NICE_COMPATIBILITY_OC2007R2
,
236 NICE_COMPATIBILITY_DRAFT19
= NICE_COMPATIBILITY_RFC5245
,
237 NICE_COMPATIBILITY_LAST
= NICE_COMPATIBILITY_OC2007R2
,
242 * @NICE_PROXY_TYPE_NONE: Do not use a proxy
243 * @NICE_PROXY_TYPE_SOCKS5: Use a SOCKS5 proxy
244 * @NICE_PROXY_TYPE_HTTP: Use an HTTP proxy
245 * @NICE_PROXY_TYPE_LAST: Dummy last proxy type
247 * An enum to specify which proxy type to use for relaying.
248 * Note that the proxies will only be used with TCP TURN relaying.
249 * <para> See also: #NiceAgent:proxy-type </para>
255 NICE_PROXY_TYPE_NONE
= 0,
256 NICE_PROXY_TYPE_SOCKS5
,
257 NICE_PROXY_TYPE_HTTP
,
258 NICE_PROXY_TYPE_LAST
= NICE_PROXY_TYPE_HTTP
,
264 * @agent: The #NiceAgent Object
265 * @stream_id: The id of the stream
266 * @component_id: The id of the component of the stream
267 * which received the data
268 * @len: The length of the data
269 * @buf: The buffer containing the data received
270 * @user_data: The user data set in nice_agent_attach_recv()
272 * Callback function when data is received on a component
275 typedef void (*NiceAgentRecvFunc
) (
276 NiceAgent
*agent
, guint stream_id
, guint component_id
, guint len
,
277 gchar
*buf
, gpointer user_data
);
282 * @ctx: The Glib Mainloop Context to use for timers
283 * @compat: The compatibility mode of the agent
285 * Create a new #NiceAgent.
286 * The returned object must be freed with g_object_unref()
288 * Returns: The new agent GObject
291 nice_agent_new (GMainContext
*ctx
, NiceCompatibility compat
);
295 * nice_agent_new_reliable:
296 * @ctx: The Glib Mainloop Context to use for timers
297 * @compat: The compatibility mode of the agent
299 * Create a new #NiceAgent in reliable mode, which uses #PseudoTcpSocket to
300 * assure reliability of the messages.
301 * The returned object must be freed with g_object_unref()
302 * <para> See also: #NiceAgent::reliable-transport-writable </para>
306 * Returns: The new agent GObject
309 nice_agent_new_reliable (GMainContext
*ctx
, NiceCompatibility compat
);
312 * nice_agent_add_local_address:
313 * @agent: The #NiceAgent Object
314 * @addr: The address to listen to
315 * If the port is 0, then a random port will be chosen by the system
317 * Add a local address from which to derive local host candidates for
318 * candidate gathering.
320 * Since 0.0.5, if this method is not called, libnice will automatically
321 * discover the local addresses available
324 * See also: nice_agent_gather_candidates()
325 * Returns: %TRUE on success, %FALSE on fatal (memory allocation) errors
328 nice_agent_add_local_address (NiceAgent
*agent
, NiceAddress
*addr
);
332 * nice_agent_add_stream:
333 * @agent: The #NiceAgent Object
334 * @n_components: The number of components to add to the stream
336 * Adds a data stream to @agent containing @n_components components.
338 * Returns: The ID of the new stream, 0 on failure
341 nice_agent_add_stream (
346 * nice_agent_remove_stream:
347 * @agent: The #NiceAgent Object
348 * @stream_id: The ID of the stream to remove
350 * Remove and free a previously created data stream from @agent
354 nice_agent_remove_stream (
360 * nice_agent_set_port_range:
361 * @agent: The #NiceAgent Object
362 * @stream_id: The ID of the stream
363 * @component_id: The ID of the component
364 * @min_port: The minimum port to use
365 * @max_port: The maximum port to use
367 * Sets a preferred port range for allocating host candidates.
369 * If a local host candidate cannot be created on that port
370 * range, then the nice_agent_gather_candidates() call will fail.
373 * This MUST be called before nice_agent_gather_candidates()
378 nice_agent_set_port_range (
386 * nice_agent_set_relay_info:
387 * @agent: The #NiceAgent Object
388 * @stream_id: The ID of the stream
389 * @component_id: The ID of the component
390 * @server_ip: The IP address of the TURN server
391 * @server_port: The port of the TURN server
392 * @username: The TURN username to use for the allocate
393 * @password: The TURN password to use for the allocate
394 * @type: The type of relay to use
396 * Sets the settings for using a relay server during the candidate discovery.
398 * Returns: %TRUE if the TURN settings were accepted.
399 * %FALSE if the address was invalid.
401 gboolean
nice_agent_set_relay_info(
405 const gchar
*server_ip
,
407 const gchar
*username
,
408 const gchar
*password
,
412 * nice_agent_gather_candidates:
413 * @agent: The #NiceAgent Object
414 * @stream_id: The id of the stream to start
416 * Start the candidate gathering process.
417 * Once done, #NiceAgent::candidate-gathering-done is called for the stream
419 * <para>See also: nice_agent_add_local_address()</para>
420 * <para>See also: nice_agent_set_port_range()</para>
422 * Returns: %FALSE if the stream id is invalid or if a host candidate couldn't be allocated
423 * on the requested interfaces/ports.
427 Local addresses can be previously set with nice_agent_add_local_address()
430 Since 0.0.5, If no local address was previously added, then the nice agent
431 will automatically detect the local address using
432 nice_interfaces_get_local_ips()
437 nice_agent_gather_candidates (
442 * nice_agent_set_remote_credentials:
443 * @agent: The #NiceAgent Object
444 * @stream_id: The ID of the stream
445 * @ufrag: NULL-terminated string containing an ICE username fragment
446 * (length must be between 22 and 256 chars)
447 * @pwd: NULL-terminated string containing an ICE password
448 * (length must be between 4 and 256 chars)
450 * Sets the remote credentials for stream @stream_id.
454 Stream credentials do not override per-candidate credentials if set
458 * Returns: %TRUE on success, %FALSE on error.
461 nice_agent_set_remote_credentials (
464 const gchar
*ufrag
, const gchar
*pwd
);
469 * nice_agent_get_local_credentials:
470 * @agent: The #NiceAgent Object
471 * @stream_id: The ID of the stream
472 * @ufrag: a pointer to a NULL-terminated string containing
473 * an ICE username fragment [OUT].
474 * This string must be freed with g_free()
475 * @pwd: a pointer to a NULL-terminated string containing an ICE
477 * This string must be freed with g_free()
479 * Gets the local credentials for stream @stream_id.
481 * Returns: %TRUE on success, %FALSE on error.
484 nice_agent_get_local_credentials (
487 gchar
**ufrag
, gchar
**pwd
);
490 * nice_agent_set_remote_candidates:
491 * @agent: The #NiceAgent Object
492 * @stream_id: The ID of the stream the candidates are for
493 * @component_id: The ID of the component the candidates are for
494 * @candidates: a #GSList of #NiceCandidate items describing each candidate to add
496 * Sets, adds or updates the remote candidates for a component of a stream.
500 NICE_AGENT_MAX_REMOTE_CANDIDATES is the absolute maximum limit
501 for remote candidates.
504 You must first call nice_agent_gather_candidates() and wait for the
505 #NiceAgent::candidate-gathering-done signale before
506 calling nice_agent_set_remote_candidates()
509 Since 0.1.3, there is no need to wait for the candidate-gathering-done signal.
510 Remote candidates can be set even while gathering local candidates.
511 Newly discovered local candidates will automatically be paired with
512 existing remote candidates.
516 * Returns: The number of candidates added, negative on errors (memory allocation
517 * or if the local candidates are not done gathering yet)
520 nice_agent_set_remote_candidates (
524 const GSList
*candidates
);
529 * @agent: The #NiceAgent Object
530 * @stream_id: The ID of the stream to send to
531 * @component_id: The ID of the component to send to
532 * @len: The length of the buffer to send
533 * @buf: The buffer of data to send
535 * Sends a data payload over a stream's component.
539 Component state MUST be NICE_COMPONENT_STATE_READY, or as a special case,
540 in any state if component was in READY state before and was then restarted
543 In reliable mode, the -1 error value means either that you are not yet
544 connected or that the send buffer is full (equivalent to EWOULDBLOCK).
545 In both cases, you simply need to wait for the
546 #NiceAgent::reliable-transport-writable signal to be fired before resending
550 In non-reliable mode, it will virtually never happen with UDP sockets, but
551 it might happen if the active candidate is a TURN-TCP connection that got
555 In both reliable and non-reliable mode, a -1 error code could also mean that
556 the stream_id and/or component_id are invalid.
560 * Returns: The number of bytes sent, or negative error code
571 * nice_agent_get_local_candidates:
572 * @agent: The #NiceAgent Object
573 * @stream_id: The ID of the stream
574 * @component_id: The ID of the component
576 * Retreive from the agent the list of all local candidates
577 * for a stream's component
581 The caller owns the returned GSList as well as the candidates contained
583 To get full results, the client should wait for the
584 #NiceAgent::candidate-gathering-done signal.
588 * Returns: a #GSList of #NiceCandidate objects representing
589 * the local candidates of @agent
592 nice_agent_get_local_candidates (
599 * nice_agent_get_remote_candidates:
600 * @agent: The #NiceAgent Object
601 * @stream_id: The ID of the stream
602 * @component_id: The ID of the component
604 * Get a list of the remote candidates set on a stream's component
608 The caller owns the returned GSList but not the candidates
612 The list of remote candidates can change during processing.
613 The client should register for the #NiceAgent::new-remote-candidate signal
614 to get notified of new remote candidates.
618 * Returns: a #GSList of #NiceCandidates objects representing
619 * the remote candidates set on the @agent
622 nice_agent_get_remote_candidates (
628 * nice_agent_restart:
629 * @agent: The #NiceAgent Object
631 * Restarts the session as defined in ICE draft 19. This function
632 * needs to be called both when initiating (ICE spec section 9.1.1.1.
633 * "ICE Restarts"), as well as when reacting (spec section 9.2.1.1.
634 * "Detecting ICE Restart") to a restart.
636 * Returns: %TRUE on success %FALSE on error
644 * nice_agent_attach_recv:
645 * @agent: The #NiceAgent Object
646 * @stream_id: The ID of stream
647 * @component_id: The ID of the component
648 * @ctx: The Glib Mainloop Context to use for listening on the component
649 * @func: The callback function to be called when data is received on
650 * the stream's component
651 * @data: user data associated with the callback
653 * Attaches the stream's component's sockets to the Glib Mainloop Context in
654 * order to be notified whenever data becomes available for a component.
656 * Returns: %TRUE on success, %FALSE if the stream or component IDs are invalid.
659 nice_agent_attach_recv (
664 NiceAgentRecvFunc func
,
669 * nice_agent_set_selected_pair:
670 * @agent: The #NiceAgent Object
671 * @stream_id: The ID of the stream
672 * @component_id: The ID of the component
673 * @lfoundation: The local foundation of the candidate to use
674 * @rfoundation: The remote foundation of the candidate to use
676 * Sets the selected candidate pair for media transmission
677 * for a given stream's component. Calling this function will
678 * disable all further ICE processing (connection check,
679 * state machine updates, etc). Note that keepalives will
680 * continue to be sent.
682 * Returns: %TRUE on success, %FALSE if the candidate pair cannot be found
685 nice_agent_set_selected_pair (
689 const gchar
*lfoundation
,
690 const gchar
*rfoundation
);
693 * nice_agent_set_selected_remote_candidate:
694 * @agent: The #NiceAgent Object
695 * @stream_id: The ID of the stream
696 * @component_id: The ID of the component
697 * @candidate: The #NiceCandidate to select
699 * Sets the selected remote candidate for media transmission
700 * for a given stream's component. This is used to force the selection of
701 * a specific remote candidate even when connectivity checks are failing
702 * (e.g. non-ICE compatible candidates).
703 * Calling this function will disable all further ICE processing
704 * (connection check, state machine updates, etc). Note that keepalives will
705 * continue to be sent.
707 * Returns: %TRUE on success, %FALSE on failure
710 nice_agent_set_selected_remote_candidate (
714 NiceCandidate
*candidate
);
718 * nice_agent_set_stream_tos:
719 * @agent: The #NiceAgent Object
720 * @stream_id: The ID of the stream
721 * @tos: The ToS to set
723 * Sets the IP_TOS and/or IPV6_TCLASS field on the stream's sockets' options
727 void nice_agent_set_stream_tos (
735 * nice_agent_set_software:
736 * @agent: The #NiceAgent Object
737 * @software: The value of the SOFTWARE attribute to add.
739 * This function will set the value of the SOFTWARE attribute to be added to
740 * STUN requests, responses and error responses sent during connectivity checks.
742 * The SOFTWARE attribute will only be added in the #NICE_COMPATIBILITY_RFC5245
743 * and #NICE_COMPATIBILITY_WLM2009 compatibility modes.
748 The @software argument will be appended with the libnice version before
752 The @software argument must be in UTF-8 encoding and only the first
753 128 characters will be sent.
760 void nice_agent_set_software (NiceAgent
*agent
, const gchar
*software
);
764 #endif /* _AGENT_H */