0

i am making call from my raspberry pi using (python pjsua ) . if receiver has disconnected. i am unable to identify that call is disconnected. I intend to create this for a land phone to cellphone VoIP call transferring device . i haven't write the full code because i cant solve this basic problem. So how can i identify the receiver ( android phone ) disconnected the call

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

`

this method not working. my program dint quit after the caller has disconnected

full code below

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()
Seyon Seyon
  • 527
  • 4
  • 14

1 Answers1

0

change as

if self.call.info().state == pjsua.CallState.CONFIRMED:
    "your statements"
if self.call.info().state == pjsua.CallState.DISCONNECTED:
    "your statements"

elif statements don't flag in your case.

You have to move that out from CONFIRMED state

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


        self.call.hangup()
        in_call = False

because when the call is disconnected the state changes to DISCONNECTED

Raady
  • 1,686
  • 5
  • 22
  • 46
  • just try to print the status and check if states are changing or not. If not check the asterisk server if properly installed or not. If server configuration is ok, it should trigger the flags correctly. – Raady Aug 14 '18 at 12:37