通过套接字发送序列化的垫

时间:2018-07-22 11:56:39

标签: java sockets opencv video-streaming

我编写了一个程序,可以将实时视频流发送到另一台计算机,我认为格式可以正常工作,并且您可以在接收计算机上看到视频,但是在几秒钟后连接中断。我想我知道为什么,但是我不知道如何解决...

这是来自服务器的代码:

 //The routine that sends the images
 while(true){
                if(picChanged){
                    picChanged = false;
                    System.out.println("Stuck before setting new bytes");
                    sendable.setBytesByMat(FXMLController.picture);

                    while(true){
                        client.writeLine("p");

                        if(client.readLine().equals("s"))           
                                break;
                    }

                    client.sendSendableMat(sendable);

                    System.out.println("wait for confirmation");

                    //Get confirmation
                    while(true){
                        if(client.readLine().equals("k")){
                            break;
                        }
                    }
                }
            }

//This is from sendable
public byte[] setBytesByMat(Mat mat){
    this.mat = mat;
    rows = mat.rows();
    cols = mat.cols();
    ch = mat.channels();
    type = mat.type();

    int bufferSize = rows * cols * ch;

    rawBytes = new byte[bufferSize];
    mat.get(0, 0, rawBytes);

    return rawBytes;
}


//This is from client class
public void writeLine(String line) throws IOException{
    out.write((line + "\n").getBytes());
    out.flush();
}

public String readLine(){
    String line = in.nextLine();

    return line;
}

public void sendSendableMat(SendableMat sendable) throws Exception{

    int pointer = 0, i = 50;
    byte[] cache = new byte[50];
    boolean running = true;


    while(running){
        if(pointer+50 >= sendable.getBytes().length){
            pointer = sendable.getBytes().length - pointer;
            i = sendable.getBytes().length;
            running = false;
        }else{
            i = pointer + 50;
        }

        cache = Arrays.copyOfRange(sendable.getBytes(), pointer, i);
        out.write(cache);
        out.flush();
        pointer = i;
    }

    System.out.println("sent pic");
}

我预先设置了所有内容,并且效果很好,因为在最初的几秒钟就有一个流,这就是为什么我不发布所有内容的原因

这是来自实际客户的相关代码:

byte[] bytes;
            SendableMat sendable = new SendableMat();
            Mat finalMat = new Mat();
            while(true){
                buffer = readLine();
                //System.out.println(buffer);
                if(buffer.equals("p")){
                    writeLine("s");
                    System.out.println("received p signal");
                    bytes = receiveMat();
                    finalMat = sendable.assembleMatByBytes(bytes, rows, cols, type);
                    controller.updateImageView(controller.currentFrame, controller.Mat2Image(finalMat));
                    System.out.println("Updated view");

                    int read=1;
                    int count = 0;
                    while(read > 0){
                        read = bufferedIn.available();
                        count += read;
                        if(read>0)
                            bufferedIn.read(new byte[read]);            //Read the rest of the bits stuck in the socket buffer so anything else can be received without being compromised
                    }


                    System.out.println("Received " + count + " more");

                    System.out.println("Sent confirmation k");
                    writeLine("k");
                }
            }

private byte[] receiveMat() throws IOException{
        byte[] buffer = new byte[200];
        byte[] bytes = new byte[length];
        int pointer = 0, receive = 0, read = 0;
        boolean running = true;
        int readingLength = buffer.length;

        while(running){
            receive = bufferedIn.available();
            if(receive > 0){
                try{

                    read = bufferedIn.read(buffer, 0, buffer.length-1);
                    readingLength = read;

                    if(pointer >= length){
                        System.out.println("pointer out of bounds");
                        break;
                    }

                    if(pointer + read >= length){
                        running = false;
                        readingLength = length - pointer;
                        System.out.println("Final pos: " + String.valueOf(readingLength+pointer));
                        if(readingLength > buffer.length){
                            readingLength = buffer.length;
                        }
                        System.out.println("To long of an array, cut it, " + String.valueOf(pointer+read));
                    }

                    System.arraycopy(buffer, 0, bytes, pointer, readingLength);
                }catch(IndexOutOfBoundsException e){
                    e.printStackTrace();
                    System.out.println("Pointer: " + pointer);
                    System.out.println("readingLength: " + readingLength);
                }

                pointer = read + pointer;
            }
        }

        System.out.println("received bytes");
        return bytes;
    }
//The read and write methods of the client are the same as for the server

每当代码被卡住时,仍然会收到最后收到的干净图像,但是while循环上方来自客户端的缓冲区中会填充图像数据,这就是为什么卡住了的原因。另一件事是,打印出的count变量的大小始终与mat的字节数组的长度相同。这意味着它被发送了两次,但我不知道原因和位置。当流停止时,有一个例外。然后,该计数要小得多,大约为130000(字节)。 我确实意识到这有点儿编译,但是我确实需要一些帮助。

谢谢你

ChopStick

0 个答案:

没有答案