agent.h 34.2 KB
Newer Older
1 2 3
/*
 * This file is part of the Nice GLib ICE library.
 *
4 5 6
 * (C) 2006-2010 Collabora Ltd.
 *  Contact: Youness Alaoui
 * (C) 2006-2010 Nokia Corporation. All rights reserved.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *  Contact: Kai Vehmanen
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the Nice GLib ICE library.
 *
 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
 * Corporation. All Rights Reserved.
 *
 * Contributors:
 *   Dafydd Harries, Collabora Ltd.
26
 *   Youness Alaoui, Collabora Ltd.
27
 *   Kai Vehmanen, Nokia
28 29 30 31 32 33 34 35 36 37 38
 *
 * Alternatively, the contents of this file may be used under the terms of the
 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
 * case the provisions of LGPL are applicable instead of those above. If you
 * wish to allow use of your version of this file only under the terms of the
 * LGPL and not to allow others to use your version of this file under the
 * MPL, indicate your decision by deleting the provisions above and replace
 * them with the notice and other provisions required by the LGPL. If you do
 * not delete the provisions above, a recipient may use your version of this
 * file under either the MPL or the LGPL.
 */
Dafydd Harries's avatar
Dafydd Harries committed
39 40 41 42

#ifndef _AGENT_H
#define _AGENT_H

43 44 45 46 47 48 49 50 51 52 53 54 55
/**
 * SECTION:agent
 * @short_description:  ICE agent API implementation
 * @see_also: #NiceCandidate
 * @see_also: #NiceAddress
 * @include: agent.h
 * @stability: Stable
 *
 * 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.
 *
56 57 58 59
 * Streams and their components are referenced by integer IDs (with respect to a
 * given #NiceAgent). These IDs are guaranteed to be positive (i.e. non-zero)
 * for valid streams/components.
 *
60 61 62 63 64 65 66 67 68 69 70 71
 * Each stream can receive data in one of two ways: using
 * nice_agent_attach_recv() or nice_agent_recv() (and the derived
 * #NiceInputStream and #NiceIOStream classes accessible using
 * nice_agent_build_io_stream()). nice_agent_attach_recv() is non-blocking: it
 * takes a user-provided callback function and attaches the stream’s socket to
 * the provided #GMainContext, invoking the callback in that context for every
 * packet received. nice_agent_recv() instead blocks on receiving a packet, and
 * writes it directly into a user-provided buffer. This reduces the number of
 * callback invokations and (potentially) buffer copies required to receive
 * packets. nice_agent_recv() (or #NiceInputStream) is designed to be used in a
 * blocking loop in a separate thread.
 *
72 73 74 75 76 77 78 79
 <example>
   <title>Simple example on how to use libnice</title>
   <programlisting>
   guint stream_id;
   gchar buffer[] = "hello world!";
   GSList *lcands = NULL;

   // Create a nice agent
80
   NiceAgent *agent = nice_agent_new (NULL, NICE_COMPATIBILITY_RFC5245);
81 82 83 84

   // Connect the signals
   g_signal_connect (G_OBJECT (agent), "candidate-gathering-done",
                     G_CALLBACK (cb_candidate_gathering_done), NULL);
85
   g_signal_connect (G_OBJECT (agent), "component-state-changed",
86
                     G_CALLBACK (cb_component_state_changed), NULL);
87
   g_signal_connect (G_OBJECT (agent), "new-selected-pair",
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
                     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!
106
   nice_agent_send (agent, stream_id, 1, sizeof(buffer), buffer);
107 108 109 110 111 112 113 114

   // Anything received will be received through the cb_nice_recv callback

   // Destroy the object
   g_object_unref(agent);

   </programlisting>
 </example>
Youness Alaoui's avatar
Youness Alaoui committed
115 116 117 118
 *
 * Refer to the examples in the examples/ subdirectory of the libnice source for
 * complete examples.
 *
119 120 121
 */


Dafydd Harries's avatar
Dafydd Harries committed
122
#include <glib-object.h>
123
#include <gio/gio.h>
124

125 126 127 128 129 130
/**
 * NiceAgent:
 *
 * The #NiceAgent is the main GObject of the libnice library and represents
 * the ICE agent.
 */
131 132
typedef struct _NiceAgent NiceAgent;

133
#include "address.h"
Dafydd Harries's avatar
Dafydd Harries committed
134
#include "candidate.h"
135
#include "debug.h"
136

137

Dafydd Harries's avatar
Dafydd Harries committed
138
G_BEGIN_DECLS
139

Dafydd Harries's avatar
Dafydd Harries committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
#define NICE_TYPE_AGENT nice_agent_get_type()

#define NICE_AGENT(obj) \
  (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
  NICE_TYPE_AGENT, NiceAgent))

#define NICE_AGENT_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_CAST ((klass), \
  NICE_TYPE_AGENT, NiceAgentClass))

#define NICE_IS_AGENT(obj) \
  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
  NICE_TYPE_AGENT))

#define NICE_IS_AGENT_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_TYPE ((klass), \
  NICE_TYPE_AGENT))

#define NICE_AGENT_GET_CLASS(obj) \
  (G_TYPE_INSTANCE_GET_CLASS ((obj), \
  NICE_TYPE_AGENT, NiceAgentClass))

Youness Alaoui's avatar
Youness Alaoui committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175
typedef struct _NiceAgentClass NiceAgentClass;

struct _NiceAgentClass
{
  GObjectClass parent_class;
};


GType nice_agent_get_type (void);


/**
 * NICE_AGENT_MAX_REMOTE_CANDIDATES:
 *
176
 * A hard limit for the number of remote candidates. This
Youness Alaoui's avatar
Youness Alaoui committed
177
 * limit is enforced to protect against malevolent remote
178 179 180 181
 * clients.
 */
#define NICE_AGENT_MAX_REMOTE_CANDIDATES    25

Youness Alaoui's avatar
Youness Alaoui committed
182 183 184 185 186 187 188 189 190 191
/**
 * NiceComponentState:
 * @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
192
 * @NICE_COMPONENT_STATE_LAST: Dummy state
Youness Alaoui's avatar
Youness Alaoui committed
193 194
 *
 * An enum representing the state of a component.
195
 * <para> See also: #NiceAgent::component-state-changed </para>
Youness Alaoui's avatar
Youness Alaoui committed
196
 */
197 198
typedef enum
{
Youness Alaoui's avatar
Youness Alaoui committed
199 200 201 202 203 204
  NICE_COMPONENT_STATE_DISCONNECTED,
  NICE_COMPONENT_STATE_GATHERING,
  NICE_COMPONENT_STATE_CONNECTING,
  NICE_COMPONENT_STATE_CONNECTED,
  NICE_COMPONENT_STATE_READY,
  NICE_COMPONENT_STATE_FAILED,
Youness Alaoui's avatar
Youness Alaoui committed
205
  NICE_COMPONENT_STATE_LAST
206 207
} NiceComponentState;

Youness Alaoui's avatar
Youness Alaoui committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

/**
 * NiceComponentType:
 * @NICE_COMPONENT_TYPE_RTP: RTP Component type
 * @NICE_COMPONENT_TYPE_RTCP: RTCP Component type
 *
 * Convenience enum representing the type of a component for use as the
 * component_id for RTP/RTCP usages.
 <example>
   <title>Example of use.</title>
   <programlisting>
   nice_agent_send (agent, stream_id, NICE_COMPONENT_TYPE_RTP, len, buf);
   </programlisting>
  </example>
 */
223 224 225 226 227 228
typedef enum
{
  NICE_COMPONENT_TYPE_RTP = 1,
  NICE_COMPONENT_TYPE_RTCP = 2
} NiceComponentType;

229

Youness Alaoui's avatar
Youness Alaoui committed
230 231
/**
 * NiceCompatibility:
232
 * @NICE_COMPATIBILITY_RFC5245: Use compatibility with the RFC5245 ICE specs
Youness Alaoui's avatar
Youness Alaoui committed
233 234
 * @NICE_COMPATIBILITY_GOOGLE: Use compatibility for Google Talk specs
 * @NICE_COMPATIBILITY_MSN: Use compatibility for MSN Messenger specs
Youness Alaoui's avatar
Youness Alaoui committed
235 236
 * @NICE_COMPATIBILITY_WLM2009: Use compatibility with Windows Live Messenger
 * 2009
Jakub Adam's avatar
Jakub Adam committed
237
 * @NICE_COMPATIBILITY_OC2007: Use compatibility with Microsoft Office Communicator 2007
Jakub Adam's avatar
Jakub Adam committed
238
 * @NICE_COMPATIBILITY_OC2007R2: Use compatibility with Microsoft Office Communicator 2007 R2
239
 * @NICE_COMPATIBILITY_DRAFT19: Use compatibility for ICE Draft 19 specs
240
 * @NICE_COMPATIBILITY_LAST: Dummy last compatibility mode
Youness Alaoui's avatar
Youness Alaoui committed
241 242 243
 *
 * An enum to specify which compatible specifications the #NiceAgent should use.
 * Use with nice_agent_new()
244 245 246 247
 *
 * <warning>@NICE_COMPATIBILITY_DRAFT19 is deprecated and should not be used
 * in newly-written code. It is kept for compatibility reasons and
 * represents the same compatibility as @NICE_COMPATIBILITY_RFC5245 </warning>
Youness Alaoui's avatar
Youness Alaoui committed
248
 */
249 250
typedef enum
{
251
  NICE_COMPATIBILITY_RFC5245 = 0,
252 253
  NICE_COMPATIBILITY_GOOGLE,
  NICE_COMPATIBILITY_MSN,
254
  NICE_COMPATIBILITY_WLM2009,
Jakub Adam's avatar
Jakub Adam committed
255
  NICE_COMPATIBILITY_OC2007,
Jakub Adam's avatar
Jakub Adam committed
256
  NICE_COMPATIBILITY_OC2007R2,
257
  NICE_COMPATIBILITY_DRAFT19 = NICE_COMPATIBILITY_RFC5245,
Jakub Adam's avatar
Jakub Adam committed
258
  NICE_COMPATIBILITY_LAST = NICE_COMPATIBILITY_OC2007R2,
259 260
} NiceCompatibility;

261 262 263 264 265 266 267 268 269
/**
 * NiceProxyType:
 * @NICE_PROXY_TYPE_NONE: Do not use a proxy
 * @NICE_PROXY_TYPE_SOCKS5: Use a SOCKS5 proxy
 * @NICE_PROXY_TYPE_HTTP: Use an HTTP proxy
 * @NICE_PROXY_TYPE_LAST: Dummy last proxy type
 *
 * An enum to specify which proxy type to use for relaying.
 * Note that the proxies will only be used with TCP TURN relaying.
270
 * <para> See also: #NiceAgent:proxy-type </para>
271 272
 *
 * Since: 0.0.4
273 274 275 276 277 278 279 280 281 282
 */
typedef enum
{
  NICE_PROXY_TYPE_NONE = 0,
  NICE_PROXY_TYPE_SOCKS5,
  NICE_PROXY_TYPE_HTTP,
  NICE_PROXY_TYPE_LAST = NICE_PROXY_TYPE_HTTP,
} NiceProxyType;


Youness Alaoui's avatar
Youness Alaoui committed
283 284 285 286 287 288 289 290 291 292 293 294 295
/**
 * NiceAgentRecvFunc:
 * @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()
 *
 * Callback function when data is received on a component
 *
 */
296 297 298 299
typedef void (*NiceAgentRecvFunc) (
  NiceAgent *agent, guint stream_id, guint component_id, guint len,
  gchar *buf, gpointer user_data);

Dafydd Harries's avatar
Dafydd Harries committed
300

Youness Alaoui's avatar
Youness Alaoui committed
301 302 303 304 305 306
/**
 * nice_agent_new:
 * @ctx: The Glib Mainloop Context to use for timers
 * @compat: The compatibility mode of the agent
 *
 * Create a new #NiceAgent.
307
 * The returned object must be freed with g_object_unref()
Youness Alaoui's avatar
Youness Alaoui committed
308
 *
309
 * Returns: The new agent GObject
Youness Alaoui's avatar
Youness Alaoui committed
310
 */
311
NiceAgent *
312
nice_agent_new (GMainContext *ctx, NiceCompatibility compat);
313

314 315 316 317 318 319 320 321 322

/**
 * nice_agent_new_reliable:
 * @ctx: The Glib Mainloop Context to use for timers
 * @compat: The compatibility mode of the agent
 *
 * Create a new #NiceAgent in reliable mode, which uses #PseudoTcpSocket to
 * assure reliability of the messages.
 * The returned object must be freed with g_object_unref()
323
 * <para> See also: #NiceAgent::reliable-transport-writable </para>
324
 *
325 326
 * Since: 0.0.11
 *
327 328 329 330 331
 * Returns: The new agent GObject
 */
NiceAgent *
nice_agent_new_reliable (GMainContext *ctx, NiceCompatibility compat);

Youness Alaoui's avatar
Youness Alaoui committed
332 333 334 335 336 337
/**
 * nice_agent_add_local_address:
 * @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
 *
338 339 340 341 342 343
 * Add a local address from which to derive local host candidates for
 * candidate gathering.
 * <para>
 * Since 0.0.5, if this method is not called, libnice will automatically
 * discover the local addresses available
 * </para>
Youness Alaoui's avatar
Youness Alaoui committed
344
 *
345
 * See also: nice_agent_gather_candidates()
Youness Alaoui's avatar
Youness Alaoui committed
346 347
 * Returns: %TRUE on success, %FALSE on fatal (memory allocation) errors
 */
348
gboolean
349
nice_agent_add_local_address (NiceAgent *agent, NiceAddress *addr);
350

Youness Alaoui's avatar
Youness Alaoui committed
351 352 353 354 355 356

/**
 * nice_agent_add_stream:
 * @agent: The #NiceAgent Object
 * @n_components: The number of components to add to the stream
 *
357 358
 * Adds a data stream to @agent containing @n_components components. The
 * returned stream ID is guaranteed to be positive on success.
Youness Alaoui's avatar
Youness Alaoui committed
359 360 361
 *
 * Returns: The ID of the new stream, 0 on failure
 **/
Dafydd Harries's avatar
Dafydd Harries committed
362
guint
363 364
nice_agent_add_stream (
  NiceAgent *agent,
365
  guint n_components);
366

Youness Alaoui's avatar
Youness Alaoui committed
367 368 369 370 371 372 373 374
/**
 * nice_agent_remove_stream:
 * @agent: The #NiceAgent Object
 * @stream_id: The ID of the stream to remove
 *
 * Remove and free a previously created data stream from @agent
 *
 **/
Dafydd Harries's avatar
Dafydd Harries committed
375 376 377 378 379
void
nice_agent_remove_stream (
  NiceAgent *agent,
  guint stream_id);

380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406

/**
 * nice_agent_set_port_range:
 * @agent: The #NiceAgent Object
 * @stream_id: The ID of the stream
 * @component_id: The ID of the component
 * @min_port: The minimum port to use
 * @max_port: The maximum port to use
 *
 * Sets a preferred port range for allocating host candidates.
 * <para>
 * If a local host candidate cannot be created on that port
 * range, then the nice_agent_gather_candidates() call will fail.
 * </para>
 * <para>
 * This MUST be called before nice_agent_gather_candidates()
 * </para>
 *
 */
void
nice_agent_set_port_range (
    NiceAgent *agent,
    guint stream_id,
    guint component_id,
    guint min_port,
    guint max_port);

Youness Alaoui's avatar
Youness Alaoui committed
407 408 409 410 411 412 413 414 415 416 417 418
/**
 * nice_agent_set_relay_info:
 * @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
 *
 * Sets the settings for using a relay server during the candidate discovery.
419 420
 * This may be called multiple times to add multiple relay servers to the
 * discovery process; one TCP and one UDP, for example.
Youness Alaoui's avatar
Youness Alaoui committed
421 422 423 424
 *
 * Returns: %TRUE if the TURN settings were accepted.
 * %FALSE if the address was invalid.
 */
425
gboolean nice_agent_set_relay_info(
426 427 428 429 430 431
    NiceAgent *agent,
    guint stream_id,
    guint component_id,
    const gchar *server_ip,
    guint server_port,
    const gchar *username,
432 433
    const gchar *password,
    NiceRelayType type);
434

Youness Alaoui's avatar
Youness Alaoui committed
435 436 437 438 439 440 441 442
/**
 * nice_agent_gather_candidates:
 * @agent: The #NiceAgent Object
 * @stream_id: The id of the stream to start
 *
 * Start the candidate gathering process.
 * Once done, #NiceAgent::candidate-gathering-done is called for the stream
 *
443 444 445 446 447
 * <para>See also: nice_agent_add_local_address()</para>
 * <para>See also: nice_agent_set_port_range()</para>
 *
 * Returns: %FALSE if the stream id is invalid or if a host candidate couldn't be allocated
 * on the requested interfaces/ports.
448
 *
Youness Alaoui's avatar
Youness Alaoui committed
449 450
 <note>
   <para>
451 452 453
    Local addresses can be previously set with nice_agent_add_local_address()
  </para>
  <para>
454 455 456
    Since 0.0.5, If no local address was previously added, then the nice agent
    will automatically detect the local address using
    nice_interfaces_get_local_ips()
Youness Alaoui's avatar
Youness Alaoui committed
457 458 459
   </para>
 </note>
 */
460
gboolean
461 462 463 464
nice_agent_gather_candidates (
  NiceAgent *agent,
  guint stream_id);

Youness Alaoui's avatar
Youness Alaoui committed
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
/**
 * nice_agent_set_remote_credentials:
 * @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)
 *
 * Sets the remote credentials for stream @stream_id.
 *
 <note>
   <para>
     Stream credentials do not override per-candidate credentials if set
   </para>
 </note>
 *
 * Returns: %TRUE on success, %FALSE on error.
 */
484 485 486 487 488 489
gboolean
nice_agent_set_remote_credentials (
  NiceAgent *agent,
  guint stream_id,
  const gchar *ufrag, const gchar *pwd);

Youness Alaoui's avatar
Youness Alaoui committed
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506


/**
 * nice_agent_get_local_credentials:
 * @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()
 *
 * Gets the local credentials for stream @stream_id.
 *
 * Returns: %TRUE on success, %FALSE on error.
 */
507 508 509 510
gboolean
nice_agent_get_local_credentials (
  NiceAgent *agent,
  guint stream_id,
511
  gchar **ufrag, gchar **pwd);
512

Youness Alaoui's avatar
Youness Alaoui committed
513 514 515 516 517
/**
 * 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
518
 * @candidates: a #GSList of #NiceCandidate items describing each candidate to add
Youness Alaoui's avatar
Youness Alaoui committed
519
 *
Youness Alaoui's avatar
Youness Alaoui committed
520
 * Sets, adds or updates the remote candidates for a component of a stream.
Youness Alaoui's avatar
Youness Alaoui committed
521 522 523
 *
 <note>
   <para>
524
    NICE_AGENT_MAX_REMOTE_CANDIDATES is the absolute maximum limit
Youness Alaoui's avatar
Youness Alaoui committed
525 526 527
    for remote candidates.
   </para>
   <para>
Youness Alaoui's avatar
fix doc  
Youness Alaoui committed
528 529
   You must first call nice_agent_gather_candidates() and wait for the
   #NiceAgent::candidate-gathering-done signale before
Youness Alaoui's avatar
Youness Alaoui committed
530
   calling nice_agent_set_remote_candidates()
Youness Alaoui's avatar
Youness Alaoui committed
531
   </para>
532 533 534 535 536 537
   <para>
    Since 0.1.3, there is no need to wait for the candidate-gathering-done signal.
    Remote candidates can be set even while gathering local candidates.
    Newly discovered local candidates will automatically be paired with
    existing remote candidates.
   </para>
Youness Alaoui's avatar
Youness Alaoui committed
538 539
 </note>
 *
Youness Alaoui's avatar
Youness Alaoui committed
540 541
 * Returns: The number of candidates added, negative on errors (memory
 * allocation error or invalid component)
Youness Alaoui's avatar
Youness Alaoui committed
542
 **/
543
int
544 545 546 547
nice_agent_set_remote_candidates (
  NiceAgent *agent,
  guint stream_id,
  guint component_id,
548
  const GSList *candidates);
549

Youness Alaoui's avatar
Youness Alaoui committed
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565

/**
 * nice_agent_send:
 * @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
 *
 * Sends a data payload over a stream's component.
 *
 <note>
   <para>
     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
   </para>
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
   <para>
   In reliable mode, the -1 error value means either that you are not yet
   connected or that the send buffer is full (equivalent to EWOULDBLOCK).
   In both cases, you simply need to wait for the
   #NiceAgent::reliable-transport-writable signal to be fired before resending
   the data.
   </para>
   <para>
   In non-reliable mode, it will virtually never happen with UDP sockets, but
   it might happen if the active candidate is a TURN-TCP connection that got
   disconnected.
   </para>
   <para>
   In both reliable and non-reliable mode, a -1 error code could also mean that
   the stream_id and/or component_id are invalid.
   </para>
</note>
Youness Alaoui's avatar
Youness Alaoui committed
583 584 585
 *
 * Returns: The number of bytes sent, or negative error code
 */
586
gint
Dafydd Harries's avatar
Dafydd Harries committed
587 588 589 590 591
nice_agent_send (
  NiceAgent *agent,
  guint stream_id,
  guint component_id,
  guint len,
592
  const gchar *buf);
Dafydd Harries's avatar
Dafydd Harries committed
593

594 595 596 597 598 599 600 601 602 603
gssize
nice_agent_send_full (
    NiceAgent *agent,
    guint stream_id,
    guint component_id,
    const guint8 *buf,
    gsize buf_len,
    GCancellable *cancellable,
    GError **error);

Youness Alaoui's avatar
Youness Alaoui committed
604 605 606 607 608 609
/**
 * nice_agent_get_local_candidates:
 * @agent: The #NiceAgent Object
 * @stream_id: The ID of the stream
 * @component_id: The ID of the component
 *
610 611
 * Retreive from the agent the list of all local candidates
 * for a stream's component
Youness Alaoui's avatar
Youness Alaoui committed
612 613 614 615 616 617
 *
 <note>
   <para>
     The caller owns the returned GSList as well as the candidates contained
     within it.
     To get full results, the client should wait for the
618
     #NiceAgent::candidate-gathering-done signal.
Youness Alaoui's avatar
Youness Alaoui committed
619 620 621 622 623 624
   </para>
 </note>
 *
 * Returns: a #GSList of #NiceCandidate objects representing
 * the local candidates of @agent
 **/
625
GSList *
626
nice_agent_get_local_candidates (
627 628 629
  NiceAgent *agent,
  guint stream_id,
  guint component_id);
Dafydd Harries's avatar
Dafydd Harries committed
630

Youness Alaoui's avatar
Youness Alaoui committed
631 632 633 634 635 636 637 638 639 640 641

/**
 * nice_agent_get_remote_candidates:
 * @agent: The #NiceAgent Object
 * @stream_id: The ID of the stream
 * @component_id: The ID of the component
 *
 * Get a list of the remote candidates set on a stream's component
 *
 <note>
   <para>
642 643
     The caller owns the returned GSList as well as the candidates contained
     within it.
Youness Alaoui's avatar
Youness Alaoui committed
644 645 646
   </para>
   <para>
     The list of remote candidates can change during processing.
647 648
     The client should register for the #NiceAgent::new-remote-candidate signal
     to get notified of new remote candidates.
Youness Alaoui's avatar
Youness Alaoui committed
649 650 651 652 653 654
   </para>
 </note>
 *
 * Returns: a #GSList of #NiceCandidates objects representing
 * the remote candidates set on the @agent
 **/
655 656 657 658 659 660
GSList *
nice_agent_get_remote_candidates (
  NiceAgent *agent,
  guint stream_id,
  guint component_id);

Youness Alaoui's avatar
Youness Alaoui committed
661 662 663 664 665 666 667 668 669 670 671 672
/**
 * nice_agent_restart:
 * @agent: The #NiceAgent Object
 *
 * 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.
 *
 * Returns: %TRUE on success %FALSE on error
 **/
gboolean
673 674 675
nice_agent_restart (
  NiceAgent *agent);

Youness Alaoui's avatar
Youness Alaoui committed
676 677 678 679 680 681 682

/**
 * nice_agent_attach_recv:
 * @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
683 684
 * @func: The callback function to be called when data is received on
 * the stream's component
Youness Alaoui's avatar
Youness Alaoui committed
685 686
 * @data: user data associated with the callback
 *
687 688
 * Attaches the stream's component's sockets to the Glib Mainloop Context in
 * order to be notified whenever data becomes available for a component.
Youness Alaoui's avatar
Youness Alaoui committed
689
 *
690 691 692
 * This must not be used in combination with nice_agent_recv() (or
 * #NiceIOStream or #NiceInputStream) on the same stream/component pair.
 *
693 694 695 696 697 698
 * Calling nice_agent_attach_recv() with a %NULL @func will detach any existing
 * callback and cause reception to be paused for the given stream/component
 * pair. You must iterate the previously specified #GMainContext sufficiently to
 * ensure all pending I/O callbacks have been received before calling this
 * function to unset @func, otherwise data loss of received packets may occur.
 *
Youness Alaoui's avatar
Youness Alaoui committed
699 700
 * Returns: %TRUE on success, %FALSE if the stream or component IDs are invalid.
 */
701
gboolean
702
nice_agent_attach_recv (
703
  NiceAgent *agent,
704 705
  guint stream_id,
  guint component_id,
706 707 708 709
  GMainContext *ctx,
  NiceAgentRecvFunc func,
  gpointer data);

710 711 712 713 714 715 716 717 718
gssize
nice_agent_recv (
    NiceAgent *agent,
    guint stream_id,
    guint component_id,
    guint8 *buf,
    gsize buf_len,
    GCancellable *cancellable,
    GError **error);
Youness Alaoui's avatar
Youness Alaoui committed
719

720 721 722 723 724 725 726 727 728 729
gssize
nice_agent_recv_nonblocking (
    NiceAgent *agent,
    guint stream_id,
    guint component_id,
    guint8 *buf,
    gsize buf_len,
    GCancellable *cancellable,
    GError **error);

Youness Alaoui's avatar
Youness Alaoui committed
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
/**
 * nice_agent_set_selected_pair:
 * @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
 *
 * 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.
 *
 * Returns: %TRUE on success, %FALSE if the candidate pair cannot be found
 */
gboolean
747 748 749 750 751 752 753
nice_agent_set_selected_pair (
  NiceAgent *agent,
  guint stream_id,
  guint component_id,
  const gchar *lfoundation,
  const gchar *rfoundation);

754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
/**
 * nice_agent_get_selected_pair:
 * @agent: The #NiceAgent Object
 * @stream_id: The ID of the stream
 * @component_id: The ID of the component
 * @local: The local selected candidate
 * @remote: The remote selected candidate
 *
 * Retreive the selected candidate pair for media transmission
 * for a given stream's component.
 *
 * Returns: %TRUE on success, %FALSE if there is no selected candidate pair
 */
gboolean
nice_agent_get_selected_pair (
  NiceAgent *agent,
  guint stream_id,
  guint component_id,
  NiceCandidate **local,
  NiceCandidate **remote);

775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
/**
 * nice_agent_get_selected_socket:
 * @agent: The #NiceAgent Object
 * @stream_id: The ID of the stream
 * @component_id: The ID of the component
 *
 * Retreive the local socket associated with the selected candidate pair
 * for media transmission for a given stream's component.
 *
 * This is useful for adding ICE support to legacy applications that already
 * have a protocol that maintains a connection. If the socket is duplicated
 * before unrefing the agent, the application can take over and continue to use
 * it. New applications are encouraged to use the built in libnice stream
 * handling instead and let libnice handle the connection maintenance.
 *
790 791 792 793 794 795 796
 * Users of this method are encouraged to not use a TURN relay or any kind
 * of proxy, as in this case, the socket will not be available to the
 * application because the packets are encapsulated.
 *
 * Returns: (transfer full) pointer to the #GSocket, or NULL if there is no
 * selected candidate or if the selected candidate is a relayed candidate.
 *
797 798 799 800 801 802 803 804
 * Since: 0.1.5
 */
GSocket *
nice_agent_get_selected_socket (
  NiceAgent *agent,
  guint stream_id,
  guint component_id);

Youness Alaoui's avatar
Youness Alaoui committed
805 806 807 808 809 810 811 812 813 814 815
/**
 * nice_agent_set_selected_remote_candidate:
 * @agent: The #NiceAgent Object
 * @stream_id: The ID of the stream
 * @component_id: The ID of the component
 * @candidate: The #NiceCandidate to select
 *
 * 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).
816 817
 * Calling this function will disable all further ICE processing
 * (connection check, state machine updates, etc). Note that keepalives will
Youness Alaoui's avatar
Youness Alaoui committed
818 819 820 821
 * continue to be sent.
 *
 * Returns: %TRUE on success, %FALSE on failure
 */
822 823 824 825 826 827 828
gboolean
nice_agent_set_selected_remote_candidate (
  NiceAgent *agent,
  guint stream_id,
  guint component_id,
  NiceCandidate *candidate);

Youness Alaoui's avatar
Youness Alaoui committed
829

830 831 832 833 834 835
/**
 * nice_agent_set_stream_tos:
 * @agent: The #NiceAgent Object
 * @stream_id: The ID of the stream
 * @tos: The ToS to set
 *
Youness Alaoui's avatar
Youness Alaoui committed
836
 * Sets the IP_TOS and/or IPV6_TCLASS field on the stream's sockets' options
837
 *
838
 * Since: 0.0.9
839 840
 */
void nice_agent_set_stream_tos (
Youness Alaoui's avatar
Youness Alaoui committed
841 842 843 844 845
  NiceAgent *agent,
  guint stream_id,
  gint tos);


846 847 848 849 850 851 852 853 854

/**
 * nice_agent_set_software:
 * @agent: The #NiceAgent Object
 * @software: The value of the SOFTWARE attribute to add.
 *
 * This function will set the value of the SOFTWARE attribute to be added to
 * STUN requests, responses and error responses sent during connectivity checks.
 * <para>
855
 * The SOFTWARE attribute will only be added in the #NICE_COMPATIBILITY_RFC5245
856
 * and #NICE_COMPATIBILITY_WLM2009 compatibility modes.
857
 *
858 859 860 861 862 863 864 865 866 867 868 869
 * </para>
 * <note>
     <para>
       The @software argument will be appended with the libnice version before
       being sent.
     </para>
     <para>
       The @software argument must be in UTF-8 encoding and only the first
       128 characters will be sent.
     </para>
   </note>
 *
870 871
 * Since: 0.0.10
 *
872
 */
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
void nice_agent_set_software (
    NiceAgent *agent,
    const gchar *software);

/**
 * nice_agent_set_stream_name:
 * @agent: The #NiceAgent Object
 * @stream_id: The ID of the stream to change
 * @name: The new name of the stream or %NULL
 *
 * This function will assign a unique name to a stream.
 * This is only useful when parsing and generating an SDP of the candidates.
 *
 * <para>See also: nice_agent_generate_local_sdp()</para>
 * <para>See also: nice_agent_parse_remote_sdp()</para>
 * <para>See also: nice_agent_get_stream_name()</para>
 *
 * Returns: %TRUE if the name has been set. %FALSE in case of error
 * (invalid stream or duplicate name).
 * Since: 0.1.4
 */
gboolean nice_agent_set_stream_name (
    NiceAgent *agent,
    guint stream_id,
    const gchar *name);

/**
 * nice_agent_get_stream_name:
 * @agent: The #NiceAgent Object
 * @stream_id: The ID of the stream to change
 *
 * This function will return the name assigned to a stream.

 * <para>See also: nice_agent_set_stream_name()</para>
 *
 * Returns: The name of the stream. The name is only valid while the stream
 * exists or until it changes through a call to nice_agent_set_stream_name().
 *
 *
 * Since: 0.1.4
 */
const gchar *nice_agent_get_stream_name (
    NiceAgent *agent,
    guint stream_id);

918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
/**
 * nice_agent_get_default_local_candidate:
 * @agent: The #NiceAgent Object
 * @stream_id: The ID of the stream
 * @component_id: The ID of the component
 *
 * This helper function will return the recommended default candidate to be
 * used for non-ICE compatible clients. This will usually be the candidate
 * with the lowest priority, since it will be the longest path but the one with
 * the most chances of success.
 * <note>
     <para>
     This function is only useful in order to manually generate the
     local SDP
     </para>
 * </note>
 *
 * Returns: The candidate to be used as the default candidate, or %NULL in case
 * of error. Must be freed with nice_candidate_free() once done.
 *
 */
NiceCandidate *
nice_agent_get_default_local_candidate (
    NiceAgent *agent,
    guint stream_id,
    guint component_id);

945 946 947 948
/**
 * nice_agent_generate_local_sdp:
 * @agent: The #NiceAgent Object
 *
949 950
 * Generate an SDP string containing the local candidates and credentials for
 * all streams and components in the agent.
951 952 953 954 955 956 957 958 959 960 961 962 963 964
 *
 <note>
   <para>
     The SDP will not contain any codec lines and the 'm' line will not list
     any payload types.
   </para>
   <para>
    It is highly recommended to set names on the streams prior to calling this
    function. Unnamed streams will show up as '-' in the 'm' line, but the SDP
    will not be parseable with nice_agent_parse_remote_sdp() if a stream is
    unnamed.
   </para>
   <para>
     The default candidate in the SDP will be selected based on the lowest
965
     priority candidate for the first component.
966 967 968 969 970
   </para>
 </note>
 *
 * <para>See also: nice_agent_set_stream_name() </para>
 * <para>See also: nice_agent_parse_remote_sdp() </para>
971 972 973
 * <para>See also: nice_agent_generate_local_stream_sdp() </para>
 * <para>See also: nice_agent_generate_local_candidate_sdp() </para>
 * <para>See also: nice_agent_get_default_local_candidate() </para>
974 975 976 977 978 979 980 981 982 983
 *
 * Returns: A string representing the local SDP. Must be freed with g_free()
 * once done.
 *
 * Since: 0.1.4
 **/
gchar *
nice_agent_generate_local_sdp (
  NiceAgent *agent);

984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
/**
 * nice_agent_generate_local_stream_sdp:
 * @agent: The #NiceAgent Object
 * @stream_id: The ID of the stream
 * @include_non_ice: Whether or not to include non ICE specific lines
 * (m=, c= and a=rtcp: lines)
 *
 * Generate an SDP string containing the local candidates and credentials
 * for a stream.
 *
 <note>
   <para>
     The SDP will not contain any codec lines and the 'm' line will not list
     any payload types.
   </para>
   <para>
    It is highly recommended to set the name of the stream prior to calling this
    function. Unnamed streams will show up as '-' in the 'm' line.
   </para>
   <para>
     The default candidate in the SDP will be selected based on the lowest
     priority candidate.
   </para>
 </note>
 *
 * <para>See also: nice_agent_set_stream_name() </para>
 * <para>See also: nice_agent_parse_remote_stream_sdp() </para>
 * <para>See also: nice_agent_generate_local_sdp() </para>
 * <para>See also: nice_agent_generate_local_candidate_sdp() </para>
 * <para>See also: nice_agent_get_default_local_candidate() </para>
 *
 * Returns: A string representing the local SDP for the stream. Must be freed
 * with g_free() once done.
 *
 * Since: 0.1.4
 **/
gchar *
nice_agent_generate_local_stream_sdp (
    NiceAgent *agent,
    guint stream_id,
    gboolean include_non_ice);

/**
 * nice_agent_generate_local_candidate_sdp:
 * @agent: The #NiceAgent Object
 * @candidate: The candidate to generate
 *
 * Generate an SDP string representing a local candidate.
 *
 * <para>See also: nice_agent_parse_remote_candidate_sdp() </para>
 * <para>See also: nice_agent_generate_local_sdp() </para>
 * <para>See also: nice_agent_generate_local_stream_sdp() </para>
 *
 * Returns: A string representing the SDP for the candidate. Must be freed
 * with g_free() once done.
 *
 * Since: 0.1.4
 **/
gchar *
nice_agent_generate_local_candidate_sdp (
    NiceAgent *agent,
    NiceCandidate *candidate);

1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
/**
 * nice_agent_parse_remote_sdp:
 * @agent: The #NiceAgent Object
 * @sdp: The remote SDP to parse
 *
 * Parse an SDP string and extracts candidates and credentials from it and sets
 * them on the agent.
 *
 <note>
   <para>
    This function will return an error if a stream has not been assigned a name
    with nice_agent_set_stream_name() as it becomes troublesome to assign the
    streams from the agent to the streams in the SDP.
   </para>
 </note>
 *
 *
 * <para>See also: nice_agent_set_stream_name() </para>
 * <para>See also: nice_agent_generate_local_sdp() </para>
1066 1067
 * <para>See also: nice_agent_parse_remote_stream_sdp() </para>
 * <para>See also: nice_agent_parse_remote_candidate_sdp() </para>
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
 *
 * Returns: The number of candidates added, negative on errors
 *
 * Since: 0.1.4
 **/
int
nice_agent_parse_remote_sdp (
    NiceAgent *agent,
    const gchar *sdp);

1078

1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
/**
 * nice_agent_parse_remote_stream_sdp:
 * @agent: The #NiceAgent Object
 * @stream_id: The ID of the stream to parse
 * @sdp: The remote SDP to parse
 * @ufrag: Pointer to store the ice ufrag if non %NULL. Must be freed with
 * g_free() after use
 * @pwd: Pointer to store the ice password if non %NULL. Must be freed with
 * g_free() after use
 *
 * Parse an SDP string representing a single stream and extracts candidates
 * and credentials from it.
 *
 * <para>See also: nice_agent_generate_local_stream_sdp() </para>
 * <para>See also: nice_agent_parse_remote_sdp() </para>
 * <para>See also: nice_agent_parse_remote_candidate_sdp() </para>
 *
 * Returns: A #GSList of candidates parsed from the SDP, or %NULL in case of
 * errors
 *
 * Since: 0.1.4
 **/
GSList *
nice_agent_parse_remote_stream_sdp (
    NiceAgent *agent,
    guint stream_id,
    const gchar *sdp,
    gchar **ufrag,
    gchar **pwd);


/**
 * nice_agent_parse_remote_candidate_sdp:
 * @agent: The #NiceAgent Object
 * @stream_id: The ID of the stream the candidate belongs to
 * @sdp: The remote SDP to parse
 *
 * Parse an SDP string and extracts the candidate from it.
 *
 * <para>See also: nice_agent_generate_local_candidate_sdp() </para>
 * <para>See also: nice_agent_parse_remote_sdp() </para>
 * <para>See also: nice_agent_parse_remote_stream_sdp() </para>
 *
 * Returns: The parsed candidate or %NULL if there was an error.
 *
 * Since: 0.1.4
 **/
NiceCandidate *
nice_agent_parse_remote_candidate_sdp (
    NiceAgent *agent,
    guint stream_id,
    const gchar *sdp);

1132 1133 1134 1135 1136 1137 1138 1139
/**
 * nice_agent_build_io_stream:
 * @agent: A #NiceAgent
 * @stream_id: The ID of the stream to wrap
 * @component_id: The ID of the component to wrap
 *
 * Build a #GIOStream wrapper around the given stream and component in
 * @agent. The I/O stream will be valid for as long as @stream_id is valid.
1140 1141
 * The #GInputStream and #GOutputStream implement #GPollableInputStream and
 * #GPollableOutputStream.
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
 *
 * This function may only be called on reliable #NiceAgents. It is an error to
 * try and create an I/O stream wrapper for an unreliable stream.
 *
 * Returns: (transfer full): A new #GIOStream.
 *
 * Since: 0.1.5
 */
GIOStream *
nice_agent_build_io_stream (
    NiceAgent *agent,
    guint stream_id,
    guint component_id);

Dafydd Harries's avatar
Dafydd Harries committed
1156 1157
G_END_DECLS

Dafydd Harries's avatar
Dafydd Harries committed
1158 1159
#endif /* _AGENT_H */