Web Interface For Lite Argus Control modules
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
3.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*!
  2. * jQuery Clipchamp MJPEG Player Plugin v0.0.1
  3. * https://github.com/clipchamp/jquery-clipchamp-mjpeg-player-plugin
  4. *
  5. * Plays back MJPEG files produced by the clipchamp
  6. * online video converter, online video compressor, and
  7. * webcam recorder.
  8. *
  9. * Copyright 2015 zfaas Pty Ltd (clipchamp.com)
  10. * https://clipchamp.com
  11. * https://zfaas.com
  12. *
  13. * Released under the MIT license
  14. */
  15. (function($) {
  16. var DEFAULT_FPS = 24,
  17. DEFAULT_AUTOLOOP = true,
  18. JPEG_MAGIG_NUMBER = [0xff, 0xd8, 0xff];
  19. var requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.msRequestAnimationFrame || window.setTimeout;
  20. function splitMJPEG(mjpegUrl, callback) {
  21. var xhr = new XMLHttpRequest();
  22. xhr.open('GET', mjpegUrl, true);
  23. xhr.overrideMimeType('application/octet-stream');
  24. xhr.responseType = 'arraybuffer';
  25. xhr.onload = function(event) {
  26. var array = new Uint8Array(xhr.response),
  27. startIndex,
  28. jpegs = [];
  29. for (var i=0, ii=array.length; i<ii; ++i) {
  30. if (array[i] === JPEG_MAGIG_NUMBER[0] && array[i+1] === JPEG_MAGIG_NUMBER[1] && array[i+2] === JPEG_MAGIG_NUMBER[2]) {
  31. if (i>0 && typeof startIndex === 'number') {
  32. jpegs.push(new Blob([array.subarray(startIndex, i)], {type: 'image/jpeg'}));
  33. }
  34. startIndex = i;
  35. }
  36. }
  37. callback(jpegs);
  38. };
  39. xhr.send();
  40. };
  41. function playMJPEGInternal(wrapperElement, mjpegUrl, fps, autoloop) {
  42. fps = (typeof fps === 'number') ? fps : DEFAULT_FPS;
  43. autoloop = (typeof autoloop === 'boolean') ? autoloop : DEFAULT_AUTOLOOP;
  44. var playbackFinished = false,
  45. imageElement = document.createElement('img'),
  46. jpegUrl;
  47. imageElement.setAttribute('style', 'width:100%;');
  48. wrapperElement.appendChild(imageElement);
  49. splitMJPEG(mjpegUrl, function(jpegFiles) {
  50. if (jpegFiles.length > 0) {
  51. var nextFrameIndex = 0;
  52. function showNextFrame() {
  53. if (imageElement) {
  54. if (jpegUrl) {
  55. URL.revokeObjectURL(jpegUrl);
  56. }
  57. jpegUrl = URL.createObjectURL(jpegFiles[nextFrameIndex++]);
  58. imageElement.onload = function() {
  59. if (imageElement) {
  60. if (autoloop || nextFrameIndex < jpegFiles.length) {
  61. nextFrameIndex = (nextFrameIndex === jpegFiles.length) ? 0 : nextFrameIndex;
  62. setTimeout(function() {
  63. requestAnimationFrame(showNextFrame);
  64. }, 1000/fps);
  65. }
  66. }
  67. };
  68. imageElement.setAttribute('src', jpegUrl);
  69. }
  70. };
  71. setTimeout(function() {
  72. requestAnimationFrame(showNextFrame);
  73. }, 1000/fps);
  74. }
  75. });
  76. return {
  77. finish: function() {
  78. if (imageElement) {
  79. imageElement.src = '';
  80. wrapperElement.removeChild(imageElement);
  81. imageElement = undefined;
  82. }
  83. }
  84. };
  85. };
  86. // optionally make available as jQuery plugin
  87. if (typeof $ === 'function') {
  88. $.fn.clipchamp_mjpeg_player = function(mjpegUrl, fps, autoloop, callback) {
  89. if (typeof mjpegUrl === 'string') {
  90. if (typeof callback === 'function') {
  91. return this.each(function() {
  92. callback($(this)[0], playMJPEGInternal($(this)[0], mjpegUrl, fps, autoloop));
  93. });
  94. } else {
  95. throw new Error('Callback must be given and must be a function');
  96. }
  97. } else {
  98. throw new Error('MJPEG URL must be a string');
  99. }
  100. }
  101. }
  102. // optionally provide AMD module definition
  103. if (typeof define === 'function') {
  104. define('jquery.clipchamp.mjpeg.player', [], function() {
  105. return {
  106. playMJPEG: function(wrapperElement, mjpegUrl, fps, autoloop) {
  107. if (wrapperElement instanceof Element) {
  108. if (typeof mjpegUrl === 'string') {
  109. return playMJPEGInternal(wrapperElement, mjpegUrl, fps, autoloop);
  110. } else {
  111. throw new Error('MJPEG URL must be a string');
  112. }
  113. } else {
  114. throw new Error('No parent element was given');
  115. }
  116. }
  117. };
  118. });
  119. }
  120. } )
  121. (typeof jQuery === 'function' ? jQuery : undefined);