3

After going thru pygui code available in PJSIP library site, I modified the example as below. I have two issues

  1. In each call back function, at the end I need to add a dummy raise exception, else it misbehaves. For example, if I comment dummy exception of onIncomingCall, incoming call will get disconnected with 500 error. In pyGUI, it looks like ttk.master.after() plays critical role. As I am trying it in a headless server (mean only cli), not sure how to handle it?

  2. When I try to createRecorder getting following error:

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 &)

Actual modified code:

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()
Sekar S
  • 163
  • 1
  • 10
  • are you able to solve this issue? – user867662 May 14 '19 at 13:19
  • No. I switched to c++. But I was able to resolve all issues except createRecorder. In addition, I did a POC by using ffmpeg to direct to file. If interested will upload to github and share. Idea was create a dummy audio device and use it as end device and at Linux OS level using ffmpeg write to a file. – Sekar S May 18 '19 at 12:21
  • I would be interested in how did you solve the call back problem on onIncomingCall. Could you share POC over github? – Krisz Jun 01 '19 at 20:15
  • Unfortunately my hard disk crashed. What I could recover, I uploaded it to https://github.com/sekarpdkt/misc. Not yet tested (after recovery). It also has code to send audio to a dummy sound device, which will further be redirecting the voice to wav file using ffmpeg. You shall be able to get an idea. – Sekar S Jun 07 '19 at 03:53

1 Answers1

0

Before you compile the pjsua swig, add following lines to pjsua2.i in pjsip-apps/src/swig folder

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

With this extra function, you can create a variable with pj_ssize_t type and pass it to createRecorder function.

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

A workaround based on this discussion

Krisz
  • 701
  • 7
  • 23