Análisis de flujo de llamadas de Libnice

Introducción a libnice:

La biblioteca libnice es un conjunto de bibliotecas de conexiones de comunicación basadas en el protocolo ICE.
La función principal es realizar la conexión p2p y el envío y recepción de flujos de datos multimedia. Es similar a libjingo en el código fuente de webrtc. Podemos usar la biblioteca libnice en nuestro proyecto para realizar una conexión ICE de un extremo a otro y el envío y la recepción de flujos de datos. Y el intercambio de candidatos (direcciones de candidatos) y SDP (archivos de descripción de medios).
La biblioteca libnice se basa en el lenguaje glibc, es multiplataforma y se puede utilizar tanto en Linux como en teléfonos móviles, pero depende de la biblioteca glib.

Proceso de llamada de función de uso común de Libnice:

#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);

Información oficial de la biblioteca: https://libnice.freedesktop.org/libnice/NiceAgent.html

Supongo que te gusta

Origin blog.csdn.net/tong5956/article/details/108323004
Recomendado
Clasificación