Pyhthon pjsip如何识别接收器已断开呼叫

时间:2018-06-14 20:29:53

标签: python raspberry-pi3 pjsua2

我使用(python pjsua)从我的覆盆子pi打电话。如果接收器断开连接我无法确定该呼叫已断开连接。我打算为陆地电话创建这个手机VoIP呼叫转移设备。我没有写完整的代码,因为我无法解决这个基本问题。那么如何识别接收器(android手机)断开呼叫

while in_call:
   if self.call.info().state == pjsua.CallState.DISCONNECTED:
      in_call = False
      break
   pass

`

此方法不起作用。调用者断开连接后我的程序dint退出

以下完整代码

import sys
import pjsua
import threading
import wave
from time import sleep
callerid = 0
#acc_cfg.ka_interval =30; re-registration period

def log_cb(level, str, len):
    print str,

class MyAccountCallback(pjsua.AccountCallback):
    sem = None

    def __init__(self, account=None):
        pjsua.AccountCallback.__init__(self, account)

    def wait(self):
        self.sem = threading.Semaphore(0)
        self.sem.acquire()

    def on_reg_state(self):
        if self.sem:
            if self.account.info().reg_status >= 200:
                self.sem.release()

def cb_func(pid) :
    print '%s playback is done' % pid
    current_call.hangup()


# Callback to receive events from Call
class MyCallCallback(pjsua.CallCallback):

    def __init__(self, call=None):
        pjsua.CallCallback.__init__(self, call)

    # Notification when call state has changed
    def on_state(self):
        global current_call
        global in_call
        print "Call with", self.call.info().remote_uri,
        print "is", self.call.info().state_text,
        print "last code =", self.call.info().last_code, 
        print "(" + self.call.info().last_reason + ")"

        if self.call.info().state == pjsua.CallState.DISCONNECTED:
            current_call = None
            print 'Current call is', current_call

            in_call = False
        elif self.call.info().state == pjsua.CallState.CONFIRMED:
            #Call is Answred
            print "Call Answred"

            call_slot = self.call.info().conf_slot

            #if self.call.info().media_state == pj.MediaState.ACTIVE:

            call_slot = self.call.info().conf_slot

            lib.conf_connect(call_slot, 0)

            lib.conf_connect(0, call_slot)

            print("Hey !!!!! Hope you are doing GOOD !!!!!!!!!!")

            while in_call:
                if self.call.info().state == pjsua.CallState.DISCONNECTED:
                    in_call = False
                    break
                pass


            self.call.hangup()
            in_call = False

    # Notification when call's media state has changed.
    def on_media_state(self):
        if self.call.info().media_state == pjsua.MediaState.ACTIVE:
            print "Media is now active"
        else:
            print "Media is inactive"

# Function to make call
def make_call(uri):
    try:
        print "Making call to", uri
        return acc.make_call(uri, cb=MyCallCallback())
    except pjsua.Error, e:
        print "Exception: " + str(e)
        return None


lib = pjsua.Lib()

try:
    lib.init(log_cfg = pjsua.LogConfig(level=4, callback=log_cb))
    lib.create_transport(pjsua.TransportType.UDP, pjsua.TransportConfig(6500))
    #lib.set_null_snd_dev()
    lib.start()
    lib.handle_events()

    acc_cfg = pjsua.AccountConfig()
    acc_cfg.id = "sip:111@192.168.1.102"
    acc_cfg.reg_uri = "sip:111@192.168.1.102"
    acc_cfg.proxy = [ "sip:111@192.168.1.102;lr" ]
    acc_cfg.auth_cred = [ pjsua.AuthCred("*", "111@111@192.168.1.102", "0000") ]


    acc_cb = MyAccountCallback()
    acc = lib.create_account(acc_cfg, cb=acc_cb)

    acc_cb.wait()

    print "\n"
    print "Registration complete, status=", acc.info().reg_status, \
          "(" + acc.info().reg_reason + ")"


    #YOURDESTINATION is landline or mobile number you want to call
    dst_uri="sip:100@192.168.1.102"

    in_call = True
    lck = lib.auto_lock()
    current_call = make_call(dst_uri)
    print 'Current call is', current_call
    del lck

    #wait for the call to end before shuting down
    while in_call:
        pass
    sys.stdin.readline()
    lib.destroy()
    lib = None

except pjsua.Error, e:
    print "Exception: " + str(e)
    lib.destroy()

1 个答案:

答案 0 :(得分:0)

更改为

if self.call.info().state == pjsua.CallState.CONFIRMED:
    "your statements"
if self.call.info().state == pjsua.CallState.DISCONNECTED:
    "your statements"
在您的情况下,elif声明不会标记。

您必须将其从确认状态移出

        while in_call:
            if self.call.info().state == pjsua.CallState.DISCONNECTED:
                in_call = False
                break
            pass


        self.call.hangup()
        in_call = False

因为当呼叫断开时,状态变为DISCONNECTED