index.vue 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255
  1. <template>
  2. <div class="push-wrap">
  3. <div
  4. ref="topRef"
  5. class="left"
  6. >
  7. <div
  8. ref="containerRef"
  9. class="container"
  10. >
  11. <canvas
  12. id="pushCanvasRef"
  13. ref="pushCanvasRef"
  14. ></canvas>
  15. <div
  16. v-if="appStore.allTrack.filter((item) => !item.hidden).length <= 0"
  17. class="add-wrap"
  18. >
  19. <n-space>
  20. <n-button
  21. v-for="(item, index) in allMediaTypeList"
  22. :key="index"
  23. class="item"
  24. @click="handleStartMedia(item)"
  25. >
  26. {{ item.txt }}
  27. </n-button>
  28. </n-space>
  29. </div>
  30. </div>
  31. <div
  32. ref="bottomRef"
  33. class="room-control"
  34. >
  35. <div class="info">
  36. <div
  37. class="avatar"
  38. :style="{ backgroundImage: `url(${userStore.userInfo?.avatar})` }"
  39. ></div>
  40. <div class="detail">
  41. <div class="top">
  42. <n-input-group>
  43. <n-input
  44. v-model:value="roomName"
  45. size="small"
  46. placeholder="输入房间名"
  47. :style="{ width: '50%' }"
  48. />
  49. <n-button
  50. size="small"
  51. type="primary"
  52. @click="confirmRoomName"
  53. >
  54. 确定
  55. </n-button>
  56. </n-input-group>
  57. </div>
  58. <div class="bottom">
  59. <span v-if="NODE_ENV === 'development'">
  60. {{ mySocketId }}
  61. </span>
  62. </div>
  63. </div>
  64. </div>
  65. <div class="rtc">
  66. <div class="item">
  67. <div class="txt">码率设置</div>
  68. <div class="down">
  69. <n-select
  70. v-model:value="currentMaxBitrate"
  71. :options="maxBitrate"
  72. />
  73. </div>
  74. </div>
  75. <div class="item">
  76. <div class="txt">帧率设置</div>
  77. <div class="down">
  78. <n-select
  79. v-model:value="currentMaxFramerate"
  80. :options="maxFramerate"
  81. />
  82. </div>
  83. </div>
  84. <div class="item">
  85. <div class="txt">分辨率设置</div>
  86. <div class="down">
  87. <n-select
  88. v-model:value="currentResolutionRatio"
  89. :options="resolutionRatio"
  90. />
  91. </div>
  92. </div>
  93. </div>
  94. <div class="other">
  95. <div class="top">
  96. <span class="item">
  97. <i class="ico"></i>
  98. <span>
  99. 正在观看:
  100. {{
  101. liveUserList.filter((item) => item.id !== mySocketId).length
  102. }}
  103. </span>
  104. </span>
  105. </div>
  106. <div class="bottom">
  107. <n-button
  108. v-if="!roomLiving"
  109. type="info"
  110. size="small"
  111. @click="handleStartLive"
  112. >
  113. 开始直播
  114. </n-button>
  115. <n-button
  116. v-else
  117. type="error"
  118. size="small"
  119. @click="handleEndLive"
  120. >
  121. 结束直播
  122. </n-button>
  123. </div>
  124. </div>
  125. </div>
  126. </div>
  127. <div class="right">
  128. <div class="resource-card">
  129. <div class="title">素材列表</div>
  130. <div class="list">
  131. <div
  132. v-for="(item, index) in appStore.allTrack.filter(
  133. (item) => !item.hidden
  134. )"
  135. :key="index"
  136. class="item"
  137. >
  138. <span class="name">
  139. {{ NODE_ENV === 'development' ? item.id : '' }}({{
  140. mediaTypeEnumMap[item.type]
  141. }}){{ item.mediaName }}
  142. </span>
  143. <div class="control">
  144. <div
  145. v-if="item.audio === 1"
  146. class="control-item"
  147. @click="handleChangeMuted(item)"
  148. >
  149. <n-popover
  150. placement="top"
  151. trigger="hover"
  152. :flip="false"
  153. >
  154. <template #trigger>
  155. <n-icon size="16">
  156. <VolumeMuteOutline v-if="item.muted"></VolumeMuteOutline>
  157. <VolumeHighOutline v-else></VolumeHighOutline>
  158. </n-icon>
  159. </template>
  160. <div class="slider">
  161. <n-slider
  162. :value="item.volume"
  163. :step="1"
  164. @update-value="(v) => handleChangeVolume(item, v)"
  165. />
  166. </div>
  167. </n-popover>
  168. </div>
  169. <div
  170. class="control-item"
  171. @click="handleEdit(item)"
  172. >
  173. <n-icon size="16">
  174. <CreateOutline></CreateOutline>
  175. </n-icon>
  176. </div>
  177. <div
  178. class="control-item"
  179. @click="handleDel(item)"
  180. >
  181. <n-icon size="16">
  182. <Close></Close>
  183. </n-icon>
  184. </div>
  185. </div>
  186. </div>
  187. </div>
  188. <div class="bottom">
  189. <n-button
  190. size="small"
  191. type="primary"
  192. @click="showSelectMediaModalCpt = true"
  193. >
  194. 添加素材
  195. </n-button>
  196. </div>
  197. </div>
  198. <div class="danmu-card">
  199. <div class="title">弹幕互动</div>
  200. <div class="list-wrap">
  201. <div
  202. ref="danmuListRef"
  203. class="list"
  204. >
  205. <div
  206. v-for="(item, index) in damuList"
  207. :key="index"
  208. class="item"
  209. >
  210. <template v-if="item.msgType === DanmuMsgTypeEnum.danmu">
  211. <span class="name">
  212. <span v-if="item.userInfo">
  213. {{ item.userInfo.username }}[{{
  214. item.userInfo.roles?.map((v) => v.role_name).join()
  215. }}]
  216. </span>
  217. <span v-else>{{ item.socket_id }}[游客]</span>
  218. </span>
  219. <span>:</span>
  220. <span
  221. class="msg"
  222. v-if="!item.msgIsFile"
  223. >
  224. {{ item.msg }}
  225. </span>
  226. <div
  227. class="msg img"
  228. v-else
  229. >
  230. <img
  231. :src="item.msg"
  232. alt=""
  233. @load="handleScrollTop"
  234. />
  235. </div>
  236. </template>
  237. <template v-else-if="item.msgType === DanmuMsgTypeEnum.otherJoin">
  238. <span class="name system">系统通知:</span>
  239. <span class="msg">
  240. <span>{{ item.userInfo?.username || item.socket_id }}</span>
  241. <span>进入直播!</span>
  242. </span>
  243. </template>
  244. <template
  245. v-else-if="item.msgType === DanmuMsgTypeEnum.userLeaved"
  246. >
  247. <span class="name system">系统通知:</span>
  248. <span class="msg">
  249. <span>{{ item.userInfo?.username || item.socket_id }}</span>
  250. <span>离开直播!</span>
  251. </span>
  252. </template>
  253. </div>
  254. </div>
  255. </div>
  256. <div
  257. class="send-msg"
  258. v-loading="msgLoading"
  259. >
  260. <div class="control">
  261. <div
  262. class="ico face"
  263. title="表情"
  264. @click="handleWait"
  265. ></div>
  266. <div
  267. class="ico img"
  268. title="图片"
  269. @click="mockClick"
  270. >
  271. <input
  272. ref="uploadRef"
  273. type="file"
  274. class="input-upload"
  275. accept=".webp,.png,.jpg,.jpeg,.gif"
  276. @change="uploadChange"
  277. />
  278. </div>
  279. </div>
  280. <textarea
  281. v-model="danmuStr"
  282. class="ipt"
  283. @keydown="keydownDanmu"
  284. ></textarea>
  285. <div
  286. class="btn"
  287. @click="sendDanmu"
  288. >
  289. 发送
  290. </div>
  291. </div>
  292. <!-- <div class="send-msg">
  293. <input
  294. v-model="danmuStr"
  295. class="ipt"
  296. @keydown="keydownDanmu"
  297. />
  298. <n-button
  299. type="info"
  300. size="small"
  301. @click="sendDanmu"
  302. >
  303. 发送
  304. </n-button>
  305. </div> -->
  306. </div>
  307. </div>
  308. <SelectMediaModalCpt
  309. v-if="showSelectMediaModalCpt"
  310. :all-media-type-list="allMediaTypeList"
  311. @close="showSelectMediaModalCpt = false"
  312. @ok="handleShowMediaModalCpt"
  313. ></SelectMediaModalCpt>
  314. <MediaModalCpt
  315. v-if="showMediaModalCpt"
  316. :is-edit="isEdit"
  317. :media-type="currentMediaType"
  318. :init-data="currentMediaData"
  319. @close="showMediaModalCpt = false"
  320. @add-ok="addMediaOk"
  321. @edit-ok="editMediaOk"
  322. ></MediaModalCpt>
  323. <OpenMicophoneTipCpt
  324. v-if="showOpenMicophoneTipCpt"
  325. @close="showOpenMicophoneTipCpt = false"
  326. ></OpenMicophoneTipCpt>
  327. <NoLiveTipModalCpt
  328. v-if="showNoLiveTipModalCpt"
  329. @close="showNoLiveTipModalCpt = false"
  330. ></NoLiveTipModalCpt>
  331. </div>
  332. </template>
  333. <script lang="ts" setup>
  334. import {
  335. Close,
  336. CreateOutline,
  337. VolumeHighOutline,
  338. VolumeMuteOutline,
  339. } from '@vicons/ionicons5';
  340. import { fabric } from 'fabric';
  341. import {
  342. Raw,
  343. markRaw,
  344. onMounted,
  345. onUnmounted,
  346. reactive,
  347. ref,
  348. watch,
  349. } from 'vue';
  350. import { useRoute } from 'vue-router';
  351. import * as workerTimers from 'worker-timers';
  352. import { QINIU_LIVE, mediaTypeEnumMap } from '@/constant';
  353. import { commentAuthTip, loginTip } from '@/hooks/use-login';
  354. import { usePush } from '@/hooks/use-push';
  355. import { useRTCParams } from '@/hooks/use-rtcParams';
  356. import { useUpload } from '@/hooks/use-upload';
  357. import { DanmuMsgTypeEnum, LiveRoomTypeEnum, MediaTypeEnum } from '@/interface';
  358. import { AppRootState, useAppStore } from '@/store/app';
  359. import { usePiniaCacheStore } from '@/store/cache';
  360. import { useNetworkStore } from '@/store/network';
  361. import { useUserStore } from '@/store/user';
  362. import {
  363. createVideo,
  364. formatDownTime,
  365. generateBase64,
  366. getRandomEnglishString,
  367. readFile,
  368. saveFile,
  369. } from '@/utils';
  370. import { NODE_ENV } from 'script/constant';
  371. import MediaModalCpt from './mediaModal/index.vue';
  372. import NoLiveTipModalCpt from './noLiveTipModal/index.vue';
  373. import OpenMicophoneTipCpt from './openMicophoneTip/index.vue';
  374. import SelectMediaModalCpt from './selectMediaModal/index.vue';
  375. const route = useRoute();
  376. const userStore = useUserStore();
  377. const appStore = useAppStore();
  378. const networkStore = useNetworkStore();
  379. const cacheStore = usePiniaCacheStore();
  380. const { maxBitrate, maxFramerate, resolutionRatio, allMediaTypeList } =
  381. useRTCParams();
  382. const {
  383. confirmRoomName,
  384. startLive,
  385. endLive,
  386. sendDanmu,
  387. keydownDanmu,
  388. sendBlob,
  389. msgIsFile,
  390. mySocketId,
  391. lastCoverImg,
  392. canvasVideoStream,
  393. roomLiving,
  394. currentResolutionRatio,
  395. currentMaxBitrate,
  396. currentMaxFramerate,
  397. danmuStr,
  398. roomName,
  399. damuList,
  400. liveUserList,
  401. } = usePush();
  402. const currentMediaType = ref(MediaTypeEnum.camera);
  403. const currentMediaData = ref<AppRootState['allTrack'][0]>();
  404. const showOpenMicophoneTipCpt = ref(false);
  405. const showSelectMediaModalCpt = ref(false);
  406. const showMediaModalCpt = ref(false);
  407. const showNoLiveTipModalCpt = ref(false);
  408. const isEdit = ref(false);
  409. const topRef = ref<HTMLDivElement>();
  410. const bottomRef = ref<HTMLDivElement>();
  411. const danmuListRef = ref<HTMLDivElement>();
  412. const containerRef = ref<HTMLDivElement>();
  413. const pushCanvasRef = ref<HTMLCanvasElement>();
  414. const webaudioVideo = ref<HTMLVideoElement>();
  415. const fabricCanvas = ref<fabric.Canvas>();
  416. const startTime = ref(+new Date());
  417. // const startTime = ref(1692807352565); // 1693027352565
  418. const msgLoading = ref(false);
  419. const uploadRef = ref<HTMLInputElement>();
  420. const nullAudioStream = ref<MediaStream>();
  421. const timeCanvasDom = ref<Raw<fabric.Text>[]>([]);
  422. const stopwatchCanvasDom = ref<Raw<fabric.Text>[]>([]);
  423. const wrapSize = reactive({
  424. width: 0,
  425. height: 0,
  426. });
  427. const workerTimerId = ref(-1);
  428. const bodyAppendChildElArr = ref<HTMLElement[]>([]);
  429. const liveType = Number(route.query.liveType);
  430. const recorder = ref<MediaRecorder>();
  431. const sendBlobTimer = ref();
  432. const bolbId = ref(0);
  433. const msrDelay = ref(1000 * 2);
  434. watch(
  435. () => roomLiving.value,
  436. () => {
  437. if (!roomLiving.value) {
  438. handleEndLive();
  439. showNoLiveTipModalCpt.value = true;
  440. }
  441. }
  442. );
  443. watch(
  444. () => currentMaxBitrate.value,
  445. () => {
  446. if (liveType === LiveRoomTypeEnum.user_msr) {
  447. const stream = pushCanvasRef.value!.captureStream();
  448. const audioTrack = webaudioVideo
  449. // @ts-ignore
  450. .value!.captureStream()
  451. .getAudioTracks()[0];
  452. stream.addTrack(audioTrack);
  453. handleMsr(stream);
  454. }
  455. }
  456. );
  457. watch(
  458. () => currentMaxFramerate.value,
  459. () => {
  460. workerTimers.clearInterval(workerTimerId.value);
  461. renderFrame();
  462. }
  463. );
  464. watch(
  465. () => appStore.pkStream,
  466. (newval) => {
  467. console.log('pkStream', newval);
  468. if (newval) {
  469. addMediaOk({
  470. id: getRandomEnglishString(8),
  471. audio: 2,
  472. video: 1,
  473. mediaName: 'pkStream',
  474. type: MediaTypeEnum.pk,
  475. track: newval.getVideoTracks()[0],
  476. trackid: newval.getVideoTracks()[0].id,
  477. stream: newval,
  478. streamid: newval.id,
  479. hidden: false,
  480. muted: false,
  481. scaleInfo: {},
  482. });
  483. }
  484. }
  485. );
  486. watch(
  487. () => damuList.value.length,
  488. () => {
  489. setTimeout(() => {
  490. handleScrollTop();
  491. }, 0);
  492. }
  493. );
  494. function handleScrollTop() {
  495. if (danmuListRef.value) {
  496. danmuListRef.value.scrollTop = danmuListRef.value.scrollHeight + 10000;
  497. }
  498. }
  499. function handleSendBlob(event: BlobEvent) {
  500. bolbId.value += 1;
  501. console.log(event.data);
  502. sendBlob({
  503. blob: event.data,
  504. blobId: `${bolbId.value}`,
  505. delay: msrDelay.value,
  506. });
  507. }
  508. function mockClick() {
  509. if (!loginTip()) {
  510. return;
  511. }
  512. if (!commentAuthTip()) {
  513. return;
  514. }
  515. uploadRef.value?.click();
  516. }
  517. function handleWait() {
  518. if (!loginTip()) {
  519. return;
  520. }
  521. if (!commentAuthTip()) {
  522. return;
  523. }
  524. window.$message.warning('敬请期待!');
  525. }
  526. async function uploadChange() {
  527. const fileList = uploadRef.value?.files;
  528. if (fileList?.length) {
  529. try {
  530. msgLoading.value = true;
  531. msgIsFile.value = true;
  532. const res = await useUpload({
  533. prefix: QINIU_LIVE.prefix['billd-live/msg-image/'],
  534. file: fileList[0],
  535. });
  536. if (res?.resultUrl) {
  537. danmuStr.value = res.resultUrl || '错误图片';
  538. sendDanmu();
  539. }
  540. } catch (error) {
  541. console.log(error);
  542. } finally {
  543. msgIsFile.value = false;
  544. msgLoading.value = false;
  545. if (uploadRef.value) {
  546. uploadRef.value.value = '';
  547. }
  548. }
  549. }
  550. }
  551. function handleAllType() {
  552. const types = [
  553. 'video/webm',
  554. 'audio/webm',
  555. 'video/mpeg',
  556. 'video/webm;codecs=vp8',
  557. 'video/webm;codecs=vp9',
  558. 'video/webm;codecs=daala',
  559. 'video/webm;codecs=h264',
  560. 'audio/webm;codecs=opus',
  561. 'audio/webm;codecs=aac',
  562. 'audio/webm;codecs=h264,opus',
  563. 'video/webm;codecs=avc1.64001f,opus',
  564. 'video/webm;codecs=avc1.4d002a,opus',
  565. ];
  566. Object.keys(types).forEach((item) => {
  567. console.log(types[item], MediaRecorder.isTypeSupported(types[item]));
  568. });
  569. }
  570. function handleMsr(stream: MediaStream) {
  571. // https://developer.mozilla.org/en-US/docs/Web/Media/Formats/codecs_parameter
  572. const mimeType = 'video/webm;codecs=avc1.4d002a,opus';
  573. // const mimeType = 'video/webm;codecs=avc1.64001f,opus'; // b站的参数
  574. if (!MediaRecorder.isTypeSupported(mimeType)) {
  575. console.error('不支持', mimeType);
  576. return;
  577. } else {
  578. console.log('支持', mimeType);
  579. }
  580. /**
  581. * 小写的 "kb/s" 表示千比特每秒,而大写的 "KB/s" 表示千字节每秒
  582. * 例如,当我们说 100 kb/s 时,意思是每秒传输100千比特(比特)的数据。而当我们说 100 KB/s 时,意思是每秒传输100千字节(字节)的数据,相当于800千比特(比特)
  583. * 千字节(KB)、兆字节(MB)、千兆字节(GB)
  584. * 8 比特(bits)等于 1 字节(byte)
  585. * 1 Kbps(千比特每秒)等于 0.125 KB/s(千字节每秒)
  586. * 1 Mbps(兆比特每秒)等于 0.125 MB/s(兆字节每秒)
  587. * bit,比特
  588. * byte,字节
  589. * videoBitsPerSecond的单位是比特,假设videoBitsPerSecond值是1000*2000,即2000000
  590. * 2000000比特等于2000000 / 8 / 1000 = 250 KB/s
  591. */
  592. recorder.value = new MediaRecorder(stream, {
  593. mimeType,
  594. // bitsPerSecond: 1000 * currentMaxBitrate.value,
  595. videoBitsPerSecond: 1000 * currentMaxBitrate.value, // 单位是比特
  596. // audioBitsPerSecond: 1000 * 2000,
  597. });
  598. recorder.value.ondataavailable = handleSendBlob;
  599. sendBlobTimer.value = setInterval(function () {
  600. recorder.value?.stop();
  601. recorder.value?.start();
  602. }, msrDelay.value);
  603. }
  604. onMounted(() => {
  605. setTimeout(() => {
  606. scrollTo(0, 0);
  607. }, 100);
  608. initUserMedia();
  609. initCanvas();
  610. handleCache();
  611. });
  612. onUnmounted(() => {
  613. recorder.value?.stop();
  614. bodyAppendChildElArr.value.forEach((el) => {
  615. el.remove();
  616. });
  617. clearFrame();
  618. });
  619. async function initUserMedia() {
  620. const res1 = await handleUserMedia({ video: true, audio: false });
  621. console.log('初始化获取摄像头成功');
  622. const res2 = await handleUserMedia({ video: false, audio: true });
  623. console.log('初始化获取麦克风成功');
  624. if (!res1 || !res2) {
  625. showOpenMicophoneTipCpt.value = true;
  626. }
  627. }
  628. function renderAll() {
  629. timeCanvasDom.value.forEach((item) => {
  630. item.text = new Date().toLocaleString();
  631. });
  632. stopwatchCanvasDom.value.forEach((item) => {
  633. item.text = formatDownTime({
  634. endTime: +new Date(),
  635. startTime: startTime.value,
  636. showMs: true,
  637. });
  638. });
  639. fabricCanvas.value?.renderAll();
  640. }
  641. function clearFrame() {
  642. if (workerTimerId.value !== -1) {
  643. workerTimers.clearInterval(workerTimerId.value);
  644. }
  645. }
  646. function renderFrame() {
  647. // currentMaxFramerate等于20,实际fps是17.68
  648. const delay = 1000 / (currentMaxFramerate.value / (17.68 / 20)); // 60帧的话即16.666666666666668
  649. workerTimerId.value = workerTimers.setInterval(() => {
  650. renderAll();
  651. }, delay);
  652. }
  653. function handleNullAudio() {
  654. // 创建AudioContext对象
  655. const audioContext = new window.AudioContext();
  656. // 创建输入和输出节点
  657. const source = audioContext.createBufferSource();
  658. const destination = audioContext.createMediaStreamDestination();
  659. // 连接输入和输出节点
  660. source.connect(destination);
  661. // 播放空白音频
  662. source.start();
  663. // 获取音频流
  664. const stream = destination.stream;
  665. // 检查是否已经获取到音频流
  666. if (stream) {
  667. console.log('已创建空的直播音频流');
  668. const video = createVideo({
  669. appendChild: true,
  670. });
  671. video.srcObject = stream;
  672. nullAudioStream.value = stream;
  673. } else {
  674. console.error('无法创建空的直播音频流');
  675. }
  676. }
  677. function handleMixedAudio() {
  678. const allAudioTrack = appStore.allTrack.filter((item) => item.audio === 1);
  679. const nullAudio = nullAudioStream.value?.getAudioTracks()[0];
  680. if (nullAudio) {
  681. allAudioTrack.push({
  682. id: getRandomEnglishString(8),
  683. audio: 2,
  684. video: 1,
  685. mediaName: '占位空音频',
  686. type: MediaTypeEnum.webAudio,
  687. track: nullAudio,
  688. trackid: nullAudio.id,
  689. stream: nullAudioStream.value,
  690. streamid: nullAudioStream.value?.id,
  691. hidden: false,
  692. muted: false,
  693. scaleInfo: {},
  694. });
  695. }
  696. const audioCtx = new AudioContext();
  697. if (canvasVideoStream.value?.getAudioTracks()[0]) {
  698. canvasVideoStream.value.removeTrack(
  699. canvasVideoStream.value.getAudioTracks()[0]
  700. );
  701. }
  702. const res: { source: MediaStreamAudioSourceNode; gainNode: GainNode }[] = [];
  703. allAudioTrack.forEach((item) => {
  704. if (!audioCtx || !item.stream) return;
  705. const source = audioCtx.createMediaStreamSource(item.stream);
  706. const gainNode = audioCtx.createGain();
  707. gainNode.gain.value = (item.volume || 100) / 100;
  708. source.connect(gainNode);
  709. res.push({ source, gainNode });
  710. console.log('混流', item.stream?.id, item.stream);
  711. });
  712. const destination = audioCtx.createMediaStreamDestination();
  713. res.forEach((item) => {
  714. item.source.connect(item.gainNode);
  715. item.gainNode.connect(destination);
  716. });
  717. if (webaudioVideo.value) {
  718. webaudioVideo.value.remove();
  719. }
  720. webaudioVideo.value = createVideo({
  721. appendChild: true,
  722. });
  723. bodyAppendChildElArr.value.push(webaudioVideo.value);
  724. webaudioVideo.value.className = 'web-audio-video';
  725. webaudioVideo.value!.srcObject = destination.stream;
  726. const resAudio = destination.stream.getAudioTracks()[0];
  727. canvasVideoStream.value?.addTrack(resAudio);
  728. networkStore.rtcMap.forEach((rtc) => {
  729. const sender = rtc.peerConnection
  730. ?.getSenders()
  731. .find((sender) => sender.track?.id === resAudio.id);
  732. if (!sender) {
  733. rtc.peerConnection
  734. ?.getSenders()
  735. ?.find((sender) => sender.track?.kind === 'audio')
  736. ?.replaceTrack(resAudio);
  737. }
  738. });
  739. }
  740. function handleEndLive() {
  741. clearInterval(sendBlobTimer.value);
  742. recorder.value?.removeEventListener('dataavailable', handleSendBlob);
  743. endLive();
  744. }
  745. function handleStartLive() {
  746. if (!appStore.allTrack.length) {
  747. window.$message.warning('至少选择一个素材');
  748. return;
  749. }
  750. handleNullAudio();
  751. handleMixedAudio();
  752. lastCoverImg.value = generateBase64(pushCanvasRef.value!);
  753. startLive({
  754. type: liveType,
  755. msrDelay: msrDelay.value,
  756. });
  757. if (liveType === LiveRoomTypeEnum.user_msr) {
  758. const stream = pushCanvasRef.value!.captureStream();
  759. // @ts-ignore
  760. const audioTrack = webaudioVideo.value!.captureStream().getAudioTracks()[0];
  761. stream.addTrack(audioTrack);
  762. handleMsr(stream);
  763. }
  764. }
  765. function handleScale({ width, height }: { width: number; height: number }) {
  766. const resolutionHeight = currentResolutionRatio.value;
  767. const resolutionWidth = currentResolutionRatio.value * appStore.videoRatio;
  768. let ratio = 1;
  769. if (width > resolutionWidth) {
  770. const r1 = resolutionWidth / width;
  771. ratio = r1;
  772. }
  773. if (height > resolutionHeight) {
  774. const r1 = resolutionHeight / height;
  775. if (ratio > r1) {
  776. ratio = r1;
  777. }
  778. }
  779. return ratio;
  780. }
  781. function autoCreateVideo({
  782. stream,
  783. id,
  784. rect,
  785. muted,
  786. }: {
  787. stream: MediaStream;
  788. id: string;
  789. rect?: { left: number; top: number };
  790. muted?: boolean;
  791. }) {
  792. const videoEl = createVideo({ appendChild: true });
  793. bodyAppendChildElArr.value.push(videoEl);
  794. videoEl.setAttribute('videoid', id);
  795. if (muted !== undefined) {
  796. videoEl.muted = muted;
  797. }
  798. videoEl.srcObject = stream;
  799. return new Promise<{
  800. canvasDom: fabric.Image;
  801. videoEl: HTMLVideoElement;
  802. scale: number;
  803. }>((resolve) => {
  804. videoEl.onloadedmetadata = () => {
  805. const width = stream.getVideoTracks()[0].getSettings().width!;
  806. const height = stream.getVideoTracks()[0].getSettings().height!;
  807. const ratio = handleScale({ width, height });
  808. videoEl.width = width;
  809. videoEl.height = height;
  810. const canvasDom = markRaw(
  811. new fabric.Image(videoEl, {
  812. top: rect?.top || 0,
  813. left: rect?.left || 0,
  814. width,
  815. height,
  816. })
  817. );
  818. console.log(
  819. '初始化',
  820. ratio,
  821. canvasDom.width,
  822. canvasDom.height,
  823. width * ratio,
  824. height * ratio,
  825. canvasDom
  826. );
  827. handleMoving({ canvasDom, id });
  828. handleScaling({ canvasDom, id });
  829. canvasDom.scale(ratio / window.devicePixelRatio);
  830. fabricCanvas.value!.add(canvasDom);
  831. resolve({ canvasDom, scale: ratio, videoEl });
  832. };
  833. });
  834. }
  835. watch(
  836. () => currentResolutionRatio.value,
  837. (newHeight, oldHeight) => {
  838. changeCanvasAttr({ newHeight, oldHeight });
  839. }
  840. );
  841. // 容器宽高,1280*720,即720p
  842. // canvas容器宽高,2560*1440,即1440p
  843. // ======
  844. // 容器宽高,960*540,即540p
  845. // dom宽高,640*480
  846. // canvas容器宽高,960*540,即540p
  847. // 将dom绘制到容器里,此时dom的大小就是640*480
  848. // 需求,不管切换多少分辨率,我要看到的dom都是一样大小,即
  849. // 960*540时,dom是640*480
  850. // 1280*720时,dom不能是640*480了,因为这样他就会对比上一个分辨率的dom看起来小了,960/1280=0.75,540/720=0.75,
  851. // 其实就是分辨率变大了,我们就要将图片也变大,即图片的宽是640/0.75=853.4,高是480/0.75=640
  852. // 坐标变化,960*540时,dom坐标是100,100
  853. // 1280*720时,dom的坐标不能再是100,100了,否则对比上一个分辨率看起来偏
  854. function changeCanvasAttr({
  855. newHeight,
  856. oldHeight,
  857. }: {
  858. newHeight: number;
  859. oldHeight: number;
  860. }) {
  861. if (fabricCanvas.value) {
  862. const resolutionHeight =
  863. currentResolutionRatio.value / window.devicePixelRatio;
  864. const resolutionWidth =
  865. (currentResolutionRatio.value / window.devicePixelRatio) *
  866. appStore.videoRatio;
  867. fabricCanvas.value.setWidth(resolutionWidth);
  868. fabricCanvas.value.setHeight(resolutionHeight);
  869. appStore.allTrack.forEach((iten) => {
  870. const item = iten.canvasDom;
  871. if (item) {
  872. // 分辨率变小了,将图片变小
  873. if (newHeight < oldHeight) {
  874. const ratio2 = oldHeight / newHeight;
  875. item.left = item.left! / ratio2;
  876. item.top = item.top! / ratio2;
  877. } else {
  878. // 分辨率变大了,将图片变大
  879. const ratio2 = oldHeight / newHeight;
  880. item.left = item.left! / ratio2;
  881. item.top = item.top! / ratio2;
  882. }
  883. }
  884. });
  885. appStore.allTrack.forEach((iten) => {
  886. const item = iten.canvasDom;
  887. if (item) {
  888. // 分辨率变小了,将图片变小
  889. if (newHeight < oldHeight) {
  890. const ratio = newHeight / oldHeight;
  891. const ratio1 = (item.scaleX || 1) * ratio;
  892. item.scale(ratio1);
  893. } else {
  894. // 分辨率变大了,将图片变大
  895. const ratio = newHeight / oldHeight;
  896. const ratio1 = (item.scaleX || 1) * ratio;
  897. item.scale(ratio1);
  898. }
  899. }
  900. });
  901. changeCanvasStyle();
  902. }
  903. }
  904. function changeCanvasStyle() {
  905. // @ts-ignore
  906. fabricCanvas.value.wrapperEl.style.width = `${wrapSize.width}px`;
  907. // @ts-ignore
  908. fabricCanvas.value.wrapperEl.style.height = `${wrapSize.height}px`;
  909. // @ts-ignore
  910. fabricCanvas.value.lowerCanvasEl.style.width = `${wrapSize.width}px`;
  911. // @ts-ignore
  912. fabricCanvas.value.lowerCanvasEl.style.height = `${wrapSize.height}px`;
  913. // @ts-ignore
  914. fabricCanvas.value.upperCanvasEl.style.width = `${wrapSize.width}px`;
  915. // @ts-ignore
  916. fabricCanvas.value.upperCanvasEl.style.height = `${wrapSize.height}px`;
  917. }
  918. function initCanvas() {
  919. const resolutionHeight =
  920. currentResolutionRatio.value / window.devicePixelRatio;
  921. const resolutionWidth =
  922. (currentResolutionRatio.value / window.devicePixelRatio) *
  923. appStore.videoRatio;
  924. const wrapWidth = containerRef.value!.getBoundingClientRect().width;
  925. // const wrapWidth = 1920;
  926. const ratio = wrapWidth / resolutionWidth;
  927. const wrapHeight = resolutionHeight * ratio;
  928. // const wrapHeight = 1080;
  929. // lower-canvas: 实际的canvas画面,也就是pushCanvasRef
  930. // upper-canvas: 操作时候的canvas
  931. const ins = markRaw(new fabric.Canvas(pushCanvasRef.value!));
  932. ins.setWidth(resolutionWidth);
  933. ins.setHeight(resolutionHeight);
  934. ins.setBackgroundColor('black', () => {
  935. console.log('setBackgroundColor回调');
  936. });
  937. wrapSize.width = wrapWidth;
  938. wrapSize.height = wrapHeight;
  939. fabricCanvas.value = ins;
  940. renderFrame();
  941. changeCanvasStyle();
  942. }
  943. /**
  944. * 1: {scaleX: 1, scaleY: 1}
  945. * 2: {scaleX: 0.5, scaleY: 0.5}
  946. * 3: {scaleX: 0.3333333333333333, scaleY: 0.3333333333333333}
  947. * 4: {scaleX: 0.25, scaleY: 0.25}
  948. */
  949. /**
  950. * 二倍屏即1px里面有2个像素;三倍屏1px里面有3个像素,以此类推
  951. * 一个图片,宽高都是100px
  952. * 一倍屏展示:100px等于100个像素,一比一展示
  953. * 二倍屏展示:100px等于100个像素,二比一展示,即在二倍屏的100px看起来会比一倍屏的100px小一倍
  954. * 如果需要在一杯和二倍屏幕的时候看的大小都一样:
  955. * 1,在二倍屏的时候,需要将100px放大一倍,即200px;
  956. * 2,在一倍屏的时候,需要将100px缩小一百,即50px;
  957. */
  958. function handleScaling({ canvasDom, id }) {
  959. canvasDom.on('scaling', () => {
  960. appStore.allTrack.forEach((item) => {
  961. if (id === item.id) {
  962. item.scaleInfo[window.devicePixelRatio] = {
  963. scaleX: canvasDom.scaleX || 1,
  964. scaleY: canvasDom.scaleY || 1,
  965. };
  966. Object.keys(item.scaleInfo).forEach((iten) => {
  967. if (window.devicePixelRatio !== Number(iten)) {
  968. if (window.devicePixelRatio > Number(iten)) {
  969. item.scaleInfo[iten] = {
  970. scaleX:
  971. item.scaleInfo[window.devicePixelRatio].scaleX *
  972. window.devicePixelRatio,
  973. scaleY:
  974. item.scaleInfo[window.devicePixelRatio].scaleY *
  975. window.devicePixelRatio,
  976. };
  977. } else {
  978. if (window.devicePixelRatio === 1) {
  979. item.scaleInfo[iten] = {
  980. scaleX: item.scaleInfo[1].scaleX / Number(iten),
  981. scaleY: item.scaleInfo[1].scaleY / Number(iten),
  982. };
  983. } else {
  984. item.scaleInfo[iten] = {
  985. scaleX: item.scaleInfo[1].scaleX * Number(iten),
  986. scaleY: item.scaleInfo[1].scaleY * Number(iten),
  987. };
  988. }
  989. }
  990. }
  991. });
  992. }
  993. });
  994. cacheStore.setResourceList(appStore.allTrack);
  995. });
  996. }
  997. function handleMoving({
  998. canvasDom,
  999. id,
  1000. }: {
  1001. canvasDom: fabric.Image | fabric.Text;
  1002. id: string;
  1003. }) {
  1004. canvasDom.on('moving', () => {
  1005. console.log(
  1006. 'moving',
  1007. canvasDom.width,
  1008. canvasDom.height,
  1009. canvasDom.scaleX,
  1010. canvasDom.scaleY
  1011. );
  1012. appStore.allTrack.forEach((item) => {
  1013. if (id === item.id) {
  1014. item.rect = {
  1015. top: (canvasDom.top || 0) * window.devicePixelRatio,
  1016. left: (canvasDom.left || 0) * window.devicePixelRatio,
  1017. };
  1018. }
  1019. });
  1020. cacheStore.setResourceList(appStore.allTrack);
  1021. });
  1022. }
  1023. async function handleUserMedia({ video, audio }) {
  1024. try {
  1025. const event = await navigator.mediaDevices.getUserMedia({
  1026. video,
  1027. audio,
  1028. });
  1029. return event;
  1030. } catch (error) {
  1031. console.log(error);
  1032. }
  1033. }
  1034. async function handleDisplayMedia({ video, audio }) {
  1035. try {
  1036. const event = await navigator.mediaDevices.getDisplayMedia({
  1037. video,
  1038. audio,
  1039. });
  1040. return event;
  1041. } catch (error) {
  1042. console.log(error);
  1043. }
  1044. }
  1045. async function handleCache() {
  1046. const res: AppRootState['allTrack'] = [];
  1047. const err: string[] = [];
  1048. const queue: any[] = [];
  1049. cacheStore['resource-list'].forEach((item) => {
  1050. // @ts-ignore
  1051. const obj: AppRootState['allTrack'][0] = {};
  1052. obj.audio = item.audio;
  1053. obj.video = item.video;
  1054. obj.id = item.id;
  1055. obj.deviceId = item.deviceId;
  1056. obj.type = item.type;
  1057. obj.hidden = item.hidden;
  1058. obj.mediaName = item.mediaName;
  1059. obj.muted = item.muted;
  1060. obj.volume = item.volume;
  1061. obj.rect = item.rect;
  1062. obj.scaleInfo = item.scaleInfo;
  1063. obj.stopwatchInfo = item.stopwatchInfo;
  1064. async function handleMediaVideo() {
  1065. const { code, file } = await readFile(item.id);
  1066. if (code === 1 && file) {
  1067. const url = URL.createObjectURL(file);
  1068. const videoEl = createVideo({
  1069. muted: item.muted ? item.muted : false,
  1070. appendChild: true,
  1071. });
  1072. bodyAppendChildElArr.value.push(videoEl);
  1073. videoEl.setAttribute('videoid', item.id);
  1074. if (obj.volume !== undefined) {
  1075. videoEl.volume = obj.volume / 100;
  1076. }
  1077. videoEl.src = url;
  1078. await new Promise((resolve) => {
  1079. videoEl.onloadedmetadata = () => {
  1080. const stream = videoEl
  1081. // @ts-ignore
  1082. .captureStream();
  1083. const width = stream.getVideoTracks()[0].getSettings().width!;
  1084. const height = stream.getVideoTracks()[0].getSettings().height!;
  1085. videoEl.width = width;
  1086. videoEl.height = height;
  1087. const canvasDom = markRaw(
  1088. new fabric.Image(videoEl, {
  1089. top: (item.rect?.top || 0) / window.devicePixelRatio,
  1090. left: (item.rect?.left || 0) / window.devicePixelRatio,
  1091. width,
  1092. height,
  1093. })
  1094. );
  1095. handleMoving({ canvasDom, id: item.id });
  1096. handleScaling({ canvasDom, id: item.id });
  1097. canvasDom.scale(
  1098. item.scaleInfo[window.devicePixelRatio].scaleX || 1
  1099. );
  1100. fabricCanvas.value!.add(canvasDom);
  1101. obj.videoEl = videoEl;
  1102. obj.canvasDom = canvasDom;
  1103. resolve({ videoEl, canvasDom });
  1104. };
  1105. });
  1106. const stream = videoEl
  1107. // @ts-ignore
  1108. .captureStream() as MediaStream;
  1109. obj.stream = stream;
  1110. obj.streamid = stream.id;
  1111. obj.track = stream.getVideoTracks()[0];
  1112. obj.trackid = stream.getVideoTracks()[0].id;
  1113. } else {
  1114. console.error('读取文件失败');
  1115. }
  1116. }
  1117. async function handleImg() {
  1118. const { code, file } = await readFile(item.id);
  1119. if (code === 1 && file) {
  1120. const imgEl = await new Promise<HTMLImageElement>((resolve) => {
  1121. const reader = new FileReader();
  1122. reader.addEventListener(
  1123. 'load',
  1124. function () {
  1125. const img = document.createElement('img');
  1126. img.src = reader.result as string;
  1127. img.onload = () => {
  1128. resolve(img);
  1129. };
  1130. },
  1131. false
  1132. );
  1133. if (file) {
  1134. reader.readAsDataURL(file);
  1135. }
  1136. });
  1137. if (fabricCanvas.value) {
  1138. const canvasDom = markRaw(
  1139. new fabric.Image(imgEl, {
  1140. top: (item.rect?.top || 0) / window.devicePixelRatio,
  1141. left: (item.rect?.left || 0) / window.devicePixelRatio,
  1142. width: imgEl.width,
  1143. height: imgEl.height,
  1144. })
  1145. );
  1146. handleMoving({ canvasDom, id: obj.id });
  1147. handleScaling({ canvasDom, id: obj.id });
  1148. canvasDom.scale(item.scaleInfo[window.devicePixelRatio].scaleX || 1);
  1149. fabricCanvas.value.add(canvasDom);
  1150. obj.canvasDom = canvasDom;
  1151. }
  1152. } else {
  1153. console.error('读取文件失败');
  1154. }
  1155. }
  1156. async function handleScreen() {
  1157. try {
  1158. const event = await handleDisplayMedia({
  1159. video: true,
  1160. audio: true,
  1161. });
  1162. if (!event) return;
  1163. const videoEl = createVideo({ appendChild: true });
  1164. bodyAppendChildElArr.value.push(videoEl);
  1165. videoEl.setAttribute('videoid', obj.id);
  1166. videoEl.srcObject = event;
  1167. await new Promise((resolve) => {
  1168. videoEl.onloadedmetadata = () => {
  1169. const stream = videoEl
  1170. // @ts-ignore
  1171. .captureStream();
  1172. const width = stream.getVideoTracks()[0].getSettings().width!;
  1173. const height = stream.getVideoTracks()[0].getSettings().height!;
  1174. videoEl.width = width;
  1175. videoEl.height = height;
  1176. const canvasDom = markRaw(
  1177. new fabric.Image(videoEl, {
  1178. top: (item.rect?.top || 0) / window.devicePixelRatio,
  1179. left: (item.rect?.left || 0) / window.devicePixelRatio,
  1180. width,
  1181. height,
  1182. })
  1183. );
  1184. handleMoving({ canvasDom, id: item.id });
  1185. handleScaling({ canvasDom, id: item.id });
  1186. canvasDom.scale(
  1187. item.scaleInfo[window.devicePixelRatio].scaleX || 1
  1188. );
  1189. fabricCanvas.value!.add(canvasDom);
  1190. obj.videoEl = videoEl;
  1191. obj.canvasDom = canvasDom;
  1192. resolve({ videoEl, canvasDom });
  1193. };
  1194. });
  1195. } catch (error) {
  1196. console.error(error);
  1197. handleDel(obj);
  1198. err.push(obj.id);
  1199. }
  1200. }
  1201. async function handleMicrophone() {
  1202. const event = await handleUserMedia({
  1203. video: false,
  1204. audio: { deviceId: obj.deviceId },
  1205. });
  1206. if (!event) return;
  1207. const videoEl = createVideo({ appendChild: true, muted: false });
  1208. bodyAppendChildElArr.value.push(videoEl);
  1209. videoEl.setAttribute('videoid', obj.id);
  1210. videoEl.srcObject = event;
  1211. if (obj.volume !== undefined) {
  1212. videoEl.volume = obj.muted ? 0 : obj.volume / 100;
  1213. }
  1214. obj.videoEl = videoEl;
  1215. obj.stream = event;
  1216. obj.streamid = event.id;
  1217. obj.track = event.getAudioTracks()[0];
  1218. obj.trackid = event.getAudioTracks()[0].id;
  1219. }
  1220. async function handleCamera() {
  1221. const event = await navigator.mediaDevices.getUserMedia({
  1222. video: { deviceId: obj.deviceId },
  1223. audio: false,
  1224. });
  1225. const videoEl = createVideo({ appendChild: true });
  1226. bodyAppendChildElArr.value.push(videoEl);
  1227. videoEl.setAttribute('videoid', obj.id);
  1228. videoEl.srcObject = event;
  1229. await new Promise((resolve) => {
  1230. videoEl.onloadedmetadata = () => {
  1231. const stream = videoEl
  1232. // @ts-ignore
  1233. .captureStream();
  1234. const width = stream.getVideoTracks()[0].getSettings().width!;
  1235. const height = stream.getVideoTracks()[0].getSettings().height!;
  1236. videoEl.width = width;
  1237. videoEl.height = height;
  1238. const canvasDom = markRaw(
  1239. new fabric.Image(videoEl, {
  1240. top: (item.rect?.top || 0) / window.devicePixelRatio,
  1241. left: (item.rect?.left || 0) / window.devicePixelRatio,
  1242. width,
  1243. height,
  1244. })
  1245. );
  1246. handleMoving({ canvasDom, id: item.id });
  1247. handleScaling({ canvasDom, id: item.id });
  1248. canvasDom.scale(item.scaleInfo[window.devicePixelRatio].scaleX || 1);
  1249. fabricCanvas.value!.add(canvasDom);
  1250. obj.videoEl = videoEl;
  1251. obj.canvasDom = canvasDom;
  1252. resolve({ videoEl, canvasDom });
  1253. };
  1254. });
  1255. }
  1256. if (item.type === MediaTypeEnum.media && item.video === 1) {
  1257. queue.push(handleMediaVideo());
  1258. } else if (item.type === MediaTypeEnum.screen) {
  1259. queue.push(handleScreen());
  1260. } else if (item.type === MediaTypeEnum.camera) {
  1261. queue.push(handleCamera());
  1262. } else if (item.type === MediaTypeEnum.microphone) {
  1263. queue.push(handleMicrophone());
  1264. } else if (item.type === MediaTypeEnum.img) {
  1265. queue.push(handleImg());
  1266. } else if (item.type === MediaTypeEnum.txt) {
  1267. obj.txtInfo = item.txtInfo;
  1268. obj.scaleInfo = item.scaleInfo;
  1269. if (fabricCanvas.value) {
  1270. const canvasDom = markRaw(
  1271. new fabric.Text(item.txtInfo?.txt || '', {
  1272. top: (item.rect?.top || 0) / window.devicePixelRatio,
  1273. left: (item.rect?.left || 0) / window.devicePixelRatio,
  1274. fill: item.txtInfo?.color,
  1275. })
  1276. );
  1277. handleMoving({ canvasDom, id: obj.id });
  1278. handleScaling({ canvasDom, id: obj.id });
  1279. // fabric.Text类型不能除以分辨率
  1280. canvasDom.scale(item.scaleInfo[window.devicePixelRatio].scaleX);
  1281. fabricCanvas.value.add(canvasDom);
  1282. obj.canvasDom = canvasDom;
  1283. }
  1284. } else if (item.type === MediaTypeEnum.time) {
  1285. obj.timeInfo = item.timeInfo;
  1286. obj.scaleInfo = item.scaleInfo;
  1287. if (fabricCanvas.value) {
  1288. const canvasDom = markRaw(
  1289. new fabric.Text(new Date().toLocaleString(), {
  1290. top: (item.rect?.top || 0) / window.devicePixelRatio,
  1291. left: (item.rect?.left || 0) / window.devicePixelRatio,
  1292. fill: item.timeInfo?.color,
  1293. })
  1294. );
  1295. timeCanvasDom.value.push(canvasDom);
  1296. handleMoving({ canvasDom, id: obj.id });
  1297. handleScaling({ canvasDom, id: obj.id });
  1298. // fabric.Text类型不能除以分辨率
  1299. canvasDom.scale(item.scaleInfo[window.devicePixelRatio].scaleX);
  1300. fabricCanvas.value.add(canvasDom);
  1301. obj.canvasDom = canvasDom;
  1302. }
  1303. } else if (item.type === MediaTypeEnum.stopwatch) {
  1304. obj.stopwatchInfo = item.stopwatchInfo;
  1305. obj.scaleInfo = item.scaleInfo;
  1306. if (fabricCanvas.value) {
  1307. const canvasDom = markRaw(
  1308. new fabric.Text('00天00时00分00秒000毫秒', {
  1309. top: (item.rect?.top || 0) / window.devicePixelRatio,
  1310. left: (item.rect?.left || 0) / window.devicePixelRatio,
  1311. fill: item.stopwatchInfo?.color,
  1312. })
  1313. );
  1314. stopwatchCanvasDom.value.push(canvasDom);
  1315. handleMoving({ canvasDom, id: obj.id });
  1316. handleScaling({ canvasDom, id: obj.id });
  1317. // fabric.Text类型不能除以分辨率
  1318. canvasDom.scale(item.scaleInfo[window.devicePixelRatio].scaleX);
  1319. fabricCanvas.value.add(canvasDom);
  1320. obj.canvasDom = canvasDom;
  1321. }
  1322. }
  1323. res.push(obj);
  1324. });
  1325. await Promise.all(queue);
  1326. canvasVideoStream.value = pushCanvasRef.value!.captureStream();
  1327. appStore.setAllTrack(res.filter((v) => !err.includes(v.id)));
  1328. }
  1329. function handleShowMediaModalCpt(val: MediaTypeEnum) {
  1330. isEdit.value = false;
  1331. currentMediaData.value = undefined;
  1332. showMediaModalCpt.value = true;
  1333. showSelectMediaModalCpt.value = false;
  1334. currentMediaType.value = val;
  1335. }
  1336. function handleEdit(item: AppRootState['allTrack'][0]) {
  1337. currentMediaType.value = item.type;
  1338. currentMediaData.value = item;
  1339. isEdit.value = true;
  1340. showMediaModalCpt.value = true;
  1341. }
  1342. function setScaleInfo({ track, canvasDom, scale = 1 }) {
  1343. [1, 2, 3, 4].forEach((devicePixelRatio) => {
  1344. track.scaleInfo[devicePixelRatio] = {
  1345. scaleX: (1 / devicePixelRatio) * scale,
  1346. scaleY: (1 / devicePixelRatio) * scale,
  1347. };
  1348. });
  1349. if (window.devicePixelRatio !== 1) {
  1350. const ratio = (1 / window.devicePixelRatio) * scale;
  1351. canvasDom.scale(ratio);
  1352. track.scaleInfo[window.devicePixelRatio] = {
  1353. scaleX: ratio,
  1354. scaleY: ratio,
  1355. };
  1356. }
  1357. }
  1358. async function addMediaOk(val: AppRootState['allTrack'][0]) {
  1359. showMediaModalCpt.value = false;
  1360. if (val.type === MediaTypeEnum.screen) {
  1361. const event = await handleDisplayMedia({
  1362. video: {
  1363. deviceId: val.deviceId,
  1364. // displaySurface: 'monitor', // browser默认标签页;window默认窗口;monitor默认整个屏幕
  1365. },
  1366. audio: true,
  1367. });
  1368. if (!event) return;
  1369. const videoTrack: AppRootState['allTrack'][0] = {
  1370. id: getRandomEnglishString(8),
  1371. audio: 2,
  1372. video: 1,
  1373. mediaName: val.mediaName,
  1374. type: MediaTypeEnum.screen,
  1375. track: event.getVideoTracks()[0],
  1376. trackid: event.getVideoTracks()[0].id,
  1377. stream: event,
  1378. streamid: event.id,
  1379. hidden: false,
  1380. muted: false,
  1381. scaleInfo: {},
  1382. };
  1383. const { canvasDom, videoEl, scale } = await autoCreateVideo({
  1384. stream: event,
  1385. id: videoTrack.id,
  1386. });
  1387. setScaleInfo({ canvasDom, track: videoTrack, scale });
  1388. videoTrack.videoEl = videoEl;
  1389. // @ts-ignore
  1390. videoTrack.canvasDom = canvasDom;
  1391. const audio = event.getAudioTracks();
  1392. if (audio.length) {
  1393. videoTrack.audio = 1;
  1394. videoTrack.volume = appStore.normalVolume;
  1395. const audioTrack: AppRootState['allTrack'][0] = {
  1396. id: videoTrack.id,
  1397. audio: 1,
  1398. video: 2,
  1399. mediaName: val.mediaName,
  1400. type: MediaTypeEnum.screen,
  1401. track: event.getAudioTracks()[0],
  1402. trackid: event.getAudioTracks()[0].id,
  1403. stream: event,
  1404. streamid: event.id,
  1405. hidden: true,
  1406. muted: false,
  1407. volume: videoTrack.volume,
  1408. scaleInfo: {},
  1409. };
  1410. const res = [...appStore.allTrack, videoTrack, audioTrack];
  1411. appStore.setAllTrack(res);
  1412. cacheStore.setResourceList(res);
  1413. handleMixedAudio();
  1414. } else {
  1415. const res = [...appStore.allTrack, videoTrack];
  1416. appStore.setAllTrack(res);
  1417. cacheStore.setResourceList(res);
  1418. }
  1419. console.log('获取窗口成功');
  1420. } else if (val.type === MediaTypeEnum.camera) {
  1421. const event = await handleUserMedia({
  1422. video: {
  1423. deviceId: val.deviceId,
  1424. },
  1425. audio: false,
  1426. });
  1427. if (!event) return;
  1428. const videoTrack: AppRootState['allTrack'][0] = {
  1429. id: getRandomEnglishString(8),
  1430. deviceId: val.deviceId,
  1431. audio: 2,
  1432. video: 1,
  1433. mediaName: val.mediaName,
  1434. type: MediaTypeEnum.camera,
  1435. track: event.getVideoTracks()[0],
  1436. trackid: event.getVideoTracks()[0].id,
  1437. stream: event,
  1438. streamid: event.id,
  1439. hidden: false,
  1440. muted: false,
  1441. scaleInfo: {},
  1442. };
  1443. const { canvasDom, videoEl, scale } = await autoCreateVideo({
  1444. stream: event,
  1445. id: videoTrack.id,
  1446. });
  1447. setScaleInfo({ canvasDom, track: videoTrack, scale });
  1448. videoTrack.videoEl = videoEl;
  1449. // @ts-ignore
  1450. videoTrack.canvasDom = canvasDom;
  1451. const res = [...appStore.allTrack, videoTrack];
  1452. appStore.setAllTrack(res);
  1453. cacheStore.setResourceList(res);
  1454. console.log('获取摄像头成功');
  1455. } else if (val.type === MediaTypeEnum.pk) {
  1456. const event = await handleUserMedia({
  1457. video: {
  1458. deviceId: val.deviceId,
  1459. },
  1460. audio: false,
  1461. });
  1462. if (!event) return;
  1463. const videoTrack: AppRootState['allTrack'][0] = {
  1464. id: getRandomEnglishString(8),
  1465. deviceId: val.deviceId,
  1466. audio: 2,
  1467. video: 1,
  1468. mediaName: val.mediaName,
  1469. type: MediaTypeEnum.pk,
  1470. track: event.getVideoTracks()[0],
  1471. trackid: event.getVideoTracks()[0].id,
  1472. stream: event,
  1473. streamid: event.id,
  1474. hidden: false,
  1475. muted: false,
  1476. scaleInfo: {},
  1477. };
  1478. const { canvasDom, videoEl, scale } = await autoCreateVideo({
  1479. stream: event,
  1480. id: videoTrack.id,
  1481. });
  1482. setScaleInfo({ canvasDom, track: videoTrack, scale });
  1483. videoTrack.videoEl = videoEl;
  1484. // @ts-ignore
  1485. videoTrack.canvasDom = canvasDom;
  1486. const res = [...appStore.allTrack, videoTrack];
  1487. appStore.setAllTrack(res);
  1488. cacheStore.setResourceList(res);
  1489. console.log('获取pk成功');
  1490. } else if (val.type === MediaTypeEnum.microphone) {
  1491. const event = await handleUserMedia({
  1492. video: false,
  1493. audio: { deviceId: val.deviceId },
  1494. });
  1495. if (!event) return;
  1496. const microphoneVideoTrack: AppRootState['allTrack'][0] = {
  1497. id: getRandomEnglishString(8),
  1498. deviceId: val.deviceId,
  1499. audio: 1,
  1500. video: 2,
  1501. mediaName: val.mediaName,
  1502. type: MediaTypeEnum.microphone,
  1503. track: event.getAudioTracks()[0],
  1504. trackid: event.getAudioTracks()[0].id,
  1505. stream: event,
  1506. streamid: event.id,
  1507. hidden: false,
  1508. muted: false,
  1509. volume: 60,
  1510. scaleInfo: {},
  1511. };
  1512. const videoEl = createVideo({ appendChild: true, muted: false });
  1513. bodyAppendChildElArr.value.push(videoEl);
  1514. videoEl.setAttribute('videoid', microphoneVideoTrack.id);
  1515. videoEl.srcObject = event;
  1516. microphoneVideoTrack.videoEl = videoEl;
  1517. const res = [...appStore.allTrack, microphoneVideoTrack];
  1518. appStore.setAllTrack(res);
  1519. cacheStore.setResourceList(res);
  1520. handleMixedAudio();
  1521. console.log('获取麦克风成功');
  1522. } else if (val.type === MediaTypeEnum.txt) {
  1523. const txtTrack: AppRootState['allTrack'][0] = {
  1524. id: getRandomEnglishString(8),
  1525. audio: 2,
  1526. video: 1,
  1527. mediaName: val.mediaName,
  1528. type: MediaTypeEnum.txt,
  1529. track: undefined,
  1530. trackid: undefined,
  1531. stream: undefined,
  1532. streamid: undefined,
  1533. hidden: false,
  1534. muted: false,
  1535. scaleInfo: {},
  1536. };
  1537. if (fabricCanvas.value) {
  1538. const canvasDom = markRaw(
  1539. new fabric.Text(val.txtInfo?.txt || '', {
  1540. top: 0,
  1541. left: 0,
  1542. fill: val.txtInfo?.color,
  1543. })
  1544. );
  1545. handleMoving({ canvasDom, id: txtTrack.id });
  1546. handleScaling({ canvasDom, id: txtTrack.id });
  1547. txtTrack.txtInfo = val.txtInfo;
  1548. if (window.devicePixelRatio !== 1) {
  1549. const ratio = 1 / window.devicePixelRatio;
  1550. canvasDom.scale(ratio);
  1551. txtTrack.scaleInfo[window.devicePixelRatio] = {
  1552. scaleX: ratio,
  1553. scaleY: ratio,
  1554. };
  1555. } else {
  1556. txtTrack.scaleInfo[window.devicePixelRatio] = { scaleX: 1, scaleY: 1 };
  1557. }
  1558. txtTrack.canvasDom = canvasDom;
  1559. fabricCanvas.value.add(canvasDom);
  1560. }
  1561. const res = [...appStore.allTrack, txtTrack];
  1562. // @ts-ignore
  1563. appStore.setAllTrack(res);
  1564. // @ts-ignore
  1565. cacheStore.setResourceList(res);
  1566. console.log('获取文字成功', fabricCanvas.value);
  1567. } else if (val.type === MediaTypeEnum.time) {
  1568. const timeTrack: AppRootState['allTrack'][0] = {
  1569. id: getRandomEnglishString(8),
  1570. audio: 2,
  1571. video: 1,
  1572. mediaName: val.mediaName,
  1573. type: MediaTypeEnum.time,
  1574. track: undefined,
  1575. trackid: undefined,
  1576. stream: undefined,
  1577. streamid: undefined,
  1578. hidden: false,
  1579. muted: false,
  1580. scaleInfo: {},
  1581. };
  1582. if (fabricCanvas.value) {
  1583. const canvasDom = markRaw(
  1584. new fabric.Text(new Date().toLocaleString(), {
  1585. top: 0,
  1586. left: 0,
  1587. fill: val.timeInfo?.color,
  1588. })
  1589. );
  1590. setScaleInfo({ canvasDom, track: timeTrack });
  1591. timeCanvasDom.value.push(canvasDom);
  1592. handleMoving({ canvasDom, id: timeTrack.id });
  1593. handleScaling({ canvasDom, id: timeTrack.id });
  1594. timeTrack.timeInfo = val.timeInfo;
  1595. timeTrack.canvasDom = canvasDom;
  1596. fabricCanvas.value.add(canvasDom);
  1597. }
  1598. const res = [...appStore.allTrack, timeTrack];
  1599. // @ts-ignore
  1600. appStore.setAllTrack(res);
  1601. // @ts-ignore
  1602. cacheStore.setResourceList(res);
  1603. console.log('获取时间成功', fabricCanvas.value);
  1604. } else if (val.type === MediaTypeEnum.stopwatch) {
  1605. const stopwatchTrack: AppRootState['allTrack'][0] = {
  1606. id: getRandomEnglishString(8),
  1607. audio: 2,
  1608. video: 1,
  1609. mediaName: val.mediaName,
  1610. type: MediaTypeEnum.stopwatch,
  1611. track: undefined,
  1612. trackid: undefined,
  1613. stream: undefined,
  1614. streamid: undefined,
  1615. hidden: false,
  1616. muted: false,
  1617. scaleInfo: {},
  1618. };
  1619. if (fabricCanvas.value) {
  1620. const canvasDom = markRaw(
  1621. new fabric.Text('00天00时00分00秒000毫秒', {
  1622. top: 0,
  1623. left: 0,
  1624. fill: val.stopwatchInfo?.color,
  1625. // editable: true,
  1626. })
  1627. );
  1628. setScaleInfo({ canvasDom, track: stopwatchTrack });
  1629. stopwatchCanvasDom.value.push(canvasDom);
  1630. handleMoving({ canvasDom, id: stopwatchTrack.id });
  1631. handleScaling({ canvasDom, id: stopwatchTrack.id });
  1632. stopwatchTrack.stopwatchInfo = val.stopwatchInfo;
  1633. stopwatchTrack.canvasDom = canvasDom;
  1634. fabricCanvas.value.add(canvasDom);
  1635. }
  1636. const res = [...appStore.allTrack, stopwatchTrack];
  1637. // @ts-ignore
  1638. appStore.setAllTrack(res);
  1639. // @ts-ignore
  1640. cacheStore.setResourceList(res);
  1641. console.log('获取秒表成功', fabricCanvas.value);
  1642. } else if (val.type === MediaTypeEnum.img) {
  1643. const imgTrack: AppRootState['allTrack'][0] = {
  1644. id: getRandomEnglishString(8),
  1645. audio: 2,
  1646. video: 1,
  1647. mediaName: val.mediaName,
  1648. type: MediaTypeEnum.img,
  1649. track: undefined,
  1650. trackid: undefined,
  1651. stream: undefined,
  1652. streamid: undefined,
  1653. hidden: false,
  1654. muted: false,
  1655. scaleInfo: {},
  1656. };
  1657. if (fabricCanvas.value) {
  1658. if (!val.imgInfo) return;
  1659. const file = val.imgInfo[0].file!;
  1660. const { code } = await saveFile({ file, fileName: imgTrack.id });
  1661. if (code !== 1) return;
  1662. const imgEl = await new Promise<HTMLImageElement>((resolve) => {
  1663. const reader = new FileReader();
  1664. reader.addEventListener(
  1665. 'load',
  1666. function () {
  1667. const img = document.createElement('img');
  1668. img.src = reader.result as string;
  1669. img.onload = () => {
  1670. resolve(img);
  1671. };
  1672. },
  1673. false
  1674. );
  1675. if (file) {
  1676. reader.readAsDataURL(file);
  1677. }
  1678. });
  1679. const canvasDom = markRaw(
  1680. new fabric.Image(imgEl, {
  1681. top: 0,
  1682. left: 0,
  1683. width: imgEl.width,
  1684. height: imgEl.height,
  1685. })
  1686. );
  1687. const scale = handleScale({ width: imgEl.width, height: imgEl.height });
  1688. setScaleInfo({ canvasDom, track: imgTrack, scale });
  1689. handleMoving({ canvasDom, id: imgTrack.id });
  1690. handleScaling({ canvasDom, id: imgTrack.id });
  1691. imgTrack.canvasDom = canvasDom;
  1692. fabricCanvas.value.add(canvasDom);
  1693. }
  1694. const res = [...appStore.allTrack, imgTrack];
  1695. // @ts-ignore
  1696. appStore.setAllTrack(res);
  1697. // @ts-ignore
  1698. cacheStore.setResourceList(res);
  1699. console.log('获取图片成功', fabricCanvas.value);
  1700. } else if (val.type === MediaTypeEnum.media) {
  1701. const mediaVideoTrack: AppRootState['allTrack'][0] = {
  1702. id: getRandomEnglishString(8),
  1703. audio: 2,
  1704. video: 1,
  1705. mediaName: val.mediaName,
  1706. type: MediaTypeEnum.media,
  1707. track: undefined,
  1708. trackid: undefined,
  1709. stream: undefined,
  1710. streamid: undefined,
  1711. hidden: false,
  1712. muted: false,
  1713. scaleInfo: {},
  1714. };
  1715. if (fabricCanvas.value) {
  1716. if (!val.mediaInfo) return;
  1717. const file = val.mediaInfo[0].file!;
  1718. const { code } = await saveFile({ file, fileName: mediaVideoTrack.id });
  1719. if (code !== 1) return;
  1720. const url = URL.createObjectURL(file);
  1721. const videoEl = createVideo({ muted: false, appendChild: true });
  1722. bodyAppendChildElArr.value.push(videoEl);
  1723. videoEl.src = url;
  1724. videoEl.muted = false;
  1725. const videoRes = await new Promise<HTMLVideoElement>((resolve) => {
  1726. videoEl.onloadedmetadata = () => {
  1727. resolve(videoEl);
  1728. };
  1729. });
  1730. // @ts-ignore
  1731. const stream = videoRes.captureStream();
  1732. const { canvasDom, scale } = await autoCreateVideo({
  1733. stream,
  1734. id: mediaVideoTrack.id,
  1735. });
  1736. setScaleInfo({ canvasDom, track: mediaVideoTrack, scale });
  1737. mediaVideoTrack.videoEl = videoEl;
  1738. mediaVideoTrack.canvasDom = canvasDom;
  1739. if (stream.getAudioTracks()[0]) {
  1740. console.log('视频有音频', stream.getAudioTracks()[0]);
  1741. mediaVideoTrack.audio = 1;
  1742. mediaVideoTrack.volume = appStore.normalVolume;
  1743. const audioTrack: AppRootState['allTrack'][0] = {
  1744. id: mediaVideoTrack.id,
  1745. audio: 1,
  1746. video: 2,
  1747. mediaName: val.mediaName,
  1748. type: MediaTypeEnum.media,
  1749. track: stream.getAudioTracks()[0],
  1750. trackid: stream.getAudioTracks()[0].id,
  1751. stream,
  1752. streamid: stream.id,
  1753. hidden: true,
  1754. muted: false,
  1755. volume: mediaVideoTrack.volume,
  1756. scaleInfo: {},
  1757. };
  1758. const res = [...appStore.allTrack, audioTrack];
  1759. appStore.setAllTrack(res);
  1760. cacheStore.setResourceList(res);
  1761. handleMixedAudio();
  1762. }
  1763. }
  1764. const res = [...appStore.allTrack, mediaVideoTrack];
  1765. // @ts-ignore
  1766. appStore.setAllTrack(res);
  1767. // @ts-ignore
  1768. cacheStore.setResourceList(res);
  1769. console.log('获取视频成功', fabricCanvas.value);
  1770. }
  1771. }
  1772. function editMediaOk(val: AppRootState['allTrack'][0]) {
  1773. showMediaModalCpt.value = false;
  1774. const res = appStore.allTrack.map((item) => {
  1775. if (item.id === val.id) {
  1776. item.mediaName = val.mediaName;
  1777. item.timeInfo = val.timeInfo;
  1778. item.stopwatchInfo = val.stopwatchInfo;
  1779. item.txtInfo = val.txtInfo;
  1780. if (
  1781. [
  1782. MediaTypeEnum.txt,
  1783. MediaTypeEnum.time,
  1784. MediaTypeEnum.stopwatch,
  1785. ].includes(val.type)
  1786. ) {
  1787. if (item.canvasDom) {
  1788. // @ts-ignore
  1789. item.canvasDom.set(
  1790. 'fill',
  1791. val.txtInfo?.color ||
  1792. val.timeInfo?.color ||
  1793. val.stopwatchInfo?.color
  1794. );
  1795. }
  1796. }
  1797. if (val.type === MediaTypeEnum.txt) {
  1798. if (item.canvasDom) {
  1799. // @ts-ignore
  1800. item.canvasDom.set('text', val.txtInfo?.txt);
  1801. }
  1802. }
  1803. }
  1804. return item;
  1805. });
  1806. appStore.setAllTrack(res);
  1807. cacheStore.setResourceList(res);
  1808. }
  1809. function handleChangeMuted(item: AppRootState['allTrack'][0]) {
  1810. if (item.videoEl) {
  1811. const res = !item.videoEl.muted;
  1812. item.videoEl.muted = res;
  1813. item.videoEl.volume = res ? 0 : appStore.normalVolume / 100;
  1814. item.volume = res ? 0 : appStore.normalVolume;
  1815. item.muted = res;
  1816. cacheStore.setResourceList(appStore.allTrack);
  1817. handleMixedAudio();
  1818. }
  1819. }
  1820. function handleChangeVolume(item: AppRootState['allTrack'][0], v) {
  1821. const res = appStore.allTrack.map((iten) => {
  1822. if (iten.id === item.id) {
  1823. if (item.volume !== undefined) {
  1824. iten.volume = v;
  1825. iten.muted = v === 0;
  1826. if (iten.videoEl) {
  1827. iten.videoEl.volume = v / 100;
  1828. iten.videoEl.muted = v === 0;
  1829. }
  1830. }
  1831. }
  1832. return iten;
  1833. });
  1834. appStore.setAllTrack(res);
  1835. cacheStore.setResourceList(res);
  1836. handleMixedAudio();
  1837. }
  1838. function handleDel(item: AppRootState['allTrack'][0]) {
  1839. if (item.canvasDom !== undefined) {
  1840. fabricCanvas.value?.remove(item.canvasDom);
  1841. item.videoEl?.remove();
  1842. item.stream?.getTracks().forEach((track) => {
  1843. track.stop();
  1844. item.stream?.removeTrack(track);
  1845. });
  1846. }
  1847. bodyAppendChildElArr.value.forEach((el) => {
  1848. const videoid = el.getAttribute('videoid');
  1849. if (item.id === videoid) {
  1850. el.remove();
  1851. }
  1852. });
  1853. const res = appStore.allTrack.filter((iten) => iten.id !== item.id);
  1854. appStore.setAllTrack(res);
  1855. cacheStore.setResourceList(res);
  1856. handleMixedAudio();
  1857. }
  1858. function handleStartMedia(item: { type: MediaTypeEnum; txt: string }) {
  1859. currentMediaType.value = item.type;
  1860. showMediaModalCpt.value = true;
  1861. }
  1862. </script>
  1863. <style lang="scss" scoped>
  1864. .slider {
  1865. width: 80px;
  1866. }
  1867. .push-wrap {
  1868. display: flex;
  1869. justify-content: space-between;
  1870. margin: 15px auto 0;
  1871. width: $w-1250;
  1872. .left {
  1873. position: relative;
  1874. display: inline-block;
  1875. overflow: hidden;
  1876. box-sizing: border-box;
  1877. width: $w-960;
  1878. height: 100%;
  1879. border-radius: 6px;
  1880. background-color: white;
  1881. color: #9499a0;
  1882. vertical-align: top;
  1883. .container {
  1884. position: relative;
  1885. overflow: hidden;
  1886. height: 100%;
  1887. background-color: rgba($color: #000000, $alpha: 0.5);
  1888. line-height: 0;
  1889. .add-wrap {
  1890. position: absolute;
  1891. top: 50%;
  1892. left: 50%;
  1893. display: flex;
  1894. align-items: center;
  1895. justify-content: space-around;
  1896. padding: 10px 20px;
  1897. width: 50%;
  1898. border-radius: 6px;
  1899. background-color: white;
  1900. transform: translate(-50%, -50%);
  1901. }
  1902. }
  1903. .room-control {
  1904. display: flex;
  1905. justify-content: space-between;
  1906. padding: 20px;
  1907. background-color: papayawhip;
  1908. .info {
  1909. display: flex;
  1910. align-items: center;
  1911. .avatar {
  1912. margin-right: 20px;
  1913. width: 55px;
  1914. height: 55px;
  1915. border-radius: 50%;
  1916. background-position: center center;
  1917. background-size: cover;
  1918. background-repeat: no-repeat;
  1919. }
  1920. .detail {
  1921. display: flex;
  1922. flex-direction: column;
  1923. flex-shrink: 0;
  1924. width: 200px;
  1925. text-align: initial;
  1926. .top {
  1927. margin-bottom: 10px;
  1928. color: #18191c;
  1929. }
  1930. .bottom {
  1931. font-size: 14px;
  1932. }
  1933. }
  1934. }
  1935. .rtc {
  1936. display: flex;
  1937. align-items: center;
  1938. flex: 1;
  1939. font-size: 14px;
  1940. .item {
  1941. display: flex;
  1942. align-items: center;
  1943. flex: 1;
  1944. .txt {
  1945. flex-shrink: 0;
  1946. width: 80px;
  1947. }
  1948. .down {
  1949. width: 90px;
  1950. user-select: none;
  1951. }
  1952. }
  1953. }
  1954. .other {
  1955. display: flex;
  1956. flex-direction: column;
  1957. justify-content: center;
  1958. font-size: 12px;
  1959. .top {
  1960. }
  1961. .bottom {
  1962. margin-top: 10px;
  1963. }
  1964. }
  1965. }
  1966. }
  1967. .right {
  1968. display: flex;
  1969. flex-direction: column;
  1970. justify-content: space-between;
  1971. box-sizing: border-box;
  1972. margin-left: 10px;
  1973. width: $w-250;
  1974. border-radius: 6px;
  1975. background-color: white;
  1976. color: #9499a0;
  1977. .resource-card {
  1978. position: relative;
  1979. box-sizing: border-box;
  1980. margin-bottom: 10px;
  1981. padding: 10px;
  1982. width: 100%;
  1983. height: 290px;
  1984. border-radius: 6px;
  1985. background-color: papayawhip;
  1986. .title {
  1987. text-align: initial;
  1988. }
  1989. .list {
  1990. overflow: scroll;
  1991. height: 218px;
  1992. @extend %customScrollbar;
  1993. .item {
  1994. display: flex;
  1995. align-items: center;
  1996. justify-content: space-between;
  1997. margin: 5px 0;
  1998. font-size: 14px;
  1999. // &:hover {
  2000. // .control {
  2001. // display: flex;
  2002. // align-items: center;
  2003. // }
  2004. // }
  2005. .control {
  2006. display: flex;
  2007. align-items: center;
  2008. .control-item {
  2009. cursor: pointer;
  2010. &:not(:last-child) {
  2011. margin-right: 6px;
  2012. }
  2013. }
  2014. }
  2015. }
  2016. }
  2017. .bottom {
  2018. position: absolute;
  2019. bottom: 0;
  2020. left: 0;
  2021. padding: 10px;
  2022. }
  2023. }
  2024. .danmu-card {
  2025. position: relative;
  2026. flex: 1;
  2027. box-sizing: border-box;
  2028. padding: 10px 10px 0px;
  2029. width: 100%;
  2030. border-radius: 6px;
  2031. background-color: papayawhip;
  2032. text-align: initial;
  2033. .title {
  2034. margin-bottom: 10px;
  2035. }
  2036. .list {
  2037. overflow: scroll;
  2038. height: 266px;
  2039. @extend %customScrollbar;
  2040. .item {
  2041. box-sizing: border-box;
  2042. margin-bottom: 4px;
  2043. padding: 2px 0;
  2044. white-space: normal;
  2045. word-wrap: break-word;
  2046. font-size: 13px;
  2047. .name {
  2048. color: #9499a0;
  2049. cursor: pointer;
  2050. &.system {
  2051. color: red;
  2052. }
  2053. }
  2054. .msg {
  2055. margin-top: 4px;
  2056. color: #61666d;
  2057. &.img {
  2058. img {
  2059. width: 80%;
  2060. }
  2061. }
  2062. }
  2063. }
  2064. }
  2065. .send-msg {
  2066. position: relative;
  2067. box-sizing: border-box;
  2068. padding: 4px 0;
  2069. width: 100%;
  2070. .control {
  2071. display: flex;
  2072. margin: 4px 0;
  2073. .ico {
  2074. margin-right: 6px;
  2075. width: 24px;
  2076. height: 24px;
  2077. cursor: pointer;
  2078. .input-upload {
  2079. width: 0;
  2080. height: 0;
  2081. opacity: 0;
  2082. }
  2083. &.face {
  2084. @include setBackground('@/assets/img/msg-face.webp');
  2085. }
  2086. &.img {
  2087. @include setBackground('@/assets/img/msg-img.webp');
  2088. }
  2089. }
  2090. }
  2091. .ipt {
  2092. display: block;
  2093. box-sizing: border-box;
  2094. margin: 0 auto;
  2095. padding: 10px;
  2096. width: 100%;
  2097. height: 60px;
  2098. outline: none;
  2099. border: 1px solid hsla(0, 0%, 60%, 0.2);
  2100. border-radius: 4px;
  2101. background-color: #f1f2f3;
  2102. font-size: 14px;
  2103. }
  2104. .btn {
  2105. box-sizing: border-box;
  2106. margin-top: 10px;
  2107. margin-left: auto;
  2108. padding: 4px;
  2109. width: 70px;
  2110. border-radius: 4px;
  2111. background-color: $theme-color-gold;
  2112. color: white;
  2113. text-align: center;
  2114. font-size: 12px;
  2115. cursor: pointer;
  2116. }
  2117. }
  2118. // .send-msg {
  2119. // display: flex;
  2120. // align-items: center;
  2121. // box-sizing: border-box;
  2122. // width: calc(100% - 20px);
  2123. // .ipt {
  2124. // display: block;
  2125. // box-sizing: border-box;
  2126. // margin: 0 auto;
  2127. // margin-right: 10px;
  2128. // padding: 10px;
  2129. // width: 80%;
  2130. // height: 30px;
  2131. // outline: none;
  2132. // border: 1px solid hsla(0, 0%, 60%, 0.2);
  2133. // border-radius: 4px;
  2134. // background-color: #f1f2f3;
  2135. // font-size: 14px;
  2136. // }
  2137. // }
  2138. }
  2139. }
  2140. }
  2141. // 屏幕宽度大于1500的时候
  2142. @media screen and (min-width: $w-1500) {
  2143. .push-wrap {
  2144. width: $w-1475;
  2145. .left {
  2146. width: $w-1150;
  2147. }
  2148. .right {
  2149. width: $w-300;
  2150. }
  2151. }
  2152. }
  2153. </style>