我正在尝试通过NAT网络使用pjsua2“(pjsip)”进行呼叫。我的客户端PJSUA位于NAT后面,我想使用STUN功能。
import pjsua2 as pj
import time
class myCall(pj.Call):
def __init__(self,acc,call_id=pj.PJSUA_INVALID_ID):
pj.Call.__init__(self,acc,call_id)
self.acc = acc
self.connected = False
self.onhold = False
def onCallState(self, prm):
ci = self.getInfo()
self.connected = ci.state == pj.PJSIP_INV_STATE_CONFIRMED
print("On Call State")
def onCallMediaState(self, prm):
ci = self.getInfo()
for mi in ci.media:
if mi.type == pj.PJMEDIA_TYPE_AUDIO and \
(mi.status == pj.PJSUA_CALL_MEDIA_ACTIVE or \
mi.status == pj.PJSUA_CALL_MEDIA_REMOTE_HOLD):
m = self.getMedia(mi.index)
am = pj.AudioMedia.typecastFromMedia(m)
# connect ports
ep.Endpoint.instance.audDevManager().getCaptureDevMedia().startTransmit(am)
am.startTransmit(ep.Endpoint.instance.audDevManager().getPlaybackDevMedia())
def onCallMediaTransportState(self, prm):
print("OnCallMediaTrasportState")
ep_cfg = pj.EpConfig()
ep_cfg.logConfig.level = 6
ep_cfg.uaConfig.threadCnt = 0
ep_cfg.uaConfig.nameserver.append("8.8.8.8")
#ep_cfg.uaConfig.nameserver.push_back("8.8.4.4")
#ep_cfg.uaConfig.nameserver.push_back("1.1.1.1")
ep_cfg.uaConfig.userAgent = "Talk 1.0.0";
ep_cfg.uaConfig.stunServer.append("stun.voip.eutelia.it:3478")
ep_cfg.uaConfig.natTypeInSdp = 2
ep = pj.Endpoint()
ep.libCreate()
time.sleep(3)
ep.audDevManager().setCaptureDev(-1)
ep.audDevManager().setPlaybackDev(-1)
#ep.audDevManager().setNullDev()
sipTpConfig = pj.TransportConfig()
sipTpConfig.boundAddress="192.168.0.214"
sipTpConfig.publicAddress="<MY public Address"
sipTpConfig.port=5061
ep.transportCreate(pj.PJSIP_TRANSPORT_UDP,sipTpConfig)
ep.libInit(ep_cfg)
time.sleep(5)
ep.libStart()
time.sleep(5)
acfg = pj.AccountConfig()
acfg.idUri = "192.168.0.214:123456"
acfg.callConfig.timerMinSESec = 120;
acfg.callConfig.timerSessExpiresSec = 1800;
cred = pj.AuthCredInfo("digest","*", "sergio",0,"")
acfg.sipConfig.authCreds.append(cred)
acfg.natConfig.mediaStunUse = pj.PJSUA_STUN_USE_DEFAULT
acfg.natConfig.sipStunUse = pj.PJSUA_STUN_USE_DEFAULT
#acfg.natConfig.iceEnabled = True
#acfg.natConfig.iceNoRtcp = True
acc = pj.Account()
acc.create(acfg)
acc.setDefault()
call_param = pj.CallOpParam()
call_param.opt.audioCount = 1
call_param.opt.videoCount = 0
call = myCall(acc)
call.makeCall("<calling address",call_param)
time.sleep(100)
call.hangup(call_param)
time.sleep(5)
ep.libDestroy()
我的日志:
20:07:58.739 os_core_unix.c !pjlib 2.10 for POSIX initialized
20:07:58.739 sip_endpoint.c .Creating endpoint instance...
20:07:58.740 pjlib .select() I/O Queue created (0x1a96e00)
20:07:58.740 sip_endpoint.c .Module "mod-msg-print" registered
20:07:58.740 sip_transport.c .Transport manager created.
20:07:58.740 pjsua_core.c .PJSUA state changed: NULL --> CREATED
20:08:01.743 pjsua_aud.c Set sound device: capture=-1, playback=-2
20:08:01.743 pjsua_aud.c Set sound device: capture=-1, playback=-1
20:08:01.744 pjsua_core.c SIP UDP socket reachable at 192.168.95.214:5061
20:08:01.744 udp0x1979230 SIP UDP transport started, published address is 192.168.95.214:5061
20:08:01.744 sip_endpoint.c .Module "mod-pjsua-log" registered
20:08:01.744 sip_endpoint.c .Module "mod-tsx-layer" registered
20:08:01.744 sip_endpoint.c .Module "mod-stateful-util" registered
20:08:01.744 sip_endpoint.c .Module "mod-ua" registered
20:08:01.744 sip_endpoint.c .Module "mod-100rel" registered
20:08:01.744 sip_endpoint.c .Module "mod-pjsua" registered
20:08:01.744 sip_endpoint.c .Module "mod-invite" registered
20:08:01.844 alsa_dev.c ..ALSA driver found 15 devices
20:08:01.844 alsa_dev.c ..ALSA initialized
20:08:01.844 pjlib ..select() I/O Queue created (0x1aab938)
20:08:01.845 sip_endpoint.c .Module "mod-evsub" registered
20:08:01.845 sip_endpoint.c .Module "mod-presence" registered
20:08:01.845 sip_endpoint.c .Module "mod-mwi" registered
20:08:01.845 sip_endpoint.c .Module "mod-refer" registered
20:08:01.845 sip_endpoint.c .Module "mod-pjsua-pres" registered
20:08:01.845 sip_endpoint.c .Module "mod-pjsua-im" registered
20:08:01.845 sip_endpoint.c .Module "mod-pjsua-options" registered
20:08:01.845 pjsua_core.c .No SIP worker threads created
20:08:01.845 pjsua_core.c .pjsua version 2.10 for Linux-5.3.0.46/x86_64/glibc-2.27 initialized
20:08:01.845 pjsua_core.c .PJSUA state changed: CREATED --> INIT
20:08:06.850 pjsua_core.c PJSUA state changed: INIT --> STARTING
20:08:06.850 sip_endpoint.c .Module "mod-unsolicited-mwi" registered
20:08:06.850 pjsua_core.c .PJSUA state changed: STARTING --> RUNNING
20:08:11.854 pjsua_acc.c Adding account: id=192.168.0.214:123456
20:08:11.854 pjsua_acc.c .Account 192.168.0.214:123456 added with id 0
20:08:11.854 pjsua_call.c Making call with acc #0 to sip:05805948@gw.collaboratespace.net
20:08:11.854 pjsua_aud.c .Set sound device: capture=-1, playback=-1
20:08:11.854 pjsua_aud.c ..Opening sound device (speaker + mic) PCM@16000/1/20ms
20:08:11.883 ec0x1add440 ...Speex AEC created, clock_rate=16000, channel=1, samples per frame=320, tail length=200 ms, latency=0 ms
20:08:11.883 pjsua_media.c .Call 0: initializing media..
OnCallMediaTrasportState
20:08:11.883 pjsua_media.c ..RTP socket reachable at 192.168.95.214:4000
20:08:11.883 pjsua_media.c ..RTCP socket reachable at 192.168.95.214:4001
OnCallMediaTrasportState
20:08:11.883 pjsua_media.c ..Media index 0 selected for audio call 0
OnCallMediaTrasportState
20:08:11.923 tsx0x1b3b088 ....Temporary failure in sending Request msg INVITE/cseq=32556 (tdta0x1b38118), will try next server: Unsupported transport (PJSIP_EUNSUPTRANSPORT)
20:08:11.923 pjsua_core.c ....TX 1394 bytes Request msg INVITE/cseq=32556 (tdta0x1b38118) to UDP 3.248.---.156:5060:
INVITE sip:0580---8@------space.net SIP/2.0
Via: SIP/2.0/UDP 192.168.95.214:5061;rport;branch=z9hG4bKPjnv7iBdFY7jrdBaxBTghSpxcAMTKvDQjh
Max-Forwards: 70
From: 192.168.0.214:123456;tag=zujBV5h2G85g9TOSPQFQP7zpfqppiHC0
To: sip:0580---8@------------space.net
Contact: <sip:192.168.95.214:5061;ob>
Call-ID: Bd2FSxV31taEzrSto.l-l3ukgWc8V-2.
CSeq: 32556 INVITE
Allow: PRACK, INVITE, ACK, BYE, CANCEL, UPDATE, INFO, SUBSCRIBE, NOTIFY, REFER, MESSAGE, OPTIONS
Supported: replaces, 100rel, timer, norefersub
Session-Expires: 1800
Min-SE: 120
User-Agent: Talk 1.0.0
Content-Type: application/sdp
Content-Length: 759
v=0
o=- 3794926091 3794926091 IN IP4 192.168.95.214
s=pjmedia
b=AS:117
t=0 0
a=X-nat:0 Unknown
m=audio 4000 RTP/AVP 96 97 98 99 3 0 8 9 100 120 121 122 123
c=IN IP4 192.168.95.214
b=TIAS:96000
a=rtcp:4001 IN IP4 192.168.95.214
a=sendrecv
a=rtpmap:96 speex/16000
a=rtpmap:97 speex/8000
a=rtpmap:98 speex/32000
a=rtpmap:99 iLBC/8000
a=fmtp:99 mode=30
a=rtpmap:3 GSM/8000
a=rtpmap:0 PCMU/8000
a=rtpmap:8 PCMA/8000
a=rtpmap:9 G722/8000
a=rtpmap:100 opus/48000/2
a=fmtp:100 useinbandfec=1
a=rtpmap:120 telephone-event/16000
a=fmtp:120 0-16
a=rtpmap:121 telephone-event/8000
a=fmtp:121 0-16
a=rtpmap:122 telephone-event/32000
a=fmtp:122 0-16
a=rtpmap:123 telephone-event/48000
a=fmtp:123 0-16
a=ssrc:1538071663 cname:1555ca32761d1274
--end msg--
On Call State
20:08:16.652 sound_port.c EC suspended because of inactivity
如您所见,函数 onCallMediaState(self,prm)从未被调用。另外,没有与电击服务器的通信。 我也尝试启用ICE模式,但是在ICE处于活动状态时,通话开始但未到达目的地
哪种是在PJSUA2中设置NAT的正确方法