Version 0.1.0
[sipe-libnice.git] / agent / agent.h
blob56232585c362a65a87b49c87a0bdd77bf6ce9bd6
1 /*
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
17 * License.
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.
24 * Contributors:
25 * Dafydd Harries, Collabora Ltd.
26 * Youness Alaoui, Collabora Ltd.
27 * Kai Vehmanen, Nokia
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.
40 #ifndef _AGENT_H
41 #define _AGENT_H
43 /**
44 * SECTION:agent
45 * @short_description: ICE agent API implementation
46 * @see_also: #NiceCandidate
47 * @see_also: #NiceAddress
48 * @include: agent.h
49 * @stability: Stable
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.
56 <example>
57 <title>Simple example on how to use libnice</title>
58 <programlisting>
59 guint stream_id;
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,
80 cb_nice_recv, 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 ...
89 // Send our message!
90 nice_agent_send (agent, stream_id, 1, sizeof(buffer), buffer);
92 // Anything received will be received through the cb_nice_recv callback
94 // Destroy the object
95 g_object_unref(agent);
97 </programlisting>
98 </example>
102 #include <glib-object.h>
105 * NiceAgent:
107 * The #NiceAgent is the main GObject of the libnice library and represents
108 * the ICE agent.
110 typedef struct _NiceAgent NiceAgent;
112 #include "address.h"
113 #include "candidate.h"
114 #include "debug.h"
117 G_BEGIN_DECLS
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), \
131 NICE_TYPE_AGENT))
133 #define NICE_IS_AGENT_CLASS(klass) \
134 (G_TYPE_CHECK_CLASS_TYPE ((klass), \
135 NICE_TYPE_AGENT))
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
157 * clients.
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
168 * is now final
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>
176 typedef enum
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;
189 * NiceComponentType:
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.
195 <example>
196 <title>Example of use.</title>
197 <programlisting>
198 nice_agent_send (agent, stream_id, NICE_COMPONENT_TYPE_RTP, len, buf);
199 </programlisting>
200 </example>
202 typedef enum
204 NICE_COMPONENT_TYPE_RTP = 1,
205 NICE_COMPONENT_TYPE_RTCP = 2
206 } NiceComponentType;
210 * NiceCompatibility:
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
215 * 2009
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>
228 typedef enum
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,
238 } NiceCompatibility;
241 * NiceProxyType:
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>
251 * Since: 0.0.4
253 typedef enum
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,
259 } NiceProxyType;
263 * NiceAgentRecvFunc:
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);
281 * nice_agent_new:
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
290 NiceAgent *
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>
304 * Since: 0.0.11
306 * Returns: The new agent GObject
308 NiceAgent *
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.
319 * <para>
320 * Since 0.0.5, if this method is not called, libnice will automatically
321 * discover the local addresses available
322 * </para>
324 * See also: nice_agent_gather_candidates()
325 * Returns: %TRUE on success, %FALSE on fatal (memory allocation) errors
327 gboolean
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
340 guint
341 nice_agent_add_stream (
342 NiceAgent *agent,
343 guint n_components);
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
353 void
354 nice_agent_remove_stream (
355 NiceAgent *agent,
356 guint stream_id);
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.
368 * <para>
369 * If a local host candidate cannot be created on that port
370 * range, then the nice_agent_gather_candidates() call will fail.
371 * </para>
372 * <para>
373 * This MUST be called before nice_agent_gather_candidates()
374 * </para>
377 void
378 nice_agent_set_port_range (
379 NiceAgent *agent,
380 guint stream_id,
381 guint component_id,
382 guint min_port,
383 guint max_port);
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(
402 NiceAgent *agent,
403 guint stream_id,
404 guint component_id,
405 const gchar *server_ip,
406 guint server_port,
407 const gchar *username,
408 const gchar *password,
409 NiceRelayType type);
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.
425 <note>
426 <para>
427 Local addresses can be previously set with nice_agent_add_local_address()
428 </para>
429 <para>
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()
433 </para>
434 </note>
436 gboolean
437 nice_agent_gather_candidates (
438 NiceAgent *agent,
439 guint stream_id);
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.
452 <note>
453 <para>
454 Stream credentials do not override per-candidate credentials if set
455 </para>
456 </note>
458 * Returns: %TRUE on success, %FALSE on error.
460 gboolean
461 nice_agent_set_remote_credentials (
462 NiceAgent *agent,
463 guint stream_id,
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
476 * password [OUT]
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.
483 gboolean
484 nice_agent_get_local_credentials (
485 NiceAgent *agent,
486 guint stream_id,
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.
498 <note>
499 <para>
500 NICE_AGENT_MAX_REMOTE_CANDIDATES is the absolute maximum limit
501 for remote candidates.
502 </para>
503 <para>
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()
507 </para>
508 </note>
510 * Returns: The number of candidates added, negative on errors (memory allocation
511 * or if the local candidates are not done gathering yet)
514 nice_agent_set_remote_candidates (
515 NiceAgent *agent,
516 guint stream_id,
517 guint component_id,
518 const GSList *candidates);
522 * nice_agent_send:
523 * @agent: The #NiceAgent Object
524 * @stream_id: The ID of the stream to send to
525 * @component_id: The ID of the component to send to
526 * @len: The length of the buffer to send
527 * @buf: The buffer of data to send
529 * Sends a data payload over a stream's component.
531 <note>
532 <para>
533 Component state MUST be NICE_COMPONENT_STATE_READY, or as a special case,
534 in any state if component was in READY state before and was then restarted
535 </para>
536 <para>
537 In reliable mode, the -1 error value means either that you are not yet
538 connected or that the send buffer is full (equivalent to EWOULDBLOCK).
539 In both cases, you simply need to wait for the
540 #NiceAgent::reliable-transport-writable signal to be fired before resending
541 the data.
542 </para>
543 <para>
544 In non-reliable mode, it will virtually never happen with UDP sockets, but
545 it might happen if the active candidate is a TURN-TCP connection that got
546 disconnected.
547 </para>
548 <para>
549 In both reliable and non-reliable mode, a -1 error code could also mean that
550 the stream_id and/or component_id are invalid.
551 </para>
552 </note>
554 * Returns: The number of bytes sent, or negative error code
556 gint
557 nice_agent_send (
558 NiceAgent *agent,
559 guint stream_id,
560 guint component_id,
561 guint len,
562 const gchar *buf);
565 * nice_agent_get_local_candidates:
566 * @agent: The #NiceAgent Object
567 * @stream_id: The ID of the stream
568 * @component_id: The ID of the component
570 * Retreive from the agent the list of all local candidates
571 * for a stream's component
573 <note>
574 <para>
575 The caller owns the returned GSList as well as the candidates contained
576 within it.
577 To get full results, the client should wait for the
578 #NiceAgent::candidate-gathering-done signal.
579 </para>
580 </note>
582 * Returns: a #GSList of #NiceCandidate objects representing
583 * the local candidates of @agent
585 GSList *
586 nice_agent_get_local_candidates (
587 NiceAgent *agent,
588 guint stream_id,
589 guint component_id);
593 * nice_agent_get_remote_candidates:
594 * @agent: The #NiceAgent Object
595 * @stream_id: The ID of the stream
596 * @component_id: The ID of the component
598 * Get a list of the remote candidates set on a stream's component
600 <note>
601 <para>
602 The caller owns the returned GSList but not the candidates
603 contained within it.
604 </para>
605 <para>
606 The list of remote candidates can change during processing.
607 The client should register for the #NiceAgent::new-remote-candidate signal
608 to get notified of new remote candidates.
609 </para>
610 </note>
612 * Returns: a #GSList of #NiceCandidates objects representing
613 * the remote candidates set on the @agent
615 GSList *
616 nice_agent_get_remote_candidates (
617 NiceAgent *agent,
618 guint stream_id,
619 guint component_id);
622 * nice_agent_restart:
623 * @agent: The #NiceAgent Object
625 * Restarts the session as defined in ICE draft 19. This function
626 * needs to be called both when initiating (ICE spec section 9.1.1.1.
627 * "ICE Restarts"), as well as when reacting (spec section 9.2.1.1.
628 * "Detecting ICE Restart") to a restart.
630 * Returns: %TRUE on success %FALSE on error
632 gboolean
633 nice_agent_restart (
634 NiceAgent *agent);
638 * nice_agent_attach_recv:
639 * @agent: The #NiceAgent Object
640 * @stream_id: The ID of stream
641 * @component_id: The ID of the component
642 * @ctx: The Glib Mainloop Context to use for listening on the component
643 * @func: The callback function to be called when data is received on
644 * the stream's component
645 * @data: user data associated with the callback
647 * Attaches the stream's component's sockets to the Glib Mainloop Context in
648 * order to be notified whenever data becomes available for a component.
650 * Returns: %TRUE on success, %FALSE if the stream or component IDs are invalid.
652 gboolean
653 nice_agent_attach_recv (
654 NiceAgent *agent,
655 guint stream_id,
656 guint component_id,
657 GMainContext *ctx,
658 NiceAgentRecvFunc func,
659 gpointer data);
663 * nice_agent_set_selected_pair:
664 * @agent: The #NiceAgent Object
665 * @stream_id: The ID of the stream
666 * @component_id: The ID of the component
667 * @lfoundation: The local foundation of the candidate to use
668 * @rfoundation: The remote foundation of the candidate to use
670 * Sets the selected candidate pair for media transmission
671 * for a given stream's component. Calling this function will
672 * disable all further ICE processing (connection check,
673 * state machine updates, etc). Note that keepalives will
674 * continue to be sent.
676 * Returns: %TRUE on success, %FALSE if the candidate pair cannot be found
678 gboolean
679 nice_agent_set_selected_pair (
680 NiceAgent *agent,
681 guint stream_id,
682 guint component_id,
683 const gchar *lfoundation,
684 const gchar *rfoundation);
687 * nice_agent_set_selected_remote_candidate:
688 * @agent: The #NiceAgent Object
689 * @stream_id: The ID of the stream
690 * @component_id: The ID of the component
691 * @candidate: The #NiceCandidate to select
693 * Sets the selected remote candidate for media transmission
694 * for a given stream's component. This is used to force the selection of
695 * a specific remote candidate even when connectivity checks are failing
696 * (e.g. non-ICE compatible candidates).
697 * Calling this function will disable all further ICE processing
698 * (connection check, state machine updates, etc). Note that keepalives will
699 * continue to be sent.
701 * Returns: %TRUE on success, %FALSE on failure
703 gboolean
704 nice_agent_set_selected_remote_candidate (
705 NiceAgent *agent,
706 guint stream_id,
707 guint component_id,
708 NiceCandidate *candidate);
712 * nice_agent_set_stream_tos:
713 * @agent: The #NiceAgent Object
714 * @stream_id: The ID of the stream
715 * @tos: The ToS to set
717 * Sets the IP_TOS and/or IPV6_TCLASS field on the stream's sockets' options
719 * Since: 0.0.9
721 void nice_agent_set_stream_tos (
722 NiceAgent *agent,
723 guint stream_id,
724 gint tos);
729 * nice_agent_set_software:
730 * @agent: The #NiceAgent Object
731 * @software: The value of the SOFTWARE attribute to add.
733 * This function will set the value of the SOFTWARE attribute to be added to
734 * STUN requests, responses and error responses sent during connectivity checks.
735 * <para>
736 * The SOFTWARE attribute will only be added in the #NICE_COMPATIBILITY_RFC5245
737 * and #NICE_COMPATIBILITY_WLM2009 compatibility modes.
739 * </para>
740 * <note>
741 <para>
742 The @software argument will be appended with the libnice version before
743 being sent.
744 </para>
745 <para>
746 The @software argument must be in UTF-8 encoding and only the first
747 128 characters will be sent.
748 </para>
749 </note>
751 * Since: 0.0.10
754 void nice_agent_set_software (NiceAgent *agent, const gchar *software);
756 G_END_DECLS
758 #endif /* _AGENT_H */