36

Using getUserMedia I can capture video stream from client's webcam/camera. And using video tag I can show it on client's browser. Code:

<video autoplay></video>

<script type="text/javascript">
    window.URL = window.URL || window.webkitURL;
    navigator.getUserMedia  = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;

    var video = $('video')[0];

    var failed = function(e) {
        console.log('Denied!', e);
    };

    if( navigator.getUserMedia ) {
        navigator.getUserMedia( {video: true, audio: true}, function( stream ) {
                video.src = window.URL.createObjectURL(stream);
            }, failed
        )
    } else {
        console.log( 'Not supported!' );
    }
</script>

Now is it possible to send this video stream, either as a realtime feed or after user has done recording and decided to upload, to a server?

I found few examples of:

Vikas
  • 8,790
  • 4
  • 38
  • 48

2 Answers2

18

MediaStreamRecorder is a WebRTC API for recording getUserMedia() streams . It allows web apps to create a file from a live audio/video session.

 <video autoplay></video>

    <script language="javascript" type="text/javascript">
    function onVideoFail(e) {
        console.log('webcam fail!', e);
      };

    function hasGetUserMedia() {
      // Note: Opera is unprefixed.
      return !!(navigator.getUserMedia || navigator.webkitGetUserMedia ||
                navigator.mozGetUserMedia || navigator.msGetUserMedia);
    }

    if (hasGetUserMedia()) {
      // Good to go!
    } else {
      alert('getUserMedia() is not supported in your browser');
    }

    window.URL = window.URL || window.webkitURL;
    navigator.getUserMedia  = navigator.getUserMedia || 
                             navigator.webkitGetUserMedia ||
                              navigator.mozGetUserMedia || 
                               navigator.msGetUserMedia;

    var video = document.querySelector('video');
    var streamRecorder;
    var webcamstream;

    if (navigator.getUserMedia) {
      navigator.getUserMedia({audio: true, video: true}, function(stream) {
        video.src = window.URL.createObjectURL(stream);
        webcamstream = stream;
    //  streamrecorder = webcamstream.record();
      }, onVideoFail);
    } else {
        alert ('failed');
    }

    function startRecording() {
        streamRecorder = webcamstream.record();
        setTimeout(stopRecording, 10000);
    }
    function stopRecording() {
        streamRecorder.getRecordedData(postVideoToServer);
    }
    function postVideoToServer(videoblob) {

        var data = {};
        data.video = videoblob;
        data.metadata = 'test metadata';
        data.action = "upload_video";
        jQuery.post("http://www.kongraju.in/uploadvideo.php", data, onUploadSuccess);
    }
    function onUploadSuccess() {
        alert ('video uploaded');
    }

    </script>

    <div id="webcamcontrols">
        <button class="recordbutton" onclick="startRecording();">RECORD</button>
    </div>

Spec:

http://www.w3.org/TR/mediastream-recording/

you can send recorded file to server.

kongaraju
  • 9,344
  • 11
  • 55
  • 78
  • 1
    Don't you think this is too slow? I'm currently using the same solution but for a 1 min audio you have a lot to upload. – Omar Al-Ithawi Nov 13 '13 at 08:58
  • 28
    I'm still looking for a streaming solution when I can send bytes as soon as they arrive. – Omar Al-Ithawi Nov 13 '13 at 08:59
  • 3
    navigator.getUserMedia as been deprecated and replaced with MediaDevices.getUserMedia, see https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia – Ric Apr 23 '18 at 15:27
  • I want to send the video data through web Socket in javascript. Is there option doing this in your solution.. Thanks in Advance.. – susan097 Jun 13 '18 at 10:07
  • @susan097 you can do trick that from html 5 canva on which your camera feed is running get image data and sent to server through socket , it will be very fast , i know its bit bandwidth consuming solution am doing it in my project and it feels like video stream , as on other end am getting these image and write it to canva back – user889030 Jul 17 '20 at 07:34
  • @user889030 I know this idea but how to transfer the audio i.e audio+image = video. – susan097 Jul 23 '20 at 16:48
  • 2
    Omar Al-Ithawi Here is a solution to upload a webcam stream to the server as soon as the bytes arrive from the webcam, https://stackoverflow.com/q/72288653/4084546 Vote up this solution please. – Stan S. May 18 '22 at 12:12
4

Take a look at this article: http://www.smartjava.org/content/face-detection-using-html5-javascript-webrtc-websockets-jetty-and-javacvopencv

It shows a use of Webrtc:

These APIs should enable building applications that can be run inside a browser, requiring no extra downloads or plugins, that allow communication between parties using audio, video and supplementary real-time communication, without having to use intervening servers (unless needed for firewall traversal, or for providing intermediary services).

ndeverge
  • 21,378
  • 4
  • 56
  • 85
  • I saw this. It is similar to example 2, I mentioned in my question. From the article: `The next step is to grab the image from the canvas, convert it to binary, and send it over a websocket`. Problem is I need audio also, so this doesn't work for me. – Vikas Nov 17 '12 at 14:34
  • 1
    But it seems the audio is available too: http://www.smartjava.org/content/record-audio-using-webrtc-chrome-and-speech-recognition-websockets – ndeverge Nov 17 '12 at 14:41