Voip UDP添加序列号(准备丢包)

时间:2016-02-20 20:57:55

标签: java udp voip

注意:我目前正在学习UDP以及与TCP系统相比VoIP系统的效率,我已经完成了TCP,所以请不要评论TCP更好等等。

所以我正在尝试为数据包添加一个序列号,这样我就可以在服务器端对它们进行排序,并为重复前面的任何丢失数据包做准备,例如

问题:我读了一个stackoverflow,说使用DataOutputStreams是一个很好的方法,所以我实现了它。然而,当使用代码dos.writeInt(sequenceNumber++);时,我得到了一个可怕的重复噼啪声。我想也许问题是我发送的字节数。

提前谢谢,任何指针都会很棒

  boolean running = true;
    try {
        AudioRecorder recorder = new AudioRecorder();
        int sequenceNumber = 0;
        while (running) {
                byte[] tempBuffer = recorder.getBlock();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                DataOutputStream dos = new DataOutputStream(baos);
                dos.write(tempBuffer);
                dos.writeInt(sequenceNumber++);

                dos.flush();
                DatagramPacket sendPacket = new DatagramPacket(baos.toByteArray(), baos.size(), clientIP, PORT);
                sending_socket.send(sendPacket);

        }
        recorder.close();
    } catch (Exception e) {
        System.out.println("Error" + e);

服务器端:

byte[] buffer = new byte[512];
        byte[] temp = new byte[512];
        try {
            // CODE TO RECEIVE THE AUDIO
            AudioPlayer player = new AudioPlayer();
            int expectedValue = 0;
            while (running) {

                //Vector used to store audio blocks (32ms/512bytes each)
                Vector<byte[]> voiceVector = new Vector<>();

                // creates a new udp packet to receive the audio
                DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
                receiving_socket.receive(packet);

                // creates a byte array from the data
                byte[] udpPacketBytes = packet.getData();
                ByteArrayInputStream baos = new ByteArrayInputStream(udpPacketBytes);

                DataInputStream dos = new DataInputStream(baos);

                int receivedValue = dos.readInt();
                if (receivedValue == expectedValue) {
                    byte[] filteredByteArray = Arrays.copyOfRange(udpPacketBytes, 4, udpPacketBytes.length - 4);
                    voiceVector.add(filteredByteArray);

                    Iterator<byte[]> voiceItr = voiceVector.iterator();

                    while (voiceItr.hasNext()) {
                        player.playBlock(voiceItr.next());
                    }

                } else {
                    // play the previous again
                    byte[] filteredByteArray = Arrays.copyOfRange(temp, 4, temp.length - 4);
                    voiceVector.add(filteredByteArray);
                    Iterator<byte[]> voiceItr = voiceVector.iterator();

                    while (voiceItr.hasNext()) {
                        player.playBlock(voiceItr.next());
                    }

                    // play the current one
                    byte[] fba = Arrays.copyOfRange(udpPacketBytes, 4, udpPacketBytes.length - 4);
                    voiceVector.add(fba);

                    Iterator<byte[]> vItr = voiceVector.iterator();

                    while (vItr.hasNext()) {
                        player.playBlock(vItr.next());
                    }
                }
                System.out.println(receivedValue + " " + expectedValue);
                expectedValue = receivedValue + 1;
                temp = packet.getData();


            }
        } catch (Exception e) {
            System.out.println("yo");
        }
        //Close the socket
        receiving_socket.close();

1 个答案:

答案 0 :(得分:2)

您正在编写序列号,但您没有阅读或删除序列号,因此序列号最终会出现在音频中。

我建议你阅读你写的格式。