player.js 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. JSMpeg.Player = (function(){ "use strict";
  2. var Player = function(url, options) {
  3. this.options = options || {};
  4. if (options.source) {
  5. this.source = new options.source(url, options);
  6. options.streaming = !!this.source.streaming;
  7. }
  8. else if (url.match(/^wss?:\/\//)) {
  9. this.source = new JSMpeg.Source.WebSocket(url, options);
  10. options.streaming = true;
  11. }
  12. else if (options.progressive !== false) {
  13. this.source = new JSMpeg.Source.AjaxProgressive(url, options);
  14. options.streaming = false;
  15. }
  16. else {
  17. this.source = new JSMpeg.Source.Ajax(url, options);
  18. options.streaming = false;
  19. }
  20. this.maxAudioLag = options.maxAudioLag || 0.25;
  21. this.loop = options.loop !== false;
  22. this.autoplay = !!options.autoplay || options.streaming;
  23. this.demuxer = new JSMpeg.Demuxer.TS(options);
  24. this.source.connect(this.demuxer);
  25. if (!options.disableWebAssembly && JSMpeg.WASMModule.IsSupported()) {
  26. this.wasmModule = new JSMpeg.WASMModule();
  27. options.wasmModule =this.wasmModule;
  28. }
  29. if (options.video !== false) {
  30. this.video = options.wasmModule
  31. ? new JSMpeg.Decoder.MPEG1VideoWASM(options)
  32. : new JSMpeg.Decoder.MPEG1Video(options);
  33. this.renderer = !options.disableGl && JSMpeg.Renderer.WebGL.IsSupported()
  34. ? new JSMpeg.Renderer.WebGL(options)
  35. : new JSMpeg.Renderer.Canvas2D(options);
  36. this.demuxer.connect(JSMpeg.Demuxer.TS.STREAM.VIDEO_1, this.video);
  37. this.video.connect(this.renderer);
  38. }
  39. if (options.audio !== false && JSMpeg.AudioOutput.WebAudio.IsSupported()) {
  40. this.audio = options.wasmModule
  41. ? new JSMpeg.Decoder.MP2AudioWASM(options)
  42. : new JSMpeg.Decoder.MP2Audio(options);
  43. this.audioOut = new JSMpeg.AudioOutput.WebAudio(options);
  44. this.demuxer.connect(JSMpeg.Demuxer.TS.STREAM.AUDIO_1, this.audio);
  45. this.audio.connect(this.audioOut);
  46. }
  47. Object.defineProperty(this, 'currentTime', {
  48. get: this.getCurrentTime,
  49. set: this.setCurrentTime
  50. });
  51. Object.defineProperty(this, 'volume', {
  52. get: this.getVolume,
  53. set: this.setVolume
  54. });
  55. this.unpauseOnShow = false;
  56. if (options.pauseWhenHidden !== false) {
  57. document.addEventListener('visibilitychange', this.showHide.bind(this));
  58. }
  59. // If we have WebAssembly support, wait until the module is compiled before
  60. // loading the source. Otherwise the decoders won't know what to do with
  61. // the source data.
  62. if (this.wasmModule) {
  63. if (JSMpeg.WASM_BINARY_INLINED) {
  64. var wasm = JSMpeg.Base64ToArrayBuffer(JSMpeg.WASM_BINARY_INLINED);
  65. this.wasmModule.loadFromBuffer(wasm, this.startLoading.bind(this));
  66. }
  67. else {
  68. this.wasmModule.loadFromFile('jsmpeg.wasm', this.startLoading.bind(this));
  69. }
  70. }
  71. else {
  72. this.startLoading();
  73. }
  74. };
  75. Player.prototype.startLoading = function() {
  76. this.source.start();
  77. if (this.autoplay) {
  78. this.play();
  79. }
  80. };
  81. Player.prototype.showHide = function(ev) {
  82. if (document.visibilityState === 'hidden') {
  83. this.unpauseOnShow = this.wantsToPlay;
  84. this.pause();
  85. }
  86. else if (this.unpauseOnShow) {
  87. this.play();
  88. }
  89. };
  90. Player.prototype.play = function(ev) {
  91. this.animationId = requestAnimationFrame(this.update.bind(this));
  92. this.wantsToPlay = true;
  93. };
  94. Player.prototype.pause = function(ev) {
  95. cancelAnimationFrame(this.animationId);
  96. this.wantsToPlay = false;
  97. this.isPlaying = false;
  98. if (this.audio && this.audio.canPlay) {
  99. // Seek to the currentTime again - audio may already be enqueued a bit
  100. // further, so we have to rewind it.
  101. this.audioOut.stop();
  102. this.seek(this.currentTime);
  103. }
  104. };
  105. Player.prototype.getVolume = function() {
  106. return this.audioOut ? this.audioOut.volume : 0;
  107. };
  108. Player.prototype.setVolume = function(volume) {
  109. if (this.audioOut) {
  110. this.audioOut.volume = volume;
  111. }
  112. };
  113. Player.prototype.stop = function(ev) {
  114. this.pause();
  115. this.seek(0);
  116. if (this.video && this.options.decodeFirstFrame !== false) {
  117. this.video.decode();
  118. }
  119. };
  120. Player.prototype.destroy = function() {
  121. this.pause();
  122. this.source.destroy();
  123. this.video && this.video.destroy();
  124. this.renderer && this.renderer.destroy();
  125. this.audio && this.audio.destroy();
  126. this.audioOut && this.audioOut.destroy();
  127. };
  128. Player.prototype.seek = function(time) {
  129. var startOffset = this.audio && this.audio.canPlay
  130. ? this.audio.startTime
  131. : this.video.startTime;
  132. if (this.video) {
  133. this.video.seek(time + startOffset);
  134. }
  135. if (this.audio) {
  136. this.audio.seek(time + startOffset);
  137. }
  138. this.startTime = JSMpeg.Now() - time;
  139. };
  140. Player.prototype.getCurrentTime = function() {
  141. return this.audio && this.audio.canPlay
  142. ? this.audio.currentTime - this.audio.startTime
  143. : this.video.currentTime - this.video.startTime;
  144. };
  145. Player.prototype.setCurrentTime = function(time) {
  146. this.seek(time);
  147. };
  148. Player.prototype.update = function() {
  149. this.animationId = requestAnimationFrame(this.update.bind(this));
  150. if (!this.source.established) {
  151. if (this.renderer) {
  152. this.renderer.renderProgress(this.source.progress);
  153. }
  154. return;
  155. }
  156. if (!this.isPlaying) {
  157. this.isPlaying = true;
  158. this.startTime = JSMpeg.Now() - this.currentTime;
  159. }
  160. if (this.options.streaming) {
  161. this.updateForStreaming();
  162. }
  163. else {
  164. this.updateForStaticFile();
  165. }
  166. };
  167. Player.prototype.updateForStreaming = function() {
  168. // When streaming, immediately decode everything we have buffered up until
  169. // now to minimize playback latency.
  170. if (this.video) {
  171. this.video.decode();
  172. }
  173. if (this.audio) {
  174. var decoded = false;
  175. do {
  176. // If there's a lot of audio enqueued already, disable output and
  177. // catch up with the encoding.
  178. if (this.audioOut.enqueuedTime > this.maxAudioLag) {
  179. this.audioOut.resetEnqueuedTime();
  180. this.audioOut.enabled = false;
  181. }
  182. decoded = this.audio.decode();
  183. } while (decoded);
  184. this.audioOut.enabled = true;
  185. }
  186. };
  187. Player.prototype.updateForStaticFile = function() {
  188. var notEnoughData = false,
  189. headroom = 0;
  190. // If we have an audio track, we always try to sync the video to the audio.
  191. // Gaps and discontinuities are far more percetable in audio than in video.
  192. if (this.audio && this.audio.canPlay) {
  193. // Do we have to decode and enqueue some more audio data?
  194. while (
  195. !notEnoughData &&
  196. this.audio.decodedTime - this.audio.currentTime < 0.25
  197. ) {
  198. notEnoughData = !this.audio.decode();
  199. }
  200. // Sync video to audio
  201. if (this.video && this.video.currentTime < this.audio.currentTime) {
  202. notEnoughData = !this.video.decode();
  203. }
  204. headroom = this.demuxer.currentTime - this.audio.currentTime;
  205. }
  206. else if (this.video) {
  207. // Video only - sync it to player's wallclock
  208. var targetTime = (JSMpeg.Now() - this.startTime) + this.video.startTime,
  209. lateTime = targetTime - this.video.currentTime,
  210. frameTime = 1/this.video.frameRate;
  211. if (this.video && lateTime > 0) {
  212. // If the video is too far behind (>2 frames), simply reset the
  213. // target time to the next frame instead of trying to catch up.
  214. if (lateTime > frameTime * 2) {
  215. this.startTime += lateTime;
  216. }
  217. notEnoughData = !this.video.decode();
  218. }
  219. headroom = this.demuxer.currentTime - targetTime;
  220. }
  221. // Notify the source of the playhead headroom, so it can decide whether to
  222. // continue loading further data.
  223. this.source.resume(headroom);
  224. // If we failed to decode and the source is complete, it means we reached
  225. // the end of our data. We may want to loop.
  226. if (notEnoughData && this.source.completed) {
  227. if (this.loop) {
  228. this.seek(0);
  229. }
  230. else {
  231. this.pause();
  232. }
  233. }
  234. };
  235. return Player;
  236. })();