libnice解讀

libnice解讀

Overview

libnice是解決p2p問題的庫(kù),兼容多種協(xié)議。本文主要在janus服務(wù)器與webrtc通信的環(huán)境下的解讀。libnice基于glib開發(fā),所以最好先了解glib

simple-example

  // loop有點(diǎn)類似ios中的runloop
  gloop = g_main_loop_new(NULL, FALSE);
  io_stdin = g_io_channel_unix_new(fileno(stdin));
  // 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);
    g_signal_connect (agent, "new-selected-pair-full",G_CALLBACK (janus_ice_cb_new_selected_pair), NULL);

  // Create a new stream with one component,(其中component序號(hào)是從1開始)
  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);  

也可以自己直接通過glib接口直接創(chuàng)建更加詳細(xì)的agent:
handle->agent = g_object_new(NICE_TYPE_AGENT, "compatibility", NICE_COMPATIBILITY_DRAFT19, "main-context", icectx, "reliable", FALSE, "full-mode", TRUE, "ice-udp", TRUE, "ice-tcp", FALSE, NULL);

reliable表示是否可靠傳輸,一般用false,UDP穿墻成功率更高,full-mode就是表示是否不是lite模式,所以false表示是liteice模式。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容