Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send video/audio over socket.io from browser

I am trying to send video and audio trought socket.io but I geting Buffer on the end how should I handle it?

Here is my code:

SERVER, HERE I RECIVE BUFFER:

io.on('connection', function (socket) {
    socket.on('radio', function (image) {
        socket.broadcast.emit('radio-reciver', {count: 1, buff: image});
    })
})

http.listen(port, function () {
    console.log('Server started!')
});

CLIENT:

//PART FOR SENDING VIDEO/AUDIO

navigator.getUserMedia({ video: true, audio: true }, loadCam, loadFail)

function loadCam(stream) {
            video.src = window.URL.createObjectURL(stream);

            var media = new MediaRecorder(stream);
            media.ondataavailable = function (e) {
                socket.emit('radio', e.data);
            }


            media.start(1000)
            logger("Cam is ok")
}

//PART FOR RECIVING, HERE I RECIVE BUFFER

socket.on('radio-reciver', function (image) {

            var sourceStream = MediaSourceStream({  // Creates a writable stream
                mimeType: 'video/webm; codecs="opus,vp8"'
            })
            image.pipe(sourceStream)

            var img = document.getElementById('play');
            img.src = window.URL.createObjectURL(sourceStream.mediaSource);
            $('#logger').text(image);
        })

As you see I am using media recorder to transfer data but not working... Any example how to fix this?

like image 210
Vladimir Djukic Avatar asked Nov 08 '22 08:11

Vladimir Djukic


1 Answers

Rather than doing image.pipe(), you should probably use:

sourceStream.write(image);

You're going to be receiving chunks here, as Buffer objects. Not a stream.

like image 163
Brad Avatar answered Nov 12 '22 17:11

Brad