UniversalMediaPlayer.jslib 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. var UniversalMediaPlayer = {
  2. states: {
  3. Empty: 0,
  4. Opening: 1,
  5. Buffering: 2,
  6. ImageReady: 3,
  7. Prepared: 4,
  8. Playing: 5,
  9. Paused: 6,
  10. Stopped: 7,
  11. EndReached: 8,
  12. EncounteredError: 9,
  13. TimeChanged: 10,
  14. PositionChanged: 11,
  15. SnapshotTaken: 12,
  16. },
  17. players: [],
  18. UMPNativeInit__deps: ["states", "players"],
  19. UMPNativeInit: function () {
  20. var player = document.createElement("video");
  21. player.crossOrigin = "anonymous";
  22. //player.setAttribute('crossOrigin', 'anonymous');
  23. player.preload="auto";
  24. var playerState = {
  25. state: _states.Empty,
  26. valueFloat: -1,
  27. valueLong: -1,
  28. valueString: undefined,
  29. };
  30. var playerData = {
  31. path: "",
  32. player: player,
  33. started: false,
  34. ready: false,
  35. playerState: {state: _states.Empty, value: undefined},
  36. playerStates: []
  37. };
  38. _players.push(playerData);
  39. player.oncanplay = function () {
  40. playerData.ready = true;
  41. };
  42. player.onwaiting = function () {
  43. playerData.playerStates.push({state: _states.Buffering, valueFloat: 0, valueLong: -1, valueString: undefined});
  44. };
  45. player.onpause = function () {
  46. if (playerData.ready && !player.ended) {
  47. playerData.playerStates.push({state: _states.Paused, valueFloat: -1, valueLong: -1, valueString: undefined});
  48. }
  49. };
  50. player.onended = function () {
  51. playerData.playerStates.push({state: _states.EndReached, valueFloat: -1, valueLong: -1, valueString: undefined});
  52. };
  53. player.ontimeupdate = function() {
  54. if (playerData.ready) {
  55. playerData.playerStates.push({state: _states.PositionChanged, valueFloat: player.currentTime / player.duration, valueLong: -1, valueString: undefined});
  56. playerData.playerStates.push({state: _states.TimeChanged, valueFloat: -1, valueLong: player.currentTime * 1000, valueString: undefined});
  57. }
  58. };
  59. player.onerror = function (message) {
  60. playerData.playerStates.push({state: _states.EncounteredError, valueFloat: -1, valueLong: -1, valueString: undefined});
  61. var errormessage = "Undefined error";
  62. switch (this.error.code) {
  63. case 1:
  64. err = "Fetching process aborted by user";
  65. break;
  66. case 2:
  67. err = "Error occurred when downloading";
  68. break;
  69. case 3:
  70. err = "Error occurred when decoding";
  71. break;
  72. case 4:
  73. err = "Audio/Video not supported";
  74. break;
  75. }
  76. console.log(err + " (errorcode=" + this.error.code + ")");
  77. };
  78. return _players.length - 1;
  79. },
  80. UMPNativeInitPlayer__deps: ["players"],
  81. UMPNativeInitPlayer: function (index, options) {
  82. },
  83. UMPNativeUpdateTexture__deps: ["players"],
  84. UMPNativeUpdateTexture: function (index, texture) {
  85. GLctx.bindTexture(GLctx.TEXTURE_2D, GL.textures[texture]);
  86. GLctx.pixelStorei(GLctx.UNPACK_FLIP_Y_WEBGL, true);
  87. GLctx.texParameteri(GLctx.TEXTURE_2D, GLctx.TEXTURE_WRAP_S, GLctx.CLAMP_TO_EDGE);
  88. GLctx.texParameteri(GLctx.TEXTURE_2D, GLctx.TEXTURE_WRAP_T, GLctx.CLAMP_TO_EDGE);
  89. GLctx.texParameteri(GLctx.TEXTURE_2D, GLctx.TEXTURE_MIN_FILTER, GLctx.LINEAR);
  90. GLctx.texParameteri(GLctx.TEXTURE_2D, GLctx.TEXTURE_MAG_FILTER, GLctx.LINEAR);
  91. GLctx.texSubImage2D(GLctx.TEXTURE_2D, 0, 0, 0, GLctx.RGBA, GLctx.UNSIGNED_BYTE, _players[index].player);
  92. GLctx.pixelStorei(GLctx.UNPACK_FLIP_Y_WEBGL, false);
  93. },
  94. UMPSetDataSource__deps: ["players"],
  95. UMPSetDataSource: function (index, path) {
  96. _players[index].path = Pointer_stringify(path);
  97. },
  98. UMPPlay__deps: ["players"],
  99. UMPPlay: function (index) {
  100. if (!_players[index].started) {
  101. _players[index].player.src = _players[index].path;
  102. _players[index].player.load();
  103. _players[index].started = true;
  104. }
  105. _players[index].player.play();
  106. return true;
  107. },
  108. UMPPause__deps: ["players"],
  109. UMPPause: function (index) {
  110. _players[index].player.pause();
  111. },
  112. UMPStop__deps: ["players"],
  113. UMPStop: function (index) {
  114. if (_players[index].started) {
  115. _players[index].ready = false;
  116. _players[index].started = false;
  117. var player = _players[index].player;
  118. player.pause();
  119. _players[index].playerStates.push({state: _states.Stopped, valueFloat: -1, valueLong: -1, valueString: undefined});
  120. }
  121. },
  122. UMPRelease__deps: ["players"],
  123. UMPRelease: function (index) {
  124. var player = _players[index].player;
  125. if (player.parentNode != null)
  126. player.parentNode.removeChild(vid);
  127. _players[index].player = null;
  128. },
  129. UMPIsPlaying__deps: ["players"],
  130. UMPIsPlaying: function (index) {
  131. var player = _players[index].player;
  132. return !(player.paused || player.ended || player.seeking || player.readyState < player.HAVE_FUTURE_DATA);
  133. },
  134. UMPIsReady__deps: ["players"],
  135. UMPIsReady: function (index) {
  136. return _players[index].ready;
  137. },
  138. UMPGetLength__deps: ["players"],
  139. UMPGetLength: function (index) {
  140. return _players[index].player.duration * 1000;
  141. },
  142. UMPGetTime__deps: ["players"],
  143. UMPGetTime: function (index) {
  144. return _players[index].player.currentTime * 1000;
  145. },
  146. UMPSetTime__deps: ["players"],
  147. UMPSetTime: function (index, time) {
  148. _players[index].player.currentTime = time * 1000;
  149. },
  150. UMPGetPosition__deps: ["players"],
  151. UMPGetPosition: function (index) {
  152. var player = _players[index].player;
  153. return player.currentTime / player.duration;
  154. },
  155. UMPSetPosition__deps: ["players"],
  156. UMPSetPosition: function (index, position) {
  157. var player = _players[index].player;
  158. player.currentTime = position * player.duration;
  159. },
  160. UMPGetRate__deps: ["players"],
  161. UMPGetRate: function (index) {
  162. return _players[index].player.playbackRate;
  163. },
  164. UMPSetRate__deps: ["players"],
  165. UMPSetRate: function (index, rate) {
  166. _players[index].player.playbackRate = rate;
  167. },
  168. UMPGetVolume__deps: ["players"],
  169. UMPGetVolume: function (index) {
  170. return _players[index].player.volume * 100;
  171. },
  172. UMPSetVolume__deps: ["players"],
  173. UMPSetVolume: function (index, volume) {
  174. _players[index].player.volume = volume / 100;
  175. },
  176. UMPGetMute__deps: ["players"],
  177. UMPGetMute: function (index) {
  178. return _players[index].player.muted;
  179. },
  180. UMPSetMute__deps: ["players"],
  181. UMPSetMute: function (index, mute) {
  182. _players[index].player.muted = mute;
  183. },
  184. UMPVideoWidth__deps: ["players"],
  185. UMPVideoWidth: function (index) {
  186. return _players[index].player.videoWidth;
  187. },
  188. UMPVideoHeight__deps: ["players"],
  189. UMPVideoHeight: function (index) {
  190. return _players[index].player.videoHeight;
  191. },
  192. UMPVideoFrameCount__deps: ["players"],
  193. UMPVideoFrameCount: function (index) {
  194. var player = _players[index].player;
  195. var frameCount = 0;
  196. if (_players[index].ready) {
  197. if (player.webkitDecodedFrameCount) {
  198. frameCount = player.webkitDecodedFrameCount;
  199. }
  200. if (player.mozDecodedFrames) {
  201. frameCount = player.mozDecodedFrames;
  202. }
  203. }
  204. return frameCount;
  205. },
  206. UMPGetState__deps: ["states", "players"],
  207. UMPGetState: function (index) {
  208. var playerStates = _players[index].playerStates;
  209. if (playerStates.length > 0) {
  210. _players[index].playerState = playerStates.shift();
  211. return _players[index].playerState.state;
  212. }
  213. return _states.Empty;
  214. },
  215. UMPGetStateFloatValue__deps: ["states", "players"],
  216. UMPGetStateFloatValue: function (index) {
  217. if (_players[index].playerState != undefined) {
  218. return _players[index].playerState.valueFloat;
  219. }
  220. return -1;
  221. },
  222. UMPGetStateLongValue__deps: ["states", "players"],
  223. UMPGetStateLongValue: function (index) {
  224. if (_players[index].playerState != undefined) {
  225. return _players[index].playerState.valueLong;
  226. }
  227. return -1;
  228. },
  229. UMPGetStateStringValue__deps: ["states", "players"],
  230. UMPGetStateStringValue: function (index) {
  231. if (_players[index].playerState != undefined) {
  232. return _players[index].playerState.valueString;
  233. }
  234. return undefined;
  235. },
  236. };
  237. autoAddDeps(UniversalMediaPlayer, 'states');
  238. autoAddDeps(UniversalMediaPlayer, 'players');
  239. mergeInto(LibraryManager.library, UniversalMediaPlayer);