从麦克风传输html5中的实时语音的最佳方法是什么?

问题描述 投票:-1回答:1

我想用html5编写实时语音通话应用程序。我已经在服务器端用C#编写了一个websocket服务器...现在,我可以从HTML5中的麦克风获取实时缓冲区。但当我要将此活动缓冲区传输到服务器,然后从服务器推送到客户端2时,它的似乎编码和解码字节数组的性能很差,并且不会出现实时声音...

请写下您对此的想法...谢谢。

html websocket voice
1个回答
0
投票

这是我的代码,用于在c#websocket服务器中为推送客户端编码字节数组:

public byte[] EncodeMessageToSend(byte[] arr,
                                  bool bool_is_data) {
    byte[] response = null;
    byte[] bytesRaw = arr
    byte[] frame = new byte[10];

    int indexStartRawData = -1;
    int length = bytesRaw.Length;

    if (bool_is_data == false) {
        frame[0] = Convert.ToByte(129);
    } else {
        frame[0] = Convert.ToByte(130);
    }
    if (length <= 125) {
        frame[1] = Convert.ToByte(length);
        indexStartRawData = 2;
    } else if (length >= 126 && length <= 65535) {
        frame[1] = Convert.ToByte(126);
        frame[2] =  Convert.ToByte((length >> 8) & 255);
        frame[3] =  Convert.ToByte(length & 255);
        indexStartRawData = 4;
    } else {
        frame[1] = Convert.ToByte(127);
        frame[2] = Convert.ToByte((length >> 56) & 255);
        frame[3] = Convert.ToByte((length >> 48) & 255);
        frame[4] = Convert.ToByte((length >> 40) & 255);
        frame[5] = Convert.ToByte((length >> 32) & 255);
        frame[6] = Convert.ToByte((length >> 24) & 255);
        frame[7] = Convert.ToByte((length >> 16) & 255);
        frame[8] = Convert.ToByte((length >> 8) & 255);
        frame[9] = Convert.ToByte(length & 255);

        indexStartRawData = 10;
    }

    response = new byte[indexStartRawData + (length - 1)];
    int i = 0, reponseIdx = 0;

    //Add the frame bytes to the reponse
    for (int i = 0; i <= indexStartRawData - 1; i ++) {
        response[reponseIdx] = frame[i];
        reponseIdx += 1;
    }

    //Add the data bytes to the response
    for (int i = 0; i <= length - 1; i ++) {
        response[reponseIdx] = bytesRaw[i];
        reponseIdx += 1;
    }

    return response;
}
© www.soinside.com 2019 - 2024. All rights reserved.