首页 > 解决方案 > 无缝视频共享

问题描述

我正在尝试在 LAN 上共享屏幕,但它显示出非常明显的延迟。我尝试了几种技术,但仍然很明显滞后。我尝试了以下技术:

  1. 从 OutputStream 在套接字上发送帧
  2. 在使用aspose库发送之前压缩帧(jpeg)
  3. 将帧添加到队列,然后轮询队列以发送它们

但是尽管尝试了所有方法,但仍然可以清楚地看到滞后。任何人都可以分享他们对如何进一步改进这一点的意见,以便在局域网上共享屏幕时没有/很少滞后?

发件人.java

class SendScreen extends Thread{

    Socket socket=null;
    Robot robot=null;
    Rectangle rectangle=null;
    boolean continueLoop=true;
    Queue<BufferedImage> q;
  
    OutputStream oos=null;

    public SendScreen(Socket socket,Robot robot,Rectangle rect) {
    this.socket=socket;
    this.robot=robot;
    rectangle=rect;
        
        q= new LinkedList<>();
    start();
    }

    public void run(){
    
        try{
    oos=socket.getOutputStream(); 
    }catch(IOException ex){
        ex.printStackTrace();
    }

    while(continueLoop){
    BufferedImage image=robot.createScreenCapture(rectangle);

    try{                                                      
             /* 
            ByteArrayOutputStream ous = new ByteArrayOutputStream();
                
           ImageIO.write(image, "jpeg", ous);

             InputStream inStream = new ByteArrayInputStream( ous.toByteArray() );

            Image original=Image.load(inStream);
    JpegOptions jpegOptions = new JpegOptions()
      {{
        // Apply compression
            setCompressionType(JpegCompressionMode.Progressive);
      }};
  
  // Save compressed image  
  ous.flush();*/ 
    q.add(image);
    //q.poll();
    ImageIO.write( q.poll(),"jpeg",oos);
//  original.save(oos);
            
            
            
        //ImageIO.write(image,"jpeg",oos);
    }catch(IOException ex){
        ex.printStackTrace();
    }
    
    
    }
    }
}

接收者.java

class ReceiveScreen extends Thread{
    private ObjectInputStream cObjectInputStream = null;
    private JPanel cPanel = null;
    private boolean continueLoop = true;
    InputStream oin = null;
    Image image1 = null;

    public ReceiveScreen(InputStream in,JPanel p){
        oin = in;
        cPanel = p;
        start();
    }

    public void run(){
        try{
            //Read screenshots of the client and then draw them
            while(continueLoop){
                byte[] bytes = new byte[1024*1024];
                int count = 0;
                do{
                    count+=oin.read(bytes,count,bytes.length-count);
                }while(!(count>4 && bytes[count-2]==(byte)-1 && bytes[count-1]==(byte)-39));

                image1 = ImageIO.read(new ByteArrayInputStream(bytes));
                image1 = image1.getScaledInstance(cPanel.getWidth(),cPanel.getHeight(),Image.SCALE_FAST);

                //Draw the received screenshots

                Graphics graphics = cPanel.getGraphics();
                graphics.drawImage(image1, 0, 0, cPanel.getWidth(), cPanel.getHeight(), cPanel);
            }

        } catch(IOException ex) {
            ex.printStackTrace();
        }
    }
}

标签: javasocketsoutputstreamscreensharing

解决方案


推荐阅读