/* * jQuery Clipchamp MJPEG Player Plugin v0.0.1 * https://github.com/clipchamp/jquery-clipchamp-mjpeg-player-plugin * * Plays back MJPEG files produced by the clipchamp * online video converter, online video compressor, and * webcam recorder. * * Copyright 2015 zfaas Pty Ltd (clipchamp.com) * https://clipchamp.com * https://zfaas.com * * Released under the MIT license */ (function($) { var DEFAULT_FPS = 15, DEFAULT_AUTOLOOP = true, JPEG_MAGIG_NUMBER = [0xff, 0xd8, 0xff], CONTINUE_PLAY = true, imageElement = null, nextFrameIndex = 0, jpegUrl, jpegFiles = null, autoloop = false, fps =1, ran=null, timerBOBA = null; var requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.msRequestAnimationFrame || window.setTimeout; function showNextFrame() { if (imageElement) { if (jpegUrl) { URL.revokeObjectURL(jpegUrl); } jpegUrl = URL.createObjectURL(jpegFiles[nextFrameIndex++]); imageElement.onload = function() { if (imageElement) { if (autoloop || nextFrameIndex < jpegFiles.length) { nextFrameIndex = (nextFrameIndex === jpegFiles.length) ? 0 : nextFrameIndex; if( ( nextFrameIndex % fps ) == 0 ) ran.value = nextFrameIndex; if( CONTINUE_PLAY ) { if( timerBOBA == null ){ timerBOBA = setInterval( //setTimeout( function() { requestAnimationFrame(showNextFrame); }, 1000/fps); }; } else { if( timerBOBA!= null ){ clearInterval( timerBOBA ); timerBOBA = null; }; }; } } }; imageElement.setAttribute('src', jpegUrl); }; }; function splitMJPEG(mjpegUrl, callback) { var xhr = new XMLHttpRequest(); xhr.open('GET', mjpegUrl, true); xhr.overrideMimeType('application/octet-stream'); xhr.responseType = 'arraybuffer'; xhr.onload = function(event) { var array = new Uint8Array(xhr.response), startIndex, jpegs = []; for (var i=0, ii=array.length; i0 && typeof startIndex === 'number') { jpegs.push(new Blob([array.subarray(startIndex, i)], {type: 'image/jpeg'})); } startIndex = i; } } callback(jpegs); }; xhr.send(); }; function playMJPEGInternal(wrapperElement, mjpegUrl, fp, aloop) { fps = (typeof fp === 'number') ? fp : DEFAULT_FPS; autoloop = (typeof aloop === 'boolean') ? aloop : DEFAULT_AUTOLOOP; var playbackFinished = false; imageElement = document.createElement('img'); var dWE = document.createElement('div'); dWE.setAttribute( 'id', 'wrpo_'+wrapperElement.id ); var W = wrapperElement.clientWidth; var H = ( W / 16 ) * 9; dWE.setAttribute( 'style', "display: block; width: 100%; height: "+H+"px; background-color: #000; background-image:url('/style/assets/loading_red_cube.gif');background-position: center;background-repeat: no-repeat;" ); dWE.appendChild(imageElement); imageElement.setAttribute('style', "width:100%;"); wrapperElement.appendChild( dWE ); //alert('SLADKIY_BUBALEKH:'+ W + " " + H ); //Show SLADKIY_BUBALEKH splitMJPEG(mjpegUrl, function( Files ) { jpegFiles = Files; //Hide SLADKIY_BUBALEKH -> or error! //alert( jpegFiles.length ); var ctlDIV = document.createElement('div'); ctlDIV.setAttribute( 'id', 'ctl_'+wrapperElement.id ); ctlDIV.setAttribute( 'class', 'player_controls' ); var btn = document.createElement('div'); btn.setAttribute( 'id', 'btn_play_stop'+wrapperElement.id ); btn.setAttribute( 'onclick', "stop_mjpeg('btn_play_stop"+wrapperElement.id+"');" ); btn.classList.add('pl_ctl_btn'); btn.classList.add('stop_button'); ctlDIV.appendChild(btn); ran = document.createElement('input'); ran.setAttribute( 'type', "range" ); ran.setAttribute( 'id', 'range_'+wrapperElement.id ); ran.setAttribute( 'step','1'); ran.setAttribute( 'min','0'); ran.setAttribute( 'max', jpegFiles.length ); ran.setAttribute( 'value','0'); ran.setAttribute( 'style','margin-left: 10px;width: 80%;' ); ran.setAttribute( 'oninput', "setpos_mjpeg(this.value);" ); ran.setAttribute( 'onchange', "setpos_s_mjpeg(this.value);" ); ctlDIV.appendChild(ran); btn = document.createElement('div'); btn.setAttribute( 'id', 'btn_loop'+wrapperElement.id ); //btn.setAttribute( 'onclick', "stop_loop_mjpeg('btn_loop"+wrapperElement.id+"');" ); btn.setAttribute( 'onclick', "downloadVideoFile( videoFileURI, videoFileFPS, videoFileDownloadTarget );" ); btn.classList.add('pl_ctl_btn'); btn.classList.add('download_button'); ctlDIV.appendChild(btn); dWE.appendChild(ctlDIV); if (jpegFiles.length > 0) { if( CONTINUE_PLAY ) { if( timerBOBA == null ){ timerBOBA = setInterval( //setTimeout( function() { requestAnimationFrame(showNextFrame); }, 1000/fps); }; } else { if( timerBOBA!= null ){ clearInterval( timerBOBA ); timerBOBA = null; }; }; //if( CONTINUE_PLAY ) setTimeout(function() { // requestAnimationFrame(showNextFrame); //}, 1000/fps); } else { alert('Error Loading Video'); }; }); return { finish: function() { if (imageElement) { imageElement.src = ''; wrapperElement.removeChild(imageElement); imageElement = undefined; } }, pause: function(){ CONTINUE_PLAY = false; }, play: function( fromBegin ){ if(typeof fromBegin === 'number') { nextFrameIndex = fromBegin; nextFrameIndex = (nextFrameIndex === jpegFiles.length) ? 0 : nextFrameIndex; }; CONTINUE_PLAY = true; setTimeout(function() { requestAnimationFrame( showNextFrame); }, 1000/fps); }, alertInfo: function(){ alert( jpegFiles.length ); } }; }; // optionally make available as jQuery plugin if (typeof $ === 'function') { $.fn.clipchamp_mjpeg_player = function(mjpegUrl, fps, autoloop, callback) { if (typeof mjpegUrl === 'string') { if (typeof callback === 'function') { return this.each(function() { callback($(this)[0], playMJPEGInternal($(this)[0], mjpegUrl, fps, autoloop)); }); } else { throw new Error('Callback must be given and must be a function'); } } else { throw new Error('MJPEG URL must be a string'); } } } // optionally provide AMD module definition if (typeof define === 'function') { define('jquery.clipchamp.mjpeg.player', [], function() { return { playMJPEG: function(wrapperElement, mjpegUrl, fps, autoloop) { if (wrapperElement instanceof Element) { if (typeof mjpegUrl === 'string') { return playMJPEGInternal(wrapperElement, mjpegUrl, fps, autoloop); } else { throw new Error('MJPEG URL must be a string'); } } else { throw new Error('No parent element was given'); } } }; }); } } ) (typeof jQuery === 'function' ? jQuery : undefined);