h5spbcontrol.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**
  2. * Interface with h5s websocket pb ctonrol API
  3. * @constructor
  4. * @param
  5. var pbconf1 = {
  6. begintime: '2019-03-23T120101+08',//{string} begintime 0 for fileplayback
  7. endtime: '2019-03-23T150101+08',//{string} endtime 0 for fileplayback
  8. serverpb: 'true', //'true' or 'false' playback from h5stream record, default false
  9. filename: 'token1.mp4', // file name need to playback (begintime == 0 & endtime == 0 and serverpb is true)
  10. callback: PlaybackCB, //{function}(event(string), userdata(object))
  11. userdata: user data // user data
  12. };
  13. var conf = {
  14. protocol: window.location.protocol, // {string} - 'http:' or 'https:'
  15. host: window.location.host, //{string} - 'localhost:8080'
  16. rootpath:window.location.pathname, // {string} - path of the app running
  17. token:'token1', // {string} - token of stream
  18. streamprofile: 'main', // {string} - stream profile, main/sub or other predefine transcoding profile
  19. pbconf: pbconf1, //This is optional, if no pbconf, this will be live.
  20. session:'c1782caf-b670-42d8-ba90-2244d0b0ee83', //{string} - session got from login
  21. consolelog: 'true' // 'true' or 'false' enable/disable console.log
  22. };
  23. */
  24. function H5sPbControl(conf)
  25. {
  26. this.wsSocket;
  27. this.keepaliveTimerId;
  28. this.bDisConnected = false;
  29. this._debug = true;
  30. if (conf.consolelog !== undefined)
  31. {
  32. if (conf.consolelog === 'false')
  33. {
  34. this._debug = false;
  35. }
  36. }
  37. this._conf = conf;
  38. if(this._debug === true) console.log("[PBCONTROL] Websocket Conf:", conf);
  39. this._pbconf = conf.pbconf;
  40. this._token = conf.token;
  41. }
  42. H5sPbControl.prototype.H5SWebSocketClient = function(h5spath)
  43. {
  44. var socket;
  45. if(this._debug === true) console.log("[PBCONTROL] H5SWebSocketClient");
  46. try {
  47. //alert(this._conf.protocol);
  48. if (this._conf.protocol == "http:")
  49. {
  50. if (typeof MozWebSocket != "undefined")
  51. {
  52. socket = new MozWebSocket('ws://' + this._conf.host + h5spath);
  53. }else
  54. {
  55. socket = new WebSocket('ws://' + this._conf.host + h5spath);
  56. }
  57. }
  58. if (this._conf.protocol == "https:")
  59. {
  60. //alert(this._conf.host);
  61. if(this._debug === true) console.log(this._conf.host);
  62. if (typeof MozWebSocket != "undefined")
  63. {
  64. socket = new MozWebSocket('wss://' + this._conf.host + h5spath);
  65. }else
  66. {
  67. socket = new WebSocket('wss://' + this._conf.host + h5spath);
  68. }
  69. }
  70. if(this._debug === true) console.log(this._conf.host);
  71. } catch (e) {
  72. alert('error');
  73. return;
  74. }
  75. return socket;
  76. }
  77. H5sPbControl.prototype.keepaliveTimer = function()
  78. {
  79. try {
  80. var j = {};
  81. j.cmd = "H5_KEEPALIVE";
  82. this.wsSocket.send(JSON.stringify(j));
  83. } catch (e) {
  84. if(this._debug === true) console.log(e);
  85. }
  86. }
  87. H5sPbControl.prototype.onWebSocketData = function(msg)
  88. {
  89. if(typeof msg.data === 'string')
  90. {
  91. if(this._debug === true) console.log("[PBCONTROL] string");
  92. if (this._pbconf != undefined && this._pbconf.callback != undefined)
  93. {
  94. this._pbconf.callback(msg.data, this._pbconf.userdata);
  95. }
  96. return;
  97. }
  98. if (this.bDisConnected === true)
  99. {
  100. return;
  101. }
  102. }
  103. H5sPbControl.prototype.setupWebSocket = function(token)
  104. {
  105. var h5spath = "api/v1/h5spbcontrolapi";
  106. var streamprofile = 'main';
  107. if (this._conf.streamprofile === undefined)
  108. {}else
  109. {
  110. streamprofile = this._conf.streamprofile;
  111. }
  112. if (this._pbconf === undefined)
  113. {
  114. h5spath = this._conf.rootpath + h5spath + "?token=" + token
  115. + "&profile=" + streamprofile + '&session=' + this._conf.session;
  116. }else
  117. {
  118. var serverpb = 'false';
  119. var filename = 'fake';
  120. if (this._pbconf.serverpb === undefined)
  121. {}else
  122. {
  123. serverpb = this._pbconf.serverpb;
  124. }
  125. if (this._pbconf.filename === undefined)
  126. {}else
  127. {
  128. filename = this._pbconf.filename;
  129. }
  130. h5spath = this._conf.rootpath + h5spath + "?token=" + token
  131. + "&playback=true"
  132. + '&profile=' + streamprofile
  133. + "&serverpb=" + serverpb
  134. + "&begintime=" + encodeURIComponent(this._pbconf.begintime)//this._pbconf.begintime
  135. + "&endtime=" + encodeURIComponent(this._pbconf.endtime)//this._pbconf.endtime
  136. + "&filename=" + filename//file name
  137. + '&session=' + this._conf.session;
  138. } + '&session=' + this._conf.session;
  139. if(this._debug === true) console.log(h5spath);
  140. this.wsSocket = this.H5SWebSocketClient(h5spath);
  141. if(this._debug === true) console.log("[PBCONTROL] setupWebSocket", this.wsSocket);
  142. this.wsSocket.binaryType = 'arraybuffer';
  143. this.wsSocket.h5 = this;
  144. this.wsSocket.onmessage = this.onWebSocketData.bind(this);
  145. this.wsSocket.onopen = function()
  146. {
  147. if(this.h5._debug === true) console.log("[PBCONTROL] wsSocket.onopen", this.h5);
  148. this.h5.keepaliveTimerId = setInterval(this.h5.keepaliveTimer.bind(this.h5), 1000);
  149. var j = {};
  150. j.type = "H5S_WS_CONNECTED";
  151. if (this.h5._pbconf != undefined && this.h5._pbconf.callback != undefined)
  152. {
  153. this.h5._pbconf.callback(JSON.stringify(j), this.h5._pbconf.userdata);
  154. }
  155. }
  156. this.wsSocket.onclose = function () {
  157. if(this.h5._debug === true) console.log("[PBCONTROL] wsSocket.onclose", this.h5);
  158. var j = {};
  159. j.type = "H5S_WS_DISCONNECTED";
  160. if (this.h5._pbconf != undefined && this.h5._pbconf.callback != undefined)
  161. {
  162. this.h5._pbconf.callback(JSON.stringify(j), this.h5._pbconf.userdata);
  163. }
  164. this.h5.CleanupWebSocket(this.h5);
  165. }
  166. }
  167. H5sPbControl.prototype.CleanupWebSocket = function(h5sPlayer)
  168. {
  169. if(h5sPlayer._debug === true) console.log('[PBCONTROL] CleanupWebSocket', h5sPlayer);
  170. clearInterval(h5sPlayer.keepaliveTimerId);
  171. }
  172. /**
  173. * Connect a websocket Stream to videoElement
  174. */
  175. H5sPbControl.prototype.connect = function() {
  176. /* start connect to server */
  177. this.setupWebSocket(this._token);
  178. }
  179. /**
  180. * Disconnect a websocket Stream and clear videoElement source
  181. */
  182. H5sPbControl.prototype.disconnect = function() {
  183. if(this._debug === true) console.log("[PBCONTROL] disconnect", this);
  184. this.bDisConnected = true;
  185. clearInterval(this.reconnectTimerId);
  186. try {
  187. if (this.wsSocket != null)
  188. {
  189. this.wsSocket.close();
  190. this.wsSocket = null;
  191. }
  192. }
  193. catch (e){}
  194. if(this._debug === true) console.log("[PBCONTROL] disconnect", this);
  195. }
  196. H5sPbControl.prototype.start = function(){
  197. try {
  198. var j = {};
  199. j.cmd = "H5_START";
  200. this.wsSocket.send(JSON.stringify(j));
  201. } catch (e) {
  202. if(this._debug === true) console.log(e);
  203. }
  204. }
  205. H5sPbControl.prototype.pause = function(){
  206. try {
  207. var j = {};
  208. j.cmd = "H5_PAUSE";
  209. this.wsSocket.send(JSON.stringify(j));
  210. } catch (e) {
  211. if(this._debug === true) console.log(e);
  212. }
  213. }
  214. H5sPbControl.prototype.resume = function(){
  215. try {
  216. var j = {};
  217. j.cmd = "H5_RESUME";
  218. this.wsSocket.send(JSON.stringify(j));
  219. } catch (e) {
  220. if(this._debug === true) console.log(e);
  221. }
  222. }
  223. H5sPbControl.prototype.seek = function(nTime){
  224. try {
  225. var j = {};
  226. j.cmd = "H5_SEEK";
  227. j.nSeekTime = nTime;
  228. this.wsSocket.send(JSON.stringify(j));
  229. } catch (e) {
  230. if(this._debug === true) console.log(e);
  231. }
  232. }
  233. H5sPbControl.prototype.speed = function(nSpeed){
  234. try {
  235. var j = {};
  236. j.cmd = "H5_SPEED";
  237. j.nSpeed = nSpeed;
  238. this.wsSocket.send(JSON.stringify(j));
  239. } catch (e) {
  240. if(this._debug === true) console.log(e);
  241. }
  242. }