NiceAgent

## NiceAgent NiceAgent — ICE agent API implementation |

## Stability Level

Stable, unless otherwise indicated

## Synopsis

    #include <agent.h>

    #define             NICE_AGENT_MAX_REMOTE_CANDIDATES
    enum                NiceComponentState;
    enum                NiceComponentType;
    enum                NiceCompatibility;
    void                (*NiceAgentRecvFunc)                (NiceAgent *agent,
                                                             guint stream_id,
                                                             guint component_id,
                                                             guint len,
                                                             gchar *buf,
                                                             gpointer user_data);
                        NiceAgent;
    NiceAgent*          nice_agent_new                      (GMainContext *ctx,
                                                             NiceCompatibility compat);
    gboolean            nice_agent_add_local_address        (NiceAgent *agent,
                                                             NiceAddress *addr);
    guint               nice_agent_add_stream               (NiceAgent *agent,
                                                             guint n_components);
    void                nice_agent_remove_stream            (NiceAgent *agent,
                                                             guint stream_id);
    gboolean            nice_agent_set_relay_info           (NiceAgent *agent,
                                                             guint stream_id,
                                                             guint component_id,
                                                             const gchar *server_ip,
                                                             guint server_port,
                                                             const gchar *username,
                                                             const gchar *password,
                                                             NiceRelayType type);
    void                nice_agent_gather_candidates        (NiceAgent *agent,
                                                             guint stream_id);
    gboolean            nice_agent_set_remote_credentials   (NiceAgent *agent,
                                                             guint stream_id,
                                                             const gchar *ufrag,
                                                             const gchar *pwd);
    gboolean            nice_agent_get_local_credentials    (NiceAgent *agent,
                                                             guint stream_id,
                                                             gchar **ufrag,
                                                             gchar **pwd);
    int                 nice_agent_set_remote_candidates    (NiceAgent *agent,
                                                             guint stream_id,
                                                             guint component_id,
                                                             const GSList *candidates);
    gint                nice_agent_send                     (NiceAgent *agent,
                                                             guint stream_id,
                                                             guint component_id,
                                                             guint len,
                                                             const gchar *buf);
    GSList*             nice_agent_get_local_candidates     (NiceAgent *agent,
                                                             guint stream_id,
                                                             guint component_id);
    GSList*             nice_agent_get_remote_candidates    (NiceAgent *agent,
                                                             guint stream_id,
                                                             guint component_id);
    gboolean            nice_agent_restart                  (NiceAgent *agent);
    gboolean            nice_agent_attach_recv              (NiceAgent *agent,
                                                             guint stream_id,
                                                             guint component_id,
                                                             GMainContext *ctx,
                                                             NiceAgentRecvFunc func,
                                                             gpointer data);
    gboolean            nice_agent_set_selected_pair        (NiceAgent *agent,
                                                             guint stream_id,
                                                             guint component_id,
                                                             const gchar *lfoundation,
                                                             const gchar *rfoundation);
    gboolean            nice_agent_set_selected_remote_candidate
                                                            (NiceAgent *agent,
                                                             guint stream_id,
                                                             guint component_id,
                                                             NiceCandidate *candidate);

## Properties

      "compatibility"            guint                 : Read / Write / Construct Only
      "controlling-mode"         gboolean              : Read / Write
      "full-mode"                gboolean              : Read / Write / Construct Only
      "main-context"             gpointer              : Read / Write / Construct Only
      "max-connectivity-checks"  guint                 : Read / Write
      "stun-pacing-timer"        guint                 : Read / Write / Construct Only
      "stun-server"              gchar*                : Read / Write
      "stun-server-port"         guint                 : Read / Write

## Signals

      "candidate-gathering-done"                       : Run Last
      "component-state-changed"                        : Run Last
      "initial-binding-request-received"               : Run Last
      "new-candidate"                                  : Run Last
      "new-remote-candidate"                           : Run Last
      "new-selected-pair"                              : Run Last

## Description

The NiceAgent is your main object when using libnice. It is the agent that will take care of everything relating to ICE. It will take care of discovering your local candidates and do connectivity checks to create a stream of data between you and your peer.

Example 1. Simple example on how to use libnice

       NiceAddress base_addr;
       guint stream_id;
       gchar buffer[] = "hello world!";
       GSList *lcands = NULL;

       // Create a nice agent
       NiceAgent *agent = nice_agent_new (NULL, NICE_COMPATIBILITY_DRAFT19);

       // Specify which local interface to use
       nice_address_set_from_string (&base_addr, "127.0.0.1");
       nice_agent_add_local_address (agent, &base_addr);

       // Connect the signals
       g_signal_connect (G_OBJECT (agent), "candidate-gathering-done",
                         G_CALLBACK (cb_candidate_gathering_done), NULL);
       g_signal_connect (G_OBJECT (lagent), "component-state-changed",
                         G_CALLBACK (cb_component_state_changed), NULL);
       g_signal_connect (G_OBJECT (lagent), "new-selected-pair",
                         G_CALLBACK (cb_new_selected_pair), NULL);

       // Create a new stream with one component and start gathering candidates
       stream_id = nice_agent_add_stream (agent, 1);
       nice_agent_gather_candidates (agent, stream_id);

       // Attach to the component to receive the data
       nice_agent_attach_recv (agent, stream_id, 1, NULL,
                              cb_nice_recv, NULL);

       // ... Wait until the signal candidate-gathering-done is fired ...
       lcands = nice_agent_get_local_candidates(agent, stream_id, 1);

       // ... Send local candidates to the peer and set the peer's remote candidates
       nice_agent_set_remote_candidates (agent, stream_id, 1, rcands);

       // ... Wait until the signal new-selected-pair is fired ...
       // Send our message!
       nice_agent_send (lagent, ls_id, 1, sizeof(buffer), buffer);

       // Anything received will be received through the cb_nice_recv callback

       // Destroy the object
       g_object_unref(agent);

## Details

### NICE_AGENT_MAX_REMOTE_CANDIDATES

    #define NICE_AGENT_MAX_REMOTE_CANDIDATES    25

A hard limit for number for remote candidates. This limit is enforced to protect against malevolent remote clients.

### enum NiceComponentState

    typedef enum
    {
      NICE_COMPONENT_STATE_DISCONNECTED,
      NICE_COMPONENT_STATE_GATHERING,
      NICE_COMPONENT_STATE_CONNECTING,
      NICE_COMPONENT_STATE_CONNECTED,
      NICE_COMPONENT_STATE_READY,
      NICE_COMPONENT_STATE_FAILED,
      NICE_COMPONENT_STATE_LAST
    } NiceComponentState;

An enum representing the state of a component. See "component-state-changed"

`NICE_COMPONENT_STATE_DISCONNECTED` | No activity scheduled
`NICE_COMPONENT_STATE_GATHERING` | Gathering local candidates
`NICE_COMPONENT_STATE_CONNECTING` | Establishing connectivity
`NICE_COMPONENT_STATE_CONNECTED` | At least one working candidate pair
`NICE_COMPONENT_STATE_READY` | ICE concluded, candidate pair selection is now final
`NICE_COMPONENT_STATE_FAILED` | Connectivity checks have been completed, but connectivity was not established
`NICE_COMPONENT_STATE_LAST` | Dummy state

### enum NiceComponentType

    typedef enum
    {
      NICE_COMPONENT_TYPE_RTP = 1,
      NICE_COMPONENT_TYPE_RTCP = 2
    } NiceComponentType;

Convenience enum representing the type of a component for use as the component_id for RTP/RTCP usages.

Example 2. Example of use.

       nice_agent_send (agent, stream_id, NICE_COMPONENT_TYPE_RTP, len, buf);

`NICE_COMPONENT_TYPE_RTP` | RTP Component type
`NICE_COMPONENT_TYPE_RTCP` | RTCP Component type

### enum NiceCompatibility

    typedef enum
    {
      NICE_COMPATIBILITY_DRAFT19 = 0,
      NICE_COMPATIBILITY_GOOGLE,
      NICE_COMPATIBILITY_MSN,
      NICE_COMPATIBILITY_LAST = NICE_COMPATIBILITY_MSN
    } NiceCompatibility;

An enum to specify which compatible specifications the NiceAgent should use. Use with `nice_agent_new()`

`NICE_COMPATIBILITY_DRAFT19` | Use compatibility for ICE Draft 19 specs
`NICE_COMPATIBILITY_GOOGLE` | Use compatibility for Google Talk specs
`NICE_COMPATIBILITY_MSN` | Use compatibility for MSN Messenger specs
`NICE_COMPATIBILITY_LAST` | Dummy last compatibility mode

### NiceAgentRecvFunc ()

    void                (*NiceAgentRecvFunc)                (NiceAgent *agent,
                                                             guint stream_id,
                                                             guint component_id,
                                                             guint len,
                                                             gchar *buf,
                                                             gpointer user_data);

Callback function when data is received on a component

`agent` : | The NiceAgent Object
`stream_id` : | The id of the stream
`component_id` : | The id of the component of the stream which received the data
`len` : | The length of the data
`buf` : | The buffer containing the data received
`user_data` : | The user data set in `nice_agent_attach_recv()`

### NiceAgent

    typedef struct {
      GObject parent;                 /**< gobject pointer */

      gboolean full_mode;             /**< property: full-mode */
      GTimeVal next_check_tv;         /**< property: next conncheck timestamp */
      gchar *stun_server_ip;          /**< property: STUN server IP */
      guint stun_server_port;         /**< property: STUN server port */
      gboolean controlling_mode;      /**< property: controlling-mode */
      guint timer_ta;                 /**< property: timer Ta */
      guint max_conn_checks;          /**< property: max connectivity checks */

      GSList *local_addresses;        /**< list of NiceAddresses for local
    				     interfaces */
      GSList *streams;                /**< list of Stream objects */
      GMainContext *main_context;     /**< main context pointer */
      guint next_candidate_id;        /**< id of next created candidate */
      guint next_stream_id;           /**< id of next created candidate */
      NiceRNG *rng;                   /**< random number generator */
      GSList *discovery_list;         /**< list of CandidateDiscovery items */
      guint discovery_unsched_items;  /**< number of discovery items unscheduled */
      GSource *discovery_timer_source; /**< source of discovery timer */
      GSource *conncheck_timer_source; /**< source of conncheck timer */
      GSource *keepalive_timer_source; /**< source of keepalive timer */
      GSList *refresh_list;         /**< list of CandidateRefresh items */
      guint64 tie_breaker;            /**< tie breaker (ICE sect 5.2
    				     "Determining Role" ID-19) */
      GStaticRecMutex mutex;                 /* Mutex used for thread-safe lib */
      NiceCompatibility compatibility; /* property: Compatibility mode */
      StunAgent stun_agent;            /* STUN agent */
      /* XXX: add pointer to internal data struct for ABI-safe extensions */
    } NiceAgent;

The NiceAgent is the main GObject of the libnice library and represents the ICE agent.

### nice_agent_new ()

    NiceAgent*          nice_agent_new                      (GMainContext *ctx,
                                                             NiceCompatibility compat);

Create a new NiceAgent. The returned object must be freed with `g_object_unref()`

`ctx` : | The Glib Mainloop Context to use for timers
`compat` : | The compatibility mode of the agent
Returns : | The new agent GObject

### nice_agent_add_local_address ()

    gboolean            nice_agent_add_local_address        (NiceAgent *agent,
                                                             NiceAddress *addr);

Add a local address from which to derive local host candidates

`agent` : | The NiceAgent Object
`addr` : | The address to listen to If the port is 0, then a random port will be chosen by the system
Returns : | `TRUE` on success, `FALSE` on fatal (memory allocation) errors

### nice_agent_add_stream ()

    guint               nice_agent_add_stream               (NiceAgent *agent,
                                                             guint n_components);

Adds a data stream to `agent` containing `n_components` components.

`agent` : | The NiceAgent Object
`n_components` : | The number of components to add to the stream
Returns : | The ID of the new stream, 0 on failure

### nice_agent_remove_stream ()

    void                nice_agent_remove_stream            (NiceAgent *agent,
                                                             guint stream_id);

Remove and free a previously created data stream from `agent`

`agent` : | The NiceAgent Object
`stream_id` : | The ID of the stream to remove

### nice_agent_set_relay_info ()

    gboolean            nice_agent_set_relay_info           (NiceAgent *agent,
                                                             guint stream_id,
                                                             guint component_id,
                                                             const gchar *server_ip,
                                                             guint server_port,
                                                             const gchar *username,
                                                             const gchar *password,
                                                             NiceRelayType type);

Sets the settings for using a relay server during the candidate discovery.

`agent` : | The NiceAgent Object
`stream_id` : | The ID of the stream
`component_id` : | The ID of the component
`server_ip` : | The IP address of the TURN server
`server_port` : | The port of the TURN server
`username` : | The TURN username to use for the allocate
`password` : | The TURN password to use for the allocate
`type` : | The type of relay to use
Returns : | `TRUE` if the TURN settings were accepted. `FALSE` if the address was invalid.

### nice_agent_gather_candidates ()

    void                nice_agent_gather_candidates        (NiceAgent *agent,
                                                             guint stream_id);

Start the candidate gathering process. Once done, "candidate-gathering-done" is called for the stream

### Note

Local addresses must be previously set with `nice_agent_add_local_address()`

`agent` : | The NiceAgent Object
`stream_id` : | The id of the stream to start

### nice_agent_set_remote_credentials ()

    gboolean            nice_agent_set_remote_credentials   (NiceAgent *agent,
                                                             guint stream_id,
                                                             const gchar *ufrag,
                                                             const gchar *pwd);

Sets the remote credentials for stream `stream_id`.

### Note

Stream credentials do not override per-candidate credentials if set

`agent` : | The NiceAgent Object
`stream_id` : | The ID of the stream
`ufrag` : | NULL-terminated string containing an ICE username fragment (length must be between 22 and 256 chars)
`pwd` : | NULL-terminated string containing an ICE password (length must be between 4 and 256 chars)
Returns : | `TRUE` on success, `FALSE` on error.

### nice_agent_get_local_credentials ()

    gboolean            nice_agent_get_local_credentials    (NiceAgent *agent,
                                                             guint stream_id,
                                                             gchar **ufrag,
                                                             gchar **pwd);

Gets the local credentials for stream `stream_id`.

`agent` : | The NiceAgent Object
`stream_id` : | The ID of the stream
`ufrag` : | a pointer to a NULL-terminated string containing an ICE username fragment [OUT]. This string must be freed with `g_free()`
`pwd` : | a pointer to a NULL-terminated string containing an ICE password [OUT] This string must be freed with `g_free()`
Returns : | `TRUE` on success, `FALSE` on error.

### nice_agent_set_remote_candidates ()

    int                 nice_agent_set_remote_candidates    (NiceAgent *agent,
                                                             guint stream_id,
                                                             guint component_id,
                                                             const GSList *candidates);

Sets, adds or updates the remote candidates for a component of a stream.

### Note

NICE_AGENT_MAX_REMOTE_CANDIDATES is the absolute maximum limit for remote candidates.

You must first call `nice_agent_gather_candidates()` and wait for the "candidate-gathering-done" signale before calling `nice_agent_set_remote_candidates()`

`agent` : | The NiceAgent Object
`stream_id` : | The ID of the stream the candidates are for
`component_id` : | The ID of the component the candidates are for
`candidates` : | a GList of NiceCandidate items describing each candidate to add
Returns : | The number of candidates added, negative on errors (memory allocation or if the local candidates are not done gathering yet)

### nice_agent_send ()

    gint                nice_agent_send                     (NiceAgent *agent,
                                                             guint stream_id,
                                                             guint component_id,
                                                             guint len,
                                                             const gchar *buf);

Sends a data payload over a stream's component.

### Note

Component state MUST be NICE_COMPONENT_STATE_READY, or as a special case, in any state if component was in READY state before and was then restarted

`agent` : | The NiceAgent Object
`stream_id` : | The ID of the stream to send to
`component_id` : | The ID of the component to send to
`len` : | The length of the buffer to send
`buf` : | The buffer of data to send
Returns : | The number of bytes sent, or negative error code

### nice_agent_get_local_candidates ()

    GSList*             nice_agent_get_local_candidates     (NiceAgent *agent,
                                                             guint stream_id,
                                                             guint component_id);

Retreive from the agent the list of all local candidates for a stream's component

### Note

The caller owns the returned GSList as well as the candidates contained within it. To get full results, the client should wait for the "candidates-gathering-done" signal.

`agent` : | The NiceAgent Object
`stream_id` : | The ID of the stream
`component_id` : | The ID of the component
Returns : | a GSList of NiceCandidate objects representing the local candidates of `agent`

### nice_agent_get_remote_candidates ()

    GSList*             nice_agent_get_remote_candidates    (NiceAgent *agent,
                                                             guint stream_id,
                                                             guint component_id);

Get a list of the remote candidates set on a stream's component

### Note

The caller owns the returned GSList but not the candidates contained within it.

The list of remote candidates can change during processing. The client should register for the "new-remote-candidate" signal to get notified of new remote candidates.

`agent` : | The NiceAgent Object
`stream_id` : | The ID of the stream
`component_id` : | The ID of the component
Returns : | a GSList of NiceCandidates objects representing the remote candidates set on the `agent`

### nice_agent_restart ()

    gboolean            nice_agent_restart                  (NiceAgent *agent);

Restarts the session as defined in ICE draft 19. This function needs to be called both when initiating (ICE spec section 9.1.1.1. "ICE Restarts"), as well as when reacting (spec section 9.2.1.1. "Detecting ICE Restart") to a restart.

`agent` : | The NiceAgent Object
Returns : | `TRUE` on success `FALSE` on error

### nice_agent_attach_recv ()

    gboolean            nice_agent_attach_recv              (NiceAgent *agent,
                                                             guint stream_id,
                                                             guint component_id,
                                                             GMainContext *ctx,
                                                             NiceAgentRecvFunc func,
                                                             gpointer data);

Attaches the stream's component's sockets to the Glib Mainloop Context in order to be notified whenever data becomes available for a component.

`agent` : | The NiceAgent Object
`stream_id` : | The ID of stream
`component_id` : | The ID of the component
`ctx` : | The Glib Mainloop Context to use for listening on the component
`func` : | The callback function to be called when data is received on the stream's component
`data` : | user data associated with the callback
Returns : | `TRUE` on success, `FALSE` if the stream or component IDs are invalid.

### nice_agent_set_selected_pair ()

    gboolean            nice_agent_set_selected_pair        (NiceAgent *agent,
                                                             guint stream_id,
                                                             guint component_id,
                                                             const gchar *lfoundation,
                                                             const gchar *rfoundation);

Sets the selected candidate pair for media transmission for a given stream's component. Calling this function will disable all further ICE processing (connection check, state machine updates, etc). Note that keepalives will continue to be sent.

`agent` : | The NiceAgent Object
`stream_id` : | The ID of the stream
`component_id` : | The ID of the component
`lfoundation` : | The local foundation of the candidate to use
`rfoundation` : | The remote foundation of the candidate to use
Returns : | `TRUE` on success, `FALSE` if the candidate pair cannot be found

### nice_agent_set_selected_remote_candidate ()

    gboolean            nice_agent_set_selected_remote_candidate
                                                            (NiceAgent *agent,
                                                             guint stream_id,
                                                             guint component_id,
                                                             NiceCandidate *candidate);

Sets the selected remote candidate for media transmission for a given stream's component. This is used to force the selection of a specific remote candidate even when connectivity checks are failing (e.g. non-ICE compatible candidates). Calling this function will disable all further ICE processing (connection check, state machine updates, etc). Note that keepalives will continue to be sent.

`agent` : | The NiceAgent Object
`stream_id` : | The ID of the stream
`component_id` : | The ID of the component
`candidate` : | The NiceCandidate to select
Returns : | `TRUE` on success, `FALSE` on failure

## Property Details

### The `"compatibility"` property

      "compatibility"            guint                 : Read / Write / Construct Only

The Nice agent can work in various compatibility modes depending on what the application/peer needs.

See also: NiceCompatibility

Allowed values: <= 2

Default value: 0

### The `"controlling-mode"` property

      "controlling-mode"         gboolean              : Read / Write

Whether the agent is in controlling mode.

Default value: FALSE

### The `"full-mode"` property

      "full-mode"                gboolean              : Read / Write / Construct Only

Whether agent runs in ICE full mode.

Default value: TRUE

### The `"main-context"` property

      "main-context"             gpointer              : Read / Write / Construct Only

A GLib main context is needed for all timeouts used by libnice. This is a property being set by the `nice_agent_new()` call.

### The `"max-connectivity-checks"` property

      "max-connectivity-checks"  guint                 : Read / Write

Upper limit for the total number of connectivity checks performed.

Default value: 0

### The `"stun-pacing-timer"` property

      "stun-pacing-timer"        guint                 : Read / Write / Construct Only

Timer 'Ta' (msecs) used in the IETF ICE specification for pacing candidate gathering and sending of connectivity checks.

Allowed values: >= 1

Default value: 20

### The `"stun-server"` property

      "stun-server"              gchar*                : Read / Write

The STUN server used to obtain server-reflexive candidates.

Default value: NULL

### The `"stun-server-port"` property

      "stun-server-port"         guint                 : Read / Write

The STUN server used to obtain server-reflexive candidates.

Allowed values: [1,65536]

Default value: 1

## Signal Details

### The `"candidate-gathering-done"` signal

    void                user_function                      (NiceAgent *stream_id,
                                                            guint      arg1,
                                                            gpointer   user_data)      : Run Last

This signal is fired whenever a stream has finished gathering its candidates after a call to `nice_agent_gather_candidates()`

`stream_id` : | The ID of the stream
`user_data` : | user data set when the signal handler was connected.

### The `"component-state-changed"` signal

    void                user_function                      (NiceAgent *stream_id,
                                                            guint      component_id,
                                                            guint      state,
                                                            guint      arg3,
                                                            gpointer   user_data)         : Run Last

This signal is fired whenever a component's state changes

`stream_id` : | The ID of the stream
`component_id` : | The ID of the component
`state` : | The NiceComponentState of the component
`user_data` : | user data set when the signal handler was connected.

### The `"initial-binding-request-received"` signal

    void                user_function                      (NiceAgent *stream_id,
                                                            guint      arg1,
                                                            gpointer   user_data)      : Run Last

This signal is fired when we received our first binding request from the peer.

`stream_id` : | The ID of the stream
`user_data` : | user data set when the signal handler was connected.

### The `"new-candidate"` signal

    void                user_function                      (NiceAgent *stream_id,
                                                            guint      component_id,
                                                            guint      foundation,
                                                            gchar     *arg3,
                                                            gpointer   user_data)         : Run Last

This signal is fired when the agent discovers a new candidate

See also: "candidates-gathering-done"

`stream_id` : | The ID of the stream
`component_id` : | The ID of the component
`foundation` : | The foundation of the new candidate
`user_data` : | user data set when the signal handler was connected.

### The `"new-remote-candidate"` signal

    void                user_function                      (NiceAgent *stream_id,
                                                            guint      component_id,
                                                            guint      foundation,
                                                            gchar     *arg3,
                                                            gpointer   user_data)         : Run Last

This signal is fired when the agent discovers a new remote candidate. This can happen with peer reflexive candidates.

See also: "candidates-gathering-done"

`stream_id` : | The ID of the stream
`component_id` : | The ID of the component
`foundation` : | The foundation of the new candidate
`user_data` : | user data set when the signal handler was connected.

### The `"new-selected-pair"` signal

    void                user_function                      (NiceAgent *stream_id,
                                                            guint      component_id,
                                                            guint      lfoundation,
                                                            gchar     *rfoundation,
                                                            gchar     *arg4,
                                                            gpointer   user_data)         : Run Last

This signal is fired once a candidate pair is selected for data transfer for a stream's component

`stream_id` : | The ID of the stream
`component_id` : | The ID of the component
`lfoundation` : | The local foundation of the selected candidate pair
`rfoundation` : | The remote foundation of the selected candidate pair
`user_data` : | user data set when the signal handler was connected.

## See Also

NiceAddress