Add nice_agent_new_reliable to the libnice API which uses pseudotcp
[sipe-libnice.git] / agent / agent.h
blobc83366a589db5ac60a7682bba9059ea76d0dd61c
1 /*
2 * This file is part of the Nice GLib ICE library.
4 * (C) 2006, 2007 Collabora Ltd.
5 * Contact: Dafydd Harries
6 * (C) 2006, 2007 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 * Kai Vehmanen, Nokia
28 * Alternatively, the contents of this file may be used under the terms of the
29 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
30 * case the provisions of LGPL are applicable instead of those above. If you
31 * wish to allow use of your version of this file only under the terms of the
32 * LGPL and not to allow others to use your version of this file under the
33 * MPL, indicate your decision by deleting the provisions above and replace
34 * them with the notice and other provisions required by the LGPL. If you do
35 * not delete the provisions above, a recipient may use your version of this
36 * file under either the MPL or the LGPL.
39 #ifndef _AGENT_H
40 #define _AGENT_H
42 /**
43 * SECTION:agent
44 * @short_description: ICE agent API implementation
45 * @see_also: #NiceCandidate
46 * @see_also: #NiceAddress
47 * @include: agent.h
48 * @stability: Stable
50 * The #NiceAgent is your main object when using libnice.
51 * It is the agent that will take care of everything relating to ICE.
52 * It will take care of discovering your local candidates and do
53 * connectivity checks to create a stream of data between you and your peer.
55 <example>
56 <title>Simple example on how to use libnice</title>
57 <programlisting>
58 guint stream_id;
59 gchar buffer[] = "hello world!";
60 GSList *lcands = NULL;
62 // Create a nice agent
63 NiceAgent *agent = nice_agent_new (NULL, NICE_COMPATIBILITY_DRAFT19);
65 // Connect the signals
66 g_signal_connect (G_OBJECT (agent), "candidate-gathering-done",
67 G_CALLBACK (cb_candidate_gathering_done), NULL);
68 g_signal_connect (G_OBJECT (agent), "component-state-changed",
69 G_CALLBACK (cb_component_state_changed), NULL);
70 g_signal_connect (G_OBJECT (agent), "new-selected-pair",
71 G_CALLBACK (cb_new_selected_pair), NULL);
73 // Create a new stream with one component and start gathering candidates
74 stream_id = nice_agent_add_stream (agent, 1);
75 nice_agent_gather_candidates (agent, stream_id);
77 // Attach to the component to receive the data
78 nice_agent_attach_recv (agent, stream_id, 1, NULL,
79 cb_nice_recv, NULL);
81 // ... Wait until the signal candidate-gathering-done is fired ...
82 lcands = nice_agent_get_local_candidates(agent, stream_id, 1);
84 // ... Send local candidates to the peer and set the peer's remote candidates
85 nice_agent_set_remote_candidates (agent, stream_id, 1, rcands);
87 // ... Wait until the signal new-selected-pair is fired ...
88 // Send our message!
89 nice_agent_send (agent, stream_id, 1, sizeof(buffer), buffer);
91 // Anything received will be received through the cb_nice_recv callback
93 // Destroy the object
94 g_object_unref(agent);
96 </programlisting>
97 </example>
101 #include <glib-object.h>
104 * NiceAgent:
106 * The #NiceAgent is the main GObject of the libnice library and represents
107 * the ICE agent.
109 typedef struct _NiceAgent NiceAgent;
111 #include "address.h"
112 #include "candidate.h"
113 #include "debug.h"
116 G_BEGIN_DECLS
118 #define NICE_TYPE_AGENT nice_agent_get_type()
120 #define NICE_AGENT(obj) \
121 (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
122 NICE_TYPE_AGENT, NiceAgent))
124 #define NICE_AGENT_CLASS(klass) \
125 (G_TYPE_CHECK_CLASS_CAST ((klass), \
126 NICE_TYPE_AGENT, NiceAgentClass))
128 #define NICE_IS_AGENT(obj) \
129 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
130 NICE_TYPE_AGENT))
132 #define NICE_IS_AGENT_CLASS(klass) \
133 (G_TYPE_CHECK_CLASS_TYPE ((klass), \
134 NICE_TYPE_AGENT))
136 #define NICE_AGENT_GET_CLASS(obj) \
137 (G_TYPE_INSTANCE_GET_CLASS ((obj), \
138 NICE_TYPE_AGENT, NiceAgentClass))
140 typedef struct _NiceAgentClass NiceAgentClass;
142 struct _NiceAgentClass
144 GObjectClass parent_class;
148 GType nice_agent_get_type (void);
152 * NICE_AGENT_MAX_REMOTE_CANDIDATES:
154 * A hard limit for the number of remote candidates. This
155 * limit is enforced to protect against malevolent remote
156 * clients.
158 #define NICE_AGENT_MAX_REMOTE_CANDIDATES 25
161 * NiceComponentState:
162 * @NICE_COMPONENT_STATE_DISCONNECTED: No activity scheduled
163 * @NICE_COMPONENT_STATE_GATHERING: Gathering local candidates
164 * @NICE_COMPONENT_STATE_CONNECTING: Establishing connectivity
165 * @NICE_COMPONENT_STATE_CONNECTED: At least one working candidate pair
166 * @NICE_COMPONENT_STATE_READY: ICE concluded, candidate pair selection
167 * is now final
168 * @NICE_COMPONENT_STATE_FAILED: Connectivity checks have been completed,
169 * but connectivity was not established
170 * @NICE_COMPONENT_STATE_LAST: Dummy state
172 * An enum representing the state of a component.
173 * <para> See also: #NiceAgent::component-state-changed </para>
175 typedef enum
177 NICE_COMPONENT_STATE_DISCONNECTED,
178 NICE_COMPONENT_STATE_GATHERING,
179 NICE_COMPONENT_STATE_CONNECTING,
180 NICE_COMPONENT_STATE_CONNECTED,
181 NICE_COMPONENT_STATE_READY,
182 NICE_COMPONENT_STATE_FAILED,
183 NICE_COMPONENT_STATE_LAST
184 } NiceComponentState;
188 * NiceComponentType:
189 * @NICE_COMPONENT_TYPE_RTP: RTP Component type
190 * @NICE_COMPONENT_TYPE_RTCP: RTCP Component type
192 * Convenience enum representing the type of a component for use as the
193 * component_id for RTP/RTCP usages.
194 <example>
195 <title>Example of use.</title>
196 <programlisting>
197 nice_agent_send (agent, stream_id, NICE_COMPONENT_TYPE_RTP, len, buf);
198 </programlisting>
199 </example>
201 typedef enum
203 NICE_COMPONENT_TYPE_RTP = 1,
204 NICE_COMPONENT_TYPE_RTCP = 2
205 } NiceComponentType;
209 * NiceCompatibility:
210 * @NICE_COMPATIBILITY_DRAFT19: Use compatibility for ICE Draft 19 specs
211 * @NICE_COMPATIBILITY_GOOGLE: Use compatibility for Google Talk specs
212 * @NICE_COMPATIBILITY_MSN: Use compatibility for MSN Messenger specs
213 * @NICE_COMPATIBILITY_WLM2009: Use compatibility with Windows Live Messenger
214 * 2009
215 * @NICE_COMPATIBILITY_LAST: Dummy last compatibility mode
217 * An enum to specify which compatible specifications the #NiceAgent should use.
218 * Use with nice_agent_new()
220 typedef enum
222 NICE_COMPATIBILITY_DRAFT19 = 0,
223 NICE_COMPATIBILITY_GOOGLE,
224 NICE_COMPATIBILITY_MSN,
225 NICE_COMPATIBILITY_WLM2009,
226 NICE_COMPATIBILITY_LAST = NICE_COMPATIBILITY_WLM2009
227 } NiceCompatibility;
230 * NiceProxyType:
231 * @NICE_PROXY_TYPE_NONE: Do not use a proxy
232 * @NICE_PROXY_TYPE_SOCKS5: Use a SOCKS5 proxy
233 * @NICE_PROXY_TYPE_HTTP: Use an HTTP proxy
234 * @NICE_PROXY_TYPE_LAST: Dummy last proxy type
236 * An enum to specify which proxy type to use for relaying.
237 * Note that the proxies will only be used with TCP TURN relaying.
238 * <para> See also: #NiceAgent:proxy-type </para>
240 typedef enum
242 NICE_PROXY_TYPE_NONE = 0,
243 NICE_PROXY_TYPE_SOCKS5,
244 NICE_PROXY_TYPE_HTTP,
245 NICE_PROXY_TYPE_LAST = NICE_PROXY_TYPE_HTTP,
246 } NiceProxyType;
250 * NiceAgentRecvFunc:
251 * @agent: The #NiceAgent Object
252 * @stream_id: The id of the stream
253 * @component_id: The id of the component of the stream
254 * which received the data
255 * @len: The length of the data
256 * @buf: The buffer containing the data received
257 * @user_data: The user data set in nice_agent_attach_recv()
259 * Callback function when data is received on a component
262 typedef void (*NiceAgentRecvFunc) (
263 NiceAgent *agent, guint stream_id, guint component_id, guint len,
264 gchar *buf, gpointer user_data);
268 * nice_agent_new:
269 * @ctx: The Glib Mainloop Context to use for timers
270 * @compat: The compatibility mode of the agent
272 * Create a new #NiceAgent.
273 * The returned object must be freed with g_object_unref()
275 * Returns: The new agent GObject
277 NiceAgent *
278 nice_agent_new (GMainContext *ctx, NiceCompatibility compat);
282 * nice_agent_new_reliable:
283 * @ctx: The Glib Mainloop Context to use for timers
284 * @compat: The compatibility mode of the agent
286 * Create a new #NiceAgent in reliable mode, which uses #PseudoTcpSocket to
287 * assure reliability of the messages.
288 * The returned object must be freed with g_object_unref()
290 * Returns: The new agent GObject
292 NiceAgent *
293 nice_agent_new_reliable (GMainContext *ctx, NiceCompatibility compat);
296 * nice_agent_add_local_address:
297 * @agent: The #NiceAgent Object
298 * @addr: The address to listen to
299 * If the port is 0, then a random port will be chosen by the system
301 * Add a local address from which to derive local host candidates
303 * Returns: %TRUE on success, %FALSE on fatal (memory allocation) errors
305 gboolean
306 nice_agent_add_local_address (NiceAgent *agent, NiceAddress *addr);
310 * nice_agent_add_stream:
311 * @agent: The #NiceAgent Object
312 * @n_components: The number of components to add to the stream
314 * Adds a data stream to @agent containing @n_components components.
316 * Returns: The ID of the new stream, 0 on failure
318 guint
319 nice_agent_add_stream (
320 NiceAgent *agent,
321 guint n_components);
324 * nice_agent_remove_stream:
325 * @agent: The #NiceAgent Object
326 * @stream_id: The ID of the stream to remove
328 * Remove and free a previously created data stream from @agent
331 void
332 nice_agent_remove_stream (
333 NiceAgent *agent,
334 guint stream_id);
337 * nice_agent_set_relay_info:
338 * @agent: The #NiceAgent Object
339 * @stream_id: The ID of the stream
340 * @component_id: The ID of the component
341 * @server_ip: The IP address of the TURN server
342 * @server_port: The port of the TURN server
343 * @username: The TURN username to use for the allocate
344 * @password: The TURN password to use for the allocate
345 * @type: The type of relay to use
347 * Sets the settings for using a relay server during the candidate discovery.
349 * Returns: %TRUE if the TURN settings were accepted.
350 * %FALSE if the address was invalid.
352 gboolean nice_agent_set_relay_info(
353 NiceAgent *agent,
354 guint stream_id,
355 guint component_id,
356 const gchar *server_ip,
357 guint server_port,
358 const gchar *username,
359 const gchar *password,
360 NiceRelayType type);
363 * nice_agent_gather_candidates:
364 * @agent: The #NiceAgent Object
365 * @stream_id: The id of the stream to start
367 * Start the candidate gathering process.
368 * Once done, #NiceAgent::candidate-gathering-done is called for the stream
370 <note>
371 <para>
372 Local addresses can be previously set with nice_agent_add_local_address()
373 </para>
374 <para>
375 If no local address was previously added, then the nice agent will
376 automatically detect the local address using nice_interfaces_get_local_ips()
377 </para>
378 </note>
380 void
381 nice_agent_gather_candidates (
382 NiceAgent *agent,
383 guint stream_id);
386 * nice_agent_set_remote_credentials:
387 * @agent: The #NiceAgent Object
388 * @stream_id: The ID of the stream
389 * @ufrag: NULL-terminated string containing an ICE username fragment
390 * (length must be between 22 and 256 chars)
391 * @pwd: NULL-terminated string containing an ICE password
392 * (length must be between 4 and 256 chars)
394 * Sets the remote credentials for stream @stream_id.
396 <note>
397 <para>
398 Stream credentials do not override per-candidate credentials if set
399 </para>
400 </note>
402 * Returns: %TRUE on success, %FALSE on error.
404 gboolean
405 nice_agent_set_remote_credentials (
406 NiceAgent *agent,
407 guint stream_id,
408 const gchar *ufrag, const gchar *pwd);
413 * nice_agent_get_local_credentials:
414 * @agent: The #NiceAgent Object
415 * @stream_id: The ID of the stream
416 * @ufrag: a pointer to a NULL-terminated string containing
417 * an ICE username fragment [OUT].
418 * This string must be freed with g_free()
419 * @pwd: a pointer to a NULL-terminated string containing an ICE
420 * password [OUT]
421 * This string must be freed with g_free()
423 * Gets the local credentials for stream @stream_id.
425 * Returns: %TRUE on success, %FALSE on error.
427 gboolean
428 nice_agent_get_local_credentials (
429 NiceAgent *agent,
430 guint stream_id,
431 gchar **ufrag, gchar **pwd);
434 * nice_agent_set_remote_candidates:
435 * @agent: The #NiceAgent Object
436 * @stream_id: The ID of the stream the candidates are for
437 * @component_id: The ID of the component the candidates are for
438 * @candidates: a #GList of #NiceCandidate items describing each candidate to add
440 * Sets, adds or updates the remote candidates for a component of a stream.
442 <note>
443 <para>
444 NICE_AGENT_MAX_REMOTE_CANDIDATES is the absolute maximum limit
445 for remote candidates.
446 </para>
447 <para>
448 You must first call nice_agent_gather_candidates() and wait for the
449 #NiceAgent::candidate-gathering-done signale before
450 calling nice_agent_set_remote_candidates()
451 </para>
452 </note>
454 * Returns: The number of candidates added, negative on errors (memory allocation
455 * or if the local candidates are not done gathering yet)
458 nice_agent_set_remote_candidates (
459 NiceAgent *agent,
460 guint stream_id,
461 guint component_id,
462 const GSList *candidates);
466 * nice_agent_send:
467 * @agent: The #NiceAgent Object
468 * @stream_id: The ID of the stream to send to
469 * @component_id: The ID of the component to send to
470 * @len: The length of the buffer to send
471 * @buf: The buffer of data to send
473 * Sends a data payload over a stream's component.
475 <note>
476 <para>
477 Component state MUST be NICE_COMPONENT_STATE_READY, or as a special case,
478 in any state if component was in READY state before and was then restarted
479 </para>
480 </note>
482 * Returns: The number of bytes sent, or negative error code
484 gint
485 nice_agent_send (
486 NiceAgent *agent,
487 guint stream_id,
488 guint component_id,
489 guint len,
490 const gchar *buf);
493 * nice_agent_get_local_candidates:
494 * @agent: The #NiceAgent Object
495 * @stream_id: The ID of the stream
496 * @component_id: The ID of the component
498 * Retreive from the agent the list of all local candidates
499 * for a stream's component
501 <note>
502 <para>
503 The caller owns the returned GSList as well as the candidates contained
504 within it.
505 To get full results, the client should wait for the
506 #NiceAgent::candidates-gathering-done signal.
507 </para>
508 </note>
510 * Returns: a #GSList of #NiceCandidate objects representing
511 * the local candidates of @agent
513 GSList *
514 nice_agent_get_local_candidates (
515 NiceAgent *agent,
516 guint stream_id,
517 guint component_id);
521 * nice_agent_get_remote_candidates:
522 * @agent: The #NiceAgent Object
523 * @stream_id: The ID of the stream
524 * @component_id: The ID of the component
526 * Get a list of the remote candidates set on a stream's component
528 <note>
529 <para>
530 The caller owns the returned GSList but not the candidates
531 contained within it.
532 </para>
533 <para>
534 The list of remote candidates can change during processing.
535 The client should register for the #NiceAgent::new-remote-candidate signal
536 to get notified of new remote candidates.
537 </para>
538 </note>
540 * Returns: a #GSList of #NiceCandidates objects representing
541 * the remote candidates set on the @agent
543 GSList *
544 nice_agent_get_remote_candidates (
545 NiceAgent *agent,
546 guint stream_id,
547 guint component_id);
550 * nice_agent_restart:
551 * @agent: The #NiceAgent Object
553 * Restarts the session as defined in ICE draft 19. This function
554 * needs to be called both when initiating (ICE spec section 9.1.1.1.
555 * "ICE Restarts"), as well as when reacting (spec section 9.2.1.1.
556 * "Detecting ICE Restart") to a restart.
558 * Returns: %TRUE on success %FALSE on error
560 gboolean
561 nice_agent_restart (
562 NiceAgent *agent);
566 * nice_agent_attach_recv:
567 * @agent: The #NiceAgent Object
568 * @stream_id: The ID of stream
569 * @component_id: The ID of the component
570 * @ctx: The Glib Mainloop Context to use for listening on the component
571 * @func: The callback function to be called when data is received on
572 * the stream's component
573 * @data: user data associated with the callback
575 * Attaches the stream's component's sockets to the Glib Mainloop Context in
576 * order to be notified whenever data becomes available for a component.
578 * Returns: %TRUE on success, %FALSE if the stream or component IDs are invalid.
580 gboolean
581 nice_agent_attach_recv (
582 NiceAgent *agent,
583 guint stream_id,
584 guint component_id,
585 GMainContext *ctx,
586 NiceAgentRecvFunc func,
587 gpointer data);
591 * nice_agent_set_selected_pair:
592 * @agent: The #NiceAgent Object
593 * @stream_id: The ID of the stream
594 * @component_id: The ID of the component
595 * @lfoundation: The local foundation of the candidate to use
596 * @rfoundation: The remote foundation of the candidate to use
598 * Sets the selected candidate pair for media transmission
599 * for a given stream's component. Calling this function will
600 * disable all further ICE processing (connection check,
601 * state machine updates, etc). Note that keepalives will
602 * continue to be sent.
604 * Returns: %TRUE on success, %FALSE if the candidate pair cannot be found
606 gboolean
607 nice_agent_set_selected_pair (
608 NiceAgent *agent,
609 guint stream_id,
610 guint component_id,
611 const gchar *lfoundation,
612 const gchar *rfoundation);
615 * nice_agent_set_selected_remote_candidate:
616 * @agent: The #NiceAgent Object
617 * @stream_id: The ID of the stream
618 * @component_id: The ID of the component
619 * @candidate: The #NiceCandidate to select
621 * Sets the selected remote candidate for media transmission
622 * for a given stream's component. This is used to force the selection of
623 * a specific remote candidate even when connectivity checks are failing
624 * (e.g. non-ICE compatible candidates).
625 * Calling this function will disable all further ICE processing
626 * (connection check, state machine updates, etc). Note that keepalives will
627 * continue to be sent.
629 * Returns: %TRUE on success, %FALSE on failure
631 gboolean
632 nice_agent_set_selected_remote_candidate (
633 NiceAgent *agent,
634 guint stream_id,
635 guint component_id,
636 NiceCandidate *candidate);
640 * nice_agent_set_stream_tos:
641 * @agent: The #NiceAgent Object
642 * @stream_id: The ID of the stream
643 * @tos: The ToS to set
645 * Sets the IP_TOS and/or IPV6_TCLASS field on the stream's sockets' options
648 void nice_agent_set_stream_tos (
649 NiceAgent *agent,
650 guint stream_id,
651 gint tos);
656 * nice_agent_set_software:
657 * @agent: The #NiceAgent Object
658 * @software: The value of the SOFTWARE attribute to add.
660 * This function will set the value of the SOFTWARE attribute to be added to
661 * STUN requests, responses and error responses sent during connectivity checks.
662 * <para>
663 * The SOFTWARE attribute will only be added in the #NICE_COMPATIBILITY_DRAFT19
664 * and #NICE_COMPATIBILITY_WLM2009 compatibility modes.
665 * </para>
666 * <note>
667 <para>
668 The @software argument will be appended with the libnice version before
669 being sent.
670 </para>
671 <para>
672 The @software argument must be in UTF-8 encoding and only the first
673 128 characters will be sent.
674 </para>
675 </note>
678 void nice_agent_set_software (NiceAgent *agent, const gchar *software);
680 G_END_DECLS
682 #endif /* _AGENT_H */