use-pull.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. import { getRandomString } from 'billd-utils';
  2. import { nextTick, onUnmounted, ref, watch } from 'vue';
  3. import { useRoute } from 'vue-router';
  4. import { commentAuthTip, loginTip } from '@/hooks/use-login';
  5. import { useFlvPlay, useHlsPlay } from '@/hooks/use-play';
  6. import { useWebsocket } from '@/hooks/use-websocket';
  7. import {
  8. DanmuMsgTypeEnum,
  9. LiveLineEnum,
  10. LiveRenderEnum,
  11. WsMessageContentTypeEnum,
  12. WsMessageMsgIsFileEnum,
  13. } from '@/interface';
  14. import { useAppStore } from '@/store/app';
  15. import { usePiniaCacheStore } from '@/store/cache';
  16. import { useNetworkStore } from '@/store/network';
  17. import {
  18. ILiveRoom,
  19. LiveRoomTypeEnum,
  20. LiveRoomUseCDNEnum,
  21. } from '@/types/ILiveRoom';
  22. import { WsMessageType, WsMsgTypeEnum } from '@/types/websocket';
  23. import { videoFullBox, videoToCanvas } from '@/utils';
  24. export function usePull(roomId: string) {
  25. const route = useRoute();
  26. const networkStore = useNetworkStore();
  27. const cacheStore = usePiniaCacheStore();
  28. const appStore = useAppStore();
  29. const danmuStr = ref('');
  30. const msgIsFile = ref(WsMessageMsgIsFileEnum.no);
  31. const danmuMsgType = ref<DanmuMsgTypeEnum>(DanmuMsgTypeEnum.danmu);
  32. const autoplayVal = ref(false);
  33. const videoLoading = ref(false);
  34. const isPlaying = ref(false);
  35. const showPlayBtn = ref(false);
  36. const flvurl = ref('');
  37. const hlsurl = ref('');
  38. const videoWrapRef = ref<HTMLDivElement>();
  39. const videoResolution = ref();
  40. const isRemoteDesk = ref(false);
  41. const videoElArr = ref<HTMLVideoElement[]>([]);
  42. const remoteVideo = ref<HTMLElement[]>([]);
  43. const { mySocketId, initWs, roomLiving, anchorInfo, liveUserList, damuList } =
  44. useWebsocket();
  45. const { flvVideoEl, flvIsPlaying, startFlvPlay, destroyFlv } = useFlvPlay();
  46. const { hlsVideoEl, hlsIsPlaying, startHlsPlay, destroyHls } = useHlsPlay();
  47. const stopDrawingArr = ref<any[]>([]);
  48. let changeWrapSizeFn;
  49. onUnmounted(() => {
  50. handleStopDrawing();
  51. });
  52. function handleStopDrawing() {
  53. destroyFlv();
  54. destroyHls();
  55. changeWrapSizeFn = undefined;
  56. stopDrawingArr.value.forEach((cb) => cb());
  57. stopDrawingArr.value = [];
  58. remoteVideo.value.forEach((el) => el.remove());
  59. remoteVideo.value = [];
  60. if (isRemoteDesk.value && videoWrapRef.value) {
  61. videoWrapRef.value.removeAttribute('style');
  62. }
  63. }
  64. function handleVideoWrapResize() {
  65. nextTick(() => {
  66. if (videoWrapRef.value) {
  67. const rect = videoWrapRef.value.getBoundingClientRect();
  68. changeWrapSizeFn?.({ width: rect.width, height: rect.height });
  69. }
  70. });
  71. }
  72. function videoPlay(videoEl: HTMLVideoElement) {
  73. stopDrawingArr.value = [];
  74. stopDrawingArr.value.forEach((cb) => cb());
  75. if (appStore.videoControls.renderMode === LiveRenderEnum.canvas) {
  76. if (videoEl && videoWrapRef.value) {
  77. const rect = videoWrapRef.value.getBoundingClientRect();
  78. const { canvas, stopDrawing, changeWrapSize } = videoToCanvas({
  79. wrapSize: {
  80. width: rect.width,
  81. height: rect.height,
  82. },
  83. videoEl,
  84. videoResize: ({ w, h }) => {
  85. videoResolution.value = `${w}x${h}`;
  86. },
  87. });
  88. changeWrapSizeFn = changeWrapSize;
  89. stopDrawingArr.value.push(stopDrawing);
  90. remoteVideo.value.push(canvas);
  91. videoElArr.value.push(videoEl);
  92. videoLoading.value = false;
  93. }
  94. } else if (appStore.videoControls.renderMode === LiveRenderEnum.video) {
  95. if (videoEl && videoWrapRef.value) {
  96. const rect = videoWrapRef.value.getBoundingClientRect();
  97. const { changeWrapSize } = videoFullBox({
  98. wrapSize: {
  99. width: rect.width,
  100. height: rect.height,
  101. },
  102. videoEl,
  103. videoResize: ({ w, h }) => {
  104. videoResolution.value = `${w}x${h}`;
  105. },
  106. });
  107. changeWrapSizeFn = changeWrapSize;
  108. remoteVideo.value.push(videoEl);
  109. videoElArr.value.push(videoEl);
  110. videoLoading.value = false;
  111. }
  112. }
  113. }
  114. watch(hlsVideoEl, (newval) => {
  115. if (newval) {
  116. videoPlay(newval);
  117. }
  118. });
  119. watch(flvVideoEl, (newval) => {
  120. if (newval) {
  121. videoPlay(newval);
  122. }
  123. });
  124. watch(
  125. () => appStore.videoControlsValue.pageFullMode,
  126. () => {
  127. handleVideoWrapResize();
  128. }
  129. );
  130. watch(
  131. () => appStore.videoControls.renderMode,
  132. () => {
  133. if (appStore.liveRoomInfo) {
  134. handlePlay(appStore.liveRoomInfo);
  135. }
  136. }
  137. );
  138. watch(
  139. () => networkStore.rtcMap,
  140. (newVal) => {
  141. if (newVal.size) {
  142. roomLiving.value = true;
  143. videoLoading.value = false;
  144. appStore.playing = true;
  145. // cacheStore.muted = false;
  146. }
  147. if (
  148. isRemoteDesk.value ||
  149. appStore.liveRoomInfo?.type === LiveRoomTypeEnum.wertc_meeting_one ||
  150. appStore.liveRoomInfo?.type === LiveRoomTypeEnum.wertc_live ||
  151. appStore.liveRoomInfo?.type === LiveRoomTypeEnum.pk ||
  152. appStore.liveRoomInfo?.type === LiveRoomTypeEnum.tencent_css_pk
  153. ) {
  154. newVal.forEach((item) => {
  155. if (appStore.allTrack.find((v) => v.mediaName === item.receiver)) {
  156. return;
  157. }
  158. const rect = videoWrapRef.value?.getBoundingClientRect();
  159. if (rect) {
  160. videoFullBox({
  161. wrapSize: {
  162. width: rect.width,
  163. height: rect.height,
  164. },
  165. videoEl: item.videoEl,
  166. videoResize: ({ w, h }) => {
  167. videoResolution.value = `${w}x${h}`;
  168. },
  169. });
  170. remoteVideo.value.push(item.videoEl);
  171. videoElArr.value.push(item.videoEl);
  172. }
  173. });
  174. nextTick(() => {
  175. if (isRemoteDesk.value && videoWrapRef.value) {
  176. if (newVal.size) {
  177. videoWrapRef.value.style.display = 'inline-block';
  178. } else {
  179. videoWrapRef.value.style.removeProperty('display');
  180. }
  181. }
  182. });
  183. }
  184. },
  185. {
  186. deep: true,
  187. immediate: true,
  188. }
  189. );
  190. watch(
  191. () => remoteVideo.value,
  192. (newval) => {
  193. newval.forEach((videoEl) => {
  194. videoWrapRef.value?.appendChild(videoEl);
  195. });
  196. },
  197. {
  198. deep: true,
  199. immediate: true,
  200. }
  201. );
  202. function handleHlsPlay() {
  203. console.log('handleHlsPlay', hlsurl.value);
  204. handleStopDrawing();
  205. videoLoading.value = true;
  206. appStore.setLiveLine(LiveLineEnum.hls);
  207. startHlsPlay({
  208. hlsurl: hlsurl.value,
  209. });
  210. }
  211. function handleFlvPlay() {
  212. console.log('handleFlvPlay', flvurl.value);
  213. handleStopDrawing();
  214. videoLoading.value = true;
  215. appStore.setLiveLine(LiveLineEnum.flv);
  216. startFlvPlay({
  217. flvurl: flvurl.value,
  218. });
  219. }
  220. function handlePlay(data: ILiveRoom) {
  221. roomLiving.value = true;
  222. flvurl.value =
  223. data.cdn === LiveRoomUseCDNEnum.yes &&
  224. [LiveRoomTypeEnum.tencent_css, LiveRoomTypeEnum.tencent_css_pk].includes(
  225. data.type!
  226. )
  227. ? data.cdn_flv_url!
  228. : data.flv_url!;
  229. hlsurl.value =
  230. data.cdn === LiveRoomUseCDNEnum.yes &&
  231. [LiveRoomTypeEnum.tencent_css, LiveRoomTypeEnum.tencent_css_pk].includes(
  232. data.type!
  233. )
  234. ? data.cdn_hls_url!
  235. : data.hls_url!;
  236. function play() {
  237. if (appStore.liveLine === LiveLineEnum.flv) {
  238. handleFlvPlay();
  239. } else if (appStore.liveLine === LiveLineEnum.hls) {
  240. handleHlsPlay();
  241. }
  242. }
  243. if (LiveRoomTypeEnum.pk === data.type && !route.query.pkKey) {
  244. play();
  245. } else if (
  246. [
  247. LiveRoomTypeEnum.system,
  248. LiveRoomTypeEnum.srs,
  249. LiveRoomTypeEnum.obs,
  250. LiveRoomTypeEnum.msr,
  251. LiveRoomTypeEnum.pk,
  252. LiveRoomTypeEnum.forward_bilibili,
  253. LiveRoomTypeEnum.forward_huya,
  254. LiveRoomTypeEnum.forward_all,
  255. LiveRoomTypeEnum.tencent_css,
  256. LiveRoomTypeEnum.tencent_css_pk,
  257. ].includes(data.type!)
  258. ) {
  259. play();
  260. } else if (
  261. [
  262. LiveRoomTypeEnum.wertc_live,
  263. LiveRoomTypeEnum.wertc_meeting_one,
  264. LiveRoomTypeEnum.wertc_meeting_two,
  265. ].includes(data.type!)
  266. ) {
  267. appStore.setLiveLine(LiveLineEnum.rtc);
  268. }
  269. }
  270. watch(
  271. [() => roomLiving.value, () => appStore.liveRoomInfo],
  272. ([val, liveRoomInfo]) => {
  273. if (val && liveRoomInfo) {
  274. showPlayBtn.value = false;
  275. if (
  276. [
  277. LiveRoomTypeEnum.system,
  278. LiveRoomTypeEnum.msr,
  279. LiveRoomTypeEnum.srs,
  280. LiveRoomTypeEnum.obs,
  281. LiveRoomTypeEnum.tencent_css,
  282. LiveRoomTypeEnum.tencent_css_pk,
  283. LiveRoomTypeEnum.forward_bilibili,
  284. LiveRoomTypeEnum.forward_huya,
  285. LiveRoomTypeEnum.forward_all,
  286. ].includes(liveRoomInfo.type!)
  287. ) {
  288. handlePlay(liveRoomInfo!);
  289. } else if (LiveRoomTypeEnum.pk === liveRoomInfo.type!) {
  290. if (!route.query.pkKey) {
  291. handlePlay(liveRoomInfo!);
  292. }
  293. }
  294. }
  295. if (!roomLiving.value) {
  296. closeRtc();
  297. handleStopDrawing();
  298. }
  299. },
  300. {
  301. deep: true,
  302. immediate: true,
  303. }
  304. );
  305. watch(
  306. () => appStore.liveLine,
  307. (newVal) => {
  308. console.log('liveLine变了', newVal);
  309. if (!roomLiving.value) {
  310. return;
  311. }
  312. switch (newVal) {
  313. case LiveLineEnum.flv:
  314. handleFlvPlay();
  315. break;
  316. case LiveLineEnum.hls:
  317. handleHlsPlay();
  318. break;
  319. case LiveLineEnum.rtc:
  320. break;
  321. }
  322. }
  323. );
  324. watch(
  325. () => cacheStore.muted,
  326. (newVal) => {
  327. videoElArr.value.forEach((el) => {
  328. el.muted = newVal;
  329. });
  330. if (!newVal) {
  331. cacheStore.volume = cacheStore.volume || appStore.normalVolume;
  332. } else {
  333. cacheStore.volume = 0;
  334. }
  335. }
  336. );
  337. watch(
  338. () => cacheStore.volume,
  339. (newVal) => {
  340. videoElArr.value.forEach((el) => {
  341. el.volume = newVal / 100;
  342. });
  343. if (!newVal) {
  344. cacheStore.muted = true;
  345. } else {
  346. cacheStore.muted = false;
  347. }
  348. }
  349. );
  350. watch(
  351. () => appStore.playing,
  352. (newVal) => {
  353. videoElArr.value.forEach((el) => {
  354. if (newVal) {
  355. el.play();
  356. } else {
  357. el.pause();
  358. }
  359. });
  360. }
  361. );
  362. watch(
  363. () => hlsIsPlaying.value,
  364. (newVal) => {
  365. isPlaying.value = newVal;
  366. }
  367. );
  368. watch(
  369. () => flvIsPlaying.value,
  370. (newVal) => {
  371. isPlaying.value = newVal;
  372. }
  373. );
  374. watch(
  375. () => appStore.remoteDesk.isClose,
  376. (newval) => {
  377. if (newval) {
  378. handleStopDrawing();
  379. }
  380. }
  381. );
  382. function initPull(data: { autolay?: boolean; isRemoteDesk?: boolean }) {
  383. if (data.autolay === undefined) {
  384. autoplayVal.value = true;
  385. } else {
  386. autoplayVal.value = data.autolay;
  387. }
  388. if (autoplayVal.value) {
  389. videoLoading.value = true;
  390. }
  391. isRemoteDesk.value = !!data.isRemoteDesk;
  392. initWs({
  393. isRemoteDesk: data.isRemoteDesk,
  394. roomId,
  395. isAnchor: false,
  396. });
  397. }
  398. function closeWs() {
  399. networkStore.wsMap.forEach((ws) => {
  400. networkStore.removeWs(ws.roomId);
  401. });
  402. }
  403. function closeRtc() {
  404. networkStore.rtcMap.forEach((rtc) => {
  405. networkStore.removeRtc(rtc.receiver);
  406. });
  407. }
  408. function keydownDanmu(event: KeyboardEvent) {
  409. const key = event.key.toLowerCase();
  410. if (key === 'enter') {
  411. event.preventDefault();
  412. sendDanmuTxt(danmuStr.value);
  413. }
  414. }
  415. function sendDanmuTxt(txt: string) {
  416. if (!loginTip()) {
  417. return;
  418. }
  419. if (!commentAuthTip()) {
  420. return;
  421. }
  422. if (!txt.trim().length) {
  423. window.$message.warning('请输入弹幕内容!');
  424. return;
  425. }
  426. const instance = networkStore.wsMap.get(roomId);
  427. if (!instance) return;
  428. const messageData: WsMessageType['data'] = {
  429. content: txt,
  430. content_type: WsMessageContentTypeEnum.txt,
  431. msg_type: DanmuMsgTypeEnum.danmu,
  432. live_room_id: Number(roomId),
  433. isBilibili: false,
  434. };
  435. instance.send({
  436. requestId: getRandomString(8),
  437. msgType: WsMsgTypeEnum.message,
  438. data: messageData,
  439. });
  440. }
  441. function sendDanmuImg(url: string) {
  442. if (!loginTip()) {
  443. return;
  444. }
  445. if (!commentAuthTip()) {
  446. return;
  447. }
  448. if (!url.trim().length) {
  449. window.$message.warning('图片不能为空!');
  450. return;
  451. }
  452. const instance = networkStore.wsMap.get(roomId);
  453. if (!instance) return;
  454. const requestId = getRandomString(8);
  455. const messageData: WsMessageType['data'] = {
  456. content: url,
  457. content_type: WsMessageContentTypeEnum.img,
  458. msg_type: DanmuMsgTypeEnum.danmu,
  459. live_room_id: Number(roomId),
  460. isBilibili: false,
  461. };
  462. instance.send({
  463. requestId,
  464. msgType: WsMsgTypeEnum.message,
  465. data: messageData,
  466. });
  467. }
  468. return {
  469. initWs,
  470. videoWrapRef,
  471. handlePlay,
  472. handleStopDrawing,
  473. initPull,
  474. closeWs,
  475. closeRtc,
  476. keydownDanmu,
  477. sendDanmuTxt,
  478. sendDanmuImg,
  479. showPlayBtn,
  480. danmuMsgType,
  481. isPlaying,
  482. msgIsFile,
  483. mySocketId,
  484. videoResolution,
  485. remoteVideo,
  486. roomLiving,
  487. autoplayVal,
  488. videoLoading,
  489. damuList,
  490. liveUserList,
  491. danmuStr,
  492. anchorInfo,
  493. };
  494. }