index.vue 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831
  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="endLive"
  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. {{ item.userInfo?.username || item.socket_id }}:
  213. </span>
  214. <span class="msg">{{ item.msg }}</span>
  215. </template>
  216. <template v-else-if="item.msgType === DanmuMsgTypeEnum.otherJoin">
  217. <span class="name system">系统通知:</span>
  218. <span class="msg">
  219. <span>{{ item.userInfo?.username || item.socket_id }}</span>
  220. <span>进入直播!</span>
  221. </span>
  222. </template>
  223. <template
  224. v-else-if="item.msgType === DanmuMsgTypeEnum.userLeaved"
  225. >
  226. <span class="name system">系统通知:</span>
  227. <span class="msg">
  228. <span>{{ item.userInfo?.username || item.socket_id }}</span>
  229. <span>离开直播!</span>
  230. </span>
  231. </template>
  232. </div>
  233. </div>
  234. </div>
  235. <div class="send-msg">
  236. <input
  237. v-model="danmuStr"
  238. class="ipt"
  239. @keydown="keydownDanmu"
  240. />
  241. <n-button
  242. type="info"
  243. size="small"
  244. @click="sendDanmu"
  245. >
  246. 发送
  247. </n-button>
  248. </div>
  249. </div>
  250. </div>
  251. <SelectMediaModalCpt
  252. v-if="showSelectMediaModalCpt"
  253. :all-media-type-list="allMediaTypeList"
  254. @close="showSelectMediaModalCpt = false"
  255. @ok="handleShowMediaModalCpt"
  256. ></SelectMediaModalCpt>
  257. <MediaModalCpt
  258. v-if="showMediaModalCpt"
  259. :is-edit="isEdit"
  260. :media-type="currentMediaType"
  261. :init-data="currentMediaData"
  262. @close="showMediaModalCpt = false"
  263. @add-ok="addMediaOk"
  264. @edit-ok="editMediaOk"
  265. ></MediaModalCpt>
  266. <OpenMicophoneTipCpt
  267. v-if="showOpenMicophoneTipCpt"
  268. @close="showOpenMicophoneTipCpt = false"
  269. ></OpenMicophoneTipCpt>
  270. </div>
  271. </template>
  272. <script lang="ts" setup>
  273. import {
  274. Close,
  275. CreateOutline,
  276. VolumeHighOutline,
  277. VolumeMuteOutline,
  278. } from '@vicons/ionicons5';
  279. import { fabric } from 'fabric';
  280. import {
  281. Raw,
  282. markRaw,
  283. onMounted,
  284. onUnmounted,
  285. reactive,
  286. ref,
  287. watch,
  288. } from 'vue';
  289. import { useRoute } from 'vue-router';
  290. import * as workerTimers from 'worker-timers';
  291. import { mediaTypeEnumMap } from '@/constant';
  292. import { usePush } from '@/hooks/use-push';
  293. import { useRTCParams } from '@/hooks/use-rtcParams';
  294. import { DanmuMsgTypeEnum, MediaTypeEnum } from '@/interface';
  295. import { AppRootState, useAppStore } from '@/store/app';
  296. import { usePiniaCacheStore } from '@/store/cache';
  297. import { useNetworkStore } from '@/store/network';
  298. import { useUserStore } from '@/store/user';
  299. import {
  300. createVideo,
  301. formatDownTime,
  302. generateBase64,
  303. getRandomEnglishString,
  304. readFile,
  305. saveFile,
  306. } from '@/utils';
  307. import { NODE_ENV } from 'script/constant';
  308. import MediaModalCpt from './mediaModal/index.vue';
  309. import OpenMicophoneTipCpt from './openMicophoneTip/index.vue';
  310. import SelectMediaModalCpt from './selectMediaModal/index.vue';
  311. const route = useRoute();
  312. const userStore = useUserStore();
  313. const appStore = useAppStore();
  314. const networkStore = useNetworkStore();
  315. const cacheStore = usePiniaCacheStore();
  316. const { maxBitrate, maxFramerate, resolutionRatio, allMediaTypeList } =
  317. useRTCParams();
  318. const {
  319. confirmRoomName,
  320. startLive,
  321. endLive,
  322. sendDanmu,
  323. keydownDanmu,
  324. mySocketId,
  325. lastCoverImg,
  326. canvasVideoStream,
  327. roomLiving,
  328. currentResolutionRatio,
  329. currentMaxBitrate,
  330. currentMaxFramerate,
  331. danmuStr,
  332. roomName,
  333. damuList,
  334. liveUserList,
  335. } = usePush();
  336. const currentMediaType = ref(MediaTypeEnum.camera);
  337. const currentMediaData = ref<AppRootState['allTrack'][0]>();
  338. const showOpenMicophoneTipCpt = ref(false);
  339. const showSelectMediaModalCpt = ref(false);
  340. const showMediaModalCpt = ref(false);
  341. const isEdit = ref(false);
  342. const topRef = ref<HTMLDivElement>();
  343. const bottomRef = ref<HTMLDivElement>();
  344. const danmuListRef = ref<HTMLDivElement>();
  345. const containerRef = ref<HTMLDivElement>();
  346. const pushCanvasRef = ref<HTMLCanvasElement>();
  347. const webaudioVideo = ref<HTMLVideoElement>();
  348. const fabricCanvas = ref<fabric.Canvas>();
  349. const startTime = ref(+new Date());
  350. // const startTime = ref(1692807352565); // 1693027352565
  351. const timeCanvasDom = ref<Raw<fabric.Text>[]>([]);
  352. const stopwatchCanvasDom = ref<Raw<fabric.Text>[]>([]);
  353. const wrapSize = reactive({
  354. width: 0,
  355. height: 0,
  356. });
  357. const workerTimerId = ref(-1);
  358. const bodyAppendChildElArr = ref<HTMLElement[]>([]);
  359. const liveType = Number(route.query.liveType);
  360. watch(
  361. () => damuList.value.length,
  362. () => {
  363. setTimeout(() => {
  364. if (danmuListRef.value) {
  365. danmuListRef.value.scrollTop = danmuListRef.value.scrollHeight;
  366. }
  367. }, 0);
  368. }
  369. );
  370. onMounted(() => {
  371. setTimeout(() => {
  372. scrollTo(0, 0);
  373. }, 100);
  374. initUserMedia();
  375. initCanvas();
  376. handleCache();
  377. });
  378. onUnmounted(() => {
  379. bodyAppendChildElArr.value.forEach((el) => {
  380. el.remove();
  381. });
  382. clearFrame();
  383. });
  384. function initUserMedia() {
  385. navigator.mediaDevices
  386. .getUserMedia({
  387. video: true,
  388. audio: false,
  389. })
  390. .then(() => {
  391. console.log('初始化获取摄像头成功');
  392. })
  393. .catch(() => {
  394. console.log('初始化获取摄像头失败');
  395. })
  396. .finally(() => {
  397. navigator.mediaDevices
  398. .getUserMedia({
  399. video: false,
  400. audio: true,
  401. })
  402. .then(() => {
  403. console.log('初始化获取麦克风成功');
  404. })
  405. .catch(() => {
  406. console.log('初始化获取麦克风失败');
  407. showOpenMicophoneTipCpt.value = true;
  408. });
  409. });
  410. }
  411. function renderAll() {
  412. timeCanvasDom.value.forEach((item) => {
  413. item.text = new Date().toLocaleString();
  414. });
  415. stopwatchCanvasDom.value.forEach((item) => {
  416. item.text = formatDownTime({
  417. endTime: +new Date(),
  418. startTime: startTime.value,
  419. showMs: true,
  420. });
  421. });
  422. fabricCanvas.value?.renderAll();
  423. }
  424. function clearFrame() {
  425. if (workerTimerId.value !== -1) {
  426. workerTimers.clearInterval(workerTimerId.value);
  427. }
  428. }
  429. function renderFrame() {
  430. const delay = 1000 / 60; // 16.666666666666668
  431. workerTimerId.value = workerTimers.setInterval(() => {
  432. renderAll();
  433. }, delay);
  434. }
  435. function handleMixedAudio() {
  436. console.log('handleMixedAudio');
  437. const allAudioTrack = appStore.allTrack.filter((item) => item.audio === 1);
  438. const audioCtx = new AudioContext();
  439. if (canvasVideoStream.value?.getAudioTracks()[0]) {
  440. canvasVideoStream.value.removeTrack(
  441. canvasVideoStream.value.getAudioTracks()[0]
  442. );
  443. }
  444. const res: { source: MediaStreamAudioSourceNode; gainNode: GainNode }[] = [];
  445. allAudioTrack.forEach((item) => {
  446. if (!audioCtx || !item.stream) return;
  447. const source = audioCtx.createMediaStreamSource(item.stream);
  448. const gainNode = audioCtx.createGain();
  449. gainNode.gain.value = (item.volume || 100) / 100;
  450. source.connect(gainNode);
  451. res.push({ source, gainNode });
  452. console.log('混流', item.stream?.id, item.stream);
  453. });
  454. const destination = audioCtx.createMediaStreamDestination();
  455. res.forEach((item) => {
  456. item.source.connect(item.gainNode);
  457. item.gainNode.connect(destination);
  458. });
  459. if (webaudioVideo.value) {
  460. webaudioVideo.value.remove();
  461. }
  462. webaudioVideo.value = createVideo({ appendChild: true, show: false });
  463. bodyAppendChildElArr.value.push(webaudioVideo.value);
  464. webaudioVideo.value.className = 'web-audio-video';
  465. webaudioVideo.value!.srcObject = destination.stream;
  466. const resAudio = destination.stream.getAudioTracks()[0];
  467. canvasVideoStream.value?.addTrack(resAudio);
  468. networkStore.rtcMap.forEach((rtc) => {
  469. const sender = rtc.peerConnection
  470. ?.getSenders()
  471. .find((sender) => sender.track?.id === resAudio.id);
  472. if (!sender) {
  473. rtc.peerConnection
  474. ?.getSenders()
  475. ?.find((sender) => sender.track?.kind === 'audio')
  476. ?.replaceTrack(resAudio);
  477. }
  478. });
  479. console.log(canvasVideoStream.value?.getAudioTracks());
  480. }
  481. function handleStartLive() {
  482. handleMixedAudio();
  483. lastCoverImg.value = generateBase64(pushCanvasRef.value!);
  484. startLive({ type: liveType, receiver: mySocketId.value });
  485. }
  486. function handleScale({ width, height }: { width: number; height: number }) {
  487. const resolutionHeight = currentResolutionRatio.value;
  488. const resolutionWidth = currentResolutionRatio.value * appStore.videoRatio;
  489. let ratio = 1;
  490. if (width > resolutionWidth) {
  491. const r1 = resolutionWidth / width;
  492. ratio = r1;
  493. }
  494. if (height > resolutionHeight) {
  495. const r1 = resolutionHeight / height;
  496. if (ratio > r1) {
  497. ratio = r1;
  498. }
  499. }
  500. return ratio;
  501. }
  502. function autoCreateVideo({
  503. stream,
  504. id,
  505. rect,
  506. muted,
  507. }: {
  508. stream: MediaStream;
  509. id: string;
  510. rect?: { left: number; top: number };
  511. muted?: boolean;
  512. }) {
  513. console.warn('autoCreateVideo', id);
  514. const videoEl = createVideo({ appendChild: true });
  515. bodyAppendChildElArr.value.push(videoEl);
  516. videoEl.setAttribute('videoid', id);
  517. if (muted !== undefined) {
  518. videoEl.muted = muted;
  519. }
  520. videoEl.srcObject = stream;
  521. return new Promise<{
  522. canvasDom: fabric.Image;
  523. videoEl: HTMLVideoElement;
  524. scale: number;
  525. }>((resolve) => {
  526. videoEl.onloadedmetadata = () => {
  527. const width = stream.getVideoTracks()[0].getSettings().width!;
  528. const height = stream.getVideoTracks()[0].getSettings().height!;
  529. const ratio = handleScale({ width, height });
  530. videoEl.width = width;
  531. videoEl.height = height;
  532. const canvasDom = markRaw(
  533. new fabric.Image(videoEl, {
  534. top: rect?.top || 0,
  535. left: rect?.left || 0,
  536. width,
  537. height,
  538. })
  539. );
  540. console.log(
  541. '初始化',
  542. ratio,
  543. canvasDom.width,
  544. canvasDom.height,
  545. width * ratio,
  546. height * ratio,
  547. canvasDom
  548. );
  549. handleMoving({ canvasDom, id });
  550. handleScaling({ canvasDom, id });
  551. canvasDom.scale(ratio / window.devicePixelRatio);
  552. fabricCanvas.value!.add(canvasDom);
  553. resolve({ canvasDom, scale: ratio, videoEl });
  554. };
  555. });
  556. }
  557. watch(
  558. () => currentResolutionRatio.value,
  559. (newHeight, oldHeight) => {
  560. changeCanvasAttr({ newHeight, oldHeight });
  561. }
  562. );
  563. // 容器宽高,1280*720,即720p
  564. // canvas容器宽高,2560*1440,即1440p
  565. // ======
  566. // 容器宽高,960*540,即540p
  567. // dom宽高,640*480
  568. // canvas容器宽高,960*540,即540p
  569. // 将dom绘制到容器里,此时dom的大小就是640*480
  570. // 需求,不管切换多少分辨率,我要看到的dom都是一样大小,即
  571. // 960*540时,dom是640*480
  572. // 1280*720时,dom不能是640*480了,因为这样他就会对比上一个分辨率的dom看起来小了,960/1280=0.75,540/720=0.75,
  573. // 其实就是分辨率变大了,我们就要将图片也变大,即图片的宽是640/0.75=853.4,高是480/0.75=640
  574. // 坐标变化,960*540时,dom坐标是100,100
  575. // 1280*720时,dom的坐标不能再是100,100了,否则对比上一个分辨率看起来偏
  576. function changeCanvasAttr({
  577. newHeight,
  578. oldHeight,
  579. }: {
  580. newHeight: number;
  581. oldHeight: number;
  582. }) {
  583. if (fabricCanvas.value) {
  584. const resolutionHeight =
  585. currentResolutionRatio.value / window.devicePixelRatio;
  586. const resolutionWidth =
  587. (currentResolutionRatio.value / window.devicePixelRatio) *
  588. appStore.videoRatio;
  589. fabricCanvas.value.setWidth(resolutionWidth);
  590. fabricCanvas.value.setHeight(resolutionHeight);
  591. appStore.allTrack.forEach((iten) => {
  592. const item = iten.canvasDom;
  593. if (item) {
  594. // 分辨率变小了,将图片变小
  595. if (newHeight < oldHeight) {
  596. const ratio2 = oldHeight / newHeight;
  597. item.left = item.left! / ratio2;
  598. item.top = item.top! / ratio2;
  599. } else {
  600. // 分辨率变大了,将图片变大
  601. const ratio2 = oldHeight / newHeight;
  602. item.left = item.left! / ratio2;
  603. item.top = item.top! / ratio2;
  604. }
  605. }
  606. });
  607. appStore.allTrack.forEach((iten) => {
  608. const item = iten.canvasDom;
  609. if (item) {
  610. // 分辨率变小了,将图片变小
  611. if (newHeight < oldHeight) {
  612. const ratio = newHeight / oldHeight;
  613. const ratio1 = (item.scaleX || 1) * ratio;
  614. item.scale(ratio1);
  615. } else {
  616. // 分辨率变大了,将图片变大
  617. const ratio = newHeight / oldHeight;
  618. const ratio1 = (item.scaleX || 1) * ratio;
  619. item.scale(ratio1);
  620. }
  621. }
  622. });
  623. changeCanvasStyle();
  624. }
  625. }
  626. function changeCanvasStyle() {
  627. // @ts-ignore
  628. fabricCanvas.value.wrapperEl.style.width = `${wrapSize.width}px`;
  629. // @ts-ignore
  630. fabricCanvas.value.wrapperEl.style.height = `${wrapSize.height}px`;
  631. // @ts-ignore
  632. fabricCanvas.value.lowerCanvasEl.style.width = `${wrapSize.width}px`;
  633. // @ts-ignore
  634. fabricCanvas.value.lowerCanvasEl.style.height = `${wrapSize.height}px`;
  635. // @ts-ignore
  636. fabricCanvas.value.upperCanvasEl.style.width = `${wrapSize.width}px`;
  637. // @ts-ignore
  638. fabricCanvas.value.upperCanvasEl.style.height = `${wrapSize.height}px`;
  639. }
  640. function initCanvas() {
  641. const resolutionHeight =
  642. currentResolutionRatio.value / window.devicePixelRatio;
  643. const resolutionWidth =
  644. (currentResolutionRatio.value / window.devicePixelRatio) *
  645. appStore.videoRatio;
  646. const wrapWidth = containerRef.value!.getBoundingClientRect().width;
  647. // const wrapWidth = 1920;
  648. const ratio = wrapWidth / resolutionWidth;
  649. const wrapHeight = resolutionHeight * ratio;
  650. // const wrapHeight = 1080;
  651. // lower-canvas: 实际的canvas画面,也就是pushCanvasRef
  652. // upper-canvas: 操作时候的canvas
  653. const ins = markRaw(new fabric.Canvas(pushCanvasRef.value!));
  654. ins.setWidth(resolutionWidth);
  655. ins.setHeight(resolutionHeight);
  656. console.log('initCanvas', { resolutionWidth, resolutionHeight });
  657. ins.setBackgroundColor('black', () => {
  658. console.log('setBackgroundColor回调');
  659. });
  660. wrapSize.width = wrapWidth;
  661. wrapSize.height = wrapHeight;
  662. fabricCanvas.value = ins;
  663. renderFrame();
  664. changeCanvasStyle();
  665. }
  666. /**
  667. * 1: {scaleX: 1, scaleY: 1}
  668. * 2: {scaleX: 0.5, scaleY: 0.5}
  669. * 3: {scaleX: 0.3333333333333333, scaleY: 0.3333333333333333}
  670. * 4: {scaleX: 0.25, scaleY: 0.25}
  671. */
  672. /**
  673. * 二倍屏即1px里面有2个像素;三倍屏1px里面有3个像素,以此类推
  674. * 一个图片,宽高都是100px
  675. * 一倍屏展示:100px等于100个像素,一比一展示
  676. * 二倍屏展示:100px等于100个像素,二比一展示,即在二倍屏的100px看起来会比一倍屏的100px小一倍
  677. * 如果需要在一杯和二倍屏幕的时候看的大小都一样:
  678. * 1,在二倍屏的时候,需要将100px放大一倍,即200px;
  679. * 2,在一倍屏的时候,需要将100px缩小一百,即50px;
  680. */
  681. function handleScaling({ canvasDom, id }) {
  682. canvasDom.on('scaling', () => {
  683. appStore.allTrack.forEach((item) => {
  684. if (id === item.id) {
  685. item.scaleInfo[window.devicePixelRatio] = {
  686. scaleX: canvasDom.scaleX || 1,
  687. scaleY: canvasDom.scaleY || 1,
  688. };
  689. Object.keys(item.scaleInfo).forEach((iten) => {
  690. if (window.devicePixelRatio !== Number(iten)) {
  691. if (window.devicePixelRatio > Number(iten)) {
  692. item.scaleInfo[iten] = {
  693. scaleX:
  694. item.scaleInfo[window.devicePixelRatio].scaleX *
  695. window.devicePixelRatio,
  696. scaleY:
  697. item.scaleInfo[window.devicePixelRatio].scaleY *
  698. window.devicePixelRatio,
  699. };
  700. } else {
  701. if (window.devicePixelRatio === 1) {
  702. item.scaleInfo[iten] = {
  703. scaleX: item.scaleInfo[1].scaleX / Number(iten),
  704. scaleY: item.scaleInfo[1].scaleY / Number(iten),
  705. };
  706. } else {
  707. item.scaleInfo[iten] = {
  708. scaleX: item.scaleInfo[1].scaleX * Number(iten),
  709. scaleY: item.scaleInfo[1].scaleY * Number(iten),
  710. };
  711. }
  712. }
  713. }
  714. });
  715. }
  716. });
  717. cacheStore.setResourceList(appStore.allTrack);
  718. });
  719. }
  720. function handleMoving({
  721. canvasDom,
  722. id,
  723. }: {
  724. canvasDom: fabric.Image | fabric.Text;
  725. id: string;
  726. }) {
  727. canvasDom.on('moving', () => {
  728. console.log(
  729. 'moving',
  730. canvasDom.width,
  731. canvasDom.height,
  732. canvasDom.scaleX,
  733. canvasDom.scaleY
  734. );
  735. appStore.allTrack.forEach((item) => {
  736. if (id === item.id) {
  737. item.rect = {
  738. top: (canvasDom.top || 0) * window.devicePixelRatio,
  739. left: (canvasDom.left || 0) * window.devicePixelRatio,
  740. };
  741. }
  742. });
  743. cacheStore.setResourceList(appStore.allTrack);
  744. });
  745. }
  746. async function handleCache() {
  747. const res: AppRootState['allTrack'] = [];
  748. const err: string[] = [];
  749. const queue: any[] = [];
  750. cacheStore['resource-list'].forEach((item) => {
  751. // @ts-ignore
  752. const obj: AppRootState['allTrack'][0] = {};
  753. obj.audio = item.audio;
  754. obj.video = item.video;
  755. obj.id = item.id;
  756. obj.deviceId = item.deviceId;
  757. obj.type = item.type;
  758. obj.hidden = item.hidden;
  759. obj.mediaName = item.mediaName;
  760. obj.muted = item.muted;
  761. obj.volume = item.volume;
  762. obj.rect = item.rect;
  763. obj.scaleInfo = item.scaleInfo;
  764. obj.stopwatchInfo = item.stopwatchInfo;
  765. async function handleMediaVideo() {
  766. const { code, file } = await readFile(item.id);
  767. if (code === 1 && file) {
  768. const url = URL.createObjectURL(file);
  769. const videoEl = createVideo({
  770. muted: item.muted ? item.muted : false,
  771. appendChild: true,
  772. });
  773. bodyAppendChildElArr.value.push(videoEl);
  774. videoEl.setAttribute('videoid', item.id);
  775. if (obj.volume !== undefined) {
  776. videoEl.volume = obj.volume / 100;
  777. }
  778. videoEl.src = url;
  779. await new Promise((resolve) => {
  780. videoEl.onloadedmetadata = () => {
  781. const stream = videoEl
  782. // @ts-ignore
  783. .captureStream();
  784. const width = stream.getVideoTracks()[0].getSettings().width!;
  785. const height = stream.getVideoTracks()[0].getSettings().height!;
  786. videoEl.width = width;
  787. videoEl.height = height;
  788. const canvasDom = markRaw(
  789. new fabric.Image(videoEl, {
  790. top: (item.rect?.top || 0) / window.devicePixelRatio,
  791. left: (item.rect?.left || 0) / window.devicePixelRatio,
  792. width,
  793. height,
  794. })
  795. );
  796. handleMoving({ canvasDom, id: item.id });
  797. handleScaling({ canvasDom, id: item.id });
  798. canvasDom.scale(
  799. item.scaleInfo[window.devicePixelRatio].scaleX || 1
  800. );
  801. fabricCanvas.value!.add(canvasDom);
  802. obj.videoEl = videoEl;
  803. obj.canvasDom = canvasDom;
  804. resolve({ videoEl, canvasDom });
  805. };
  806. });
  807. const stream = videoEl
  808. // @ts-ignore
  809. .captureStream() as MediaStream;
  810. obj.stream = stream;
  811. obj.streamid = stream.id;
  812. obj.track = stream.getVideoTracks()[0];
  813. obj.trackid = stream.getVideoTracks()[0].id;
  814. } else {
  815. console.error('读取文件失败');
  816. }
  817. }
  818. async function handleImg() {
  819. const { code, file } = await readFile(item.id);
  820. if (code === 1 && file) {
  821. const imgEl = await new Promise<HTMLImageElement>((resolve) => {
  822. const reader = new FileReader();
  823. reader.addEventListener(
  824. 'load',
  825. function () {
  826. const img = document.createElement('img');
  827. img.src = reader.result as string;
  828. img.onload = () => {
  829. resolve(img);
  830. };
  831. },
  832. false
  833. );
  834. if (file) {
  835. reader.readAsDataURL(file);
  836. }
  837. });
  838. if (fabricCanvas.value) {
  839. const canvasDom = markRaw(
  840. new fabric.Image(imgEl, {
  841. top: (item.rect?.top || 0) / window.devicePixelRatio,
  842. left: (item.rect?.left || 0) / window.devicePixelRatio,
  843. width: imgEl.width,
  844. height: imgEl.height,
  845. })
  846. );
  847. handleMoving({ canvasDom, id: obj.id });
  848. handleScaling({ canvasDom, id: obj.id });
  849. canvasDom.scale(item.scaleInfo[window.devicePixelRatio].scaleX || 1);
  850. fabricCanvas.value.add(canvasDom);
  851. obj.canvasDom = canvasDom;
  852. }
  853. } else {
  854. console.error('读取文件失败');
  855. }
  856. }
  857. async function handleScreen() {
  858. try {
  859. const event = await navigator.mediaDevices.getDisplayMedia({
  860. video: true,
  861. audio: true,
  862. });
  863. const videoEl = createVideo({ appendChild: true });
  864. bodyAppendChildElArr.value.push(videoEl);
  865. videoEl.setAttribute('videoid', obj.id);
  866. videoEl.srcObject = event;
  867. await new Promise((resolve) => {
  868. videoEl.onloadedmetadata = () => {
  869. const stream = videoEl
  870. // @ts-ignore
  871. .captureStream();
  872. const width = stream.getVideoTracks()[0].getSettings().width!;
  873. const height = stream.getVideoTracks()[0].getSettings().height!;
  874. videoEl.width = width;
  875. videoEl.height = height;
  876. const canvasDom = markRaw(
  877. new fabric.Image(videoEl, {
  878. top: (item.rect?.top || 0) / window.devicePixelRatio,
  879. left: (item.rect?.left || 0) / window.devicePixelRatio,
  880. width,
  881. height,
  882. })
  883. );
  884. handleMoving({ canvasDom, id: item.id });
  885. handleScaling({ canvasDom, id: item.id });
  886. canvasDom.scale(
  887. item.scaleInfo[window.devicePixelRatio].scaleX || 1
  888. );
  889. fabricCanvas.value!.add(canvasDom);
  890. obj.videoEl = videoEl;
  891. obj.canvasDom = canvasDom;
  892. resolve({ videoEl, canvasDom });
  893. };
  894. });
  895. } catch (error) {
  896. console.error(error);
  897. handleDel(obj);
  898. err.push(obj.id);
  899. }
  900. }
  901. async function handleMicrophone() {
  902. const event = await navigator.mediaDevices.getUserMedia({
  903. video: false,
  904. audio: { deviceId: obj.deviceId },
  905. });
  906. const videoEl = createVideo({ appendChild: true, muted: false });
  907. bodyAppendChildElArr.value.push(videoEl);
  908. videoEl.setAttribute('videoid', obj.id);
  909. videoEl.srcObject = event;
  910. if (obj.volume !== undefined) {
  911. videoEl.volume = obj.muted ? 0 : obj.volume / 100;
  912. }
  913. obj.videoEl = videoEl;
  914. obj.stream = event;
  915. obj.streamid = event.id;
  916. obj.track = event.getAudioTracks()[0];
  917. obj.trackid = event.getAudioTracks()[0].id;
  918. }
  919. async function handleCamera() {
  920. const event = await navigator.mediaDevices.getUserMedia({
  921. video: { deviceId: obj.deviceId },
  922. audio: false,
  923. });
  924. const videoEl = createVideo({ appendChild: true });
  925. bodyAppendChildElArr.value.push(videoEl);
  926. videoEl.setAttribute('videoid', obj.id);
  927. videoEl.srcObject = event;
  928. await new Promise((resolve) => {
  929. videoEl.onloadedmetadata = () => {
  930. const stream = videoEl
  931. // @ts-ignore
  932. .captureStream();
  933. const width = stream.getVideoTracks()[0].getSettings().width!;
  934. const height = stream.getVideoTracks()[0].getSettings().height!;
  935. videoEl.width = width;
  936. videoEl.height = height;
  937. const canvasDom = markRaw(
  938. new fabric.Image(videoEl, {
  939. top: (item.rect?.top || 0) / window.devicePixelRatio,
  940. left: (item.rect?.left || 0) / window.devicePixelRatio,
  941. width,
  942. height,
  943. })
  944. );
  945. handleMoving({ canvasDom, id: item.id });
  946. handleScaling({ canvasDom, id: item.id });
  947. canvasDom.scale(item.scaleInfo[window.devicePixelRatio].scaleX || 1);
  948. fabricCanvas.value!.add(canvasDom);
  949. obj.videoEl = videoEl;
  950. obj.canvasDom = canvasDom;
  951. resolve({ videoEl, canvasDom });
  952. };
  953. });
  954. }
  955. if (item.type === MediaTypeEnum.media && item.video === 1) {
  956. queue.push(handleMediaVideo());
  957. } else if (item.type === MediaTypeEnum.screen) {
  958. queue.push(handleScreen());
  959. } else if (item.type === MediaTypeEnum.camera) {
  960. queue.push(handleCamera());
  961. } else if (item.type === MediaTypeEnum.microphone) {
  962. queue.push(handleMicrophone());
  963. } else if (item.type === MediaTypeEnum.img) {
  964. queue.push(handleImg());
  965. } else if (item.type === MediaTypeEnum.txt) {
  966. obj.txtInfo = item.txtInfo;
  967. obj.scaleInfo = item.scaleInfo;
  968. if (fabricCanvas.value) {
  969. const canvasDom = markRaw(
  970. new fabric.Text(item.txtInfo?.txt || '', {
  971. top: (item.rect?.top || 0) / window.devicePixelRatio,
  972. left: (item.rect?.left || 0) / window.devicePixelRatio,
  973. fill: item.txtInfo?.color,
  974. })
  975. );
  976. handleMoving({ canvasDom, id: obj.id });
  977. handleScaling({ canvasDom, id: obj.id });
  978. // fabric.Text类型不能除以分辨率
  979. canvasDom.scale(item.scaleInfo[window.devicePixelRatio].scaleX);
  980. fabricCanvas.value.add(canvasDom);
  981. obj.canvasDom = canvasDom;
  982. }
  983. } else if (item.type === MediaTypeEnum.time) {
  984. obj.timeInfo = item.timeInfo;
  985. obj.scaleInfo = item.scaleInfo;
  986. if (fabricCanvas.value) {
  987. const canvasDom = markRaw(
  988. new fabric.Text(new Date().toLocaleString(), {
  989. top: (item.rect?.top || 0) / window.devicePixelRatio,
  990. left: (item.rect?.left || 0) / window.devicePixelRatio,
  991. fill: item.timeInfo?.color,
  992. })
  993. );
  994. timeCanvasDom.value.push(canvasDom);
  995. handleMoving({ canvasDom, id: obj.id });
  996. handleScaling({ canvasDom, id: obj.id });
  997. // fabric.Text类型不能除以分辨率
  998. canvasDom.scale(item.scaleInfo[window.devicePixelRatio].scaleX);
  999. fabricCanvas.value.add(canvasDom);
  1000. obj.canvasDom = canvasDom;
  1001. }
  1002. } else if (item.type === MediaTypeEnum.stopwatch) {
  1003. obj.stopwatchInfo = item.stopwatchInfo;
  1004. obj.scaleInfo = item.scaleInfo;
  1005. if (fabricCanvas.value) {
  1006. const canvasDom = markRaw(
  1007. new fabric.Text('00天00时00分00秒000毫秒', {
  1008. top: (item.rect?.top || 0) / window.devicePixelRatio,
  1009. left: (item.rect?.left || 0) / window.devicePixelRatio,
  1010. fill: item.stopwatchInfo?.color,
  1011. })
  1012. );
  1013. stopwatchCanvasDom.value.push(canvasDom);
  1014. handleMoving({ canvasDom, id: obj.id });
  1015. handleScaling({ canvasDom, id: obj.id });
  1016. // fabric.Text类型不能除以分辨率
  1017. canvasDom.scale(item.scaleInfo[window.devicePixelRatio].scaleX);
  1018. fabricCanvas.value.add(canvasDom);
  1019. obj.canvasDom = canvasDom;
  1020. }
  1021. }
  1022. res.push(obj);
  1023. });
  1024. await Promise.all(queue);
  1025. canvasVideoStream.value = pushCanvasRef.value!.captureStream();
  1026. appStore.setAllTrack(res.filter((v) => !err.includes(v.id)));
  1027. }
  1028. function handleShowMediaModalCpt(val: MediaTypeEnum) {
  1029. isEdit.value = false;
  1030. currentMediaData.value = undefined;
  1031. showMediaModalCpt.value = true;
  1032. showSelectMediaModalCpt.value = false;
  1033. currentMediaType.value = val;
  1034. }
  1035. function handleEdit(item: AppRootState['allTrack'][0]) {
  1036. currentMediaType.value = item.type;
  1037. currentMediaData.value = item;
  1038. isEdit.value = true;
  1039. showMediaModalCpt.value = true;
  1040. }
  1041. function setScaleInfo({ track, canvasDom, scale = 1 }) {
  1042. [1, 2, 3, 4].forEach((devicePixelRatio) => {
  1043. track.scaleInfo[devicePixelRatio] = {
  1044. scaleX: (1 / devicePixelRatio) * scale,
  1045. scaleY: (1 / devicePixelRatio) * scale,
  1046. };
  1047. });
  1048. if (window.devicePixelRatio !== 1) {
  1049. const ratio = (1 / window.devicePixelRatio) * scale;
  1050. canvasDom.scale(ratio);
  1051. track.scaleInfo[window.devicePixelRatio] = {
  1052. scaleX: ratio,
  1053. scaleY: ratio,
  1054. };
  1055. }
  1056. }
  1057. async function addMediaOk(val: AppRootState['allTrack'][0]) {
  1058. showMediaModalCpt.value = false;
  1059. if (val.type === MediaTypeEnum.screen) {
  1060. const event = await navigator.mediaDevices.getDisplayMedia({
  1061. video: {
  1062. deviceId: val.deviceId,
  1063. // displaySurface: 'monitor', // browser默认标签页;window默认窗口;monitor默认整个屏幕
  1064. },
  1065. audio: true,
  1066. });
  1067. const videoTrack: AppRootState['allTrack'][0] = {
  1068. id: getRandomEnglishString(8),
  1069. audio: 2,
  1070. video: 1,
  1071. mediaName: val.mediaName,
  1072. type: MediaTypeEnum.screen,
  1073. track: event.getVideoTracks()[0],
  1074. trackid: event.getVideoTracks()[0].id,
  1075. stream: event,
  1076. streamid: event.id,
  1077. hidden: false,
  1078. muted: false,
  1079. scaleInfo: {},
  1080. };
  1081. const { canvasDom, videoEl, scale } = await autoCreateVideo({
  1082. stream: event,
  1083. id: videoTrack.id,
  1084. });
  1085. setScaleInfo({ canvasDom, track: videoTrack, scale });
  1086. videoTrack.videoEl = videoEl;
  1087. // @ts-ignore
  1088. videoTrack.canvasDom = canvasDom;
  1089. const audio = event.getAudioTracks();
  1090. if (audio.length) {
  1091. videoTrack.audio = 1;
  1092. videoTrack.volume = appStore.normalVolume;
  1093. const audioTrack: AppRootState['allTrack'][0] = {
  1094. id: videoTrack.id,
  1095. audio: 1,
  1096. video: 2,
  1097. mediaName: val.mediaName,
  1098. type: MediaTypeEnum.screen,
  1099. track: event.getAudioTracks()[0],
  1100. trackid: event.getAudioTracks()[0].id,
  1101. stream: event,
  1102. streamid: event.id,
  1103. hidden: true,
  1104. muted: false,
  1105. volume: videoTrack.volume,
  1106. scaleInfo: {},
  1107. };
  1108. const res = [...appStore.allTrack, videoTrack, audioTrack];
  1109. appStore.setAllTrack(res);
  1110. cacheStore.setResourceList(res);
  1111. handleMixedAudio();
  1112. } else {
  1113. const res = [...appStore.allTrack, videoTrack];
  1114. appStore.setAllTrack(res);
  1115. cacheStore.setResourceList(res);
  1116. // @ts-ignore
  1117. }
  1118. console.log('获取窗口成功');
  1119. } else if (val.type === MediaTypeEnum.camera) {
  1120. const event = await navigator.mediaDevices.getUserMedia({
  1121. video: {
  1122. deviceId: val.deviceId,
  1123. },
  1124. audio: false,
  1125. });
  1126. const videoTrack: AppRootState['allTrack'][0] = {
  1127. id: getRandomEnglishString(8),
  1128. deviceId: val.deviceId,
  1129. audio: 2,
  1130. video: 1,
  1131. mediaName: val.mediaName,
  1132. type: MediaTypeEnum.camera,
  1133. track: event.getVideoTracks()[0],
  1134. trackid: event.getVideoTracks()[0].id,
  1135. stream: event,
  1136. streamid: event.id,
  1137. hidden: false,
  1138. muted: false,
  1139. scaleInfo: {},
  1140. };
  1141. const { canvasDom, videoEl, scale } = await autoCreateVideo({
  1142. stream: event,
  1143. id: videoTrack.id,
  1144. });
  1145. setScaleInfo({ canvasDom, track: videoTrack, scale });
  1146. videoTrack.videoEl = videoEl;
  1147. // @ts-ignore
  1148. videoTrack.canvasDom = canvasDom;
  1149. const res = [...appStore.allTrack, videoTrack];
  1150. appStore.setAllTrack(res);
  1151. cacheStore.setResourceList(res);
  1152. // @ts-ignore
  1153. console.log('获取摄像头成功');
  1154. } else if (val.type === MediaTypeEnum.microphone) {
  1155. const event = await navigator.mediaDevices.getUserMedia({
  1156. video: false,
  1157. audio: { deviceId: val.deviceId },
  1158. });
  1159. const microphoneVideoTrack: AppRootState['allTrack'][0] = {
  1160. id: getRandomEnglishString(8),
  1161. deviceId: val.deviceId,
  1162. audio: 1,
  1163. video: 2,
  1164. mediaName: val.mediaName,
  1165. type: MediaTypeEnum.microphone,
  1166. track: event.getAudioTracks()[0],
  1167. trackid: event.getAudioTracks()[0].id,
  1168. stream: event,
  1169. streamid: event.id,
  1170. hidden: false,
  1171. muted: false,
  1172. volume: 60,
  1173. scaleInfo: {},
  1174. };
  1175. const videoEl = createVideo({ appendChild: true, muted: false });
  1176. bodyAppendChildElArr.value.push(videoEl);
  1177. videoEl.setAttribute('videoid', microphoneVideoTrack.id);
  1178. videoEl.srcObject = event;
  1179. microphoneVideoTrack.videoEl = videoEl;
  1180. const res = [...appStore.allTrack, microphoneVideoTrack];
  1181. appStore.setAllTrack(res);
  1182. cacheStore.setResourceList(res);
  1183. handleMixedAudio();
  1184. console.log('获取麦克风成功');
  1185. } else if (val.type === MediaTypeEnum.txt) {
  1186. const txtTrack: AppRootState['allTrack'][0] = {
  1187. id: getRandomEnglishString(8),
  1188. audio: 2,
  1189. video: 1,
  1190. mediaName: val.mediaName,
  1191. type: MediaTypeEnum.txt,
  1192. track: undefined,
  1193. trackid: undefined,
  1194. stream: undefined,
  1195. streamid: undefined,
  1196. hidden: false,
  1197. muted: false,
  1198. scaleInfo: {},
  1199. };
  1200. if (fabricCanvas.value) {
  1201. const canvasDom = markRaw(
  1202. new fabric.Text(val.txtInfo?.txt || '', {
  1203. top: 0,
  1204. left: 0,
  1205. fill: val.txtInfo?.color,
  1206. })
  1207. );
  1208. handleMoving({ canvasDom, id: txtTrack.id });
  1209. handleScaling({ canvasDom, id: txtTrack.id });
  1210. txtTrack.txtInfo = val.txtInfo;
  1211. if (window.devicePixelRatio !== 1) {
  1212. const ratio = 1 / window.devicePixelRatio;
  1213. canvasDom.scale(ratio);
  1214. txtTrack.scaleInfo[window.devicePixelRatio] = {
  1215. scaleX: ratio,
  1216. scaleY: ratio,
  1217. };
  1218. } else {
  1219. txtTrack.scaleInfo[window.devicePixelRatio] = { scaleX: 1, scaleY: 1 };
  1220. }
  1221. txtTrack.canvasDom = canvasDom;
  1222. fabricCanvas.value.add(canvasDom);
  1223. }
  1224. const res = [...appStore.allTrack, txtTrack];
  1225. // @ts-ignore
  1226. appStore.setAllTrack(res);
  1227. // @ts-ignore
  1228. cacheStore.setResourceList(res);
  1229. console.log('获取文字成功', fabricCanvas.value);
  1230. } else if (val.type === MediaTypeEnum.time) {
  1231. const timeTrack: AppRootState['allTrack'][0] = {
  1232. id: getRandomEnglishString(8),
  1233. audio: 2,
  1234. video: 1,
  1235. mediaName: val.mediaName,
  1236. type: MediaTypeEnum.time,
  1237. track: undefined,
  1238. trackid: undefined,
  1239. stream: undefined,
  1240. streamid: undefined,
  1241. hidden: false,
  1242. muted: false,
  1243. scaleInfo: {},
  1244. };
  1245. if (fabricCanvas.value) {
  1246. const canvasDom = markRaw(
  1247. new fabric.Text(new Date().toLocaleString(), {
  1248. top: 0,
  1249. left: 0,
  1250. fill: val.timeInfo?.color,
  1251. })
  1252. );
  1253. setScaleInfo({ canvasDom, track: timeTrack });
  1254. timeCanvasDom.value.push(canvasDom);
  1255. handleMoving({ canvasDom, id: timeTrack.id });
  1256. handleScaling({ canvasDom, id: timeTrack.id });
  1257. timeTrack.timeInfo = val.timeInfo;
  1258. timeTrack.canvasDom = canvasDom;
  1259. fabricCanvas.value.add(canvasDom);
  1260. }
  1261. const res = [...appStore.allTrack, timeTrack];
  1262. // @ts-ignore
  1263. appStore.setAllTrack(res);
  1264. // @ts-ignore
  1265. cacheStore.setResourceList(res);
  1266. console.log('获取时间成功', fabricCanvas.value);
  1267. } else if (val.type === MediaTypeEnum.stopwatch) {
  1268. const stopwatchTrack: AppRootState['allTrack'][0] = {
  1269. id: getRandomEnglishString(8),
  1270. audio: 2,
  1271. video: 1,
  1272. mediaName: val.mediaName,
  1273. type: MediaTypeEnum.stopwatch,
  1274. track: undefined,
  1275. trackid: undefined,
  1276. stream: undefined,
  1277. streamid: undefined,
  1278. hidden: false,
  1279. muted: false,
  1280. scaleInfo: {},
  1281. };
  1282. if (fabricCanvas.value) {
  1283. const canvasDom = markRaw(
  1284. new fabric.Text('00天00时00分00秒000毫秒', {
  1285. top: 0,
  1286. left: 0,
  1287. fill: val.stopwatchInfo?.color,
  1288. // editable: true,
  1289. })
  1290. );
  1291. setScaleInfo({ canvasDom, track: stopwatchTrack });
  1292. stopwatchCanvasDom.value.push(canvasDom);
  1293. handleMoving({ canvasDom, id: stopwatchTrack.id });
  1294. handleScaling({ canvasDom, id: stopwatchTrack.id });
  1295. stopwatchTrack.stopwatchInfo = val.stopwatchInfo;
  1296. stopwatchTrack.canvasDom = canvasDom;
  1297. fabricCanvas.value.add(canvasDom);
  1298. }
  1299. const res = [...appStore.allTrack, stopwatchTrack];
  1300. // @ts-ignore
  1301. appStore.setAllTrack(res);
  1302. // @ts-ignore
  1303. cacheStore.setResourceList(res);
  1304. console.log('获取秒表成功', fabricCanvas.value);
  1305. } else if (val.type === MediaTypeEnum.img) {
  1306. const imgTrack: AppRootState['allTrack'][0] = {
  1307. id: getRandomEnglishString(8),
  1308. audio: 2,
  1309. video: 1,
  1310. mediaName: val.mediaName,
  1311. type: MediaTypeEnum.img,
  1312. track: undefined,
  1313. trackid: undefined,
  1314. stream: undefined,
  1315. streamid: undefined,
  1316. hidden: false,
  1317. muted: false,
  1318. scaleInfo: {},
  1319. };
  1320. if (fabricCanvas.value) {
  1321. if (!val.imgInfo) return;
  1322. const file = val.imgInfo[0].file!;
  1323. const { code } = await saveFile({ file, fileName: imgTrack.id });
  1324. if (code !== 1) return;
  1325. const imgEl = await new Promise<HTMLImageElement>((resolve) => {
  1326. const reader = new FileReader();
  1327. reader.addEventListener(
  1328. 'load',
  1329. function () {
  1330. const img = document.createElement('img');
  1331. img.src = reader.result as string;
  1332. img.onload = () => {
  1333. resolve(img);
  1334. };
  1335. },
  1336. false
  1337. );
  1338. if (file) {
  1339. reader.readAsDataURL(file);
  1340. }
  1341. });
  1342. const canvasDom = markRaw(
  1343. new fabric.Image(imgEl, {
  1344. top: 0,
  1345. left: 0,
  1346. width: imgEl.width,
  1347. height: imgEl.height,
  1348. })
  1349. );
  1350. const scale = handleScale({ width: imgEl.width, height: imgEl.height });
  1351. setScaleInfo({ canvasDom, track: imgTrack, scale });
  1352. handleMoving({ canvasDom, id: imgTrack.id });
  1353. handleScaling({ canvasDom, id: imgTrack.id });
  1354. imgTrack.canvasDom = canvasDom;
  1355. fabricCanvas.value.add(canvasDom);
  1356. }
  1357. const res = [...appStore.allTrack, imgTrack];
  1358. // @ts-ignore
  1359. appStore.setAllTrack(res);
  1360. // @ts-ignore
  1361. cacheStore.setResourceList(res);
  1362. console.log('获取图片成功', fabricCanvas.value);
  1363. } else if (val.type === MediaTypeEnum.media) {
  1364. const mediaVideoTrack: AppRootState['allTrack'][0] = {
  1365. id: getRandomEnglishString(8),
  1366. audio: 2,
  1367. video: 1,
  1368. mediaName: val.mediaName,
  1369. type: MediaTypeEnum.media,
  1370. track: undefined,
  1371. trackid: undefined,
  1372. stream: undefined,
  1373. streamid: undefined,
  1374. hidden: false,
  1375. muted: false,
  1376. scaleInfo: {},
  1377. };
  1378. if (fabricCanvas.value) {
  1379. if (!val.mediaInfo) return;
  1380. const file = val.mediaInfo[0].file!;
  1381. const { code } = await saveFile({ file, fileName: mediaVideoTrack.id });
  1382. if (code !== 1) return;
  1383. const url = URL.createObjectURL(file);
  1384. const videoEl = createVideo({ muted: false, appendChild: true });
  1385. bodyAppendChildElArr.value.push(videoEl);
  1386. videoEl.src = url;
  1387. videoEl.muted = false;
  1388. const videoRes = await new Promise<HTMLVideoElement>((resolve) => {
  1389. videoEl.onloadedmetadata = () => {
  1390. resolve(videoEl);
  1391. };
  1392. });
  1393. // @ts-ignore
  1394. const stream = videoRes.captureStream();
  1395. const { canvasDom, scale } = await autoCreateVideo({
  1396. stream,
  1397. id: mediaVideoTrack.id,
  1398. });
  1399. setScaleInfo({ canvasDom, track: mediaVideoTrack, scale });
  1400. mediaVideoTrack.videoEl = videoEl;
  1401. mediaVideoTrack.canvasDom = canvasDom;
  1402. if (stream.getAudioTracks()[0]) {
  1403. console.log('视频有音频', stream.getAudioTracks()[0]);
  1404. mediaVideoTrack.audio = 1;
  1405. mediaVideoTrack.volume = appStore.normalVolume;
  1406. const audioTrack: AppRootState['allTrack'][0] = {
  1407. id: mediaVideoTrack.id,
  1408. audio: 1,
  1409. video: 2,
  1410. mediaName: val.mediaName,
  1411. type: MediaTypeEnum.media,
  1412. track: stream.getAudioTracks()[0],
  1413. trackid: stream.getAudioTracks()[0].id,
  1414. stream,
  1415. streamid: stream.id,
  1416. hidden: true,
  1417. muted: false,
  1418. volume: mediaVideoTrack.volume,
  1419. scaleInfo: {},
  1420. };
  1421. const res = [...appStore.allTrack, audioTrack];
  1422. appStore.setAllTrack(res);
  1423. cacheStore.setResourceList(res);
  1424. handleMixedAudio();
  1425. }
  1426. }
  1427. const res = [...appStore.allTrack, mediaVideoTrack];
  1428. // @ts-ignore
  1429. appStore.setAllTrack(res);
  1430. // @ts-ignore
  1431. cacheStore.setResourceList(res);
  1432. console.log('获取视频成功', fabricCanvas.value);
  1433. }
  1434. }
  1435. function editMediaOk(val: AppRootState['allTrack'][0]) {
  1436. showMediaModalCpt.value = false;
  1437. const res = appStore.allTrack.map((item) => {
  1438. if (item.id === val.id) {
  1439. item.mediaName = val.mediaName;
  1440. item.timeInfo = val.timeInfo;
  1441. item.stopwatchInfo = val.stopwatchInfo;
  1442. item.txtInfo = val.txtInfo;
  1443. if (
  1444. [
  1445. MediaTypeEnum.txt,
  1446. MediaTypeEnum.time,
  1447. MediaTypeEnum.stopwatch,
  1448. ].includes(val.type)
  1449. ) {
  1450. if (item.canvasDom) {
  1451. // @ts-ignore
  1452. item.canvasDom.set(
  1453. 'fill',
  1454. val.txtInfo?.color ||
  1455. val.timeInfo?.color ||
  1456. val.stopwatchInfo?.color
  1457. );
  1458. }
  1459. }
  1460. if (val.type === MediaTypeEnum.txt) {
  1461. if (item.canvasDom) {
  1462. // @ts-ignore
  1463. item.canvasDom.set('text', val.txtInfo?.txt);
  1464. }
  1465. }
  1466. }
  1467. return item;
  1468. });
  1469. appStore.setAllTrack(res);
  1470. cacheStore.setResourceList(res);
  1471. }
  1472. function handleChangeMuted(item: AppRootState['allTrack'][0]) {
  1473. if (item.videoEl) {
  1474. const res = !item.videoEl.muted;
  1475. item.videoEl.muted = res;
  1476. item.videoEl.volume = res ? 0 : appStore.normalVolume / 100;
  1477. item.volume = res ? 0 : appStore.normalVolume;
  1478. item.muted = res;
  1479. cacheStore.setResourceList(appStore.allTrack);
  1480. handleMixedAudio();
  1481. }
  1482. }
  1483. function handleChangeVolume(item: AppRootState['allTrack'][0], v) {
  1484. const res = appStore.allTrack.map((iten) => {
  1485. if (iten.id === item.id) {
  1486. if (item.volume !== undefined) {
  1487. iten.volume = v;
  1488. iten.muted = v === 0;
  1489. if (iten.videoEl) {
  1490. iten.videoEl.volume = v / 100;
  1491. iten.videoEl.muted = v === 0;
  1492. }
  1493. }
  1494. }
  1495. return iten;
  1496. });
  1497. appStore.setAllTrack(res);
  1498. cacheStore.setResourceList(res);
  1499. handleMixedAudio();
  1500. }
  1501. function handleDel(item: AppRootState['allTrack'][0]) {
  1502. if (item.canvasDom !== undefined) {
  1503. fabricCanvas.value?.remove(item.canvasDom);
  1504. item.videoEl?.remove();
  1505. item.stream?.getTracks().forEach((track) => {
  1506. track.stop();
  1507. item.stream?.removeTrack(track);
  1508. });
  1509. }
  1510. bodyAppendChildElArr.value.forEach((el) => {
  1511. const videoid = el.getAttribute('videoid');
  1512. if (item.id === videoid) {
  1513. el.remove();
  1514. }
  1515. });
  1516. const res = appStore.allTrack.filter((iten) => iten.id !== item.id);
  1517. appStore.setAllTrack(res);
  1518. cacheStore.setResourceList(res);
  1519. handleMixedAudio();
  1520. }
  1521. function handleStartMedia(item: { type: MediaTypeEnum; txt: string }) {
  1522. currentMediaType.value = item.type;
  1523. showMediaModalCpt.value = true;
  1524. }
  1525. </script>
  1526. <style lang="scss" scoped>
  1527. .slider {
  1528. width: 80px;
  1529. }
  1530. .push-wrap {
  1531. display: flex;
  1532. justify-content: space-between;
  1533. margin: 15px auto 0;
  1534. width: $w-1250;
  1535. .left {
  1536. position: relative;
  1537. display: inline-block;
  1538. overflow: hidden;
  1539. box-sizing: border-box;
  1540. width: $w-960;
  1541. height: 100%;
  1542. border-radius: 6px;
  1543. background-color: white;
  1544. color: #9499a0;
  1545. vertical-align: top;
  1546. .container {
  1547. position: relative;
  1548. overflow: hidden;
  1549. height: 100%;
  1550. background-color: rgba($color: #000000, $alpha: 0.5);
  1551. line-height: 0;
  1552. .add-wrap {
  1553. position: absolute;
  1554. top: 50%;
  1555. left: 50%;
  1556. display: flex;
  1557. align-items: center;
  1558. justify-content: space-around;
  1559. padding: 10px 20px;
  1560. width: 50%;
  1561. border-radius: 6px;
  1562. background-color: white;
  1563. transform: translate(-50%, -50%);
  1564. }
  1565. }
  1566. .room-control {
  1567. display: flex;
  1568. justify-content: space-between;
  1569. padding: 20px;
  1570. background-color: papayawhip;
  1571. .info {
  1572. display: flex;
  1573. align-items: center;
  1574. .avatar {
  1575. margin-right: 20px;
  1576. width: 55px;
  1577. height: 55px;
  1578. border-radius: 50%;
  1579. background-position: center center;
  1580. background-size: cover;
  1581. background-repeat: no-repeat;
  1582. }
  1583. .detail {
  1584. display: flex;
  1585. flex-direction: column;
  1586. flex-shrink: 0;
  1587. width: 200px;
  1588. text-align: initial;
  1589. .top {
  1590. margin-bottom: 10px;
  1591. color: #18191c;
  1592. }
  1593. .bottom {
  1594. font-size: 14px;
  1595. }
  1596. }
  1597. }
  1598. .rtc {
  1599. display: flex;
  1600. align-items: center;
  1601. flex: 1;
  1602. font-size: 14px;
  1603. .item {
  1604. display: flex;
  1605. align-items: center;
  1606. flex: 1;
  1607. .txt {
  1608. flex-shrink: 0;
  1609. width: 80px;
  1610. }
  1611. .down {
  1612. width: 90px;
  1613. user-select: none;
  1614. }
  1615. }
  1616. }
  1617. .other {
  1618. display: flex;
  1619. flex-direction: column;
  1620. justify-content: center;
  1621. font-size: 12px;
  1622. .top {
  1623. }
  1624. .bottom {
  1625. margin-top: 10px;
  1626. }
  1627. }
  1628. }
  1629. }
  1630. .right {
  1631. display: flex;
  1632. flex-direction: column;
  1633. justify-content: space-between;
  1634. box-sizing: border-box;
  1635. margin-left: 10px;
  1636. width: $w-250;
  1637. border-radius: 6px;
  1638. background-color: white;
  1639. color: #9499a0;
  1640. .resource-card {
  1641. position: relative;
  1642. box-sizing: border-box;
  1643. margin-bottom: 10px;
  1644. padding: 10px;
  1645. width: 100%;
  1646. height: 290px;
  1647. border-radius: 6px;
  1648. background-color: papayawhip;
  1649. .title {
  1650. text-align: initial;
  1651. }
  1652. .item {
  1653. display: flex;
  1654. align-items: center;
  1655. justify-content: space-between;
  1656. margin: 5px 0;
  1657. font-size: 14px;
  1658. // &:hover {
  1659. // .control {
  1660. // display: flex;
  1661. // align-items: center;
  1662. // }
  1663. // }
  1664. .control {
  1665. display: flex;
  1666. align-items: center;
  1667. .control-item {
  1668. cursor: pointer;
  1669. &:not(:last-child) {
  1670. margin-right: 6px;
  1671. }
  1672. }
  1673. }
  1674. }
  1675. .bottom {
  1676. position: absolute;
  1677. bottom: 0;
  1678. left: 0;
  1679. padding: 10px;
  1680. }
  1681. }
  1682. .danmu-card {
  1683. position: relative;
  1684. flex: 1;
  1685. box-sizing: border-box;
  1686. padding: 10px;
  1687. width: 100%;
  1688. border-radius: 6px;
  1689. background-color: papayawhip;
  1690. text-align: initial;
  1691. .title {
  1692. margin-bottom: 10px;
  1693. }
  1694. .list {
  1695. overflow: scroll;
  1696. height: 360px;
  1697. @extend %hideScrollbar;
  1698. .item {
  1699. margin-bottom: 10px;
  1700. font-size: 12px;
  1701. .name {
  1702. color: #9499a0;
  1703. }
  1704. .msg {
  1705. color: #61666d;
  1706. }
  1707. }
  1708. }
  1709. .send-msg {
  1710. position: absolute;
  1711. bottom: 10px;
  1712. left: 50%;
  1713. display: flex;
  1714. align-items: center;
  1715. box-sizing: border-box;
  1716. width: calc(100% - 20px);
  1717. transform: translateX(-50%);
  1718. .ipt {
  1719. display: block;
  1720. box-sizing: border-box;
  1721. margin: 0 auto;
  1722. margin-right: 10px;
  1723. padding: 10px;
  1724. width: 80%;
  1725. height: 30px;
  1726. outline: none;
  1727. border: 1px solid hsla(0, 0%, 60%, 0.2);
  1728. border-radius: 4px;
  1729. background-color: #f1f2f3;
  1730. font-size: 14px;
  1731. }
  1732. }
  1733. }
  1734. }
  1735. }
  1736. // 屏幕宽度大于1500的时候
  1737. @media screen and (min-width: $w-1500) {
  1738. .push-wrap {
  1739. width: $w-1475;
  1740. .left {
  1741. width: $w-1150;
  1742. }
  1743. .right {
  1744. width: $w-300;
  1745. }
  1746. }
  1747. }
  1748. </style>