Showing posts with label media source api. Show all posts
Showing posts with label media source api. Show all posts

Javascript getUserMedia API

Capturing Audio and Video in HTML5: 


Earlier were the days when internet started that people started using chat as a medium of communication in the mid 90's. People used to capture audio or video and then send it over the internet.These features required the use of plugins like flash and Silverlight and these two had certain technical problems.

With the emergence of WebRTC the plugins has made its way out. One of the API that is derived from WebRTC is getUserMedia API.

Javascript getUserMedia API

 Get started with getUserMedia API:


Several multimedia streams such as video, audio and both can be accessed from local devices.The users need not use additional plugins to access the video or audio.It helps the non-technical user to use the software without any additional plugins.However this API allows users to acquire the video and audio without having to send the data or storing it in a file. This API uses one method called as method.getUserMedia() which belongs to the window.navigator object.

Syntax :

   navigator.getUserMedia(constraints,successCallback,errorCallback); 

Constraints: 


It has a constraints parameter which is an object that has two properties called audio and video. The properties contain two values true and false. True means to request the stream and false means do not request the stream.

 Example: to request only video the parameter is as follows


    { 
        Video:true, 
        Audio:false 
     } 
Example -2: If the user wants a high resolution of 1280x720 of both audio and video and if the frame rate is 30 and if we want a frame rate of 60 then the following code could be used

    { 
        video:{ 
            mandatory:{ 
                 minWidth:1280, 
                 minHeight:720, 
                 minFrameRate:30 
            }, 
            optional:[{minFrameRate :60}] 
         }, 
         audio:true 
     }

successCallback:


The function on the calling application to invoke when passing the LocalMediaStream object. The getUserMedia() will call the successCallback using the LocalMediaStream object.

 Example :

     function(localMediaStream) { 
          var myvideo=document.querySelector(‘video’); 
          myvideo.src=window.URL.createObjectURL(localMediaStream); 
          myvideo.onloadedmetadata=function() { 
                 //play or do something with the video. 
          };
     },  

errorCallBack: 


The getUserMedia() function will call the function called as errorCallback with error codes as follows-
Permission_denied: The user is denied the permission to use a media device required for the operation. Not_supported_error: A constraint specified is not supported by the browser. Mandatory_unsatisfied_error: If no media tracks of the type specified in the constraints are found an error occurs.

 To get permissions:


To get getUserMedia() installable on the app the following code should be embedded inside the file-

      “permissions”:{ 
            “audio-capture”:{ 
                  “description”:”required to capture audio via getUserMedia” 
              } 
        } 

Compatibility of the browser: 


The getUserMedia() API is supported by the desktop browser but it’s performance of mobile browser is poor.The current browsers that support this API are Chrome, Opera and Firefox.Some browsers also have a security issue such that upon calling the getUserMedia() API, the browsers give an option to the user saying that the user can grant/deny access to the camera/mic.

Conclusion:


The getUserAPI() derived from WebRTC project has worked wonders in making web applications successful.This API is very flexible allowing user to select its own parameters such as the audio and the video streams.The compatibility among the browsers is not very good but it would increase eventually.
HTML5: Recording Audio,Video Streams

HTML5: Recording Audio,Video Streams

MediaStreamRecorder API is webRTC API that supports recording streams with getUserMedia or remote streams passed over peerconnection session.



 
    <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("uploadvideo.php", data, onUploadSuccess);
    }
    function onUploadSuccess() {
        alert ('video uploaded');
    }
 
    </script>
 



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


"getRecorderdedData" return  blob that was recorded.


HTML5 Video Streaming: Media Source API

Streaming Video using HTML5 is now very simple, Media Source API gives streaming video as chunks. playing video with chunks of data is now possible.


This API allows to generate media streams, using  javascript streaming is enabled by extending HMTLElement.

<video id="video" src="" height="300" width="300">
      Video element is not supported in your browser
</video>




window.MediaSource = window.MediaSource || window.WebKitMediaSource;

var mediasource = new MediaSource();

var video = document.getElementById('video');
video.src = window.URL.createObjectURL(ms);

mediasource.addEventListener('webkitsourceopen', function(e) {
  ...
  var sourceBuffer = mediasource.addSourceBuffer('video/webm; codecs="vorbis,vp8"');
  sourceBuffer.append(oneVideoWebMChunk);
  ...
//start playing after video header recieved(i.e; first chunk)
//call media.endOfStream(); before appending last chunk
}, false);

playing large volume media files is a bit hard, just make them as chunks and stream with video element. Remember that Video file header contains vital information to play video, this should be necessary to seek and play stream video. if you want to play video from a segment you should first load video fie header then load the segment data to stream your data.

you start playing the stream after receiving header only.

Media Source API now works only chrome( set --enable-media-source flag ).
Media Source Extensions specification changed, changes expected to be made in next release.