Advertisement
Guest User

Untitled

a guest
May 13th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. # SIP Client (Soft phone prototype)
  2. # Created by Arpit Singh
  3. # Run the server before running this file "$sudo asterisk -r" in other terminal
  4. #In user URI enter only the Called user name
  5.  
  6.  
  7.  
  8. import sys
  9.  
  10. import pjsua as pj
  11.  
  12. import threading
  13.  
  14. import time
  15.  
  16.  
  17.  
  18. # Log of callback class
  19.  
  20. def log_cb(level, str, len):
  21.  
  22. print(str),
  23.  
  24.  
  25.  
  26. # Account Callback class to get notifications of Account registration
  27.  
  28. class MyAccountCallback(pj.AccountCallback):
  29.  
  30. def __init__(self, acc):
  31.  
  32. pj.AccountCallback.__init__(self, acc)
  33.  
  34.  
  35.  
  36. # Call Callback to receive events from Call
  37.  
  38. class SRCallCallback(pj.CallCallback):
  39.  
  40. def __init__(self, call=None):
  41.  
  42. pj.CallCallback.__init__(self, call)
  43.  
  44.  
  45.  
  46.  
  47.  
  48. def on_state(self):
  49.  
  50. print("Call is :", self.call.info().state_text),
  51.  
  52. print("last code :", self.call.info().last_code),
  53.  
  54. print("(" + self.call.info().last_reason + ")")
  55.  
  56.  
  57.  
  58. # Notification when call's media state is changed
  59.  
  60. def on_media_state(self):
  61.  
  62. global lib
  63.  
  64. if self.call.info().media_state == pj.MediaState.ACTIVE:
  65.  
  66. # Connect the call to sound device
  67.  
  68. call_slot = self.call.info().conf_slot
  69.  
  70. lib.conf_connect(call_slot, 0)
  71.  
  72. lib.conf_connect(0, call_slot)
  73.  
  74. print("Hey !!!!! Can you hear me !!!!!!!!!!")
  75.  
  76. print (lib)
  77.  
  78.  
  79. # Main loop
  80.  
  81. try:
  82.  
  83. # Start of the Main Class
  84.  
  85. # Create library instance of Lib class
  86.  
  87. lib = pj.Lib()
  88.  
  89.  
  90.  
  91. # Instantiate library with default config
  92.  
  93. lib.init(log_cfg = pj.LogConfig(level=3, callback=log_cb))
  94.  
  95.  
  96.  
  97. # Configuring one Transport Object and setting it to listen at 5060 port and UDP protocol
  98.  
  99. trans_conf = pj.TransportConfig()
  100.  
  101. print "____________________REGISTRATION PROCESS BEGINS_______________________"
  102. print "\n\n"
  103.  
  104. # 12345 is default port for SIP
  105. trans_conf.port = 12345
  106.  
  107. # Here the client address is same as the Servers Address
  108. a=raw_input("Please Enter the IP address of the Client: ")
  109. print "Not Using the default port number, Instead using: 12345"
  110.  
  111. trans_conf.bound_addr = a
  112.  
  113. transport = lib.create_transport(pj.TransportType.UDP,trans_conf)
  114.  
  115.  
  116.  
  117. # Starting the instance of Lib class
  118.  
  119. lib.start()
  120.  
  121. lib.set_null_snd_dev()
  122.  
  123.  
  124.  
  125. # Configuring Account class to register with Registrar server
  126.  
  127. # Giving information to create header of REGISTER SIP message
  128.  
  129.  
  130. # Hardcoded these values
  131. ab4="192.168.131.152" # Server's address
  132. ab='2020' # This clients User name
  133. ab1="password" # Password same as "password"
  134. ab2='y'
  135.  
  136. ab3=ab
  137.  
  138.  
  139. acc_conf = pj.AccountConfig(domain = ab4, username = ab, password =ab1, display = ab3)
  140.  
  141. # registrar = 'sip:'+ab4+':5060', proxy = 'sip:'+ab4+':5060')
  142.  
  143. acc_conf.id ="sip:"+ab
  144.  
  145. acc_conf.reg_uri ='sip:'+ab4+':12345'
  146.  
  147. acc_callback = MyAccountCallback(acc_conf)
  148.  
  149. acc = lib.create_account(acc_conf,cb=acc_callback)
  150.  
  151.  
  152.  
  153. # creating instance of AccountCallback class
  154.  
  155. acc.set_callback(acc_callback)
  156.  
  157.  
  158.  
  159. print('\n')
  160. print "Registration Complete-----------"
  161. print('Status= ',acc.info().reg_status, \
  162.  
  163. '(' + acc.info().reg_reason + ')')
  164.  
  165.  
  166.  
  167.  
  168.  
  169. # Starting Calling process.
  170. b=raw_input("Enter the username to be called: ")
  171. #sip and address are hard coded here
  172. b1="sip:"+ str(b)+"@192.168.0.1:5066"
  173.  
  174. call = acc.make_call(b1, SRCallCallback())
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181. print('Press <ENTER> to exit and destroy library')
  182.  
  183. input = sys.stdin.readline().rstrip('\r\n')
  184.  
  185.  
  186.  
  187. # Shutting down the library
  188. lib.destroy()
  189.  
  190. lib = None
  191.  
  192.  
  193.  
  194. except pj.Error, e:
  195.  
  196. print("Exception, error occured at : " + str(e))
  197.  
  198. lib.destroy()
  199.  
  200. lib = None
  201.  
  202. sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement