2

I'm trying to send video from the camera to the server and take it back to my computer, the video from the camera is perfectly displayed on the first screen, and on the second screen should be the same video, but received from the server. I send the video to the server well and the server accepts it and sends it back to the computer. But the video is not displayed on the second screen.

Here is my code.

Client:

<video autoplay class="video" id="video1"></video>
<video autoplay class="video" id="video2"></video>
<script type="text/javascript">
var video1, video2;
var port="<%out.write(port);%>";
var ws;
var connect=false;
switch(lang){
    case "en":
        text1="roll up";
        text2="expand";
        break;
    case "ru":
        text1="свернуть";
        text2="развернуть";
        break;
    }
video1=document.querySelector('#video1');
ws = new WebSocket("ws://127.0.0.1:"+port);
ws.binaryType = 'arraybuffer';
navigator.mediaDevices.getUserMedia({ audio: true, video: {width: 400,         height: 400}})
    .then(stream => {
        var streamChunks = [];
        var outChunks = [];
        video1.src = window.URL.createObjectURL(stream);
        const mediaRecorder = new MediaRecorder(stream);
        //запись потока в массив
        mediaRecorder.addEventListener("dataavailable",function(event) {
            if(connect==true){
                streamChunks.push(event.data);
                ws.send(streamChunks[0]);
            }
            streamChunks = [];
        });
        mediaRecorder.start();
        setInterval(function() {
            mediaRecorder.requestData();
        }, 1000);
});
var mediaSource = new MediaSource();
var buffer;
var queue = [];
video2=document.querySelector('#video2');
ws.onopen = function(e) {
      connect=true;
};
mediaSource.addEventListener('sourceopen', function(e) {
    video2.play();
    buffer = mediaSource.addSourceBuffer('video/webm; codecs="vorbis,vp8"');

    buffer.addEventListener('update', function() { //Note: Have tried 'updateend'
        if (queue.length > 0 && !buffer.updating) {
            buffer.appendBuffer(queue.shift());
        }
    });
}, false);
video2.src=window.URL.createObjectURL(mediaSource);
ws.onmessage = function(event) {
    if (event.data instanceof ArrayBuffer) {
        try {
           if (buffer.updating || queue.length > 0) {
            queue.push(event.data);
           } else {
            buffer.appendBuffer(event.data);
           }
        }  catch (e) {
            console.log(e);
        }                               
}  else  {
    writeResponse(event.data); 
}
};
</script>

Server

package model;

import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.HashSet;
import java.util.Set;

import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;

public class video_chat_server extends WebSocketServer {
    private Set<WebSocket> conns;
    public video_chat_server( int port ) throws UnknownHostException {
        super( new InetSocketAddress(port) );
        conns = new HashSet<>();
    }

    public video_chat_server( InetSocketAddress address ) {
        super(address);
    }

@Override
public void onMessage(WebSocket conn, ByteBuffer blob) {
    // TODO Auto-generated method stub
    for (WebSocket sock : conns) {
        sock.send(blob);
    }
}

@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
    // TODO Auto-generated method stub
    if(conns.size()<2)conns.add(conn);
}
}
Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
  • You say _"the server.. sends it back to the computer. But the video is not displayed"_ Does it play if your "second screen" is a media player like [**VLC**](https://www.videolan.org/index.ru.html)? just use "open file/URL" option and paste your stream/server link. See if the video displays or you get useful errors. Otherwise try to save the received bytes as binary file (to disk) so we can check if data is correct. Give link to short example file. – VC.One Oct 05 '18 at 06:04
  • 1
    saved the received bytes on the server to a separate file and through VLC excellent video and sound works https://drive.google.com/open?id=160rP9gRsSVsqlB3GBNV5nJ4a-rp5zJa4 – Mikhail Grebenev Oct 05 '18 at 15:26
  • const audioUrl = URL.createObjectURL(event.data); video2.src = audioUrl; this code displays the first piece of received binary data from the server; subsequent pieces of data do not display – Mikhail Grebenev Oct 05 '18 at 15:27
  • log in Chrome see me this error: This SourceBuffer has been removed from the parent media source. – Mikhail Grebenev Oct 05 '18 at 20:11
  • I forgot to say earlier... I tried your link. It seems no browser (Chrome, Firefox) likes MKV format in ` – VC.One Oct 05 '18 at 20:22
  • PS: First try just recording / playing video (avc/mp4) without sound. To eliminate where problem happens. Also are you okay to use FFmpeg at Java stage to re-encode output as fragmented MP4? Something like: https://stackoverflow.com/questions/8616855/how-to-output-fragmented-mp4-with-ffmpeg – VC.One Oct 05 '18 at 20:35

0 Answers0