Workers.js 45 KB

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