绍兴公厕前端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

293 lines
15 KiB

3 years ago
  1. var WebsocketServer = function (a, b) {
  2. function c() {
  3. }
  4. function d(a, b, c, d) {
  5. var e = "";
  6. switch (a) {
  7. case"OPTIONS":
  8. case"TEARDOWN":
  9. case"GET_PARAMETER":
  10. case"SET_PARAMETERS":
  11. e = a + " " + M + " RTSP/1.0\r\nCSeq: " + B + "\r\n" + z + "\r\n";
  12. break;
  13. case"DESCRIBE":
  14. e = a + " " + M + " RTSP/1.0\r\nCSeq: " + B + "\r\n" + z + "\r\n";
  15. break;
  16. case"SETUP":
  17. debug.log("trackID: " + b), e = a + " " + M + "/trackID=" + b + " RTSP/1.0\r\nCSeq: " + B + "\r\n" + z + "Transport: DH/AVP/TCP;unicast;interleaved=" + 2 * b + "-" + (2 * b + 1) + "\r\n", e += 0 != G ? "Session: " + G + "\r\n\r\n" : "\r\n";
  18. break;
  19. case"PLAY":
  20. e = a + " " + M + " RTSP/1.0\r\nCSeq: " + B + "\r\nSession: " + G + "\r\n", void 0 != d && 0 != d ? (e += "Range: npt=" + d + "-\r\n", e += z + "\r\n") : e += z + "\r\n";
  21. break;
  22. case"PAUSE":
  23. e = a + " " + M + " RTSP/1.0\r\nCSeq: " + B + "\r\nSession: " + G + "\r\n\r\n";
  24. break;
  25. case"SCALE":
  26. e = "PLAY " + M + " RTSP/1.0\r\nCSeq: " + B + "\r\nSession: " + G + "\r\n", e += "Scale: " + d + "\r\n", e += z + "\r\n"
  27. }
  28. return debug.log(e), e
  29. }
  30. function e(a) {
  31. debug.log(a);
  32. var b = {}, e = a.search("CSeq: ") + 5;
  33. if (B = parseInt(a.slice(e, e + 10)) + 1, b = m(a), b.ResponseCode === x.UNAUTHORIZED && "" === z) f(b); else if (b.ResponseCode === x.OK) {
  34. if ("Options" === E) return E = "Describe", d("DESCRIBE", null, null);
  35. if ("Describe" === E) {
  36. I = !1, D = n(a), "undefined" != typeof b.ContentBase && (D.ContentBase = b.ContentBase);
  37. var g = 0;
  38. for (g = 0; g < D.Sessions.length; g += 1) {
  39. var i = {};
  40. "JPEG" === D.Sessions[g].CodecMime || "H264" === D.Sessions[g].CodecMime || "H265" === D.Sessions[g].CodecMime || "H264-SVC" == D.Sessions[g].CodecMime ? (i.codecName = D.Sessions[g].CodecMime, "H264-SVC" == D.Sessions[g].CodecMime && (i.codecName = "H264"), "H265" == D.Sessions[g].CodecMime && c.prototype.setLiveMode("canvas"), i.trackID = D.Sessions[g].ControlURL, i.ClockFreq = D.Sessions[g].ClockFreq, i.Port = parseInt(D.Sessions[g].Port), "undefined" != typeof D.Sessions[g].Framerate && (i.Framerate = parseInt(D.Sessions[g].Framerate), w.setFPS(i.Framerate), N(i.Framerate)), A.push(i)) : "PCMU" === D.Sessions[g].CodecMime || -1 !== D.Sessions[g].CodecMime.search("G726-16") || -1 !== D.Sessions[g].CodecMime.search("G726-24") || -1 !== D.Sessions[g].CodecMime.search("G726-32") || -1 !== D.Sessions[g].CodecMime.search("G726-40") || "PCMA" === D.Sessions[g].CodecMime ? ("PCMU" === D.Sessions[g].CodecMime ? i.codecName = "G.711Mu" : "G726-16" === D.Sessions[g].CodecMime ? i.codecName = "G.726-16" : "G726-24" === D.Sessions[g].CodecMime ? i.codecName = "G.726-24" : "G726-32" === D.Sessions[g].CodecMime ? i.codecName = "G.726-32" : "G726-40" === D.Sessions[g].CodecMime ? i.codecName = "G.726-40" : "PCMA" === D.Sessions[g].CodecMime && (i.codecName = "G.711A"), i.trackID = D.Sessions[g].ControlURL, i.ClockFreq = D.Sessions[g].ClockFreq, i.Port = parseInt(D.Sessions[g].Port), i.Bitrate = parseInt(D.Sessions[g].Bitrate), A.push(i)) : "mpeg4-generic" === D.Sessions[g].CodecMime || "MPEG4-GENERIC" === D.Sessions[g].CodecMime ? (i.codecName = "mpeg4-generic", i.trackID = D.Sessions[g].ControlURL, i.ClockFreq = D.Sessions[g].ClockFreq, i.Port = parseInt(D.Sessions[g].Port), i.Bitrate = parseInt(D.Sessions[g].Bitrate), A.push(i)) : "vnd.onvif.metadata" === D.Sessions[g].CodecMime ? (i.codecName = "MetaData", i.trackID = D.Sessions[g].ControlURL, i.ClockFreq = D.Sessions[g].ClockFreq, i.Port = parseInt(D.Sessions[g].Port), A.push(i)) : "stream-assist-frame" === D.Sessions[g].CodecMime ? (i.codecName = "stream-assist-frame", i.trackID = D.Sessions[g].ControlURL, i.ClockFreq = D.Sessions[g].ClockFreq, i.Port = parseInt(D.Sessions[g].Port), A.push(i)) : debug.log("Unknown codec type:", D.Sessions[g].CodecMime, D.Sessions[g].ControlURL)
  41. }
  42. return F = 0, E = "Setup", debug.log(A), d("SETUP", F)
  43. }
  44. if ("Setup" === E) {
  45. if (G = b.SessionID, F < A.length) return A[F].RtpInterlevedID = b.RtpInterlevedID, A[F].RtcpInterlevedID = b.RtcpInterlevedID, F += 1, F !== A.length ? d("SETUP", A[F].trackID.split("=")[1] - 0) : (w.sendSdpInfo(A, L, I), E = "Play", d("PLAY", null));
  46. debug.log("Unknown setup SDP index")
  47. } else if ("Play" === E) {
  48. G = b.SessionID, clearInterval(J), J = setInterval(function () {
  49. return h(d("GET_PARAMETER", null, null))
  50. }, y);
  51. E = "Playing"
  52. } else "Playing" === E || debug.log("unknown rtsp state:" + E)
  53. } else if (b.ResponseCode === x.NOTSERVICE) {
  54. if ("Setup" === E && -1 !== A[F].trackID.search("trackID=t")) return A[F].RtpInterlevedID = -1, A[F].RtcpInterlevedID = -1, F += 1, I = !1, C({
  55. errorCode: "504",
  56. description: "Talk Service Unavilable",
  57. place: "RtspClient.js"
  58. }), F < A.length ? d("SETUP", A[F].trackID) : (E = "Play", d("PLAY", null));
  59. C({errorCode: "503", description: "Service Unavilable"})
  60. } else if (b.ResponseCode === x.NOTFOUND) {
  61. if ("Describe" === E || "Options" === E) return void C({errorCode: 404, description: "rtsp not found"})
  62. } else if (b.ResponseCode === x.INVALID_RANGE) return ("backup" === H || "playback" === H) && C({
  63. errorCode: "457",
  64. description: "Invalid range"
  65. }), void debug.log("RTP disconnection detect!!!")
  66. }
  67. function f(a) {
  68. var b = P.username, c = P.passWord, e = {Method: null, Realm: null, Nonce: null, Uri: null}, f = null;
  69. e = {
  70. Method: E.toUpperCase(),
  71. Realm: a.Realm,
  72. Nonce: a.Nonce,
  73. Uri: M
  74. }, f = g(b, c, e.Uri, e.Realm, e.Nonce, e.Method), z = 'Authorization: Digest username="' + b + '", realm="' + e.Realm + '",', z += ' nonce="' + e.Nonce + '", uri="' + e.Uri + '", response="' + f + '"', z += "\r\n", h(d("OPTIONS", null, null))
  75. }
  76. function g(a, b, c, d, e, f) {
  77. var g = null, h = null, i = null;
  78. return g = hex_md5(a + ":" + d + ":" + b).toLowerCase(), h = hex_md5(f + ":" + c).toLowerCase(), i = hex_md5(g + ":" + e + ":" + h).toLowerCase()
  79. }
  80. function h(a) {
  81. if (void 0 != a && null != a && "" != a) if (null !== o && o.readyState === WebSocket.OPEN) {
  82. if (v === !1) {
  83. var b = a.search("DESCRIBE");
  84. -1 !== b && (u = !0, v = !0)
  85. }
  86. console.info('ws request', a);
  87. void 0 != a && o.send(i(a))
  88. } else debug.log("ws未连接")
  89. }
  90. function i(a) {
  91. for (var b = a.length, c = new Uint8Array(new ArrayBuffer(b)), d = 0; b > d; d++) c[d] = a.charCodeAt(d);
  92. return c
  93. }
  94. function j(a) {
  95. var b = new Uint8Array, c = new Uint8Array(a.data);
  96. for (b = new Uint8Array(c.length), b.set(c, 0), s = b.length; s > 0;) if (36 !== b[0]) {
  97. var d = String.fromCharCode.apply(null, b), f = null;
  98. -1 !== d.indexOf("OffLine:File Over") && O(), u === !0 ? (f = d.lastIndexOf("\r\n"), u = !1) : f = d.search("\r\n\r\n");
  99. var g = d.search("RTSP");
  100. if (-1 === g) return void (b = new Uint8Array);
  101. if (-1 === f) return void (s = b.length);
  102. q = b.subarray(g, f + p), b = b.subarray(f + p);
  103. var i = String.fromCharCode.apply(null, q);
  104. h(e(i)), s = b.length
  105. } else {
  106. if (r = b.subarray(0, p), t = r[2] << 24 | r[3] << 16 | r[4] << 8 | r[5], !(t + p <= b.length)) return void (s = b.length);
  107. var j = b.subarray(p, t + p);
  108. l(r, j), b = b.subarray(t + p), s = b.length
  109. }
  110. }
  111. function k(a) {
  112. K = a
  113. }
  114. function l(a, b) {
  115. w.parseRTPData(a, b), k(!0)
  116. }
  117. function m(a) {
  118. var b = {}, c = 0, d = 0, e = null, f = null, g = null;
  119. if (-1 !== a.search("Content-Type: application/sdp")) {
  120. var h = a.split("\r\n\r\n");
  121. g = h[0]
  122. } else g = a;
  123. var i = g.split("\r\n"), j = i[0].split(" ");
  124. if (j.length > 2 && (b.ResponseCode = parseInt(j[1]), b.ResponseMessage = j[2]), b.ResponseCode === x.OK) {
  125. for (c = 1; c < i.length; c++) if (f = i[c].split(":"), "Public" === f[0]) b.MethodsSupported = f[1].split(","); else if ("CSeq" === f[0]) b.CSeq = parseInt(f[1]); else if ("Content-Type" === f[0]) b.ContentType = f[1], -1 !== b.ContentType.search("application/sdp") && (b.SDPData = n(a)); else if ("Content-Length" === f[0]) b.ContentLength = parseInt(f[1]); else if ("Content-Base" === f[0]) {
  126. var k = i[c].search("Content-Base:");
  127. -1 !== k && (b.ContentBase = i[c].substr(k + 13))
  128. } else if ("Session" === f[0]) {
  129. var l = f[1].split(";");
  130. b.SessionID = parseInt(l[0])
  131. } else if ("Transport" === f[0]) {
  132. var m = f[1].split(";");
  133. for (d = 0; d < m.length; d++) {
  134. var o = m[d].search("interleaved=");
  135. if (-1 !== o) {
  136. var p = m[d].substr(o + 12), q = p.split("-");
  137. q.length > 1 && (b.RtpInterlevedID = parseInt(q[0]), b.RtcpInterlevedID = parseInt(q[1]))
  138. }
  139. }
  140. } else if ("RTP-Info" === f[0]) {
  141. f[1] = i[c].substr(9);
  142. var r = f[1].split(",");
  143. for (b.RTPInfoList = [], d = 0; d < r.length; d++) {
  144. var s = r[d].split(";"), t = {}, u = 0;
  145. for (u = 0; u < s.length; u++) {
  146. var v = s[u].search("url=");
  147. -1 !== v && (t.URL = s[u].substr(v + 4)), v = s[u].search("seq="), -1 !== v && (t.Seq = parseInt(s[u].substr(v + 4)))
  148. }
  149. b.RTPInfoList.push(t)
  150. }
  151. }
  152. } else if (b.ResponseCode === x.UNAUTHORIZED) for (c = 1; c < i.length; c++) if (f = i[c].split(":"), "CSeq" === f[0]) b.CSeq = parseInt(f[1]); else if ("WWW-Authenticate" === f[0]) {
  153. var w = f[1].split(",");
  154. for (d = 0; d < w.length; d++) {
  155. var y = w[d].search("Digest realm=");
  156. if (-1 !== y) {
  157. e = w[d].substr(y + 13);
  158. var z = e.split('"');
  159. b.Realm = z[1]
  160. }
  161. if (y = w[d].search("nonce="), -1 !== y) {
  162. e = w[d].substr(y + 6);
  163. var A = e.split('"');
  164. b.Nonce = A[1]
  165. }
  166. }
  167. }
  168. return b
  169. }
  170. function n(a) {
  171. var b = {}, c = [];
  172. b.Sessions = c;
  173. var d = null;
  174. if (-1 !== a.search("Content-Type: application/sdp")) {
  175. var e = a.split("\r\n\r\n");
  176. d = e[1]
  177. } else d = a;
  178. var f = d.split("\r\n"), g = 0, h = !1;
  179. for (g = 0; g < f.length; g++) {
  180. var i = f[g].split("=");
  181. if (i.length > 0) switch (i[0]) {
  182. case"a":
  183. var j = i[1].split(":");
  184. if (j.length > 1) if ("control" === j[0]) {
  185. var k = f[g].search("control:");
  186. h === !0 ? -1 !== k && (b.Sessions[b.Sessions.length - 1].ControlURL = f[g].substr(k + 8)) : -1 !== k && (b.BaseURL = f[g].substr(k + 8))
  187. } else if ("rtpmap" === j[0]) {
  188. var l = j[1].split(" ");
  189. b.Sessions[b.Sessions.length - 1].PayloadType = l[0];
  190. var m = l[1].split("/");
  191. b.Sessions[b.Sessions.length - 1].CodecMime = m[0], m.length > 1 && (b.Sessions[b.Sessions.length - 1].ClockFreq = m[1])
  192. } else if ("framesize" === j[0]) {
  193. var n = j[1].split(" ");
  194. if (n.length > 1) {
  195. var o = n[1].split("-");
  196. b.Sessions[b.Sessions.length - 1].Width = o[0], b.Sessions[b.Sessions.length - 1].Height = o[1]
  197. }
  198. } else if ("framerate" === j[0]) b.Sessions[b.Sessions.length - 1].Framerate = j[1]; else if ("fmtp" === j[0]) {
  199. var p = f[g].split(" ");
  200. if (p.length < 2) continue;
  201. for (var q = 1; q < p.length; q++) {
  202. var r = p[q].split(";"), s = 0;
  203. for (s = 0; s < r.length; s++) {
  204. var t = r[s].search("mode=");
  205. if (-1 !== t && (b.Sessions[b.Sessions.length - 1].mode = r[s].substr(t + 5)), t = r[s].search("config="), -1 !== t && (b.Sessions[b.Sessions.length - 1].config = r[s].substr(t + 7), L.config = b.Sessions[b.Sessions.length - 1].config, L.clockFreq = b.Sessions[b.Sessions.length - 1].ClockFreq, L.bitrate = b.Sessions[b.Sessions.length - 1].Bitrate), t = r[s].search("sprop-vps="), -1 !== t && (b.Sessions[b.Sessions.length - 1].VPS = r[s].substr(t + 10)), t = r[s].search("sprop-sps="), -1 !== t && (b.Sessions[b.Sessions.length - 1].SPS = r[s].substr(t + 10)), t = r[s].search("sprop-pps="), -1 !== t && (b.Sessions[b.Sessions.length - 1].PPS = r[s].substr(t + 10)), t = r[s].search("sprop-parameter-sets="), -1 !== t) {
  206. var u = r[s].substr(t + 21), v = u.split(",");
  207. v.length > 1 && (b.Sessions[b.Sessions.length - 1].SPS = v[0], b.Sessions[b.Sessions.length - 1].PPS = v[1])
  208. }
  209. }
  210. }
  211. }
  212. break;
  213. case"m":
  214. var w = i[1].split(" "), x = {};
  215. x.Type = w[0], x.Port = w[1], x.Payload = w[3], b.Sessions.push(x), h = !0;
  216. break;
  217. case"b":
  218. if (h === !0) {
  219. var y = i[1].split(":");
  220. b.Sessions[b.Sessions.length - 1].Bitrate = y[1]
  221. }
  222. }
  223. }
  224. return b
  225. }
  226. var a = a, o = null, p = 6, q = null, r = null, s = 0, t = 0, u = !1, v = !1, w = new WorkerManager,
  227. x = {OK: 200, UNAUTHORIZED: 401, NOTFOUND: 404, INVALID_RANGE: 457, NOTSERVICE: 503, DISCONNECT: 999}, y = 4e4,
  228. z = "", A = [], B = 1, C = null, D = {}, E = "Options", F = null, G = null, H = "", I = !1, J = null, K = !1,
  229. L = {}, M = b, N = null, O = null, P = {}, Q = "";
  230. return c.prototype = {
  231. init: function (a, b) {
  232. w.init(a, b)
  233. }, connect: function () {
  234. o || (o = new WebSocket(a), o.binaryType = "arraybuffer", o.addEventListener("message", j, !1), o.onopen = function () {
  235. var a = i("OPTIONS " + M + " RTSP/1.0\r\nCSeq: " + B + "\r\n\r\n");
  236. o.send(a)
  237. })
  238. }, disconnect: function () {
  239. h(d("TEARDOWN", null, null)), clearInterval(J), J = null, null !== o && o.readyState === WebSocket.OPEN && (o.close(), o = null, G = null), w.terminate()
  240. }, controlPlayer: function (a) {
  241. var b = "";
  242. switch (Q = a.command, a.command) {
  243. case"PLAY":
  244. if (E = "Play", null != a.range) {
  245. b = d("PLAY", null, null, a.range);
  246. break
  247. }
  248. b = d("PLAY", null, null), Q && w.initStartTime();
  249. break;
  250. case"PAUSE":
  251. if ("PAUSE" === E) break;
  252. E = "PAUSE", b = d("PAUSE", null, null);
  253. break;
  254. case"SCALE":
  255. b = d("SCALE", null, null, a.data), w.playbackSpeed(a.data);
  256. break;
  257. case"TEARDOWN":
  258. b = d("TEARDOWN", null, null);
  259. break;
  260. case"audioPlay":
  261. case"volumn":
  262. case"audioSamplingRate":
  263. w.controlAudio(a.command, a.data);
  264. break;
  265. default:
  266. debug.log("未知指令: " + a.command)
  267. }
  268. "" != b && h(b)
  269. }, setLiveMode: function (a) {
  270. w.setLiveMode(a)
  271. }, setRTSPURL: function (a) {
  272. M = a
  273. }, setCallback: function (a, b) {
  274. "GetFrameRate" === a ? N = b : "FileOver" === a ? O = b : w.setCallback(a, b), "Error" == a && (C = b)
  275. }, setUserInfo: function (a, b) {
  276. P.username = a, P.passWord = b
  277. }, capture: function (a) {
  278. w.capture(a)
  279. }
  280. }, new c
  281. };