index.vue 68 KB

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