UNPKG

zdpjs_webrtc

Version:
348 lines (301 loc) 13.9 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>EasyRTC Documentation</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> <link href="../assets/easyrtc-docs-jsdoc-styles-1.css" media="all" rel="stylesheet" /> </head> <body> <header class="template"> <div class="wrapper-content"> <a href="../index.html"><img src="../assets/easyrtc-logo.png" alt="EasyRTC Documentation"></a> <h1 title="EasyRTC Documentation">Documentation</h1> </div> </header> <div class="wrapper-docs"> <nav> <h2><a href="../index.html">Docs Home</a></h2> <h3>EasyRTC Guides</h3> <ul> <li><a href="../easyrtc_faq.html">FAQ</a></li> <li><a href="../easyrtc_gettingStarted.html">Getting Started</a></li> <li><a href="../easyrtc_server_install.html">EasyRTC Server: Installation</a></li> <li><a href="../easyrtc_client_tutorial.html">EasyRTC Framework Tutorial</a></li> <li><a href="../easyrtc_rooms.html">Rooms</a></li> <li><a href="../easyrtc_server_configuration.html">EasyRTC Server: Configuration</a></li> <li><a href="../easyrtc_server_events.html">EasyRTC Server: Events</a></li> <li><a href="../easyrtc_server_ice.html">EasyRTC Server: ICE Configuration</a></li> <li><a href="../easyrtc_with_other_servers.html">Using Alongside Other Servers</a></li> <li><a href="../easyrtc_server_ssl.html">EasyRTC Server: Using SSL</a></li> <li><a href="../easyrtc_webrtc_problems.html">WebRTC Problems and Possible Fixes</a></li> <li><a href="../easyrtc_upcoming_features.html">Upcoming Features</a></li> <li><a href="../easyrtc_changelog.html">Change Log</a></li> </ul> <h3>EasyRTC Client Classes</h3> <ul> <li><a href="../client-api/Easyrtc.html">Easyrtc</a></li> <li><a href="../client-api/Easyrtc_App.html">Easyrtc_App</a></li> <li><a href="../client-api/Easyrtc_ft.html">Easyrtc_ft</a></li> <li><a href="../client-api/Easyrtc_IframeCapture.html">Easyrtc_IframeCapture</a></li> <li><a href="../client-api/Easyrtc_No_IframeCapture.html">Easyrtc_No_IframeCapture</a></li> <li><a href="../client-api/Easyrtc_Rates.html">Easyrtc_Rates</a></li> <li><a href="../client-api/Easyrtc_Recorder.html">Easyrtc_Recorder</a></li> </ul> <h3>EasyRTC Server Modules</h3> <ul> <li><a href="../server-api/module-easyrtc_default_event_listeners.html">easyrtc_default_event_listeners</a></li> <li><a href="../server-api/module-easyrtc_default_options.html">easyrtc_default_options</a></li> <li><a href="../server-api/module-easyrtc_private_obj.html">easyrtc_private_obj</a></li> <li><a href="../server-api/module-easyrtc_public_obj.html">easyrtc_public_obj</a></li> <li><a href="../server-api/module-easyrtc_util.html">easyrtc_util</a></li> <li><a href="../server-api/module-general_util.html">general_util</a></li> </ul> <h3>EasyRTC Server Classes</h3> <ul> <li><a href="../server-api/module-easyrtc_default_event_listeners-eventListener.html">eventListener</a></li> <li><a href="../server-api/module-easyrtc_public_obj-pub.html">pub</a></li> <li><a href="../server-api/module-easyrtc_public_obj-pub.events.html">events</a></li> <li><a href="../server-api/module-easyrtc_public_obj-pub.util.html">util</a></li> <li><a href="../server-api/module-easyrtc_util-eu.html">eu</a></li> <li><a href="../server-api/module-general_util-g.html">g</a></li> <li><a href="../server-api/pub.appObj.html">appObj</a></li> <li><a href="../server-api/pub.appObj.connectionObj.html">connectionObj</a></li> <li><a href="../server-api/pub.appObj.connectionObj.connectionRoomObj.html">connectionRoomObj</a></li> <li><a href="../server-api/pub.appObj.roomObj.html">roomObj</a></li> <li><a href="../server-api/pub.appObj.sessionObj.html">sessionObj</a></li> </ul> </nav> <div class="easyrtc-docs-content"> <div id="main"> <h1 class="page-title">Source: labs/easyrtc_recorder.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>/* 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' &amp;&amp; define.amd) { //RequireJS (AMD) build system define(['easyrtc'], factory); } else if (typeof module === 'object' &amp;&amp; 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) { /** * Provides methods for building MediaStream recorders. * @class Easyrtc_Recorder */ /** * Determines if recording is supported by the browser. * @function * @memberOf Easyrtc_Recorder * @returns true if recording is supported. */ easyrtc.supportsRecording = function() { return (typeof MediaRecorder !== "undefined" &amp;&amp; navigator.getUserMedia ); }; /** * Check if a particular codec can be used for recording. * @function * @memberOf Easyrtc_Recorder * @param {String} codecName, either "vp8" or "vp9 or "h264" * @returns true if the type can be used, or if the browser doesn't * support a method to find out. */ easyrtc.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. * @function * @memberOf Easyrtc_Recorder * @param {String} codecName, either "vp8" or "vp9 or "h264" * @returns true if the type can be used. */ easyrtc.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 media stream to it. * @function * @memberOf Easyrtc_Recorder * @param {HTMLMediaStream} mediaStream * @returns a recorder object or null if recording not supported. */ function startRecording( mediaStream) { if( !easyrtc.supportsRecording ) { console.log("recording not supported by your browser"); return null; } var mediaRecorder = new MediaRecorder(mediaStream, {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 ondataavailable * 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. * @function * @memberOf Easyrtc_Recorder * @param {HTMLMediaStream} mediaStream a local or remote media stream. * @param {Function} dataCallback a function to receive the webm data from. */ easyrtc.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. * @function * @memberOf Easyrtc_Recorder * @param {HTMLMediaStream} 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. **/ easyrtc.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. * @function * @memberOf Easyrtc_Recorder * @param {HTMLMediaStream} 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. **/ easyrtc.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; })); </code></pre> </article> </section> </div> <nav class="hide"><h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Easyrtc.html">Easyrtc</a></li><li><a href="Easyrtc_App.html">Easyrtc_App</a></li><li><a href="Easyrtc_ft.html">Easyrtc_ft</a></li><li><a href="Easyrtc_IframeCapture.html">Easyrtc_IframeCapture</a></li><li><a href="Easyrtc_No_IframeCapture.html">Easyrtc_No_IframeCapture</a></li><li><a href="Easyrtc_Rates.html">Easyrtc_Rates</a></li><li><a href="Easyrtc_Recorder.html">Easyrtc_Recorder</a></li></ul></nav> </div> </div> <footer class="template"> <div class="wrapper-content"> <p>Copyright &copy;2016 EasyRTC</p> </div> </footer> <!-- <script>prettyPrint();</script> --> <!-- <script src="scripts/linenumber.js"></script> --> </body> </html>