I'm trying to develop a code in Python that first makes a sip call to an extension and when the call is answered it plays an audio file, I managed to authenticate the account but the call is not made, below is my code, me too I want to implement call reception in sequence, I can even see the call arriving but I don't know how to configure it to answer. and I would like the community's help to adjust the code below to serve as a basic reference for authentication, originating calls and receiving calls using Python.
import pjsua2 as pj
import time
class Account(pj.Account):
def onRegState(self, prm):
print("***OnRegState: " + prm.reason)
if prm.code == 200:
make_call()
class MyCall(pj.Call):
def __init__(self, acc, dest_uri):
pj.Call.__init__(self, acc)
self.dest_uri = dest_uri
def on_state(self):
if self.info().state == pj.PJSIP_INV_STATE_CALLING:
print("Chamando", self.dest_uri)
elif self.info().state == pj.PJSIP_INV_STATE_INCOMING:
print("Chamada recebida de", self.info().remote_uri)
self.answer()
elif self.info().state == pj.PJSIP_INV_STATE_EARLY:
pass
elif self.info().state == pj.PJSIP_INV_STATE_CONNECTING:
pass
elif self.info().state == pj.PJSIP_INV_STATE_CONFIRMED:
print("Chamada conectada. Reproduzindo áudio...")
self.start_audio("testeaudio.wav")
elif self.info().state == pj.PJSIP_INV_STATE_DISCONNECTED:
print("Chamada desconectada, motivo:", self.info().last_reason)
self.delete()
# pjsua2 test function
def pjsua2_test():
# Create and initialize the library
ep_cfg = pj.EpConfig()
ep = pj.Endpoint()
ep.libCreate()
ep.libInit(ep_cfg)
# Create SIP transport. Error handling sample is shown
sipTpConfig = pj.TransportConfig()
sipTpConfig.port = 5064
ep.transportCreate(pj.PJSIP_TRANSPORT_UDP, sipTpConfig)
# Start the library
ep.libStart()
acfg = pj.AccountConfig()
acfg.idUri = "sip:1523@sbc.dominio.cloud:5064"
acfg.regConfig.registrarUri = "sip:sbc.dominio.cloud:5064"
cred = pj.AuthCredInfo("digest", "*", "9999", 0, "9999#")
acfg.sipConfig.authCreds.append(cred)
# Create the account
acc = Account()
acc.create(acfg)
# Here we don't have anything else to do..
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("Encerrando chamada...")
if acc.calls.count > 0:
call = acc.calls[0]
call.hangup()
time.sleep(2)
ep.libDestroy()
def make_call():
dest_uri = "sip:1571@sbc.dominio.cloud:5064" # Ramal 1571
acc = pj.Account()
call = MyCall(acc, dest_uri)
call.make_call(dest_uri)
#
# main()
#
if __name__ == "__main__":
pjsua2_test()
Try call construction like this
ci = call.getInfo()
mi = ci[0]
m = call.getMedia(mi.index)
am = pj.AudioMedia.typecastFromMedia(m)
wav_player = pj.AudioMediaPlayer()
wav_player.createPlayer('path/to/audio.wav', True)
wav_player.startTransmit(am)
play_media = True```