Changeset 916 for pjproject/trunk/pjsip-apps/src/py_pjsua/pjsua_app.py
- Timestamp:
- Jan 29, 2007 6:36:38 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjsip-apps/src/py_pjsua/pjsua_app.py
r851 r916 6 6 # Configurations 7 7 # 8 APP= "pjsua_app.py"8 THIS_FILE = "pjsua_app.py" 9 9 C_QUIT = 0 10 C_LOG_LEVEL = 3 11 10 C_LOG_LEVEL = 4 11 12 # STUN config. 13 # Set C_STUN_SRV to the address of the STUN server to enable STUN 14 # 15 C_STUN_SRV = "" 12 16 C_SIP_PORT = 5060 13 C_STUN_SRV = ""14 17 C_STUN_PORT = 3478 15 18 16 C_ACC_REGISTRAR = "" 17 #C_ACC_REGISTRAR = "sip:iptel.org" 18 C_ACC_ID = "sip:bulukucing1@iptel.org" 19 C_ACC_REALM = "iptel.org" 20 C_ACC_USERNAME = "bulukucing1" 21 C_ACC_PASSWORD = "netura" 22 23 # Display PJ error and exit 19 20 # Globals 21 # 22 g_acc_id = py_pjsua.PJSUA_INVALID_ID 23 g_current_call = py_pjsua.PJSUA_INVALID_ID 24 25 26 # Utility to get call info 27 # 28 def call_name(call_id): 29 ci = py_pjsua.call_get_info(call_id) 30 return "[Call " + `call_id` + " " + ci.remote_info + "]" 31 32 # Handler when invite state has changed. 33 # 34 def on_call_state(call_id, e): 35 global g_current_call 36 ci = py_pjsua.call_get_info(call_id) 37 write_log(3, call_name(call_id) + " state = " + `ci.state_text`) 38 if ci.state == 6: 39 g_current_call = py_pjsua.PJSUA_INVALID_ID 40 41 # Handler for incoming call 42 # 43 def on_incoming_call(acc_id, call_id, rdata): 44 global g_current_call 45 if g_current_call != py_pjsua.PJSUA_INVALID_ID: 46 py_pjsua.call_answer(call_id, 486, "", None) 47 return 48 g_current_call = call_id 49 ci = py_pjsua.call_get_info(call_id) 50 write_log(3, "Incoming call: " + call_name(call_id)) 51 py_pjsua.call_answer(call_id, 200, "", None) 52 53 54 # Handler when media state has changed (e.g. established or terminated) 55 # 56 def on_call_media_state(call_id): 57 ci = py_pjsua.call_get_info(call_id) 58 if ci.media_status == 1: 59 py_pjsua.conf_connect(ci.conf_slot, 0) 60 py_pjsua.conf_connect(0, ci.conf_slot) 61 write_log(3, call_name(call_id) + ": media is active") 62 else: 63 write_log(3, call_name(call_id) + ": media is inactive") 64 65 66 # Handler when account registration state has changed 67 # 68 def on_reg_state(acc_id): 69 acc_info = py_pjsua.acc_get_info(acc_id) 70 if acc_info.status != 0 and acc_info.status != 200: 71 write_log(3, "Account (un)registration failed: rc=" + `acc_info.status` + " " + acc_info.status_text) 72 else: 73 write_log(3, "Account successfully (un)registered") 74 75 76 # Utility: display PJ error and exit 77 # 24 78 def err_exit(title, rc): 25 py_pjsua.perror( APP, title, rc)79 py_pjsua.perror(THIS_FILE, title, rc) 26 80 exit(1) 27 81 28 # Logging callback 82 83 # Logging function (also callback, called by pjsua-lib) 84 # 29 85 def log_cb(level, str, len): 30 if level >= C_LOG_LEVEL:86 if level <= C_LOG_LEVEL: 31 87 print str, 32 88 33 # Initialize pjsua 89 def write_log(level, str): 90 log_cb(level, str + "\n", 0) 91 92 93 # 94 # Initialize pjsua. 95 # 34 96 def app_init(): 35 # Create pjsua before anything else 36 status = py_pjsua.create() 37 if status != 0: 38 err_exit("pjsua create() error", status) 39 40 # Create and initialize logging config 41 log_cfg = py_pjsua.logging_config_default() 42 log_cfg.level = C_LOG_LEVEL 43 log_cfg.cb = log_cb 44 45 # Create and initialize pjsua config 46 ua_cfg = py_pjsua.config_default() 47 ua_cfg.thread_cnt = 0 48 ua_cfg.user_agent = "PJSUA/Python 0.1" 49 50 # Create and initialize media config 51 med_cfg = py_pjsua.media_config_default() 52 med_cfg.ec_tail_len = 0 53 54 # 55 # Initialize pjsua!! 56 # 57 status = py_pjsua.init(ua_cfg, log_cfg, med_cfg) 58 if status != 0: 59 err_exit("pjsua init() error", status) 60 61 # Configure STUN config 62 stun_cfg = py_pjsua.stun_config_default() 63 stun_cfg.stun_srv1 = C_STUN_SRV 64 stun_cfg.stun_srv2 = C_STUN_SRV 65 stun_cfg.stun_port1 = C_STUN_PORT 66 stun_cfg.stun_port2 = C_STUN_PORT 67 68 # Configure UDP transport config 69 transport_cfg = py_pjsua.transport_config_default() 70 transport_cfg.port = C_SIP_PORT 71 transport_cfg.stun_config = stun_cfg 72 if C_STUN_SRV != "": 73 transport_cfg.use_stun = 1 74 75 # Create UDP transport 76 # Note: transport_id is supposed to be integer 77 status, transport_id = py_pjsua.transport_create(1, transport_cfg) 78 if status != 0: 79 py_pjsua.destroy() 80 err_exit("Error creating UDP transport", status) 81 82 83 # Configure account configuration 84 acc_cfg = py_pjsua.acc_config_default() 85 acc_cfg.id = C_ACC_ID 86 acc_cfg.reg_uri = C_ACC_REGISTRAR 87 acc_cfg.cred_count = 1 88 acc_cfg.cred_info[0].realm = C_ACC_REALM 89 acc_cfg.cred_info[0].scheme = "digest" 90 acc_cfg.cred_info[0].username = C_ACC_USERNAME 91 acc_cfg.cred_info[0].data_type = 0 92 acc_cfg.cred_info[0].data = C_ACC_PASSWORD 93 94 # Add new SIP account 95 # Note: acc_id is supposed to be integer 96 status, acc_id = py_pjsua.acc_add(acc_cfg, 1) 97 if status != 0: 98 py_pjsua.destroy() 99 err_exit("Error adding SIP account", status) 100 101 102 # Worker thread function 97 global g_acc_id 98 99 # Create pjsua before anything else 100 status = py_pjsua.create() 101 if status != 0: 102 err_exit("pjsua create() error", status) 103 104 # Create and initialize logging config 105 log_cfg = py_pjsua.logging_config_default() 106 log_cfg.level = C_LOG_LEVEL 107 log_cfg.cb = log_cb 108 109 # Create and initialize pjsua config 110 # Note: for this Python module, thread_cnt must be 0 since Python 111 # doesn't like to be called from alien thread (pjsua's thread 112 # in this case) 113 ua_cfg = py_pjsua.config_default() 114 ua_cfg.thread_cnt = 0 115 ua_cfg.user_agent = "PJSUA/Python 0.1" 116 ua_cfg.cb.on_incoming_call = on_incoming_call 117 ua_cfg.cb.on_call_media_state = on_call_media_state 118 ua_cfg.cb.on_reg_state = on_reg_state 119 ua_cfg.cb.on_call_state = on_call_state 120 121 # Create and initialize media config 122 med_cfg = py_pjsua.media_config_default() 123 med_cfg.ec_tail_len = 0 124 125 # 126 # Initialize pjsua!! 127 # 128 status = py_pjsua.init(ua_cfg, log_cfg, med_cfg) 129 if status != 0: 130 err_exit("pjsua init() error", status) 131 132 # Configure STUN config 133 stun_cfg = py_pjsua.stun_config_default() 134 stun_cfg.stun_srv1 = C_STUN_SRV 135 stun_cfg.stun_srv2 = C_STUN_SRV 136 stun_cfg.stun_port1 = C_STUN_PORT 137 stun_cfg.stun_port2 = C_STUN_PORT 138 139 # Configure UDP transport config 140 transport_cfg = py_pjsua.transport_config_default() 141 transport_cfg.port = C_SIP_PORT 142 transport_cfg.stun_config = stun_cfg 143 if C_STUN_SRV != "": 144 transport_cfg.use_stun = 1 145 146 # Create UDP transport 147 status, transport_id = py_pjsua.transport_create(1, transport_cfg) 148 if status != 0: 149 py_pjsua.destroy() 150 err_exit("Error creating UDP transport", status) 151 152 # Create initial default account 153 status, acc_id = py_pjsua.acc_add_local(transport_id, 1) 154 if status != 0: 155 py_pjsua.destroy() 156 err_exit("Error creating account", status) 157 158 g_acc_id = acc_id 159 160 # Add SIP account interractively 161 # 162 def add_account(): 163 global g_acc_id 164 165 acc_domain = "" 166 acc_username = "" 167 acc_passwd ="" 168 confirm = "" 169 170 # Input account configs 171 print "Your SIP domain (e.g. myprovider.com): ", 172 acc_domain = sys.stdin.readline() 173 if acc_domain == "\n": 174 return 175 acc_domain = acc_domain.replace("\n", "") 176 177 print "Your username (e.g. alice): ", 178 acc_username = sys.stdin.readline() 179 if acc_username == "\n": 180 return 181 acc_username = acc_username.replace("\n", "") 182 183 print "Your password (e.g. secret): ", 184 acc_passwd = sys.stdin.readline() 185 if acc_passwd == "\n": 186 return 187 acc_passwd = acc_passwd.replace("\n", "") 188 189 # Configure account configuration 190 acc_cfg = py_pjsua.acc_config_default() 191 acc_cfg.id = "sip:" + acc_username + "@" + acc_domain 192 acc_cfg.reg_uri = "sip:" + acc_domain 193 acc_cfg.cred_count = 1 194 acc_cfg.cred_info[0].realm = acc_domain 195 acc_cfg.cred_info[0].scheme = "digest" 196 acc_cfg.cred_info[0].username = acc_username 197 acc_cfg.cred_info[0].data_type = 0 198 acc_cfg.cred_info[0].data = acc_passwd 199 200 # Add new SIP account 201 status, acc_id = py_pjsua.acc_add(acc_cfg, 1) 202 if status != 0: 203 py_pjsua.perror(THIS_FILE, "Error adding SIP account", status) 204 else: 205 g_acc_id = acc_id 206 write_log(3, "Account " + acc_cfg.id + " added") 207 208 209 # 210 # Worker thread function. 211 # Python doesn't like it when it's called from an alien thread 212 # (pjsua's worker thread, in this case), so for Python we must 213 # disable worker thread in pjsua and poll pjsua from Python instead. 214 # 103 215 def worker_thread_main(arg): 104 thread_desc = 0; 105 status = py_pjsua.thread_register("worker thread", thread_desc) 106 if status != 0: 107 py_pjsua.perror(APP, "Error registering thread", status) 108 else: 109 while C_QUIT == 0: 110 py_pjsua.handle_events(50) 216 global C_QUIT 217 thread_desc = 0; 218 status = py_pjsua.thread_register("python worker", thread_desc) 219 if status != 0: 220 py_pjsua.perror(THIS_FILE, "Error registering thread", status) 221 else: 222 while C_QUIT == 0: 223 py_pjsua.handle_events(50) 224 print "Worker thread quitting.." 225 C_QUIT = 2 226 111 227 112 228 # Start pjsua 229 # 113 230 def app_start(): 114 115 116 117 118 119 120 121 122 231 # Done with initialization, start pjsua!! 232 # 233 status = py_pjsua.start() 234 if status != 0: 235 py_pjsua.destroy() 236 err_exit("Error starting pjsua!", status) 237 238 # Start worker thread 239 thr = thread.start_new(worker_thread_main, (0,)) 123 240 124 241 print "PJSUA Started!!" 125 242 126 243 127 244 # Print application menu 245 # 128 246 def print_menu(): 129 print "Menu:" 130 print " q Quit application" 131 print " s Add buddy" 132 print "Choice: ", 133 134 247 print """ 248 Menu: 249 q Quit application 250 +a Add account 251 +b Add buddy 252 m Make call 253 h Hangup current call (if any) 254 i Send instant message 255 """ 256 print "Choice: ", 257 135 258 # Menu 259 # 136 260 def app_menu(): 137 quit = 0 138 while quit == 0: 139 print_menu() 140 choice = sys.stdin.readline() 141 if choice[0] == "q": 142 quit = 1 143 elif choice[0] == "s": 144 bc = py_pjsua.Buddy_Config() 145 print "Buddy URI: ", 146 bc.uri = sys.stdin.readline() 147 if bc.uri == "": 148 continue 261 global g_acc_id 262 global g_current_call 263 264 quit = 0 265 while quit == 0: 266 print_menu() 267 choice = sys.stdin.readline() 268 269 if choice[0] == "q": 270 quit = 1 271 272 elif choice[0] == "i": 273 # Sending IM 274 print "Send IM to SIP URL: ", 275 url = sys.stdin.readline() 276 if url == "\n": 277 continue 278 279 # Send typing indication 280 py_pjsua.im_typing(g_acc_id, url, 1, None) 281 282 print "The content: ", 283 message = sys.stdin.readline() 284 if message == "\n": 285 py_pjsua.im_typing(g_acc_id, url, 0, None) 286 continue 287 288 # Send the IM! 289 py_pjsua.im_send(g_acc_id, url, "", message, None, 0) 290 291 elif choice[0] == "m": 292 # Make call 293 print "Using account ", g_acc_id 294 print "Make call to SIP URL: ", 295 url = sys.stdin.readline() 296 url = url.replace("\n", "") 297 if url == "": 298 continue 299 300 # Initiate the call! 301 status, call_id = py_pjsua.call_make_call(g_acc_id, url, 0, 0, None) 149 302 150 bc.subscribe = 1 151 status = py_pjsua.buddy_add(bc) 152 if status != 0: 153 py_pjsua.perror(APP, "Error adding buddy", status) 303 if status != 0: 304 py_pjsua.perror(THIS_FILE, "Error making call", status) 305 else: 306 g_current_call = call_id 307 308 elif choice[0] == "+" and choice[1] == "b": 309 # Add new buddy 310 bc = py_pjsua.Buddy_Config() 311 print "Buddy URL: ", 312 bc.uri = sys.stdin.readline() 313 if bc.uri == "\n": 314 continue 315 316 bc.subscribe = 1 317 status, buddy_id = py_pjsua.buddy_add(bc) 318 if status != 0: 319 py_pjsua.perror(THIS_FILE, "Error adding buddy", status) 320 321 elif choice[0] == "+" and choice[1] == "a": 322 # Add account 323 add_account() 324 325 elif choice[0] == "h": 326 if g_current_call != py_pjsua.PJSUA_INVALID_ID: 327 py_pjsua.call_hangup(g_current_call, 603, "", None) 328 else: 329 print "No current call" 154 330 155 331 … … 166 342 print "PJSUA shutting down.." 167 343 C_QUIT = 1 344 # Give the worker thread chance to quit itself 345 while C_QUIT != 2: 346 py_pjsua.handle_events(50) 347 348 print "PJSUA destroying.." 168 349 py_pjsua.destroy() 169 350
Note: See TracChangeset
for help on using the changeset viewer.