wvp.jslib 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. var WebPlayer = {
  2. states: {
  3. NA: 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. },
  16. players: [],
  17. WVPInitialize__deps: ["states","players"],
  18. WVPInitialize: function(autoplay, loop, muted) {
  19. var player = document.createElement('video');
  20. //player.crossOrigin = "anonymous";
  21. player.preload = "auto";
  22. //player.controls=true;
  23. player.autoplay = autoplay;
  24. player.loop = loop;
  25. player.muted = muted;
  26. player.playsInline=true;
  27. player.disablepictureinpicture=true;
  28. //document.body.appendChild(player);
  29. var playerState = {
  30. state: _states.NA,
  31. valueFloat: -1,
  32. valueLong: -1,
  33. valueString: undefined,
  34. };
  35. var playerData = {
  36. path: "",
  37. player: player,
  38. started: false,
  39. ready: false,
  40. playerState: {state: _states.NA, value: undefined},
  41. playerStates: []
  42. };
  43. _players.push(playerData);
  44. player.oncanplay = function () {
  45. playerData.ready = true;
  46. };
  47. player.onwaiting = function () {
  48. playerData.playerStates.push({state: _states.Buffering, valueFloat: 0, valueLong: -1, valueString: undefined});
  49. };
  50. player.onpause = function () {
  51. if (playerData.ready && !player.ended) {
  52. playerData.playerStates.push({state: _states.Paused, valueFloat: -1, valueLong: -1, valueString: undefined});
  53. }
  54. };
  55. player.onended = function () {
  56. playerData.playerStates.push({state: _states.EndReached, valueFloat: -1, valueLong: -1, valueString: undefined});
  57. };
  58. player.ontimeupdate = function() {
  59. if (playerData.ready) {
  60. playerData.playerStates.push({state: _states.PositionChanged, valueFloat: player.currentTime / player.duration, valueLong: -1, valueString: undefined});
  61. playerData.playerStates.push({state: _states.TimeChanged, valueFloat: -1, valueLong: player.currentTime * 1000, valueString: undefined});
  62. }
  63. };
  64. player.onerror = function (message) {
  65. playerData.playerStates.push({state: _states.EncounteredError, valueFloat: -1, valueLong: -1, valueString: undefined});
  66. var errormessage = "Undefined error";
  67. switch (this.error.code) {
  68. case 1:
  69. err = "Fetching process aborted by user";
  70. break;
  71. case 2:
  72. err = "Error occurred when downloading";
  73. break;
  74. case 3:
  75. err = "Error occurred when decoding";
  76. break;
  77. case 4:
  78. err = "Audio/Video not supported";
  79. break;
  80. }
  81. console.log(err + " (errorcode=" + this.error.code + ")");
  82. };
  83. return _players.length - 1;
  84. },
  85. WVPUpdateTexture__deps: ["players"],
  86. WVPUpdateTexture: function(indx, textureId)
  87. {
  88. GLctx.bindTexture(GLctx.TEXTURE_2D, GL.textures[textureId]);
  89. GLctx.pixelStorei(GLctx.UNPACK_FLIP_Y_WEBGL, true);
  90. GLctx.texParameteri(GLctx.TEXTURE_2D, GLctx.TEXTURE_WRAP_S, GLctx.CLAMP_TO_EDGE);
  91. GLctx.texParameteri(GLctx.TEXTURE_2D, GLctx.TEXTURE_WRAP_T, GLctx.CLAMP_TO_EDGE);
  92. GLctx.texParameteri(GLctx.TEXTURE_2D, GLctx.TEXTURE_MIN_FILTER, GLctx.LINEAR);
  93. GLctx.texParameteri(GLctx.TEXTURE_2D, GLctx.TEXTURE_MAG_FILTER, GLctx.LINEAR);
  94. GLctx.texSubImage2D(GLctx.TEXTURE_2D, 0, 0, 0, GLctx.RGBA, GLctx.UNSIGNED_BYTE, _players[indx].player);
  95. GLctx.pixelStorei(GLctx.UNPACK_FLIP_Y_WEBGL, false);
  96. },
  97. WVPSetDataSource__deps: ["players"],
  98. WVPSetDataSource: function (indx, path)
  99. {
  100. _players[indx].path = UTF8ToString(path);
  101. },
  102. WVPSourcePlay__deps: ["players"],
  103. WVPSourcePlay: function (indx)
  104. {
  105. if (!_players[indx].started) {
  106. _players[indx].player.src = _players[indx].path;
  107. _players[indx].player.load();
  108. _players[indx].started = true;
  109. }
  110. _players[indx].player.play();
  111. return true;
  112. },
  113. WVPSourcePause__deps: ["players"],
  114. WVPSourcePause: function (indx)
  115. {
  116. _players[indx].player.pause();
  117. },
  118. WVPSourceStop__deps: ["players"],
  119. WVPSourceStop: function(indx)
  120. {
  121. if(_players[indx].started)
  122. {
  123. _players[indx].ready=false;
  124. _players[indx].started=false;
  125. var pl = _players[indx].player;
  126. pl.pause();
  127. _players[indx].playerStates.push({state: _states.Stopped, valueFloat:-1, valueLong: -1, valueString: undefined});
  128. }
  129. },
  130. WVPSourceRelease__deps: ["players"],
  131. WVPSourceRelease: function(indx)
  132. {
  133. var player = _players[indx].player;
  134. if(player.parentNode!=null)
  135. {
  136. player.parentNode.removeChild(vid);
  137. }
  138. _players[indx].player=null;
  139. },
  140. WVPSourceIsPlaying__deps:["players"],
  141. WVPSourceIsPlaying: function(indx)
  142. {
  143. var player = _players[indx].player;
  144. return !(player.paused || player.ended || player.seeking);
  145. },
  146. WVPSourceIsReady__deps: ["players"],
  147. WVPSourceIsReady: function(indx)
  148. {
  149. return _players[indx].ready;
  150. },
  151. WVPSourceDuration__deps: ["players"],
  152. WVPSourceDuration: function(indx)
  153. {
  154. return _players[indx].player.duration;
  155. },
  156. WVPSourceIsMute__deps: ["players"],
  157. WVPSourceIsMute: function(indx)
  158. {
  159. return _players[indx].player.muted;
  160. },
  161. WVPSourceSetMute__deps: ["players"],
  162. WVPSourceSetMute: function(indx, mute)
  163. {
  164. _players[indx].player.muted = mute;
  165. },
  166. WVPSourceSetLoop__deps: ["players"],
  167. WVPSourceSetLoop: function(indx, loop)
  168. {
  169. return _players[indx].player.loop = loop;
  170. },
  171. WVPSourceWidth__deps: ["players"],
  172. WVPSourceWidth: function(indx)
  173. {
  174. return _players[indx].player.videoWidth;
  175. },
  176. WVPSourceHeight__deps: ["players"],
  177. WVPSourceHeight: function(indx)
  178. {
  179. return _players[indx].player.videoHeight;
  180. },
  181. WVPSourceFrameTime__deps: ["players"],
  182. WVPSourceFrameTime: function(indx)
  183. {
  184. return _players[indx].player.currentTime;
  185. },
  186. WVPSourceSetFrameTime__deps: ["players"],
  187. WVPSourceSetFrameTime: function(indx, time)
  188. {
  189. _players[indx].player.currentTime = time;
  190. },
  191. WVPGetPlayerState__deps: ["states","players"],
  192. WVPGetPlayerState: function(indx)
  193. {
  194. var states = _players[indx].playerStates;
  195. if(states.length>0)
  196. {
  197. _players[indx].playerState = states.shift();
  198. return _players[indx].playerState.state;
  199. }
  200. return _states.Empty;
  201. },
  202. };
  203. autoAddDeps(WebPlayer, "states");
  204. autoAddDeps(WebPlayer, "players");
  205. mergeInto(LibraryManager.library, WebPlayer);