gpt4 book ai didi

python - PJSUA2 - Python - 如何处理回调和 createRecorder?

转载 作者:行者123 更新时间:2023-11-28 18:59:45 24 4
gpt4 key购买 nike

通过 PJSIP 库站点中提供的 pygui 代码后,我修改了示例如下。我有两个问题

  1. 在每个回调函数中,最后我需要添加一个虚拟的 raise 异常,否则它会出错。例如,如果我评论 onIncomingCall 的虚拟异常,来电将断开连接并出现 500 错误。在 pyGUI 中,看起来 ttk.master.after() 起着至关重要的作用。当我在 headless 服务器(意味着只有 cli)中尝试它时,不确定如何处理它?<​​/p>

  2. 当我尝试创建记录器时出现以下错误:

Traceback (most recent call last): File "pjsua2_cli_demo.py", line 33, in onCallState self.recorder.createRecorder('xxxxxxxxxxx/PJSUA2/example/pygui/file.wav'); File "xxxxxxxxxxx/.local/lib/python3.6/site-packages/pjsua2.py", line 4110, in createRecorder return _pjsua2.AudioMediaRecorder_createRecorder(self, file_name, enc_type, max_size, options) NotImplementedError: Wrong number or type of arguments for overloaded function 'AudioMediaRecorder_createRecorder'. Possible C/C++ prototypes are: pj::AudioMediaRecorder::createRecorder(pj::string const &,unsigned int,pj_ssize_t,unsigned int) pj::AudioMediaRecorder::createRecorder(pj::string const &,unsigned int,pj_ssize_t) pj::AudioMediaRecorder::createRecorder(pj::string const &,unsigned int) pj::AudioMediaRecorder::createRecorder(pj::string const &)

实际修改代码:

import pjsua2 as pj
import time
# Subclass to extend the Account and get notifications etc.

ep=None
# Call class
class Call(pj.Call):
"""
High level Python Call object, derived from pjsua2's Call object.
"""
def __init__(self, acc, peer_uri='', chat=None, call_id = pj.PJSUA_INVALID_ID):
pj.Call.__init__(self, acc, call_id)
self.acc = acc

self.aud_med=pj.AudioMedia

def onCallState(self, prm):
ci = self.getInfo()
self.connected = ci.state == pj.PJSIP_INV_STATE_CONFIRMED
self.recorder=None
if(self.connected ==True):
player=pj.AudioMediaPlayer()
#Play welcome message
player.createPlayer('xxxxxxxxxxxxxx/PJSUA2/example/pygui/welcomeFull.wav');

self.recorder=pj.AudioMediaRecorder()
self.recorder.createRecorder('xxxxxxxxxxx/PJSUA2/example/pygui/file.wav', enc_type=0, max_size=0, options=0);
i=0
for media in ci.media:

if (media.type == pj.PJMEDIA_TYPE_AUDIO):
self.aud_med = self.getMedia(i);
break;
i=i+1;
if self.aud_med!=None:
# This will connect the sound device/mic to the call audio media
mym= pj.AudioMedia.typecastFromMedia(self.aud_med)
player.startTransmit( mym);
#mym.startTransmit( self.recorder);
if(ci.state==pj.PJSIP_INV_STATE_DISCONNECTED):
print(">>>>>>>>>>>>>>>>>>>>>>> Call disconnected")
#mym= pj.AudioMedia.typecastFromMedia(self.aud_med)
#mym.stopTransmit(self.recorder);
raise Exception('onCallState done!')


if self.chat:
self.chat.updateCallState(self, ci)

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):
if mi.status == pj.PJSUA_CALL_MEDIA_REMOTE_HOLD and not self.onhold:
self.chat.addMessage(None, "'%s' sets call onhold" % (self.peerUri))
self.onhold = True
elif mi.status == pj.PJSUA_CALL_MEDIA_ACTIVE and self.onhold:
self.chat.addMessage(None, "'%s' sets call active" % (self.peerUri))
self.onhold = False
raise Exception('onCallMediaState done!')

class Account(pj.Account):
def onRegState(self, prm):
print ("***OnRegState: " + prm.reason)
def onIncomingCall(self, prm):
c = Call(self, call_id=prm.callId)
call_prm = pj.CallOpParam()
call_prm.statusCode = 180
c.answer(call_prm)

ci = c.getInfo()
msg = "Incoming call from '%s'" % (ci.remoteUri)
print(msg)
call_prm.statusCode = 200
c.answer(call_prm)
raise Exception('onIncomingCall done!')



# pjsua2 test function
def pjsua2_test():
# Create and initialize the library
ep_cfg = pj.EpConfig()
ep_cfg.uaConfig.threadCnt = 0
ep_cfg.uaConfig.mainThreadOnly = False
ep = pj.Endpoint()
ep.libCreate()
ep.libInit(ep_cfg)

# Create SIP transport. Error handling sample is shown
sipTpConfig = pj.TransportConfig();
sipTpConfig.port = 12345;
tp=ep.transportCreate(pj.PJSIP_TRANSPORT_UDP, sipTpConfig);
# Start the library
ep.libStart();

acfg = pj.AccountConfig();

acfg.idUri = "sip:192.168.1.11:12345";

# Create the account
acc = Account();
acc.create(acfg)


while True:
ep.libHandleEvents(10)


ep.libDestroy()
del ep;

#
# main()
#
if __name__ == "__main__":
pjsua2_test()

最佳答案

在编译 pjsua swig 之前,将以下行添加到 pjsip-apps/src/swig 文件夹中的 pjsua2.i

%inline %{
pj_ssize_t new_pj_ssize_t(int s) {
return (pj_ssize_t) s;
}
%}

使用这个额外的函数,您可以创建一个 pj_ssize_t 类型的变量并将其传递给 createRecorder 函数。

max_size=pj.new_pj_ssize_t(0)
recorder.createRecorder(file_to_record, max_size=max_size)

基于 this 的解决方法讨论

关于python - PJSUA2 - Python - 如何处理回调和 createRecorder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54025192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com