index - 副本.html 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="description" content="jMuxer - a simple javascript mp4 muxer for non-standard streaming communications protocol">
  6. <meta name="keywords" content="h264 player, mp4 player, mse, mp4 muxing, jmuxer, aac player">
  7. <title>JMuxer demo</title>
  8. <script async defer src="https://buttons.github.io/buttons.js"></script>
  9. <style type="text/css">
  10. .github-tools {
  11. position: absolute;
  12. top: 15px;
  13. right: 15px;
  14. }
  15. a.h264-player {
  16. font-size: 20px;
  17. text-decoration: none;
  18. color: #07568e;
  19. margin-top: 10px;
  20. display: block;
  21. }
  22. .gesture {
  23. font-size: 15px;
  24. color: #ad4903;
  25. margin-top: 10px;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <h2>jMuxer Demo</h2>
  31. <p>Sample demo node server is running on heroku free hosting</p>
  32. <br /><br />
  33. <div class="github-tools">
  34. <!-- Place this tag where you want the button to render. -->
  35. </div>
  36. <div id="container" style="width: 600px; margin: 0 auto;">
  37. <video width="100%" disablePictureInPicture ="true" autoplay poster="images/loader-thumb.jpg" id="player"></video>
  38. <audio width="50%" preload="none" autoplay controls poster="images/loader-thumb.jpg" id="audioPlayer" ></audio>
  39. <div class="gesture">If it does not play automatically, Click the `video play button` to initiate the video</div>
  40. </div>
  41. <body oncontextmenu="Back()">
  42. </body>
  43. <script src="helper.js" >
  44. </script>
  45. <script>
  46. //隐藏控件 controls
  47. var fpsCount = 0;
  48. var requestCount = 0;
  49. var timeCount = 0;
  50. var isVisuable = true;
  51. var isFeed = true;
  52. var isDrag = false;
  53. var isEnough = true;
  54. var delayTime = new Date().getTime();
  55. var feedTime = new Date().getTime();
  56. var requestTime = new Date().getTime() ;
  57. var curTime = new Date().getTime();
  58. var requestTime = new Date().getTime();//记录离开时间
  59. var myVideo = document.getElementById("player");
  60. var myAudio = document.getElementById("audioPlayer");
  61. var audioBuffer = [];
  62. var audioBack = [];
  63. document.addEventListener("visibilitychange", () => {
  64. if (document.visibilityState == "visible")
  65. {
  66. console.log("页面可见,继续喂视频");
  67. //requestTime = new Date().getTime();
  68. isVisuable = true;
  69. }
  70. else
  71. {
  72. isVisuable = false;
  73. isFeed = false;
  74. myVideo.pause();
  75. }
  76. });
  77. myVideo.addEventListener('pause',function(){
  78. //console.log("视频播放暂停");
  79. isFeed = false;
  80. });
  81. myAudio.addEventListener('pause',function(){
  82. console.log("音频播放暂停");
  83. myAudio.play();
  84. });
  85. //解协议
  86. function ParseProto(data)
  87. {
  88. var temp = "";
  89. var input = new Uint8Array(data),
  90. duration,
  91. video,
  92. audio;
  93. if(input[0] == 0 && input[1] == 0 && input[2] == 0 && input[3] == 1)
  94. {
  95. // debugger
  96. video = input;
  97. duration = 24;
  98. var nalType = input[4] &0x1f;//nalType == 0x07|| nalType == 0x08 || nalType == 0x05
  99. if(!isFeed)
  100. {
  101. if(nalType == 0x05)
  102. {
  103. console.log("发现I帧");
  104. }
  105. if(nalType == 0x05 && isVisuable)
  106. {
  107. console.log("检测到I帧 %d,重新渲染, 耗时 %d ms", nalType , new Date().getTime() - requestTime);
  108. isFeed = true;
  109. }
  110. }
  111. }
  112. else if(input[0] == 0xff)
  113. {
  114. if(!isEnough)
  115. {
  116. requestCount++;
  117. }
  118. audio = input;
  119. if(new Date().getTime() - curTime > 100)
  120. {
  121. delayTime = new Date().getTime();
  122. console.log("接收时间 %d ms", new Date().getTime() - curTime);
  123. }
  124. curTime = new Date().getTime();
  125. duration = 24;
  126. //console.log("duration %d", duration);
  127. }
  128. else if(input[0] == 0x68)
  129. {
  130. if(input[23] == 0x05)//横竖屏标识
  131. {
  132. var state = CheckScreenDirection(input.slice(24, 24 + 8));
  133. if(state == 1)
  134. {
  135. console.log("安卓卡此时竖屏");
  136. //竖屏处理
  137. }
  138. else
  139. {
  140. console.log("安卓卡此时横屏");
  141. //横屏处理
  142. }
  143. }
  144. if(input[23] == 0x0b)
  145. {
  146. console.log("多端登陆");
  147. }
  148. //console.log("屏幕旋转 %s", PrintArry(input));
  149. }
  150. return {
  151. audio: audio,
  152. video: video,
  153. duration: duration
  154. };
  155. }
  156. window.onload = function() {
  157. var socketURL = 'wss://jmuxer-demo-server.herokuapp.com';
  158. //socketURL = "ws://127.0.0.1:8080"
  159. //socketURL = "ws://192.168.11.238:8080"
  160. //socketURL = "ws://14.215.128.98:14112";
  161. socketURL = "ws://192.168.11.66:9101";
  162. //socketURL = "wss://192.168.11.242:9104";
  163. var jmuxer = new JMuxer({
  164. node: 'player',
  165. flushingTime:15 ,
  166. fps: 30,
  167. mode:'video',
  168. debug: false
  169. });
  170. var audioMuxer = new JMuxer({
  171. node: 'audioPlayer',
  172. flushingTime:1,
  173. clearBuffer: true,
  174. fps: 60,//可以不选,原先43
  175. mode:'audio',
  176. debug: false
  177. });
  178. /*var costTime = new Date().getTime() - curTime;
  179. if(costTime > 5)
  180. {
  181. console.log("websocket接收延迟 %d ms", costTime);
  182. }*/
  183. curTime = new Date().getTime();
  184. var ws = new WebSocket(socketURL);
  185. ws.binaryType = 'arraybuffer';
  186. //断开检测
  187. ws.onclose = function (e) {
  188. alert("websocket连接断开");
  189. console.log('websocket 断开: ' + e.code + ' ' + e.reason + ' ' + e.wasClean);
  190. console.log(e);
  191. }
  192. ws.addEventListener('open', function (event) {
  193. console.log("发送配置帧");
  194. ws.send(ConfigChannel("RK3923C1201900139"));
  195. });
  196. ws.addEventListener('error', function (event) {
  197. console.log("连接失败");
  198. });
  199. /*setInterval(function()
  200. {
  201. var audioData =
  202. {
  203. audio: null,
  204. video: null,
  205. duration: 24
  206. };
  207. if(myAudio != null)
  208. {
  209. if(audioBuffer.length < 1)
  210. {
  211. if(audioBack.length > 0)
  212. {
  213. audioData.audio = audioBack[0];
  214. audioMuxer.feed(audioData);
  215. audioBack.shift();
  216. }
  217. return;
  218. }
  219. if(audioBack.length > 10)
  220. {
  221. audioBack.shift();
  222. }
  223. if(audioBuffer.length > 50)
  224. {
  225. console.log("当前大小 %d", audioBuffer.length);
  226. audioBuffer = [];
  227. }
  228. else if(audioBuffer.length > 1)
  229. {
  230. var time = new Date().getTime();
  231. audioData.audio = audioBuffer[0];
  232. audioMuxer.feed(audioData);
  233. if(time - feedTime > 80)
  234. {
  235. console.log("喂数据间隔 %d ms, 队列长度", time - feedTime, audioBuffer.length);
  236. }
  237. feedTime = time;
  238. audioBuffer.shift();
  239. }
  240. }
  241. }, 1);*/
  242. ws.addEventListener('message',function(event) {
  243. var data = ParseProto(event.data);//JAVA服务器转发
  244. //console.log("收到数据");
  245. var audioData = {
  246. audio: data.audio,
  247. video: null,
  248. duration: data.duration
  249. };
  250. var videoData = {
  251. audio: null,
  252. video: data.video,
  253. duration: data.duration
  254. };
  255. if(myAudio.readyState == 2)
  256. {
  257. requestTime = new Date().getTime();
  258. isEnough = false;
  259. console.log("数据存储不够,出现声音停止");
  260. //myAudio.playbackRate = 2;
  261. }
  262. else if(myAudio.readyState == 4 && isEnough== false)
  263. {
  264. var time = new Date().getTime();
  265. isEnough = true;
  266. console.log("填满耗时 %d ms, 填充帧数 %d, 填充延迟 %d ms", time - requestTime, requestCount, requestCount * 23);
  267. requestCount = 0;
  268. console.log("----接收到启动 %d ms---", time - delayTime);
  269. }
  270. if(data.audio != null)//喂音频
  271. {
  272. //audioBuffer.push(data.audio);
  273. //audioBack.push(data.audio) ;
  274. audioMuxer.feed(audioData);
  275. }
  276. if(data.video != null)//喂视频
  277. {
  278. if(isFeed)
  279. {
  280. jmuxer.feed(data);
  281. }
  282. //jmuxer.feed(videoData);
  283. }
  284. });
  285. myVideo.onmousedown = function(event)
  286. {
  287. //放在此处只是为了方便演示,实际使用中查找横竖屏只要刚连接上时调用一次就好。
  288. //var checkBuffer = GetScreenState();
  289. //ws.send(checkBuffer);
  290. if(!isFeed)
  291. {
  292. console.log("重新申请I帧");
  293. requestTime = new Date().getTime();
  294. var buffer = RequestIFrame();
  295. //var buffer = new Uint8Array([0x01]);
  296. ws.send(buffer);
  297. }
  298. //console.log("报文 %s", PrintArry(buffer));
  299. if(event.button == 0)
  300. {
  301. var posX = event.offsetX * 1080 *1.0/myVideo.clientWidth;
  302. var posY = event.offsetY * 1920 *1.0/myVideo.clientHeight;
  303. var buffer = ExexuteMouseDown(posX.toString(), posY.toString());
  304. ws.send(buffer);
  305. isDrag = true;
  306. }
  307. }
  308. myVideo.onmousemove = function(event)
  309. {
  310. if(isDrag && event.button == 0)
  311. {
  312. var posX = event.offsetX * 1080 *1.0/myVideo.clientWidth;
  313. var posY = event.offsetY * 1920 *1.0/myVideo.clientHeight;
  314. var buffer = ExexuteMouseMove(posX.toString(), posY.toString());
  315. ws.send(buffer);
  316. //console.log("移动位置 %d, %d", posX, posY);
  317. }
  318. }
  319. myVideo.onmouseup = function(event)
  320. {
  321. isDrag = false;
  322. var posX = event.offsetX * 1080 *1.0/myVideo.clientWidth;
  323. var posY = event.offsetY * 1920 *1.0/myVideo.clientHeight;
  324. var buffer = ExexuteMouseUp(posX.toString(), posY.toString());
  325. ws.send(buffer);
  326. }
  327. myVideo.onkeydown = function(event)
  328. {
  329. ExexuteKeyDown(e.keyCode);
  330. }
  331. }
  332. function Back()
  333. {
  334. if(event.button == 2)
  335. {
  336. //ExexuteKeyDown(4);
  337. }
  338. ExexuteKeyDown(4);
  339. window.event.returnValue=false;
  340. return false;
  341. }
  342. </script>
  343. <script type="text/javascript" src="jmuxer.js"></script>
  344. </body>
  345. </html>