java UDP声音流:为什么我有干扰?

时间:2016-01-03 22:21:30

标签: java udp audio-streaming

我正在尝试使用源和接收器构建一个非常简单的音频流。但是当我在“接收器”中收到声音时,我会受到一些干扰。我正在使用UDP协议。有没有办法“改进”我的代码以避免这些干扰?

这是我的音频服务器:

import java.io.File;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;

public class AudioPlayerServer implements Runnable {

    private SourceDataLine sLine;
    private AudioFormat audioFormat;
    private AudioInputStream audioInputStream=null;
    private String host="127.0.0.1";
    private int port=8000;
    private DatagramSocket server;
    private DatagramPacket packet;
    private long startTime;
    private long endTime=System.nanoTime();;
    private long elapsed=System.nanoTime();;
    private double sleepTime;
    private long sleepTimeMillis;
    private int sleepTimeNanos, epsilon;

    AudioPlayerServer(String host, int port) {      
        this.host=host;
        this.port=port;
        init();
    }

    public void init() {
        File file = new File("test.wav");
        try {
            audioInputStream=AudioSystem.getAudioInputStream(file);

        } catch (Exception e) {
            e.printStackTrace();
        }

        audioFormat = new AudioFormat(44100, 16, 2, true, false);
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
        System.out.println(info);

        try {
            server = new DatagramSocket();
            System.out.println("Server started");

        } catch (SocketException e) {
            e.printStackTrace();
        }       
    }

    public void run() {
        try {
            byte bytes[] =  new byte[4096];
            byte bytes2[] =  new byte[1024];
            int bytesRead=0;
            //The sending rythm of the data have to be compatible with an audio streaming.
            //So, I'll sleep the streaming thread for (1/SampleRate) seconds * (bytes.lenght/4) - epsilon
            //=> bytes.lenght/4 because 4 values = 1 frame => For ex, in  1024 bits, there are 1024/4 = 256 frames
            //epsilon because the instructions themselves takes time.
            //The value have to be convert in milliseconds et nanoseconds.
            sleepTime=(1024/audioFormat.getSampleRate());
            epsilon=400000;
            sleepTimeMillis=(long)(sleepTime*1000);
            sleepTimeNanos=(int)((sleepTime*1000-sleepTimeMillis)*1000000);
            System.out.println("Sleep time :"+sleepTimeMillis+" ms, "+sleepTimeNanos+" ns");

            while ((bytesRead=audioInputStream.read(bytes, 0, bytes.length))!= -1) {
                //getSignalLevel(bytes);

                try {                   
                    //startTime=System.nanoTime();
                    packet = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(host), port);
                    packet.setData(bytes);
                    server.send(packet);                    
                    packet.setLength(bytes.length);                 
                    //endTime=System.nanoTime();
                    //System.out.println(endTime-startTime);
                    Thread.sleep(sleepTimeMillis,sleepTimeNanos);                   
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }   
            System.out.println("No bytes anymore !");                   
        } catch (Exception e) {
            e.printStackTrace();
        }
        sLine.close();
        System.out.println("Line closed");

    }

}

这是客户:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;

public class AudioReceiver implements Runnable{
    private String host;
    private int port;
    private SourceDataLine sLine;
    private AudioFormat audioFormat;
    byte[] buffer=new byte[4096];
    DatagramPacket packet;

    AudioReceiver (String host, int port) {
        this.host=host;
        this.port=port;
        init();
        Thread t1=new Thread(new Reader());
        t1.start();
    }

    public void init() {
        audioFormat = new AudioFormat(44100, 16, 2, true, false);
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);

        try  {
            System.out.println(info);
            sLine=(SourceDataLine) AudioSystem.getLine(info);
            System.out.println(sLine.getLineInfo() + " - sample rate : "+audioFormat.getSampleRate());
        } catch (Exception e) {
            e.printStackTrace();
        }       
    }

    public void run() {
        System.out.println("Client started");
        try {
            sLine.open(audioFormat);
        } catch (Exception e){
            e.printStackTrace();
        }
        sLine.start();
        System.out.println("Line started");

        try {

            DatagramSocket client = new DatagramSocket(port, InetAddress.getByName(host));
            while (true) {
                try {
                    packet = new DatagramPacket(buffer, buffer.length);
                    //System.out.println("Reception beggins for host "+host+" : "+port);
                    client.receive(packet);
                    //System.out.println("Reception ends");
                    buffer=packet.getData();

                    //sLine.write(packet.getData(), 0, buffer.length);
                    packet.setLength(buffer.length);
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        } catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        }

    }

    public class Reader implements Runnable {
        public void run() {
            while (true) {
                if (packet!=null) {
                    sLine.write(packet.getData(), 0, buffer.length);
                }
            }           
        }       
    }   
}

2 个答案:

答案 0 :(得分:2)

创建UDP流式系统时,通常使用RTP协议。 RTP使用UDP,这是一种无连接的不可靠协议。在传输层(UDP),您需要处理丢失和无序到达。此外,网络层是突发性的,数据不会达到很好的均匀速率。相反,数据包将以不一致的到达速率到达。因此,您必须在本地缓冲数据以处理此网络抖动 This post answers and explains关于java,UDP,RTP,网络抖动,缓冲,丢包。 处理损失也有不同的策略。您可以用静音填充它或估计丢失的数据。此外,您的客户端可能比您的服务器更快地播放样本,并最终耗尽数据。这是由于没有公共总线的两个系统之间的时钟晶体的变化。 This post answers and explains处理数据包丢失和时钟漂移。

答案 1 :(得分:0)

  

我正在使用UDP协议。有没有办法“改进”我的代码以避免这些干扰?

有两种可能性:

  1. UDP消息正在丢失。

  2. 客户端或服务器端的应用程序逻辑出现问题,导致音频流数据损坏。

  3. 假设问题是#1,最简单的选择是切换到TCP。

    UDP本质上容易受到数据包丢失的影响,丢失的数据包会导致失真。 TCP没有损耗,如果“管道”中存在一些延迟(即客户端缓冲),您应该能够避免由于偶尔丢包和重传(在TCP级别)引起的抖动引起的失真。 p>

    我还注意到您当前的客户端/服务器逻辑正在尝试使用sleep控制服务器端的播放速率。您需要注意sleep无法保证在完全的时间点唤醒您的睡眠线程。语义是“为至少指定时间”。这个,以及客户端没有任何缓冲,也可能导致失真。