簡體   English   中英

PJSUA2 - Python - 如何處理回調和createRecorder?

[英]PJSUA2 - Python - How to handle callbacks and createRecorder?

在通過PJSIP庫網站提供的pygui代碼后,我修改了如下例子。 我有兩個問題

  1. 在每個回調函數中,最后我需要添加一個虛擬引發異常,否則它會行為異常。 例如,如果我評論onIncomingCall的虛擬異常,則來電將因500錯誤而斷開連接。 在pyGUI中,它看起來像ttk.master.after()扮演關鍵角色。 正如我在一個無頭服務器(僅意味着cli)中嘗試它,不知道如何處理它?

  2. 當我嘗試createRecorder獲取以下錯誤時:

回溯(最近一次調用最后一次):文件“pjsua2_cli_demo.py”,第33行,在onCallState self.recorder.createRecorder('xxxxxxxxxxx / PJSUA2 / example / pygui / file.wav'); 文件“xxxxxxxxxxx / .local / lib / python3.6 / site-packages / pjsua2.py”,第4110行,在createRecorder中返回_pjsua2.AudioMediaRecorder_createRecorder(self,file_name,enc_type,max_size,options)NotImplementedError:參數的數量或類型錯誤對於重載函數'AudioMediaRecorder_createRecorder'。 可能的C / C ++原型是: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)

基於討論的解決方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM