⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pjsua_app.py

📁 一个开源SIP协议栈
💻 PY
📖 第 1 页 / 共 2 页
字号:

def dump_call_quality():
	global g_current_call
	
	buf = ""
	if g_current_call != -1:
		buf = py_pjsua.call_dump(g_current_call, 1, 1024, "  ")
		write_log(3, "\n" + buf)
	else:
		write_log(3, "No current call")

def xfer_call():
	global g_current_call
	
	if g_current_call == -1:
		
		write_log(3, "No current call")

	else:
		call = g_current_call		
		ci = py_pjsua.call_get_info(g_current_call)
		print "Transfering current call ["+ `g_current_call` + "] " + ci.remote_info
		print "Enter sip url : "
		url = sys.stdin.readline()
		if url == "\n": 
			return
		url = url.replace("\n", "")
		if call != g_current_call:
			print "Call has been disconnected"
			return
		msg_data = py_pjsua.msg_data_init()
		status = py_pjsua.call_xfer(g_current_call, url, msg_data);
		if status != 0:
			py_pjsua.perror(THIS_FILE, "Error transfering call ", status)
		else:		
			write_log(3, "Call transfered to " + url)
		
def xfer_call_replaces():
	if g_current_call == -1:
		write_log(3, "No current call")
	else:
		call = g_current_call
		
		ids = py_pjsua.enum_calls()
		if len(ids) <= 1:
			print "There are no other calls"
			return		

		ci = py_pjsua.call_get_info(g_current_call)
		print "Transfer call [" + `g_current_call` + "] " + ci.remote_info + " to one of the following:"
		for i in range(0, len(ids)):		    
			if ids[i] == call:
				continue
			call_info = py_pjsua.call_get_info(ids[i])
			print `ids[i]` + "  " +  call_info.remote_info + "  [" + call_info.state_text + "]"		

		print "Enter call number to be replaced : "
		buf = sys.stdin.readline()
		buf = buf.replace("\n","")
		if buf == "":
			return
		dst_call = int(buf)
		
		if call != g_current_call:
			print "Call has been disconnected"
			return		
		
		if dst_call == call:
			print "Destination call number must not be the same as the call being transfered"
			return
		
		if dst_call >= py_pjsua.PJSUA_MAX_CALLS:
			print "Invalid destination call number"
			return
	
		if py_pjsua.call_is_active(dst_call) == 0:
			print "Invalid destination call number"
			return		

		py_pjsua.call_xfer_replaces(call, dst_call, 0, None)
		
#
# Worker thread function.
# Python doesn't like it when it's called from an alien thread
# (pjsua's worker thread, in this case), so for Python we must
# disable worker thread in pjsua and poll pjsua from Python instead.
#
def worker_thread_main(arg):
	global C_QUIT
	thread_desc = 0;
	status = py_pjsua.thread_register("python worker", thread_desc)
	if status != 0:
		py_pjsua.perror(THIS_FILE, "Error registering thread", status)
	else:
		while C_QUIT == 0:
			py_pjsua.handle_events(50)
		print "Worker thread quitting.."
		C_QUIT = 2


# Start pjsua
#
def app_start():
	# Done with initialization, start pjsua!!
	#
	status = py_pjsua.start()
	if status != 0:
		py_pjsua.destroy()
		err_exit("Error starting pjsua!", status)

	# Start worker thread
	thr = thread.start_new(worker_thread_main, (0,))
    
	print "PJSUA Started!!"


# Print account and buddy list
def print_acc_buddy_list():
	global g_acc_id
	
	acc_ids = py_pjsua.enum_accs()
	print "Account list:"
	for acc_id in acc_ids:
		acc_info = py_pjsua.acc_get_info(acc_id)
		if acc_info.has_registration == 0:
			acc_status = acc_info.status_text
		else:
			acc_status = `acc_info.status` + "/" + acc_info.status_text + " (expires=" + `acc_info.expires` + ")"

		if acc_id == g_acc_id:
			print " *",
		else:
			print "  ",

		print "[" + `acc_id` + "] " + acc_info.acc_uri + ": " + acc_status
		print "       Presence status: ",
		if acc_info.online_status != 0:
			print "Online"
		else:
			print "Invisible"

	if py_pjsua.get_buddy_count() > 0:
		print ""
		print "Buddy list:"
		buddy_ids = py_pjsua.enum_buddies()
		for buddy_id in buddy_ids:
			bi = py_pjsua.buddy_get_info(buddy_id)
			print "   [" + `buddy_id` + "] " + bi.status_text + " " + bi.uri
	
		
# Print application menu
#
def print_menu():
	print ""
	print ">>>"
	print_acc_buddy_list()
	print """
+============================================================================+
|         Call Commands :      |  Buddy, IM & Presence:   |    Account:      |
|                              |                          |                  |
|  m  Make call                | +b  Add buddy            | +a  Add account  |
|  a  Answer current call      | -b  Delete buddy         | -a  Delete accnt |
|  h  Hangup current call      |                          |                  |
|  H  Hold call                |  i  Send instant message | rr  register     |
|  v  re-inVite (release Hold) |  s  Subscribe presence   | ru  Unregister   |
|  #  Send DTMF string         |  u  Unsubscribe presence |                  |
| dq  Dump curr. call quality  |  t  ToGgle Online status |                  |
|                              +--------------------------+------------------+
|  x  Xfer call                |     Media Commands:      |    Status:       |
|  X  Xfer with Replaces       |                          |                  |
|                              | cl  List ports           |  d  Dump status  |
|                              | cc  Connect port         | dd  Dump detail  |
|                              | cd  Disconnect port      |                  |
|                              | +p  Add file player      |                  |
|------------------------------+ +r  Add file recorder    |                  |
|  q  Quit application         |                          |                  |
+============================================================================+"""
	print "You have " + `py_pjsua.call_get_count()` + " active call(s)"
	print ">>>", 

# Menu
#
def app_menu():
	global g_acc_id
	global g_current_call

	quit = 0
	while quit == 0:
		print_menu()
		choice = sys.stdin.readline()

		if choice[0] == "q":
			quit = 1

		elif choice[0] == "i":
			# Sending IM	
			print "Send IM to SIP URL: ",
			url = sys.stdin.readline()
			if url == "\n":
				continue

			# Send typing indication
			py_pjsua.im_typing(g_acc_id, url, 1, None) 

			print "The content: ",
			message = sys.stdin.readline()
			if message == "\n":
				py_pjsua.im_typing(g_acc_id, url, 0, None) 		
				continue

			# Send the IM!
			py_pjsua.im_send(g_acc_id, url, None, message, None, 0)

		elif choice[0] == "m":
			# Make call 
			print "Using account ", g_acc_id
			print "Make call to SIP URL: ",
			url = sys.stdin.readline()
			url = url.replace("\n", "")
			if url == "":
				continue

			# Initiate the call!
			status, call_id = py_pjsua.call_make_call(g_acc_id, url, 0, 0, None)
            
			if status != 0:
				py_pjsua.perror(THIS_FILE, "Error making call", status)
			else:
				g_current_call = call_id

		elif choice[0] == "+" and choice[1] == "b":
			# Add new buddy
			bc = py_pjsua.Buddy_Config()
			print "Buddy URL: ",
			bc.uri = sys.stdin.readline()
			if bc.uri == "\n":
				continue
            
			bc.uri = bc.uri.replace("\n", "")
			bc.subscribe = 1
			status, buddy_id = py_pjsua.buddy_add(bc)
			if status != 0:
				py_pjsua.perror(THIS_FILE, "Error adding buddy", status)
		elif choice[0] == "-" and choice[1] == "b":
			print "Enter buddy ID to delete : "
			buf = sys.stdin.readline()
			buf = buf.replace("\n","")
			if buf == "":
				continue
			i = int(buf)
			if py_pjsua.buddy_is_valid(i) == 0:
				print "Invalid buddy id " + `i`
			else:
				py_pjsua.buddy_del(i)
				print "Buddy " + `i` + " deleted"		
		elif choice[0] == "+" and choice[1] == "a":
			# Add account
			add_account()
		elif choice[0] == "-" and choice[1] == "a":
			print "Enter account ID to delete : "
			buf = sys.stdin.readline()
			buf = buf.replace("\n","")
			if buf == "":
				continue
			i = int(buf)

			if py_pjsua.acc_is_valid(i) == 0:
				print "Invalid account id " + `i`
			else:
				py_pjsua.acc_del(i)
				print "Account " + `i` + " deleted"
	    
		elif choice[0] == "+" and choice[1] == "p":
			add_player()
		elif choice[0] == "+" and choice[1] == "r":
			add_recorder()
		elif choice[0] == "c" and choice[1] == "l":
			conf_list()
		elif choice[0] == "c" and choice[1] == "c":
			connect_port()
		elif choice[0] == "c" and choice[1] == "d":
			disconnect_port()
		elif choice[0] == "d" and choice[1] == "q":
			dump_call_quality()
		elif choice[0] == "x":
			xfer_call()
		elif choice[0] == "X":
			xfer_call_replaces()
		elif choice[0] == "h":
			if g_current_call != py_pjsua.PJSUA_INVALID_ID:
				py_pjsua.call_hangup(g_current_call, 603, None, None)
			else:
				print "No current call"
		elif choice[0] == "H":
			if g_current_call != py_pjsua.PJSUA_INVALID_ID:
				py_pjsua.call_set_hold(g_current_call, None)
		
			else:
				print "No current call"
		elif choice[0] == "v":
			if g_current_call != py_pjsua.PJSUA_INVALID_ID:
		
				py_pjsua.call_reinvite(g_current_call, 1, None);

			else:
				print "No current call"
		elif choice[0] == "#":
			if g_current_call == py_pjsua.PJSUA_INVALID_ID:		
				print "No current call"
			elif py_pjsua.call_has_media(g_current_call) == 0:
				print "Media is not established yet!"
			else:
				call = g_current_call
				print "DTMF strings to send (0-9*#A-B)"
				buf = sys.stdin.readline()
				buf = buf.replace("\n", "")
				if buf == "":
					continue
				if call != g_current_call:
					print "Call has been disconnected"
					continue
				status = py_pjsua.call_dial_dtmf(g_current_call, buf)
				if status != 0:
					py_pjsua.perror(THIS_FILE, "Unable to send DTMF", status);
				else:
					print "DTMF digits enqueued for transmission"
		elif choice[0] == "s":
			print "Subscribe presence of (buddy id) : "
			buf = sys.stdin.readline()
			buf = buf.replace("\n","")
			if buf == "":
				continue
			i = int(buf)
			py_pjsua.buddy_subscribe_pres(i, 1)
		elif choice[0] == "u":
			print "Unsubscribe presence of (buddy id) : "
			buf = sys.stdin.readline()
			buf = buf.replace("\n","")
			if buf == "":
				continue
			i = int(buf)
			py_pjsua.buddy_subscribe_pres(i, 0)
		elif choice[0] == "t":
			acc_info = py_pjsua.acc_get_info(g_acc_id)
			if acc_info.online_status == 0:
				acc_info.online_status = 1
			else:
				acc_info.online_status = 0
			py_pjsua.acc_set_online_status(g_acc_id, acc_info.online_status)
			st = ""
			if acc_info.online_status == 0:
				st = "offline"
			else:
				st = "online"
			print "Setting " + acc_info.acc_uri + " online status to " + st
		elif choice[0] == "r":
			if choice[1] == "r":	    
				py_pjsua.acc_set_registration(g_acc_id, 1)
			elif choice[1] == "u":
				py_pjsua.acc_set_registration(g_acc_id, 0)
		elif choice[0] == "d":
			py_pjsua.dump(choice[1] == "d")
		elif choice[0] == "a":
			if g_current_call != py_pjsua.PJSUA_INVALID_ID:				
				
				py_pjsua.call_answer(g_current_call, 200, None, None)
			else:
				print "No current call"


#
# main
#
app_init()
app_start()
app_menu()

#
# Done, quitting..
#
print "PJSUA shutting down.."
C_QUIT = 1
# Give the worker thread chance to quit itself
while C_QUIT != 2:
    py_pjsua.handle_events(50)

print "PJSUA destroying.."
py_pjsua.destroy()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -