Libnice call flow analysis

Introduction to libnice:

The libnice library is a set of communication connection libraries based on the ICE protocol.
The main function is to realize p2p connection and media data stream sending and receiving. It is similar to libjingo in the webrtc source code. We can use libnice library in our project to realize end-to-end ICE connection and data stream sending and receiving. And the exchange of candidates (candidate addresses) and SDP (media description files).
The libnice library is based on the glibc language, cross-platform, and can be used on both Linux and mobile phones, but it depends on the glib library.

Libnice commonly used function call process:

#include <agent.h>

guint stream_id;
gchar buffer[] = "hello world!";
gchar *ufrag = NULL, *pwd = NULL;
gchar *remote_ufrag, *remote_pwd;
GSList *lcands = NULL;

// Create a nice agent, passing in the global default GMainContext.
NiceAgent *agent = nice_agent_new (NULL, NICE_COMPATIBILITY_RFC5245);
spawn_thread_to_run_main_loop (g_main_loop_new (NULL, FALSE));

// Connect the signals
g_signal_connect (G_OBJECT (agent), "candidate-gathering-done",
                  G_CALLBACK (cb_candidate_gathering_done), NULL);
g_signal_connect (G_OBJECT (agent), "component-state-changed",
                  G_CALLBACK (cb_component_state_changed), NULL);
g_signal_connect (G_OBJECT (agent), "new-selected-pair",
                  G_CALLBACK (cb_new_selected_pair), NULL);

// Create a new stream with one component and start gathering candidates
stream_id = nice_agent_add_stream (agent, 1);
nice_agent_gather_candidates (agent, stream_id);

// Attach I/O callback the component to ensure that:
// 1) agent gets its STUN packets (not delivered to cb_nice_recv)
// 2) you get your own data
nice_agent_attach_recv (agent, stream_id, 1, NULL,
                       cb_nice_recv, NULL);

// ... Wait until the signal candidate-gathering-done is fired ...
lcands = nice_agent_get_local_candidates(agent, stream_id, 1);

nice_agent_get_local_credentials(agent, stream_id, &ufrag, &pwd);

// ... Send local candidates and credentials to the peer

// Set the peer's remote credentials and remote candidates
nice_agent_set_remote_credentials (agent, stream_id, remote_ufrag, remote_pwd);
nice_agent_set_remote_candidates (agent, stream_id, 1, rcands);

// ... Wait until the signal new-selected-pair is fired ...
// Send our message!
nice_agent_send (agent, stream_id, 1, sizeof(buffer), buffer);

// Anything received will be received through the cb_nice_recv callback.
// You must be running a GMainLoop on the global default GMainContext in
// another thread for this to work.

// Destroy the object
g_object_unref(agent);

Official libnice information: https://libnice.freedesktop.org/libnice/NiceAgent.html

Guess you like

Origin blog.csdn.net/tong5956/article/details/108323004