从IPCAM播放音频流的极端延迟

时间:2019-04-12 17:28:28

标签: java audio audioinputstream

(抱歉,不是会讲英语的人,期望有很多语法/语法错误)

我正在开发一款软件来管理D-Link Ip Cam(DCS-xxxx系列等)。由于此摄像机会暴露音频流(某些型号甚至具有用于双向通信的扬声器),因此我想根据用户要求进行播放。

所有入口都位于http基本身份验证的后面(但是很奇怪,我不能使用http:\ USER:PASS@192.168.1.100,因为我得到了401)。

我使用javax.sound。*包来执行此操作,但是由于某些原因,音频会在 15至20秒后开始播放,总延迟为30-40秒 EDIT :平均为45秒,但音频是从一开始就播放的,因此效果甚至更糟。

这是课程(最低要求,仅用于测试目的)

import java.io.IOException;
import java.net.Authenticator;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class AudioPlayer implements Runnable{

    private URL URL;
    private String USERNAME;
    private String PASSWORD;

    private volatile boolean stop = false;

    public AudioPlayer(String url, String user, String pass) throws MalformedURLException{
        this.URL = new URL(url);
        this.USERNAME = user;
        this.PASSWORD = pass;
    }

    public void shutdown() {
        stop = true;
    }

    @Override
    public void run() {
        Authenticator.setDefault (new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication (USERNAME, PASSWORD.toCharArray());
            }
        });

        try {
            Clip clip = AudioSystem.getClip();
            AudioInputStream inputStream = AudioSystem.getAudioInputStream(URL);
            clip.open(inputStream);
            clip.start();
            while(!stop && clip.isRunning()) {}
            clip.stop();
            System.err.println("AUDIO PLAYER STOPPED");
        } catch (LineUnavailableException | IOException | UnsupportedAudioFileException e) {
            e.printStackTrace();
        }
    }

}

需要身份验证器部分,因为ipcam使用基本的http认证。

我在某处读到AudioSystem使用不同的算法多次通过以获得正确的算法,然后将流重置为开始,然后才开始播放。 因此,因此,AudioSystem可能在实现使用哪种类型的编解码器时可能会遇到一些问题(可能需要某种报头),并且花了相当长的时间才开始播放音频。

值得一提的是,即使VLC也在努力跟上流媒体的步伐,在播放前最多损失了8秒(8秒比20秒要好得多)。 IpCam在本地网络上。

我的代码有问题吗?我看不到某些方法?

真的不知道在哪里看。

我在这里或其他地方找不到任何有意义的答案。

1 个答案:

答案 0 :(得分:0)

弄一个答案后,我找到了解决方案,它提供了1到2秒的延迟(此延迟与官方应用程序或网页配置的延迟相同,非常完美)。

private void playStreamedURL() throws IOException {

        //to avoid 401 error
        Authenticator.setDefault (new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                //USERNAME and PASSWORD are defined in the class
                return new PasswordAuthentication (USERNAME, PASSWORD.toCharArray()); 
            }
        });

        AudioInputStream AIS = null;
        SourceDataLine line = null;

        try {
//get the input stream
            AIS = AudioSystem.getAudioInputStream(this.URL);

//get the format, Very Important!
            AudioFormat format = AIS.getFormat();
            DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

//create the output line
            line = (SourceDataLine) AudioSystem.getLine(info);
//open the line with the specified format (other solution manually create the format
//and thats is a big problem because things like sampleRate aren't standard
//For example, the IpCam i use for testing use 11205 as sample rate.
            line.open(format);

            int framesize = format.getFrameSize();

//NOT_SPECIFIED is -1, wich create problem with the buffer definition, so it's revalued if necessary
            if(framesize == AudioSystem.NOT_SPECIFIED)
                framesize = 1;

//the buffer used to read and write bytes from stream to audio line
            byte[] buffer = new byte[4 * 1024 * framesize];
            int total = 0;

            boolean playing = false;
            int r, towrite, remaining;
            while( (r = AIS.read(buffer, total, buffer.length - total)) >= 0 ) { //or !=-1
                total += r;

//avoid start the line more than one time
                if (!playing) {
                    line.start();
                    playing = true;
                }

//actually play the sound (the frames in the buffer)
                towrite = (total / framesize) * framesize;
                line.write(buffer, 0, towrite);

//if some byte remain, overwrite them into the buffer and change the total
                remaining = total - towrite;
                if (remaining > 0)
                    System.arraycopy(buffer, towrite, buffer, 0, remaining);
                total = remaining;
            }

//line.drain() can be used, but it will consume the rest of the buffer.
            line.stop();
            line.flush();
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
            e.printStackTrace();
        } finally {
            if (line != null)
                line.close();
            if (AIS != null)
                AIS.close();
        }

    }

仍然可以进行一些优化,但是可以。