KmsIceNiceAgent * kms_ice_nice_agent_new (GMainContext * context, KmsWebrtcSession * session) { GObject *obj; KmsIceNiceAgent *agent_object; obj = g_object_new (KMS_TYPE_ICE_NICE_AGENT, NULL); agent_object = KMS_ICE_NICE_AGENT (obj); agent_object->priv->context = context; agent_object->priv->agent = nice_agent_new (agent_object->priv->context, NICE_COMPATIBILITY_RFC5245); agent_object->priv->session = session; g_object_set (agent_object->priv->agent, "upnp", FALSE, NULL); g_signal_connect (agent_object->priv->agent, "new-candidate", G_CALLBACK (kms_ice_nice_agent_new_candidate), agent_object); g_signal_connect (agent_object->priv->agent, "candidate-gathering-done", G_CALLBACK (kms_ice_nice_agent_gathering_done), agent_object); g_signal_connect (agent_object->priv->agent, "component-state-changed", G_CALLBACK (kms_ice_nice_agent_component_state_change), agent_object); return agent_object; }
void* _video_send_main() { printf("[send video]\n"); //NiceAgent *agent; //guint streamID = 0; //nice_debug_enable(TRUE); /* Init agent */ RpiData_SendVideo->agent = nice_agent_new(g_main_loop_get_context (gloop), NICE_COMPATIBILITY_RFC5245); if (RpiData_SendVideo->agent == NULL) g_error("Failed to create agent"); g_object_set(G_OBJECT(RpiData_SendVideo->agent), "stun-server", STUNSR_ADDR, NULL); g_object_set(G_OBJECT(RpiData_SendVideo->agent), "stun-server-port", STUNSR_PORT, NULL); g_object_set(G_OBJECT(RpiData_SendVideo->agent), "controlling-mode", CONTROLLING_MODE, NULL); g_signal_connect(G_OBJECT(RpiData_SendVideo->agent), "candidate-gathering-done", G_CALLBACK( _video_send_cb_candidate_gathering_done), NULL); //g_signal_connect(G_OBJECT(agent), "new-selected-pair", //G_CALLBACK( _video_receive_cb_new_selected_pair), NULL); RpiData_SendVideo->streamID = nice_agent_add_stream(RpiData_SendVideo->agent, 1); if (RpiData_SendVideo->streamID == 0) g_error("Failed to add stream"); /* Init Gstreamer */ _video_send_init_gstreamer(RpiData_SendVideo->agent, RpiData_SendVideo->streamID); nice_agent_attach_recv(RpiData_SendVideo->agent, RpiData_SendVideo->streamID, 1, g_main_loop_get_context (gloop), _video_send_cb_nice_recv, NULL); /* Start gathering local candidates */ if (!nice_agent_gather_candidates(RpiData_SendVideo->agent, RpiData_SendVideo->streamID)) g_error("Failed to start candidate gathering"); printf("[send video] Start Gathering!\n"); printf("[send video] Agent = %d!\n", RpiData_SendVideo->agent); }
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; }
JNIEXPORT jlong JNICALL CAST_JNI(createAgentNative,jlong gloopLong,jint useReliable) { GMainLoop* tmp_gloop = (GMainLoop*)gloopLong; NiceAgent* tmp_agent = NULL; // initialize the Ctx AgentCtx* agentCtx = (AgentCtx*) calloc(1,sizeof(AgentCtx)); agentCtx->hasStateObserver = 0; agentCtx->stateObserverObj = 0; agentCtx->cbCandidateGatheringDoneId=0; agentCtx->cbComponentStateChangedId=0; for(int i=0;i<MAX_COMPONENT;i++) { agentCtx->recvCallbackCtx[i] = NULL; } agentCtx->gloop = tmp_gloop; // Create the nice agent if(useReliable==1) { tmp_agent = nice_agent_new_reliable(g_main_loop_get_context (tmp_gloop), NICE_COMPATIBILITY_RFC5245); } else { tmp_agent = nice_agent_new(g_main_loop_get_context (tmp_gloop), NICE_COMPATIBILITY_RFC5245); } if (tmp_agent == NULL) { LOGD("Failed to create agent"); return 0; } agentCtx->agent = tmp_agent; // Connect to the signals g_signal_connect(tmp_agent, "candidate-gathering-done", G_CALLBACK(cb_candidate_gathering_done), agentCtx); g_signal_connect(tmp_agent, "component-state-changed", G_CALLBACK(cb_component_state_changed), agentCtx); g_signal_connect(tmp_agent, "new-selected-pair", G_CALLBACK(cb_new_selected_pair), agentCtx); return (long)agentCtx; }
int main (void) { NiceAgent *agent; NiceAddress addr; #ifdef G_OS_WIN32 WSADATA w; WSAStartup(0x0202, &w); #endif nice_address_init (&addr); g_type_init (); g_thread_init (NULL); if (!nice_address_set_from_string (&addr, "127.0.0.1")) g_assert_not_reached (); agent = nice_agent_new (NULL, NICE_COMPATIBILITY_RFC5245); nice_agent_add_local_address (agent, &addr); g_assert (nice_agent_add_stream (agent, 1) == 1); g_assert (nice_agent_add_stream (agent, 10) == 2); g_assert (nice_agent_add_stream (agent, 2) == 3); g_assert (NULL != agent->streams); nice_agent_remove_stream (agent, 1); nice_agent_remove_stream (agent, 2); nice_agent_remove_stream (agent, 3); g_assert (NULL == agent->streams); g_object_unref (agent); #ifdef G_OS_WIN32 WSACleanup(); #endif return 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; }
static void * example_thread(void *data) { NiceAgent *agent; GIOChannel* io_stdin; guint stream_id; gchar *line = NULL; gchar *sdp, *sdp64; #ifdef G_OS_WIN32 io_stdin = g_io_channel_win32_new_fd(_fileno(stdin)); #else io_stdin = g_io_channel_unix_new(fileno(stdin)); #endif g_io_channel_set_flags(io_stdin, G_IO_FLAG_NONBLOCK, NULL); // Create the nice agent agent = nice_agent_new(g_main_loop_get_context (gloop), NICE_COMPATIBILITY_RFC5245); if (agent == NULL) g_error("Failed to create agent"); // Set the STUN settings and controlling mode if (stun_addr) { g_object_set(agent, "stun-server", stun_addr, NULL); g_object_set(agent, "stun-server-port", stun_port, NULL); } g_object_set(agent, "controlling-mode", controlling, NULL); // Connect to the signals g_signal_connect(agent, "candidate-gathering-done", G_CALLBACK(cb_candidate_gathering_done), NULL); g_signal_connect(agent, "component-state-changed", G_CALLBACK(cb_component_state_changed), NULL); // Create a new stream with one component stream_id = nice_agent_add_stream(agent, 1); if (stream_id == 0) g_error("Failed to add stream"); nice_agent_set_stream_name (agent, stream_id, "text"); // Attach to the component to receive the data // Without this call, candidates cannot be gathered nice_agent_attach_recv(agent, stream_id, 1, g_main_loop_get_context (gloop), cb_nice_recv, NULL); // Start gathering local candidates if (!nice_agent_gather_candidates(agent, stream_id)) g_error("Failed to start candidate gathering"); g_debug("waiting for candidate-gathering-done signal..."); g_mutex_lock(&gather_mutex); while (!exit_thread && !candidate_gathering_done) g_cond_wait(&gather_cond, &gather_mutex); g_mutex_unlock(&gather_mutex); if (exit_thread) goto end; // Candidate gathering is done. Send our local candidates on stdout sdp = nice_agent_generate_local_sdp (agent); printf("Generated SDP from agent :\n%s\n\n", sdp); printf("Copy the following line to remote client:\n"); sdp64 = g_base64_encode ((const guchar *)sdp, strlen (sdp)); printf("\n %s\n", sdp64); g_free (sdp); g_free (sdp64); // Listen on stdin for the remote candidate list printf("Enter remote data (single line, no wrapping):\n"); printf("> "); fflush (stdout); while (!exit_thread) { GIOStatus s = g_io_channel_read_line (io_stdin, &line, NULL, NULL, NULL); if (s == G_IO_STATUS_NORMAL) { gsize sdp_len; sdp = (gchar *) g_base64_decode (line, &sdp_len); // Parse remote candidate list and set it on the agent if (sdp && nice_agent_parse_remote_sdp (agent, sdp) > 0) { g_free (sdp); g_free (line); break; } else { fprintf(stderr, "ERROR: failed to parse remote data\n"); printf("Enter remote data (single line, no wrapping):\n"); printf("> "); fflush (stdout); } g_free (sdp); g_free (line); } else if (s == G_IO_STATUS_AGAIN) { g_usleep (100000); } } g_debug("waiting for state READY or FAILED signal..."); g_mutex_lock(&negotiate_mutex); while (!exit_thread && !negotiation_done) g_cond_wait(&negotiate_cond, &negotiate_mutex); g_mutex_unlock(&negotiate_mutex); if (exit_thread) goto end; // Listen to stdin and send data written to it printf("\nSend lines to remote (Ctrl-D to quit):\n"); printf("> "); fflush (stdout); while (!exit_thread) { GIOStatus s = g_io_channel_read_line (io_stdin, &line, NULL, NULL, NULL); if (s == G_IO_STATUS_NORMAL) { nice_agent_send(agent, stream_id, 1, strlen(line), line); g_free (line); printf("> "); fflush (stdout); } else if (s == G_IO_STATUS_AGAIN) { g_usleep (100000); } else { // Ctrl-D was pressed. nice_agent_send(agent, stream_id, 1, 1, "\0"); break; } } end: g_object_unref(agent); g_io_channel_unref (io_stdin); g_main_loop_quit (gloop); return NULL; }
soynetXMPPAgent* soy_net_xmpp_agent_construct (GType object_type) { soynetXMPPAgent * self = NULL; soynet_NetworkThread* _tmp0_ = NULL; GMainContext* _tmp1_ = NULL; NiceAgent* _tmp2_ = NULL; NiceAgent* _tmp3_ = NULL; NiceAgent* _tmp4_ = NULL; NiceAgent* _tmp5_ = NULL; NiceAgent* _tmp6_ = NULL; NiceAgent* _tmp7_ = NULL; NiceAgent* _tmp8_ = NULL; NiceAgent* _tmp9_ = NULL; NiceAgent* _tmp10_ = NULL; #line 64 "/home/jeff/Documents/libraries/libsoy/src/net/XMPPAgent.gs" self = (soynetXMPPAgent*) g_object_new (object_type, NULL); #line 65 "/home/jeff/Documents/libraries/libsoy/src/net/XMPPAgent.gs" _tmp0_ = soy_net__thread; #line 65 "/home/jeff/Documents/libraries/libsoy/src/net/XMPPAgent.gs" _tmp1_ = _tmp0_->context; #line 65 "/home/jeff/Documents/libraries/libsoy/src/net/XMPPAgent.gs" _tmp2_ = nice_agent_new (_tmp1_, (gint) NICE_COMPATIBILITY_RFC5245); #line 65 "/home/jeff/Documents/libraries/libsoy/src/net/XMPPAgent.gs" _g_object_unref0 (self->agent); #line 65 "/home/jeff/Documents/libraries/libsoy/src/net/XMPPAgent.gs" self->agent = _tmp2_; #line 66 "/home/jeff/Documents/libraries/libsoy/src/net/XMPPAgent.gs" _tmp3_ = self->agent; #line 66 "/home/jeff/Documents/libraries/libsoy/src/net/XMPPAgent.gs" g_object_set (_tmp3_, "stun-server", "132.177.123.13", NULL); #line 67 "/home/jeff/Documents/libraries/libsoy/src/net/XMPPAgent.gs" _tmp4_ = self->agent; #line 67 "/home/jeff/Documents/libraries/libsoy/src/net/XMPPAgent.gs" g_object_set (_tmp4_, "stun-server-port", (guint) 3478, NULL); #line 70 "/home/jeff/Documents/libraries/libsoy/src/net/XMPPAgent.gs" _tmp5_ = self->agent; #line 70 "/home/jeff/Documents/libraries/libsoy/src/net/XMPPAgent.gs" g_signal_connect_object (_tmp5_, "component-state-changed", (GCallback) _soy_net_xmpp_agent_component_state_changed_nice_agent_component_state_changed, self, 0); #line 71 "/home/jeff/Documents/libraries/libsoy/src/net/XMPPAgent.gs" _tmp6_ = self->agent; #line 71 "/home/jeff/Documents/libraries/libsoy/src/net/XMPPAgent.gs" g_signal_connect_object (_tmp6_, "initial-binding-request-received", (GCallback) _soy_net_xmpp_agent_initial_binding_request_received_nice_agent_initial_binding_request_received, self, 0); #line 72 "/home/jeff/Documents/libraries/libsoy/src/net/XMPPAgent.gs" _tmp7_ = self->agent; #line 72 "/home/jeff/Documents/libraries/libsoy/src/net/XMPPAgent.gs" g_signal_connect_object (_tmp7_, "new-candidate", (GCallback) _soy_net_xmpp_agent_new_candidate_nice_agent_new_candidate, self, 0); #line 73 "/home/jeff/Documents/libraries/libsoy/src/net/XMPPAgent.gs" _tmp8_ = self->agent; #line 73 "/home/jeff/Documents/libraries/libsoy/src/net/XMPPAgent.gs" g_signal_connect_object (_tmp8_, "new-remote-candidate", (GCallback) _soy_net_xmpp_agent_new_remote_candidate_nice_agent_new_remote_candidate, self, 0); #line 74 "/home/jeff/Documents/libraries/libsoy/src/net/XMPPAgent.gs" _tmp9_ = self->agent; #line 74 "/home/jeff/Documents/libraries/libsoy/src/net/XMPPAgent.gs" g_signal_connect_object (_tmp9_, "new-selected-pair", (GCallback) _soy_net_xmpp_agent_new_selected_pair_nice_agent_new_selected_pair, self, 0); #line 75 "/home/jeff/Documents/libraries/libsoy/src/net/XMPPAgent.gs" _tmp10_ = self->agent; #line 75 "/home/jeff/Documents/libraries/libsoy/src/net/XMPPAgent.gs" g_signal_connect_object (_tmp10_, "reliable-transport-writable", (GCallback) _soy_net_xmpp_agent_reliable_transport_writable_nice_agent_reliable_transport_writable, self, 0); #line 64 "/home/jeff/Documents/libraries/libsoy/src/net/XMPPAgent.gs" return self; #line 222 "XMPPAgent.c" }
NiceConnection::NiceConnection(MediaType med, const std::string &transport_name,NiceConnectionListener* listener, unsigned int iceComponents, const IceConfig& iceConfig, std::string username, std::string password) : mediaType(med), agent_(NULL), listener_(listener), candsDelivered_(0), context_(NULL), iceState_(NICE_INITIAL), iceComponents_(iceComponents) { localCandidates.reset(new std::vector<CandidateInfo>()); transportName.reset(new std::string(transport_name)); for (unsigned int i = 1; i<=iceComponents_; i++) { comp_state_list_[i] = NICE_INITIAL; } g_type_init(); context_ = g_main_context_new(); g_main_context_set_poll_func(context_,timed_poll); ELOG_DEBUG("Creating Agent"); nice_debug_enable( FALSE ); // Create a nice agent agent_ = nice_agent_new(context_, NICE_COMPATIBILITY_RFC5245); GValue controllingMode = { 0 }; g_value_init(&controllingMode, G_TYPE_BOOLEAN); g_value_set_boolean(&controllingMode, false); g_object_set_property(G_OBJECT( agent_ ), "controlling-mode", &controllingMode); GValue checks = { 0 }; g_value_init(&checks, G_TYPE_UINT); g_value_set_uint(&checks, 100); g_object_set_property(G_OBJECT( agent_ ), "max-connectivity-checks", &checks); if (iceConfig.stunServer.compare("") != 0 && iceConfig.stunPort!=0){ GValue val = { 0 }, val2 = { 0 }; g_value_init(&val, G_TYPE_STRING); g_value_set_string(&val, iceConfig.stunServer.c_str()); g_object_set_property(G_OBJECT( agent_ ), "stun-server", &val); g_value_init(&val2, G_TYPE_UINT); g_value_set_uint(&val2, iceConfig.stunPort); g_object_set_property(G_OBJECT( agent_ ), "stun-server-port", &val2); ELOG_DEBUG("Setting STUN server %s:%d", iceConfig.stunServer.c_str(), iceConfig.stunPort); } // Connect the signals g_signal_connect( G_OBJECT( agent_ ), "candidate-gathering-done", G_CALLBACK( cb_candidate_gathering_done ), this); g_signal_connect( G_OBJECT( agent_ ), "component-state-changed", G_CALLBACK( cb_component_state_changed ), this); g_signal_connect( G_OBJECT( agent_ ), "new-selected-pair", G_CALLBACK( cb_new_selected_pair ), this); g_signal_connect( G_OBJECT( agent_ ), "new-candidate", G_CALLBACK( cb_new_candidate ), this); // Create a new stream and start gathering candidates ELOG_DEBUG("Adding Stream... Number of components %d", iceComponents_); nice_agent_add_stream(agent_, iceComponents_); gchar *ufrag = NULL, *upass = NULL; nice_agent_get_local_credentials(agent_, 1, &ufrag, &upass); ufrag_ = std::string(ufrag); g_free(ufrag); upass_ = std::string(upass); g_free(upass); // Set our remote credentials. This must be done *after* we add a stream. if (username.compare("")!=0 && password.compare("")!=0){ ELOG_DEBUG("Setting remote credentials in constructor"); this->setRemoteCredentials(username, password); } // Set Port Range ----> If this doesn't work when linking the file libnice.sym has to be modified to include this call if (iceConfig.minPort!=0 && iceConfig.maxPort!=0){ ELOG_DEBUG("Setting port range: %d to %d\n", iceConfig.minPort, iceConfig.maxPort); nice_agent_set_port_range(agent_, (guint)1, (guint)1, (guint)iceConfig.minPort, (guint)iceConfig.maxPort); } if (iceConfig.turnServer.compare("") != 0 && iceConfig.turnPort!=0){ ELOG_DEBUG("Setting TURN server %s:%d", iceConfig.turnServer.c_str(), iceConfig.turnPort); ELOG_DEBUG("Setting TURN credentials %s:%s", iceConfig.turnUsername.c_str(), iceConfig.turnPass.c_str()); for (unsigned int i = 1; i <= iceComponents_ ; i++){ nice_agent_set_relay_info (agent_, 1, i, iceConfig.turnServer.c_str(), // TURN Server IP iceConfig.turnPort, // TURN Server PORT iceConfig.turnUsername.c_str(), // Username iceConfig.turnPass.c_str(), // Pass NICE_RELAY_TYPE_TURN_UDP); } } if(agent_){ for (unsigned int i = 1; i<=iceComponents_; i++){ nice_agent_attach_recv(agent_, 1, i, context_, cb_nice_recv, this); } running_ = true; } else{ running_=false; } m_Thread_ = boost::thread(&NiceConnection::init, this); }
int main(void) { NiceAgent *lagent = NULL, *ragent = NULL; GThread *stun_thread = NULL; NiceAddress baseaddr; GSource *src; int sock; global_cancellable = g_cancellable_new (); src = g_cancellable_source_new (global_cancellable); g_source_set_dummy_callback (src); g_source_attach (src, NULL); sock = listen_socket (&stun_port); if (sock == -1) { g_assert_not_reached (); } stun_thread = g_thread_new ("listen for STUN requests", stun_thread_func, GINT_TO_POINTER (sock)); // Once the the thread is forked, we want to listen for a signal // that the socket was opened successfully g_mutex_lock (stun_thread_mutex_ptr); g_cond_wait (stun_thread_signal_ptr, stun_thread_mutex_ptr); lagent = nice_agent_new (NULL, NICE_COMPATIBILITY_RFC5245); ragent = nice_agent_new (NULL, NICE_COMPATIBILITY_RFC5245); g_object_set (G_OBJECT (lagent), "ice-tcp", FALSE, NULL); g_object_set (G_OBJECT (ragent), "ice-tcp", FALSE, NULL); g_object_set (G_OBJECT (lagent), "controlling-mode", TRUE, NULL); g_object_set (G_OBJECT (ragent), "controlling-mode", FALSE, NULL); g_object_set (G_OBJECT (lagent), "upnp", USE_UPNP, NULL); g_object_set (G_OBJECT (ragent), "upnp", USE_UPNP, NULL); g_object_set (G_OBJECT (lagent), "stun-server", "127.0.0.1", NULL); g_object_set (G_OBJECT (lagent), "stun-server-port", stun_port, NULL); g_object_set_data (G_OBJECT (lagent), "other-agent", ragent); g_object_set_data (G_OBJECT (ragent), "other-agent", lagent); g_assert (nice_address_set_from_string (&baseaddr, "127.0.0.1")); nice_agent_add_local_address (lagent, &baseaddr); nice_agent_add_local_address (ragent, &baseaddr); g_signal_connect(G_OBJECT(lagent), "candidate-gathering-done", G_CALLBACK(cb_candidate_gathering_done), LEFT_AGENT); g_signal_connect(G_OBJECT(ragent), "candidate-gathering-done", G_CALLBACK(cb_candidate_gathering_done), RIGHT_AGENT); g_signal_connect(G_OBJECT(lagent), "component-state-changed", G_CALLBACK(cb_component_state_changed), LEFT_AGENT); g_signal_connect(G_OBJECT(ragent), "component-state-changed", G_CALLBACK(cb_component_state_changed), RIGHT_AGENT); standard_test (lagent, ragent); bad_credentials_test (lagent, ragent); bad_candidate_test (lagent, ragent); new_candidate_test (lagent, ragent); // Do this to make sure the STUN thread exits exit_stun_thread = TRUE; drop_stun_packets = TRUE; send_dummy_data (); g_object_add_weak_pointer (G_OBJECT (lagent), (gpointer *) &lagent); g_object_add_weak_pointer (G_OBJECT (ragent), (gpointer *) &ragent); g_object_unref (lagent); g_object_unref (ragent); g_thread_join (stun_thread); g_object_unref (global_cancellable); g_source_destroy (src); g_source_unref (src); WAIT_UNTIL_UNSET (lagent, NULL); WAIT_UNTIL_UNSET (ragent, NULL); return 0; }
int main (void) { NiceAgent *lagent, *ragent; /* agent's L and R */ NiceAddress baseaddr; int result; guint timer_id; #ifdef G_OS_WIN32 WSADATA w; WSAStartup(0x0202, &w); #endif g_type_init (); 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_RFC5245); ragent = nice_agent_new (g_main_loop_get_context (global_mainloop), NICE_COMPATIBILITY_RFC5245); g_object_set (G_OBJECT (lagent), "ice-udp", FALSE, NULL); g_object_set (G_OBJECT (ragent), "ice-udp", FALSE, NULL); nice_agent_set_software (lagent, "Test-icetcp, Left Agent"); nice_agent_set_software (ragent, "Test-icetcp, Right Agent"); /* step: add a timer to catch state changes triggered by signals */ timer_id = g_timeout_add (30000, timer_cb, NULL); /* step: specify which local interface to use */ 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); 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 the first time */ g_debug ("test-icetcp: TEST STARTS / running test for the 1st time"); result = run_full_test (lagent, ragent, &baseaddr, 4 ,0); priv_print_global_status (); g_assert (result == 0); g_assert (global_lagent_state[0] == NICE_COMPONENT_STATE_READY); g_assert (global_lagent_state[1] == NICE_COMPONENT_STATE_READY); g_assert (global_ragent_state[0] == NICE_COMPONENT_STATE_READY); g_assert (global_ragent_state[1] == NICE_COMPONENT_STATE_READY); /* note: verify that correct number of local candidates were reported */ g_assert (global_lagent_cands >= 2); g_assert (global_ragent_cands >= 2); /* step: run test again without unref'ing agents */ g_debug ("test-icetcp: TEST STARTS / running test for the 2nd time"); result = run_full_test (lagent, ragent, &baseaddr, 4, 0); priv_print_global_status (); g_assert (result == 0); g_assert (global_lagent_state[0] == NICE_COMPONENT_STATE_READY); g_assert (global_lagent_state[1] == NICE_COMPONENT_STATE_READY); g_assert (global_ragent_state[0] == NICE_COMPONENT_STATE_READY); g_assert (global_ragent_state[1] == NICE_COMPONENT_STATE_READY); /* note: verify that correct number of local candidates were reported */ g_assert (global_lagent_cands >= 2); g_assert (global_ragent_cands >= 2); 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 result; }
void NiceConnection::init() { if(this->checkIceState() != NICE_INITIAL){ ELOG_DEBUG("Initing NiceConnection not in INITIAL state, exiting... %p", this); return; }; { boost::unique_lock<boost::mutex> lock(agentMutex_); if(!running_) return; this->updateIceState(NICE_INITIAL); g_type_init(); context_ = g_main_context_new(); g_main_context_set_poll_func(context_,timed_poll); /* loop_ = g_main_loop_new(context_, FALSE); */ ELOG_DEBUG("Creating Agent"); //loop_ = g_main_loop_new(NULL, FALSE); // nice_debug_enable( TRUE ); // Create a nice agent //agent_ = nice_agent_new(g_main_loop_get_context(loop_), NICE_COMPATIBILITY_RFC5245); agent_ = nice_agent_new(context_, NICE_COMPATIBILITY_RFC5245); GValue controllingMode = { 0 }; g_value_init(&controllingMode, G_TYPE_BOOLEAN); g_value_set_boolean(&controllingMode, false); g_object_set_property(G_OBJECT( agent_ ), "controlling-mode", &controllingMode); // NiceAddress* naddr = nice_address_new(); // nice_agent_add_local_address(agent_, naddr); if (stunServer_.compare("") != 0 && stunPort_!=0){ GValue val = { 0 }, val2 = { 0 }; g_value_init(&val, G_TYPE_STRING); g_value_set_string(&val, stunServer_.c_str()); g_object_set_property(G_OBJECT( agent_ ), "stun-server", &val); g_value_init(&val2, G_TYPE_UINT); g_value_set_uint(&val2, stunPort_); g_object_set_property(G_OBJECT( agent_ ), "stun-server-port", &val2); ELOG_DEBUG("Setting STUN server %s:%d", stunServer_.c_str(), stunPort_); } // Connect the signals g_signal_connect( G_OBJECT( agent_ ), "candidate-gathering-done", G_CALLBACK( cb_candidate_gathering_done ), this); g_signal_connect( G_OBJECT( agent_ ), "component-state-changed", G_CALLBACK( cb_component_state_changed ), this); g_signal_connect( G_OBJECT( agent_ ), "new-selected-pair", G_CALLBACK( cb_new_selected_pair ), this); // Create a new stream and start gathering candidates ELOG_DEBUG("Adding Stream... Number of components %d", iceComponents_); nice_agent_add_stream(agent_, iceComponents_); // Set Port Range ----> If this doesn't work when linking the file libnice.sym has to be modified to include this call if (minPort_!=0 && maxPort_!=0){ ELOG_DEBUG("Setting port range: %d to %d\n", minPort_, maxPort_); nice_agent_set_port_range(agent_, (guint)1, (guint)1, (guint)minPort_, (guint)maxPort_); } if (SERVER_SIDE_TURN){ for (int i = 1; i < (iceComponents_ +1); i++){ ELOG_DEBUG("Setting TURN Comp %d\n", i); nice_agent_set_relay_info (agent_, 1, i, "", // TURN Server IP 3479, // TURN Server PORT "", // Username "", // Pass NICE_RELAY_TYPE_TURN_UDP); } } ELOG_DEBUG("Gathering candidates %p", this); nice_agent_gather_candidates(agent_, 1); if(agent_){ nice_agent_attach_recv(agent_, 1, 1, context_, cb_nice_recv, this); if (iceComponents_ > 1) { nice_agent_attach_recv(agent_, 1, 2, context_,cb_nice_recv, this); } }else{ running_=false; } } // Attach to the component to receive the data while(running_){ boost::unique_lock<boost::mutex> lockContext(agentMutex_); if(this->checkIceState()>=NICE_FINISHED) break; g_main_context_iteration(context_, true); } ELOG_DEBUG("LibNice thread finished %p", this); }
NiceAgent* LibNiceInterfaceImpl::NiceAgentNew(GMainContext* context) { return nice_agent_new(context, NICE_COMPATIBILITY_RFC5245); }
int main (void) { NiceAgent *lagent, *ragent; /* agent's L and R */ NiceAddress baseaddr; int result; guint timer_id; const char *stun_server = NULL, *stun_server_port = NULL; #ifdef G_OS_WIN32 WSADATA w; WSAStartup(0x0202, &w); #endif g_type_init (); #if !GLIB_CHECK_VERSION(2,31,8) g_thread_init(NULL); #endif global_mainloop = g_main_loop_new (NULL, FALSE); /* Note: impl limits ... * - no multi-stream support * - no IPv6 support */ /* step: create the agents L and R */ #if USE_RELIABLE lagent = nice_agent_new_reliable (g_main_loop_get_context (global_mainloop), NICE_COMPATIBILITY); ragent = nice_agent_new_reliable (g_main_loop_get_context (global_mainloop), NICE_COMPATIBILITY); #else lagent = nice_agent_new (g_main_loop_get_context (global_mainloop), NICE_COMPATIBILITY); ragent = nice_agent_new (g_main_loop_get_context (global_mainloop), NICE_COMPATIBILITY); #endif nice_agent_set_software (lagent, "Test-fullmode, Left Agent"); nice_agent_set_software (ragent, "Test-fullmode, Right Agent"); /* step: add a timer to catch state changes triggered by signals */ #if USE_TURN timer_id = g_timeout_add (300000, timer_cb, NULL); #else timer_id = g_timeout_add (30000, timer_cb, NULL); #endif /* step: specify which local interface to use */ #if USE_LOOPBACK 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); #endif 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)); stun_server = getenv ("NICE_STUN_SERVER"); stun_server_port = getenv ("NICE_STUN_SERVER_PORT"); if (stun_server) { g_object_set (G_OBJECT (lagent), "stun-server", stun_server, NULL); g_object_set (G_OBJECT (lagent), "stun-server-port", atoi (stun_server_port), NULL); g_object_set (G_OBJECT (ragent), "stun-server", stun_server, NULL); g_object_set (G_OBJECT (ragent), "stun-server-port", atoi (stun_server_port), NULL); } g_object_set (G_OBJECT (lagent), "upnp", USE_UPNP, NULL); g_object_set (G_OBJECT (lagent), "proxy-ip", PROXY_IP, NULL); g_object_set (G_OBJECT (lagent), "proxy-port", PROXY_PORT, NULL); g_object_set (G_OBJECT (lagent), "proxy-type", PROXY_TYPE, NULL); g_object_set (G_OBJECT (lagent), "proxy-username", PROXY_USERNAME, NULL); g_object_set (G_OBJECT (lagent), "proxy-password", PROXY_PASSWORD, NULL); g_object_set (G_OBJECT (ragent), "upnp", USE_UPNP, NULL); g_object_set (G_OBJECT (ragent), "proxy-ip", PROXY_IP, NULL); g_object_set (G_OBJECT (ragent), "proxy-port", PROXY_PORT, NULL); g_object_set (G_OBJECT (ragent), "proxy-type", PROXY_TYPE, NULL); g_object_set (G_OBJECT (ragent), "proxy-username", PROXY_USERNAME, NULL); g_object_set (G_OBJECT (ragent), "proxy-password", PROXY_PASSWORD, NULL); /* step: test setter/getter functions for properties */ { guint max_checks = 0; gchar *string = NULL; guint port = 0; gboolean mode = FALSE; g_object_get (G_OBJECT (lagent), "stun-server", &string, NULL); g_assert (stun_server == NULL || strcmp (string, stun_server) == 0); g_free (string); g_object_get (G_OBJECT (lagent), "stun-server-port", &port, NULL); g_assert (stun_server_port == NULL || port == (guint)atoi (stun_server_port)); g_object_get (G_OBJECT (lagent), "proxy-ip", &string, NULL); g_assert (strcmp (string, PROXY_IP) == 0); g_free (string); g_object_get (G_OBJECT (lagent), "proxy-port", &port, NULL); g_assert (port == PROXY_PORT); g_object_get (G_OBJECT (lagent), "controlling-mode", &mode, NULL); g_assert (mode == TRUE); g_object_set (G_OBJECT (lagent), "max-connectivity-checks", 300, NULL); g_object_get (G_OBJECT (lagent), "max-connectivity-checks", &max_checks, NULL); g_assert (max_checks == 300); } /* step: run test the first time */ g_debug ("test-fullmode: TEST STARTS / running test for the 1st time"); result = run_full_test (lagent, ragent, &baseaddr, 4 ,0); priv_print_global_status (); g_assert (result == 0); g_assert (global_lagent_state[0] == NICE_COMPONENT_STATE_READY); g_assert (global_lagent_state[1] == NICE_COMPONENT_STATE_READY); g_assert (global_ragent_state[0] == NICE_COMPONENT_STATE_READY); g_assert (global_ragent_state[1] == NICE_COMPONENT_STATE_READY); /* When using TURN, we get peer reflexive candidates for the host cands that we removed so we can get another new_selected_pair signal later depending on timing/racing, we could double (or not) the amount we expected */ #if !(USE_TURN) /* note: verify that correct number of local candidates were reported */ g_assert (global_lagent_cands == 2); g_assert (global_ragent_cands == 2); #endif /* step: run test again without unref'ing agents */ g_debug ("test-fullmode: TEST STARTS / running test for the 2nd time"); result = run_full_test (lagent, ragent, &baseaddr, 4, 0); priv_print_global_status (); g_assert (result == 0); g_assert (global_lagent_state[0] == NICE_COMPONENT_STATE_READY); g_assert (global_lagent_state[1] == NICE_COMPONENT_STATE_READY); g_assert (global_ragent_state[0] == NICE_COMPONENT_STATE_READY); g_assert (global_ragent_state[1] == NICE_COMPONENT_STATE_READY); /* When using TURN, we get peer reflexive candidates for the host cands that we removed so we can get another new_selected_pair signal later depending on timing/racing, we could double (or not) the amount we expected */ #if !(USE_TURN) /* note: verify that correct number of local candidates were reported */ g_assert (global_lagent_cands == 2); g_assert (global_ragent_cands == 2); #endif /* step: run test simulating delayed SDP answer */ g_debug ("test-fullmode: TEST STARTS / delayed SDP answer"); result = run_full_test_delayed_answer (lagent, ragent, &baseaddr, 4, 0); priv_print_global_status (); g_assert (result == 0); g_assert (global_lagent_state[0] == NICE_COMPONENT_STATE_READY); g_assert (global_lagent_state[1] == NICE_COMPONENT_STATE_READY); g_assert (global_ragent_state[0] == NICE_COMPONENT_STATE_READY); g_assert (global_ragent_state[1] == NICE_COMPONENT_STATE_READY); /* note: verify that correct number of local candidates were reported */ /* When using TURN, we get peer reflexive candidates for the host cands that we removed so we can get another new_selected_pair signal later depending on timing/racing, we could double (or not) the amount we expected */ #if !(USE_TURN) g_assert (global_lagent_cands == 2); g_assert (global_ragent_cands == 2); #endif #if TEST_GOOGLE return result; #endif /* run test with incorrect credentials (make sure process fails) */ g_debug ("test-fullmode: TEST STARTS / incorrect credentials"); result = run_full_test_wrong_password (lagent, ragent, &baseaddr); priv_print_global_status (); g_assert (result == 0); g_assert (global_lagent_state[0] == NICE_COMPONENT_STATE_FAILED); g_assert (global_lagent_state[1] == NICE_COMPONENT_STATE_LAST); g_assert (global_ragent_state[0] == NICE_COMPONENT_STATE_FAILED); g_assert (global_ragent_state[1] == NICE_COMPONENT_STATE_LAST); /* The max connectivity checks test can't be run with TURN because we'll have 3 local candidates instead of 1 and the checks will be random, so we can't predict how many will fail/succeed */ #if USE_TURN == 0 /* step: run test with a hard limit for connecitivity checks */ g_debug ("test-fullmode: TEST STARTS / max connectivity checks"); g_object_set (G_OBJECT (lagent), "max-connectivity-checks", 1, NULL); g_object_set (G_OBJECT (ragent), "max-connectivity-checks", 1, NULL); result = run_full_test (lagent, ragent, &baseaddr, 2, 2); priv_print_global_status (); g_assert (result == 0); /* should FAIL as agent L can't send any checks: */ g_assert (global_lagent_state[0] == NICE_COMPONENT_STATE_FAILED || global_lagent_state[1] == NICE_COMPONENT_STATE_FAILED); g_assert (global_lagent_state[0] == NICE_COMPONENT_STATE_FAILED || global_lagent_state[1] == NICE_COMPONENT_STATE_FAILED); #endif g_object_set (G_OBJECT (lagent), "max-connectivity-checks", 100, NULL); g_object_set (G_OBJECT (ragent), "max-connectivity-checks", 100, NULL); result = run_full_test (lagent, ragent, &baseaddr, 4, 0); priv_print_global_status (); /* should SUCCEED as agent L can send the checks: */ g_assert (result == 0); g_assert (global_lagent_state[0] == NICE_COMPONENT_STATE_READY); g_assert (global_lagent_state[1] == NICE_COMPONENT_STATE_READY); g_assert (global_ragent_state[0] == NICE_COMPONENT_STATE_READY); g_assert (global_ragent_state[1] == NICE_COMPONENT_STATE_READY); g_object_set (G_OBJECT (lagent), "max-connectivity-checks", 100, NULL); /* run test with a conflict in controlling mode: controlling-controlling */ g_debug ("test-fullmode: TEST STARTS / controlling mode conflict case-1"); result = run_full_test_control_conflict (lagent, ragent, &baseaddr, TRUE); priv_print_global_status (); g_assert (result == 0); g_assert (global_lagent_state[0] == NICE_COMPONENT_STATE_READY); g_assert (global_lagent_state[1] == NICE_COMPONENT_STATE_READY); g_assert (global_ragent_state[0] == NICE_COMPONENT_STATE_READY); g_assert (global_ragent_state[1] == NICE_COMPONENT_STATE_READY); /* run test with a conflict in controlling mode: controlled-controlled */ g_debug ("test-fullmode: TEST STARTS / controlling mode conflict case-2"); result = run_full_test_control_conflict (lagent, ragent, &baseaddr, FALSE); priv_print_global_status (); g_assert (result == 0); g_assert (global_lagent_state[0] == NICE_COMPONENT_STATE_READY); g_assert (global_lagent_state[1] == NICE_COMPONENT_STATE_READY); g_assert (global_ragent_state[0] == NICE_COMPONENT_STATE_READY); g_assert (global_ragent_state[1] == NICE_COMPONENT_STATE_READY); 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 result; }
int main(void) { NiceAgent *lagent = NULL, *ragent = NULL; GThread *stun_thread = NULL; NiceAddress baseaddr; g_type_init(); global_mainloop = g_main_loop_new (NULL, FALSE); #if !GLIB_CHECK_VERSION(2,31,8) g_thread_init (NULL); stun_thread = g_thread_create (stun_thread_func, global_mainloop, TRUE, NULL); stun_mutex_ptr = g_mutex_new (); stun_signal_ptr = g_cond_new (); #else stun_thread = g_thread_new ("listen for STUN requests", stun_thread_func, NULL); #endif lagent = nice_agent_new (g_main_loop_get_context (global_mainloop), NICE_COMPATIBILITY_RFC5245); ragent = nice_agent_new (g_main_loop_get_context (global_mainloop), NICE_COMPATIBILITY_RFC5245); g_object_set (G_OBJECT (lagent), "controlling-mode", TRUE, NULL); g_object_set (G_OBJECT (ragent), "controlling-mode", FALSE, NULL); g_object_set (G_OBJECT (lagent), "upnp", USE_UPNP, NULL); g_object_set (G_OBJECT (ragent), "upnp", USE_UPNP, NULL); g_object_set (G_OBJECT (lagent), "stun-server", "127.0.0.1", NULL); g_object_set (G_OBJECT (lagent), "stun-server-port", IPPORT_STUN, NULL); g_object_set_data (G_OBJECT (lagent), "other-agent", ragent); g_object_set_data (G_OBJECT (ragent), "other-agent", lagent); g_assert (nice_address_set_from_string (&baseaddr, "127.0.0.1")); nice_agent_add_local_address (lagent, &baseaddr); nice_agent_add_local_address (ragent, &baseaddr); g_signal_connect(G_OBJECT(lagent), "candidate-gathering-done", G_CALLBACK(cb_candidate_gathering_done), LEFT_AGENT); g_signal_connect(G_OBJECT(ragent), "candidate-gathering-done", G_CALLBACK(cb_candidate_gathering_done), RIGHT_AGENT); g_signal_connect(G_OBJECT(lagent), "component-state-changed", G_CALLBACK(cb_component_state_changed), LEFT_AGENT); g_signal_connect(G_OBJECT(ragent), "component-state-changed", G_CALLBACK(cb_component_state_changed), RIGHT_AGENT); standard_test (lagent, ragent); bad_credentials_test (lagent, ragent); bad_candidate_test (lagent, ragent); new_candidate_test (lagent, ragent); // Do this to make sure the STUN thread exits exit_stun_thread = TRUE; drop_stun_packets = TRUE; send_dummy_data (); g_object_unref (lagent); g_object_unref (ragent); g_thread_join (stun_thread); #if !GLIB_CHECK_VERSION(2,31,8) g_mutex_free (stun_mutex_ptr); g_cond_free (stun_signal_ptr); #endif g_main_loop_unref (global_mainloop); return 0; }
static void * example_thread(void *data) { NiceAgent *agent; NiceCandidate *local, *remote; GIOChannel* io_stdin; guint stream_id; gchar *line = NULL; int rval; #ifdef G_OS_WIN32 io_stdin = g_io_channel_win32_new_fd(_fileno(stdin)); #else io_stdin = g_io_channel_unix_new(fileno(stdin)); #endif g_io_channel_set_flags (io_stdin, G_IO_FLAG_NONBLOCK, NULL); // Create the nice agent agent = nice_agent_new(g_main_loop_get_context (gloop), NICE_COMPATIBILITY_RFC5245); if (agent == NULL) g_error("Failed to create agent"); // Set the STUN settings and controlling mode if (stun_addr) { g_object_set(agent, "stun-server", stun_addr, NULL); g_object_set(agent, "stun-server-port", stun_port, NULL); } g_object_set(agent, "controlling-mode", controlling, NULL); // Connect to the signals g_signal_connect(agent, "candidate-gathering-done", G_CALLBACK(cb_candidate_gathering_done), NULL); g_signal_connect(agent, "new-selected-pair", G_CALLBACK(cb_new_selected_pair), NULL); g_signal_connect(agent, "component-state-changed", G_CALLBACK(cb_component_state_changed), NULL); // Create a new stream with one component stream_id = nice_agent_add_stream(agent, 1); if (stream_id == 0) g_error("Failed to add stream"); // Attach to the component to receive the data // Without this call, candidates cannot be gathered nice_agent_attach_recv(agent, stream_id, 1, g_main_loop_get_context (gloop), cb_nice_recv, NULL); // Start gathering local candidates if (!nice_agent_gather_candidates(agent, stream_id)) g_error("Failed to start candidate gathering"); g_debug("waiting for candidate-gathering-done signal..."); g_mutex_lock(&gather_mutex); while (!exit_thread && !candidate_gathering_done) g_cond_wait(&gather_cond, &gather_mutex); g_mutex_unlock(&gather_mutex); if (exit_thread) goto end; // Candidate gathering is done. Send our local candidates on stdout printf("Copy this line to remote client:\n"); printf("\n "); print_local_data(agent, stream_id, 1); printf("\n"); // Listen on stdin for the remote candidate list printf("Enter remote data (single line, no wrapping):\n"); printf("> "); fflush (stdout); while (!exit_thread) { GIOStatus s = g_io_channel_read_line (io_stdin, &line, NULL, NULL, NULL); if (s == G_IO_STATUS_NORMAL) { // Parse remote candidate list and set it on the agent rval = parse_remote_data(agent, stream_id, 1, line); if (rval == EXIT_SUCCESS) { g_free (line); break; } else { fprintf(stderr, "ERROR: failed to parse remote data\n"); printf("Enter remote data (single line, no wrapping):\n"); printf("> "); fflush (stdout); } g_free (line); } else if (s == G_IO_STATUS_AGAIN) { g_usleep (100000); } } g_debug("waiting for state READY or FAILED signal..."); g_mutex_lock(&negotiate_mutex); while (!exit_thread && !negotiation_done) g_cond_wait(&negotiate_cond, &negotiate_mutex); g_mutex_unlock(&negotiate_mutex); if (exit_thread) goto end; // Get current selected candidate pair and print IP address used if (nice_agent_get_selected_pair (agent, stream_id, 1, &local, &remote)) { gchar ipaddr[INET6_ADDRSTRLEN]; nice_address_to_string(&local->addr, ipaddr); printf("\nNegotiation complete: ([%s]:%d,", ipaddr, nice_address_get_port(&local->addr)); nice_address_to_string(&remote->addr, ipaddr); printf(" [%s]:%d)\n", ipaddr, nice_address_get_port(&remote->addr)); } // Listen to stdin and send data written to it printf("\nSend lines to remote (Ctrl-D to quit):\n"); printf("> "); fflush (stdout); while (!exit_thread) { GIOStatus s = g_io_channel_read_line (io_stdin, &line, NULL, NULL, NULL); if (s == G_IO_STATUS_NORMAL) { nice_agent_send(agent, stream_id, 1, strlen(line), line); g_free (line); printf("> "); fflush (stdout); } else if (s == G_IO_STATUS_AGAIN) { g_usleep (100000); } else { // Ctrl-D was pressed. nice_agent_send(agent, stream_id, 1, 1, "\0"); break; } } end: g_io_channel_unref (io_stdin); g_object_unref(agent); g_main_loop_quit (gloop); return NULL; }
void NiceConnection::init() { streamsGathered = 0; this->updateIceState(NICE_INITIAL); g_type_init(); ELOG_DEBUG("Creating Main Context"); context_ = g_main_context_new(); ELOG_DEBUG("Creating Main Loop"); loop_ = g_main_loop_new(context_, FALSE); ELOG_DEBUG("Creating Agent"); //loop_ = g_main_loop_new(NULL, FALSE); // nice_debug_enable( TRUE ); // Create a nice agent //agent_ = nice_agent_new(g_main_loop_get_context(loop_), NICE_COMPATIBILITY_RFC5245); agent_ = nice_agent_new(context_, NICE_COMPATIBILITY_RFC5245); GValue controllingMode = { 0 }; g_value_init(&controllingMode, G_TYPE_BOOLEAN); g_value_set_boolean(&controllingMode, false); g_object_set_property(G_OBJECT( agent_ ), "controlling-mode", &controllingMode); // NiceAddress* naddr = nice_address_new(); // nice_agent_add_local_address(agent_, naddr); if (stunServer_.compare("") != 0 && stunPort_!=0) { GValue val = { 0 }, val2 = { 0 }; g_value_init(&val, G_TYPE_STRING); g_value_set_string(&val, stunServer_.c_str()); g_object_set_property(G_OBJECT( agent_ ), "stun-server", &val); g_value_init(&val2, G_TYPE_UINT); g_value_set_uint(&val2, stunPort_); g_object_set_property(G_OBJECT( agent_ ), "stun-server-port", &val2); ELOG_WARN("Stun Server %s:%d", stunServer_.c_str(), stunPort_); } // Connect the signals g_signal_connect( G_OBJECT( agent_ ), "candidate-gathering-done", G_CALLBACK( cb_candidate_gathering_done ), this); g_signal_connect( G_OBJECT( agent_ ), "component-state-changed", G_CALLBACK( cb_component_state_changed ), this); g_signal_connect( G_OBJECT( agent_ ), "new-selected-pair", G_CALLBACK( cb_new_selected_pair ), this); // Create a new stream and start gathering candidates ELOG_DEBUG("Adding Stream... Number of components %d", iceComponents_); nice_agent_add_stream(agent_, iceComponents_); // Set Port Range ----> If this doesn't work when linking the file libnice.sym has to be modified to include this call if (minPort_!=0 && maxPort_!=0) { nice_agent_set_port_range(agent_, (guint)1, (guint)1, (guint)minPort_, (guint)maxPort_); } nice_agent_gather_candidates(agent_, 1); nice_agent_attach_recv(agent_, 1, 1, context_, cb_nice_recv, this); if (iceComponents_ > 1) { nice_agent_attach_recv(agent_, 1, 2, context_, cb_nice_recv, this); } // Attach to the component to receive the data g_main_loop_run(loop_); }
int main(int argc, char *argv[]) { NiceAgent *agent; gchar *stun_addr = NULL; guint stun_port = 0; gboolean controlling; // Parse arguments if (argc > 4 || argc < 2 || argv[1][1] != '\0') { fprintf(stderr, "Usage: %s 0|1 stun_addr [stun_port]\n", argv[0]); return EXIT_FAILURE; } controlling = argv[1][0] - '0'; if (controlling != 0 && controlling != 1) { fprintf(stderr, "Usage: %s 0|1 stun_addr [stun_port]\n", argv[0]); return EXIT_FAILURE; } if (argc > 2) { stun_addr = argv[2]; if (argc > 3) stun_port = atoi(argv[3]); else stun_port = 3478; g_debug("Using stun server '[%s]:%u'\n", stun_addr, stun_port); } g_networking_init(); gloop = g_main_loop_new(NULL, FALSE); #ifdef G_OS_WIN32 io_stdin = g_io_channel_win32_new_fd(_fileno(stdin)); #else io_stdin = g_io_channel_unix_new(fileno(stdin)); #endif // Create the nice agent agent = nice_agent_new(g_main_loop_get_context (gloop), NICE_COMPATIBILITY_RFC5245); if (agent == NULL) g_error("Failed to create agent"); // Set the STUN settings and controlling mode if (stun_addr) { g_object_set(agent, "stun-server", stun_addr, NULL); g_object_set(agent, "stun-server-port", stun_port, NULL); } g_object_set(agent, "controlling-mode", controlling, NULL); // Connect to the signals g_signal_connect(agent, "candidate-gathering-done", G_CALLBACK(cb_candidate_gathering_done), NULL); g_signal_connect(agent, "new-selected-pair", G_CALLBACK(cb_new_selected_pair), NULL); g_signal_connect(agent, "component-state-changed", G_CALLBACK(cb_component_state_changed), NULL); // Create a new stream with one component stream_id = nice_agent_add_stream(agent, 1); if (stream_id == 0) g_error("Failed to add stream"); // Attach to the component to receive the data // Without this call, candidates cannot be gathered nice_agent_attach_recv(agent, stream_id, 1, g_main_loop_get_context (gloop), cb_nice_recv, NULL); // Start gathering local candidates if (!nice_agent_gather_candidates(agent, stream_id)) g_error("Failed to start candidate gathering"); g_debug("waiting for candidate-gathering-done signal..."); // Run the mainloop. Everything else will happen asynchronously // when the candidates are done gathering. g_main_loop_run (gloop); g_main_loop_unref(gloop); g_object_unref(agent); g_io_channel_unref (io_stdin); return EXIT_SUCCESS; }
int main (void) { NiceAgent *lagent, *ragent; /* agent's L and R */ NiceAddress baseaddr; int result; guint timer_id; const char *stun_server = NULL, *stun_server_port = NULL; #ifdef G_OS_WIN32 WSADATA w; WSAStartup(0x0202, &w); #endif g_type_init (); global_mainloop = g_main_loop_new (NULL, FALSE); /* Note: impl limits ... * - no multi-stream support * - no IPv6 support */ /* step: create the agents L and R */ lagent = nice_agent_new (g_main_loop_get_context (global_mainloop), NICE_COMPATIBILITY_RFC5245); ragent = nice_agent_new (g_main_loop_get_context (global_mainloop), NICE_COMPATIBILITY_RFC5245); /* step: add a timer to catch state changes triggered by signals */ timer_id = g_timeout_add (30000, timer_cb, NULL); /* step: specify which local interface to use */ 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); g_signal_connect (G_OBJECT (lagent), "candidate-gathering-done", G_CALLBACK (cb_candidate_gathering_done), (gpointer)1); g_signal_connect (G_OBJECT (ragent), "candidate-gathering-done", G_CALLBACK (cb_candidate_gathering_done), (gpointer)2); g_signal_connect (G_OBJECT (lagent), "component-state-changed", G_CALLBACK (cb_component_state_changed), (gpointer)1); g_signal_connect (G_OBJECT (ragent), "component-state-changed", G_CALLBACK (cb_component_state_changed), (gpointer)2); g_signal_connect (G_OBJECT (lagent), "new-selected-pair", G_CALLBACK (cb_new_selected_pair), (gpointer)1); g_signal_connect (G_OBJECT (ragent), "new-selected-pair", G_CALLBACK (cb_new_selected_pair), (gpointer)2); g_signal_connect (G_OBJECT (lagent), "new-candidate", G_CALLBACK (cb_new_candidate), (gpointer)1); g_signal_connect (G_OBJECT (ragent), "new-candidate", G_CALLBACK (cb_new_candidate), (gpointer)2); g_signal_connect (G_OBJECT (lagent), "initial-binding-request-received", G_CALLBACK (cb_initial_binding_request_received), (gpointer)1); g_signal_connect (G_OBJECT (ragent), "initial-binding-request-received", G_CALLBACK (cb_initial_binding_request_received), (gpointer)2); stun_server = getenv ("NICE_STUN_SERVER"); stun_server_port = getenv ("NICE_STUN_SERVER_PORT"); if (stun_server) { g_object_set (G_OBJECT (lagent), "stun-server", stun_server, NULL); g_object_set (G_OBJECT (lagent), "stun-server-port", atoi (stun_server_port), NULL); g_object_set (G_OBJECT (ragent), "stun-server", stun_server, NULL); g_object_set (G_OBJECT (ragent), "stun-server-port", atoi (stun_server_port), NULL); } /* step: run test the first time */ g_debug ("test-fallback: TEST STARTS / fallback test"); result = run_fallback_test (lagent, ragent, &baseaddr); priv_print_global_status (); g_assert (result == 0); g_assert (global_lagent_state == NICE_COMPONENT_STATE_READY); g_assert (global_ragent_state == NICE_COMPONENT_STATE_READY); /* step: run the safe test without sending any stnu */ g_debug ("test-fallback: TEST STARTS / safe fallback test"); result = run_safe_fallback_test (lagent, ragent, &baseaddr); priv_print_global_status (); g_assert (result == 0); g_assert (global_lagent_state == NICE_COMPONENT_STATE_READY); g_assert (global_ragent_state == NICE_COMPONENT_STATE_READY); 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 result; }
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; }