在用python编写的TCP服务器中,音频流最终会产生额外的杂音和杂音

在用python编写的TCP服务器中,音频流最终会产生额外的杂音和杂音,python,Python,我们正在开发一个移动应用程序,其中我们在python3上有一个服务器,并通过该服务器容纳流 但是当安卓客户端连接到服务器时,语音会延迟5秒左右,而且会变得太嘈杂,并包含啁啾声,我们在服务器端和安卓端都有我们最好的,需要有人引导 以下是示例代码: 导入套接字 导入时间 从线程导入线程 从multiprocessing.dummy导入池作为线程池 class VoiP(Thread): def __init__(self): Thread.__init__(self)

我们正在开发一个移动应用程序,其中我们在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):

如果len(a)

我需要的理想情况下,有最小的延迟和噪音。以及两种声音的混合。

嗯,这是一个复杂的问题,我不能给出一个明确的答案,但我可以给出一些提示

首先,这称为抖动,通常发生在数据包未按顺序接收或其中一些数据包比其他数据包花费更多时间时。因此:

  • 首先,通过仅使用本地连接或在您知道没有流量的受控网络中执行测试来丢弃网络问题。我想你已经这样做了
  • 第二,考虑使用UDP代替TCP。是的,如果网络繁忙,你必须控制自己的顺序,UDP可能会丢失一些数据包(它们可以以不同的发送顺序接收,必要时你必须输入订单号并重新排序),但它比TCP快,在通话中,丢失一两毫秒的音频其实并不重要
  • 第三:对于音频处理,请确保正确使用库的C绑定函数。这可以在性能上产生巨大的差异

    • 嗯,这是一个复杂的问题,我不能给出一个明确的答案,但我可以给出一些提示

      首先,这称为抖动,通常发生在数据包未按顺序接收或其中一些数据包比其他数据包花费更多时间时。因此:

      • 首先,通过仅使用本地连接或在您知道没有流量的受控网络中执行测试来丢弃网络问题。我想你已经这样做了
      • 第二,考虑使用UDP代替TCP。是的,如果网络繁忙,你必须控制自己的顺序,UDP可能会丢失一些数据包(它们可以以不同的发送顺序接收,必要时你必须输入订单号并重新排序),但它比TCP快,在通话中,丢失一两毫秒的音频其实并不重要
      • 第三:对于音频处理,请确保正确使用库的C绑定函数。这可以在性能上产生巨大的差异

      事实上,我们在本地工作,我们希望使用TCP/IP,因为我们需要可靠的数据包传递方法,UDP用于发送消息。事实上,我们在本地工作,我们希望使用TCP/IP,因为我们需要可靠的数据包传递方法,UDP用于发送消息。
              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()