Chinaunix首页 | 论坛 | 博客
  • 博客访问: 68614
  • 博文数量: 11
  • 博客积分: 286
  • 博客等级: 二等列兵
  • 技术积分: 136
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-03 15:31
文章分类

全部博文(11)

文章存档

2015年(5)

2014年(3)

2011年(3)

我的朋友

分类: Python/Ruby

2014-12-24 13:58:13


  1. import pjsua as pj
  2. import time


  3. class sip_acc_cb(pj.AccountCallback):
  4.     def __init__(self, account = None):
  5.         pj.AccountCallback.__init__(self, account)
  6.         
  7.     def on_reg_state(self):
  8.         if self.account.info().reg_status == 200:
  9.             print self.account.info().uri, "register ok", self.account.info().reg_expires
  10.         elif self.account.info().reg_status > 200:
  11.             print self.account.info().uri, "register fail :", self.account.info().reg_status, self.account.info().reg_reason, self.account.info().reg_active, self.account.info().reg_expires

  12.     def on_incoming_call2(self, call, rdata):
  13.         print "incoming call :", "contact :", call.info().remote_contact, "from :", call.info().remote_uri
  14.         incoming_call_cb = sip_call_cb(call)
  15.         call.set_callback(incoming_call_cb)
  16.         call.answer(200)
  17.         
  18. class sip_call_cb(pj.CallCallback):
  19.     def __init__(self, call = None, ring_time = 60, call_duration = 60):
  20.         pj.CallCallback.__init__(self, call)
  21.         self.call_done = 0
  22.         self.call_ok = 0
  23.         self.ring_time = ring_time
  24.         self.call_duration = call_duration
  25.         
  26.     def bye(self, ring_time = 60, call_duration = 60):
  27.         cancel_time = 0
  28.         bye_time = 0
  29.         while( not self.call_done ):
  30.             time.sleep(1)
  31.             cancel_time += 1
  32.             if self.call_ok:
  33.                 bye_time += 1
  34.                 if bye_time >= call_duration and not self.call_done:
  35.                     print "bye call"
  36.                     self.call.hangup()
  37.                     break
  38.             elif cancel_time >= ring_time and not self.call_done:
  39.                 print "cancel call"
  40.                 self.call.hangup()
  41.                 break
  42.         else:
  43.             print "call done"
  44.         
  45.     def on_state(self):
  46.         print self.call.info().remote_uri, self.call.info().state, self.call.info().state_text, self.call.info().last_code, self.call.info().last_reason
  47.         if self.call.info().state == pj.CallState.DISCONNECTED:
  48.             self.call_done = 1
  49.         elif self.call.info().state == pj.CallState.CONFIRMED:
  50.             self.call_ok = 1
  51.             self.bye(self.ring_time, self.call_duration)

  52. class sip_ua():
  53.     def __init__(self, local_ip = "", local_port = 5060, enable_siptrace = False, level = 5, console_level = 5):
  54.         self.lib = pj.Lib()
  55.         ua_cfg = None
  56.         log_cfg = pj.LogConfig(callback = self.log_cb, level = level, console_level = console_level)
  57.         log_cfg.msg_logging = enable_siptrace
  58.         media_cfg = None
  59.         self.lib.init(ua_cfg = ua_cfg, log_cfg = log_cfg, media_cfg = media_cfg)
  60.         tp_cfg = pj.TransportConfig(local_port, local_ip)
  61.         udp = self.lib.create_transport(pj.TransportType.UDP, tp_cfg)
  62.         self.lib.start()
  63.         #print self.lib.get_snd_dev()
  64.         self.lib.set_null_snd_dev()
  65.         
  66.         self.acount_list = {}
  67.         
  68.     def create_account(self, account_name = "", domain = "", username = "", password = "", enable_register = True, register_expires = 3600):
  69.         sip_uri = pj.SIPUri()
  70.         sip_uri.scheme = "sip"
  71.         sip_uri.user = username
  72.         sip_uri.host = domain
  73.         acc_cfg = pj.AccountConfig()
  74.         acc_cfg.id = sip_uri.encode()
  75.         if enable_register:
  76.             acc_cfg.reg_uri = acc_cfg.id
  77.             acc_cfg.reg_timeout = register_expires
  78.             acc_cfg.auth_cred = [pj.AuthCred("*", username, password)]
  79.         acc_cb = sip_acc_cb()
  80.         acc = self.lib.create_account(acc_cfg, cb = acc_cb)
  81.         self.acount_list[account_name] = acc
  82.         
  83.     def create_call(self, account_name = "", callee_id = "", ring_time = 60, call_duration = 60):
  84.         call_cb = sip_call_cb(ring_time = ring_time, call_duration = call_duration)
  85.         self.call = self.acount_list[account_name].make_call(callee_id, cb = call_cb)
  86.     
  87.     def log_cb(self, log_level, log_str, log_len):
  88.         print log_level, log_str,
  89.     
  90.     def main_loop(self):
  91.         while True:
  92.             pass
  93.     
  94.     def end(self):
  95.         self.lib.destroy()
  96.         print "sip ua exit done"
  97.         

  98. uac = sip_ua(local_ip = "192.168.56.101", local_port = 5061, enable_siptrace = False, level = 1, console_level = 1)
  99. uac.create_account(account_name = "test1", domain = "192.168.56.104:5060", username = "1001", password = "1234", register_expires = 60, enable_register = True)
  100. uac.create_call(account_name = "test1", callee_id = "sip:5000@192.168.56.104:5060", ring_time = 60, call_duration = 6)
  101. uac.main_loop()


阅读(2503) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~