Workers.js 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  1. const defaultLogger = {
  2. info: console.log,
  3. debug: console.log,
  4. error: console.error,
  5. infoAndReportMeasurement: (...i) => {},
  6. };
  7. let USER_ID = "987412365",
  8. PAGE_SESSION = "aaabbbccc",
  9. SERVER_SESSION = "cccbbbaaa",
  10. COS_PREFIX = "error-bitstreams-auto-uploaded-from-application/",
  11. FRAME_COMPOSE_LENGTH = 5;
  12. import CircularArray from "./CircularArray.js";
  13. import SecondArray from "./SecondArray.js";
  14. import v2Decoder from "./v2Decoder/index.js";
  15. export default class Workers {
  16. constructor(e, t) {
  17. (this.rtcp = e),
  18. (this.cacheSize = 0),
  19. (this.cacheBuffer = new Uint8Array(262144)),
  20. (this.cacheFrameCnt = 0),
  21. (this.startReceiveTime = 0),
  22. (this.cacheFrameComposes = new Array(0)),
  23. (this.cacheSizes = new Array(5).fill(0)),
  24. (this.cacheFrameCnts = new Array(5).fill(-1)),
  25. (this.cacheStartReceiveTimes = new Array(5).fill(0)),
  26. (this.cacheBuffers = [
  27. new Uint8Array(262144),
  28. new Uint8Array(262144),
  29. new Uint8Array(262144),
  30. new Uint8Array(262144),
  31. new Uint8Array(262144),
  32. ]),
  33. (this.panoCacheSize = 0),
  34. (this.panoCacheBuffer = new Uint8Array(2097152)),
  35. (this.cachePanoTileID = 0),
  36. (this.receivedMedia = 0),
  37. (this.receivedMedia_worker = 0),
  38. (this.receivedYUV = 0),
  39. (this.receivedEmit = 0),
  40. (this.returnFrames = 0),
  41. (this.lastReturnFrames = 0),
  42. (this.lastReceivedEmit = 0),
  43. (this.mediaBytesReceived = 0),
  44. (this.metaBytesReceived = 0),
  45. (this.noWasmBytesReceived = 0),
  46. (this.rtcBytesReceived = 0),
  47. (this.rtcMessageReceived = 0),
  48. (this.packetsDrop = 0),
  49. (this.framesAwait = 0),
  50. (this.sendOutBuffer = 0),
  51. (this.decodeTimePerFrame = 0),
  52. (this.decodeTimeMaxFrame = 0),
  53. (this.lastRenderTs = 0),
  54. (this.JankTimes = 0),
  55. (this.bigJankTimes = 0),
  56. (this.DecodeJankTimes = 0),
  57. (this.bigDecodeJankTimes = 0),
  58. (this.saveframe = false),
  59. (this.SaveMediaStream = false),
  60. (this.packetsLost = 0),
  61. (this.showAllReceivedMetadata = false),
  62. (this.firstMediaArrival = 0),
  63. (this.firstMediaReceived = false),
  64. (this.firstYUVDecoded = 0),
  65. (this.firstRender = 0),
  66. (this.firstYUVReceived = false),
  67. (this.reconnectSignal = false),
  68. (this.serverFrameSlow = 0),
  69. (this.serverFrameFast = 0),
  70. (this.clientFrameSlow = 0),
  71. (this.clientFrameFast = 0),
  72. (this.lastServerTS = 0),
  73. (this.lastClientTS = 0),
  74. (this.lastSeq = 0),
  75. (this.lastIsPureMeta = false),
  76. (this.lastHBPacketTs = 0),
  77. (this.HBPacketInterval = 0),
  78. (this.lastHBPacketSrvSentTs = 0),
  79. (this.HBPacketIntervalSrvSent = 0),
  80. (this.cachedLength = 2),
  81. (this.cachedStreams = new Array(this.cachedLength)),
  82. (this.cachedMetas = new Array(this.cachedLength)),
  83. (this.cachedPtss = new Array(this.cachedLength)),
  84. (this.cachedRender = Array(this.cachedLength).fill(false)),
  85. (this.cachedResolution = new Array(this.cachedLength)),
  86. (this.getPtr = 0),
  87. (this.setPtr = 0),
  88. (this.receiveIframes = 0),
  89. (this.decodeIframes = 0),
  90. (this.prevSenderTs = -1),
  91. (this.serverSendTimeArray = new CircularArray(120, false, [])),
  92. (this.inPanoMode = false),
  93. (this.PanoStatus = {
  94. x: 0,
  95. y: 0,
  96. z: 0,
  97. tiles: [],
  98. }),
  99. (this.DynamicPanoTest = false),
  100. (this.PanoMask = new ArrayBuffer(8)),
  101. (this.PanoView = new DataView(this.PanoMask)),
  102. (this.userId_test = ""),
  103. (this.PendingMasks = []),
  104. (this.traceIdMap = new Map()),
  105. (this.responseTimeArray = []),
  106. (this.processTimeArray = []),
  107. (this.displayTimeArray = []),
  108. (this.overallTimeArray = []),
  109. (this.responseMiss = 0),
  110. (this.processMiss = 0),
  111. (this.displayMiss = 0),
  112. (this.joyStickTraceIdMap = new Map()),
  113. (this.joyStickResponseTimeArray = []),
  114. (this.joyStickProcessTimeArray = []),
  115. (this.joyStickDisplayTimeArray = []),
  116. (this.joyStickOverallTimeArray = []),
  117. (this.joyStickResponseMiss = 0),
  118. (this.joyStickProcessMiss = 0),
  119. (this.joyStickDisplayMiss = 0),
  120. (this.updateYUVCircular = new CircularArray(120, false, [])),
  121. (this.updateDropFrame = 0),
  122. (this.metaParseArray = []),
  123. (this.responseMoveMiss = 0),
  124. (this.processMoveMiss = 0),
  125. (this.displayMoveMiss = 0),
  126. (this.MovingTraceId = ""),
  127. (this.PendingMovingTraceId = ""),
  128. (this.inMovingMode = false),
  129. (this.StartMovingTs = 0),
  130. (this.PendingStartMovingTs = 0),
  131. (this.moveEvent = ""),
  132. (this.MoveToFrameCnt = 0),
  133. (this.lastIsMoving = 0),
  134. (this.MoveResponseDelay = 0),
  135. (this.MoveProcessDelay = 0),
  136. (this.MoveDisplayDelay = 0),
  137. (this.lastMoveResponseTime = 0),
  138. (this.lastMoveProcessTime = 0),
  139. (this.lastMoveDisplayTime = 0),
  140. (this.moveResponseCircular = new CircularArray(120, true, [
  141. STUCK_STAGE_GOOD,
  142. STUCK_STAGE_WELL,
  143. STUCK_STAGE_FAIR,
  144. STUCK_STAGE_BAD,
  145. ])),
  146. (this.moveProcessCircular = new CircularArray(120, true, [
  147. STUCK_STAGE_GOOD,
  148. STUCK_STAGE_WELL,
  149. STUCK_STAGE_FAIR,
  150. STUCK_STAGE_BAD,
  151. ])),
  152. (this.moveDisplayCircular = new CircularArray(120, true, [
  153. STUCK_STAGE_GOOD,
  154. STUCK_STAGE_WELL,
  155. STUCK_STAGE_FAIR,
  156. STUCK_STAGE_BAD,
  157. ])),
  158. (this.moveStartPts = -1),
  159. (this.frameServerCircular = new CircularArray(120, false, [])),
  160. (this.srvMetaIntervalCircular = new CircularArray(120, false, [])),
  161. (this.srvMediaIntervalCircular = new CircularArray(120, false, [])),
  162. (this.srvHBMetaIntervalCircular = new CircularArray(120, false, [])),
  163. (this.srvHBMetaIntervalSrvSentCircular = new CircularArray(
  164. 120,
  165. false,
  166. []
  167. )),
  168. (this.frameClientCircular = new CircularArray(120, false, [])),
  169. (this.unmarshalStreamExecutionArray = new SecondArray()),
  170. (this.receiveYUVExecutionArray = new SecondArray()),
  171. (this.postMessageWaitArray = new SecondArray()),
  172. (this.firstUpdateYUV = true),
  173. (this.functionMap = new Map()),
  174. (this.WASM_VERSION = "WASM-1.1"),
  175. (this.frameHistory = []),
  176. (this.getVersion = function () {
  177. return DECODER_VERSION;
  178. }),
  179. (this.downloadBlob = (r, n, o) => {
  180. const a = new Blob([r], {
  181. type: o,
  182. }),
  183. s = window.URL.createObjectURL(a);
  184. this.downloadURL(s, n),
  185. setTimeout(function () {
  186. return window.URL.revokeObjectURL(s);
  187. }, 1e3);
  188. }),
  189. (this.downloadURL = function (r, n) {
  190. const o = document.createElement("a");
  191. (o.href = r),
  192. (o.download = n),
  193. document.body.appendChild(o),
  194. (o.style.display = "none"),
  195. o.click(),
  196. o.remove();
  197. }),
  198. (this.Stringify = function (r) {
  199. let n = "";
  200. for (let a = 0; a < r.length / 8192; a++)
  201. n += String.fromCharCode.apply(
  202. null,
  203. r.slice(a * 8192, (a + 1) * 8192)
  204. );
  205. return n;
  206. }),
  207. (this._rtcp = e);
  208. }
  209. registerLogger(e) {
  210. //defaultLogger = e
  211. }
  212. registerFunction(e, t) {
  213. this.functionMap.set(e, t);
  214. }
  215. hasFrmCntInCache(e) {
  216. let t = -1;
  217. for (let r = 0; r < this.cacheFrameComposes.length; r++)
  218. this.cacheFrameComposes[r].frameCnt == e && (t = r);
  219. return t;
  220. }
  221. requestPanoramaTest(e, t, r, n, o) {
  222. const a = o,
  223. s = {
  224. action_type: 16,
  225. change_rotation_render_type_action: {
  226. render_type: 5,
  227. player: {
  228. position: {
  229. x: 0,
  230. y: 0,
  231. z: 0,
  232. },
  233. angle: {
  234. yaw: 0,
  235. pitch: 0,
  236. roll: 0,
  237. },
  238. },
  239. camera: {
  240. position: {
  241. x: e,
  242. y: t,
  243. z: r,
  244. },
  245. angle: {
  246. yaw: 0,
  247. pitch: 0,
  248. roll: 0,
  249. },
  250. },
  251. client_pano_titles_bitmap: n,
  252. },
  253. trace_id: a,
  254. user_id: this.userId_test,
  255. packet_id: a,
  256. };
  257. defaultLogger.debug("send data: ", s), this._rtcp.sendData(s);
  258. }
  259. onRotateInPanoMode(e) {
  260. const t = e.traceId,
  261. r = {};
  262. (r.width = 1280),
  263. (r.height = 720),
  264. (r.horz_fov = 92),
  265. (r.angle = {
  266. yaw: 100,
  267. pitch: 30,
  268. });
  269. const n = new ArrayBuffer(8),
  270. o = new DataView(n);
  271. getTilesInView(r, n);
  272. const a = n.slice(0);
  273. this.PendingMasks.unshift({
  274. buffer: a,
  275. angle: r.angle,
  276. }),
  277. MaskSetToOne(18, this.PanoView),
  278. operateForDataView(o, this.PanoView, o, (s, l) => s ^ (s & l)),
  279. this.requestPanoramaTest(
  280. 0,
  281. 0,
  282. 0,
  283. [
  284. o.getUint8(0),
  285. o.getUint8(1),
  286. o.getUint8(2),
  287. o.getUint8(3),
  288. o.getUint8(4),
  289. o.getUint8(5),
  290. o.getUint8(6),
  291. o.getUint8(7),
  292. ],
  293. t
  294. );
  295. }
  296. processMetaWithTraceId(e) {
  297. for (const i of e.traceIds) {
  298. if (this.traceIdMap.has(i)) {
  299. const o = this.traceIdMap.get(i);
  300. o != null && ((o.receiveTime = Date.now()), (o.status = 1));
  301. }
  302. if (this.joyStickTraceIdMap.has(i)) {
  303. const o = this.joyStickTraceIdMap.get(i);
  304. o != null && ((o.receiveTime = Date.now()), (o.status = 1));
  305. }
  306. if (i == this.PendingMovingTraceId) {
  307. (this.inMovingMode = true),
  308. (this.MovingTraceId = this.PendingMovingTraceId),
  309. (this.StartMovingTs = this.PendingStartMovingTs),
  310. (this.PendingMovingTraceId = ""),
  311. (this.PendingStartMovingTs = 0),
  312. defaultLogger.info(
  313. "MoveTo TraceId match",
  314. this.StartMovingTs,
  315. Date.now()
  316. );
  317. const o = Date.now();
  318. (this.lastMoveResponseTime = o),
  319. (this.lastMoveProcessTime = o),
  320. (this.lastMoveDisplayTime = o),
  321. this.frameServerCircular.clear(),
  322. this.frameClientCircular.clear();
  323. }
  324. }
  325. }
  326. onTraceId(e, i = this) {
  327. const o = e.traceId,
  328. s = e.timestamp,
  329. c = e.event;
  330. if (c === "Rotation") {
  331. const d = {
  332. traceId: o,
  333. pts: 0,
  334. startTime: s,
  335. receiveTime: 0,
  336. readyTime: 0,
  337. displayTime: 0,
  338. status: 0,
  339. };
  340. this.traceIdMap.set(o, d);
  341. const _ = setTimeout(() => {
  342. if ((_ && clearTimeout(_), this.traceIdMap.has(o))) {
  343. const b = this.traceIdMap.get(o);
  344. switch (b == null ? void 0 : b.status) {
  345. case 0: {
  346. this.responseMiss += 1;
  347. break;
  348. }
  349. case 1: {
  350. this.processMiss += 1;
  351. const k = b.receiveTime - b.startTime;
  352. this.responseTimeArray.push(k);
  353. break;
  354. }
  355. case 2: {
  356. this.displayMiss += 1;
  357. const k = b.receiveTime - b.startTime,
  358. j = b.readyTime - b.receiveTime;
  359. this.responseTimeArray.push(k), this.processTimeArray.push(j);
  360. break;
  361. }
  362. case 3:
  363. defaultLogger.debug("status is 3");
  364. }
  365. }
  366. }, 1e3);
  367. } else if (c === "Joystick") {
  368. const d = {
  369. traceId: o,
  370. pts: 0,
  371. startTime: s,
  372. receiveTime: 0,
  373. readyTime: 0,
  374. displayTime: 0,
  375. status: 0,
  376. };
  377. this.joyStickTraceIdMap.set(o, d);
  378. const _ = setTimeout(() => {
  379. if ((_ && clearTimeout(_), this.joyStickTraceIdMap.has(o))) {
  380. const b = this.joyStickTraceIdMap.get(o);
  381. switch (b == null ? void 0 : b.status) {
  382. case 0: {
  383. this.joyStickResponseMiss += 1;
  384. break;
  385. }
  386. case 1: {
  387. this.joyStickProcessMiss += 1;
  388. const k = b.receiveTime - b.startTime;
  389. this.joyStickResponseTimeArray.push(k);
  390. break;
  391. }
  392. case 2: {
  393. this.joyStickDisplayMiss += 1;
  394. const k = b.receiveTime - b.startTime,
  395. j = b.readyTime - b.receiveTime;
  396. this.joyStickResponseTimeArray.push(k),
  397. this.joyStickProcessTimeArray.push(j);
  398. break;
  399. }
  400. case 3:
  401. defaultLogger.debug("status is 3");
  402. }
  403. }
  404. }, 1e3);
  405. } else
  406. c === "MoveTo"
  407. ? (defaultLogger.info("receive moveto traceId ", o, " at timestamp", s),
  408. (this.PendingMovingTraceId = o),
  409. (this.PendingStartMovingTs = s),
  410. (this.moveEvent = c),
  411. this.frameServerCircular.clear())
  412. : c === "GetOnAirship" || c === "GetOnVehicle"
  413. ? (defaultLogger.info(
  414. "receive airship traceId ",
  415. o,
  416. " at timestamp ",
  417. s
  418. ),
  419. (this.PendingMovingTraceId = o),
  420. (this.PendingStartMovingTs = s),
  421. (this.moveEvent = c),
  422. this.frameServerCircular.clear())
  423. : (c === "GetOffAirship" || c === "GetOffVehicle") &&
  424. this.clearMoveArray();
  425. }
  426. executeFunction(e, t) {
  427. if (this.functionMap.has(e)) {
  428. const r = this.functionMap.get(e);
  429. r != null && r(t);
  430. }
  431. }
  432. UpdateStats(e) {
  433. var i;
  434. (i = this._rtcp.connection) == null ||
  435. i.getStats(null).then((o) => {
  436. o.forEach((s) => {
  437. s.type == "data-channel" &&
  438. ((this.rtcMessageReceived = s.messagesReceived - s.messagesSent),
  439. (this.rtcBytesReceived = s.bytesReceived));
  440. });
  441. }),
  442. (this.receivedMedia_worker = e.data.framesReceived),
  443. (this.receivedYUV = e.data.framesDecoded),
  444. (this.receivedEmit = e.data.framesRendered),
  445. (this.mediaBytesReceived = e.data.mediaBytesReceived),
  446. (this.metaBytesReceived = e.data.metaBytesReceived),
  447. (this.packetsLost = e.data.packetsLost),
  448. (this.packetsDrop = e.data.packetsDrop),
  449. (this.framesAwait = e.data.framesAwait),
  450. (this.decodeTimePerFrame = e.data.decodeTimePerFrame),
  451. (this.decodeTimeMaxFrame = e.data.decodeTimeMaxFrame),
  452. (this.returnFrames = e.data.framesReturned),
  453. (this.sendOutBuffer = e.data.sendOutBuffer),
  454. (this.DecodeJankTimes = e.data.JankTimes),
  455. (this.bigDecodeJankTimes = e.data.bigJankTimes),
  456. (this.receiveIframes = e.data.receivedIframe),
  457. (this.decodeIframes = e.data.decodedIframe);
  458. }
  459. ReceiveDecodeMessage(e) {
  460. var b;
  461. const i = Date.now(),
  462. o = i - e.data.postTs;
  463. this.postMessageWaitArray.add(o);
  464. if (!this.firstYUVReceived) {
  465. this.firstYUVDecoded = e.data.yuv_ts;
  466. const k = this.firstYUVDecoded - this.rtcp.network.room._startTime;
  467. defaultLogger.infoAndReportMeasurement({
  468. metric: "firstYUVDecodedAt",
  469. value: k,
  470. group: "joinRoom",
  471. });
  472. this.firstRender = Date.now();
  473. const j = this.firstYUVDecoded - this.rtcp.network.room._startTime;
  474. defaultLogger.infoAndReportMeasurement({
  475. metric: "firstRenderAt",
  476. value: j,
  477. group: "joinRoom",
  478. });
  479. this.firstYUVReceived = true;
  480. this.lastRenderTs = Date.now();
  481. }
  482. if (
  483. !this.cachedRender[this.setPtr] &&
  484. this.cachedMetas[this.setPtr] != null
  485. ) {
  486. if (
  487. this.cachedStreams[this.setPtr] != null &&
  488. this.cachedStreams[this.setPtr].byteLength != 0
  489. ) {
  490. if (e.data.data == null) {
  491. this.executeFunction("stream", {
  492. stream: this.cachedStreams[this.setPtr],
  493. width: this.cachedResolution[this.setPtr].width,
  494. height: this.cachedResolution[this.setPtr].height,
  495. pts: this.cachedPtss[this.setPtr],
  496. });
  497. this.executeFunction("signal", {
  498. signal: this.cachedMetas[this.setPtr],
  499. pts: this.cachedPtss[this.setPtr],
  500. alreadyUpdateYUV: true,
  501. });
  502. } else {
  503. this.updateDropFrame += 1;
  504. }
  505. //重新decode cache ptr
  506. console.log("this.cachedStreams", this.cachedStreams, this.setPtr);
  507. this.decoderWorker.postMessage(
  508. {
  509. t: 2,
  510. frameCnt: this.cachedPtss[this.setPtr],
  511. buffer: this.cachedStreams[this.setPtr],
  512. },
  513. [this.cachedStreams[this.setPtr].buffer]
  514. );
  515. }
  516. this.getPtr = (this.getPtr + 1) % this.cachedLength;
  517. }
  518. const s = e.data.metadata;
  519. if ((b = s == null ? void 0 : s.traceIds) != null && b.length)
  520. for (const k of s.traceIds) {
  521. if (this.traceIdMap.has(k)) {
  522. const j = this.traceIdMap.get(k);
  523. j != null && ((j.readyTime = Date.now()), (j.status = 2));
  524. }
  525. if (this.joyStickTraceIdMap.has(k)) {
  526. const j = this.joyStickTraceIdMap.get(k);
  527. j != null && ((j.readyTime = Date.now()), (j.status = 2));
  528. }
  529. }
  530. e.data.pts == this.moveStartPts &&
  531. (this.MoveProcessDelay = Date.now() - this.StartMovingTs);
  532. this.userId_test = this.rtcp.network.room.userId;
  533. if (this.inMovingMode) {
  534. const k = Date.now(),
  535. j = k - this.lastMoveProcessTime;
  536. this.moveProcessCircular.add(j), (this.lastMoveProcessTime = k);
  537. }
  538. const c = this.setPtr;
  539. this.cachedStreams[c] = e.data.data;
  540. this.cachedMetas[c] = e.data.metadata;
  541. this.cachedPtss[c] = e.data.pts;
  542. this.cachedRender[c] = false;
  543. this.cachedResolution[c] = {
  544. width: e.data.width,
  545. height: e.data.height,
  546. };
  547. this.setPtr = (this.setPtr + 1) % this.cachedLength;
  548. const _ = Date.now() - i;
  549. this.receiveYUVExecutionArray.add(_);
  550. }
  551. SendCacheFrameInfo(e) {
  552. var h, f, d, _, g, m, v;
  553. const t = e.data.cachedKey,
  554. r = e.data.metadata,
  555. n = t,
  556. o = r,
  557. a =
  558. (d =
  559. (f =
  560. (h = o.newUserStates) == null
  561. ? void 0
  562. : h.find((y) => y.userId === this.rtcp.network.room.userId)) ==
  563. null
  564. ? void 0
  565. : f.playerState) == null
  566. ? void 0
  567. : d.roomTypeId,
  568. s = this.rtcp.network.room.skinId,
  569. l =
  570. (v =
  571. (m =
  572. (g =
  573. (_ = o.newUserStates) == null
  574. ? void 0
  575. : _.find((y) => y.userId === this._rtcp.network.room.userId)) ==
  576. null
  577. ? void 0
  578. : g.playerState) == null
  579. ? void 0
  580. : m.player) == null
  581. ? void 0
  582. : v.position,
  583. u = {
  584. MsgType: 1,
  585. FrameCacheMsg: {
  586. FrameIndex: n,
  587. RoomTypeId: a,
  588. SkinID: s,
  589. Position: l,
  590. },
  591. };
  592. let c = "";
  593. try {
  594. c = JSON.stringify(u);
  595. } catch (y) {
  596. defaultLogger.error(y);
  597. return;
  598. }
  599. }
  600. ReceivePanoramaDecodeMessage(e) {
  601. defaultLogger.info("Receive Panorama Image in Workers.ts"),
  602. MaskSetToOne(e.data.tileId, this.PanoView);
  603. let i = 0,
  604. o;
  605. const s = this.PendingMasks.length;
  606. for (i = 0; i < s; i++) {
  607. const c = this.PendingMasks[i].buffer,
  608. d = new DataView(c),
  609. _ = new ArrayBuffer(8),
  610. b = new DataView(_);
  611. if (
  612. (operateForDataView(this.PanoView, d, b, (k, j) => j ^ (k & j)),
  613. IsAll0(b))
  614. ) {
  615. o = this.PendingMasks[i].angle;
  616. break;
  617. }
  618. }
  619. for (let c = i; c < s; c++) this.PendingMasks.pop();
  620. this.executeFunction("panorama", {
  621. data: e.data.data,
  622. metadata: e.data.metadata,
  623. tileId: e.data.tileId,
  624. finished: true,
  625. matchAngle: o,
  626. });
  627. }
  628. enable_decoder_queue_logging() {
  629. this.decoderWorker.postMessage({
  630. t: 100,
  631. status: true,
  632. });
  633. }
  634. disable_decoder_queue_logging() {
  635. this.decoderWorker.postMessage({
  636. t: 100,
  637. status: false,
  638. });
  639. }
  640. async init(
  641. e = {
  642. width: 1280,
  643. height: 720,
  644. userID: "testUserId",
  645. pageSession: "pagesession1",
  646. serverSession: "serversession1",
  647. }
  648. ) {
  649. for (let o = 0; o < FRAME_COMPOSE_LENGTH; o++) {
  650. const s = {
  651. buffer: new Uint8Array(262144),
  652. size: 0,
  653. startReceiveTime: 0,
  654. serverTime: 0,
  655. frameCnt: -1,
  656. };
  657. this.cacheFrameComposes.push(s);
  658. }
  659. USER_ID = e.userID;
  660. PAGE_SESSION = e.pageSession;
  661. SERVER_SESSION = e.serverSession;
  662. // console.log("v2Decoder", v2Decoder);
  663. const i = new Blob([v2Decoder], {
  664. type: "application/javascript",
  665. });
  666. this.decoderWorker = new Worker(URL.createObjectURL(i));
  667. // this.enable_decoder_queue_logging();
  668. this.decoderWorker.postMessage({
  669. t: 9,
  670. url: WASM_URLS[WASM_Version],
  671. jitterLength: DECODER_PASSIVE_JITTER,
  672. });
  673. this.decoderWorker.postMessage({
  674. t: 1,
  675. config: e,
  676. });
  677. return new Promise((o) => {
  678. this.decoderWorker.onmessage = (s) => {
  679. switch (s.data.t) {
  680. case 0:
  681. this.ReceiveDecodeMessage(s);
  682. break;
  683. case 1:
  684. this.UpdateStats(s);
  685. break;
  686. case 2:
  687. o();
  688. break;
  689. case 3:
  690. this.SendCacheFrameInfo(s);
  691. break;
  692. case 4: {
  693. const c = new Date().toISOString(),
  694. d =
  695. USER_ID +
  696. "-" +
  697. PAGE_SESSION +
  698. "-" +
  699. SERVER_SESSION +
  700. "-" +
  701. c +
  702. ".264";
  703. console.log(" s.data.fileObj", s.data.fileObj);
  704. // uploadStream(COS_PREFIX + d, s.data.fileObj);
  705. break;
  706. }
  707. case 5:
  708. // console.log(
  709. // "接受webrtc请求(来自worker):" + JSON.stringify(s.data.metadata)
  710. // );
  711. this.executeFunction("signal", {
  712. signal: s.data.metadata,
  713. pts: -1,
  714. alreadyUpdateYUV: false,
  715. });
  716. break;
  717. case 6:
  718. defaultLogger.infoAndReportMeasurement(s.data.data),
  719. defaultLogger.debug("WASM Ready Cost");
  720. break;
  721. case 7:
  722. this.ReceivePanoramaDecodeMessage(s);
  723. break;
  724. case 8: {
  725. const c = {
  726. MstType: 0,
  727. };
  728. let d = "";
  729. try {
  730. d = JSON.stringify(c);
  731. } catch (b) {
  732. defaultLogger.error(b);
  733. return;
  734. }
  735. const _ = "wasm:" + d;
  736. this._rtcp.sendStringData(_);
  737. break;
  738. }
  739. case 9: {
  740. defaultLogger.info(s.data.printMsg);
  741. break;
  742. }
  743. case 10: {
  744. defaultLogger.error(s.data.printMsg),
  745. this.executeFunction("error", {
  746. code: s.data.code,
  747. message: s.data.printMsg,
  748. });
  749. break;
  750. }
  751. default:
  752. defaultLogger.error("Receive unknown message event from decoder"),
  753. defaultLogger.debug(s.data);
  754. break;
  755. }
  756. };
  757. });
  758. }
  759. setPageSession(e) {
  760. PAGE_SESSION = e;
  761. }
  762. UpdateYUV() {
  763. var i, o;
  764. const e = this.getPtr;
  765. // console.log("UpdateYUV", this.getPtr);
  766. // console.log("UpdateYUV", this.cachedMetas[e]);
  767. // console.log("UpdateYUV", this.cachedStreams[e]);
  768. if (this.cachedMetas[e] != null && !this.cachedRender[e]) {
  769. const s = Date.now();
  770. if (this.firstUpdateYUV) {
  771. const $ =
  772. ((i = this.cachedStreams[e]) == null ? void 0 : i.byteLength) || 0;
  773. defaultLogger.infoAndReportMeasurement({
  774. metric: "firstUpdateStreamLength",
  775. value: $,
  776. group: "joinRoom",
  777. });
  778. this.firstUpdateYUV = false;
  779. }
  780. if (this.cachedStreams[e] != null) {
  781. console.log("cachedStreams", this.cachedStreams[e], e);
  782. this.executeFunction("stream", {
  783. stream: this.cachedStreams[e],
  784. width: this.cachedResolution[e].width,
  785. height: this.cachedResolution[e].height,
  786. pts: this.cachedPtss[e],
  787. });
  788. }
  789. const c = Date.now();
  790. this.cachedStreams[e] != null &&
  791. this.decoderWorker.postMessage(
  792. {
  793. t: 2,
  794. frameCnt: this.cachedPtss[e],
  795. buffer: this.cachedStreams[e],
  796. },
  797. [this.cachedStreams[e].buffer]
  798. );
  799. const d = Date.now(),
  800. _ = c - s,
  801. b = d - c;
  802. (_ > 33 || b > 10) &&
  803. defaultLogger.debug(
  804. "[wwwarning] updateYUV takes ",
  805. _,
  806. " ms, postMessage takes ",
  807. b,
  808. " ms for index ",
  809. this.cachedPtss[e]
  810. ),
  811. c - this.lastRenderTs > 84 && this.JankTimes++,
  812. c - this.lastRenderTs > 125 && this.bigJankTimes++,
  813. (this.lastRenderTs = c);
  814. const k = c - s;
  815. this.updateYUVCircular.add(k);
  816. const j = this.cachedMetas[e];
  817. if ((o = j == null ? void 0 : j.traceIds) != null && o.length)
  818. for (const $ of j.traceIds) {
  819. if (this.traceIdMap.has($)) {
  820. const _e = this.traceIdMap.get($);
  821. if (_e != null) {
  822. (_e.displayTime = Date.now()), (_e.status = 3);
  823. const et = _e.receiveTime - _e.startTime,
  824. tt = _e.readyTime - _e.receiveTime,
  825. rt = _e.displayTime - _e.readyTime,
  826. it = _e.displayTime - _e.startTime;
  827. this.responseTimeArray.push(et),
  828. this.processTimeArray.push(tt),
  829. this.displayTimeArray.push(rt),
  830. this.overallTimeArray.push(it),
  831. this.traceIdMap.delete($);
  832. }
  833. }
  834. if (this.joyStickTraceIdMap.has($)) {
  835. const _e = this.joyStickTraceIdMap.get($);
  836. if (_e != null) {
  837. (_e.displayTime = Date.now()), (_e.status = 3);
  838. const et = _e.receiveTime - _e.startTime,
  839. tt = _e.readyTime - _e.receiveTime,
  840. rt = _e.displayTime - _e.readyTime,
  841. it = _e.displayTime - _e.startTime;
  842. this.joyStickResponseTimeArray.push(et),
  843. this.joyStickProcessTimeArray.push(tt),
  844. this.joyStickDisplayTimeArray.push(rt),
  845. this.joyStickOverallTimeArray.push(it),
  846. this.joyStickTraceIdMap.delete($);
  847. }
  848. }
  849. }
  850. this.cachedPtss[e] == this.moveStartPts &&
  851. (this.MoveDisplayDelay = Date.now() - this.StartMovingTs);
  852. if (this.inMovingMode) {
  853. const $ = Date.now(),
  854. _e = $ - this.lastMoveDisplayTime;
  855. this.moveDisplayCircular.add(_e), (this.lastMoveDisplayTime = $);
  856. }
  857. // console.log(
  858. // "接受webrtc请求(定时执行UpdateYUV):" +
  859. // JSON.stringify(this.cachedMetas[e])
  860. // );
  861. // console.log('回传',{
  862. // signal: this.cachedMetas[e],
  863. // pts: this.cachedPtss[e],
  864. // alreadyUpdateYUV: true,
  865. // })
  866. this.executeFunction("signal", {
  867. signal: this.cachedMetas[e],
  868. pts: this.cachedPtss[e],
  869. alreadyUpdateYUV: true,
  870. });
  871. this.cachedRender[e] = true;
  872. this.getPtr = (this.getPtr + 1) % this.cachedLength;
  873. }
  874. }
  875. unmarshalPano(e) {
  876. return false;
  877. }
  878. unmarshalPanobk(e) {
  879. const i = new DataView(e),
  880. o = i.getUint32(0);
  881. if (o == 1723558763) return true;
  882. if (o != 2296221069) return false;
  883. console.log("Receive Pano Message"), i.getUint16(4);
  884. const s = i.getUint16(6),
  885. c = i.getUint32(8),
  886. d = i.getUint32(12),
  887. _ = i.getUint32(16),
  888. b = i.getUint32(20),
  889. k = e.byteLength - s;
  890. if (k == d) {
  891. const j = new Uint8Array(e).subarray(s).slice(0, _),
  892. _e = {
  893. data: new Uint8Array(e).subarray(s).slice(_, d),
  894. mediaLen: d - _,
  895. metadata: j,
  896. metaLen: _,
  897. tileId: c,
  898. };
  899. this.decoderWorker.postMessage({
  900. t: 8,
  901. data: _e,
  902. });
  903. } else {
  904. const j = new Uint8Array(e, s, k);
  905. if (this.cachePanoTileID == c) {
  906. if (
  907. (this.panoCacheBuffer.set(j, b),
  908. (this.panoCacheSize += k),
  909. this.panoCacheSize === d)
  910. ) {
  911. const $ = new Uint8Array(this.panoCacheBuffer).slice(0, _),
  912. et = {
  913. data: new Uint8Array(this.panoCacheBuffer).slice(_, d),
  914. mediaLen: d - _,
  915. metadata: $,
  916. metaLen: _,
  917. tileId: c,
  918. };
  919. this.decoderWorker.postMessage({
  920. t: 8,
  921. data: et,
  922. }),
  923. (this.panoCacheSize = 0);
  924. }
  925. } else
  926. this.panoCacheBuffer.set(j, b),
  927. (this.panoCacheSize = k),
  928. (this.cachePanoTileID = c);
  929. }
  930. return true;
  931. }
  932. clearMoveArray() {
  933. (this.MovingTraceId = ""),
  934. (this.inMovingMode = false),
  935. (this.StartMovingTs = 0),
  936. (this.MoveToFrameCnt = 0),
  937. (this.MoveResponseDelay = 0),
  938. (this.MoveProcessDelay = 0),
  939. (this.MoveDisplayDelay = 0),
  940. (this.moveStartPts = -1),
  941. this.moveResponseCircular.clear(),
  942. this.moveProcessCircular.clear(),
  943. this.moveDisplayCircular.clear(),
  944. (this.moveEvent = "");
  945. }
  946. getIsMoving(e) {
  947. let i;
  948. if (typeof e.newUserStates != "undefined")
  949. for (let o = 0; o < e.newUserStates.length; o++) {
  950. const s = e.newUserStates[o];
  951. if (s.userId == this.rtcp.network.room.userId) {
  952. i = s.renderInfo.isMoving;
  953. break;
  954. }
  955. }
  956. return i;
  957. }
  958. isHeartBeatPacket(e, i) {
  959. return new DataView(e).getUint32(0) == 2009889916;
  960. }
  961. resetSendTimeDiff() {
  962. (this.prevSenderTs = 0), this.serverSendTimeArray.clear();
  963. }
  964. calcSendTimeDiff(e) {
  965. if (this.prevSenderTs == -1) {
  966. this.prevSenderTs = e;
  967. return;
  968. }
  969. const i = e - this.prevSenderTs;
  970. this.serverSendTimeArray.add(i), (this.prevSenderTs = e);
  971. }
  972. // new unmarshalStream
  973. unmarshalStreamBk(data) {
  974. const dateView = new DataView(data);
  975. // const pts = Date.now();
  976. if (dateView.getUint32(0) != 1437227610) return false; // flag auth
  977. return true;
  978. }
  979. // backup main unmarshalStreamBk
  980. unmarshalStream(e) {
  981. var lt, ft, ht, pt, dt, _t, mt, vt, yt, Et;
  982. const i = Date.now();
  983. const o = new DataView(e);
  984. if (o.getUint32(0) != 1437227610) return false; // 32位0bit 流标识位
  985. o.getUint16(4); // 2 ?暂时未用
  986. const c = o.getUint16(6), // 16位6bit 36 ? 可能是混数
  987. d = o.getUint16(8), // 16位8bit 存当前帧数
  988. fCnt = d,
  989. b = o.getUint16(10); // 1 3 255 1,3 ? 方向 空包 255 可能是 DIR 方向
  990. let k = false;
  991. b == 1 && (k = true); //
  992. const j = o.getUint32(12), // 16位12bit ? 帧 byteLength 长度 metaLen
  993. $ = o.getUint32(16), // 16位16bit 有效流总大小 0 是空包 metaLen
  994. _e = o.getUint32(20), // calcSendTimeDiff 与时间有关
  995. et = o.getUint16(24), // 大多数为 0,待定 与inPanoMode 开关有关系
  996. tt = o.getUint16(26), // 0 / 1 待定 与用户状态有关
  997. rt = o.getUint32(28), // 0 cachedKey
  998. it = o.getUint32(c - 4), // ?同步I帧或大小slice steam切片 按开始大小 $是总大小
  999. nt = j + $, // ?cnt 宏块 总包大小
  1000. ot = e.byteLength - c, // cnt 宏块 有效长段
  1001. at = new Uint8Array(e, c, ot); // e流 真实有效 以32位前6bit 可能是留白,
  1002. this.calcSendTimeDiff(_e);
  1003. // console.log("流--", e.byteLength);
  1004. console.log("gemer", {
  1005. byteLength: e.byteLength,
  1006. frame: fCnt,
  1007. mediaLen: $,
  1008. metaLen: j,
  1009. serverTime: _e,
  1010. it: it,
  1011. start: c,
  1012. end: ot,
  1013. total: nt,
  1014. isPureMeta: ot === nt,
  1015. at: at,
  1016. });
  1017. // debugger;
  1018. const down = Date.now();
  1019. let st;
  1020. if (this.inPanoMode && ($ > 0 || et))
  1021. return (
  1022. defaultLogger.error(
  1023. "Stream Protocal Violation: receive illegal stream in Pano mode"
  1024. ),
  1025. true
  1026. );
  1027. // cnt总包大小 === cnt 有效长段
  1028. if (ot === nt) {
  1029. this.receivedMedia++;
  1030. // ?宏块位置重新较正, c是留白,从留白开计?
  1031. const At = new Uint8Array(e).subarray(c);
  1032. _e - this.lastServerTS > 60
  1033. ? this.serverFrameSlow++
  1034. : _e - this.lastServerTS < 16 && this.serverFrameFast++;
  1035. const gt = Date.now();
  1036. gt - this.lastClientTS > 60
  1037. ? this.clientFrameSlow++
  1038. : gt - this.lastClientTS < 16 && this.clientFrameFast++;
  1039. const St = $ === 0,
  1040. xt = _e - this.lastServerTS;
  1041. this.lastServerTS != 0 &&
  1042. ((d + 65536 - this.lastSeq) % 65536 === 1 &&
  1043. this.lastIsPureMeta == St &&
  1044. (St
  1045. ? this.srvMetaIntervalCircular.add(xt)
  1046. : this.srvMediaIntervalCircular.add(xt)),
  1047. this.frameServerCircular.add(xt),
  1048. this.frameClientCircular.add(gt - this.lastClientTS)),
  1049. (this.lastSeq = d),
  1050. (this.lastIsPureMeta = St),
  1051. (this.lastServerTS = _e),
  1052. (this.lastClientTS = gt);
  1053. const bt = At.subarray(0, j); // 将坐标插入流,再拿出来?
  1054. const Rt = Date.now();
  1055. const Mt = JSON.parse(this.Stringify(bt)); // json 与帧map的坐标数据
  1056. const Pt = Date.now();
  1057. console.log("gemer2", fCnt, it, Mt);
  1058. this.showAllReceivedMetadata && console.log(_e, gt, Mt);
  1059. // console.log("Pt - Rt", Pt - Rt, Pt, Rt);
  1060. this.metaParseArray.push(Pt - Rt);
  1061. (lt = Mt.traceIds) != null &&
  1062. lt.length &&
  1063. this.processMetaWithTraceId(Mt),
  1064. $ != 0 &&
  1065. this.moveStartPts == -1 &&
  1066. this.inMovingMode &&
  1067. (this.moveStartPts = d),
  1068. this.moveStartPts == d &&
  1069. ((this.MoveResponseDelay = Date.now() - this.StartMovingTs),
  1070. console.log(
  1071. "move response delay: ",
  1072. d,
  1073. this.moveStartPts,
  1074. this.MoveResponseDelay
  1075. ));
  1076. const It = this.getIsMoving(Mt);
  1077. if (
  1078. (this.inMovingMode &&
  1079. It == 0 &&
  1080. this.lastIsMoving == 1 &&
  1081. this.clearMoveArray(),
  1082. typeof It != "undefined" && (this.lastIsMoving = It),
  1083. this.inMovingMode)
  1084. ) {
  1085. const Ct = Date.now(),
  1086. Ot = Ct - this.lastMoveResponseTime;
  1087. this.moveResponseCircular.add(Ot), (this.lastMoveResponseTime = Ct);
  1088. }
  1089. // 流与状态有关
  1090. (et || tt) &&
  1091. (st =
  1092. (dt =
  1093. (pt =
  1094. (ht =
  1095. (ft = Mt.newUserStates) == null
  1096. ? void 0
  1097. : ft.find(
  1098. (Ct) => Ct.userId === this._rtcp.network.room.userId
  1099. )) == null
  1100. ? void 0
  1101. : ht.playerState) == null
  1102. ? void 0
  1103. : pt.player) == null
  1104. ? void 0
  1105. : dt.position);
  1106. //封装格式
  1107. const Dt = {
  1108. t: 0,
  1109. data: At,
  1110. mediaLen: $,
  1111. metaLen: j,
  1112. metadata: Mt,
  1113. frameCnt: fCnt,
  1114. server_ts: _e,
  1115. isIDR: k,
  1116. cacheRequest: tt,
  1117. cached: et,
  1118. cachedKey: rt,
  1119. position: st,
  1120. };
  1121. // console.warn('Dt',Dt);
  1122. if (this.inPanoMode) {
  1123. // console.log('接受webrtc请求:'+JSON.stringify(Mt));
  1124. this.executeFunction("signal", {
  1125. signal: Mt,
  1126. pts: -1,
  1127. alreadyUpdateYUV: true,
  1128. });
  1129. return true;
  1130. }
  1131. if (
  1132. (this.decoderWorker.postMessage(Dt, [At.buffer]),
  1133. !this.firstMediaReceived)
  1134. ) {
  1135. this.firstMediaArrival = Date.now();
  1136. const Ct = this.firstMediaArrival - this.rtcp.network.room._startTime;
  1137. defaultLogger.infoAndReportMeasurement({
  1138. metric: "firstMediaArravalAt",
  1139. value: Ct,
  1140. group: "joinRoom",
  1141. });
  1142. // this.downloadBlob(
  1143. // [At.buffer],
  1144. // `first.h264`,
  1145. // "application/octet-stream"
  1146. // );
  1147. this.firstMediaReceived = true;
  1148. }
  1149. } else {
  1150. const At = this.hasFrmCntInCache(fCnt);
  1151. // console.log("At", At, at, it);
  1152. // console.log(
  1153. // "this.cacheFrameComposes[At].buffer",
  1154. // this.cacheFrameComposes[At],
  1155. // );
  1156. // if (At !== -1) {
  1157. // console.log(
  1158. // "this.cacheFrameComposes[At].buffer",
  1159. // this.cacheFrameComposes[At].buffer
  1160. // );
  1161. // }
  1162. if (At != -1)
  1163. if (
  1164. (this.cacheFrameComposes[At].buffer.set(at, it),
  1165. (this.cacheFrameComposes[At].size += ot),
  1166. this.cacheFrameComposes[At].size === nt)
  1167. ) {
  1168. const gt = new Uint8Array(this.cacheFrameComposes[At].buffer).slice(
  1169. 0,
  1170. nt
  1171. );
  1172. this.cacheFrameComposes[At].frameCnt = -1;
  1173. this.cacheFrameComposes[At].size = 0;
  1174. this.cacheFrameComposes[At].startReceiveTime = 0;
  1175. this.cacheFrameComposes[At].serverTime = 0;
  1176. this.receivedMedia++;
  1177. _e - this.lastServerTS > 60
  1178. ? this.serverFrameSlow++
  1179. : _e - this.lastServerTS < 16 && this.serverFrameFast++;
  1180. const St = Date.now();
  1181. St - this.lastClientTS > 60
  1182. ? this.clientFrameSlow++
  1183. : St - this.lastClientTS < 16 && this.clientFrameFast++,
  1184. this.lastServerTS != 0 &&
  1185. (this.frameServerCircular.add(_e - this.lastServerTS),
  1186. this.frameClientCircular.add(St - this.lastClientTS)),
  1187. (this.lastServerTS = _e),
  1188. (this.lastClientTS = St);
  1189. const xt = gt.subarray(0, j);
  1190. // console.log("xt", xt);
  1191. const bt = Date.now();
  1192. const Rt = JSON.parse(this.Stringify(xt));
  1193. const Mt = Date.now();
  1194. this.showAllReceivedMetadata && console.log(_e, St, Rt),
  1195. this.metaParseArray.push(Mt - bt),
  1196. (_t = Rt.traceIds) != null &&
  1197. _t.length &&
  1198. this.processMetaWithTraceId(Rt),
  1199. $ != 0 &&
  1200. this.moveStartPts == -1 &&
  1201. this.inMovingMode &&
  1202. (this.moveStartPts = d),
  1203. this.moveStartPts == d &&
  1204. (this.MoveResponseDelay = Date.now() - this.StartMovingTs);
  1205. const Pt = this.getIsMoving(Rt);
  1206. if (
  1207. (this.inMovingMode &&
  1208. Pt == 0 &&
  1209. this.lastIsMoving == 1 &&
  1210. this.clearMoveArray(),
  1211. typeof Pt != "undefined" && (this.lastIsMoving = Pt),
  1212. this.inMovingMode)
  1213. ) {
  1214. const Dt = Date.now(),
  1215. Ct = Dt - this.lastMoveResponseTime;
  1216. this.moveResponseCircular.add(Ct), (this.lastMoveResponseTime = Dt);
  1217. }
  1218. (et || tt) &&
  1219. (st =
  1220. (Et =
  1221. (yt =
  1222. (vt =
  1223. (mt = Rt.newUserStates) == null
  1224. ? void 0
  1225. : mt.find(
  1226. (Dt) => Dt.userId === this._rtcp.network.room.userId
  1227. )) == null
  1228. ? void 0
  1229. : vt.playerState) == null
  1230. ? void 0
  1231. : yt.player) == null
  1232. ? void 0
  1233. : Et.position);
  1234. const It = {
  1235. t: 0,
  1236. data: gt,
  1237. mediaLen: $,
  1238. metaLen: j,
  1239. metadata: Rt,
  1240. frameCnt: fCnt,
  1241. server_ts: _e,
  1242. isIDR: k,
  1243. cacheRequest: tt,
  1244. cached: et,
  1245. cachedKey: rt,
  1246. position: st,
  1247. };
  1248. //有效Iframe
  1249. console.error("IT", fCnt, It);
  1250. console.error("IT", fCnt, JSON.stringify(Rt));
  1251. // console.log("IT-fCnt", It.data);
  1252. // this.downloadBlob(
  1253. // It.data.subarray(It.metaLen, It.metaLen + It.mediaLen),
  1254. // `${It.frameCnt}.v2p.264`,
  1255. // "application/octet-stream"
  1256. // );
  1257. if (this.inPanoMode) {
  1258. // console.log("接受webrtc请求:" + JSON.stringify(Rt));
  1259. this.executeFunction("signal", {
  1260. signal: Rt,
  1261. pts: -1,
  1262. alreadyUpdateYUV: true,
  1263. });
  1264. return true;
  1265. }
  1266. if (
  1267. (this.decoderWorker.postMessage(It, [gt.buffer]),
  1268. !this.firstMediaReceived)
  1269. ) {
  1270. // this.downloadBlob([gt.buffer], `gt.h264`, "application/octet-stream");
  1271. this.firstMediaArrival = Date.now();
  1272. const Dt =
  1273. this.firstMediaArrival - this.rtcp.network.room._startTime;
  1274. defaultLogger.infoAndReportMeasurement({
  1275. metric: "firstMediaArravalAt",
  1276. value: Dt,
  1277. group: "joinRoom",
  1278. });
  1279. this.firstMediaReceived = true;
  1280. }
  1281. } else
  1282. this.cacheFrameComposes[At].size > nt &&
  1283. defaultLogger.debug(
  1284. "I frame exceed, cache size is ",
  1285. this.cacheSize,
  1286. ", total size is ",
  1287. nt
  1288. );
  1289. else if (At == -1) {
  1290. let gt = this.hasFrmCntInCache(-1);
  1291. if (gt == -1) {
  1292. let St = Date.now() + 1e18,
  1293. xt = -1;
  1294. for (let bt = 0; bt < this.cacheFrameComposes.length; bt++)
  1295. this.cacheFrameComposes[bt].serverTime < St &&
  1296. ((St = this.cacheFrameComposes[bt].serverTime), (xt = bt));
  1297. gt = xt;
  1298. }
  1299. this.cacheFrameComposes[gt].buffer.set(at, it),
  1300. (this.cacheFrameComposes[gt].size = ot),
  1301. (this.cacheFrameComposes[gt].frameCnt = fCnt),
  1302. (this.cacheFrameComposes[gt].startReceiveTime = Date.now()),
  1303. (this.cacheFrameComposes[gt].serverTime = _e);
  1304. }
  1305. }
  1306. const ct = Date.now() - i;
  1307. return this.unmarshalStreamExecutionArray.add(ct), true;
  1308. }
  1309. reset() {
  1310. defaultLogger.debug("Worker reset is called"),
  1311. (this.cacheFrameCnt = 0),
  1312. (this.receivedMedia = 0),
  1313. (this.reconnectSignal = true),
  1314. this.decoderWorker.postMessage({
  1315. t: 4,
  1316. });
  1317. }
  1318. dataHandleOff(e) {
  1319. defaultLogger.debug("hhh");
  1320. }
  1321. dataHandle(e) {
  1322. if (this.saveframe) {
  1323. console.log("接受webrtc请求:录制视频");
  1324. this.decoderWorker.postMessage({
  1325. t: 6,
  1326. });
  1327. this.saveframe = false;
  1328. }
  1329. if (this.SaveMediaStream) {
  1330. console.log("接受webrtc请求:下载视频");
  1331. this.decoderWorker.postMessage({
  1332. t: 7,
  1333. }),
  1334. (this.SaveMediaStream = false);
  1335. }
  1336. const i = new Uint8Array(e);
  1337. if (i.length >= 4 && this.isHeartBeatPacket(i.buffer, i.length) == true) {
  1338. // console.log("接受webrtc请求:心跳");
  1339. return;
  1340. }
  1341. if (i.length > 36 && this.unmarshalStream(i.buffer) == true) {
  1342. this.reconnectSignal &&
  1343. (this.executeFunction("reconnectedFrame", {}),
  1344. (this.reconnectSignal = false));
  1345. // console.log("接受webrtc请求:视频流");
  1346. return;
  1347. }
  1348. if (i.length > 20 && this.unmarshalPano(i.buffer) == true) {
  1349. // console.log("接受webrtc请求:全景图");
  1350. return;
  1351. }
  1352. this.noWasmBytesReceived += e.byteLength;
  1353. const o = JSON.parse(this.Stringify(i));
  1354. // console.log("接受webrtc请求:" + JSON.stringify(o));
  1355. this.executeFunction("signal", {
  1356. signal: o,
  1357. pts: -1,
  1358. alreadyUpdateYUV: true,
  1359. });
  1360. }
  1361. changePanoMode(e) {
  1362. this.inPanoMode = e;
  1363. }
  1364. uploadDataToServer() {
  1365. // delete all
  1366. }
  1367. }