Benny Prijono | 9c46114 | 2008-07-10 22:41:20 +0000 | [diff] [blame] | 1 | # $Id:$ |
| 2 | # |
| 3 | # SIP account and registration sample. In this sample, the program |
| 4 | # will block to wait until registration is complete |
| 5 | # |
| 6 | # Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
| 7 | # |
| 8 | import sys |
| 9 | import pjsua as pj |
| 10 | import threading |
| 11 | |
| 12 | |
| 13 | def log_cb(level, str, len): |
| 14 | print str, |
| 15 | |
| 16 | class MyAccountCallback(pj.AccountCallback): |
| 17 | sem = None |
| 18 | |
| 19 | def __init__(self, account): |
| 20 | pj.AccountCallback.__init__(self, account) |
| 21 | |
| 22 | def wait(self): |
| 23 | self.sem = threading.Semaphore(0) |
| 24 | self.sem.acquire() |
| 25 | |
| 26 | def on_reg_state(self): |
| 27 | if self.sem: |
| 28 | if self.account.info().reg_status >= 200: |
| 29 | self.sem.release() |
| 30 | |
| 31 | lib = pj.Lib() |
| 32 | |
| 33 | try: |
| 34 | lib.init(log_cfg = pj.LogConfig(level=4, callback=log_cb)) |
| 35 | lib.create_transport(pj.TransportType.UDP, pj.TransportConfig(5080)) |
| 36 | lib.start() |
| 37 | |
| 38 | acc = lib.create_account(pj.AccountConfig("pjsip.org", "bennylp", "***")) |
| 39 | |
| 40 | acc_cb = MyAccountCallback(acc) |
| 41 | acc.set_callback(acc_cb) |
| 42 | acc_cb.wait() |
| 43 | |
| 44 | print "\n" |
| 45 | print "Registration complete, status=", acc.info().reg_status, \ |
| 46 | "(" + acc.info().reg_reason + ")" |
| 47 | print "\nPress ENTER to quit" |
| 48 | sys.stdin.readline() |
| 49 | |
| 50 | lib.destroy() |
| 51 | lib = None |
| 52 | |
| 53 | except pj.Error, e: |
| 54 | print "Exception: " + str(e) |
| 55 | lib.destroy() |
| 56 | |