agent.h 15.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * This file is part of the Nice GLib ICE library.
 *
 * (C) 2006, 2007 Collabora Ltd.
 *  Contact: Dafydd Harries
 * (C) 2006, 2007 Nokia Corporation. All rights reserved.
 *  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
 *   Kai Vehmanen, Nokia
27 28 29 30 31 32 33 34 35 36 37
 *
 * 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
38 39 40 41

#ifndef _AGENT_H
#define _AGENT_H

Dafydd Harries's avatar
Dafydd Harries committed
42
#include <glib-object.h>
43

44 45
typedef struct _NiceAgent NiceAgent;

46
#include "address.h"
Dafydd Harries's avatar
Dafydd Harries committed
47
#include "candidate.h"
48
#include "debug.h"
49

Youness Alaoui's avatar
Youness Alaoui committed
50 51 52 53 54 55
/**
 * SECTION:agent
 * @short_description:  ICE agent API implementation
 *
 * 
 */
56

Dafydd Harries's avatar
Dafydd Harries committed
57
G_BEGIN_DECLS
58

Dafydd Harries's avatar
Dafydd Harries committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#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
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

typedef struct _NiceAgentClass NiceAgentClass;

struct _NiceAgentClass
{
  GObjectClass parent_class;
};


GType nice_agent_get_type (void);


/**
 * NICE_AGENT_MAX_REMOTE_CANDIDATES:
 *
96
 * A hard limit for number for remote candidates. This
Youness Alaoui's avatar
Youness Alaoui committed
97
 * limit is enforced to protect against malevolent remote
98 99 100 101
 * clients.
 */
#define NICE_AGENT_MAX_REMOTE_CANDIDATES    25

Youness Alaoui's avatar
Youness Alaoui committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115
/**
 * 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
 *
 * An enum representing the state of a component.
 * See #NiceAgent::component-state-changed
 */
116 117
typedef enum
{
Youness Alaoui's avatar
Youness Alaoui committed
118 119 120 121 122 123
  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
124
  NICE_COMPONENT_STATE_LAST
125 126
} NiceComponentState;

Youness Alaoui's avatar
Youness Alaoui committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

/**
 * 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>
 */
142 143 144 145 146 147
typedef enum
{
  NICE_COMPONENT_TYPE_RTP = 1,
  NICE_COMPONENT_TYPE_RTCP = 2
} NiceComponentType;

148

Youness Alaoui's avatar
Youness Alaoui committed
149 150 151 152 153 154 155 156 157
/**
 * NiceCompatibility:
 * @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
 *
 * An enum to specify which compatible specifications the #NiceAgent should use.
 * Use with nice_agent_new()
 */
158 159
typedef enum
{
160
  NICE_COMPATIBILITY_DRAFT19 = 0,
161 162
  NICE_COMPATIBILITY_GOOGLE,
  NICE_COMPATIBILITY_MSN,
163
  NICE_COMPATIBILITY_LAST = NICE_COMPATIBILITY_MSN
164 165
} NiceCompatibility;

Youness Alaoui's avatar
Youness Alaoui committed
166 167 168 169 170 171 172 173 174 175 176 177 178
/**
 * 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
 *
 */
179 180 181 182
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
183

Youness Alaoui's avatar
Youness Alaoui committed
184 185 186 187 188 189 190 191 192
/**
 * nice_agent_new:
 * @ctx: The Glib Mainloop Context to use for timers
 * @compat: The compatibility mode of the agent
 *
 * Create a new #NiceAgent.
 *
 * Returns: the new agent GObject
 */
193
NiceAgent *
194
nice_agent_new (GMainContext *ctx, NiceCompatibility compat);
195

Youness Alaoui's avatar
Youness Alaoui committed
196 197 198 199 200 201 202 203 204 205
/**
 * 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
 *
 * Add a local address from which to derive local host candidates
 *
 * Returns: %TRUE on success, %FALSE on fatal (memory allocation) errors
 */
206
gboolean
207
nice_agent_add_local_address (NiceAgent *agent, NiceAddress *addr);
208

Youness Alaoui's avatar
Youness Alaoui committed
209 210 211 212 213 214 215 216 217 218

/**
 * nice_agent_add_stream:
 * @agent: The #NiceAgent Object
 * @n_components: The number of components to add to the stream
 *
 * Adds a data stream to @agent containing @n_components components.
 *
 * Returns: The ID of the new stream, 0 on failure
 **/
Dafydd Harries's avatar
Dafydd Harries committed
219
guint
220 221
nice_agent_add_stream (
  NiceAgent *agent,
222
  guint n_components);
223

Youness Alaoui's avatar
Youness Alaoui committed
224 225 226 227 228 229 230 231
/**
 * 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
232 233 234 235 236
void
nice_agent_remove_stream (
  NiceAgent *agent,
  guint stream_id);

Youness Alaoui's avatar
Youness Alaoui committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
/**
 * 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.
 *
 * Returns: %TRUE if the TURN settings were accepted.
 * %FALSE if the address was invalid.
 */
253
gboolean nice_agent_set_relay_info(
254 255 256 257 258 259
    NiceAgent *agent,
    guint stream_id,
    guint component_id,
    const gchar *server_ip,
    guint server_port,
    const gchar *username,
260 261
    const gchar *password,
    NiceRelayType type);
262

Youness Alaoui's avatar
Youness Alaoui committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276
/**
 * 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
 *
 <note>
   <para>
    Local addresses must be previously set with nice_agent_add_local_address()
   </para>
 </note>
 */
277 278 279 280 281
void
nice_agent_gather_candidates (
  NiceAgent *agent,
  guint stream_id);

Youness Alaoui's avatar
Youness Alaoui committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
/**
 * 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.
 */
301 302 303 304 305 306
gboolean
nice_agent_set_remote_credentials (
  NiceAgent *agent,
  guint stream_id,
  const gchar *ufrag, const gchar *pwd);

Youness Alaoui's avatar
Youness Alaoui committed
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323


/**
 * 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.
 */
324 325 326 327 328 329
gboolean
nice_agent_get_local_credentials (
  NiceAgent *agent,
  guint stream_id,
  const gchar **ufrag, const gchar **pwd);

Youness Alaoui's avatar
Youness Alaoui committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352

/**
 * nice_agent_add_remote_candidate:
 * @agent: The #NiceAgent Object
 * @stream_id: The ID of the stream the candidate is for
 * @component_id: The ID of the component the candidate is for
 * @type: The type of the new candidate
 * @addr: The new candidate's IP address
 * @username: The new candidate's username
 * (optional - overrides the value set in nice_agent_set_remote_credentials())
 * @password: The new candidate's password
 * (optional - overrides the value set in nice_agent_set_remote_credentials())
 *
 * Adds a new remote candidate to the agent
 *
 <note>
   <para>
    NICE_AGENT_MAX_REMOTE_CANDIDATES is the absolute maximum limit for remote candidates
   </para>
 </note>
 *
 * Returns: %TRUE on success, %FALSE on fatal (memory alloc) errors
 **/
353
gboolean
354 355
nice_agent_add_remote_candidate (
  NiceAgent *agent,
356 357
  guint stream_id,
  guint component_id,
358 359
  NiceCandidateType type,
  NiceAddress *addr,
360 361
  const gchar *username,
  const gchar *password);
362

Youness Alaoui's avatar
Youness Alaoui committed
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
/**
 * 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
 *
 * Sets the remote candidates for a component of a stream.
 * Replaces any existing remote candidates.
 *
 <note>
   <para>
    NICE_AGENT_MAX_REMOTE_CANDIDATES is the absolute maximum limit for remote candidates
   </para>
 </note>
 *
 * Returns: The number of candidates added, negative on fatal (memory allocs) errors
 **/
381
int
382 383 384 385
nice_agent_set_remote_candidates (
  NiceAgent *agent,
  guint stream_id,
  guint component_id,
386
  const GSList *candidates);
387

Youness Alaoui's avatar
Youness Alaoui committed
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

/**
 * 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>
 </note>
 *
 * Returns: The number of bytes sent, or negative error code
 */
408
gint
Dafydd Harries's avatar
Dafydd Harries committed
409 410 411 412 413
nice_agent_send (
  NiceAgent *agent,
  guint stream_id,
  guint component_id,
  guint len,
414
  const gchar *buf);
Dafydd Harries's avatar
Dafydd Harries committed
415

Youness Alaoui's avatar
Youness Alaoui committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
/**
 * nice_agent_get_local_candidates:
 * @agent: The #NiceAgent Object
 * @stream_id: The ID of the stream
 * @component_id: The ID of the component
 *
 * Retreive from the agent the list of all local candidates for a stream's component
 *
 <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
     #NiceAgent::candidates-gathering-done signal.
   </para>
 </note>
 *
 * Returns: a #GSList of #NiceCandidate objects representing
 * the local candidates of @agent
 **/
436
GSList *
437
nice_agent_get_local_candidates (
438 439 440
  NiceAgent *agent,
  guint stream_id,
  guint component_id);
Dafydd Harries's avatar
Dafydd Harries committed
441

Youness Alaoui's avatar
Youness Alaoui committed
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464

/**
 * 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>
     The caller owns the returned GSList but not the candidates contained within it.
   </para>
   <para>
     The list of remote candidates can change during processing.
     The client should register for the #NiceAgent::new-remote-candidate signal to
     get notification of new remote candidates.
   </para>
 </note>
 *
 * Returns: a #GSList of #NiceCandidates objects representing
 * the remote candidates set on the @agent
 **/
465 466 467 468 469 470
GSList *
nice_agent_get_remote_candidates (
  NiceAgent *agent,
  guint stream_id,
  guint component_id);

Youness Alaoui's avatar
Youness Alaoui committed
471 472 473 474 475 476 477 478 479 480 481 482
/**
 * 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
483 484 485
nice_agent_restart (
  NiceAgent *agent);

Youness Alaoui's avatar
Youness Alaoui committed
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500

/**
 * 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
 * @func: The callback function to be called when data is received on the component
 * @data: user data associated with the callback
 *
 * Attaches the stream's component's sockets to the Glib Mainloop Context in order
 * to be notified whenever data becomes available for a component.
 *
 * Returns: %TRUE on success, %FALSE if the stream or component IDs are invalid.
 */
501
gboolean
502
nice_agent_attach_recv (
503
  NiceAgent *agent,
504 505
  guint stream_id,
  guint component_id,
506 507 508 509
  GMainContext *ctx,
  NiceAgentRecvFunc func,
  gpointer data);

Youness Alaoui's avatar
Youness Alaoui committed
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527

/**
 * 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
528 529 530 531 532 533 534
nice_agent_set_selected_pair (
  NiceAgent *agent,
  guint stream_id,
  guint component_id,
  const gchar *lfoundation,
  const gchar *rfoundation);

Youness Alaoui's avatar
Youness Alaoui committed
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
/**
 * 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).
 * 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 on failure
 */
552 553 554 555 556 557 558
gboolean
nice_agent_set_selected_remote_candidate (
  NiceAgent *agent,
  guint stream_id,
  guint component_id,
  NiceCandidate *candidate);

Dafydd Harries's avatar
Dafydd Harries committed
559 560
G_END_DECLS

Dafydd Harries's avatar
Dafydd Harries committed
561 562
#endif /* _AGENT_H */