Source: easyrtc_recorder.js

/* global define, module, require, console */
/*!
  Script: easyrtc_recorder.js

    This code demonstrate recording of local and remote streams.

  About: License

    Copyright (c) 2016, Priologic Software Inc.
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:

        * Redistributions of source code must retain the above copyright notice,
          this list of conditions and the following disclaimer.
        * Redistributions in binary form must reproduce the above copyright
          notice, this list of conditions and the following disclaimer in the
          documentation and/or other materials provided with the distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/

"use strict";

(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        //RequireJS (AMD) build system
        define(['easyrtc'], factory);
    } else if (typeof module === 'object' && module.exports) {
        //CommonJS build system
        module.exports = factory(require('easyrtc'));
    } else {
        //Vanilla JS, ensure dependencies are loaded correctly
        if (typeof window.easyrtc !== 'object' || !window.easyrtc) {
            throw new Error("easyrtc_recorder requires easyrtc");
        }
        root.easyrtc = factory(window.easyrtc);
  }
}(this, function (easyrtc, undefined) {

    /** @class Easyrtc_Recorder
     * Provides methods for building MediaStream recorders.
     * These methods are actually added to easyrtc, not in an Easyrtc_Recorder class.
     */
var Easyrtc_Recorder = function() {

   /**
     * Determines if recording is supported by the browser. 
     * @returns true if recording is supported.
     */
   this.supportsRecording = function() {
        return (typeof MediaRecorder !== "undefined" && navigator.getUserMedia );
   };

   /**
    * Check if a particular codec can be used for recording.
    * @param {String} codecName, either "vp8" or "vp9 or "h264"
    * @param {Object} options object. Currently the only supported field
    *    is mimeType.
    * @returns true if the type can be used, or if the browser doesn't
    *  support a method to find out.
    */ 
   this.isRecordingTypeSupported = function(videoCodecName) {
       var mimeType = "video/webm;codecs=" + videoCodecName;
       if( MediaRecorder.isTypeSupported ) {
           // chrome definitely, maybe firefox
           return MediaRecorder.isTypeSupported(mimeType);
       }
       else if( MediaRecorder.isMimeTypeSupported ) {
           // maybe firefox
           return MediaRecorder.isMimeTypeSupported(mimeType);
       }
       else {
          if( typeof easyrtc.hasNoRecordTypeCheck === "undefined") {
             easyrtc.hasNoRecordTypeCheck = true;
             window.alert("This browser doesn't know what media types it supports. Assuming all types.");
          }
          return true;
       }
   };

   var mimeType;

   /**
     * Set the desired codec for the video encoding. 
     @param {String} codecName, either "vp8" or "vp9 or "h264"
     @returns true if the type can be used.
     */ 
   this.setRecordingVideoCodec = function(videoCodecName) {
       if( !easyrtc.supportsRecording ) {
           return false;
       }
       if(easyrtc.isRecordingTypeSupported(videoCodecName)) {
           mimeType = "video/webm;codecs=" + videoCodecName;
           return true;
       }
       else {
          return false;
       }
   };

   if( easyrtc.supportsRecording()) {
       easyrtc.setRecordingVideoCodec("vp8");
   }

   /**
     * Create a recording object and attach a local media stream to it.
     * @param mediaStream 
     * @param onError a callback for errors encountered while recording.
     * @returns a recorder object or null if recording not supported.
     */
    function startRecording( localStream) {

        if( !easyrtc.supportsRecording ) {
           console.log("recording not supported by your browser");
           return null;
        }

        var mediaRecorder = new MediaRecorder(localStream, {mimeType: mimeType});
        if( !mediaRecorder ) {
           console.log("no media recorder");
           return;
        }
        mediaRecorder.start();

        mediaRecorder.onerror = function(e) {
           console.log("Media recording error:", e);
        }

        mediaRecorder.onwarning = function(e) {
           console.log("Media recording error:", e);
        }
    
        mediaRecorder.onstart = function(e) {
           console.log("Media recording started");
        }

        mediaRecorder.onstop = function(e) {
           console.log("Media recording stopped");
        }

        return mediaRecorder;
   };

   /** This method creates a media recorder and populates it's ondataavalable
     * method so that your own callback gets called with the data.
     * Use the media recorder's start(), stop(), pause() and resume() methods
     * on the returned object.
     * @param mediaStream a local or remote media stream.
     * @param {Function} dataCallback a function to receive the webm data from.
     */
   this.recordToCallback = function (mediaStream, dataCallback) {
       var mediaRecorder = startRecording(mediaStream);
       if( !mediaRecorder) {
           return null;
       }
       mediaRecorder.ondataavailable = function(e) {
           dataCallback(e.data);
       }
       return mediaRecorder;
   };

   /** This method creates a media recorder that builds a blob 
    * Use the media recorder's start(), stop(), pause() and resume() methods
    * on the returned object.
    * @param mediaStream a local or remote media stream.
    * @param {Function} blobCallback a callback function that gets called with a
    *    blob once you invoke the stop method.
    **/
   this.recordToBlob = function(mediaStream, blobCallback) {
       var chunks = [];

       function dataConsumer(chunk) {
          chunks.push(chunk);
       }

       var mediaRecorder = easyrtc.recordToCallback(mediaStream,
              dataConsumer);

       if( !mediaRecorder) {
           return null;
       }

       mediaRecorder.onstop = function() {
            blobCallback( new Blob(chunks, {type:"video/webm"}));
            chunks = [];
       }
       return mediaRecorder;
   };

   /** This method creates a media recorder that builds a file.
    * Use the media recorder's start(), stop(), pause() and resume() methods
    * on the returned object.
    * @param mediaStream a local or remote media stream.
    * @param {Object} downloadLink an anchor tag to attach the file to.
    * @param {String} basename the name of the file. A .webm will be appended
    *    to the file if its not already present. The file doesn't get written
    *    until you call the mediaRecorder's stop method.
    **/
   this.recordToFile = function(mediaStream, downloadLink, basename) {
       function blobCallback( blob ) {
           var videoURL = window.URL.createObjectURL(blob);
          
           downloadLink.href = videoURL;
           downloadLink.appendChild(document.createTextNode(basename));
    
           var name = basename + ((basename.indexOf(".webm")>0)?"": ".webm") ;
           downloadLink.setAttribute( "download", name);
           downloadLink.setAttribute( "name", name);
       }

       downloadLink.innerHTML = "";
       var mediaRecorder = easyrtc.recordToBlob(mediaStream, blobCallback);
       return mediaRecorder;
   };
}

return easyrtc;

}));