libnice_simple_test.nim prints selected pair

This commit is contained in:
demotomohiro 2020-12-06 20:18:32 +09:00
commit 24b5adab30

View file

@ -9,7 +9,7 @@ when (compiles do: import gintro/gtk):
else: else:
import gintro/[dummygtk, glib, gobject, gio, nice] # For system has glib but no gtk import gintro/[dummygtk, glib, gobject, gio, nice] # For system has glib but no gtk
import strformat import strformat, nativesockets
const const
ltestMessage = "some test message" ltestMessage = "some test message"
@ -26,6 +26,9 @@ proc toIntVal(i: int): Value =
discard init(result, gtype) discard init(result, gtype)
setInt(result, i) setInt(result, i)
proc addrString(niceAddr: Address): string =
getAddrString(unsafeAddr niceAddr.`addr`)
proc cbCandidateGatheringDone(agent: nice.Agent; streamId: int) = proc cbCandidateGatheringDone(agent: nice.Agent; streamId: int) =
proc copyCandidates(src, dst: nice.Agent; srcStreamId, dstStreamId: int) = proc copyCandidates(src, dst: nice.Agent; srcStreamId, dstStreamId: int) =
var var
@ -50,6 +53,9 @@ proc cbCandidateGatheringDone(agent: nice.Agent; streamId: int) =
inc gatheringCount inc gatheringCount
proc cbNewSelectedPairFull(self: Agent; streamId: int; componentId: int; lcandidate: Candidate; rcandidate: Candidate) =
echo &"SIGNAL: selected pair ({lcandidate.impl.`addr`.addrString}:{lcandidate.impl.`addr`.port} {rcandidate.impl.`addr`.addrString}:{rcandidate.impl.`addr`.port})"
proc cbComponentStateChanged(agent: nice.Agent; streamId: int; componentId: int; state: int) = proc cbComponentStateChanged(agent: nice.Agent; streamId: int; componentId: int; state: int) =
doAssert agent == lagent or agent == ragent doAssert agent == lagent or agent == ragent
@ -57,6 +63,13 @@ proc cbComponentStateChanged(agent: nice.Agent; streamId: int; componentId: int;
echo &"SIGNAL: state changed {streamId}, {componentId}, {stat}" echo &"SIGNAL: state changed {streamId}, {componentId}, {stat}"
case stat case stat
of ComponentState.connected:
var
local = Candidate(ignoreFinalizer: true)
remote = Candidate(ignoreFinalizer: true)
## Get current selected candidate pair and print IP address used
if getSelectedPair(agent, streamId, componentId, local, remote):
echo &"Negotiation complete: ({local.impl.`addr`.addrString}:{local.impl.`addr`.port} {remote.impl.`addr`.addrString}:{remote.impl.`addr`.port})"
of ComponentState.ready: of ComponentState.ready:
if agent == lagent: if agent == lagent:
doAssert agent.send(streamId, componentId, ltestMessage.len, ltestMessage.cstring) == ltestMessage.len doAssert agent.send(streamId, componentId, ltestMessage.len, ltestMessage.cstring) == ltestMessage.len
@ -99,6 +112,8 @@ proc main =
lagent.connect("candidate-gathering-done", cbCandidateGatheringDone) lagent.connect("candidate-gathering-done", cbCandidateGatheringDone)
ragent.connect("candidate-gathering-done", cbCandidateGatheringDone) ragent.connect("candidate-gathering-done", cbCandidateGatheringDone)
lagent.connect("new-selected-pair-full", cbNewSelectedPairFull)
ragent.connect("new-selected-pair-full", cbNewSelectedPairFull)
lagent.connect("component-state-changed", cbComponentStateChanged) lagent.connect("component-state-changed", cbComponentStateChanged)
ragent.connect("component-state-changed", cbComponentStateChanged) ragent.connect("component-state-changed", cbComponentStateChanged)