Example #1
0
static void
kms_ice_nice_agent_new_candidate (NiceAgent * agent,
    guint stream_id,
    guint component_id, gchar * foundation, KmsIceNiceAgent * self)
{
  KmsIceBaseAgent *parent = KMS_ICE_BASE_AGENT (self);
  GSList *candidates;
  GSList *walk;

  GST_TRACE_OBJECT (self,
      "stream_id: %d, component_id: %d, foundation: %s", stream_id,
      component_id, foundation);

  candidates = nice_agent_get_local_candidates (agent, stream_id, component_id);

  for (walk = candidates; walk; walk = walk->next) {
    NiceCandidate *cand = walk->data;

    if (cand->stream_id == stream_id &&
        cand->component_id == component_id &&
        g_strcmp0 (foundation, cand->foundation) == 0) {
      kms_ice_nice_agent_sdp_msg_add_ice_candidate (self->priv->session,
          self->priv->agent, cand, parent);
    }
  }
  g_slist_free_full (candidates, (GDestroyNotify) nice_candidate_free);
}
Example #2
0
static void cb_agent_new_candidate(NiceAgent *agent, guint stream_id, guint component_id, gchar *foundation, gpointer user_data)
{
  NiceAgent *other = g_object_get_data (G_OBJECT (agent), "other-agent");
  GSList *cands = nice_agent_get_local_candidates (agent, stream_id,
                                                   component_id);
  GSList *i = NULL;
  GSList *remote_cands = NULL;
  NiceCandidate* temp;
  gpointer tmp;
  guint id;

  g_debug ("test-dribblemode:%s: %p", G_STRFUNC, user_data);

  tmp = g_object_get_data (G_OBJECT (other), "id");
  id = GPOINTER_TO_UINT (tmp);

  for (i = cands; i; i = i->next) {
    temp = (NiceCandidate*) i->data;
    if (g_strcmp0(temp->foundation, foundation) == 0) {
      g_debug ("Adding new local candidate to other agent's connchecks");
      remote_cands = g_slist_prepend (remote_cands, nice_candidate_copy(temp));
      g_assert (nice_agent_set_remote_candidates (other, id,
                                                  NICE_COMPONENT_TYPE_RTP,
                                                  remote_cands));
    }
  }

  g_slist_free_full (remote_cands, (GDestroyNotify) nice_candidate_free);
  g_slist_free_full (cands, (GDestroyNotify) nice_candidate_free);

}
Example #3
0
int  _video_send_print_local_data(NiceAgent *agent, guint stream_id, guint component_id)
{
	int result = EXIT_FAILURE;
	gchar *local_ufrag = NULL;
	gchar *local_password = NULL;
	gchar ipaddr[INET6_ADDRSTRLEN];
	GSList *cands = NULL, *item;

	if (!nice_agent_get_local_credentials(agent, stream_id,
	&local_ufrag, &local_password))
	goto end;

	cands = nice_agent_get_local_candidates(agent, stream_id, component_id);
	if (cands == NULL)
	goto end;

	mInfo_SendVideo = (gchar*)malloc(181*sizeof(gchar));

	//printf("%s %s", local_ufrag, local_password);
	sprintf(mInfo_SendVideo, "%s %s", local_ufrag, local_password);
	for (item = cands; item; item = item->next) {
	NiceCandidate *c = (NiceCandidate *)item->data;

		nice_address_to_string(&c->addr, ipaddr);

		// (foundation),(prio),(addr),(port),(type)
		/*printf(" %s,%u,%s,%u,%s",
		c->foundation,
		c->priority,
		ipaddr,
		nice_address_get_port(&c->addr),
		candidate_type_name[c->type]);*/
		sprintf(mInfo_SendVideo + strlen(mInfo_SendVideo), " %s,%u,%s,%u,%s",
		c->foundation,
		c->priority,
		ipaddr,
		nice_address_get_port(&c->addr),
		candidate_type_name[c->type]);
	}
	printf("\n");

	//printf("\nmInfo_SendVideo:\n");
	//printf("%s\n", mInfo_SendVideo);
	result = EXIT_SUCCESS;

	end:
	if (local_ufrag)
		g_free(local_ufrag);
	if (local_password)
		g_free(local_password);
	if (cands)
		g_slist_free_full(cands, (GDestroyNotify)&nice_candidate_free);

	return result;
}
Example #4
0
void
local_credentials_to_string(NiceAgent *agent, guint stream_id, guint component_id, gchar** out) {
  gchar buf[1024];
  gchar tmp[1024];
  gchar *local_ufrag = NULL;
  gchar *local_password = NULL;
  gchar ipaddr[INET6_ADDRSTRLEN];
  GSList *cands = NULL, *item;

  if(!nice_agent_get_local_credentials(agent, stream_id,
      &local_ufrag, &local_password)) {
    g_critical("Error reading local credentials!");
    g_object_unref(agent);
    exit(1);
  }

  cands = nice_agent_get_local_candidates(agent, stream_id, component_id);
  if(cands == NULL) {
    g_critical("Error reading local candidates!");
    g_object_unref(agent);
    exit(1);
  }

  sprintf(buf, "%s %s", local_ufrag, local_password);

  for (item = cands; item; item = item->next) {
    NiceCandidate *c = (NiceCandidate *)item->data;

    nice_address_to_string(&c->addr, ipaddr);

    // (foundation),(prio),(addr),(port),(type)
    sprintf(tmp, " %s,%u,%s,%u,%s",
        c->foundation,
        c->priority,
        ipaddr,
        nice_address_get_port(&c->addr),
        candidate_type_name[c->type]);
    strcat(buf, tmp);
  }
  sprintf(tmp, "\n");
  strcat(buf, tmp);

  g_free(local_ufrag);
  g_free(local_password);
  g_slist_free_full(cands, (GDestroyNotify)&nice_candidate_free);

  *out = g_malloc(strlen(buf)+1);
  strcpy(*out, buf);
}
Example #5
0
static void priv_get_local_addr (NiceAgent *agent, guint stream_id, guint component_id, NiceAddress *dstaddr)
{
  GSList *cands, *i;
  cands = nice_agent_get_local_candidates(agent, stream_id, component_id);
  for (i = cands; i; i = i->next) {
    NiceCandidate *cand = i->data;
    if (cand) {
      g_assert (dstaddr);
      *dstaddr = cand->addr;
    }
  }
  for (i = cands; i; i = i->next)
    nice_candidate_free ((NiceCandidate *) i->data);
  g_slist_free (cands);
}
Example #6
0
static void swap_candidates(NiceAgent *local, guint local_id, NiceAgent *remote, guint remote_id, gboolean signal_stun_reply)
{
  GSList *cands = NULL;

  g_debug ("test-dribblemode:%s", G_STRFUNC);
  cands = nice_agent_get_local_candidates(local, local_id,
                                          NICE_COMPONENT_TYPE_RTP);
  g_assert(nice_agent_set_remote_candidates(remote, remote_id,
                                            NICE_COMPONENT_TYPE_RTP, cands));

  if (signal_stun_reply) {
    g_mutex_lock (stun_mutex_ptr);
    g_cond_signal (stun_signal_ptr);
    g_mutex_unlock (stun_mutex_ptr);
  }

  g_slist_free_full (cands, (GDestroyNotify) nice_candidate_free);
}
Example #7
0
static GSList *priv_get_local_candidate (NiceAgent *agent, guint stream_id, guint component_id)
{
  GSList *cands, *i;
  GSList *result = NULL;
  NiceCandidate *out_cand = NULL;
  cands = nice_agent_get_local_candidates(agent, stream_id, component_id);
  for (i = cands; i; i = i->next) {
    NiceCandidate *cand = i->data;
    if (cand) {
      out_cand = cand;
    }
  }
  result = g_slist_append (result, nice_candidate_copy (out_cand));

  for (i = cands; i; i = i->next)
    nice_candidate_free ((NiceCandidate *) i->data);
  g_slist_free (cands);
  return result;
}
Example #8
0
int
main (void)
{
  NiceAgent *agent;
  NiceAddress addr;
  guint stream;

  nice_address_init (&addr);
  g_type_init ();
  g_thread_init(NULL);

  loop = g_main_loop_new (NULL, FALSE);

  agent = nice_agent_new (g_main_loop_get_context (loop), NICE_COMPATIBILITY_RFC5245);
  nice_address_set_ipv4 (&addr, 0x7f000001);
  nice_agent_add_local_address (agent, &addr);
  stream = nice_agent_add_stream (agent, 1);
  nice_agent_gather_candidates (agent, stream);

  // attach to default main context
  nice_agent_attach_recv (agent, stream, NICE_COMPONENT_TYPE_RTP,
      g_main_loop_get_context (loop), recv_cb, GUINT_TO_POINTER (42));

    {
      NiceCandidate *candidate;
      GSList *candidates, *i;

      candidates = nice_agent_get_local_candidates (agent, 1, 1);
      candidate = candidates->data;

      nice_socket_send (candidate->sockptr, &(candidate->addr), 6, "\x80hello");
      for (i = candidates; i; i = i->next)
        nice_candidate_free ((NiceCandidate *) i->data);
      g_slist_free (candidates);
    }

  g_main_loop_run (loop);

  g_object_unref (agent);
  return 0;
}
Example #9
0
static void set_candidates (NiceAgent *from, guint from_stream,
    NiceAgent *to, guint to_stream, guint component, gboolean remove_non_relay)
{
  GSList *cands = NULL, *i;

  cands = nice_agent_get_local_candidates (from, from_stream, component);
  if (remove_non_relay) {
  restart:
    for (i = cands; i; i = i->next) {
      NiceCandidate *cand = i->data;
      if (cand->type != NICE_CANDIDATE_TYPE_RELAYED) {
        cands = g_slist_remove (cands, cand);
        nice_candidate_free (cand);
        goto restart;
      }
    }
  }
  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);
}
Example #10
0
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);

 restart:
  for (i = cands; i; i = i->next) {
    NiceCandidate *cand = i->data;
    if (cand->transport == NICE_CANDIDATE_TRANSPORT_UDP) {
      cands = g_slist_remove (cands, cand);
      nice_candidate_free (cand);
      goto restart;
    }
  }


  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);
}
Example #11
0
void NiceConnection::gatheringDone(uint stream_id) {
    int currentCompId = 1;
    lcands = nice_agent_get_local_candidates(agent_, stream_id, currentCompId++);
    NiceCandidate *cand;
    GSList* iterator;
    gchar *ufrag = NULL, *upass = NULL;
    nice_agent_get_local_credentials(agent_, stream_id, &ufrag, &upass);
    //////False candidate for testing when there is no network (like in the train) :)
    /*
    	NiceCandidate* thecandidate = nice_candidate_new(NICE_CANDIDATE_TYPE_HOST);
    	NiceAddress* naddr = nice_address_new();
    	nice_address_set_from_string(naddr, "127.0.0.1");
    	nice_address_set_port(naddr, 50000);
    	thecandidate->addr = *naddr;
    	char* uname = (char*) malloc(50);
    	char* pass = (char*) malloc(50);
    	sprintf(thecandidate->foundation, "%s", "1");
    	sprintf(uname, "%s", "Pedro");
    	sprintf(pass, "%s", "oooo");

    	thecandidate->username = uname;
    	thecandidate->password = pass;
    	thecandidate->stream_id = (guint) 1;
    	thecandidate->component_id = 1;
    	thecandidate->priority = 1000;
    	thecandidate->transport = NICE_CANDIDATE_TRANSPORT_UDP;
    	lcands = g_slist_append(lcands, thecandidate);
    */

    //	ELOG_DEBUG("gathering done %u",stream_id);
    //ELOG_DEBUG("Candidates---------------------------------------------------->");
    while (lcands != NULL) {
        for (iterator = lcands; iterator; iterator = iterator->next) {
            char address[40], baseAddress[40];
            cand = (NiceCandidate*) iterator->data;
            nice_address_to_string(&cand->addr, address);
            nice_address_to_string(&cand->base_addr, baseAddress);
            if (strstr(address, ":") != NULL) {
                ELOG_DEBUG("Ignoring IPV6 candidate %s", address);
                continue;
            }
//			ELOG_DEBUG("foundation %s", cand->foundation);
//			ELOG_DEBUG("compid %u", cand->component_id);
//			ELOG_DEBUG("stream_id %u", cand->stream_id);
//			ELOG_DEBUG("priority %u", cand->priority);
//			ELOG_DEBUG("username %s", cand->username);
//			ELOG_DEBUG("password %s", cand->password);
            CandidateInfo cand_info;
            cand_info.componentId = cand->component_id;
            cand_info.foundation = cand->foundation;
            cand_info.priority = cand->priority;
            cand_info.hostAddress = std::string(address);
            cand_info.hostPort = nice_address_get_port(&cand->addr);
            cand_info.mediaType = mediaType;

            /*
             *   NICE_CANDIDATE_TYPE_HOST,
             * 	  NICE_CANDIDATE_TYPE_SERVER_REFLEXIVE,
             *	  NICE_CANDIDATE_TYPE_PEER_REFLEXIVE,
             * 	  NICE_CANDIDATE_TYPE_RELAYED,
             */
            switch (cand->type) {
            case NICE_CANDIDATE_TYPE_HOST:
                cand_info.hostType = HOST;
                break;
            case NICE_CANDIDATE_TYPE_SERVER_REFLEXIVE:
                cand_info.hostType = SRLFX;
                cand_info.baseAddress = std::string(baseAddress);
                cand_info.basePort = nice_address_get_port(&cand->base_addr);
                break;
            case NICE_CANDIDATE_TYPE_PEER_REFLEXIVE:
                cand_info.hostType = PRFLX;
                break;
            case NICE_CANDIDATE_TYPE_RELAYED:
                ELOG_DEBUG("WARNING TURN NOT IMPLEMENTED YET");
                cand_info.hostType = RELAY;
                break;
            default:
                break;
            }
            cand_info.netProtocol = "udp";
            cand_info.transProtocol = std::string(*transportName);

            cand_info.username = std::string(ufrag);

            cand_info.password = std::string(upass);
            /*
            if (cand->username)
            	cand_info.username = std::string(cand->username);
            else
            	cand_info.username = std::string("(null)");

            if (cand->password)
            	cand_info.password = std::string(cand->password);
            else
            	cand_info.password = std::string("(null)");
            */

            localCandidates->push_back(cand_info);
        }
        lcands = nice_agent_get_local_candidates(agent_, stream_id,
                 currentCompId++);
    }
    ELOG_INFO("candidate_gathering done with %d candidates", localCandidates->size());

    if (localCandidates->size()==0) {
        ELOG_WARN("No local candidates found, check your network connection");
        exit(0);
    }
    updateIceState(NICE_CANDIDATES_GATHERED);
}
Example #12
0
int main (void)
{
  NiceAgent *lagent, *ragent;      /* agent's L and R */
  NiceAddress baseaddr;
  guint timer_id;
  GSList *cands, *i;
  guint ls_id, rs_id;

  g_type_init ();
  g_thread_init (NULL);
  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);
  ragent = nice_agent_new (g_main_loop_get_context (global_mainloop),
      NICE_COMPATIBILITY_GOOGLE);

  if (!nice_address_set_from_string (&baseaddr, "127.0.0.1"))
    g_assert_not_reached ();
  nice_agent_add_local_address (lagent, &baseaddr);
  nice_agent_add_local_address (ragent, &baseaddr);

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

  g_signal_connect (G_OBJECT (lagent), "candidate-gathering-done",
      G_CALLBACK (cb_candidate_gathering_done), GUINT_TO_POINTER(1));
  g_signal_connect (G_OBJECT (ragent), "candidate-gathering-done",
      G_CALLBACK (cb_candidate_gathering_done), GUINT_TO_POINTER (2));
  g_signal_connect (G_OBJECT (lagent), "component-state-changed",
      G_CALLBACK (cb_component_state_changed), GUINT_TO_POINTER (1));
  g_signal_connect (G_OBJECT (ragent), "component-state-changed",
      G_CALLBACK (cb_component_state_changed), GUINT_TO_POINTER (2));
  g_signal_connect (G_OBJECT (lagent), "new-selected-pair",
      G_CALLBACK (cb_new_selected_pair), GUINT_TO_POINTER(1));
  g_signal_connect (G_OBJECT (ragent), "new-selected-pair",
      G_CALLBACK (cb_new_selected_pair), GUINT_TO_POINTER (2));
  g_signal_connect (G_OBJECT (lagent), "new-candidate",
      G_CALLBACK (cb_new_candidate), GUINT_TO_POINTER (1));
  g_signal_connect (G_OBJECT (ragent), "new-candidate",
      G_CALLBACK (cb_new_candidate), GUINT_TO_POINTER (2));
  g_signal_connect (G_OBJECT (lagent), "initial-binding-request-received",
      G_CALLBACK (cb_initial_binding_request_received),
      GUINT_TO_POINTER (1));
  g_signal_connect (G_OBJECT (ragent), "initial-binding-request-received",
      G_CALLBACK (cb_initial_binding_request_received),
      GUINT_TO_POINTER (2));

  /* step: run test */
  g_debug ("test-dribble: running test");

  /* step: initialize variables modified by the callbacks */
  global_components_ready = 0;
  global_components_ready_exit = 2;
  global_components_failed = 0;
  global_components_failed_exit = 0;
  global_lagent_gathering_done = FALSE;
  global_ragent_gathering_done = FALSE;
  global_lagent_ibr_received =
    global_ragent_ibr_received = FALSE;
  global_lagent_cands =
    global_ragent_cands = 0;

  g_object_set (G_OBJECT (lagent), "controlling-mode", TRUE, NULL);
  g_object_set (G_OBJECT (ragent), "controlling-mode", FALSE, NULL);

  /* step: add one stream, with RTP+RTCP components, to each agent */
  ls_id = nice_agent_add_stream (lagent, 1);

  rs_id = nice_agent_add_stream (ragent, 1);
  g_assert (ls_id > 0);
  g_assert (rs_id > 0);


  nice_agent_gather_candidates (lagent, ls_id);
  nice_agent_gather_candidates (ragent, rs_id);

  /* 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,
      GUINT_TO_POINTER (1));
  nice_agent_attach_recv (ragent, rs_id, NICE_COMPONENT_TYPE_RTP,
      g_main_loop_get_context (global_mainloop), cb_nice_recv,
      GUINT_TO_POINTER (2));

  /* step: run mainloop until local candidates are ready
   *       (see timer_cb() above) */
  if (global_lagent_gathering_done != TRUE ||
      global_ragent_gathering_done != TRUE) {
    g_debug ("test-dribble: Added streams, running mainloop until 'candidate-gathering-done'...");
    g_main_loop_run (global_mainloop);
    g_assert (global_lagent_gathering_done == TRUE);
    g_assert (global_ragent_gathering_done == TRUE);
  }

  {
      gchar *ufrag = NULL, *password = NULL;
      nice_agent_get_local_credentials(lagent, ls_id, &ufrag, &password);
      nice_agent_set_remote_credentials (ragent,
          rs_id, ufrag, password);
      g_free (ufrag);
      g_free (password);
      nice_agent_get_local_credentials(ragent, rs_id, &ufrag, &password);
      nice_agent_set_remote_credentials (lagent,
          ls_id, ufrag, password);
      g_free (ufrag);
      g_free (password);
  }
  cands = nice_agent_get_local_candidates (ragent, rs_id, NICE_COMPONENT_TYPE_RTP);
  nice_agent_set_remote_candidates (lagent, ls_id, NICE_COMPONENT_TYPE_RTP, cands);
  for (i = cands; i; i = i->next)
    nice_candidate_free ((NiceCandidate *) i->data);
  g_slist_free (cands);
  cands = nice_agent_get_local_candidates (lagent, ls_id, NICE_COMPONENT_TYPE_RTP);
  nice_agent_set_remote_candidates (ragent, rs_id, NICE_COMPONENT_TYPE_RTP, cands);
  for (i = cands; i; i = i->next)
    nice_candidate_free ((NiceCandidate *) i->data);
  g_slist_free (cands);

  g_debug ("test-dribble: Set properties, next running mainloop until connectivity checks succeed...");

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

  /* note: verify that STUN binding requests were sent */
  g_assert (global_lagent_ibr_received == TRUE);
  g_assert (global_ragent_ibr_received == TRUE);

  g_assert (global_lagent_state == NICE_COMPONENT_STATE_READY);
  g_assert (global_ragent_state == NICE_COMPONENT_STATE_READY);
  /* note: verify that correct number of local candidates were reported */
  g_assert (global_lagent_cands == 1);
  g_assert (global_ragent_cands == 1);

  g_debug ("test-dribble: agents are ready.. now adding new buggy candidate");

  g_timeout_add (500, quit_loop_cb, NULL);
  g_main_loop_run (global_mainloop);

  global_components_ready--;

  cands = nice_agent_get_local_candidates (ragent, rs_id, NICE_COMPONENT_TYPE_RTP);
  nice_address_set_port(&((NiceCandidate *) cands->data)->addr, 80);
  nice_agent_set_remote_candidates (lagent, ls_id, NICE_COMPONENT_TYPE_RTP, cands);
  for (i = cands; i; i = i->next)
    nice_candidate_free ((NiceCandidate *) i->data);
  g_slist_free (cands);

  g_assert (global_lagent_state == NICE_COMPONENT_STATE_CONNECTED);
  g_main_loop_run (global_mainloop);
  g_assert (global_lagent_state == NICE_COMPONENT_STATE_READY);

  /*
  g_debug ("test-dribble: buggy candidate worked, testing lower priority cand");

  cands = nice_agent_get_local_candidates (ragent, rs_id, NICE_COMPONENT_TYPE_RTP);
  nice_address_set_port(&((NiceCandidate *) cands->data)->addr, 80);
  ((NiceCandidate *) cands->data)->priority -= 100;
  nice_agent_set_remote_candidates (lagent, ls_id, NICE_COMPONENT_TYPE_RTP, cands);
  for (i = cands; i; i = i->next)
    nice_candidate_free ((NiceCandidate *) i->data);
  g_slist_free (cands);

  g_assert (global_lagent_state == NICE_COMPONENT_STATE_READY);*/

  /* note: test payload send and receive */
  global_ragent_read = 0;
  g_assert (nice_agent_send (lagent, ls_id, 1, 16, "1234567812345678") == 16);
  g_main_loop_run (global_mainloop);
  g_assert (global_ragent_read == 16);

  g_debug ("test-dribble: Ran mainloop, removing streams...");

  /* step: clean up resources and exit */

  nice_agent_remove_stream (lagent, ls_id);
  nice_agent_remove_stream (ragent, rs_id);
  priv_print_global_status ();
  g_assert (global_lagent_state == NICE_COMPONENT_STATE_READY);
  g_assert (global_ragent_state == NICE_COMPONENT_STATE_READY);
  /* note: verify that correct number of local candidates were reported */
  g_assert (global_lagent_cands == 1);
  g_assert (global_ragent_cands == 1);


  g_object_unref (lagent);
  g_object_unref (ragent);

  g_main_loop_unref (global_mainloop);
  global_mainloop = NULL;

  g_source_remove (timer_id);

  return 0;
}
Example #13
0
gint
main (void)
{
  NiceAgent *agent;
  NiceAddress addr_local, addr_remote;
  NiceCandidate *candidate;
  GSList *candidates, *i;
  guint stream_id;

#ifdef G_OS_WIN32
  WSADATA w;

  WSAStartup(0x0202, &w);
#endif

  nice_address_init (&addr_local);
  nice_address_init (&addr_remote);
  g_type_init ();

#if !GLIB_CHECK_VERSION(2,31,8)
  g_thread_init(NULL);
#endif

  g_assert (nice_address_set_from_string (&addr_local, "127.0.0.1"));
  g_assert (nice_address_set_from_string (&addr_remote, "127.0.0.1"));
  nice_address_set_port (&addr_remote, 2345);

  agent = nice_agent_new ( NULL, NICE_COMPATIBILITY_RFC5245);

  g_assert (agent->local_addresses == NULL);

  /* add one local address */
  nice_agent_add_local_address (agent, &addr_local);

  g_assert (agent->local_addresses != NULL);
  g_assert (g_slist_length (agent->local_addresses) == 1);
  g_assert (nice_address_equal (agent->local_addresses->data, &addr_local));

  /* add a stream */
  stream_id = nice_agent_add_stream (agent, 1);
  nice_agent_gather_candidates (agent, stream_id);

  /* adding a stream should cause host candidates to be generated */
  candidates = nice_agent_get_local_candidates (agent, stream_id, 1);
  g_assert (g_slist_length (candidates) == 1);
  candidate = candidates->data;
  /* socket manager uses random port number */
  nice_address_set_port (&addr_local, 1);
  nice_address_set_port (&(candidate->addr), 1);
  g_assert (nice_address_equal (&(candidate->addr), &addr_local));
  g_assert (strncmp (candidate->foundation, "1", 1) == 0);
  for (i = candidates; i; i = i->next)
    nice_candidate_free ((NiceCandidate *) i->data);
  g_slist_free (candidates);

  /* clean up */
  g_object_unref (agent);
#ifdef G_OS_WIN32
  WSACleanup();
#endif
  return 0;
}
Example #14
0
static int run_full_test (NiceAgent *lagent, NiceAgent *ragent, NiceAddress *baseaddr, guint ready, guint failed)
{
  guint ls_id, rs_id;
  gint ret;

  /* XXX: dear compiler, this is for you */
  (void)baseaddr;

  /* step: initialize variables modified by the callbacks */
  global_components_ready = 0;
  global_components_ready_exit = ready;
  global_components_failed = 0;
  global_components_failed_exit = failed;
  global_lagent_gathering_done = FALSE;
  global_ragent_gathering_done = FALSE;
  global_lagent_ibr_received =
    global_ragent_ibr_received = FALSE;
  global_lagent_cands =
    global_ragent_cands = 0;
  global_ready_reached = FALSE;

  g_object_set (G_OBJECT (lagent), "controlling-mode", TRUE, NULL);
  g_object_set (G_OBJECT (ragent), "controlling-mode", FALSE, NULL);

  /* step: add one stream, with RTP+RTCP components, to each agent */
  ls_id = nice_agent_add_stream (lagent, 2);

  rs_id = nice_agent_add_stream (ragent, 2);
  g_assert (ls_id > 0);
  g_assert (rs_id > 0);

  /* Gather candidates */
  g_assert (nice_agent_gather_candidates (lagent, ls_id) == TRUE);
  g_assert (nice_agent_gather_candidates (ragent, rs_id) == TRUE);

  {
    GSList *cands = NULL, *i;
    NiceCandidate *cand = NULL;

    cands = nice_agent_get_local_candidates (lagent, ls_id, 1);
    g_assert (g_slist_length (cands) == 2);
    cand = cands->data;
    g_assert (cand->type == NICE_CANDIDATE_TYPE_HOST);
    g_assert (cand->transport == NICE_CANDIDATE_TRANSPORT_TCP_ACTIVE ||
              cand->transport == NICE_CANDIDATE_TRANSPORT_TCP_PASSIVE);
    cand = cands->next->data;
    g_assert (cand->type == NICE_CANDIDATE_TYPE_HOST);
    g_assert (cand->transport == NICE_CANDIDATE_TRANSPORT_TCP_ACTIVE ||
              cand->transport == NICE_CANDIDATE_TRANSPORT_TCP_PASSIVE);
    for (i = cands; i; i = i->next)
      nice_candidate_free ((NiceCandidate *) i->data);
    g_slist_free (cands);
  }

  /* 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,
      GUINT_TO_POINTER (1));
  nice_agent_attach_recv (lagent, ls_id, NICE_COMPONENT_TYPE_RTCP,
      g_main_loop_get_context (global_mainloop), cb_nice_recv,
      GUINT_TO_POINTER (1));
  nice_agent_attach_recv (ragent, rs_id, NICE_COMPONENT_TYPE_RTP,
      g_main_loop_get_context (global_mainloop), cb_nice_recv,
      GUINT_TO_POINTER (2));
  nice_agent_attach_recv (ragent, rs_id, NICE_COMPONENT_TYPE_RTCP,
      g_main_loop_get_context (global_mainloop), cb_nice_recv,
      GUINT_TO_POINTER (2));

  /* step: run mainloop until local candidates are ready
   *       (see timer_cb() above) */
  if (global_lagent_gathering_done != TRUE ||
      global_ragent_gathering_done != TRUE) {
    g_debug ("test-icetcp: Added streams, running mainloop until 'candidate-gathering-done'...");
    g_main_loop_run (global_mainloop);
    g_assert (global_lagent_gathering_done == TRUE);
    g_assert (global_ragent_gathering_done == TRUE);
  }

  set_credentials (lagent, ls_id, ragent, rs_id);

  /* step: pass the remote candidates to agents  */
  set_candidates (ragent, rs_id, lagent, ls_id, NICE_COMPONENT_TYPE_RTP);
  set_candidates (ragent, rs_id, lagent, ls_id, NICE_COMPONENT_TYPE_RTCP);
  set_candidates (lagent, ls_id, ragent, rs_id, NICE_COMPONENT_TYPE_RTP);
  set_candidates (lagent, ls_id, ragent, rs_id, NICE_COMPONENT_TYPE_RTCP);

  g_debug ("test-icetcp: Set properties, next running mainloop until connectivity checks succeed...");

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

  /* note: verify that STUN binding requests were sent */
  g_assert (global_lagent_ibr_received == TRUE);
  g_assert (global_ragent_ibr_received == TRUE);

  /* note: test payload send and receive */
  global_ragent_read = 0;
  ret = nice_agent_send (lagent, ls_id, 1, 16, "1234567812345678");
  if (ret == -1)
  {
    gboolean reliable = FALSE;
    g_object_get (G_OBJECT (lagent), "reliable", &reliable, NULL);
    g_debug ("Sending data returned -1 in %s mode", reliable?"Reliable":"Non-reliable");
    if (reliable) {
      gulong signal_handler;
      signal_handler = g_signal_connect (G_OBJECT (lagent),
          "reliable-transport-writable", G_CALLBACK (cb_writable), NULL);
      g_debug ("Running mainloop until transport is writable");
      g_main_loop_run (global_mainloop);
      g_signal_handler_disconnect(G_OBJECT (lagent), signal_handler);

      ret = nice_agent_send (lagent, ls_id, 1, 16, "1234567812345678");
    }
  }
  g_debug ("Sent %d bytes", ret);
  g_assert (ret == 16);
  g_main_loop_run (global_mainloop);
  g_assert (global_ragent_read == 16);

  g_debug ("test-icetcp: Ran mainloop, removing streams...");

  /* step: clean up resources and exit */

  nice_agent_remove_stream (lagent, ls_id);
  nice_agent_remove_stream (ragent, rs_id);

  return 0;
}
Example #15
0
  void NiceConnection::getCandidate(uint stream_id, uint component_id, const std::string &foundation) {
    GSList* lcands = nice_agent_get_local_candidates(agent_, stream_id, component_id);
    // We only want to get the new candidates
    int listLength = g_slist_length(lcands);
    ELOG_DEBUG("List length %u, candsDelivered %u", listLength, candsDelivered_);
    if (candsDelivered_ <= g_slist_length(lcands)){
      lcands = g_slist_nth(lcands, (candsDelivered_));
    }
    ELOG_DEBUG("getCandidate %u", g_slist_length(lcands));
    for (GSList* iterator = lcands; iterator; iterator = iterator->next) {
      ELOG_DEBUG("Candidate");
      char address[NICE_ADDRESS_STRING_LEN], baseAddress[NICE_ADDRESS_STRING_LEN];
      NiceCandidate *cand = (NiceCandidate*) iterator->data;
      nice_address_to_string(&cand->addr, address);
      nice_address_to_string(&cand->base_addr, baseAddress);
      candsDelivered_++;
      if (strstr(address, ":") != NULL) { // We ignore IPv6 candidates at this point
        continue;
      }
      CandidateInfo cand_info;
      cand_info.componentId = cand->component_id;
      cand_info.foundation = cand->foundation;
      cand_info.priority = cand->priority;
      cand_info.hostAddress = std::string(address);
      cand_info.hostPort = nice_address_get_port(&cand->addr);
      cand_info.mediaType = mediaType;

      /*
       *   NICE_CANDIDATE_TYPE_HOST,
       *    NICE_CANDIDATE_TYPE_SERVER_REFLEXIVE,
       *    NICE_CANDIDATE_TYPE_PEER_REFLEXIVE,
       *    NICE_CANDIDATE_TYPE_RELAYED,
       */
      switch (cand->type) {
        case NICE_CANDIDATE_TYPE_HOST:
          cand_info.hostType = HOST;
          break;
        case NICE_CANDIDATE_TYPE_SERVER_REFLEXIVE:
          cand_info.hostType = SRFLX;
          cand_info.rAddress = std::string(baseAddress);
          cand_info.rPort = nice_address_get_port(&cand->base_addr);
          break;
        case NICE_CANDIDATE_TYPE_PEER_REFLEXIVE:
          cand_info.hostType = PRFLX;
          break;
        case NICE_CANDIDATE_TYPE_RELAYED:
          char turnAddres[NICE_ADDRESS_STRING_LEN];
          ELOG_DEBUG("TURN LOCAL CANDIDATE");
          nice_address_to_string(&cand->turn->server,turnAddres);
          ELOG_DEBUG("address %s", address);
          ELOG_DEBUG("baseAddress %s", baseAddress);
          ELOG_DEBUG("stream_id %u", cand->stream_id);
          ELOG_DEBUG("priority %u", cand->priority);
          ELOG_DEBUG("TURN ADDRESS %s", turnAddres);

          cand_info.hostType = RELAY;
          cand_info.rAddress = std::string(baseAddress);
          cand_info.rPort = nice_address_get_port(&cand->base_addr);
          break;
        default:
          break;
      }
      cand_info.netProtocol = "udp";
      cand_info.transProtocol = std::string(*transportName.get());
      cand_info.username = ufrag_;
      cand_info.password = upass_;
      //localCandidates->push_back(cand_info);
      this->getNiceListener()->onCandidate(cand_info, this);
    }
    // for nice_agent_get_local_candidates,  the caller owns the returned GSList as well as the candidates contained within it.
    // let's free everything in the list, as well as the list.
    g_slist_free_full(lcands, (GDestroyNotify)&nice_candidate_free);

  }
Example #16
0
  void NiceConnection::gatheringDone(uint stream_id) {
    if (this->checkIceState() >= NICE_FINISHED) {
      ELOG_DEBUG("gathering Done after FINISHED");
      nice_agent_remove_stream (agent_,1);
      return;
    }
    ELOG_DEBUG("Gathering Done %p", this);
    int currentCompId = 1;
    GSList* lcands = nice_agent_get_local_candidates(agent_, stream_id, currentCompId++);
    gchar *ufrag = NULL, *upass = NULL;
    nice_agent_get_local_credentials(agent_, stream_id, &ufrag, &upass);
    while (lcands != NULL) {
      for (GSList* iterator = lcands; iterator; iterator = iterator->next) {
        char address[NICE_ADDRESS_STRING_LEN], baseAddress[NICE_ADDRESS_STRING_LEN];
        NiceCandidate *cand = (NiceCandidate*) iterator->data;
        nice_address_to_string(&cand->addr, address);
        nice_address_to_string(&cand->base_addr, baseAddress);
        if (strstr(address, ":") != NULL) {
          ELOG_DEBUG("Ignoring IPV6 candidate %s %p", address, this);
          continue;
        }

        CandidateInfo cand_info;
        cand_info.componentId = cand->component_id;
        cand_info.foundation = cand->foundation;
        cand_info.priority = cand->priority;
        cand_info.hostAddress = std::string(address);
        cand_info.hostPort = nice_address_get_port(&cand->addr);
        cand_info.mediaType = mediaType;

        /*
         *   NICE_CANDIDATE_TYPE_HOST,
         * 	  NICE_CANDIDATE_TYPE_SERVER_REFLEXIVE,
         *	  NICE_CANDIDATE_TYPE_PEER_REFLEXIVE,
         * 	  NICE_CANDIDATE_TYPE_RELAYED,
         */
        switch (cand->type) {
          case NICE_CANDIDATE_TYPE_HOST:
            cand_info.hostType = HOST;
            break;
          case NICE_CANDIDATE_TYPE_SERVER_REFLEXIVE:
            cand_info.hostType = SRFLX;
            cand_info.rAddress = std::string(baseAddress);
            cand_info.rPort = nice_address_get_port(&cand->base_addr);
            break;
          case NICE_CANDIDATE_TYPE_PEER_REFLEXIVE:
            cand_info.hostType = PRFLX;
            break;
          case NICE_CANDIDATE_TYPE_RELAYED:
            char turnAddres[NICE_ADDRESS_STRING_LEN];
            ELOG_DEBUG("TURN LOCAL CANDIDATE");
            nice_address_to_string(&cand->turn->server,turnAddres);
        		ELOG_DEBUG("address %s", address);
        		ELOG_DEBUG("baseAddress %s", baseAddress);
        		ELOG_DEBUG("stream_id %u", cand->stream_id);
        		ELOG_DEBUG("priority %u", cand->priority);
        		ELOG_DEBUG("TURN ADDRESS %s", turnAddres);
           
            cand_info.hostType = RELAY;
            cand_info.rAddress = std::string(baseAddress);
            cand_info.rPort = nice_address_get_port(&cand->base_addr);
            break;
          default:
            break;
        }
        cand_info.netProtocol = "udp";
        cand_info.transProtocol = std::string(*transportName.get());
        cand_info.username = std::string(ufrag);
        cand_info.password = std::string(upass);
        localCandidates->push_back(cand_info);
      }
      // for nice_agent_get_local_candidates,  the caller owns the returned GSList as well as the candidates contained within it.
      // let's free everything in the list, as well as the list.
      g_slist_free_full(lcands, (GDestroyNotify)&nice_candidate_free);
      lcands = nice_agent_get_local_candidates(agent_, stream_id, currentCompId++);
    }
    // According to libnice, this is how these must be free'd
    g_free(ufrag);
    g_free(upass);

    ELOG_INFO("candidate_gathering done with %lu candidates %p", localCandidates->size(), this);

    if (localCandidates->size()==0){
      ELOG_WARN("No local candidates found, check your network connection");
      exit(0);
    }
    updateIceState(NICE_CANDIDATES_GATHERED);
  }
Example #17
0
GSList* LibNiceInterfaceImpl::NiceAgentGetLocalCandidates(NiceAgent* agent, unsigned int stream_id,
        unsigned int component_id) {
    return nice_agent_get_local_candidates(agent, stream_id, component_id);
}
Example #18
0
static int run_full_test_control_conflict (NiceAgent *lagent, NiceAgent *ragent, NiceAddress *baseaddr, gboolean role)
{
  NiceAddress laddr, raddr;   
  NiceCandidate cdes;
  GSList *cands, *i;
  guint ls_id, rs_id;

  init_candidate (&cdes);

  /* XXX: dear compiler, this is for you */
  (void)baseaddr;

  global_components_ready = 0;
  global_components_ready_exit = 2;
  global_components_failed = 0;
  global_components_failed_exit = 0;
  global_lagent_gathering_done =
    global_ragent_gathering_done = FALSE;
  global_lagent_cands = 
    global_ragent_cands = 0;
  global_lagent_ibr_received =
    global_ragent_ibr_received = FALSE;

  g_object_set (G_OBJECT (lagent), "controlling-mode", role, NULL);
  g_object_set (G_OBJECT (ragent), "controlling-mode", role, NULL);

  /* step: add one stream, with one component, to each agent */
  ls_id = nice_agent_add_stream (lagent, 1);

  rs_id = nice_agent_add_stream (ragent, 1);
  g_assert (ls_id > 0);
  g_assert (rs_id > 0);
#if USE_TURN
  nice_agent_set_relay_info(lagent, ls_id, 1,
      TURN_IP, TURN_PORT, TURN_USER, TURN_PASS, TURN_TYPE);
  nice_agent_set_relay_info(ragent, rs_id, 1,
      TURN_IP, TURN_PORT, TURN_USER, TURN_PASS, TURN_TYPE);
#endif

  nice_agent_gather_candidates (lagent, ls_id);
  nice_agent_gather_candidates (ragent, rs_id);

  /* 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,
      GUINT_TO_POINTER (1));
  nice_agent_attach_recv (ragent, rs_id, NICE_COMPONENT_TYPE_RTP,
      g_main_loop_get_context (global_mainloop), cb_nice_recv,
      GUINT_TO_POINTER (2));

  /* step: run mainloop until local candidates are ready 
   *       (see timer_cb() above) */
  if (global_lagent_gathering_done != TRUE ||
      global_ragent_gathering_done != TRUE) {
    g_debug ("test-fullmode: Added streams, running mainloop until 'candidate-gathering-done'...");
    g_main_loop_run (global_mainloop);
    g_assert (global_lagent_gathering_done == TRUE);
    g_assert (global_ragent_gathering_done == TRUE);
  }

  /* step: find out the local candidates of each agent */
  cands = nice_agent_get_local_candidates(lagent, ls_id, NICE_COMPONENT_TYPE_RTP);
  for (i = cands; i; i = i->next) {
    NiceCandidate *cand = i->data;
    if (cand) {
      g_debug ("test-fullmode: local port L %u",
               nice_address_get_port (&cand->addr));
      laddr = cand->addr;
    }
  }
  for (i = cands; i; i = i->next)
    nice_candidate_free ((NiceCandidate *) i->data);
  g_slist_free (cands);

  cands = nice_agent_get_local_candidates(ragent, rs_id, NICE_COMPONENT_TYPE_RTP);
  for (i = cands; i; i = i->next) {
    NiceCandidate *cand = i->data;
    if (cand) {
      g_debug ("test-fullmode: local port R %u",
               nice_address_get_port (&cand->addr));
      raddr = cand->addr;
    }
  }
  for (i = cands; i; i = i->next)
    nice_candidate_free ((NiceCandidate *) i->data);
  g_slist_free (cands);
  g_debug ("test-fullmode: Got local candidates...");
 
  /* step: pass the remote candidates to agents  */
  cands = g_slist_append (NULL, &cdes);
  {
      gchar *ufrag = NULL, *password = NULL;
      nice_agent_get_local_credentials(lagent, ls_id, &ufrag, &password);
      nice_agent_set_remote_credentials (ragent,
					 rs_id, ufrag, password);
      g_free (ufrag);
      g_free (password);
      nice_agent_get_local_credentials(ragent, rs_id, &ufrag, &password);
      nice_agent_set_remote_credentials (lagent,
					 ls_id, ufrag, password);
      g_free (ufrag);
      g_free (password);
  }
  cdes.addr = raddr;
  nice_agent_set_remote_candidates (lagent, ls_id, NICE_COMPONENT_TYPE_RTP, cands);
  cdes.addr = laddr;
  nice_agent_set_remote_candidates (ragent, rs_id, NICE_COMPONENT_TYPE_RTP, cands);
  g_slist_free (cands);

  g_debug ("test-fullmode: Set properties, next running mainloop until connectivity checks succeed...");

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

  /* note: verify that correct number of local candidates were reported */
  g_assert (global_lagent_cands == 1);
  g_assert (global_ragent_cands == 1);

  g_debug ("test-fullmode: Ran mainloop, removing streams...");

  /* step: clean up resources and exit */

  nice_agent_remove_stream (lagent, ls_id);
  nice_agent_remove_stream (ragent, rs_id);

  return 0;
}