简体   繁体   English

音频数据的TCP套接字通信 - 多服务器到多个客户端

[英]TCP Socket Communication for Audio Data - Multiple Server to Multiple Client

I am new to socket programming and don't have much idea on how it works, Here is the use case, Im developing an iPhone app, where users stream real time audio originated from another iPhone device (in short Multi casting) 我是套接字编程的新手,并且对它的工作方式不太了解。这是用例,我正在开发一个iPhone应用程序,用户可以流式传输来自其他iPhone设备的实时音频(简而言之,多播)

What I have done so far: 到目前为止我做了什么:

I opened a port on server which keep listening to incoming data from clients. 我在服务器上打开了一个端口,它继续监听来自客户端的传入数据。 On the iOS side, i implemented methods thats read the packets received on the server and process it accordingly (i have used GCDAsyncSocket) 在iOS方面,我实现了读取服务器上收到的数据包并相应处理它的方法(我使用过GCDAsyncSocket)

Problem where I need help: 我需要帮助的问题:

The above use case works perfect for 2 users, one that sends the audio data to the server and the other one reads that data to play audio. 上述用例适用于2个用户,一个用于将音频数据发送到服务器,另一个用于读取该数据以播放音频。 But actually there would not always be a single user originating audio data, they could be more than 100+, Now when all of them are sending different audio data to server how could i filter data for the listeners that everyone receive only there data, I overcome this problem adding a token on every packet like 但实际上并不总是有一个用户发起音频数据,它们可能超过100+,现在当它们都向服务器发送不同的音频数据时,我怎样才能为每个人只收到数据的听众过滤数据,克服了这个问题,在每个数据包上添加一个令牌

unique_token:<ffdefa09 fedead3...... //Audio Data

But this process is way too slow as every client is listening all the packets and only process the ones with the token they are assigned. 但是这个过程太慢了,因为每个客户端都在监听所有数据包,只处理具有分配的令牌的数据包。

Is there anyway, we can make a peer to peer connection by which the originating device become server and only sends data to its listeners and don't bother anyone else? 无论如何,我们可以建立一个对等连接,通过该连接,始发设备成为服务器并且只向其监听器发送数据而不打扰其他任何人?

Can't you add something like a light protocol before you start steaming audio data to server ? 在开始将音频数据传输到服务器之前,是否无法添加类似光协议的内容?

iPhone -> server [Request: Start Stream Audio]
server -> iPhone [OK: TCP Port:4444]
// iphone sending audio packets to port 4444


iPhone2 -> server [Request: Start Stream Audio]
server -> iPhone2 [OK: TCP Port:4445]

then the server can filter all audio channels with TCP port ID instead of packet ID (or maybe I misunderstood your issue) 然后服务器可以使用TCP端口ID而不是数据包ID过滤所有音频通道(或者我可能误解了您的问题)

Btw I don't think you can do any "real" P2P with iPhone on cellular networks because of providers firewalls 顺便说一句,由于提供商的防火墙,我不认为你可以在蜂窝网络上用iPhone做任何“真正的”P2P

for every end who send the audio data, you create a socket and recv audio data, and for every end who receive the audio data, you create a socket and send audio data. 对于发送音频数据的每一端,您创建一个套接字和recv音频数据,并为接收音频数据的每一端创建一个套接字并发送音频数据。 P2P is lots of work , because many device are behind the public address. P2P是很多工作,因为许多设备都在公共广播的后面。

You need to separate your command data from your streaming/audio data. 您需要将命令数据与流/音频数据分开。

First you need the iphones to tell the server what they want, 首先你需要iphone告诉服务器他们想要什么,

  • iphone 1: "i want to stream this data with id 1" iphone 1:“我想用id 1传输这些数据”
  • iphone 2: "i want to listen to stream with id 1" iphone 2:“我想听id为1的流”

that way the server can tell the iphone where to connect for getting the data it needs, as HaneTV suggested with port numbers 通过这种方式,服务器可以告诉iphone在哪里连接以获取所需的数据,正如HaneTV建议的端口号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM