test-different-number-streams.c 7.96 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "agent.h"

#include <stdlib.h>
#include <string.h>

#define ADD_2_STREAMS TRUE
#define USE_SECOND_STREAM TRUE

static GMainLoop *global_mainloop = NULL;

static guint global_components_ready = 0;
static guint global_components_ready_exit = 0;

static gboolean timer_cb (gpointer pointer)
{
  g_debug ("test-different-number-streams:%s: %p", G_STRFUNC, pointer);

  /* signal status via a global variable */

  /* note: should not be reached, abort */
  g_error ("ERROR: test has got stuck, aborting...");

  return FALSE;
}

static void cb_candidate_gathering_done(NiceAgent *agent, guint stream_id, gpointer data)
{
  g_debug ("%p: gathering done (stream_id: %u)", agent, stream_id);
}

static void cb_component_state_changed (NiceAgent *agent, guint stream_id, guint component_id, guint state, gpointer data)
{
  g_debug ("%p: component state changed (stream_id: %u, component_id: %u, state: %s)",
           agent, stream_id, component_id, nice_component_state_to_string (state));

  if (state == NICE_COMPONENT_STATE_READY) {
    global_components_ready++;
  }

  /* signal status via a global variable */
  if (global_components_ready == global_components_ready_exit) {
    g_debug ("Components ready/failed achieved. Stopping mailoop");
    g_main_loop_quit (global_mainloop);
  }
}

static void set_candidates (NiceAgent *from, guint from_stream,
    NiceAgent *to, guint to_stream, guint component)
{
  GSList *cands = NULL, *i;

  cands = nice_agent_get_local_candidates (from, from_stream, component);
  nice_agent_set_remote_candidates (to, to_stream, component, cands);

  for (i = cands; i; i = i->next)
    nice_candidate_free ((NiceCandidate *) i->data);
  g_slist_free (cands);
}

static void cb_nice_recv (NiceAgent *agent, guint stream_id, guint component_id, guint len, gchar *buf, gpointer user_data)
{
  g_debug ("%p: recv (stream_id: %u, component_id: %u)", agent, stream_id, component_id);
}

int main (void)
{
  NiceAgent *lagent, *ragent;
  guint timer_id;
  guint ls_id, rs_id_1, rs_id_2;
  gchar *lufrag = NULL, *lpassword = NULL;
  gchar *rufrag1 = NULL, *rpassword1 = NULL, *rufrag2 = NULL, *rpassword2 = NULL;
76 77
  NiceAddress *addr;

78 79 80 81 82 83 84

#ifdef G_OS_WIN32
  WSADATA w;

  WSAStartup(0x0202, &w);
#endif

85 86 87 88
  /* Initialize nice agents */
  addr = nice_address_new ();
  nice_address_set_from_string (addr, "127.0.0.1");

89 90 91 92 93 94
  global_mainloop = g_main_loop_new (NULL, FALSE);

  /* step: create the agents L and R */
  lagent = nice_agent_new (g_main_loop_get_context (global_mainloop),
      NICE_COMPATIBILITY_GOOGLE);
  g_debug ("lagent: %p", lagent);
95 96

  nice_agent_add_local_address (lagent, addr);
97 98 99 100 101 102 103 104 105 106 107 108
  nice_agent_set_software (lagent, "test-different-number-streams, Left Agent");
  g_object_set (G_OBJECT (lagent), "ice-tcp", FALSE,  NULL);
  g_object_set (G_OBJECT (lagent), "controlling-mode", TRUE, NULL);
  g_object_set (G_OBJECT (lagent), "upnp", FALSE,  NULL);
  g_signal_connect (G_OBJECT (lagent), "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);

  ragent = nice_agent_new (g_main_loop_get_context (global_mainloop),
      NICE_COMPATIBILITY_GOOGLE);
  g_debug ("ragent: %p", ragent);
109 110

  nice_agent_add_local_address (ragent, addr);
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
  nice_agent_set_software (ragent, "test-different-number-streams, Right Agent");
  g_object_set (G_OBJECT (ragent), "ice-tcp", FALSE,  NULL);
  g_object_set (G_OBJECT (ragent), "controlling-mode", FALSE, NULL);
  g_object_set (G_OBJECT (ragent), "upnp", FALSE,  NULL);
  g_signal_connect (G_OBJECT (ragent), "candidate-gathering-done",
      G_CALLBACK (cb_candidate_gathering_done), NULL);
  g_signal_connect (G_OBJECT (ragent), "component-state-changed",
      G_CALLBACK (cb_component_state_changed), NULL);

  /* step: add a timer to catch state changes triggered by signals */
  timer_id = g_timeout_add (30000, timer_cb, NULL);

  ls_id = nice_agent_add_stream (lagent, 2);
  g_assert (ls_id > 0);
  nice_agent_get_local_credentials(lagent, ls_id, &lufrag, &lpassword);

  /* step: attach to mainloop (needed to register the fds) */
  nice_agent_attach_recv (lagent, ls_id, NICE_COMPONENT_TYPE_RTP,
      g_main_loop_get_context (global_mainloop), cb_nice_recv, NULL);
  nice_agent_attach_recv (lagent, ls_id, NICE_COMPONENT_TYPE_RTCP,
      g_main_loop_get_context (global_mainloop), cb_nice_recv, NULL);

  global_components_ready_exit = 4;

  if (ADD_2_STREAMS) {
    rs_id_1 = nice_agent_add_stream (ragent, 2);
    g_assert (rs_id_1 > 0);
    nice_agent_get_local_credentials(ragent, rs_id_1, &rufrag1, &rpassword1);

    rs_id_2 = nice_agent_add_stream (ragent, 2);
    g_assert (rs_id_2 > 0);
    nice_agent_get_local_credentials(ragent, rs_id_2, &rufrag2, &rpassword2);

    nice_agent_set_remote_credentials (ragent, rs_id_2, lufrag, lpassword);
    nice_agent_set_remote_credentials (lagent, ls_id, rufrag2, rpassword2);

    g_assert (nice_agent_gather_candidates (lagent, ls_id) == TRUE);
    g_assert (nice_agent_gather_candidates (ragent, rs_id_2) == TRUE);
    g_assert (nice_agent_gather_candidates (ragent, rs_id_1) == TRUE);

    if (USE_SECOND_STREAM) {
      set_candidates (ragent, rs_id_2, lagent, ls_id, NICE_COMPONENT_TYPE_RTP);
      set_candidates (ragent, rs_id_2, lagent, ls_id, NICE_COMPONENT_TYPE_RTCP);
      set_candidates (lagent, ls_id, ragent, rs_id_2, NICE_COMPONENT_TYPE_RTP);
      set_candidates (lagent, ls_id, ragent, rs_id_2, NICE_COMPONENT_TYPE_RTCP);
    } else {
      set_candidates (ragent, rs_id_1, lagent, ls_id, NICE_COMPONENT_TYPE_RTP);
      set_candidates (ragent, rs_id_1, lagent, ls_id, NICE_COMPONENT_TYPE_RTCP);
      set_candidates (lagent, ls_id, ragent, rs_id_1, NICE_COMPONENT_TYPE_RTP);
      set_candidates (lagent, ls_id, ragent, rs_id_1, NICE_COMPONENT_TYPE_RTCP);
    }

    /* step: attach to mainloop (needed to register the fds) */
    nice_agent_attach_recv (ragent, rs_id_1, NICE_COMPONENT_TYPE_RTP,
        g_main_loop_get_context (global_mainloop), cb_nice_recv, NULL);
    nice_agent_attach_recv (ragent, rs_id_1, NICE_COMPONENT_TYPE_RTCP,
        g_main_loop_get_context (global_mainloop), cb_nice_recv, NULL);
    nice_agent_attach_recv (ragent, rs_id_2, NICE_COMPONENT_TYPE_RTP,
        g_main_loop_get_context (global_mainloop), cb_nice_recv, NULL);
    nice_agent_attach_recv (ragent, rs_id_2, NICE_COMPONENT_TYPE_RTCP,
        g_main_loop_get_context (global_mainloop), cb_nice_recv, NULL);
  } else {
    rs_id_1 = nice_agent_add_stream (ragent, 2);
    g_assert (rs_id_1 > 0);
    nice_agent_get_local_credentials(ragent, rs_id_1, &rufrag1, &rpassword1);

    nice_agent_set_remote_credentials (ragent, rs_id_1, lufrag, lpassword);
    nice_agent_set_remote_credentials (lagent, ls_id, rufrag1, rpassword1);

    g_assert (nice_agent_gather_candidates (lagent, ls_id) == TRUE);
    g_assert (nice_agent_gather_candidates (ragent, rs_id_1) == TRUE);

    /* step: attach to mainloop (needed to register the fds) */
    nice_agent_attach_recv (ragent, rs_id_1, NICE_COMPONENT_TYPE_RTP,
        g_main_loop_get_context (global_mainloop), cb_nice_recv, NULL);
    nice_agent_attach_recv (ragent, rs_id_1, NICE_COMPONENT_TYPE_RTCP,
        g_main_loop_get_context (global_mainloop), cb_nice_recv, NULL);

    set_candidates (ragent, rs_id_1, lagent, ls_id, NICE_COMPONENT_TYPE_RTP);
    set_candidates (ragent, rs_id_1, lagent, ls_id, NICE_COMPONENT_TYPE_RTCP);
    set_candidates (lagent, ls_id, ragent, rs_id_1, NICE_COMPONENT_TYPE_RTP);
    set_candidates (lagent, ls_id, ragent, rs_id_1, NICE_COMPONENT_TYPE_RTCP);
  }

  /* step: run the mainloop until connectivity checks succeed
  *       (see timer_cb() above) */
  g_main_loop_run (global_mainloop);

  g_free (lufrag);
  g_free (lpassword);
  g_free (rufrag1);
  g_free (rpassword1);
  g_free (rufrag2);
  g_free (rpassword2);
  g_object_unref (lagent);
  g_object_unref (ragent);

  g_main_loop_unref (global_mainloop);
  global_mainloop = NULL;

  g_source_remove (timer_id);

#ifdef G_OS_WIN32
  WSACleanup();
#endif

  return 0;
}