Workers.js 43 KB

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