简体   繁体   English

如何使用libwebsocket将16bit音频数据发送到服务器

[英]how to send 16bit audio data to server using libwebsocket

I want to send the audio data to server using libwebsocket. 我想使用libwebsocket将音频数据发送到服务器。

audio data is 16bits, 16Khz format. 音频数据为16位,16Khz格式。

But ws_service_callback on the server side is like below: 但是服务器端的ws_service_callback如下所示:

static int ws_service_callback(
                         struct lws *wsi,
                         enum lws_callback_reasons reason, void *user,
                         *void *in*, size_t len)

Here ws_service_callback is the callback function of server when server receives something from client side. 这里ws_service_callback是服务器从客户端接收到某些消息时服务器的回调函数。

*void *in* is the data from client side, and it is 8 bit format. *void *in*是来自客户端的数据,它是8位格式。 (here 8 bit and 16 bit mitmatch) (此处为8位和16位mitmatch)

Should the client side split the audio data to two 8 bits, then send it to the server side?? 客户端是否应该将音频数据拆分为两个8位,然后将其发送到服务器端?

Then the server side needs to sum the two 8bit to one 16bit? 那么服务器端需要将两个8bit加到一个16bit?

Usually the 16b audio data are stored in memory as continuous block of bits (and also bytes then), so whether you treat them as 16b or 8b doesn't matter during transmission of the whole block (or save/load to disc, etc). 通常,16b音频数据以连续的位块(然后还有字节)的形式存储在内存中,因此在整个块传输(或保存/加载到磁盘等)期间,将它们视为16b还是8b都没有关系。 。 Ie 1234 16b values can be treated as 2468 8b values in this context the network layer doesn't need to care. 即,在这种情况下,网络层无需关心,可以将1234 16b值视为2468 8b值。

But then processing the data on the server side, when it will treat the two consecutive 8 bit values as single 16b value, needs to know how the original data should be treated, because there are two common ways (and infinite amount of uncommon/custom including things like compressed data or specifically designed order of bits, delta encoding, etc... none of that supported by the CPU on single machine instruction level, ie these need implementation in the code), little-endian vs big-endian (one of these is for sure supported in "native" way by the CPU, some CPUs even support both and can be switched at boot time). 但是随后在服务器端处理数据时,当它将两个连续的8位值视为单个16b值时,需要知道应如何处理原始数据,因为有两种常见的方式(以及无限数量的不常见/自定义包括诸如压缩数据或专门设计的位顺序,增量编码等之类的东西……在单机指令级别上,CPU均不支持这些东西,即这些都需要在代码中实现),小端与大端(一个)当然,其中的某些功能确实受到CPU的“本机”支持,某些CPU甚至支持两者,并且可以在引导时进行切换。

For example Intel CPUs are "little-endian", so when you write 16b value 0x1234 into memory in the "native" way, it will occupy two bytes in this order: 0x34, 0x12 (lower 8 bits are stored first, then higher 8 bits), or 32b value 0x12345678 is stored as four bytes 78 56 34 12 . 例如,Intel CPU是“ little-endian”,因此当您以“本机”方式将16b值0x1234写入内存时,它将按以下顺序占用两个字节:0x34、0x12(低8位先存储,然后高8位位)或32b值0x12345678存储为四个字节78 56 34 12

On big-endian platforms the order is opposite, so high-order bytes are stored first, and 32b value 0x12345678 is stored (in CPU "native" way) as four bytes 12 34 56 78 . 在big-endian平台上,顺序是相反的,因此首先存储高位字节,然后将32b值0x12345678(以CPU“本机”方式)存储为四个字节12 34 56 78

As you can see, two platforms talking through network layer, must be aware of the endian-ness of each, and convert such data to interpret them correctly. 如您所见,通过网络层进行通信的两个平台必须了解每个平台的字节序,并转换此类数据以正确解释它们。

As you are basically designing binary network protocol for this service, you should define what is the expected order of the bits. 在基本为该服务设计二进制网络协议时,应定义所需的位顺序。 If both client and server are expected to be of particular platform, for best performance a simplest implementation you can define the protocol to follow the native way of those plaftorms. 如果期望客户端和服务器都属于特定平台,则为获得最佳性能和最简单的实现,可以定义协议以遵循这些原始方法。 Then if you will implement this protocol on platform which has different native way, you will have to add conversion of the data before sending it to server. 然后,如果您要在具有不同本机方式的平台上实施此协议,则必须先添加数据转换,然后再将其发送到服务器。 (or if you expect the clients to be low-power devices and server to have abundance of power, you can define protocol which supports both common ways by having some flag, and do the conversion on server side for the "other" one). (或者,如果您希望客户端是低功耗设备,而服务器具有丰富的功能,则可以通过设置一些标志来定义支持两种常用方法的协议,并在服务器端为“其他”标志进行转换)。

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

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