我们正在开发一个移动应用程序,其中我们在python3上有一个服务器,并通过该服务器容纳流
但是当安卓客户端连接到服务器时,语音会延迟5秒左右,而且会变得太嘈杂,并包含啁啾声,我们在服务器端和安卓端都有我们最好的,需要有人引导
以下是示例代码:
导入套接字 导入时间 从线程导入线程 从multiprocessing.dummy导入池作为线程池
class VoiP(Thread):
def __init__(self):
Thread.__init__(self)
def server_start(self, TCP_IP = '0.0.0.0', TCP_PORT = 2004 ,BUFFER_SIZE = 1024, Connections = 4):
self.Server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.Server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.Server.bind((TCP_IP, TCP_PORT))
self.Server.listen(Connections)
print("Server is starting.")
def client_connection(self):
try:
print("Waiting for Room1 Client A \n")
(self.Room1a, address) = self.Server.accept()
print (type(self.Room1a))
print("Room1 Client A connected \n")
print("Waiting for Room1 Client B \n")
(self.Room1b, address) = self.Server.accept()
print("Room1 Client B connected \n")
print("Waiting for Room2 Client A \n")
(self.Room2a, address) = self.Server.accept()
print("Room2 Client A connected \n")
print("Waiting for Room2 Client B \n")
(self.Room2b, address) = self.Server.accept()
print("Room2 Client B connected \n")
print("Print All clients connected")
except Exception as e:
print("Error While Connecting to client")
def byte_logic(self, byte, data):
if byte is not b'':
data = byte + data
byte = b''
if len(data) % 2 is not 0:
byte = (data[-1])
data = data[0:-1]
return np.frombuffer(data, np.int16), byte
这是添加客户端语音的加法器 def阵列加法器(自、a、b):
if len(a) < len(b):
c = b.copy()
c[:len(a)] += a
else:
c = a.copy()
c[:len(b)] += b
return bytes(c)
def room1a_transcev(self):
while True:
try:
self.data1a = self.Room1a.recv(2048)
self.np_r1a, self.extra_byte_1 = self.byte_logic(self.extra_byte_1, self.data1a)
self.room1_stream = self.array_adder(self.np_r1a, self.np_r1b)
self.Room2a.sendall(self.room1_stream)
self.Room2b.sendall(self.room1_stream)
except Exception as e:
print(e, "room1a_transcev error")
def room1b_rec(self):
while True:
try:
self.data1b = self.Room1b.recv(2048)
self.np_r1b, self.extra_byte_2 = self.byte_logic(self.extra_byte_2, self.data1b)
except Exception as e:
print(e, "room1b_transcev error")
def room2a_transcev(self):
while True:
try:
self.data2a = self.Room2a.recv(2048)
self.np_r2a, self.extra_byte_3 = self.byte_logic(self.extra_byte_3, self.data2a)
self.room2_stream = self.array_adder(self.np_r2a, self.np_r2b)
self.Room1a.sendall(self.room2_stream)
self.Room1b.sendall(self.room2_stream)
except Exception as e:
print(e, "room2a_transcev error")
def room2b_rec(self):
while True:
try:
self.data2b = self.Room2b.recv(2048)
self.np_r2b, self.extra_byte_4 = self.byte_logic(self.extra_byte_4, self.data2b)
except Exception as e:
print(e, "room2b_rec error")
if __name__ == "__main__":
objVOIP = VoiP()
objVOIP.server_start()
objVOIP.client_connection()
# asyncio.run(objVOIP.main())
thread1 = Thread(target=objVOIP.room1a_transcev)
thread2 = Thread(target=objVOIP.room1b_rec)
thread3 = Thread(target=objVOIP.room2a_transcev)
thread4 = Thread(target=objVOIP.room2b_rec)
thread1.setDaemon = True
thread2.setDaemon = True
thread3.setDaemon = True
thread4.setDaemon = True
thread1.start()
thread2.start()
thread3.start()
thread4.start()
我需要的理想情况下,有最小的延迟和噪音。两种声音的混合
嗯,这是一个复杂的问题,我不能给出一个明确的答案,但我可以给出一些提示
首先,这称为抖动,通常发生在数据包未按顺序接收或其中一些数据包比其他数据包花费更多时间时。因此:
相关问题 更多 >
编程相关推荐